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;
913 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
919 uint32_t probability
;
920 uint32_t collector_set_id
;
921 uint32_t obs_domain_id
;
922 uint32_t obs_point_id
;
925 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
926 "pcp=%i,output=%"SCNi32
")%n",
927 &vid
, &pcp
, &output
, &n1
)) {
931 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
936 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
937 cookie
.sflow
.vlan_tci
= htons(tci
);
938 cookie
.sflow
.output
= output
;
940 user_data_size
= sizeof cookie
.sflow
;
941 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
946 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
947 cookie
.slow_path
.unused
= 0;
948 cookie
.slow_path
.reason
= 0;
950 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
951 &cookie
.slow_path
.reason
,
952 SLOW_PATH_REASON_MASK
, NULL
);
953 if (res
< 0 || s
[n
+ res
] != ')') {
959 user_data_size
= sizeof cookie
.slow_path
;
960 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
961 "collector_set_id=%"SCNi32
","
962 "obs_domain_id=%"SCNi32
","
963 "obs_point_id=%"SCNi32
")%n",
964 &probability
, &collector_set_id
,
965 &obs_domain_id
, &obs_point_id
, &n1
)) {
968 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
969 cookie
.flow_sample
.probability
= probability
;
970 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
971 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
972 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
974 user_data_size
= sizeof cookie
.flow_sample
;
975 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
978 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
979 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
981 user_data_size
= sizeof cookie
.ipfix
;
982 } else if (ovs_scan(&s
[n
], ",userdata(%n",
987 ofpbuf_init(&buf
, 16);
988 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
992 user_data
= buf
.data
;
993 user_data_size
= buf
.size
;
1000 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1002 include_actions
= true;
1008 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1009 &tunnel_out_port
, &n1
)) {
1010 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1011 tunnel_out_port
, include_actions
, actions
);
1013 } else if (s
[n
] == ')') {
1014 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1015 ODPP_NONE
, include_actions
, actions
);
1024 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1026 struct eth_header
*eth
;
1027 struct ip_header
*ip
;
1028 struct ovs_16aligned_ip6_hdr
*ip6
;
1029 struct udp_header
*udp
;
1030 struct gre_base_hdr
*greh
;
1031 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1033 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1037 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1040 eth
= (struct eth_header
*) data
->header
;
1041 l3
= (data
->header
+ sizeof *eth
);
1042 ip
= (struct ip_header
*) l3
;
1043 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1044 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1045 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1048 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1052 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1053 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1056 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1059 eth
->eth_type
= htons(dl_type
);
1061 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1063 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1064 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1067 &ip
->ip_proto
, &ip
->ip_tos
,
1068 &ip
->ip_ttl
, &ip
->ip_frag_off
)) {
1071 put_16aligned_be32(&ip
->ip_src
, sip
);
1072 put_16aligned_be32(&ip
->ip_dst
, dip
);
1073 ip_len
= sizeof *ip
;
1075 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1076 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1077 struct in6_addr sip6
, dip6
;
1080 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1081 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1082 ",hlimit=%"SCNi8
"),",
1083 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1084 &tclass
, &ip6
->ip6_hlim
)
1085 || (label
& ~IPV6_LABEL_MASK
) != 0
1086 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1087 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1090 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1091 htonl(tclass
<< 20) | htonl(label
));
1092 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1093 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1094 ip_len
= sizeof *ip6
;
1098 l4
= ((uint8_t *) l3
+ ip_len
);
1099 udp
= (struct udp_header
*) l4
;
1100 greh
= (struct gre_base_hdr
*) l4
;
1101 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1102 &udp_src
, &udp_dst
, &csum
)) {
1103 uint32_t vx_flags
, vni
;
1105 udp
->udp_src
= htons(udp_src
);
1106 udp
->udp_dst
= htons(udp_dst
);
1108 udp
->udp_csum
= htons(csum
);
1110 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1112 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1114 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1115 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1116 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1117 header_len
= sizeof *eth
+ ip_len
+
1118 sizeof *udp
+ sizeof *vxh
;
1119 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1120 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1122 memset(gnh
, 0, sizeof *gnh
);
1123 header_len
= sizeof *eth
+ ip_len
+
1124 sizeof *udp
+ sizeof *gnh
;
1126 if (ovs_scan_len(s
, &n
, "oam,")) {
1129 if (ovs_scan_len(s
, &n
, "crit,")) {
1132 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1135 if (ovs_scan_len(s
, &n
, ",options(")) {
1136 struct geneve_scan options
;
1139 memset(&options
, 0, sizeof options
);
1140 len
= scan_geneve(s
+ n
, &options
, NULL
);
1145 memcpy(gnh
->options
, options
.d
, options
.len
);
1146 gnh
->opt_len
= options
.len
/ 4;
1147 header_len
+= options
.len
;
1151 if (!ovs_scan_len(s
, &n
, "))")) {
1155 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1156 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1157 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1161 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1162 &gre_flags
, &gre_proto
)){
1164 tnl_type
= OVS_VPORT_TYPE_GRE
;
1165 greh
->flags
= htons(gre_flags
);
1166 greh
->protocol
= htons(gre_proto
);
1167 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1169 if (greh
->flags
& htons(GRE_CSUM
)) {
1170 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1174 memset(options
, 0, sizeof *options
);
1175 *((ovs_be16
*)options
) = htons(csum
);
1178 if (greh
->flags
& htons(GRE_KEY
)) {
1181 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1185 put_16aligned_be32(options
, htonl(key
));
1188 if (greh
->flags
& htons(GRE_SEQ
)) {
1191 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1194 put_16aligned_be32(options
, htonl(seq
));
1198 if (!ovs_scan_len(s
, &n
, "))")) {
1202 header_len
= sizeof *eth
+ ip_len
+
1203 ((uint8_t *) options
- (uint8_t *) greh
);
1208 /* check tunnel meta data. */
1209 if (data
->tnl_type
!= tnl_type
) {
1212 if (data
->header_len
!= header_len
) {
1217 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1224 struct ct_nat_params
{
1230 struct in6_addr ip6
;
1234 struct in6_addr ip6
;
1244 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1246 if (ovs_scan_len(s
, n
, "=")) {
1247 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1248 struct in6_addr ipv6
;
1250 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1251 p
->addr_len
= sizeof p
->addr_min
.ip
;
1252 if (ovs_scan_len(s
, n
, "-")) {
1253 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1254 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1258 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1259 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1260 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1261 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1262 p
->addr_min
.ip6
= ipv6
;
1263 if (ovs_scan_len(s
, n
, "-")) {
1264 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1265 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1266 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1267 p
->addr_max
.ip6
= ipv6
;
1275 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1276 if (ovs_scan_len(s
, n
, "-")) {
1277 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1287 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1291 if (ovs_scan_len(s
, &n
, "nat")) {
1292 memset(p
, 0, sizeof *p
);
1294 if (ovs_scan_len(s
, &n
, "(")) {
1298 end
= strchr(s
+ n
, ')');
1305 n
+= strspn(s
+ n
, delimiters
);
1306 if (ovs_scan_len(s
, &n
, "src")) {
1307 int err
= scan_ct_nat_range(s
, &n
, p
);
1314 if (ovs_scan_len(s
, &n
, "dst")) {
1315 int err
= scan_ct_nat_range(s
, &n
, p
);
1322 if (ovs_scan_len(s
, &n
, "persistent")) {
1323 p
->persistent
= true;
1326 if (ovs_scan_len(s
, &n
, "hash")) {
1327 p
->proto_hash
= true;
1330 if (ovs_scan_len(s
, &n
, "random")) {
1331 p
->proto_random
= true;
1337 if (p
->snat
&& p
->dnat
) {
1340 if ((p
->addr_len
!= 0 &&
1341 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1342 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1343 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1346 if (p
->proto_hash
&& p
->proto_random
) {
1356 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1358 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1361 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1362 } else if (p
->dnat
) {
1363 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1367 if (p
->addr_len
!= 0) {
1368 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1370 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1371 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1375 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1376 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1377 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1380 if (p
->persistent
) {
1381 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1383 if (p
->proto_hash
) {
1384 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1386 if (p
->proto_random
) {
1387 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1391 nl_msg_end_nested(actions
, start
);
1395 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1399 if (ovs_scan(s
, "ct")) {
1400 const char *helper
= NULL
;
1401 size_t helper_len
= 0;
1402 bool commit
= false;
1407 } ct_mark
= { 0, 0 };
1412 struct ct_nat_params nat_params
;
1413 bool have_nat
= false;
1417 memset(&ct_label
, 0, sizeof(ct_label
));
1420 if (ovs_scan(s
, "(")) {
1423 end
= strchr(s
, ')');
1431 s
+= strspn(s
, delimiters
);
1432 if (ovs_scan(s
, "commit%n", &n
)) {
1437 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1441 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1444 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1447 ct_mark
.mask
= UINT32_MAX
;
1451 if (ovs_scan(s
, "label=%n", &n
)) {
1455 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1462 if (ovs_scan(s
, "helper=%n", &n
)) {
1464 helper_len
= strcspn(s
, delimiters_end
);
1465 if (!helper_len
|| helper_len
> 15) {
1473 n
= scan_ct_nat(s
, &nat_params
);
1478 /* end points to the end of the nested, nat action.
1479 * find the real end. */
1482 /* Nothing matched. */
1488 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1490 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1493 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1496 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1499 if (!ovs_u128_is_zero(&ct_label
.mask
)) {
1500 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1504 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1508 nl_msg_put_ct_nat(&nat_params
, actions
);
1510 nl_msg_end_nested(actions
, start
);
1517 parse_odp_action(const char *s
, const struct simap
*port_names
,
1518 struct ofpbuf
*actions
)
1524 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1525 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1531 int len
= strcspn(s
, delimiters
);
1532 struct simap_node
*node
;
1534 node
= simap_find_len(port_names
, s
, len
);
1536 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1545 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1546 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1551 if (!strncmp(s
, "userspace(", 10)) {
1552 return parse_odp_userspace_action(s
, actions
);
1555 if (!strncmp(s
, "set(", 4)) {
1558 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1559 struct ofpbuf maskbuf
;
1560 struct nlattr
*nested
, *key
;
1563 /* 'mask' is big enough to hold any key. */
1564 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1566 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1567 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1571 if (s
[retval
+ 4] != ')') {
1575 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1578 size
= nl_attr_get_size(mask
);
1579 if (size
== nl_attr_get_size(key
)) {
1580 /* Change to masked set action if not fully masked. */
1581 if (!is_all_ones(mask
+ 1, size
)) {
1582 key
->nla_len
+= size
;
1583 ofpbuf_put(actions
, mask
+ 1, size
);
1584 /* 'actions' may have been reallocated by ofpbuf_put(). */
1585 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1586 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
1590 nl_msg_end_nested(actions
, start_ofs
);
1595 struct ovs_action_push_vlan push
;
1596 int tpid
= ETH_TYPE_VLAN
;
1601 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
1602 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
1603 &vid
, &pcp
, &cfi
, &n
)
1604 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
1605 &tpid
, &vid
, &pcp
, &n
)
1606 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
1607 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
1608 push
.vlan_tpid
= htons(tpid
);
1609 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
1610 | (pcp
<< VLAN_PCP_SHIFT
)
1611 | (cfi
? VLAN_CFI
: 0));
1612 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
1613 &push
, sizeof push
);
1619 if (!strncmp(s
, "pop_vlan", 8)) {
1620 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
1628 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
1629 && percentage
>= 0. && percentage
<= 100.0) {
1630 size_t sample_ofs
, actions_ofs
;
1633 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
1634 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
1635 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
1636 (probability
<= 0 ? 0
1637 : probability
>= UINT32_MAX
? UINT32_MAX
1640 actions_ofs
= nl_msg_start_nested(actions
,
1641 OVS_SAMPLE_ATTR_ACTIONS
);
1645 n
+= strspn(s
+ n
, delimiters
);
1650 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1656 nl_msg_end_nested(actions
, actions_ofs
);
1657 nl_msg_end_nested(actions
, sample_ofs
);
1659 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
1667 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
1668 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
1676 retval
= parse_conntrack_action(s
, actions
);
1683 struct ovs_action_push_tnl data
;
1686 n
= ovs_parse_tnl_push(s
, &data
);
1688 odp_put_tnl_push_action(actions
, &data
);
1697 /* Parses the string representation of datapath actions, in the format output
1698 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
1699 * value. On success, the ODP actions are appended to 'actions' as a series of
1700 * Netlink attributes. On failure, no data is appended to 'actions'. Either
1701 * way, 'actions''s data might be reallocated. */
1703 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
1704 struct ofpbuf
*actions
)
1708 if (!strcasecmp(s
, "drop")) {
1712 old_size
= actions
->size
;
1716 s
+= strspn(s
, delimiters
);
1721 retval
= parse_odp_action(s
, port_names
, actions
);
1722 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
1723 actions
->size
= old_size
;
1732 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
1733 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
1736 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
1737 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
1738 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
1739 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
1740 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
1741 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
1742 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
1743 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
1744 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
1745 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
1746 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
1747 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
1748 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
1749 .next
= ovs_vxlan_ext_attr_lens
,
1750 .next_max
= OVS_VXLAN_EXT_MAX
},
1751 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
1752 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
1755 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
1756 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
1757 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
1758 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
1759 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
1760 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
1761 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
1762 .next
= ovs_tun_key_attr_lens
,
1763 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
1764 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
1765 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
1766 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
1767 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
1768 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
1769 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
1770 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
1771 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
1772 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
1773 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
1774 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
1775 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
1776 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
1777 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
1778 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
1779 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
1780 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
1781 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
1782 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
1785 /* Returns the correct length of the payload for a flow key attribute of the
1786 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
1787 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
1788 * payload is a nested type. */
1790 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_len
, uint16_t type
)
1792 if (type
> max_len
) {
1793 return ATTR_LEN_INVALID
;
1796 return tbl
[type
].len
;
1800 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
1802 size_t len
= nl_attr_get_size(a
);
1804 const uint8_t *unspec
;
1807 unspec
= nl_attr_get(a
);
1808 for (i
= 0; i
< len
; i
++) {
1810 ds_put_char(ds
, ' ');
1812 ds_put_format(ds
, "%02x", unspec
[i
]);
1818 ovs_frag_type_to_string(enum ovs_frag_type type
)
1821 case OVS_FRAG_TYPE_NONE
:
1823 case OVS_FRAG_TYPE_FIRST
:
1825 case OVS_FRAG_TYPE_LATER
:
1827 case __OVS_FRAG_TYPE_MAX
:
1833 static enum odp_key_fitness
1834 odp_tun_key_from_attr__(const struct nlattr
*attr
,
1835 const struct nlattr
*flow_attrs
, size_t flow_attr_len
,
1836 const struct flow_tnl
*src_tun
, struct flow_tnl
*tun
,
1840 const struct nlattr
*a
;
1842 bool unknown
= false;
1844 NL_NESTED_FOR_EACH(a
, left
, attr
) {
1845 uint16_t type
= nl_attr_type(a
);
1846 size_t len
= nl_attr_get_size(a
);
1847 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
1848 OVS_TUNNEL_ATTR_MAX
, type
);
1850 if (len
!= expected_len
&& expected_len
>= 0) {
1851 return ODP_FIT_ERROR
;
1855 case OVS_TUNNEL_KEY_ATTR_ID
:
1856 tun
->tun_id
= nl_attr_get_be64(a
);
1857 tun
->flags
|= FLOW_TNL_F_KEY
;
1859 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
1860 tun
->ip_src
= nl_attr_get_be32(a
);
1862 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
1863 tun
->ip_dst
= nl_attr_get_be32(a
);
1865 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
1866 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
1868 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
1869 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
1871 case OVS_TUNNEL_KEY_ATTR_TOS
:
1872 tun
->ip_tos
= nl_attr_get_u8(a
);
1874 case OVS_TUNNEL_KEY_ATTR_TTL
:
1875 tun
->ip_ttl
= nl_attr_get_u8(a
);
1878 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
1879 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
1881 case OVS_TUNNEL_KEY_ATTR_CSUM
:
1882 tun
->flags
|= FLOW_TNL_F_CSUM
;
1884 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
1885 tun
->tp_src
= nl_attr_get_be16(a
);
1887 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
1888 tun
->tp_dst
= nl_attr_get_be16(a
);
1890 case OVS_TUNNEL_KEY_ATTR_OAM
:
1891 tun
->flags
|= FLOW_TNL_F_OAM
;
1893 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
1894 static const struct nl_policy vxlan_opts_policy
[] = {
1895 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
1897 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
1899 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
1900 return ODP_FIT_ERROR
;
1903 if (ext
[OVS_VXLAN_EXT_GBP
]) {
1904 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
1906 tun
->gbp_id
= htons(gbp
& 0xFFFF);
1907 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
1912 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1913 if (tun_metadata_from_geneve_nlattr(a
, flow_attrs
, flow_attr_len
,
1914 src_tun
, udpif
, tun
)) {
1915 return ODP_FIT_ERROR
;
1920 /* Allow this to show up as unexpected, if there are unknown
1921 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
1928 return ODP_FIT_ERROR
;
1931 return ODP_FIT_TOO_MUCH
;
1933 return ODP_FIT_PERFECT
;
1936 enum odp_key_fitness
1937 odp_tun_key_from_attr(const struct nlattr
*attr
, bool udpif
,
1938 struct flow_tnl
*tun
)
1940 memset(tun
, 0, sizeof *tun
);
1941 return odp_tun_key_from_attr__(attr
, NULL
, 0, NULL
, tun
, udpif
);
1945 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
1946 const struct flow_tnl
*tun_flow_key
,
1947 const struct ofpbuf
*key_buf
)
1951 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
1953 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
1954 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
1955 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
1957 if (tun_key
->ip_src
) {
1958 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
1960 if (tun_key
->ip_dst
) {
1961 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
1963 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
1964 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
1966 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
1967 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
1969 if (tun_key
->ip_tos
) {
1970 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
1972 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
1973 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
1974 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
1976 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
1977 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
1979 if (tun_key
->tp_src
) {
1980 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
1982 if (tun_key
->tp_dst
) {
1983 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
1985 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
1986 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
1988 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
1989 size_t vxlan_opts_ofs
;
1991 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
1992 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
1993 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
1994 nl_msg_end_nested(a
, vxlan_opts_ofs
);
1996 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
1998 nl_msg_end_nested(a
, tun_key_ofs
);
2002 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2004 return is_all_zeros(nl_attr_get(ma
), nl_attr_get_size(ma
));
2008 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2010 if (attr
== OVS_KEY_ATTR_TCP_FLAGS
) {
2011 return TCP_FLAGS(*(ovs_be16
*)mask
) == TCP_FLAGS(OVS_BE16_MAX
);
2013 if (attr
== OVS_KEY_ATTR_IPV6
) {
2014 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2017 ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2018 == htonl(IPV6_LABEL_MASK
))
2019 && ipv6_mask
->ipv6_proto
== UINT8_MAX
2020 && ipv6_mask
->ipv6_tclass
== UINT8_MAX
2021 && ipv6_mask
->ipv6_hlimit
== UINT8_MAX
2022 && ipv6_mask
->ipv6_frag
== UINT8_MAX
2023 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_src
)
2024 && ipv6_mask_is_exact((const struct in6_addr
*)ipv6_mask
->ipv6_dst
);
2026 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2030 if (attr
== OVS_KEY_ATTR_ARP
) {
2031 /* ARP key has padding, ignore it. */
2032 BUILD_ASSERT_DECL(sizeof(struct ovs_key_arp
) == 24);
2033 BUILD_ASSERT_DECL(offsetof(struct ovs_key_arp
, arp_tha
) == 10 + 6);
2034 size
= offsetof(struct ovs_key_arp
, arp_tha
) + ETH_ADDR_LEN
;
2035 ovs_assert(((uint16_t *)mask
)[size
/2] == 0);
2038 return is_all_ones(mask
, size
);
2042 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2044 enum ovs_key_attr attr
= nl_attr_type(ma
);
2048 if (attr
== OVS_KEY_ATTR_TUNNEL
) {
2051 mask
= nl_attr_get(ma
);
2052 size
= nl_attr_get_size(ma
);
2055 return odp_mask_is_exact(attr
, mask
, size
);
2059 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2062 struct odp_portno_names
*odp_portno_names
;
2064 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2065 odp_portno_names
->port_no
= port_no
;
2066 odp_portno_names
->name
= xstrdup(port_name
);
2067 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2068 hash_odp_port(port_no
));
2072 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2074 struct odp_portno_names
*odp_portno_names
;
2076 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2077 hash_odp_port(port_no
), portno_names
) {
2078 if (odp_portno_names
->port_no
== port_no
) {
2079 return odp_portno_names
->name
;
2086 odp_portno_names_destroy(struct hmap
*portno_names
)
2088 struct odp_portno_names
*odp_portno_names
, *odp_portno_names_next
;
2089 HMAP_FOR_EACH_SAFE (odp_portno_names
, odp_portno_names_next
,
2090 hmap_node
, portno_names
) {
2091 hmap_remove(portno_names
, &odp_portno_names
->hmap_node
);
2092 free(odp_portno_names
->name
);
2093 free(odp_portno_names
);
2097 /* Format helpers. */
2100 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2101 const struct eth_addr
*mask
, bool verbose
)
2103 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2105 if (verbose
|| !mask_empty
) {
2106 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2109 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2111 ds_put_format(ds
, "%s=", name
);
2112 eth_format_masked(key
, mask
, ds
);
2113 ds_put_char(ds
, ',');
2119 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2120 const ovs_be64
*mask
, bool verbose
)
2122 bool mask_empty
= mask
&& !*mask
;
2124 if (verbose
|| !mask_empty
) {
2125 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2127 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2128 if (!mask_full
) { /* Partially masked. */
2129 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2131 ds_put_char(ds
, ',');
2136 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2137 const ovs_be32
*mask
, bool verbose
)
2139 bool mask_empty
= mask
&& !*mask
;
2141 if (verbose
|| !mask_empty
) {
2142 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2144 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2145 if (!mask_full
) { /* Partially masked. */
2146 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2148 ds_put_char(ds
, ',');
2153 format_in6_addr(struct ds
*ds
, const char *name
,
2154 const struct in6_addr
*key
,
2155 const struct in6_addr
*mask
,
2158 char buf
[INET6_ADDRSTRLEN
];
2159 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2161 if (verbose
|| !mask_empty
) {
2162 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2164 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2165 ds_put_format(ds
, "%s=%s", name
, buf
);
2166 if (!mask_full
) { /* Partially masked. */
2167 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2168 ds_put_format(ds
, "/%s", buf
);
2170 ds_put_char(ds
, ',');
2175 format_ipv6(struct ds
*ds
, const char *name
, const ovs_be32 key_
[4],
2176 const ovs_be32 (*mask_
)[4], bool verbose
)
2178 format_in6_addr(ds
, name
,
2179 (const struct in6_addr
*)key_
,
2180 mask_
? (const struct in6_addr
*)*mask_
: NULL
,
2185 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2186 const ovs_be32
*mask
, bool verbose
)
2188 bool mask_empty
= mask
&& !*mask
;
2190 if (verbose
|| !mask_empty
) {
2191 bool mask_full
= !mask
2192 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2194 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2195 if (!mask_full
) { /* Partially masked. */
2196 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2198 ds_put_char(ds
, ',');
2203 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2204 const uint8_t *mask
, bool verbose
)
2206 bool mask_empty
= mask
&& !*mask
;
2208 if (verbose
|| !mask_empty
) {
2209 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2211 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2212 if (!mask_full
) { /* Partially masked. */
2213 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2215 ds_put_char(ds
, ',');
2220 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2221 const uint8_t *mask
, bool verbose
)
2223 bool mask_empty
= mask
&& !*mask
;
2225 if (verbose
|| !mask_empty
) {
2226 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2228 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2229 if (!mask_full
) { /* Partially masked. */
2230 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2232 ds_put_char(ds
, ',');
2237 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2238 const ovs_be16
*mask
, bool verbose
)
2240 bool mask_empty
= mask
&& !*mask
;
2242 if (verbose
|| !mask_empty
) {
2243 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2245 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2246 if (!mask_full
) { /* Partially masked. */
2247 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2249 ds_put_char(ds
, ',');
2254 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2255 const ovs_be16
*mask
, bool verbose
)
2257 bool mask_empty
= mask
&& !*mask
;
2259 if (verbose
|| !mask_empty
) {
2260 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2262 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2263 if (!mask_full
) { /* Partially masked. */
2264 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2266 ds_put_char(ds
, ',');
2271 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2272 const uint16_t *mask
, bool verbose
)
2274 bool mask_empty
= mask
&& !*mask
;
2276 if (verbose
|| !mask_empty
) {
2277 ds_put_cstr(ds
, name
);
2278 ds_put_char(ds
, '(');
2280 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2281 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2282 } else { /* Fully masked. */
2283 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2285 ds_put_cstr(ds
, "),");
2290 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2291 const struct attr_len_tbl tbl
[], int max_len
, bool need_key
)
2295 expected_len
= odp_key_attr_len(tbl
, max_len
, nl_attr_type(a
));
2296 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2297 expected_len
!= ATTR_LEN_NESTED
) {
2299 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2300 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2302 if (bad_key_len
|| bad_mask_len
) {
2304 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2307 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2308 nl_attr_get_size(a
), expected_len
);
2310 format_generic_odp_key(a
, ds
);
2312 ds_put_char(ds
, '/');
2314 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2315 nl_attr_get_size(ma
), expected_len
);
2317 format_generic_odp_key(ma
, ds
);
2319 ds_put_char(ds
, ')');
2328 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2329 const struct nlattr
*ma
)
2331 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2332 format_generic_odp_key(a
, ds
);
2333 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2334 ds_put_char(ds
, '/');
2335 format_generic_odp_key(ma
, ds
);
2337 ds_put_cstr(ds
, "),");
2341 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2342 const struct nlattr
*mask_attr
, struct ds
*ds
,
2346 const struct nlattr
*a
;
2349 ofpbuf_init(&ofp
, 100);
2350 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2351 uint16_t type
= nl_attr_type(a
);
2352 const struct nlattr
*ma
= NULL
;
2355 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2356 nl_attr_get_size(mask_attr
), type
);
2358 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2364 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2365 OVS_VXLAN_EXT_MAX
, true)) {
2370 case OVS_VXLAN_EXT_GBP
: {
2371 uint32_t key
= nl_attr_get_u32(a
);
2372 ovs_be16 id
, id_mask
;
2373 uint8_t flags
, flags_mask
;
2375 id
= htons(key
& 0xFFFF);
2376 flags
= (key
>> 16) & 0xFF;
2378 uint32_t mask
= nl_attr_get_u32(ma
);
2379 id_mask
= htons(mask
& 0xFFFF);
2380 flags_mask
= (mask
>> 16) & 0xFF;
2383 ds_put_cstr(ds
, "gbp(");
2384 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2385 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2387 ds_put_cstr(ds
, "),");
2392 format_unknown_key(ds
, a
, ma
);
2398 ofpbuf_uninit(&ofp
);
2401 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2404 format_geneve_opts(const struct geneve_opt
*opt
,
2405 const struct geneve_opt
*mask
, int opts_len
,
2406 struct ds
*ds
, bool verbose
)
2408 while (opts_len
> 0) {
2410 uint8_t data_len
, data_len_mask
;
2412 if (opts_len
< sizeof *opt
) {
2413 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2414 opts_len
, sizeof *opt
);
2418 data_len
= opt
->length
* 4;
2420 if (mask
->length
== 0x1f) {
2421 data_len_mask
= UINT8_MAX
;
2423 data_len_mask
= mask
->length
;
2426 len
= sizeof *opt
+ data_len
;
2427 if (len
> opts_len
) {
2428 ds_put_format(ds
, "opt len %u greater than remaining %u",
2433 ds_put_char(ds
, '{');
2434 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2436 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2437 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2439 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2440 ds_put_hex(ds
, opt
+ 1, data_len
);
2441 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2442 ds_put_char(ds
, '/');
2443 ds_put_hex(ds
, mask
+ 1, data_len
);
2448 ds_put_char(ds
, '}');
2450 opt
+= len
/ sizeof(*opt
);
2452 mask
+= len
/ sizeof(*opt
);
2459 format_odp_tun_geneve(const struct nlattr
*attr
,
2460 const struct nlattr
*mask_attr
, struct ds
*ds
,
2463 int opts_len
= nl_attr_get_size(attr
);
2464 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2465 const struct geneve_opt
*mask
= mask_attr
?
2466 nl_attr_get(mask_attr
) : NULL
;
2468 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2469 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2470 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2474 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2478 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2479 struct ds
*ds
, bool verbose
)
2482 const struct nlattr
*a
;
2484 uint16_t mask_flags
= 0;
2487 ofpbuf_init(&ofp
, 100);
2488 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2489 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2490 const struct nlattr
*ma
= NULL
;
2493 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2494 nl_attr_get_size(mask_attr
), type
);
2496 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
2497 OVS_TUNNEL_KEY_ATTR_MAX
,
2502 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
2503 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
2508 case OVS_TUNNEL_KEY_ATTR_ID
:
2509 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
2510 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2511 flags
|= FLOW_TNL_F_KEY
;
2513 mask_flags
|= FLOW_TNL_F_KEY
;
2516 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2517 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
2518 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2520 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2521 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
2522 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2524 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
2525 struct in6_addr ipv6_src
;
2526 ipv6_src
= nl_attr_get_in6_addr(a
);
2527 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
2528 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2531 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
2532 struct in6_addr ipv6_dst
;
2533 ipv6_dst
= nl_attr_get_in6_addr(a
);
2534 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
2535 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2538 case OVS_TUNNEL_KEY_ATTR_TOS
:
2539 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
2540 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2542 case OVS_TUNNEL_KEY_ATTR_TTL
:
2543 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
2544 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2546 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2547 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2549 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2550 flags
|= FLOW_TNL_F_CSUM
;
2552 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2553 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
2554 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2556 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2557 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
2558 ma
? nl_attr_get(ma
) : NULL
, verbose
);
2560 case OVS_TUNNEL_KEY_ATTR_OAM
:
2561 flags
|= FLOW_TNL_F_OAM
;
2563 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2564 ds_put_cstr(ds
, "vxlan(");
2565 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
2566 ds_put_cstr(ds
, "),");
2568 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2569 ds_put_cstr(ds
, "geneve(");
2570 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
2571 ds_put_cstr(ds
, "),");
2573 case __OVS_TUNNEL_KEY_ATTR_MAX
:
2575 format_unknown_key(ds
, a
, ma
);
2580 /* Flags can have a valid mask even if the attribute is not set, so
2581 * we need to collect these separately. */
2583 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
2584 switch (nl_attr_type(a
)) {
2585 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2586 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2588 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2589 mask_flags
|= FLOW_TNL_F_CSUM
;
2591 case OVS_TUNNEL_KEY_ATTR_OAM
:
2592 mask_flags
|= FLOW_TNL_F_OAM
;
2598 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
2601 ofpbuf_uninit(&ofp
);
2605 odp_ct_state_to_string(uint32_t flag
)
2608 case OVS_CS_F_REPLY_DIR
:
2610 case OVS_CS_F_TRACKED
:
2614 case OVS_CS_F_ESTABLISHED
:
2616 case OVS_CS_F_RELATED
:
2618 case OVS_CS_F_INVALID
:
2620 case OVS_CS_F_SRC_NAT
:
2622 case OVS_CS_F_DST_NAT
:
2630 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
2631 const uint8_t *mask
, bool verbose
)
2633 bool mask_empty
= mask
&& !*mask
;
2635 /* ODP frag is an enumeration field; partial masks are not meaningful. */
2636 if (verbose
|| !mask_empty
) {
2637 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2639 if (!mask_full
) { /* Partially masked. */
2640 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
2643 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
2649 mask_empty(const struct nlattr
*ma
)
2657 mask
= nl_attr_get(ma
);
2658 n
= nl_attr_get_size(ma
);
2660 return is_all_zeros(mask
, n
);
2664 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
2665 const struct hmap
*portno_names
, struct ds
*ds
,
2668 enum ovs_key_attr attr
= nl_attr_type(a
);
2669 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
2672 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
2674 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
2676 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
2677 OVS_KEY_ATTR_MAX
, false)) {
2681 ds_put_char(ds
, '(');
2683 case OVS_KEY_ATTR_ENCAP
:
2684 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
2685 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
2686 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
2688 } else if (nl_attr_get_size(a
)) {
2689 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
2694 case OVS_KEY_ATTR_PRIORITY
:
2695 case OVS_KEY_ATTR_SKB_MARK
:
2696 case OVS_KEY_ATTR_DP_HASH
:
2697 case OVS_KEY_ATTR_RECIRC_ID
:
2698 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2700 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2704 case OVS_KEY_ATTR_CT_MARK
:
2705 if (verbose
|| !mask_empty(ma
)) {
2706 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2708 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2713 case OVS_KEY_ATTR_CT_STATE
:
2715 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
2717 ds_put_format(ds
, "/%#"PRIx32
,
2718 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
2720 } else if (!is_exact
) {
2721 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
2723 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
2726 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
2730 case OVS_KEY_ATTR_CT_ZONE
:
2731 if (verbose
|| !mask_empty(ma
)) {
2732 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
2734 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
2739 case OVS_KEY_ATTR_CT_LABELS
: {
2740 const ovs_u128
*value
= nl_attr_get(a
);
2741 const ovs_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2743 format_u128(ds
, value
, mask
, verbose
);
2747 case OVS_KEY_ATTR_TUNNEL
:
2748 format_odp_tun_attr(a
, ma
, ds
, verbose
);
2751 case OVS_KEY_ATTR_IN_PORT
:
2752 if (portno_names
&& verbose
&& is_exact
) {
2753 char *name
= odp_portno_names_get(portno_names
,
2754 u32_to_odp(nl_attr_get_u32(a
)));
2756 ds_put_format(ds
, "%s", name
);
2758 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2761 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
2763 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
2768 case OVS_KEY_ATTR_ETHERNET
: {
2769 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2770 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
2772 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
2773 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
2777 case OVS_KEY_ATTR_VLAN
:
2778 format_vlan_tci(ds
, nl_attr_get_be16(a
),
2779 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
2782 case OVS_KEY_ATTR_MPLS
: {
2783 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
2784 const struct ovs_key_mpls
*mpls_mask
= NULL
;
2785 size_t size
= nl_attr_get_size(a
);
2787 if (!size
|| size
% sizeof *mpls_key
) {
2788 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
2792 mpls_mask
= nl_attr_get(ma
);
2793 if (size
!= nl_attr_get_size(ma
)) {
2794 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
2795 "mask length %"PRIuSIZE
")",
2796 size
, nl_attr_get_size(ma
));
2800 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
2803 case OVS_KEY_ATTR_ETHERTYPE
:
2804 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
2806 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
2810 case OVS_KEY_ATTR_IPV4
: {
2811 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
2812 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2814 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
2815 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
2816 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
2818 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
2819 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
2820 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
2825 case OVS_KEY_ATTR_IPV6
: {
2826 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
2827 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2829 format_ipv6(ds
, "src", key
->ipv6_src
, MASK(mask
, ipv6_src
), verbose
);
2830 format_ipv6(ds
, "dst", key
->ipv6_dst
, MASK(mask
, ipv6_dst
), verbose
);
2831 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
2833 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
2835 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
2837 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
2839 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
2844 /* These have the same structure and format. */
2845 case OVS_KEY_ATTR_TCP
:
2846 case OVS_KEY_ATTR_UDP
:
2847 case OVS_KEY_ATTR_SCTP
: {
2848 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
2849 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2851 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
2852 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
2856 case OVS_KEY_ATTR_TCP_FLAGS
:
2858 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
2859 ntohs(nl_attr_get_be16(a
)),
2860 TCP_FLAGS(nl_attr_get_be16(ma
)),
2861 TCP_FLAGS(OVS_BE16_MAX
));
2863 format_flags(ds
, packet_tcp_flag_to_string
,
2864 ntohs(nl_attr_get_be16(a
)), '|');
2868 case OVS_KEY_ATTR_ICMP
: {
2869 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
2870 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2872 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
2873 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
2877 case OVS_KEY_ATTR_ICMPV6
: {
2878 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
2879 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2881 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
2883 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
2888 case OVS_KEY_ATTR_ARP
: {
2889 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2890 const struct ovs_key_arp
*key
= nl_attr_get(a
);
2892 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
2893 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
2894 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
2895 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
2896 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
2900 case OVS_KEY_ATTR_ND
: {
2901 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
2902 const struct ovs_key_nd
*key
= nl_attr_get(a
);
2904 format_ipv6(ds
, "target", key
->nd_target
, MASK(mask
, nd_target
),
2906 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
2907 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
2912 case OVS_KEY_ATTR_UNSPEC
:
2913 case __OVS_KEY_ATTR_MAX
:
2915 format_generic_odp_key(a
, ds
);
2917 ds_put_char(ds
, '/');
2918 format_generic_odp_key(ma
, ds
);
2922 ds_put_char(ds
, ')');
2925 static struct nlattr
*
2926 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
2927 struct ofpbuf
*ofp
, const struct nlattr
*key
)
2929 const struct nlattr
*a
;
2931 int type
= nl_attr_type(key
);
2932 int size
= nl_attr_get_size(key
);
2934 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
2935 nl_msg_put_unspec_zero(ofp
, type
, size
);
2939 if (tbl
[type
].next
) {
2940 tbl
= tbl
[type
].next
;
2941 max
= tbl
[type
].next_max
;
2944 nested_mask
= nl_msg_start_nested(ofp
, type
);
2945 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
2946 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
2948 nl_msg_end_nested(ofp
, nested_mask
);
2955 format_u128(struct ds
*ds
, const ovs_u128
*key
, const ovs_u128
*mask
,
2958 if (verbose
|| (mask
&& !ovs_u128_is_zero(mask
))) {
2961 value
= hton128(*key
);
2962 ds_put_hex(ds
, &value
, sizeof value
);
2963 if (mask
&& !(ovs_u128_is_ones(mask
))) {
2964 value
= hton128(*mask
);
2965 ds_put_char(ds
, '/');
2966 ds_put_hex(ds
, &value
, sizeof value
);
2972 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
2974 char *s
= CONST_CAST(char *, s_
);
2978 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
2979 *value
= ntoh128(be_value
);
2984 if (ovs_scan(s
, "/%n", &n
)) {
2988 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
2989 sizeof be_mask
, &s
);
2993 *mask
= ntoh128(be_mask
);
2995 *mask
= OVS_U128_MAX
;
3005 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3009 if (ovs_scan(s
, "ufid:")) {
3012 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3024 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3026 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3029 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3030 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3031 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3032 * non-null and 'verbose' is true, translates odp port number to its name. */
3034 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3035 const struct nlattr
*mask
, size_t mask_len
,
3036 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3039 const struct nlattr
*a
;
3041 bool has_ethtype_key
= false;
3042 const struct nlattr
*ma
= NULL
;
3044 bool first_field
= true;
3046 ofpbuf_init(&ofp
, 100);
3047 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3048 bool is_nested_attr
;
3049 bool is_wildcard
= false;
3050 int attr_type
= nl_attr_type(a
);
3052 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3053 has_ethtype_key
= true;
3056 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3057 OVS_KEY_ATTR_MAX
, attr_type
) ==
3060 if (mask
&& mask_len
) {
3061 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3062 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3065 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3066 if (is_wildcard
&& !ma
) {
3067 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3072 ds_put_char(ds
, ',');
3074 format_odp_key_attr(a
, ma
, portno_names
, ds
, verbose
);
3075 first_field
= false;
3079 ofpbuf_uninit(&ofp
);
3084 if (left
== key_len
) {
3085 ds_put_cstr(ds
, "<empty>");
3087 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3088 for (i
= 0; i
< left
; i
++) {
3089 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3091 ds_put_char(ds
, ')');
3093 if (!has_ethtype_key
) {
3094 ma
= nl_attr_find__(mask
, mask_len
, OVS_KEY_ATTR_ETHERTYPE
);
3096 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3097 ntohs(nl_attr_get_be16(ma
)));
3101 ds_put_cstr(ds
, "<empty>");
3105 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3106 * OVS_KEY_ATTR_* attributes in 'key'. */
3108 odp_flow_key_format(const struct nlattr
*key
,
3109 size_t key_len
, struct ds
*ds
)
3111 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3115 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3117 if (!strcasecmp(s
, "no")) {
3118 *type
= OVS_FRAG_TYPE_NONE
;
3119 } else if (!strcasecmp(s
, "first")) {
3120 *type
= OVS_FRAG_TYPE_FIRST
;
3121 } else if (!strcasecmp(s
, "later")) {
3122 *type
= OVS_FRAG_TYPE_LATER
;
3132 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3136 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3137 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3141 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3142 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3145 memset(mask
, 0xff, sizeof *mask
);
3154 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3158 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3162 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3163 IP_SCAN_ARGS(mask
), &n
)) {
3166 *mask
= OVS_BE32_MAX
;
3175 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3178 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3180 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3181 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3185 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3186 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3189 memset(mask
, 0xff, sizeof *mask
);
3198 scan_ipv6(const char *s
, ovs_be32 (*key
)[4], ovs_be32 (*mask
)[4])
3200 return scan_in6_addr(s
, key
? (struct in6_addr
*) *key
: NULL
,
3201 mask
? (struct in6_addr
*) *mask
: NULL
);
3205 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3210 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3211 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3216 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3217 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3219 *mask
= htonl(mask_
);
3221 *mask
= htonl(IPV6_LABEL_MASK
);
3230 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3234 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3238 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3250 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3254 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3258 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3270 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3274 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3278 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3290 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3292 uint16_t key_
, mask_
;
3295 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3300 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3302 *mask
= htons(mask_
);
3304 *mask
= OVS_BE16_MAX
;
3313 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3315 uint64_t key_
, mask_
;
3318 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3321 *key
= htonll(key_
);
3323 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3325 *mask
= htonll(mask_
);
3327 *mask
= OVS_BE64_MAX
;
3336 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3338 uint32_t flags
, fmask
;
3341 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3342 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3343 if (n
>= 0 && s
[n
] == ')') {
3354 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3356 uint32_t flags
, fmask
;
3359 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3360 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3362 *key
= htons(flags
);
3364 *mask
= htons(fmask
);
3372 ovs_to_odp_ct_state(uint8_t state
)
3376 if (state
& CS_NEW
) {
3377 odp
|= OVS_CS_F_NEW
;
3379 if (state
& CS_ESTABLISHED
) {
3380 odp
|= OVS_CS_F_ESTABLISHED
;
3382 if (state
& CS_RELATED
) {
3383 odp
|= OVS_CS_F_RELATED
;
3385 if (state
& CS_INVALID
) {
3386 odp
|= OVS_CS_F_INVALID
;
3388 if (state
& CS_REPLY_DIR
) {
3389 odp
|= OVS_CS_F_REPLY_DIR
;
3391 if (state
& CS_TRACKED
) {
3392 odp
|= OVS_CS_F_TRACKED
;
3394 if (state
& CS_SRC_NAT
) {
3395 odp
|= OVS_CS_F_SRC_NAT
;
3397 if (state
& CS_DST_NAT
) {
3398 odp
|= OVS_CS_F_DST_NAT
;
3405 odp_to_ovs_ct_state(uint32_t flags
)
3409 if (flags
& OVS_CS_F_NEW
) {
3412 if (flags
& OVS_CS_F_ESTABLISHED
) {
3413 state
|= CS_ESTABLISHED
;
3415 if (flags
& OVS_CS_F_RELATED
) {
3416 state
|= CS_RELATED
;
3418 if (flags
& OVS_CS_F_INVALID
) {
3419 state
|= CS_INVALID
;
3421 if (flags
& OVS_CS_F_REPLY_DIR
) {
3422 state
|= CS_REPLY_DIR
;
3424 if (flags
& OVS_CS_F_TRACKED
) {
3425 state
|= CS_TRACKED
;
3427 if (flags
& OVS_CS_F_SRC_NAT
) {
3428 state
|= CS_SRC_NAT
;
3430 if (flags
& OVS_CS_F_DST_NAT
) {
3431 state
|= CS_DST_NAT
;
3438 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3440 uint32_t flags
, fmask
;
3443 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
3444 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
3445 mask
? &fmask
: NULL
);
3458 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
3462 enum ovs_frag_type frag_type
;
3464 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
3465 && ovs_frag_type_from_string(frag
, &frag_type
)) {
3478 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
3479 const struct simap
*port_names
)
3483 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3487 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3494 } else if (port_names
) {
3495 const struct simap_node
*node
;
3498 len
= strcspn(s
, ")");
3499 node
= simap_find_len(port_names
, s
, len
);
3512 /* Helper for vlan parsing. */
3513 struct ovs_key_vlan__
{
3518 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
3520 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
3522 if (value
>> bits
) {
3526 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
3531 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
3534 uint16_t key_
, mask_
;
3537 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3540 if (set_be16_bf(key
, bits
, offset
, key_
)) {
3542 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3545 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
3549 *mask
|= htons(((1U << bits
) - 1) << offset
);
3559 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3561 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
3565 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3567 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
3571 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3573 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
3578 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
3580 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
3582 if (value
>> bits
) {
3586 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
3591 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
3594 uint32_t key_
, mask_
;
3597 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3600 if (set_be32_bf(key
, bits
, offset
, key_
)) {
3602 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3605 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
3609 *mask
|= htonl(((1U << bits
) - 1) << offset
);
3619 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3621 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
3625 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3627 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
3631 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3633 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
3637 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3639 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
3643 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
3645 const char *s_base
= s
;
3646 ovs_be16 id
= 0, id_mask
= 0;
3647 uint8_t flags
= 0, flags_mask
= 0;
3649 if (!strncmp(s
, "id=", 3)) {
3651 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
3657 if (!strncmp(s
, "flags=", 6)) {
3659 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
3662 if (!strncmp(s
, "))", 2)) {
3665 *key
= (flags
<< 16) | ntohs(id
);
3667 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
3677 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
3679 const char *s_base
= s
;
3680 struct geneve_opt
*opt
= key
->d
;
3681 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
3682 int len_remain
= sizeof key
->d
;
3684 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
3688 len_remain
-= sizeof *opt
;
3690 if (!strncmp(s
, "class=", 6)) {
3692 s
+= scan_be16(s
, &opt
->opt_class
,
3693 mask
? &opt_mask
->opt_class
: NULL
);
3695 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
3701 if (!strncmp(s
, "type=", 5)) {
3703 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
3705 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3711 if (!strncmp(s
, "len=", 4)) {
3712 uint8_t opt_len
, opt_len_mask
;
3714 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
3716 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
3719 opt
->length
= opt_len
/ 4;
3721 opt_mask
->length
= opt_len_mask
;
3725 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
3731 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
3738 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
3739 data_len
, (char **)&s
)) {
3750 opt
+= 1 + data_len
/ 4;
3752 opt_mask
+= 1 + data_len
/ 4;
3754 len_remain
-= data_len
;
3759 int len
= sizeof key
->d
- len_remain
;
3773 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
3775 const uint16_t *flags
= data_
;
3777 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3778 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3780 if (*flags
& FLOW_TNL_F_CSUM
) {
3781 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3783 if (*flags
& FLOW_TNL_F_OAM
) {
3784 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3789 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
3791 const uint32_t *gbp
= data_
;
3794 size_t vxlan_opts_ofs
;
3796 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3797 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
3798 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3803 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
3805 const struct geneve_scan
*geneve
= data_
;
3807 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
3811 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
3813 unsigned long call_fn = (unsigned long)FUNC; \
3815 typedef void (*fn)(struct ofpbuf *, const void *); \
3817 func(BUF, &(DATA)); \
3819 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
3823 #define SCAN_IF(NAME) \
3824 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3825 const char *start = s; \
3830 /* Usually no special initialization is needed. */
3831 #define SCAN_BEGIN(NAME, TYPE) \
3834 memset(&skey, 0, sizeof skey); \
3835 memset(&smask, 0, sizeof smask); \
3839 /* Init as fully-masked as mask will not be scanned. */
3840 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
3843 memset(&skey, 0, sizeof skey); \
3844 memset(&smask, 0xff, sizeof smask); \
3848 /* VLAN needs special initialization. */
3849 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
3851 TYPE skey = KEY_INIT; \
3852 TYPE smask = MASK_INIT; \
3856 /* Scan unnamed entry as 'TYPE' */
3857 #define SCAN_TYPE(TYPE, KEY, MASK) \
3858 len = scan_##TYPE(s, KEY, MASK); \
3864 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
3865 #define SCAN_FIELD(NAME, TYPE, FIELD) \
3866 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3867 s += strlen(NAME); \
3868 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
3872 #define SCAN_FINISH() \
3873 } while (*s++ == ',' && len != 0); \
3874 if (s[-1] != ')') { \
3878 #define SCAN_FINISH_SINGLE() \
3880 if (*s++ != ')') { \
3884 /* Beginning of nested attribute. */
3885 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
3887 size_t key_offset, mask_offset; \
3888 key_offset = nl_msg_start_nested(key, ATTR); \
3890 mask_offset = nl_msg_start_nested(mask, ATTR); \
3895 #define SCAN_END_NESTED() \
3897 nl_msg_end_nested(key, key_offset); \
3899 nl_msg_end_nested(mask, mask_offset); \
3904 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
3905 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
3907 memset(&skey, 0, sizeof skey); \
3908 memset(&smask, 0xff, sizeof smask); \
3909 s += strlen(NAME); \
3910 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3911 SCAN_PUT(ATTR, FUNC); \
3915 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
3916 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
3918 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
3919 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
3921 #define SCAN_PUT(ATTR, FUNC) \
3922 if (!mask || !is_all_zeros(&smask, sizeof smask)) { \
3923 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
3925 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
3929 #define SCAN_END(ATTR) \
3931 SCAN_PUT(ATTR, NULL); \
3935 #define SCAN_END_SINGLE(ATTR) \
3936 SCAN_FINISH_SINGLE(); \
3937 SCAN_PUT(ATTR, NULL); \
3941 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
3942 SCAN_BEGIN(NAME, TYPE) { \
3943 SCAN_TYPE(SCAN_AS, &skey, &smask); \
3944 } SCAN_END_SINGLE(ATTR)
3946 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
3947 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
3948 SCAN_TYPE(SCAN_AS, &skey, NULL); \
3949 } SCAN_END_SINGLE(ATTR)
3951 /* scan_port needs one extra argument. */
3952 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
3953 SCAN_BEGIN(NAME, TYPE) { \
3954 len = scan_port(s, &skey, &smask, port_names); \
3959 } SCAN_END_SINGLE(ATTR)
3962 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
3963 struct ofpbuf
*key
, struct ofpbuf
*mask
)
3969 len
= odp_ufid_from_string(s
, &ufid
);
3974 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
3975 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
3976 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
3977 OVS_KEY_ATTR_RECIRC_ID
);
3978 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
3980 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
3981 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
3982 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
3983 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
3985 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
3986 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
3987 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
3988 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
3989 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
3990 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
3991 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
3992 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
3993 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
3994 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
3995 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
3996 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
3998 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
3999 } SCAN_END_NESTED();
4001 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4003 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4004 SCAN_FIELD("src=", eth
, eth_src
);
4005 SCAN_FIELD("dst=", eth
, eth_dst
);
4006 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4008 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4009 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4010 SCAN_FIELD("vid=", vid
, tci
);
4011 SCAN_FIELD("pcp=", pcp
, tci
);
4012 SCAN_FIELD("cfi=", cfi
, tci
);
4013 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4015 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4017 SCAN_BEGIN("mpls(", struct ovs_key_mpls
) {
4018 SCAN_FIELD("label=", mpls_label
, mpls_lse
);
4019 SCAN_FIELD("tc=", mpls_tc
, mpls_lse
);
4020 SCAN_FIELD("ttl=", mpls_ttl
, mpls_lse
);
4021 SCAN_FIELD("bos=", mpls_bos
, mpls_lse
);
4022 } SCAN_END(OVS_KEY_ATTR_MPLS
);
4024 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4025 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4026 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4027 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4028 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4029 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4030 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4031 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4033 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4034 SCAN_FIELD("src=", ipv6
, ipv6_src
);
4035 SCAN_FIELD("dst=", ipv6
, ipv6_dst
);
4036 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4037 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4038 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4039 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4040 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4041 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4043 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4044 SCAN_FIELD("src=", be16
, tcp_src
);
4045 SCAN_FIELD("dst=", be16
, tcp_dst
);
4046 } SCAN_END(OVS_KEY_ATTR_TCP
);
4048 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4050 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4051 SCAN_FIELD("src=", be16
, udp_src
);
4052 SCAN_FIELD("dst=", be16
, udp_dst
);
4053 } SCAN_END(OVS_KEY_ATTR_UDP
);
4055 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4056 SCAN_FIELD("src=", be16
, sctp_src
);
4057 SCAN_FIELD("dst=", be16
, sctp_dst
);
4058 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4060 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4061 SCAN_FIELD("type=", u8
, icmp_type
);
4062 SCAN_FIELD("code=", u8
, icmp_code
);
4063 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4065 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4066 SCAN_FIELD("type=", u8
, icmpv6_type
);
4067 SCAN_FIELD("code=", u8
, icmpv6_code
);
4068 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4070 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4071 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4072 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4073 SCAN_FIELD("op=", be16
, arp_op
);
4074 SCAN_FIELD("sha=", eth
, arp_sha
);
4075 SCAN_FIELD("tha=", eth
, arp_tha
);
4076 } SCAN_END(OVS_KEY_ATTR_ARP
);
4078 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4079 SCAN_FIELD("target=", ipv6
, nd_target
);
4080 SCAN_FIELD("sll=", eth
, nd_sll
);
4081 SCAN_FIELD("tll=", eth
, nd_tll
);
4082 } SCAN_END(OVS_KEY_ATTR_ND
);
4084 /* Encap open-coded. */
4085 if (!strncmp(s
, "encap(", 6)) {
4086 const char *start
= s
;
4087 size_t encap
, encap_mask
= 0;
4089 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4091 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4098 s
+= strspn(s
, delimiters
);
4101 } else if (*s
== ')') {
4105 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4113 nl_msg_end_nested(key
, encap
);
4115 nl_msg_end_nested(mask
, encap_mask
);
4124 /* Parses the string representation of a datapath flow key, in the
4125 * format output by odp_flow_key_format(). Returns 0 if successful,
4126 * otherwise a positive errno value. On success, the flow key is
4127 * appended to 'key' as a series of Netlink attributes. On failure, no
4128 * data is appended to 'key'. Either way, 'key''s data might be
4131 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4132 * to a port number. (Port names may be used instead of port numbers in
4135 * On success, the attributes appended to 'key' are individually syntactically
4136 * valid, but they may not be valid as a sequence. 'key' might, for example,
4137 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4139 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4140 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4142 const size_t old_size
= key
->size
;
4146 s
+= strspn(s
, delimiters
);
4151 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4153 key
->size
= old_size
;
4163 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4166 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4167 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4168 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4169 * must use a zero mask for the netlink frag field, and all ones mask
4171 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4173 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4174 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4175 : OVS_FRAG_TYPE_FIRST
;
4178 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4179 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4180 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4182 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4184 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4186 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4188 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4189 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4190 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4191 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4193 /* These share the same layout. */
4195 struct ovs_key_tcp tcp
;
4196 struct ovs_key_udp udp
;
4197 struct ovs_key_sctp sctp
;
4200 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4201 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4204 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4205 bool export_mask
, struct ofpbuf
*buf
)
4207 struct ovs_key_ethernet
*eth_key
;
4209 const struct flow
*flow
= parms
->flow
;
4210 const struct flow
*data
= export_mask
? parms
->mask
: parms
->flow
;
4212 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4214 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4215 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4219 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4221 if (parms
->support
.ct_state
) {
4222 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4223 ovs_to_odp_ct_state(data
->ct_state
));
4225 if (parms
->support
.ct_zone
) {
4226 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4228 if (parms
->support
.ct_mark
) {
4229 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4231 if (parms
->support
.ct_label
) {
4232 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4233 sizeof(data
->ct_label
));
4235 if (parms
->support
.recirc
) {
4236 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4237 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4240 /* Add an ingress port attribute if this is a mask or 'odp_in_port'
4241 * is not the magical value "ODPP_NONE". */
4242 if (export_mask
|| parms
->odp_in_port
!= ODPP_NONE
) {
4243 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, parms
->odp_in_port
);
4246 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4248 get_ethernet_key(data
, eth_key
);
4250 if (flow
->vlan_tci
!= htons(0) || flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
4252 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4254 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, htons(ETH_TYPE_VLAN
));
4256 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlan_tci
);
4257 encap
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4258 if (flow
->vlan_tci
== htons(0)) {
4265 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4266 /* For backwards compatibility with kernels that don't support
4267 * wildcarding, the following convention is used to encode the
4268 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4271 * -------- -------- -------
4272 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4273 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4274 * <none> 0xffff Any non-Ethernet II frame (except valid
4275 * 802.3 SNAP packet with valid eth_type).
4278 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4283 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4285 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4286 struct ovs_key_ipv4
*ipv4_key
;
4288 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4290 get_ipv4_key(data
, ipv4_key
, export_mask
);
4291 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4292 struct ovs_key_ipv6
*ipv6_key
;
4294 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4296 get_ipv6_key(data
, ipv6_key
, export_mask
);
4297 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4298 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4299 struct ovs_key_arp
*arp_key
;
4301 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4303 get_arp_key(data
, arp_key
);
4304 } else if (eth_type_mpls(flow
->dl_type
)) {
4305 struct ovs_key_mpls
*mpls_key
;
4308 n
= flow_count_mpls_labels(flow
, NULL
);
4310 n
= MIN(n
, parms
->support
.max_mpls_depth
);
4312 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
4313 n
* sizeof *mpls_key
);
4314 for (i
= 0; i
< n
; i
++) {
4315 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
4319 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4320 if (flow
->nw_proto
== IPPROTO_TCP
) {
4321 union ovs_key_tp
*tcp_key
;
4323 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
4325 get_tp_key(data
, tcp_key
);
4326 if (data
->tcp_flags
) {
4327 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
4329 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
4330 union ovs_key_tp
*udp_key
;
4332 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
4334 get_tp_key(data
, udp_key
);
4335 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
4336 union ovs_key_tp
*sctp_key
;
4338 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
4340 get_tp_key(data
, sctp_key
);
4341 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
4342 && flow
->nw_proto
== IPPROTO_ICMP
) {
4343 struct ovs_key_icmp
*icmp_key
;
4345 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
4347 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
4348 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
4349 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4350 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
4351 struct ovs_key_icmpv6
*icmpv6_key
;
4353 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
4354 sizeof *icmpv6_key
);
4355 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
4356 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
4358 if (flow
->tp_dst
== htons(0)
4359 && (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
)
4360 || flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))
4361 && (!export_mask
|| (data
->tp_src
== htons(0xffff)
4362 && data
->tp_dst
== htons(0xffff)))) {
4364 struct ovs_key_nd
*nd_key
;
4366 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
4368 memcpy(nd_key
->nd_target
, &data
->nd_target
,
4369 sizeof nd_key
->nd_target
);
4370 nd_key
->nd_sll
= data
->arp_sha
;
4371 nd_key
->nd_tll
= data
->arp_tha
;
4378 nl_msg_end_nested(buf
, encap
);
4382 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
4384 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4385 * capable of being expanded to allow for that much space. */
4387 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
4390 odp_flow_key_from_flow__(parms
, false, buf
);
4393 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
4396 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
4397 * capable of being expanded to allow for that much space. */
4399 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
4402 odp_flow_key_from_flow__(parms
, true, buf
);
4405 /* Generate ODP flow key from the given packet metadata */
4407 odp_key_from_pkt_metadata(struct ofpbuf
*buf
, const struct pkt_metadata
*md
)
4409 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
4411 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
4412 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
4415 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
4418 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4419 ovs_to_odp_ct_state(md
->ct_state
));
4421 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
4424 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
4426 if (!ovs_u128_is_zero(&md
->ct_label
)) {
4427 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
4428 sizeof(md
->ct_label
));
4432 /* Add an ingress port attribute if 'odp_in_port' is not the magical
4433 * value "ODPP_NONE". */
4434 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
4435 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
4439 /* Generate packet metadata from the given ODP flow key. */
4441 odp_key_to_pkt_metadata(const struct nlattr
*key
, size_t key_len
,
4442 struct pkt_metadata
*md
)
4444 const struct nlattr
*nla
;
4446 uint32_t wanted_attrs
= 1u << OVS_KEY_ATTR_PRIORITY
|
4447 1u << OVS_KEY_ATTR_SKB_MARK
| 1u << OVS_KEY_ATTR_TUNNEL
|
4448 1u << OVS_KEY_ATTR_IN_PORT
;
4450 pkt_metadata_init(md
, ODPP_NONE
);
4452 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4453 uint16_t type
= nl_attr_type(nla
);
4454 size_t len
= nl_attr_get_size(nla
);
4455 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4456 OVS_KEY_ATTR_MAX
, type
);
4458 if (len
!= expected_len
&& expected_len
>= 0) {
4463 case OVS_KEY_ATTR_RECIRC_ID
:
4464 md
->recirc_id
= nl_attr_get_u32(nla
);
4465 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_RECIRC_ID
);
4467 case OVS_KEY_ATTR_DP_HASH
:
4468 md
->dp_hash
= nl_attr_get_u32(nla
);
4469 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_DP_HASH
);
4471 case OVS_KEY_ATTR_PRIORITY
:
4472 md
->skb_priority
= nl_attr_get_u32(nla
);
4473 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_PRIORITY
);
4475 case OVS_KEY_ATTR_SKB_MARK
:
4476 md
->pkt_mark
= nl_attr_get_u32(nla
);
4477 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_SKB_MARK
);
4479 case OVS_KEY_ATTR_CT_STATE
:
4480 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
4481 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_STATE
);
4483 case OVS_KEY_ATTR_CT_ZONE
:
4484 md
->ct_zone
= nl_attr_get_u16(nla
);
4485 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_ZONE
);
4487 case OVS_KEY_ATTR_CT_MARK
:
4488 md
->ct_mark
= nl_attr_get_u32(nla
);
4489 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_MARK
);
4491 case OVS_KEY_ATTR_CT_LABELS
: {
4492 const ovs_u128
*cl
= nl_attr_get(nla
);
4495 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_CT_LABELS
);
4498 case OVS_KEY_ATTR_TUNNEL
: {
4499 enum odp_key_fitness res
;
4501 res
= odp_tun_key_from_attr(nla
, true, &md
->tunnel
);
4502 if (res
== ODP_FIT_ERROR
) {
4503 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
4504 } else if (res
== ODP_FIT_PERFECT
) {
4505 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_TUNNEL
);
4509 case OVS_KEY_ATTR_IN_PORT
:
4510 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
4511 wanted_attrs
&= ~(1u << OVS_KEY_ATTR_IN_PORT
);
4517 if (!wanted_attrs
) {
4518 return; /* Have everything. */
4524 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
4526 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
4527 return hash_words(ALIGNED_CAST(const uint32_t *, key
),
4528 key_len
/ sizeof(uint32_t), 0);
4532 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
4533 uint64_t attrs
, int out_of_range_attr
,
4534 const struct nlattr
*key
, size_t key_len
)
4539 if (VLOG_DROP_DBG(rl
)) {
4544 for (i
= 0; i
< 64; i
++) {
4545 if (attrs
& (UINT64_C(1) << i
)) {
4546 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4548 ds_put_format(&s
, " %s",
4549 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
4552 if (out_of_range_attr
) {
4553 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
4556 ds_put_cstr(&s
, ": ");
4557 odp_flow_key_format(key
, key_len
, &s
);
4559 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
4564 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
4566 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4569 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
4572 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
4573 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
4574 return 0xff; /* Error. */
4577 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
4578 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
4579 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
4583 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
4584 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
4585 int *out_of_range_attrp
)
4587 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4588 const struct nlattr
*nla
;
4589 uint64_t present_attrs
;
4592 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
4594 *out_of_range_attrp
= 0;
4595 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
4596 uint16_t type
= nl_attr_type(nla
);
4597 size_t len
= nl_attr_get_size(nla
);
4598 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4599 OVS_KEY_ATTR_MAX
, type
);
4601 if (len
!= expected_len
&& expected_len
>= 0) {
4602 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4604 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
4605 "length %d", ovs_key_attr_to_string(type
, namebuf
,
4611 if (type
> OVS_KEY_ATTR_MAX
) {
4612 *out_of_range_attrp
= type
;
4614 if (present_attrs
& (UINT64_C(1) << type
)) {
4615 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4617 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
4618 ovs_key_attr_to_string(type
,
4619 namebuf
, sizeof namebuf
));
4623 present_attrs
|= UINT64_C(1) << type
;
4628 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
4632 *present_attrsp
= present_attrs
;
4636 static enum odp_key_fitness
4637 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
4638 uint64_t expected_attrs
,
4639 const struct nlattr
*key
, size_t key_len
)
4641 uint64_t missing_attrs
;
4642 uint64_t extra_attrs
;
4644 missing_attrs
= expected_attrs
& ~present_attrs
;
4645 if (missing_attrs
) {
4646 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4647 log_odp_key_attributes(&rl
, "expected but not present",
4648 missing_attrs
, 0, key
, key_len
);
4649 return ODP_FIT_TOO_LITTLE
;
4652 extra_attrs
= present_attrs
& ~expected_attrs
;
4653 if (extra_attrs
|| out_of_range_attr
) {
4654 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
4655 log_odp_key_attributes(&rl
, "present but not expected",
4656 extra_attrs
, out_of_range_attr
, key
, key_len
);
4657 return ODP_FIT_TOO_MUCH
;
4660 return ODP_FIT_PERFECT
;
4664 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4665 uint64_t present_attrs
, uint64_t *expected_attrs
,
4666 struct flow
*flow
, const struct flow
*src_flow
)
4668 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4669 bool is_mask
= flow
!= src_flow
;
4671 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
4672 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
4673 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4674 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
4675 ntohs(flow
->dl_type
));
4678 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
4679 flow
->dl_type
!= htons(0xffff)) {
4682 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
4685 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
4686 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
4687 /* See comments in odp_flow_key_from_flow__(). */
4688 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
4695 static enum odp_key_fitness
4696 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4697 uint64_t present_attrs
, int out_of_range_attr
,
4698 uint64_t expected_attrs
, struct flow
*flow
,
4699 const struct nlattr
*key
, size_t key_len
,
4700 const struct flow
*src_flow
)
4702 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4703 bool is_mask
= src_flow
!= flow
;
4704 const void *check_start
= NULL
;
4705 size_t check_len
= 0;
4706 enum ovs_key_attr expected_bit
= 0xff;
4708 if (eth_type_mpls(src_flow
->dl_type
)) {
4709 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4710 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
4712 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
4713 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
4714 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
4715 int n
= size
/ sizeof(ovs_be32
);
4718 if (!size
|| size
% sizeof(ovs_be32
)) {
4719 return ODP_FIT_ERROR
;
4721 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
4722 return ODP_FIT_ERROR
;
4725 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
4726 flow
->mpls_lse
[i
] = mpls_lse
[i
];
4728 if (n
> FLOW_MAX_MPLS_LABELS
) {
4729 return ODP_FIT_TOO_MUCH
;
4733 /* BOS may be set only in the innermost label. */
4734 for (i
= 0; i
< n
- 1; i
++) {
4735 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
4736 return ODP_FIT_ERROR
;
4740 /* BOS must be set in the innermost label. */
4741 if (n
< FLOW_MAX_MPLS_LABELS
4742 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
4743 return ODP_FIT_TOO_LITTLE
;
4749 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4751 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
4753 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
4754 const struct ovs_key_ipv4
*ipv4_key
;
4756 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
4757 put_ipv4_key(ipv4_key
, flow
, is_mask
);
4758 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4759 return ODP_FIT_ERROR
;
4762 check_start
= ipv4_key
;
4763 check_len
= sizeof *ipv4_key
;
4764 expected_bit
= OVS_KEY_ATTR_IPV4
;
4767 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4769 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
4771 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
4772 const struct ovs_key_ipv6
*ipv6_key
;
4774 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
4775 put_ipv6_key(ipv6_key
, flow
, is_mask
);
4776 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
4777 return ODP_FIT_ERROR
;
4780 check_start
= ipv6_key
;
4781 check_len
= sizeof *ipv6_key
;
4782 expected_bit
= OVS_KEY_ATTR_IPV6
;
4785 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4786 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4788 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
4790 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
4791 const struct ovs_key_arp
*arp_key
;
4793 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
4794 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
4795 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
4796 "key", ntohs(arp_key
->arp_op
));
4797 return ODP_FIT_ERROR
;
4799 put_arp_key(arp_key
, flow
);
4801 check_start
= arp_key
;
4802 check_len
= sizeof *arp_key
;
4803 expected_bit
= OVS_KEY_ATTR_ARP
;
4809 if (check_len
> 0) { /* Happens only when 'is_mask'. */
4810 if (!is_all_zeros(check_start
, check_len
) &&
4811 flow
->dl_type
!= htons(0xffff)) {
4812 return ODP_FIT_ERROR
;
4814 expected_attrs
|= UINT64_C(1) << expected_bit
;
4818 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
4819 if (src_flow
->nw_proto
== IPPROTO_TCP
4820 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4821 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4822 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4824 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
4826 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
4827 const union ovs_key_tp
*tcp_key
;
4829 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
4830 put_tp_key(tcp_key
, flow
);
4831 expected_bit
= OVS_KEY_ATTR_TCP
;
4833 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
4834 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
4835 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
4837 } else if (src_flow
->nw_proto
== IPPROTO_UDP
4838 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4839 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4840 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4842 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
4844 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
4845 const union ovs_key_tp
*udp_key
;
4847 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
4848 put_tp_key(udp_key
, flow
);
4849 expected_bit
= OVS_KEY_ATTR_UDP
;
4851 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
4852 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
4853 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
4854 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4856 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
4858 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
4859 const union ovs_key_tp
*sctp_key
;
4861 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
4862 put_tp_key(sctp_key
, flow
);
4863 expected_bit
= OVS_KEY_ATTR_SCTP
;
4865 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
4866 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
4867 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4869 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
4871 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
4872 const struct ovs_key_icmp
*icmp_key
;
4874 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
4875 flow
->tp_src
= htons(icmp_key
->icmp_type
);
4876 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
4877 expected_bit
= OVS_KEY_ATTR_ICMP
;
4879 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
4880 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
4881 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
4883 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
4885 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
4886 const struct ovs_key_icmpv6
*icmpv6_key
;
4888 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
4889 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
4890 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
4891 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
4892 if (src_flow
->tp_dst
== htons(0) &&
4893 (src_flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
4894 src_flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
))) {
4896 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4898 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
4899 const struct ovs_key_nd
*nd_key
;
4901 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
4902 memcpy(&flow
->nd_target
, nd_key
->nd_target
,
4903 sizeof flow
->nd_target
);
4904 flow
->arp_sha
= nd_key
->nd_sll
;
4905 flow
->arp_tha
= nd_key
->nd_tll
;
4907 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
4908 (flow
->tp_src
!= htons(0xffff) ||
4909 flow
->tp_dst
!= htons(0xffff))) {
4910 return ODP_FIT_ERROR
;
4912 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
4919 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
4920 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
4921 return ODP_FIT_ERROR
;
4923 expected_attrs
|= UINT64_C(1) << expected_bit
;
4928 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
4932 /* Parse 802.1Q header then encapsulated L3 attributes. */
4933 static enum odp_key_fitness
4934 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
4935 uint64_t present_attrs
, int out_of_range_attr
,
4936 uint64_t expected_attrs
, struct flow
*flow
,
4937 const struct nlattr
*key
, size_t key_len
,
4938 const struct flow
*src_flow
)
4940 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
4941 bool is_mask
= src_flow
!= flow
;
4943 const struct nlattr
*encap
4944 = (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
4945 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
4946 enum odp_key_fitness encap_fitness
;
4947 enum odp_key_fitness fitness
;
4949 /* Calculate fitness of outer attributes. */
4951 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
4952 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
4954 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
4955 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
4957 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
4958 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
4961 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
4962 expected_attrs
, key
, key_len
);
4965 * Remove the TPID from dl_type since it's not the real Ethertype. */
4966 flow
->dl_type
= htons(0);
4967 flow
->vlan_tci
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
4968 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
4971 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
))) {
4972 return ODP_FIT_TOO_LITTLE
;
4973 } else if (flow
->vlan_tci
== htons(0)) {
4974 /* Corner case for a truncated 802.1Q header. */
4975 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
4976 return ODP_FIT_TOO_MUCH
;
4979 } else if (!(flow
->vlan_tci
& htons(VLAN_CFI
))) {
4980 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
4981 "but CFI bit is not set", ntohs(flow
->vlan_tci
));
4982 return ODP_FIT_ERROR
;
4985 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
4990 /* Now parse the encapsulated attributes. */
4991 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
4992 attrs
, &present_attrs
, &out_of_range_attr
)) {
4993 return ODP_FIT_ERROR
;
4997 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
, src_flow
)) {
4998 return ODP_FIT_ERROR
;
5000 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5001 expected_attrs
, flow
, key
, key_len
,
5004 /* The overall fitness is the worse of the outer and inner attributes. */
5005 return MAX(fitness
, encap_fitness
);
5008 static enum odp_key_fitness
5009 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5010 const struct nlattr
*src_key
, size_t src_key_len
,
5011 struct flow
*flow
, const struct flow
*src_flow
,
5014 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5015 uint64_t expected_attrs
;
5016 uint64_t present_attrs
;
5017 int out_of_range_attr
;
5018 bool is_mask
= src_flow
!= flow
;
5020 memset(flow
, 0, sizeof *flow
);
5022 /* Parse attributes. */
5023 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5024 &out_of_range_attr
)) {
5025 return ODP_FIT_ERROR
;
5030 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5031 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5032 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5033 } else if (is_mask
) {
5034 /* Always exact match recirc_id if it is not specified. */
5035 flow
->recirc_id
= UINT32_MAX
;
5038 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5039 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5040 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5042 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5043 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5044 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5047 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5048 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5049 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5052 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5053 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5055 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5056 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5058 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5059 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5060 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5062 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5063 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5064 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5066 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5067 const ovs_u128
*cl
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5069 flow
->ct_label
= *cl
;
5070 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5073 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5074 enum odp_key_fitness res
;
5076 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
],
5077 is_mask
? src_key
: NULL
,
5078 src_key_len
, &src_flow
->tunnel
,
5079 &flow
->tunnel
, udpif
);
5080 if (res
== ODP_FIT_ERROR
) {
5081 return ODP_FIT_ERROR
;
5082 } else if (res
== ODP_FIT_PERFECT
) {
5083 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5087 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5088 flow
->in_port
.odp_port
5089 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5090 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5091 } else if (!is_mask
) {
5092 flow
->in_port
.odp_port
= ODPP_NONE
;
5095 /* Ethernet header. */
5096 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5097 const struct ovs_key_ethernet
*eth_key
;
5099 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5100 put_ethernet_key(eth_key
, flow
);
5102 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5106 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5109 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5110 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5112 return ODP_FIT_ERROR
;
5116 ? (src_flow
->vlan_tci
& htons(VLAN_CFI
)) != 0
5117 : src_flow
->dl_type
== htons(ETH_TYPE_VLAN
)) {
5118 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5119 expected_attrs
, flow
, key
, key_len
, src_flow
);
5122 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5123 flow
->vlan_tci
= htons(0xffff);
5124 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5125 flow
->vlan_tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5126 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5129 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5130 expected_attrs
, flow
, key
, key_len
, src_flow
);
5133 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5134 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5135 * 'key' fits our expectations for what a flow key should contain.
5137 * The 'in_port' will be the datapath's understanding of the port. The
5138 * caller will need to translate with odp_port_to_ofp_port() if the
5139 * OpenFlow port is needed.
5141 * This function doesn't take the packet itself as an argument because none of
5142 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5143 * it is always possible to infer which additional attribute(s) should appear
5144 * by looking at the attributes for lower-level protocols, e.g. if the network
5145 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5146 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5147 * must be absent. */
5148 enum odp_key_fitness
5149 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5152 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, false);
5155 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5156 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5157 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5158 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5159 * well 'key' fits our expectations for what a flow key should contain. */
5160 enum odp_key_fitness
5161 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5162 const struct nlattr
*flow_key
, size_t flow_key_len
,
5163 struct flow
*mask
, const struct flow
*flow
)
5165 return odp_flow_key_to_flow__(mask_key
, mask_key_len
, flow_key
, flow_key_len
,
5169 /* These functions are similar to their non-"_udpif" variants but output a
5170 * 'flow' that is suitable for fast-path packet processing.
5172 * Some fields have different representation for flow setup and per-
5173 * packet processing (i.e. different between ofproto-dpif and userspace
5174 * datapath). In particular, with the non-"_udpif" functions, struct
5175 * tun_metadata is in the per-flow format (using 'present.map' and 'opts.u8');
5176 * with these functions, struct tun_metadata is in the per-packet format
5177 * (using 'present.len' and 'opts.gnv'). */
5178 enum odp_key_fitness
5179 odp_flow_key_to_flow_udpif(const struct nlattr
*key
, size_t key_len
,
5182 return odp_flow_key_to_flow__(key
, key_len
, NULL
, 0, flow
, flow
, true);
5185 enum odp_key_fitness
5186 odp_flow_key_to_mask_udpif(const struct nlattr
*mask_key
, size_t mask_key_len
,
5187 const struct nlattr
*flow_key
, size_t flow_key_len
,
5188 struct flow
*mask
, const struct flow
*flow
)
5190 return odp_flow_key_to_flow__(mask_key
, mask_key_len
, flow_key
, flow_key_len
,
5194 /* Returns 'fitness' as a string, for use in debug messages. */
5196 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
5199 case ODP_FIT_PERFECT
:
5201 case ODP_FIT_TOO_MUCH
:
5203 case ODP_FIT_TOO_LITTLE
:
5204 return "too_little";
5212 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
5213 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
5214 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
5215 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
5216 * null, then the return value is not meaningful.) */
5218 odp_put_userspace_action(uint32_t pid
,
5219 const void *userdata
, size_t userdata_size
,
5220 odp_port_t tunnel_out_port
,
5221 bool include_actions
,
5222 struct ofpbuf
*odp_actions
)
5224 size_t userdata_ofs
;
5227 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
5228 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
5230 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
5232 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
5233 * module before Linux 3.10 required the userdata to be exactly 8 bytes
5236 * - The kernel rejected shorter userdata with -ERANGE.
5238 * - The kernel silently dropped userdata beyond the first 8 bytes.
5240 * Thus, for maximum compatibility, always put at least 8 bytes. (We
5241 * separately disable features that required more than 8 bytes.) */
5242 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
5243 MAX(8, userdata_size
)),
5244 userdata
, userdata_size
);
5248 if (tunnel_out_port
!= ODPP_NONE
) {
5249 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
5252 if (include_actions
) {
5253 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
5255 nl_msg_end_nested(odp_actions
, offset
);
5257 return userdata_ofs
;
5261 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
5262 struct ofpbuf
*odp_actions
)
5264 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5265 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
5266 nl_msg_end_nested(odp_actions
, offset
);
5270 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
5271 struct ovs_action_push_tnl
*data
)
5273 int size
= offsetof(struct ovs_action_push_tnl
, header
);
5275 size
+= data
->header_len
;
5276 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
5280 /* The commit_odp_actions() function and its helpers. */
5283 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
5284 const void *key
, size_t key_size
)
5286 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
5287 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
5288 nl_msg_end_nested(odp_actions
, offset
);
5291 /* Masked set actions have a mask following the data within the netlink
5292 * attribute. The unmasked bits in the data will be cleared as the data
5293 * is copied to the action. */
5295 commit_masked_set_action(struct ofpbuf
*odp_actions
,
5296 enum ovs_key_attr key_type
,
5297 const void *key_
, const void *mask_
, size_t key_size
)
5299 size_t offset
= nl_msg_start_nested(odp_actions
,
5300 OVS_ACTION_ATTR_SET_MASKED
);
5301 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
5302 const char *key
= key_
, *mask
= mask_
;
5304 memcpy(data
+ key_size
, mask
, key_size
);
5305 /* Clear unmasked bits while copying. */
5306 while (key_size
--) {
5307 *data
++ = *key
++ & *mask
++;
5309 nl_msg_end_nested(odp_actions
, offset
);
5312 /* If any of the flow key data that ODP actions can modify are different in
5313 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
5314 * 'odp_actions' that change the flow tunneling information in key from
5315 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
5316 * same way. In other words, operates the same as commit_odp_actions(), but
5317 * only on tunneling information. */
5319 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
5320 struct ofpbuf
*odp_actions
)
5322 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
5323 * must have non-zero ipv6_dst. */
5324 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
5325 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
5328 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
5329 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
5334 commit(enum ovs_key_attr attr
, bool use_masked_set
,
5335 const void *key
, void *base
, void *mask
, size_t size
,
5336 struct ofpbuf
*odp_actions
)
5338 if (memcmp(key
, base
, size
)) {
5339 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
5341 if (use_masked_set
&& !fully_masked
) {
5342 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
5344 if (!fully_masked
) {
5345 memset(mask
, 0xff, size
);
5347 commit_set_action(odp_actions
, attr
, key
, size
);
5349 memcpy(base
, key
, size
);
5352 /* Mask bits are set when we have either read or set the corresponding
5353 * values. Masked bits will be exact-matched, no need to set them
5354 * if the value did not actually change. */
5360 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
5362 eth
->eth_src
= flow
->dl_src
;
5363 eth
->eth_dst
= flow
->dl_dst
;
5367 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
5369 flow
->dl_src
= eth
->eth_src
;
5370 flow
->dl_dst
= eth
->eth_dst
;
5374 commit_set_ether_addr_action(const struct flow
*flow
, struct flow
*base_flow
,
5375 struct ofpbuf
*odp_actions
,
5376 struct flow_wildcards
*wc
,
5379 struct ovs_key_ethernet key
, base
, mask
;
5381 get_ethernet_key(flow
, &key
);
5382 get_ethernet_key(base_flow
, &base
);
5383 get_ethernet_key(&wc
->masks
, &mask
);
5385 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
5386 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5387 put_ethernet_key(&base
, base_flow
);
5388 put_ethernet_key(&mask
, &wc
->masks
);
5393 pop_vlan(struct flow
*base
,
5394 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5396 memset(&wc
->masks
.vlan_tci
, 0xff, sizeof wc
->masks
.vlan_tci
);
5398 if (base
->vlan_tci
& htons(VLAN_CFI
)) {
5399 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
5405 commit_vlan_action(ovs_be16 vlan_tci
, struct flow
*base
,
5406 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5408 if (base
->vlan_tci
== vlan_tci
) {
5412 pop_vlan(base
, odp_actions
, wc
);
5413 if (vlan_tci
& htons(VLAN_CFI
)) {
5414 struct ovs_action_push_vlan vlan
;
5416 vlan
.vlan_tpid
= htons(ETH_TYPE_VLAN
);
5417 vlan
.vlan_tci
= vlan_tci
;
5418 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
5419 &vlan
, sizeof vlan
);
5421 base
->vlan_tci
= vlan_tci
;
5424 /* Wildcarding already done at action translation time. */
5426 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
5427 struct ofpbuf
*odp_actions
)
5429 int base_n
= flow_count_mpls_labels(base
, NULL
);
5430 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
5431 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
5434 while (base_n
> common_n
) {
5435 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
5436 /* If there is only one more LSE in base than there are common
5437 * between base and flow; and flow has at least one more LSE than
5438 * is common then the topmost LSE of base may be updated using
5440 struct ovs_key_mpls mpls_key
;
5442 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
5443 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
5444 &mpls_key
, sizeof mpls_key
);
5445 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
5448 /* Otherwise, if there more LSEs in base than are common between
5449 * base and flow then pop the topmost one. */
5453 /* If all the LSEs are to be popped and this is not the outermost
5454 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
5455 * POP_MPLS action instead of flow->dl_type.
5457 * This is because the POP_MPLS action requires its ethertype
5458 * argument to be an MPLS ethernet type but in this case
5459 * flow->dl_type will be a non-MPLS ethernet type.
5461 * When the final POP_MPLS action occurs it use flow->dl_type and
5462 * the and the resulting packet will have the desired dl_type. */
5463 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
5464 dl_type
= htons(ETH_TYPE_MPLS
);
5466 dl_type
= flow
->dl_type
;
5468 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
5469 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
5475 /* If, after the above popping and setting, there are more LSEs in flow
5476 * than base then some LSEs need to be pushed. */
5477 while (base_n
< flow_n
) {
5478 struct ovs_action_push_mpls
*mpls
;
5480 mpls
= nl_msg_put_unspec_zero(odp_actions
,
5481 OVS_ACTION_ATTR_PUSH_MPLS
,
5483 mpls
->mpls_ethertype
= flow
->dl_type
;
5484 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
5485 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
);
5486 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
5492 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
5494 ipv4
->ipv4_src
= flow
->nw_src
;
5495 ipv4
->ipv4_dst
= flow
->nw_dst
;
5496 ipv4
->ipv4_proto
= flow
->nw_proto
;
5497 ipv4
->ipv4_tos
= flow
->nw_tos
;
5498 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
5499 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5503 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
5505 flow
->nw_src
= ipv4
->ipv4_src
;
5506 flow
->nw_dst
= ipv4
->ipv4_dst
;
5507 flow
->nw_proto
= ipv4
->ipv4_proto
;
5508 flow
->nw_tos
= ipv4
->ipv4_tos
;
5509 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
5510 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
5514 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
5515 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5518 struct ovs_key_ipv4 key
, mask
, base
;
5520 /* Check that nw_proto and nw_frag remain unchanged. */
5521 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5522 flow
->nw_frag
== base_flow
->nw_frag
);
5524 get_ipv4_key(flow
, &key
, false);
5525 get_ipv4_key(base_flow
, &base
, false);
5526 get_ipv4_key(&wc
->masks
, &mask
, true);
5527 mask
.ipv4_proto
= 0; /* Not writeable. */
5528 mask
.ipv4_frag
= 0; /* Not writable. */
5530 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5532 put_ipv4_key(&base
, base_flow
, false);
5533 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
5534 put_ipv4_key(&mask
, &wc
->masks
, true);
5540 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
5542 memcpy(ipv6
->ipv6_src
, &flow
->ipv6_src
, sizeof ipv6
->ipv6_src
);
5543 memcpy(ipv6
->ipv6_dst
, &flow
->ipv6_dst
, sizeof ipv6
->ipv6_dst
);
5544 ipv6
->ipv6_label
= flow
->ipv6_label
;
5545 ipv6
->ipv6_proto
= flow
->nw_proto
;
5546 ipv6
->ipv6_tclass
= flow
->nw_tos
;
5547 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
5548 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
5552 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
5554 memcpy(&flow
->ipv6_src
, ipv6
->ipv6_src
, sizeof flow
->ipv6_src
);
5555 memcpy(&flow
->ipv6_dst
, ipv6
->ipv6_dst
, sizeof flow
->ipv6_dst
);
5556 flow
->ipv6_label
= ipv6
->ipv6_label
;
5557 flow
->nw_proto
= ipv6
->ipv6_proto
;
5558 flow
->nw_tos
= ipv6
->ipv6_tclass
;
5559 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
5560 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
5564 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
5565 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5568 struct ovs_key_ipv6 key
, mask
, base
;
5570 /* Check that nw_proto and nw_frag remain unchanged. */
5571 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
5572 flow
->nw_frag
== base_flow
->nw_frag
);
5574 get_ipv6_key(flow
, &key
, false);
5575 get_ipv6_key(base_flow
, &base
, false);
5576 get_ipv6_key(&wc
->masks
, &mask
, true);
5577 mask
.ipv6_proto
= 0; /* Not writeable. */
5578 mask
.ipv6_frag
= 0; /* Not writable. */
5580 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5582 put_ipv6_key(&base
, base_flow
, false);
5583 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
5584 put_ipv6_key(&mask
, &wc
->masks
, true);
5590 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
5592 /* ARP key has padding, clear it. */
5593 memset(arp
, 0, sizeof *arp
);
5595 arp
->arp_sip
= flow
->nw_src
;
5596 arp
->arp_tip
= flow
->nw_dst
;
5597 arp
->arp_op
= htons(flow
->nw_proto
);
5598 arp
->arp_sha
= flow
->arp_sha
;
5599 arp
->arp_tha
= flow
->arp_tha
;
5603 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
5605 flow
->nw_src
= arp
->arp_sip
;
5606 flow
->nw_dst
= arp
->arp_tip
;
5607 flow
->nw_proto
= ntohs(arp
->arp_op
);
5608 flow
->arp_sha
= arp
->arp_sha
;
5609 flow
->arp_tha
= arp
->arp_tha
;
5612 static enum slow_path_reason
5613 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
5614 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5616 struct ovs_key_arp key
, mask
, base
;
5618 get_arp_key(flow
, &key
);
5619 get_arp_key(base_flow
, &base
);
5620 get_arp_key(&wc
->masks
, &mask
);
5622 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
5624 put_arp_key(&base
, base_flow
);
5625 put_arp_key(&mask
, &wc
->masks
);
5632 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
5634 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5635 icmp
->icmp_type
= ntohs(flow
->tp_src
);
5636 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
5640 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
5642 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
5643 flow
->tp_src
= htons(icmp
->icmp_type
);
5644 flow
->tp_dst
= htons(icmp
->icmp_code
);
5647 static enum slow_path_reason
5648 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
5649 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
5651 struct ovs_key_icmp key
, mask
, base
;
5652 enum ovs_key_attr attr
;
5654 if (is_icmpv4(flow
)) {
5655 attr
= OVS_KEY_ATTR_ICMP
;
5656 } else if (is_icmpv6(flow
)) {
5657 attr
= OVS_KEY_ATTR_ICMPV6
;
5662 get_icmp_key(flow
, &key
);
5663 get_icmp_key(base_flow
, &base
);
5664 get_icmp_key(&wc
->masks
, &mask
);
5666 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
5667 put_icmp_key(&base
, base_flow
);
5668 put_icmp_key(&mask
, &wc
->masks
);
5675 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
5677 memcpy(nd
->nd_target
, &flow
->nd_target
, sizeof flow
->nd_target
);
5678 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5679 nd
->nd_sll
= flow
->arp_sha
;
5680 nd
->nd_tll
= flow
->arp_tha
;
5684 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
5686 memcpy(&flow
->nd_target
, nd
->nd_target
, sizeof flow
->nd_target
);
5687 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
5688 flow
->arp_sha
= nd
->nd_sll
;
5689 flow
->arp_tha
= nd
->nd_tll
;
5692 static enum slow_path_reason
5693 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
5694 struct ofpbuf
*odp_actions
,
5695 struct flow_wildcards
*wc
, bool use_masked
)
5697 struct ovs_key_nd key
, mask
, base
;
5699 get_nd_key(flow
, &key
);
5700 get_nd_key(base_flow
, &base
);
5701 get_nd_key(&wc
->masks
, &mask
);
5703 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5705 put_nd_key(&base
, base_flow
);
5706 put_nd_key(&mask
, &wc
->masks
);
5713 static enum slow_path_reason
5714 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
5715 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5718 /* Check if 'flow' really has an L3 header. */
5719 if (!flow
->nw_proto
) {
5723 switch (ntohs(base
->dl_type
)) {
5725 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
5729 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
5730 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
5733 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
5739 /* TCP, UDP, and SCTP keys have the same layout. */
5740 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
5741 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
5744 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
5746 tp
->tcp
.tcp_src
= flow
->tp_src
;
5747 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
5751 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
5753 flow
->tp_src
= tp
->tcp
.tcp_src
;
5754 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
5758 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
5759 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5762 enum ovs_key_attr key_type
;
5763 union ovs_key_tp key
, mask
, base
;
5765 /* Check if 'flow' really has an L3 header. */
5766 if (!flow
->nw_proto
) {
5770 if (!is_ip_any(base_flow
)) {
5774 if (flow
->nw_proto
== IPPROTO_TCP
) {
5775 key_type
= OVS_KEY_ATTR_TCP
;
5776 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5777 key_type
= OVS_KEY_ATTR_UDP
;
5778 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5779 key_type
= OVS_KEY_ATTR_SCTP
;
5784 get_tp_key(flow
, &key
);
5785 get_tp_key(base_flow
, &base
);
5786 get_tp_key(&wc
->masks
, &mask
);
5788 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
5790 put_tp_key(&base
, base_flow
);
5791 put_tp_key(&mask
, &wc
->masks
);
5796 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
5797 struct ofpbuf
*odp_actions
,
5798 struct flow_wildcards
*wc
,
5801 uint32_t key
, mask
, base
;
5803 key
= flow
->skb_priority
;
5804 base
= base_flow
->skb_priority
;
5805 mask
= wc
->masks
.skb_priority
;
5807 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
5808 sizeof key
, odp_actions
)) {
5809 base_flow
->skb_priority
= base
;
5810 wc
->masks
.skb_priority
= mask
;
5815 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
5816 struct ofpbuf
*odp_actions
,
5817 struct flow_wildcards
*wc
,
5820 uint32_t key
, mask
, base
;
5822 key
= flow
->pkt_mark
;
5823 base
= base_flow
->pkt_mark
;
5824 mask
= wc
->masks
.pkt_mark
;
5826 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
5827 sizeof key
, odp_actions
)) {
5828 base_flow
->pkt_mark
= base
;
5829 wc
->masks
.pkt_mark
= mask
;
5833 /* If any of the flow key data that ODP actions can modify are different in
5834 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
5835 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
5836 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
5837 * in addition to this function if needed. Sets fields in 'wc' that are
5838 * used as part of the action.
5840 * Returns a reason to force processing the flow's packets into the userspace
5841 * slow path, if there is one, otherwise 0. */
5842 enum slow_path_reason
5843 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
5844 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
5847 enum slow_path_reason slow1
, slow2
;
5849 commit_set_ether_addr_action(flow
, base
, odp_actions
, wc
, use_masked
);
5850 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
5851 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
5852 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
5853 commit_mpls_action(flow
, base
, odp_actions
);
5854 commit_vlan_action(flow
->vlan_tci
, base
, odp_actions
, wc
);
5855 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
5856 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
5858 return slow1
? slow1
: slow2
;