2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
29 #include "byte-order.h"
32 #include "openvswitch/dynamic-string.h"
35 #include "openvswitch/ofpbuf.h"
39 #include "tun-metadata.h"
40 #include "unaligned.h"
43 #include "openvswitch/vlog.h"
45 VLOG_DEFINE_THIS_MODULE(odp_util
);
47 /* The interface between userspace and kernel uses an "OVS_*" prefix.
48 * Since this is fairly non-specific for the OVS userspace components,
49 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
50 * interactions with the datapath.
53 /* The set of characters that may separate one action or one key attribute
55 static const char *delimiters
= ", \t\r\n";
56 static const char *delimiters_end
= ", \t\r\n)";
60 const struct attr_len_tbl
*next
;
63 #define ATTR_LEN_INVALID -1
64 #define ATTR_LEN_VARIABLE -2
65 #define ATTR_LEN_NESTED -3
67 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
68 struct ofpbuf
*, struct ofpbuf
*);
69 static void format_odp_key_attr(const struct nlattr
*a
,
70 const struct nlattr
*ma
,
71 const struct hmap
*portno_names
, struct ds
*ds
,
75 struct geneve_opt d
[63];
79 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
80 struct geneve_scan
*mask
);
81 static void format_geneve_opts(const struct geneve_opt
*opt
,
82 const struct geneve_opt
*mask
, int opts_len
,
83 struct ds
*, bool verbose
);
85 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
86 int max
, struct ofpbuf
*,
87 const struct nlattr
*key
);
88 static void format_u128(struct ds
*ds
, const ovs_u128
*value
,
89 const ovs_u128
*mask
, bool verbose
);
90 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
92 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
95 * - For an action whose argument has a fixed length, returned that
96 * nonnegative length in bytes.
98 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
100 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
102 odp_action_len(uint16_t type
)
104 if (type
> OVS_ACTION_ATTR_MAX
) {
108 switch ((enum ovs_action_attr
) type
) {
109 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
110 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
111 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
112 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
113 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
114 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
115 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
116 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
117 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
118 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
119 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
120 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
121 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
122 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
123 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
125 case OVS_ACTION_ATTR_UNSPEC
:
126 case __OVS_ACTION_ATTR_MAX
:
127 return ATTR_LEN_INVALID
;
130 return ATTR_LEN_INVALID
;
133 /* Returns a string form of 'attr'. The return value is either a statically
134 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
135 * should be at least OVS_KEY_ATTR_BUFSIZE. */
136 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
138 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
141 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
142 case OVS_KEY_ATTR_ENCAP
: return "encap";
143 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
144 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
145 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
146 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
147 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
148 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
149 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
150 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
151 case OVS_KEY_ATTR_ETHERNET
: return "eth";
152 case OVS_KEY_ATTR_VLAN
: return "vlan";
153 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
154 case OVS_KEY_ATTR_IPV4
: return "ipv4";
155 case OVS_KEY_ATTR_IPV6
: return "ipv6";
156 case OVS_KEY_ATTR_TCP
: return "tcp";
157 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
158 case OVS_KEY_ATTR_UDP
: return "udp";
159 case OVS_KEY_ATTR_SCTP
: return "sctp";
160 case OVS_KEY_ATTR_ICMP
: return "icmp";
161 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
162 case OVS_KEY_ATTR_ARP
: return "arp";
163 case OVS_KEY_ATTR_ND
: return "nd";
164 case OVS_KEY_ATTR_MPLS
: return "mpls";
165 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
166 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
168 case __OVS_KEY_ATTR_MAX
:
170 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
176 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
178 size_t len
= nl_attr_get_size(a
);
180 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
182 const uint8_t *unspec
;
185 unspec
= nl_attr_get(a
);
186 for (i
= 0; i
< len
; i
++) {
187 ds_put_char(ds
, i
? ' ': '(');
188 ds_put_format(ds
, "%02x", unspec
[i
]);
190 ds_put_char(ds
, ')');
195 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
197 static const struct nl_policy ovs_sample_policy
[] = {
198 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
199 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
201 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
203 const struct nlattr
*nla_acts
;
206 ds_put_cstr(ds
, "sample");
208 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
209 ds_put_cstr(ds
, "(error)");
213 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
216 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
218 ds_put_cstr(ds
, "actions(");
219 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
220 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
221 format_odp_actions(ds
, nla_acts
, len
);
222 ds_put_format(ds
, "))");
226 slow_path_reason_to_string(uint32_t reason
)
228 switch ((enum slow_path_reason
) reason
) {
229 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
238 slow_path_reason_to_explanation(enum slow_path_reason reason
)
241 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
250 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
251 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
253 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
254 res_flags
, allowed
, res_mask
);
258 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
260 static const struct nl_policy ovs_userspace_policy
[] = {
261 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
262 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
264 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
266 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
269 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
270 const struct nlattr
*userdata_attr
;
271 const struct nlattr
*tunnel_out_port_attr
;
273 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
274 ds_put_cstr(ds
, "userspace(error)");
278 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
279 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
281 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
284 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
285 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
286 bool userdata_unspec
= true;
287 union user_action_cookie cookie
;
289 if (userdata_len
>= sizeof cookie
.type
290 && userdata_len
<= sizeof cookie
) {
292 memset(&cookie
, 0, sizeof cookie
);
293 memcpy(&cookie
, userdata
, userdata_len
);
295 userdata_unspec
= false;
297 if (userdata_len
== sizeof cookie
.sflow
298 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
299 ds_put_format(ds
, ",sFlow("
300 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
301 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
302 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
303 cookie
.sflow
.output
);
304 } else if (userdata_len
== sizeof cookie
.slow_path
305 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
306 ds_put_cstr(ds
, ",slow_path(");
307 format_flags(ds
, slow_path_reason_to_string
,
308 cookie
.slow_path
.reason
, ',');
309 ds_put_format(ds
, ")");
310 } else if (userdata_len
== sizeof cookie
.flow_sample
311 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
312 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
313 ",collector_set_id=%"PRIu32
314 ",obs_domain_id=%"PRIu32
315 ",obs_point_id=%"PRIu32
316 ",output_port=%"PRIu32
")",
317 cookie
.flow_sample
.probability
,
318 cookie
.flow_sample
.collector_set_id
,
319 cookie
.flow_sample
.obs_domain_id
,
320 cookie
.flow_sample
.obs_point_id
,
321 cookie
.flow_sample
.output_odp_port
);
322 } else if (userdata_len
>= sizeof cookie
.ipfix
323 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
324 ds_put_format(ds
, ",ipfix(output_port=%"PRIu32
")",
325 cookie
.ipfix
.output_odp_port
);
327 userdata_unspec
= true;
331 if (userdata_unspec
) {
333 ds_put_format(ds
, ",userdata(");
334 for (i
= 0; i
< userdata_len
; i
++) {
335 ds_put_format(ds
, "%02x", userdata
[i
]);
337 ds_put_char(ds
, ')');
341 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
342 ds_put_cstr(ds
, ",actions");
345 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
346 if (tunnel_out_port_attr
) {
347 ds_put_format(ds
, ",tunnel_out_port=%"PRIu32
,
348 nl_attr_get_u32(tunnel_out_port_attr
));
351 ds_put_char(ds
, ')');
355 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
357 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
358 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
359 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
360 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
362 ds_put_char(ds
, ',');
364 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
365 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
366 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
367 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
369 ds_put_char(ds
, ',');
371 if (!(tci
& htons(VLAN_CFI
))) {
372 ds_put_cstr(ds
, "cfi=0");
373 ds_put_char(ds
, ',');
379 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
381 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
382 mpls_lse_to_label(mpls_lse
),
383 mpls_lse_to_tc(mpls_lse
),
384 mpls_lse_to_ttl(mpls_lse
),
385 mpls_lse_to_bos(mpls_lse
));
389 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
390 const struct ovs_key_mpls
*mpls_mask
, int n
)
392 for (int i
= 0; i
< n
; i
++) {
393 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
395 if (mpls_mask
== NULL
) {
396 format_mpls_lse(ds
, key
);
398 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
400 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
401 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
402 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
403 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
404 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
406 ds_put_char(ds
, ',');
412 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
414 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
418 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
420 ds_put_format(ds
, "hash(");
422 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
423 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
425 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
428 ds_put_format(ds
, ")");
432 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
434 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
435 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
436 ntohs(udp
->udp_csum
));
442 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
444 const struct eth_header
*eth
;
447 const struct udp_header
*udp
;
449 eth
= (const struct eth_header
*)data
->header
;
454 ds_put_format(ds
, "header(size=%"PRIu8
",type=%"PRIu8
",eth(dst=",
455 data
->header_len
, data
->tnl_type
);
456 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
457 ds_put_format(ds
, ",src=");
458 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
459 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
461 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
463 const struct ip_header
*ip
;
464 ip
= (const struct ip_header
*) l3
;
465 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
466 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
467 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
468 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
469 ip
->ip_proto
, ip
->ip_tos
,
471 ntohs(ip
->ip_frag_off
));
474 const struct ip6_hdr
*ip6
;
475 ip6
= (const struct ip6_hdr
*) l3
;
476 ds_put_format(ds
, "ipv6(src=");
477 ipv6_format_addr(&ip6
->ip6_src
, ds
);
478 ds_put_format(ds
, ",dst=");
479 ipv6_format_addr(&ip6
->ip6_dst
, ds
);
480 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx8
481 ",hlimit=%"PRIu8
"),",
482 ntohl(ip6
->ip6_flow
) & IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
483 (ntohl(ip6
->ip6_flow
) >> 20) & 0xff, ip6
->ip6_hlim
);
487 udp
= (const struct udp_header
*) l4
;
489 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
490 const struct vxlanhdr
*vxh
;
492 vxh
= format_udp_tnl_push_header(ds
, udp
);
494 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
495 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
496 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
497 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
498 const struct genevehdr
*gnh
;
500 gnh
= format_udp_tnl_push_header(ds
, udp
);
502 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
503 gnh
->oam
? "oam," : "",
504 gnh
->critical
? "crit," : "",
505 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
508 ds_put_cstr(ds
, ",options(");
509 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
511 ds_put_char(ds
, ')');
514 ds_put_char(ds
, ')');
515 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
516 const struct gre_base_hdr
*greh
;
517 ovs_16aligned_be32
*options
;
519 greh
= (const struct gre_base_hdr
*) l4
;
521 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
522 ntohs(greh
->flags
), ntohs(greh
->protocol
));
523 options
= (ovs_16aligned_be32
*)(greh
+ 1);
524 if (greh
->flags
& htons(GRE_CSUM
)) {
525 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
528 if (greh
->flags
& htons(GRE_KEY
)) {
529 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
532 if (greh
->flags
& htons(GRE_SEQ
)) {
533 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
536 ds_put_format(ds
, ")");
538 ds_put_format(ds
, ")");
542 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
)
544 struct ovs_action_push_tnl
*data
;
546 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
548 ds_put_format(ds
, "tnl_push(tnl_port(%"PRIu32
"),", data
->tnl_port
);
549 format_odp_tnl_push_header(ds
, data
);
550 ds_put_format(ds
, ",out_port(%"PRIu32
"))", data
->out_port
);
553 static const struct nl_policy ovs_nat_policy
[] = {
554 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
555 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
556 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
557 .min_len
= sizeof(struct in_addr
),
558 .max_len
= sizeof(struct in6_addr
)},
559 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
560 .min_len
= sizeof(struct in_addr
),
561 .max_len
= sizeof(struct in6_addr
)},
562 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
563 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
564 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
565 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
566 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
570 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
572 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
574 ovs_be32 ip_min
, ip_max
;
575 struct in6_addr ip6_min
, ip6_max
;
576 uint16_t proto_min
, proto_max
;
578 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
579 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
582 /* If no type, then nothing else either. */
583 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
584 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
585 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
586 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
587 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
588 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
591 /* Both SNAT & DNAT may not be specified. */
592 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
593 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
596 /* proto may not appear without ip. */
597 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
598 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
601 /* MAX may not appear without MIN. */
602 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
603 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
604 ds_put_cstr(ds
, "nat(error: range max without min.)");
607 /* Address sizes must match. */
608 if ((a
[OVS_NAT_ATTR_IP_MIN
]
609 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
610 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
611 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
612 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
613 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
614 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
618 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
619 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
620 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
621 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
622 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
623 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
624 if (addr_len
== sizeof ip6_min
) {
625 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
626 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
628 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
629 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
632 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
633 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
634 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
635 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
637 if ((addr_len
== sizeof(ovs_be32
)
638 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
639 || (addr_len
== sizeof(struct in6_addr
)
640 && !ipv6_mask_is_any(&ip6_max
)
641 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
642 || (proto_max
&& proto_min
> proto_max
)) {
643 ds_put_cstr(ds
, "nat(range error)");
647 ds_put_cstr(ds
, "nat");
648 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
649 ds_put_char(ds
, '(');
650 if (a
[OVS_NAT_ATTR_SRC
]) {
651 ds_put_cstr(ds
, "src");
652 } else if (a
[OVS_NAT_ATTR_DST
]) {
653 ds_put_cstr(ds
, "dst");
657 ds_put_cstr(ds
, "=");
659 if (addr_len
== sizeof ip_min
) {
660 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
662 if (ip_max
&& ip_max
!= ip_min
) {
663 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
665 } else if (addr_len
== sizeof ip6_min
) {
666 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
668 if (!ipv6_mask_is_any(&ip6_max
) &&
669 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
670 ds_put_char(ds
, '-');
671 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
675 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
677 if (proto_max
&& proto_max
!= proto_min
) {
678 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
682 ds_put_char(ds
, ',');
683 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
684 ds_put_cstr(ds
, "persistent,");
686 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
687 ds_put_cstr(ds
, "hash,");
689 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
690 ds_put_cstr(ds
, "random,");
693 ds_put_char(ds
, ')');
697 static const struct nl_policy ovs_conntrack_policy
[] = {
698 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
699 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
700 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
701 .min_len
= sizeof(uint32_t) * 2 },
702 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
703 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
704 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
705 .min_len
= 1, .max_len
= 16 },
706 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
710 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
712 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
713 const ovs_u128
*label
;
714 const uint32_t *mark
;
718 const struct nlattr
*nat
;
720 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
721 ds_put_cstr(ds
, "ct(error)");
725 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
726 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
727 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
728 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
729 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
730 nat
= a
[OVS_CT_ATTR_NAT
];
732 ds_put_format(ds
, "ct");
733 if (commit
|| zone
|| mark
|| label
|| helper
|| nat
) {
734 ds_put_cstr(ds
, "(");
736 ds_put_format(ds
, "commit,");
739 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
742 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
746 ds_put_format(ds
, "label=");
747 format_u128(ds
, label
, label
+ 1, true);
748 ds_put_char(ds
, ',');
751 ds_put_format(ds
, "helper=%s,", helper
);
754 format_odp_ct_nat(ds
, nat
);
757 ds_put_cstr(ds
, ")");
762 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
765 enum ovs_action_attr type
= nl_attr_type(a
);
768 expected_len
= odp_action_len(nl_attr_type(a
));
769 if (expected_len
!= ATTR_LEN_VARIABLE
&&
770 nl_attr_get_size(a
) != expected_len
) {
771 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
772 nl_attr_get_size(a
), expected_len
);
773 format_generic_odp_action(ds
, a
);
778 case OVS_ACTION_ATTR_OUTPUT
:
779 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
781 case OVS_ACTION_ATTR_TRUNC
: {
782 const struct ovs_action_trunc
*trunc
=
783 nl_attr_get_unspec(a
, sizeof *trunc
);
785 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
789 case OVS_ACTION_ATTR_TUNNEL_POP
:
790 ds_put_format(ds
, "tnl_pop(%"PRIu32
")", nl_attr_get_u32(a
));
792 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
793 format_odp_tnl_push_action(ds
, a
);
795 case OVS_ACTION_ATTR_USERSPACE
:
796 format_odp_userspace_action(ds
, a
);
798 case OVS_ACTION_ATTR_RECIRC
:
799 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
801 case OVS_ACTION_ATTR_HASH
:
802 format_odp_hash_action(ds
, nl_attr_get(a
));
804 case OVS_ACTION_ATTR_SET_MASKED
:
806 size
= nl_attr_get_size(a
) / 2;
807 ds_put_cstr(ds
, "set(");
809 /* Masked set action not supported for tunnel key, which is bigger. */
810 if (size
<= sizeof(struct ovs_key_ipv6
)) {
811 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
812 sizeof(struct nlattr
))];
813 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
814 sizeof(struct nlattr
))];
816 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
817 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
818 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
819 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
820 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
822 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
824 ds_put_cstr(ds
, ")");
826 case OVS_ACTION_ATTR_SET
:
827 ds_put_cstr(ds
, "set(");
828 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
829 ds_put_cstr(ds
, ")");
831 case OVS_ACTION_ATTR_PUSH_VLAN
: {
832 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
833 ds_put_cstr(ds
, "push_vlan(");
834 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
835 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
837 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
838 ds_put_char(ds
, ')');
841 case OVS_ACTION_ATTR_POP_VLAN
:
842 ds_put_cstr(ds
, "pop_vlan");
844 case OVS_ACTION_ATTR_PUSH_MPLS
: {
845 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
846 ds_put_cstr(ds
, "push_mpls(");
847 format_mpls_lse(ds
, mpls
->mpls_lse
);
848 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
851 case OVS_ACTION_ATTR_POP_MPLS
: {
852 ovs_be16 ethertype
= nl_attr_get_be16(a
);
853 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
856 case OVS_ACTION_ATTR_SAMPLE
:
857 format_odp_sample_action(ds
, a
);
859 case OVS_ACTION_ATTR_CT
:
860 format_odp_conntrack_action(ds
, a
);
862 case OVS_ACTION_ATTR_UNSPEC
:
863 case __OVS_ACTION_ATTR_MAX
:
865 format_generic_odp_action(ds
, a
);
871 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
875 const struct nlattr
*a
;
878 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
880 ds_put_char(ds
, ',');
882 format_odp_action(ds
, a
);
887 if (left
== actions_len
) {
888 ds_put_cstr(ds
, "<empty>");
890 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
891 for (i
= 0; i
< left
; i
++) {
892 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
894 ds_put_char(ds
, ')');
897 ds_put_cstr(ds
, "drop");
901 /* Separate out parse_odp_userspace_action() function. */
903 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
906 union user_action_cookie cookie
;
908 odp_port_t tunnel_out_port
;
910 void *user_data
= NULL
;
911 size_t user_data_size
= 0;
912 bool include_actions
= false;
915 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
919 ofpbuf_init(&buf
, 16);
923 uint32_t probability
;
924 uint32_t collector_set_id
;
925 uint32_t obs_domain_id
;
926 uint32_t obs_point_id
;
929 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
930 "pcp=%i,output=%"SCNi32
")%n",
931 &vid
, &pcp
, &output
, &n1
)) {
935 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
940 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
941 cookie
.sflow
.vlan_tci
= htons(tci
);
942 cookie
.sflow
.output
= output
;
944 user_data_size
= sizeof cookie
.sflow
;
945 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
948 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
949 cookie
.slow_path
.unused
= 0;
950 cookie
.slow_path
.reason
= 0;
952 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
953 &cookie
.slow_path
.reason
,
954 SLOW_PATH_REASON_MASK
, NULL
);
955 if (res
< 0 || s
[n
+ res
] != ')') {
961 user_data_size
= sizeof cookie
.slow_path
;
962 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
963 "collector_set_id=%"SCNi32
","
964 "obs_domain_id=%"SCNi32
","
965 "obs_point_id=%"SCNi32
","
966 "output_port=%"SCNi32
")%n",
967 &probability
, &collector_set_id
,
968 &obs_domain_id
, &obs_point_id
,
972 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
973 cookie
.flow_sample
.probability
= probability
;
974 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
975 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
976 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
977 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
979 user_data_size
= sizeof cookie
.flow_sample
;
980 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
983 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
984 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
986 user_data_size
= sizeof cookie
.ipfix
;
987 } else if (ovs_scan(&s
[n
], ",userdata(%n",
992 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
997 user_data
= buf
.data
;
998 user_data_size
= buf
.size
;
1005 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1007 include_actions
= true;
1013 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1014 &tunnel_out_port
, &n1
)) {
1015 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1016 tunnel_out_port
, include_actions
, actions
);
1018 } else if (s
[n
] == ')') {
1019 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1020 ODPP_NONE
, include_actions
, actions
);
1027 ofpbuf_uninit(&buf
);
1032 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1034 struct eth_header
*eth
;
1035 struct ip_header
*ip
;
1036 struct ovs_16aligned_ip6_hdr
*ip6
;
1037 struct udp_header
*udp
;
1038 struct gre_base_hdr
*greh
;
1039 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1041 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1045 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1048 eth
= (struct eth_header
*) data
->header
;
1049 l3
= (data
->header
+ sizeof *eth
);
1050 ip
= (struct ip_header
*) l3
;
1051 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1052 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1053 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1056 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1060 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1061 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1064 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1067 eth
->eth_type
= htons(dl_type
);
1069 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1071 uint16_t ip_frag_off
;
1072 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1073 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1076 &ip
->ip_proto
, &ip
->ip_tos
,
1077 &ip
->ip_ttl
, &ip_frag_off
)) {
1080 put_16aligned_be32(&ip
->ip_src
, sip
);
1081 put_16aligned_be32(&ip
->ip_dst
, dip
);
1082 ip
->ip_frag_off
= htons(ip_frag_off
);
1083 ip_len
= sizeof *ip
;
1085 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1086 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1087 struct in6_addr sip6
, dip6
;
1090 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1091 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1092 ",hlimit=%"SCNi8
"),",
1093 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1094 &tclass
, &ip6
->ip6_hlim
)
1095 || (label
& ~IPV6_LABEL_MASK
) != 0
1096 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1097 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1100 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1101 htonl(tclass
<< 20) | htonl(label
));
1102 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1103 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1104 ip_len
= sizeof *ip6
;
1108 l4
= ((uint8_t *) l3
+ ip_len
);
1109 udp
= (struct udp_header
*) l4
;
1110 greh
= (struct gre_base_hdr
*) l4
;
1111 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1112 &udp_src
, &udp_dst
, &csum
)) {
1113 uint32_t vx_flags
, vni
;
1115 udp
->udp_src
= htons(udp_src
);
1116 udp
->udp_dst
= htons(udp_dst
);
1118 udp
->udp_csum
= htons(csum
);
1120 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1122 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1124 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1125 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1126 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1127 header_len
= sizeof *eth
+ ip_len
+
1128 sizeof *udp
+ sizeof *vxh
;
1129 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1130 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1132 memset(gnh
, 0, sizeof *gnh
);
1133 header_len
= sizeof *eth
+ ip_len
+
1134 sizeof *udp
+ sizeof *gnh
;
1136 if (ovs_scan_len(s
, &n
, "oam,")) {
1139 if (ovs_scan_len(s
, &n
, "crit,")) {
1142 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1145 if (ovs_scan_len(s
, &n
, ",options(")) {
1146 struct geneve_scan options
;
1149 memset(&options
, 0, sizeof options
);
1150 len
= scan_geneve(s
+ n
, &options
, NULL
);
1155 memcpy(gnh
->options
, options
.d
, options
.len
);
1156 gnh
->opt_len
= options
.len
/ 4;
1157 header_len
+= options
.len
;
1161 if (!ovs_scan_len(s
, &n
, "))")) {
1165 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1166 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1167 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1171 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1172 &gre_flags
, &gre_proto
)){
1174 tnl_type
= OVS_VPORT_TYPE_GRE
;
1175 greh
->flags
= htons(gre_flags
);
1176 greh
->protocol
= htons(gre_proto
);
1177 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1179 if (greh
->flags
& htons(GRE_CSUM
)) {
1180 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1184 memset(options
, 0, sizeof *options
);
1185 *((ovs_be16
*)options
) = htons(csum
);
1188 if (greh
->flags
& htons(GRE_KEY
)) {
1191 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1195 put_16aligned_be32(options
, htonl(key
));
1198 if (greh
->flags
& htons(GRE_SEQ
)) {
1201 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1204 put_16aligned_be32(options
, htonl(seq
));
1208 if (!ovs_scan_len(s
, &n
, "))")) {
1212 header_len
= sizeof *eth
+ ip_len
+
1213 ((uint8_t *) options
- (uint8_t *) greh
);
1218 /* check tunnel meta data. */
1219 if (data
->tnl_type
!= tnl_type
) {
1222 if (data
->header_len
!= header_len
) {
1227 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1234 struct ct_nat_params
{
1240 struct in6_addr ip6
;
1244 struct in6_addr ip6
;
1254 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1256 if (ovs_scan_len(s
, n
, "=")) {
1257 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1258 struct in6_addr ipv6
;
1260 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1261 p
->addr_len
= sizeof p
->addr_min
.ip
;
1262 if (ovs_scan_len(s
, n
, "-")) {
1263 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1264 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1268 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1269 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1270 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1271 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1272 p
->addr_min
.ip6
= ipv6
;
1273 if (ovs_scan_len(s
, n
, "-")) {
1274 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1275 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1276 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1277 p
->addr_max
.ip6
= ipv6
;
1285 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1286 if (ovs_scan_len(s
, n
, "-")) {
1287 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1297 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1301 if (ovs_scan_len(s
, &n
, "nat")) {
1302 memset(p
, 0, sizeof *p
);
1304 if (ovs_scan_len(s
, &n
, "(")) {
1308 end
= strchr(s
+ n
, ')');
1315 n
+= strspn(s
+ n
, delimiters
);
1316 if (ovs_scan_len(s
, &n
, "src")) {
1317 int err
= scan_ct_nat_range(s
, &n
, p
);
1324 if (ovs_scan_len(s
, &n
, "dst")) {
1325 int err
= scan_ct_nat_range(s
, &n
, p
);
1332 if (ovs_scan_len(s
, &n
, "persistent")) {
1333 p
->persistent
= true;
1336 if (ovs_scan_len(s
, &n
, "hash")) {
1337 p
->proto_hash
= true;
1340 if (ovs_scan_len(s
, &n
, "random")) {
1341 p
->proto_random
= true;
1347 if (p
->snat
&& p
->dnat
) {
1350 if ((p
->addr_len
!= 0 &&
1351 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1352 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1353 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1356 if (p
->proto_hash
&& p
->proto_random
) {
1366 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1368 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1371 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1372 } else if (p
->dnat
) {
1373 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1377 if (p
->addr_len
!= 0) {
1378 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1380 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1381 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1385 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1386 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1387 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1390 if (p
->persistent
) {
1391 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1393 if (p
->proto_hash
) {
1394 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1396 if (p
->proto_random
) {
1397 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1401 nl_msg_end_nested(actions
, start
);
1405 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1409 if (ovs_scan(s
, "ct")) {
1410 const char *helper
= NULL
;
1411 size_t helper_len
= 0;
1412 bool commit
= false;
1417 } ct_mark
= { 0, 0 };
1422 struct ct_nat_params nat_params
;
1423 bool have_nat
= false;
1427 memset(&ct_label
, 0, sizeof(ct_label
));
1430 if (ovs_scan(s
, "(")) {
1433 end
= strchr(s
, ')');
1441 s
+= strspn(s
, delimiters
);
1442 if (ovs_scan(s
, "commit%n", &n
)) {
1447 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1451 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1454 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1457 ct_mark
.mask
= UINT32_MAX
;
1461 if (ovs_scan(s
, "label=%n", &n
)) {
1465 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1472 if (ovs_scan(s
, "helper=%n", &n
)) {
1474 helper_len
= strcspn(s
, delimiters_end
);
1475 if (!helper_len
|| helper_len
> 15) {
1483 n
= scan_ct_nat(s
, &nat_params
);
1488 /* end points to the end of the nested, nat action.
1489 * find the real end. */
1492 /* Nothing matched. */
1498 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1500 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1503 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1506 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1509 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1510 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1514 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1518 nl_msg_put_ct_nat(&nat_params
, actions
);
1520 nl_msg_end_nested(actions
, start
);
1527 parse_odp_action(const char *s
, const struct simap
*port_names
,
1528 struct ofpbuf
*actions
)
1534 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1535 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1544 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
1545 struct ovs_action_trunc
*trunc
;
1547 trunc
= nl_msg_put_unspec_uninit(actions
,
1548 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1549 trunc
->max_len
= max_len
;
1555 int len
= strcspn(s
, delimiters
);
1556 struct simap_node
*node
;
1558 node
= simap_find_len(port_names
, s
, len
);
1560 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1569 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1570 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1575 if (!strncmp(s
, "userspace(", 10)) {
1576 return parse_odp_userspace_action(s
, actions
);
1579 if (!strncmp(s
, "set(", 4)) {
1582 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1583 struct ofpbuf maskbuf
;
1584 struct nlattr
*nested
, *key
;
1587 /* 'mask' is big enough to hold any key. */
1588 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1590 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1591 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1595 if (s
[retval
+ 4] != ')') {
1599 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1602 size
= nl_attr_get_size(mask
);
1603 if (size
== nl_attr_get_size(key
)) {
1604 /* Change to masked set action if not fully masked. */
1605 if (!is_all_ones(mask
+ 1, size
)) {
1606 key
->nla_len
+= size
;
1607 ofpbuf_put(actions
, mask
+ 1, size
);
1608 /* 'actions' may have been reallocated by ofpbuf_put(). */
1609 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1610 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1614 nl_msg_end_nested(actions
, start_ofs
);
1619 struct ovs_action_push_vlan push
;
1620 int tpid
= ETH_TYPE_VLAN
;
1625 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1626 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1627 &vid
, &pcp
, &cfi
, &n
)
1628 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1629 &tpid
, &vid
, &pcp
, &n
)
1630 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1631 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1632 push
.vlan_tpid
= htons(tpid
);
1633 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1634 | (pcp
<< VLAN_PCP_SHIFT
)
1635 | (cfi
? VLAN_CFI
: 0));
1636 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1637 &push
, sizeof push
);
1643 if (!strncmp(s
, "pop_vlan", 8)) {
1644 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1652 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1653 && percentage
>= 0. && percentage
<= 100.0) {
1654 size_t sample_ofs
, actions_ofs
;
1657 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1658 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1659 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1660 (probability
<= 0 ? 0
1661 : probability
>= UINT32_MAX
? UINT32_MAX
1664 actions_ofs
= nl_msg_start_nested(actions
,
1665 OVS_SAMPLE_ATTR_ACTIONS
);
1669 n
+= strspn(s
+ n
, delimiters
);
1674 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1680 nl_msg_end_nested(actions
, actions_ofs
);
1681 nl_msg_end_nested(actions
, sample_ofs
);
1683 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1691 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1692 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1700 retval
= parse_conntrack_action(s
, actions
);
1707 struct ovs_action_push_tnl data
;
1710 n
= ovs_parse_tnl_push(s
, &data
);
1712 odp_put_tnl_push_action(actions
, &data
);
1721 /* Parses the string representation of datapath actions, in the format output
1722 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1723 * value. On success, the ODP actions are appended to 'actions' as a series of
1724 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1725 * way, 'actions''s data might be reallocated. */
1727 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1728 struct ofpbuf
*actions
)
1732 if (!strcasecmp(s
, "drop")) {
1736 old_size
= actions
->size
;
1740 s
+= strspn(s
, delimiters
);
1745 retval
= parse_odp_action(s
, port_names
, actions
);
1746 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1747 actions
->size
= old_size
;
1756 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1757 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1760 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1761 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1762 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1763 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1764 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1765 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1766 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1767 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1768 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1769 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1770 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1771 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1772 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1773 .next
= ovs_vxlan_ext_attr_lens
,
1774 .next_max
= OVS_VXLAN_EXT_MAX
},
1775 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1776 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1779 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1780 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1781 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1782 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1783 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1784 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1785 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1786 .next
= ovs_tun_key_attr_lens
,
1787 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1788 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1789 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1790 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1791 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1792 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1793 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1794 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1795 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1796 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1797 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1798 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1799 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1800 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1801 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1802 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1803 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1804 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1805 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1806 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1809 /* Returns the correct length of the payload for a flow key attribute of the
1810 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1811 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1812 * payload is a nested type. */
1814 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1816 if (type
> max_len
) {
1817 return ATTR_LEN_INVALID
;
1820 return tbl
[type
].len
;
1824 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1826 size_t len
= nl_attr_get_size(a
);
1828 const uint8_t *unspec
;
1831 unspec
= nl_attr_get(a
);
1832 for (i
= 0; i
< len
; i
++) {
1834 ds_put_char(ds
, ' ');
1836 ds_put_format(ds
, "%02x", unspec
[i
]);
1842 ovs_frag_type_to_string(enum ovs_frag_type type
)
1845 case OVS_FRAG_TYPE_NONE
:
1847 case OVS_FRAG_TYPE_FIRST
:
1849 case OVS_FRAG_TYPE_LATER
:
1851 case __OVS_FRAG_TYPE_MAX
:
1857 static enum odp_key_fitness
1858 odp_tun_key_from_attr__(const struct nlattr
*attr
,
1859 const struct nlattr
*flow_attrs
, size_t flow_attr_len
,
1860 const struct flow_tnl
*src_tun
, struct flow_tnl
*tun
,
1864 const struct nlattr
*a
;
1866 bool unknown
= false;
1868 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1869 uint16_t type
= nl_attr_type(a
);
1870 size_t len
= nl_attr_get_size(a
);
1871 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1872 OVS_TUNNEL_ATTR_MAX
, type
);
1874 if (len
!= expected_len
&& expected_len
>= 0) {
1875 return ODP_FIT_ERROR
;
1879 case OVS_TUNNEL_KEY_ATTR_ID
:
1880 tun
->tun_id
= nl_attr_get_be64(a
);
1881 tun
->flags
|= FLOW_TNL_F_KEY
;
1883 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1884 tun
->ip_src
= nl_attr_get_be32(a
);
1886 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1887 tun
->ip_dst
= nl_attr_get_be32(a
);
1889 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
1890 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
1892 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
1893 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
1895 case OVS_TUNNEL_KEY_ATTR_TOS
:
1896 tun
->ip_tos
= nl_attr_get_u8(a
);
1898 case OVS_TUNNEL_KEY_ATTR_TTL
:
1899 tun
->ip_ttl
= nl_attr_get_u8(a
);
1902 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1903 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1905 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1906 tun
->flags
|= FLOW_TNL_F_CSUM
;
1908 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1909 tun
->tp_src
= nl_attr_get_be16(a
);
1911 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1912 tun
->tp_dst
= nl_attr_get_be16(a
);
1914 case OVS_TUNNEL_KEY_ATTR_OAM
:
1915 tun
->flags
|= FLOW_TNL_F_OAM
;
1917 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1918 static const struct nl_policy vxlan_opts_policy
[] = {
1919 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1921 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1923 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1924 return ODP_FIT_ERROR
;
1927 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1928 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1930 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1931 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
1936 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1937 if (tun_metadata_from_geneve_nlattr(a
, flow_attrs
, flow_attr_len
,
1938 src_tun
, udpif
, tun
)) {
1939 return ODP_FIT_ERROR
;
1944 /* Allow this to show up as unexpected, if there are unknown
1945 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1952 return ODP_FIT_ERROR
;
1955 return ODP_FIT_TOO_MUCH
;
1957 return ODP_FIT_PERFECT
;
1960 enum odp_key_fitness
1961 odp_tun_key_from_attr(const struct nlattr
*attr
, bool udpif
,
1962 struct flow_tnl
*tun
)
1964 memset(tun
, 0, sizeof *tun
);
1965 return odp_tun_key_from_attr__(attr
, NULL
, 0, NULL
, tun
, udpif
);
1969 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
1970 const struct flow_tnl
*tun_flow_key
,
1971 const struct ofpbuf
*key_buf
)
1975 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
1977 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1978 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
1979 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
1981 if (tun_key
->ip_src
) {
1982 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
1984 if (tun_key
->ip_dst
) {
1985 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
1987 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
1988 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
1990 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
1991 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
1993 if (tun_key
->ip_tos
) {
1994 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
1996 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
1997 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
1998 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2000 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2001 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2003 if (tun_key
->tp_src
) {
2004 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2006 if (tun_key
->tp_dst
) {
2007 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2009 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2010 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2012 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
2013 size_t vxlan_opts_ofs
;
2015 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2016 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2017 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2018 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2020 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2022 nl_msg_end_nested(a
, tun_key_ofs
);
2026 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2028 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
2032 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2034 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
2035 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
2037 if (attr
== OVS_KEY_ATTR_IPV6
) {
2038 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2041 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2042 == htonl(IPV6_LABEL_MASK
))
2043 && ipv6_mask
->ipv6_proto
== UINT8_MAX
2044 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
2045 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
2046 && ipv6_mask
->ipv6_frag
== UINT8_MAX
2047 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_src
)
2048 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_dst
);
2050 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2054 if (attr
== OVS_KEY_ATTR_ARP
) {
2055 /* ARP key has padding, ignore it. */
2056 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
2057 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
2058 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
2059 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
2062 return is_all_ones(mask
, size
);
2066 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2068 enum ovs_key_attr attr
= nl_attr_type(ma
);
2072 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2075 mask
= nl_attr_get(ma
);
2076 size
= nl_attr_get_size(ma
);
2079 return odp_mask_is_exact(attr
, mask
, size
);
2083 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2086 struct odp_portno_names
*odp_portno_names
;
2088 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2089 odp_portno_names
->port_no
= port_no
;
2090 odp_portno_names
->name
= xstrdup(port_name
);
2091 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2092 hash_odp_port(port_no
));
2096 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2098 struct odp_portno_names
*odp_portno_names
;
2100 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2101 hash_odp_port(port_no
), portno_names
) {
2102 if (odp_portno_names
->port_no
== port_no
) {
2103 return odp_portno_names
->name
;
2110 odp_portno_names_destroy(struct hmap
*portno_names
)
2112 struct odp_portno_names
*odp_portno_names
;
2114 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
2115 free(odp_portno_names
->name
);
2116 free(odp_portno_names
);
2120 /* Format helpers. */
2123 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2124 const struct eth_addr
*mask
, bool verbose
)
2126 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2128 if (verbose
|| !mask_empty
) {
2129 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2132 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2134 ds_put_format(ds
, "%s=", name
);
2135 eth_format_masked(key
, mask
, ds
);
2136 ds_put_char(ds
, ',');
2142 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2143 const ovs_be64
*mask
, bool verbose
)
2145 bool mask_empty
= mask
&& !*mask
;
2147 if (verbose
|| !mask_empty
) {
2148 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2150 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2151 if (!mask_full
) { /* Partially masked. */
2152 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2154 ds_put_char(ds
, ',');
2159 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2160 const ovs_be32
*mask
, bool verbose
)
2162 bool mask_empty
= mask
&& !*mask
;
2164 if (verbose
|| !mask_empty
) {
2165 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2167 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2168 if (!mask_full
) { /* Partially masked. */
2169 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2171 ds_put_char(ds
, ',');
2176 format_in6_addr(struct ds
*ds
, const char *name
,
2177 const struct in6_addr
*key
,
2178 const struct in6_addr
*mask
,
2181 char buf
[INET6_ADDRSTRLEN
];
2182 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2184 if (verbose
|| !mask_empty
) {
2185 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2187 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2188 ds_put_format(ds
, "%s=%s", name
, buf
);
2189 if (!mask_full
) { /* Partially masked. */
2190 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2191 ds_put_format(ds
, "/%s", buf
);
2193 ds_put_char(ds
, ',');
2198 format_ipv6(struct ds
*ds
, const char *name
, const ovs_be32 key_
[4],
2199 const ovs_be32 (*mask_
)[4], bool verbose
)
2201 format_in6_addr(ds
, name
,
2202 (const struct in6_addr
*)key_
,
2203 mask_
? (const struct in6_addr
*)*mask_
: NULL
,
2208 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2209 const ovs_be32
*mask
, bool verbose
)
2211 bool mask_empty
= mask
&& !*mask
;
2213 if (verbose
|| !mask_empty
) {
2214 bool mask_full
= !mask
2215 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2217 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2218 if (!mask_full
) { /* Partially masked. */
2219 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2221 ds_put_char(ds
, ',');
2226 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2227 const uint8_t *mask
, bool verbose
)
2229 bool mask_empty
= mask
&& !*mask
;
2231 if (verbose
|| !mask_empty
) {
2232 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2234 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2235 if (!mask_full
) { /* Partially masked. */
2236 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2238 ds_put_char(ds
, ',');
2243 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2244 const uint8_t *mask
, bool verbose
)
2246 bool mask_empty
= mask
&& !*mask
;
2248 if (verbose
|| !mask_empty
) {
2249 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2251 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2252 if (!mask_full
) { /* Partially masked. */
2253 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2255 ds_put_char(ds
, ',');
2260 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2261 const ovs_be16
*mask
, bool verbose
)
2263 bool mask_empty
= mask
&& !*mask
;
2265 if (verbose
|| !mask_empty
) {
2266 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2268 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2269 if (!mask_full
) { /* Partially masked. */
2270 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2272 ds_put_char(ds
, ',');
2277 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2278 const ovs_be16
*mask
, bool verbose
)
2280 bool mask_empty
= mask
&& !*mask
;
2282 if (verbose
|| !mask_empty
) {
2283 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2285 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2286 if (!mask_full
) { /* Partially masked. */
2287 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2289 ds_put_char(ds
, ',');
2294 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2295 const uint16_t *mask
, bool verbose
)
2297 bool mask_empty
= mask
&& !*mask
;
2299 if (verbose
|| !mask_empty
) {
2300 ds_put_cstr(ds
, name
);
2301 ds_put_char(ds
, '(');
2303 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2304 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2305 } else { /* Fully masked. */
2306 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2308 ds_put_cstr(ds
, "),");
2313 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2314 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2318 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2319 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2320 expected_len
!= ATTR_LEN_NESTED
) {
2322 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2323 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2325 if (bad_key_len
|| bad_mask_len
) {
2327 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2330 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2331 nl_attr_get_size(a
), expected_len
);
2333 format_generic_odp_key(a
, ds
);
2335 ds_put_char(ds
, '/');
2337 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2338 nl_attr_get_size(ma
), expected_len
);
2340 format_generic_odp_key(ma
, ds
);
2342 ds_put_char(ds
, ')');
2351 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2352 const struct nlattr
*ma
)
2354 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2355 format_generic_odp_key(a
, ds
);
2356 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2357 ds_put_char(ds
, '/');
2358 format_generic_odp_key(ma
, ds
);
2360 ds_put_cstr(ds
, "),");
2364 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2365 const struct nlattr
*mask_attr
, struct ds
*ds
,
2369 const struct nlattr
*a
;
2372 ofpbuf_init(&ofp
, 100);
2373 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2374 uint16_t type
= nl_attr_type(a
);
2375 const struct nlattr
*ma
= NULL
;
2378 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2379 nl_attr_get_size(mask_attr
), type
);
2381 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2387 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2388 OVS_VXLAN_EXT_MAX
, true)) {
2393 case OVS_VXLAN_EXT_GBP
: {
2394 uint32_t key
= nl_attr_get_u32(a
);
2395 ovs_be16 id
, id_mask
;
2396 uint8_t flags
, flags_mask
= 0;
2398 id
= htons(key
& 0xFFFF);
2399 flags
= (key
>> 16) & 0xFF;
2401 uint32_t mask
= nl_attr_get_u32(ma
);
2402 id_mask
= htons(mask
& 0xFFFF);
2403 flags_mask
= (mask
>> 16) & 0xFF;
2406 ds_put_cstr(ds
, "gbp(");
2407 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2408 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2410 ds_put_cstr(ds
, "),");
2415 format_unknown_key(ds
, a
, ma
);
2421 ofpbuf_uninit(&ofp
);
2424 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2427 format_geneve_opts(const struct geneve_opt
*opt
,
2428 const struct geneve_opt
*mask
, int opts_len
,
2429 struct ds
*ds
, bool verbose
)
2431 while (opts_len
> 0) {
2433 uint8_t data_len
, data_len_mask
;
2435 if (opts_len
< sizeof *opt
) {
2436 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2437 opts_len
, sizeof *opt
);
2441 data_len
= opt
->length
* 4;
2443 if (mask
->length
== 0x1f) {
2444 data_len_mask
= UINT8_MAX
;
2446 data_len_mask
= mask
->length
;
2449 len
= sizeof *opt
+ data_len
;
2450 if (len
> opts_len
) {
2451 ds_put_format(ds
, "opt len %u greater than remaining %u",
2456 ds_put_char(ds
, '{');
2457 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2459 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2460 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2462 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2463 ds_put_hex(ds
, opt
+ 1, data_len
);
2464 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2465 ds_put_char(ds
, '/');
2466 ds_put_hex(ds
, mask
+ 1, data_len
);
2471 ds_put_char(ds
, '}');
2473 opt
+= len
/ sizeof(*opt
);
2475 mask
+= len
/ sizeof(*opt
);
2482 format_odp_tun_geneve(const struct nlattr
*attr
,
2483 const struct nlattr
*mask_attr
, struct ds
*ds
,
2486 int opts_len
= nl_attr_get_size(attr
);
2487 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2488 const struct geneve_opt
*mask
= mask_attr
?
2489 nl_attr_get(mask_attr
) : NULL
;
2491 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2492 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2493 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2497 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2501 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2502 struct ds
*ds
, bool verbose
)
2505 const struct nlattr
*a
;
2507 uint16_t mask_flags
= 0;
2510 ofpbuf_init(&ofp
, 100);
2511 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2512 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2513 const struct nlattr
*ma
= NULL
;
2516 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2517 nl_attr_get_size(mask_attr
), type
);
2519 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2520 OVS_TUNNEL_KEY_ATTR_MAX
,
2525 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2526 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2531 case OVS_TUNNEL_KEY_ATTR_ID
:
2532 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2533 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2534 flags
|= FLOW_TNL_F_KEY
;
2536 mask_flags
|= FLOW_TNL_F_KEY
;
2539 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2540 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2541 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2543 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2544 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2545 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2547 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2548 struct in6_addr ipv6_src
;
2549 ipv6_src
= nl_attr_get_in6_addr(a
);
2550 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2551 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2554 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2555 struct in6_addr ipv6_dst
;
2556 ipv6_dst
= nl_attr_get_in6_addr(a
);
2557 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2558 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2561 case OVS_TUNNEL_KEY_ATTR_TOS
:
2562 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2563 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2565 case OVS_TUNNEL_KEY_ATTR_TTL
:
2566 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2567 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2569 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2570 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2572 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2573 flags
|= FLOW_TNL_F_CSUM
;
2575 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2576 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2577 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2579 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2580 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2581 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2583 case OVS_TUNNEL_KEY_ATTR_OAM
:
2584 flags
|= FLOW_TNL_F_OAM
;
2586 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2587 ds_put_cstr(ds
, "vxlan(");
2588 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2589 ds_put_cstr(ds
, "),");
2591 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2592 ds_put_cstr(ds
, "geneve(");
2593 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2594 ds_put_cstr(ds
, "),");
2596 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2598 format_unknown_key(ds
, a
, ma
);
2603 /* Flags can have a valid mask even if the attribute is not set, so
2604 * we need to collect these separately. */
2606 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2607 switch (nl_attr_type(a
)) {
2608 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2609 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2611 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2612 mask_flags
|= FLOW_TNL_F_CSUM
;
2614 case OVS_TUNNEL_KEY_ATTR_OAM
:
2615 mask_flags
|= FLOW_TNL_F_OAM
;
2621 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2624 ofpbuf_uninit(&ofp
);
2628 odp_ct_state_to_string(uint32_t flag
)
2631 case OVS_CS_F_REPLY_DIR
:
2633 case OVS_CS_F_TRACKED
:
2637 case OVS_CS_F_ESTABLISHED
:
2639 case OVS_CS_F_RELATED
:
2641 case OVS_CS_F_INVALID
:
2643 case OVS_CS_F_SRC_NAT
:
2645 case OVS_CS_F_DST_NAT
:
2653 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2654 const uint8_t *mask
, bool verbose
)
2656 bool mask_empty
= mask
&& !*mask
;
2658 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2659 if (verbose
|| !mask_empty
) {
2660 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2662 if (!mask_full
) { /* Partially masked. */
2663 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2666 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2672 mask_empty(const struct nlattr
*ma
)
2680 mask
= nl_attr_get(ma
);
2681 n
= nl_attr_get_size(ma
);
2683 return is_all_zeros(mask
, n
);
2687 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2688 const struct hmap
*portno_names
, struct ds
*ds
,
2691 enum ovs_key_attr attr
= nl_attr_type(a
);
2692 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2695 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2697 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2699 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2700 OVS_KEY_ATTR_MAX
, false)) {
2704 ds_put_char(ds
, '(');
2706 case OVS_KEY_ATTR_ENCAP
:
2707 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2708 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2709 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2711 } else if (nl_attr_get_size(a
)) {
2712 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2717 case OVS_KEY_ATTR_PRIORITY
:
2718 case OVS_KEY_ATTR_SKB_MARK
:
2719 case OVS_KEY_ATTR_DP_HASH
:
2720 case OVS_KEY_ATTR_RECIRC_ID
:
2721 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2723 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2727 case OVS_KEY_ATTR_CT_MARK
:
2728 if (verbose
|| !mask_empty(ma
)) {
2729 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2731 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2736 case OVS_KEY_ATTR_CT_STATE
:
2738 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2740 ds_put_format(ds
, "/%#"PRIx32
,
2741 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2743 } else if (!is_exact
) {
2744 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2746 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2749 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2753 case OVS_KEY_ATTR_CT_ZONE
:
2754 if (verbose
|| !mask_empty(ma
)) {
2755 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2757 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2762 case OVS_KEY_ATTR_CT_LABELS
: {
2763 const ovs_u128
*value
= nl_attr_get(a
);
2764 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2766 format_u128(ds
, value
, mask
, verbose
);
2770 case OVS_KEY_ATTR_TUNNEL
:
2771 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2774 case OVS_KEY_ATTR_IN_PORT
:
2775 if (portno_names
&& verbose
&& is_exact
) {
2776 char *name
= odp_portno_names_get(portno_names
,
2777 u32_to_odp(nl_attr_get_u32(a
)));
2779 ds_put_format(ds
, "%s", name
);
2781 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2784 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2786 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2791 case OVS_KEY_ATTR_ETHERNET
: {
2792 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2793 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2795 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2796 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2800 case OVS_KEY_ATTR_VLAN
:
2801 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2802 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2805 case OVS_KEY_ATTR_MPLS
: {
2806 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2807 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2808 size_t size
= nl_attr_get_size(a
);
2810 if (!size
|| size
% sizeof *mpls_key
) {
2811 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2815 mpls_mask
= nl_attr_get(ma
);
2816 if (size
!= nl_attr_get_size(ma
)) {
2817 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2818 "mask length %"PRIuSIZE
")",
2819 size
, nl_attr_get_size(ma
));
2823 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2826 case OVS_KEY_ATTR_ETHERTYPE
:
2827 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2829 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2833 case OVS_KEY_ATTR_IPV4
: {
2834 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2835 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2837 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2838 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2839 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2841 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2842 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2843 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2848 case OVS_KEY_ATTR_IPV6
: {
2849 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2850 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2852 format_ipv6(ds
, "src", key
->ipv6_src
, MASK(mask
, ipv6_src
), verbose
);
2853 format_ipv6(ds
, "dst", key
->ipv6_dst
, MASK(mask
, ipv6_dst
), verbose
);
2854 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2856 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2858 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2860 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2862 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2867 /* These have the same structure and format. */
2868 case OVS_KEY_ATTR_TCP
:
2869 case OVS_KEY_ATTR_UDP
:
2870 case OVS_KEY_ATTR_SCTP
: {
2871 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2872 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2874 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2875 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2879 case OVS_KEY_ATTR_TCP_FLAGS
:
2881 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2882 ntohs(nl_attr_get_be16(a
)),
2883 TCP_FLAGS(nl_attr_get_be16(ma
)),
2884 TCP_FLAGS(OVS_BE16_MAX
));
2886 format_flags(ds
, packet_tcp_flag_to_string
,
2887 ntohs(nl_attr_get_be16(a
)), '|');
2891 case OVS_KEY_ATTR_ICMP
: {
2892 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2893 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2895 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2896 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2900 case OVS_KEY_ATTR_ICMPV6
: {
2901 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2902 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2904 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2906 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2911 case OVS_KEY_ATTR_ARP
: {
2912 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2913 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2915 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2916 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2917 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2918 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2919 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2923 case OVS_KEY_ATTR_ND
: {
2924 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2925 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2927 format_ipv6(ds
, "target", key
->nd_target
, MASK(mask
, nd_target
),
2929 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2930 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2935 case OVS_KEY_ATTR_UNSPEC
:
2936 case __OVS_KEY_ATTR_MAX
:
2938 format_generic_odp_key(a
, ds
);
2940 ds_put_char(ds
, '/');
2941 format_generic_odp_key(ma
, ds
);
2945 ds_put_char(ds
, ')');
2948 static struct nlattr
*
2949 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2950 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2952 const struct nlattr
*a
;
2954 int type
= nl_attr_type(key
);
2955 int size
= nl_attr_get_size(key
);
2957 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2958 nl_msg_put_unspec_zero(ofp
, type
, size
);
2962 if (tbl
[type
].next
) {
2963 tbl
= tbl
[type
].next
;
2964 max
= tbl
[type
].next_max
;
2967 nested_mask
= nl_msg_start_nested(ofp
, type
);
2968 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2969 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2971 nl_msg_end_nested(ofp
, nested_mask
);
2978 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
2981 if (verbose
|| (mask
&& !ovs_u128_is_zero(*mask
))) {
2984 value
= hton128(*key
);
2985 ds_put_hex(ds
, &value
, sizeof value
);
2986 if (mask
&& !(ovs_u128_is_ones(*mask
))) {
2987 value
= hton128(*mask
);
2988 ds_put_char(ds
, '/');
2989 ds_put_hex(ds
, &value
, sizeof value
);
2995 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
2997 char *s
= CONST_CAST(char *, s_
);
3001 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3002 *value
= ntoh128(be_value
);
3007 if (ovs_scan(s
, "/%n", &n
)) {
3011 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3012 sizeof be_mask
, &s
);
3016 *mask
= ntoh128(be_mask
);
3018 *mask
= OVS_U128_MAX
;
3028 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3032 if (ovs_scan(s
, "ufid:")) {
3035 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3047 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3049 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3052 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3053 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3054 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3055 * non-null and 'verbose' is true, translates odp port number to its name. */
3057 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3058 const struct nlattr
*mask
, size_t mask_len
,
3059 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3062 const struct nlattr
*a
;
3064 bool has_ethtype_key
= false;
3065 const struct nlattr
*ma
= NULL
;
3067 bool first_field
= true;
3069 ofpbuf_init(&ofp
, 100);
3070 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3071 bool is_nested_attr
;
3072 bool is_wildcard
= false;
3073 int attr_type
= nl_attr_type(a
);
3075 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3076 has_ethtype_key
= true;
3079 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3080 OVS_KEY_ATTR_MAX
, attr_type
) ==
3083 if (mask
&& mask_len
) {
3084 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3085 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3088 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3089 if (is_wildcard
&& !ma
) {
3090 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3095 ds_put_char(ds
, ',');
3097 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3098 first_field
= false;
3102 ofpbuf_uninit(&ofp
);
3107 if (left
== key_len
) {
3108 ds_put_cstr(ds
, "<empty>");
3110 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3111 for (i
= 0; i
< left
; i
++) {
3112 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3114 ds_put_char(ds
, ')');
3116 if (!has_ethtype_key
) {
3117 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3119 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3120 ntohs(nl_attr_get_be16(ma
)));
3124 ds_put_cstr(ds
, "<empty>");
3128 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3129 * OVS_KEY_ATTR_* attributes in 'key'. */
3131 odp_flow_key_format(const struct nlattr
*key
,
3132 size_t key_len
, struct ds
*ds
)
3134 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3138 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3140 if (!strcasecmp(s
, "no")) {
3141 *type
= OVS_FRAG_TYPE_NONE
;
3142 } else if (!strcasecmp(s
, "first")) {
3143 *type
= OVS_FRAG_TYPE_FIRST
;
3144 } else if (!strcasecmp(s
, "later")) {
3145 *type
= OVS_FRAG_TYPE_LATER
;
3155 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3159 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3160 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3164 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3165 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3168 memset(mask
, 0xff, sizeof *mask
);
3177 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3181 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3185 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3186 IP_SCAN_ARGS(mask
), &n
)) {
3189 *mask
= OVS_BE32_MAX
;
3198 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3201 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3203 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3204 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3208 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3209 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3212 memset(mask
, 0xff, sizeof *mask
);
3221 scan_ipv6(const char *s
, ovs_be32 (*key
)[4], ovs_be32 (*mask
)[4])
3223 return scan_in6_addr(s
, key
? (struct in6_addr
*) *key
: NULL
,
3224 mask
? (struct in6_addr
*) *mask
: NULL
);
3228 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3233 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3234 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3239 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3240 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3242 *mask
= htonl(mask_
);
3244 *mask
= htonl(IPV6_LABEL_MASK
);
3253 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3257 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3261 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3273 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3277 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3281 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3293 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3297 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3301 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3313 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3315 uint16_t key_
, mask_
;
3318 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3323 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3325 *mask
= htons(mask_
);
3327 *mask
= OVS_BE16_MAX
;
3336 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3338 uint64_t key_
, mask_
;
3341 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3344 *key
= htonll(key_
);
3346 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3348 *mask
= htonll(mask_
);
3350 *mask
= OVS_BE64_MAX
;
3359 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3361 uint32_t flags
, fmask
;
3364 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3365 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3366 if (n
>= 0 && s
[n
] == ')') {
3377 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3379 uint32_t flags
, fmask
;
3382 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3383 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3385 *key
= htons(flags
);
3387 *mask
= htons(fmask
);
3395 ovs_to_odp_ct_state(uint8_t state
)
3399 if (state
& CS_NEW
) {
3400 odp
|= OVS_CS_F_NEW
;
3402 if (state
& CS_ESTABLISHED
) {
3403 odp
|= OVS_CS_F_ESTABLISHED
;
3405 if (state
& CS_RELATED
) {
3406 odp
|= OVS_CS_F_RELATED
;
3408 if (state
& CS_INVALID
) {
3409 odp
|= OVS_CS_F_INVALID
;
3411 if (state
& CS_REPLY_DIR
) {
3412 odp
|= OVS_CS_F_REPLY_DIR
;
3414 if (state
& CS_TRACKED
) {
3415 odp
|= OVS_CS_F_TRACKED
;
3417 if (state
& CS_SRC_NAT
) {
3418 odp
|= OVS_CS_F_SRC_NAT
;
3420 if (state
& CS_DST_NAT
) {
3421 odp
|= OVS_CS_F_DST_NAT
;
3428 odp_to_ovs_ct_state(uint32_t flags
)
3432 if (flags
& OVS_CS_F_NEW
) {
3435 if (flags
& OVS_CS_F_ESTABLISHED
) {
3436 state
|= CS_ESTABLISHED
;
3438 if (flags
& OVS_CS_F_RELATED
) {
3439 state
|= CS_RELATED
;
3441 if (flags
& OVS_CS_F_INVALID
) {
3442 state
|= CS_INVALID
;
3444 if (flags
& OVS_CS_F_REPLY_DIR
) {
3445 state
|= CS_REPLY_DIR
;
3447 if (flags
& OVS_CS_F_TRACKED
) {
3448 state
|= CS_TRACKED
;
3450 if (flags
& OVS_CS_F_SRC_NAT
) {
3451 state
|= CS_SRC_NAT
;
3453 if (flags
& OVS_CS_F_DST_NAT
) {
3454 state
|= CS_DST_NAT
;
3461 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3463 uint32_t flags
, fmask
;
3466 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3467 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3468 mask
? &fmask
: NULL
);
3481 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3485 enum ovs_frag_type frag_type
;
3487 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3488 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3501 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3502 const struct simap
*port_names
)
3506 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3510 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3517 } else if (port_names
) {
3518 const struct simap_node
*node
;
3521 len
= strcspn(s
, ")");
3522 node
= simap_find_len(port_names
, s
, len
);
3535 /* Helper for vlan parsing. */
3536 struct ovs_key_vlan__
{
3541 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3543 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3545 if (value
>> bits
) {
3549 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3554 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3557 uint16_t key_
, mask_
;
3560 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3563 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3565 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3568 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3572 *mask
|= htons(((1U << bits
) - 1) << offset
);
3582 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3584 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3588 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3590 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3594 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3596 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3601 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3603 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3605 if (value
>> bits
) {
3609 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3614 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3617 uint32_t key_
, mask_
;
3620 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3623 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3625 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3628 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3632 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3642 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3644 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3648 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3650 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3654 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3656 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3660 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3662 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3666 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3668 const char *s_base
= s
;
3669 ovs_be16 id
= 0, id_mask
= 0;
3670 uint8_t flags
= 0, flags_mask
= 0;
3672 if (!strncmp(s
, "id=", 3)) {
3674 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3680 if (!strncmp(s
, "flags=", 6)) {
3682 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3685 if (!strncmp(s
, "))", 2)) {
3688 *key
= (flags
<< 16) | ntohs(id
);
3690 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3700 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3702 const char *s_base
= s
;
3703 struct geneve_opt
*opt
= key
->d
;
3704 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3705 int len_remain
= sizeof key
->d
;
3707 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3711 len_remain
-= sizeof *opt
;
3713 if (!strncmp(s
, "class=", 6)) {
3715 s
+= scan_be16(s
, &opt
->opt_class
,
3716 mask
? &opt_mask
->opt_class
: NULL
);
3718 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3724 if (!strncmp(s
, "type=", 5)) {
3726 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3728 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3734 if (!strncmp(s
, "len=", 4)) {
3735 uint8_t opt_len
, opt_len_mask
;
3737 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3739 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3742 opt
->length
= opt_len
/ 4;
3744 opt_mask
->length
= opt_len_mask
;
3748 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3754 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3761 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3762 data_len
, (char **)&s
)) {
3773 opt
+= 1 + data_len
/ 4;
3775 opt_mask
+= 1 + data_len
/ 4;
3777 len_remain
-= data_len
;
3782 int len
= sizeof key
->d
- len_remain
;
3796 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3798 const uint16_t *flags
= data_
;
3800 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3801 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3803 if (*flags
& FLOW_TNL_F_CSUM
) {
3804 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3806 if (*flags
& FLOW_TNL_F_OAM
) {
3807 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3812 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3814 const uint32_t *gbp
= data_
;
3817 size_t vxlan_opts_ofs
;
3819 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3820 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3821 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3826 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3828 const struct geneve_scan
*geneve
= data_
;
3830 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3834 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3836 unsigned long call_fn = (unsigned long)FUNC; \
3838 typedef void (*fn)(struct ofpbuf *, const void *); \
3840 func(BUF, &(DATA)); \
3842 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3846 #define SCAN_IF(NAME) \
3847 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3848 const char *start = s; \
3853 /* Usually no special initialization is needed. */
3854 #define SCAN_BEGIN(NAME, TYPE) \
3857 memset(&skey, 0, sizeof skey); \
3858 memset(&smask, 0, sizeof smask); \
3862 /* Init as fully-masked as mask will not be scanned. */
3863 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3866 memset(&skey, 0, sizeof skey); \
3867 memset(&smask, 0xff, sizeof smask); \
3871 /* VLAN needs special initialization. */
3872 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3874 TYPE skey = KEY_INIT; \
3875 TYPE smask = MASK_INIT; \
3879 /* Scan unnamed entry as 'TYPE' */
3880 #define SCAN_TYPE(TYPE, KEY, MASK) \
3881 len = scan_##TYPE(s, KEY, MASK); \
3887 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3888 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3889 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3890 s += strlen(NAME); \
3891 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3895 #define SCAN_FINISH() \
3896 } while (*s++ == ',' && len != 0); \
3897 if (s[-1] != ')') { \
3901 #define SCAN_FINISH_SINGLE() \
3903 if (*s++ != ')') { \
3907 /* Beginning of nested attribute. */
3908 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3910 size_t key_offset, mask_offset; \
3911 key_offset = nl_msg_start_nested(key, ATTR); \
3913 mask_offset = nl_msg_start_nested(mask, ATTR); \
3918 #define SCAN_END_NESTED() \
3920 nl_msg_end_nested(key, key_offset); \
3922 nl_msg_end_nested(mask, mask_offset); \
3927 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3928 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3930 memset(&skey, 0, sizeof skey); \
3931 memset(&smask, 0xff, sizeof smask); \
3932 s += strlen(NAME); \
3933 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3934 SCAN_PUT(ATTR, FUNC); \
3938 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3939 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3941 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3942 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3944 #define SCAN_PUT(ATTR, FUNC) \
3945 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3947 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3949 #define SCAN_END(ATTR) \
3951 SCAN_PUT(ATTR, NULL); \
3955 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
3957 TYPE skey[CNT], smask[CNT]; \
3958 memset(&skey, 0, sizeof skey); \
3959 memset(&smask, 0, sizeof smask); \
3960 int idx = 0, cnt = CNT; \
3961 uint64_t fields = 0; \
3966 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3967 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
3968 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3969 if (fields & (1UL << field)) { \
3971 if (++idx == cnt) { \
3975 s += strlen(NAME); \
3976 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
3977 fields |= 1UL << field; \
3982 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
3983 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
3985 #define SCAN_PUT_ARRAY(ATTR, CNT) \
3986 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
3988 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
3991 #define SCAN_END_ARRAY(ATTR) \
3996 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4000 #define SCAN_END_SINGLE(ATTR) \
4001 SCAN_FINISH_SINGLE(); \
4002 SCAN_PUT(ATTR, NULL); \
4006 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4007 SCAN_BEGIN(NAME, TYPE) { \
4008 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4009 } SCAN_END_SINGLE(ATTR)
4011 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4012 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4013 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4014 } SCAN_END_SINGLE(ATTR)
4016 /* scan_port needs one extra argument. */
4017 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4018 SCAN_BEGIN(NAME, TYPE) { \
4019 len = scan_port(s, &skey, &smask, port_names); \
4024 } SCAN_END_SINGLE(ATTR)
4027 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4028 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4034 len
= odp_ufid_from_string(s
, &ufid
);
4039 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4040 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4041 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4042 OVS_KEY_ATTR_RECIRC_ID
);
4043 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4045 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4046 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4047 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4048 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4050 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4051 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4052 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4053 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4054 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4055 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4056 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4057 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4058 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4059 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4060 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4061 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4063 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4064 } SCAN_END_NESTED();
4066 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4068 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4069 SCAN_FIELD("src=", eth
, eth_src
);
4070 SCAN_FIELD("dst=", eth
, eth_dst
);
4071 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4073 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4074 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4075 SCAN_FIELD("vid=", vid
, tci
);
4076 SCAN_FIELD("pcp=", pcp
, tci
);
4077 SCAN_FIELD("cfi=", cfi
, tci
);
4078 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4080 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4082 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4083 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4084 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4085 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4086 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4087 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4089 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4090 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4091 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4092 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4093 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4094 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4095 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4096 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4098 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4099 SCAN_FIELD("src=", ipv6
, ipv6_src
);
4100 SCAN_FIELD("dst=", ipv6
, ipv6_dst
);
4101 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4102 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4103 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4104 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4105 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4106 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4108 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4109 SCAN_FIELD("src=", be16
, tcp_src
);
4110 SCAN_FIELD("dst=", be16
, tcp_dst
);
4111 } SCAN_END(OVS_KEY_ATTR_TCP
);
4113 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4115 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4116 SCAN_FIELD("src=", be16
, udp_src
);
4117 SCAN_FIELD("dst=", be16
, udp_dst
);
4118 } SCAN_END(OVS_KEY_ATTR_UDP
);
4120 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4121 SCAN_FIELD("src=", be16
, sctp_src
);
4122 SCAN_FIELD("dst=", be16
, sctp_dst
);
4123 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4125 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4126 SCAN_FIELD("type=", u8
, icmp_type
);
4127 SCAN_FIELD("code=", u8
, icmp_code
);
4128 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4130 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4131 SCAN_FIELD("type=", u8
, icmpv6_type
);
4132 SCAN_FIELD("code=", u8
, icmpv6_code
);
4133 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4135 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4136 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4137 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4138 SCAN_FIELD("op=", be16
, arp_op
);
4139 SCAN_FIELD("sha=", eth
, arp_sha
);
4140 SCAN_FIELD("tha=", eth
, arp_tha
);
4141 } SCAN_END(OVS_KEY_ATTR_ARP
);
4143 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4144 SCAN_FIELD("target=", ipv6
, nd_target
);
4145 SCAN_FIELD("sll=", eth
, nd_sll
);
4146 SCAN_FIELD("tll=", eth
, nd_tll
);
4147 } SCAN_END(OVS_KEY_ATTR_ND
);
4149 /* Encap open-coded. */
4150 if (!strncmp(s
, "encap(", 6)) {
4151 const char *start
= s
;
4152 size_t encap
, encap_mask
= 0;
4154 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4156 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4163 s
+= strspn(s
, delimiters
);
4166 } else if (*s
== ')') {
4170 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4178 nl_msg_end_nested(key
, encap
);
4180 nl_msg_end_nested(mask
, encap_mask
);
4189 /* Parses the string representation of a datapath flow key, in the
4190 * format output by odp_flow_key_format(). Returns 0 if successful,
4191 * otherwise a positive errno value. On success, the flow key is
4192 * appended to 'key' as a series of Netlink attributes. On failure, no
4193 * data is appended to 'key'. Either way, 'key''s data might be
4196 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4197 * to a port number. (Port names may be used instead of port numbers in
4200 * On success, the attributes appended to 'key' are individually syntactically
4201 * valid, but they may not be valid as a sequence. 'key' might, for example,
4202 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4204 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4205 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4207 const size_t old_size
= key
->size
;
4211 s
+= strspn(s
, delimiters
);
4216 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4218 key
->size
= old_size
;
4228 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4231 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4232 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4233 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4234 * must use a zero mask for the netlink frag field, and all ones mask
4236 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4238 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4239 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4240 : OVS_FRAG_TYPE_FIRST
;
4243 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4244 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4245 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4247 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4249 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4251 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4253 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4254 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4255 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4256 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4258 /* These share the same layout. */
4260 struct ovs_key_tcp tcp
;
4261 struct ovs_key_udp udp
;
4262 struct ovs_key_sctp sctp
;
4265 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4266 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4269 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4270 bool export_mask
, struct ofpbuf
*buf
)
4272 struct ovs_key_ethernet
*eth_key
;
4274 const struct flow
*flow
= parms
->flow
;
4275 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4277 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4279 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4280 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4284 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4286 if (parms
->support
.ct_state
) {
4287 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4288 ovs_to_odp_ct_state(data
->ct_state
));
4290 if (parms
->support
.ct_zone
) {
4291 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4293 if (parms
->support
.ct_mark
) {
4294 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4296 if (parms
->support
.ct_label
) {
4297 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4298 sizeof(data
->ct_label
));
4300 if (parms
->support
.recirc
) {
4301 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4302 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4305 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4306 * is not the magical value "ODPP_NONE". */
4307 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4308 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4311 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4313 get_ethernet_key(data
, eth_key
);
4315 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4317 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4319 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4321 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4322 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4323 if (flow
->vlan_tci
== htons(0)) {
4330 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4331 /* For backwards compatibility with kernels that don't support
4332 * wildcarding, the following convention is used to encode the
4333 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4336 * -------- -------- -------
4337 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4338 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4339 * <none> 0xffff Any non-Ethernet II frame (except valid
4340 * 802.3 SNAP packet with valid eth_type).
4343 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4348 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4350 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4351 struct ovs_key_ipv4
*ipv4_key
;
4353 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4355 get_ipv4_key(data
, ipv4_key
, export_mask
);
4356 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4357 struct ovs_key_ipv6
*ipv6_key
;
4359 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4361 get_ipv6_key(data
, ipv6_key
, export_mask
);
4362 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4363 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4364 struct ovs_key_arp
*arp_key
;
4366 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4368 get_arp_key(data
, arp_key
);
4369 } else if (eth_type_mpls(flow
->dl_type
)) {
4370 struct ovs_key_mpls
*mpls_key
;
4373 n
= flow_count_mpls_labels(flow
, NULL
);
4375 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4377 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4378 n
* sizeof *mpls_key
);
4379 for (i
= 0; i
< n
; i
++) {
4380 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4384 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4385 if (flow
->nw_proto
== IPPROTO_TCP
) {
4386 union ovs_key_tp
*tcp_key
;
4388 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4390 get_tp_key(data
, tcp_key
);
4391 if (data
->tcp_flags
) {
4392 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4394 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4395 union ovs_key_tp
*udp_key
;
4397 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4399 get_tp_key(data
, udp_key
);
4400 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4401 union ovs_key_tp
*sctp_key
;
4403 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4405 get_tp_key(data
, sctp_key
);
4406 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4407 && flow
->nw_proto
== IPPROTO_ICMP
) {
4408 struct ovs_key_icmp
*icmp_key
;
4410 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4412 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4413 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4414 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4415 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4416 struct ovs_key_icmpv6
*icmpv6_key
;
4418 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4419 sizeof *icmpv6_key
);
4420 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4421 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4423 if (flow
->tp_dst
== htons(0)
4424 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)
4425 || flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))
4426 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4427 * type and code are 8 bits wide. Therefore, an exact match
4428 * looks like htons(0xff), not htons(0xffff). See
4429 * xlate_wc_finish() for details. */
4430 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4431 && data
->tp_dst
== htons(0xff)))) {
4433 struct ovs_key_nd
*nd_key
;
4435 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4437 memcpy(nd_key
->nd_target
, &data
->nd_target
,
4438 sizeof nd_key
->nd_target
);
4439 nd_key
->nd_sll
= data
->arp_sha
;
4440 nd_key
->nd_tll
= data
->arp_tha
;
4447 nl_msg_end_nested(buf
, encap
);
4451 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4453 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4454 * capable of being expanded to allow for that much space. */
4456 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4459 odp_flow_key_from_flow__(parms
, false, buf
);
4462 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4465 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4466 * capable of being expanded to allow for that much space. */
4468 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4471 odp_flow_key_from_flow__(parms
, true, buf
);
4474 /* Generate ODP flow key from the given packet metadata */
4476 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4478 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4480 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4481 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4484 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4487 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4488 ovs_to_odp_ct_state(md
->ct_state
));
4490 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4493 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4495 if (!ovs_u128_is_zero(md
->ct_label
)) {
4496 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4497 sizeof(md
->ct_label
));
4501 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4502 * value "ODPP_NONE". */
4503 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4504 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4508 /* Generate packet metadata from the given ODP flow key. */
4510 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4511 struct pkt_metadata
*md
)
4513 const struct nlattr
*nla
;
4515 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4516 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4517 1u << OVS_KEY_ATTR_IN_PORT
;
4519 pkt_metadata_init(md
, ODPP_NONE
);
4521 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4522 uint16_t type
= nl_attr_type(nla
);
4523 size_t len
= nl_attr_get_size(nla
);
4524 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4525 OVS_KEY_ATTR_MAX
, type
);
4527 if (len
!= expected_len
&& expected_len
>= 0) {
4532 case OVS_KEY_ATTR_RECIRC_ID
:
4533 md
->recirc_id
= nl_attr_get_u32(nla
);
4534 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4536 case OVS_KEY_ATTR_DP_HASH
:
4537 md
->dp_hash
= nl_attr_get_u32(nla
);
4538 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4540 case OVS_KEY_ATTR_PRIORITY
:
4541 md
->skb_priority
= nl_attr_get_u32(nla
);
4542 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4544 case OVS_KEY_ATTR_SKB_MARK
:
4545 md
->pkt_mark
= nl_attr_get_u32(nla
);
4546 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4548 case OVS_KEY_ATTR_CT_STATE
:
4549 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4550 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4552 case OVS_KEY_ATTR_CT_ZONE
:
4553 md
->ct_zone
= nl_attr_get_u16(nla
);
4554 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4556 case OVS_KEY_ATTR_CT_MARK
:
4557 md
->ct_mark
= nl_attr_get_u32(nla
);
4558 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4560 case OVS_KEY_ATTR_CT_LABELS
: {
4561 const ovs_u128
*cl
= nl_attr_get(nla
);
4564 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4567 case OVS_KEY_ATTR_TUNNEL
: {
4568 enum odp_key_fitness res
;
4570 res
= odp_tun_key_from_attr(nla
, true, &md
->tunnel
);
4571 if (res
== ODP_FIT_ERROR
) {
4572 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4573 } else if (res
== ODP_FIT_PERFECT
) {
4574 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4578 case OVS_KEY_ATTR_IN_PORT
:
4579 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4580 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4586 if (!wanted_attrs
) {
4587 return; /* Have everything. */
4593 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4595 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4596 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
4600 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4601 uint64_t attrs
, int out_of_range_attr
,
4602 const struct nlattr
*key
, size_t key_len
)
4607 if (VLOG_DROP_DBG(rl
)) {
4612 for (i
= 0; i
< 64; i
++) {
4613 if (attrs
& (UINT64_C(1) << i
)) {
4614 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4616 ds_put_format(&s
, " %s",
4617 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4620 if (out_of_range_attr
) {
4621 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4624 ds_put_cstr(&s
, ": ");
4625 odp_flow_key_format(key
, key_len
, &s
);
4627 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4632 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4634 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4637 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4640 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4641 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4642 return 0xff; /* Error. */
4645 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4646 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4647 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4651 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4652 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4653 int *out_of_range_attrp
)
4655 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4656 const struct nlattr
*nla
;
4657 uint64_t present_attrs
;
4660 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4662 *out_of_range_attrp
= 0;
4663 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4664 uint16_t type
= nl_attr_type(nla
);
4665 size_t len
= nl_attr_get_size(nla
);
4666 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4667 OVS_KEY_ATTR_MAX
, type
);
4669 if (len
!= expected_len
&& expected_len
>= 0) {
4670 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4672 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4673 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4679 if (type
> OVS_KEY_ATTR_MAX
) {
4680 *out_of_range_attrp
= type
;
4682 if (present_attrs
& (UINT64_C(1) << type
)) {
4683 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4685 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4686 ovs_key_attr_to_string(type
,
4687 namebuf
, sizeof namebuf
));
4691 present_attrs
|= UINT64_C(1) << type
;
4696 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4700 *present_attrsp
= present_attrs
;
4704 static enum odp_key_fitness
4705 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4706 uint64_t expected_attrs
,
4707 const struct nlattr
*key
, size_t key_len
)
4709 uint64_t missing_attrs
;
4710 uint64_t extra_attrs
;
4712 missing_attrs
= expected_attrs
& ~present_attrs
;
4713 if (missing_attrs
) {
4714 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4715 log_odp_key_attributes(&rl
, "expected but not present",
4716 missing_attrs
, 0, key
, key_len
);
4717 return ODP_FIT_TOO_LITTLE
;
4720 extra_attrs
= present_attrs
& ~expected_attrs
;
4721 if (extra_attrs
|| out_of_range_attr
) {
4722 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4723 log_odp_key_attributes(&rl
, "present but not expected",
4724 extra_attrs
, out_of_range_attr
, key
, key_len
);
4725 return ODP_FIT_TOO_MUCH
;
4728 return ODP_FIT_PERFECT
;
4732 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4733 uint64_t present_attrs
, uint64_t *expected_attrs
,
4734 struct flow
*flow
, const struct flow
*src_flow
)
4736 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4737 bool is_mask
= flow
!= src_flow
;
4739 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4740 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4741 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4742 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4743 ntohs(flow
->dl_type
));
4746 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4747 flow
->dl_type
!= htons(0xffff)) {
4750 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4753 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4754 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4755 /* See comments in odp_flow_key_from_flow__(). */
4756 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4763 static enum odp_key_fitness
4764 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4765 uint64_t present_attrs
, int out_of_range_attr
,
4766 uint64_t expected_attrs
, struct flow
*flow
,
4767 const struct nlattr
*key
, size_t key_len
,
4768 const struct flow
*src_flow
)
4770 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4771 bool is_mask
= src_flow
!= flow
;
4772 const void *check_start
= NULL
;
4773 size_t check_len
= 0;
4774 enum ovs_key_attr expected_bit
= 0xff;
4776 if (eth_type_mpls(src_flow
->dl_type
)) {
4777 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4778 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4780 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4781 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4782 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4783 int n
= size
/ sizeof(ovs_be32
);
4786 if (!size
|| size
% sizeof(ovs_be32
)) {
4787 return ODP_FIT_ERROR
;
4789 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4790 return ODP_FIT_ERROR
;
4793 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4794 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4796 if (n
> FLOW_MAX_MPLS_LABELS
) {
4797 return ODP_FIT_TOO_MUCH
;
4801 /* BOS may be set only in the innermost label. */
4802 for (i
= 0; i
< n
- 1; i
++) {
4803 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4804 return ODP_FIT_ERROR
;
4808 /* BOS must be set in the innermost label. */
4809 if (n
< FLOW_MAX_MPLS_LABELS
4810 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4811 return ODP_FIT_TOO_LITTLE
;
4817 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4819 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4821 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4822 const struct ovs_key_ipv4
*ipv4_key
;
4824 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4825 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4826 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4827 return ODP_FIT_ERROR
;
4830 check_start
= ipv4_key
;
4831 check_len
= sizeof *ipv4_key
;
4832 expected_bit
= OVS_KEY_ATTR_IPV4
;
4835 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4837 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4839 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4840 const struct ovs_key_ipv6
*ipv6_key
;
4842 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4843 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4844 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4845 return ODP_FIT_ERROR
;
4848 check_start
= ipv6_key
;
4849 check_len
= sizeof *ipv6_key
;
4850 expected_bit
= OVS_KEY_ATTR_IPV6
;
4853 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4854 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4856 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4858 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4859 const struct ovs_key_arp
*arp_key
;
4861 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4862 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4863 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4864 "key", ntohs(arp_key
->arp_op
));
4865 return ODP_FIT_ERROR
;
4867 put_arp_key(arp_key
, flow
);
4869 check_start
= arp_key
;
4870 check_len
= sizeof *arp_key
;
4871 expected_bit
= OVS_KEY_ATTR_ARP
;
4877 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4878 if (!is_all_zeros(check_start
, check_len
) &&
4879 flow
->dl_type
!= htons(0xffff)) {
4880 return ODP_FIT_ERROR
;
4882 expected_attrs
|= UINT64_C(1) << expected_bit
;
4886 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4887 if (src_flow
->nw_proto
== IPPROTO_TCP
4888 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4889 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4890 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4892 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4894 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4895 const union ovs_key_tp
*tcp_key
;
4897 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4898 put_tp_key(tcp_key
, flow
);
4899 expected_bit
= OVS_KEY_ATTR_TCP
;
4901 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4902 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4903 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4905 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4906 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4907 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4908 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4910 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4912 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4913 const union ovs_key_tp
*udp_key
;
4915 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4916 put_tp_key(udp_key
, flow
);
4917 expected_bit
= OVS_KEY_ATTR_UDP
;
4919 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4920 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4921 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4922 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4924 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4926 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4927 const union ovs_key_tp
*sctp_key
;
4929 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4930 put_tp_key(sctp_key
, flow
);
4931 expected_bit
= OVS_KEY_ATTR_SCTP
;
4933 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4934 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4935 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4937 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4939 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4940 const struct ovs_key_icmp
*icmp_key
;
4942 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4943 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4944 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4945 expected_bit
= OVS_KEY_ATTR_ICMP
;
4947 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
4948 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4949 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4951 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
4953 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
4954 const struct ovs_key_icmpv6
*icmpv6_key
;
4956 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
4957 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
4958 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
4959 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
4960 if (src_flow
->tp_dst
== htons(0) &&
4961 (src_flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
4962 src_flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))) {
4964 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4966 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
4967 const struct ovs_key_nd
*nd_key
;
4969 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
4970 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
4971 sizeof flow
->nd_target
);
4972 flow
->arp_sha
= nd_key
->nd_sll
;
4973 flow
->arp_tha
= nd_key
->nd_tll
;
4975 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
4976 * ICMP type and code are 8 bits wide. Therefore, an
4977 * exact match looks like htons(0xff), not
4978 * htons(0xffff). See xlate_wc_finish() for details.
4980 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
4981 (flow
->tp_src
!= htons(0xff) ||
4982 flow
->tp_dst
!= htons(0xff))) {
4983 return ODP_FIT_ERROR
;
4985 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4992 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
4993 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
4994 return ODP_FIT_ERROR
;
4996 expected_attrs
|= UINT64_C(1) << expected_bit
;
5001 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5005 /* Parse 802.1Q header then encapsulated L3 attributes. */
5006 static enum odp_key_fitness
5007 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5008 uint64_t present_attrs
, int out_of_range_attr
,
5009 uint64_t expected_attrs
, struct flow
*flow
,
5010 const struct nlattr
*key
, size_t key_len
,
5011 const struct flow
*src_flow
)
5013 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5014 bool is_mask
= src_flow
!= flow
;
5016 const struct nlattr
*encap
5017 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5018 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5019 enum odp_key_fitness encap_fitness
;
5020 enum odp_key_fitness fitness
;
5022 /* Calculate fitness of outer attributes. */
5024 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5025 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5027 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5028 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5030 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5031 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5034 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5035 expected_attrs
, key
, key_len
);
5038 * Remove the TPID from dl_type since it's not the real Ethertype. */
5039 flow
->dl_type
= htons(0);
5040 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5041 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5044 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
5045 return ODP_FIT_TOO_LITTLE
;
5046 } else if (flow
->vlan_tci
== htons(0)) {
5047 /* Corner case for a truncated 802.1Q header. */
5048 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5049 return ODP_FIT_TOO_MUCH
;
5052 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
5053 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5054 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
5055 return ODP_FIT_ERROR
;
5058 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5063 /* Now parse the encapsulated attributes. */
5064 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5065 attrs
, &present_attrs
, &out_of_range_attr
)) {
5066 return ODP_FIT_ERROR
;
5070 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
5071 return ODP_FIT_ERROR
;
5073 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5074 expected_attrs
, flow
, key
, key_len
,
5077 /* The overall fitness is the worse of the outer and inner attributes. */
5078 return MAX(fitness
, encap_fitness
);
5081 static enum odp_key_fitness
5082 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5083 const struct nlattr
*src_key
, size_t src_key_len
,
5084 struct flow
*flow
, const struct flow
*src_flow
,
5087 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5088 uint64_t expected_attrs
;
5089 uint64_t present_attrs
;
5090 int out_of_range_attr
;
5091 bool is_mask
= src_flow
!= flow
;
5093 memset(flow
, 0, sizeof *flow
);
5095 /* Parse attributes. */
5096 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5097 &out_of_range_attr
)) {
5098 return ODP_FIT_ERROR
;
5103 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5104 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5105 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5106 } else if (is_mask
) {
5107 /* Always exact match recirc_id if it is not specified. */
5108 flow
->recirc_id
= UINT32_MAX
;
5111 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5112 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5113 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5115 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5116 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5117 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5120 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5121 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5122 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5125 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5126 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5128 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5129 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5131 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5132 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5133 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5135 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5136 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5137 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5139 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5140 const ovs_u128
*cl
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5142 flow
->ct_label
= *cl
;
5143 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5146 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5147 enum odp_key_fitness res
;
5149 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
],
5150 is_mask
? src_key
: NULL
,
5151 src_key_len
, &src_flow
->tunnel
,
5152 &flow
->tunnel
, udpif
);
5153 if (res
== ODP_FIT_ERROR
) {
5154 return ODP_FIT_ERROR
;
5155 } else if (res
== ODP_FIT_PERFECT
) {
5156 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5160 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5161 flow
->in_port
.odp_port
5162 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5163 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5164 } else if (!is_mask
) {
5165 flow
->in_port
.odp_port
= ODPP_NONE
;
5168 /* Ethernet header. */
5169 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5170 const struct ovs_key_ethernet
*eth_key
;
5172 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5173 put_ethernet_key(eth_key
, flow
);
5175 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5179 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5182 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5183 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5185 return ODP_FIT_ERROR
;
5189 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5190 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5191 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5192 expected_attrs
, flow
, key
, key_len
, src_flow
);
5195 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5196 flow
->vlan_tci
= htons(0xffff);
5197 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5198 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5199 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5202 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5203 expected_attrs
, flow
, key
, key_len
, src_flow
);
5206 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5207 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5208 * 'key' fits our expectations for what a flow key should contain.
5210 * The 'in_port' will be the datapath's understanding of the port. The
5211 * caller will need to translate with odp_port_to_ofp_port() if the
5212 * OpenFlow port is needed.
5214 * This function doesn't take the packet itself as an argument because none of
5215 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5216 * it is always possible to infer which additional attribute(s) should appear
5217 * by looking at the attributes for lower-level protocols, e.g. if the network
5218 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5219 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5220 * must be absent. */
5221 enum odp_key_fitness
5222 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5225 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, false);
5228 static enum odp_key_fitness
5229 odp_flow_key_to_mask__(const struct nlattr
*mask_key
, size_t mask_key_len
,
5230 const struct nlattr
*flow_key
, size_t flow_key_len
,
5231 struct flow_wildcards
*mask
,
5232 const struct flow
*src_flow
,
5236 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5237 flow_key
, flow_key_len
,
5238 &mask
->masks
, src_flow
, udpif
);
5241 /* A missing mask means that the flow should be exact matched.
5242 * Generate an appropriate exact wildcard for the flow. */
5243 flow_wildcards_init_for_packet(mask
, src_flow
);
5245 return ODP_FIT_PERFECT
;
5248 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5249 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5250 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5251 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5252 * well 'key' fits our expectations for what a flow key should contain. */
5253 enum odp_key_fitness
5254 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5255 const struct nlattr
*flow_key
, size_t flow_key_len
,
5256 struct flow_wildcards
*mask
, const struct flow
*flow
)
5258 return odp_flow_key_to_mask__(mask_key
, mask_key_len
,
5259 flow_key
, flow_key_len
,
5263 /* These functions are similar to their non-"_udpif" variants but output a
5264 * 'flow' that is suitable for fast-path packet processing.
5266 * Some fields have different representation for flow setup and per-
5267 * packet processing (i.e. different between ofproto-dpif and userspace
5268 * datapath). In particular, with the non-"_udpif" functions, struct
5269 * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
5270 * with these functions, struct tun_metadata is in the per-packet format
5271 * (using 'present.len' and 'opts.gnv'). */
5272 enum odp_key_fitness
5273 odp_flow_key_to_flow_udpif(const struct nlattr
*key
, size_t key_len
,
5276 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, true);
5279 enum odp_key_fitness
5280 odp_flow_key_to_mask_udpif(const struct nlattr
*mask_key
, size_t mask_key_len
,
5281 const struct nlattr
*flow_key
, size_t flow_key_len
,
5282 struct flow_wildcards
*mask
,
5283 const struct flow
*flow
)
5285 return odp_flow_key_to_mask__(mask_key
, mask_key_len
,
5286 flow_key
, flow_key_len
,
5290 /* Returns 'fitness' as a string, for use in debug messages. */
5292 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5295 case ODP_FIT_PERFECT
:
5297 case ODP_FIT_TOO_MUCH
:
5299 case ODP_FIT_TOO_LITTLE
:
5300 return "too_little";
5308 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5309 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5310 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5311 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5312 * null, then the return value is not meaningful.) */
5314 odp_put_userspace_action(uint32_t pid
,
5315 const void *userdata
, size_t userdata_size
,
5316 odp_port_t tunnel_out_port
,
5317 bool include_actions
,
5318 struct ofpbuf
*odp_actions
)
5320 size_t userdata_ofs
;
5323 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5324 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5326 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5328 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5329 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5332 * - The kernel rejected shorter userdata with -ERANGE.
5334 * - The kernel silently dropped userdata beyond the first 8 bytes.
5336 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5337 * separately disable features that required more than 8 bytes.) */
5338 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5339 MAX(8, userdata_size
)),
5340 userdata
, userdata_size
);
5344 if (tunnel_out_port
!= ODPP_NONE
) {
5345 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5348 if (include_actions
) {
5349 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5351 nl_msg_end_nested(odp_actions
, offset
);
5353 return userdata_ofs
;
5357 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5358 struct ofpbuf
*odp_actions
)
5360 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5361 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5362 nl_msg_end_nested(odp_actions
, offset
);
5366 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5367 struct ovs_action_push_tnl
*data
)
5369 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5371 size
+= data
->header_len
;
5372 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5376 /* The commit_odp_actions() function and its helpers. */
5379 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5380 const void *key
, size_t key_size
)
5382 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5383 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5384 nl_msg_end_nested(odp_actions
, offset
);
5387 /* Masked set actions have a mask following the data within the netlink
5388 * attribute. The unmasked bits in the data will be cleared as the data
5389 * is copied to the action. */
5391 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5392 enum ovs_key_attr key_type
,
5393 const void *key_
, const void *mask_
, size_t key_size
)
5395 size_t offset
= nl_msg_start_nested(odp_actions
,
5396 OVS_ACTION_ATTR_SET_MASKED
);
5397 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5398 const char *key
= key_
, *mask
= mask_
;
5400 memcpy(data
+ key_size
, mask
, key_size
);
5401 /* Clear unmasked bits while copying. */
5402 while (key_size
--) {
5403 *data
++ = *key
++ & *mask
++;
5405 nl_msg_end_nested(odp_actions
, offset
);
5408 /* If any of the flow key data that ODP actions can modify are different in
5409 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5410 * 'odp_actions' that change the flow tunneling information in key from
5411 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5412 * same way. In other words, operates the same as commit_odp_actions(), but
5413 * only on tunneling information. */
5415 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5416 struct ofpbuf
*odp_actions
)
5418 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5419 * must have non-zero ipv6_dst. */
5420 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5421 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5424 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5425 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5430 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5431 const void *key
, void *base
, void *mask
, size_t size
,
5432 struct ofpbuf
*odp_actions
)
5434 if (memcmp(key
, base
, size
)) {
5435 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5437 if (use_masked_set
&& !fully_masked
) {
5438 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5440 if (!fully_masked
) {
5441 memset(mask
, 0xff, size
);
5443 commit_set_action(odp_actions
, attr
, key
, size
);
5445 memcpy(base
, key
, size
);
5448 /* Mask bits are set when we have either read or set the corresponding
5449 * values. Masked bits will be exact-matched, no need to set them
5450 * if the value did not actually change. */
5456 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5458 eth
->eth_src
= flow
->dl_src
;
5459 eth
->eth_dst
= flow
->dl_dst
;
5463 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5465 flow
->dl_src
= eth
->eth_src
;
5466 flow
->dl_dst
= eth
->eth_dst
;
5470 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5471 struct ofpbuf
*odp_actions
,
5472 struct flow_wildcards
*wc
,
5475 struct ovs_key_ethernet key
, base
, mask
;
5477 get_ethernet_key(flow
, &key
);
5478 get_ethernet_key(base_flow
, &base
);
5479 get_ethernet_key(&wc
->masks
, &mask
);
5481 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5482 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5483 put_ethernet_key(&base
, base_flow
);
5484 put_ethernet_key(&mask
, &wc
->masks
);
5489 pop_vlan(struct flow
*base
,
5490 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5492 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5494 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5495 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5501 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5502 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5504 if (base
->vlan_tci
== vlan_tci
) {
5508 pop_vlan(base
, odp_actions
, wc
);
5509 if (vlan_tci
& htons(VLAN_CFI
)) {
5510 struct ovs_action_push_vlan vlan
;
5512 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5513 vlan
.vlan_tci
= vlan_tci
;
5514 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5515 &vlan
, sizeof vlan
);
5517 base
->vlan_tci
= vlan_tci
;
5520 /* Wildcarding already done at action translation time. */
5522 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5523 struct ofpbuf
*odp_actions
)
5525 int base_n
= flow_count_mpls_labels(base
, NULL
);
5526 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5527 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5530 while (base_n
> common_n
) {
5531 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5532 /* If there is only one more LSE in base than there are common
5533 * between base and flow; and flow has at least one more LSE than
5534 * is common then the topmost LSE of base may be updated using
5536 struct ovs_key_mpls mpls_key
;
5538 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5539 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5540 &mpls_key
, sizeof mpls_key
);
5541 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5544 /* Otherwise, if there more LSEs in base than are common between
5545 * base and flow then pop the topmost one. */
5549 /* If all the LSEs are to be popped and this is not the outermost
5550 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5551 * POP_MPLS action instead of flow->dl_type.
5553 * This is because the POP_MPLS action requires its ethertype
5554 * argument to be an MPLS ethernet type but in this case
5555 * flow->dl_type will be a non-MPLS ethernet type.
5557 * When the final POP_MPLS action occurs it use flow->dl_type and
5558 * the and the resulting packet will have the desired dl_type. */
5559 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5560 dl_type
= htons(ETH_TYPE_MPLS
);
5562 dl_type
= flow
->dl_type
;
5564 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5565 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5571 /* If, after the above popping and setting, there are more LSEs in flow
5572 * than base then some LSEs need to be pushed. */
5573 while (base_n
< flow_n
) {
5574 struct ovs_action_push_mpls
*mpls
;
5576 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5577 OVS_ACTION_ATTR_PUSH_MPLS
,
5579 mpls
->mpls_ethertype
= flow
->dl_type
;
5580 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5581 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
);
5582 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5588 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5590 ipv4
->ipv4_src
= flow
->nw_src
;
5591 ipv4
->ipv4_dst
= flow
->nw_dst
;
5592 ipv4
->ipv4_proto
= flow
->nw_proto
;
5593 ipv4
->ipv4_tos
= flow
->nw_tos
;
5594 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5595 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5599 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5601 flow
->nw_src
= ipv4
->ipv4_src
;
5602 flow
->nw_dst
= ipv4
->ipv4_dst
;
5603 flow
->nw_proto
= ipv4
->ipv4_proto
;
5604 flow
->nw_tos
= ipv4
->ipv4_tos
;
5605 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5606 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5610 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5611 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5614 struct ovs_key_ipv4 key
, mask
, base
;
5616 /* Check that nw_proto and nw_frag remain unchanged. */
5617 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5618 flow
->nw_frag
== base_flow
->nw_frag
);
5620 get_ipv4_key(flow
, &key
, false);
5621 get_ipv4_key(base_flow
, &base
, false);
5622 get_ipv4_key(&wc
->masks
, &mask
, true);
5623 mask
.ipv4_proto
= 0; /* Not writeable. */
5624 mask
.ipv4_frag
= 0; /* Not writable. */
5626 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5628 put_ipv4_key(&base
, base_flow
, false);
5629 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5630 put_ipv4_key(&mask
, &wc
->masks
, true);
5636 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5638 memcpy(ipv6
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6
->ipv6_src
);
5639 memcpy(ipv6
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6
->ipv6_dst
);
5640 ipv6
->ipv6_label
= flow
->ipv6_label
;
5641 ipv6
->ipv6_proto
= flow
->nw_proto
;
5642 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5643 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5644 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5648 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5650 memcpy(&flow
->ipv6_src
, ipv6
->ipv6_src
, sizeof flow
->ipv6_src
);
5651 memcpy(&flow
->ipv6_dst
, ipv6
->ipv6_dst
, sizeof flow
->ipv6_dst
);
5652 flow
->ipv6_label
= ipv6
->ipv6_label
;
5653 flow
->nw_proto
= ipv6
->ipv6_proto
;
5654 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5655 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5656 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5660 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5661 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5664 struct ovs_key_ipv6 key
, mask
, base
;
5666 /* Check that nw_proto and nw_frag remain unchanged. */
5667 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5668 flow
->nw_frag
== base_flow
->nw_frag
);
5670 get_ipv6_key(flow
, &key
, false);
5671 get_ipv6_key(base_flow
, &base
, false);
5672 get_ipv6_key(&wc
->masks
, &mask
, true);
5673 mask
.ipv6_proto
= 0; /* Not writeable. */
5674 mask
.ipv6_frag
= 0; /* Not writable. */
5676 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5678 put_ipv6_key(&base
, base_flow
, false);
5679 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5680 put_ipv6_key(&mask
, &wc
->masks
, true);
5686 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5688 /* ARP key has padding, clear it. */
5689 memset(arp
, 0, sizeof *arp
);
5691 arp
->arp_sip
= flow
->nw_src
;
5692 arp
->arp_tip
= flow
->nw_dst
;
5693 arp
->arp_op
= htons(flow
->nw_proto
);
5694 arp
->arp_sha
= flow
->arp_sha
;
5695 arp
->arp_tha
= flow
->arp_tha
;
5699 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5701 flow
->nw_src
= arp
->arp_sip
;
5702 flow
->nw_dst
= arp
->arp_tip
;
5703 flow
->nw_proto
= ntohs(arp
->arp_op
);
5704 flow
->arp_sha
= arp
->arp_sha
;
5705 flow
->arp_tha
= arp
->arp_tha
;
5708 static enum slow_path_reason
5709 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5710 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5712 struct ovs_key_arp key
, mask
, base
;
5714 get_arp_key(flow
, &key
);
5715 get_arp_key(base_flow
, &base
);
5716 get_arp_key(&wc
->masks
, &mask
);
5718 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5720 put_arp_key(&base
, base_flow
);
5721 put_arp_key(&mask
, &wc
->masks
);
5728 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5730 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5731 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5732 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5736 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5738 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5739 flow
->tp_src
= htons(icmp
->icmp_type
);
5740 flow
->tp_dst
= htons(icmp
->icmp_code
);
5743 static enum slow_path_reason
5744 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5745 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5747 struct ovs_key_icmp key
, mask
, base
;
5748 enum ovs_key_attr attr
;
5750 if (is_icmpv4(flow
, NULL
)) {
5751 attr
= OVS_KEY_ATTR_ICMP
;
5752 } else if (is_icmpv6(flow
, NULL
)) {
5753 attr
= OVS_KEY_ATTR_ICMPV6
;
5758 get_icmp_key(flow
, &key
);
5759 get_icmp_key(base_flow
, &base
);
5760 get_icmp_key(&wc
->masks
, &mask
);
5762 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5763 put_icmp_key(&base
, base_flow
);
5764 put_icmp_key(&mask
, &wc
->masks
);
5771 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5773 memcpy(nd
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
5774 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5775 nd
->nd_sll
= flow
->arp_sha
;
5776 nd
->nd_tll
= flow
->arp_tha
;
5780 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5782 memcpy(&flow
->nd_target
, nd
->nd_target
, sizeof flow
->nd_target
);
5783 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5784 flow
->arp_sha
= nd
->nd_sll
;
5785 flow
->arp_tha
= nd
->nd_tll
;
5788 static enum slow_path_reason
5789 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5790 struct ofpbuf
*odp_actions
,
5791 struct flow_wildcards
*wc
, bool use_masked
)
5793 struct ovs_key_nd key
, mask
, base
;
5795 get_nd_key(flow
, &key
);
5796 get_nd_key(base_flow
, &base
);
5797 get_nd_key(&wc
->masks
, &mask
);
5799 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5801 put_nd_key(&base
, base_flow
);
5802 put_nd_key(&mask
, &wc
->masks
);
5809 static enum slow_path_reason
5810 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5811 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5814 /* Check if 'flow' really has an L3 header. */
5815 if (!flow
->nw_proto
) {
5819 switch (ntohs(base
->dl_type
)) {
5821 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5825 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5826 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5829 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5835 /* TCP, UDP, and SCTP keys have the same layout. */
5836 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5837 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5840 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5842 tp
->tcp
.tcp_src
= flow
->tp_src
;
5843 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5847 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5849 flow
->tp_src
= tp
->tcp
.tcp_src
;
5850 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5854 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5855 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5858 enum ovs_key_attr key_type
;
5859 union ovs_key_tp key
, mask
, base
;
5861 /* Check if 'flow' really has an L3 header. */
5862 if (!flow
->nw_proto
) {
5866 if (!is_ip_any(base_flow
)) {
5870 if (flow
->nw_proto
== IPPROTO_TCP
) {
5871 key_type
= OVS_KEY_ATTR_TCP
;
5872 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5873 key_type
= OVS_KEY_ATTR_UDP
;
5874 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5875 key_type
= OVS_KEY_ATTR_SCTP
;
5880 get_tp_key(flow
, &key
);
5881 get_tp_key(base_flow
, &base
);
5882 get_tp_key(&wc
->masks
, &mask
);
5884 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5886 put_tp_key(&base
, base_flow
);
5887 put_tp_key(&mask
, &wc
->masks
);
5892 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5893 struct ofpbuf
*odp_actions
,
5894 struct flow_wildcards
*wc
,
5897 uint32_t key
, mask
, base
;
5899 key
= flow
->skb_priority
;
5900 base
= base_flow
->skb_priority
;
5901 mask
= wc
->masks
.skb_priority
;
5903 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5904 sizeof key
, odp_actions
)) {
5905 base_flow
->skb_priority
= base
;
5906 wc
->masks
.skb_priority
= mask
;
5911 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5912 struct ofpbuf
*odp_actions
,
5913 struct flow_wildcards
*wc
,
5916 uint32_t key
, mask
, base
;
5918 key
= flow
->pkt_mark
;
5919 base
= base_flow
->pkt_mark
;
5920 mask
= wc
->masks
.pkt_mark
;
5922 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5923 sizeof key
, odp_actions
)) {
5924 base_flow
->pkt_mark
= base
;
5925 wc
->masks
.pkt_mark
= mask
;
5929 /* If any of the flow key data that ODP actions can modify are different in
5930 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5931 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5932 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5933 * in addition to this function if needed. Sets fields in 'wc' that are
5934 * used as part of the action.
5936 * Returns a reason to force processing the flow's packets into the userspace
5937 * slow path, if there is one, otherwise 0. */
5938 enum slow_path_reason
5939 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5940 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5943 enum slow_path_reason slow1
, slow2
;
5945 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5946 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5947 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5948 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5949 commit_mpls_action(flow
, base
, odp_actions
);
5950 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5951 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5952 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5954 return slow1
? slow1
: slow2
;