2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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"
44 #include "openvswitch/match.h"
46 VLOG_DEFINE_THIS_MODULE(odp_util
);
48 /* The interface between userspace and kernel uses an "OVS_*" prefix.
49 * Since this is fairly non-specific for the OVS userspace components,
50 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
51 * interactions with the datapath.
54 /* The set of characters that may separate one action or one key attribute
56 static const char *delimiters
= ", \t\r\n";
57 static const char *delimiters_end
= ", \t\r\n)";
61 const struct attr_len_tbl
*next
;
64 #define ATTR_LEN_INVALID -1
65 #define ATTR_LEN_VARIABLE -2
66 #define ATTR_LEN_NESTED -3
68 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
69 struct ofpbuf
*, struct ofpbuf
*);
70 static void format_odp_key_attr(const struct nlattr
*a
,
71 const struct nlattr
*ma
,
72 const struct hmap
*portno_names
, struct ds
*ds
,
76 struct geneve_opt d
[63];
80 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
81 struct geneve_scan
*mask
);
82 static void format_geneve_opts(const struct geneve_opt
*opt
,
83 const struct geneve_opt
*mask
, int opts_len
,
84 struct ds
*, bool verbose
);
86 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
87 int max
, struct ofpbuf
*,
88 const struct nlattr
*key
);
89 static void format_u128(struct ds
*d
, const ovs_32aligned_u128
*key
,
90 const ovs_32aligned_u128
*mask
, bool verbose
);
91 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
93 static int parse_odp_action(const char *s
, const struct simap
*port_names
,
94 struct ofpbuf
*actions
);
96 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
99 * - For an action whose argument has a fixed length, returned that
100 * nonnegative length in bytes.
102 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
104 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
106 odp_action_len(uint16_t type
)
108 if (type
> OVS_ACTION_ATTR_MAX
) {
112 switch ((enum ovs_action_attr
) type
) {
113 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
114 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
115 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
116 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
117 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
118 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
119 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
120 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
121 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
122 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
123 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
124 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
125 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
126 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
127 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
128 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
129 case OVS_ACTION_ATTR_PUSH_ETH
: return sizeof(struct ovs_action_push_eth
);
130 case OVS_ACTION_ATTR_POP_ETH
: return 0;
131 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
133 case OVS_ACTION_ATTR_UNSPEC
:
134 case __OVS_ACTION_ATTR_MAX
:
135 return ATTR_LEN_INVALID
;
138 return ATTR_LEN_INVALID
;
141 /* Returns a string form of 'attr'. The return value is either a statically
142 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
143 * should be at least OVS_KEY_ATTR_BUFSIZE. */
144 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
146 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
149 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
150 case OVS_KEY_ATTR_ENCAP
: return "encap";
151 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
152 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
153 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
154 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
155 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
156 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
157 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: return "ct_tuple4";
158 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: return "ct_tuple6";
159 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
160 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
161 case OVS_KEY_ATTR_ETHERNET
: return "eth";
162 case OVS_KEY_ATTR_VLAN
: return "vlan";
163 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
164 case OVS_KEY_ATTR_IPV4
: return "ipv4";
165 case OVS_KEY_ATTR_IPV6
: return "ipv6";
166 case OVS_KEY_ATTR_TCP
: return "tcp";
167 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
168 case OVS_KEY_ATTR_UDP
: return "udp";
169 case OVS_KEY_ATTR_SCTP
: return "sctp";
170 case OVS_KEY_ATTR_ICMP
: return "icmp";
171 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
172 case OVS_KEY_ATTR_ARP
: return "arp";
173 case OVS_KEY_ATTR_ND
: return "nd";
174 case OVS_KEY_ATTR_MPLS
: return "mpls";
175 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
176 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
177 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
179 case __OVS_KEY_ATTR_MAX
:
181 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
187 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
189 size_t len
= nl_attr_get_size(a
);
191 ds_put_format(ds
, "action%d", nl_attr_type(a
));
193 const uint8_t *unspec
;
196 unspec
= nl_attr_get(a
);
197 for (i
= 0; i
< len
; i
++) {
198 ds_put_char(ds
, i
? ' ': '(');
199 ds_put_format(ds
, "%02x", unspec
[i
]);
201 ds_put_char(ds
, ')');
206 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
207 const struct hmap
*portno_names
)
209 static const struct nl_policy ovs_sample_policy
[] = {
210 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
211 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
213 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
215 const struct nlattr
*nla_acts
;
218 ds_put_cstr(ds
, "sample");
220 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
221 ds_put_cstr(ds
, "(error)");
225 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
228 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
230 ds_put_cstr(ds
, "actions(");
231 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
232 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
233 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
234 ds_put_format(ds
, "))");
238 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
239 const struct hmap
*portno_names
)
241 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
242 int len
= nl_attr_get_size(attr
);
244 ds_put_cstr(ds
, "clone");
245 ds_put_format(ds
, "(");
246 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
247 ds_put_format(ds
, ")");
251 slow_path_reason_to_string(uint32_t reason
)
253 switch ((enum slow_path_reason
) reason
) {
254 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
263 slow_path_reason_to_explanation(enum slow_path_reason reason
)
266 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
275 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
276 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
278 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
279 res_flags
, allowed
, res_mask
);
283 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
284 const struct hmap
*portno_names
)
286 static const struct nl_policy ovs_userspace_policy
[] = {
287 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
288 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
290 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
292 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
295 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
296 const struct nlattr
*userdata_attr
;
297 const struct nlattr
*tunnel_out_port_attr
;
299 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
300 ds_put_cstr(ds
, "userspace(error)");
304 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
305 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
307 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
310 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
311 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
312 bool userdata_unspec
= true;
313 union user_action_cookie cookie
;
315 if (userdata_len
>= sizeof cookie
.type
316 && userdata_len
<= sizeof cookie
) {
318 memset(&cookie
, 0, sizeof cookie
);
319 memcpy(&cookie
, userdata
, userdata_len
);
321 userdata_unspec
= false;
323 if (userdata_len
== sizeof cookie
.sflow
324 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
325 ds_put_format(ds
, ",sFlow("
326 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
327 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
328 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
329 cookie
.sflow
.output
);
330 } else if (userdata_len
== sizeof cookie
.slow_path
331 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
332 ds_put_cstr(ds
, ",slow_path(");
333 format_flags(ds
, slow_path_reason_to_string
,
334 cookie
.slow_path
.reason
, ',');
335 ds_put_format(ds
, ")");
336 } else if (userdata_len
== sizeof cookie
.flow_sample
337 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
338 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
339 ",collector_set_id=%"PRIu32
340 ",obs_domain_id=%"PRIu32
341 ",obs_point_id=%"PRIu32
343 cookie
.flow_sample
.probability
,
344 cookie
.flow_sample
.collector_set_id
,
345 cookie
.flow_sample
.obs_domain_id
,
346 cookie
.flow_sample
.obs_point_id
);
347 odp_portno_name_format(portno_names
,
348 cookie
.flow_sample
.output_odp_port
, ds
);
349 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
350 ds_put_cstr(ds
, ",ingress");
351 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
352 ds_put_cstr(ds
, ",egress");
354 ds_put_char(ds
, ')');
355 } else if (userdata_len
>= sizeof cookie
.ipfix
356 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
357 ds_put_format(ds
, ",ipfix(output_port=");
358 odp_portno_name_format(portno_names
,
359 cookie
.ipfix
.output_odp_port
, ds
);
360 ds_put_char(ds
, ')');
362 userdata_unspec
= true;
366 if (userdata_unspec
) {
368 ds_put_format(ds
, ",userdata(");
369 for (i
= 0; i
< userdata_len
; i
++) {
370 ds_put_format(ds
, "%02x", userdata
[i
]);
372 ds_put_char(ds
, ')');
376 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
377 ds_put_cstr(ds
, ",actions");
380 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
381 if (tunnel_out_port_attr
) {
382 ds_put_format(ds
, ",tunnel_out_port=");
383 odp_portno_name_format(portno_names
,
384 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
387 ds_put_char(ds
, ')');
391 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
393 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
394 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
395 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
396 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
398 ds_put_char(ds
, ',');
400 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
401 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
402 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
403 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
405 ds_put_char(ds
, ',');
407 if (!(tci
& htons(VLAN_CFI
))) {
408 ds_put_cstr(ds
, "cfi=0");
409 ds_put_char(ds
, ',');
415 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
417 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
418 mpls_lse_to_label(mpls_lse
),
419 mpls_lse_to_tc(mpls_lse
),
420 mpls_lse_to_ttl(mpls_lse
),
421 mpls_lse_to_bos(mpls_lse
));
425 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
426 const struct ovs_key_mpls
*mpls_mask
, int n
)
428 for (int i
= 0; i
< n
; i
++) {
429 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
431 if (mpls_mask
== NULL
) {
432 format_mpls_lse(ds
, key
);
434 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
436 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
437 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
438 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
439 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
440 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
442 ds_put_char(ds
, ',');
448 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
450 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
454 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
456 ds_put_format(ds
, "hash(");
458 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
459 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
461 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
464 ds_put_format(ds
, ")");
468 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
470 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
471 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
472 ntohs(udp
->udp_csum
));
478 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
480 const struct eth_header
*eth
;
483 const struct udp_header
*udp
;
485 eth
= (const struct eth_header
*)data
->header
;
490 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
491 data
->header_len
, data
->tnl_type
);
492 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
493 ds_put_format(ds
, ",src=");
494 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
495 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
497 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
499 const struct ip_header
*ip
= l3
;
500 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
501 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
502 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
503 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
504 ip
->ip_proto
, ip
->ip_tos
,
506 ntohs(ip
->ip_frag_off
));
509 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
510 struct in6_addr src
, dst
;
511 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
512 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
513 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
515 ds_put_format(ds
, "ipv6(src=");
516 ipv6_format_addr(&src
, ds
);
517 ds_put_format(ds
, ",dst=");
518 ipv6_format_addr(&dst
, ds
);
519 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
520 ",hlimit=%"PRIu8
"),",
521 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
522 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
526 udp
= (const struct udp_header
*) l4
;
528 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
529 const struct vxlanhdr
*vxh
;
531 vxh
= format_udp_tnl_push_header(ds
, udp
);
533 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
534 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
535 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
536 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
537 const struct genevehdr
*gnh
;
539 gnh
= format_udp_tnl_push_header(ds
, udp
);
541 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
542 gnh
->oam
? "oam," : "",
543 gnh
->critical
? "crit," : "",
544 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
547 ds_put_cstr(ds
, ",options(");
548 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
550 ds_put_char(ds
, ')');
553 ds_put_char(ds
, ')');
554 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
555 const struct gre_base_hdr
*greh
;
556 ovs_16aligned_be32
*options
;
558 greh
= (const struct gre_base_hdr
*) l4
;
560 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
561 ntohs(greh
->flags
), ntohs(greh
->protocol
));
562 options
= (ovs_16aligned_be32
*)(greh
+ 1);
563 if (greh
->flags
& htons(GRE_CSUM
)) {
564 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
567 if (greh
->flags
& htons(GRE_KEY
)) {
568 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
571 if (greh
->flags
& htons(GRE_SEQ
)) {
572 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
575 ds_put_format(ds
, ")");
577 ds_put_format(ds
, ")");
581 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
582 const struct hmap
*portno_names
)
584 struct ovs_action_push_tnl
*data
;
586 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
588 ds_put_cstr(ds
, "tnl_push(tnl_port(");
589 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
590 ds_put_cstr(ds
, "),");
591 format_odp_tnl_push_header(ds
, data
);
592 ds_put_format(ds
, ",out_port(");
593 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
594 ds_put_cstr(ds
, "))");
597 static const struct nl_policy ovs_nat_policy
[] = {
598 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
599 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
600 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
601 .min_len
= sizeof(struct in_addr
),
602 .max_len
= sizeof(struct in6_addr
)},
603 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
604 .min_len
= sizeof(struct in_addr
),
605 .max_len
= sizeof(struct in6_addr
)},
606 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
607 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
608 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
609 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
610 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
614 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
616 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
618 ovs_be32 ip_min
, ip_max
;
619 struct in6_addr ip6_min
, ip6_max
;
620 uint16_t proto_min
, proto_max
;
622 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
623 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
626 /* If no type, then nothing else either. */
627 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
628 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
629 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
630 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
631 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
632 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
635 /* Both SNAT & DNAT may not be specified. */
636 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
637 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
640 /* proto may not appear without ip. */
641 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
642 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
645 /* MAX may not appear without MIN. */
646 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
647 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
648 ds_put_cstr(ds
, "nat(error: range max without min.)");
651 /* Address sizes must match. */
652 if ((a
[OVS_NAT_ATTR_IP_MIN
]
653 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
654 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
655 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
656 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
657 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
658 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
662 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
663 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
664 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
665 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
666 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
667 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
668 if (addr_len
== sizeof ip6_min
) {
669 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
670 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
672 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
673 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
676 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
677 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
678 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
679 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
681 if ((addr_len
== sizeof(ovs_be32
)
682 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
683 || (addr_len
== sizeof(struct in6_addr
)
684 && !ipv6_mask_is_any(&ip6_max
)
685 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
686 || (proto_max
&& proto_min
> proto_max
)) {
687 ds_put_cstr(ds
, "nat(range error)");
691 ds_put_cstr(ds
, "nat");
692 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
693 ds_put_char(ds
, '(');
694 if (a
[OVS_NAT_ATTR_SRC
]) {
695 ds_put_cstr(ds
, "src");
696 } else if (a
[OVS_NAT_ATTR_DST
]) {
697 ds_put_cstr(ds
, "dst");
701 ds_put_cstr(ds
, "=");
703 if (addr_len
== sizeof ip_min
) {
704 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
706 if (ip_max
&& ip_max
!= ip_min
) {
707 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
709 } else if (addr_len
== sizeof ip6_min
) {
710 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
712 if (!ipv6_mask_is_any(&ip6_max
) &&
713 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
714 ds_put_char(ds
, '-');
715 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
719 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
721 if (proto_max
&& proto_max
!= proto_min
) {
722 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
726 ds_put_char(ds
, ',');
727 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
728 ds_put_cstr(ds
, "persistent,");
730 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
731 ds_put_cstr(ds
, "hash,");
733 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
734 ds_put_cstr(ds
, "random,");
737 ds_put_char(ds
, ')');
741 static const struct nl_policy ovs_conntrack_policy
[] = {
742 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
743 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
744 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
745 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
746 .min_len
= sizeof(uint32_t) * 2 },
747 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
748 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
749 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
750 .min_len
= 1, .max_len
= 16 },
751 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
755 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
757 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
759 ovs_32aligned_u128 value
;
760 ovs_32aligned_u128 mask
;
762 const uint32_t *mark
;
766 const struct nlattr
*nat
;
768 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
769 ds_put_cstr(ds
, "ct(error)");
773 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
774 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
775 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
776 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
777 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
778 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
779 nat
= a
[OVS_CT_ATTR_NAT
];
781 ds_put_format(ds
, "ct");
782 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
783 ds_put_cstr(ds
, "(");
785 ds_put_format(ds
, "commit,");
788 ds_put_format(ds
, "force_commit,");
791 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
794 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
798 ds_put_format(ds
, "label=");
799 format_u128(ds
, &label
->value
, &label
->mask
, true);
800 ds_put_char(ds
, ',');
803 ds_put_format(ds
, "helper=%s,", helper
);
806 format_odp_ct_nat(ds
, nat
);
809 ds_put_cstr(ds
, ")");
814 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
815 const struct hmap
*portno_names
)
818 enum ovs_action_attr type
= nl_attr_type(a
);
821 expected_len
= odp_action_len(nl_attr_type(a
));
822 if (expected_len
!= ATTR_LEN_VARIABLE
&&
823 nl_attr_get_size(a
) != expected_len
) {
824 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
825 nl_attr_get_size(a
), expected_len
);
826 format_generic_odp_action(ds
, a
);
831 case OVS_ACTION_ATTR_METER
:
832 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
834 case OVS_ACTION_ATTR_OUTPUT
:
835 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
837 case OVS_ACTION_ATTR_TRUNC
: {
838 const struct ovs_action_trunc
*trunc
=
839 nl_attr_get_unspec(a
, sizeof *trunc
);
841 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
844 case OVS_ACTION_ATTR_TUNNEL_POP
:
845 ds_put_cstr(ds
, "tnl_pop(");
846 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
847 ds_put_char(ds
, ')');
849 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
850 format_odp_tnl_push_action(ds
, a
, portno_names
);
852 case OVS_ACTION_ATTR_USERSPACE
:
853 format_odp_userspace_action(ds
, a
, portno_names
);
855 case OVS_ACTION_ATTR_RECIRC
:
856 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
858 case OVS_ACTION_ATTR_HASH
:
859 format_odp_hash_action(ds
, nl_attr_get(a
));
861 case OVS_ACTION_ATTR_SET_MASKED
:
863 size
= nl_attr_get_size(a
) / 2;
864 ds_put_cstr(ds
, "set(");
866 /* Masked set action not supported for tunnel key, which is bigger. */
867 if (size
<= sizeof(struct ovs_key_ipv6
)) {
868 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
869 sizeof(struct nlattr
))];
870 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
871 sizeof(struct nlattr
))];
873 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
874 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
875 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
876 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
877 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
879 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
881 ds_put_cstr(ds
, ")");
883 case OVS_ACTION_ATTR_SET
:
884 ds_put_cstr(ds
, "set(");
885 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
886 ds_put_cstr(ds
, ")");
888 case OVS_ACTION_ATTR_PUSH_ETH
: {
889 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
890 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
891 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
892 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
895 case OVS_ACTION_ATTR_POP_ETH
:
896 ds_put_cstr(ds
, "pop_eth");
898 case OVS_ACTION_ATTR_PUSH_VLAN
: {
899 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
900 ds_put_cstr(ds
, "push_vlan(");
901 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
902 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
904 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
905 ds_put_char(ds
, ')');
908 case OVS_ACTION_ATTR_POP_VLAN
:
909 ds_put_cstr(ds
, "pop_vlan");
911 case OVS_ACTION_ATTR_PUSH_MPLS
: {
912 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
913 ds_put_cstr(ds
, "push_mpls(");
914 format_mpls_lse(ds
, mpls
->mpls_lse
);
915 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
918 case OVS_ACTION_ATTR_POP_MPLS
: {
919 ovs_be16 ethertype
= nl_attr_get_be16(a
);
920 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
923 case OVS_ACTION_ATTR_SAMPLE
:
924 format_odp_sample_action(ds
, a
, portno_names
);
926 case OVS_ACTION_ATTR_CT
:
927 format_odp_conntrack_action(ds
, a
);
929 case OVS_ACTION_ATTR_CLONE
:
930 format_odp_clone_action(ds
, a
, portno_names
);
932 case OVS_ACTION_ATTR_UNSPEC
:
933 case __OVS_ACTION_ATTR_MAX
:
935 format_generic_odp_action(ds
, a
);
941 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
942 size_t actions_len
, const struct hmap
*portno_names
)
945 const struct nlattr
*a
;
948 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
950 ds_put_char(ds
, ',');
952 format_odp_action(ds
, a
, portno_names
);
957 if (left
== actions_len
) {
958 ds_put_cstr(ds
, "<empty>");
960 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
961 for (i
= 0; i
< left
; i
++) {
962 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
964 ds_put_char(ds
, ')');
967 ds_put_cstr(ds
, "drop");
971 /* Separate out parse_odp_userspace_action() function. */
973 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
976 union user_action_cookie cookie
;
978 odp_port_t tunnel_out_port
;
980 void *user_data
= NULL
;
981 size_t user_data_size
= 0;
982 bool include_actions
= false;
985 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
989 ofpbuf_init(&buf
, 16);
993 uint32_t probability
;
994 uint32_t collector_set_id
;
995 uint32_t obs_domain_id
;
996 uint32_t obs_point_id
;
999 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1000 "pcp=%i,output=%"SCNi32
")%n",
1001 &vid
, &pcp
, &output
, &n1
)) {
1005 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1010 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1011 cookie
.sflow
.vlan_tci
= htons(tci
);
1012 cookie
.sflow
.output
= output
;
1013 user_data
= &cookie
;
1014 user_data_size
= sizeof cookie
.sflow
;
1015 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1018 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1019 cookie
.slow_path
.unused
= 0;
1020 cookie
.slow_path
.reason
= 0;
1022 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1023 &cookie
.slow_path
.reason
,
1024 SLOW_PATH_REASON_MASK
, NULL
);
1025 if (res
< 0 || s
[n
+ res
] != ')') {
1030 user_data
= &cookie
;
1031 user_data_size
= sizeof cookie
.slow_path
;
1032 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1033 "collector_set_id=%"SCNi32
","
1034 "obs_domain_id=%"SCNi32
","
1035 "obs_point_id=%"SCNi32
","
1036 "output_port=%"SCNi32
"%n",
1037 &probability
, &collector_set_id
,
1038 &obs_domain_id
, &obs_point_id
,
1042 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1043 cookie
.flow_sample
.probability
= probability
;
1044 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1045 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1046 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1047 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1048 user_data
= &cookie
;
1049 user_data_size
= sizeof cookie
.flow_sample
;
1051 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1052 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1054 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1055 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1058 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1065 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1068 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1069 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1070 user_data
= &cookie
;
1071 user_data_size
= sizeof cookie
.ipfix
;
1072 } else if (ovs_scan(&s
[n
], ",userdata(%n",
1077 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1078 if (end
[0] != ')') {
1082 user_data
= buf
.data
;
1083 user_data_size
= buf
.size
;
1090 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1092 include_actions
= true;
1098 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1099 &tunnel_out_port
, &n1
)) {
1100 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1101 tunnel_out_port
, include_actions
, actions
);
1104 } else if (s
[n
] == ')') {
1105 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1106 ODPP_NONE
, include_actions
, actions
);
1113 struct ovs_action_push_eth push
;
1117 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1118 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1119 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1120 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1123 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1124 &push
, sizeof push
);
1131 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1132 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1139 ofpbuf_uninit(&buf
);
1144 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1146 struct eth_header
*eth
;
1147 struct ip_header
*ip
;
1148 struct ovs_16aligned_ip6_hdr
*ip6
;
1149 struct udp_header
*udp
;
1150 struct gre_base_hdr
*greh
;
1151 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1153 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1157 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1160 eth
= (struct eth_header
*) data
->header
;
1161 l3
= (struct ip_header
*) (eth
+ 1);
1162 ip
= (struct ip_header
*) l3
;
1163 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1164 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1165 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1168 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1172 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1173 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1176 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1179 eth
->eth_type
= htons(dl_type
);
1181 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1183 uint16_t ip_frag_off
;
1184 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1185 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1188 &ip
->ip_proto
, &ip
->ip_tos
,
1189 &ip
->ip_ttl
, &ip_frag_off
)) {
1192 put_16aligned_be32(&ip
->ip_src
, sip
);
1193 put_16aligned_be32(&ip
->ip_dst
, dip
);
1194 ip
->ip_frag_off
= htons(ip_frag_off
);
1195 ip_len
= sizeof *ip
;
1197 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1198 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1199 struct in6_addr sip6
, dip6
;
1202 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1203 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1204 ",hlimit=%"SCNi8
"),",
1205 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1206 &tclass
, &ip6
->ip6_hlim
)
1207 || (label
& ~IPV6_LABEL_MASK
) != 0
1208 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1209 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1212 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1213 htonl(tclass
<< 20) | htonl(label
));
1214 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1215 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1216 ip_len
= sizeof *ip6
;
1220 l4
= ((uint8_t *) l3
+ ip_len
);
1221 udp
= (struct udp_header
*) l4
;
1222 greh
= (struct gre_base_hdr
*) l4
;
1223 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1224 &udp_src
, &udp_dst
, &csum
)) {
1225 uint32_t vx_flags
, vni
;
1227 udp
->udp_src
= htons(udp_src
);
1228 udp
->udp_dst
= htons(udp_dst
);
1230 udp
->udp_csum
= htons(csum
);
1232 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1234 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1236 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1237 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1238 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1239 header_len
= sizeof *eth
+ ip_len
+
1240 sizeof *udp
+ sizeof *vxh
;
1241 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1242 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1244 memset(gnh
, 0, sizeof *gnh
);
1245 header_len
= sizeof *eth
+ ip_len
+
1246 sizeof *udp
+ sizeof *gnh
;
1248 if (ovs_scan_len(s
, &n
, "oam,")) {
1251 if (ovs_scan_len(s
, &n
, "crit,")) {
1254 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1257 if (ovs_scan_len(s
, &n
, ",options(")) {
1258 struct geneve_scan options
;
1261 memset(&options
, 0, sizeof options
);
1262 len
= scan_geneve(s
+ n
, &options
, NULL
);
1267 memcpy(gnh
->options
, options
.d
, options
.len
);
1268 gnh
->opt_len
= options
.len
/ 4;
1269 header_len
+= options
.len
;
1273 if (!ovs_scan_len(s
, &n
, "))")) {
1277 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1278 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1279 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1283 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1284 &gre_flags
, &gre_proto
)){
1286 tnl_type
= OVS_VPORT_TYPE_GRE
;
1287 greh
->flags
= htons(gre_flags
);
1288 greh
->protocol
= htons(gre_proto
);
1289 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1291 if (greh
->flags
& htons(GRE_CSUM
)) {
1292 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1296 memset(options
, 0, sizeof *options
);
1297 *((ovs_be16
*)options
) = htons(csum
);
1300 if (greh
->flags
& htons(GRE_KEY
)) {
1303 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1307 put_16aligned_be32(options
, htonl(key
));
1310 if (greh
->flags
& htons(GRE_SEQ
)) {
1313 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1316 put_16aligned_be32(options
, htonl(seq
));
1320 if (!ovs_scan_len(s
, &n
, "))")) {
1324 header_len
= sizeof *eth
+ ip_len
+
1325 ((uint8_t *) options
- (uint8_t *) greh
);
1330 /* check tunnel meta data. */
1331 if (data
->tnl_type
!= tnl_type
) {
1334 if (data
->header_len
!= header_len
) {
1339 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1346 struct ct_nat_params
{
1352 struct in6_addr ip6
;
1356 struct in6_addr ip6
;
1366 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1368 if (ovs_scan_len(s
, n
, "=")) {
1369 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1370 struct in6_addr ipv6
;
1372 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1373 p
->addr_len
= sizeof p
->addr_min
.ip
;
1374 if (ovs_scan_len(s
, n
, "-")) {
1375 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1376 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1380 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1381 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1382 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1383 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1384 p
->addr_min
.ip6
= ipv6
;
1385 if (ovs_scan_len(s
, n
, "-")) {
1386 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1387 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1388 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1389 p
->addr_max
.ip6
= ipv6
;
1397 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1398 if (ovs_scan_len(s
, n
, "-")) {
1399 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1409 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1413 if (ovs_scan_len(s
, &n
, "nat")) {
1414 memset(p
, 0, sizeof *p
);
1416 if (ovs_scan_len(s
, &n
, "(")) {
1420 end
= strchr(s
+ n
, ')');
1427 n
+= strspn(s
+ n
, delimiters
);
1428 if (ovs_scan_len(s
, &n
, "src")) {
1429 int err
= scan_ct_nat_range(s
, &n
, p
);
1436 if (ovs_scan_len(s
, &n
, "dst")) {
1437 int err
= scan_ct_nat_range(s
, &n
, p
);
1444 if (ovs_scan_len(s
, &n
, "persistent")) {
1445 p
->persistent
= true;
1448 if (ovs_scan_len(s
, &n
, "hash")) {
1449 p
->proto_hash
= true;
1452 if (ovs_scan_len(s
, &n
, "random")) {
1453 p
->proto_random
= true;
1459 if (p
->snat
&& p
->dnat
) {
1462 if ((p
->addr_len
!= 0 &&
1463 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1464 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1465 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1468 if (p
->proto_hash
&& p
->proto_random
) {
1478 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1480 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1483 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1484 } else if (p
->dnat
) {
1485 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1489 if (p
->addr_len
!= 0) {
1490 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1492 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1493 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1497 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1498 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1499 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1502 if (p
->persistent
) {
1503 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1505 if (p
->proto_hash
) {
1506 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1508 if (p
->proto_random
) {
1509 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1513 nl_msg_end_nested(actions
, start
);
1517 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1521 if (ovs_scan(s
, "ct")) {
1522 const char *helper
= NULL
;
1523 size_t helper_len
= 0;
1524 bool commit
= false;
1525 bool force_commit
= false;
1530 } ct_mark
= { 0, 0 };
1535 struct ct_nat_params nat_params
;
1536 bool have_nat
= false;
1540 memset(&ct_label
, 0, sizeof(ct_label
));
1543 if (ovs_scan(s
, "(")) {
1546 end
= strchr(s
, ')');
1554 s
+= strspn(s
, delimiters
);
1555 if (ovs_scan(s
, "commit%n", &n
)) {
1560 if (ovs_scan(s
, "force_commit%n", &n
)) {
1561 force_commit
= true;
1565 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1569 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1572 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1575 ct_mark
.mask
= UINT32_MAX
;
1579 if (ovs_scan(s
, "label=%n", &n
)) {
1583 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1590 if (ovs_scan(s
, "helper=%n", &n
)) {
1592 helper_len
= strcspn(s
, delimiters_end
);
1593 if (!helper_len
|| helper_len
> 15) {
1601 n
= scan_ct_nat(s
, &nat_params
);
1606 /* end points to the end of the nested, nat action.
1607 * find the real end. */
1610 /* Nothing matched. */
1615 if (commit
&& force_commit
) {
1619 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1621 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1622 } else if (force_commit
) {
1623 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1626 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1629 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1632 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1633 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1637 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1641 nl_msg_put_ct_nat(&nat_params
, actions
);
1643 nl_msg_end_nested(actions
, start
);
1650 parse_action_list(const char *s
, const struct simap
*port_names
,
1651 struct ofpbuf
*actions
)
1658 n
+= strspn(s
+ n
, delimiters
);
1662 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1673 parse_odp_action(const char *s
, const struct simap
*port_names
,
1674 struct ofpbuf
*actions
)
1680 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1681 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1690 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
1691 struct ovs_action_trunc
*trunc
;
1693 trunc
= nl_msg_put_unspec_uninit(actions
,
1694 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1695 trunc
->max_len
= max_len
;
1701 int len
= strcspn(s
, delimiters
);
1702 struct simap_node
*node
;
1704 node
= simap_find_len(port_names
, s
, len
);
1706 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1715 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1716 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1721 if (!strncmp(s
, "userspace(", 10)) {
1722 return parse_odp_userspace_action(s
, actions
);
1725 if (!strncmp(s
, "set(", 4)) {
1728 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1729 struct ofpbuf maskbuf
;
1730 struct nlattr
*nested
, *key
;
1733 /* 'mask' is big enough to hold any key. */
1734 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1736 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1737 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1741 if (s
[retval
+ 4] != ')') {
1745 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1748 size
= nl_attr_get_size(mask
);
1749 if (size
== nl_attr_get_size(key
)) {
1750 /* Change to masked set action if not fully masked. */
1751 if (!is_all_ones(mask
+ 1, size
)) {
1752 key
->nla_len
+= size
;
1753 ofpbuf_put(actions
, mask
+ 1, size
);
1754 /* 'actions' may have been reallocated by ofpbuf_put(). */
1755 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1756 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1760 nl_msg_end_nested(actions
, start_ofs
);
1765 struct ovs_action_push_vlan push
;
1766 int tpid
= ETH_TYPE_VLAN
;
1771 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1772 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1773 &vid
, &pcp
, &cfi
, &n
)
1774 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1775 &tpid
, &vid
, &pcp
, &n
)
1776 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1777 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1778 push
.vlan_tpid
= htons(tpid
);
1779 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1780 | (pcp
<< VLAN_PCP_SHIFT
)
1781 | (cfi
? VLAN_CFI
: 0));
1782 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1783 &push
, sizeof push
);
1789 if (!strncmp(s
, "pop_vlan", 8)) {
1790 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1795 unsigned long long int meter_id
;
1798 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
1799 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
1808 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1809 && percentage
>= 0. && percentage
<= 100.0) {
1810 size_t sample_ofs
, actions_ofs
;
1813 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1814 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1815 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1816 (probability
<= 0 ? 0
1817 : probability
>= UINT32_MAX
? UINT32_MAX
1820 actions_ofs
= nl_msg_start_nested(actions
,
1821 OVS_SAMPLE_ATTR_ACTIONS
);
1822 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
1827 nl_msg_end_nested(actions
, actions_ofs
);
1828 nl_msg_end_nested(actions
, sample_ofs
);
1830 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1835 if (!strncmp(s
, "clone(", 6)) {
1839 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
1840 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
1845 nl_msg_end_nested(actions
, actions_ofs
);
1854 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1855 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1863 retval
= parse_conntrack_action(s
, actions
);
1870 struct ovs_action_push_tnl data
;
1873 n
= ovs_parse_tnl_push(s
, &data
);
1875 odp_put_tnl_push_action(actions
, &data
);
1884 /* Parses the string representation of datapath actions, in the format output
1885 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1886 * value. On success, the ODP actions are appended to 'actions' as a series of
1887 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1888 * way, 'actions''s data might be reallocated. */
1890 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1891 struct ofpbuf
*actions
)
1895 if (!strcasecmp(s
, "drop")) {
1899 old_size
= actions
->size
;
1903 s
+= strspn(s
, delimiters
);
1908 retval
= parse_odp_action(s
, port_names
, actions
);
1909 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1910 actions
->size
= old_size
;
1919 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1920 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1923 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1924 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1925 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1926 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1927 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1928 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1929 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1930 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1931 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1932 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1933 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1934 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1935 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1936 .next
= ovs_vxlan_ext_attr_lens
,
1937 .next_max
= OVS_VXLAN_EXT_MAX
},
1938 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1939 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1942 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1943 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1944 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1945 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1946 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1947 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1948 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1949 .next
= ovs_tun_key_attr_lens
,
1950 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1951 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1952 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1953 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1954 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1955 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1956 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1957 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1958 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1959 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1960 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1961 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1962 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1963 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1964 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1965 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1966 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1967 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1968 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1969 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1970 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
1971 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
1972 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
1975 /* Returns the correct length of the payload for a flow key attribute of the
1976 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1977 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1978 * payload is a nested type. */
1980 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
1982 if (type
> max_type
) {
1983 return ATTR_LEN_INVALID
;
1986 return tbl
[type
].len
;
1990 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1992 size_t len
= nl_attr_get_size(a
);
1994 const uint8_t *unspec
;
1997 unspec
= nl_attr_get(a
);
1998 for (i
= 0; i
< len
; i
++) {
2000 ds_put_char(ds
, ' ');
2002 ds_put_format(ds
, "%02x", unspec
[i
]);
2008 ovs_frag_type_to_string(enum ovs_frag_type type
)
2011 case OVS_FRAG_TYPE_NONE
:
2013 case OVS_FRAG_TYPE_FIRST
:
2015 case OVS_FRAG_TYPE_LATER
:
2017 case __OVS_FRAG_TYPE_MAX
:
2023 static enum odp_key_fitness
2024 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2025 struct flow_tnl
*tun
)
2028 const struct nlattr
*a
;
2030 bool unknown
= false;
2032 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2033 uint16_t type
= nl_attr_type(a
);
2034 size_t len
= nl_attr_get_size(a
);
2035 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2036 OVS_TUNNEL_ATTR_MAX
, type
);
2038 if (len
!= expected_len
&& expected_len
>= 0) {
2039 return ODP_FIT_ERROR
;
2043 case OVS_TUNNEL_KEY_ATTR_ID
:
2044 tun
->tun_id
= nl_attr_get_be64(a
);
2045 tun
->flags
|= FLOW_TNL_F_KEY
;
2047 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2048 tun
->ip_src
= nl_attr_get_be32(a
);
2050 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2051 tun
->ip_dst
= nl_attr_get_be32(a
);
2053 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2054 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2056 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2057 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2059 case OVS_TUNNEL_KEY_ATTR_TOS
:
2060 tun
->ip_tos
= nl_attr_get_u8(a
);
2062 case OVS_TUNNEL_KEY_ATTR_TTL
:
2063 tun
->ip_ttl
= nl_attr_get_u8(a
);
2066 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2067 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2069 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2070 tun
->flags
|= FLOW_TNL_F_CSUM
;
2072 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2073 tun
->tp_src
= nl_attr_get_be16(a
);
2075 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2076 tun
->tp_dst
= nl_attr_get_be16(a
);
2078 case OVS_TUNNEL_KEY_ATTR_OAM
:
2079 tun
->flags
|= FLOW_TNL_F_OAM
;
2081 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2082 static const struct nl_policy vxlan_opts_policy
[] = {
2083 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2085 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2087 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2088 return ODP_FIT_ERROR
;
2091 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2092 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2094 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2095 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2100 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2101 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2105 /* Allow this to show up as unexpected, if there are unknown
2106 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2113 return ODP_FIT_ERROR
;
2116 return ODP_FIT_TOO_MUCH
;
2118 return ODP_FIT_PERFECT
;
2121 enum odp_key_fitness
2122 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2124 memset(tun
, 0, sizeof *tun
);
2125 return odp_tun_key_from_attr__(attr
, false, tun
);
2129 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2130 const struct flow_tnl
*tun_flow_key
,
2131 const struct ofpbuf
*key_buf
)
2135 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2137 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2138 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2139 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2141 if (tun_key
->ip_src
) {
2142 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2144 if (tun_key
->ip_dst
) {
2145 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2147 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2148 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2150 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2151 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2153 if (tun_key
->ip_tos
) {
2154 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2156 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2157 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2158 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2160 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2161 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2163 if (tun_key
->tp_src
) {
2164 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2166 if (tun_key
->tp_dst
) {
2167 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2169 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2170 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2172 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
2173 size_t vxlan_opts_ofs
;
2175 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2176 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2177 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2178 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2180 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2182 nl_msg_end_nested(a
, tun_key_ofs
);
2186 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2189 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2190 * that -1 becomes all-1-bits and 0 does not change. */
2191 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2192 uint32_t u32
= constant
;
2193 uint8_t u8
= constant
;
2194 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
2197 case OVS_KEY_ATTR_UNSPEC
:
2198 case OVS_KEY_ATTR_ENCAP
:
2199 case __OVS_KEY_ATTR_MAX
:
2203 case OVS_KEY_ATTR_PRIORITY
:
2204 case OVS_KEY_ATTR_IN_PORT
:
2205 case OVS_KEY_ATTR_ETHERNET
:
2206 case OVS_KEY_ATTR_VLAN
:
2207 case OVS_KEY_ATTR_ETHERTYPE
:
2208 case OVS_KEY_ATTR_IPV4
:
2209 case OVS_KEY_ATTR_TCP
:
2210 case OVS_KEY_ATTR_UDP
:
2211 case OVS_KEY_ATTR_ICMP
:
2212 case OVS_KEY_ATTR_ICMPV6
:
2213 case OVS_KEY_ATTR_ND
:
2214 case OVS_KEY_ATTR_SKB_MARK
:
2215 case OVS_KEY_ATTR_TUNNEL
:
2216 case OVS_KEY_ATTR_SCTP
:
2217 case OVS_KEY_ATTR_DP_HASH
:
2218 case OVS_KEY_ATTR_RECIRC_ID
:
2219 case OVS_KEY_ATTR_MPLS
:
2220 case OVS_KEY_ATTR_CT_STATE
:
2221 case OVS_KEY_ATTR_CT_ZONE
:
2222 case OVS_KEY_ATTR_CT_MARK
:
2223 case OVS_KEY_ATTR_CT_LABELS
:
2224 case OVS_KEY_ATTR_PACKET_TYPE
:
2225 return is_all_byte(mask
, size
, u8
);
2227 case OVS_KEY_ATTR_TCP_FLAGS
:
2228 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
2230 case OVS_KEY_ATTR_IPV6
: {
2231 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2232 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2233 == htonl(IPV6_LABEL_MASK
& u32
)
2234 && ipv6_mask
->ipv6_proto
== u8
2235 && ipv6_mask
->ipv6_tclass
== u8
2236 && ipv6_mask
->ipv6_hlimit
== u8
2237 && ipv6_mask
->ipv6_frag
== u8
2238 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
2239 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
2242 case OVS_KEY_ATTR_ARP
:
2243 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
2245 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
2246 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
2249 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
2250 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
2255 /* The caller must already have verified that 'ma' has a correct length.
2257 * The main purpose of this function is formatting, to allow code to figure out
2258 * whether the mask can be omitted. It doesn't try hard for attributes that
2259 * contain sub-attributes, etc., because normally those would be broken down
2260 * further for formatting. */
2262 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2264 return odp_mask_is_constant__(nl_attr_type(ma
),
2265 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
2268 /* The caller must already have verified that 'size' is a correct length for
2271 * The main purpose of this function is formatting, to allow code to figure out
2272 * whether the mask can be omitted. It doesn't try hard for attributes that
2273 * contain sub-attributes, etc., because normally those would be broken down
2274 * further for formatting. */
2276 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2278 return odp_mask_is_constant__(attr
, mask
, size
, -1);
2281 /* The caller must already have verified that 'ma' has a correct length. */
2283 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2285 enum ovs_key_attr attr
= nl_attr_type(ma
);
2286 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
2290 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2293 struct odp_portno_names
*odp_portno_names
;
2295 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2296 odp_portno_names
->port_no
= port_no
;
2297 odp_portno_names
->name
= xstrdup(port_name
);
2298 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2299 hash_odp_port(port_no
));
2303 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2306 struct odp_portno_names
*odp_portno_names
;
2308 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2309 hash_odp_port(port_no
), portno_names
) {
2310 if (odp_portno_names
->port_no
== port_no
) {
2311 return odp_portno_names
->name
;
2319 odp_portno_names_destroy(struct hmap
*portno_names
)
2321 struct odp_portno_names
*odp_portno_names
;
2323 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
2324 free(odp_portno_names
->name
);
2325 free(odp_portno_names
);
2330 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
2333 const char *name
= odp_portno_names_get(portno_names
, port_no
);
2335 ds_put_cstr(s
, name
);
2337 ds_put_format(s
, "%"PRIu32
, port_no
);
2341 /* Format helpers. */
2344 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2345 const struct eth_addr
*mask
, bool verbose
)
2347 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2349 if (verbose
|| !mask_empty
) {
2350 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2353 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2355 ds_put_format(ds
, "%s=", name
);
2356 eth_format_masked(key
, mask
, ds
);
2357 ds_put_char(ds
, ',');
2363 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2364 const ovs_be64
*mask
, bool verbose
)
2366 bool mask_empty
= mask
&& !*mask
;
2368 if (verbose
|| !mask_empty
) {
2369 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2371 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2372 if (!mask_full
) { /* Partially masked. */
2373 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2375 ds_put_char(ds
, ',');
2380 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2381 const ovs_be32
*mask
, bool verbose
)
2383 bool mask_empty
= mask
&& !*mask
;
2385 if (verbose
|| !mask_empty
) {
2386 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2388 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2389 if (!mask_full
) { /* Partially masked. */
2390 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2392 ds_put_char(ds
, ',');
2397 format_in6_addr(struct ds
*ds
, const char *name
,
2398 const struct in6_addr
*key
,
2399 const struct in6_addr
*mask
,
2402 char buf
[INET6_ADDRSTRLEN
];
2403 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2405 if (verbose
|| !mask_empty
) {
2406 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2408 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2409 ds_put_format(ds
, "%s=%s", name
, buf
);
2410 if (!mask_full
) { /* Partially masked. */
2411 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2412 ds_put_format(ds
, "/%s", buf
);
2414 ds_put_char(ds
, ',');
2419 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2420 const ovs_be32
*mask
, bool verbose
)
2422 bool mask_empty
= mask
&& !*mask
;
2424 if (verbose
|| !mask_empty
) {
2425 bool mask_full
= !mask
2426 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2428 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2429 if (!mask_full
) { /* Partially masked. */
2430 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2432 ds_put_char(ds
, ',');
2437 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2438 const uint8_t *mask
, bool verbose
)
2440 bool mask_empty
= mask
&& !*mask
;
2442 if (verbose
|| !mask_empty
) {
2443 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2445 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2446 if (!mask_full
) { /* Partially masked. */
2447 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2449 ds_put_char(ds
, ',');
2454 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2455 const uint8_t *mask
, bool verbose
)
2457 bool mask_empty
= mask
&& !*mask
;
2459 if (verbose
|| !mask_empty
) {
2460 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2462 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2463 if (!mask_full
) { /* Partially masked. */
2464 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2466 ds_put_char(ds
, ',');
2471 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2472 const ovs_be16
*mask
, bool verbose
)
2474 bool mask_empty
= mask
&& !*mask
;
2476 if (verbose
|| !mask_empty
) {
2477 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2479 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2480 if (!mask_full
) { /* Partially masked. */
2481 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2483 ds_put_char(ds
, ',');
2488 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2489 const ovs_be16
*mask
, bool verbose
)
2491 bool mask_empty
= mask
&& !*mask
;
2493 if (verbose
|| !mask_empty
) {
2494 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2496 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2497 if (!mask_full
) { /* Partially masked. */
2498 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2500 ds_put_char(ds
, ',');
2505 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2506 const uint16_t *mask
, bool verbose
)
2508 bool mask_empty
= mask
&& !*mask
;
2510 if (verbose
|| !mask_empty
) {
2511 ds_put_cstr(ds
, name
);
2512 ds_put_char(ds
, '(');
2514 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2515 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2516 } else { /* Fully masked. */
2517 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2519 ds_put_cstr(ds
, "),");
2524 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2525 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
2529 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
2530 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2531 expected_len
!= ATTR_LEN_NESTED
) {
2533 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2534 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2536 if (bad_key_len
|| bad_mask_len
) {
2538 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2541 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2542 nl_attr_get_size(a
), expected_len
);
2544 format_generic_odp_key(a
, ds
);
2546 ds_put_char(ds
, '/');
2548 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2549 nl_attr_get_size(ma
), expected_len
);
2551 format_generic_odp_key(ma
, ds
);
2553 ds_put_char(ds
, ')');
2562 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2563 const struct nlattr
*ma
)
2565 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2566 format_generic_odp_key(a
, ds
);
2567 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2568 ds_put_char(ds
, '/');
2569 format_generic_odp_key(ma
, ds
);
2571 ds_put_cstr(ds
, "),");
2575 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2576 const struct nlattr
*mask_attr
, struct ds
*ds
,
2580 const struct nlattr
*a
;
2583 ofpbuf_init(&ofp
, 100);
2584 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2585 uint16_t type
= nl_attr_type(a
);
2586 const struct nlattr
*ma
= NULL
;
2589 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2590 nl_attr_get_size(mask_attr
), type
);
2592 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2598 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2599 OVS_VXLAN_EXT_MAX
, true)) {
2604 case OVS_VXLAN_EXT_GBP
: {
2605 uint32_t key
= nl_attr_get_u32(a
);
2606 ovs_be16 id
, id_mask
;
2607 uint8_t flags
, flags_mask
= 0;
2609 id
= htons(key
& 0xFFFF);
2610 flags
= (key
>> 16) & 0xFF;
2612 uint32_t mask
= nl_attr_get_u32(ma
);
2613 id_mask
= htons(mask
& 0xFFFF);
2614 flags_mask
= (mask
>> 16) & 0xFF;
2617 ds_put_cstr(ds
, "gbp(");
2618 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2619 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2621 ds_put_cstr(ds
, "),");
2626 format_unknown_key(ds
, a
, ma
);
2632 ofpbuf_uninit(&ofp
);
2635 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2638 format_geneve_opts(const struct geneve_opt
*opt
,
2639 const struct geneve_opt
*mask
, int opts_len
,
2640 struct ds
*ds
, bool verbose
)
2642 while (opts_len
> 0) {
2644 uint8_t data_len
, data_len_mask
;
2646 if (opts_len
< sizeof *opt
) {
2647 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2648 opts_len
, sizeof *opt
);
2652 data_len
= opt
->length
* 4;
2654 if (mask
->length
== 0x1f) {
2655 data_len_mask
= UINT8_MAX
;
2657 data_len_mask
= mask
->length
;
2660 len
= sizeof *opt
+ data_len
;
2661 if (len
> opts_len
) {
2662 ds_put_format(ds
, "opt len %u greater than remaining %u",
2667 ds_put_char(ds
, '{');
2668 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2670 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2671 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2673 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2674 ds_put_hex(ds
, opt
+ 1, data_len
);
2675 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2676 ds_put_char(ds
, '/');
2677 ds_put_hex(ds
, mask
+ 1, data_len
);
2682 ds_put_char(ds
, '}');
2684 opt
+= len
/ sizeof(*opt
);
2686 mask
+= len
/ sizeof(*opt
);
2693 format_odp_tun_geneve(const struct nlattr
*attr
,
2694 const struct nlattr
*mask_attr
, struct ds
*ds
,
2697 int opts_len
= nl_attr_get_size(attr
);
2698 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2699 const struct geneve_opt
*mask
= mask_attr
?
2700 nl_attr_get(mask_attr
) : NULL
;
2702 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2703 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2704 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2708 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2712 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2713 struct ds
*ds
, bool verbose
)
2716 const struct nlattr
*a
;
2718 uint16_t mask_flags
= 0;
2721 ofpbuf_init(&ofp
, 100);
2722 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2723 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2724 const struct nlattr
*ma
= NULL
;
2727 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2728 nl_attr_get_size(mask_attr
), type
);
2730 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2731 OVS_TUNNEL_KEY_ATTR_MAX
,
2736 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2737 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2742 case OVS_TUNNEL_KEY_ATTR_ID
:
2743 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2744 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2745 flags
|= FLOW_TNL_F_KEY
;
2747 mask_flags
|= FLOW_TNL_F_KEY
;
2750 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2751 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2752 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2754 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2755 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2756 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2758 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2759 struct in6_addr ipv6_src
;
2760 ipv6_src
= nl_attr_get_in6_addr(a
);
2761 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2762 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2765 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2766 struct in6_addr ipv6_dst
;
2767 ipv6_dst
= nl_attr_get_in6_addr(a
);
2768 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2769 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2772 case OVS_TUNNEL_KEY_ATTR_TOS
:
2773 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2774 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2776 case OVS_TUNNEL_KEY_ATTR_TTL
:
2777 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2778 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2780 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2781 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2783 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2784 flags
|= FLOW_TNL_F_CSUM
;
2786 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2787 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2788 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2790 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2791 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2792 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2794 case OVS_TUNNEL_KEY_ATTR_OAM
:
2795 flags
|= FLOW_TNL_F_OAM
;
2797 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2798 ds_put_cstr(ds
, "vxlan(");
2799 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2800 ds_put_cstr(ds
, "),");
2802 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2803 ds_put_cstr(ds
, "geneve(");
2804 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2805 ds_put_cstr(ds
, "),");
2807 case OVS_TUNNEL_KEY_ATTR_PAD
:
2809 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2811 format_unknown_key(ds
, a
, ma
);
2816 /* Flags can have a valid mask even if the attribute is not set, so
2817 * we need to collect these separately. */
2819 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2820 switch (nl_attr_type(a
)) {
2821 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2822 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2824 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2825 mask_flags
|= FLOW_TNL_F_CSUM
;
2827 case OVS_TUNNEL_KEY_ATTR_OAM
:
2828 mask_flags
|= FLOW_TNL_F_OAM
;
2834 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2837 ofpbuf_uninit(&ofp
);
2841 odp_ct_state_to_string(uint32_t flag
)
2844 case OVS_CS_F_REPLY_DIR
:
2846 case OVS_CS_F_TRACKED
:
2850 case OVS_CS_F_ESTABLISHED
:
2852 case OVS_CS_F_RELATED
:
2854 case OVS_CS_F_INVALID
:
2856 case OVS_CS_F_SRC_NAT
:
2858 case OVS_CS_F_DST_NAT
:
2866 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2867 const uint8_t *mask
, bool verbose
)
2869 bool mask_empty
= mask
&& !*mask
;
2871 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2872 if (verbose
|| !mask_empty
) {
2873 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2875 if (!mask_full
) { /* Partially masked. */
2876 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2879 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2885 mask_empty(const struct nlattr
*ma
)
2893 mask
= nl_attr_get(ma
);
2894 n
= nl_attr_get_size(ma
);
2896 return is_all_zeros(mask
, n
);
2899 /* The caller must have already verified that 'a' and 'ma' have correct
2902 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
2903 const struct hmap
*portno_names
, struct ds
*ds
,
2906 enum ovs_key_attr attr
= nl_attr_type(a
);
2907 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2910 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2912 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2914 ds_put_char(ds
, '(');
2916 case OVS_KEY_ATTR_ENCAP
:
2917 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2918 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2919 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2921 } else if (nl_attr_get_size(a
)) {
2922 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2927 case OVS_KEY_ATTR_PRIORITY
:
2928 case OVS_KEY_ATTR_SKB_MARK
:
2929 case OVS_KEY_ATTR_DP_HASH
:
2930 case OVS_KEY_ATTR_RECIRC_ID
:
2931 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2933 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2937 case OVS_KEY_ATTR_CT_MARK
:
2938 if (verbose
|| !mask_empty(ma
)) {
2939 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2941 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2946 case OVS_KEY_ATTR_CT_STATE
:
2948 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2950 ds_put_format(ds
, "/%#"PRIx32
,
2951 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2953 } else if (!is_exact
) {
2954 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2956 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2959 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2963 case OVS_KEY_ATTR_CT_ZONE
:
2964 if (verbose
|| !mask_empty(ma
)) {
2965 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2967 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2972 case OVS_KEY_ATTR_CT_LABELS
: {
2973 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
2974 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2976 format_u128(ds
, value
, mask
, verbose
);
2980 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
2981 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
2982 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2984 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2985 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2986 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2988 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
2990 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
2996 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
2997 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
2998 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3000 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3002 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3004 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3006 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3008 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3014 case OVS_KEY_ATTR_TUNNEL
:
3015 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3018 case OVS_KEY_ATTR_IN_PORT
:
3020 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3022 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3024 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3029 case OVS_KEY_ATTR_PACKET_TYPE
: {
3030 ovs_be32 value
= nl_attr_get_be32(a
);
3031 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3033 ovs_be16 ns
= htons(pt_ns(value
));
3034 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3035 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3037 ovs_be16 ns_type
= pt_ns_type_be(value
);
3038 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3039 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3045 case OVS_KEY_ATTR_ETHERNET
: {
3046 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3047 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3049 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3050 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3054 case OVS_KEY_ATTR_VLAN
:
3055 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3056 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3059 case OVS_KEY_ATTR_MPLS
: {
3060 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3061 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3062 size_t size
= nl_attr_get_size(a
);
3064 if (!size
|| size
% sizeof *mpls_key
) {
3065 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3069 mpls_mask
= nl_attr_get(ma
);
3070 if (size
!= nl_attr_get_size(ma
)) {
3071 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3072 "mask length %"PRIuSIZE
")",
3073 size
, nl_attr_get_size(ma
));
3077 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
3080 case OVS_KEY_ATTR_ETHERTYPE
:
3081 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
3083 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
3087 case OVS_KEY_ATTR_IPV4
: {
3088 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
3089 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3091 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3092 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3093 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3095 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
3096 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
3097 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
3102 case OVS_KEY_ATTR_IPV6
: {
3103 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
3104 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3106 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3108 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3110 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
3112 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3114 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
3116 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
3118 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
3123 /* These have the same structure and format. */
3124 case OVS_KEY_ATTR_TCP
:
3125 case OVS_KEY_ATTR_UDP
:
3126 case OVS_KEY_ATTR_SCTP
: {
3127 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
3128 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3130 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
3131 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
3135 case OVS_KEY_ATTR_TCP_FLAGS
:
3137 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
3138 ntohs(nl_attr_get_be16(a
)),
3139 TCP_FLAGS(nl_attr_get_be16(ma
)),
3140 TCP_FLAGS(OVS_BE16_MAX
));
3142 format_flags(ds
, packet_tcp_flag_to_string
,
3143 ntohs(nl_attr_get_be16(a
)), '|');
3147 case OVS_KEY_ATTR_ICMP
: {
3148 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
3149 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3151 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
3152 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
3156 case OVS_KEY_ATTR_ICMPV6
: {
3157 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
3158 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3160 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
3162 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
3167 case OVS_KEY_ATTR_ARP
: {
3168 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3169 const struct ovs_key_arp
*key
= nl_attr_get(a
);
3171 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
3172 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
3173 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
3174 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
3175 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
3179 case OVS_KEY_ATTR_ND
: {
3180 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3181 const struct ovs_key_nd
*key
= nl_attr_get(a
);
3183 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
3185 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
3186 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
3191 case OVS_KEY_ATTR_UNSPEC
:
3192 case __OVS_KEY_ATTR_MAX
:
3194 format_generic_odp_key(a
, ds
);
3196 ds_put_char(ds
, '/');
3197 format_generic_odp_key(ma
, ds
);
3201 ds_put_char(ds
, ')');
3205 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
3206 const struct hmap
*portno_names
, struct ds
*ds
,
3209 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
3210 OVS_KEY_ATTR_MAX
, false)) {
3211 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
3215 static struct nlattr
*
3216 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
3217 struct ofpbuf
*ofp
, const struct nlattr
*key
)
3219 const struct nlattr
*a
;
3221 int type
= nl_attr_type(key
);
3222 int size
= nl_attr_get_size(key
);
3224 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
3225 nl_msg_put_unspec_zero(ofp
, type
, size
);
3229 if (tbl
[type
].next
) {
3230 tbl
= tbl
[type
].next
;
3231 max
= tbl
[type
].next_max
;
3234 nested_mask
= nl_msg_start_nested(ofp
, type
);
3235 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
3236 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
3238 nl_msg_end_nested(ofp
, nested_mask
);
3245 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
3246 const ovs_32aligned_u128
*mask
, bool verbose
)
3248 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
3249 ovs_be128 value
= hton128(get_32aligned_u128(key
));
3250 ds_put_hex(ds
, &value
, sizeof value
);
3251 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
3252 value
= hton128(get_32aligned_u128(mask
));
3253 ds_put_char(ds
, '/');
3254 ds_put_hex(ds
, &value
, sizeof value
);
3259 /* Read the string from 's_' as a 128-bit value. If the string contains
3260 * a "/", the rest of the string will be treated as a 128-bit mask.
3262 * If either the value or mask is larger than 64 bits, the string must
3263 * be in hexadecimal.
3266 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
3268 char *s
= CONST_CAST(char *, s_
);
3272 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3273 *value
= ntoh128(be_value
);
3278 if (ovs_scan(s
, "/%n", &n
)) {
3282 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3283 sizeof be_mask
, &s
);
3287 *mask
= ntoh128(be_mask
);
3289 *mask
= OVS_U128_MAX
;
3299 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3303 if (ovs_scan(s
, "ufid:")) {
3306 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3318 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3320 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3323 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3324 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3325 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3326 * non-null, translates odp port number to its name. */
3328 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3329 const struct nlattr
*mask
, size_t mask_len
,
3330 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3333 const struct nlattr
*a
;
3335 bool has_ethtype_key
= false;
3337 bool first_field
= true;
3339 ofpbuf_init(&ofp
, 100);
3340 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3341 int attr_type
= nl_attr_type(a
);
3342 const struct nlattr
*ma
= (mask
&& mask_len
3343 ? nl_attr_find__(mask
, mask_len
,
3346 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
3347 OVS_KEY_ATTR_MAX
, false)) {
3351 bool is_nested_attr
;
3352 bool is_wildcard
= false;
3354 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3355 has_ethtype_key
= true;
3358 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3359 OVS_KEY_ATTR_MAX
, attr_type
) ==
3362 if (mask
&& mask_len
) {
3363 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3364 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3367 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3368 if (is_wildcard
&& !ma
) {
3369 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3374 ds_put_char(ds
, ',');
3376 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
3377 first_field
= false;
3381 ofpbuf_uninit(&ofp
);
3386 if (left
== key_len
) {
3387 ds_put_cstr(ds
, "<empty>");
3389 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3390 for (i
= 0; i
< left
; i
++) {
3391 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3393 ds_put_char(ds
, ')');
3395 if (!has_ethtype_key
) {
3396 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
3397 OVS_KEY_ATTR_ETHERTYPE
);
3399 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3400 ntohs(nl_attr_get_be16(ma
)));
3404 ds_put_cstr(ds
, "<empty>");
3408 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3409 * OVS_KEY_ATTR_* attributes in 'key'. */
3411 odp_flow_key_format(const struct nlattr
*key
,
3412 size_t key_len
, struct ds
*ds
)
3414 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3418 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3420 if (!strcasecmp(s
, "no")) {
3421 *type
= OVS_FRAG_TYPE_NONE
;
3422 } else if (!strcasecmp(s
, "first")) {
3423 *type
= OVS_FRAG_TYPE_FIRST
;
3424 } else if (!strcasecmp(s
, "later")) {
3425 *type
= OVS_FRAG_TYPE_LATER
;
3435 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3439 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3440 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3444 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3445 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3448 memset(mask
, 0xff, sizeof *mask
);
3457 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3461 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3465 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3466 IP_SCAN_ARGS(mask
), &n
)) {
3469 *mask
= OVS_BE32_MAX
;
3478 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3481 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3483 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3484 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3488 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3489 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3492 memset(mask
, 0xff, sizeof *mask
);
3501 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3506 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3507 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3512 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3513 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3515 *mask
= htonl(mask_
);
3517 *mask
= htonl(IPV6_LABEL_MASK
);
3526 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3530 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3534 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3546 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3550 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3554 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3566 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3570 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3574 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3586 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3588 uint16_t key_
, mask_
;
3591 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3596 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3598 *mask
= htons(mask_
);
3600 *mask
= OVS_BE16_MAX
;
3609 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3611 uint64_t key_
, mask_
;
3614 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3617 *key
= htonll(key_
);
3619 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3621 *mask
= htonll(mask_
);
3623 *mask
= OVS_BE64_MAX
;
3632 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3634 uint32_t flags
, fmask
;
3637 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3638 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3639 if (n
>= 0 && s
[n
] == ')') {
3650 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3652 uint32_t flags
, fmask
;
3655 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3656 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3658 *key
= htons(flags
);
3660 *mask
= htons(fmask
);
3668 ovs_to_odp_ct_state(uint8_t state
)
3672 #define CS_STATE(ENUM, INDEX, NAME) \
3673 if (state & CS_##ENUM) { \
3674 odp |= OVS_CS_F_##ENUM; \
3683 odp_to_ovs_ct_state(uint32_t flags
)
3687 #define CS_STATE(ENUM, INDEX, NAME) \
3688 if (flags & OVS_CS_F_##ENUM) { \
3689 state |= CS_##ENUM; \
3698 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3700 uint32_t flags
, fmask
;
3703 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3704 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3705 mask
? &fmask
: NULL
);
3718 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3722 enum ovs_frag_type frag_type
;
3724 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3725 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3738 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3739 const struct simap
*port_names
)
3743 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3747 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3754 } else if (port_names
) {
3755 const struct simap_node
*node
;
3758 len
= strcspn(s
, ")");
3759 node
= simap_find_len(port_names
, s
, len
);
3772 /* Helper for vlan parsing. */
3773 struct ovs_key_vlan__
{
3778 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3780 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3782 if (value
>> bits
) {
3786 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3791 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3794 uint16_t key_
, mask_
;
3797 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3800 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3802 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3805 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3809 *mask
|= htons(((1U << bits
) - 1) << offset
);
3819 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3821 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3825 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3827 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3831 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3833 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3838 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3840 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3842 if (value
>> bits
) {
3846 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3851 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3854 uint32_t key_
, mask_
;
3857 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3860 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3862 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3865 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3869 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3879 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3881 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3885 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3887 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3891 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3893 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3897 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3899 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3903 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3905 const char *s_base
= s
;
3906 ovs_be16 id
= 0, id_mask
= 0;
3907 uint8_t flags
= 0, flags_mask
= 0;
3909 if (!strncmp(s
, "id=", 3)) {
3911 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3917 if (!strncmp(s
, "flags=", 6)) {
3919 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3922 if (!strncmp(s
, "))", 2)) {
3925 *key
= (flags
<< 16) | ntohs(id
);
3927 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3937 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3939 const char *s_base
= s
;
3940 struct geneve_opt
*opt
= key
->d
;
3941 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3942 int len_remain
= sizeof key
->d
;
3944 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3948 len_remain
-= sizeof *opt
;
3950 if (!strncmp(s
, "class=", 6)) {
3952 s
+= scan_be16(s
, &opt
->opt_class
,
3953 mask
? &opt_mask
->opt_class
: NULL
);
3955 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3961 if (!strncmp(s
, "type=", 5)) {
3963 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3965 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3971 if (!strncmp(s
, "len=", 4)) {
3972 uint8_t opt_len
, opt_len_mask
;
3974 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3976 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3979 opt
->length
= opt_len
/ 4;
3981 opt_mask
->length
= opt_len_mask
;
3985 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3991 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3998 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3999 data_len
, (char **)&s
)) {
4010 opt
+= 1 + data_len
/ 4;
4012 opt_mask
+= 1 + data_len
/ 4;
4014 len_remain
-= data_len
;
4019 int len
= sizeof key
->d
- len_remain
;
4033 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
4035 const uint16_t *flags
= data_
;
4037 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
4038 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
4040 if (*flags
& FLOW_TNL_F_CSUM
) {
4041 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
4043 if (*flags
& FLOW_TNL_F_OAM
) {
4044 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
4049 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
4051 const uint32_t *gbp
= data_
;
4054 size_t vxlan_opts_ofs
;
4056 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
4057 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
4058 nl_msg_end_nested(a
, vxlan_opts_ofs
);
4063 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
4065 const struct geneve_scan
*geneve
= data_
;
4067 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
4071 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
4073 unsigned long call_fn = (unsigned long)FUNC; \
4075 typedef void (*fn)(struct ofpbuf *, const void *); \
4077 func(BUF, &(DATA)); \
4079 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
4083 #define SCAN_IF(NAME) \
4084 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4085 const char *start = s; \
4090 /* Usually no special initialization is needed. */
4091 #define SCAN_BEGIN(NAME, TYPE) \
4094 memset(&skey, 0, sizeof skey); \
4095 memset(&smask, 0, sizeof smask); \
4099 /* Init as fully-masked as mask will not be scanned. */
4100 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
4103 memset(&skey, 0, sizeof skey); \
4104 memset(&smask, 0xff, sizeof smask); \
4108 /* VLAN needs special initialization. */
4109 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
4111 TYPE skey = KEY_INIT; \
4112 TYPE smask = MASK_INIT; \
4116 /* Scan unnamed entry as 'TYPE' */
4117 #define SCAN_TYPE(TYPE, KEY, MASK) \
4118 len = scan_##TYPE(s, KEY, MASK); \
4124 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4125 #define SCAN_FIELD(NAME, TYPE, FIELD) \
4126 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4127 s += strlen(NAME); \
4128 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
4132 #define SCAN_FINISH() \
4133 } while (*s++ == ',' && len != 0); \
4134 if (s[-1] != ')') { \
4138 #define SCAN_FINISH_SINGLE() \
4140 if (*s++ != ')') { \
4144 /* Beginning of nested attribute. */
4145 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
4147 size_t key_offset, mask_offset; \
4148 key_offset = nl_msg_start_nested(key, ATTR); \
4150 mask_offset = nl_msg_start_nested(mask, ATTR); \
4155 #define SCAN_END_NESTED() \
4157 nl_msg_end_nested(key, key_offset); \
4159 nl_msg_end_nested(mask, mask_offset); \
4164 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
4165 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4167 memset(&skey, 0, sizeof skey); \
4168 memset(&smask, 0xff, sizeof smask); \
4169 s += strlen(NAME); \
4170 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4171 SCAN_PUT(ATTR, FUNC); \
4175 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
4176 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
4178 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
4179 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
4181 #define SCAN_PUT(ATTR, FUNC) \
4182 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
4184 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
4186 #define SCAN_END(ATTR) \
4188 SCAN_PUT(ATTR, NULL); \
4192 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
4194 TYPE skey[CNT], smask[CNT]; \
4195 memset(&skey, 0, sizeof skey); \
4196 memset(&smask, 0, sizeof smask); \
4197 int idx = 0, cnt = CNT; \
4198 uint64_t fields = 0; \
4203 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4204 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
4205 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4206 if (fields & (1UL << field)) { \
4208 if (++idx == cnt) { \
4212 s += strlen(NAME); \
4213 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
4214 fields |= 1UL << field; \
4219 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
4220 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
4222 #define SCAN_PUT_ARRAY(ATTR, CNT) \
4223 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
4225 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
4228 #define SCAN_END_ARRAY(ATTR) \
4233 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4237 #define SCAN_END_SINGLE(ATTR) \
4238 SCAN_FINISH_SINGLE(); \
4239 SCAN_PUT(ATTR, NULL); \
4243 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4244 SCAN_BEGIN(NAME, TYPE) { \
4245 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4246 } SCAN_END_SINGLE(ATTR)
4248 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4249 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4250 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4251 } SCAN_END_SINGLE(ATTR)
4253 /* scan_port needs one extra argument. */
4254 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4255 SCAN_BEGIN(NAME, TYPE) { \
4256 len = scan_port(s, &skey, &smask, port_names); \
4261 } SCAN_END_SINGLE(ATTR)
4264 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4265 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4271 len
= odp_ufid_from_string(s
, &ufid
);
4276 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4277 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4278 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4279 OVS_KEY_ATTR_RECIRC_ID
);
4280 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4282 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4283 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4284 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4285 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4287 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
4288 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4289 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4290 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4291 SCAN_FIELD("tp_src=", be16
, src_port
);
4292 SCAN_FIELD("tp_dst=", be16
, dst_port
);
4293 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
4295 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
4296 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4297 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4298 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4299 SCAN_FIELD("tp_src=", be16
, src_port
);
4300 SCAN_FIELD("tp_dst=", be16
, dst_port
);
4301 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
4303 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4304 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4305 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4306 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4307 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4308 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4309 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4310 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4311 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4312 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4313 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4314 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4316 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4317 } SCAN_END_NESTED();
4319 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4321 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4322 SCAN_FIELD("src=", eth
, eth_src
);
4323 SCAN_FIELD("dst=", eth
, eth_dst
);
4324 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4326 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4327 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4328 SCAN_FIELD("vid=", vid
, tci
);
4329 SCAN_FIELD("pcp=", pcp
, tci
);
4330 SCAN_FIELD("cfi=", cfi
, tci
);
4331 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4333 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4335 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4336 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4337 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4338 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4339 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4340 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4342 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4343 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4344 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4345 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4346 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4347 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4348 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4349 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4351 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4352 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4353 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4354 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4355 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4356 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4357 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4358 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4359 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4361 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4362 SCAN_FIELD("src=", be16
, tcp_src
);
4363 SCAN_FIELD("dst=", be16
, tcp_dst
);
4364 } SCAN_END(OVS_KEY_ATTR_TCP
);
4366 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4368 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4369 SCAN_FIELD("src=", be16
, udp_src
);
4370 SCAN_FIELD("dst=", be16
, udp_dst
);
4371 } SCAN_END(OVS_KEY_ATTR_UDP
);
4373 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4374 SCAN_FIELD("src=", be16
, sctp_src
);
4375 SCAN_FIELD("dst=", be16
, sctp_dst
);
4376 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4378 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4379 SCAN_FIELD("type=", u8
, icmp_type
);
4380 SCAN_FIELD("code=", u8
, icmp_code
);
4381 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4383 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4384 SCAN_FIELD("type=", u8
, icmpv6_type
);
4385 SCAN_FIELD("code=", u8
, icmpv6_code
);
4386 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4388 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4389 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4390 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4391 SCAN_FIELD("op=", be16
, arp_op
);
4392 SCAN_FIELD("sha=", eth
, arp_sha
);
4393 SCAN_FIELD("tha=", eth
, arp_tha
);
4394 } SCAN_END(OVS_KEY_ATTR_ARP
);
4396 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4397 SCAN_FIELD("target=", in6_addr
, nd_target
);
4398 SCAN_FIELD("sll=", eth
, nd_sll
);
4399 SCAN_FIELD("tll=", eth
, nd_tll
);
4400 } SCAN_END(OVS_KEY_ATTR_ND
);
4402 struct packet_type
{
4406 SCAN_BEGIN("packet_type(", struct packet_type
) {
4407 SCAN_FIELD("ns=", be16
, ns
);
4408 SCAN_FIELD("id=", be16
, id
);
4409 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
4411 /* Encap open-coded. */
4412 if (!strncmp(s
, "encap(", 6)) {
4413 const char *start
= s
;
4414 size_t encap
, encap_mask
= 0;
4416 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4418 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4425 s
+= strspn(s
, delimiters
);
4428 } else if (*s
== ')') {
4432 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4440 nl_msg_end_nested(key
, encap
);
4442 nl_msg_end_nested(mask
, encap_mask
);
4451 /* Parses the string representation of a datapath flow key, in the
4452 * format output by odp_flow_key_format(). Returns 0 if successful,
4453 * otherwise a positive errno value. On success, the flow key is
4454 * appended to 'key' as a series of Netlink attributes. On failure, no
4455 * data is appended to 'key'. Either way, 'key''s data might be
4458 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4459 * to a port number. (Port names may be used instead of port numbers in
4462 * On success, the attributes appended to 'key' are individually syntactically
4463 * valid, but they may not be valid as a sequence. 'key' might, for example,
4464 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4466 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4467 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4469 const size_t old_size
= key
->size
;
4473 s
+= strspn(s
, delimiters
);
4478 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4480 key
->size
= old_size
;
4490 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4493 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4494 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4495 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4496 * must use a zero mask for the netlink frag field, and all ones mask
4498 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4500 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4501 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4502 : OVS_FRAG_TYPE_FIRST
;
4505 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4506 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4507 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4509 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4511 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4513 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4515 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4516 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4517 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4518 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4520 /* These share the same layout. */
4522 struct ovs_key_tcp tcp
;
4523 struct ovs_key_udp udp
;
4524 struct ovs_key_sctp sctp
;
4527 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4528 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4531 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4532 bool export_mask
, struct ofpbuf
*buf
)
4534 struct ovs_key_ethernet
*eth_key
;
4535 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
4537 const struct flow
*flow
= parms
->flow
;
4538 const struct flow
*mask
= parms
->mask
;
4539 const struct flow
*data
= export_mask
? mask
: flow
;
4541 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4543 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4544 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4548 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4550 if (parms
->support
.ct_state
) {
4551 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4552 ovs_to_odp_ct_state(data
->ct_state
));
4554 if (parms
->support
.ct_zone
) {
4555 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4557 if (parms
->support
.ct_mark
) {
4558 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4560 if (parms
->support
.ct_label
) {
4561 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4562 sizeof(data
->ct_label
));
4564 if (flow
->ct_nw_proto
) {
4565 if (parms
->support
.ct_orig_tuple
4566 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4567 struct ovs_key_ct_tuple_ipv4 ct
= {
4574 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
4576 } else if (parms
->support
.ct_orig_tuple6
4577 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4578 struct ovs_key_ct_tuple_ipv6 ct
= {
4585 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
4589 if (parms
->support
.recirc
) {
4590 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4591 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4594 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4595 * is not the magical value "ODPP_NONE". */
4596 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4597 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4600 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
4602 if (OVS_UNLIKELY(parms
->probe
)) {
4603 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
4605 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
4608 /* Conditionally add L2 attributes for Ethernet packets */
4609 if (flow
->packet_type
== htonl(PT_ETH
)) {
4610 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4612 get_ethernet_key(data
, eth_key
);
4614 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
4615 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
4617 if (flow
->vlans
[encaps
].tci
== htons(0)) {
4618 if (eth_type_vlan(flow
->dl_type
)) {
4619 /* If VLAN was truncated the tpid is in dl_type */
4620 tpid
= flow
->dl_type
;
4627 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4629 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
4631 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
4632 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4633 if (flow
->vlans
[encaps
].tci
== htons(0)) {
4639 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4640 /* For backwards compatibility with kernels that don't support
4641 * wildcarding, the following convention is used to encode the
4642 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4645 * -------- -------- -------
4646 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4647 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4648 * <none> 0xffff Any non-Ethernet II frame (except valid
4649 * 802.3 SNAP packet with valid eth_type).
4652 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4657 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4659 if (eth_type_vlan(flow
->dl_type
)) {
4663 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4664 struct ovs_key_ipv4
*ipv4_key
;
4666 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4668 get_ipv4_key(data
, ipv4_key
, export_mask
);
4669 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4670 struct ovs_key_ipv6
*ipv6_key
;
4672 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4674 get_ipv6_key(data
, ipv6_key
, export_mask
);
4675 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4676 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4677 struct ovs_key_arp
*arp_key
;
4679 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4681 get_arp_key(data
, arp_key
);
4682 } else if (eth_type_mpls(flow
->dl_type
)) {
4683 struct ovs_key_mpls
*mpls_key
;
4686 n
= flow_count_mpls_labels(flow
, NULL
);
4688 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4690 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4691 n
* sizeof *mpls_key
);
4692 for (i
= 0; i
< n
; i
++) {
4693 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4697 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4698 if (flow
->nw_proto
== IPPROTO_TCP
) {
4699 union ovs_key_tp
*tcp_key
;
4701 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4703 get_tp_key(data
, tcp_key
);
4704 if (data
->tcp_flags
) {
4705 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4707 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4708 union ovs_key_tp
*udp_key
;
4710 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4712 get_tp_key(data
, udp_key
);
4713 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4714 union ovs_key_tp
*sctp_key
;
4716 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4718 get_tp_key(data
, sctp_key
);
4719 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4720 && flow
->nw_proto
== IPPROTO_ICMP
) {
4721 struct ovs_key_icmp
*icmp_key
;
4723 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4725 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4726 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4727 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4728 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4729 struct ovs_key_icmpv6
*icmpv6_key
;
4731 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4732 sizeof *icmpv6_key
);
4733 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4734 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4736 if (is_nd(flow
, NULL
)
4737 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4738 * type and code are 8 bits wide. Therefore, an exact match
4739 * looks like htons(0xff), not htons(0xffff). See
4740 * xlate_wc_finish() for details. */
4741 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4742 && data
->tp_dst
== htons(0xff)))) {
4744 struct ovs_key_nd
*nd_key
;
4746 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4748 nd_key
->nd_target
= data
->nd_target
;
4749 nd_key
->nd_sll
= data
->arp_sha
;
4750 nd_key
->nd_tll
= data
->arp_tha
;
4756 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
4757 if (encap
[encaps
]) {
4758 nl_msg_end_nested(buf
, encap
[encaps
]);
4763 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4765 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4766 * capable of being expanded to allow for that much space. */
4768 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4771 odp_flow_key_from_flow__(parms
, false, buf
);
4774 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4777 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4778 * capable of being expanded to allow for that much space. */
4780 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4783 odp_flow_key_from_flow__(parms
, true, buf
);
4786 /* Generate ODP flow key from the given packet metadata */
4788 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
4790 const struct pkt_metadata
*md
= &packet
->md
;
4792 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4794 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4795 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4798 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4801 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4802 ovs_to_odp_ct_state(md
->ct_state
));
4804 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4807 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4809 if (!ovs_u128_is_zero(md
->ct_label
)) {
4810 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4811 sizeof(md
->ct_label
));
4813 if (md
->ct_orig_tuple_ipv6
) {
4814 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
4815 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
4816 &md
->ct_orig_tuple
.ipv6
,
4817 sizeof md
->ct_orig_tuple
.ipv6
);
4820 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
4821 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
4822 &md
->ct_orig_tuple
.ipv4
,
4823 sizeof md
->ct_orig_tuple
.ipv4
);
4828 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4829 * value "ODPP_NONE". */
4830 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4831 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4834 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
4835 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
4836 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
4837 pt_ns_type_be(packet
->packet_type
));
4841 /* Generate packet metadata from the given ODP flow key. */
4843 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
4844 struct dp_packet
*packet
)
4846 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4847 const struct nlattr
*nla
;
4848 struct pkt_metadata
*md
= &packet
->md
;
4849 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
4850 ovs_be16 ethertype
= 0;
4852 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4853 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4854 1u << OVS_KEY_ATTR_IN_PORT
| 1u << OVS_KEY_ATTR_ETHERTYPE
|
4855 1u << OVS_KEY_ATTR_ETHERNET
;
4857 pkt_metadata_init(md
, ODPP_NONE
);
4859 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4860 uint16_t type
= nl_attr_type(nla
);
4861 size_t len
= nl_attr_get_size(nla
);
4862 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4863 OVS_KEY_ATTR_MAX
, type
);
4865 if (len
!= expected_len
&& expected_len
>= 0) {
4870 case OVS_KEY_ATTR_RECIRC_ID
:
4871 md
->recirc_id
= nl_attr_get_u32(nla
);
4872 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4874 case OVS_KEY_ATTR_DP_HASH
:
4875 md
->dp_hash
= nl_attr_get_u32(nla
);
4876 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4878 case OVS_KEY_ATTR_PRIORITY
:
4879 md
->skb_priority
= nl_attr_get_u32(nla
);
4880 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4882 case OVS_KEY_ATTR_SKB_MARK
:
4883 md
->pkt_mark
= nl_attr_get_u32(nla
);
4884 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4886 case OVS_KEY_ATTR_CT_STATE
:
4887 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4888 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4890 case OVS_KEY_ATTR_CT_ZONE
:
4891 md
->ct_zone
= nl_attr_get_u16(nla
);
4892 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4894 case OVS_KEY_ATTR_CT_MARK
:
4895 md
->ct_mark
= nl_attr_get_u32(nla
);
4896 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4898 case OVS_KEY_ATTR_CT_LABELS
: {
4899 md
->ct_label
= nl_attr_get_u128(nla
);
4900 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4903 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
4904 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
4905 md
->ct_orig_tuple
.ipv4
= *ct
;
4906 md
->ct_orig_tuple_ipv6
= false;
4907 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
4910 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
4911 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
4913 md
->ct_orig_tuple
.ipv6
= *ct
;
4914 md
->ct_orig_tuple_ipv6
= true;
4915 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
4918 case OVS_KEY_ATTR_TUNNEL
: {
4919 enum odp_key_fitness res
;
4921 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
4922 if (res
== ODP_FIT_ERROR
) {
4923 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4924 } else if (res
== ODP_FIT_PERFECT
) {
4925 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4929 case OVS_KEY_ATTR_IN_PORT
:
4930 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4931 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4933 case OVS_KEY_ATTR_ETHERNET
:
4934 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
4935 packet_type
= htonl(PT_ETH
);
4936 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_ETHERNET
);
4938 case OVS_KEY_ATTR_ETHERTYPE
:
4939 ethertype
= nl_attr_get_be16(nla
);
4940 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_ETHERTYPE
);
4946 if (!wanted_attrs
) {
4947 break; /* Have everything. */
4951 if (packet_type
== htonl(PT_ETH
)) {
4952 packet
->packet_type
= htonl(PT_ETH
);
4953 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
4954 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
4957 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
4962 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4964 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4965 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
4969 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4970 uint64_t attrs
, int out_of_range_attr
,
4971 const struct nlattr
*key
, size_t key_len
)
4976 if (VLOG_DROP_DBG(rl
)) {
4981 for (i
= 0; i
< 64; i
++) {
4982 if (attrs
& (UINT64_C(1) << i
)) {
4983 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4985 ds_put_format(&s
, " %s",
4986 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4989 if (out_of_range_attr
) {
4990 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4993 ds_put_cstr(&s
, ": ");
4994 odp_flow_key_format(key
, key_len
, &s
);
4996 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
5001 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
5003 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5006 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
5009 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
5010 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
5011 return 0xff; /* Error. */
5014 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
5015 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
5016 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
5020 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
5021 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
5022 int *out_of_range_attrp
)
5024 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5025 const struct nlattr
*nla
;
5026 uint64_t present_attrs
;
5029 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
5031 *out_of_range_attrp
= 0;
5032 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
5033 uint16_t type
= nl_attr_type(nla
);
5034 size_t len
= nl_attr_get_size(nla
);
5035 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
5036 OVS_KEY_ATTR_MAX
, type
);
5038 if (len
!= expected_len
&& expected_len
>= 0) {
5039 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
5041 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
5042 "length %d", ovs_key_attr_to_string(type
, namebuf
,
5048 if (type
> OVS_KEY_ATTR_MAX
) {
5049 *out_of_range_attrp
= type
;
5051 if (present_attrs
& (UINT64_C(1) << type
)) {
5052 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
5054 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
5055 ovs_key_attr_to_string(type
,
5056 namebuf
, sizeof namebuf
));
5060 present_attrs
|= UINT64_C(1) << type
;
5065 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
5069 *present_attrsp
= present_attrs
;
5073 static enum odp_key_fitness
5074 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
5075 uint64_t expected_attrs
,
5076 const struct nlattr
*key
, size_t key_len
)
5078 uint64_t missing_attrs
;
5079 uint64_t extra_attrs
;
5081 missing_attrs
= expected_attrs
& ~present_attrs
;
5082 if (missing_attrs
) {
5083 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5084 log_odp_key_attributes(&rl
, "expected but not present",
5085 missing_attrs
, 0, key
, key_len
);
5086 return ODP_FIT_TOO_LITTLE
;
5089 extra_attrs
= present_attrs
& ~expected_attrs
;
5090 if (extra_attrs
|| out_of_range_attr
) {
5091 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5092 log_odp_key_attributes(&rl
, "present but not expected",
5093 extra_attrs
, out_of_range_attr
, key
, key_len
);
5094 return ODP_FIT_TOO_MUCH
;
5097 return ODP_FIT_PERFECT
;
5101 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5102 uint64_t present_attrs
, uint64_t *expected_attrs
,
5103 struct flow
*flow
, const struct flow
*src_flow
)
5105 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5106 bool is_mask
= flow
!= src_flow
;
5108 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
5109 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
5110 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5111 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
5112 ntohs(flow
->dl_type
));
5115 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
5116 flow
->dl_type
!= htons(0xffff)) {
5119 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
5122 /* Default ethertype for well-known L3 packets. */
5123 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
5124 flow
->dl_type
= htons(ETH_TYPE_IP
);
5125 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
5126 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
5127 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5128 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
5130 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
5132 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
5133 /* dl_type is mandatory for non-Ethernet packets */
5134 flow
->dl_type
= htons(0xffff);
5135 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
5136 /* See comments in odp_flow_key_from_flow__(). */
5137 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
5144 static enum odp_key_fitness
5145 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5146 uint64_t present_attrs
, int out_of_range_attr
,
5147 uint64_t expected_attrs
, struct flow
*flow
,
5148 const struct nlattr
*key
, size_t key_len
,
5149 const struct flow
*src_flow
)
5151 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5152 bool is_mask
= src_flow
!= flow
;
5153 const void *check_start
= NULL
;
5154 size_t check_len
= 0;
5155 enum ovs_key_attr expected_bit
= 0xff;
5157 if (eth_type_mpls(src_flow
->dl_type
)) {
5158 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5159 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
5161 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5162 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
5163 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
5164 int n
= size
/ sizeof(ovs_be32
);
5167 if (!size
|| size
% sizeof(ovs_be32
)) {
5168 return ODP_FIT_ERROR
;
5170 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
5171 return ODP_FIT_ERROR
;
5174 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
5175 flow
->mpls_lse
[i
] = mpls_lse
[i
];
5177 if (n
> FLOW_MAX_MPLS_LABELS
) {
5178 return ODP_FIT_TOO_MUCH
;
5182 /* BOS may be set only in the innermost label. */
5183 for (i
= 0; i
< n
- 1; i
++) {
5184 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
5185 return ODP_FIT_ERROR
;
5189 /* BOS must be set in the innermost label. */
5190 if (n
< FLOW_MAX_MPLS_LABELS
5191 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
5192 return ODP_FIT_TOO_LITTLE
;
5198 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5200 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
5202 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
5203 const struct ovs_key_ipv4
*ipv4_key
;
5205 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
5206 put_ipv4_key(ipv4_key
, flow
, is_mask
);
5207 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
5208 return ODP_FIT_ERROR
;
5211 check_start
= ipv4_key
;
5212 check_len
= sizeof *ipv4_key
;
5213 expected_bit
= OVS_KEY_ATTR_IPV4
;
5216 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5218 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
5220 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
5221 const struct ovs_key_ipv6
*ipv6_key
;
5223 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
5224 put_ipv6_key(ipv6_key
, flow
, is_mask
);
5225 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
5226 return ODP_FIT_ERROR
;
5229 check_start
= ipv6_key
;
5230 check_len
= sizeof *ipv6_key
;
5231 expected_bit
= OVS_KEY_ATTR_IPV6
;
5234 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5235 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5237 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
5239 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
5240 const struct ovs_key_arp
*arp_key
;
5242 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
5243 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
5244 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
5245 "key", ntohs(arp_key
->arp_op
));
5246 return ODP_FIT_ERROR
;
5248 put_arp_key(arp_key
, flow
);
5250 check_start
= arp_key
;
5251 check_len
= sizeof *arp_key
;
5252 expected_bit
= OVS_KEY_ATTR_ARP
;
5258 if (check_len
> 0) { /* Happens only when 'is_mask'. */
5259 if (!is_all_zeros(check_start
, check_len
) &&
5260 flow
->dl_type
!= htons(0xffff)) {
5261 return ODP_FIT_ERROR
;
5263 expected_attrs
|= UINT64_C(1) << expected_bit
;
5267 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
5268 if (src_flow
->nw_proto
== IPPROTO_TCP
5269 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5270 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5271 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5273 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
5275 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
5276 const union ovs_key_tp
*tcp_key
;
5278 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
5279 put_tp_key(tcp_key
, flow
);
5280 expected_bit
= OVS_KEY_ATTR_TCP
;
5282 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
5283 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
5284 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
5286 } else if (src_flow
->nw_proto
== IPPROTO_UDP
5287 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5288 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5289 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5291 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
5293 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
5294 const union ovs_key_tp
*udp_key
;
5296 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
5297 put_tp_key(udp_key
, flow
);
5298 expected_bit
= OVS_KEY_ATTR_UDP
;
5300 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
5301 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5302 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5303 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5305 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
5307 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
5308 const union ovs_key_tp
*sctp_key
;
5310 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
5311 put_tp_key(sctp_key
, flow
);
5312 expected_bit
= OVS_KEY_ATTR_SCTP
;
5314 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
5315 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
5316 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5318 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
5320 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
5321 const struct ovs_key_icmp
*icmp_key
;
5323 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
5324 flow
->tp_src
= htons(icmp_key
->icmp_type
);
5325 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
5326 expected_bit
= OVS_KEY_ATTR_ICMP
;
5328 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
5329 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5330 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5332 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
5334 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
5335 const struct ovs_key_icmpv6
*icmpv6_key
;
5337 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
5338 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
5339 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
5340 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
5341 if (is_nd(src_flow
, NULL
)) {
5343 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5345 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
5346 const struct ovs_key_nd
*nd_key
;
5348 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
5349 flow
->nd_target
= nd_key
->nd_target
;
5350 flow
->arp_sha
= nd_key
->nd_sll
;
5351 flow
->arp_tha
= nd_key
->nd_tll
;
5353 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
5354 * ICMP type and code are 8 bits wide. Therefore, an
5355 * exact match looks like htons(0xff), not
5356 * htons(0xffff). See xlate_wc_finish() for details.
5358 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
5359 (flow
->tp_src
!= htons(0xff) ||
5360 flow
->tp_dst
!= htons(0xff))) {
5361 return ODP_FIT_ERROR
;
5363 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5370 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
5371 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
5372 return ODP_FIT_ERROR
;
5374 expected_attrs
|= UINT64_C(1) << expected_bit
;
5379 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5383 /* Parse 802.1Q header then encapsulated L3 attributes. */
5384 static enum odp_key_fitness
5385 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5386 uint64_t present_attrs
, int out_of_range_attr
,
5387 uint64_t expected_attrs
, struct flow
*flow
,
5388 const struct nlattr
*key
, size_t key_len
,
5389 const struct flow
*src_flow
)
5391 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5392 bool is_mask
= src_flow
!= flow
;
5394 const struct nlattr
*encap
;
5395 enum odp_key_fitness encap_fitness
;
5396 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
5399 while (encaps
< flow_vlan_limit
&&
5401 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
5402 : eth_type_vlan(flow
->dl_type
))) {
5404 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5405 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5407 /* Calculate fitness of outer attributes. */
5409 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5410 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5412 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5413 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5415 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5416 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5419 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5420 expected_attrs
, key
, key_len
);
5423 * Remove the TPID from dl_type since it's not the real Ethertype. */
5424 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
5425 flow
->dl_type
= htons(0);
5426 flow
->vlans
[encaps
].tci
=
5427 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5428 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5431 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
5432 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5433 return ODP_FIT_TOO_LITTLE
;
5434 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
5435 /* Corner case for a truncated 802.1Q header. */
5436 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5437 return ODP_FIT_TOO_MUCH
;
5440 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
5441 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5442 "but CFI bit is not set",
5443 ntohs(flow
->vlans
[encaps
].tci
));
5444 return ODP_FIT_ERROR
;
5447 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5452 /* Now parse the encapsulated attributes. */
5453 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5454 attrs
, &present_attrs
, &out_of_range_attr
)) {
5455 return ODP_FIT_ERROR
;
5459 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
5461 return ODP_FIT_ERROR
;
5467 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5468 expected_attrs
, flow
, key
, key_len
,
5471 /* The overall fitness is the worse of the outer and inner attributes. */
5472 return MAX(fitness
, encap_fitness
);
5475 static enum odp_key_fitness
5476 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5477 struct flow
*flow
, const struct flow
*src_flow
)
5479 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5480 uint64_t expected_attrs
;
5481 uint64_t present_attrs
;
5482 int out_of_range_attr
;
5483 bool is_mask
= src_flow
!= flow
;
5485 memset(flow
, 0, sizeof *flow
);
5487 /* Parse attributes. */
5488 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5489 &out_of_range_attr
)) {
5490 return ODP_FIT_ERROR
;
5495 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5496 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5497 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5498 } else if (is_mask
) {
5499 /* Always exact match recirc_id if it is not specified. */
5500 flow
->recirc_id
= UINT32_MAX
;
5503 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5504 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5505 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5507 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5508 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5509 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5512 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5513 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5514 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5517 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5518 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5520 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5521 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5523 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5524 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5525 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5527 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5528 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5529 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5531 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5532 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5533 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5535 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
5536 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
5537 flow
->ct_nw_src
= ct
->ipv4_src
;
5538 flow
->ct_nw_dst
= ct
->ipv4_dst
;
5539 flow
->ct_nw_proto
= ct
->ipv4_proto
;
5540 flow
->ct_tp_src
= ct
->src_port
;
5541 flow
->ct_tp_dst
= ct
->dst_port
;
5542 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
5544 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
5545 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
5547 flow
->ct_ipv6_src
= ct
->ipv6_src
;
5548 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
5549 flow
->ct_nw_proto
= ct
->ipv6_proto
;
5550 flow
->ct_tp_src
= ct
->src_port
;
5551 flow
->ct_tp_dst
= ct
->dst_port
;
5552 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
5555 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5556 enum odp_key_fitness res
;
5558 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
5560 if (res
== ODP_FIT_ERROR
) {
5561 return ODP_FIT_ERROR
;
5562 } else if (res
== ODP_FIT_PERFECT
) {
5563 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5567 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5568 flow
->in_port
.odp_port
5569 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5570 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5571 } else if (!is_mask
) {
5572 flow
->in_port
.odp_port
= ODPP_NONE
;
5575 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
5577 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
5578 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
5579 } else if (!is_mask
) {
5580 flow
->packet_type
= htonl(PT_ETH
);
5583 /* Check for Ethernet header. */
5584 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5585 const struct ovs_key_ethernet
*eth_key
;
5587 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5588 put_ethernet_key(eth_key
, flow
);
5590 flow
->packet_type
= htonl(PT_ETH
);
5592 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5594 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
5595 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
5597 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
5600 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
5603 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5604 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5606 return ODP_FIT_ERROR
;
5610 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
5611 : eth_type_vlan(src_flow
->dl_type
)) {
5612 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5613 expected_attrs
, flow
, key
, key_len
, src_flow
);
5616 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5617 flow
->vlans
[0].tpid
= htons(0xffff);
5618 flow
->vlans
[0].tci
= htons(0xffff);
5619 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5620 flow
->vlans
[0].tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5621 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5624 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5625 expected_attrs
, flow
, key
, key_len
, src_flow
);
5628 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5629 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5630 * 'key' fits our expectations for what a flow key should contain.
5632 * The 'in_port' will be the datapath's understanding of the port. The
5633 * caller will need to translate with odp_port_to_ofp_port() if the
5634 * OpenFlow port is needed.
5636 * This function doesn't take the packet itself as an argument because none of
5637 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5638 * it is always possible to infer which additional attribute(s) should appear
5639 * by looking at the attributes for lower-level protocols, e.g. if the network
5640 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5641 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5642 * must be absent. */
5643 enum odp_key_fitness
5644 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5647 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
5650 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5651 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5652 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5653 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5654 * well 'key' fits our expectations for what a flow key should contain. */
5655 enum odp_key_fitness
5656 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5657 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
5660 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5661 &mask
->masks
, src_flow
);
5664 /* A missing mask means that the flow should be exact matched.
5665 * Generate an appropriate exact wildcard for the flow. */
5666 flow_wildcards_init_for_packet(mask
, src_flow
);
5668 return ODP_FIT_PERFECT
;
5672 /* Converts the netlink formated key/mask to match.
5673 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
5674 * disagree on the acceptable form of flow */
5676 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
5677 const struct nlattr
*mask
, size_t mask_len
,
5678 struct match
*match
)
5680 enum odp_key_fitness fitness
;
5682 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
);
5684 /* This should not happen: it indicates that
5685 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
5686 * the acceptable form of a flow. Log the problem as an error,
5687 * with enough details to enable debugging. */
5688 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5690 if (!VLOG_DROP_ERR(&rl
)) {
5694 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
5695 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
5702 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
);
5704 /* This should not happen: it indicates that
5705 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
5706 * disagree on the acceptable form of a mask. Log the problem
5707 * as an error, with enough details to enable debugging. */
5708 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5710 if (!VLOG_DROP_ERR(&rl
)) {
5714 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
5716 VLOG_ERR("internal error parsing flow mask %s (%s)",
5717 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
5727 /* Returns 'fitness' as a string, for use in debug messages. */
5729 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5732 case ODP_FIT_PERFECT
:
5734 case ODP_FIT_TOO_MUCH
:
5736 case ODP_FIT_TOO_LITTLE
:
5737 return "too_little";
5745 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5746 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5747 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5748 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5749 * null, then the return value is not meaningful.) */
5751 odp_put_userspace_action(uint32_t pid
,
5752 const void *userdata
, size_t userdata_size
,
5753 odp_port_t tunnel_out_port
,
5754 bool include_actions
,
5755 struct ofpbuf
*odp_actions
)
5757 size_t userdata_ofs
;
5760 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5761 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5763 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5765 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5766 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5769 * - The kernel rejected shorter userdata with -ERANGE.
5771 * - The kernel silently dropped userdata beyond the first 8 bytes.
5773 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5774 * separately disable features that required more than 8 bytes.) */
5775 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5776 MAX(8, userdata_size
)),
5777 userdata
, userdata_size
);
5781 if (tunnel_out_port
!= ODPP_NONE
) {
5782 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5785 if (include_actions
) {
5786 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5788 nl_msg_end_nested(odp_actions
, offset
);
5790 return userdata_ofs
;
5794 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
5796 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
5800 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
5801 const struct eth_addr
*eth_src
,
5802 const struct eth_addr
*eth_dst
)
5804 struct ovs_action_push_eth eth
;
5806 memset(ð
, 0, sizeof eth
);
5808 eth
.addresses
.eth_src
= *eth_src
;
5811 eth
.addresses
.eth_dst
= *eth_dst
;
5814 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
5819 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5820 struct ofpbuf
*odp_actions
)
5822 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5823 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5824 nl_msg_end_nested(odp_actions
, offset
);
5828 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5829 struct ovs_action_push_tnl
*data
)
5831 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5833 size
+= data
->header_len
;
5834 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5838 /* The commit_odp_actions() function and its helpers. */
5841 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5842 const void *key
, size_t key_size
)
5844 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5845 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5846 nl_msg_end_nested(odp_actions
, offset
);
5849 /* Masked set actions have a mask following the data within the netlink
5850 * attribute. The unmasked bits in the data will be cleared as the data
5851 * is copied to the action. */
5853 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5854 enum ovs_key_attr key_type
,
5855 const void *key_
, const void *mask_
, size_t key_size
)
5857 size_t offset
= nl_msg_start_nested(odp_actions
,
5858 OVS_ACTION_ATTR_SET_MASKED
);
5859 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5860 const char *key
= key_
, *mask
= mask_
;
5862 memcpy(data
+ key_size
, mask
, key_size
);
5863 /* Clear unmasked bits while copying. */
5864 while (key_size
--) {
5865 *data
++ = *key
++ & *mask
++;
5867 nl_msg_end_nested(odp_actions
, offset
);
5870 /* If any of the flow key data that ODP actions can modify are different in
5871 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5872 * 'odp_actions' that change the flow tunneling information in key from
5873 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5874 * same way. In other words, operates the same as commit_odp_actions(), but
5875 * only on tunneling information. */
5877 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5878 struct ofpbuf
*odp_actions
)
5880 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5881 * must have non-zero ipv6_dst. */
5882 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5883 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5886 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5887 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5892 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5893 const void *key
, void *base
, void *mask
, size_t size
,
5894 struct ofpbuf
*odp_actions
)
5896 if (memcmp(key
, base
, size
)) {
5897 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5899 if (use_masked_set
&& !fully_masked
) {
5900 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5902 if (!fully_masked
) {
5903 memset(mask
, 0xff, size
);
5905 commit_set_action(odp_actions
, attr
, key
, size
);
5907 memcpy(base
, key
, size
);
5910 /* Mask bits are set when we have either read or set the corresponding
5911 * values. Masked bits will be exact-matched, no need to set them
5912 * if the value did not actually change. */
5918 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5920 eth
->eth_src
= flow
->dl_src
;
5921 eth
->eth_dst
= flow
->dl_dst
;
5925 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5927 flow
->dl_src
= eth
->eth_src
;
5928 flow
->dl_dst
= eth
->eth_dst
;
5932 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5933 struct ofpbuf
*odp_actions
,
5934 struct flow_wildcards
*wc
,
5937 struct ovs_key_ethernet key
, base
, mask
;
5939 get_ethernet_key(flow
, &key
);
5940 get_ethernet_key(base_flow
, &base
);
5941 get_ethernet_key(&wc
->masks
, &mask
);
5943 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5944 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5945 put_ethernet_key(&base
, base_flow
);
5946 put_ethernet_key(&mask
, &wc
->masks
);
5951 commit_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
5952 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5955 if (flow
->packet_type
== htonl(PT_ETH
)) {
5956 if (base_flow
->packet_type
!= htonl(PT_ETH
)) {
5957 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
, &flow
->dl_dst
);
5958 base_flow
->packet_type
= flow
->packet_type
;
5959 base_flow
->dl_src
= flow
->dl_src
;
5960 base_flow
->dl_dst
= flow
->dl_dst
;
5962 commit_set_ether_addr_action(flow
, base_flow
, odp_actions
, wc
,
5966 if (base_flow
->packet_type
== htonl(PT_ETH
)) {
5967 odp_put_pop_eth_action(odp_actions
);
5968 base_flow
->packet_type
= flow
->packet_type
;
5974 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
5975 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5977 int base_n
= flow_count_vlan_headers(base
);
5978 int flow_n
= flow_count_vlan_headers(flow
);
5979 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
5981 /* Pop all mismatching vlan of base, push those of flow */
5982 for (; base_n
>= 0; base_n
--) {
5983 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5984 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
5987 for (; flow_n
>= 0; flow_n
--) {
5988 struct ovs_action_push_vlan vlan
;
5990 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
5991 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
5992 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5993 &vlan
, sizeof vlan
);
5995 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
5998 /* Wildcarding already done at action translation time. */
6000 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
6001 struct ofpbuf
*odp_actions
)
6003 int base_n
= flow_count_mpls_labels(base
, NULL
);
6004 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
6005 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
6008 while (base_n
> common_n
) {
6009 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
6010 /* If there is only one more LSE in base than there are common
6011 * between base and flow; and flow has at least one more LSE than
6012 * is common then the topmost LSE of base may be updated using
6014 struct ovs_key_mpls mpls_key
;
6016 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
6017 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
6018 &mpls_key
, sizeof mpls_key
);
6019 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
6022 /* Otherwise, if there more LSEs in base than are common between
6023 * base and flow then pop the topmost one. */
6027 /* If all the LSEs are to be popped and this is not the outermost
6028 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
6029 * POP_MPLS action instead of flow->dl_type.
6031 * This is because the POP_MPLS action requires its ethertype
6032 * argument to be an MPLS ethernet type but in this case
6033 * flow->dl_type will be a non-MPLS ethernet type.
6035 * When the final POP_MPLS action occurs it use flow->dl_type and
6036 * the and the resulting packet will have the desired dl_type. */
6037 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
6038 dl_type
= htons(ETH_TYPE_MPLS
);
6040 dl_type
= flow
->dl_type
;
6042 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
6043 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
6049 /* If, after the above popping and setting, there are more LSEs in flow
6050 * than base then some LSEs need to be pushed. */
6051 while (base_n
< flow_n
) {
6052 struct ovs_action_push_mpls
*mpls
;
6054 mpls
= nl_msg_put_unspec_zero(odp_actions
,
6055 OVS_ACTION_ATTR_PUSH_MPLS
,
6057 mpls
->mpls_ethertype
= flow
->dl_type
;
6058 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
6059 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
6060 * headers if the flow is restored later due to returning from a patch
6061 * port or group bucket. */
6062 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
6063 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
6069 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
6071 ipv4
->ipv4_src
= flow
->nw_src
;
6072 ipv4
->ipv4_dst
= flow
->nw_dst
;
6073 ipv4
->ipv4_proto
= flow
->nw_proto
;
6074 ipv4
->ipv4_tos
= flow
->nw_tos
;
6075 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
6076 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
6080 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
6082 flow
->nw_src
= ipv4
->ipv4_src
;
6083 flow
->nw_dst
= ipv4
->ipv4_dst
;
6084 flow
->nw_proto
= ipv4
->ipv4_proto
;
6085 flow
->nw_tos
= ipv4
->ipv4_tos
;
6086 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
6087 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
6091 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
6092 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6095 struct ovs_key_ipv4 key
, mask
, base
;
6097 /* Check that nw_proto and nw_frag remain unchanged. */
6098 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
6099 flow
->nw_frag
== base_flow
->nw_frag
);
6101 get_ipv4_key(flow
, &key
, false);
6102 get_ipv4_key(base_flow
, &base
, false);
6103 get_ipv4_key(&wc
->masks
, &mask
, true);
6104 mask
.ipv4_proto
= 0; /* Not writeable. */
6105 mask
.ipv4_frag
= 0; /* Not writable. */
6107 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6109 put_ipv4_key(&base
, base_flow
, false);
6110 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
6111 put_ipv4_key(&mask
, &wc
->masks
, true);
6117 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
6119 ipv6
->ipv6_src
= flow
->ipv6_src
;
6120 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
6121 ipv6
->ipv6_label
= flow
->ipv6_label
;
6122 ipv6
->ipv6_proto
= flow
->nw_proto
;
6123 ipv6
->ipv6_tclass
= flow
->nw_tos
;
6124 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
6125 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
6129 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
6131 flow
->ipv6_src
= ipv6
->ipv6_src
;
6132 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
6133 flow
->ipv6_label
= ipv6
->ipv6_label
;
6134 flow
->nw_proto
= ipv6
->ipv6_proto
;
6135 flow
->nw_tos
= ipv6
->ipv6_tclass
;
6136 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
6137 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
6141 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
6142 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6145 struct ovs_key_ipv6 key
, mask
, base
;
6147 /* Check that nw_proto and nw_frag remain unchanged. */
6148 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
6149 flow
->nw_frag
== base_flow
->nw_frag
);
6151 get_ipv6_key(flow
, &key
, false);
6152 get_ipv6_key(base_flow
, &base
, false);
6153 get_ipv6_key(&wc
->masks
, &mask
, true);
6154 mask
.ipv6_proto
= 0; /* Not writeable. */
6155 mask
.ipv6_frag
= 0; /* Not writable. */
6157 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6159 put_ipv6_key(&base
, base_flow
, false);
6160 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
6161 put_ipv6_key(&mask
, &wc
->masks
, true);
6167 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
6169 /* ARP key has padding, clear it. */
6170 memset(arp
, 0, sizeof *arp
);
6172 arp
->arp_sip
= flow
->nw_src
;
6173 arp
->arp_tip
= flow
->nw_dst
;
6174 arp
->arp_op
= htons(flow
->nw_proto
);
6175 arp
->arp_sha
= flow
->arp_sha
;
6176 arp
->arp_tha
= flow
->arp_tha
;
6180 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
6182 flow
->nw_src
= arp
->arp_sip
;
6183 flow
->nw_dst
= arp
->arp_tip
;
6184 flow
->nw_proto
= ntohs(arp
->arp_op
);
6185 flow
->arp_sha
= arp
->arp_sha
;
6186 flow
->arp_tha
= arp
->arp_tha
;
6189 static enum slow_path_reason
6190 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
6191 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
6193 struct ovs_key_arp key
, mask
, base
;
6195 get_arp_key(flow
, &key
);
6196 get_arp_key(base_flow
, &base
);
6197 get_arp_key(&wc
->masks
, &mask
);
6199 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
6201 put_arp_key(&base
, base_flow
);
6202 put_arp_key(&mask
, &wc
->masks
);
6209 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
6211 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6212 icmp
->icmp_type
= ntohs(flow
->tp_src
);
6213 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
6217 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
6219 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6220 flow
->tp_src
= htons(icmp
->icmp_type
);
6221 flow
->tp_dst
= htons(icmp
->icmp_code
);
6224 static enum slow_path_reason
6225 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
6226 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
6228 struct ovs_key_icmp key
, mask
, base
;
6229 enum ovs_key_attr attr
;
6231 if (is_icmpv4(flow
, NULL
)) {
6232 attr
= OVS_KEY_ATTR_ICMP
;
6233 } else if (is_icmpv6(flow
, NULL
)) {
6234 attr
= OVS_KEY_ATTR_ICMPV6
;
6239 get_icmp_key(flow
, &key
);
6240 get_icmp_key(base_flow
, &base
);
6241 get_icmp_key(&wc
->masks
, &mask
);
6243 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
6244 put_icmp_key(&base
, base_flow
);
6245 put_icmp_key(&mask
, &wc
->masks
);
6252 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
6254 nd
->nd_target
= flow
->nd_target
;
6255 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6256 nd
->nd_sll
= flow
->arp_sha
;
6257 nd
->nd_tll
= flow
->arp_tha
;
6261 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
6263 flow
->nd_target
= nd
->nd_target
;
6264 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6265 flow
->arp_sha
= nd
->nd_sll
;
6266 flow
->arp_tha
= nd
->nd_tll
;
6269 static enum slow_path_reason
6270 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
6271 struct ofpbuf
*odp_actions
,
6272 struct flow_wildcards
*wc
, bool use_masked
)
6274 struct ovs_key_nd key
, mask
, base
;
6276 get_nd_key(flow
, &key
);
6277 get_nd_key(base_flow
, &base
);
6278 get_nd_key(&wc
->masks
, &mask
);
6280 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6282 put_nd_key(&base
, base_flow
);
6283 put_nd_key(&mask
, &wc
->masks
);
6290 static enum slow_path_reason
6291 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
6292 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6295 /* Check if 'flow' really has an L3 header. */
6296 if (!flow
->nw_proto
) {
6300 switch (ntohs(base
->dl_type
)) {
6302 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
6306 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
6307 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
6310 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
6316 /* TCP, UDP, and SCTP keys have the same layout. */
6317 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
6318 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
6321 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
6323 tp
->tcp
.tcp_src
= flow
->tp_src
;
6324 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
6328 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
6330 flow
->tp_src
= tp
->tcp
.tcp_src
;
6331 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
6335 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
6336 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6339 enum ovs_key_attr key_type
;
6340 union ovs_key_tp key
, mask
, base
;
6342 /* Check if 'flow' really has an L3 header. */
6343 if (!flow
->nw_proto
) {
6347 if (!is_ip_any(base_flow
)) {
6351 if (flow
->nw_proto
== IPPROTO_TCP
) {
6352 key_type
= OVS_KEY_ATTR_TCP
;
6353 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
6354 key_type
= OVS_KEY_ATTR_UDP
;
6355 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
6356 key_type
= OVS_KEY_ATTR_SCTP
;
6361 get_tp_key(flow
, &key
);
6362 get_tp_key(base_flow
, &base
);
6363 get_tp_key(&wc
->masks
, &mask
);
6365 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6367 put_tp_key(&base
, base_flow
);
6368 put_tp_key(&mask
, &wc
->masks
);
6373 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
6374 struct ofpbuf
*odp_actions
,
6375 struct flow_wildcards
*wc
,
6378 uint32_t key
, mask
, base
;
6380 key
= flow
->skb_priority
;
6381 base
= base_flow
->skb_priority
;
6382 mask
= wc
->masks
.skb_priority
;
6384 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
6385 sizeof key
, odp_actions
)) {
6386 base_flow
->skb_priority
= base
;
6387 wc
->masks
.skb_priority
= mask
;
6392 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
6393 struct ofpbuf
*odp_actions
,
6394 struct flow_wildcards
*wc
,
6397 uint32_t key
, mask
, base
;
6399 key
= flow
->pkt_mark
;
6400 base
= base_flow
->pkt_mark
;
6401 mask
= wc
->masks
.pkt_mark
;
6403 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
6404 sizeof key
, odp_actions
)) {
6405 base_flow
->pkt_mark
= base
;
6406 wc
->masks
.pkt_mark
= mask
;
6410 /* If any of the flow key data that ODP actions can modify are different in
6411 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
6412 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
6413 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
6414 * in addition to this function if needed. Sets fields in 'wc' that are
6415 * used as part of the action.
6417 * Returns a reason to force processing the flow's packets into the userspace
6418 * slow path, if there is one, otherwise 0. */
6419 enum slow_path_reason
6420 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
6421 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6424 enum slow_path_reason slow1
, slow2
;
6425 bool mpls_done
= false;
6427 commit_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
6428 /* Make packet a non-MPLS packet before committing L3/4 actions,
6429 * which would otherwise do nothing. */
6430 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
6431 commit_mpls_action(flow
, base
, odp_actions
);
6434 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
6435 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
6436 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
6438 commit_mpls_action(flow
, base
, odp_actions
);
6440 commit_vlan_action(flow
, base
, odp_actions
, wc
);
6441 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
6442 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
6444 return slow1
? slow1
: slow2
;