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((const struct in6_addr
*)ipv6_mask
->ipv6_src
)
2063 && ipv6_mask_is_exact((const struct in6_addr
*)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(struct ds
*ds
, const char *name
, const ovs_be32 key_
[4],
2214 const ovs_be32 (*mask_
)[4], bool verbose
)
2216 format_in6_addr(ds
, name
,
2217 (const struct in6_addr
*)key_
,
2218 mask_
? (const struct in6_addr
*)*mask_
: NULL
,
2223 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2224 const ovs_be32
*mask
, bool verbose
)
2226 bool mask_empty
= mask
&& !*mask
;
2228 if (verbose
|| !mask_empty
) {
2229 bool mask_full
= !mask
2230 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2232 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2233 if (!mask_full
) { /* Partially masked. */
2234 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2236 ds_put_char(ds
, ',');
2241 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2242 const uint8_t *mask
, bool verbose
)
2244 bool mask_empty
= mask
&& !*mask
;
2246 if (verbose
|| !mask_empty
) {
2247 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2249 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2250 if (!mask_full
) { /* Partially masked. */
2251 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2253 ds_put_char(ds
, ',');
2258 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2259 const uint8_t *mask
, bool verbose
)
2261 bool mask_empty
= mask
&& !*mask
;
2263 if (verbose
|| !mask_empty
) {
2264 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2266 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2267 if (!mask_full
) { /* Partially masked. */
2268 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2270 ds_put_char(ds
, ',');
2275 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2276 const ovs_be16
*mask
, bool verbose
)
2278 bool mask_empty
= mask
&& !*mask
;
2280 if (verbose
|| !mask_empty
) {
2281 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2283 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2284 if (!mask_full
) { /* Partially masked. */
2285 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2287 ds_put_char(ds
, ',');
2292 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2293 const ovs_be16
*mask
, bool verbose
)
2295 bool mask_empty
= mask
&& !*mask
;
2297 if (verbose
|| !mask_empty
) {
2298 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2300 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2301 if (!mask_full
) { /* Partially masked. */
2302 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2304 ds_put_char(ds
, ',');
2309 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2310 const uint16_t *mask
, bool verbose
)
2312 bool mask_empty
= mask
&& !*mask
;
2314 if (verbose
|| !mask_empty
) {
2315 ds_put_cstr(ds
, name
);
2316 ds_put_char(ds
, '(');
2318 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2319 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2320 } else { /* Fully masked. */
2321 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2323 ds_put_cstr(ds
, "),");
2328 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2329 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2333 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2334 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2335 expected_len
!= ATTR_LEN_NESTED
) {
2337 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2338 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2340 if (bad_key_len
|| bad_mask_len
) {
2342 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2345 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2346 nl_attr_get_size(a
), expected_len
);
2348 format_generic_odp_key(a
, ds
);
2350 ds_put_char(ds
, '/');
2352 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2353 nl_attr_get_size(ma
), expected_len
);
2355 format_generic_odp_key(ma
, ds
);
2357 ds_put_char(ds
, ')');
2366 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2367 const struct nlattr
*ma
)
2369 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2370 format_generic_odp_key(a
, ds
);
2371 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2372 ds_put_char(ds
, '/');
2373 format_generic_odp_key(ma
, ds
);
2375 ds_put_cstr(ds
, "),");
2379 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2380 const struct nlattr
*mask_attr
, struct ds
*ds
,
2384 const struct nlattr
*a
;
2387 ofpbuf_init(&ofp
, 100);
2388 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2389 uint16_t type
= nl_attr_type(a
);
2390 const struct nlattr
*ma
= NULL
;
2393 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2394 nl_attr_get_size(mask_attr
), type
);
2396 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2402 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2403 OVS_VXLAN_EXT_MAX
, true)) {
2408 case OVS_VXLAN_EXT_GBP
: {
2409 uint32_t key
= nl_attr_get_u32(a
);
2410 ovs_be16 id
, id_mask
;
2411 uint8_t flags
, flags_mask
= 0;
2413 id
= htons(key
& 0xFFFF);
2414 flags
= (key
>> 16) & 0xFF;
2416 uint32_t mask
= nl_attr_get_u32(ma
);
2417 id_mask
= htons(mask
& 0xFFFF);
2418 flags_mask
= (mask
>> 16) & 0xFF;
2421 ds_put_cstr(ds
, "gbp(");
2422 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2423 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2425 ds_put_cstr(ds
, "),");
2430 format_unknown_key(ds
, a
, ma
);
2436 ofpbuf_uninit(&ofp
);
2439 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2442 format_geneve_opts(const struct geneve_opt
*opt
,
2443 const struct geneve_opt
*mask
, int opts_len
,
2444 struct ds
*ds
, bool verbose
)
2446 while (opts_len
> 0) {
2448 uint8_t data_len
, data_len_mask
;
2450 if (opts_len
< sizeof *opt
) {
2451 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2452 opts_len
, sizeof *opt
);
2456 data_len
= opt
->length
* 4;
2458 if (mask
->length
== 0x1f) {
2459 data_len_mask
= UINT8_MAX
;
2461 data_len_mask
= mask
->length
;
2464 len
= sizeof *opt
+ data_len
;
2465 if (len
> opts_len
) {
2466 ds_put_format(ds
, "opt len %u greater than remaining %u",
2471 ds_put_char(ds
, '{');
2472 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2474 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2475 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2477 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2478 ds_put_hex(ds
, opt
+ 1, data_len
);
2479 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2480 ds_put_char(ds
, '/');
2481 ds_put_hex(ds
, mask
+ 1, data_len
);
2486 ds_put_char(ds
, '}');
2488 opt
+= len
/ sizeof(*opt
);
2490 mask
+= len
/ sizeof(*opt
);
2497 format_odp_tun_geneve(const struct nlattr
*attr
,
2498 const struct nlattr
*mask_attr
, struct ds
*ds
,
2501 int opts_len
= nl_attr_get_size(attr
);
2502 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2503 const struct geneve_opt
*mask
= mask_attr
?
2504 nl_attr_get(mask_attr
) : NULL
;
2506 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2507 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2508 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2512 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2516 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2517 struct ds
*ds
, bool verbose
)
2520 const struct nlattr
*a
;
2522 uint16_t mask_flags
= 0;
2525 ofpbuf_init(&ofp
, 100);
2526 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2527 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2528 const struct nlattr
*ma
= NULL
;
2531 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2532 nl_attr_get_size(mask_attr
), type
);
2534 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2535 OVS_TUNNEL_KEY_ATTR_MAX
,
2540 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2541 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2546 case OVS_TUNNEL_KEY_ATTR_ID
:
2547 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2548 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2549 flags
|= FLOW_TNL_F_KEY
;
2551 mask_flags
|= FLOW_TNL_F_KEY
;
2554 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2555 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2556 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2558 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2559 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2560 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2562 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2563 struct in6_addr ipv6_src
;
2564 ipv6_src
= nl_attr_get_in6_addr(a
);
2565 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2566 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2569 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2570 struct in6_addr ipv6_dst
;
2571 ipv6_dst
= nl_attr_get_in6_addr(a
);
2572 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2573 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2576 case OVS_TUNNEL_KEY_ATTR_TOS
:
2577 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2578 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2580 case OVS_TUNNEL_KEY_ATTR_TTL
:
2581 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2582 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2584 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2585 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2587 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2588 flags
|= FLOW_TNL_F_CSUM
;
2590 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2591 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2592 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2594 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2595 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2596 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2598 case OVS_TUNNEL_KEY_ATTR_OAM
:
2599 flags
|= FLOW_TNL_F_OAM
;
2601 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2602 ds_put_cstr(ds
, "vxlan(");
2603 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2604 ds_put_cstr(ds
, "),");
2606 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2607 ds_put_cstr(ds
, "geneve(");
2608 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2609 ds_put_cstr(ds
, "),");
2611 case OVS_TUNNEL_KEY_ATTR_PAD
:
2613 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2615 format_unknown_key(ds
, a
, ma
);
2620 /* Flags can have a valid mask even if the attribute is not set, so
2621 * we need to collect these separately. */
2623 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2624 switch (nl_attr_type(a
)) {
2625 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2626 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2628 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2629 mask_flags
|= FLOW_TNL_F_CSUM
;
2631 case OVS_TUNNEL_KEY_ATTR_OAM
:
2632 mask_flags
|= FLOW_TNL_F_OAM
;
2638 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2641 ofpbuf_uninit(&ofp
);
2645 odp_ct_state_to_string(uint32_t flag
)
2648 case OVS_CS_F_REPLY_DIR
:
2650 case OVS_CS_F_TRACKED
:
2654 case OVS_CS_F_ESTABLISHED
:
2656 case OVS_CS_F_RELATED
:
2658 case OVS_CS_F_INVALID
:
2660 case OVS_CS_F_SRC_NAT
:
2662 case OVS_CS_F_DST_NAT
:
2670 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2671 const uint8_t *mask
, bool verbose
)
2673 bool mask_empty
= mask
&& !*mask
;
2675 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2676 if (verbose
|| !mask_empty
) {
2677 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2679 if (!mask_full
) { /* Partially masked. */
2680 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2683 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2689 mask_empty(const struct nlattr
*ma
)
2697 mask
= nl_attr_get(ma
);
2698 n
= nl_attr_get_size(ma
);
2700 return is_all_zeros(mask
, n
);
2704 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2705 const struct hmap
*portno_names
, struct ds
*ds
,
2708 enum ovs_key_attr attr
= nl_attr_type(a
);
2709 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2712 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2714 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2716 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2717 OVS_KEY_ATTR_MAX
, false)) {
2721 ds_put_char(ds
, '(');
2723 case OVS_KEY_ATTR_ENCAP
:
2724 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2725 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2726 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2728 } else if (nl_attr_get_size(a
)) {
2729 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2734 case OVS_KEY_ATTR_PRIORITY
:
2735 case OVS_KEY_ATTR_SKB_MARK
:
2736 case OVS_KEY_ATTR_DP_HASH
:
2737 case OVS_KEY_ATTR_RECIRC_ID
:
2738 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2740 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2744 case OVS_KEY_ATTR_CT_MARK
:
2745 if (verbose
|| !mask_empty(ma
)) {
2746 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2748 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2753 case OVS_KEY_ATTR_CT_STATE
:
2755 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2757 ds_put_format(ds
, "/%#"PRIx32
,
2758 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2760 } else if (!is_exact
) {
2761 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2763 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2766 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2770 case OVS_KEY_ATTR_CT_ZONE
:
2771 if (verbose
|| !mask_empty(ma
)) {
2772 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2774 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2779 case OVS_KEY_ATTR_CT_LABELS
: {
2780 const ovs_u128
*value
= nl_attr_get(a
);
2781 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2783 format_u128(ds
, value
, mask
, verbose
);
2787 case OVS_KEY_ATTR_TUNNEL
:
2788 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2791 case OVS_KEY_ATTR_IN_PORT
:
2792 if (portno_names
&& verbose
&& is_exact
) {
2793 char *name
= odp_portno_names_get(portno_names
,
2794 nl_attr_get_odp_port(a
));
2796 ds_put_format(ds
, "%s", name
);
2798 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2801 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2803 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2808 case OVS_KEY_ATTR_ETHERNET
: {
2809 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2810 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2812 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2813 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2817 case OVS_KEY_ATTR_VLAN
:
2818 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2819 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2822 case OVS_KEY_ATTR_MPLS
: {
2823 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2824 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2825 size_t size
= nl_attr_get_size(a
);
2827 if (!size
|| size
% sizeof *mpls_key
) {
2828 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2832 mpls_mask
= nl_attr_get(ma
);
2833 if (size
!= nl_attr_get_size(ma
)) {
2834 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2835 "mask length %"PRIuSIZE
")",
2836 size
, nl_attr_get_size(ma
));
2840 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2843 case OVS_KEY_ATTR_ETHERTYPE
:
2844 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2846 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2850 case OVS_KEY_ATTR_IPV4
: {
2851 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2852 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2854 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2855 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2856 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2858 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2859 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2860 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2865 case OVS_KEY_ATTR_IPV6
: {
2866 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2867 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2869 format_ipv6(ds
, "src", key
->ipv6_src
, MASK(mask
, ipv6_src
), verbose
);
2870 format_ipv6(ds
, "dst", key
->ipv6_dst
, MASK(mask
, ipv6_dst
), verbose
);
2871 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2873 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2875 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2877 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2879 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2884 /* These have the same structure and format. */
2885 case OVS_KEY_ATTR_TCP
:
2886 case OVS_KEY_ATTR_UDP
:
2887 case OVS_KEY_ATTR_SCTP
: {
2888 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2889 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2891 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2892 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2896 case OVS_KEY_ATTR_TCP_FLAGS
:
2898 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2899 ntohs(nl_attr_get_be16(a
)),
2900 TCP_FLAGS(nl_attr_get_be16(ma
)),
2901 TCP_FLAGS(OVS_BE16_MAX
));
2903 format_flags(ds
, packet_tcp_flag_to_string
,
2904 ntohs(nl_attr_get_be16(a
)), '|');
2908 case OVS_KEY_ATTR_ICMP
: {
2909 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2910 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2912 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2913 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2917 case OVS_KEY_ATTR_ICMPV6
: {
2918 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2919 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2921 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2923 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2928 case OVS_KEY_ATTR_ARP
: {
2929 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2930 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2932 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2933 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2934 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2935 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2936 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2940 case OVS_KEY_ATTR_ND
: {
2941 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2942 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2944 format_ipv6(ds
, "target", key
->nd_target
, MASK(mask
, nd_target
),
2946 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2947 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2952 case OVS_KEY_ATTR_UNSPEC
:
2953 case __OVS_KEY_ATTR_MAX
:
2955 format_generic_odp_key(a
, ds
);
2957 ds_put_char(ds
, '/');
2958 format_generic_odp_key(ma
, ds
);
2962 ds_put_char(ds
, ')');
2965 static struct nlattr
*
2966 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2967 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2969 const struct nlattr
*a
;
2971 int type
= nl_attr_type(key
);
2972 int size
= nl_attr_get_size(key
);
2974 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2975 nl_msg_put_unspec_zero(ofp
, type
, size
);
2979 if (tbl
[type
].next
) {
2980 tbl
= tbl
[type
].next
;
2981 max
= tbl
[type
].next_max
;
2984 nested_mask
= nl_msg_start_nested(ofp
, type
);
2985 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2986 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2988 nl_msg_end_nested(ofp
, nested_mask
);
2995 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
2998 if (verbose
|| (mask
&& !ovs_u128_is_zero(*mask
))) {
3001 value
= hton128(*key
);
3002 ds_put_hex(ds
, &value
, sizeof value
);
3003 if (mask
&& !(ovs_u128_is_ones(*mask
))) {
3004 value
= hton128(*mask
);
3005 ds_put_char(ds
, '/');
3006 ds_put_hex(ds
, &value
, sizeof value
);
3011 /* Read the string from 's_' as a 128-bit value. If the string contains
3012 * a "/", the rest of the string will be treated as a 128-bit mask.
3014 * If either the value or mask is larger than 64 bits, the string must
3015 * be in hexadecimal.
3018 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
3020 char *s
= CONST_CAST(char *, s_
);
3024 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3025 *value
= ntoh128(be_value
);
3030 if (ovs_scan(s
, "/%n", &n
)) {
3034 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3035 sizeof be_mask
, &s
);
3039 *mask
= ntoh128(be_mask
);
3041 *mask
= OVS_U128_MAX
;
3051 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3055 if (ovs_scan(s
, "ufid:")) {
3058 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3070 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3072 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3075 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3076 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3077 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3078 * non-null and 'verbose' is true, translates odp port number to its name. */
3080 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3081 const struct nlattr
*mask
, size_t mask_len
,
3082 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3085 const struct nlattr
*a
;
3087 bool has_ethtype_key
= false;
3088 const struct nlattr
*ma
= NULL
;
3090 bool first_field
= true;
3092 ofpbuf_init(&ofp
, 100);
3093 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3094 bool is_nested_attr
;
3095 bool is_wildcard
= false;
3096 int attr_type
= nl_attr_type(a
);
3098 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3099 has_ethtype_key
= true;
3102 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3103 OVS_KEY_ATTR_MAX
, attr_type
) ==
3106 if (mask
&& mask_len
) {
3107 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3108 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3111 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3112 if (is_wildcard
&& !ma
) {
3113 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3118 ds_put_char(ds
, ',');
3120 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3121 first_field
= false;
3125 ofpbuf_uninit(&ofp
);
3130 if (left
== key_len
) {
3131 ds_put_cstr(ds
, "<empty>");
3133 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3134 for (i
= 0; i
< left
; i
++) {
3135 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3137 ds_put_char(ds
, ')');
3139 if (!has_ethtype_key
) {
3140 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3142 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3143 ntohs(nl_attr_get_be16(ma
)));
3147 ds_put_cstr(ds
, "<empty>");
3151 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3152 * OVS_KEY_ATTR_* attributes in 'key'. */
3154 odp_flow_key_format(const struct nlattr
*key
,
3155 size_t key_len
, struct ds
*ds
)
3157 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3161 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3163 if (!strcasecmp(s
, "no")) {
3164 *type
= OVS_FRAG_TYPE_NONE
;
3165 } else if (!strcasecmp(s
, "first")) {
3166 *type
= OVS_FRAG_TYPE_FIRST
;
3167 } else if (!strcasecmp(s
, "later")) {
3168 *type
= OVS_FRAG_TYPE_LATER
;
3178 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3182 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3183 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3187 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3188 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3191 memset(mask
, 0xff, sizeof *mask
);
3200 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3204 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3208 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3209 IP_SCAN_ARGS(mask
), &n
)) {
3212 *mask
= OVS_BE32_MAX
;
3221 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3224 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3226 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3227 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3231 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3232 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3235 memset(mask
, 0xff, sizeof *mask
);
3244 scan_ipv6(const char *s
, ovs_be32 (*key
)[4], ovs_be32 (*mask
)[4])
3246 return scan_in6_addr(s
, key
? (struct in6_addr
*) *key
: NULL
,
3247 mask
? (struct in6_addr
*) *mask
: NULL
);
3251 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3256 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3257 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3262 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3263 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3265 *mask
= htonl(mask_
);
3267 *mask
= htonl(IPV6_LABEL_MASK
);
3276 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3280 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3284 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3296 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3300 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3304 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3316 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3320 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3324 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3336 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3338 uint16_t key_
, mask_
;
3341 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3346 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3348 *mask
= htons(mask_
);
3350 *mask
= OVS_BE16_MAX
;
3359 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3361 uint64_t key_
, mask_
;
3364 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3367 *key
= htonll(key_
);
3369 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3371 *mask
= htonll(mask_
);
3373 *mask
= OVS_BE64_MAX
;
3382 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3384 uint32_t flags
, fmask
;
3387 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3388 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3389 if (n
>= 0 && s
[n
] == ')') {
3400 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3402 uint32_t flags
, fmask
;
3405 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3406 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3408 *key
= htons(flags
);
3410 *mask
= htons(fmask
);
3418 ovs_to_odp_ct_state(uint8_t state
)
3422 if (state
& CS_NEW
) {
3423 odp
|= OVS_CS_F_NEW
;
3425 if (state
& CS_ESTABLISHED
) {
3426 odp
|= OVS_CS_F_ESTABLISHED
;
3428 if (state
& CS_RELATED
) {
3429 odp
|= OVS_CS_F_RELATED
;
3431 if (state
& CS_INVALID
) {
3432 odp
|= OVS_CS_F_INVALID
;
3434 if (state
& CS_REPLY_DIR
) {
3435 odp
|= OVS_CS_F_REPLY_DIR
;
3437 if (state
& CS_TRACKED
) {
3438 odp
|= OVS_CS_F_TRACKED
;
3440 if (state
& CS_SRC_NAT
) {
3441 odp
|= OVS_CS_F_SRC_NAT
;
3443 if (state
& CS_DST_NAT
) {
3444 odp
|= OVS_CS_F_DST_NAT
;
3451 odp_to_ovs_ct_state(uint32_t flags
)
3455 if (flags
& OVS_CS_F_NEW
) {
3458 if (flags
& OVS_CS_F_ESTABLISHED
) {
3459 state
|= CS_ESTABLISHED
;
3461 if (flags
& OVS_CS_F_RELATED
) {
3462 state
|= CS_RELATED
;
3464 if (flags
& OVS_CS_F_INVALID
) {
3465 state
|= CS_INVALID
;
3467 if (flags
& OVS_CS_F_REPLY_DIR
) {
3468 state
|= CS_REPLY_DIR
;
3470 if (flags
& OVS_CS_F_TRACKED
) {
3471 state
|= CS_TRACKED
;
3473 if (flags
& OVS_CS_F_SRC_NAT
) {
3474 state
|= CS_SRC_NAT
;
3476 if (flags
& OVS_CS_F_DST_NAT
) {
3477 state
|= CS_DST_NAT
;
3484 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3486 uint32_t flags
, fmask
;
3489 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3490 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3491 mask
? &fmask
: NULL
);
3504 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3508 enum ovs_frag_type frag_type
;
3510 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3511 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3524 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3525 const struct simap
*port_names
)
3529 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3533 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3540 } else if (port_names
) {
3541 const struct simap_node
*node
;
3544 len
= strcspn(s
, ")");
3545 node
= simap_find_len(port_names
, s
, len
);
3558 /* Helper for vlan parsing. */
3559 struct ovs_key_vlan__
{
3564 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3566 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3568 if (value
>> bits
) {
3572 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3577 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3580 uint16_t key_
, mask_
;
3583 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3586 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3588 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3591 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3595 *mask
|= htons(((1U << bits
) - 1) << offset
);
3605 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3607 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3611 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3613 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3617 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3619 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3624 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3626 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3628 if (value
>> bits
) {
3632 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3637 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3640 uint32_t key_
, mask_
;
3643 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3646 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3648 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3651 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3655 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3665 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3667 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3671 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3673 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3677 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3679 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3683 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3685 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3689 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3691 const char *s_base
= s
;
3692 ovs_be16 id
= 0, id_mask
= 0;
3693 uint8_t flags
= 0, flags_mask
= 0;
3695 if (!strncmp(s
, "id=", 3)) {
3697 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3703 if (!strncmp(s
, "flags=", 6)) {
3705 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3708 if (!strncmp(s
, "))", 2)) {
3711 *key
= (flags
<< 16) | ntohs(id
);
3713 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3723 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3725 const char *s_base
= s
;
3726 struct geneve_opt
*opt
= key
->d
;
3727 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3728 int len_remain
= sizeof key
->d
;
3730 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3734 len_remain
-= sizeof *opt
;
3736 if (!strncmp(s
, "class=", 6)) {
3738 s
+= scan_be16(s
, &opt
->opt_class
,
3739 mask
? &opt_mask
->opt_class
: NULL
);
3741 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3747 if (!strncmp(s
, "type=", 5)) {
3749 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3751 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3757 if (!strncmp(s
, "len=", 4)) {
3758 uint8_t opt_len
, opt_len_mask
;
3760 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3762 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3765 opt
->length
= opt_len
/ 4;
3767 opt_mask
->length
= opt_len_mask
;
3771 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3777 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3784 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3785 data_len
, (char **)&s
)) {
3796 opt
+= 1 + data_len
/ 4;
3798 opt_mask
+= 1 + data_len
/ 4;
3800 len_remain
-= data_len
;
3805 int len
= sizeof key
->d
- len_remain
;
3819 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3821 const uint16_t *flags
= data_
;
3823 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3824 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3826 if (*flags
& FLOW_TNL_F_CSUM
) {
3827 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3829 if (*flags
& FLOW_TNL_F_OAM
) {
3830 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3835 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3837 const uint32_t *gbp
= data_
;
3840 size_t vxlan_opts_ofs
;
3842 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3843 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3844 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3849 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3851 const struct geneve_scan
*geneve
= data_
;
3853 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3857 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3859 unsigned long call_fn = (unsigned long)FUNC; \
3861 typedef void (*fn)(struct ofpbuf *, const void *); \
3863 func(BUF, &(DATA)); \
3865 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3869 #define SCAN_IF(NAME) \
3870 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3871 const char *start = s; \
3876 /* Usually no special initialization is needed. */
3877 #define SCAN_BEGIN(NAME, TYPE) \
3880 memset(&skey, 0, sizeof skey); \
3881 memset(&smask, 0, sizeof smask); \
3885 /* Init as fully-masked as mask will not be scanned. */
3886 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3889 memset(&skey, 0, sizeof skey); \
3890 memset(&smask, 0xff, sizeof smask); \
3894 /* VLAN needs special initialization. */
3895 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3897 TYPE skey = KEY_INIT; \
3898 TYPE smask = MASK_INIT; \
3902 /* Scan unnamed entry as 'TYPE' */
3903 #define SCAN_TYPE(TYPE, KEY, MASK) \
3904 len = scan_##TYPE(s, KEY, MASK); \
3910 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3911 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3912 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3913 s += strlen(NAME); \
3914 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3918 #define SCAN_FINISH() \
3919 } while (*s++ == ',' && len != 0); \
3920 if (s[-1] != ')') { \
3924 #define SCAN_FINISH_SINGLE() \
3926 if (*s++ != ')') { \
3930 /* Beginning of nested attribute. */
3931 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3933 size_t key_offset, mask_offset; \
3934 key_offset = nl_msg_start_nested(key, ATTR); \
3936 mask_offset = nl_msg_start_nested(mask, ATTR); \
3941 #define SCAN_END_NESTED() \
3943 nl_msg_end_nested(key, key_offset); \
3945 nl_msg_end_nested(mask, mask_offset); \
3950 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3951 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3953 memset(&skey, 0, sizeof skey); \
3954 memset(&smask, 0xff, sizeof smask); \
3955 s += strlen(NAME); \
3956 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3957 SCAN_PUT(ATTR, FUNC); \
3961 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3962 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3964 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3965 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3967 #define SCAN_PUT(ATTR, FUNC) \
3968 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3970 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3972 #define SCAN_END(ATTR) \
3974 SCAN_PUT(ATTR, NULL); \
3978 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
3980 TYPE skey[CNT], smask[CNT]; \
3981 memset(&skey, 0, sizeof skey); \
3982 memset(&smask, 0, sizeof smask); \
3983 int idx = 0, cnt = CNT; \
3984 uint64_t fields = 0; \
3989 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3990 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
3991 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3992 if (fields & (1UL << field)) { \
3994 if (++idx == cnt) { \
3998 s += strlen(NAME); \
3999 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
4000 fields |= 1UL << field; \
4005 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
4006 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
4008 #define SCAN_PUT_ARRAY(ATTR, CNT) \
4009 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
4011 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
4014 #define SCAN_END_ARRAY(ATTR) \
4019 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4023 #define SCAN_END_SINGLE(ATTR) \
4024 SCAN_FINISH_SINGLE(); \
4025 SCAN_PUT(ATTR, NULL); \
4029 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4030 SCAN_BEGIN(NAME, TYPE) { \
4031 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4032 } SCAN_END_SINGLE(ATTR)
4034 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4035 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4036 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4037 } SCAN_END_SINGLE(ATTR)
4039 /* scan_port needs one extra argument. */
4040 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4041 SCAN_BEGIN(NAME, TYPE) { \
4042 len = scan_port(s, &skey, &smask, port_names); \
4047 } SCAN_END_SINGLE(ATTR)
4050 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4051 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4057 len
= odp_ufid_from_string(s
, &ufid
);
4062 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4063 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4064 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4065 OVS_KEY_ATTR_RECIRC_ID
);
4066 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4068 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4069 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4070 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4071 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4073 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4074 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4075 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4076 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4077 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4078 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4079 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4080 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4081 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4082 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4083 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4084 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4086 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4087 } SCAN_END_NESTED();
4089 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4091 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4092 SCAN_FIELD("src=", eth
, eth_src
);
4093 SCAN_FIELD("dst=", eth
, eth_dst
);
4094 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4096 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4097 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4098 SCAN_FIELD("vid=", vid
, tci
);
4099 SCAN_FIELD("pcp=", pcp
, tci
);
4100 SCAN_FIELD("cfi=", cfi
, tci
);
4101 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4103 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4105 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4106 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4107 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4108 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4109 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4110 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4112 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4113 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4114 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4115 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4116 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4117 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4118 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4119 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4121 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4122 SCAN_FIELD("src=", ipv6
, ipv6_src
);
4123 SCAN_FIELD("dst=", ipv6
, ipv6_dst
);
4124 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4125 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4126 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4127 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4128 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4129 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4131 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4132 SCAN_FIELD("src=", be16
, tcp_src
);
4133 SCAN_FIELD("dst=", be16
, tcp_dst
);
4134 } SCAN_END(OVS_KEY_ATTR_TCP
);
4136 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4138 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4139 SCAN_FIELD("src=", be16
, udp_src
);
4140 SCAN_FIELD("dst=", be16
, udp_dst
);
4141 } SCAN_END(OVS_KEY_ATTR_UDP
);
4143 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4144 SCAN_FIELD("src=", be16
, sctp_src
);
4145 SCAN_FIELD("dst=", be16
, sctp_dst
);
4146 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4148 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4149 SCAN_FIELD("type=", u8
, icmp_type
);
4150 SCAN_FIELD("code=", u8
, icmp_code
);
4151 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4153 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4154 SCAN_FIELD("type=", u8
, icmpv6_type
);
4155 SCAN_FIELD("code=", u8
, icmpv6_code
);
4156 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4158 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4159 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4160 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4161 SCAN_FIELD("op=", be16
, arp_op
);
4162 SCAN_FIELD("sha=", eth
, arp_sha
);
4163 SCAN_FIELD("tha=", eth
, arp_tha
);
4164 } SCAN_END(OVS_KEY_ATTR_ARP
);
4166 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4167 SCAN_FIELD("target=", ipv6
, nd_target
);
4168 SCAN_FIELD("sll=", eth
, nd_sll
);
4169 SCAN_FIELD("tll=", eth
, nd_tll
);
4170 } SCAN_END(OVS_KEY_ATTR_ND
);
4172 /* Encap open-coded. */
4173 if (!strncmp(s
, "encap(", 6)) {
4174 const char *start
= s
;
4175 size_t encap
, encap_mask
= 0;
4177 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4179 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4186 s
+= strspn(s
, delimiters
);
4189 } else if (*s
== ')') {
4193 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4201 nl_msg_end_nested(key
, encap
);
4203 nl_msg_end_nested(mask
, encap_mask
);
4212 /* Parses the string representation of a datapath flow key, in the
4213 * format output by odp_flow_key_format(). Returns 0 if successful,
4214 * otherwise a positive errno value. On success, the flow key is
4215 * appended to 'key' as a series of Netlink attributes. On failure, no
4216 * data is appended to 'key'. Either way, 'key''s data might be
4219 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4220 * to a port number. (Port names may be used instead of port numbers in
4223 * On success, the attributes appended to 'key' are individually syntactically
4224 * valid, but they may not be valid as a sequence. 'key' might, for example,
4225 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4227 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4228 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4230 const size_t old_size
= key
->size
;
4234 s
+= strspn(s
, delimiters
);
4239 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4241 key
->size
= old_size
;
4251 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4254 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4255 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4256 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4257 * must use a zero mask for the netlink frag field, and all ones mask
4259 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4261 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4262 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4263 : OVS_FRAG_TYPE_FIRST
;
4266 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4267 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4268 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4270 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4272 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4274 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4276 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4277 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4278 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4279 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4281 /* These share the same layout. */
4283 struct ovs_key_tcp tcp
;
4284 struct ovs_key_udp udp
;
4285 struct ovs_key_sctp sctp
;
4288 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4289 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4292 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4293 bool export_mask
, struct ofpbuf
*buf
)
4295 struct ovs_key_ethernet
*eth_key
;
4297 const struct flow
*flow
= parms
->flow
;
4298 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4300 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4302 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4303 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4307 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4309 if (parms
->support
.ct_state
) {
4310 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4311 ovs_to_odp_ct_state(data
->ct_state
));
4313 if (parms
->support
.ct_zone
) {
4314 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4316 if (parms
->support
.ct_mark
) {
4317 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4319 if (parms
->support
.ct_label
) {
4320 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4321 sizeof(data
->ct_label
));
4323 if (parms
->support
.recirc
) {
4324 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4325 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4328 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4329 * is not the magical value "ODPP_NONE". */
4330 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4331 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4334 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4336 get_ethernet_key(data
, eth_key
);
4338 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4340 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4342 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4344 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4345 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4346 if (flow
->vlan_tci
== htons(0)) {
4353 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4354 /* For backwards compatibility with kernels that don't support
4355 * wildcarding, the following convention is used to encode the
4356 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4359 * -------- -------- -------
4360 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4361 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4362 * <none> 0xffff Any non-Ethernet II frame (except valid
4363 * 802.3 SNAP packet with valid eth_type).
4366 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4371 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4373 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4374 struct ovs_key_ipv4
*ipv4_key
;
4376 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4378 get_ipv4_key(data
, ipv4_key
, export_mask
);
4379 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4380 struct ovs_key_ipv6
*ipv6_key
;
4382 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4384 get_ipv6_key(data
, ipv6_key
, export_mask
);
4385 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4386 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4387 struct ovs_key_arp
*arp_key
;
4389 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4391 get_arp_key(data
, arp_key
);
4392 } else if (eth_type_mpls(flow
->dl_type
)) {
4393 struct ovs_key_mpls
*mpls_key
;
4396 n
= flow_count_mpls_labels(flow
, NULL
);
4398 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4400 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4401 n
* sizeof *mpls_key
);
4402 for (i
= 0; i
< n
; i
++) {
4403 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4407 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4408 if (flow
->nw_proto
== IPPROTO_TCP
) {
4409 union ovs_key_tp
*tcp_key
;
4411 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4413 get_tp_key(data
, tcp_key
);
4414 if (data
->tcp_flags
) {
4415 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4417 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4418 union ovs_key_tp
*udp_key
;
4420 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4422 get_tp_key(data
, udp_key
);
4423 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4424 union ovs_key_tp
*sctp_key
;
4426 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4428 get_tp_key(data
, sctp_key
);
4429 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4430 && flow
->nw_proto
== IPPROTO_ICMP
) {
4431 struct ovs_key_icmp
*icmp_key
;
4433 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4435 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4436 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4437 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4438 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4439 struct ovs_key_icmpv6
*icmpv6_key
;
4441 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4442 sizeof *icmpv6_key
);
4443 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4444 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4446 if (is_nd(flow
, NULL
)
4447 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4448 * type and code are 8 bits wide. Therefore, an exact match
4449 * looks like htons(0xff), not htons(0xffff). See
4450 * xlate_wc_finish() for details. */
4451 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4452 && data
->tp_dst
== htons(0xff)))) {
4454 struct ovs_key_nd
*nd_key
;
4456 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4458 memcpy(nd_key
->nd_target
, &data
->nd_target
,
4459 sizeof nd_key
->nd_target
);
4460 nd_key
->nd_sll
= data
->arp_sha
;
4461 nd_key
->nd_tll
= data
->arp_tha
;
4468 nl_msg_end_nested(buf
, encap
);
4472 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4474 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4475 * capable of being expanded to allow for that much space. */
4477 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4480 odp_flow_key_from_flow__(parms
, false, buf
);
4483 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4486 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4487 * capable of being expanded to allow for that much space. */
4489 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4492 odp_flow_key_from_flow__(parms
, true, buf
);
4495 /* Generate ODP flow key from the given packet metadata */
4497 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4499 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4501 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4502 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4505 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4508 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4509 ovs_to_odp_ct_state(md
->ct_state
));
4511 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4514 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4516 if (!ovs_u128_is_zero(md
->ct_label
)) {
4517 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4518 sizeof(md
->ct_label
));
4522 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4523 * value "ODPP_NONE". */
4524 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4525 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4529 /* Generate packet metadata from the given ODP flow key. */
4531 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4532 struct pkt_metadata
*md
)
4534 const struct nlattr
*nla
;
4536 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4537 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4538 1u << OVS_KEY_ATTR_IN_PORT
;
4540 pkt_metadata_init(md
, ODPP_NONE
);
4542 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4543 uint16_t type
= nl_attr_type(nla
);
4544 size_t len
= nl_attr_get_size(nla
);
4545 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4546 OVS_KEY_ATTR_MAX
, type
);
4548 if (len
!= expected_len
&& expected_len
>= 0) {
4553 case OVS_KEY_ATTR_RECIRC_ID
:
4554 md
->recirc_id
= nl_attr_get_u32(nla
);
4555 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4557 case OVS_KEY_ATTR_DP_HASH
:
4558 md
->dp_hash
= nl_attr_get_u32(nla
);
4559 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4561 case OVS_KEY_ATTR_PRIORITY
:
4562 md
->skb_priority
= nl_attr_get_u32(nla
);
4563 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4565 case OVS_KEY_ATTR_SKB_MARK
:
4566 md
->pkt_mark
= nl_attr_get_u32(nla
);
4567 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4569 case OVS_KEY_ATTR_CT_STATE
:
4570 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4571 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4573 case OVS_KEY_ATTR_CT_ZONE
:
4574 md
->ct_zone
= nl_attr_get_u16(nla
);
4575 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4577 case OVS_KEY_ATTR_CT_MARK
:
4578 md
->ct_mark
= nl_attr_get_u32(nla
);
4579 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4581 case OVS_KEY_ATTR_CT_LABELS
: {
4582 const ovs_u128
*cl
= nl_attr_get(nla
);
4585 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4588 case OVS_KEY_ATTR_TUNNEL
: {
4589 enum odp_key_fitness res
;
4591 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
4592 if (res
== ODP_FIT_ERROR
) {
4593 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4594 } else if (res
== ODP_FIT_PERFECT
) {
4595 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4599 case OVS_KEY_ATTR_IN_PORT
:
4600 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4601 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4607 if (!wanted_attrs
) {
4608 return; /* Have everything. */
4614 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4616 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4617 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
4621 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4622 uint64_t attrs
, int out_of_range_attr
,
4623 const struct nlattr
*key
, size_t key_len
)
4628 if (VLOG_DROP_DBG(rl
)) {
4633 for (i
= 0; i
< 64; i
++) {
4634 if (attrs
& (UINT64_C(1) << i
)) {
4635 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4637 ds_put_format(&s
, " %s",
4638 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4641 if (out_of_range_attr
) {
4642 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4645 ds_put_cstr(&s
, ": ");
4646 odp_flow_key_format(key
, key_len
, &s
);
4648 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4653 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4655 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4658 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4661 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4662 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4663 return 0xff; /* Error. */
4666 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4667 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4668 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4672 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4673 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4674 int *out_of_range_attrp
)
4676 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4677 const struct nlattr
*nla
;
4678 uint64_t present_attrs
;
4681 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4683 *out_of_range_attrp
= 0;
4684 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4685 uint16_t type
= nl_attr_type(nla
);
4686 size_t len
= nl_attr_get_size(nla
);
4687 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4688 OVS_KEY_ATTR_MAX
, type
);
4690 if (len
!= expected_len
&& expected_len
>= 0) {
4691 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4693 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4694 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4700 if (type
> OVS_KEY_ATTR_MAX
) {
4701 *out_of_range_attrp
= type
;
4703 if (present_attrs
& (UINT64_C(1) << type
)) {
4704 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4706 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4707 ovs_key_attr_to_string(type
,
4708 namebuf
, sizeof namebuf
));
4712 present_attrs
|= UINT64_C(1) << type
;
4717 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4721 *present_attrsp
= present_attrs
;
4725 static enum odp_key_fitness
4726 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4727 uint64_t expected_attrs
,
4728 const struct nlattr
*key
, size_t key_len
)
4730 uint64_t missing_attrs
;
4731 uint64_t extra_attrs
;
4733 missing_attrs
= expected_attrs
& ~present_attrs
;
4734 if (missing_attrs
) {
4735 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4736 log_odp_key_attributes(&rl
, "expected but not present",
4737 missing_attrs
, 0, key
, key_len
);
4738 return ODP_FIT_TOO_LITTLE
;
4741 extra_attrs
= present_attrs
& ~expected_attrs
;
4742 if (extra_attrs
|| out_of_range_attr
) {
4743 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4744 log_odp_key_attributes(&rl
, "present but not expected",
4745 extra_attrs
, out_of_range_attr
, key
, key_len
);
4746 return ODP_FIT_TOO_MUCH
;
4749 return ODP_FIT_PERFECT
;
4753 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4754 uint64_t present_attrs
, uint64_t *expected_attrs
,
4755 struct flow
*flow
, const struct flow
*src_flow
)
4757 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4758 bool is_mask
= flow
!= src_flow
;
4760 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4761 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4762 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4763 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4764 ntohs(flow
->dl_type
));
4767 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4768 flow
->dl_type
!= htons(0xffff)) {
4771 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4774 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4775 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4776 /* See comments in odp_flow_key_from_flow__(). */
4777 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4784 static enum odp_key_fitness
4785 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4786 uint64_t present_attrs
, int out_of_range_attr
,
4787 uint64_t expected_attrs
, struct flow
*flow
,
4788 const struct nlattr
*key
, size_t key_len
,
4789 const struct flow
*src_flow
)
4791 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4792 bool is_mask
= src_flow
!= flow
;
4793 const void *check_start
= NULL
;
4794 size_t check_len
= 0;
4795 enum ovs_key_attr expected_bit
= 0xff;
4797 if (eth_type_mpls(src_flow
->dl_type
)) {
4798 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4799 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4801 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4802 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4803 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4804 int n
= size
/ sizeof(ovs_be32
);
4807 if (!size
|| size
% sizeof(ovs_be32
)) {
4808 return ODP_FIT_ERROR
;
4810 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4811 return ODP_FIT_ERROR
;
4814 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4815 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4817 if (n
> FLOW_MAX_MPLS_LABELS
) {
4818 return ODP_FIT_TOO_MUCH
;
4822 /* BOS may be set only in the innermost label. */
4823 for (i
= 0; i
< n
- 1; i
++) {
4824 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4825 return ODP_FIT_ERROR
;
4829 /* BOS must be set in the innermost label. */
4830 if (n
< FLOW_MAX_MPLS_LABELS
4831 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4832 return ODP_FIT_TOO_LITTLE
;
4838 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4840 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4842 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4843 const struct ovs_key_ipv4
*ipv4_key
;
4845 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4846 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4847 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4848 return ODP_FIT_ERROR
;
4851 check_start
= ipv4_key
;
4852 check_len
= sizeof *ipv4_key
;
4853 expected_bit
= OVS_KEY_ATTR_IPV4
;
4856 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4858 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4860 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4861 const struct ovs_key_ipv6
*ipv6_key
;
4863 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4864 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4865 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4866 return ODP_FIT_ERROR
;
4869 check_start
= ipv6_key
;
4870 check_len
= sizeof *ipv6_key
;
4871 expected_bit
= OVS_KEY_ATTR_IPV6
;
4874 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4875 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4877 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4879 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4880 const struct ovs_key_arp
*arp_key
;
4882 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4883 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4884 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4885 "key", ntohs(arp_key
->arp_op
));
4886 return ODP_FIT_ERROR
;
4888 put_arp_key(arp_key
, flow
);
4890 check_start
= arp_key
;
4891 check_len
= sizeof *arp_key
;
4892 expected_bit
= OVS_KEY_ATTR_ARP
;
4898 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4899 if (!is_all_zeros(check_start
, check_len
) &&
4900 flow
->dl_type
!= htons(0xffff)) {
4901 return ODP_FIT_ERROR
;
4903 expected_attrs
|= UINT64_C(1) << expected_bit
;
4907 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4908 if (src_flow
->nw_proto
== IPPROTO_TCP
4909 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4910 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4911 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4913 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4915 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4916 const union ovs_key_tp
*tcp_key
;
4918 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4919 put_tp_key(tcp_key
, flow
);
4920 expected_bit
= OVS_KEY_ATTR_TCP
;
4922 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4923 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4924 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4926 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4927 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4928 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4929 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4931 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4933 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4934 const union ovs_key_tp
*udp_key
;
4936 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4937 put_tp_key(udp_key
, flow
);
4938 expected_bit
= OVS_KEY_ATTR_UDP
;
4940 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4941 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4942 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4943 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4945 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4947 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4948 const union ovs_key_tp
*sctp_key
;
4950 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4951 put_tp_key(sctp_key
, flow
);
4952 expected_bit
= OVS_KEY_ATTR_SCTP
;
4954 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4955 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4956 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4958 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4960 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4961 const struct ovs_key_icmp
*icmp_key
;
4963 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4964 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4965 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4966 expected_bit
= OVS_KEY_ATTR_ICMP
;
4968 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
4969 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4970 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4972 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
4974 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
4975 const struct ovs_key_icmpv6
*icmpv6_key
;
4977 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
4978 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
4979 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
4980 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
4981 if (is_nd(src_flow
, NULL
)) {
4983 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4985 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
4986 const struct ovs_key_nd
*nd_key
;
4988 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
4989 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
4990 sizeof flow
->nd_target
);
4991 flow
->arp_sha
= nd_key
->nd_sll
;
4992 flow
->arp_tha
= nd_key
->nd_tll
;
4994 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
4995 * ICMP type and code are 8 bits wide. Therefore, an
4996 * exact match looks like htons(0xff), not
4997 * htons(0xffff). See xlate_wc_finish() for details.
4999 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
5000 (flow
->tp_src
!= htons(0xff) ||
5001 flow
->tp_dst
!= htons(0xff))) {
5002 return ODP_FIT_ERROR
;
5004 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5011 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
5012 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
5013 return ODP_FIT_ERROR
;
5015 expected_attrs
|= UINT64_C(1) << expected_bit
;
5020 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5024 /* Parse 802.1Q header then encapsulated L3 attributes. */
5025 static enum odp_key_fitness
5026 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5027 uint64_t present_attrs
, int out_of_range_attr
,
5028 uint64_t expected_attrs
, struct flow
*flow
,
5029 const struct nlattr
*key
, size_t key_len
,
5030 const struct flow
*src_flow
)
5032 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5033 bool is_mask
= src_flow
!= flow
;
5035 const struct nlattr
*encap
5036 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5037 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5038 enum odp_key_fitness encap_fitness
;
5039 enum odp_key_fitness fitness
;
5041 /* Calculate fitness of outer attributes. */
5043 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5044 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5046 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5047 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5049 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5050 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5053 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5054 expected_attrs
, key
, key_len
);
5057 * Remove the TPID from dl_type since it's not the real Ethertype. */
5058 flow
->dl_type
= htons(0);
5059 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5060 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5063 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
5064 return ODP_FIT_TOO_LITTLE
;
5065 } else if (flow
->vlan_tci
== htons(0)) {
5066 /* Corner case for a truncated 802.1Q header. */
5067 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5068 return ODP_FIT_TOO_MUCH
;
5071 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
5072 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5073 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
5074 return ODP_FIT_ERROR
;
5077 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5082 /* Now parse the encapsulated attributes. */
5083 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5084 attrs
, &present_attrs
, &out_of_range_attr
)) {
5085 return ODP_FIT_ERROR
;
5089 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
5090 return ODP_FIT_ERROR
;
5092 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5093 expected_attrs
, flow
, key
, key_len
,
5096 /* The overall fitness is the worse of the outer and inner attributes. */
5097 return MAX(fitness
, encap_fitness
);
5100 static enum odp_key_fitness
5101 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5102 struct flow
*flow
, const struct flow
*src_flow
)
5104 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5105 uint64_t expected_attrs
;
5106 uint64_t present_attrs
;
5107 int out_of_range_attr
;
5108 bool is_mask
= src_flow
!= flow
;
5110 memset(flow
, 0, sizeof *flow
);
5112 /* Parse attributes. */
5113 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5114 &out_of_range_attr
)) {
5115 return ODP_FIT_ERROR
;
5120 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5121 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5122 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5123 } else if (is_mask
) {
5124 /* Always exact match recirc_id if it is not specified. */
5125 flow
->recirc_id
= UINT32_MAX
;
5128 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5129 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5130 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5132 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5133 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5134 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5137 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5138 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5139 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5142 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5143 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5145 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5146 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5148 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5149 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5150 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5152 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5153 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5154 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5156 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5157 const ovs_u128
*cl
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5159 flow
->ct_label
= *cl
;
5160 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5163 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5164 enum odp_key_fitness res
;
5166 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
5168 if (res
== ODP_FIT_ERROR
) {
5169 return ODP_FIT_ERROR
;
5170 } else if (res
== ODP_FIT_PERFECT
) {
5171 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5175 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5176 flow
->in_port
.odp_port
5177 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5178 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5179 } else if (!is_mask
) {
5180 flow
->in_port
.odp_port
= ODPP_NONE
;
5183 /* Ethernet header. */
5184 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5185 const struct ovs_key_ethernet
*eth_key
;
5187 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5188 put_ethernet_key(eth_key
, flow
);
5190 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5194 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5197 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5198 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5200 return ODP_FIT_ERROR
;
5204 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5205 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5206 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5207 expected_attrs
, flow
, key
, key_len
, src_flow
);
5210 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5211 flow
->vlan_tci
= htons(0xffff);
5212 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5213 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5214 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5217 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5218 expected_attrs
, flow
, key
, key_len
, src_flow
);
5221 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5222 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5223 * 'key' fits our expectations for what a flow key should contain.
5225 * The 'in_port' will be the datapath's understanding of the port. The
5226 * caller will need to translate with odp_port_to_ofp_port() if the
5227 * OpenFlow port is needed.
5229 * This function doesn't take the packet itself as an argument because none of
5230 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5231 * it is always possible to infer which additional attribute(s) should appear
5232 * by looking at the attributes for lower-level protocols, e.g. if the network
5233 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5234 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5235 * must be absent. */
5236 enum odp_key_fitness
5237 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5240 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
5243 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5244 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5245 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5246 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5247 * well 'key' fits our expectations for what a flow key should contain. */
5248 enum odp_key_fitness
5249 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5250 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
5253 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5254 &mask
->masks
, src_flow
);
5257 /* A missing mask means that the flow should be exact matched.
5258 * Generate an appropriate exact wildcard for the flow. */
5259 flow_wildcards_init_for_packet(mask
, src_flow
);
5261 return ODP_FIT_PERFECT
;
5265 /* Returns 'fitness' as a string, for use in debug messages. */
5267 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5270 case ODP_FIT_PERFECT
:
5272 case ODP_FIT_TOO_MUCH
:
5274 case ODP_FIT_TOO_LITTLE
:
5275 return "too_little";
5283 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5284 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5285 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5286 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5287 * null, then the return value is not meaningful.) */
5289 odp_put_userspace_action(uint32_t pid
,
5290 const void *userdata
, size_t userdata_size
,
5291 odp_port_t tunnel_out_port
,
5292 bool include_actions
,
5293 struct ofpbuf
*odp_actions
)
5295 size_t userdata_ofs
;
5298 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5299 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5301 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5303 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5304 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5307 * - The kernel rejected shorter userdata with -ERANGE.
5309 * - The kernel silently dropped userdata beyond the first 8 bytes.
5311 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5312 * separately disable features that required more than 8 bytes.) */
5313 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5314 MAX(8, userdata_size
)),
5315 userdata
, userdata_size
);
5319 if (tunnel_out_port
!= ODPP_NONE
) {
5320 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5323 if (include_actions
) {
5324 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5326 nl_msg_end_nested(odp_actions
, offset
);
5328 return userdata_ofs
;
5332 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5333 struct ofpbuf
*odp_actions
)
5335 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5336 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5337 nl_msg_end_nested(odp_actions
, offset
);
5341 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5342 struct ovs_action_push_tnl
*data
)
5344 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5346 size
+= data
->header_len
;
5347 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5351 /* The commit_odp_actions() function and its helpers. */
5354 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5355 const void *key
, size_t key_size
)
5357 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5358 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5359 nl_msg_end_nested(odp_actions
, offset
);
5362 /* Masked set actions have a mask following the data within the netlink
5363 * attribute. The unmasked bits in the data will be cleared as the data
5364 * is copied to the action. */
5366 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5367 enum ovs_key_attr key_type
,
5368 const void *key_
, const void *mask_
, size_t key_size
)
5370 size_t offset
= nl_msg_start_nested(odp_actions
,
5371 OVS_ACTION_ATTR_SET_MASKED
);
5372 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5373 const char *key
= key_
, *mask
= mask_
;
5375 memcpy(data
+ key_size
, mask
, key_size
);
5376 /* Clear unmasked bits while copying. */
5377 while (key_size
--) {
5378 *data
++ = *key
++ & *mask
++;
5380 nl_msg_end_nested(odp_actions
, offset
);
5383 /* If any of the flow key data that ODP actions can modify are different in
5384 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5385 * 'odp_actions' that change the flow tunneling information in key from
5386 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5387 * same way. In other words, operates the same as commit_odp_actions(), but
5388 * only on tunneling information. */
5390 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5391 struct ofpbuf
*odp_actions
)
5393 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5394 * must have non-zero ipv6_dst. */
5395 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5396 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5399 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5400 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5405 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5406 const void *key
, void *base
, void *mask
, size_t size
,
5407 struct ofpbuf
*odp_actions
)
5409 if (memcmp(key
, base
, size
)) {
5410 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5412 if (use_masked_set
&& !fully_masked
) {
5413 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5415 if (!fully_masked
) {
5416 memset(mask
, 0xff, size
);
5418 commit_set_action(odp_actions
, attr
, key
, size
);
5420 memcpy(base
, key
, size
);
5423 /* Mask bits are set when we have either read or set the corresponding
5424 * values. Masked bits will be exact-matched, no need to set them
5425 * if the value did not actually change. */
5431 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5433 eth
->eth_src
= flow
->dl_src
;
5434 eth
->eth_dst
= flow
->dl_dst
;
5438 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5440 flow
->dl_src
= eth
->eth_src
;
5441 flow
->dl_dst
= eth
->eth_dst
;
5445 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5446 struct ofpbuf
*odp_actions
,
5447 struct flow_wildcards
*wc
,
5450 struct ovs_key_ethernet key
, base
, mask
;
5452 get_ethernet_key(flow
, &key
);
5453 get_ethernet_key(base_flow
, &base
);
5454 get_ethernet_key(&wc
->masks
, &mask
);
5456 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5457 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5458 put_ethernet_key(&base
, base_flow
);
5459 put_ethernet_key(&mask
, &wc
->masks
);
5464 pop_vlan(struct flow
*base
,
5465 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5467 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5469 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5470 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5476 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5477 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5479 if (base
->vlan_tci
== vlan_tci
) {
5483 pop_vlan(base
, odp_actions
, wc
);
5484 if (vlan_tci
& htons(VLAN_CFI
)) {
5485 struct ovs_action_push_vlan vlan
;
5487 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5488 vlan
.vlan_tci
= vlan_tci
;
5489 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5490 &vlan
, sizeof vlan
);
5492 base
->vlan_tci
= vlan_tci
;
5495 /* Wildcarding already done at action translation time. */
5497 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5498 struct ofpbuf
*odp_actions
)
5500 int base_n
= flow_count_mpls_labels(base
, NULL
);
5501 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5502 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5505 while (base_n
> common_n
) {
5506 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5507 /* If there is only one more LSE in base than there are common
5508 * between base and flow; and flow has at least one more LSE than
5509 * is common then the topmost LSE of base may be updated using
5511 struct ovs_key_mpls mpls_key
;
5513 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5514 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5515 &mpls_key
, sizeof mpls_key
);
5516 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5519 /* Otherwise, if there more LSEs in base than are common between
5520 * base and flow then pop the topmost one. */
5524 /* If all the LSEs are to be popped and this is not the outermost
5525 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5526 * POP_MPLS action instead of flow->dl_type.
5528 * This is because the POP_MPLS action requires its ethertype
5529 * argument to be an MPLS ethernet type but in this case
5530 * flow->dl_type will be a non-MPLS ethernet type.
5532 * When the final POP_MPLS action occurs it use flow->dl_type and
5533 * the and the resulting packet will have the desired dl_type. */
5534 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5535 dl_type
= htons(ETH_TYPE_MPLS
);
5537 dl_type
= flow
->dl_type
;
5539 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5540 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5546 /* If, after the above popping and setting, there are more LSEs in flow
5547 * than base then some LSEs need to be pushed. */
5548 while (base_n
< flow_n
) {
5549 struct ovs_action_push_mpls
*mpls
;
5551 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5552 OVS_ACTION_ATTR_PUSH_MPLS
,
5554 mpls
->mpls_ethertype
= flow
->dl_type
;
5555 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5556 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
5557 * headers if the flow is restored later due to returning from a patch
5558 * port or group bucket. */
5559 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
5560 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5566 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5568 ipv4
->ipv4_src
= flow
->nw_src
;
5569 ipv4
->ipv4_dst
= flow
->nw_dst
;
5570 ipv4
->ipv4_proto
= flow
->nw_proto
;
5571 ipv4
->ipv4_tos
= flow
->nw_tos
;
5572 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5573 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5577 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5579 flow
->nw_src
= ipv4
->ipv4_src
;
5580 flow
->nw_dst
= ipv4
->ipv4_dst
;
5581 flow
->nw_proto
= ipv4
->ipv4_proto
;
5582 flow
->nw_tos
= ipv4
->ipv4_tos
;
5583 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5584 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5588 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5589 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5592 struct ovs_key_ipv4 key
, mask
, base
;
5594 /* Check that nw_proto and nw_frag remain unchanged. */
5595 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5596 flow
->nw_frag
== base_flow
->nw_frag
);
5598 get_ipv4_key(flow
, &key
, false);
5599 get_ipv4_key(base_flow
, &base
, false);
5600 get_ipv4_key(&wc
->masks
, &mask
, true);
5601 mask
.ipv4_proto
= 0; /* Not writeable. */
5602 mask
.ipv4_frag
= 0; /* Not writable. */
5604 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5606 put_ipv4_key(&base
, base_flow
, false);
5607 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5608 put_ipv4_key(&mask
, &wc
->masks
, true);
5614 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5616 memcpy(ipv6
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6
->ipv6_src
);
5617 memcpy(ipv6
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6
->ipv6_dst
);
5618 ipv6
->ipv6_label
= flow
->ipv6_label
;
5619 ipv6
->ipv6_proto
= flow
->nw_proto
;
5620 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5621 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5622 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5626 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5628 memcpy(&flow
->ipv6_src
, ipv6
->ipv6_src
, sizeof flow
->ipv6_src
);
5629 memcpy(&flow
->ipv6_dst
, ipv6
->ipv6_dst
, sizeof flow
->ipv6_dst
);
5630 flow
->ipv6_label
= ipv6
->ipv6_label
;
5631 flow
->nw_proto
= ipv6
->ipv6_proto
;
5632 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5633 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5634 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5638 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5639 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5642 struct ovs_key_ipv6 key
, mask
, base
;
5644 /* Check that nw_proto and nw_frag remain unchanged. */
5645 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5646 flow
->nw_frag
== base_flow
->nw_frag
);
5648 get_ipv6_key(flow
, &key
, false);
5649 get_ipv6_key(base_flow
, &base
, false);
5650 get_ipv6_key(&wc
->masks
, &mask
, true);
5651 mask
.ipv6_proto
= 0; /* Not writeable. */
5652 mask
.ipv6_frag
= 0; /* Not writable. */
5654 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5656 put_ipv6_key(&base
, base_flow
, false);
5657 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5658 put_ipv6_key(&mask
, &wc
->masks
, true);
5664 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5666 /* ARP key has padding, clear it. */
5667 memset(arp
, 0, sizeof *arp
);
5669 arp
->arp_sip
= flow
->nw_src
;
5670 arp
->arp_tip
= flow
->nw_dst
;
5671 arp
->arp_op
= htons(flow
->nw_proto
);
5672 arp
->arp_sha
= flow
->arp_sha
;
5673 arp
->arp_tha
= flow
->arp_tha
;
5677 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5679 flow
->nw_src
= arp
->arp_sip
;
5680 flow
->nw_dst
= arp
->arp_tip
;
5681 flow
->nw_proto
= ntohs(arp
->arp_op
);
5682 flow
->arp_sha
= arp
->arp_sha
;
5683 flow
->arp_tha
= arp
->arp_tha
;
5686 static enum slow_path_reason
5687 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5688 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5690 struct ovs_key_arp key
, mask
, base
;
5692 get_arp_key(flow
, &key
);
5693 get_arp_key(base_flow
, &base
);
5694 get_arp_key(&wc
->masks
, &mask
);
5696 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5698 put_arp_key(&base
, base_flow
);
5699 put_arp_key(&mask
, &wc
->masks
);
5706 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5708 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5709 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5710 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5714 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5716 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5717 flow
->tp_src
= htons(icmp
->icmp_type
);
5718 flow
->tp_dst
= htons(icmp
->icmp_code
);
5721 static enum slow_path_reason
5722 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5723 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5725 struct ovs_key_icmp key
, mask
, base
;
5726 enum ovs_key_attr attr
;
5728 if (is_icmpv4(flow
, NULL
)) {
5729 attr
= OVS_KEY_ATTR_ICMP
;
5730 } else if (is_icmpv6(flow
, NULL
)) {
5731 attr
= OVS_KEY_ATTR_ICMPV6
;
5736 get_icmp_key(flow
, &key
);
5737 get_icmp_key(base_flow
, &base
);
5738 get_icmp_key(&wc
->masks
, &mask
);
5740 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5741 put_icmp_key(&base
, base_flow
);
5742 put_icmp_key(&mask
, &wc
->masks
);
5749 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5751 memcpy(nd
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
5752 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5753 nd
->nd_sll
= flow
->arp_sha
;
5754 nd
->nd_tll
= flow
->arp_tha
;
5758 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5760 memcpy(&flow
->nd_target
, nd
->nd_target
, sizeof flow
->nd_target
);
5761 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5762 flow
->arp_sha
= nd
->nd_sll
;
5763 flow
->arp_tha
= nd
->nd_tll
;
5766 static enum slow_path_reason
5767 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5768 struct ofpbuf
*odp_actions
,
5769 struct flow_wildcards
*wc
, bool use_masked
)
5771 struct ovs_key_nd key
, mask
, base
;
5773 get_nd_key(flow
, &key
);
5774 get_nd_key(base_flow
, &base
);
5775 get_nd_key(&wc
->masks
, &mask
);
5777 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5779 put_nd_key(&base
, base_flow
);
5780 put_nd_key(&mask
, &wc
->masks
);
5787 static enum slow_path_reason
5788 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5789 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5792 /* Check if 'flow' really has an L3 header. */
5793 if (!flow
->nw_proto
) {
5797 switch (ntohs(base
->dl_type
)) {
5799 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5803 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5804 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5807 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5813 /* TCP, UDP, and SCTP keys have the same layout. */
5814 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5815 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5818 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5820 tp
->tcp
.tcp_src
= flow
->tp_src
;
5821 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5825 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5827 flow
->tp_src
= tp
->tcp
.tcp_src
;
5828 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5832 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5833 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5836 enum ovs_key_attr key_type
;
5837 union ovs_key_tp key
, mask
, base
;
5839 /* Check if 'flow' really has an L3 header. */
5840 if (!flow
->nw_proto
) {
5844 if (!is_ip_any(base_flow
)) {
5848 if (flow
->nw_proto
== IPPROTO_TCP
) {
5849 key_type
= OVS_KEY_ATTR_TCP
;
5850 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5851 key_type
= OVS_KEY_ATTR_UDP
;
5852 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5853 key_type
= OVS_KEY_ATTR_SCTP
;
5858 get_tp_key(flow
, &key
);
5859 get_tp_key(base_flow
, &base
);
5860 get_tp_key(&wc
->masks
, &mask
);
5862 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5864 put_tp_key(&base
, base_flow
);
5865 put_tp_key(&mask
, &wc
->masks
);
5870 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5871 struct ofpbuf
*odp_actions
,
5872 struct flow_wildcards
*wc
,
5875 uint32_t key
, mask
, base
;
5877 key
= flow
->skb_priority
;
5878 base
= base_flow
->skb_priority
;
5879 mask
= wc
->masks
.skb_priority
;
5881 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5882 sizeof key
, odp_actions
)) {
5883 base_flow
->skb_priority
= base
;
5884 wc
->masks
.skb_priority
= mask
;
5889 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5890 struct ofpbuf
*odp_actions
,
5891 struct flow_wildcards
*wc
,
5894 uint32_t key
, mask
, base
;
5896 key
= flow
->pkt_mark
;
5897 base
= base_flow
->pkt_mark
;
5898 mask
= wc
->masks
.pkt_mark
;
5900 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5901 sizeof key
, odp_actions
)) {
5902 base_flow
->pkt_mark
= base
;
5903 wc
->masks
.pkt_mark
= mask
;
5907 /* If any of the flow key data that ODP actions can modify are different in
5908 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5909 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5910 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5911 * in addition to this function if needed. Sets fields in 'wc' that are
5912 * used as part of the action.
5914 * Returns a reason to force processing the flow's packets into the userspace
5915 * slow path, if there is one, otherwise 0. */
5916 enum slow_path_reason
5917 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5918 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5921 enum slow_path_reason slow1
, slow2
;
5922 bool mpls_done
= false;
5924 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5925 /* Make packet a non-MPLS packet before committing L3/4 actions,
5926 * which would otherwise do nothing. */
5927 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
5928 commit_mpls_action(flow
, base
, odp_actions
);
5931 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5932 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5933 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5935 commit_mpls_action(flow
, base
, odp_actions
);
5937 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5938 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5939 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5941 return slow1
? slow1
: slow2
;