2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019 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 <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 #include <netinet/icmp6.h>
26 #include <netinet/ip6.h>
30 #include "byte-order.h"
33 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/ofpbuf.h"
40 #include "tun-metadata.h"
41 #include "unaligned.h"
44 #include "openvswitch/vlog.h"
45 #include "openvswitch/match.h"
46 #include "odp-netlink-macros.h"
49 VLOG_DEFINE_THIS_MODULE(odp_util
);
51 /* The interface between userspace and kernel uses an "OVS_*" prefix.
52 * Since this is fairly non-specific for the OVS userspace components,
53 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
54 * interactions with the datapath.
57 /* The set of characters that may separate one action or one key attribute
59 static const char *delimiters
= ", \t\r\n";
60 static const char *delimiters_end
= ", \t\r\n)";
62 #define MAX_ODP_NESTED 32
64 struct parse_odp_context
{
65 const struct simap
*port_names
;
66 int depth
; /* Current nested depth of odp string. */
69 static int parse_odp_key_mask_attr(struct parse_odp_context
*, const char *,
70 struct ofpbuf
*, struct ofpbuf
*);
72 static int parse_odp_key_mask_attr__(struct parse_odp_context
*, const char *,
73 struct ofpbuf
*, struct ofpbuf
*);
75 static void format_odp_key_attr(const struct nlattr
*a
,
76 const struct nlattr
*ma
,
77 const struct hmap
*portno_names
, struct ds
*ds
,
81 struct geneve_opt d
[63];
85 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
86 struct geneve_scan
*mask
);
87 static void format_geneve_opts(const struct geneve_opt
*opt
,
88 const struct geneve_opt
*mask
, int opts_len
,
89 struct ds
*, bool verbose
);
91 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
92 int max
, struct ofpbuf
*,
93 const struct nlattr
*key
);
94 static void format_u128(struct ds
*d
, const ovs_32aligned_u128
*key
,
95 const ovs_32aligned_u128
*mask
, bool verbose
);
96 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
98 static int parse_odp_action(struct parse_odp_context
*context
, const char *s
,
99 struct ofpbuf
*actions
);
101 static int parse_odp_action__(struct parse_odp_context
*context
, const char *s
,
102 struct ofpbuf
*actions
);
104 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
107 * - For an action whose argument has a fixed length, returned that
108 * nonnegative length in bytes.
110 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
112 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
114 odp_action_len(uint16_t type
)
116 if (type
> OVS_ACTION_ATTR_MAX
) {
120 switch ((enum ovs_action_attr
) type
) {
121 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
122 case OVS_ACTION_ATTR_LB_OUTPUT
: return sizeof(uint32_t);
123 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
124 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
125 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
126 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
127 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
128 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
129 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
130 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
131 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
132 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
133 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
134 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
135 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
136 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
137 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
138 case OVS_ACTION_ATTR_CT_CLEAR
: return 0;
139 case OVS_ACTION_ATTR_PUSH_ETH
: return sizeof(struct ovs_action_push_eth
);
140 case OVS_ACTION_ATTR_POP_ETH
: return 0;
141 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
142 case OVS_ACTION_ATTR_PUSH_NSH
: return ATTR_LEN_VARIABLE
;
143 case OVS_ACTION_ATTR_POP_NSH
: return 0;
144 case OVS_ACTION_ATTR_CHECK_PKT_LEN
: return ATTR_LEN_VARIABLE
;
145 case OVS_ACTION_ATTR_DROP
: return sizeof(uint32_t);
147 case OVS_ACTION_ATTR_UNSPEC
:
148 case __OVS_ACTION_ATTR_MAX
:
149 return ATTR_LEN_INVALID
;
152 return ATTR_LEN_INVALID
;
155 /* Returns a string form of 'attr'. The return value is either a statically
156 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
157 * should be at least OVS_KEY_ATTR_BUFSIZE. */
158 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
160 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
163 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
164 case OVS_KEY_ATTR_ENCAP
: return "encap";
165 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
166 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
167 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
168 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
169 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
170 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
171 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: return "ct_tuple4";
172 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: return "ct_tuple6";
173 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
174 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
175 case OVS_KEY_ATTR_ETHERNET
: return "eth";
176 case OVS_KEY_ATTR_VLAN
: return "vlan";
177 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
178 case OVS_KEY_ATTR_IPV4
: return "ipv4";
179 case OVS_KEY_ATTR_IPV6
: return "ipv6";
180 case OVS_KEY_ATTR_TCP
: return "tcp";
181 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
182 case OVS_KEY_ATTR_UDP
: return "udp";
183 case OVS_KEY_ATTR_SCTP
: return "sctp";
184 case OVS_KEY_ATTR_ICMP
: return "icmp";
185 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
186 case OVS_KEY_ATTR_ARP
: return "arp";
187 case OVS_KEY_ATTR_ND
: return "nd";
188 case OVS_KEY_ATTR_ND_EXTENSIONS
: return "nd_ext";
189 case OVS_KEY_ATTR_MPLS
: return "mpls";
190 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
191 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
192 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
193 case OVS_KEY_ATTR_NSH
: return "nsh";
195 case __OVS_KEY_ATTR_MAX
:
197 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
203 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
205 size_t len
= nl_attr_get_size(a
);
207 ds_put_format(ds
, "action%d", nl_attr_type(a
));
209 const uint8_t *unspec
;
212 unspec
= nl_attr_get(a
);
213 for (i
= 0; i
< len
; i
++) {
214 ds_put_char(ds
, i
? ' ': '(');
215 ds_put_format(ds
, "%02x", unspec
[i
]);
217 ds_put_char(ds
, ')');
222 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
223 const struct hmap
*portno_names
)
225 static const struct nl_policy ovs_sample_policy
[] = {
226 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
227 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
229 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
231 const struct nlattr
*nla_acts
;
234 ds_put_cstr(ds
, "sample");
236 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
237 ds_put_cstr(ds
, "(error)");
241 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
244 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
246 ds_put_cstr(ds
, "actions(");
247 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
248 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
249 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
250 ds_put_format(ds
, "))");
254 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
255 const struct hmap
*portno_names
)
257 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
258 int len
= nl_attr_get_size(attr
);
260 ds_put_cstr(ds
, "clone");
261 ds_put_format(ds
, "(");
262 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
263 ds_put_format(ds
, ")");
267 format_nsh_key(struct ds
*ds
, const struct ovs_key_nsh
*key
)
269 ds_put_format(ds
, "flags=%d", key
->flags
);
270 ds_put_format(ds
, ",ttl=%d", key
->ttl
);
271 ds_put_format(ds
, ",mdtype=%d", key
->mdtype
);
272 ds_put_format(ds
, ",np=%d", key
->np
);
273 ds_put_format(ds
, ",spi=0x%x",
274 nsh_path_hdr_to_spi_uint32(key
->path_hdr
));
275 ds_put_format(ds
, ",si=%d",
276 nsh_path_hdr_to_si(key
->path_hdr
));
278 switch (key
->mdtype
) {
280 for (int i
= 0; i
< 4; i
++) {
281 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->context
[i
]));
286 /* No support for matching other metadata formats yet. */
292 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
293 uint8_t value
, uint8_t mask
)
299 ds_put_format(s
, "%s=", name
);
300 if (mask
== UINT8_MAX
) {
301 ds_put_format(s
, "%"PRIu8
, value
);
303 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
310 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
311 ovs_be32 value
, ovs_be32 mask
)
313 if (mask
!= htonl(0)) {
317 ds_put_format(s
, "%s=", name
);
318 if (mask
== OVS_BE32_MAX
) {
319 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
321 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
322 ntohl(value
), ntohl(mask
));
329 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
330 const struct ovs_key_nsh
*mask
)
333 format_nsh_key(ds
, key
);
336 uint32_t spi
= nsh_path_hdr_to_spi_uint32(key
->path_hdr
);
337 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
->path_hdr
);
338 if (spi_mask
== (NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
339 spi_mask
= UINT32_MAX
;
341 uint8_t si
= nsh_path_hdr_to_si(key
->path_hdr
);
342 uint8_t si_mask
= nsh_path_hdr_to_si(mask
->path_hdr
);
344 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
345 format_uint8_masked(ds
, &first
, "ttl", key
->ttl
, mask
->ttl
);
346 format_uint8_masked(ds
, &first
, "mdtype", key
->mdtype
, mask
->mdtype
);
347 format_uint8_masked(ds
, &first
, "np", key
->np
, mask
->np
);
348 format_be32_masked(ds
, &first
, "spi", htonl(spi
), htonl(spi_mask
));
349 format_uint8_masked(ds
, &first
, "si", si
, si_mask
);
350 format_be32_masked(ds
, &first
, "c1", key
->context
[0],
352 format_be32_masked(ds
, &first
, "c2", key
->context
[1],
354 format_be32_masked(ds
, &first
, "c3", key
->context
[2],
356 format_be32_masked(ds
, &first
, "c4", key
->context
[3],
362 format_odp_push_nsh_action(struct ds
*ds
,
363 const struct nsh_hdr
*nsh_hdr
)
365 size_t mdlen
= nsh_hdr_len(nsh_hdr
) - NSH_BASE_HDR_LEN
;
366 uint32_t spi
= ntohl(nsh_get_spi(nsh_hdr
));
367 uint8_t si
= nsh_get_si(nsh_hdr
);
368 uint8_t flags
= nsh_get_flags(nsh_hdr
);
369 uint8_t ttl
= nsh_get_ttl(nsh_hdr
);
371 ds_put_cstr(ds
, "push_nsh(");
372 ds_put_format(ds
, "flags=%d", flags
);
373 ds_put_format(ds
, ",ttl=%d", ttl
);
374 ds_put_format(ds
, ",mdtype=%d", nsh_hdr
->md_type
);
375 ds_put_format(ds
, ",np=%d", nsh_hdr
->next_proto
);
376 ds_put_format(ds
, ",spi=0x%x", spi
);
377 ds_put_format(ds
, ",si=%d", si
);
378 switch (nsh_hdr
->md_type
) {
380 const struct nsh_md1_ctx
*md1_ctx
= &nsh_hdr
->md1
;
381 for (int i
= 0; i
< 4; i
++) {
382 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
383 ntohl(get_16aligned_be32(&md1_ctx
->context
[i
])));
388 const struct nsh_md2_tlv
*md2_ctx
= &nsh_hdr
->md2
;
389 ds_put_cstr(ds
, ",md2=");
390 ds_put_hex(ds
, md2_ctx
, mdlen
);
396 ds_put_format(ds
, ")");
400 slow_path_reason_to_string(uint32_t reason
)
402 switch ((enum slow_path_reason
) reason
) {
403 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
412 slow_path_reason_to_explanation(enum slow_path_reason reason
)
415 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
424 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
425 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
427 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
428 res_flags
, allowed
, res_mask
);
432 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
433 const struct hmap
*portno_names
)
435 static const struct nl_policy ovs_userspace_policy
[] = {
436 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
437 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
439 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
441 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
444 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
445 const struct nlattr
*userdata_attr
;
446 const struct nlattr
*tunnel_out_port_attr
;
448 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
449 ds_put_cstr(ds
, "userspace(error)");
453 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
454 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
456 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
459 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
460 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
461 bool userdata_unspec
= true;
462 struct user_action_cookie cookie
;
464 if (userdata_len
== sizeof cookie
) {
465 memcpy(&cookie
, userdata
, sizeof cookie
);
467 userdata_unspec
= false;
469 if (cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
470 ds_put_format(ds
, ",sFlow("
471 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
472 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
473 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
474 cookie
.sflow
.output
);
475 } else if (cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
476 ds_put_cstr(ds
, ",slow_path(");
477 format_flags(ds
, slow_path_reason_to_string
,
478 cookie
.slow_path
.reason
, ',');
479 ds_put_format(ds
, ")");
480 } else if (cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
481 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
482 ",collector_set_id=%"PRIu32
483 ",obs_domain_id=%"PRIu32
484 ",obs_point_id=%"PRIu32
486 cookie
.flow_sample
.probability
,
487 cookie
.flow_sample
.collector_set_id
,
488 cookie
.flow_sample
.obs_domain_id
,
489 cookie
.flow_sample
.obs_point_id
);
490 odp_portno_name_format(portno_names
,
491 cookie
.flow_sample
.output_odp_port
, ds
);
492 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
493 ds_put_cstr(ds
, ",ingress");
494 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
495 ds_put_cstr(ds
, ",egress");
497 ds_put_char(ds
, ')');
498 } else if (cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
499 ds_put_format(ds
, ",ipfix(output_port=");
500 odp_portno_name_format(portno_names
,
501 cookie
.ipfix
.output_odp_port
, ds
);
502 ds_put_char(ds
, ')');
503 } else if (cookie
.type
== USER_ACTION_COOKIE_CONTROLLER
) {
504 ds_put_format(ds
, ",controller(reason=%"PRIu16
508 ",rule_cookie=%#"PRIx64
509 ",controller_id=%"PRIu16
511 cookie
.controller
.reason
,
512 !!cookie
.controller
.dont_send
,
513 !!cookie
.controller
.continuation
,
514 cookie
.controller
.recirc_id
,
515 ntohll(get_32aligned_be64(
516 &cookie
.controller
.rule_cookie
)),
517 cookie
.controller
.controller_id
,
518 cookie
.controller
.max_len
);
519 ds_put_char(ds
, ')');
521 userdata_unspec
= true;
525 if (userdata_unspec
) {
527 ds_put_format(ds
, ",userdata(");
528 for (i
= 0; i
< userdata_len
; i
++) {
529 ds_put_format(ds
, "%02x", userdata
[i
]);
531 ds_put_char(ds
, ')');
535 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
536 ds_put_cstr(ds
, ",actions");
539 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
540 if (tunnel_out_port_attr
) {
541 ds_put_format(ds
, ",tunnel_out_port=");
542 odp_portno_name_format(portno_names
,
543 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
546 ds_put_char(ds
, ')');
550 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
552 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
553 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
554 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
555 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
557 ds_put_char(ds
, ',');
559 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
560 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
561 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
562 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
564 ds_put_char(ds
, ',');
566 if (!(tci
& htons(VLAN_CFI
))) {
567 ds_put_cstr(ds
, "cfi=0");
568 ds_put_char(ds
, ',');
574 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
576 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
577 mpls_lse_to_label(mpls_lse
),
578 mpls_lse_to_tc(mpls_lse
),
579 mpls_lse_to_ttl(mpls_lse
),
580 mpls_lse_to_bos(mpls_lse
));
584 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
585 const struct ovs_key_mpls
*mpls_mask
, int n
)
587 for (int i
= 0; i
< n
; i
++) {
588 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
590 if (mpls_mask
== NULL
) {
591 format_mpls_lse(ds
, key
);
593 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
595 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
596 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
597 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
598 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
599 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
601 ds_put_char(ds
, ',');
607 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
609 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
613 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
615 ds_put_format(ds
, "hash(");
617 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
618 ds_put_format(ds
, "l4(%"PRIu32
")", hash_act
->hash_basis
);
619 } else if (hash_act
->hash_alg
== OVS_HASH_ALG_SYM_L4
) {
620 ds_put_format(ds
, "sym_l4(%"PRIu32
")", hash_act
->hash_basis
);
622 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
625 ds_put_format(ds
, ")");
629 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
631 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
632 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
633 ntohs(udp
->udp_csum
));
639 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
641 const struct eth_header
*eth
;
644 const struct udp_header
*udp
;
646 eth
= (const struct eth_header
*)data
->header
;
651 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
652 data
->header_len
, data
->tnl_type
);
653 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
654 ds_put_format(ds
, ",src=");
655 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
656 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
658 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
660 const struct ip_header
*ip
= l3
;
661 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
662 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
663 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
664 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
665 ip
->ip_proto
, ip
->ip_tos
,
667 ntohs(ip
->ip_frag_off
));
670 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
671 struct in6_addr src
, dst
;
672 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
673 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
674 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
676 ds_put_format(ds
, "ipv6(src=");
677 ipv6_format_addr(&src
, ds
);
678 ds_put_format(ds
, ",dst=");
679 ipv6_format_addr(&dst
, ds
);
680 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
681 ",hlimit=%"PRIu8
"),",
682 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
683 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
687 udp
= (const struct udp_header
*) l4
;
689 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
690 const struct vxlanhdr
*vxh
;
692 vxh
= format_udp_tnl_push_header(ds
, udp
);
694 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
695 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
696 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
697 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
698 const struct genevehdr
*gnh
;
700 gnh
= format_udp_tnl_push_header(ds
, udp
);
702 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
703 gnh
->oam
? "oam," : "",
704 gnh
->critical
? "crit," : "",
705 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
708 ds_put_cstr(ds
, ",options(");
709 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
711 ds_put_char(ds
, ')');
714 ds_put_char(ds
, ')');
715 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
716 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
717 const struct gre_base_hdr
*greh
;
718 ovs_16aligned_be32
*options
;
720 greh
= (const struct gre_base_hdr
*) l4
;
722 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
723 ntohs(greh
->flags
), ntohs(greh
->protocol
));
724 options
= (ovs_16aligned_be32
*)(greh
+ 1);
725 if (greh
->flags
& htons(GRE_CSUM
)) {
726 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
729 if (greh
->flags
& htons(GRE_KEY
)) {
730 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
733 if (greh
->flags
& htons(GRE_SEQ
)) {
734 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
737 ds_put_format(ds
, ")");
738 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
739 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
740 const struct gre_base_hdr
*greh
;
741 const struct erspan_base_hdr
*ersh
;
743 greh
= (const struct gre_base_hdr
*) l4
;
744 ersh
= ERSPAN_HDR(greh
);
746 if (ersh
->ver
== 1) {
747 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
749 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
750 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
751 } else if (ersh
->ver
== 2) {
752 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
754 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
755 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
756 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
758 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
760 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GTPU
) {
761 const struct gtpuhdr
*gtph
;
763 gtph
= format_udp_tnl_push_header(ds
, udp
);
765 ds_put_format(ds
, "gtpu(flags=0x%"PRIx8
766 ",msgtype=%"PRIu8
",teid=0x%"PRIx32
")",
767 gtph
->md
.flags
, gtph
->md
.msgtype
,
768 ntohl(get_16aligned_be32(>ph
->teid
)));
771 ds_put_format(ds
, ")");
775 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
776 const struct hmap
*portno_names
)
778 struct ovs_action_push_tnl
*data
;
780 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
782 ds_put_cstr(ds
, "tnl_push(tnl_port(");
783 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
784 ds_put_cstr(ds
, "),");
785 format_odp_tnl_push_header(ds
, data
);
786 ds_put_format(ds
, ",out_port(");
787 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
788 ds_put_cstr(ds
, "))");
791 static const struct nl_policy ovs_nat_policy
[] = {
792 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
793 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
794 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
795 .min_len
= sizeof(struct in_addr
),
796 .max_len
= sizeof(struct in6_addr
)},
797 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
798 .min_len
= sizeof(struct in_addr
),
799 .max_len
= sizeof(struct in6_addr
)},
800 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
801 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
802 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
803 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
804 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
808 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
810 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
812 ovs_be32 ip_min
, ip_max
;
813 struct in6_addr ip6_min
, ip6_max
;
814 uint16_t proto_min
, proto_max
;
816 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
817 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
820 /* If no type, then nothing else either. */
821 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
822 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
823 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
824 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
825 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
826 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
829 /* Both SNAT & DNAT may not be specified. */
830 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
831 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
834 /* proto may not appear without ip. */
835 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
836 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
839 /* MAX may not appear without MIN. */
840 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
841 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
842 ds_put_cstr(ds
, "nat(error: range max without min.)");
845 /* Address sizes must match. */
846 if ((a
[OVS_NAT_ATTR_IP_MIN
]
847 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
848 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
849 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
850 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
851 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
852 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
856 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
857 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
858 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
859 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
860 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
861 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
862 if (addr_len
== sizeof ip6_min
) {
863 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
864 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
866 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
867 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
870 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
871 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
872 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
873 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
875 if ((addr_len
== sizeof(ovs_be32
)
876 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
877 || (addr_len
== sizeof(struct in6_addr
)
878 && !ipv6_mask_is_any(&ip6_max
)
879 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
880 || (proto_max
&& proto_min
> proto_max
)) {
881 ds_put_cstr(ds
, "nat(range error)");
885 ds_put_cstr(ds
, "nat");
886 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
887 ds_put_char(ds
, '(');
888 if (a
[OVS_NAT_ATTR_SRC
]) {
889 ds_put_cstr(ds
, "src");
890 } else if (a
[OVS_NAT_ATTR_DST
]) {
891 ds_put_cstr(ds
, "dst");
895 ds_put_cstr(ds
, "=");
897 if (addr_len
== sizeof ip_min
) {
898 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
900 if (ip_max
&& ip_max
!= ip_min
) {
901 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
903 } else if (addr_len
== sizeof ip6_min
) {
904 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
906 if (!ipv6_mask_is_any(&ip6_max
) &&
907 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
908 ds_put_char(ds
, '-');
909 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
913 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
915 if (proto_max
&& proto_max
!= proto_min
) {
916 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
920 ds_put_char(ds
, ',');
921 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
922 ds_put_cstr(ds
, "persistent,");
924 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
925 ds_put_cstr(ds
, "hash,");
927 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
928 ds_put_cstr(ds
, "random,");
931 ds_put_char(ds
, ')');
935 static const struct nl_policy ovs_conntrack_policy
[] = {
936 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
937 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
938 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
939 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
940 .min_len
= sizeof(uint32_t) * 2 },
941 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
942 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
943 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
944 .min_len
= 1, .max_len
= 16 },
945 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
946 [OVS_CT_ATTR_TIMEOUT
] = { .type
= NL_A_STRING
, .optional
= true,
947 .min_len
= 1, .max_len
= 32 },
951 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
953 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
955 ovs_32aligned_u128 value
;
956 ovs_32aligned_u128 mask
;
958 const uint32_t *mark
;
959 const char *helper
, *timeout
;
962 const struct nlattr
*nat
;
964 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
965 ds_put_cstr(ds
, "ct(error)");
969 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
970 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
971 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
972 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
973 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
974 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
975 timeout
= a
[OVS_CT_ATTR_TIMEOUT
] ?
976 nl_attr_get(a
[OVS_CT_ATTR_TIMEOUT
]) : NULL
;
977 nat
= a
[OVS_CT_ATTR_NAT
];
979 ds_put_format(ds
, "ct");
980 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| timeout
|| nat
) {
981 ds_put_cstr(ds
, "(");
983 ds_put_format(ds
, "commit,");
986 ds_put_format(ds
, "force_commit,");
989 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
992 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
996 ds_put_format(ds
, "label=");
997 format_u128(ds
, &label
->value
, &label
->mask
, true);
998 ds_put_char(ds
, ',');
1001 ds_put_format(ds
, "helper=%s,", helper
);
1004 ds_put_format(ds
, "timeout=%s", timeout
);
1007 format_odp_ct_nat(ds
, nat
);
1010 ds_put_cstr(ds
, ")");
1014 static const struct attr_len_tbl
1015 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
1016 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
1017 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
1018 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
1022 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
1025 const struct nlattr
*a
;
1026 struct ovs_key_nsh nsh
;
1027 struct ovs_key_nsh nsh_mask
;
1029 memset(&nsh
, 0, sizeof nsh
);
1030 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
1032 NL_NESTED_FOR_EACH (a
, left
, attr
) {
1033 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
1034 size_t len
= nl_attr_get_size(a
);
1036 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1040 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1041 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1046 case OVS_NSH_KEY_ATTR_UNSPEC
:
1048 case OVS_NSH_KEY_ATTR_BASE
: {
1049 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1050 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1051 memcpy(&nsh
, base
, sizeof(*base
));
1052 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1055 case OVS_NSH_KEY_ATTR_MD1
: {
1056 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1057 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1058 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1059 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1062 case OVS_NSH_KEY_ATTR_MD2
:
1063 case __OVS_NSH_KEY_ATTR_MAX
:
1065 /* No support for matching other metadata formats yet. */
1070 ds_put_cstr(ds
, "set(nsh(");
1071 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1072 ds_put_cstr(ds
, "))");
1076 format_odp_check_pkt_len_action(struct ds
*ds
, const struct nlattr
*attr
,
1077 const struct hmap
*portno_names OVS_UNUSED
)
1079 static const struct nl_policy ovs_cpl_policy
[] = {
1080 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] = { .type
= NL_A_U16
},
1081 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] = { .type
= NL_A_NESTED
},
1082 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
]
1083 = { .type
= NL_A_NESTED
},
1085 struct nlattr
*a
[ARRAY_SIZE(ovs_cpl_policy
)];
1086 ds_put_cstr(ds
, "check_pkt_len");
1087 if (!nl_parse_nested(attr
, ovs_cpl_policy
, a
, ARRAY_SIZE(a
))) {
1088 ds_put_cstr(ds
, "(error)");
1092 if (!a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] ||
1093 !a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
]) {
1094 ds_put_cstr(ds
, "(error)");
1098 uint16_t pkt_len
= nl_attr_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]);
1099 ds_put_format(ds
, "(size=%u,gt(", pkt_len
);
1100 const struct nlattr
*acts
;
1101 acts
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
];
1102 format_odp_actions(ds
, nl_attr_get(acts
), nl_attr_get_size(acts
),
1105 ds_put_cstr(ds
, "),le(");
1106 acts
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
];
1107 format_odp_actions(ds
, nl_attr_get(acts
), nl_attr_get_size(acts
),
1109 ds_put_cstr(ds
, "))");
1113 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1114 const struct hmap
*portno_names
)
1117 enum ovs_action_attr type
= nl_attr_type(a
);
1120 expected_len
= odp_action_len(nl_attr_type(a
));
1121 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1122 nl_attr_get_size(a
) != expected_len
) {
1123 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1124 nl_attr_get_size(a
), expected_len
);
1125 format_generic_odp_action(ds
, a
);
1130 case OVS_ACTION_ATTR_METER
:
1131 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1133 case OVS_ACTION_ATTR_OUTPUT
:
1134 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1136 case OVS_ACTION_ATTR_LB_OUTPUT
:
1137 ds_put_format(ds
, "lb_output(%"PRIu32
")", nl_attr_get_u32(a
));
1139 case OVS_ACTION_ATTR_TRUNC
: {
1140 const struct ovs_action_trunc
*trunc
=
1141 nl_attr_get_unspec(a
, sizeof *trunc
);
1143 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1146 case OVS_ACTION_ATTR_TUNNEL_POP
:
1147 ds_put_cstr(ds
, "tnl_pop(");
1148 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1149 ds_put_char(ds
, ')');
1151 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1152 format_odp_tnl_push_action(ds
, a
, portno_names
);
1154 case OVS_ACTION_ATTR_USERSPACE
:
1155 format_odp_userspace_action(ds
, a
, portno_names
);
1157 case OVS_ACTION_ATTR_RECIRC
:
1158 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1160 case OVS_ACTION_ATTR_HASH
:
1161 format_odp_hash_action(ds
, nl_attr_get(a
));
1163 case OVS_ACTION_ATTR_SET_MASKED
:
1165 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1166 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1167 format_odp_set_nsh(ds
, a
);
1170 size
= nl_attr_get_size(a
) / 2;
1171 ds_put_cstr(ds
, "set(");
1173 /* Masked set action not supported for tunnel key, which is bigger. */
1174 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1175 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1176 sizeof(struct nlattr
))];
1177 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1178 sizeof(struct nlattr
))];
1180 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1181 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1182 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1183 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1184 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1186 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1188 ds_put_cstr(ds
, ")");
1190 case OVS_ACTION_ATTR_SET
:
1191 ds_put_cstr(ds
, "set(");
1192 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1193 ds_put_cstr(ds
, ")");
1195 case OVS_ACTION_ATTR_PUSH_ETH
: {
1196 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1197 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1198 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1199 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1202 case OVS_ACTION_ATTR_POP_ETH
:
1203 ds_put_cstr(ds
, "pop_eth");
1205 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1206 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1207 ds_put_cstr(ds
, "push_vlan(");
1208 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1209 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1211 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1212 ds_put_char(ds
, ')');
1215 case OVS_ACTION_ATTR_POP_VLAN
:
1216 ds_put_cstr(ds
, "pop_vlan");
1218 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1219 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1220 ds_put_cstr(ds
, "push_mpls(");
1221 format_mpls_lse(ds
, mpls
->mpls_lse
);
1222 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1225 case OVS_ACTION_ATTR_POP_MPLS
: {
1226 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1227 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1230 case OVS_ACTION_ATTR_SAMPLE
:
1231 format_odp_sample_action(ds
, a
, portno_names
);
1233 case OVS_ACTION_ATTR_CT
:
1234 format_odp_conntrack_action(ds
, a
);
1236 case OVS_ACTION_ATTR_CT_CLEAR
:
1237 ds_put_cstr(ds
, "ct_clear");
1239 case OVS_ACTION_ATTR_CLONE
:
1240 format_odp_clone_action(ds
, a
, portno_names
);
1242 case OVS_ACTION_ATTR_PUSH_NSH
: {
1243 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1244 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1245 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1246 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1247 format_odp_push_nsh_action(ds
, nsh_hdr
);
1250 case OVS_ACTION_ATTR_POP_NSH
:
1251 ds_put_cstr(ds
, "pop_nsh()");
1253 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
1254 format_odp_check_pkt_len_action(ds
, a
, portno_names
);
1256 case OVS_ACTION_ATTR_DROP
:
1257 ds_put_cstr(ds
, "drop");
1259 case OVS_ACTION_ATTR_UNSPEC
:
1260 case __OVS_ACTION_ATTR_MAX
:
1262 format_generic_odp_action(ds
, a
);
1268 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1269 size_t actions_len
, const struct hmap
*portno_names
)
1272 const struct nlattr
*a
;
1275 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1277 ds_put_char(ds
, ',');
1279 format_odp_action(ds
, a
, portno_names
);
1284 if (left
== actions_len
) {
1285 ds_put_cstr(ds
, "<empty>");
1287 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1288 for (i
= 0; i
< left
; i
++) {
1289 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1291 ds_put_char(ds
, ')');
1294 ds_put_cstr(ds
, "drop");
1298 /* Separate out parse_odp_userspace_action() function. */
1300 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1303 struct user_action_cookie cookie
;
1305 odp_port_t tunnel_out_port
;
1307 void *user_data
= NULL
;
1308 size_t user_data_size
= 0;
1309 bool include_actions
= false;
1312 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1316 ofpbuf_init(&buf
, 16);
1317 memset(&cookie
, 0, sizeof cookie
);
1319 user_data
= &cookie
;
1320 user_data_size
= sizeof cookie
;
1323 uint32_t probability
;
1324 uint32_t collector_set_id
;
1325 uint32_t obs_domain_id
;
1326 uint32_t obs_point_id
;
1328 /* USER_ACTION_COOKIE_CONTROLLER. */
1330 uint8_t continuation
;
1333 uint64_t rule_cookie
;
1334 uint16_t controller_id
;
1339 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1340 "pcp=%i,output=%"SCNi32
")%n",
1341 &vid
, &pcp
, &output
, &n1
)) {
1345 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1350 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1351 cookie
.ofp_in_port
= OFPP_NONE
;
1352 cookie
.ofproto_uuid
= UUID_ZERO
;
1353 cookie
.sflow
.vlan_tci
= htons(tci
);
1354 cookie
.sflow
.output
= output
;
1355 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1358 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1359 cookie
.ofp_in_port
= OFPP_NONE
;
1360 cookie
.ofproto_uuid
= UUID_ZERO
;
1361 cookie
.slow_path
.reason
= 0;
1363 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1364 &cookie
.slow_path
.reason
,
1365 SLOW_PATH_REASON_MASK
, NULL
);
1366 if (res
< 0 || s
[n
+ res
] != ')') {
1370 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1371 "collector_set_id=%"SCNi32
","
1372 "obs_domain_id=%"SCNi32
","
1373 "obs_point_id=%"SCNi32
","
1374 "output_port=%"SCNi32
"%n",
1375 &probability
, &collector_set_id
,
1376 &obs_domain_id
, &obs_point_id
,
1380 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1381 cookie
.ofp_in_port
= OFPP_NONE
;
1382 cookie
.ofproto_uuid
= UUID_ZERO
;
1383 cookie
.flow_sample
.probability
= probability
;
1384 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1385 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1386 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1387 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1389 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1390 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1392 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1393 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1396 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1403 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1406 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1407 cookie
.ofp_in_port
= OFPP_NONE
;
1408 cookie
.ofproto_uuid
= UUID_ZERO
;
1409 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1410 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1412 ",continuation=%"SCNu8
1413 ",recirc_id=%"SCNu32
1414 ",rule_cookie=%"SCNx64
1415 ",controller_id=%"SCNu16
1416 ",max_len=%"SCNu16
")%n",
1417 &reason
, &dont_send
, &continuation
, &recirc_id
,
1418 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1420 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1421 cookie
.ofp_in_port
= OFPP_NONE
;
1422 cookie
.ofproto_uuid
= UUID_ZERO
;
1423 cookie
.controller
.dont_send
= dont_send
? true : false;
1424 cookie
.controller
.continuation
= continuation
? true : false;
1425 cookie
.controller
.reason
= reason
;
1426 cookie
.controller
.recirc_id
= recirc_id
;
1427 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1428 htonll(rule_cookie
));
1429 cookie
.controller
.controller_id
= controller_id
;
1430 cookie
.controller
.max_len
= max_len
;
1431 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1435 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1436 if (end
[0] != ')') {
1440 user_data
= buf
.data
;
1441 user_data_size
= buf
.size
;
1448 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1450 include_actions
= true;
1456 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1457 &tunnel_out_port
, &n1
)) {
1458 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1459 tunnel_out_port
, include_actions
, actions
);
1462 } else if (s
[n
] == ')') {
1463 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1464 ODPP_NONE
, include_actions
, actions
);
1471 struct ovs_action_push_eth push
;
1475 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1476 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1477 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1478 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1481 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1482 &push
, sizeof push
);
1489 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1490 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1497 ofpbuf_uninit(&buf
);
1502 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1504 struct eth_header
*eth
;
1505 struct ip_header
*ip
;
1506 struct ovs_16aligned_ip6_hdr
*ip6
;
1507 struct udp_header
*udp
;
1508 struct gre_base_hdr
*greh
;
1509 struct erspan_base_hdr
*ersh
;
1510 struct erspan_md2
*md2
;
1511 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, udp_csum
, sid
;
1513 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1518 uint8_t gtpu_flags
, gtpu_msgtype
;
1520 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1523 eth
= (struct eth_header
*) data
->header
;
1524 l3
= (struct ip_header
*) (eth
+ 1);
1525 ip
= (struct ip_header
*) l3
;
1526 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1527 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1528 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1531 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1535 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1536 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1539 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1542 eth
->eth_type
= htons(dl_type
);
1544 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1546 uint16_t ip_frag_off
;
1547 memset(ip
, 0, sizeof(*ip
));
1548 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1549 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1552 &ip
->ip_proto
, &ip
->ip_tos
,
1553 &ip
->ip_ttl
, &ip_frag_off
)) {
1556 put_16aligned_be32(&ip
->ip_src
, sip
);
1557 put_16aligned_be32(&ip
->ip_dst
, dip
);
1558 ip
->ip_frag_off
= htons(ip_frag_off
);
1559 ip
->ip_ihl_ver
= IP_IHL_VER(5, 4);
1560 ip_len
= sizeof *ip
;
1561 ip
->ip_csum
= csum(ip
, ip_len
);
1563 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1564 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1565 struct in6_addr sip6
, dip6
;
1568 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1569 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1570 ",hlimit=%"SCNi8
"),",
1571 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1572 &tclass
, &ip6
->ip6_hlim
)
1573 || (label
& ~IPV6_LABEL_MASK
) != 0
1574 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1575 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1578 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1579 htonl(tclass
<< 20) | htonl(label
));
1580 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1581 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1582 ip_len
= sizeof *ip6
;
1586 l4
= ((uint8_t *) l3
+ ip_len
);
1587 udp
= (struct udp_header
*) l4
;
1588 greh
= (struct gre_base_hdr
*) l4
;
1589 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1590 &udp_src
, &udp_dst
, &udp_csum
)) {
1591 uint32_t vx_flags
, vni
;
1593 udp
->udp_src
= htons(udp_src
);
1594 udp
->udp_dst
= htons(udp_dst
);
1596 udp
->udp_csum
= htons(udp_csum
);
1598 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1600 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1602 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1603 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1604 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1605 header_len
= sizeof *eth
+ ip_len
+
1606 sizeof *udp
+ sizeof *vxh
;
1607 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1608 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1610 memset(gnh
, 0, sizeof *gnh
);
1611 header_len
= sizeof *eth
+ ip_len
+
1612 sizeof *udp
+ sizeof *gnh
;
1614 if (ovs_scan_len(s
, &n
, "oam,")) {
1617 if (ovs_scan_len(s
, &n
, "crit,")) {
1620 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1623 if (ovs_scan_len(s
, &n
, ",options(")) {
1624 struct geneve_scan options
;
1627 memset(&options
, 0, sizeof options
);
1628 len
= scan_geneve(s
+ n
, &options
, NULL
);
1633 memcpy(gnh
->options
, options
.d
, options
.len
);
1634 gnh
->opt_len
= options
.len
/ 4;
1635 header_len
+= options
.len
;
1639 if (!ovs_scan_len(s
, &n
, "))")) {
1643 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1644 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1645 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1649 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1650 &gre_flags
, &gre_proto
)){
1652 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1653 tnl_type
= OVS_VPORT_TYPE_GRE
;
1655 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1657 greh
->flags
= htons(gre_flags
);
1658 greh
->protocol
= htons(gre_proto
);
1659 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1661 if (greh
->flags
& htons(GRE_CSUM
)) {
1663 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1667 memset(options
, 0, sizeof *options
);
1668 *((ovs_be16
*)options
) = htons(csum
);
1671 if (greh
->flags
& htons(GRE_KEY
)) {
1674 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1678 put_16aligned_be32(options
, htonl(key
));
1681 if (greh
->flags
& htons(GRE_SEQ
)) {
1684 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1687 put_16aligned_be32(options
, htonl(seq
));
1691 if (!ovs_scan_len(s
, &n
, "))")) {
1695 header_len
= sizeof *eth
+ ip_len
+
1696 ((uint8_t *) options
- (uint8_t *) greh
);
1697 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1698 &sid
, &erspan_idx
)) {
1699 ersh
= ERSPAN_HDR(greh
);
1700 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1703 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1704 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1706 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1709 greh
->flags
= htons(GRE_SEQ
);
1710 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1714 put_16aligned_be32(index
, htonl(erspan_idx
));
1716 if (!ovs_scan_len(s
, &n
, ")")) {
1719 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1720 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1722 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1723 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1725 ersh
= ERSPAN_HDR(greh
);
1726 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1728 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1729 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1731 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1734 greh
->flags
= htons(GRE_SEQ
);
1735 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1739 set_hwid(md2
, hwid
);
1742 if (!ovs_scan_len(s
, &n
, ")")) {
1746 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1747 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1749 } else if (ovs_scan_len(s
, &n
, "gtpu(flags=%"SCNi8
",msgtype=%"
1750 SCNu8
",teid=0x%"SCNx32
"))",
1751 >pu_flags
, >pu_msgtype
, &teid
)) {
1752 struct gtpuhdr
*gtph
= (struct gtpuhdr
*) (udp
+ 1);
1754 gtph
->md
.flags
= gtpu_flags
;
1755 gtph
->md
.msgtype
= gtpu_msgtype
;
1756 put_16aligned_be32(>ph
->teid
, htonl(teid
));
1757 tnl_type
= OVS_VPORT_TYPE_GTPU
;
1758 header_len
= sizeof *eth
+ ip_len
+
1759 sizeof *udp
+ sizeof *gtph
;
1764 /* check tunnel meta data. */
1765 if (data
->tnl_type
!= tnl_type
) {
1768 if (data
->header_len
!= header_len
) {
1773 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1780 struct ct_nat_params
{
1786 struct in6_addr ip6
;
1790 struct in6_addr ip6
;
1800 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1802 if (ovs_scan_len(s
, n
, "=")) {
1803 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1804 struct in6_addr ipv6
;
1806 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1807 p
->addr_len
= sizeof p
->addr_min
.ip
;
1808 if (ovs_scan_len(s
, n
, "-")) {
1809 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1810 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1814 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1815 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1816 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1817 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1818 p
->addr_min
.ip6
= ipv6
;
1819 if (ovs_scan_len(s
, n
, "-")) {
1820 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1821 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1822 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1823 p
->addr_max
.ip6
= ipv6
;
1831 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1832 if (ovs_scan_len(s
, n
, "-")) {
1833 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1843 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1847 if (ovs_scan_len(s
, &n
, "nat")) {
1848 memset(p
, 0, sizeof *p
);
1850 if (ovs_scan_len(s
, &n
, "(")) {
1854 end
= strchr(s
+ n
, ')');
1861 n
+= strspn(s
+ n
, delimiters
);
1862 if (ovs_scan_len(s
, &n
, "src")) {
1863 int err
= scan_ct_nat_range(s
, &n
, p
);
1870 if (ovs_scan_len(s
, &n
, "dst")) {
1871 int err
= scan_ct_nat_range(s
, &n
, p
);
1878 if (ovs_scan_len(s
, &n
, "persistent")) {
1879 p
->persistent
= true;
1882 if (ovs_scan_len(s
, &n
, "hash")) {
1883 p
->proto_hash
= true;
1886 if (ovs_scan_len(s
, &n
, "random")) {
1887 p
->proto_random
= true;
1893 if (p
->snat
&& p
->dnat
) {
1896 if ((p
->addr_len
!= 0 &&
1897 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1898 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1899 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1902 if (p
->proto_hash
&& p
->proto_random
) {
1912 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1914 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1917 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1918 } else if (p
->dnat
) {
1919 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1923 if (p
->addr_len
!= 0) {
1924 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1926 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1927 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1931 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1932 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1933 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1936 if (p
->persistent
) {
1937 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1939 if (p
->proto_hash
) {
1940 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1942 if (p
->proto_random
) {
1943 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1947 nl_msg_end_nested(actions
, start
);
1951 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1955 if (ovs_scan(s
, "ct")) {
1956 const char *helper
= NULL
, *timeout
= NULL
;
1957 size_t helper_len
= 0, timeout_len
= 0;
1958 bool commit
= false;
1959 bool force_commit
= false;
1964 } ct_mark
= { 0, 0 };
1969 struct ct_nat_params nat_params
;
1970 bool have_nat
= false;
1974 memset(&ct_label
, 0, sizeof(ct_label
));
1977 if (ovs_scan(s
, "(")) {
1980 end
= strchr(s
, ')');
1988 s
+= strspn(s
, delimiters
);
1989 if (ovs_scan(s
, "commit%n", &n
)) {
1994 if (ovs_scan(s
, "force_commit%n", &n
)) {
1995 force_commit
= true;
1999 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
2003 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
2006 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
2009 ct_mark
.mask
= UINT32_MAX
;
2013 if (ovs_scan(s
, "label=%n", &n
)) {
2017 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
2024 if (ovs_scan(s
, "helper=%n", &n
)) {
2026 helper_len
= strcspn(s
, delimiters_end
);
2027 if (!helper_len
|| helper_len
> 15) {
2034 if (ovs_scan(s
, "timeout=%n", &n
)) {
2036 timeout_len
= strcspn(s
, delimiters_end
);
2037 if (!timeout_len
|| timeout_len
> 31) {
2045 n
= scan_ct_nat(s
, &nat_params
);
2050 /* end points to the end of the nested, nat action.
2051 * find the real end. */
2054 /* Nothing matched. */
2059 if (commit
&& force_commit
) {
2063 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
2065 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
2066 } else if (force_commit
) {
2067 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
2070 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
2073 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
2076 if (!ovs_u128_is_zero(ct_label
.mask
)) {
2077 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
2081 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
2085 nl_msg_put_string__(actions
, OVS_CT_ATTR_TIMEOUT
, timeout
,
2089 nl_msg_put_ct_nat(&nat_params
, actions
);
2091 nl_msg_end_nested(actions
, start
);
2098 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
2099 uint8_t * metadata
, size_t md_size
,
2103 struct ovs_nsh_key_base base
;
2105 base
.flags
= nsh
->flags
;
2106 base
.ttl
= nsh
->ttl
;
2107 base
.mdtype
= nsh
->mdtype
;
2109 base
.path_hdr
= nsh
->path_hdr
;
2111 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
2112 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
2115 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2116 sizeof nsh
->context
);
2118 switch (nsh
->mdtype
) {
2120 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2121 sizeof nsh
->context
);
2124 if (metadata
&& md_size
> 0) {
2125 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2130 /* No match support for other MD formats yet. */
2134 nl_msg_end_nested(buf
, nsh_key_ofs
);
2139 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2146 struct ovs_key_nsh nsh
;
2147 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2148 uint8_t md_size
= 0;
2150 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2155 /* The default is NSH_M_TYPE1 */
2158 nsh
.mdtype
= NSH_M_TYPE1
;
2159 nsh
.np
= NSH_P_ETHERNET
;
2160 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2161 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2164 n
+= strspn(s
+ n
, delimiters
);
2169 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2172 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2175 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2176 switch (nsh
.mdtype
) {
2178 /* This is the default format. */;
2181 /* Length will be updated later. */
2190 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2193 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2196 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2199 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2200 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2201 nsh
.context
[0] = htonl(cd
);
2204 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2205 nsh
.context
[1] = htonl(cd
);
2208 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2209 nsh
.context
[2] = htonl(cd
);
2212 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2213 nsh
.context
[3] = htonl(cd
);
2217 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2220 size_t mdlen
, padding
;
2221 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)
2222 && n
/2 <= sizeof metadata
) {
2223 ofpbuf_use_stub(&b
, metadata
, sizeof metadata
);
2224 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2225 /* Pad metadata to 4 bytes. */
2226 padding
= PAD_SIZE(mdlen
, 4);
2228 ofpbuf_put_zeros(&b
, padding
);
2230 md_size
= mdlen
+ padding
;
2241 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2242 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2243 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2244 nl_msg_end_nested(actions
, offset
);
2251 parse_action_list(struct parse_odp_context
*context
, const char *s
,
2252 struct ofpbuf
*actions
)
2259 n
+= strspn(s
+ n
, delimiters
);
2263 retval
= parse_odp_action(context
, s
+ n
, actions
);
2270 if (actions
->size
> UINT16_MAX
) {
2279 parse_odp_action(struct parse_odp_context
*context
, const char *s
,
2280 struct ofpbuf
*actions
)
2286 if (context
->depth
== MAX_ODP_NESTED
) {
2289 retval
= parse_odp_action__(context
, s
, actions
);
2299 parse_odp_action__(struct parse_odp_context
*context
, const char *s
,
2300 struct ofpbuf
*actions
)
2306 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2307 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2316 if (ovs_scan(s
, "lb_output(%"PRIu32
")%n", &bond_id
, &n
)) {
2317 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_LB_OUTPUT
, bond_id
);
2326 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2327 struct ovs_action_trunc
*trunc
;
2329 trunc
= nl_msg_put_unspec_uninit(actions
,
2330 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2331 trunc
->max_len
= max_len
;
2336 if (context
->port_names
) {
2337 int len
= strcspn(s
, delimiters
);
2338 struct simap_node
*node
;
2340 node
= simap_find_len(context
->port_names
, s
, len
);
2342 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2351 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2352 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2357 if (!strncmp(s
, "userspace(", 10)) {
2358 return parse_odp_userspace_action(s
, actions
);
2361 if (!strncmp(s
, "set(", 4)) {
2364 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2365 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2366 struct nlattr
*nested
, *key
;
2369 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2370 retval
= parse_odp_key_mask_attr(context
, s
+ 4, actions
, &maskbuf
);
2372 ofpbuf_uninit(&maskbuf
);
2375 if (s
[retval
+ 4] != ')') {
2376 ofpbuf_uninit(&maskbuf
);
2380 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2383 size
= nl_attr_get_size(mask
);
2384 if (size
== nl_attr_get_size(key
)) {
2385 /* Change to masked set action if not fully masked. */
2386 if (!is_all_ones(mask
+ 1, size
)) {
2387 /* Remove padding of eariler key payload */
2388 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2390 /* Put mask payload right after key payload */
2391 key
->nla_len
+= size
;
2392 ofpbuf_put(actions
, mask
+ 1, size
);
2394 /* 'actions' may have been reallocated by ofpbuf_put(). */
2395 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2396 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2399 /* Add new padding as needed */
2400 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2404 ofpbuf_uninit(&maskbuf
);
2406 nl_msg_end_nested(actions
, start_ofs
);
2411 struct ovs_action_push_vlan push
;
2412 int tpid
= ETH_TYPE_VLAN
;
2417 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2418 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2419 &vid
, &pcp
, &cfi
, &n
)
2420 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2421 &tpid
, &vid
, &pcp
, &n
)
2422 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2423 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2424 if ((vid
& ~(VLAN_VID_MASK
>> VLAN_VID_SHIFT
)) != 0
2425 || (pcp
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) != 0) {
2428 push
.vlan_tpid
= htons(tpid
);
2429 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2430 | (pcp
<< VLAN_PCP_SHIFT
)
2431 | (cfi
? VLAN_CFI
: 0));
2432 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2433 &push
, sizeof push
);
2439 if (!strncmp(s
, "pop_vlan", 8)) {
2440 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2445 unsigned long long int meter_id
;
2448 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2449 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2458 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2459 && percentage
>= 0. && percentage
<= 100.0) {
2460 size_t sample_ofs
, actions_ofs
;
2463 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2464 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2465 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2466 (probability
<= 0 ? 0
2467 : probability
>= UINT32_MAX
? UINT32_MAX
2470 actions_ofs
= nl_msg_start_nested(actions
,
2471 OVS_SAMPLE_ATTR_ACTIONS
);
2472 int retval
= parse_action_list(context
, s
+ n
, actions
);
2479 nl_msg_end_nested(actions
, actions_ofs
);
2480 nl_msg_end_nested(actions
, sample_ofs
);
2482 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2487 if (!strncmp(s
, "clone(", 6)) {
2491 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2492 int retval
= parse_action_list(context
, s
+ n
, actions
);
2497 nl_msg_end_nested(actions
, actions_ofs
);
2503 if (!strncmp(s
, "push_nsh(", 9)) {
2504 int retval
= parse_odp_push_nsh_action(s
, actions
);
2514 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2515 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2524 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2525 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2531 if (!strncmp(s
, "ct_clear", 8)) {
2532 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2540 if (ovs_scan(s
, "check_pkt_len(size=%"SCNi16
",gt(%n", &pkt_len
, &n
)) {
2541 size_t cpl_ofs
, actions_ofs
;
2542 cpl_ofs
= nl_msg_start_nested(actions
,
2543 OVS_ACTION_ATTR_CHECK_PKT_LEN
);
2544 nl_msg_put_u16(actions
, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
, pkt_len
);
2545 actions_ofs
= nl_msg_start_nested(
2546 actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
);
2549 if (!strncasecmp(s
+ n
, "drop", 4)) {
2552 retval
= parse_action_list(context
, s
+ n
, actions
);
2559 nl_msg_end_nested(actions
, actions_ofs
);
2561 if (!ovs_scan(s
+ n
, "),le(%n", &retval
)) {
2566 actions_ofs
= nl_msg_start_nested(
2567 actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
);
2568 if (!strncasecmp(s
+ n
, "drop", 4)) {
2571 retval
= parse_action_list(context
, s
+ n
, actions
);
2577 nl_msg_end_nested(actions
, actions_ofs
);
2578 nl_msg_end_nested(actions
, cpl_ofs
);
2579 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2586 retval
= parse_conntrack_action(s
, actions
);
2593 struct ovs_action_push_tnl data
;
2596 n
= ovs_parse_tnl_push(s
, &data
);
2598 odp_put_tnl_push_action(actions
, &data
);
2608 /* Parses the string representation of datapath actions, in the format output
2609 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2610 * value. On success, the ODP actions are appended to 'actions' as a series of
2611 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2612 * way, 'actions''s data might be reallocated. */
2614 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2615 struct ofpbuf
*actions
)
2619 if (!strcasecmp(s
, "drop")) {
2620 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_DROP
, XLATE_OK
);
2624 struct parse_odp_context context
= (struct parse_odp_context
) {
2625 .port_names
= port_names
,
2628 old_size
= actions
->size
;
2632 s
+= strspn(s
, delimiters
);
2637 retval
= parse_odp_action(&context
, s
, actions
);
2639 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2640 actions
->size
= old_size
;
2649 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2650 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2653 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2654 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2655 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2656 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2657 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2658 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2659 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2660 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2661 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2662 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2663 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2664 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2665 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2666 .next
= ovs_vxlan_ext_attr_lens
,
2667 .next_max
= OVS_VXLAN_EXT_MAX
},
2668 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2669 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2670 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2671 [OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2674 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2675 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2676 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2677 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2678 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2679 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2680 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2681 .next
= ovs_tun_key_attr_lens
,
2682 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2683 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2684 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2685 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2686 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2687 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2688 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2689 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2690 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2691 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2692 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2693 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2694 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2695 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2696 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2697 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2698 [OVS_KEY_ATTR_ND_EXTENSIONS
] = { .len
= sizeof(struct ovs_key_nd_extensions
) },
2699 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2700 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2701 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2702 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2703 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2704 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2705 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2706 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2707 .next
= ovs_nsh_key_attr_lens
,
2708 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2711 /* Returns the correct length of the payload for a flow key attribute of the
2712 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2713 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2714 * payload is a nested type. */
2716 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2718 if (type
> max_type
) {
2719 return ATTR_LEN_INVALID
;
2722 return tbl
[type
].len
;
2726 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2728 size_t len
= nl_attr_get_size(a
);
2730 const uint8_t *unspec
;
2733 unspec
= nl_attr_get(a
);
2734 for (i
= 0; i
< len
; i
++) {
2736 ds_put_char(ds
, ' ');
2738 ds_put_format(ds
, "%02x", unspec
[i
]);
2744 ovs_frag_type_to_string(enum ovs_frag_type type
)
2747 case OVS_FRAG_TYPE_NONE
:
2749 case OVS_FRAG_TYPE_FIRST
:
2751 case OVS_FRAG_TYPE_LATER
:
2753 case __OVS_FRAG_TYPE_MAX
:
2759 enum odp_key_fitness
2760 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2761 struct nsh_hdr
*nsh_hdr
, size_t size
)
2764 const struct nlattr
*a
;
2765 bool unknown
= false;
2769 bool has_md1
= false;
2770 bool has_md2
= false;
2772 memset(nsh_hdr
, 0, size
);
2774 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2775 uint16_t type
= nl_attr_type(a
);
2776 size_t len
= nl_attr_get_size(a
);
2777 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2778 OVS_NSH_KEY_ATTR_MAX
, type
);
2780 if (len
!= expected_len
&& expected_len
>= 0) {
2781 return ODP_FIT_ERROR
;
2785 case OVS_NSH_KEY_ATTR_BASE
: {
2786 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2787 nsh_hdr
->next_proto
= base
->np
;
2788 nsh_hdr
->md_type
= base
->mdtype
;
2789 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2790 flags
= base
->flags
;
2794 case OVS_NSH_KEY_ATTR_MD1
: {
2795 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2796 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2798 mdlen
= nl_attr_get_size(a
);
2799 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2800 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2801 return ODP_FIT_ERROR
;
2803 memcpy(md1_dst
, md1
, mdlen
);
2806 case OVS_NSH_KEY_ATTR_MD2
: {
2807 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2808 const uint8_t *md2
= nl_attr_get(a
);
2810 mdlen
= nl_attr_get_size(a
);
2811 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2812 return ODP_FIT_ERROR
;
2814 memcpy(md2_dst
, md2
, mdlen
);
2818 /* Allow this to show up as unexpected, if there are unknown
2819 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2826 return ODP_FIT_TOO_MUCH
;
2829 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2830 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2831 return ODP_FIT_ERROR
;
2834 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2835 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2837 return ODP_FIT_PERFECT
;
2840 /* Reports the error 'msg', which is formatted as with printf().
2842 * If 'errorp' is nonnull, then some the wants the error report to come
2843 * directly back to it, so the function stores the error message into '*errorp'
2844 * (after first freeing it in case there's something there already).
2846 * Otherwise, logs the message at WARN level, rate-limited. */
2847 static void OVS_PRINTF_FORMAT(3, 4)
2848 odp_parse_error(struct vlog_rate_limit
*rl
, char **errorp
,
2849 const char *msg
, ...)
2851 if (OVS_UNLIKELY(errorp
)) {
2855 va_start(args
, msg
);
2856 *errorp
= xvasprintf(msg
, args
);
2858 } else if (!VLOG_DROP_WARN(rl
)) {
2860 va_start(args
, msg
);
2861 char *error
= xvasprintf(msg
, args
);
2864 VLOG_WARN("%s", error
);
2870 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2871 * returns fitness. If the attribute is a key, 'is_mask' should be false;
2872 * if it is a mask, 'is_mask' should be true. If 'errorp' is nonnull and the
2873 * function returns ODP_FIT_ERROR, stores a malloc()'d error message in
2875 static enum odp_key_fitness
2876 odp_nsh_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2877 struct ovs_key_nsh
*nsh
, struct ovs_key_nsh
*nsh_mask
,
2880 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2886 const struct nlattr
*a
;
2887 bool unknown
= false;
2888 bool has_md1
= false;
2890 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2891 uint16_t type
= nl_attr_type(a
);
2892 size_t len
= nl_attr_get_size(a
);
2893 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2894 OVS_NSH_KEY_ATTR_MAX
, type
);
2899 if (len
!= expected_len
) {
2900 odp_parse_error(&rl
, errorp
, "NSH %s attribute %"PRIu16
" "
2901 "should have length %d but actually has "
2903 nsh_mask
? "mask" : "key",
2904 type
, expected_len
, len
);
2905 return ODP_FIT_ERROR
;
2910 case OVS_NSH_KEY_ATTR_UNSPEC
:
2912 case OVS_NSH_KEY_ATTR_BASE
: {
2913 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2914 nsh
->flags
= base
->flags
;
2915 nsh
->ttl
= base
->ttl
;
2916 nsh
->mdtype
= base
->mdtype
;
2918 nsh
->path_hdr
= base
->path_hdr
;
2919 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2920 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2921 nsh_mask
->flags
= base_mask
->flags
;
2922 nsh_mask
->ttl
= base_mask
->ttl
;
2923 nsh_mask
->mdtype
= base_mask
->mdtype
;
2924 nsh_mask
->np
= base_mask
->np
;
2925 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2929 case OVS_NSH_KEY_ATTR_MD1
: {
2930 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2932 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2933 if (len
== 2 * sizeof(*md1
)) {
2934 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2935 memcpy(nsh_mask
->context
, md1_mask
->context
,
2940 case OVS_NSH_KEY_ATTR_MD2
:
2942 /* Allow this to show up as unexpected, if there are unknown
2943 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2950 return ODP_FIT_TOO_MUCH
;
2953 if (!is_mask
&& has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
&& !nsh_mask
) {
2954 odp_parse_error(&rl
, errorp
, "OVS_NSH_KEY_ATTR_MD1 present but "
2955 "declared mdtype %"PRIu8
" is not %d (NSH_M_TYPE1)",
2956 nsh
->mdtype
, NSH_M_TYPE1
);
2957 return ODP_FIT_ERROR
;
2960 return ODP_FIT_PERFECT
;
2963 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2964 * returns fitness. The attribute should be a key (not a mask). If 'errorp'
2965 * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error
2966 * message in '*errorp'. */
2967 enum odp_key_fitness
2968 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2969 struct ovs_key_nsh
*nsh_mask
, char **errorp
)
2971 return odp_nsh_key_from_attr__(attr
, false, nsh
, nsh_mask
, errorp
);
2974 /* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
2975 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
2976 * 'is_mask' should be true. If 'errorp' is nonnull and the function returns
2977 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
2978 static enum odp_key_fitness
2979 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2980 struct flow_tnl
*tun
, char **errorp
)
2982 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2984 const struct nlattr
*a
;
2986 bool unknown
= false;
2988 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2989 uint16_t type
= nl_attr_type(a
);
2990 size_t len
= nl_attr_get_size(a
);
2991 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2992 OVS_TUNNEL_ATTR_MAX
, type
);
2994 if (len
!= expected_len
&& expected_len
>= 0) {
2995 odp_parse_error(&rl
, errorp
, "tunnel key attribute %"PRIu16
" "
2996 "should have length %d but actually has %"PRIuSIZE
,
2997 type
, expected_len
, len
);
2998 return ODP_FIT_ERROR
;
3002 case OVS_TUNNEL_KEY_ATTR_ID
:
3003 tun
->tun_id
= nl_attr_get_be64(a
);
3004 tun
->flags
|= FLOW_TNL_F_KEY
;
3006 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3007 tun
->ip_src
= nl_attr_get_be32(a
);
3009 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3010 tun
->ip_dst
= nl_attr_get_be32(a
);
3012 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
3013 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
3015 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
3016 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
3018 case OVS_TUNNEL_KEY_ATTR_TOS
:
3019 tun
->ip_tos
= nl_attr_get_u8(a
);
3021 case OVS_TUNNEL_KEY_ATTR_TTL
:
3022 tun
->ip_ttl
= nl_attr_get_u8(a
);
3025 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3026 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3028 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3029 tun
->flags
|= FLOW_TNL_F_CSUM
;
3031 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3032 tun
->tp_src
= nl_attr_get_be16(a
);
3034 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3035 tun
->tp_dst
= nl_attr_get_be16(a
);
3037 case OVS_TUNNEL_KEY_ATTR_OAM
:
3038 tun
->flags
|= FLOW_TNL_F_OAM
;
3040 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
3041 static const struct nl_policy vxlan_opts_policy
[] = {
3042 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
3044 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
3046 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
3047 odp_parse_error(&rl
, errorp
, "error parsing VXLAN options");
3048 return ODP_FIT_ERROR
;
3051 if (ext
[OVS_VXLAN_EXT_GBP
]) {
3052 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
3054 tun
->gbp_id
= htons(gbp
& 0xFFFF);
3055 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
3060 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3061 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
3063 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
3064 const struct erspan_metadata
*opts
= nl_attr_get(a
);
3066 tun
->erspan_ver
= opts
->version
;
3067 if (tun
->erspan_ver
== 1) {
3068 tun
->erspan_idx
= ntohl(opts
->u
.index
);
3069 } else if (tun
->erspan_ver
== 2) {
3070 tun
->erspan_dir
= opts
->u
.md2
.dir
;
3071 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
3073 VLOG_WARN("%s invalid erspan version\n", __func__
);
3077 case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
: {
3078 const struct gtpu_metadata
*opts
= nl_attr_get(a
);
3080 tun
->gtpu_flags
= opts
->flags
;
3081 tun
->gtpu_msgtype
= opts
->msgtype
;
3086 /* Allow this to show up as unexpected, if there are unknown
3087 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3094 odp_parse_error(&rl
, errorp
, "tunnel options missing TTL");
3095 return ODP_FIT_ERROR
;
3098 return ODP_FIT_TOO_MUCH
;
3100 return ODP_FIT_PERFECT
;
3103 /* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
3104 * fitness. The attribute should be a key (not a mask). If 'errorp' is
3105 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
3107 enum odp_key_fitness
3108 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
,
3114 memset(tun
, 0, sizeof *tun
);
3115 return odp_tun_key_from_attr__(attr
, false, tun
, errorp
);
3119 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
3120 const struct flow_tnl
*tun_flow_key
,
3121 const struct ofpbuf
*key_buf
, const char *tnl_type
)
3125 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
3127 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
3128 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
3129 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
3131 if (tun_key
->ip_src
) {
3132 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
3134 if (tun_key
->ip_dst
) {
3135 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
3137 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
3138 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
3140 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
3141 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
3143 if (tun_key
->ip_tos
) {
3144 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
3146 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
3147 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3148 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3150 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
3151 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3153 if (tun_key
->tp_src
) {
3154 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
3156 if (tun_key
->tp_dst
) {
3157 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
3159 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
3160 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3163 /* If tnl_type is set to a particular type of output tunnel,
3164 * only put its relevant tunnel metadata to the nlattr.
3165 * If tnl_type is NULL, put tunnel metadata according to the
3168 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
3169 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
3170 size_t vxlan_opts_ofs
;
3172 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3173 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
3174 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
3175 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3178 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
3179 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
3182 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
3183 !strcmp(tnl_type
, "ip6erspan")) &&
3184 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
3185 struct erspan_metadata opts
;
3187 opts
.version
= tun_key
->erspan_ver
;
3188 if (opts
.version
== 1) {
3189 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
3191 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
3192 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
3194 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
3195 &opts
, sizeof(opts
));
3198 if ((!tnl_type
|| !strcmp(tnl_type
, "gtpu")) &&
3199 (tun_key
->gtpu_flags
&& tun_key
->gtpu_msgtype
)) {
3200 struct gtpu_metadata opts
;
3202 opts
.flags
= tun_key
->gtpu_flags
;
3203 opts
.msgtype
= tun_key
->gtpu_msgtype
;
3204 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
3205 &opts
, sizeof(opts
));
3207 nl_msg_end_nested(a
, tun_key_ofs
);
3211 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
3214 /* Convert 'constant' to all the widths we need. C conversion rules ensure
3215 * that -1 becomes all-1-bits and 0 does not change. */
3216 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
3217 uint32_t u32
= constant
;
3218 uint8_t u8
= constant
;
3219 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
3222 case OVS_KEY_ATTR_UNSPEC
:
3223 case OVS_KEY_ATTR_ENCAP
:
3224 case __OVS_KEY_ATTR_MAX
:
3228 case OVS_KEY_ATTR_PRIORITY
:
3229 case OVS_KEY_ATTR_IN_PORT
:
3230 case OVS_KEY_ATTR_ETHERNET
:
3231 case OVS_KEY_ATTR_VLAN
:
3232 case OVS_KEY_ATTR_ETHERTYPE
:
3233 case OVS_KEY_ATTR_IPV4
:
3234 case OVS_KEY_ATTR_TCP
:
3235 case OVS_KEY_ATTR_UDP
:
3236 case OVS_KEY_ATTR_ICMP
:
3237 case OVS_KEY_ATTR_ICMPV6
:
3238 case OVS_KEY_ATTR_ND
:
3239 case OVS_KEY_ATTR_ND_EXTENSIONS
:
3240 case OVS_KEY_ATTR_SKB_MARK
:
3241 case OVS_KEY_ATTR_TUNNEL
:
3242 case OVS_KEY_ATTR_SCTP
:
3243 case OVS_KEY_ATTR_DP_HASH
:
3244 case OVS_KEY_ATTR_RECIRC_ID
:
3245 case OVS_KEY_ATTR_MPLS
:
3246 case OVS_KEY_ATTR_CT_STATE
:
3247 case OVS_KEY_ATTR_CT_ZONE
:
3248 case OVS_KEY_ATTR_CT_MARK
:
3249 case OVS_KEY_ATTR_CT_LABELS
:
3250 case OVS_KEY_ATTR_PACKET_TYPE
:
3251 case OVS_KEY_ATTR_NSH
:
3252 return is_all_byte(mask
, size
, u8
);
3254 case OVS_KEY_ATTR_TCP_FLAGS
:
3255 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
3257 case OVS_KEY_ATTR_IPV6
: {
3258 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
3259 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
3260 == htonl(IPV6_LABEL_MASK
& u32
)
3261 && ipv6_mask
->ipv6_proto
== u8
3262 && ipv6_mask
->ipv6_tclass
== u8
3263 && ipv6_mask
->ipv6_hlimit
== u8
3264 && ipv6_mask
->ipv6_frag
== u8
3265 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
3266 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
3269 case OVS_KEY_ATTR_ARP
:
3270 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
3272 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
3273 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
3276 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
3277 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
3282 /* The caller must already have verified that 'ma' has a correct length.
3284 * The main purpose of this function is formatting, to allow code to figure out
3285 * whether the mask can be omitted. It doesn't try hard for attributes that
3286 * contain sub-attributes, etc., because normally those would be broken down
3287 * further for formatting. */
3289 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
3291 return odp_mask_is_constant__(nl_attr_type(ma
),
3292 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
3295 /* The caller must already have verified that 'size' is a correct length for
3298 * The main purpose of this function is formatting, to allow code to figure out
3299 * whether the mask can be omitted. It doesn't try hard for attributes that
3300 * contain sub-attributes, etc., because normally those would be broken down
3301 * further for formatting. */
3303 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
3305 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3308 /* The caller must already have verified that 'ma' has a correct length. */
3310 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3312 enum ovs_key_attr attr
= nl_attr_type(ma
);
3313 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3317 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3320 struct odp_portno_names
*odp_portno_names
;
3322 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3323 odp_portno_names
->port_no
= port_no
;
3324 odp_portno_names
->name
= xstrdup(port_name
);
3325 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3326 hash_odp_port(port_no
));
3330 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3333 struct odp_portno_names
*odp_portno_names
;
3335 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3336 hash_odp_port(port_no
), portno_names
) {
3337 if (odp_portno_names
->port_no
== port_no
) {
3338 return odp_portno_names
->name
;
3346 odp_portno_names_destroy(struct hmap
*portno_names
)
3348 struct odp_portno_names
*odp_portno_names
;
3350 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3351 free(odp_portno_names
->name
);
3352 free(odp_portno_names
);
3357 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3360 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3362 ds_put_cstr(s
, name
);
3364 ds_put_format(s
, "%"PRIu32
, port_no
);
3368 /* Format helpers. */
3371 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3372 const struct eth_addr
*mask
, bool verbose
)
3374 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3376 if (verbose
|| !mask_empty
) {
3377 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3380 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3382 ds_put_format(ds
, "%s=", name
);
3383 eth_format_masked(key
, mask
, ds
);
3384 ds_put_char(ds
, ',');
3391 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3392 const ovs_be64
*mask
, bool verbose
)
3394 bool mask_empty
= mask
&& !*mask
;
3396 if (verbose
|| !mask_empty
) {
3397 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3399 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3400 if (!mask_full
) { /* Partially masked. */
3401 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3403 ds_put_char(ds
, ',');
3408 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3409 const ovs_be32
*mask
, bool verbose
)
3411 bool mask_empty
= mask
&& !*mask
;
3413 if (verbose
|| !mask_empty
) {
3414 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3416 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3417 if (!mask_full
) { /* Partially masked. */
3418 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3420 ds_put_char(ds
, ',');
3425 format_in6_addr(struct ds
*ds
, const char *name
,
3426 const struct in6_addr
*key
,
3427 const struct in6_addr
*mask
,
3430 char buf
[INET6_ADDRSTRLEN
];
3431 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3433 if (verbose
|| !mask_empty
) {
3434 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3436 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3437 ds_put_format(ds
, "%s=%s", name
, buf
);
3438 if (!mask_full
) { /* Partially masked. */
3439 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3440 ds_put_format(ds
, "/%s", buf
);
3442 ds_put_char(ds
, ',');
3447 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3448 const ovs_be32
*mask
, bool verbose
)
3450 bool mask_empty
= mask
&& !*mask
;
3452 if (verbose
|| !mask_empty
) {
3453 bool mask_full
= !mask
3454 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3456 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3457 if (!mask_full
) { /* Partially masked. */
3458 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3460 ds_put_char(ds
, ',');
3465 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3466 const uint8_t *mask
, bool verbose
)
3468 bool mask_empty
= mask
&& !*mask
;
3470 if (verbose
|| !mask_empty
) {
3471 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3473 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3474 if (!mask_full
) { /* Partially masked. */
3475 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3477 ds_put_char(ds
, ',');
3482 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3483 const uint8_t *mask
, bool verbose
)
3485 bool mask_empty
= mask
&& !*mask
;
3487 if (verbose
|| !mask_empty
) {
3488 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3490 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3491 if (!mask_full
) { /* Partially masked. */
3492 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3494 ds_put_char(ds
, ',');
3499 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3500 const ovs_be16
*mask
, bool verbose
)
3502 bool mask_empty
= mask
&& !*mask
;
3504 if (verbose
|| !mask_empty
) {
3505 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3507 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3508 if (!mask_full
) { /* Partially masked. */
3509 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3511 ds_put_char(ds
, ',');
3516 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3517 const ovs_be16
*mask
, bool verbose
)
3519 bool mask_empty
= mask
&& !*mask
;
3521 if (verbose
|| !mask_empty
) {
3522 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3524 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3525 if (!mask_full
) { /* Partially masked. */
3526 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3528 ds_put_char(ds
, ',');
3533 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3534 const uint16_t *mask
, bool verbose
)
3536 bool mask_empty
= mask
&& !*mask
;
3538 if (verbose
|| !mask_empty
) {
3539 ds_put_cstr(ds
, name
);
3540 ds_put_char(ds
, '(');
3542 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3543 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3544 } else { /* Fully masked. */
3545 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3547 ds_put_cstr(ds
, "),");
3552 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3553 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3557 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3558 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3559 expected_len
!= ATTR_LEN_NESTED
) {
3561 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3562 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3564 if (bad_key_len
|| bad_mask_len
) {
3566 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3569 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3570 nl_attr_get_size(a
), expected_len
);
3572 format_generic_odp_key(a
, ds
);
3574 ds_put_char(ds
, '/');
3576 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3577 nl_attr_get_size(ma
), expected_len
);
3579 format_generic_odp_key(ma
, ds
);
3581 ds_put_char(ds
, ')');
3590 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3591 const struct nlattr
*ma
)
3593 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3594 format_generic_odp_key(a
, ds
);
3595 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3596 ds_put_char(ds
, '/');
3597 format_generic_odp_key(ma
, ds
);
3599 ds_put_cstr(ds
, "),");
3603 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3604 const struct nlattr
*mask_attr
, struct ds
*ds
,
3608 const struct nlattr
*a
;
3611 ofpbuf_init(&ofp
, 100);
3612 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3613 uint16_t type
= nl_attr_type(a
);
3614 const struct nlattr
*ma
= NULL
;
3617 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3618 nl_attr_get_size(mask_attr
), type
);
3620 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3626 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3627 OVS_VXLAN_EXT_MAX
, true)) {
3632 case OVS_VXLAN_EXT_GBP
: {
3633 uint32_t key
= nl_attr_get_u32(a
);
3634 ovs_be16 id
, id_mask
;
3635 uint8_t flags
, flags_mask
= 0;
3637 id
= htons(key
& 0xFFFF);
3638 flags
= (key
>> 16) & 0xFF;
3640 uint32_t mask
= nl_attr_get_u32(ma
);
3641 id_mask
= htons(mask
& 0xFFFF);
3642 flags_mask
= (mask
>> 16) & 0xFF;
3645 ds_put_cstr(ds
, "gbp(");
3646 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3647 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3649 ds_put_cstr(ds
, "),");
3654 format_unknown_key(ds
, a
, ma
);
3660 ofpbuf_uninit(&ofp
);
3664 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3665 const struct nlattr
*mask_attr
, struct ds
*ds
,
3668 const struct erspan_metadata
*opts
, *mask
;
3669 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3671 opts
= nl_attr_get(attr
);
3672 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3674 ver
= (uint8_t)opts
->version
;
3676 ver_ma
= (uint8_t)mask
->version
;
3679 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3681 if (opts
->version
== 1) {
3683 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3684 ntohl(opts
->u
.index
),
3685 ntohl(mask
->u
.index
));
3687 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3689 } else if (opts
->version
== 2) {
3690 dir
= opts
->u
.md2
.dir
;
3691 hwid
= opts
->u
.md2
.hwid
;
3693 dir_ma
= mask
->u
.md2
.dir
;
3694 hwid_ma
= mask
->u
.md2
.hwid
;
3697 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3698 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3704 format_odp_tun_gtpu_opt(const struct nlattr
*attr
,
3705 const struct nlattr
*mask_attr
, struct ds
*ds
,
3708 const struct gtpu_metadata
*opts
, *mask
;
3710 opts
= nl_attr_get(attr
);
3711 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3713 format_u8x(ds
, "flags", opts
->flags
, mask
? &mask
->flags
: NULL
, verbose
);
3714 format_u8u(ds
, "msgtype", opts
->msgtype
, mask
? &mask
->msgtype
: NULL
,
3719 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3722 format_geneve_opts(const struct geneve_opt
*opt
,
3723 const struct geneve_opt
*mask
, int opts_len
,
3724 struct ds
*ds
, bool verbose
)
3726 while (opts_len
> 0) {
3728 uint8_t data_len
, data_len_mask
;
3730 if (opts_len
< sizeof *opt
) {
3731 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3732 opts_len
, sizeof *opt
);
3736 data_len
= opt
->length
* 4;
3738 if (mask
->length
== 0x1f) {
3739 data_len_mask
= UINT8_MAX
;
3741 data_len_mask
= mask
->length
;
3744 len
= sizeof *opt
+ data_len
;
3745 if (len
> opts_len
) {
3746 ds_put_format(ds
, "opt len %u greater than remaining %u",
3751 ds_put_char(ds
, '{');
3752 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3754 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3755 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3757 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3758 ds_put_hex(ds
, opt
+ 1, data_len
);
3759 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3760 ds_put_char(ds
, '/');
3761 ds_put_hex(ds
, mask
+ 1, data_len
);
3766 ds_put_char(ds
, '}');
3768 opt
+= len
/ sizeof(*opt
);
3770 mask
+= len
/ sizeof(*opt
);
3777 format_odp_tun_geneve(const struct nlattr
*attr
,
3778 const struct nlattr
*mask_attr
, struct ds
*ds
,
3781 int opts_len
= nl_attr_get_size(attr
);
3782 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3783 const struct geneve_opt
*mask
= mask_attr
?
3784 nl_attr_get(mask_attr
) : NULL
;
3786 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3787 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3788 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3792 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3796 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3800 const struct nlattr
*a
;
3801 struct ovs_key_nsh nsh
;
3802 struct ovs_key_nsh nsh_mask
;
3804 memset(&nsh
, 0, sizeof nsh
);
3805 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3807 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3808 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3809 const struct nlattr
*ma
= NULL
;
3812 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3813 nl_attr_get_size(mask_attr
), type
);
3816 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3817 OVS_NSH_KEY_ATTR_MAX
, true)) {
3822 case OVS_NSH_KEY_ATTR_UNSPEC
:
3824 case OVS_NSH_KEY_ATTR_BASE
: {
3825 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3826 const struct ovs_nsh_key_base
*base_mask
3827 = ma
? nl_attr_get(ma
) : NULL
;
3828 nsh
.flags
= base
->flags
;
3829 nsh
.ttl
= base
->ttl
;
3830 nsh
.mdtype
= base
->mdtype
;
3832 nsh
.path_hdr
= base
->path_hdr
;
3834 nsh_mask
.flags
= base_mask
->flags
;
3835 nsh_mask
.ttl
= base_mask
->ttl
;
3836 nsh_mask
.mdtype
= base_mask
->mdtype
;
3837 nsh_mask
.np
= base_mask
->np
;
3838 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3842 case OVS_NSH_KEY_ATTR_MD1
: {
3843 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3844 const struct ovs_nsh_key_md1
*md1_mask
3845 = ma
? nl_attr_get(ma
) : NULL
;
3846 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3848 memcpy(nsh_mask
.context
, md1_mask
->context
,
3849 sizeof md1_mask
->context
);
3853 case OVS_NSH_KEY_ATTR_MD2
:
3854 case __OVS_NSH_KEY_ATTR_MAX
:
3856 /* No support for matching other metadata formats yet. */
3862 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3864 format_nsh_key(ds
, &nsh
);
3869 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3870 struct ds
*ds
, bool verbose
)
3873 const struct nlattr
*a
;
3875 uint16_t mask_flags
= 0;
3878 ofpbuf_init(&ofp
, 100);
3879 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3880 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3881 const struct nlattr
*ma
= NULL
;
3884 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3885 nl_attr_get_size(mask_attr
), type
);
3887 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3888 OVS_TUNNEL_KEY_ATTR_MAX
,
3893 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3894 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3899 case OVS_TUNNEL_KEY_ATTR_ID
:
3900 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3901 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3902 flags
|= FLOW_TNL_F_KEY
;
3904 mask_flags
|= FLOW_TNL_F_KEY
;
3907 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3908 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3909 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3911 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3912 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3913 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3915 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3916 struct in6_addr ipv6_src
;
3917 ipv6_src
= nl_attr_get_in6_addr(a
);
3918 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3919 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3922 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3923 struct in6_addr ipv6_dst
;
3924 ipv6_dst
= nl_attr_get_in6_addr(a
);
3925 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3926 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3929 case OVS_TUNNEL_KEY_ATTR_TOS
:
3930 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3931 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3933 case OVS_TUNNEL_KEY_ATTR_TTL
:
3934 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3935 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3937 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3938 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3940 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3941 flags
|= FLOW_TNL_F_CSUM
;
3943 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3944 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3945 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3947 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3948 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3949 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3951 case OVS_TUNNEL_KEY_ATTR_OAM
:
3952 flags
|= FLOW_TNL_F_OAM
;
3954 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3955 ds_put_cstr(ds
, "vxlan(");
3956 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3957 ds_put_cstr(ds
, "),");
3959 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3960 ds_put_cstr(ds
, "geneve(");
3961 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3962 ds_put_cstr(ds
, "),");
3964 case OVS_TUNNEL_KEY_ATTR_PAD
:
3966 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3967 ds_put_cstr(ds
, "erspan(");
3968 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3969 ds_put_cstr(ds
, "),");
3971 case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
:
3972 ds_put_cstr(ds
, "gtpu(");
3973 format_odp_tun_gtpu_opt(a
, ma
, ds
, verbose
);
3974 ds_put_cstr(ds
, "),");
3976 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3978 format_unknown_key(ds
, a
, ma
);
3983 /* Flags can have a valid mask even if the attribute is not set, so
3984 * we need to collect these separately. */
3986 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3987 switch (nl_attr_type(a
)) {
3988 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3989 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3991 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3992 mask_flags
|= FLOW_TNL_F_CSUM
;
3994 case OVS_TUNNEL_KEY_ATTR_OAM
:
3995 mask_flags
|= FLOW_TNL_F_OAM
;
4001 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
4004 ofpbuf_uninit(&ofp
);
4008 odp_ct_state_to_string(uint32_t flag
)
4011 case OVS_CS_F_REPLY_DIR
:
4013 case OVS_CS_F_TRACKED
:
4017 case OVS_CS_F_ESTABLISHED
:
4019 case OVS_CS_F_RELATED
:
4021 case OVS_CS_F_INVALID
:
4023 case OVS_CS_F_SRC_NAT
:
4025 case OVS_CS_F_DST_NAT
:
4033 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
4034 const uint8_t *mask
, bool verbose OVS_UNUSED
)
4036 bool mask_empty
= mask
&& !*mask
;
4037 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
4039 /* ODP frag is an enumeration field; partial masks are not meaningful. */
4040 if (!mask_empty
&& !mask_full
) {
4041 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
4043 } else if (!mask_empty
) {
4044 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
4049 mask_empty(const struct nlattr
*ma
)
4057 mask
= nl_attr_get(ma
);
4058 n
= nl_attr_get_size(ma
);
4060 return is_all_zeros(mask
, n
);
4063 /* The caller must have already verified that 'a' and 'ma' have correct
4066 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
4067 const struct hmap
*portno_names
, struct ds
*ds
,
4070 enum ovs_key_attr attr
= nl_attr_type(a
);
4071 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4074 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
4076 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
4078 ds_put_char(ds
, '(');
4080 case OVS_KEY_ATTR_ENCAP
:
4081 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
4082 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
4083 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
4085 } else if (nl_attr_get_size(a
)) {
4086 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
4091 case OVS_KEY_ATTR_PRIORITY
:
4092 case OVS_KEY_ATTR_SKB_MARK
:
4093 case OVS_KEY_ATTR_DP_HASH
:
4094 case OVS_KEY_ATTR_RECIRC_ID
:
4095 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4097 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4101 case OVS_KEY_ATTR_CT_MARK
:
4102 if (verbose
|| !mask_empty(ma
)) {
4103 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4105 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4110 case OVS_KEY_ATTR_CT_STATE
:
4112 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4114 ds_put_format(ds
, "/%#"PRIx32
,
4115 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
4117 } else if (!is_exact
) {
4118 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
4120 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
4123 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
4127 case OVS_KEY_ATTR_CT_ZONE
:
4128 if (verbose
|| !mask_empty(ma
)) {
4129 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
4131 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
4136 case OVS_KEY_ATTR_CT_LABELS
: {
4137 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
4138 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4140 format_u128(ds
, value
, mask
, verbose
);
4144 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
4145 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
4146 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4148 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
4149 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
4150 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
4152 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
4154 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
4160 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
4161 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
4162 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4164 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
4166 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
4168 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
4170 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
4172 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
4178 case OVS_KEY_ATTR_TUNNEL
:
4179 format_odp_tun_attr(a
, ma
, ds
, verbose
);
4182 case OVS_KEY_ATTR_IN_PORT
:
4184 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
4186 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
4188 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4193 case OVS_KEY_ATTR_PACKET_TYPE
: {
4194 ovs_be32 value
= nl_attr_get_be32(a
);
4195 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
4197 ovs_be16 ns
= htons(pt_ns(value
));
4198 ovs_be16 ns_mask
= htons(pt_ns(mask
));
4199 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
4201 ovs_be16 ns_type
= pt_ns_type_be(value
);
4202 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
4203 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
4209 case OVS_KEY_ATTR_ETHERNET
: {
4210 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4211 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
4213 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
4214 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
4218 case OVS_KEY_ATTR_VLAN
:
4219 format_vlan_tci(ds
, nl_attr_get_be16(a
),
4220 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
4223 case OVS_KEY_ATTR_MPLS
: {
4224 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
4225 const struct ovs_key_mpls
*mpls_mask
= NULL
;
4226 size_t size
= nl_attr_get_size(a
);
4228 if (!size
|| size
% sizeof *mpls_key
) {
4229 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
4233 mpls_mask
= nl_attr_get(ma
);
4234 if (size
!= nl_attr_get_size(ma
)) {
4235 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
4236 "mask length %"PRIuSIZE
")",
4237 size
, nl_attr_get_size(ma
));
4241 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
4244 case OVS_KEY_ATTR_ETHERTYPE
:
4245 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
4247 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
4251 case OVS_KEY_ATTR_IPV4
: {
4252 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
4253 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4255 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
4256 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
4257 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
4259 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
4260 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
4261 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
4266 case OVS_KEY_ATTR_IPV6
: {
4267 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
4268 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4270 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
4272 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
4274 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
4276 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
4278 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
4280 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
4282 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
4287 /* These have the same structure and format. */
4288 case OVS_KEY_ATTR_TCP
:
4289 case OVS_KEY_ATTR_UDP
:
4290 case OVS_KEY_ATTR_SCTP
: {
4291 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
4292 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4294 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
4295 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
4299 case OVS_KEY_ATTR_TCP_FLAGS
:
4301 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
4302 ntohs(nl_attr_get_be16(a
)),
4303 TCP_FLAGS(nl_attr_get_be16(ma
)),
4304 TCP_FLAGS(OVS_BE16_MAX
));
4306 format_flags(ds
, packet_tcp_flag_to_string
,
4307 ntohs(nl_attr_get_be16(a
)), '|');
4311 case OVS_KEY_ATTR_ICMP
: {
4312 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
4313 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4315 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
4316 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
4320 case OVS_KEY_ATTR_ICMPV6
: {
4321 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
4322 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4324 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
4326 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4331 case OVS_KEY_ATTR_ARP
: {
4332 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4333 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4335 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4336 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4337 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4338 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4339 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4343 case OVS_KEY_ATTR_ND
: {
4344 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4345 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4347 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4349 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4350 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4355 case OVS_KEY_ATTR_ND_EXTENSIONS
: {
4356 const struct ovs_key_nd_extensions
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4357 const struct ovs_key_nd_extensions
*key
= nl_attr_get(a
);
4360 format_be32_masked(ds
, &first
, "nd_reserved", key
->nd_reserved
,
4362 ds_put_char(ds
, ',');
4364 format_u8u(ds
, "nd_options_type", key
->nd_options_type
,
4365 MASK(mask
, nd_options_type
), verbose
);
4370 case OVS_KEY_ATTR_NSH
: {
4371 format_odp_nsh_attr(a
, ma
, ds
);
4374 case OVS_KEY_ATTR_UNSPEC
:
4375 case __OVS_KEY_ATTR_MAX
:
4377 format_generic_odp_key(a
, ds
);
4379 ds_put_char(ds
, '/');
4380 format_generic_odp_key(ma
, ds
);
4384 ds_put_char(ds
, ')');
4388 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4389 const struct hmap
*portno_names
, struct ds
*ds
,
4392 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4393 OVS_KEY_ATTR_MAX
, false)) {
4394 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4398 static struct nlattr
*
4399 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4400 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4402 const struct nlattr
*a
;
4404 int type
= nl_attr_type(key
);
4405 int size
= nl_attr_get_size(key
);
4407 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4408 nl_msg_put_unspec_zero(ofp
, type
, size
);
4412 if (tbl
[type
].next
) {
4413 const struct attr_len_tbl
*entry
= &tbl
[type
];
4415 max
= entry
->next_max
;
4418 nested_mask
= nl_msg_start_nested(ofp
, type
);
4419 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4420 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4422 nl_msg_end_nested(ofp
, nested_mask
);
4429 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4430 const ovs_32aligned_u128
*mask
, bool verbose
)
4432 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4433 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4434 ds_put_hex(ds
, &value
, sizeof value
);
4435 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4436 value
= hton128(get_32aligned_u128(mask
));
4437 ds_put_char(ds
, '/');
4438 ds_put_hex(ds
, &value
, sizeof value
);
4443 /* Read the string from 's_' as a 128-bit value. If the string contains
4444 * a "/", the rest of the string will be treated as a 128-bit mask.
4446 * If either the value or mask is larger than 64 bits, the string must
4447 * be in hexadecimal.
4450 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4452 char *s
= CONST_CAST(char *, s_
);
4456 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4457 *value
= ntoh128(be_value
);
4462 if (ovs_scan(s
, "/%n", &n
)) {
4466 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4467 sizeof be_mask
, &s
);
4471 *mask
= ntoh128(be_mask
);
4473 *mask
= OVS_U128_MAX
;
4483 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4487 if (ovs_scan(s
, "ufid:")) {
4490 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4502 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4504 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4507 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4508 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4509 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4510 * non-null, translates odp port number to its name. */
4512 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4513 const struct nlattr
*mask
, size_t mask_len
,
4514 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4517 const struct nlattr
*a
;
4519 bool has_ethtype_key
= false;
4520 bool has_packet_type_key
= false;
4522 bool first_field
= true;
4524 ofpbuf_init(&ofp
, 100);
4525 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4526 int attr_type
= nl_attr_type(a
);
4527 const struct nlattr
*ma
= (mask
&& mask_len
4528 ? nl_attr_find__(mask
, mask_len
,
4531 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4532 OVS_KEY_ATTR_MAX
, false)) {
4536 bool is_nested_attr
;
4537 bool is_wildcard
= false;
4539 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4540 has_ethtype_key
= true;
4541 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4542 has_packet_type_key
= true;
4545 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4546 OVS_KEY_ATTR_MAX
, attr_type
) ==
4549 if (mask
&& mask_len
) {
4550 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4551 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4554 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4555 if (is_wildcard
&& !ma
) {
4556 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4561 ds_put_char(ds
, ',');
4563 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4564 first_field
= false;
4565 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4566 && !has_packet_type_key
) {
4567 /* This special case reflects differences between the kernel
4568 * and userspace datapaths regarding the root type of the
4569 * packet being matched (typically Ethernet but some tunnels
4570 * can encapsulate IPv4 etc.). The kernel datapath does not
4571 * have an explicit way to indicate packet type; instead:
4573 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4574 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4575 * Ethertype encoded in the Ethernet header.
4577 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4578 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4579 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4580 * packet is an IPv4 packet).
4582 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4583 * all-wildcarded, it is important to print it.
4585 * On the other hand, the userspace datapath supports
4586 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4587 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4588 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4590 ds_put_char(ds
, ',');
4592 ds_put_cstr(ds
, "eth()");
4596 ofpbuf_uninit(&ofp
);
4601 if (left
== key_len
) {
4602 ds_put_cstr(ds
, "<empty>");
4604 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4605 for (i
= 0; i
< left
; i
++) {
4606 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4608 ds_put_char(ds
, ')');
4610 if (!has_ethtype_key
) {
4611 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4612 OVS_KEY_ATTR_ETHERTYPE
);
4614 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4615 ntohs(nl_attr_get_be16(ma
)));
4619 ds_put_cstr(ds
, "<empty>");
4623 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4624 * OVS_KEY_ATTR_* attributes in 'key'. */
4626 odp_flow_key_format(const struct nlattr
*key
,
4627 size_t key_len
, struct ds
*ds
)
4629 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4633 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4635 if (!strcasecmp(s
, "no")) {
4636 *type
= OVS_FRAG_TYPE_NONE
;
4637 } else if (!strcasecmp(s
, "first")) {
4638 *type
= OVS_FRAG_TYPE_FIRST
;
4639 } else if (!strcasecmp(s
, "later")) {
4640 *type
= OVS_FRAG_TYPE_LATER
;
4650 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4654 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4655 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4659 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4660 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4663 memset(mask
, 0xff, sizeof *mask
);
4672 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4676 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4680 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4681 IP_SCAN_ARGS(mask
), &n
)) {
4684 *mask
= OVS_BE32_MAX
;
4693 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4696 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4698 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4699 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4703 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4704 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4707 memset(mask
, 0xff, sizeof *mask
);
4716 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4721 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4722 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4727 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4728 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4730 *mask
= htonl(mask_
);
4732 *mask
= htonl(IPV6_LABEL_MASK
);
4741 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4745 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4749 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4761 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4765 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4769 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4781 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4785 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4789 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4801 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4803 uint16_t key_
, mask_
;
4806 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4811 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4813 *mask
= htons(mask_
);
4815 *mask
= OVS_BE16_MAX
;
4824 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4826 uint32_t key_
, mask_
;
4829 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4834 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4836 *mask
= htonl(mask_
);
4838 *mask
= OVS_BE32_MAX
;
4847 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4849 uint64_t key_
, mask_
;
4852 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4855 *key
= htonll(key_
);
4857 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4859 *mask
= htonll(mask_
);
4861 *mask
= OVS_BE64_MAX
;
4870 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4872 uint32_t flags
, fmask
;
4875 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4876 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4877 if (n
>= 0 && s
[n
] == ')') {
4888 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4890 uint32_t flags
, fmask
;
4893 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4894 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4896 *key
= htons(flags
);
4898 *mask
= htons(fmask
);
4906 ovs_to_odp_ct_state(uint8_t state
)
4910 #define CS_STATE(ENUM, INDEX, NAME) \
4911 if (state & CS_##ENUM) { \
4912 odp |= OVS_CS_F_##ENUM; \
4921 odp_to_ovs_ct_state(uint32_t flags
)
4925 #define CS_STATE(ENUM, INDEX, NAME) \
4926 if (flags & OVS_CS_F_##ENUM) { \
4927 state |= CS_##ENUM; \
4936 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4938 uint32_t flags
, fmask
;
4941 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4942 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4943 mask
? &fmask
: NULL
);
4956 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4960 enum ovs_frag_type frag_type
;
4962 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4963 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4976 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4977 const struct simap
*port_names
)
4981 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4985 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4992 } else if (port_names
) {
4993 const struct simap_node
*node
;
4996 len
= strcspn(s
, ")");
4997 node
= simap_find_len(port_names
, s
, len
);
5010 /* Helper for vlan parsing. */
5011 struct ovs_key_vlan__
{
5016 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
5018 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
5020 if (value
>> bits
) {
5024 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
5029 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
5032 uint16_t key_
, mask_
;
5035 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
5038 if (set_be16_bf(key
, bits
, offset
, key_
)) {
5040 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
5043 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
5047 *mask
|= htons(((1U << bits
) - 1) << offset
);
5057 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5059 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
5063 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5065 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
5069 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5071 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
5076 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
5078 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
5080 if (value
>> bits
) {
5084 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
5089 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
5092 uint32_t key_
, mask_
;
5095 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
5098 if (set_be32_bf(key
, bits
, offset
, key_
)) {
5100 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
5103 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
5107 *mask
|= htonl(((1U << bits
) - 1) << offset
);
5117 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5119 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
5123 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5125 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
5129 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5131 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
5135 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5137 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
5141 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
5143 const char *s_base
= s
;
5144 ovs_be16 id
= 0, id_mask
= 0;
5145 uint8_t flags
= 0, flags_mask
= 0;
5148 if (!strncmp(s
, "id=", 3)) {
5150 len
= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
5160 if (!strncmp(s
, "flags=", 6)) {
5162 len
= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
5169 if (!strncmp(s
, "))", 2)) {
5172 *key
= (flags
<< 16) | ntohs(id
);
5174 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
5184 scan_gtpu_metadata(const char *s
,
5185 struct gtpu_metadata
*key
,
5186 struct gtpu_metadata
*mask
)
5188 const char *s_base
= s
;
5189 uint8_t flags
= 0, flags_ma
= 0;
5190 uint8_t msgtype
= 0, msgtype_ma
= 0;
5193 if (!strncmp(s
, "flags=", 6)) {
5195 len
= scan_u8(s
, &flags
, mask
? &flags_ma
: NULL
);
5206 if (!strncmp(s
, "msgtype=", 8)) {
5208 len
= scan_u8(s
, &msgtype
, mask
? &msgtype_ma
: NULL
);
5215 if (!strncmp(s
, ")", 1)) {
5218 key
->msgtype
= msgtype
;
5220 mask
->flags
= flags_ma
;
5221 mask
->msgtype
= msgtype_ma
;
5228 scan_erspan_metadata(const char *s
,
5229 struct erspan_metadata
*key
,
5230 struct erspan_metadata
*mask
)
5232 const char *s_base
= s
;
5233 uint32_t idx
= 0, idx_mask
= 0;
5234 uint8_t ver
= 0, dir
= 0, hwid
= 0;
5235 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
5238 if (!strncmp(s
, "ver=", 4)) {
5240 len
= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
5252 if (!strncmp(s
, "idx=", 4)) {
5254 len
= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
5261 if (!strncmp(s
, ")", 1)) {
5264 key
->u
.index
= htonl(idx
);
5266 mask
->u
.index
= htonl(idx_mask
);
5271 } else if (ver
== 2) {
5272 if (!strncmp(s
, "dir=", 4)) {
5274 len
= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
5283 if (!strncmp(s
, "hwid=", 5)) {
5285 len
= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
5292 if (!strncmp(s
, ")", 1)) {
5295 key
->u
.md2
.hwid
= hwid
;
5296 key
->u
.md2
.dir
= dir
;
5298 mask
->u
.md2
.hwid
= hwid_mask
;
5299 mask
->u
.md2
.dir
= dir_mask
;
5309 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
5311 const char *s_base
= s
;
5312 struct geneve_opt
*opt
= key
->d
;
5313 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
5314 int len_remain
= sizeof key
->d
;
5317 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
5321 len_remain
-= sizeof *opt
;
5323 if (!strncmp(s
, "class=", 6)) {
5325 len
= scan_be16(s
, &opt
->opt_class
,
5326 mask
? &opt_mask
->opt_class
: NULL
);
5332 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
5338 if (!strncmp(s
, "type=", 5)) {
5340 len
= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
5346 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5352 if (!strncmp(s
, "len=", 4)) {
5353 uint8_t opt_len
, opt_len_mask
;
5355 len
= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
5361 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
5364 opt
->length
= opt_len
/ 4;
5366 opt_mask
->length
= opt_len_mask
;
5370 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5375 if (parse_int_string(s
, (uint8_t *)(opt
+ 1),
5376 data_len
, (char **)&s
)) {
5383 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
5384 data_len
, (char **)&s
)) {
5395 opt
+= 1 + data_len
/ 4;
5397 opt_mask
+= 1 + data_len
/ 4;
5399 len_remain
-= data_len
;
5406 len
= sizeof key
->d
- len_remain
;
5420 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
5422 const uint16_t *flags
= data_
;
5424 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
5425 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5427 if (*flags
& FLOW_TNL_F_CSUM
) {
5428 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5430 if (*flags
& FLOW_TNL_F_OAM
) {
5431 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5436 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5438 const uint32_t *gbp
= data_
;
5441 size_t vxlan_opts_ofs
;
5443 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5444 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5445 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5450 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5452 const struct geneve_scan
*geneve
= data_
;
5454 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5459 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5461 const struct erspan_metadata
*md
= data_
;
5463 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5468 gtpu_to_attr(struct ofpbuf
*a
, const void *data_
)
5470 const struct gtpu_metadata
*md
= data_
;
5472 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
, md
,
5476 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5478 unsigned long call_fn = (unsigned long)FUNC; \
5480 typedef void (*fn)(struct ofpbuf *, const void *); \
5482 func(BUF, &(DATA)); \
5484 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5488 #define SCAN_IF(NAME) \
5489 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5490 const char *start = s; \
5495 /* Usually no special initialization is needed. */
5496 #define SCAN_BEGIN(NAME, TYPE) \
5499 memset(&skey, 0, sizeof skey); \
5500 memset(&smask, 0, sizeof smask); \
5504 /* Init as fully-masked as mask will not be scanned. */
5505 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5508 memset(&skey, 0, sizeof skey); \
5509 memset(&smask, 0xff, sizeof smask); \
5513 /* VLAN needs special initialization. */
5514 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5516 TYPE skey = KEY_INIT; \
5517 TYPE smask = MASK_INIT; \
5521 /* Scan unnamed entry as 'TYPE' */
5522 #define SCAN_TYPE(TYPE, KEY, MASK) \
5523 len = scan_##TYPE(s, KEY, MASK); \
5529 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5530 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5531 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5532 s += strlen(NAME); \
5533 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5537 #define SCAN_FINISH() \
5538 } while (*s++ == ',' && len != 0); \
5539 if (s[-1] != ')') { \
5543 #define SCAN_FINISH_SINGLE() \
5545 if (*s++ != ')') { \
5549 /* Beginning of nested attribute. */
5550 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5552 size_t key_offset, mask_offset = 0; \
5553 key_offset = nl_msg_start_nested(key, ATTR); \
5555 mask_offset = nl_msg_start_nested(mask, ATTR); \
5560 #define SCAN_END_NESTED() \
5562 if (nl_attr_oversized(key->size - key_offset - NLA_HDRLEN)) { \
5565 nl_msg_end_nested(key, key_offset); \
5567 nl_msg_end_nested(mask, mask_offset); \
5572 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5573 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5575 memset(&skey, 0, sizeof skey); \
5576 memset(&smask, 0xff, sizeof smask); \
5577 s += strlen(NAME); \
5578 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5579 SCAN_PUT(ATTR, FUNC); \
5583 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5584 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5586 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5587 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5589 #define SCAN_PUT(ATTR, FUNC) \
5590 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5592 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5594 #define SCAN_END(ATTR) \
5596 SCAN_PUT(ATTR, NULL); \
5600 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5602 TYPE skey[CNT], smask[CNT]; \
5603 memset(&skey, 0, sizeof skey); \
5604 memset(&smask, 0, sizeof smask); \
5605 int idx = 0, cnt = CNT; \
5606 uint64_t fields = 0; \
5611 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5612 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5613 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5614 if (fields & (1UL << field)) { \
5616 if (++idx == cnt) { \
5620 s += strlen(NAME); \
5621 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5622 fields |= 1UL << field; \
5627 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5628 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5630 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5631 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5633 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5636 #define SCAN_END_ARRAY(ATTR) \
5641 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5645 #define SCAN_END_SINGLE(ATTR) \
5646 SCAN_FINISH_SINGLE(); \
5647 SCAN_PUT(ATTR, NULL); \
5651 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5652 SCAN_BEGIN(NAME, TYPE) { \
5653 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5654 } SCAN_END_SINGLE(ATTR)
5656 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5657 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5658 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5659 } SCAN_END_SINGLE(ATTR)
5661 /* scan_port needs one extra argument. */
5662 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5663 SCAN_BEGIN(NAME, TYPE) { \
5664 len = scan_port(s, &skey, &smask, \
5665 context->port_names); \
5670 } SCAN_END_SINGLE(ATTR)
5673 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5674 struct ofpbuf
*mask
)
5676 if (strncmp(s
, "nsh(", 4) == 0) {
5677 const char *start
= s
;
5679 struct ovs_key_nsh skey
, smask
;
5680 uint32_t spi
= 0, spi_mask
= 0;
5681 uint8_t si
= 0, si_mask
= 0;
5685 memset(&skey
, 0, sizeof skey
);
5686 memset(&smask
, 0, sizeof smask
);
5690 if (strncmp(s
, "flags=", 6) == 0) {
5692 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5700 if (strncmp(s
, "mdtype=", 7) == 0) {
5702 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5710 if (strncmp(s
, "np=", 3) == 0) {
5712 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5720 if (strncmp(s
, "spi=", 4) == 0) {
5722 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5730 if (strncmp(s
, "si=", 3) == 0) {
5732 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5740 if (strncmp(s
, "c1=", 3) == 0) {
5742 len
= scan_be32(s
, &skey
.context
[0],
5743 mask
? &smask
.context
[0] : NULL
);
5751 if (strncmp(s
, "c2=", 3) == 0) {
5753 len
= scan_be32(s
, &skey
.context
[1],
5754 mask
? &smask
.context
[1] : NULL
);
5762 if (strncmp(s
, "c3=", 3) == 0) {
5764 len
= scan_be32(s
, &skey
.context
[2],
5765 mask
? &smask
.context
[2] : NULL
);
5773 if (strncmp(s
, "c4=", 3) == 0) {
5775 len
= scan_be32(s
, &skey
.context
[3],
5776 mask
? &smask
.context
[3] : NULL
);
5783 } while (*s
++ == ',' && len
!= 0);
5788 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5789 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5791 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5793 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5801 parse_odp_key_mask_attr(struct parse_odp_context
*context
, const char *s
,
5802 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5808 if (context
->depth
== MAX_ODP_NESTED
) {
5811 retval
= parse_odp_key_mask_attr__(context
, s
, key
, mask
);
5820 parse_odp_key_mask_attr__(struct parse_odp_context
*context
, const char *s
,
5821 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5823 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5824 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5825 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5826 OVS_KEY_ATTR_RECIRC_ID
);
5827 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5829 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5830 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5831 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5832 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5834 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5835 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5836 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5837 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5838 SCAN_FIELD("tp_src=", be16
, src_port
);
5839 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5840 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5842 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5843 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5844 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5845 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5846 SCAN_FIELD("tp_src=", be16
, src_port
);
5847 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5848 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5850 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5851 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5852 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5853 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5854 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5855 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5856 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5857 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5858 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5859 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5860 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5862 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5863 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5865 SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata
, gtpu_metadata
,
5867 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5868 } SCAN_END_NESTED();
5870 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5872 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5873 SCAN_FIELD("src=", eth
, eth_src
);
5874 SCAN_FIELD("dst=", eth
, eth_dst
);
5875 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5877 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5878 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5879 SCAN_FIELD("vid=", vid
, tci
);
5880 SCAN_FIELD("pcp=", pcp
, tci
);
5881 SCAN_FIELD("cfi=", cfi
, tci
);
5882 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5884 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5886 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5887 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5888 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5889 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5890 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5891 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5893 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5894 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5895 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5896 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5897 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5898 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5899 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5900 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5902 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5903 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5904 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5905 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5906 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5907 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5908 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5909 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5910 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5912 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5913 SCAN_FIELD("src=", be16
, tcp_src
);
5914 SCAN_FIELD("dst=", be16
, tcp_dst
);
5915 } SCAN_END(OVS_KEY_ATTR_TCP
);
5917 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5919 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5920 SCAN_FIELD("src=", be16
, udp_src
);
5921 SCAN_FIELD("dst=", be16
, udp_dst
);
5922 } SCAN_END(OVS_KEY_ATTR_UDP
);
5924 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5925 SCAN_FIELD("src=", be16
, sctp_src
);
5926 SCAN_FIELD("dst=", be16
, sctp_dst
);
5927 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5929 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5930 SCAN_FIELD("type=", u8
, icmp_type
);
5931 SCAN_FIELD("code=", u8
, icmp_code
);
5932 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5934 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5935 SCAN_FIELD("type=", u8
, icmpv6_type
);
5936 SCAN_FIELD("code=", u8
, icmpv6_code
);
5937 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5939 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5940 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5941 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5942 SCAN_FIELD("op=", be16
, arp_op
);
5943 SCAN_FIELD("sha=", eth
, arp_sha
);
5944 SCAN_FIELD("tha=", eth
, arp_tha
);
5945 } SCAN_END(OVS_KEY_ATTR_ARP
);
5947 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5948 SCAN_FIELD("target=", in6_addr
, nd_target
);
5949 SCAN_FIELD("sll=", eth
, nd_sll
);
5950 SCAN_FIELD("tll=", eth
, nd_tll
);
5951 } SCAN_END(OVS_KEY_ATTR_ND
);
5953 SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions
) {
5954 SCAN_FIELD("nd_reserved=", be32
, nd_reserved
);
5955 SCAN_FIELD("nd_options_type=", u8
, nd_options_type
);
5956 } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS
);
5958 struct packet_type
{
5962 SCAN_BEGIN("packet_type(", struct packet_type
) {
5963 SCAN_FIELD("ns=", be16
, ns
);
5964 SCAN_FIELD("id=", be16
, id
);
5965 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5967 /* nsh is nested, it needs special process */
5968 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5975 /* Encap open-coded. */
5976 if (!strncmp(s
, "encap(", 6)) {
5977 const char *start
= s
;
5978 size_t encap
, encap_mask
= 0;
5980 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5982 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5989 s
+= strspn(s
, delimiters
);
5992 } else if (*s
== ')') {
5996 retval
= parse_odp_key_mask_attr(context
, s
, key
, mask
);
6001 if (nl_attr_oversized(key
->size
- encap
- NLA_HDRLEN
)) {
6008 nl_msg_end_nested(key
, encap
);
6010 nl_msg_end_nested(mask
, encap_mask
);
6019 /* Parses the string representation of a datapath flow key, in the format
6020 * output by odp_flow_key_format(). Returns 0 if successful, otherwise a
6021 * positive errno value. On success, stores NULL into '*errorp' and the flow
6022 * key is appended to 'key' as a series of Netlink attributes. On failure,
6023 * stores a malloc()'d error message in '*errorp' without changing the data in
6024 * 'key'. Either way, 'key''s data might be reallocated.
6026 * If 'port_names' is nonnull, it points to an simap that maps from a port name
6027 * to a port number. (Port names may be used instead of port numbers in
6030 * On success, the attributes appended to 'key' are individually syntactically
6031 * valid, but they may not be valid as a sequence. 'key' might, for example,
6032 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
6034 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
6035 struct ofpbuf
*key
, struct ofpbuf
*mask
,
6042 const size_t old_size
= key
->size
;
6043 struct parse_odp_context context
= (struct parse_odp_context
) {
6044 .port_names
= port_names
,
6049 s
+= strspn(s
, delimiters
);
6056 retval
= odp_ufid_from_string(s
, &ufid
);
6059 *errorp
= xasprintf("syntax error at %s", s
);
6061 key
->size
= old_size
;
6063 } else if (retval
> 0) {
6065 s
+= s
[0] == ' ' ? 1 : 0;
6068 retval
= parse_odp_key_mask_attr(&context
, s
, key
, mask
);
6071 *errorp
= xasprintf("syntax error at %s", s
);
6073 key
->size
= old_size
;
6083 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
6086 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
6087 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
6088 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
6089 * must use a zero mask for the netlink frag field, and all ones mask
6091 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
6093 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
6094 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
6095 : OVS_FRAG_TYPE_FIRST
;
6098 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
6099 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
6100 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
6102 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
6104 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
6106 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
6108 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
6109 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
6110 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
6111 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
6112 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
6114 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
6117 /* These share the same layout. */
6119 struct ovs_key_tcp tcp
;
6120 struct ovs_key_udp udp
;
6121 struct ovs_key_sctp sctp
;
6124 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
6125 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
6128 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
6129 bool export_mask
, struct ofpbuf
*buf
)
6131 /* New "struct flow" fields that are visible to the datapath (including all
6132 * data fields) should be translated into equivalent datapath flow fields
6133 * here (you will have to add a OVS_KEY_ATTR_* for them). */
6134 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
6136 struct ovs_key_ethernet
*eth_key
;
6137 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
6139 const struct flow
*flow
= parms
->flow
;
6140 const struct flow
*mask
= parms
->mask
;
6141 const struct flow
*data
= export_mask
? mask
: flow
;
6143 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
6145 if (flow_tnl_dst_is_set(&flow
->tunnel
) ||
6146 flow_tnl_src_is_set(&flow
->tunnel
) || export_mask
) {
6147 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
6148 parms
->key_buf
, NULL
);
6151 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
6153 if (parms
->support
.ct_state
) {
6154 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
6155 ovs_to_odp_ct_state(data
->ct_state
));
6157 if (parms
->support
.ct_zone
) {
6158 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
6160 if (parms
->support
.ct_mark
) {
6161 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
6163 if (parms
->support
.ct_label
) {
6164 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
6165 sizeof(data
->ct_label
));
6167 if (flow
->ct_nw_proto
) {
6168 if (parms
->support
.ct_orig_tuple
6169 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6170 struct ovs_key_ct_tuple_ipv4
*ct
;
6172 /* 'struct ovs_key_ct_tuple_ipv4' has padding, clear it. */
6173 ct
= nl_msg_put_unspec_zero(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6175 ct
->ipv4_src
= data
->ct_nw_src
;
6176 ct
->ipv4_dst
= data
->ct_nw_dst
;
6177 ct
->src_port
= data
->ct_tp_src
;
6178 ct
->dst_port
= data
->ct_tp_dst
;
6179 ct
->ipv4_proto
= data
->ct_nw_proto
;
6180 } else if (parms
->support
.ct_orig_tuple6
6181 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6182 struct ovs_key_ct_tuple_ipv6
*ct
;
6184 /* 'struct ovs_key_ct_tuple_ipv6' has padding, clear it. */
6185 ct
= nl_msg_put_unspec_zero(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6187 ct
->ipv6_src
= data
->ct_ipv6_src
;
6188 ct
->ipv6_dst
= data
->ct_ipv6_dst
;
6189 ct
->src_port
= data
->ct_tp_src
;
6190 ct
->dst_port
= data
->ct_tp_dst
;
6191 ct
->ipv6_proto
= data
->ct_nw_proto
;
6194 if (parms
->support
.recirc
) {
6195 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
6196 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
6199 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
6200 * is not the magical value "ODPP_NONE". */
6201 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
6202 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
6205 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
6207 if (OVS_UNLIKELY(parms
->probe
)) {
6208 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
6210 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
6213 /* Conditionally add L2 attributes for Ethernet packets */
6214 if (flow
->packet_type
== htonl(PT_ETH
)) {
6215 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
6217 get_ethernet_key(data
, eth_key
);
6219 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
6220 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
6222 if (flow
->vlans
[encaps
].tci
== htons(0)) {
6223 if (eth_type_vlan(flow
->dl_type
)) {
6224 /* If VLAN was truncated the tpid is in dl_type */
6225 tpid
= flow
->dl_type
;
6232 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
6234 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
6236 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
6237 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
6238 if (flow
->vlans
[encaps
].tci
== htons(0)) {
6244 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6245 /* For backwards compatibility with kernels that don't support
6246 * wildcarding, the following convention is used to encode the
6247 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
6250 * -------- -------- -------
6251 * >0x5ff 0xffff Specified Ethernet II Ethertype.
6252 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
6253 * <none> 0xffff Any non-Ethernet II frame (except valid
6254 * 802.3 SNAP packet with valid eth_type).
6257 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
6262 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
6264 if (eth_type_vlan(flow
->dl_type
)) {
6268 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6269 struct ovs_key_ipv4
*ipv4_key
;
6271 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
6273 get_ipv4_key(data
, ipv4_key
, export_mask
);
6274 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6275 struct ovs_key_ipv6
*ipv6_key
;
6277 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
6279 get_ipv6_key(data
, ipv6_key
, export_mask
);
6280 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6281 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6282 struct ovs_key_arp
*arp_key
;
6284 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
6286 get_arp_key(data
, arp_key
);
6287 } else if (eth_type_mpls(flow
->dl_type
)) {
6288 struct ovs_key_mpls
*mpls_key
;
6291 n
= flow_count_mpls_labels(flow
, NULL
);
6293 n
= MIN(n
, parms
->support
.max_mpls_depth
);
6295 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
6296 n
* sizeof *mpls_key
);
6297 for (i
= 0; i
< n
; i
++) {
6298 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
6300 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6301 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
6304 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6305 if (flow
->nw_proto
== IPPROTO_TCP
) {
6306 union ovs_key_tp
*tcp_key
;
6308 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
6310 get_tp_key(data
, tcp_key
);
6311 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
6312 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
6314 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
6315 union ovs_key_tp
*udp_key
;
6317 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
6319 get_tp_key(data
, udp_key
);
6320 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
6321 union ovs_key_tp
*sctp_key
;
6323 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
6325 get_tp_key(data
, sctp_key
);
6326 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
6327 && flow
->nw_proto
== IPPROTO_ICMP
) {
6328 struct ovs_key_icmp
*icmp_key
;
6330 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
6332 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
6333 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
6334 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6335 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
6336 struct ovs_key_icmpv6
*icmpv6_key
;
6338 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
6339 sizeof *icmpv6_key
);
6340 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
6341 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
6343 if (is_nd(flow
, NULL
)
6344 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
6345 * type and code are 8 bits wide. Therefore, an exact match
6346 * looks like htons(0xff), not htons(0xffff). See
6347 * xlate_wc_finish() for details. */
6348 && (!export_mask
|| (data
->tp_src
== htons(0xff)
6349 && data
->tp_dst
== htons(0xff)))) {
6350 struct ovs_key_nd
*nd_key
;
6351 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
6353 nd_key
->nd_target
= data
->nd_target
;
6354 nd_key
->nd_sll
= data
->arp_sha
;
6355 nd_key
->nd_tll
= data
->arp_tha
;
6357 /* Add ND Extensions Attr only if supported and reserved field
6358 * or options type is set. */
6359 if (parms
->support
.nd_ext
) {
6360 struct ovs_key_nd_extensions
*nd_ext_key
;
6362 if (data
->igmp_group_ip4
!= 0 || data
->tcp_flags
!= 0) {
6363 /* 'struct ovs_key_nd_extensions' has padding,
6365 nd_ext_key
= nl_msg_put_unspec_zero(buf
,
6366 OVS_KEY_ATTR_ND_EXTENSIONS
,
6367 sizeof *nd_ext_key
);
6368 nd_ext_key
->nd_reserved
= data
->igmp_group_ip4
;
6369 nd_ext_key
->nd_options_type
= ntohs(data
->tcp_flags
);
6377 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
6378 if (encap
[encaps
]) {
6379 nl_msg_end_nested(buf
, encap
[encaps
]);
6384 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6386 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6387 * capable of being expanded to allow for that much space. */
6389 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
6392 odp_flow_key_from_flow__(parms
, false, buf
);
6395 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6398 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6399 * capable of being expanded to allow for that much space. */
6401 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
6404 odp_flow_key_from_flow__(parms
, true, buf
);
6407 /* Generate ODP flow key from the given packet metadata */
6409 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
6411 const struct pkt_metadata
*md
= &packet
->md
;
6413 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
6416 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, md
->dp_hash
);
6419 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
6420 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
6423 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
6426 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
6427 ovs_to_odp_ct_state(md
->ct_state
));
6429 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
6432 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
6434 if (!ovs_u128_is_zero(md
->ct_label
)) {
6435 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
6436 sizeof(md
->ct_label
));
6438 if (md
->ct_orig_tuple_ipv6
) {
6439 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
6440 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6441 &md
->ct_orig_tuple
.ipv6
,
6442 sizeof md
->ct_orig_tuple
.ipv6
);
6445 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
6446 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6447 &md
->ct_orig_tuple
.ipv4
,
6448 sizeof md
->ct_orig_tuple
.ipv4
);
6453 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6454 * value "ODPP_NONE". */
6455 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
6456 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
6459 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6460 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
6461 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
6462 pt_ns_type_be(packet
->packet_type
));
6466 /* Generate packet metadata from the given ODP flow key. */
6468 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
6469 struct dp_packet
*packet
)
6471 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6472 const struct nlattr
*nla
;
6473 struct pkt_metadata
*md
= &packet
->md
;
6474 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
6475 ovs_be16 ethertype
= 0;
6478 pkt_metadata_init(md
, ODPP_NONE
);
6480 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6481 enum ovs_key_attr type
= nl_attr_type(nla
);
6482 size_t len
= nl_attr_get_size(nla
);
6483 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6484 OVS_KEY_ATTR_MAX
, type
);
6486 if (len
!= expected_len
&& expected_len
>= 0) {
6491 case OVS_KEY_ATTR_RECIRC_ID
:
6492 md
->recirc_id
= nl_attr_get_u32(nla
);
6494 case OVS_KEY_ATTR_DP_HASH
:
6495 md
->dp_hash
= nl_attr_get_u32(nla
);
6497 case OVS_KEY_ATTR_PRIORITY
:
6498 md
->skb_priority
= nl_attr_get_u32(nla
);
6500 case OVS_KEY_ATTR_SKB_MARK
:
6501 md
->pkt_mark
= nl_attr_get_u32(nla
);
6503 case OVS_KEY_ATTR_CT_STATE
:
6504 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
6506 case OVS_KEY_ATTR_CT_ZONE
:
6507 md
->ct_zone
= nl_attr_get_u16(nla
);
6509 case OVS_KEY_ATTR_CT_MARK
:
6510 md
->ct_mark
= nl_attr_get_u32(nla
);
6512 case OVS_KEY_ATTR_CT_LABELS
: {
6513 md
->ct_label
= nl_attr_get_u128(nla
);
6516 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6517 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6518 md
->ct_orig_tuple
.ipv4
= *ct
;
6519 md
->ct_orig_tuple_ipv6
= false;
6522 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6523 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6525 md
->ct_orig_tuple
.ipv6
= *ct
;
6526 md
->ct_orig_tuple_ipv6
= true;
6529 case OVS_KEY_ATTR_TUNNEL
: {
6530 enum odp_key_fitness res
;
6532 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
, NULL
);
6533 if (res
== ODP_FIT_ERROR
) {
6534 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6538 case OVS_KEY_ATTR_IN_PORT
:
6539 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6541 case OVS_KEY_ATTR_ETHERNET
:
6542 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6543 packet_type
= htonl(PT_ETH
);
6545 case OVS_KEY_ATTR_ETHERTYPE
:
6546 ethertype
= nl_attr_get_be16(nla
);
6548 case OVS_KEY_ATTR_UNSPEC
:
6549 case OVS_KEY_ATTR_ENCAP
:
6550 case OVS_KEY_ATTR_VLAN
:
6551 case OVS_KEY_ATTR_IPV4
:
6552 case OVS_KEY_ATTR_IPV6
:
6553 case OVS_KEY_ATTR_TCP
:
6554 case OVS_KEY_ATTR_UDP
:
6555 case OVS_KEY_ATTR_ICMP
:
6556 case OVS_KEY_ATTR_ICMPV6
:
6557 case OVS_KEY_ATTR_ARP
:
6558 case OVS_KEY_ATTR_ND
:
6559 case OVS_KEY_ATTR_ND_EXTENSIONS
:
6560 case OVS_KEY_ATTR_SCTP
:
6561 case OVS_KEY_ATTR_TCP_FLAGS
:
6562 case OVS_KEY_ATTR_MPLS
:
6563 case OVS_KEY_ATTR_PACKET_TYPE
:
6564 case OVS_KEY_ATTR_NSH
:
6565 case __OVS_KEY_ATTR_MAX
:
6571 if (packet_type
== htonl(PT_ETH
)) {
6572 packet
->packet_type
= htonl(PT_ETH
);
6573 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6574 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6577 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6581 /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'.
6582 * Generated value has format of random UUID. */
6584 odp_flow_key_hash(const void *key
, size_t key_len
, ovs_u128
*hash
)
6586 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6587 static uint32_t secret
;
6589 if (ovsthread_once_start(&once
)) {
6590 secret
= random_uint32();
6591 ovsthread_once_done(&once
);
6593 hash_bytes128(key
, key_len
, secret
, hash
);
6594 uuid_set_bits_v4((struct uuid
*)hash
);
6598 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6599 uint64_t attrs
, int out_of_range_attr
,
6600 const struct nlattr
*key
, size_t key_len
)
6605 if (VLOG_DROP_DBG(rl
)) {
6610 for (i
= 0; i
< 64; i
++) {
6611 if (attrs
& (UINT64_C(1) << i
)) {
6612 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6614 ds_put_format(&s
, " %s",
6615 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6618 if (out_of_range_attr
) {
6619 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6622 ds_put_cstr(&s
, ": ");
6623 odp_flow_key_format(key
, key_len
, &s
);
6625 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6630 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6632 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6635 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6638 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6639 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6640 return 0xff; /* Error. */
6643 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6644 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6645 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6648 /* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6649 * must have OVS_KEY_ATTR_MAX + 1 elements. Stores each attribute in 'key'
6650 * into the corresponding element of 'attrs'.
6652 * Stores a bitmask of the attributes' indexes found in 'key' into
6653 * '*present_attrsp'.
6655 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6656 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6658 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6659 * error message in '*errorp'. */
6661 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6662 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6663 int *out_of_range_attrp
, char **errorp
)
6665 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6666 const struct nlattr
*nla
;
6667 uint64_t present_attrs
;
6670 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6672 *out_of_range_attrp
= 0;
6673 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6674 uint16_t type
= nl_attr_type(nla
);
6675 size_t len
= nl_attr_get_size(nla
);
6676 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6677 OVS_KEY_ATTR_MAX
, type
);
6679 if (len
!= expected_len
&& expected_len
>= 0) {
6680 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6682 odp_parse_error(&rl
, errorp
, "attribute %s has length %"PRIuSIZE
" "
6683 "but should have length %d",
6684 ovs_key_attr_to_string(type
, namebuf
,
6690 if (type
> OVS_KEY_ATTR_MAX
) {
6691 *out_of_range_attrp
= type
;
6693 if (present_attrs
& (UINT64_C(1) << type
)) {
6694 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6696 odp_parse_error(&rl
, errorp
,
6697 "duplicate %s attribute in flow key",
6698 ovs_key_attr_to_string(type
, namebuf
,
6703 present_attrs
|= UINT64_C(1) << type
;
6708 odp_parse_error(&rl
, errorp
, "trailing garbage in flow key");
6712 *present_attrsp
= present_attrs
;
6716 static enum odp_key_fitness
6717 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6718 uint64_t expected_attrs
,
6719 const struct nlattr
*key
, size_t key_len
)
6721 uint64_t missing_attrs
;
6722 uint64_t extra_attrs
;
6724 missing_attrs
= expected_attrs
& ~present_attrs
;
6725 if (missing_attrs
) {
6726 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6727 log_odp_key_attributes(&rl
, "expected but not present",
6728 missing_attrs
, 0, key
, key_len
);
6729 return ODP_FIT_TOO_LITTLE
;
6732 extra_attrs
= present_attrs
& ~expected_attrs
;
6733 if (extra_attrs
|| out_of_range_attr
) {
6734 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6735 log_odp_key_attributes(&rl
, "present but not expected",
6736 extra_attrs
, out_of_range_attr
, key
, key_len
);
6737 return ODP_FIT_TOO_MUCH
;
6740 return ODP_FIT_PERFECT
;
6743 /* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
6744 * attributes in the bit-mask 'present_attrs' are present. Returns true if
6745 * successful, false on failure.
6747 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6748 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6749 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6750 * previously parsed flow key.
6752 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6753 * error message in '*errorp'. */
6755 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6756 uint64_t present_attrs
, uint64_t *expected_attrs
,
6757 struct flow
*flow
, const struct flow
*src_flow
,
6760 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6761 bool is_mask
= flow
!= src_flow
;
6763 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6764 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6765 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6766 odp_parse_error(&rl
, errorp
,
6767 "invalid Ethertype %"PRIu16
" in flow key",
6768 ntohs(flow
->dl_type
));
6771 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6772 flow
->dl_type
!= htons(0xffff)) {
6773 odp_parse_error(&rl
, errorp
, "can't bitwise match non-Ethernet II "
6774 "\"Ethertype\" %#"PRIx16
" (with mask %#"PRIx16
")",
6775 ntohs(src_flow
->dl_type
), ntohs(flow
->dl_type
));
6778 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6781 /* Default ethertype for well-known L3 packets. */
6782 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6783 flow
->dl_type
= htons(ETH_TYPE_IP
);
6784 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6785 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6786 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6787 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6789 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6791 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6792 /* dl_type is mandatory for non-Ethernet packets */
6793 flow
->dl_type
= htons(0xffff);
6794 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6795 /* See comments in odp_flow_key_from_flow__(). */
6796 odp_parse_error(&rl
, errorp
,
6797 "mask expected for non-Ethernet II frame");
6804 /* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
6805 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
6806 * present. The caller also indicates an out-of-range attribute
6807 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
6808 * cannot be perfect).
6810 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6811 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6812 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6813 * previously parsed flow key.
6815 * Returns fitness based on any discrepancies between present and expected
6816 * attributes, except that a 'need_check' of false overrides this.
6818 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6819 * error message in '*errorp'. 'key' and 'key_len' are just used for error
6820 * reporting in this case. */
6821 static enum odp_key_fitness
6822 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6823 uint64_t present_attrs
, int out_of_range_attr
,
6824 uint64_t *expected_attrs
, struct flow
*flow
,
6825 const struct nlattr
*key
, size_t key_len
,
6826 const struct flow
*src_flow
, bool need_check
, char **errorp
)
6828 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6829 bool is_mask
= src_flow
!= flow
;
6830 const void *check_start
= NULL
;
6831 size_t check_len
= 0;
6832 enum ovs_key_attr expected_bit
= 0xff;
6834 if (eth_type_mpls(src_flow
->dl_type
)) {
6835 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6836 *expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6838 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6839 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6840 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6841 int n
= size
/ sizeof(ovs_be32
);
6844 if (!size
|| size
% sizeof(ovs_be32
)) {
6845 odp_parse_error(&rl
, errorp
,
6846 "MPLS LSEs have invalid length %"PRIuSIZE
,
6848 return ODP_FIT_ERROR
;
6850 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6851 odp_parse_error(&rl
, errorp
,
6852 "unexpected MPLS Ethertype mask %x"PRIx16
,
6853 ntohs(flow
->dl_type
));
6854 return ODP_FIT_ERROR
;
6857 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6858 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6860 if (n
> FLOW_MAX_MPLS_LABELS
) {
6861 return ODP_FIT_TOO_MUCH
;
6865 /* BOS may be set only in the innermost label. */
6866 for (i
= 0; i
< n
- 1; i
++) {
6867 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6868 odp_parse_error(&rl
, errorp
,
6869 "MPLS BOS set in non-innermost label");
6870 return ODP_FIT_ERROR
;
6874 /* BOS must be set in the innermost label. */
6875 if (n
< FLOW_MAX_MPLS_LABELS
6876 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6877 return ODP_FIT_TOO_LITTLE
;
6883 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6885 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6887 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6888 const struct ovs_key_ipv4
*ipv4_key
;
6890 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6891 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6892 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6893 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV4 has invalid "
6894 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6895 return ODP_FIT_ERROR
;
6899 check_start
= ipv4_key
;
6900 check_len
= sizeof *ipv4_key
;
6901 expected_bit
= OVS_KEY_ATTR_IPV4
;
6904 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6906 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6908 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6909 const struct ovs_key_ipv6
*ipv6_key
;
6911 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6912 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6913 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6914 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV6 has invalid "
6915 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6916 return ODP_FIT_ERROR
;
6919 check_start
= ipv6_key
;
6920 check_len
= sizeof *ipv6_key
;
6921 expected_bit
= OVS_KEY_ATTR_IPV6
;
6924 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6925 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6927 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6929 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6930 const struct ovs_key_arp
*arp_key
;
6932 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6933 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6934 odp_parse_error(&rl
, errorp
,
6935 "unsupported ARP opcode %"PRIu16
" in flow "
6936 "key", ntohs(arp_key
->arp_op
));
6937 return ODP_FIT_ERROR
;
6939 put_arp_key(arp_key
, flow
);
6941 check_start
= arp_key
;
6942 check_len
= sizeof *arp_key
;
6943 expected_bit
= OVS_KEY_ATTR_ARP
;
6946 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6948 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6950 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6951 if (odp_nsh_key_from_attr__(attrs
[OVS_KEY_ATTR_NSH
],
6952 is_mask
, &flow
->nsh
,
6953 NULL
, errorp
) == ODP_FIT_ERROR
) {
6954 return ODP_FIT_ERROR
;
6957 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6958 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6959 expected_bit
= OVS_KEY_ATTR_NSH
;
6965 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6966 if (!is_all_zeros(check_start
, check_len
) &&
6967 flow
->dl_type
!= htons(0xffff)) {
6968 odp_parse_error(&rl
, errorp
, "unexpected L3 matching with "
6969 "masked Ethertype %#"PRIx16
"/%#"PRIx16
,
6970 ntohs(src_flow
->dl_type
),
6971 ntohs(flow
->dl_type
));
6972 return ODP_FIT_ERROR
;
6974 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6978 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6979 if (src_flow
->nw_proto
== IPPROTO_TCP
6980 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6981 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6982 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6984 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6986 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6987 const union ovs_key_tp
*tcp_key
;
6989 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6990 put_tp_key(tcp_key
, flow
);
6991 expected_bit
= OVS_KEY_ATTR_TCP
;
6993 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6994 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6995 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6997 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6998 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6999 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
7000 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7002 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
7004 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
7005 const union ovs_key_tp
*udp_key
;
7007 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
7008 put_tp_key(udp_key
, flow
);
7009 expected_bit
= OVS_KEY_ATTR_UDP
;
7011 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
7012 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
7013 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
7014 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7016 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
7018 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
7019 const union ovs_key_tp
*sctp_key
;
7021 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
7022 put_tp_key(sctp_key
, flow
);
7023 expected_bit
= OVS_KEY_ATTR_SCTP
;
7025 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
7026 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
7027 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7029 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
7031 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
7032 const struct ovs_key_icmp
*icmp_key
;
7034 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
7035 flow
->tp_src
= htons(icmp_key
->icmp_type
);
7036 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
7037 expected_bit
= OVS_KEY_ATTR_ICMP
;
7039 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
7040 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
7041 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7043 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
7045 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
7046 const struct ovs_key_icmpv6
*icmpv6_key
;
7048 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
7049 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
7050 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
7051 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
7052 if (is_nd(src_flow
, NULL
)) {
7054 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
7056 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
7057 const struct ovs_key_nd
*nd_key
;
7059 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
7060 flow
->nd_target
= nd_key
->nd_target
;
7061 flow
->arp_sha
= nd_key
->nd_sll
;
7062 flow
->arp_tha
= nd_key
->nd_tll
;
7064 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7065 * ICMP type and code are 8 bits wide. Therefore, an
7066 * exact match looks like htons(0xff), not
7067 * htons(0xffff). See xlate_wc_finish() for details.
7069 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
7070 (flow
->tp_src
!= htons(0xff) ||
7071 flow
->tp_dst
!= htons(0xff))) {
7072 odp_parse_error(&rl
, errorp
,
7073 "ICMP (src,dst) masks should be "
7074 "(0xff,0xff) but are actually "
7075 "(%#"PRIx16
",%#"PRIx16
")",
7076 ntohs(flow
->tp_src
),
7077 ntohs(flow
->tp_dst
));
7078 return ODP_FIT_ERROR
;
7080 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
7085 (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
)) {
7086 const struct ovs_key_nd_extensions
*nd_ext_key
;
7089 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
7093 nl_attr_get(attrs
[OVS_KEY_ATTR_ND_EXTENSIONS
]);
7094 flow
->igmp_group_ip4
= nd_ext_key
->nd_reserved
;
7095 flow
->tcp_flags
= htons(nd_ext_key
->nd_options_type
);
7098 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7099 * ICMP type and code are 8 bits wide. Therefore, an
7100 * exact match looks like htons(0xff), not
7101 * htons(0xffff). See xlate_wc_finish() for details.
7103 if (!is_all_zeros(nd_ext_key
, sizeof *nd_ext_key
) &&
7104 (flow
->tp_src
!= htons(0xff) ||
7105 flow
->tp_dst
!= htons(0xff))) {
7106 return ODP_FIT_ERROR
;
7109 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
7115 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
7116 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
7117 /* OVS userspace parses the IGMP type, code, and group, but its
7118 * datapaths do not, so there is always missing information. */
7119 return ODP_FIT_TOO_LITTLE
;
7121 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
7122 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
7123 odp_parse_error(&rl
, errorp
, "flow matches on L4 ports but does "
7124 "not define an L4 protocol");
7125 return ODP_FIT_ERROR
;
7127 *expected_attrs
|= UINT64_C(1) << expected_bit
;
7132 return need_check
? check_expectations(present_attrs
, out_of_range_attr
,
7133 *expected_attrs
, key
, key_len
) : ODP_FIT_PERFECT
;
7136 /* Parse 802.1Q header then encapsulated L3 attributes. */
7137 static enum odp_key_fitness
7138 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
7139 uint64_t present_attrs
, int out_of_range_attr
,
7140 uint64_t expected_attrs
, struct flow
*flow
,
7141 const struct nlattr
*key
, size_t key_len
,
7142 const struct flow
*src_flow
, char **errorp
)
7144 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7145 bool is_mask
= src_flow
!= flow
;
7147 const struct nlattr
*encap
;
7148 enum odp_key_fitness encap_fitness
;
7149 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
7152 while (encaps
< flow_vlan_limit
&&
7154 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
7155 : eth_type_vlan(flow
->dl_type
))) {
7157 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
7158 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
7160 /* Calculate fitness of outer attributes. */
7162 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
7163 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
7165 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
7166 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
7168 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
7169 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
7172 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
7173 expected_attrs
, key
, key_len
);
7176 * Remove the TPID from dl_type since it's not the real Ethertype. */
7177 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
7178 flow
->dl_type
= htons(0);
7179 flow
->vlans
[encaps
].tci
=
7180 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
7181 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
7184 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
7185 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
7186 return ODP_FIT_TOO_LITTLE
;
7187 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
7188 /* Corner case for a truncated 802.1Q header. */
7189 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
7190 return ODP_FIT_TOO_MUCH
;
7193 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
7195 &rl
, errorp
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
7196 "but CFI bit is not set", ntohs(flow
->vlans
[encaps
].tci
));
7197 return ODP_FIT_ERROR
;
7200 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
7205 /* Now parse the encapsulated attributes. */
7206 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
7207 attrs
, &present_attrs
, &out_of_range_attr
,
7209 return ODP_FIT_ERROR
;
7213 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
7214 flow
, src_flow
, errorp
)) {
7215 return ODP_FIT_ERROR
;
7217 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
,
7221 src_flow
, false, errorp
);
7222 if (encap_fitness
!= ODP_FIT_PERFECT
) {
7223 return encap_fitness
;
7228 return check_expectations(present_attrs
, out_of_range_attr
,
7229 expected_attrs
, key
, key_len
);
7232 static enum odp_key_fitness
7233 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
7234 struct flow
*flow
, const struct flow
*src_flow
,
7237 /* New "struct flow" fields that are visible to the datapath (including all
7238 * data fields) should be translated from equivalent datapath flow fields
7239 * here (you will have to add a OVS_KEY_ATTR_* for them). */
7240 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
7242 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
7247 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
7248 uint64_t expected_attrs
;
7249 uint64_t present_attrs
;
7250 int out_of_range_attr
;
7251 bool is_mask
= src_flow
!= flow
;
7253 memset(flow
, 0, sizeof *flow
);
7255 /* Parse attributes. */
7256 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
7257 &out_of_range_attr
, errorp
)) {
7263 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
7264 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
7265 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
7266 } else if (is_mask
) {
7267 /* Always exact match recirc_id if it is not specified. */
7268 flow
->recirc_id
= UINT32_MAX
;
7271 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
7272 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
7273 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
7275 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
7276 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
7277 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
7280 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
7281 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
7282 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
7285 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
7286 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
7288 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
7289 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
7291 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
7292 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
7293 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
7295 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
7296 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
7297 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
7299 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
7300 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
7301 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
7303 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
7304 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
7305 flow
->ct_nw_src
= ct
->ipv4_src
;
7306 flow
->ct_nw_dst
= ct
->ipv4_dst
;
7307 flow
->ct_nw_proto
= ct
->ipv4_proto
;
7308 flow
->ct_tp_src
= ct
->src_port
;
7309 flow
->ct_tp_dst
= ct
->dst_port
;
7310 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
7312 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
7313 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
7315 flow
->ct_ipv6_src
= ct
->ipv6_src
;
7316 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
7317 flow
->ct_nw_proto
= ct
->ipv6_proto
;
7318 flow
->ct_tp_src
= ct
->src_port
;
7319 flow
->ct_tp_dst
= ct
->dst_port
;
7320 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
7323 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
7324 enum odp_key_fitness res
;
7326 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
7327 &flow
->tunnel
, errorp
);
7328 if (res
== ODP_FIT_ERROR
) {
7330 } else if (res
== ODP_FIT_PERFECT
) {
7331 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
7335 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
7336 flow
->in_port
.odp_port
7337 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
7338 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
7339 } else if (!is_mask
) {
7340 flow
->in_port
.odp_port
= ODPP_NONE
;
7343 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
7345 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
7346 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
7347 if (pt_ns(src_flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
7348 flow
->dl_type
= pt_ns_type_be(flow
->packet_type
);
7350 } else if (!is_mask
) {
7351 flow
->packet_type
= htonl(PT_ETH
);
7354 /* Check for Ethernet header. */
7355 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
7356 const struct ovs_key_ethernet
*eth_key
;
7358 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
7359 put_ethernet_key(eth_key
, flow
);
7361 flow
->packet_type
= htonl(PT_ETH
);
7363 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
7365 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
7366 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
7368 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
7371 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
7374 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7375 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
7376 src_flow
, errorp
)) {
7381 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
7382 : eth_type_vlan(src_flow
->dl_type
)) {
7383 fitness
= parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
7384 expected_attrs
, flow
, key
, key_len
,
7388 /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7390 flow
->vlans
[0].tpid
= htons(0xffff);
7391 flow
->vlans
[0].tci
= htons(0xffff);
7392 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
7393 flow
->vlans
[0].tci
= nl_attr_get_be16(
7394 attrs
[OVS_KEY_ATTR_VLAN
]);
7395 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
7398 fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
7399 &expected_attrs
, flow
, key
, key_len
,
7400 src_flow
, true, errorp
);
7404 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7405 if (fitness
== ODP_FIT_ERROR
&& (errorp
|| !VLOG_DROP_WARN(&rl
))) {
7406 struct ds s
= DS_EMPTY_INITIALIZER
;
7408 ds_put_cstr(&s
, "the flow mask in error is: ");
7409 odp_flow_key_format(key
, key_len
, &s
);
7410 ds_put_cstr(&s
, ", for the following flow key: ");
7411 flow_format(&s
, src_flow
, NULL
);
7413 ds_put_cstr(&s
, "the flow key in error is: ");
7414 odp_flow_key_format(key
, key_len
, &s
);
7417 char *old_error
= *errorp
;
7418 *errorp
= xasprintf("%s; %s", old_error
, ds_cstr(&s
));
7421 VLOG_WARN("%s", ds_cstr(&s
));
7428 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7429 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
7430 * 'key' fits our expectations for what a flow key should contain.
7432 * The 'in_port' will be the datapath's understanding of the port. The
7433 * caller will need to translate with odp_port_to_ofp_port() if the
7434 * OpenFlow port is needed.
7436 * This function doesn't take the packet itself as an argument because none of
7437 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
7438 * it is always possible to infer which additional attribute(s) should appear
7439 * by looking at the attributes for lower-level protocols, e.g. if the network
7440 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7441 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7444 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7445 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7446 * '*errorp', otherwise NULL. */
7447 enum odp_key_fitness
7448 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
7449 struct flow
*flow
, char **errorp
)
7451 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
, errorp
);
7454 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7455 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
7456 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7457 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
7458 * well 'key' fits our expectations for what a flow key should contain.
7460 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7461 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7462 * '*errorp', otherwise NULL. */
7463 enum odp_key_fitness
7464 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
7465 struct flow_wildcards
*mask
, const struct flow
*src_flow
,
7469 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
7470 &mask
->masks
, src_flow
, errorp
);
7476 /* A missing mask means that the flow should be exact matched.
7477 * Generate an appropriate exact wildcard for the flow. */
7478 flow_wildcards_init_for_packet(mask
, src_flow
);
7480 return ODP_FIT_PERFECT
;
7484 /* Converts the netlink formated key/mask to match.
7485 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7486 * disagree on the acceptable form of flow */
7488 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
7489 const struct nlattr
*mask
, size_t mask_len
,
7490 struct match
*match
)
7492 enum odp_key_fitness fitness
;
7494 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
, NULL
);
7496 /* This should not happen: it indicates that
7497 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
7498 * the acceptable form of a flow. Log the problem as an error,
7499 * with enough details to enable debugging. */
7500 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7502 if (!VLOG_DROP_ERR(&rl
)) {
7506 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
7507 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
7514 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
,
7517 /* This should not happen: it indicates that
7518 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
7519 * disagree on the acceptable form of a mask. Log the problem
7520 * as an error, with enough details to enable debugging. */
7521 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7523 if (!VLOG_DROP_ERR(&rl
)) {
7527 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
7529 VLOG_ERR("internal error parsing flow mask %s (%s)",
7530 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
7540 /* Returns 'fitness' as a string, for use in debug messages. */
7542 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
7545 case ODP_FIT_PERFECT
:
7547 case ODP_FIT_TOO_MUCH
:
7549 case ODP_FIT_TOO_LITTLE
:
7550 return "too_little";
7558 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7559 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
7560 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
7561 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
7562 * null, then the return value is not meaningful.) */
7564 odp_put_userspace_action(uint32_t pid
,
7565 const void *userdata
, size_t userdata_size
,
7566 odp_port_t tunnel_out_port
,
7567 bool include_actions
,
7568 struct ofpbuf
*odp_actions
)
7570 size_t userdata_ofs
;
7573 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
7574 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
7576 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
7578 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7579 * module before Linux 3.10 required the userdata to be exactly 8 bytes
7582 * - The kernel rejected shorter userdata with -ERANGE.
7584 * - The kernel silently dropped userdata beyond the first 8 bytes.
7586 * Thus, for maximum compatibility, always put at least 8 bytes. (We
7587 * separately disable features that required more than 8 bytes.) */
7588 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
7589 MAX(8, userdata_size
)),
7590 userdata
, userdata_size
);
7594 if (tunnel_out_port
!= ODPP_NONE
) {
7595 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
7598 if (include_actions
) {
7599 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
7601 nl_msg_end_nested(odp_actions
, offset
);
7603 return userdata_ofs
;
7607 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
7609 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
7613 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
7614 const struct eth_addr
*eth_src
,
7615 const struct eth_addr
*eth_dst
)
7617 struct ovs_action_push_eth eth
;
7619 memset(ð
, 0, sizeof eth
);
7621 eth
.addresses
.eth_src
= *eth_src
;
7624 eth
.addresses
.eth_dst
= *eth_dst
;
7627 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
7632 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
7633 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7635 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7636 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
7637 nl_msg_end_nested(odp_actions
, offset
);
7641 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
7642 struct ovs_action_push_tnl
*data
)
7644 int size
= offsetof(struct ovs_action_push_tnl
, header
);
7646 size
+= data
->header_len
;
7647 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
7651 /* The commit_odp_actions() function and its helpers. */
7654 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
7655 const void *key
, size_t key_size
)
7657 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7658 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
7659 nl_msg_end_nested(odp_actions
, offset
);
7662 /* Masked set actions have a mask following the data within the netlink
7663 * attribute. The unmasked bits in the data will be cleared as the data
7664 * is copied to the action. */
7666 commit_masked_set_action(struct ofpbuf
*odp_actions
,
7667 enum ovs_key_attr key_type
,
7668 const void *key_
, const void *mask_
, size_t key_size
)
7670 size_t offset
= nl_msg_start_nested(odp_actions
,
7671 OVS_ACTION_ATTR_SET_MASKED
);
7672 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
7673 const char *key
= key_
, *mask
= mask_
;
7675 memcpy(data
+ key_size
, mask
, key_size
);
7676 /* Clear unmasked bits while copying. */
7677 while (key_size
--) {
7678 *data
++ = *key
++ & *mask
++;
7680 nl_msg_end_nested(odp_actions
, offset
);
7683 /* If any of the flow key data that ODP actions can modify are different in
7684 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7685 * 'odp_actions' that change the flow tunneling information in key from
7686 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7687 * same way. In other words, operates the same as commit_odp_actions(), but
7688 * only on tunneling information. */
7690 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7691 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7693 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7694 * must have non-zero ipv6_dst. */
7695 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7696 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7699 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7700 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7704 struct offsetof_sizeof
{
7710 /* Performs bitwise OR over the fields in 'dst_' and 'src_' specified in
7711 * 'offsetof_sizeof_arr' array. Result is stored in 'dst_'. */
7713 or_masks(void *dst_
, const void *src_
,
7714 struct offsetof_sizeof
*offsetof_sizeof_arr
)
7716 int field
, size
, offset
;
7717 const uint8_t *src
= src_
;
7718 uint8_t *dst
= dst_
;
7720 for (field
= 0; ; field
++) {
7721 size
= offsetof_sizeof_arr
[field
].size
;
7722 offset
= offsetof_sizeof_arr
[field
].offset
;
7727 or_bytes(dst
+ offset
, src
+ offset
, size
);
7731 /* Compares each of the fields in 'key0' and 'key1'. The fields are specified
7732 * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field.
7733 * Returns true if all of the fields are equal, false if at least one differs.
7734 * As a side effect, for each field that is the same in 'key0' and 'key1',
7735 * zeros the corresponding bytes in 'mask'. */
7737 keycmp_mask(const void *key0
, const void *key1
,
7738 struct offsetof_sizeof
*offsetof_sizeof_arr
, void *mask
)
7740 bool differ
= false;
7742 for (int field
= 0 ; ; field
++) {
7743 int size
= offsetof_sizeof_arr
[field
].size
;
7744 int offset
= offsetof_sizeof_arr
[field
].offset
;
7749 char *pkey0
= ((char *)key0
) + offset
;
7750 char *pkey1
= ((char *)key1
) + offset
;
7751 char *pmask
= ((char *)mask
) + offset
;
7752 if (memcmp(pkey0
, pkey1
, size
) == 0) {
7753 memset(pmask
, 0, size
);
7763 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7764 const void *key
, void *base
, void *mask
, size_t size
,
7765 struct offsetof_sizeof
*offsetof_sizeof_arr
,
7766 struct ofpbuf
*odp_actions
)
7768 if (keycmp_mask(key
, base
, offsetof_sizeof_arr
, mask
)) {
7769 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7771 if (use_masked_set
&& !fully_masked
) {
7772 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7774 if (!fully_masked
) {
7775 memset(mask
, 0xff, size
);
7777 commit_set_action(odp_actions
, attr
, key
, size
);
7779 memcpy(base
, key
, size
);
7782 /* Mask bits are set when we have either read or set the corresponding
7783 * values. Masked bits will be exact-matched, no need to set them
7784 * if the value did not actually change. */
7790 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7792 eth
->eth_src
= flow
->dl_src
;
7793 eth
->eth_dst
= flow
->dl_dst
;
7797 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7799 flow
->dl_src
= eth
->eth_src
;
7800 flow
->dl_dst
= eth
->eth_dst
;
7804 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7805 struct ofpbuf
*odp_actions
,
7806 struct flow_wildcards
*wc
,
7809 struct ovs_key_ethernet key
, base
, mask
, orig_mask
;
7810 struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr
[] =
7811 OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR
;
7813 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7817 get_ethernet_key(flow
, &key
);
7818 get_ethernet_key(base_flow
, &base
);
7819 get_ethernet_key(&wc
->masks
, &mask
);
7820 memcpy(&orig_mask
, &mask
, sizeof mask
);
7822 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7823 &key
, &base
, &mask
, sizeof key
,
7824 ovs_key_ethernet_offsetof_sizeof_arr
, odp_actions
)) {
7825 put_ethernet_key(&base
, base_flow
);
7826 or_masks(&mask
, &orig_mask
, ovs_key_ethernet_offsetof_sizeof_arr
);
7827 put_ethernet_key(&mask
, &wc
->masks
);
7832 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7833 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7835 int base_n
= flow_count_vlan_headers(base
);
7836 int flow_n
= flow_count_vlan_headers(flow
);
7837 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7839 /* Pop all mismatching vlan of base, push those of flow */
7840 for (; base_n
>= 0; base_n
--) {
7841 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7842 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7845 for (; flow_n
>= 0; flow_n
--) {
7846 struct ovs_action_push_vlan vlan
;
7848 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7849 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7850 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7851 &vlan
, sizeof vlan
);
7853 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7856 /* Wildcarding already done at action translation time. */
7858 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7859 struct ofpbuf
*odp_actions
)
7861 int base_n
= flow_count_mpls_labels(base
, NULL
);
7862 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7863 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7866 while (base_n
> common_n
) {
7867 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7868 /* If there is only one more LSE in base than there are common
7869 * between base and flow; and flow has at least one more LSE than
7870 * is common then the topmost LSE of base may be updated using
7872 struct ovs_key_mpls mpls_key
;
7874 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7875 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7876 &mpls_key
, sizeof mpls_key
);
7877 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7880 /* Otherwise, if there more LSEs in base than are common between
7881 * base and flow then pop the topmost one. */
7883 /* If all the LSEs are to be popped and this is not the outermost
7884 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7885 * POP_MPLS action instead of flow->dl_type.
7887 * This is because the POP_MPLS action requires its ethertype
7888 * argument to be an MPLS ethernet type but in this case
7889 * flow->dl_type will be a non-MPLS ethernet type.
7891 * When the final POP_MPLS action occurs it use flow->dl_type and
7892 * the and the resulting packet will have the desired dl_type. */
7893 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7894 dl_type
= htons(ETH_TYPE_MPLS
);
7896 dl_type
= flow
->dl_type
;
7898 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7899 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7904 /* If, after the above popping and setting, there are more LSEs in flow
7905 * than base then some LSEs need to be pushed. */
7906 while (base_n
< flow_n
) {
7907 struct ovs_action_push_mpls
*mpls
;
7909 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7910 OVS_ACTION_ATTR_PUSH_MPLS
,
7912 mpls
->mpls_ethertype
= flow
->dl_type
;
7913 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7914 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7915 * headers if the flow is restored later due to returning from a patch
7916 * port or group bucket. */
7917 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7918 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7924 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7926 ipv4
->ipv4_src
= flow
->nw_src
;
7927 ipv4
->ipv4_dst
= flow
->nw_dst
;
7928 ipv4
->ipv4_proto
= flow
->nw_proto
;
7929 ipv4
->ipv4_tos
= flow
->nw_tos
;
7930 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7931 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7935 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7937 flow
->nw_src
= ipv4
->ipv4_src
;
7938 flow
->nw_dst
= ipv4
->ipv4_dst
;
7939 flow
->nw_proto
= ipv4
->ipv4_proto
;
7940 flow
->nw_tos
= ipv4
->ipv4_tos
;
7941 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7942 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7946 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7947 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7950 struct ovs_key_ipv4 key
, mask
, orig_mask
, base
;
7951 struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr
[] =
7952 OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR
;
7954 /* Check that nw_proto and nw_frag remain unchanged. */
7955 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7956 flow
->nw_frag
== base_flow
->nw_frag
);
7958 get_ipv4_key(flow
, &key
, false);
7959 get_ipv4_key(base_flow
, &base
, false);
7960 get_ipv4_key(&wc
->masks
, &mask
, true);
7961 memcpy(&orig_mask
, &mask
, sizeof mask
);
7962 mask
.ipv4_proto
= 0; /* Not writeable. */
7963 mask
.ipv4_frag
= 0; /* Not writable. */
7965 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7966 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7967 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7970 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7971 ovs_key_ipv4_offsetof_sizeof_arr
, odp_actions
)) {
7972 put_ipv4_key(&base
, base_flow
, false);
7973 or_masks(&mask
, &orig_mask
, ovs_key_ipv4_offsetof_sizeof_arr
);
7974 put_ipv4_key(&mask
, &wc
->masks
, true);
7979 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7981 ipv6
->ipv6_src
= flow
->ipv6_src
;
7982 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7983 ipv6
->ipv6_label
= flow
->ipv6_label
;
7984 ipv6
->ipv6_proto
= flow
->nw_proto
;
7985 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7986 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7987 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7991 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7993 flow
->ipv6_src
= ipv6
->ipv6_src
;
7994 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7995 flow
->ipv6_label
= ipv6
->ipv6_label
;
7996 flow
->nw_proto
= ipv6
->ipv6_proto
;
7997 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7998 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7999 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
8003 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
8004 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8007 struct ovs_key_ipv6 key
, mask
, orig_mask
, base
;
8008 struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr
[] =
8009 OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR
;
8011 /* Check that nw_proto and nw_frag remain unchanged. */
8012 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
8013 flow
->nw_frag
== base_flow
->nw_frag
);
8015 get_ipv6_key(flow
, &key
, false);
8016 get_ipv6_key(base_flow
, &base
, false);
8017 get_ipv6_key(&wc
->masks
, &mask
, true);
8018 memcpy(&orig_mask
, &mask
, sizeof mask
);
8019 mask
.ipv6_proto
= 0; /* Not writeable. */
8020 mask
.ipv6_frag
= 0; /* Not writable. */
8021 mask
.ipv6_label
&= htonl(IPV6_LABEL_MASK
); /* Not writable. */
8023 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
8024 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
8025 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
8028 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8029 ovs_key_ipv6_offsetof_sizeof_arr
, odp_actions
)) {
8030 put_ipv6_key(&base
, base_flow
, false);
8031 or_masks(&mask
, &orig_mask
, ovs_key_ipv6_offsetof_sizeof_arr
);
8032 put_ipv6_key(&mask
, &wc
->masks
, true);
8037 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
8039 /* ARP key has padding, clear it. */
8040 memset(arp
, 0, sizeof *arp
);
8042 arp
->arp_sip
= flow
->nw_src
;
8043 arp
->arp_tip
= flow
->nw_dst
;
8044 arp
->arp_op
= flow
->nw_proto
== UINT8_MAX
?
8045 OVS_BE16_MAX
: htons(flow
->nw_proto
);
8046 arp
->arp_sha
= flow
->arp_sha
;
8047 arp
->arp_tha
= flow
->arp_tha
;
8051 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
8053 flow
->nw_src
= arp
->arp_sip
;
8054 flow
->nw_dst
= arp
->arp_tip
;
8055 flow
->nw_proto
= ntohs(arp
->arp_op
);
8056 flow
->arp_sha
= arp
->arp_sha
;
8057 flow
->arp_tha
= arp
->arp_tha
;
8060 static enum slow_path_reason
8061 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
8062 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
8064 struct ovs_key_arp key
, mask
, orig_mask
, base
;
8065 struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr
[] =
8066 OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR
;
8068 get_arp_key(flow
, &key
);
8069 get_arp_key(base_flow
, &base
);
8070 get_arp_key(&wc
->masks
, &mask
);
8071 memcpy(&orig_mask
, &mask
, sizeof mask
);
8073 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
8074 ovs_key_arp_offsetof_sizeof_arr
, odp_actions
)) {
8075 put_arp_key(&base
, base_flow
);
8076 or_masks(&mask
, &orig_mask
, ovs_key_arp_offsetof_sizeof_arr
);
8077 put_arp_key(&mask
, &wc
->masks
);
8084 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
8086 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8087 icmp
->icmp_type
= ntohs(flow
->tp_src
);
8088 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
8092 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
8094 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8095 flow
->tp_src
= htons(icmp
->icmp_type
);
8096 flow
->tp_dst
= htons(icmp
->icmp_code
);
8099 static enum slow_path_reason
8100 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
8101 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
8103 struct ovs_key_icmp key
, mask
, orig_mask
, base
;
8104 struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr
[] =
8105 OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR
;
8106 enum ovs_key_attr attr
;
8108 if (is_icmpv4(flow
, NULL
)) {
8109 attr
= OVS_KEY_ATTR_ICMP
;
8110 } else if (is_icmpv6(flow
, NULL
)) {
8111 attr
= OVS_KEY_ATTR_ICMPV6
;
8116 get_icmp_key(flow
, &key
);
8117 get_icmp_key(base_flow
, &base
);
8118 get_icmp_key(&wc
->masks
, &mask
);
8119 memcpy(&orig_mask
, &mask
, sizeof mask
);
8121 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
,
8122 ovs_key_icmp_offsetof_sizeof_arr
, odp_actions
)) {
8123 put_icmp_key(&base
, base_flow
);
8124 or_masks(&mask
, &orig_mask
, ovs_key_icmp_offsetof_sizeof_arr
);
8125 put_icmp_key(&mask
, &wc
->masks
);
8132 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
8134 nd
->nd_target
= flow
->nd_target
;
8135 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8136 nd
->nd_sll
= flow
->arp_sha
;
8137 nd
->nd_tll
= flow
->arp_tha
;
8141 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
8143 flow
->nd_target
= nd
->nd_target
;
8144 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8145 flow
->arp_sha
= nd
->nd_sll
;
8146 flow
->arp_tha
= nd
->nd_tll
;
8150 get_nd_extensions_key(const struct flow
*flow
,
8151 struct ovs_key_nd_extensions
*nd_ext
)
8153 /* ND Extensions key has padding, clear it. */
8154 memset(nd_ext
, 0, sizeof *nd_ext
);
8155 nd_ext
->nd_reserved
= flow
->igmp_group_ip4
;
8156 nd_ext
->nd_options_type
= ntohs(flow
->tcp_flags
);
8160 put_nd_extensions_key(const struct ovs_key_nd_extensions
*nd_ext
,
8163 flow
->igmp_group_ip4
= nd_ext
->nd_reserved
;
8164 flow
->tcp_flags
= htons(nd_ext
->nd_options_type
);
8167 static enum slow_path_reason
8168 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
8169 struct ofpbuf
*odp_actions
,
8170 struct flow_wildcards
*wc
, bool use_masked
)
8172 struct ovs_key_nd key
, mask
, orig_mask
, base
;
8173 struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr
[] =
8174 OVS_KEY_ND_OFFSETOF_SIZEOF_ARR
;
8176 get_nd_key(flow
, &key
);
8177 get_nd_key(base_flow
, &base
);
8178 get_nd_key(&wc
->masks
, &mask
);
8179 memcpy(&orig_mask
, &mask
, sizeof mask
);
8181 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8182 ovs_key_nd_offsetof_sizeof_arr
, odp_actions
)) {
8183 put_nd_key(&base
, base_flow
);
8184 or_masks(&mask
, &orig_mask
, ovs_key_nd_offsetof_sizeof_arr
);
8185 put_nd_key(&mask
, &wc
->masks
);
8192 static enum slow_path_reason
8193 commit_set_nd_extensions_action(const struct flow
*flow
,
8194 struct flow
*base_flow
,
8195 struct ofpbuf
*odp_actions
,
8196 struct flow_wildcards
*wc
, bool use_masked
)
8198 struct ovs_key_nd_extensions key
, mask
, orig_mask
, base
;
8199 struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr
[] =
8200 OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR
;
8202 get_nd_extensions_key(flow
, &key
);
8203 get_nd_extensions_key(base_flow
, &base
);
8204 get_nd_extensions_key(&wc
->masks
, &mask
);
8205 memcpy(&orig_mask
, &mask
, sizeof mask
);
8207 if (commit(OVS_KEY_ATTR_ND_EXTENSIONS
, use_masked
, &key
, &base
, &mask
,
8208 sizeof key
, ovs_key_nd_extensions_offsetof_sizeof_arr
,
8210 put_nd_extensions_key(&base
, base_flow
);
8211 or_masks(&mask
, &orig_mask
, ovs_key_nd_extensions_offsetof_sizeof_arr
);
8212 put_nd_extensions_key(&mask
, &wc
->masks
);
8218 static enum slow_path_reason
8219 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
8220 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8225 /* Check if 'flow' really has an L3 header. */
8226 if (!flow
->nw_proto
) {
8230 switch (ntohs(base
->dl_type
)) {
8232 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
8236 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
8237 if (base
->nw_proto
== IPPROTO_ICMPV6
) {
8238 /* Commit extended attrs first to make sure
8239 correct options are added.*/
8240 reason
= commit_set_nd_extensions_action(flow
, base
,
8241 odp_actions
, wc
, use_masked
);
8242 reason
|= commit_set_nd_action(flow
, base
, odp_actions
,
8249 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
8256 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
8260 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
8261 memset(nsh
->context
, 0, sizeof(nsh
->context
));
8267 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
8268 bool is_mask OVS_UNUSED
)
8271 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
8272 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
8277 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
8278 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
8279 struct ovs_key_nsh
*mask
, size_t size
,
8280 struct ofpbuf
*odp_actions
)
8282 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
8284 if (memcmp(key
, base
, size
) == 0) {
8285 /* Mask bits are set when we have either read or set the corresponding
8286 * values. Masked bits will be exact-matched, no need to set them
8287 * if the value did not actually change. */
8291 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
8293 if (use_masked_set
&& !fully_masked
) {
8295 struct ovs_nsh_key_base nsh_base
;
8296 struct ovs_nsh_key_base nsh_base_mask
;
8297 struct ovs_nsh_key_md1 md1
;
8298 struct ovs_nsh_key_md1 md1_mask
;
8299 size_t offset
= nl_msg_start_nested(odp_actions
,
8300 OVS_ACTION_ATTR_SET_MASKED
);
8302 nsh_base
.flags
= key
->flags
;
8303 nsh_base
.ttl
= key
->ttl
;
8304 nsh_base
.mdtype
= key
->mdtype
;
8305 nsh_base
.np
= key
->np
;
8306 nsh_base
.path_hdr
= key
->path_hdr
;
8308 nsh_base_mask
.flags
= mask
->flags
;
8309 nsh_base_mask
.ttl
= mask
->ttl
;
8310 nsh_base_mask
.mdtype
= mask
->mdtype
;
8311 nsh_base_mask
.np
= mask
->np
;
8312 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
8314 /* OVS_KEY_ATTR_NSH keys */
8315 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
8317 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
8318 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
8319 OVS_NSH_KEY_ATTR_BASE
,
8320 2 * sizeof(nsh_base
));
8321 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
8322 size_t lkey_size
= sizeof(nsh_base
);
8324 while (lkey_size
--) {
8325 *data
++ = *lkey
++ & *lmask
++;
8327 lmask
= (char *)&nsh_base_mask
;
8328 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
8330 switch (key
->mdtype
) {
8332 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
8333 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
8335 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
8336 data
= nl_msg_put_unspec_uninit(odp_actions
,
8337 OVS_NSH_KEY_ATTR_MD1
,
8339 lkey
= (char *)&md1
;
8340 lmask
= (char *)&md1_mask
;
8341 lkey_size
= sizeof(md1
);
8343 while (lkey_size
--) {
8344 *data
++ = *lkey
++ & *lmask
++;
8346 lmask
= (char *)&md1_mask
;
8347 memcpy(data
, lmask
, sizeof(md1_mask
));
8351 /* No match support for other MD formats yet. */
8355 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
8357 nl_msg_end_nested(odp_actions
, offset
);
8359 if (!fully_masked
) {
8360 memset(mask
, 0xff, size
);
8362 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
8363 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
8364 nl_msg_end_nested(odp_actions
, offset
);
8366 memcpy(base
, key
, size
);
8371 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
8372 struct ofpbuf
*odp_actions
,
8373 struct flow_wildcards
*wc
,
8376 struct ovs_key_nsh key
, mask
, base
;
8378 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
8379 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
8383 /* Check that mdtype and np remain unchanged. */
8384 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
8385 flow
->nsh
.np
== base_flow
->nsh
.np
);
8387 get_nsh_key(flow
, &key
, false);
8388 get_nsh_key(base_flow
, &base
, false);
8389 get_nsh_key(&wc
->masks
, &mask
, true);
8390 mask
.mdtype
= 0; /* Not writable. */
8391 mask
.np
= 0; /* Not writable. */
8393 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
8394 sizeof key
, odp_actions
)) {
8395 put_nsh_key(&base
, base_flow
, false);
8396 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
8397 put_nsh_key(&mask
, &wc
->masks
, true);
8402 /* TCP, UDP, and SCTP keys have the same layout. */
8403 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
8404 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
8407 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
8409 tp
->tcp
.tcp_src
= flow
->tp_src
;
8410 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
8414 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
8416 flow
->tp_src
= tp
->tcp
.tcp_src
;
8417 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
8421 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
8422 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8425 enum ovs_key_attr key_type
;
8426 union ovs_key_tp key
, mask
, orig_mask
, base
;
8427 struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr
[] =
8428 OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR
;
8430 /* Check if 'flow' really has an L3 header. */
8431 if (!flow
->nw_proto
) {
8435 if (!is_ip_any(base_flow
)) {
8439 if (flow
->nw_proto
== IPPROTO_TCP
) {
8440 key_type
= OVS_KEY_ATTR_TCP
;
8441 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
8442 key_type
= OVS_KEY_ATTR_UDP
;
8443 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
8444 key_type
= OVS_KEY_ATTR_SCTP
;
8449 get_tp_key(flow
, &key
);
8450 get_tp_key(base_flow
, &base
);
8451 get_tp_key(&wc
->masks
, &mask
);
8452 memcpy(&orig_mask
, &mask
, sizeof mask
);
8454 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8455 ovs_key_tp_offsetof_sizeof_arr
, odp_actions
)) {
8456 put_tp_key(&base
, base_flow
);
8457 or_masks(&mask
, &orig_mask
, ovs_key_tp_offsetof_sizeof_arr
);
8458 put_tp_key(&mask
, &wc
->masks
);
8463 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
8464 struct ofpbuf
*odp_actions
,
8465 struct flow_wildcards
*wc
,
8468 uint32_t key
, mask
, base
;
8469 struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr
[] = {
8470 {0, sizeof(uint32_t)},
8474 key
= flow
->skb_priority
;
8475 base
= base_flow
->skb_priority
;
8476 mask
= wc
->masks
.skb_priority
;
8478 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
8479 sizeof key
, ovs_key_prio_offsetof_sizeof_arr
, odp_actions
)) {
8480 base_flow
->skb_priority
= base
;
8481 wc
->masks
.skb_priority
|= mask
;
8486 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
8487 struct ofpbuf
*odp_actions
,
8488 struct flow_wildcards
*wc
,
8491 uint32_t key
, mask
, base
;
8492 struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr
[] = {
8493 {0, sizeof(uint32_t)},
8497 key
= flow
->pkt_mark
;
8498 base
= base_flow
->pkt_mark
;
8499 mask
= wc
->masks
.pkt_mark
;
8501 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
8502 sizeof key
, ovs_key_pkt_mark_offsetof_sizeof_arr
,
8504 base_flow
->pkt_mark
= base
;
8505 wc
->masks
.pkt_mark
|= mask
;
8510 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
8512 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
8516 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
8517 const struct flow
*flow
,
8518 struct ofpbuf
*encap_data
)
8520 uint8_t * metadata
= NULL
;
8521 uint8_t md_size
= 0;
8523 switch (flow
->nsh
.mdtype
) {
8526 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
8527 metadata
= encap_data
->data
;
8528 md_size
= encap_data
->size
;
8537 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
8538 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
8539 nl_msg_end_nested(odp_actions
, offset
);
8543 commit_encap_decap_action(const struct flow
*flow
,
8544 struct flow
*base_flow
,
8545 struct ofpbuf
*odp_actions
,
8546 struct flow_wildcards
*wc
,
8547 bool pending_encap
, bool pending_decap
,
8548 struct ofpbuf
*encap_data
)
8550 if (pending_encap
) {
8551 switch (ntohl(flow
->packet_type
)) {
8554 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
8556 base_flow
->packet_type
= flow
->packet_type
;
8557 base_flow
->dl_src
= flow
->dl_src
;
8558 base_flow
->dl_dst
= flow
->dl_dst
;
8563 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
8564 base_flow
->packet_type
= flow
->packet_type
;
8565 /* Update all packet headers in base_flow. */
8566 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
8567 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
8570 /* Only the above protocols are supported for encap.
8571 * The check is done at action translation. */
8574 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
8575 /* This is an explicit or implicit decap case. */
8576 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
8577 base_flow
->packet_type
== htonl(PT_ETH
)) {
8578 /* Generate pop_eth and continue without recirculation. */
8579 odp_put_pop_eth_action(odp_actions
);
8580 base_flow
->packet_type
= flow
->packet_type
;
8581 base_flow
->dl_src
= eth_addr_zero
;
8582 base_flow
->dl_dst
= eth_addr_zero
;
8584 /* All other decap cases require recirculation.
8585 * No need to update the base flow here. */
8586 switch (ntohl(base_flow
->packet_type
)) {
8589 odp_put_pop_nsh_action(odp_actions
);
8592 /* Checks are done during translation. */
8598 wc
->masks
.packet_type
= OVS_BE32_MAX
;
8601 /* If any of the flow key data that ODP actions can modify are different in
8602 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8603 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
8604 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
8605 * in addition to this function if needed. Sets fields in 'wc' that are
8606 * used as part of the action.
8608 * In the common case, this function returns 0. If the flow key modification
8609 * requires the flow's packets to be forced into the userspace slow path, this
8610 * function returns SLOW_ACTION. This only happens when there is no ODP action
8611 * to modify some field that was actually modified. For example, there is no
8612 * ODP action to modify any ARP field, so such a modification triggers
8613 * SLOW_ACTION. (When this happens, packets that need such modification get
8614 * flushed to userspace and handled there, which works OK but much more slowly
8615 * than if the datapath handled it directly.) */
8616 enum slow_path_reason
8617 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
8618 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8619 bool use_masked
, bool pending_encap
, bool pending_decap
,
8620 struct ofpbuf
*encap_data
)
8622 /* If you add a field that OpenFlow actions can change, and that is visible
8623 * to the datapath (including all data fields), then you should also add
8624 * code here to commit changes to the field. */
8625 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
8627 enum slow_path_reason slow1
, slow2
;
8628 bool mpls_done
= false;
8630 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
8631 pending_encap
, pending_decap
, encap_data
);
8632 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
8633 /* Make packet a non-MPLS packet before committing L3/4 actions,
8634 * which would otherwise do nothing. */
8635 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
8636 commit_mpls_action(flow
, base
, odp_actions
);
8639 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
8640 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
8641 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
8642 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
8644 commit_mpls_action(flow
, base
, odp_actions
);
8646 commit_vlan_action(flow
, base
, odp_actions
, wc
);
8647 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
8648 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
8650 return slow1
? slow1
: slow2
;