2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 "dynamic-string.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_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
111 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
112 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
113 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
114 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
115 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
116 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
117 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
118 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
119 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
120 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
121 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
122 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
124 case OVS_ACTION_ATTR_UNSPEC
:
125 case __OVS_ACTION_ATTR_MAX
:
126 return ATTR_LEN_INVALID
;
129 return ATTR_LEN_INVALID
;
132 /* Returns a string form of 'attr'. The return value is either a statically
133 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
134 * should be at least OVS_KEY_ATTR_BUFSIZE. */
135 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
137 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
140 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
141 case OVS_KEY_ATTR_ENCAP
: return "encap";
142 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
143 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
144 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
145 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
146 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
147 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
148 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
149 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
150 case OVS_KEY_ATTR_ETHERNET
: return "eth";
151 case OVS_KEY_ATTR_VLAN
: return "vlan";
152 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
153 case OVS_KEY_ATTR_IPV4
: return "ipv4";
154 case OVS_KEY_ATTR_IPV6
: return "ipv6";
155 case OVS_KEY_ATTR_TCP
: return "tcp";
156 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
157 case OVS_KEY_ATTR_UDP
: return "udp";
158 case OVS_KEY_ATTR_SCTP
: return "sctp";
159 case OVS_KEY_ATTR_ICMP
: return "icmp";
160 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
161 case OVS_KEY_ATTR_ARP
: return "arp";
162 case OVS_KEY_ATTR_ND
: return "nd";
163 case OVS_KEY_ATTR_MPLS
: return "mpls";
164 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
165 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
167 case __OVS_KEY_ATTR_MAX
:
169 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
175 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
177 size_t len
= nl_attr_get_size(a
);
179 ds_put_format(ds
, "action%"PRId16
, nl_attr_type(a
));
181 const uint8_t *unspec
;
184 unspec
= nl_attr_get(a
);
185 for (i
= 0; i
< len
; i
++) {
186 ds_put_char(ds
, i
? ' ': '(');
187 ds_put_format(ds
, "%02x", unspec
[i
]);
189 ds_put_char(ds
, ')');
194 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
)
196 static const struct nl_policy ovs_sample_policy
[] = {
197 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
198 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
200 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
202 const struct nlattr
*nla_acts
;
205 ds_put_cstr(ds
, "sample");
207 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
208 ds_put_cstr(ds
, "(error)");
212 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
215 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
217 ds_put_cstr(ds
, "actions(");
218 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
219 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
220 format_odp_actions(ds
, nla_acts
, len
);
221 ds_put_format(ds
, "))");
225 slow_path_reason_to_string(uint32_t reason
)
227 switch ((enum slow_path_reason
) reason
) {
228 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
237 slow_path_reason_to_explanation(enum slow_path_reason reason
)
240 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
249 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
250 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
252 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
253 res_flags
, allowed
, res_mask
);
257 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
)
259 static const struct nl_policy ovs_userspace_policy
[] = {
260 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
261 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
263 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
265 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
268 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
269 const struct nlattr
*userdata_attr
;
270 const struct nlattr
*tunnel_out_port_attr
;
272 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
273 ds_put_cstr(ds
, "userspace(error)");
277 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
278 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
280 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
283 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
284 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
285 bool userdata_unspec
= true;
286 union user_action_cookie cookie
;
288 if (userdata_len
>= sizeof cookie
.type
289 && userdata_len
<= sizeof cookie
) {
291 memset(&cookie
, 0, sizeof cookie
);
292 memcpy(&cookie
, userdata
, userdata_len
);
294 userdata_unspec
= false;
296 if (userdata_len
== sizeof cookie
.sflow
297 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
298 ds_put_format(ds
, ",sFlow("
299 "vid=%"PRIu16
",pcp=%"PRIu8
",output=%"PRIu32
")",
300 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
301 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
302 cookie
.sflow
.output
);
303 } else if (userdata_len
== sizeof cookie
.slow_path
304 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
305 ds_put_cstr(ds
, ",slow_path(");
306 format_flags(ds
, slow_path_reason_to_string
,
307 cookie
.slow_path
.reason
, ',');
308 ds_put_format(ds
, ")");
309 } else if (userdata_len
== sizeof cookie
.flow_sample
310 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
311 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
312 ",collector_set_id=%"PRIu32
313 ",obs_domain_id=%"PRIu32
314 ",obs_point_id=%"PRIu32
")",
315 cookie
.flow_sample
.probability
,
316 cookie
.flow_sample
.collector_set_id
,
317 cookie
.flow_sample
.obs_domain_id
,
318 cookie
.flow_sample
.obs_point_id
);
319 } else if (userdata_len
>= sizeof cookie
.ipfix
320 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
321 ds_put_format(ds
, ",ipfix(output_port=%"PRIu32
")",
322 cookie
.ipfix
.output_odp_port
);
324 userdata_unspec
= true;
328 if (userdata_unspec
) {
330 ds_put_format(ds
, ",userdata(");
331 for (i
= 0; i
< userdata_len
; i
++) {
332 ds_put_format(ds
, "%02x", userdata
[i
]);
334 ds_put_char(ds
, ')');
338 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
339 ds_put_cstr(ds
, ",actions");
342 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
343 if (tunnel_out_port_attr
) {
344 ds_put_format(ds
, ",tunnel_out_port=%"PRIu32
,
345 nl_attr_get_u32(tunnel_out_port_attr
));
348 ds_put_char(ds
, ')');
352 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
354 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
355 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
356 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
357 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
359 ds_put_char(ds
, ',');
361 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
362 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
363 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
364 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
366 ds_put_char(ds
, ',');
368 if (!(tci
& htons(VLAN_CFI
))) {
369 ds_put_cstr(ds
, "cfi=0");
370 ds_put_char(ds
, ',');
376 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
378 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
379 mpls_lse_to_label(mpls_lse
),
380 mpls_lse_to_tc(mpls_lse
),
381 mpls_lse_to_ttl(mpls_lse
),
382 mpls_lse_to_bos(mpls_lse
));
386 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
387 const struct ovs_key_mpls
*mpls_mask
, int n
)
390 ovs_be32 key
= mpls_key
->mpls_lse
;
392 if (mpls_mask
== NULL
) {
393 format_mpls_lse(ds
, key
);
395 ovs_be32 mask
= mpls_mask
->mpls_lse
;
397 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
398 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
399 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
400 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
401 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
406 for (i
= 0; i
< n
; i
++) {
407 ds_put_format(ds
, "lse%d=%#"PRIx32
,
408 i
, ntohl(mpls_key
[i
].mpls_lse
));
410 ds_put_format(ds
, "/%#"PRIx32
, ntohl(mpls_mask
[i
].mpls_lse
));
412 ds_put_char(ds
, ',');
419 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
421 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
425 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
427 ds_put_format(ds
, "hash(");
429 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
430 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
432 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
435 ds_put_format(ds
, ")");
439 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
441 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
442 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
443 ntohs(udp
->udp_csum
));
449 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
451 const struct eth_header
*eth
;
454 const struct udp_header
*udp
;
456 eth
= (const struct eth_header
*)data
->header
;
461 ds_put_format(ds
, "header(size=%"PRIu8
",type=%"PRIu8
",eth(dst=",
462 data
->header_len
, data
->tnl_type
);
463 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
464 ds_put_format(ds
, ",src=");
465 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
466 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
468 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
470 const struct ip_header
*ip
;
471 ip
= (const struct ip_header
*) l3
;
472 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
473 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
474 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
475 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
476 ip
->ip_proto
, ip
->ip_tos
,
481 const struct ip6_hdr
*ip6
;
482 ip6
= (const struct ip6_hdr
*) l3
;
483 ds_put_format(ds
, "ipv6(src=");
484 ipv6_format_addr(&ip6
->ip6_src
, ds
);
485 ds_put_format(ds
, ",dst=");
486 ipv6_format_addr(&ip6
->ip6_dst
, ds
);
487 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx8
488 ",hlimit=%"PRIu8
"),",
489 ntohl(ip6
->ip6_flow
) & IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
490 (ntohl(ip6
->ip6_flow
) >> 20) & 0xff, ip6
->ip6_hlim
);
494 udp
= (const struct udp_header
*) l4
;
496 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
497 const struct vxlanhdr
*vxh
;
499 vxh
= format_udp_tnl_push_header(ds
, udp
);
501 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
502 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
503 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
504 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
505 const struct genevehdr
*gnh
;
507 gnh
= format_udp_tnl_push_header(ds
, udp
);
509 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
510 gnh
->oam
? "oam," : "",
511 gnh
->critical
? "crit," : "",
512 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
515 ds_put_cstr(ds
, ",options(");
516 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
518 ds_put_char(ds
, ')');
521 ds_put_char(ds
, ')');
522 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
523 const struct gre_base_hdr
*greh
;
524 ovs_16aligned_be32
*options
;
526 greh
= (const struct gre_base_hdr
*) l4
;
528 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
529 ntohs(greh
->flags
), ntohs(greh
->protocol
));
530 options
= (ovs_16aligned_be32
*)(greh
+ 1);
531 if (greh
->flags
& htons(GRE_CSUM
)) {
532 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
535 if (greh
->flags
& htons(GRE_KEY
)) {
536 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
539 if (greh
->flags
& htons(GRE_SEQ
)) {
540 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
543 ds_put_format(ds
, ")");
545 ds_put_format(ds
, ")");
549 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
)
551 struct ovs_action_push_tnl
*data
;
553 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
555 ds_put_format(ds
, "tnl_push(tnl_port(%"PRIu32
"),", data
->tnl_port
);
556 format_odp_tnl_push_header(ds
, data
);
557 ds_put_format(ds
, ",out_port(%"PRIu32
"))", data
->out_port
);
560 static const struct nl_policy ovs_nat_policy
[] = {
561 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
562 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
563 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
564 .min_len
= sizeof(struct in_addr
),
565 .max_len
= sizeof(struct in6_addr
)},
566 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
567 .min_len
= sizeof(struct in_addr
),
568 .max_len
= sizeof(struct in6_addr
)},
569 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
570 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
571 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
572 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
573 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
577 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
579 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
581 ovs_be32 ip_min
, ip_max
;
582 struct in6_addr ip6_min
, ip6_max
;
583 uint16_t proto_min
, proto_max
;
585 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
586 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
589 /* If no type, then nothing else either. */
590 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
591 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
592 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
593 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
594 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
595 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
598 /* Both SNAT & DNAT may not be specified. */
599 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
600 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
603 /* proto may not appear without ip. */
604 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
605 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
608 /* MAX may not appear without MIN. */
609 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
610 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
611 ds_put_cstr(ds
, "nat(error: range max without min.)");
614 /* Address sizes must match. */
615 if ((a
[OVS_NAT_ATTR_IP_MIN
]
616 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
617 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
618 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
619 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
620 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
621 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
625 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
626 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
627 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
628 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
629 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
630 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
631 if (addr_len
== sizeof ip6_min
) {
632 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
633 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
635 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
636 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
639 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
640 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
641 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
642 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
644 if ((addr_len
== sizeof(ovs_be32
)
645 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
646 || (addr_len
== sizeof(struct in6_addr
)
647 && !ipv6_mask_is_any(&ip6_max
)
648 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
649 || (proto_max
&& proto_min
> proto_max
)) {
650 ds_put_cstr(ds
, "nat(range error)");
654 ds_put_cstr(ds
, "nat");
655 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
656 ds_put_char(ds
, '(');
657 if (a
[OVS_NAT_ATTR_SRC
]) {
658 ds_put_cstr(ds
, "src");
659 } else if (a
[OVS_NAT_ATTR_DST
]) {
660 ds_put_cstr(ds
, "dst");
664 ds_put_cstr(ds
, "=");
666 if (addr_len
== sizeof ip_min
) {
667 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
669 if (ip_max
&& ip_max
!= ip_min
) {
670 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
672 } else if (addr_len
== sizeof ip6_min
) {
673 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
675 if (!ipv6_mask_is_any(&ip6_max
) &&
676 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
677 ds_put_char(ds
, '-');
678 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
682 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
684 if (proto_max
&& proto_max
!= proto_min
) {
685 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
689 ds_put_char(ds
, ',');
690 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
691 ds_put_cstr(ds
, "persistent,");
693 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
694 ds_put_cstr(ds
, "hash,");
696 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
697 ds_put_cstr(ds
, "random,");
700 ds_put_char(ds
, ')');
704 static const struct nl_policy ovs_conntrack_policy
[] = {
705 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
706 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
707 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
708 .min_len
= sizeof(uint32_t) * 2 },
709 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
710 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
711 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
712 .min_len
= 1, .max_len
= 16 },
713 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
717 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
719 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
720 const ovs_u128
*label
;
721 const uint32_t *mark
;
725 const struct nlattr
*nat
;
727 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
728 ds_put_cstr(ds
, "ct(error)");
732 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
733 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
734 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
735 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
736 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
737 nat
= a
[OVS_CT_ATTR_NAT
];
739 ds_put_format(ds
, "ct");
740 if (commit
|| zone
|| mark
|| label
|| helper
|| nat
) {
741 ds_put_cstr(ds
, "(");
743 ds_put_format(ds
, "commit,");
746 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
749 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
753 ds_put_format(ds
, "label=");
754 format_u128(ds
, label
, label
+ 1, true);
755 ds_put_char(ds
, ',');
758 ds_put_format(ds
, "helper=%s,", helper
);
761 format_odp_ct_nat(ds
, nat
);
764 ds_put_cstr(ds
, ")");
769 format_odp_action(struct ds
*ds
, const struct nlattr
*a
)
772 enum ovs_action_attr type
= nl_attr_type(a
);
775 expected_len
= odp_action_len(nl_attr_type(a
));
776 if (expected_len
!= ATTR_LEN_VARIABLE
&&
777 nl_attr_get_size(a
) != expected_len
) {
778 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
779 nl_attr_get_size(a
), expected_len
);
780 format_generic_odp_action(ds
, a
);
785 case OVS_ACTION_ATTR_OUTPUT
:
786 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
788 case OVS_ACTION_ATTR_TUNNEL_POP
:
789 ds_put_format(ds
, "tnl_pop(%"PRIu32
")", nl_attr_get_u32(a
));
791 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
792 format_odp_tnl_push_action(ds
, a
);
794 case OVS_ACTION_ATTR_USERSPACE
:
795 format_odp_userspace_action(ds
, a
);
797 case OVS_ACTION_ATTR_RECIRC
:
798 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
800 case OVS_ACTION_ATTR_HASH
:
801 format_odp_hash_action(ds
, nl_attr_get(a
));
803 case OVS_ACTION_ATTR_SET_MASKED
:
805 size
= nl_attr_get_size(a
) / 2;
806 ds_put_cstr(ds
, "set(");
808 /* Masked set action not supported for tunnel key, which is bigger. */
809 if (size
<= sizeof(struct ovs_key_ipv6
)) {
810 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
811 sizeof(struct nlattr
))];
812 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
813 sizeof(struct nlattr
))];
815 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
816 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
817 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
818 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
819 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
821 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
823 ds_put_cstr(ds
, ")");
825 case OVS_ACTION_ATTR_SET
:
826 ds_put_cstr(ds
, "set(");
827 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
828 ds_put_cstr(ds
, ")");
830 case OVS_ACTION_ATTR_PUSH_VLAN
: {
831 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
832 ds_put_cstr(ds
, "push_vlan(");
833 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
834 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
836 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
837 ds_put_char(ds
, ')');
840 case OVS_ACTION_ATTR_POP_VLAN
:
841 ds_put_cstr(ds
, "pop_vlan");
843 case OVS_ACTION_ATTR_PUSH_MPLS
: {
844 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
845 ds_put_cstr(ds
, "push_mpls(");
846 format_mpls_lse(ds
, mpls
->mpls_lse
);
847 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
850 case OVS_ACTION_ATTR_POP_MPLS
: {
851 ovs_be16 ethertype
= nl_attr_get_be16(a
);
852 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
855 case OVS_ACTION_ATTR_SAMPLE
:
856 format_odp_sample_action(ds
, a
);
858 case OVS_ACTION_ATTR_CT
:
859 format_odp_conntrack_action(ds
, a
);
861 case OVS_ACTION_ATTR_UNSPEC
:
862 case __OVS_ACTION_ATTR_MAX
:
864 format_generic_odp_action(ds
, a
);
870 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
874 const struct nlattr
*a
;
877 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
879 ds_put_char(ds
, ',');
881 format_odp_action(ds
, a
);
886 if (left
== actions_len
) {
887 ds_put_cstr(ds
, "<empty>");
889 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
890 for (i
= 0; i
< left
; i
++) {
891 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
893 ds_put_char(ds
, ')');
896 ds_put_cstr(ds
, "drop");
900 /* Separate out parse_odp_userspace_action() function. */
902 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
905 union user_action_cookie cookie
;
907 odp_port_t tunnel_out_port
;
909 void *user_data
= NULL
;
910 size_t user_data_size
= 0;
911 bool include_actions
= false;
914 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
918 ofpbuf_init(&buf
, 16);
922 uint32_t probability
;
923 uint32_t collector_set_id
;
924 uint32_t obs_domain_id
;
925 uint32_t obs_point_id
;
928 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
929 "pcp=%i,output=%"SCNi32
")%n",
930 &vid
, &pcp
, &output
, &n1
)) {
934 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
939 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
940 cookie
.sflow
.vlan_tci
= htons(tci
);
941 cookie
.sflow
.output
= output
;
943 user_data_size
= sizeof cookie
.sflow
;
944 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
947 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
948 cookie
.slow_path
.unused
= 0;
949 cookie
.slow_path
.reason
= 0;
951 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
952 &cookie
.slow_path
.reason
,
953 SLOW_PATH_REASON_MASK
, NULL
);
954 if (res
< 0 || s
[n
+ res
] != ')') {
960 user_data_size
= sizeof cookie
.slow_path
;
961 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
962 "collector_set_id=%"SCNi32
","
963 "obs_domain_id=%"SCNi32
","
964 "obs_point_id=%"SCNi32
")%n",
965 &probability
, &collector_set_id
,
966 &obs_domain_id
, &obs_point_id
, &n1
)) {
969 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
970 cookie
.flow_sample
.probability
= probability
;
971 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
972 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
973 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
975 user_data_size
= sizeof cookie
.flow_sample
;
976 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
979 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
980 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
982 user_data_size
= sizeof cookie
.ipfix
;
983 } else if (ovs_scan(&s
[n
], ",userdata(%n",
988 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
993 user_data
= buf
.data
;
994 user_data_size
= buf
.size
;
1001 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1003 include_actions
= true;
1009 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1010 &tunnel_out_port
, &n1
)) {
1011 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1012 tunnel_out_port
, include_actions
, actions
);
1014 } else if (s
[n
] == ')') {
1015 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1016 ODPP_NONE
, include_actions
, actions
);
1023 ofpbuf_uninit(&buf
);
1028 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1030 struct eth_header
*eth
;
1031 struct ip_header
*ip
;
1032 struct ovs_16aligned_ip6_hdr
*ip6
;
1033 struct udp_header
*udp
;
1034 struct gre_base_hdr
*greh
;
1035 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1037 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1041 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1044 eth
= (struct eth_header
*) data
->header
;
1045 l3
= (data
->header
+ sizeof *eth
);
1046 ip
= (struct ip_header
*) l3
;
1047 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1048 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1049 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1052 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1056 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1057 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1060 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1063 eth
->eth_type
= htons(dl_type
);
1065 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1067 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1068 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1071 &ip
->ip_proto
, &ip
->ip_tos
,
1072 &ip
->ip_ttl
, &ip
->ip_frag_off
)) {
1075 put_16aligned_be32(&ip
->ip_src
, sip
);
1076 put_16aligned_be32(&ip
->ip_dst
, dip
);
1077 ip_len
= sizeof *ip
;
1079 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1080 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1081 struct in6_addr sip6
, dip6
;
1084 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1085 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1086 ",hlimit=%"SCNi8
"),",
1087 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1088 &tclass
, &ip6
->ip6_hlim
)
1089 || (label
& ~IPV6_LABEL_MASK
) != 0
1090 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1091 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1094 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1095 htonl(tclass
<< 20) | htonl(label
));
1096 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1097 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1098 ip_len
= sizeof *ip6
;
1102 l4
= ((uint8_t *) l3
+ ip_len
);
1103 udp
= (struct udp_header
*) l4
;
1104 greh
= (struct gre_base_hdr
*) l4
;
1105 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1106 &udp_src
, &udp_dst
, &csum
)) {
1107 uint32_t vx_flags
, vni
;
1109 udp
->udp_src
= htons(udp_src
);
1110 udp
->udp_dst
= htons(udp_dst
);
1112 udp
->udp_csum
= htons(csum
);
1114 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1116 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1118 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1119 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1120 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1121 header_len
= sizeof *eth
+ ip_len
+
1122 sizeof *udp
+ sizeof *vxh
;
1123 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1124 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1126 memset(gnh
, 0, sizeof *gnh
);
1127 header_len
= sizeof *eth
+ ip_len
+
1128 sizeof *udp
+ sizeof *gnh
;
1130 if (ovs_scan_len(s
, &n
, "oam,")) {
1133 if (ovs_scan_len(s
, &n
, "crit,")) {
1136 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1139 if (ovs_scan_len(s
, &n
, ",options(")) {
1140 struct geneve_scan options
;
1143 memset(&options
, 0, sizeof options
);
1144 len
= scan_geneve(s
+ n
, &options
, NULL
);
1149 memcpy(gnh
->options
, options
.d
, options
.len
);
1150 gnh
->opt_len
= options
.len
/ 4;
1151 header_len
+= options
.len
;
1155 if (!ovs_scan_len(s
, &n
, "))")) {
1159 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1160 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1161 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1165 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1166 &gre_flags
, &gre_proto
)){
1168 tnl_type
= OVS_VPORT_TYPE_GRE
;
1169 greh
->flags
= htons(gre_flags
);
1170 greh
->protocol
= htons(gre_proto
);
1171 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1173 if (greh
->flags
& htons(GRE_CSUM
)) {
1174 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1178 memset(options
, 0, sizeof *options
);
1179 *((ovs_be16
*)options
) = htons(csum
);
1182 if (greh
->flags
& htons(GRE_KEY
)) {
1185 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1189 put_16aligned_be32(options
, htonl(key
));
1192 if (greh
->flags
& htons(GRE_SEQ
)) {
1195 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1198 put_16aligned_be32(options
, htonl(seq
));
1202 if (!ovs_scan_len(s
, &n
, "))")) {
1206 header_len
= sizeof *eth
+ ip_len
+
1207 ((uint8_t *) options
- (uint8_t *) greh
);
1212 /* check tunnel meta data. */
1213 if (data
->tnl_type
!= tnl_type
) {
1216 if (data
->header_len
!= header_len
) {
1221 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1228 struct ct_nat_params
{
1234 struct in6_addr ip6
;
1238 struct in6_addr ip6
;
1248 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1250 if (ovs_scan_len(s
, n
, "=")) {
1251 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1252 struct in6_addr ipv6
;
1254 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1255 p
->addr_len
= sizeof p
->addr_min
.ip
;
1256 if (ovs_scan_len(s
, n
, "-")) {
1257 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1258 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1262 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1263 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1264 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1265 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1266 p
->addr_min
.ip6
= ipv6
;
1267 if (ovs_scan_len(s
, n
, "-")) {
1268 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1269 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1270 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1271 p
->addr_max
.ip6
= ipv6
;
1279 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1280 if (ovs_scan_len(s
, n
, "-")) {
1281 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1291 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1295 if (ovs_scan_len(s
, &n
, "nat")) {
1296 memset(p
, 0, sizeof *p
);
1298 if (ovs_scan_len(s
, &n
, "(")) {
1302 end
= strchr(s
+ n
, ')');
1309 n
+= strspn(s
+ n
, delimiters
);
1310 if (ovs_scan_len(s
, &n
, "src")) {
1311 int err
= scan_ct_nat_range(s
, &n
, p
);
1318 if (ovs_scan_len(s
, &n
, "dst")) {
1319 int err
= scan_ct_nat_range(s
, &n
, p
);
1326 if (ovs_scan_len(s
, &n
, "persistent")) {
1327 p
->persistent
= true;
1330 if (ovs_scan_len(s
, &n
, "hash")) {
1331 p
->proto_hash
= true;
1334 if (ovs_scan_len(s
, &n
, "random")) {
1335 p
->proto_random
= true;
1341 if (p
->snat
&& p
->dnat
) {
1344 if ((p
->addr_len
!= 0 &&
1345 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1346 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1347 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1350 if (p
->proto_hash
&& p
->proto_random
) {
1360 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1362 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1365 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1366 } else if (p
->dnat
) {
1367 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1371 if (p
->addr_len
!= 0) {
1372 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1374 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1375 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1379 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1380 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1381 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1384 if (p
->persistent
) {
1385 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1387 if (p
->proto_hash
) {
1388 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1390 if (p
->proto_random
) {
1391 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1395 nl_msg_end_nested(actions
, start
);
1399 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1403 if (ovs_scan(s
, "ct")) {
1404 const char *helper
= NULL
;
1405 size_t helper_len
= 0;
1406 bool commit
= false;
1411 } ct_mark
= { 0, 0 };
1416 struct ct_nat_params nat_params
;
1417 bool have_nat
= false;
1421 memset(&ct_label
, 0, sizeof(ct_label
));
1424 if (ovs_scan(s
, "(")) {
1427 end
= strchr(s
, ')');
1435 s
+= strspn(s
, delimiters
);
1436 if (ovs_scan(s
, "commit%n", &n
)) {
1441 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1445 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1448 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1451 ct_mark
.mask
= UINT32_MAX
;
1455 if (ovs_scan(s
, "label=%n", &n
)) {
1459 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1466 if (ovs_scan(s
, "helper=%n", &n
)) {
1468 helper_len
= strcspn(s
, delimiters_end
);
1469 if (!helper_len
|| helper_len
> 15) {
1477 n
= scan_ct_nat(s
, &nat_params
);
1482 /* end points to the end of the nested, nat action.
1483 * find the real end. */
1486 /* Nothing matched. */
1492 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1494 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1497 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1500 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1503 if (!ovs_u128_is_zero(&ct_label
.mask
)) {
1504 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1508 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1512 nl_msg_put_ct_nat(&nat_params
, actions
);
1514 nl_msg_end_nested(actions
, start
);
1521 parse_odp_action(const char *s
, const struct simap
*port_names
,
1522 struct ofpbuf
*actions
)
1528 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1529 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1535 int len
= strcspn(s
, delimiters
);
1536 struct simap_node
*node
;
1538 node
= simap_find_len(port_names
, s
, len
);
1540 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1549 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1550 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1555 if (!strncmp(s
, "userspace(", 10)) {
1556 return parse_odp_userspace_action(s
, actions
);
1559 if (!strncmp(s
, "set(", 4)) {
1562 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1563 struct ofpbuf maskbuf
;
1564 struct nlattr
*nested
, *key
;
1567 /* 'mask' is big enough to hold any key. */
1568 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1570 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1571 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1575 if (s
[retval
+ 4] != ')') {
1579 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1582 size
= nl_attr_get_size(mask
);
1583 if (size
== nl_attr_get_size(key
)) {
1584 /* Change to masked set action if not fully masked. */
1585 if (!is_all_ones(mask
+ 1, size
)) {
1586 key
->nla_len
+= size
;
1587 ofpbuf_put(actions
, mask
+ 1, size
);
1588 /* 'actions' may have been reallocated by ofpbuf_put(). */
1589 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1590 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1594 nl_msg_end_nested(actions
, start_ofs
);
1599 struct ovs_action_push_vlan push
;
1600 int tpid
= ETH_TYPE_VLAN
;
1605 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1606 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1607 &vid
, &pcp
, &cfi
, &n
)
1608 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1609 &tpid
, &vid
, &pcp
, &n
)
1610 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1611 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1612 push
.vlan_tpid
= htons(tpid
);
1613 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1614 | (pcp
<< VLAN_PCP_SHIFT
)
1615 | (cfi
? VLAN_CFI
: 0));
1616 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1617 &push
, sizeof push
);
1623 if (!strncmp(s
, "pop_vlan", 8)) {
1624 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1632 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1633 && percentage
>= 0. && percentage
<= 100.0) {
1634 size_t sample_ofs
, actions_ofs
;
1637 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1638 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1639 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1640 (probability
<= 0 ? 0
1641 : probability
>= UINT32_MAX
? UINT32_MAX
1644 actions_ofs
= nl_msg_start_nested(actions
,
1645 OVS_SAMPLE_ATTR_ACTIONS
);
1649 n
+= strspn(s
+ n
, delimiters
);
1654 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1660 nl_msg_end_nested(actions
, actions_ofs
);
1661 nl_msg_end_nested(actions
, sample_ofs
);
1663 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1671 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1672 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1680 retval
= parse_conntrack_action(s
, actions
);
1687 struct ovs_action_push_tnl data
;
1690 n
= ovs_parse_tnl_push(s
, &data
);
1692 odp_put_tnl_push_action(actions
, &data
);
1701 /* Parses the string representation of datapath actions, in the format output
1702 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1703 * value. On success, the ODP actions are appended to 'actions' as a series of
1704 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1705 * way, 'actions''s data might be reallocated. */
1707 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1708 struct ofpbuf
*actions
)
1712 if (!strcasecmp(s
, "drop")) {
1716 old_size
= actions
->size
;
1720 s
+= strspn(s
, delimiters
);
1725 retval
= parse_odp_action(s
, port_names
, actions
);
1726 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1727 actions
->size
= old_size
;
1736 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1737 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1740 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1741 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1742 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1743 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1744 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1745 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1746 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1747 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1748 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1749 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1750 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1751 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1752 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1753 .next
= ovs_vxlan_ext_attr_lens
,
1754 .next_max
= OVS_VXLAN_EXT_MAX
},
1755 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1756 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1759 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1760 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1761 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1762 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1763 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1764 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1765 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1766 .next
= ovs_tun_key_attr_lens
,
1767 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1768 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1769 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1770 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1771 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1772 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1773 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1774 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1775 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1776 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1777 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1778 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1779 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1780 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1781 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1782 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1783 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1784 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1785 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1786 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1789 /* Returns the correct length of the payload for a flow key attribute of the
1790 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1791 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1792 * payload is a nested type. */
1794 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1796 if (type
> max_len
) {
1797 return ATTR_LEN_INVALID
;
1800 return tbl
[type
].len
;
1804 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1806 size_t len
= nl_attr_get_size(a
);
1808 const uint8_t *unspec
;
1811 unspec
= nl_attr_get(a
);
1812 for (i
= 0; i
< len
; i
++) {
1814 ds_put_char(ds
, ' ');
1816 ds_put_format(ds
, "%02x", unspec
[i
]);
1822 ovs_frag_type_to_string(enum ovs_frag_type type
)
1825 case OVS_FRAG_TYPE_NONE
:
1827 case OVS_FRAG_TYPE_FIRST
:
1829 case OVS_FRAG_TYPE_LATER
:
1831 case __OVS_FRAG_TYPE_MAX
:
1837 static enum odp_key_fitness
1838 odp_tun_key_from_attr__(const struct nlattr
*attr
,
1839 const struct nlattr
*flow_attrs
, size_t flow_attr_len
,
1840 const struct flow_tnl
*src_tun
, struct flow_tnl
*tun
,
1844 const struct nlattr
*a
;
1846 bool unknown
= false;
1848 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1849 uint16_t type
= nl_attr_type(a
);
1850 size_t len
= nl_attr_get_size(a
);
1851 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1852 OVS_TUNNEL_ATTR_MAX
, type
);
1854 if (len
!= expected_len
&& expected_len
>= 0) {
1855 return ODP_FIT_ERROR
;
1859 case OVS_TUNNEL_KEY_ATTR_ID
:
1860 tun
->tun_id
= nl_attr_get_be64(a
);
1861 tun
->flags
|= FLOW_TNL_F_KEY
;
1863 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1864 tun
->ip_src
= nl_attr_get_be32(a
);
1866 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1867 tun
->ip_dst
= nl_attr_get_be32(a
);
1869 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
1870 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
1872 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
1873 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
1875 case OVS_TUNNEL_KEY_ATTR_TOS
:
1876 tun
->ip_tos
= nl_attr_get_u8(a
);
1878 case OVS_TUNNEL_KEY_ATTR_TTL
:
1879 tun
->ip_ttl
= nl_attr_get_u8(a
);
1882 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1883 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1885 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1886 tun
->flags
|= FLOW_TNL_F_CSUM
;
1888 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1889 tun
->tp_src
= nl_attr_get_be16(a
);
1891 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1892 tun
->tp_dst
= nl_attr_get_be16(a
);
1894 case OVS_TUNNEL_KEY_ATTR_OAM
:
1895 tun
->flags
|= FLOW_TNL_F_OAM
;
1897 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1898 static const struct nl_policy vxlan_opts_policy
[] = {
1899 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1901 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1903 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1904 return ODP_FIT_ERROR
;
1907 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1908 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1910 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1911 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
1916 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1917 if (tun_metadata_from_geneve_nlattr(a
, flow_attrs
, flow_attr_len
,
1918 src_tun
, udpif
, tun
)) {
1919 return ODP_FIT_ERROR
;
1924 /* Allow this to show up as unexpected, if there are unknown
1925 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1932 return ODP_FIT_ERROR
;
1935 return ODP_FIT_TOO_MUCH
;
1937 return ODP_FIT_PERFECT
;
1940 enum odp_key_fitness
1941 odp_tun_key_from_attr(const struct nlattr
*attr
, bool udpif
,
1942 struct flow_tnl
*tun
)
1944 memset(tun
, 0, sizeof *tun
);
1945 return odp_tun_key_from_attr__(attr
, NULL
, 0, NULL
, tun
, udpif
);
1949 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
1950 const struct flow_tnl
*tun_flow_key
,
1951 const struct ofpbuf
*key_buf
)
1955 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
1957 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1958 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
1959 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
1961 if (tun_key
->ip_src
) {
1962 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
1964 if (tun_key
->ip_dst
) {
1965 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
1967 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
1968 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
1970 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
1971 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
1973 if (tun_key
->ip_tos
) {
1974 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
1976 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
1977 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
1978 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
1980 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
1981 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
1983 if (tun_key
->tp_src
) {
1984 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
1986 if (tun_key
->tp_dst
) {
1987 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
1989 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
1990 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
1992 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
1993 size_t vxlan_opts_ofs
;
1995 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
1996 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
1997 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
1998 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2000 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2002 nl_msg_end_nested(a
, tun_key_ofs
);
2006 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2008 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
2012 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2014 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
2015 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
2017 if (attr
== OVS_KEY_ATTR_IPV6
) {
2018 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2021 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2022 == htonl(IPV6_LABEL_MASK
))
2023 && ipv6_mask
->ipv6_proto
== UINT8_MAX
2024 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
2025 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
2026 && ipv6_mask
->ipv6_frag
== UINT8_MAX
2027 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_src
)
2028 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_dst
);
2030 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2034 if (attr
== OVS_KEY_ATTR_ARP
) {
2035 /* ARP key has padding, ignore it. */
2036 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
2037 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
2038 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
2039 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
2042 return is_all_ones(mask
, size
);
2046 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2048 enum ovs_key_attr attr
= nl_attr_type(ma
);
2052 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2055 mask
= nl_attr_get(ma
);
2056 size
= nl_attr_get_size(ma
);
2059 return odp_mask_is_exact(attr
, mask
, size
);
2063 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2066 struct odp_portno_names
*odp_portno_names
;
2068 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2069 odp_portno_names
->port_no
= port_no
;
2070 odp_portno_names
->name
= xstrdup(port_name
);
2071 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2072 hash_odp_port(port_no
));
2076 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2078 struct odp_portno_names
*odp_portno_names
;
2080 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2081 hash_odp_port(port_no
), portno_names
) {
2082 if (odp_portno_names
->port_no
== port_no
) {
2083 return odp_portno_names
->name
;
2090 odp_portno_names_destroy(struct hmap
*portno_names
)
2092 struct odp_portno_names
*odp_portno_names
, *odp_portno_names_next
;
2093 HMAP_FOR_EACH_SAFE (odp_portno_names
, odp_portno_names_next
,
2094 hmap_node
, portno_names
) {
2095 hmap_remove(portno_names
, &odp_portno_names
->hmap_node
);
2096 free(odp_portno_names
->name
);
2097 free(odp_portno_names
);
2101 /* Format helpers. */
2104 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2105 const struct eth_addr
*mask
, bool verbose
)
2107 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2109 if (verbose
|| !mask_empty
) {
2110 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2113 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2115 ds_put_format(ds
, "%s=", name
);
2116 eth_format_masked(key
, mask
, ds
);
2117 ds_put_char(ds
, ',');
2123 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2124 const ovs_be64
*mask
, bool verbose
)
2126 bool mask_empty
= mask
&& !*mask
;
2128 if (verbose
|| !mask_empty
) {
2129 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2131 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2132 if (!mask_full
) { /* Partially masked. */
2133 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2135 ds_put_char(ds
, ',');
2140 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2141 const ovs_be32
*mask
, bool verbose
)
2143 bool mask_empty
= mask
&& !*mask
;
2145 if (verbose
|| !mask_empty
) {
2146 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2148 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2149 if (!mask_full
) { /* Partially masked. */
2150 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2152 ds_put_char(ds
, ',');
2157 format_in6_addr(struct ds
*ds
, const char *name
,
2158 const struct in6_addr
*key
,
2159 const struct in6_addr
*mask
,
2162 char buf
[INET6_ADDRSTRLEN
];
2163 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2165 if (verbose
|| !mask_empty
) {
2166 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2168 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2169 ds_put_format(ds
, "%s=%s", name
, buf
);
2170 if (!mask_full
) { /* Partially masked. */
2171 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2172 ds_put_format(ds
, "/%s", buf
);
2174 ds_put_char(ds
, ',');
2179 format_ipv6(struct ds
*ds
, const char *name
, const ovs_be32 key_
[4],
2180 const ovs_be32 (*mask_
)[4], bool verbose
)
2182 format_in6_addr(ds
, name
,
2183 (const struct in6_addr
*)key_
,
2184 mask_
? (const struct in6_addr
*)*mask_
: NULL
,
2189 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2190 const ovs_be32
*mask
, bool verbose
)
2192 bool mask_empty
= mask
&& !*mask
;
2194 if (verbose
|| !mask_empty
) {
2195 bool mask_full
= !mask
2196 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2198 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2199 if (!mask_full
) { /* Partially masked. */
2200 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2202 ds_put_char(ds
, ',');
2207 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2208 const uint8_t *mask
, bool verbose
)
2210 bool mask_empty
= mask
&& !*mask
;
2212 if (verbose
|| !mask_empty
) {
2213 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2215 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2216 if (!mask_full
) { /* Partially masked. */
2217 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2219 ds_put_char(ds
, ',');
2224 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2225 const uint8_t *mask
, bool verbose
)
2227 bool mask_empty
= mask
&& !*mask
;
2229 if (verbose
|| !mask_empty
) {
2230 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2232 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2233 if (!mask_full
) { /* Partially masked. */
2234 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2236 ds_put_char(ds
, ',');
2241 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2242 const ovs_be16
*mask
, bool verbose
)
2244 bool mask_empty
= mask
&& !*mask
;
2246 if (verbose
|| !mask_empty
) {
2247 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2249 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2250 if (!mask_full
) { /* Partially masked. */
2251 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2253 ds_put_char(ds
, ',');
2258 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2259 const ovs_be16
*mask
, bool verbose
)
2261 bool mask_empty
= mask
&& !*mask
;
2263 if (verbose
|| !mask_empty
) {
2264 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2266 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2267 if (!mask_full
) { /* Partially masked. */
2268 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2270 ds_put_char(ds
, ',');
2275 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2276 const uint16_t *mask
, bool verbose
)
2278 bool mask_empty
= mask
&& !*mask
;
2280 if (verbose
|| !mask_empty
) {
2281 ds_put_cstr(ds
, name
);
2282 ds_put_char(ds
, '(');
2284 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2285 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2286 } else { /* Fully masked. */
2287 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2289 ds_put_cstr(ds
, "),");
2294 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2295 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2299 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2300 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2301 expected_len
!= ATTR_LEN_NESTED
) {
2303 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2304 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2306 if (bad_key_len
|| bad_mask_len
) {
2308 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2311 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2312 nl_attr_get_size(a
), expected_len
);
2314 format_generic_odp_key(a
, ds
);
2316 ds_put_char(ds
, '/');
2318 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2319 nl_attr_get_size(ma
), expected_len
);
2321 format_generic_odp_key(ma
, ds
);
2323 ds_put_char(ds
, ')');
2332 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2333 const struct nlattr
*ma
)
2335 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2336 format_generic_odp_key(a
, ds
);
2337 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2338 ds_put_char(ds
, '/');
2339 format_generic_odp_key(ma
, ds
);
2341 ds_put_cstr(ds
, "),");
2345 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2346 const struct nlattr
*mask_attr
, struct ds
*ds
,
2350 const struct nlattr
*a
;
2353 ofpbuf_init(&ofp
, 100);
2354 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2355 uint16_t type
= nl_attr_type(a
);
2356 const struct nlattr
*ma
= NULL
;
2359 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2360 nl_attr_get_size(mask_attr
), type
);
2362 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2368 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2369 OVS_VXLAN_EXT_MAX
, true)) {
2374 case OVS_VXLAN_EXT_GBP
: {
2375 uint32_t key
= nl_attr_get_u32(a
);
2376 ovs_be16 id
, id_mask
;
2377 uint8_t flags
, flags_mask
;
2379 id
= htons(key
& 0xFFFF);
2380 flags
= (key
>> 16) & 0xFF;
2382 uint32_t mask
= nl_attr_get_u32(ma
);
2383 id_mask
= htons(mask
& 0xFFFF);
2384 flags_mask
= (mask
>> 16) & 0xFF;
2387 ds_put_cstr(ds
, "gbp(");
2388 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2389 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2391 ds_put_cstr(ds
, "),");
2396 format_unknown_key(ds
, a
, ma
);
2402 ofpbuf_uninit(&ofp
);
2405 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2408 format_geneve_opts(const struct geneve_opt
*opt
,
2409 const struct geneve_opt
*mask
, int opts_len
,
2410 struct ds
*ds
, bool verbose
)
2412 while (opts_len
> 0) {
2414 uint8_t data_len
, data_len_mask
;
2416 if (opts_len
< sizeof *opt
) {
2417 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2418 opts_len
, sizeof *opt
);
2422 data_len
= opt
->length
* 4;
2424 if (mask
->length
== 0x1f) {
2425 data_len_mask
= UINT8_MAX
;
2427 data_len_mask
= mask
->length
;
2430 len
= sizeof *opt
+ data_len
;
2431 if (len
> opts_len
) {
2432 ds_put_format(ds
, "opt len %u greater than remaining %u",
2437 ds_put_char(ds
, '{');
2438 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2440 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2441 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2443 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2444 ds_put_hex(ds
, opt
+ 1, data_len
);
2445 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2446 ds_put_char(ds
, '/');
2447 ds_put_hex(ds
, mask
+ 1, data_len
);
2452 ds_put_char(ds
, '}');
2454 opt
+= len
/ sizeof(*opt
);
2456 mask
+= len
/ sizeof(*opt
);
2463 format_odp_tun_geneve(const struct nlattr
*attr
,
2464 const struct nlattr
*mask_attr
, struct ds
*ds
,
2467 int opts_len
= nl_attr_get_size(attr
);
2468 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2469 const struct geneve_opt
*mask
= mask_attr
?
2470 nl_attr_get(mask_attr
) : NULL
;
2472 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2473 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2474 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2478 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2482 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2483 struct ds
*ds
, bool verbose
)
2486 const struct nlattr
*a
;
2488 uint16_t mask_flags
= 0;
2491 ofpbuf_init(&ofp
, 100);
2492 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2493 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2494 const struct nlattr
*ma
= NULL
;
2497 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2498 nl_attr_get_size(mask_attr
), type
);
2500 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2501 OVS_TUNNEL_KEY_ATTR_MAX
,
2506 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2507 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2512 case OVS_TUNNEL_KEY_ATTR_ID
:
2513 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2514 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2515 flags
|= FLOW_TNL_F_KEY
;
2517 mask_flags
|= FLOW_TNL_F_KEY
;
2520 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2521 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2522 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2524 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2525 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2526 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2528 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2529 struct in6_addr ipv6_src
;
2530 ipv6_src
= nl_attr_get_in6_addr(a
);
2531 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2532 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2535 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2536 struct in6_addr ipv6_dst
;
2537 ipv6_dst
= nl_attr_get_in6_addr(a
);
2538 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2539 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2542 case OVS_TUNNEL_KEY_ATTR_TOS
:
2543 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2544 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2546 case OVS_TUNNEL_KEY_ATTR_TTL
:
2547 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2548 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2550 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2551 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2553 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2554 flags
|= FLOW_TNL_F_CSUM
;
2556 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2557 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2558 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2560 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2561 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2562 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2564 case OVS_TUNNEL_KEY_ATTR_OAM
:
2565 flags
|= FLOW_TNL_F_OAM
;
2567 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2568 ds_put_cstr(ds
, "vxlan(");
2569 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2570 ds_put_cstr(ds
, "),");
2572 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2573 ds_put_cstr(ds
, "geneve(");
2574 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2575 ds_put_cstr(ds
, "),");
2577 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2579 format_unknown_key(ds
, a
, ma
);
2584 /* Flags can have a valid mask even if the attribute is not set, so
2585 * we need to collect these separately. */
2587 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2588 switch (nl_attr_type(a
)) {
2589 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2590 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2592 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2593 mask_flags
|= FLOW_TNL_F_CSUM
;
2595 case OVS_TUNNEL_KEY_ATTR_OAM
:
2596 mask_flags
|= FLOW_TNL_F_OAM
;
2602 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2605 ofpbuf_uninit(&ofp
);
2609 odp_ct_state_to_string(uint32_t flag
)
2612 case OVS_CS_F_REPLY_DIR
:
2614 case OVS_CS_F_TRACKED
:
2618 case OVS_CS_F_ESTABLISHED
:
2620 case OVS_CS_F_RELATED
:
2622 case OVS_CS_F_INVALID
:
2624 case OVS_CS_F_SRC_NAT
:
2626 case OVS_CS_F_DST_NAT
:
2634 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2635 const uint8_t *mask
, bool verbose
)
2637 bool mask_empty
= mask
&& !*mask
;
2639 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2640 if (verbose
|| !mask_empty
) {
2641 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2643 if (!mask_full
) { /* Partially masked. */
2644 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2647 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2653 mask_empty(const struct nlattr
*ma
)
2661 mask
= nl_attr_get(ma
);
2662 n
= nl_attr_get_size(ma
);
2664 return is_all_zeros(mask
, n
);
2668 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2669 const struct hmap
*portno_names
, struct ds
*ds
,
2672 enum ovs_key_attr attr
= nl_attr_type(a
);
2673 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2676 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2678 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2680 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2681 OVS_KEY_ATTR_MAX
, false)) {
2685 ds_put_char(ds
, '(');
2687 case OVS_KEY_ATTR_ENCAP
:
2688 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2689 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2690 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2692 } else if (nl_attr_get_size(a
)) {
2693 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2698 case OVS_KEY_ATTR_PRIORITY
:
2699 case OVS_KEY_ATTR_SKB_MARK
:
2700 case OVS_KEY_ATTR_DP_HASH
:
2701 case OVS_KEY_ATTR_RECIRC_ID
:
2702 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2704 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2708 case OVS_KEY_ATTR_CT_MARK
:
2709 if (verbose
|| !mask_empty(ma
)) {
2710 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2712 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2717 case OVS_KEY_ATTR_CT_STATE
:
2719 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2721 ds_put_format(ds
, "/%#"PRIx32
,
2722 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2724 } else if (!is_exact
) {
2725 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2727 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2730 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2734 case OVS_KEY_ATTR_CT_ZONE
:
2735 if (verbose
|| !mask_empty(ma
)) {
2736 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2738 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2743 case OVS_KEY_ATTR_CT_LABELS
: {
2744 const ovs_u128
*value
= nl_attr_get(a
);
2745 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2747 format_u128(ds
, value
, mask
, verbose
);
2751 case OVS_KEY_ATTR_TUNNEL
:
2752 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2755 case OVS_KEY_ATTR_IN_PORT
:
2756 if (portno_names
&& verbose
&& is_exact
) {
2757 char *name
= odp_portno_names_get(portno_names
,
2758 u32_to_odp(nl_attr_get_u32(a
)));
2760 ds_put_format(ds
, "%s", name
);
2762 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2765 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2767 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2772 case OVS_KEY_ATTR_ETHERNET
: {
2773 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2774 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2776 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2777 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2781 case OVS_KEY_ATTR_VLAN
:
2782 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2783 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2786 case OVS_KEY_ATTR_MPLS
: {
2787 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2788 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2789 size_t size
= nl_attr_get_size(a
);
2791 if (!size
|| size
% sizeof *mpls_key
) {
2792 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2796 mpls_mask
= nl_attr_get(ma
);
2797 if (size
!= nl_attr_get_size(ma
)) {
2798 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2799 "mask length %"PRIuSIZE
")",
2800 size
, nl_attr_get_size(ma
));
2804 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2807 case OVS_KEY_ATTR_ETHERTYPE
:
2808 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2810 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2814 case OVS_KEY_ATTR_IPV4
: {
2815 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2816 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2818 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2819 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2820 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2822 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2823 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2824 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2829 case OVS_KEY_ATTR_IPV6
: {
2830 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2831 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2833 format_ipv6(ds
, "src", key
->ipv6_src
, MASK(mask
, ipv6_src
), verbose
);
2834 format_ipv6(ds
, "dst", key
->ipv6_dst
, MASK(mask
, ipv6_dst
), verbose
);
2835 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2837 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2839 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2841 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2843 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2848 /* These have the same structure and format. */
2849 case OVS_KEY_ATTR_TCP
:
2850 case OVS_KEY_ATTR_UDP
:
2851 case OVS_KEY_ATTR_SCTP
: {
2852 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2853 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2855 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2856 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2860 case OVS_KEY_ATTR_TCP_FLAGS
:
2862 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2863 ntohs(nl_attr_get_be16(a
)),
2864 TCP_FLAGS(nl_attr_get_be16(ma
)),
2865 TCP_FLAGS(OVS_BE16_MAX
));
2867 format_flags(ds
, packet_tcp_flag_to_string
,
2868 ntohs(nl_attr_get_be16(a
)), '|');
2872 case OVS_KEY_ATTR_ICMP
: {
2873 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2874 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2876 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2877 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2881 case OVS_KEY_ATTR_ICMPV6
: {
2882 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2883 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2885 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2887 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2892 case OVS_KEY_ATTR_ARP
: {
2893 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2894 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2896 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2897 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2898 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2899 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2900 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2904 case OVS_KEY_ATTR_ND
: {
2905 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2906 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2908 format_ipv6(ds
, "target", key
->nd_target
, MASK(mask
, nd_target
),
2910 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2911 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2916 case OVS_KEY_ATTR_UNSPEC
:
2917 case __OVS_KEY_ATTR_MAX
:
2919 format_generic_odp_key(a
, ds
);
2921 ds_put_char(ds
, '/');
2922 format_generic_odp_key(ma
, ds
);
2926 ds_put_char(ds
, ')');
2929 static struct nlattr
*
2930 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2931 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2933 const struct nlattr
*a
;
2935 int type
= nl_attr_type(key
);
2936 int size
= nl_attr_get_size(key
);
2938 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2939 nl_msg_put_unspec_zero(ofp
, type
, size
);
2943 if (tbl
[type
].next
) {
2944 tbl
= tbl
[type
].next
;
2945 max
= tbl
[type
].next_max
;
2948 nested_mask
= nl_msg_start_nested(ofp
, type
);
2949 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2950 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2952 nl_msg_end_nested(ofp
, nested_mask
);
2959 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
2962 if (verbose
|| (mask
&& !ovs_u128_is_zero(mask
))) {
2965 value
= hton128(*key
);
2966 ds_put_hex(ds
, &value
, sizeof value
);
2967 if (mask
&& !(ovs_u128_is_ones(mask
))) {
2968 value
= hton128(*mask
);
2969 ds_put_char(ds
, '/');
2970 ds_put_hex(ds
, &value
, sizeof value
);
2976 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
2978 char *s
= CONST_CAST(char *, s_
);
2982 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
2983 *value
= ntoh128(be_value
);
2988 if (ovs_scan(s
, "/%n", &n
)) {
2992 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
2993 sizeof be_mask
, &s
);
2997 *mask
= ntoh128(be_mask
);
2999 *mask
= OVS_U128_MAX
;
3009 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3013 if (ovs_scan(s
, "ufid:")) {
3016 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3028 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3030 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3033 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3034 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3035 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3036 * non-null and 'verbose' is true, translates odp port number to its name. */
3038 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3039 const struct nlattr
*mask
, size_t mask_len
,
3040 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3043 const struct nlattr
*a
;
3045 bool has_ethtype_key
= false;
3046 const struct nlattr
*ma
= NULL
;
3048 bool first_field
= true;
3050 ofpbuf_init(&ofp
, 100);
3051 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3052 bool is_nested_attr
;
3053 bool is_wildcard
= false;
3054 int attr_type
= nl_attr_type(a
);
3056 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3057 has_ethtype_key
= true;
3060 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3061 OVS_KEY_ATTR_MAX
, attr_type
) ==
3064 if (mask
&& mask_len
) {
3065 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3066 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3069 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3070 if (is_wildcard
&& !ma
) {
3071 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3076 ds_put_char(ds
, ',');
3078 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3079 first_field
= false;
3083 ofpbuf_uninit(&ofp
);
3088 if (left
== key_len
) {
3089 ds_put_cstr(ds
, "<empty>");
3091 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3092 for (i
= 0; i
< left
; i
++) {
3093 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3095 ds_put_char(ds
, ')');
3097 if (!has_ethtype_key
) {
3098 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3100 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3101 ntohs(nl_attr_get_be16(ma
)));
3105 ds_put_cstr(ds
, "<empty>");
3109 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3110 * OVS_KEY_ATTR_* attributes in 'key'. */
3112 odp_flow_key_format(const struct nlattr
*key
,
3113 size_t key_len
, struct ds
*ds
)
3115 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3119 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3121 if (!strcasecmp(s
, "no")) {
3122 *type
= OVS_FRAG_TYPE_NONE
;
3123 } else if (!strcasecmp(s
, "first")) {
3124 *type
= OVS_FRAG_TYPE_FIRST
;
3125 } else if (!strcasecmp(s
, "later")) {
3126 *type
= OVS_FRAG_TYPE_LATER
;
3136 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3140 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3141 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3145 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3146 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3149 memset(mask
, 0xff, sizeof *mask
);
3158 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3162 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3166 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3167 IP_SCAN_ARGS(mask
), &n
)) {
3170 *mask
= OVS_BE32_MAX
;
3179 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3182 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3184 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3185 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3189 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3190 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3193 memset(mask
, 0xff, sizeof *mask
);
3202 scan_ipv6(const char *s
, ovs_be32 (*key
)[4], ovs_be32 (*mask
)[4])
3204 return scan_in6_addr(s
, key
? (struct in6_addr
*) *key
: NULL
,
3205 mask
? (struct in6_addr
*) *mask
: NULL
);
3209 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3214 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3215 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3220 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3221 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3223 *mask
= htonl(mask_
);
3225 *mask
= htonl(IPV6_LABEL_MASK
);
3234 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3238 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3242 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3254 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3258 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3262 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3274 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3278 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3282 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3294 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3296 uint16_t key_
, mask_
;
3299 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3304 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3306 *mask
= htons(mask_
);
3308 *mask
= OVS_BE16_MAX
;
3317 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3319 uint64_t key_
, mask_
;
3322 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3325 *key
= htonll(key_
);
3327 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3329 *mask
= htonll(mask_
);
3331 *mask
= OVS_BE64_MAX
;
3340 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3342 uint32_t flags
, fmask
;
3345 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3346 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3347 if (n
>= 0 && s
[n
] == ')') {
3358 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3360 uint32_t flags
, fmask
;
3363 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3364 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3366 *key
= htons(flags
);
3368 *mask
= htons(fmask
);
3376 ovs_to_odp_ct_state(uint8_t state
)
3380 if (state
& CS_NEW
) {
3381 odp
|= OVS_CS_F_NEW
;
3383 if (state
& CS_ESTABLISHED
) {
3384 odp
|= OVS_CS_F_ESTABLISHED
;
3386 if (state
& CS_RELATED
) {
3387 odp
|= OVS_CS_F_RELATED
;
3389 if (state
& CS_INVALID
) {
3390 odp
|= OVS_CS_F_INVALID
;
3392 if (state
& CS_REPLY_DIR
) {
3393 odp
|= OVS_CS_F_REPLY_DIR
;
3395 if (state
& CS_TRACKED
) {
3396 odp
|= OVS_CS_F_TRACKED
;
3398 if (state
& CS_SRC_NAT
) {
3399 odp
|= OVS_CS_F_SRC_NAT
;
3401 if (state
& CS_DST_NAT
) {
3402 odp
|= OVS_CS_F_DST_NAT
;
3409 odp_to_ovs_ct_state(uint32_t flags
)
3413 if (flags
& OVS_CS_F_NEW
) {
3416 if (flags
& OVS_CS_F_ESTABLISHED
) {
3417 state
|= CS_ESTABLISHED
;
3419 if (flags
& OVS_CS_F_RELATED
) {
3420 state
|= CS_RELATED
;
3422 if (flags
& OVS_CS_F_INVALID
) {
3423 state
|= CS_INVALID
;
3425 if (flags
& OVS_CS_F_REPLY_DIR
) {
3426 state
|= CS_REPLY_DIR
;
3428 if (flags
& OVS_CS_F_TRACKED
) {
3429 state
|= CS_TRACKED
;
3431 if (flags
& OVS_CS_F_SRC_NAT
) {
3432 state
|= CS_SRC_NAT
;
3434 if (flags
& OVS_CS_F_DST_NAT
) {
3435 state
|= CS_DST_NAT
;
3442 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3444 uint32_t flags
, fmask
;
3447 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3448 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3449 mask
? &fmask
: NULL
);
3462 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3466 enum ovs_frag_type frag_type
;
3468 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3469 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3482 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3483 const struct simap
*port_names
)
3487 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3491 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3498 } else if (port_names
) {
3499 const struct simap_node
*node
;
3502 len
= strcspn(s
, ")");
3503 node
= simap_find_len(port_names
, s
, len
);
3516 /* Helper for vlan parsing. */
3517 struct ovs_key_vlan__
{
3522 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3524 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3526 if (value
>> bits
) {
3530 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3535 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3538 uint16_t key_
, mask_
;
3541 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3544 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3546 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3549 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3553 *mask
|= htons(((1U << bits
) - 1) << offset
);
3563 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3565 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3569 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3571 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3575 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3577 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3582 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3584 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3586 if (value
>> bits
) {
3590 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3595 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3598 uint32_t key_
, mask_
;
3601 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3604 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3606 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3609 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3613 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3623 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3625 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3629 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3631 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3635 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3637 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3641 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3643 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3647 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3649 const char *s_base
= s
;
3650 ovs_be16 id
= 0, id_mask
= 0;
3651 uint8_t flags
= 0, flags_mask
= 0;
3653 if (!strncmp(s
, "id=", 3)) {
3655 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3661 if (!strncmp(s
, "flags=", 6)) {
3663 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3666 if (!strncmp(s
, "))", 2)) {
3669 *key
= (flags
<< 16) | ntohs(id
);
3671 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3681 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3683 const char *s_base
= s
;
3684 struct geneve_opt
*opt
= key
->d
;
3685 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3686 int len_remain
= sizeof key
->d
;
3688 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3692 len_remain
-= sizeof *opt
;
3694 if (!strncmp(s
, "class=", 6)) {
3696 s
+= scan_be16(s
, &opt
->opt_class
,
3697 mask
? &opt_mask
->opt_class
: NULL
);
3699 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3705 if (!strncmp(s
, "type=", 5)) {
3707 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3709 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3715 if (!strncmp(s
, "len=", 4)) {
3716 uint8_t opt_len
, opt_len_mask
;
3718 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3720 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3723 opt
->length
= opt_len
/ 4;
3725 opt_mask
->length
= opt_len_mask
;
3729 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3735 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3742 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3743 data_len
, (char **)&s
)) {
3754 opt
+= 1 + data_len
/ 4;
3756 opt_mask
+= 1 + data_len
/ 4;
3758 len_remain
-= data_len
;
3763 int len
= sizeof key
->d
- len_remain
;
3777 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3779 const uint16_t *flags
= data_
;
3781 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3782 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3784 if (*flags
& FLOW_TNL_F_CSUM
) {
3785 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3787 if (*flags
& FLOW_TNL_F_OAM
) {
3788 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3793 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3795 const uint32_t *gbp
= data_
;
3798 size_t vxlan_opts_ofs
;
3800 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3801 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3802 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3807 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3809 const struct geneve_scan
*geneve
= data_
;
3811 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3815 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3817 unsigned long call_fn = (unsigned long)FUNC; \
3819 typedef void (*fn)(struct ofpbuf *, const void *); \
3821 func(BUF, &(DATA)); \
3823 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3827 #define SCAN_IF(NAME) \
3828 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3829 const char *start = s; \
3834 /* Usually no special initialization is needed. */
3835 #define SCAN_BEGIN(NAME, TYPE) \
3838 memset(&skey, 0, sizeof skey); \
3839 memset(&smask, 0, sizeof smask); \
3843 /* Init as fully-masked as mask will not be scanned. */
3844 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3847 memset(&skey, 0, sizeof skey); \
3848 memset(&smask, 0xff, sizeof smask); \
3852 /* VLAN needs special initialization. */
3853 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3855 TYPE skey = KEY_INIT; \
3856 TYPE smask = MASK_INIT; \
3860 /* Scan unnamed entry as 'TYPE' */
3861 #define SCAN_TYPE(TYPE, KEY, MASK) \
3862 len = scan_##TYPE(s, KEY, MASK); \
3868 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3869 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3870 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3871 s += strlen(NAME); \
3872 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3876 #define SCAN_FINISH() \
3877 } while (*s++ == ',' && len != 0); \
3878 if (s[-1] != ')') { \
3882 #define SCAN_FINISH_SINGLE() \
3884 if (*s++ != ')') { \
3888 /* Beginning of nested attribute. */
3889 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3891 size_t key_offset, mask_offset; \
3892 key_offset = nl_msg_start_nested(key, ATTR); \
3894 mask_offset = nl_msg_start_nested(mask, ATTR); \
3899 #define SCAN_END_NESTED() \
3901 nl_msg_end_nested(key, key_offset); \
3903 nl_msg_end_nested(mask, mask_offset); \
3908 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3909 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3911 memset(&skey, 0, sizeof skey); \
3912 memset(&smask, 0xff, sizeof smask); \
3913 s += strlen(NAME); \
3914 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3915 SCAN_PUT(ATTR, FUNC); \
3919 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3920 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3922 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3923 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3925 #define SCAN_PUT(ATTR, FUNC) \
3926 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3928 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3930 #define SCAN_END(ATTR) \
3932 SCAN_PUT(ATTR, NULL); \
3936 #define SCAN_END_SINGLE(ATTR) \
3937 SCAN_FINISH_SINGLE(); \
3938 SCAN_PUT(ATTR, NULL); \
3942 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3943 SCAN_BEGIN(NAME, TYPE) { \
3944 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3945 } SCAN_END_SINGLE(ATTR)
3947 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3948 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3949 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3950 } SCAN_END_SINGLE(ATTR)
3952 /* scan_port needs one extra argument. */
3953 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3954 SCAN_BEGIN(NAME, TYPE) { \
3955 len = scan_port(s, &skey, &smask, port_names); \
3960 } SCAN_END_SINGLE(ATTR)
3963 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
3964 struct ofpbuf
*key
, struct ofpbuf
*mask
)
3970 len
= odp_ufid_from_string(s
, &ufid
);
3975 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
3976 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
3977 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
3978 OVS_KEY_ATTR_RECIRC_ID
);
3979 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
3981 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
3982 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
3983 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
3984 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
3986 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
3987 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
3988 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
3989 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
3990 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
3991 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
3992 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
3993 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
3994 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
3995 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
3996 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
3997 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
3999 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4000 } SCAN_END_NESTED();
4002 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4004 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4005 SCAN_FIELD("src=", eth
, eth_src
);
4006 SCAN_FIELD("dst=", eth
, eth_dst
);
4007 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4009 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4010 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4011 SCAN_FIELD("vid=", vid
, tci
);
4012 SCAN_FIELD("pcp=", pcp
, tci
);
4013 SCAN_FIELD("cfi=", cfi
, tci
);
4014 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4016 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4018 SCAN_BEGIN("mpls(", struct ovs_key_mpls
) {
4019 SCAN_FIELD("label=", mpls_label
, mpls_lse
);
4020 SCAN_FIELD("tc=", mpls_tc
, mpls_lse
);
4021 SCAN_FIELD("ttl=", mpls_ttl
, mpls_lse
);
4022 SCAN_FIELD("bos=", mpls_bos
, mpls_lse
);
4023 } SCAN_END(OVS_KEY_ATTR_MPLS
);
4025 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4026 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4027 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4028 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4029 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4030 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4031 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4032 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4034 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4035 SCAN_FIELD("src=", ipv6
, ipv6_src
);
4036 SCAN_FIELD("dst=", ipv6
, ipv6_dst
);
4037 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4038 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4039 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4040 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4041 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4042 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4044 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4045 SCAN_FIELD("src=", be16
, tcp_src
);
4046 SCAN_FIELD("dst=", be16
, tcp_dst
);
4047 } SCAN_END(OVS_KEY_ATTR_TCP
);
4049 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4051 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4052 SCAN_FIELD("src=", be16
, udp_src
);
4053 SCAN_FIELD("dst=", be16
, udp_dst
);
4054 } SCAN_END(OVS_KEY_ATTR_UDP
);
4056 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4057 SCAN_FIELD("src=", be16
, sctp_src
);
4058 SCAN_FIELD("dst=", be16
, sctp_dst
);
4059 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4061 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4062 SCAN_FIELD("type=", u8
, icmp_type
);
4063 SCAN_FIELD("code=", u8
, icmp_code
);
4064 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4066 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4067 SCAN_FIELD("type=", u8
, icmpv6_type
);
4068 SCAN_FIELD("code=", u8
, icmpv6_code
);
4069 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4071 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4072 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4073 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4074 SCAN_FIELD("op=", be16
, arp_op
);
4075 SCAN_FIELD("sha=", eth
, arp_sha
);
4076 SCAN_FIELD("tha=", eth
, arp_tha
);
4077 } SCAN_END(OVS_KEY_ATTR_ARP
);
4079 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4080 SCAN_FIELD("target=", ipv6
, nd_target
);
4081 SCAN_FIELD("sll=", eth
, nd_sll
);
4082 SCAN_FIELD("tll=", eth
, nd_tll
);
4083 } SCAN_END(OVS_KEY_ATTR_ND
);
4085 /* Encap open-coded. */
4086 if (!strncmp(s
, "encap(", 6)) {
4087 const char *start
= s
;
4088 size_t encap
, encap_mask
= 0;
4090 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4092 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4099 s
+= strspn(s
, delimiters
);
4102 } else if (*s
== ')') {
4106 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4114 nl_msg_end_nested(key
, encap
);
4116 nl_msg_end_nested(mask
, encap_mask
);
4125 /* Parses the string representation of a datapath flow key, in the
4126 * format output by odp_flow_key_format(). Returns 0 if successful,
4127 * otherwise a positive errno value. On success, the flow key is
4128 * appended to 'key' as a series of Netlink attributes. On failure, no
4129 * data is appended to 'key'. Either way, 'key''s data might be
4132 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4133 * to a port number. (Port names may be used instead of port numbers in
4136 * On success, the attributes appended to 'key' are individually syntactically
4137 * valid, but they may not be valid as a sequence. 'key' might, for example,
4138 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4140 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4141 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4143 const size_t old_size
= key
->size
;
4147 s
+= strspn(s
, delimiters
);
4152 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4154 key
->size
= old_size
;
4164 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4167 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4168 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4169 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4170 * must use a zero mask for the netlink frag field, and all ones mask
4172 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4174 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4175 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4176 : OVS_FRAG_TYPE_FIRST
;
4179 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4180 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4181 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4183 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4185 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4187 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4189 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4190 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4191 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4192 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4194 /* These share the same layout. */
4196 struct ovs_key_tcp tcp
;
4197 struct ovs_key_udp udp
;
4198 struct ovs_key_sctp sctp
;
4201 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4202 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4205 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4206 bool export_mask
, struct ofpbuf
*buf
)
4208 struct ovs_key_ethernet
*eth_key
;
4210 const struct flow
*flow
= parms
->flow
;
4211 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4213 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4215 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4216 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4220 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4222 if (parms
->support
.ct_state
) {
4223 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4224 ovs_to_odp_ct_state(data
->ct_state
));
4226 if (parms
->support
.ct_zone
) {
4227 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4229 if (parms
->support
.ct_mark
) {
4230 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4232 if (parms
->support
.ct_label
) {
4233 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4234 sizeof(data
->ct_label
));
4236 if (parms
->support
.recirc
) {
4237 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4238 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4241 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
4242 * is not the magical value "ODPP_NONE". */
4243 if (export_mask
|| parms
->odp_in_port
!= ODPP_NONE
) {
4244 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, parms
->odp_in_port
);
4247 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4249 get_ethernet_key(data
, eth_key
);
4251 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4253 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4255 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4257 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4258 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4259 if (flow
->vlan_tci
== htons(0)) {
4266 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4267 /* For backwards compatibility with kernels that don't support
4268 * wildcarding, the following convention is used to encode the
4269 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4272 * -------- -------- -------
4273 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4274 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4275 * <none> 0xffff Any non-Ethernet II frame (except valid
4276 * 802.3 SNAP packet with valid eth_type).
4279 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4284 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4286 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4287 struct ovs_key_ipv4
*ipv4_key
;
4289 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4291 get_ipv4_key(data
, ipv4_key
, export_mask
);
4292 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4293 struct ovs_key_ipv6
*ipv6_key
;
4295 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4297 get_ipv6_key(data
, ipv6_key
, export_mask
);
4298 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4299 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4300 struct ovs_key_arp
*arp_key
;
4302 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4304 get_arp_key(data
, arp_key
);
4305 } else if (eth_type_mpls(flow
->dl_type
)) {
4306 struct ovs_key_mpls
*mpls_key
;
4309 n
= flow_count_mpls_labels(flow
, NULL
);
4311 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4313 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4314 n
* sizeof *mpls_key
);
4315 for (i
= 0; i
< n
; i
++) {
4316 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4320 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4321 if (flow
->nw_proto
== IPPROTO_TCP
) {
4322 union ovs_key_tp
*tcp_key
;
4324 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4326 get_tp_key(data
, tcp_key
);
4327 if (data
->tcp_flags
) {
4328 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4330 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4331 union ovs_key_tp
*udp_key
;
4333 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4335 get_tp_key(data
, udp_key
);
4336 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4337 union ovs_key_tp
*sctp_key
;
4339 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4341 get_tp_key(data
, sctp_key
);
4342 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4343 && flow
->nw_proto
== IPPROTO_ICMP
) {
4344 struct ovs_key_icmp
*icmp_key
;
4346 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4348 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4349 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4350 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4351 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4352 struct ovs_key_icmpv6
*icmpv6_key
;
4354 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4355 sizeof *icmpv6_key
);
4356 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4357 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4359 if (flow
->tp_dst
== htons(0)
4360 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)
4361 || flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))
4362 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
4363 * type and code are 8 bits wide. Therefore, an exact match
4364 * looks like htons(0xff), not htons(0xffff). See
4365 * xlate_wc_finish() for details. */
4366 && (!export_mask
|| (data
->tp_src
== htons(0xff)
4367 && data
->tp_dst
== htons(0xff)))) {
4369 struct ovs_key_nd
*nd_key
;
4371 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4373 memcpy(nd_key
->nd_target
, &data
->nd_target
,
4374 sizeof nd_key
->nd_target
);
4375 nd_key
->nd_sll
= data
->arp_sha
;
4376 nd_key
->nd_tll
= data
->arp_tha
;
4383 nl_msg_end_nested(buf
, encap
);
4387 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4389 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4390 * capable of being expanded to allow for that much space. */
4392 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4395 odp_flow_key_from_flow__(parms
, false, buf
);
4398 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4401 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4402 * capable of being expanded to allow for that much space. */
4404 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4407 odp_flow_key_from_flow__(parms
, true, buf
);
4410 /* Generate ODP flow key from the given packet metadata */
4412 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4414 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4416 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4417 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4420 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4423 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4424 ovs_to_odp_ct_state(md
->ct_state
));
4426 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4429 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4431 if (!ovs_u128_is_zero(&md
->ct_label
)) {
4432 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4433 sizeof(md
->ct_label
));
4437 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4438 * value "ODPP_NONE". */
4439 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4440 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4444 /* Generate packet metadata from the given ODP flow key. */
4446 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4447 struct pkt_metadata
*md
)
4449 const struct nlattr
*nla
;
4451 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4452 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4453 1u << OVS_KEY_ATTR_IN_PORT
;
4455 pkt_metadata_init(md
, ODPP_NONE
);
4457 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4458 uint16_t type
= nl_attr_type(nla
);
4459 size_t len
= nl_attr_get_size(nla
);
4460 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4461 OVS_KEY_ATTR_MAX
, type
);
4463 if (len
!= expected_len
&& expected_len
>= 0) {
4468 case OVS_KEY_ATTR_RECIRC_ID
:
4469 md
->recirc_id
= nl_attr_get_u32(nla
);
4470 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4472 case OVS_KEY_ATTR_DP_HASH
:
4473 md
->dp_hash
= nl_attr_get_u32(nla
);
4474 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4476 case OVS_KEY_ATTR_PRIORITY
:
4477 md
->skb_priority
= nl_attr_get_u32(nla
);
4478 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4480 case OVS_KEY_ATTR_SKB_MARK
:
4481 md
->pkt_mark
= nl_attr_get_u32(nla
);
4482 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4484 case OVS_KEY_ATTR_CT_STATE
:
4485 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4486 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4488 case OVS_KEY_ATTR_CT_ZONE
:
4489 md
->ct_zone
= nl_attr_get_u16(nla
);
4490 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4492 case OVS_KEY_ATTR_CT_MARK
:
4493 md
->ct_mark
= nl_attr_get_u32(nla
);
4494 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4496 case OVS_KEY_ATTR_CT_LABELS
: {
4497 const ovs_u128
*cl
= nl_attr_get(nla
);
4500 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4503 case OVS_KEY_ATTR_TUNNEL
: {
4504 enum odp_key_fitness res
;
4506 res
= odp_tun_key_from_attr(nla
, true, &md
->tunnel
);
4507 if (res
== ODP_FIT_ERROR
) {
4508 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4509 } else if (res
== ODP_FIT_PERFECT
) {
4510 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4514 case OVS_KEY_ATTR_IN_PORT
:
4515 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4516 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4522 if (!wanted_attrs
) {
4523 return; /* Have everything. */
4529 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4531 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4532 return hash_words(ALIGNED_CAST(const uint32_t *, key
),
4533 key_len
/ sizeof(uint32_t), 0);
4537 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4538 uint64_t attrs
, int out_of_range_attr
,
4539 const struct nlattr
*key
, size_t key_len
)
4544 if (VLOG_DROP_DBG(rl
)) {
4549 for (i
= 0; i
< 64; i
++) {
4550 if (attrs
& (UINT64_C(1) << i
)) {
4551 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4553 ds_put_format(&s
, " %s",
4554 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4557 if (out_of_range_attr
) {
4558 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4561 ds_put_cstr(&s
, ": ");
4562 odp_flow_key_format(key
, key_len
, &s
);
4564 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4569 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4571 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4574 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4577 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4578 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4579 return 0xff; /* Error. */
4582 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4583 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4584 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4588 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4589 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4590 int *out_of_range_attrp
)
4592 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4593 const struct nlattr
*nla
;
4594 uint64_t present_attrs
;
4597 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4599 *out_of_range_attrp
= 0;
4600 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4601 uint16_t type
= nl_attr_type(nla
);
4602 size_t len
= nl_attr_get_size(nla
);
4603 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4604 OVS_KEY_ATTR_MAX
, type
);
4606 if (len
!= expected_len
&& expected_len
>= 0) {
4607 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4609 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4610 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4616 if (type
> OVS_KEY_ATTR_MAX
) {
4617 *out_of_range_attrp
= type
;
4619 if (present_attrs
& (UINT64_C(1) << type
)) {
4620 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4622 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4623 ovs_key_attr_to_string(type
,
4624 namebuf
, sizeof namebuf
));
4628 present_attrs
|= UINT64_C(1) << type
;
4633 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4637 *present_attrsp
= present_attrs
;
4641 static enum odp_key_fitness
4642 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4643 uint64_t expected_attrs
,
4644 const struct nlattr
*key
, size_t key_len
)
4646 uint64_t missing_attrs
;
4647 uint64_t extra_attrs
;
4649 missing_attrs
= expected_attrs
& ~present_attrs
;
4650 if (missing_attrs
) {
4651 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4652 log_odp_key_attributes(&rl
, "expected but not present",
4653 missing_attrs
, 0, key
, key_len
);
4654 return ODP_FIT_TOO_LITTLE
;
4657 extra_attrs
= present_attrs
& ~expected_attrs
;
4658 if (extra_attrs
|| out_of_range_attr
) {
4659 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4660 log_odp_key_attributes(&rl
, "present but not expected",
4661 extra_attrs
, out_of_range_attr
, key
, key_len
);
4662 return ODP_FIT_TOO_MUCH
;
4665 return ODP_FIT_PERFECT
;
4669 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4670 uint64_t present_attrs
, uint64_t *expected_attrs
,
4671 struct flow
*flow
, const struct flow
*src_flow
)
4673 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4674 bool is_mask
= flow
!= src_flow
;
4676 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4677 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4678 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4679 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4680 ntohs(flow
->dl_type
));
4683 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4684 flow
->dl_type
!= htons(0xffff)) {
4687 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4690 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4691 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4692 /* See comments in odp_flow_key_from_flow__(). */
4693 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4700 static enum odp_key_fitness
4701 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4702 uint64_t present_attrs
, int out_of_range_attr
,
4703 uint64_t expected_attrs
, struct flow
*flow
,
4704 const struct nlattr
*key
, size_t key_len
,
4705 const struct flow
*src_flow
)
4707 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4708 bool is_mask
= src_flow
!= flow
;
4709 const void *check_start
= NULL
;
4710 size_t check_len
= 0;
4711 enum ovs_key_attr expected_bit
= 0xff;
4713 if (eth_type_mpls(src_flow
->dl_type
)) {
4714 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4715 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4717 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4718 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4719 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4720 int n
= size
/ sizeof(ovs_be32
);
4723 if (!size
|| size
% sizeof(ovs_be32
)) {
4724 return ODP_FIT_ERROR
;
4726 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4727 return ODP_FIT_ERROR
;
4730 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4731 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4733 if (n
> FLOW_MAX_MPLS_LABELS
) {
4734 return ODP_FIT_TOO_MUCH
;
4738 /* BOS may be set only in the innermost label. */
4739 for (i
= 0; i
< n
- 1; i
++) {
4740 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4741 return ODP_FIT_ERROR
;
4745 /* BOS must be set in the innermost label. */
4746 if (n
< FLOW_MAX_MPLS_LABELS
4747 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4748 return ODP_FIT_TOO_LITTLE
;
4754 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4756 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4758 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4759 const struct ovs_key_ipv4
*ipv4_key
;
4761 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4762 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4763 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4764 return ODP_FIT_ERROR
;
4767 check_start
= ipv4_key
;
4768 check_len
= sizeof *ipv4_key
;
4769 expected_bit
= OVS_KEY_ATTR_IPV4
;
4772 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4774 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4776 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4777 const struct ovs_key_ipv6
*ipv6_key
;
4779 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4780 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4781 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4782 return ODP_FIT_ERROR
;
4785 check_start
= ipv6_key
;
4786 check_len
= sizeof *ipv6_key
;
4787 expected_bit
= OVS_KEY_ATTR_IPV6
;
4790 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4791 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4793 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4795 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4796 const struct ovs_key_arp
*arp_key
;
4798 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4799 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4800 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4801 "key", ntohs(arp_key
->arp_op
));
4802 return ODP_FIT_ERROR
;
4804 put_arp_key(arp_key
, flow
);
4806 check_start
= arp_key
;
4807 check_len
= sizeof *arp_key
;
4808 expected_bit
= OVS_KEY_ATTR_ARP
;
4814 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4815 if (!is_all_zeros(check_start
, check_len
) &&
4816 flow
->dl_type
!= htons(0xffff)) {
4817 return ODP_FIT_ERROR
;
4819 expected_attrs
|= UINT64_C(1) << expected_bit
;
4823 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4824 if (src_flow
->nw_proto
== IPPROTO_TCP
4825 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4826 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4827 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4829 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4831 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4832 const union ovs_key_tp
*tcp_key
;
4834 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4835 put_tp_key(tcp_key
, flow
);
4836 expected_bit
= OVS_KEY_ATTR_TCP
;
4838 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4839 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4840 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4842 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4843 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4844 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4845 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4847 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4849 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4850 const union ovs_key_tp
*udp_key
;
4852 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4853 put_tp_key(udp_key
, flow
);
4854 expected_bit
= OVS_KEY_ATTR_UDP
;
4856 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4857 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4858 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4859 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4861 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4863 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4864 const union ovs_key_tp
*sctp_key
;
4866 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4867 put_tp_key(sctp_key
, flow
);
4868 expected_bit
= OVS_KEY_ATTR_SCTP
;
4870 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4871 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4872 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4874 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4876 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4877 const struct ovs_key_icmp
*icmp_key
;
4879 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4880 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4881 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4882 expected_bit
= OVS_KEY_ATTR_ICMP
;
4884 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
4885 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4886 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4888 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
4890 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
4891 const struct ovs_key_icmpv6
*icmpv6_key
;
4893 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
4894 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
4895 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
4896 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
4897 if (src_flow
->tp_dst
== htons(0) &&
4898 (src_flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
4899 src_flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))) {
4901 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4903 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
4904 const struct ovs_key_nd
*nd_key
;
4906 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
4907 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
4908 sizeof flow
->nd_target
);
4909 flow
->arp_sha
= nd_key
->nd_sll
;
4910 flow
->arp_tha
= nd_key
->nd_tll
;
4912 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
4913 * ICMP type and code are 8 bits wide. Therefore, an
4914 * exact match looks like htons(0xff), not
4915 * htons(0xffff). See xlate_wc_finish() for details.
4917 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
4918 (flow
->tp_src
!= htons(0xff) ||
4919 flow
->tp_dst
!= htons(0xff))) {
4920 return ODP_FIT_ERROR
;
4922 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4929 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
4930 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
4931 return ODP_FIT_ERROR
;
4933 expected_attrs
|= UINT64_C(1) << expected_bit
;
4938 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
4942 /* Parse 802.1Q header then encapsulated L3 attributes. */
4943 static enum odp_key_fitness
4944 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4945 uint64_t present_attrs
, int out_of_range_attr
,
4946 uint64_t expected_attrs
, struct flow
*flow
,
4947 const struct nlattr
*key
, size_t key_len
,
4948 const struct flow
*src_flow
)
4950 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4951 bool is_mask
= src_flow
!= flow
;
4953 const struct nlattr
*encap
4954 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
4955 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
4956 enum odp_key_fitness encap_fitness
;
4957 enum odp_key_fitness fitness
;
4959 /* Calculate fitness of outer attributes. */
4961 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
4962 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
4964 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
4965 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
4967 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
4968 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
4971 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
4972 expected_attrs
, key
, key_len
);
4975 * Remove the TPID from dl_type since it's not the real Ethertype. */
4976 flow
->dl_type
= htons(0);
4977 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
4978 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
4981 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
4982 return ODP_FIT_TOO_LITTLE
;
4983 } else if (flow
->vlan_tci
== htons(0)) {
4984 /* Corner case for a truncated 802.1Q header. */
4985 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
4986 return ODP_FIT_TOO_MUCH
;
4989 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
4990 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
4991 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
4992 return ODP_FIT_ERROR
;
4995 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5000 /* Now parse the encapsulated attributes. */
5001 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5002 attrs
, &present_attrs
, &out_of_range_attr
)) {
5003 return ODP_FIT_ERROR
;
5007 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
5008 return ODP_FIT_ERROR
;
5010 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5011 expected_attrs
, flow
, key
, key_len
,
5014 /* The overall fitness is the worse of the outer and inner attributes. */
5015 return MAX(fitness
, encap_fitness
);
5018 static enum odp_key_fitness
5019 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5020 const struct nlattr
*src_key
, size_t src_key_len
,
5021 struct flow
*flow
, const struct flow
*src_flow
,
5024 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5025 uint64_t expected_attrs
;
5026 uint64_t present_attrs
;
5027 int out_of_range_attr
;
5028 bool is_mask
= src_flow
!= flow
;
5030 memset(flow
, 0, sizeof *flow
);
5032 /* Parse attributes. */
5033 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5034 &out_of_range_attr
)) {
5035 return ODP_FIT_ERROR
;
5040 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5041 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5042 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5043 } else if (is_mask
) {
5044 /* Always exact match recirc_id if it is not specified. */
5045 flow
->recirc_id
= UINT32_MAX
;
5048 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5049 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5050 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5052 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5053 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5054 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5057 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5058 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5059 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5062 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5063 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5065 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5066 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5068 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5069 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5070 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5072 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5073 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5074 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5076 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5077 const ovs_u128
*cl
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5079 flow
->ct_label
= *cl
;
5080 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5083 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5084 enum odp_key_fitness res
;
5086 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
],
5087 is_mask
? src_key
: NULL
,
5088 src_key_len
, &src_flow
->tunnel
,
5089 &flow
->tunnel
, udpif
);
5090 if (res
== ODP_FIT_ERROR
) {
5091 return ODP_FIT_ERROR
;
5092 } else if (res
== ODP_FIT_PERFECT
) {
5093 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5097 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5098 flow
->in_port
.odp_port
5099 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5100 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5101 } else if (!is_mask
) {
5102 flow
->in_port
.odp_port
= ODPP_NONE
;
5105 /* Ethernet header. */
5106 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5107 const struct ovs_key_ethernet
*eth_key
;
5109 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5110 put_ethernet_key(eth_key
, flow
);
5112 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5116 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5119 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5120 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5122 return ODP_FIT_ERROR
;
5126 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5127 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5128 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5129 expected_attrs
, flow
, key
, key_len
, src_flow
);
5132 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5133 flow
->vlan_tci
= htons(0xffff);
5134 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5135 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5136 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5139 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5140 expected_attrs
, flow
, key
, key_len
, src_flow
);
5143 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5144 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5145 * 'key' fits our expectations for what a flow key should contain.
5147 * The 'in_port' will be the datapath's understanding of the port. The
5148 * caller will need to translate with odp_port_to_ofp_port() if the
5149 * OpenFlow port is needed.
5151 * This function doesn't take the packet itself as an argument because none of
5152 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5153 * it is always possible to infer which additional attribute(s) should appear
5154 * by looking at the attributes for lower-level protocols, e.g. if the network
5155 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5156 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5157 * must be absent. */
5158 enum odp_key_fitness
5159 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5162 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, false);
5165 static enum odp_key_fitness
5166 odp_flow_key_to_mask__(const struct nlattr
*mask_key
, size_t mask_key_len
,
5167 const struct nlattr
*flow_key
, size_t flow_key_len
,
5168 struct flow_wildcards
*mask
,
5169 const struct flow
*src_flow
,
5173 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5174 flow_key
, flow_key_len
,
5175 &mask
->masks
, src_flow
, udpif
);
5178 /* A missing mask means that the flow should be exact matched.
5179 * Generate an appropriate exact wildcard for the flow. */
5180 flow_wildcards_init_for_packet(mask
, src_flow
);
5182 return ODP_FIT_PERFECT
;
5185 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5186 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5187 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5188 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5189 * well 'key' fits our expectations for what a flow key should contain. */
5190 enum odp_key_fitness
5191 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5192 const struct nlattr
*flow_key
, size_t flow_key_len
,
5193 struct flow_wildcards
*mask
, const struct flow
*flow
)
5195 return odp_flow_key_to_mask__(mask_key
, mask_key_len
,
5196 flow_key
, flow_key_len
,
5200 /* These functions are similar to their non-"_udpif" variants but output a
5201 * 'flow' that is suitable for fast-path packet processing.
5203 * Some fields have different representation for flow setup and per-
5204 * packet processing (i.e. different between ofproto-dpif and userspace
5205 * datapath). In particular, with the non-"_udpif" functions, struct
5206 * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
5207 * with these functions, struct tun_metadata is in the per-packet format
5208 * (using 'present.len' and 'opts.gnv'). */
5209 enum odp_key_fitness
5210 odp_flow_key_to_flow_udpif(const struct nlattr
*key
, size_t key_len
,
5213 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, true);
5216 enum odp_key_fitness
5217 odp_flow_key_to_mask_udpif(const struct nlattr
*mask_key
, size_t mask_key_len
,
5218 const struct nlattr
*flow_key
, size_t flow_key_len
,
5219 struct flow_wildcards
*mask
,
5220 const struct flow
*flow
)
5222 return odp_flow_key_to_mask__(mask_key
, mask_key_len
,
5223 flow_key
, flow_key_len
,
5227 /* Returns 'fitness' as a string, for use in debug messages. */
5229 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5232 case ODP_FIT_PERFECT
:
5234 case ODP_FIT_TOO_MUCH
:
5236 case ODP_FIT_TOO_LITTLE
:
5237 return "too_little";
5245 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5246 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5247 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5248 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5249 * null, then the return value is not meaningful.) */
5251 odp_put_userspace_action(uint32_t pid
,
5252 const void *userdata
, size_t userdata_size
,
5253 odp_port_t tunnel_out_port
,
5254 bool include_actions
,
5255 struct ofpbuf
*odp_actions
)
5257 size_t userdata_ofs
;
5260 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5261 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5263 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5265 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5266 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5269 * - The kernel rejected shorter userdata with -ERANGE.
5271 * - The kernel silently dropped userdata beyond the first 8 bytes.
5273 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5274 * separately disable features that required more than 8 bytes.) */
5275 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5276 MAX(8, userdata_size
)),
5277 userdata
, userdata_size
);
5281 if (tunnel_out_port
!= ODPP_NONE
) {
5282 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5285 if (include_actions
) {
5286 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5288 nl_msg_end_nested(odp_actions
, offset
);
5290 return userdata_ofs
;
5294 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5295 struct ofpbuf
*odp_actions
)
5297 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5298 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5299 nl_msg_end_nested(odp_actions
, offset
);
5303 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5304 struct ovs_action_push_tnl
*data
)
5306 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5308 size
+= data
->header_len
;
5309 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5313 /* The commit_odp_actions() function and its helpers. */
5316 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5317 const void *key
, size_t key_size
)
5319 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5320 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5321 nl_msg_end_nested(odp_actions
, offset
);
5324 /* Masked set actions have a mask following the data within the netlink
5325 * attribute. The unmasked bits in the data will be cleared as the data
5326 * is copied to the action. */
5328 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5329 enum ovs_key_attr key_type
,
5330 const void *key_
, const void *mask_
, size_t key_size
)
5332 size_t offset
= nl_msg_start_nested(odp_actions
,
5333 OVS_ACTION_ATTR_SET_MASKED
);
5334 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5335 const char *key
= key_
, *mask
= mask_
;
5337 memcpy(data
+ key_size
, mask
, key_size
);
5338 /* Clear unmasked bits while copying. */
5339 while (key_size
--) {
5340 *data
++ = *key
++ & *mask
++;
5342 nl_msg_end_nested(odp_actions
, offset
);
5345 /* If any of the flow key data that ODP actions can modify are different in
5346 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5347 * 'odp_actions' that change the flow tunneling information in key from
5348 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5349 * same way. In other words, operates the same as commit_odp_actions(), but
5350 * only on tunneling information. */
5352 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5353 struct ofpbuf
*odp_actions
)
5355 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5356 * must have non-zero ipv6_dst. */
5357 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5358 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5361 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5362 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5367 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5368 const void *key
, void *base
, void *mask
, size_t size
,
5369 struct ofpbuf
*odp_actions
)
5371 if (memcmp(key
, base
, size
)) {
5372 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5374 if (use_masked_set
&& !fully_masked
) {
5375 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5377 if (!fully_masked
) {
5378 memset(mask
, 0xff, size
);
5380 commit_set_action(odp_actions
, attr
, key
, size
);
5382 memcpy(base
, key
, size
);
5385 /* Mask bits are set when we have either read or set the corresponding
5386 * values. Masked bits will be exact-matched, no need to set them
5387 * if the value did not actually change. */
5393 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5395 eth
->eth_src
= flow
->dl_src
;
5396 eth
->eth_dst
= flow
->dl_dst
;
5400 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5402 flow
->dl_src
= eth
->eth_src
;
5403 flow
->dl_dst
= eth
->eth_dst
;
5407 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5408 struct ofpbuf
*odp_actions
,
5409 struct flow_wildcards
*wc
,
5412 struct ovs_key_ethernet key
, base
, mask
;
5414 get_ethernet_key(flow
, &key
);
5415 get_ethernet_key(base_flow
, &base
);
5416 get_ethernet_key(&wc
->masks
, &mask
);
5418 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5419 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5420 put_ethernet_key(&base
, base_flow
);
5421 put_ethernet_key(&mask
, &wc
->masks
);
5426 pop_vlan(struct flow
*base
,
5427 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5429 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5431 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5432 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5438 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5439 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5441 if (base
->vlan_tci
== vlan_tci
) {
5445 pop_vlan(base
, odp_actions
, wc
);
5446 if (vlan_tci
& htons(VLAN_CFI
)) {
5447 struct ovs_action_push_vlan vlan
;
5449 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5450 vlan
.vlan_tci
= vlan_tci
;
5451 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5452 &vlan
, sizeof vlan
);
5454 base
->vlan_tci
= vlan_tci
;
5457 /* Wildcarding already done at action translation time. */
5459 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5460 struct ofpbuf
*odp_actions
)
5462 int base_n
= flow_count_mpls_labels(base
, NULL
);
5463 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5464 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5467 while (base_n
> common_n
) {
5468 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5469 /* If there is only one more LSE in base than there are common
5470 * between base and flow; and flow has at least one more LSE than
5471 * is common then the topmost LSE of base may be updated using
5473 struct ovs_key_mpls mpls_key
;
5475 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5476 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5477 &mpls_key
, sizeof mpls_key
);
5478 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5481 /* Otherwise, if there more LSEs in base than are common between
5482 * base and flow then pop the topmost one. */
5486 /* If all the LSEs are to be popped and this is not the outermost
5487 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5488 * POP_MPLS action instead of flow->dl_type.
5490 * This is because the POP_MPLS action requires its ethertype
5491 * argument to be an MPLS ethernet type but in this case
5492 * flow->dl_type will be a non-MPLS ethernet type.
5494 * When the final POP_MPLS action occurs it use flow->dl_type and
5495 * the and the resulting packet will have the desired dl_type. */
5496 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5497 dl_type
= htons(ETH_TYPE_MPLS
);
5499 dl_type
= flow
->dl_type
;
5501 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5502 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5508 /* If, after the above popping and setting, there are more LSEs in flow
5509 * than base then some LSEs need to be pushed. */
5510 while (base_n
< flow_n
) {
5511 struct ovs_action_push_mpls
*mpls
;
5513 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5514 OVS_ACTION_ATTR_PUSH_MPLS
,
5516 mpls
->mpls_ethertype
= flow
->dl_type
;
5517 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5518 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
);
5519 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5525 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5527 ipv4
->ipv4_src
= flow
->nw_src
;
5528 ipv4
->ipv4_dst
= flow
->nw_dst
;
5529 ipv4
->ipv4_proto
= flow
->nw_proto
;
5530 ipv4
->ipv4_tos
= flow
->nw_tos
;
5531 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5532 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5536 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5538 flow
->nw_src
= ipv4
->ipv4_src
;
5539 flow
->nw_dst
= ipv4
->ipv4_dst
;
5540 flow
->nw_proto
= ipv4
->ipv4_proto
;
5541 flow
->nw_tos
= ipv4
->ipv4_tos
;
5542 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5543 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5547 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5548 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5551 struct ovs_key_ipv4 key
, mask
, base
;
5553 /* Check that nw_proto and nw_frag remain unchanged. */
5554 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5555 flow
->nw_frag
== base_flow
->nw_frag
);
5557 get_ipv4_key(flow
, &key
, false);
5558 get_ipv4_key(base_flow
, &base
, false);
5559 get_ipv4_key(&wc
->masks
, &mask
, true);
5560 mask
.ipv4_proto
= 0; /* Not writeable. */
5561 mask
.ipv4_frag
= 0; /* Not writable. */
5563 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5565 put_ipv4_key(&base
, base_flow
, false);
5566 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5567 put_ipv4_key(&mask
, &wc
->masks
, true);
5573 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5575 memcpy(ipv6
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6
->ipv6_src
);
5576 memcpy(ipv6
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6
->ipv6_dst
);
5577 ipv6
->ipv6_label
= flow
->ipv6_label
;
5578 ipv6
->ipv6_proto
= flow
->nw_proto
;
5579 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5580 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5581 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5585 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5587 memcpy(&flow
->ipv6_src
, ipv6
->ipv6_src
, sizeof flow
->ipv6_src
);
5588 memcpy(&flow
->ipv6_dst
, ipv6
->ipv6_dst
, sizeof flow
->ipv6_dst
);
5589 flow
->ipv6_label
= ipv6
->ipv6_label
;
5590 flow
->nw_proto
= ipv6
->ipv6_proto
;
5591 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5592 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5593 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5597 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5598 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5601 struct ovs_key_ipv6 key
, mask
, base
;
5603 /* Check that nw_proto and nw_frag remain unchanged. */
5604 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5605 flow
->nw_frag
== base_flow
->nw_frag
);
5607 get_ipv6_key(flow
, &key
, false);
5608 get_ipv6_key(base_flow
, &base
, false);
5609 get_ipv6_key(&wc
->masks
, &mask
, true);
5610 mask
.ipv6_proto
= 0; /* Not writeable. */
5611 mask
.ipv6_frag
= 0; /* Not writable. */
5613 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5615 put_ipv6_key(&base
, base_flow
, false);
5616 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5617 put_ipv6_key(&mask
, &wc
->masks
, true);
5623 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5625 /* ARP key has padding, clear it. */
5626 memset(arp
, 0, sizeof *arp
);
5628 arp
->arp_sip
= flow
->nw_src
;
5629 arp
->arp_tip
= flow
->nw_dst
;
5630 arp
->arp_op
= htons(flow
->nw_proto
);
5631 arp
->arp_sha
= flow
->arp_sha
;
5632 arp
->arp_tha
= flow
->arp_tha
;
5636 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5638 flow
->nw_src
= arp
->arp_sip
;
5639 flow
->nw_dst
= arp
->arp_tip
;
5640 flow
->nw_proto
= ntohs(arp
->arp_op
);
5641 flow
->arp_sha
= arp
->arp_sha
;
5642 flow
->arp_tha
= arp
->arp_tha
;
5645 static enum slow_path_reason
5646 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5647 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5649 struct ovs_key_arp key
, mask
, base
;
5651 get_arp_key(flow
, &key
);
5652 get_arp_key(base_flow
, &base
);
5653 get_arp_key(&wc
->masks
, &mask
);
5655 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5657 put_arp_key(&base
, base_flow
);
5658 put_arp_key(&mask
, &wc
->masks
);
5665 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5667 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5668 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5669 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5673 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5675 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5676 flow
->tp_src
= htons(icmp
->icmp_type
);
5677 flow
->tp_dst
= htons(icmp
->icmp_code
);
5680 static enum slow_path_reason
5681 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5682 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5684 struct ovs_key_icmp key
, mask
, base
;
5685 enum ovs_key_attr attr
;
5687 if (is_icmpv4(flow
)) {
5688 attr
= OVS_KEY_ATTR_ICMP
;
5689 } else if (is_icmpv6(flow
)) {
5690 attr
= OVS_KEY_ATTR_ICMPV6
;
5695 get_icmp_key(flow
, &key
);
5696 get_icmp_key(base_flow
, &base
);
5697 get_icmp_key(&wc
->masks
, &mask
);
5699 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5700 put_icmp_key(&base
, base_flow
);
5701 put_icmp_key(&mask
, &wc
->masks
);
5708 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5710 memcpy(nd
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
5711 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5712 nd
->nd_sll
= flow
->arp_sha
;
5713 nd
->nd_tll
= flow
->arp_tha
;
5717 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5719 memcpy(&flow
->nd_target
, nd
->nd_target
, sizeof flow
->nd_target
);
5720 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5721 flow
->arp_sha
= nd
->nd_sll
;
5722 flow
->arp_tha
= nd
->nd_tll
;
5725 static enum slow_path_reason
5726 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5727 struct ofpbuf
*odp_actions
,
5728 struct flow_wildcards
*wc
, bool use_masked
)
5730 struct ovs_key_nd key
, mask
, base
;
5732 get_nd_key(flow
, &key
);
5733 get_nd_key(base_flow
, &base
);
5734 get_nd_key(&wc
->masks
, &mask
);
5736 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5738 put_nd_key(&base
, base_flow
);
5739 put_nd_key(&mask
, &wc
->masks
);
5746 static enum slow_path_reason
5747 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5748 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5751 /* Check if 'flow' really has an L3 header. */
5752 if (!flow
->nw_proto
) {
5756 switch (ntohs(base
->dl_type
)) {
5758 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5762 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5763 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5766 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5772 /* TCP, UDP, and SCTP keys have the same layout. */
5773 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5774 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5777 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5779 tp
->tcp
.tcp_src
= flow
->tp_src
;
5780 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5784 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5786 flow
->tp_src
= tp
->tcp
.tcp_src
;
5787 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5791 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5792 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5795 enum ovs_key_attr key_type
;
5796 union ovs_key_tp key
, mask
, base
;
5798 /* Check if 'flow' really has an L3 header. */
5799 if (!flow
->nw_proto
) {
5803 if (!is_ip_any(base_flow
)) {
5807 if (flow
->nw_proto
== IPPROTO_TCP
) {
5808 key_type
= OVS_KEY_ATTR_TCP
;
5809 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5810 key_type
= OVS_KEY_ATTR_UDP
;
5811 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5812 key_type
= OVS_KEY_ATTR_SCTP
;
5817 get_tp_key(flow
, &key
);
5818 get_tp_key(base_flow
, &base
);
5819 get_tp_key(&wc
->masks
, &mask
);
5821 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5823 put_tp_key(&base
, base_flow
);
5824 put_tp_key(&mask
, &wc
->masks
);
5829 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5830 struct ofpbuf
*odp_actions
,
5831 struct flow_wildcards
*wc
,
5834 uint32_t key
, mask
, base
;
5836 key
= flow
->skb_priority
;
5837 base
= base_flow
->skb_priority
;
5838 mask
= wc
->masks
.skb_priority
;
5840 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5841 sizeof key
, odp_actions
)) {
5842 base_flow
->skb_priority
= base
;
5843 wc
->masks
.skb_priority
= mask
;
5848 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5849 struct ofpbuf
*odp_actions
,
5850 struct flow_wildcards
*wc
,
5853 uint32_t key
, mask
, base
;
5855 key
= flow
->pkt_mark
;
5856 base
= base_flow
->pkt_mark
;
5857 mask
= wc
->masks
.pkt_mark
;
5859 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5860 sizeof key
, odp_actions
)) {
5861 base_flow
->pkt_mark
= base
;
5862 wc
->masks
.pkt_mark
= mask
;
5866 /* If any of the flow key data that ODP actions can modify are different in
5867 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5868 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5869 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5870 * in addition to this function if needed. Sets fields in 'wc' that are
5871 * used as part of the action.
5873 * Returns a reason to force processing the flow's packets into the userspace
5874 * slow path, if there is one, otherwise 0. */
5875 enum slow_path_reason
5876 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5877 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5880 enum slow_path_reason slow1
, slow2
;
5882 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5883 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5884 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5885 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5886 commit_mpls_action(flow
, base
, odp_actions
);
5887 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5888 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5889 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5891 return slow1
? slow1
: slow2
;