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 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
323 ds_put_cstr(ds
, ",ingress");
324 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
325 ds_put_cstr(ds
, ",egress");
327 ds_put_char(ds
, ')');
328 } else if (userdata_len
>= sizeof cookie
.ipfix
329 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
330 ds_put_format(ds
, ",ipfix(output_port=%"PRIu32
")",
331 cookie
.ipfix
.output_odp_port
);
333 userdata_unspec
= true;
337 if (userdata_unspec
) {
339 ds_put_format(ds
, ",userdata(");
340 for (i
= 0; i
< userdata_len
; i
++) {
341 ds_put_format(ds
, "%02x", userdata
[i
]);
343 ds_put_char(ds
, ')');
347 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
348 ds_put_cstr(ds
, ",actions");
351 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
352 if (tunnel_out_port_attr
) {
353 ds_put_format(ds
, ",tunnel_out_port=%"PRIu32
,
354 nl_attr_get_u32(tunnel_out_port_attr
));
357 ds_put_char(ds
, ')');
361 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
363 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
364 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
365 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
366 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
368 ds_put_char(ds
, ',');
370 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
371 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
372 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
373 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
375 ds_put_char(ds
, ',');
377 if (!(tci
& htons(VLAN_CFI
))) {
378 ds_put_cstr(ds
, "cfi=0");
379 ds_put_char(ds
, ',');
385 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
387 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
388 mpls_lse_to_label(mpls_lse
),
389 mpls_lse_to_tc(mpls_lse
),
390 mpls_lse_to_ttl(mpls_lse
),
391 mpls_lse_to_bos(mpls_lse
));
395 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
396 const struct ovs_key_mpls
*mpls_mask
, int n
)
398 for (int i
= 0; i
< n
; i
++) {
399 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
401 if (mpls_mask
== NULL
) {
402 format_mpls_lse(ds
, key
);
404 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
406 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
407 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
408 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
409 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
410 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
412 ds_put_char(ds
, ',');
418 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
420 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
424 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
426 ds_put_format(ds
, "hash(");
428 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
429 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
431 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
434 ds_put_format(ds
, ")");
438 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
440 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
441 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
442 ntohs(udp
->udp_csum
));
448 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
450 const struct eth_header
*eth
;
453 const struct udp_header
*udp
;
455 eth
= (const struct eth_header
*)data
->header
;
460 ds_put_format(ds
, "header(size=%"PRIu8
",type=%"PRIu8
",eth(dst=",
461 data
->header_len
, data
->tnl_type
);
462 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
463 ds_put_format(ds
, ",src=");
464 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
465 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
467 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
469 const struct ip_header
*ip
;
470 ip
= (const struct ip_header
*) l3
;
471 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
472 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
473 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
474 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
475 ip
->ip_proto
, ip
->ip_tos
,
477 ntohs(ip
->ip_frag_off
));
480 const struct ip6_hdr
*ip6
;
481 ip6
= (const struct ip6_hdr
*) l3
;
482 ds_put_format(ds
, "ipv6(src=");
483 ipv6_format_addr(&ip6
->ip6_src
, ds
);
484 ds_put_format(ds
, ",dst=");
485 ipv6_format_addr(&ip6
->ip6_dst
, ds
);
486 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx8
487 ",hlimit=%"PRIu8
"),",
488 ntohl(ip6
->ip6_flow
) & IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
489 (ntohl(ip6
->ip6_flow
) >> 20) & 0xff, ip6
->ip6_hlim
);
493 udp
= (const struct udp_header
*) l4
;
495 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
496 const struct vxlanhdr
*vxh
;
498 vxh
= format_udp_tnl_push_header(ds
, udp
);
500 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
501 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
502 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
503 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
504 const struct genevehdr
*gnh
;
506 gnh
= format_udp_tnl_push_header(ds
, udp
);
508 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
509 gnh
->oam
? "oam," : "",
510 gnh
->critical
? "crit," : "",
511 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
514 ds_put_cstr(ds
, ",options(");
515 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
517 ds_put_char(ds
, ')');
520 ds_put_char(ds
, ')');
521 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
522 const struct gre_base_hdr
*greh
;
523 ovs_16aligned_be32
*options
;
525 greh
= (const struct gre_base_hdr
*) l4
;
527 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
528 ntohs(greh
->flags
), ntohs(greh
->protocol
));
529 options
= (ovs_16aligned_be32
*)(greh
+ 1);
530 if (greh
->flags
& htons(GRE_CSUM
)) {
531 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
534 if (greh
->flags
& htons(GRE_KEY
)) {
535 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
538 if (greh
->flags
& htons(GRE_SEQ
)) {
539 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
542 ds_put_format(ds
, ")");
544 ds_put_format(ds
, ")");
548 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
)
550 struct ovs_action_push_tnl
*data
;
552 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
554 ds_put_format(ds
, "tnl_push(tnl_port(%"PRIu32
"),", data
->tnl_port
);
555 format_odp_tnl_push_header(ds
, data
);
556 ds_put_format(ds
, ",out_port(%"PRIu32
"))", data
->out_port
);
559 static const struct nl_policy ovs_nat_policy
[] = {
560 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
561 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
562 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
563 .min_len
= sizeof(struct in_addr
),
564 .max_len
= sizeof(struct in6_addr
)},
565 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
566 .min_len
= sizeof(struct in_addr
),
567 .max_len
= sizeof(struct in6_addr
)},
568 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
569 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
570 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
571 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
572 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
576 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
578 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
580 ovs_be32 ip_min
, ip_max
;
581 struct in6_addr ip6_min
, ip6_max
;
582 uint16_t proto_min
, proto_max
;
584 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
585 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
588 /* If no type, then nothing else either. */
589 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
590 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
591 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
592 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
593 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
594 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
597 /* Both SNAT & DNAT may not be specified. */
598 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
599 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
602 /* proto may not appear without ip. */
603 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
604 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
607 /* MAX may not appear without MIN. */
608 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
609 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
610 ds_put_cstr(ds
, "nat(error: range max without min.)");
613 /* Address sizes must match. */
614 if ((a
[OVS_NAT_ATTR_IP_MIN
]
615 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
616 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
617 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
618 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
619 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
620 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
624 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
625 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
626 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
627 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
628 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
629 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
630 if (addr_len
== sizeof ip6_min
) {
631 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
632 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
634 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
635 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
638 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
639 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
640 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
641 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
643 if ((addr_len
== sizeof(ovs_be32
)
644 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
645 || (addr_len
== sizeof(struct in6_addr
)
646 && !ipv6_mask_is_any(&ip6_max
)
647 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
648 || (proto_max
&& proto_min
> proto_max
)) {
649 ds_put_cstr(ds
, "nat(range error)");
653 ds_put_cstr(ds
, "nat");
654 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
655 ds_put_char(ds
, '(');
656 if (a
[OVS_NAT_ATTR_SRC
]) {
657 ds_put_cstr(ds
, "src");
658 } else if (a
[OVS_NAT_ATTR_DST
]) {
659 ds_put_cstr(ds
, "dst");
663 ds_put_cstr(ds
, "=");
665 if (addr_len
== sizeof ip_min
) {
666 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
668 if (ip_max
&& ip_max
!= ip_min
) {
669 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
671 } else if (addr_len
== sizeof ip6_min
) {
672 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
674 if (!ipv6_mask_is_any(&ip6_max
) &&
675 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
676 ds_put_char(ds
, '-');
677 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
681 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
683 if (proto_max
&& proto_max
!= proto_min
) {
684 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
688 ds_put_char(ds
, ',');
689 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
690 ds_put_cstr(ds
, "persistent,");
692 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
693 ds_put_cstr(ds
, "hash,");
695 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
696 ds_put_cstr(ds
, "random,");
699 ds_put_char(ds
, ')');
703 static const struct nl_policy ovs_conntrack_policy
[] = {
704 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
705 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
706 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
707 .min_len
= sizeof(uint32_t) * 2 },
708 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
709 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
710 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
711 .min_len
= 1, .max_len
= 16 },
712 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
716 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
718 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
719 const ovs_u128
*label
;
720 const uint32_t *mark
;
724 const struct nlattr
*nat
;
726 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
727 ds_put_cstr(ds
, "ct(error)");
731 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
732 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
733 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
734 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
735 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
736 nat
= a
[OVS_CT_ATTR_NAT
];
738 ds_put_format(ds
, "ct");
739 if (commit
|| zone
|| mark
|| label
|| helper
|| nat
) {
740 ds_put_cstr(ds
, "(");
742 ds_put_format(ds
, "commit,");
745 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
748 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
752 ds_put_format(ds
, "label=");
753 format_u128(ds
, label
, label
+ 1, true);
754 ds_put_char(ds
, ',');
757 ds_put_format(ds
, "helper=%s,", helper
);
760 format_odp_ct_nat(ds
, nat
);
763 ds_put_cstr(ds
, ")");
768 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
771 enum ovs_action_attr type
= nl_attr_type(a
);
774 expected_len
= odp_action_len(nl_attr_type(a
));
775 if (expected_len
!= ATTR_LEN_VARIABLE
&&
776 nl_attr_get_size(a
) != expected_len
) {
777 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
778 nl_attr_get_size(a
), expected_len
);
779 format_generic_odp_action(ds
, a
);
784 case OVS_ACTION_ATTR_OUTPUT
:
785 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
787 case OVS_ACTION_ATTR_TRUNC
: {
788 const struct ovs_action_trunc
*trunc
=
789 nl_attr_get_unspec(a
, sizeof *trunc
);
791 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
795 case OVS_ACTION_ATTR_TUNNEL_POP
:
796 ds_put_format(ds
, "tnl_pop(%"PRIu32
")", nl_attr_get_u32(a
));
798 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
799 format_odp_tnl_push_action(ds
, a
);
801 case OVS_ACTION_ATTR_USERSPACE
:
802 format_odp_userspace_action(ds
, a
);
804 case OVS_ACTION_ATTR_RECIRC
:
805 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
807 case OVS_ACTION_ATTR_HASH
:
808 format_odp_hash_action(ds
, nl_attr_get(a
));
810 case OVS_ACTION_ATTR_SET_MASKED
:
812 size
= nl_attr_get_size(a
) / 2;
813 ds_put_cstr(ds
, "set(");
815 /* Masked set action not supported for tunnel key, which is bigger. */
816 if (size
<= sizeof(struct ovs_key_ipv6
)) {
817 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
818 sizeof(struct nlattr
))];
819 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
820 sizeof(struct nlattr
))];
822 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
823 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
824 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
825 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
826 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
828 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
830 ds_put_cstr(ds
, ")");
832 case OVS_ACTION_ATTR_SET
:
833 ds_put_cstr(ds
, "set(");
834 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
835 ds_put_cstr(ds
, ")");
837 case OVS_ACTION_ATTR_PUSH_VLAN
: {
838 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
839 ds_put_cstr(ds
, "push_vlan(");
840 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
841 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
843 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
844 ds_put_char(ds
, ')');
847 case OVS_ACTION_ATTR_POP_VLAN
:
848 ds_put_cstr(ds
, "pop_vlan");
850 case OVS_ACTION_ATTR_PUSH_MPLS
: {
851 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
852 ds_put_cstr(ds
, "push_mpls(");
853 format_mpls_lse(ds
, mpls
->mpls_lse
);
854 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
857 case OVS_ACTION_ATTR_POP_MPLS
: {
858 ovs_be16 ethertype
= nl_attr_get_be16(a
);
859 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
862 case OVS_ACTION_ATTR_SAMPLE
:
863 format_odp_sample_action(ds
, a
);
865 case OVS_ACTION_ATTR_CT
:
866 format_odp_conntrack_action(ds
, a
);
868 case OVS_ACTION_ATTR_UNSPEC
:
869 case __OVS_ACTION_ATTR_MAX
:
871 format_generic_odp_action(ds
, a
);
877 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
881 const struct nlattr
*a
;
884 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
886 ds_put_char(ds
, ',');
888 format_odp_action(ds
, a
);
893 if (left
== actions_len
) {
894 ds_put_cstr(ds
, "<empty>");
896 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
897 for (i
= 0; i
< left
; i
++) {
898 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
900 ds_put_char(ds
, ')');
903 ds_put_cstr(ds
, "drop");
907 /* Separate out parse_odp_userspace_action() function. */
909 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
912 union user_action_cookie cookie
;
914 odp_port_t tunnel_out_port
;
916 void *user_data
= NULL
;
917 size_t user_data_size
= 0;
918 bool include_actions
= false;
921 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
925 ofpbuf_init(&buf
, 16);
929 uint32_t probability
;
930 uint32_t collector_set_id
;
931 uint32_t obs_domain_id
;
932 uint32_t obs_point_id
;
935 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
936 "pcp=%i,output=%"SCNi32
")%n",
937 &vid
, &pcp
, &output
, &n1
)) {
941 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
946 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
947 cookie
.sflow
.vlan_tci
= htons(tci
);
948 cookie
.sflow
.output
= output
;
950 user_data_size
= sizeof cookie
.sflow
;
951 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
954 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
955 cookie
.slow_path
.unused
= 0;
956 cookie
.slow_path
.reason
= 0;
958 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
959 &cookie
.slow_path
.reason
,
960 SLOW_PATH_REASON_MASK
, NULL
);
961 if (res
< 0 || s
[n
+ res
] != ')') {
967 user_data_size
= sizeof cookie
.slow_path
;
968 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
969 "collector_set_id=%"SCNi32
","
970 "obs_domain_id=%"SCNi32
","
971 "obs_point_id=%"SCNi32
","
972 "output_port=%"SCNi32
"%n",
973 &probability
, &collector_set_id
,
974 &obs_domain_id
, &obs_point_id
,
978 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
979 cookie
.flow_sample
.probability
= probability
;
980 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
981 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
982 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
983 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
985 user_data_size
= sizeof cookie
.flow_sample
;
987 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
988 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
990 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
991 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
994 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1001 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1004 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1005 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1006 user_data
= &cookie
;
1007 user_data_size
= sizeof cookie
.ipfix
;
1008 } else if (ovs_scan(&s
[n
], ",userdata(%n",
1013 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1014 if (end
[0] != ')') {
1018 user_data
= buf
.data
;
1019 user_data_size
= buf
.size
;
1026 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1028 include_actions
= true;
1034 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1035 &tunnel_out_port
, &n1
)) {
1036 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1037 tunnel_out_port
, include_actions
, actions
);
1039 } else if (s
[n
] == ')') {
1040 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1041 ODPP_NONE
, include_actions
, actions
);
1048 ofpbuf_uninit(&buf
);
1053 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1055 struct eth_header
*eth
;
1056 struct ip_header
*ip
;
1057 struct ovs_16aligned_ip6_hdr
*ip6
;
1058 struct udp_header
*udp
;
1059 struct gre_base_hdr
*greh
;
1060 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1062 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1066 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1069 eth
= (struct eth_header
*) data
->header
;
1070 l3
= (data
->header
+ sizeof *eth
);
1071 ip
= (struct ip_header
*) l3
;
1072 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1073 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1074 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1077 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1081 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1082 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1085 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1088 eth
->eth_type
= htons(dl_type
);
1090 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1092 uint16_t ip_frag_off
;
1093 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1094 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1097 &ip
->ip_proto
, &ip
->ip_tos
,
1098 &ip
->ip_ttl
, &ip_frag_off
)) {
1101 put_16aligned_be32(&ip
->ip_src
, sip
);
1102 put_16aligned_be32(&ip
->ip_dst
, dip
);
1103 ip
->ip_frag_off
= htons(ip_frag_off
);
1104 ip_len
= sizeof *ip
;
1106 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1107 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1108 struct in6_addr sip6
, dip6
;
1111 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1112 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1113 ",hlimit=%"SCNi8
"),",
1114 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1115 &tclass
, &ip6
->ip6_hlim
)
1116 || (label
& ~IPV6_LABEL_MASK
) != 0
1117 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1118 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1121 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1122 htonl(tclass
<< 20) | htonl(label
));
1123 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1124 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1125 ip_len
= sizeof *ip6
;
1129 l4
= ((uint8_t *) l3
+ ip_len
);
1130 udp
= (struct udp_header
*) l4
;
1131 greh
= (struct gre_base_hdr
*) l4
;
1132 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1133 &udp_src
, &udp_dst
, &csum
)) {
1134 uint32_t vx_flags
, vni
;
1136 udp
->udp_src
= htons(udp_src
);
1137 udp
->udp_dst
= htons(udp_dst
);
1139 udp
->udp_csum
= htons(csum
);
1141 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1143 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1145 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1146 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1147 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1148 header_len
= sizeof *eth
+ ip_len
+
1149 sizeof *udp
+ sizeof *vxh
;
1150 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1151 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1153 memset(gnh
, 0, sizeof *gnh
);
1154 header_len
= sizeof *eth
+ ip_len
+
1155 sizeof *udp
+ sizeof *gnh
;
1157 if (ovs_scan_len(s
, &n
, "oam,")) {
1160 if (ovs_scan_len(s
, &n
, "crit,")) {
1163 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1166 if (ovs_scan_len(s
, &n
, ",options(")) {
1167 struct geneve_scan options
;
1170 memset(&options
, 0, sizeof options
);
1171 len
= scan_geneve(s
+ n
, &options
, NULL
);
1176 memcpy(gnh
->options
, options
.d
, options
.len
);
1177 gnh
->opt_len
= options
.len
/ 4;
1178 header_len
+= options
.len
;
1182 if (!ovs_scan_len(s
, &n
, "))")) {
1186 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1187 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1188 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1192 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1193 &gre_flags
, &gre_proto
)){
1195 tnl_type
= OVS_VPORT_TYPE_GRE
;
1196 greh
->flags
= htons(gre_flags
);
1197 greh
->protocol
= htons(gre_proto
);
1198 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1200 if (greh
->flags
& htons(GRE_CSUM
)) {
1201 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1205 memset(options
, 0, sizeof *options
);
1206 *((ovs_be16
*)options
) = htons(csum
);
1209 if (greh
->flags
& htons(GRE_KEY
)) {
1212 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1216 put_16aligned_be32(options
, htonl(key
));
1219 if (greh
->flags
& htons(GRE_SEQ
)) {
1222 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1225 put_16aligned_be32(options
, htonl(seq
));
1229 if (!ovs_scan_len(s
, &n
, "))")) {
1233 header_len
= sizeof *eth
+ ip_len
+
1234 ((uint8_t *) options
- (uint8_t *) greh
);
1239 /* check tunnel meta data. */
1240 if (data
->tnl_type
!= tnl_type
) {
1243 if (data
->header_len
!= header_len
) {
1248 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1255 struct ct_nat_params
{
1261 struct in6_addr ip6
;
1265 struct in6_addr ip6
;
1275 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1277 if (ovs_scan_len(s
, n
, "=")) {
1278 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1279 struct in6_addr ipv6
;
1281 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1282 p
->addr_len
= sizeof p
->addr_min
.ip
;
1283 if (ovs_scan_len(s
, n
, "-")) {
1284 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1285 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1289 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1290 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1291 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1292 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1293 p
->addr_min
.ip6
= ipv6
;
1294 if (ovs_scan_len(s
, n
, "-")) {
1295 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1296 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1297 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1298 p
->addr_max
.ip6
= ipv6
;
1306 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1307 if (ovs_scan_len(s
, n
, "-")) {
1308 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1318 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1322 if (ovs_scan_len(s
, &n
, "nat")) {
1323 memset(p
, 0, sizeof *p
);
1325 if (ovs_scan_len(s
, &n
, "(")) {
1329 end
= strchr(s
+ n
, ')');
1336 n
+= strspn(s
+ n
, delimiters
);
1337 if (ovs_scan_len(s
, &n
, "src")) {
1338 int err
= scan_ct_nat_range(s
, &n
, p
);
1345 if (ovs_scan_len(s
, &n
, "dst")) {
1346 int err
= scan_ct_nat_range(s
, &n
, p
);
1353 if (ovs_scan_len(s
, &n
, "persistent")) {
1354 p
->persistent
= true;
1357 if (ovs_scan_len(s
, &n
, "hash")) {
1358 p
->proto_hash
= true;
1361 if (ovs_scan_len(s
, &n
, "random")) {
1362 p
->proto_random
= true;
1368 if (p
->snat
&& p
->dnat
) {
1371 if ((p
->addr_len
!= 0 &&
1372 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1373 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1374 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1377 if (p
->proto_hash
&& p
->proto_random
) {
1387 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1389 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1392 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1393 } else if (p
->dnat
) {
1394 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1398 if (p
->addr_len
!= 0) {
1399 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1401 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1402 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1406 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1407 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1408 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1411 if (p
->persistent
) {
1412 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1414 if (p
->proto_hash
) {
1415 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1417 if (p
->proto_random
) {
1418 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1422 nl_msg_end_nested(actions
, start
);
1426 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1430 if (ovs_scan(s
, "ct")) {
1431 const char *helper
= NULL
;
1432 size_t helper_len
= 0;
1433 bool commit
= false;
1438 } ct_mark
= { 0, 0 };
1443 struct ct_nat_params nat_params
;
1444 bool have_nat
= false;
1448 memset(&ct_label
, 0, sizeof(ct_label
));
1451 if (ovs_scan(s
, "(")) {
1454 end
= strchr(s
, ')');
1462 s
+= strspn(s
, delimiters
);
1463 if (ovs_scan(s
, "commit%n", &n
)) {
1468 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1472 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1475 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1478 ct_mark
.mask
= UINT32_MAX
;
1482 if (ovs_scan(s
, "label=%n", &n
)) {
1486 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1493 if (ovs_scan(s
, "helper=%n", &n
)) {
1495 helper_len
= strcspn(s
, delimiters_end
);
1496 if (!helper_len
|| helper_len
> 15) {
1504 n
= scan_ct_nat(s
, &nat_params
);
1509 /* end points to the end of the nested, nat action.
1510 * find the real end. */
1513 /* Nothing matched. */
1519 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1521 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1524 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1527 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1530 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1531 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1535 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1539 nl_msg_put_ct_nat(&nat_params
, actions
);
1541 nl_msg_end_nested(actions
, start
);
1548 parse_odp_action(const char *s
, const struct simap
*port_names
,
1549 struct ofpbuf
*actions
)
1555 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1556 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1565 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
1566 struct ovs_action_trunc
*trunc
;
1568 trunc
= nl_msg_put_unspec_uninit(actions
,
1569 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1570 trunc
->max_len
= max_len
;
1576 int len
= strcspn(s
, delimiters
);
1577 struct simap_node
*node
;
1579 node
= simap_find_len(port_names
, s
, len
);
1581 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1590 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1591 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1596 if (!strncmp(s
, "userspace(", 10)) {
1597 return parse_odp_userspace_action(s
, actions
);
1600 if (!strncmp(s
, "set(", 4)) {
1603 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1604 struct ofpbuf maskbuf
;
1605 struct nlattr
*nested
, *key
;
1608 /* 'mask' is big enough to hold any key. */
1609 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1611 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1612 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1616 if (s
[retval
+ 4] != ')') {
1620 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1623 size
= nl_attr_get_size(mask
);
1624 if (size
== nl_attr_get_size(key
)) {
1625 /* Change to masked set action if not fully masked. */
1626 if (!is_all_ones(mask
+ 1, size
)) {
1627 key
->nla_len
+= size
;
1628 ofpbuf_put(actions
, mask
+ 1, size
);
1629 /* 'actions' may have been reallocated by ofpbuf_put(). */
1630 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1631 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1635 nl_msg_end_nested(actions
, start_ofs
);
1640 struct ovs_action_push_vlan push
;
1641 int tpid
= ETH_TYPE_VLAN
;
1646 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1647 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1648 &vid
, &pcp
, &cfi
, &n
)
1649 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1650 &tpid
, &vid
, &pcp
, &n
)
1651 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1652 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1653 push
.vlan_tpid
= htons(tpid
);
1654 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1655 | (pcp
<< VLAN_PCP_SHIFT
)
1656 | (cfi
? VLAN_CFI
: 0));
1657 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1658 &push
, sizeof push
);
1664 if (!strncmp(s
, "pop_vlan", 8)) {
1665 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1673 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1674 && percentage
>= 0. && percentage
<= 100.0) {
1675 size_t sample_ofs
, actions_ofs
;
1678 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1679 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1680 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1681 (probability
<= 0 ? 0
1682 : probability
>= UINT32_MAX
? UINT32_MAX
1685 actions_ofs
= nl_msg_start_nested(actions
,
1686 OVS_SAMPLE_ATTR_ACTIONS
);
1690 n
+= strspn(s
+ n
, delimiters
);
1695 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1701 nl_msg_end_nested(actions
, actions_ofs
);
1702 nl_msg_end_nested(actions
, sample_ofs
);
1704 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1712 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1713 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1721 retval
= parse_conntrack_action(s
, actions
);
1728 struct ovs_action_push_tnl data
;
1731 n
= ovs_parse_tnl_push(s
, &data
);
1733 odp_put_tnl_push_action(actions
, &data
);
1742 /* Parses the string representation of datapath actions, in the format output
1743 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1744 * value. On success, the ODP actions are appended to 'actions' as a series of
1745 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1746 * way, 'actions''s data might be reallocated. */
1748 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1749 struct ofpbuf
*actions
)
1753 if (!strcasecmp(s
, "drop")) {
1757 old_size
= actions
->size
;
1761 s
+= strspn(s
, delimiters
);
1766 retval
= parse_odp_action(s
, port_names
, actions
);
1767 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1768 actions
->size
= old_size
;
1777 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1778 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1781 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1782 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1783 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1784 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1785 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1786 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1787 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1788 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1789 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1790 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1791 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1792 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1793 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1794 .next
= ovs_vxlan_ext_attr_lens
,
1795 .next_max
= OVS_VXLAN_EXT_MAX
},
1796 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1797 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1800 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1801 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1802 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1803 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1804 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1805 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1806 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1807 .next
= ovs_tun_key_attr_lens
,
1808 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1809 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1810 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1811 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1812 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1813 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1814 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1815 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1816 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1817 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1818 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1819 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1820 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1821 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1822 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1823 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1824 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1825 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1826 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1827 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1830 /* Returns the correct length of the payload for a flow key attribute of the
1831 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1832 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1833 * payload is a nested type. */
1835 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1837 if (type
> max_len
) {
1838 return ATTR_LEN_INVALID
;
1841 return tbl
[type
].len
;
1845 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1847 size_t len
= nl_attr_get_size(a
);
1849 const uint8_t *unspec
;
1852 unspec
= nl_attr_get(a
);
1853 for (i
= 0; i
< len
; i
++) {
1855 ds_put_char(ds
, ' ');
1857 ds_put_format(ds
, "%02x", unspec
[i
]);
1863 ovs_frag_type_to_string(enum ovs_frag_type type
)
1866 case OVS_FRAG_TYPE_NONE
:
1868 case OVS_FRAG_TYPE_FIRST
:
1870 case OVS_FRAG_TYPE_LATER
:
1872 case __OVS_FRAG_TYPE_MAX
:
1878 static enum odp_key_fitness
1879 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
1880 struct flow_tnl
*tun
)
1883 const struct nlattr
*a
;
1885 bool unknown
= false;
1887 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1888 uint16_t type
= nl_attr_type(a
);
1889 size_t len
= nl_attr_get_size(a
);
1890 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1891 OVS_TUNNEL_ATTR_MAX
, type
);
1893 if (len
!= expected_len
&& expected_len
>= 0) {
1894 return ODP_FIT_ERROR
;
1898 case OVS_TUNNEL_KEY_ATTR_ID
:
1899 tun
->tun_id
= nl_attr_get_be64(a
);
1900 tun
->flags
|= FLOW_TNL_F_KEY
;
1902 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1903 tun
->ip_src
= nl_attr_get_be32(a
);
1905 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1906 tun
->ip_dst
= nl_attr_get_be32(a
);
1908 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
1909 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
1911 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
1912 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
1914 case OVS_TUNNEL_KEY_ATTR_TOS
:
1915 tun
->ip_tos
= nl_attr_get_u8(a
);
1917 case OVS_TUNNEL_KEY_ATTR_TTL
:
1918 tun
->ip_ttl
= nl_attr_get_u8(a
);
1921 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1922 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1924 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1925 tun
->flags
|= FLOW_TNL_F_CSUM
;
1927 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1928 tun
->tp_src
= nl_attr_get_be16(a
);
1930 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1931 tun
->tp_dst
= nl_attr_get_be16(a
);
1933 case OVS_TUNNEL_KEY_ATTR_OAM
:
1934 tun
->flags
|= FLOW_TNL_F_OAM
;
1936 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1937 static const struct nl_policy vxlan_opts_policy
[] = {
1938 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1940 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1942 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1943 return ODP_FIT_ERROR
;
1946 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1947 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1949 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1950 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
1955 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1956 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
1960 /* Allow this to show up as unexpected, if there are unknown
1961 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1968 return ODP_FIT_ERROR
;
1971 return ODP_FIT_TOO_MUCH
;
1973 return ODP_FIT_PERFECT
;
1976 enum odp_key_fitness
1977 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
1979 memset(tun
, 0, sizeof *tun
);
1980 return odp_tun_key_from_attr__(attr
, false, tun
);
1984 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
1985 const struct flow_tnl
*tun_flow_key
,
1986 const struct ofpbuf
*key_buf
)
1990 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
1992 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1993 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
1994 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
1996 if (tun_key
->ip_src
) {
1997 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
1999 if (tun_key
->ip_dst
) {
2000 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2002 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2003 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2005 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2006 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2008 if (tun_key
->ip_tos
) {
2009 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2011 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2012 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2013 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2015 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2016 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2018 if (tun_key
->tp_src
) {
2019 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2021 if (tun_key
->tp_dst
) {
2022 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2024 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2025 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2027 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
2028 size_t vxlan_opts_ofs
;
2030 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2031 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2032 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2033 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2035 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2037 nl_msg_end_nested(a
, tun_key_ofs
);
2041 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2043 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
2047 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2049 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
2050 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
2052 if (attr
== OVS_KEY_ATTR_IPV6
) {
2053 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2056 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2057 == htonl(IPV6_LABEL_MASK
))
2058 && ipv6_mask
->ipv6_proto
== UINT8_MAX
2059 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
2060 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
2061 && ipv6_mask
->ipv6_frag
== UINT8_MAX
2062 && ipv6_mask_is_exact(&ipv6_mask
->ipv6_src
)
2063 && ipv6_mask_is_exact(&ipv6_mask
->ipv6_dst
);
2065 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2069 if (attr
== OVS_KEY_ATTR_ARP
) {
2070 /* ARP key has padding, ignore it. */
2071 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
2072 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
2073 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
2074 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
2077 return is_all_ones(mask
, size
);
2081 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2083 enum ovs_key_attr attr
= nl_attr_type(ma
);
2087 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2090 mask
= nl_attr_get(ma
);
2091 size
= nl_attr_get_size(ma
);
2094 return odp_mask_is_exact(attr
, mask
, size
);
2098 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2101 struct odp_portno_names
*odp_portno_names
;
2103 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2104 odp_portno_names
->port_no
= port_no
;
2105 odp_portno_names
->name
= xstrdup(port_name
);
2106 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2107 hash_odp_port(port_no
));
2111 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2113 struct odp_portno_names
*odp_portno_names
;
2115 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2116 hash_odp_port(port_no
), portno_names
) {
2117 if (odp_portno_names
->port_no
== port_no
) {
2118 return odp_portno_names
->name
;
2125 odp_portno_names_destroy(struct hmap
*portno_names
)
2127 struct odp_portno_names
*odp_portno_names
;
2129 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
2130 free(odp_portno_names
->name
);
2131 free(odp_portno_names
);
2135 /* Format helpers. */
2138 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2139 const struct eth_addr
*mask
, bool verbose
)
2141 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2143 if (verbose
|| !mask_empty
) {
2144 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2147 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2149 ds_put_format(ds
, "%s=", name
);
2150 eth_format_masked(key
, mask
, ds
);
2151 ds_put_char(ds
, ',');
2157 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2158 const ovs_be64
*mask
, bool verbose
)
2160 bool mask_empty
= mask
&& !*mask
;
2162 if (verbose
|| !mask_empty
) {
2163 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2165 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2166 if (!mask_full
) { /* Partially masked. */
2167 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2169 ds_put_char(ds
, ',');
2174 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2175 const ovs_be32
*mask
, bool verbose
)
2177 bool mask_empty
= mask
&& !*mask
;
2179 if (verbose
|| !mask_empty
) {
2180 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2182 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2183 if (!mask_full
) { /* Partially masked. */
2184 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2186 ds_put_char(ds
, ',');
2191 format_in6_addr(struct ds
*ds
, const char *name
,
2192 const struct in6_addr
*key
,
2193 const struct in6_addr
*mask
,
2196 char buf
[INET6_ADDRSTRLEN
];
2197 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2199 if (verbose
|| !mask_empty
) {
2200 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2202 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2203 ds_put_format(ds
, "%s=%s", name
, buf
);
2204 if (!mask_full
) { /* Partially masked. */
2205 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2206 ds_put_format(ds
, "/%s", buf
);
2208 ds_put_char(ds
, ',');
2213 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2214 const ovs_be32
*mask
, bool verbose
)
2216 bool mask_empty
= mask
&& !*mask
;
2218 if (verbose
|| !mask_empty
) {
2219 bool mask_full
= !mask
2220 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2222 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2223 if (!mask_full
) { /* Partially masked. */
2224 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2226 ds_put_char(ds
, ',');
2231 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2232 const uint8_t *mask
, bool verbose
)
2234 bool mask_empty
= mask
&& !*mask
;
2236 if (verbose
|| !mask_empty
) {
2237 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2239 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2240 if (!mask_full
) { /* Partially masked. */
2241 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2243 ds_put_char(ds
, ',');
2248 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2249 const uint8_t *mask
, bool verbose
)
2251 bool mask_empty
= mask
&& !*mask
;
2253 if (verbose
|| !mask_empty
) {
2254 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2256 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2257 if (!mask_full
) { /* Partially masked. */
2258 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2260 ds_put_char(ds
, ',');
2265 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2266 const ovs_be16
*mask
, bool verbose
)
2268 bool mask_empty
= mask
&& !*mask
;
2270 if (verbose
|| !mask_empty
) {
2271 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2273 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2274 if (!mask_full
) { /* Partially masked. */
2275 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2277 ds_put_char(ds
, ',');
2282 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2283 const ovs_be16
*mask
, bool verbose
)
2285 bool mask_empty
= mask
&& !*mask
;
2287 if (verbose
|| !mask_empty
) {
2288 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2290 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2291 if (!mask_full
) { /* Partially masked. */
2292 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2294 ds_put_char(ds
, ',');
2299 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2300 const uint16_t *mask
, bool verbose
)
2302 bool mask_empty
= mask
&& !*mask
;
2304 if (verbose
|| !mask_empty
) {
2305 ds_put_cstr(ds
, name
);
2306 ds_put_char(ds
, '(');
2308 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2309 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2310 } else { /* Fully masked. */
2311 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2313 ds_put_cstr(ds
, "),");
2318 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2319 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2323 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2324 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2325 expected_len
!= ATTR_LEN_NESTED
) {
2327 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2328 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2330 if (bad_key_len
|| bad_mask_len
) {
2332 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2335 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2336 nl_attr_get_size(a
), expected_len
);
2338 format_generic_odp_key(a
, ds
);
2340 ds_put_char(ds
, '/');
2342 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2343 nl_attr_get_size(ma
), expected_len
);
2345 format_generic_odp_key(ma
, ds
);
2347 ds_put_char(ds
, ')');
2356 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2357 const struct nlattr
*ma
)
2359 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2360 format_generic_odp_key(a
, ds
);
2361 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2362 ds_put_char(ds
, '/');
2363 format_generic_odp_key(ma
, ds
);
2365 ds_put_cstr(ds
, "),");
2369 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2370 const struct nlattr
*mask_attr
, struct ds
*ds
,
2374 const struct nlattr
*a
;
2377 ofpbuf_init(&ofp
, 100);
2378 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2379 uint16_t type
= nl_attr_type(a
);
2380 const struct nlattr
*ma
= NULL
;
2383 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2384 nl_attr_get_size(mask_attr
), type
);
2386 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2392 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2393 OVS_VXLAN_EXT_MAX
, true)) {
2398 case OVS_VXLAN_EXT_GBP
: {
2399 uint32_t key
= nl_attr_get_u32(a
);
2400 ovs_be16 id
, id_mask
;
2401 uint8_t flags
, flags_mask
= 0;
2403 id
= htons(key
& 0xFFFF);
2404 flags
= (key
>> 16) & 0xFF;
2406 uint32_t mask
= nl_attr_get_u32(ma
);
2407 id_mask
= htons(mask
& 0xFFFF);
2408 flags_mask
= (mask
>> 16) & 0xFF;
2411 ds_put_cstr(ds
, "gbp(");
2412 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2413 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2415 ds_put_cstr(ds
, "),");
2420 format_unknown_key(ds
, a
, ma
);
2426 ofpbuf_uninit(&ofp
);
2429 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2432 format_geneve_opts(const struct geneve_opt
*opt
,
2433 const struct geneve_opt
*mask
, int opts_len
,
2434 struct ds
*ds
, bool verbose
)
2436 while (opts_len
> 0) {
2438 uint8_t data_len
, data_len_mask
;
2440 if (opts_len
< sizeof *opt
) {
2441 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2442 opts_len
, sizeof *opt
);
2446 data_len
= opt
->length
* 4;
2448 if (mask
->length
== 0x1f) {
2449 data_len_mask
= UINT8_MAX
;
2451 data_len_mask
= mask
->length
;
2454 len
= sizeof *opt
+ data_len
;
2455 if (len
> opts_len
) {
2456 ds_put_format(ds
, "opt len %u greater than remaining %u",
2461 ds_put_char(ds
, '{');
2462 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2464 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2465 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2467 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2468 ds_put_hex(ds
, opt
+ 1, data_len
);
2469 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2470 ds_put_char(ds
, '/');
2471 ds_put_hex(ds
, mask
+ 1, data_len
);
2476 ds_put_char(ds
, '}');
2478 opt
+= len
/ sizeof(*opt
);
2480 mask
+= len
/ sizeof(*opt
);
2487 format_odp_tun_geneve(const struct nlattr
*attr
,
2488 const struct nlattr
*mask_attr
, struct ds
*ds
,
2491 int opts_len
= nl_attr_get_size(attr
);
2492 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2493 const struct geneve_opt
*mask
= mask_attr
?
2494 nl_attr_get(mask_attr
) : NULL
;
2496 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2497 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2498 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2502 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2506 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2507 struct ds
*ds
, bool verbose
)
2510 const struct nlattr
*a
;
2512 uint16_t mask_flags
= 0;
2515 ofpbuf_init(&ofp
, 100);
2516 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2517 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2518 const struct nlattr
*ma
= NULL
;
2521 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2522 nl_attr_get_size(mask_attr
), type
);
2524 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2525 OVS_TUNNEL_KEY_ATTR_MAX
,
2530 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2531 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2536 case OVS_TUNNEL_KEY_ATTR_ID
:
2537 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2538 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2539 flags
|= FLOW_TNL_F_KEY
;
2541 mask_flags
|= FLOW_TNL_F_KEY
;
2544 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2545 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2546 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2548 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2549 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2550 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2552 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2553 struct in6_addr ipv6_src
;
2554 ipv6_src
= nl_attr_get_in6_addr(a
);
2555 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2556 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2559 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2560 struct in6_addr ipv6_dst
;
2561 ipv6_dst
= nl_attr_get_in6_addr(a
);
2562 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2563 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2566 case OVS_TUNNEL_KEY_ATTR_TOS
:
2567 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2568 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2570 case OVS_TUNNEL_KEY_ATTR_TTL
:
2571 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2572 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2574 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2575 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2577 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2578 flags
|= FLOW_TNL_F_CSUM
;
2580 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2581 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2582 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2584 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2585 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2586 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2588 case OVS_TUNNEL_KEY_ATTR_OAM
:
2589 flags
|= FLOW_TNL_F_OAM
;
2591 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2592 ds_put_cstr(ds
, "vxlan(");
2593 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2594 ds_put_cstr(ds
, "),");
2596 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2597 ds_put_cstr(ds
, "geneve(");
2598 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2599 ds_put_cstr(ds
, "),");
2601 case OVS_TUNNEL_KEY_ATTR_PAD
:
2603 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2605 format_unknown_key(ds
, a
, ma
);
2610 /* Flags can have a valid mask even if the attribute is not set, so
2611 * we need to collect these separately. */
2613 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2614 switch (nl_attr_type(a
)) {
2615 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2616 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2618 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2619 mask_flags
|= FLOW_TNL_F_CSUM
;
2621 case OVS_TUNNEL_KEY_ATTR_OAM
:
2622 mask_flags
|= FLOW_TNL_F_OAM
;
2628 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2631 ofpbuf_uninit(&ofp
);
2635 odp_ct_state_to_string(uint32_t flag
)
2638 case OVS_CS_F_REPLY_DIR
:
2640 case OVS_CS_F_TRACKED
:
2644 case OVS_CS_F_ESTABLISHED
:
2646 case OVS_CS_F_RELATED
:
2648 case OVS_CS_F_INVALID
:
2650 case OVS_CS_F_SRC_NAT
:
2652 case OVS_CS_F_DST_NAT
:
2660 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2661 const uint8_t *mask
, bool verbose
)
2663 bool mask_empty
= mask
&& !*mask
;
2665 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2666 if (verbose
|| !mask_empty
) {
2667 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2669 if (!mask_full
) { /* Partially masked. */
2670 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2673 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2679 mask_empty(const struct nlattr
*ma
)
2687 mask
= nl_attr_get(ma
);
2688 n
= nl_attr_get_size(ma
);
2690 return is_all_zeros(mask
, n
);
2694 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2695 const struct hmap
*portno_names
, struct ds
*ds
,
2698 enum ovs_key_attr attr
= nl_attr_type(a
);
2699 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2702 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2704 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2706 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2707 OVS_KEY_ATTR_MAX
, false)) {
2711 ds_put_char(ds
, '(');
2713 case OVS_KEY_ATTR_ENCAP
:
2714 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2715 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2716 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2718 } else if (nl_attr_get_size(a
)) {
2719 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2724 case OVS_KEY_ATTR_PRIORITY
:
2725 case OVS_KEY_ATTR_SKB_MARK
:
2726 case OVS_KEY_ATTR_DP_HASH
:
2727 case OVS_KEY_ATTR_RECIRC_ID
:
2728 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2730 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2734 case OVS_KEY_ATTR_CT_MARK
:
2735 if (verbose
|| !mask_empty(ma
)) {
2736 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2738 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2743 case OVS_KEY_ATTR_CT_STATE
:
2745 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2747 ds_put_format(ds
, "/%#"PRIx32
,
2748 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2750 } else if (!is_exact
) {
2751 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2753 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2756 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2760 case OVS_KEY_ATTR_CT_ZONE
:
2761 if (verbose
|| !mask_empty(ma
)) {
2762 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2764 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2769 case OVS_KEY_ATTR_CT_LABELS
: {
2770 const ovs_u128
*value
= nl_attr_get(a
);
2771 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2773 format_u128(ds
, value
, mask
, verbose
);
2777 case OVS_KEY_ATTR_TUNNEL
:
2778 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2781 case OVS_KEY_ATTR_IN_PORT
:
2782 if (portno_names
&& verbose
&& is_exact
) {
2783 char *name
= odp_portno_names_get(portno_names
,
2784 nl_attr_get_odp_port(a
));
2786 ds_put_format(ds
, "%s", name
);
2788 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2791 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2793 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2798 case OVS_KEY_ATTR_ETHERNET
: {
2799 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2800 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2802 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2803 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2807 case OVS_KEY_ATTR_VLAN
:
2808 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2809 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2812 case OVS_KEY_ATTR_MPLS
: {
2813 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2814 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2815 size_t size
= nl_attr_get_size(a
);
2817 if (!size
|| size
% sizeof *mpls_key
) {
2818 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2822 mpls_mask
= nl_attr_get(ma
);
2823 if (size
!= nl_attr_get_size(ma
)) {
2824 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2825 "mask length %"PRIuSIZE
")",
2826 size
, nl_attr_get_size(ma
));
2830 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2833 case OVS_KEY_ATTR_ETHERTYPE
:
2834 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2836 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2840 case OVS_KEY_ATTR_IPV4
: {
2841 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2842 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2844 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2845 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2846 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2848 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2849 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2850 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2855 case OVS_KEY_ATTR_IPV6
: {
2856 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2857 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2859 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
2861 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
2863 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2865 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2867 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2869 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2871 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2876 /* These have the same structure and format. */
2877 case OVS_KEY_ATTR_TCP
:
2878 case OVS_KEY_ATTR_UDP
:
2879 case OVS_KEY_ATTR_SCTP
: {
2880 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2881 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2883 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2884 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2888 case OVS_KEY_ATTR_TCP_FLAGS
:
2890 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2891 ntohs(nl_attr_get_be16(a
)),
2892 TCP_FLAGS(nl_attr_get_be16(ma
)),
2893 TCP_FLAGS(OVS_BE16_MAX
));
2895 format_flags(ds
, packet_tcp_flag_to_string
,
2896 ntohs(nl_attr_get_be16(a
)), '|');
2900 case OVS_KEY_ATTR_ICMP
: {
2901 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2902 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2904 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2905 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2909 case OVS_KEY_ATTR_ICMPV6
: {
2910 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2911 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2913 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2915 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2920 case OVS_KEY_ATTR_ARP
: {
2921 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2922 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2924 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2925 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2926 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2927 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2928 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2932 case OVS_KEY_ATTR_ND
: {
2933 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2934 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2936 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
2938 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2939 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2944 case OVS_KEY_ATTR_UNSPEC
:
2945 case __OVS_KEY_ATTR_MAX
:
2947 format_generic_odp_key(a
, ds
);
2949 ds_put_char(ds
, '/');
2950 format_generic_odp_key(ma
, ds
);
2954 ds_put_char(ds
, ')');
2957 static struct nlattr
*
2958 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2959 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2961 const struct nlattr
*a
;
2963 int type
= nl_attr_type(key
);
2964 int size
= nl_attr_get_size(key
);
2966 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2967 nl_msg_put_unspec_zero(ofp
, type
, size
);
2971 if (tbl
[type
].next
) {
2972 tbl
= tbl
[type
].next
;
2973 max
= tbl
[type
].next_max
;
2976 nested_mask
= nl_msg_start_nested(ofp
, type
);
2977 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2978 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2980 nl_msg_end_nested(ofp
, nested_mask
);
2987 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
2990 if (verbose
|| (mask
&& !ovs_u128_is_zero(*mask
))) {
2993 value
= hton128(*key
);
2994 ds_put_hex(ds
, &value
, sizeof value
);
2995 if (mask
&& !(ovs_u128_is_ones(*mask
))) {
2996 value
= hton128(*mask
);
2997 ds_put_char(ds
, '/');
2998 ds_put_hex(ds
, &value
, sizeof value
);
3003 /* Read the string from 's_' as a 128-bit value. If the string contains
3004 * a "/", the rest of the string will be treated as a 128-bit mask.
3006 * If either the value or mask is larger than 64 bits, the string must
3007 * be in hexadecimal.
3010 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
3012 char *s
= CONST_CAST(char *, s_
);
3016 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3017 *value
= ntoh128(be_value
);
3022 if (ovs_scan(s
, "/%n", &n
)) {
3026 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3027 sizeof be_mask
, &s
);
3031 *mask
= ntoh128(be_mask
);
3033 *mask
= OVS_U128_MAX
;
3043 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3047 if (ovs_scan(s
, "ufid:")) {
3050 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3062 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3064 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3067 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3068 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3069 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3070 * non-null and 'verbose' is true, translates odp port number to its name. */
3072 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3073 const struct nlattr
*mask
, size_t mask_len
,
3074 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3077 const struct nlattr
*a
;
3079 bool has_ethtype_key
= false;
3080 const struct nlattr
*ma
= NULL
;
3082 bool first_field
= true;
3084 ofpbuf_init(&ofp
, 100);
3085 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3086 bool is_nested_attr
;
3087 bool is_wildcard
= false;
3088 int attr_type
= nl_attr_type(a
);
3090 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3091 has_ethtype_key
= true;
3094 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3095 OVS_KEY_ATTR_MAX
, attr_type
) ==
3098 if (mask
&& mask_len
) {
3099 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3100 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3103 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3104 if (is_wildcard
&& !ma
) {
3105 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3110 ds_put_char(ds
, ',');
3112 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3113 first_field
= false;
3117 ofpbuf_uninit(&ofp
);
3122 if (left
== key_len
) {
3123 ds_put_cstr(ds
, "<empty>");
3125 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3126 for (i
= 0; i
< left
; i
++) {
3127 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3129 ds_put_char(ds
, ')');
3131 if (!has_ethtype_key
) {
3132 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3134 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3135 ntohs(nl_attr_get_be16(ma
)));
3139 ds_put_cstr(ds
, "<empty>");
3143 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3144 * OVS_KEY_ATTR_* attributes in 'key'. */
3146 odp_flow_key_format(const struct nlattr
*key
,
3147 size_t key_len
, struct ds
*ds
)
3149 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3153 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3155 if (!strcasecmp(s
, "no")) {
3156 *type
= OVS_FRAG_TYPE_NONE
;
3157 } else if (!strcasecmp(s
, "first")) {
3158 *type
= OVS_FRAG_TYPE_FIRST
;
3159 } else if (!strcasecmp(s
, "later")) {
3160 *type
= OVS_FRAG_TYPE_LATER
;
3170 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3174 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3175 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3179 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3180 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3183 memset(mask
, 0xff, sizeof *mask
);
3192 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3196 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3200 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3201 IP_SCAN_ARGS(mask
), &n
)) {
3204 *mask
= OVS_BE32_MAX
;
3213 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3216 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3218 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3219 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3223 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3224 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3227 memset(mask
, 0xff, sizeof *mask
);
3236 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3241 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3242 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3247 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3248 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3250 *mask
= htonl(mask_
);
3252 *mask
= htonl(IPV6_LABEL_MASK
);
3261 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3265 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3269 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3281 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3285 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3289 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3301 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3305 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3309 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3321 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3323 uint16_t key_
, mask_
;
3326 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3331 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3333 *mask
= htons(mask_
);
3335 *mask
= OVS_BE16_MAX
;
3344 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3346 uint64_t key_
, mask_
;
3349 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3352 *key
= htonll(key_
);
3354 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3356 *mask
= htonll(mask_
);
3358 *mask
= OVS_BE64_MAX
;
3367 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3369 uint32_t flags
, fmask
;
3372 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3373 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3374 if (n
>= 0 && s
[n
] == ')') {
3385 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3387 uint32_t flags
, fmask
;
3390 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3391 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3393 *key
= htons(flags
);
3395 *mask
= htons(fmask
);
3403 ovs_to_odp_ct_state(uint8_t state
)
3407 if (state
& CS_NEW
) {
3408 odp
|= OVS_CS_F_NEW
;
3410 if (state
& CS_ESTABLISHED
) {
3411 odp
|= OVS_CS_F_ESTABLISHED
;
3413 if (state
& CS_RELATED
) {
3414 odp
|= OVS_CS_F_RELATED
;
3416 if (state
& CS_INVALID
) {
3417 odp
|= OVS_CS_F_INVALID
;
3419 if (state
& CS_REPLY_DIR
) {
3420 odp
|= OVS_CS_F_REPLY_DIR
;
3422 if (state
& CS_TRACKED
) {
3423 odp
|= OVS_CS_F_TRACKED
;
3425 if (state
& CS_SRC_NAT
) {
3426 odp
|= OVS_CS_F_SRC_NAT
;
3428 if (state
& CS_DST_NAT
) {
3429 odp
|= OVS_CS_F_DST_NAT
;
3436 odp_to_ovs_ct_state(uint32_t flags
)
3440 if (flags
& OVS_CS_F_NEW
) {
3443 if (flags
& OVS_CS_F_ESTABLISHED
) {
3444 state
|= CS_ESTABLISHED
;
3446 if (flags
& OVS_CS_F_RELATED
) {
3447 state
|= CS_RELATED
;
3449 if (flags
& OVS_CS_F_INVALID
) {
3450 state
|= CS_INVALID
;
3452 if (flags
& OVS_CS_F_REPLY_DIR
) {
3453 state
|= CS_REPLY_DIR
;
3455 if (flags
& OVS_CS_F_TRACKED
) {
3456 state
|= CS_TRACKED
;
3458 if (flags
& OVS_CS_F_SRC_NAT
) {
3459 state
|= CS_SRC_NAT
;
3461 if (flags
& OVS_CS_F_DST_NAT
) {
3462 state
|= CS_DST_NAT
;
3469 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3471 uint32_t flags
, fmask
;
3474 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3475 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3476 mask
? &fmask
: NULL
);
3489 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3493 enum ovs_frag_type frag_type
;
3495 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3496 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3509 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3510 const struct simap
*port_names
)
3514 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3518 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3525 } else if (port_names
) {
3526 const struct simap_node
*node
;
3529 len
= strcspn(s
, ")");
3530 node
= simap_find_len(port_names
, s
, len
);
3543 /* Helper for vlan parsing. */
3544 struct ovs_key_vlan__
{
3549 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3551 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3553 if (value
>> bits
) {
3557 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3562 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3565 uint16_t key_
, mask_
;
3568 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3571 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3573 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3576 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3580 *mask
|= htons(((1U << bits
) - 1) << offset
);
3590 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3592 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3596 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3598 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3602 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3604 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3609 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3611 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3613 if (value
>> bits
) {
3617 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3622 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3625 uint32_t key_
, mask_
;
3628 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3631 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3633 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3636 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3640 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3650 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3652 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3656 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3658 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3662 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3664 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3668 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3670 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3674 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3676 const char *s_base
= s
;
3677 ovs_be16 id
= 0, id_mask
= 0;
3678 uint8_t flags
= 0, flags_mask
= 0;
3680 if (!strncmp(s
, "id=", 3)) {
3682 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3688 if (!strncmp(s
, "flags=", 6)) {
3690 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3693 if (!strncmp(s
, "))", 2)) {
3696 *key
= (flags
<< 16) | ntohs(id
);
3698 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3708 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3710 const char *s_base
= s
;
3711 struct geneve_opt
*opt
= key
->d
;
3712 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3713 int len_remain
= sizeof key
->d
;
3715 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3719 len_remain
-= sizeof *opt
;
3721 if (!strncmp(s
, "class=", 6)) {
3723 s
+= scan_be16(s
, &opt
->opt_class
,
3724 mask
? &opt_mask
->opt_class
: NULL
);
3726 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3732 if (!strncmp(s
, "type=", 5)) {
3734 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3736 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3742 if (!strncmp(s
, "len=", 4)) {
3743 uint8_t opt_len
, opt_len_mask
;
3745 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3747 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3750 opt
->length
= opt_len
/ 4;
3752 opt_mask
->length
= opt_len_mask
;
3756 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3762 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3769 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3770 data_len
, (char **)&s
)) {
3781 opt
+= 1 + data_len
/ 4;
3783 opt_mask
+= 1 + data_len
/ 4;
3785 len_remain
-= data_len
;
3790 int len
= sizeof key
->d
- len_remain
;
3804 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3806 const uint16_t *flags
= data_
;
3808 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3809 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3811 if (*flags
& FLOW_TNL_F_CSUM
) {
3812 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3814 if (*flags
& FLOW_TNL_F_OAM
) {
3815 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3820 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3822 const uint32_t *gbp
= data_
;
3825 size_t vxlan_opts_ofs
;
3827 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3828 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3829 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3834 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3836 const struct geneve_scan
*geneve
= data_
;
3838 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3842 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3844 unsigned long call_fn = (unsigned long)FUNC; \
3846 typedef void (*fn)(struct ofpbuf *, const void *); \
3848 func(BUF, &(DATA)); \
3850 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3854 #define SCAN_IF(NAME) \
3855 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3856 const char *start = s; \
3861 /* Usually no special initialization is needed. */
3862 #define SCAN_BEGIN(NAME, TYPE) \
3865 memset(&skey, 0, sizeof skey); \
3866 memset(&smask, 0, sizeof smask); \
3870 /* Init as fully-masked as mask will not be scanned. */
3871 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3874 memset(&skey, 0, sizeof skey); \
3875 memset(&smask, 0xff, sizeof smask); \
3879 /* VLAN needs special initialization. */
3880 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3882 TYPE skey = KEY_INIT; \
3883 TYPE smask = MASK_INIT; \
3887 /* Scan unnamed entry as 'TYPE' */
3888 #define SCAN_TYPE(TYPE, KEY, MASK) \
3889 len = scan_##TYPE(s, KEY, MASK); \
3895 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3896 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3897 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3898 s += strlen(NAME); \
3899 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3903 #define SCAN_FINISH() \
3904 } while (*s++ == ',' && len != 0); \
3905 if (s[-1] != ')') { \
3909 #define SCAN_FINISH_SINGLE() \
3911 if (*s++ != ')') { \
3915 /* Beginning of nested attribute. */
3916 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3918 size_t key_offset, mask_offset; \
3919 key_offset = nl_msg_start_nested(key, ATTR); \
3921 mask_offset = nl_msg_start_nested(mask, ATTR); \
3926 #define SCAN_END_NESTED() \
3928 nl_msg_end_nested(key, key_offset); \
3930 nl_msg_end_nested(mask, mask_offset); \
3935 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3936 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3938 memset(&skey, 0, sizeof skey); \
3939 memset(&smask, 0xff, sizeof smask); \
3940 s += strlen(NAME); \
3941 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3942 SCAN_PUT(ATTR, FUNC); \
3946 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3947 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3949 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3950 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3952 #define SCAN_PUT(ATTR, FUNC) \
3953 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3955 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3957 #define SCAN_END(ATTR) \
3959 SCAN_PUT(ATTR, NULL); \
3963 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
3965 TYPE skey[CNT], smask[CNT]; \
3966 memset(&skey, 0, sizeof skey); \
3967 memset(&smask, 0, sizeof smask); \
3968 int idx = 0, cnt = CNT; \
3969 uint64_t fields = 0; \
3974 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3975 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
3976 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3977 if (fields & (1UL << field)) { \
3979 if (++idx == cnt) { \
3983 s += strlen(NAME); \
3984 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
3985 fields |= 1UL << field; \
3990 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
3991 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
3993 #define SCAN_PUT_ARRAY(ATTR, CNT) \
3994 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
3996 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
3999 #define SCAN_END_ARRAY(ATTR) \
4004 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4008 #define SCAN_END_SINGLE(ATTR) \
4009 SCAN_FINISH_SINGLE(); \
4010 SCAN_PUT(ATTR, NULL); \
4014 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4015 SCAN_BEGIN(NAME, TYPE) { \
4016 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4017 } SCAN_END_SINGLE(ATTR)
4019 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4020 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4021 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4022 } SCAN_END_SINGLE(ATTR)
4024 /* scan_port needs one extra argument. */
4025 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4026 SCAN_BEGIN(NAME, TYPE) { \
4027 len = scan_port(s, &skey, &smask, port_names); \
4032 } SCAN_END_SINGLE(ATTR)
4035 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4036 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4042 len
= odp_ufid_from_string(s
, &ufid
);
4047 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4048 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4049 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4050 OVS_KEY_ATTR_RECIRC_ID
);
4051 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4053 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4054 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4055 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4056 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4058 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4059 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4060 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4061 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4062 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4063 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4064 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4065 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4066 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4067 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4068 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4069 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4071 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4072 } SCAN_END_NESTED();
4074 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4076 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4077 SCAN_FIELD("src=", eth
, eth_src
);
4078 SCAN_FIELD("dst=", eth
, eth_dst
);
4079 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4081 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4082 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4083 SCAN_FIELD("vid=", vid
, tci
);
4084 SCAN_FIELD("pcp=", pcp
, tci
);
4085 SCAN_FIELD("cfi=", cfi
, tci
);
4086 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4088 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4090 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4091 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4092 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4093 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4094 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4095 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4097 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4098 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4099 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4100 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4101 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4102 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4103 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4104 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4106 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4107 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4108 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4109 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4110 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4111 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4112 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4113 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4114 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4116 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4117 SCAN_FIELD("src=", be16
, tcp_src
);
4118 SCAN_FIELD("dst=", be16
, tcp_dst
);
4119 } SCAN_END(OVS_KEY_ATTR_TCP
);
4121 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4123 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4124 SCAN_FIELD("src=", be16
, udp_src
);
4125 SCAN_FIELD("dst=", be16
, udp_dst
);
4126 } SCAN_END(OVS_KEY_ATTR_UDP
);
4128 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4129 SCAN_FIELD("src=", be16
, sctp_src
);
4130 SCAN_FIELD("dst=", be16
, sctp_dst
);
4131 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4133 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4134 SCAN_FIELD("type=", u8
, icmp_type
);
4135 SCAN_FIELD("code=", u8
, icmp_code
);
4136 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4138 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4139 SCAN_FIELD("type=", u8
, icmpv6_type
);
4140 SCAN_FIELD("code=", u8
, icmpv6_code
);
4141 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4143 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4144 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4145 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4146 SCAN_FIELD("op=", be16
, arp_op
);
4147 SCAN_FIELD("sha=", eth
, arp_sha
);
4148 SCAN_FIELD("tha=", eth
, arp_tha
);
4149 } SCAN_END(OVS_KEY_ATTR_ARP
);
4151 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4152 SCAN_FIELD("target=", in6_addr
, nd_target
);
4153 SCAN_FIELD("sll=", eth
, nd_sll
);
4154 SCAN_FIELD("tll=", eth
, nd_tll
);
4155 } SCAN_END(OVS_KEY_ATTR_ND
);
4157 /* Encap open-coded. */
4158 if (!strncmp(s
, "encap(", 6)) {
4159 const char *start
= s
;
4160 size_t encap
, encap_mask
= 0;
4162 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4164 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4171 s
+= strspn(s
, delimiters
);
4174 } else if (*s
== ')') {
4178 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4186 nl_msg_end_nested(key
, encap
);
4188 nl_msg_end_nested(mask
, encap_mask
);
4197 /* Parses the string representation of a datapath flow key, in the
4198 * format output by odp_flow_key_format(). Returns 0 if successful,
4199 * otherwise a positive errno value. On success, the flow key is
4200 * appended to 'key' as a series of Netlink attributes. On failure, no
4201 * data is appended to 'key'. Either way, 'key''s data might be
4204 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4205 * to a port number. (Port names may be used instead of port numbers in
4208 * On success, the attributes appended to 'key' are individually syntactically
4209 * valid, but they may not be valid as a sequence. 'key' might, for example,
4210 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4212 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4213 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4215 const size_t old_size
= key
->size
;
4219 s
+= strspn(s
, delimiters
);
4224 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4226 key
->size
= old_size
;
4236 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4239 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4240 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4241 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4242 * must use a zero mask for the netlink frag field, and all ones mask
4244 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4246 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4247 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4248 : OVS_FRAG_TYPE_FIRST
;
4251 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4252 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4253 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4255 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4257 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4259 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4261 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4262 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4263 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4264 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4266 /* These share the same layout. */
4268 struct ovs_key_tcp tcp
;
4269 struct ovs_key_udp udp
;
4270 struct ovs_key_sctp sctp
;
4273 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4274 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4277 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4278 bool export_mask
, struct ofpbuf
*buf
)
4280 struct ovs_key_ethernet
*eth_key
;
4282 const struct flow
*flow
= parms
->flow
;
4283 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4285 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4287 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4288 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4292 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4294 if (parms
->support
.ct_state
) {
4295 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4296 ovs_to_odp_ct_state(data
->ct_state
));
4298 if (parms
->support
.ct_zone
) {
4299 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4301 if (parms
->support
.ct_mark
) {
4302 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4304 if (parms
->support
.ct_label
) {
4305 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4306 sizeof(data
->ct_label
));
4308 if (parms
->support
.recirc
) {
4309 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4310 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4313 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4314 * is not the magical value "ODPP_NONE". */
4315 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4316 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4319 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4321 get_ethernet_key(data
, eth_key
);
4323 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4325 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4327 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4329 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4330 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4331 if (flow
->vlan_tci
== htons(0)) {
4338 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4339 /* For backwards compatibility with kernels that don't support
4340 * wildcarding, the following convention is used to encode the
4341 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4344 * -------- -------- -------
4345 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4346 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4347 * <none> 0xffff Any non-Ethernet II frame (except valid
4348 * 802.3 SNAP packet with valid eth_type).
4351 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4356 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4358 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4359 struct ovs_key_ipv4
*ipv4_key
;
4361 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4363 get_ipv4_key(data
, ipv4_key
, export_mask
);
4364 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4365 struct ovs_key_ipv6
*ipv6_key
;
4367 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4369 get_ipv6_key(data
, ipv6_key
, export_mask
);
4370 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4371 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4372 struct ovs_key_arp
*arp_key
;
4374 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4376 get_arp_key(data
, arp_key
);
4377 } else if (eth_type_mpls(flow
->dl_type
)) {
4378 struct ovs_key_mpls
*mpls_key
;
4381 n
= flow_count_mpls_labels(flow
, NULL
);
4383 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4385 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4386 n
* sizeof *mpls_key
);
4387 for (i
= 0; i
< n
; i
++) {
4388 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4392 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4393 if (flow
->nw_proto
== IPPROTO_TCP
) {
4394 union ovs_key_tp
*tcp_key
;
4396 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4398 get_tp_key(data
, tcp_key
);
4399 if (data
->tcp_flags
) {
4400 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4402 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4403 union ovs_key_tp
*udp_key
;
4405 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4407 get_tp_key(data
, udp_key
);
4408 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4409 union ovs_key_tp
*sctp_key
;
4411 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4413 get_tp_key(data
, sctp_key
);
4414 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4415 && flow
->nw_proto
== IPPROTO_ICMP
) {
4416 struct ovs_key_icmp
*icmp_key
;
4418 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4420 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4421 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4422 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4423 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4424 struct ovs_key_icmpv6
*icmpv6_key
;
4426 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4427 sizeof *icmpv6_key
);
4428 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4429 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4431 if (is_nd(flow
, NULL
)
4432 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4433 * type and code are 8 bits wide. Therefore, an exact match
4434 * looks like htons(0xff), not htons(0xffff). See
4435 * xlate_wc_finish() for details. */
4436 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4437 && data
->tp_dst
== htons(0xff)))) {
4439 struct ovs_key_nd
*nd_key
;
4441 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4443 nd_key
->nd_target
= data
->nd_target
;
4444 nd_key
->nd_sll
= data
->arp_sha
;
4445 nd_key
->nd_tll
= data
->arp_tha
;
4452 nl_msg_end_nested(buf
, encap
);
4456 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4458 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4459 * capable of being expanded to allow for that much space. */
4461 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4464 odp_flow_key_from_flow__(parms
, false, buf
);
4467 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4470 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4471 * capable of being expanded to allow for that much space. */
4473 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4476 odp_flow_key_from_flow__(parms
, true, buf
);
4479 /* Generate ODP flow key from the given packet metadata */
4481 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4483 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4485 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4486 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4489 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4492 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4493 ovs_to_odp_ct_state(md
->ct_state
));
4495 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4498 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4500 if (!ovs_u128_is_zero(md
->ct_label
)) {
4501 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4502 sizeof(md
->ct_label
));
4506 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4507 * value "ODPP_NONE". */
4508 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4509 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4513 /* Generate packet metadata from the given ODP flow key. */
4515 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4516 struct pkt_metadata
*md
)
4518 const struct nlattr
*nla
;
4520 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4521 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4522 1u << OVS_KEY_ATTR_IN_PORT
;
4524 pkt_metadata_init(md
, ODPP_NONE
);
4526 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4527 uint16_t type
= nl_attr_type(nla
);
4528 size_t len
= nl_attr_get_size(nla
);
4529 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4530 OVS_KEY_ATTR_MAX
, type
);
4532 if (len
!= expected_len
&& expected_len
>= 0) {
4537 case OVS_KEY_ATTR_RECIRC_ID
:
4538 md
->recirc_id
= nl_attr_get_u32(nla
);
4539 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4541 case OVS_KEY_ATTR_DP_HASH
:
4542 md
->dp_hash
= nl_attr_get_u32(nla
);
4543 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4545 case OVS_KEY_ATTR_PRIORITY
:
4546 md
->skb_priority
= nl_attr_get_u32(nla
);
4547 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4549 case OVS_KEY_ATTR_SKB_MARK
:
4550 md
->pkt_mark
= nl_attr_get_u32(nla
);
4551 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4553 case OVS_KEY_ATTR_CT_STATE
:
4554 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4555 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4557 case OVS_KEY_ATTR_CT_ZONE
:
4558 md
->ct_zone
= nl_attr_get_u16(nla
);
4559 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4561 case OVS_KEY_ATTR_CT_MARK
:
4562 md
->ct_mark
= nl_attr_get_u32(nla
);
4563 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4565 case OVS_KEY_ATTR_CT_LABELS
: {
4566 const ovs_u128
*cl
= nl_attr_get(nla
);
4569 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4572 case OVS_KEY_ATTR_TUNNEL
: {
4573 enum odp_key_fitness res
;
4575 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
4576 if (res
== ODP_FIT_ERROR
) {
4577 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4578 } else if (res
== ODP_FIT_PERFECT
) {
4579 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4583 case OVS_KEY_ATTR_IN_PORT
:
4584 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4585 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4591 if (!wanted_attrs
) {
4592 return; /* Have everything. */
4598 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4600 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4601 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
4605 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4606 uint64_t attrs
, int out_of_range_attr
,
4607 const struct nlattr
*key
, size_t key_len
)
4612 if (VLOG_DROP_DBG(rl
)) {
4617 for (i
= 0; i
< 64; i
++) {
4618 if (attrs
& (UINT64_C(1) << i
)) {
4619 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4621 ds_put_format(&s
, " %s",
4622 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4625 if (out_of_range_attr
) {
4626 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4629 ds_put_cstr(&s
, ": ");
4630 odp_flow_key_format(key
, key_len
, &s
);
4632 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4637 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4639 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4642 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4645 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4646 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4647 return 0xff; /* Error. */
4650 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4651 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4652 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4656 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4657 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4658 int *out_of_range_attrp
)
4660 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4661 const struct nlattr
*nla
;
4662 uint64_t present_attrs
;
4665 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4667 *out_of_range_attrp
= 0;
4668 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4669 uint16_t type
= nl_attr_type(nla
);
4670 size_t len
= nl_attr_get_size(nla
);
4671 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4672 OVS_KEY_ATTR_MAX
, type
);
4674 if (len
!= expected_len
&& expected_len
>= 0) {
4675 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4677 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4678 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4684 if (type
> OVS_KEY_ATTR_MAX
) {
4685 *out_of_range_attrp
= type
;
4687 if (present_attrs
& (UINT64_C(1) << type
)) {
4688 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4690 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4691 ovs_key_attr_to_string(type
,
4692 namebuf
, sizeof namebuf
));
4696 present_attrs
|= UINT64_C(1) << type
;
4701 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4705 *present_attrsp
= present_attrs
;
4709 static enum odp_key_fitness
4710 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4711 uint64_t expected_attrs
,
4712 const struct nlattr
*key
, size_t key_len
)
4714 uint64_t missing_attrs
;
4715 uint64_t extra_attrs
;
4717 missing_attrs
= expected_attrs
& ~present_attrs
;
4718 if (missing_attrs
) {
4719 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4720 log_odp_key_attributes(&rl
, "expected but not present",
4721 missing_attrs
, 0, key
, key_len
);
4722 return ODP_FIT_TOO_LITTLE
;
4725 extra_attrs
= present_attrs
& ~expected_attrs
;
4726 if (extra_attrs
|| out_of_range_attr
) {
4727 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4728 log_odp_key_attributes(&rl
, "present but not expected",
4729 extra_attrs
, out_of_range_attr
, key
, key_len
);
4730 return ODP_FIT_TOO_MUCH
;
4733 return ODP_FIT_PERFECT
;
4737 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4738 uint64_t present_attrs
, uint64_t *expected_attrs
,
4739 struct flow
*flow
, const struct flow
*src_flow
)
4741 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4742 bool is_mask
= flow
!= src_flow
;
4744 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4745 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4746 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4747 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4748 ntohs(flow
->dl_type
));
4751 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4752 flow
->dl_type
!= htons(0xffff)) {
4755 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4758 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4759 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4760 /* See comments in odp_flow_key_from_flow__(). */
4761 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4768 static enum odp_key_fitness
4769 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4770 uint64_t present_attrs
, int out_of_range_attr
,
4771 uint64_t expected_attrs
, struct flow
*flow
,
4772 const struct nlattr
*key
, size_t key_len
,
4773 const struct flow
*src_flow
)
4775 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4776 bool is_mask
= src_flow
!= flow
;
4777 const void *check_start
= NULL
;
4778 size_t check_len
= 0;
4779 enum ovs_key_attr expected_bit
= 0xff;
4781 if (eth_type_mpls(src_flow
->dl_type
)) {
4782 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4783 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4785 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4786 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4787 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4788 int n
= size
/ sizeof(ovs_be32
);
4791 if (!size
|| size
% sizeof(ovs_be32
)) {
4792 return ODP_FIT_ERROR
;
4794 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4795 return ODP_FIT_ERROR
;
4798 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4799 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4801 if (n
> FLOW_MAX_MPLS_LABELS
) {
4802 return ODP_FIT_TOO_MUCH
;
4806 /* BOS may be set only in the innermost label. */
4807 for (i
= 0; i
< n
- 1; i
++) {
4808 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4809 return ODP_FIT_ERROR
;
4813 /* BOS must be set in the innermost label. */
4814 if (n
< FLOW_MAX_MPLS_LABELS
4815 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4816 return ODP_FIT_TOO_LITTLE
;
4822 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4824 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4826 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4827 const struct ovs_key_ipv4
*ipv4_key
;
4829 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4830 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4831 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4832 return ODP_FIT_ERROR
;
4835 check_start
= ipv4_key
;
4836 check_len
= sizeof *ipv4_key
;
4837 expected_bit
= OVS_KEY_ATTR_IPV4
;
4840 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4842 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4844 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4845 const struct ovs_key_ipv6
*ipv6_key
;
4847 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4848 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4849 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4850 return ODP_FIT_ERROR
;
4853 check_start
= ipv6_key
;
4854 check_len
= sizeof *ipv6_key
;
4855 expected_bit
= OVS_KEY_ATTR_IPV6
;
4858 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4859 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4861 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4863 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4864 const struct ovs_key_arp
*arp_key
;
4866 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4867 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4868 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4869 "key", ntohs(arp_key
->arp_op
));
4870 return ODP_FIT_ERROR
;
4872 put_arp_key(arp_key
, flow
);
4874 check_start
= arp_key
;
4875 check_len
= sizeof *arp_key
;
4876 expected_bit
= OVS_KEY_ATTR_ARP
;
4882 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4883 if (!is_all_zeros(check_start
, check_len
) &&
4884 flow
->dl_type
!= htons(0xffff)) {
4885 return ODP_FIT_ERROR
;
4887 expected_attrs
|= UINT64_C(1) << expected_bit
;
4891 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4892 if (src_flow
->nw_proto
== IPPROTO_TCP
4893 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4894 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4895 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4897 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4899 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4900 const union ovs_key_tp
*tcp_key
;
4902 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4903 put_tp_key(tcp_key
, flow
);
4904 expected_bit
= OVS_KEY_ATTR_TCP
;
4906 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4907 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4908 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4910 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4911 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4912 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4913 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4915 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4917 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4918 const union ovs_key_tp
*udp_key
;
4920 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4921 put_tp_key(udp_key
, flow
);
4922 expected_bit
= OVS_KEY_ATTR_UDP
;
4924 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4925 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4926 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4927 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4929 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4931 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4932 const union ovs_key_tp
*sctp_key
;
4934 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4935 put_tp_key(sctp_key
, flow
);
4936 expected_bit
= OVS_KEY_ATTR_SCTP
;
4938 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4939 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4940 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4942 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4944 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4945 const struct ovs_key_icmp
*icmp_key
;
4947 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4948 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4949 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4950 expected_bit
= OVS_KEY_ATTR_ICMP
;
4952 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
4953 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4954 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4956 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
4958 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
4959 const struct ovs_key_icmpv6
*icmpv6_key
;
4961 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
4962 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
4963 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
4964 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
4965 if (is_nd(src_flow
, NULL
)) {
4967 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4969 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
4970 const struct ovs_key_nd
*nd_key
;
4972 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
4973 flow
->nd_target
= nd_key
->nd_target
;
4974 flow
->arp_sha
= nd_key
->nd_sll
;
4975 flow
->arp_tha
= nd_key
->nd_tll
;
4977 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
4978 * ICMP type and code are 8 bits wide. Therefore, an
4979 * exact match looks like htons(0xff), not
4980 * htons(0xffff). See xlate_wc_finish() for details.
4982 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
4983 (flow
->tp_src
!= htons(0xff) ||
4984 flow
->tp_dst
!= htons(0xff))) {
4985 return ODP_FIT_ERROR
;
4987 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4994 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
4995 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
4996 return ODP_FIT_ERROR
;
4998 expected_attrs
|= UINT64_C(1) << expected_bit
;
5003 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5007 /* Parse 802.1Q header then encapsulated L3 attributes. */
5008 static enum odp_key_fitness
5009 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5010 uint64_t present_attrs
, int out_of_range_attr
,
5011 uint64_t expected_attrs
, struct flow
*flow
,
5012 const struct nlattr
*key
, size_t key_len
,
5013 const struct flow
*src_flow
)
5015 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5016 bool is_mask
= src_flow
!= flow
;
5018 const struct nlattr
*encap
5019 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5020 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5021 enum odp_key_fitness encap_fitness
;
5022 enum odp_key_fitness fitness
;
5024 /* Calculate fitness of outer attributes. */
5026 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5027 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5029 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5030 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5032 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5033 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5036 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5037 expected_attrs
, key
, key_len
);
5040 * Remove the TPID from dl_type since it's not the real Ethertype. */
5041 flow
->dl_type
= htons(0);
5042 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5043 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5046 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
5047 return ODP_FIT_TOO_LITTLE
;
5048 } else if (flow
->vlan_tci
== htons(0)) {
5049 /* Corner case for a truncated 802.1Q header. */
5050 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5051 return ODP_FIT_TOO_MUCH
;
5054 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
5055 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5056 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
5057 return ODP_FIT_ERROR
;
5060 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5065 /* Now parse the encapsulated attributes. */
5066 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5067 attrs
, &present_attrs
, &out_of_range_attr
)) {
5068 return ODP_FIT_ERROR
;
5072 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
5073 return ODP_FIT_ERROR
;
5075 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5076 expected_attrs
, flow
, key
, key_len
,
5079 /* The overall fitness is the worse of the outer and inner attributes. */
5080 return MAX(fitness
, encap_fitness
);
5083 static enum odp_key_fitness
5084 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5085 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
], is_mask
,
5151 if (res
== ODP_FIT_ERROR
) {
5152 return ODP_FIT_ERROR
;
5153 } else if (res
== ODP_FIT_PERFECT
) {
5154 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5158 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5159 flow
->in_port
.odp_port
5160 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5161 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5162 } else if (!is_mask
) {
5163 flow
->in_port
.odp_port
= ODPP_NONE
;
5166 /* Ethernet header. */
5167 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5168 const struct ovs_key_ethernet
*eth_key
;
5170 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5171 put_ethernet_key(eth_key
, flow
);
5173 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5177 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5180 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5181 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5183 return ODP_FIT_ERROR
;
5187 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5188 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5189 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5190 expected_attrs
, flow
, key
, key_len
, src_flow
);
5193 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5194 flow
->vlan_tci
= htons(0xffff);
5195 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5196 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5197 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5200 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5201 expected_attrs
, flow
, key
, key_len
, src_flow
);
5204 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5205 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5206 * 'key' fits our expectations for what a flow key should contain.
5208 * The 'in_port' will be the datapath's understanding of the port. The
5209 * caller will need to translate with odp_port_to_ofp_port() if the
5210 * OpenFlow port is needed.
5212 * This function doesn't take the packet itself as an argument because none of
5213 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5214 * it is always possible to infer which additional attribute(s) should appear
5215 * by looking at the attributes for lower-level protocols, e.g. if the network
5216 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5217 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5218 * must be absent. */
5219 enum odp_key_fitness
5220 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5223 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
5226 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5227 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5228 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5229 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5230 * well 'key' fits our expectations for what a flow key should contain. */
5231 enum odp_key_fitness
5232 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5233 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
5236 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5237 &mask
->masks
, src_flow
);
5240 /* A missing mask means that the flow should be exact matched.
5241 * Generate an appropriate exact wildcard for the flow. */
5242 flow_wildcards_init_for_packet(mask
, src_flow
);
5244 return ODP_FIT_PERFECT
;
5248 /* Returns 'fitness' as a string, for use in debug messages. */
5250 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5253 case ODP_FIT_PERFECT
:
5255 case ODP_FIT_TOO_MUCH
:
5257 case ODP_FIT_TOO_LITTLE
:
5258 return "too_little";
5266 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5267 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5268 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5269 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5270 * null, then the return value is not meaningful.) */
5272 odp_put_userspace_action(uint32_t pid
,
5273 const void *userdata
, size_t userdata_size
,
5274 odp_port_t tunnel_out_port
,
5275 bool include_actions
,
5276 struct ofpbuf
*odp_actions
)
5278 size_t userdata_ofs
;
5281 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5282 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5284 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5286 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5287 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5290 * - The kernel rejected shorter userdata with -ERANGE.
5292 * - The kernel silently dropped userdata beyond the first 8 bytes.
5294 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5295 * separately disable features that required more than 8 bytes.) */
5296 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5297 MAX(8, userdata_size
)),
5298 userdata
, userdata_size
);
5302 if (tunnel_out_port
!= ODPP_NONE
) {
5303 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5306 if (include_actions
) {
5307 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5309 nl_msg_end_nested(odp_actions
, offset
);
5311 return userdata_ofs
;
5315 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5316 struct ofpbuf
*odp_actions
)
5318 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5319 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5320 nl_msg_end_nested(odp_actions
, offset
);
5324 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5325 struct ovs_action_push_tnl
*data
)
5327 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5329 size
+= data
->header_len
;
5330 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5334 /* The commit_odp_actions() function and its helpers. */
5337 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5338 const void *key
, size_t key_size
)
5340 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5341 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5342 nl_msg_end_nested(odp_actions
, offset
);
5345 /* Masked set actions have a mask following the data within the netlink
5346 * attribute. The unmasked bits in the data will be cleared as the data
5347 * is copied to the action. */
5349 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5350 enum ovs_key_attr key_type
,
5351 const void *key_
, const void *mask_
, size_t key_size
)
5353 size_t offset
= nl_msg_start_nested(odp_actions
,
5354 OVS_ACTION_ATTR_SET_MASKED
);
5355 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5356 const char *key
= key_
, *mask
= mask_
;
5358 memcpy(data
+ key_size
, mask
, key_size
);
5359 /* Clear unmasked bits while copying. */
5360 while (key_size
--) {
5361 *data
++ = *key
++ & *mask
++;
5363 nl_msg_end_nested(odp_actions
, offset
);
5366 /* If any of the flow key data that ODP actions can modify are different in
5367 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5368 * 'odp_actions' that change the flow tunneling information in key from
5369 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5370 * same way. In other words, operates the same as commit_odp_actions(), but
5371 * only on tunneling information. */
5373 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5374 struct ofpbuf
*odp_actions
)
5376 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5377 * must have non-zero ipv6_dst. */
5378 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5379 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5382 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5383 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5388 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5389 const void *key
, void *base
, void *mask
, size_t size
,
5390 struct ofpbuf
*odp_actions
)
5392 if (memcmp(key
, base
, size
)) {
5393 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5395 if (use_masked_set
&& !fully_masked
) {
5396 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5398 if (!fully_masked
) {
5399 memset(mask
, 0xff, size
);
5401 commit_set_action(odp_actions
, attr
, key
, size
);
5403 memcpy(base
, key
, size
);
5406 /* Mask bits are set when we have either read or set the corresponding
5407 * values. Masked bits will be exact-matched, no need to set them
5408 * if the value did not actually change. */
5414 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5416 eth
->eth_src
= flow
->dl_src
;
5417 eth
->eth_dst
= flow
->dl_dst
;
5421 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5423 flow
->dl_src
= eth
->eth_src
;
5424 flow
->dl_dst
= eth
->eth_dst
;
5428 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5429 struct ofpbuf
*odp_actions
,
5430 struct flow_wildcards
*wc
,
5433 struct ovs_key_ethernet key
, base
, mask
;
5435 get_ethernet_key(flow
, &key
);
5436 get_ethernet_key(base_flow
, &base
);
5437 get_ethernet_key(&wc
->masks
, &mask
);
5439 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5440 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5441 put_ethernet_key(&base
, base_flow
);
5442 put_ethernet_key(&mask
, &wc
->masks
);
5447 pop_vlan(struct flow
*base
,
5448 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5450 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5452 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5453 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5459 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5460 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5462 if (base
->vlan_tci
== vlan_tci
) {
5466 pop_vlan(base
, odp_actions
, wc
);
5467 if (vlan_tci
& htons(VLAN_CFI
)) {
5468 struct ovs_action_push_vlan vlan
;
5470 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5471 vlan
.vlan_tci
= vlan_tci
;
5472 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5473 &vlan
, sizeof vlan
);
5475 base
->vlan_tci
= vlan_tci
;
5478 /* Wildcarding already done at action translation time. */
5480 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5481 struct ofpbuf
*odp_actions
)
5483 int base_n
= flow_count_mpls_labels(base
, NULL
);
5484 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5485 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5488 while (base_n
> common_n
) {
5489 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5490 /* If there is only one more LSE in base than there are common
5491 * between base and flow; and flow has at least one more LSE than
5492 * is common then the topmost LSE of base may be updated using
5494 struct ovs_key_mpls mpls_key
;
5496 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5497 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5498 &mpls_key
, sizeof mpls_key
);
5499 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5502 /* Otherwise, if there more LSEs in base than are common between
5503 * base and flow then pop the topmost one. */
5507 /* If all the LSEs are to be popped and this is not the outermost
5508 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5509 * POP_MPLS action instead of flow->dl_type.
5511 * This is because the POP_MPLS action requires its ethertype
5512 * argument to be an MPLS ethernet type but in this case
5513 * flow->dl_type will be a non-MPLS ethernet type.
5515 * When the final POP_MPLS action occurs it use flow->dl_type and
5516 * the and the resulting packet will have the desired dl_type. */
5517 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5518 dl_type
= htons(ETH_TYPE_MPLS
);
5520 dl_type
= flow
->dl_type
;
5522 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5523 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5529 /* If, after the above popping and setting, there are more LSEs in flow
5530 * than base then some LSEs need to be pushed. */
5531 while (base_n
< flow_n
) {
5532 struct ovs_action_push_mpls
*mpls
;
5534 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5535 OVS_ACTION_ATTR_PUSH_MPLS
,
5537 mpls
->mpls_ethertype
= flow
->dl_type
;
5538 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5539 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
5540 * headers if the flow is restored later due to returning from a patch
5541 * port or group bucket. */
5542 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
5543 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5549 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5551 ipv4
->ipv4_src
= flow
->nw_src
;
5552 ipv4
->ipv4_dst
= flow
->nw_dst
;
5553 ipv4
->ipv4_proto
= flow
->nw_proto
;
5554 ipv4
->ipv4_tos
= flow
->nw_tos
;
5555 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5556 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5560 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5562 flow
->nw_src
= ipv4
->ipv4_src
;
5563 flow
->nw_dst
= ipv4
->ipv4_dst
;
5564 flow
->nw_proto
= ipv4
->ipv4_proto
;
5565 flow
->nw_tos
= ipv4
->ipv4_tos
;
5566 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5567 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5571 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5572 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5575 struct ovs_key_ipv4 key
, mask
, base
;
5577 /* Check that nw_proto and nw_frag remain unchanged. */
5578 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5579 flow
->nw_frag
== base_flow
->nw_frag
);
5581 get_ipv4_key(flow
, &key
, false);
5582 get_ipv4_key(base_flow
, &base
, false);
5583 get_ipv4_key(&wc
->masks
, &mask
, true);
5584 mask
.ipv4_proto
= 0; /* Not writeable. */
5585 mask
.ipv4_frag
= 0; /* Not writable. */
5587 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5589 put_ipv4_key(&base
, base_flow
, false);
5590 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5591 put_ipv4_key(&mask
, &wc
->masks
, true);
5597 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5599 ipv6
->ipv6_src
= flow
->ipv6_src
;
5600 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
5601 ipv6
->ipv6_label
= flow
->ipv6_label
;
5602 ipv6
->ipv6_proto
= flow
->nw_proto
;
5603 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5604 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5605 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5609 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5611 flow
->ipv6_src
= ipv6
->ipv6_src
;
5612 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
5613 flow
->ipv6_label
= ipv6
->ipv6_label
;
5614 flow
->nw_proto
= ipv6
->ipv6_proto
;
5615 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5616 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5617 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5621 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5622 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5625 struct ovs_key_ipv6 key
, mask
, base
;
5627 /* Check that nw_proto and nw_frag remain unchanged. */
5628 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5629 flow
->nw_frag
== base_flow
->nw_frag
);
5631 get_ipv6_key(flow
, &key
, false);
5632 get_ipv6_key(base_flow
, &base
, false);
5633 get_ipv6_key(&wc
->masks
, &mask
, true);
5634 mask
.ipv6_proto
= 0; /* Not writeable. */
5635 mask
.ipv6_frag
= 0; /* Not writable. */
5637 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5639 put_ipv6_key(&base
, base_flow
, false);
5640 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5641 put_ipv6_key(&mask
, &wc
->masks
, true);
5647 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5649 /* ARP key has padding, clear it. */
5650 memset(arp
, 0, sizeof *arp
);
5652 arp
->arp_sip
= flow
->nw_src
;
5653 arp
->arp_tip
= flow
->nw_dst
;
5654 arp
->arp_op
= htons(flow
->nw_proto
);
5655 arp
->arp_sha
= flow
->arp_sha
;
5656 arp
->arp_tha
= flow
->arp_tha
;
5660 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5662 flow
->nw_src
= arp
->arp_sip
;
5663 flow
->nw_dst
= arp
->arp_tip
;
5664 flow
->nw_proto
= ntohs(arp
->arp_op
);
5665 flow
->arp_sha
= arp
->arp_sha
;
5666 flow
->arp_tha
= arp
->arp_tha
;
5669 static enum slow_path_reason
5670 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5671 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5673 struct ovs_key_arp key
, mask
, base
;
5675 get_arp_key(flow
, &key
);
5676 get_arp_key(base_flow
, &base
);
5677 get_arp_key(&wc
->masks
, &mask
);
5679 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5681 put_arp_key(&base
, base_flow
);
5682 put_arp_key(&mask
, &wc
->masks
);
5689 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5691 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5692 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5693 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5697 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5699 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5700 flow
->tp_src
= htons(icmp
->icmp_type
);
5701 flow
->tp_dst
= htons(icmp
->icmp_code
);
5704 static enum slow_path_reason
5705 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5706 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5708 struct ovs_key_icmp key
, mask
, base
;
5709 enum ovs_key_attr attr
;
5711 if (is_icmpv4(flow
, NULL
)) {
5712 attr
= OVS_KEY_ATTR_ICMP
;
5713 } else if (is_icmpv6(flow
, NULL
)) {
5714 attr
= OVS_KEY_ATTR_ICMPV6
;
5719 get_icmp_key(flow
, &key
);
5720 get_icmp_key(base_flow
, &base
);
5721 get_icmp_key(&wc
->masks
, &mask
);
5723 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5724 put_icmp_key(&base
, base_flow
);
5725 put_icmp_key(&mask
, &wc
->masks
);
5732 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5734 nd
->nd_target
= flow
->nd_target
;
5735 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5736 nd
->nd_sll
= flow
->arp_sha
;
5737 nd
->nd_tll
= flow
->arp_tha
;
5741 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5743 flow
->nd_target
= nd
->nd_target
;
5744 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5745 flow
->arp_sha
= nd
->nd_sll
;
5746 flow
->arp_tha
= nd
->nd_tll
;
5749 static enum slow_path_reason
5750 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5751 struct ofpbuf
*odp_actions
,
5752 struct flow_wildcards
*wc
, bool use_masked
)
5754 struct ovs_key_nd key
, mask
, base
;
5756 get_nd_key(flow
, &key
);
5757 get_nd_key(base_flow
, &base
);
5758 get_nd_key(&wc
->masks
, &mask
);
5760 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5762 put_nd_key(&base
, base_flow
);
5763 put_nd_key(&mask
, &wc
->masks
);
5770 static enum slow_path_reason
5771 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5772 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5775 /* Check if 'flow' really has an L3 header. */
5776 if (!flow
->nw_proto
) {
5780 switch (ntohs(base
->dl_type
)) {
5782 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5786 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5787 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5790 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5796 /* TCP, UDP, and SCTP keys have the same layout. */
5797 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5798 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5801 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5803 tp
->tcp
.tcp_src
= flow
->tp_src
;
5804 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5808 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5810 flow
->tp_src
= tp
->tcp
.tcp_src
;
5811 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5815 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5816 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5819 enum ovs_key_attr key_type
;
5820 union ovs_key_tp key
, mask
, base
;
5822 /* Check if 'flow' really has an L3 header. */
5823 if (!flow
->nw_proto
) {
5827 if (!is_ip_any(base_flow
)) {
5831 if (flow
->nw_proto
== IPPROTO_TCP
) {
5832 key_type
= OVS_KEY_ATTR_TCP
;
5833 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5834 key_type
= OVS_KEY_ATTR_UDP
;
5835 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5836 key_type
= OVS_KEY_ATTR_SCTP
;
5841 get_tp_key(flow
, &key
);
5842 get_tp_key(base_flow
, &base
);
5843 get_tp_key(&wc
->masks
, &mask
);
5845 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5847 put_tp_key(&base
, base_flow
);
5848 put_tp_key(&mask
, &wc
->masks
);
5853 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5854 struct ofpbuf
*odp_actions
,
5855 struct flow_wildcards
*wc
,
5858 uint32_t key
, mask
, base
;
5860 key
= flow
->skb_priority
;
5861 base
= base_flow
->skb_priority
;
5862 mask
= wc
->masks
.skb_priority
;
5864 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5865 sizeof key
, odp_actions
)) {
5866 base_flow
->skb_priority
= base
;
5867 wc
->masks
.skb_priority
= mask
;
5872 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5873 struct ofpbuf
*odp_actions
,
5874 struct flow_wildcards
*wc
,
5877 uint32_t key
, mask
, base
;
5879 key
= flow
->pkt_mark
;
5880 base
= base_flow
->pkt_mark
;
5881 mask
= wc
->masks
.pkt_mark
;
5883 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5884 sizeof key
, odp_actions
)) {
5885 base_flow
->pkt_mark
= base
;
5886 wc
->masks
.pkt_mark
= mask
;
5890 /* If any of the flow key data that ODP actions can modify are different in
5891 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5892 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5893 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5894 * in addition to this function if needed. Sets fields in 'wc' that are
5895 * used as part of the action.
5897 * Returns a reason to force processing the flow's packets into the userspace
5898 * slow path, if there is one, otherwise 0. */
5899 enum slow_path_reason
5900 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5901 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5904 enum slow_path_reason slow1
, slow2
;
5905 bool mpls_done
= false;
5907 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5908 /* Make packet a non-MPLS packet before committing L3/4 actions,
5909 * which would otherwise do nothing. */
5910 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
5911 commit_mpls_action(flow
, base
, odp_actions
);
5914 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5915 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5916 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5918 commit_mpls_action(flow
, base
, odp_actions
);
5920 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5921 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5922 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5924 return slow1
? slow1
: slow2
;