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_TRUNC
: return sizeof(struct ovs_action_trunc
);
123 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
124 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
125 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
126 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
127 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
128 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
129 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
130 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
131 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
132 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
133 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
134 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
135 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
136 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
137 case OVS_ACTION_ATTR_CT_CLEAR
: return 0;
138 case OVS_ACTION_ATTR_PUSH_ETH
: return sizeof(struct ovs_action_push_eth
);
139 case OVS_ACTION_ATTR_POP_ETH
: return 0;
140 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
141 case OVS_ACTION_ATTR_PUSH_NSH
: return ATTR_LEN_VARIABLE
;
142 case OVS_ACTION_ATTR_POP_NSH
: return 0;
143 case OVS_ACTION_ATTR_CHECK_PKT_LEN
: return ATTR_LEN_VARIABLE
;
144 case OVS_ACTION_ATTR_DROP
: return sizeof(uint32_t);
146 case OVS_ACTION_ATTR_UNSPEC
:
147 case __OVS_ACTION_ATTR_MAX
:
148 return ATTR_LEN_INVALID
;
151 return ATTR_LEN_INVALID
;
154 /* Returns a string form of 'attr'. The return value is either a statically
155 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
156 * should be at least OVS_KEY_ATTR_BUFSIZE. */
157 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
159 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
162 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
163 case OVS_KEY_ATTR_ENCAP
: return "encap";
164 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
165 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
166 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
167 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
168 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
169 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
170 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: return "ct_tuple4";
171 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: return "ct_tuple6";
172 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
173 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
174 case OVS_KEY_ATTR_ETHERNET
: return "eth";
175 case OVS_KEY_ATTR_VLAN
: return "vlan";
176 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
177 case OVS_KEY_ATTR_IPV4
: return "ipv4";
178 case OVS_KEY_ATTR_IPV6
: return "ipv6";
179 case OVS_KEY_ATTR_TCP
: return "tcp";
180 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
181 case OVS_KEY_ATTR_UDP
: return "udp";
182 case OVS_KEY_ATTR_SCTP
: return "sctp";
183 case OVS_KEY_ATTR_ICMP
: return "icmp";
184 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
185 case OVS_KEY_ATTR_ARP
: return "arp";
186 case OVS_KEY_ATTR_ND
: return "nd";
187 case OVS_KEY_ATTR_ND_EXTENSIONS
: return "nd_ext";
188 case OVS_KEY_ATTR_MPLS
: return "mpls";
189 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
190 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
191 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
192 case OVS_KEY_ATTR_NSH
: return "nsh";
194 case __OVS_KEY_ATTR_MAX
:
196 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
202 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
204 size_t len
= nl_attr_get_size(a
);
206 ds_put_format(ds
, "action%d", nl_attr_type(a
));
208 const uint8_t *unspec
;
211 unspec
= nl_attr_get(a
);
212 for (i
= 0; i
< len
; i
++) {
213 ds_put_char(ds
, i
? ' ': '(');
214 ds_put_format(ds
, "%02x", unspec
[i
]);
216 ds_put_char(ds
, ')');
221 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
222 const struct hmap
*portno_names
)
224 static const struct nl_policy ovs_sample_policy
[] = {
225 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
226 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
228 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
230 const struct nlattr
*nla_acts
;
233 ds_put_cstr(ds
, "sample");
235 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
236 ds_put_cstr(ds
, "(error)");
240 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
243 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
245 ds_put_cstr(ds
, "actions(");
246 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
247 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
248 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
249 ds_put_format(ds
, "))");
253 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
254 const struct hmap
*portno_names
)
256 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
257 int len
= nl_attr_get_size(attr
);
259 ds_put_cstr(ds
, "clone");
260 ds_put_format(ds
, "(");
261 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
262 ds_put_format(ds
, ")");
266 format_nsh_key(struct ds
*ds
, const struct ovs_key_nsh
*key
)
268 ds_put_format(ds
, "flags=%d", key
->flags
);
269 ds_put_format(ds
, ",ttl=%d", key
->ttl
);
270 ds_put_format(ds
, ",mdtype=%d", key
->mdtype
);
271 ds_put_format(ds
, ",np=%d", key
->np
);
272 ds_put_format(ds
, ",spi=0x%x",
273 nsh_path_hdr_to_spi_uint32(key
->path_hdr
));
274 ds_put_format(ds
, ",si=%d",
275 nsh_path_hdr_to_si(key
->path_hdr
));
277 switch (key
->mdtype
) {
279 for (int i
= 0; i
< 4; i
++) {
280 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->context
[i
]));
285 /* No support for matching other metadata formats yet. */
291 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
292 uint8_t value
, uint8_t mask
)
298 ds_put_format(s
, "%s=", name
);
299 if (mask
== UINT8_MAX
) {
300 ds_put_format(s
, "%"PRIu8
, value
);
302 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
309 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
310 ovs_be32 value
, ovs_be32 mask
)
312 if (mask
!= htonl(0)) {
316 ds_put_format(s
, "%s=", name
);
317 if (mask
== OVS_BE32_MAX
) {
318 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
320 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
321 ntohl(value
), ntohl(mask
));
328 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
329 const struct ovs_key_nsh
*mask
)
332 format_nsh_key(ds
, key
);
335 uint32_t spi
= nsh_path_hdr_to_spi_uint32(key
->path_hdr
);
336 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
->path_hdr
);
337 if (spi_mask
== (NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
338 spi_mask
= UINT32_MAX
;
340 uint8_t si
= nsh_path_hdr_to_si(key
->path_hdr
);
341 uint8_t si_mask
= nsh_path_hdr_to_si(mask
->path_hdr
);
343 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
344 format_uint8_masked(ds
, &first
, "ttl", key
->ttl
, mask
->ttl
);
345 format_uint8_masked(ds
, &first
, "mdtype", key
->mdtype
, mask
->mdtype
);
346 format_uint8_masked(ds
, &first
, "np", key
->np
, mask
->np
);
347 format_be32_masked(ds
, &first
, "spi", htonl(spi
), htonl(spi_mask
));
348 format_uint8_masked(ds
, &first
, "si", si
, si_mask
);
349 format_be32_masked(ds
, &first
, "c1", key
->context
[0],
351 format_be32_masked(ds
, &first
, "c2", key
->context
[1],
353 format_be32_masked(ds
, &first
, "c3", key
->context
[2],
355 format_be32_masked(ds
, &first
, "c4", key
->context
[3],
361 format_odp_push_nsh_action(struct ds
*ds
,
362 const struct nsh_hdr
*nsh_hdr
)
364 size_t mdlen
= nsh_hdr_len(nsh_hdr
) - NSH_BASE_HDR_LEN
;
365 uint32_t spi
= ntohl(nsh_get_spi(nsh_hdr
));
366 uint8_t si
= nsh_get_si(nsh_hdr
);
367 uint8_t flags
= nsh_get_flags(nsh_hdr
);
368 uint8_t ttl
= nsh_get_ttl(nsh_hdr
);
370 ds_put_cstr(ds
, "push_nsh(");
371 ds_put_format(ds
, "flags=%d", flags
);
372 ds_put_format(ds
, ",ttl=%d", ttl
);
373 ds_put_format(ds
, ",mdtype=%d", nsh_hdr
->md_type
);
374 ds_put_format(ds
, ",np=%d", nsh_hdr
->next_proto
);
375 ds_put_format(ds
, ",spi=0x%x", spi
);
376 ds_put_format(ds
, ",si=%d", si
);
377 switch (nsh_hdr
->md_type
) {
379 const struct nsh_md1_ctx
*md1_ctx
= &nsh_hdr
->md1
;
380 for (int i
= 0; i
< 4; i
++) {
381 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
382 ntohl(get_16aligned_be32(&md1_ctx
->context
[i
])));
387 const struct nsh_md2_tlv
*md2_ctx
= &nsh_hdr
->md2
;
388 ds_put_cstr(ds
, ",md2=");
389 ds_put_hex(ds
, md2_ctx
, mdlen
);
395 ds_put_format(ds
, ")");
399 slow_path_reason_to_string(uint32_t reason
)
401 switch ((enum slow_path_reason
) reason
) {
402 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
411 slow_path_reason_to_explanation(enum slow_path_reason reason
)
414 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
423 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
424 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
426 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
427 res_flags
, allowed
, res_mask
);
431 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
432 const struct hmap
*portno_names
)
434 static const struct nl_policy ovs_userspace_policy
[] = {
435 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
436 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
438 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
440 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
443 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
444 const struct nlattr
*userdata_attr
;
445 const struct nlattr
*tunnel_out_port_attr
;
447 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
448 ds_put_cstr(ds
, "userspace(error)");
452 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
453 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
455 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
458 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
459 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
460 bool userdata_unspec
= true;
461 struct user_action_cookie cookie
;
463 if (userdata_len
== sizeof cookie
) {
464 memcpy(&cookie
, userdata
, sizeof cookie
);
466 userdata_unspec
= false;
468 if (cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
469 ds_put_format(ds
, ",sFlow("
470 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
471 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
472 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
473 cookie
.sflow
.output
);
474 } else if (cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
475 ds_put_cstr(ds
, ",slow_path(");
476 format_flags(ds
, slow_path_reason_to_string
,
477 cookie
.slow_path
.reason
, ',');
478 ds_put_format(ds
, ")");
479 } else if (cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
480 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
481 ",collector_set_id=%"PRIu32
482 ",obs_domain_id=%"PRIu32
483 ",obs_point_id=%"PRIu32
485 cookie
.flow_sample
.probability
,
486 cookie
.flow_sample
.collector_set_id
,
487 cookie
.flow_sample
.obs_domain_id
,
488 cookie
.flow_sample
.obs_point_id
);
489 odp_portno_name_format(portno_names
,
490 cookie
.flow_sample
.output_odp_port
, ds
);
491 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
492 ds_put_cstr(ds
, ",ingress");
493 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
494 ds_put_cstr(ds
, ",egress");
496 ds_put_char(ds
, ')');
497 } else if (cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
498 ds_put_format(ds
, ",ipfix(output_port=");
499 odp_portno_name_format(portno_names
,
500 cookie
.ipfix
.output_odp_port
, ds
);
501 ds_put_char(ds
, ')');
502 } else if (cookie
.type
== USER_ACTION_COOKIE_CONTROLLER
) {
503 ds_put_format(ds
, ",controller(reason=%"PRIu16
507 ",rule_cookie=%#"PRIx64
508 ",controller_id=%"PRIu16
510 cookie
.controller
.reason
,
511 !!cookie
.controller
.dont_send
,
512 !!cookie
.controller
.continuation
,
513 cookie
.controller
.recirc_id
,
514 ntohll(get_32aligned_be64(
515 &cookie
.controller
.rule_cookie
)),
516 cookie
.controller
.controller_id
,
517 cookie
.controller
.max_len
);
518 ds_put_char(ds
, ')');
520 userdata_unspec
= true;
524 if (userdata_unspec
) {
526 ds_put_format(ds
, ",userdata(");
527 for (i
= 0; i
< userdata_len
; i
++) {
528 ds_put_format(ds
, "%02x", userdata
[i
]);
530 ds_put_char(ds
, ')');
534 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
535 ds_put_cstr(ds
, ",actions");
538 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
539 if (tunnel_out_port_attr
) {
540 ds_put_format(ds
, ",tunnel_out_port=");
541 odp_portno_name_format(portno_names
,
542 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
545 ds_put_char(ds
, ')');
549 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
551 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
552 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
553 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
554 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
556 ds_put_char(ds
, ',');
558 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
559 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
560 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
561 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
563 ds_put_char(ds
, ',');
565 if (!(tci
& htons(VLAN_CFI
))) {
566 ds_put_cstr(ds
, "cfi=0");
567 ds_put_char(ds
, ',');
573 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
575 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
576 mpls_lse_to_label(mpls_lse
),
577 mpls_lse_to_tc(mpls_lse
),
578 mpls_lse_to_ttl(mpls_lse
),
579 mpls_lse_to_bos(mpls_lse
));
583 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
584 const struct ovs_key_mpls
*mpls_mask
, int n
)
586 for (int i
= 0; i
< n
; i
++) {
587 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
589 if (mpls_mask
== NULL
) {
590 format_mpls_lse(ds
, key
);
592 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
594 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
595 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
596 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
597 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
598 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
600 ds_put_char(ds
, ',');
606 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
608 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
612 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
614 ds_put_format(ds
, "hash(");
616 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
617 ds_put_format(ds
, "l4(%"PRIu32
")", hash_act
->hash_basis
);
618 } else if (hash_act
->hash_alg
== OVS_HASH_ALG_SYM_L4
) {
619 ds_put_format(ds
, "sym_l4(%"PRIu32
")", hash_act
->hash_basis
);
621 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
624 ds_put_format(ds
, ")");
628 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
630 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
631 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
632 ntohs(udp
->udp_csum
));
638 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
640 const struct eth_header
*eth
;
643 const struct udp_header
*udp
;
645 eth
= (const struct eth_header
*)data
->header
;
650 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
651 data
->header_len
, data
->tnl_type
);
652 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
653 ds_put_format(ds
, ",src=");
654 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
655 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
657 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
659 const struct ip_header
*ip
= l3
;
660 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
661 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
662 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
663 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
664 ip
->ip_proto
, ip
->ip_tos
,
666 ntohs(ip
->ip_frag_off
));
669 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
670 struct in6_addr src
, dst
;
671 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
672 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
673 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
675 ds_put_format(ds
, "ipv6(src=");
676 ipv6_format_addr(&src
, ds
);
677 ds_put_format(ds
, ",dst=");
678 ipv6_format_addr(&dst
, ds
);
679 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
680 ",hlimit=%"PRIu8
"),",
681 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
682 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
686 udp
= (const struct udp_header
*) l4
;
688 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
689 const struct vxlanhdr
*vxh
;
691 vxh
= format_udp_tnl_push_header(ds
, udp
);
693 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
694 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
695 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
696 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
697 const struct genevehdr
*gnh
;
699 gnh
= format_udp_tnl_push_header(ds
, udp
);
701 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
702 gnh
->oam
? "oam," : "",
703 gnh
->critical
? "crit," : "",
704 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
707 ds_put_cstr(ds
, ",options(");
708 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
710 ds_put_char(ds
, ')');
713 ds_put_char(ds
, ')');
714 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
715 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
716 const struct gre_base_hdr
*greh
;
717 ovs_16aligned_be32
*options
;
719 greh
= (const struct gre_base_hdr
*) l4
;
721 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
722 ntohs(greh
->flags
), ntohs(greh
->protocol
));
723 options
= (ovs_16aligned_be32
*)(greh
+ 1);
724 if (greh
->flags
& htons(GRE_CSUM
)) {
725 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
728 if (greh
->flags
& htons(GRE_KEY
)) {
729 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
732 if (greh
->flags
& htons(GRE_SEQ
)) {
733 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
736 ds_put_format(ds
, ")");
737 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
738 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
739 const struct gre_base_hdr
*greh
;
740 const struct erspan_base_hdr
*ersh
;
742 greh
= (const struct gre_base_hdr
*) l4
;
743 ersh
= ERSPAN_HDR(greh
);
745 if (ersh
->ver
== 1) {
746 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
748 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
749 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
750 } else if (ersh
->ver
== 2) {
751 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
753 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
754 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
755 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
757 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
759 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GTPU
) {
760 const struct gtpuhdr
*gtph
;
762 gtph
= format_udp_tnl_push_header(ds
, udp
);
764 ds_put_format(ds
, "gtpu(flags=0x%"PRIx8
765 ",msgtype=%"PRIu8
",teid=0x%"PRIx32
")",
766 gtph
->md
.flags
, gtph
->md
.msgtype
,
767 ntohl(get_16aligned_be32(>ph
->teid
)));
770 ds_put_format(ds
, ")");
774 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
775 const struct hmap
*portno_names
)
777 struct ovs_action_push_tnl
*data
;
779 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
781 ds_put_cstr(ds
, "tnl_push(tnl_port(");
782 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
783 ds_put_cstr(ds
, "),");
784 format_odp_tnl_push_header(ds
, data
);
785 ds_put_format(ds
, ",out_port(");
786 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
787 ds_put_cstr(ds
, "))");
790 static const struct nl_policy ovs_nat_policy
[] = {
791 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
792 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
793 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
794 .min_len
= sizeof(struct in_addr
),
795 .max_len
= sizeof(struct in6_addr
)},
796 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
797 .min_len
= sizeof(struct in_addr
),
798 .max_len
= sizeof(struct in6_addr
)},
799 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
800 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
801 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
802 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
803 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
807 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
809 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
811 ovs_be32 ip_min
, ip_max
;
812 struct in6_addr ip6_min
, ip6_max
;
813 uint16_t proto_min
, proto_max
;
815 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
816 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
819 /* If no type, then nothing else either. */
820 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
821 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
822 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
823 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
824 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
825 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
828 /* Both SNAT & DNAT may not be specified. */
829 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
830 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
833 /* proto may not appear without ip. */
834 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
835 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
838 /* MAX may not appear without MIN. */
839 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
840 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
841 ds_put_cstr(ds
, "nat(error: range max without min.)");
844 /* Address sizes must match. */
845 if ((a
[OVS_NAT_ATTR_IP_MIN
]
846 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
847 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
848 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
849 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
850 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
851 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
855 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
856 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
857 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
858 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
859 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
860 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
861 if (addr_len
== sizeof ip6_min
) {
862 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
863 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
865 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
866 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
869 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
870 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
871 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
872 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
874 if ((addr_len
== sizeof(ovs_be32
)
875 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
876 || (addr_len
== sizeof(struct in6_addr
)
877 && !ipv6_mask_is_any(&ip6_max
)
878 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
879 || (proto_max
&& proto_min
> proto_max
)) {
880 ds_put_cstr(ds
, "nat(range error)");
884 ds_put_cstr(ds
, "nat");
885 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
886 ds_put_char(ds
, '(');
887 if (a
[OVS_NAT_ATTR_SRC
]) {
888 ds_put_cstr(ds
, "src");
889 } else if (a
[OVS_NAT_ATTR_DST
]) {
890 ds_put_cstr(ds
, "dst");
894 ds_put_cstr(ds
, "=");
896 if (addr_len
== sizeof ip_min
) {
897 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
899 if (ip_max
&& ip_max
!= ip_min
) {
900 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
902 } else if (addr_len
== sizeof ip6_min
) {
903 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
905 if (!ipv6_mask_is_any(&ip6_max
) &&
906 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
907 ds_put_char(ds
, '-');
908 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
912 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
914 if (proto_max
&& proto_max
!= proto_min
) {
915 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
919 ds_put_char(ds
, ',');
920 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
921 ds_put_cstr(ds
, "persistent,");
923 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
924 ds_put_cstr(ds
, "hash,");
926 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
927 ds_put_cstr(ds
, "random,");
930 ds_put_char(ds
, ')');
934 static const struct nl_policy ovs_conntrack_policy
[] = {
935 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
936 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
937 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
938 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
939 .min_len
= sizeof(uint32_t) * 2 },
940 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
941 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
942 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
943 .min_len
= 1, .max_len
= 16 },
944 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
945 [OVS_CT_ATTR_TIMEOUT
] = { .type
= NL_A_STRING
, .optional
= true,
946 .min_len
= 1, .max_len
= 32 },
950 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
952 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
954 ovs_32aligned_u128 value
;
955 ovs_32aligned_u128 mask
;
957 const uint32_t *mark
;
958 const char *helper
, *timeout
;
961 const struct nlattr
*nat
;
963 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
964 ds_put_cstr(ds
, "ct(error)");
968 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
969 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
970 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
971 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
972 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
973 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
974 timeout
= a
[OVS_CT_ATTR_TIMEOUT
] ?
975 nl_attr_get(a
[OVS_CT_ATTR_TIMEOUT
]) : NULL
;
976 nat
= a
[OVS_CT_ATTR_NAT
];
978 ds_put_format(ds
, "ct");
979 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| timeout
|| nat
) {
980 ds_put_cstr(ds
, "(");
982 ds_put_format(ds
, "commit,");
985 ds_put_format(ds
, "force_commit,");
988 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
991 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
995 ds_put_format(ds
, "label=");
996 format_u128(ds
, &label
->value
, &label
->mask
, true);
997 ds_put_char(ds
, ',');
1000 ds_put_format(ds
, "helper=%s,", helper
);
1003 ds_put_format(ds
, "timeout=%s", timeout
);
1006 format_odp_ct_nat(ds
, nat
);
1009 ds_put_cstr(ds
, ")");
1013 static const struct attr_len_tbl
1014 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
1015 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
1016 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
1017 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
1021 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
1024 const struct nlattr
*a
;
1025 struct ovs_key_nsh nsh
;
1026 struct ovs_key_nsh nsh_mask
;
1028 memset(&nsh
, 0, sizeof nsh
);
1029 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
1031 NL_NESTED_FOR_EACH (a
, left
, attr
) {
1032 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
1033 size_t len
= nl_attr_get_size(a
);
1035 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1039 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1040 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1045 case OVS_NSH_KEY_ATTR_UNSPEC
:
1047 case OVS_NSH_KEY_ATTR_BASE
: {
1048 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1049 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1050 memcpy(&nsh
, base
, sizeof(*base
));
1051 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1054 case OVS_NSH_KEY_ATTR_MD1
: {
1055 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1056 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1057 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1058 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1061 case OVS_NSH_KEY_ATTR_MD2
:
1062 case __OVS_NSH_KEY_ATTR_MAX
:
1064 /* No support for matching other metadata formats yet. */
1069 ds_put_cstr(ds
, "set(nsh(");
1070 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1071 ds_put_cstr(ds
, "))");
1075 format_odp_check_pkt_len_action(struct ds
*ds
, const struct nlattr
*attr
,
1076 const struct hmap
*portno_names OVS_UNUSED
)
1078 static const struct nl_policy ovs_cpl_policy
[] = {
1079 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] = { .type
= NL_A_U16
},
1080 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] = { .type
= NL_A_NESTED
},
1081 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
]
1082 = { .type
= NL_A_NESTED
},
1084 struct nlattr
*a
[ARRAY_SIZE(ovs_cpl_policy
)];
1085 ds_put_cstr(ds
, "check_pkt_len");
1086 if (!nl_parse_nested(attr
, ovs_cpl_policy
, a
, ARRAY_SIZE(a
))) {
1087 ds_put_cstr(ds
, "(error)");
1091 if (!a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] ||
1092 !a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
]) {
1093 ds_put_cstr(ds
, "(error)");
1097 uint16_t pkt_len
= nl_attr_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]);
1098 ds_put_format(ds
, "(size=%u,gt(", pkt_len
);
1099 const struct nlattr
*acts
;
1100 acts
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
];
1101 format_odp_actions(ds
, nl_attr_get(acts
), nl_attr_get_size(acts
),
1104 ds_put_cstr(ds
, "),le(");
1105 acts
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
];
1106 format_odp_actions(ds
, nl_attr_get(acts
), nl_attr_get_size(acts
),
1108 ds_put_cstr(ds
, "))");
1112 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1113 const struct hmap
*portno_names
)
1116 enum ovs_action_attr type
= nl_attr_type(a
);
1119 expected_len
= odp_action_len(nl_attr_type(a
));
1120 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1121 nl_attr_get_size(a
) != expected_len
) {
1122 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1123 nl_attr_get_size(a
), expected_len
);
1124 format_generic_odp_action(ds
, a
);
1129 case OVS_ACTION_ATTR_METER
:
1130 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1132 case OVS_ACTION_ATTR_OUTPUT
:
1133 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1135 case OVS_ACTION_ATTR_TRUNC
: {
1136 const struct ovs_action_trunc
*trunc
=
1137 nl_attr_get_unspec(a
, sizeof *trunc
);
1139 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1142 case OVS_ACTION_ATTR_TUNNEL_POP
:
1143 ds_put_cstr(ds
, "tnl_pop(");
1144 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1145 ds_put_char(ds
, ')');
1147 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1148 format_odp_tnl_push_action(ds
, a
, portno_names
);
1150 case OVS_ACTION_ATTR_USERSPACE
:
1151 format_odp_userspace_action(ds
, a
, portno_names
);
1153 case OVS_ACTION_ATTR_RECIRC
:
1154 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1156 case OVS_ACTION_ATTR_HASH
:
1157 format_odp_hash_action(ds
, nl_attr_get(a
));
1159 case OVS_ACTION_ATTR_SET_MASKED
:
1161 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1162 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1163 format_odp_set_nsh(ds
, a
);
1166 size
= nl_attr_get_size(a
) / 2;
1167 ds_put_cstr(ds
, "set(");
1169 /* Masked set action not supported for tunnel key, which is bigger. */
1170 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1171 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1172 sizeof(struct nlattr
))];
1173 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1174 sizeof(struct nlattr
))];
1176 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1177 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1178 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1179 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1180 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1182 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1184 ds_put_cstr(ds
, ")");
1186 case OVS_ACTION_ATTR_SET
:
1187 ds_put_cstr(ds
, "set(");
1188 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1189 ds_put_cstr(ds
, ")");
1191 case OVS_ACTION_ATTR_PUSH_ETH
: {
1192 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1193 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1194 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1195 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1198 case OVS_ACTION_ATTR_POP_ETH
:
1199 ds_put_cstr(ds
, "pop_eth");
1201 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1202 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1203 ds_put_cstr(ds
, "push_vlan(");
1204 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1205 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1207 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1208 ds_put_char(ds
, ')');
1211 case OVS_ACTION_ATTR_POP_VLAN
:
1212 ds_put_cstr(ds
, "pop_vlan");
1214 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1215 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1216 ds_put_cstr(ds
, "push_mpls(");
1217 format_mpls_lse(ds
, mpls
->mpls_lse
);
1218 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1221 case OVS_ACTION_ATTR_POP_MPLS
: {
1222 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1223 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1226 case OVS_ACTION_ATTR_SAMPLE
:
1227 format_odp_sample_action(ds
, a
, portno_names
);
1229 case OVS_ACTION_ATTR_CT
:
1230 format_odp_conntrack_action(ds
, a
);
1232 case OVS_ACTION_ATTR_CT_CLEAR
:
1233 ds_put_cstr(ds
, "ct_clear");
1235 case OVS_ACTION_ATTR_CLONE
:
1236 format_odp_clone_action(ds
, a
, portno_names
);
1238 case OVS_ACTION_ATTR_PUSH_NSH
: {
1239 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1240 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1241 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1242 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1243 format_odp_push_nsh_action(ds
, nsh_hdr
);
1246 case OVS_ACTION_ATTR_POP_NSH
:
1247 ds_put_cstr(ds
, "pop_nsh()");
1249 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
1250 format_odp_check_pkt_len_action(ds
, a
, portno_names
);
1252 case OVS_ACTION_ATTR_DROP
:
1253 ds_put_cstr(ds
, "drop");
1255 case OVS_ACTION_ATTR_UNSPEC
:
1256 case __OVS_ACTION_ATTR_MAX
:
1258 format_generic_odp_action(ds
, a
);
1264 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1265 size_t actions_len
, const struct hmap
*portno_names
)
1268 const struct nlattr
*a
;
1271 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1273 ds_put_char(ds
, ',');
1275 format_odp_action(ds
, a
, portno_names
);
1280 if (left
== actions_len
) {
1281 ds_put_cstr(ds
, "<empty>");
1283 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1284 for (i
= 0; i
< left
; i
++) {
1285 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1287 ds_put_char(ds
, ')');
1290 ds_put_cstr(ds
, "drop");
1294 /* Separate out parse_odp_userspace_action() function. */
1296 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1299 struct user_action_cookie cookie
;
1301 odp_port_t tunnel_out_port
;
1303 void *user_data
= NULL
;
1304 size_t user_data_size
= 0;
1305 bool include_actions
= false;
1308 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1312 ofpbuf_init(&buf
, 16);
1313 memset(&cookie
, 0, sizeof cookie
);
1315 user_data
= &cookie
;
1316 user_data_size
= sizeof cookie
;
1319 uint32_t probability
;
1320 uint32_t collector_set_id
;
1321 uint32_t obs_domain_id
;
1322 uint32_t obs_point_id
;
1324 /* USER_ACTION_COOKIE_CONTROLLER. */
1326 uint8_t continuation
;
1329 uint64_t rule_cookie
;
1330 uint16_t controller_id
;
1335 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1336 "pcp=%i,output=%"SCNi32
")%n",
1337 &vid
, &pcp
, &output
, &n1
)) {
1341 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1346 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1347 cookie
.ofp_in_port
= OFPP_NONE
;
1348 cookie
.ofproto_uuid
= UUID_ZERO
;
1349 cookie
.sflow
.vlan_tci
= htons(tci
);
1350 cookie
.sflow
.output
= output
;
1351 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1354 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1355 cookie
.ofp_in_port
= OFPP_NONE
;
1356 cookie
.ofproto_uuid
= UUID_ZERO
;
1357 cookie
.slow_path
.reason
= 0;
1359 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1360 &cookie
.slow_path
.reason
,
1361 SLOW_PATH_REASON_MASK
, NULL
);
1362 if (res
< 0 || s
[n
+ res
] != ')') {
1366 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1367 "collector_set_id=%"SCNi32
","
1368 "obs_domain_id=%"SCNi32
","
1369 "obs_point_id=%"SCNi32
","
1370 "output_port=%"SCNi32
"%n",
1371 &probability
, &collector_set_id
,
1372 &obs_domain_id
, &obs_point_id
,
1376 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1377 cookie
.ofp_in_port
= OFPP_NONE
;
1378 cookie
.ofproto_uuid
= UUID_ZERO
;
1379 cookie
.flow_sample
.probability
= probability
;
1380 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1381 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1382 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1383 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1385 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1386 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1388 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1389 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1392 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1399 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1402 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1403 cookie
.ofp_in_port
= OFPP_NONE
;
1404 cookie
.ofproto_uuid
= UUID_ZERO
;
1405 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1406 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1408 ",continuation=%"SCNu8
1409 ",recirc_id=%"SCNu32
1410 ",rule_cookie=%"SCNx64
1411 ",controller_id=%"SCNu16
1412 ",max_len=%"SCNu16
")%n",
1413 &reason
, &dont_send
, &continuation
, &recirc_id
,
1414 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1416 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1417 cookie
.ofp_in_port
= OFPP_NONE
;
1418 cookie
.ofproto_uuid
= UUID_ZERO
;
1419 cookie
.controller
.dont_send
= dont_send
? true : false;
1420 cookie
.controller
.continuation
= continuation
? true : false;
1421 cookie
.controller
.reason
= reason
;
1422 cookie
.controller
.recirc_id
= recirc_id
;
1423 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1424 htonll(rule_cookie
));
1425 cookie
.controller
.controller_id
= controller_id
;
1426 cookie
.controller
.max_len
= max_len
;
1427 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1431 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1432 if (end
[0] != ')') {
1436 user_data
= buf
.data
;
1437 user_data_size
= buf
.size
;
1444 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1446 include_actions
= true;
1452 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1453 &tunnel_out_port
, &n1
)) {
1454 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1455 tunnel_out_port
, include_actions
, actions
);
1458 } else if (s
[n
] == ')') {
1459 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1460 ODPP_NONE
, include_actions
, actions
);
1467 struct ovs_action_push_eth push
;
1471 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1472 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1473 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1474 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1477 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1478 &push
, sizeof push
);
1485 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1486 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1493 ofpbuf_uninit(&buf
);
1498 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1500 struct eth_header
*eth
;
1501 struct ip_header
*ip
;
1502 struct ovs_16aligned_ip6_hdr
*ip6
;
1503 struct udp_header
*udp
;
1504 struct gre_base_hdr
*greh
;
1505 struct erspan_base_hdr
*ersh
;
1506 struct erspan_md2
*md2
;
1507 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, udp_csum
, sid
;
1509 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1514 uint8_t gtpu_flags
, gtpu_msgtype
;
1516 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1519 eth
= (struct eth_header
*) data
->header
;
1520 l3
= (struct ip_header
*) (eth
+ 1);
1521 ip
= (struct ip_header
*) l3
;
1522 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1523 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1524 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1527 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1531 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1532 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1535 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1538 eth
->eth_type
= htons(dl_type
);
1540 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1542 uint16_t ip_frag_off
;
1543 memset(ip
, 0, sizeof(*ip
));
1544 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1545 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1548 &ip
->ip_proto
, &ip
->ip_tos
,
1549 &ip
->ip_ttl
, &ip_frag_off
)) {
1552 put_16aligned_be32(&ip
->ip_src
, sip
);
1553 put_16aligned_be32(&ip
->ip_dst
, dip
);
1554 ip
->ip_frag_off
= htons(ip_frag_off
);
1555 ip
->ip_ihl_ver
= IP_IHL_VER(5, 4);
1556 ip_len
= sizeof *ip
;
1557 ip
->ip_csum
= csum(ip
, ip_len
);
1559 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1560 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1561 struct in6_addr sip6
, dip6
;
1564 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1565 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1566 ",hlimit=%"SCNi8
"),",
1567 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1568 &tclass
, &ip6
->ip6_hlim
)
1569 || (label
& ~IPV6_LABEL_MASK
) != 0
1570 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1571 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1574 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1575 htonl(tclass
<< 20) | htonl(label
));
1576 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1577 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1578 ip_len
= sizeof *ip6
;
1582 l4
= ((uint8_t *) l3
+ ip_len
);
1583 udp
= (struct udp_header
*) l4
;
1584 greh
= (struct gre_base_hdr
*) l4
;
1585 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1586 &udp_src
, &udp_dst
, &udp_csum
)) {
1587 uint32_t vx_flags
, vni
;
1589 udp
->udp_src
= htons(udp_src
);
1590 udp
->udp_dst
= htons(udp_dst
);
1592 udp
->udp_csum
= htons(udp_csum
);
1594 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1596 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1598 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1599 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1600 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1601 header_len
= sizeof *eth
+ ip_len
+
1602 sizeof *udp
+ sizeof *vxh
;
1603 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1604 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1606 memset(gnh
, 0, sizeof *gnh
);
1607 header_len
= sizeof *eth
+ ip_len
+
1608 sizeof *udp
+ sizeof *gnh
;
1610 if (ovs_scan_len(s
, &n
, "oam,")) {
1613 if (ovs_scan_len(s
, &n
, "crit,")) {
1616 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1619 if (ovs_scan_len(s
, &n
, ",options(")) {
1620 struct geneve_scan options
;
1623 memset(&options
, 0, sizeof options
);
1624 len
= scan_geneve(s
+ n
, &options
, NULL
);
1629 memcpy(gnh
->options
, options
.d
, options
.len
);
1630 gnh
->opt_len
= options
.len
/ 4;
1631 header_len
+= options
.len
;
1635 if (!ovs_scan_len(s
, &n
, "))")) {
1639 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1640 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1641 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1645 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1646 &gre_flags
, &gre_proto
)){
1648 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1649 tnl_type
= OVS_VPORT_TYPE_GRE
;
1651 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1653 greh
->flags
= htons(gre_flags
);
1654 greh
->protocol
= htons(gre_proto
);
1655 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1657 if (greh
->flags
& htons(GRE_CSUM
)) {
1659 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1663 memset(options
, 0, sizeof *options
);
1664 *((ovs_be16
*)options
) = htons(csum
);
1667 if (greh
->flags
& htons(GRE_KEY
)) {
1670 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1674 put_16aligned_be32(options
, htonl(key
));
1677 if (greh
->flags
& htons(GRE_SEQ
)) {
1680 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1683 put_16aligned_be32(options
, htonl(seq
));
1687 if (!ovs_scan_len(s
, &n
, "))")) {
1691 header_len
= sizeof *eth
+ ip_len
+
1692 ((uint8_t *) options
- (uint8_t *) greh
);
1693 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1694 &sid
, &erspan_idx
)) {
1695 ersh
= ERSPAN_HDR(greh
);
1696 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1699 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1700 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1702 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1705 greh
->flags
= htons(GRE_SEQ
);
1706 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1710 put_16aligned_be32(index
, htonl(erspan_idx
));
1712 if (!ovs_scan_len(s
, &n
, ")")) {
1715 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1716 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1718 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1719 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1721 ersh
= ERSPAN_HDR(greh
);
1722 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1724 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1725 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1727 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1730 greh
->flags
= htons(GRE_SEQ
);
1731 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1735 set_hwid(md2
, hwid
);
1738 if (!ovs_scan_len(s
, &n
, ")")) {
1742 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1743 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1745 } else if (ovs_scan_len(s
, &n
, "gtpu(flags=%"SCNi8
",msgtype=%"
1746 SCNu8
",teid=0x%"SCNx32
"))",
1747 >pu_flags
, >pu_msgtype
, &teid
)) {
1748 struct gtpuhdr
*gtph
= (struct gtpuhdr
*) (udp
+ 1);
1750 gtph
->md
.flags
= gtpu_flags
;
1751 gtph
->md
.msgtype
= gtpu_msgtype
;
1752 put_16aligned_be32(>ph
->teid
, htonl(teid
));
1753 tnl_type
= OVS_VPORT_TYPE_GTPU
;
1754 header_len
= sizeof *eth
+ ip_len
+
1755 sizeof *udp
+ sizeof *gtph
;
1760 /* check tunnel meta data. */
1761 if (data
->tnl_type
!= tnl_type
) {
1764 if (data
->header_len
!= header_len
) {
1769 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1776 struct ct_nat_params
{
1782 struct in6_addr ip6
;
1786 struct in6_addr ip6
;
1796 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1798 if (ovs_scan_len(s
, n
, "=")) {
1799 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1800 struct in6_addr ipv6
;
1802 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1803 p
->addr_len
= sizeof p
->addr_min
.ip
;
1804 if (ovs_scan_len(s
, n
, "-")) {
1805 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1806 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1810 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1811 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1812 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1813 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1814 p
->addr_min
.ip6
= ipv6
;
1815 if (ovs_scan_len(s
, n
, "-")) {
1816 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1817 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1818 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1819 p
->addr_max
.ip6
= ipv6
;
1827 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1828 if (ovs_scan_len(s
, n
, "-")) {
1829 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1839 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1843 if (ovs_scan_len(s
, &n
, "nat")) {
1844 memset(p
, 0, sizeof *p
);
1846 if (ovs_scan_len(s
, &n
, "(")) {
1850 end
= strchr(s
+ n
, ')');
1857 n
+= strspn(s
+ n
, delimiters
);
1858 if (ovs_scan_len(s
, &n
, "src")) {
1859 int err
= scan_ct_nat_range(s
, &n
, p
);
1866 if (ovs_scan_len(s
, &n
, "dst")) {
1867 int err
= scan_ct_nat_range(s
, &n
, p
);
1874 if (ovs_scan_len(s
, &n
, "persistent")) {
1875 p
->persistent
= true;
1878 if (ovs_scan_len(s
, &n
, "hash")) {
1879 p
->proto_hash
= true;
1882 if (ovs_scan_len(s
, &n
, "random")) {
1883 p
->proto_random
= true;
1889 if (p
->snat
&& p
->dnat
) {
1892 if ((p
->addr_len
!= 0 &&
1893 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1894 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1895 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1898 if (p
->proto_hash
&& p
->proto_random
) {
1908 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1910 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1913 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1914 } else if (p
->dnat
) {
1915 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1919 if (p
->addr_len
!= 0) {
1920 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1922 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1923 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1927 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1928 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1929 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1932 if (p
->persistent
) {
1933 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1935 if (p
->proto_hash
) {
1936 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1938 if (p
->proto_random
) {
1939 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1943 nl_msg_end_nested(actions
, start
);
1947 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1951 if (ovs_scan(s
, "ct")) {
1952 const char *helper
= NULL
, *timeout
= NULL
;
1953 size_t helper_len
= 0, timeout_len
= 0;
1954 bool commit
= false;
1955 bool force_commit
= false;
1960 } ct_mark
= { 0, 0 };
1965 struct ct_nat_params nat_params
;
1966 bool have_nat
= false;
1970 memset(&ct_label
, 0, sizeof(ct_label
));
1973 if (ovs_scan(s
, "(")) {
1976 end
= strchr(s
, ')');
1984 s
+= strspn(s
, delimiters
);
1985 if (ovs_scan(s
, "commit%n", &n
)) {
1990 if (ovs_scan(s
, "force_commit%n", &n
)) {
1991 force_commit
= true;
1995 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1999 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
2002 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
2005 ct_mark
.mask
= UINT32_MAX
;
2009 if (ovs_scan(s
, "label=%n", &n
)) {
2013 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
2020 if (ovs_scan(s
, "helper=%n", &n
)) {
2022 helper_len
= strcspn(s
, delimiters_end
);
2023 if (!helper_len
|| helper_len
> 15) {
2030 if (ovs_scan(s
, "timeout=%n", &n
)) {
2032 timeout_len
= strcspn(s
, delimiters_end
);
2033 if (!timeout_len
|| timeout_len
> 31) {
2041 n
= scan_ct_nat(s
, &nat_params
);
2046 /* end points to the end of the nested, nat action.
2047 * find the real end. */
2050 /* Nothing matched. */
2055 if (commit
&& force_commit
) {
2059 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
2061 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
2062 } else if (force_commit
) {
2063 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
2066 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
2069 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
2072 if (!ovs_u128_is_zero(ct_label
.mask
)) {
2073 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
2077 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
2081 nl_msg_put_string__(actions
, OVS_CT_ATTR_TIMEOUT
, timeout
,
2085 nl_msg_put_ct_nat(&nat_params
, actions
);
2087 nl_msg_end_nested(actions
, start
);
2094 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
2095 uint8_t * metadata
, size_t md_size
,
2099 struct ovs_nsh_key_base base
;
2101 base
.flags
= nsh
->flags
;
2102 base
.ttl
= nsh
->ttl
;
2103 base
.mdtype
= nsh
->mdtype
;
2105 base
.path_hdr
= nsh
->path_hdr
;
2107 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
2108 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
2111 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2112 sizeof nsh
->context
);
2114 switch (nsh
->mdtype
) {
2116 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2117 sizeof nsh
->context
);
2120 if (metadata
&& md_size
> 0) {
2121 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2126 /* No match support for other MD formats yet. */
2130 nl_msg_end_nested(buf
, nsh_key_ofs
);
2135 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2142 struct ovs_key_nsh nsh
;
2143 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2144 uint8_t md_size
= 0;
2146 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2151 /* The default is NSH_M_TYPE1 */
2154 nsh
.mdtype
= NSH_M_TYPE1
;
2155 nsh
.np
= NSH_P_ETHERNET
;
2156 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2157 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2160 n
+= strspn(s
+ n
, delimiters
);
2165 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2168 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2171 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2172 switch (nsh
.mdtype
) {
2174 /* This is the default format. */;
2177 /* Length will be updated later. */
2186 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2189 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2192 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2195 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2196 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2197 nsh
.context
[0] = htonl(cd
);
2200 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2201 nsh
.context
[1] = htonl(cd
);
2204 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2205 nsh
.context
[2] = htonl(cd
);
2208 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2209 nsh
.context
[3] = htonl(cd
);
2213 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2216 size_t mdlen
, padding
;
2217 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)
2218 && n
/2 <= sizeof metadata
) {
2219 ofpbuf_use_stub(&b
, metadata
, sizeof metadata
);
2220 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2221 /* Pad metadata to 4 bytes. */
2222 padding
= PAD_SIZE(mdlen
, 4);
2224 ofpbuf_put_zeros(&b
, padding
);
2226 md_size
= mdlen
+ padding
;
2237 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2238 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2239 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2240 nl_msg_end_nested(actions
, offset
);
2247 parse_action_list(struct parse_odp_context
*context
, const char *s
,
2248 struct ofpbuf
*actions
)
2255 n
+= strspn(s
+ n
, delimiters
);
2259 retval
= parse_odp_action(context
, s
+ n
, actions
);
2266 if (actions
->size
> UINT16_MAX
) {
2275 parse_odp_action(struct parse_odp_context
*context
, const char *s
,
2276 struct ofpbuf
*actions
)
2282 if (context
->depth
== MAX_ODP_NESTED
) {
2285 retval
= parse_odp_action__(context
, s
, actions
);
2295 parse_odp_action__(struct parse_odp_context
*context
, const char *s
,
2296 struct ofpbuf
*actions
)
2302 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2303 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2312 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2313 struct ovs_action_trunc
*trunc
;
2315 trunc
= nl_msg_put_unspec_uninit(actions
,
2316 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2317 trunc
->max_len
= max_len
;
2322 if (context
->port_names
) {
2323 int len
= strcspn(s
, delimiters
);
2324 struct simap_node
*node
;
2326 node
= simap_find_len(context
->port_names
, s
, len
);
2328 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2337 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2338 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2343 if (!strncmp(s
, "userspace(", 10)) {
2344 return parse_odp_userspace_action(s
, actions
);
2347 if (!strncmp(s
, "set(", 4)) {
2350 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2351 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2352 struct nlattr
*nested
, *key
;
2355 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2356 retval
= parse_odp_key_mask_attr(context
, s
+ 4, actions
, &maskbuf
);
2358 ofpbuf_uninit(&maskbuf
);
2361 if (s
[retval
+ 4] != ')') {
2362 ofpbuf_uninit(&maskbuf
);
2366 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2369 size
= nl_attr_get_size(mask
);
2370 if (size
== nl_attr_get_size(key
)) {
2371 /* Change to masked set action if not fully masked. */
2372 if (!is_all_ones(mask
+ 1, size
)) {
2373 /* Remove padding of eariler key payload */
2374 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2376 /* Put mask payload right after key payload */
2377 key
->nla_len
+= size
;
2378 ofpbuf_put(actions
, mask
+ 1, size
);
2380 /* 'actions' may have been reallocated by ofpbuf_put(). */
2381 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2382 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2385 /* Add new padding as needed */
2386 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2390 ofpbuf_uninit(&maskbuf
);
2392 nl_msg_end_nested(actions
, start_ofs
);
2397 struct ovs_action_push_vlan push
;
2398 int tpid
= ETH_TYPE_VLAN
;
2403 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2404 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2405 &vid
, &pcp
, &cfi
, &n
)
2406 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2407 &tpid
, &vid
, &pcp
, &n
)
2408 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2409 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2410 if ((vid
& ~(VLAN_VID_MASK
>> VLAN_VID_SHIFT
)) != 0
2411 || (pcp
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) != 0) {
2414 push
.vlan_tpid
= htons(tpid
);
2415 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2416 | (pcp
<< VLAN_PCP_SHIFT
)
2417 | (cfi
? VLAN_CFI
: 0));
2418 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2419 &push
, sizeof push
);
2425 if (!strncmp(s
, "pop_vlan", 8)) {
2426 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2431 unsigned long long int meter_id
;
2434 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2435 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2444 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2445 && percentage
>= 0. && percentage
<= 100.0) {
2446 size_t sample_ofs
, actions_ofs
;
2449 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2450 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2451 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2452 (probability
<= 0 ? 0
2453 : probability
>= UINT32_MAX
? UINT32_MAX
2456 actions_ofs
= nl_msg_start_nested(actions
,
2457 OVS_SAMPLE_ATTR_ACTIONS
);
2458 int retval
= parse_action_list(context
, s
+ n
, actions
);
2465 nl_msg_end_nested(actions
, actions_ofs
);
2466 nl_msg_end_nested(actions
, sample_ofs
);
2468 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2473 if (!strncmp(s
, "clone(", 6)) {
2477 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2478 int retval
= parse_action_list(context
, s
+ n
, actions
);
2483 nl_msg_end_nested(actions
, actions_ofs
);
2489 if (!strncmp(s
, "push_nsh(", 9)) {
2490 int retval
= parse_odp_push_nsh_action(s
, actions
);
2500 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2501 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2510 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2511 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2517 if (!strncmp(s
, "ct_clear", 8)) {
2518 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2526 if (ovs_scan(s
, "check_pkt_len(size=%"SCNi16
",gt(%n", &pkt_len
, &n
)) {
2527 size_t cpl_ofs
, actions_ofs
;
2528 cpl_ofs
= nl_msg_start_nested(actions
,
2529 OVS_ACTION_ATTR_CHECK_PKT_LEN
);
2530 nl_msg_put_u16(actions
, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
, pkt_len
);
2531 actions_ofs
= nl_msg_start_nested(
2532 actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
);
2535 if (!strncasecmp(s
+ n
, "drop", 4)) {
2538 retval
= parse_action_list(context
, s
+ n
, actions
);
2545 nl_msg_end_nested(actions
, actions_ofs
);
2547 if (!ovs_scan(s
+ n
, "),le(%n", &retval
)) {
2552 actions_ofs
= nl_msg_start_nested(
2553 actions
, OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
);
2554 if (!strncasecmp(s
+ n
, "drop", 4)) {
2557 retval
= parse_action_list(context
, s
+ n
, actions
);
2563 nl_msg_end_nested(actions
, actions_ofs
);
2564 nl_msg_end_nested(actions
, cpl_ofs
);
2565 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2572 retval
= parse_conntrack_action(s
, actions
);
2579 struct ovs_action_push_tnl data
;
2582 n
= ovs_parse_tnl_push(s
, &data
);
2584 odp_put_tnl_push_action(actions
, &data
);
2594 /* Parses the string representation of datapath actions, in the format output
2595 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2596 * value. On success, the ODP actions are appended to 'actions' as a series of
2597 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2598 * way, 'actions''s data might be reallocated. */
2600 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2601 struct ofpbuf
*actions
)
2605 if (!strcasecmp(s
, "drop")) {
2606 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_DROP
, XLATE_OK
);
2610 struct parse_odp_context context
= (struct parse_odp_context
) {
2611 .port_names
= port_names
,
2614 old_size
= actions
->size
;
2618 s
+= strspn(s
, delimiters
);
2623 retval
= parse_odp_action(&context
, s
, actions
);
2625 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2626 actions
->size
= old_size
;
2635 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2636 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2639 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2640 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2641 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2642 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2643 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2644 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2645 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2646 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2647 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2648 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2649 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2650 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2651 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2652 .next
= ovs_vxlan_ext_attr_lens
,
2653 .next_max
= OVS_VXLAN_EXT_MAX
},
2654 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2655 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2656 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2657 [OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2660 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2661 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2662 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2663 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2664 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2665 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2666 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2667 .next
= ovs_tun_key_attr_lens
,
2668 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2669 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2670 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2671 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2672 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2673 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2674 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2675 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2676 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2677 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2678 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2679 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2680 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2681 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2682 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2683 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2684 [OVS_KEY_ATTR_ND_EXTENSIONS
] = { .len
= sizeof(struct ovs_key_nd_extensions
) },
2685 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2686 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2687 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2688 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2689 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2690 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2691 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2692 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2693 .next
= ovs_nsh_key_attr_lens
,
2694 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2697 /* Returns the correct length of the payload for a flow key attribute of the
2698 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2699 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2700 * payload is a nested type. */
2702 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2704 if (type
> max_type
) {
2705 return ATTR_LEN_INVALID
;
2708 return tbl
[type
].len
;
2712 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2714 size_t len
= nl_attr_get_size(a
);
2716 const uint8_t *unspec
;
2719 unspec
= nl_attr_get(a
);
2720 for (i
= 0; i
< len
; i
++) {
2722 ds_put_char(ds
, ' ');
2724 ds_put_format(ds
, "%02x", unspec
[i
]);
2730 ovs_frag_type_to_string(enum ovs_frag_type type
)
2733 case OVS_FRAG_TYPE_NONE
:
2735 case OVS_FRAG_TYPE_FIRST
:
2737 case OVS_FRAG_TYPE_LATER
:
2739 case __OVS_FRAG_TYPE_MAX
:
2745 enum odp_key_fitness
2746 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2747 struct nsh_hdr
*nsh_hdr
, size_t size
)
2750 const struct nlattr
*a
;
2751 bool unknown
= false;
2755 bool has_md1
= false;
2756 bool has_md2
= false;
2758 memset(nsh_hdr
, 0, size
);
2760 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2761 uint16_t type
= nl_attr_type(a
);
2762 size_t len
= nl_attr_get_size(a
);
2763 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2764 OVS_NSH_KEY_ATTR_MAX
, type
);
2766 if (len
!= expected_len
&& expected_len
>= 0) {
2767 return ODP_FIT_ERROR
;
2771 case OVS_NSH_KEY_ATTR_BASE
: {
2772 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2773 nsh_hdr
->next_proto
= base
->np
;
2774 nsh_hdr
->md_type
= base
->mdtype
;
2775 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2776 flags
= base
->flags
;
2780 case OVS_NSH_KEY_ATTR_MD1
: {
2781 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2782 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2784 mdlen
= nl_attr_get_size(a
);
2785 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2786 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2787 return ODP_FIT_ERROR
;
2789 memcpy(md1_dst
, md1
, mdlen
);
2792 case OVS_NSH_KEY_ATTR_MD2
: {
2793 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2794 const uint8_t *md2
= nl_attr_get(a
);
2796 mdlen
= nl_attr_get_size(a
);
2797 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2798 return ODP_FIT_ERROR
;
2800 memcpy(md2_dst
, md2
, mdlen
);
2804 /* Allow this to show up as unexpected, if there are unknown
2805 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2812 return ODP_FIT_TOO_MUCH
;
2815 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2816 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2817 return ODP_FIT_ERROR
;
2820 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2821 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2823 return ODP_FIT_PERFECT
;
2826 /* Reports the error 'msg', which is formatted as with printf().
2828 * If 'errorp' is nonnull, then some the wants the error report to come
2829 * directly back to it, so the function stores the error message into '*errorp'
2830 * (after first freeing it in case there's something there already).
2832 * Otherwise, logs the message at WARN level, rate-limited. */
2833 static void OVS_PRINTF_FORMAT(3, 4)
2834 odp_parse_error(struct vlog_rate_limit
*rl
, char **errorp
,
2835 const char *msg
, ...)
2837 if (OVS_UNLIKELY(errorp
)) {
2841 va_start(args
, msg
);
2842 *errorp
= xvasprintf(msg
, args
);
2844 } else if (!VLOG_DROP_WARN(rl
)) {
2846 va_start(args
, msg
);
2847 char *error
= xvasprintf(msg
, args
);
2850 VLOG_WARN("%s", error
);
2856 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2857 * returns fitness. If the attribute is a key, 'is_mask' should be false;
2858 * if it is a mask, 'is_mask' should be true. If 'errorp' is nonnull and the
2859 * function returns ODP_FIT_ERROR, stores a malloc()'d error message in
2861 static enum odp_key_fitness
2862 odp_nsh_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2863 struct ovs_key_nsh
*nsh
, struct ovs_key_nsh
*nsh_mask
,
2866 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2872 const struct nlattr
*a
;
2873 bool unknown
= false;
2874 bool has_md1
= false;
2876 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2877 uint16_t type
= nl_attr_type(a
);
2878 size_t len
= nl_attr_get_size(a
);
2879 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2880 OVS_NSH_KEY_ATTR_MAX
, type
);
2885 if (len
!= expected_len
) {
2886 odp_parse_error(&rl
, errorp
, "NSH %s attribute %"PRIu16
" "
2887 "should have length %d but actually has "
2889 nsh_mask
? "mask" : "key",
2890 type
, expected_len
, len
);
2891 return ODP_FIT_ERROR
;
2896 case OVS_NSH_KEY_ATTR_UNSPEC
:
2898 case OVS_NSH_KEY_ATTR_BASE
: {
2899 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2900 nsh
->flags
= base
->flags
;
2901 nsh
->ttl
= base
->ttl
;
2902 nsh
->mdtype
= base
->mdtype
;
2904 nsh
->path_hdr
= base
->path_hdr
;
2905 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2906 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2907 nsh_mask
->flags
= base_mask
->flags
;
2908 nsh_mask
->ttl
= base_mask
->ttl
;
2909 nsh_mask
->mdtype
= base_mask
->mdtype
;
2910 nsh_mask
->np
= base_mask
->np
;
2911 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2915 case OVS_NSH_KEY_ATTR_MD1
: {
2916 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2918 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2919 if (len
== 2 * sizeof(*md1
)) {
2920 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2921 memcpy(nsh_mask
->context
, md1_mask
->context
,
2926 case OVS_NSH_KEY_ATTR_MD2
:
2928 /* Allow this to show up as unexpected, if there are unknown
2929 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2936 return ODP_FIT_TOO_MUCH
;
2939 if (!is_mask
&& has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
&& !nsh_mask
) {
2940 odp_parse_error(&rl
, errorp
, "OVS_NSH_KEY_ATTR_MD1 present but "
2941 "declared mdtype %"PRIu8
" is not %d (NSH_M_TYPE1)",
2942 nsh
->mdtype
, NSH_M_TYPE1
);
2943 return ODP_FIT_ERROR
;
2946 return ODP_FIT_PERFECT
;
2949 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2950 * returns fitness. The attribute should be a key (not a mask). If 'errorp'
2951 * is nonnull and the function returns ODP_FIT_ERROR, stores a malloc()'d error
2952 * message in '*errorp'. */
2953 enum odp_key_fitness
2954 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2955 struct ovs_key_nsh
*nsh_mask
, char **errorp
)
2957 return odp_nsh_key_from_attr__(attr
, false, nsh
, nsh_mask
, errorp
);
2960 /* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
2961 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
2962 * 'is_mask' should be true. If 'errorp' is nonnull and the function returns
2963 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
2964 static enum odp_key_fitness
2965 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2966 struct flow_tnl
*tun
, char **errorp
)
2968 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2970 const struct nlattr
*a
;
2972 bool unknown
= false;
2974 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2975 uint16_t type
= nl_attr_type(a
);
2976 size_t len
= nl_attr_get_size(a
);
2977 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2978 OVS_TUNNEL_ATTR_MAX
, type
);
2980 if (len
!= expected_len
&& expected_len
>= 0) {
2981 odp_parse_error(&rl
, errorp
, "tunnel key attribute %"PRIu16
" "
2982 "should have length %d but actually has %"PRIuSIZE
,
2983 type
, expected_len
, len
);
2984 return ODP_FIT_ERROR
;
2988 case OVS_TUNNEL_KEY_ATTR_ID
:
2989 tun
->tun_id
= nl_attr_get_be64(a
);
2990 tun
->flags
|= FLOW_TNL_F_KEY
;
2992 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2993 tun
->ip_src
= nl_attr_get_be32(a
);
2995 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2996 tun
->ip_dst
= nl_attr_get_be32(a
);
2998 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2999 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
3001 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
3002 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
3004 case OVS_TUNNEL_KEY_ATTR_TOS
:
3005 tun
->ip_tos
= nl_attr_get_u8(a
);
3007 case OVS_TUNNEL_KEY_ATTR_TTL
:
3008 tun
->ip_ttl
= nl_attr_get_u8(a
);
3011 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3012 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3014 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3015 tun
->flags
|= FLOW_TNL_F_CSUM
;
3017 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3018 tun
->tp_src
= nl_attr_get_be16(a
);
3020 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3021 tun
->tp_dst
= nl_attr_get_be16(a
);
3023 case OVS_TUNNEL_KEY_ATTR_OAM
:
3024 tun
->flags
|= FLOW_TNL_F_OAM
;
3026 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
3027 static const struct nl_policy vxlan_opts_policy
[] = {
3028 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
3030 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
3032 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
3033 odp_parse_error(&rl
, errorp
, "error parsing VXLAN options");
3034 return ODP_FIT_ERROR
;
3037 if (ext
[OVS_VXLAN_EXT_GBP
]) {
3038 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
3040 tun
->gbp_id
= htons(gbp
& 0xFFFF);
3041 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
3046 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3047 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
3049 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
3050 const struct erspan_metadata
*opts
= nl_attr_get(a
);
3052 tun
->erspan_ver
= opts
->version
;
3053 if (tun
->erspan_ver
== 1) {
3054 tun
->erspan_idx
= ntohl(opts
->u
.index
);
3055 } else if (tun
->erspan_ver
== 2) {
3056 tun
->erspan_dir
= opts
->u
.md2
.dir
;
3057 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
3059 VLOG_WARN("%s invalid erspan version\n", __func__
);
3063 case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
: {
3064 const struct gtpu_metadata
*opts
= nl_attr_get(a
);
3066 tun
->gtpu_flags
= opts
->flags
;
3067 tun
->gtpu_msgtype
= opts
->msgtype
;
3072 /* Allow this to show up as unexpected, if there are unknown
3073 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
3080 odp_parse_error(&rl
, errorp
, "tunnel options missing TTL");
3081 return ODP_FIT_ERROR
;
3084 return ODP_FIT_TOO_MUCH
;
3086 return ODP_FIT_PERFECT
;
3089 /* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
3090 * fitness. The attribute should be a key (not a mask). If 'errorp' is
3091 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
3093 enum odp_key_fitness
3094 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
,
3100 memset(tun
, 0, sizeof *tun
);
3101 return odp_tun_key_from_attr__(attr
, false, tun
, errorp
);
3105 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
3106 const struct flow_tnl
*tun_flow_key
,
3107 const struct ofpbuf
*key_buf
, const char *tnl_type
)
3111 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
3113 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
3114 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
3115 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
3117 if (tun_key
->ip_src
) {
3118 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
3120 if (tun_key
->ip_dst
) {
3121 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
3123 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
3124 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
3126 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
3127 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
3129 if (tun_key
->ip_tos
) {
3130 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
3132 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
3133 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
3134 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
3136 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
3137 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
3139 if (tun_key
->tp_src
) {
3140 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
3142 if (tun_key
->tp_dst
) {
3143 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
3145 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
3146 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
3149 /* If tnl_type is set to a particular type of output tunnel,
3150 * only put its relevant tunnel metadata to the nlattr.
3151 * If tnl_type is NULL, put tunnel metadata according to the
3154 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
3155 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
3156 size_t vxlan_opts_ofs
;
3158 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
3159 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
3160 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
3161 nl_msg_end_nested(a
, vxlan_opts_ofs
);
3164 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
3165 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
3168 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
3169 !strcmp(tnl_type
, "ip6erspan")) &&
3170 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
3171 struct erspan_metadata opts
;
3173 opts
.version
= tun_key
->erspan_ver
;
3174 if (opts
.version
== 1) {
3175 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
3177 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
3178 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
3180 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
3181 &opts
, sizeof(opts
));
3184 if ((!tnl_type
|| !strcmp(tnl_type
, "gtpu")) &&
3185 (tun_key
->gtpu_flags
&& tun_key
->gtpu_msgtype
)) {
3186 struct gtpu_metadata opts
;
3188 opts
.flags
= tun_key
->gtpu_flags
;
3189 opts
.msgtype
= tun_key
->gtpu_msgtype
;
3190 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
3191 &opts
, sizeof(opts
));
3193 nl_msg_end_nested(a
, tun_key_ofs
);
3197 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
3200 /* Convert 'constant' to all the widths we need. C conversion rules ensure
3201 * that -1 becomes all-1-bits and 0 does not change. */
3202 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
3203 uint32_t u32
= constant
;
3204 uint8_t u8
= constant
;
3205 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
3208 case OVS_KEY_ATTR_UNSPEC
:
3209 case OVS_KEY_ATTR_ENCAP
:
3210 case __OVS_KEY_ATTR_MAX
:
3214 case OVS_KEY_ATTR_PRIORITY
:
3215 case OVS_KEY_ATTR_IN_PORT
:
3216 case OVS_KEY_ATTR_ETHERNET
:
3217 case OVS_KEY_ATTR_VLAN
:
3218 case OVS_KEY_ATTR_ETHERTYPE
:
3219 case OVS_KEY_ATTR_IPV4
:
3220 case OVS_KEY_ATTR_TCP
:
3221 case OVS_KEY_ATTR_UDP
:
3222 case OVS_KEY_ATTR_ICMP
:
3223 case OVS_KEY_ATTR_ICMPV6
:
3224 case OVS_KEY_ATTR_ND
:
3225 case OVS_KEY_ATTR_ND_EXTENSIONS
:
3226 case OVS_KEY_ATTR_SKB_MARK
:
3227 case OVS_KEY_ATTR_TUNNEL
:
3228 case OVS_KEY_ATTR_SCTP
:
3229 case OVS_KEY_ATTR_DP_HASH
:
3230 case OVS_KEY_ATTR_RECIRC_ID
:
3231 case OVS_KEY_ATTR_MPLS
:
3232 case OVS_KEY_ATTR_CT_STATE
:
3233 case OVS_KEY_ATTR_CT_ZONE
:
3234 case OVS_KEY_ATTR_CT_MARK
:
3235 case OVS_KEY_ATTR_CT_LABELS
:
3236 case OVS_KEY_ATTR_PACKET_TYPE
:
3237 case OVS_KEY_ATTR_NSH
:
3238 return is_all_byte(mask
, size
, u8
);
3240 case OVS_KEY_ATTR_TCP_FLAGS
:
3241 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
3243 case OVS_KEY_ATTR_IPV6
: {
3244 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
3245 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
3246 == htonl(IPV6_LABEL_MASK
& u32
)
3247 && ipv6_mask
->ipv6_proto
== u8
3248 && ipv6_mask
->ipv6_tclass
== u8
3249 && ipv6_mask
->ipv6_hlimit
== u8
3250 && ipv6_mask
->ipv6_frag
== u8
3251 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
3252 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
3255 case OVS_KEY_ATTR_ARP
:
3256 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
3258 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
3259 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
3262 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
3263 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
3268 /* The caller must already have verified that 'ma' has a correct length.
3270 * The main purpose of this function is formatting, to allow code to figure out
3271 * whether the mask can be omitted. It doesn't try hard for attributes that
3272 * contain sub-attributes, etc., because normally those would be broken down
3273 * further for formatting. */
3275 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
3277 return odp_mask_is_constant__(nl_attr_type(ma
),
3278 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
3281 /* The caller must already have verified that 'size' is a correct length for
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_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
3291 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3294 /* The caller must already have verified that 'ma' has a correct length. */
3296 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3298 enum ovs_key_attr attr
= nl_attr_type(ma
);
3299 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3303 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3306 struct odp_portno_names
*odp_portno_names
;
3308 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3309 odp_portno_names
->port_no
= port_no
;
3310 odp_portno_names
->name
= xstrdup(port_name
);
3311 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3312 hash_odp_port(port_no
));
3316 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3319 struct odp_portno_names
*odp_portno_names
;
3321 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3322 hash_odp_port(port_no
), portno_names
) {
3323 if (odp_portno_names
->port_no
== port_no
) {
3324 return odp_portno_names
->name
;
3332 odp_portno_names_destroy(struct hmap
*portno_names
)
3334 struct odp_portno_names
*odp_portno_names
;
3336 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3337 free(odp_portno_names
->name
);
3338 free(odp_portno_names
);
3343 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3346 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3348 ds_put_cstr(s
, name
);
3350 ds_put_format(s
, "%"PRIu32
, port_no
);
3354 /* Format helpers. */
3357 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3358 const struct eth_addr
*mask
, bool verbose
)
3360 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3362 if (verbose
|| !mask_empty
) {
3363 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3366 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3368 ds_put_format(ds
, "%s=", name
);
3369 eth_format_masked(key
, mask
, ds
);
3370 ds_put_char(ds
, ',');
3377 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3378 const ovs_be64
*mask
, bool verbose
)
3380 bool mask_empty
= mask
&& !*mask
;
3382 if (verbose
|| !mask_empty
) {
3383 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3385 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3386 if (!mask_full
) { /* Partially masked. */
3387 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3389 ds_put_char(ds
, ',');
3394 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3395 const ovs_be32
*mask
, bool verbose
)
3397 bool mask_empty
= mask
&& !*mask
;
3399 if (verbose
|| !mask_empty
) {
3400 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3402 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3403 if (!mask_full
) { /* Partially masked. */
3404 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3406 ds_put_char(ds
, ',');
3411 format_in6_addr(struct ds
*ds
, const char *name
,
3412 const struct in6_addr
*key
,
3413 const struct in6_addr
*mask
,
3416 char buf
[INET6_ADDRSTRLEN
];
3417 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3419 if (verbose
|| !mask_empty
) {
3420 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3422 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3423 ds_put_format(ds
, "%s=%s", name
, buf
);
3424 if (!mask_full
) { /* Partially masked. */
3425 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3426 ds_put_format(ds
, "/%s", buf
);
3428 ds_put_char(ds
, ',');
3433 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3434 const ovs_be32
*mask
, bool verbose
)
3436 bool mask_empty
= mask
&& !*mask
;
3438 if (verbose
|| !mask_empty
) {
3439 bool mask_full
= !mask
3440 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3442 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3443 if (!mask_full
) { /* Partially masked. */
3444 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3446 ds_put_char(ds
, ',');
3451 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3452 const uint8_t *mask
, bool verbose
)
3454 bool mask_empty
= mask
&& !*mask
;
3456 if (verbose
|| !mask_empty
) {
3457 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3459 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3460 if (!mask_full
) { /* Partially masked. */
3461 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3463 ds_put_char(ds
, ',');
3468 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3469 const uint8_t *mask
, bool verbose
)
3471 bool mask_empty
= mask
&& !*mask
;
3473 if (verbose
|| !mask_empty
) {
3474 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3476 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3477 if (!mask_full
) { /* Partially masked. */
3478 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3480 ds_put_char(ds
, ',');
3485 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3486 const ovs_be16
*mask
, bool verbose
)
3488 bool mask_empty
= mask
&& !*mask
;
3490 if (verbose
|| !mask_empty
) {
3491 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3493 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3494 if (!mask_full
) { /* Partially masked. */
3495 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3497 ds_put_char(ds
, ',');
3502 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3503 const ovs_be16
*mask
, bool verbose
)
3505 bool mask_empty
= mask
&& !*mask
;
3507 if (verbose
|| !mask_empty
) {
3508 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3510 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3511 if (!mask_full
) { /* Partially masked. */
3512 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3514 ds_put_char(ds
, ',');
3519 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3520 const uint16_t *mask
, bool verbose
)
3522 bool mask_empty
= mask
&& !*mask
;
3524 if (verbose
|| !mask_empty
) {
3525 ds_put_cstr(ds
, name
);
3526 ds_put_char(ds
, '(');
3528 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3529 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3530 } else { /* Fully masked. */
3531 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3533 ds_put_cstr(ds
, "),");
3538 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3539 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3543 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3544 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3545 expected_len
!= ATTR_LEN_NESTED
) {
3547 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3548 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3550 if (bad_key_len
|| bad_mask_len
) {
3552 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3555 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3556 nl_attr_get_size(a
), expected_len
);
3558 format_generic_odp_key(a
, ds
);
3560 ds_put_char(ds
, '/');
3562 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3563 nl_attr_get_size(ma
), expected_len
);
3565 format_generic_odp_key(ma
, ds
);
3567 ds_put_char(ds
, ')');
3576 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3577 const struct nlattr
*ma
)
3579 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3580 format_generic_odp_key(a
, ds
);
3581 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3582 ds_put_char(ds
, '/');
3583 format_generic_odp_key(ma
, ds
);
3585 ds_put_cstr(ds
, "),");
3589 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3590 const struct nlattr
*mask_attr
, struct ds
*ds
,
3594 const struct nlattr
*a
;
3597 ofpbuf_init(&ofp
, 100);
3598 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3599 uint16_t type
= nl_attr_type(a
);
3600 const struct nlattr
*ma
= NULL
;
3603 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3604 nl_attr_get_size(mask_attr
), type
);
3606 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3612 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3613 OVS_VXLAN_EXT_MAX
, true)) {
3618 case OVS_VXLAN_EXT_GBP
: {
3619 uint32_t key
= nl_attr_get_u32(a
);
3620 ovs_be16 id
, id_mask
;
3621 uint8_t flags
, flags_mask
= 0;
3623 id
= htons(key
& 0xFFFF);
3624 flags
= (key
>> 16) & 0xFF;
3626 uint32_t mask
= nl_attr_get_u32(ma
);
3627 id_mask
= htons(mask
& 0xFFFF);
3628 flags_mask
= (mask
>> 16) & 0xFF;
3631 ds_put_cstr(ds
, "gbp(");
3632 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3633 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3635 ds_put_cstr(ds
, "),");
3640 format_unknown_key(ds
, a
, ma
);
3646 ofpbuf_uninit(&ofp
);
3650 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3651 const struct nlattr
*mask_attr
, struct ds
*ds
,
3654 const struct erspan_metadata
*opts
, *mask
;
3655 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3657 opts
= nl_attr_get(attr
);
3658 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3660 ver
= (uint8_t)opts
->version
;
3662 ver_ma
= (uint8_t)mask
->version
;
3665 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3667 if (opts
->version
== 1) {
3669 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3670 ntohl(opts
->u
.index
),
3671 ntohl(mask
->u
.index
));
3673 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3675 } else if (opts
->version
== 2) {
3676 dir
= opts
->u
.md2
.dir
;
3677 hwid
= opts
->u
.md2
.hwid
;
3679 dir_ma
= mask
->u
.md2
.dir
;
3680 hwid_ma
= mask
->u
.md2
.hwid
;
3683 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3684 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3690 format_odp_tun_gtpu_opt(const struct nlattr
*attr
,
3691 const struct nlattr
*mask_attr
, struct ds
*ds
,
3694 const struct gtpu_metadata
*opts
, *mask
;
3696 opts
= nl_attr_get(attr
);
3697 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3699 format_u8x(ds
, "flags", opts
->flags
, mask
? &mask
->flags
: NULL
, verbose
);
3700 format_u8u(ds
, "msgtype", opts
->msgtype
, mask
? &mask
->msgtype
: NULL
,
3705 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3708 format_geneve_opts(const struct geneve_opt
*opt
,
3709 const struct geneve_opt
*mask
, int opts_len
,
3710 struct ds
*ds
, bool verbose
)
3712 while (opts_len
> 0) {
3714 uint8_t data_len
, data_len_mask
;
3716 if (opts_len
< sizeof *opt
) {
3717 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3718 opts_len
, sizeof *opt
);
3722 data_len
= opt
->length
* 4;
3724 if (mask
->length
== 0x1f) {
3725 data_len_mask
= UINT8_MAX
;
3727 data_len_mask
= mask
->length
;
3730 len
= sizeof *opt
+ data_len
;
3731 if (len
> opts_len
) {
3732 ds_put_format(ds
, "opt len %u greater than remaining %u",
3737 ds_put_char(ds
, '{');
3738 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3740 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3741 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3743 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3744 ds_put_hex(ds
, opt
+ 1, data_len
);
3745 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3746 ds_put_char(ds
, '/');
3747 ds_put_hex(ds
, mask
+ 1, data_len
);
3752 ds_put_char(ds
, '}');
3754 opt
+= len
/ sizeof(*opt
);
3756 mask
+= len
/ sizeof(*opt
);
3763 format_odp_tun_geneve(const struct nlattr
*attr
,
3764 const struct nlattr
*mask_attr
, struct ds
*ds
,
3767 int opts_len
= nl_attr_get_size(attr
);
3768 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3769 const struct geneve_opt
*mask
= mask_attr
?
3770 nl_attr_get(mask_attr
) : NULL
;
3772 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3773 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3774 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3778 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3782 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3786 const struct nlattr
*a
;
3787 struct ovs_key_nsh nsh
;
3788 struct ovs_key_nsh nsh_mask
;
3790 memset(&nsh
, 0, sizeof nsh
);
3791 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3793 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3794 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3795 const struct nlattr
*ma
= NULL
;
3798 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3799 nl_attr_get_size(mask_attr
), type
);
3802 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3803 OVS_NSH_KEY_ATTR_MAX
, true)) {
3808 case OVS_NSH_KEY_ATTR_UNSPEC
:
3810 case OVS_NSH_KEY_ATTR_BASE
: {
3811 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3812 const struct ovs_nsh_key_base
*base_mask
3813 = ma
? nl_attr_get(ma
) : NULL
;
3814 nsh
.flags
= base
->flags
;
3815 nsh
.ttl
= base
->ttl
;
3816 nsh
.mdtype
= base
->mdtype
;
3818 nsh
.path_hdr
= base
->path_hdr
;
3820 nsh_mask
.flags
= base_mask
->flags
;
3821 nsh_mask
.ttl
= base_mask
->ttl
;
3822 nsh_mask
.mdtype
= base_mask
->mdtype
;
3823 nsh_mask
.np
= base_mask
->np
;
3824 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3828 case OVS_NSH_KEY_ATTR_MD1
: {
3829 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3830 const struct ovs_nsh_key_md1
*md1_mask
3831 = ma
? nl_attr_get(ma
) : NULL
;
3832 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3834 memcpy(nsh_mask
.context
, md1_mask
->context
,
3835 sizeof md1_mask
->context
);
3839 case OVS_NSH_KEY_ATTR_MD2
:
3840 case __OVS_NSH_KEY_ATTR_MAX
:
3842 /* No support for matching other metadata formats yet. */
3848 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3850 format_nsh_key(ds
, &nsh
);
3855 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3856 struct ds
*ds
, bool verbose
)
3859 const struct nlattr
*a
;
3861 uint16_t mask_flags
= 0;
3864 ofpbuf_init(&ofp
, 100);
3865 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3866 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3867 const struct nlattr
*ma
= NULL
;
3870 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3871 nl_attr_get_size(mask_attr
), type
);
3873 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3874 OVS_TUNNEL_KEY_ATTR_MAX
,
3879 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3880 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3885 case OVS_TUNNEL_KEY_ATTR_ID
:
3886 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3887 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3888 flags
|= FLOW_TNL_F_KEY
;
3890 mask_flags
|= FLOW_TNL_F_KEY
;
3893 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3894 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3895 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3897 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3898 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3899 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3901 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3902 struct in6_addr ipv6_src
;
3903 ipv6_src
= nl_attr_get_in6_addr(a
);
3904 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3905 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3908 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3909 struct in6_addr ipv6_dst
;
3910 ipv6_dst
= nl_attr_get_in6_addr(a
);
3911 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3912 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3915 case OVS_TUNNEL_KEY_ATTR_TOS
:
3916 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3917 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3919 case OVS_TUNNEL_KEY_ATTR_TTL
:
3920 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3921 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3923 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3924 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3926 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3927 flags
|= FLOW_TNL_F_CSUM
;
3929 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3930 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3931 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3933 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3934 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3935 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3937 case OVS_TUNNEL_KEY_ATTR_OAM
:
3938 flags
|= FLOW_TNL_F_OAM
;
3940 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3941 ds_put_cstr(ds
, "vxlan(");
3942 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3943 ds_put_cstr(ds
, "),");
3945 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3946 ds_put_cstr(ds
, "geneve(");
3947 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3948 ds_put_cstr(ds
, "),");
3950 case OVS_TUNNEL_KEY_ATTR_PAD
:
3952 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3953 ds_put_cstr(ds
, "erspan(");
3954 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3955 ds_put_cstr(ds
, "),");
3957 case OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
:
3958 ds_put_cstr(ds
, "gtpu(");
3959 format_odp_tun_gtpu_opt(a
, ma
, ds
, verbose
);
3960 ds_put_cstr(ds
, ")");
3962 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3964 format_unknown_key(ds
, a
, ma
);
3969 /* Flags can have a valid mask even if the attribute is not set, so
3970 * we need to collect these separately. */
3972 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3973 switch (nl_attr_type(a
)) {
3974 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3975 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3977 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3978 mask_flags
|= FLOW_TNL_F_CSUM
;
3980 case OVS_TUNNEL_KEY_ATTR_OAM
:
3981 mask_flags
|= FLOW_TNL_F_OAM
;
3987 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3990 ofpbuf_uninit(&ofp
);
3994 odp_ct_state_to_string(uint32_t flag
)
3997 case OVS_CS_F_REPLY_DIR
:
3999 case OVS_CS_F_TRACKED
:
4003 case OVS_CS_F_ESTABLISHED
:
4005 case OVS_CS_F_RELATED
:
4007 case OVS_CS_F_INVALID
:
4009 case OVS_CS_F_SRC_NAT
:
4011 case OVS_CS_F_DST_NAT
:
4019 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
4020 const uint8_t *mask
, bool verbose OVS_UNUSED
)
4022 bool mask_empty
= mask
&& !*mask
;
4023 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
4025 /* ODP frag is an enumeration field; partial masks are not meaningful. */
4026 if (!mask_empty
&& !mask_full
) {
4027 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
4029 } else if (!mask_empty
) {
4030 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
4035 mask_empty(const struct nlattr
*ma
)
4043 mask
= nl_attr_get(ma
);
4044 n
= nl_attr_get_size(ma
);
4046 return is_all_zeros(mask
, n
);
4049 /* The caller must have already verified that 'a' and 'ma' have correct
4052 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
4053 const struct hmap
*portno_names
, struct ds
*ds
,
4056 enum ovs_key_attr attr
= nl_attr_type(a
);
4057 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
4060 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
4062 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
4064 ds_put_char(ds
, '(');
4066 case OVS_KEY_ATTR_ENCAP
:
4067 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
4068 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
4069 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
4071 } else if (nl_attr_get_size(a
)) {
4072 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
4077 case OVS_KEY_ATTR_PRIORITY
:
4078 case OVS_KEY_ATTR_SKB_MARK
:
4079 case OVS_KEY_ATTR_DP_HASH
:
4080 case OVS_KEY_ATTR_RECIRC_ID
:
4081 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4083 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4087 case OVS_KEY_ATTR_CT_MARK
:
4088 if (verbose
|| !mask_empty(ma
)) {
4089 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4091 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4096 case OVS_KEY_ATTR_CT_STATE
:
4098 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
4100 ds_put_format(ds
, "/%#"PRIx32
,
4101 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
4103 } else if (!is_exact
) {
4104 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
4106 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
4109 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
4113 case OVS_KEY_ATTR_CT_ZONE
:
4114 if (verbose
|| !mask_empty(ma
)) {
4115 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
4117 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
4122 case OVS_KEY_ATTR_CT_LABELS
: {
4123 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
4124 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4126 format_u128(ds
, value
, mask
, verbose
);
4130 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
4131 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
4132 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4134 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
4135 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
4136 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
4138 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
4140 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
4146 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
4147 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
4148 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4150 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
4152 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
4154 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
4156 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
4158 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
4164 case OVS_KEY_ATTR_TUNNEL
:
4165 format_odp_tun_attr(a
, ma
, ds
, verbose
);
4168 case OVS_KEY_ATTR_IN_PORT
:
4170 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
4172 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
4174 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
4179 case OVS_KEY_ATTR_PACKET_TYPE
: {
4180 ovs_be32 value
= nl_attr_get_be32(a
);
4181 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
4183 ovs_be16 ns
= htons(pt_ns(value
));
4184 ovs_be16 ns_mask
= htons(pt_ns(mask
));
4185 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
4187 ovs_be16 ns_type
= pt_ns_type_be(value
);
4188 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
4189 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
4195 case OVS_KEY_ATTR_ETHERNET
: {
4196 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4197 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
4199 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
4200 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
4204 case OVS_KEY_ATTR_VLAN
:
4205 format_vlan_tci(ds
, nl_attr_get_be16(a
),
4206 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
4209 case OVS_KEY_ATTR_MPLS
: {
4210 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
4211 const struct ovs_key_mpls
*mpls_mask
= NULL
;
4212 size_t size
= nl_attr_get_size(a
);
4214 if (!size
|| size
% sizeof *mpls_key
) {
4215 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
4219 mpls_mask
= nl_attr_get(ma
);
4220 if (size
!= nl_attr_get_size(ma
)) {
4221 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
4222 "mask length %"PRIuSIZE
")",
4223 size
, nl_attr_get_size(ma
));
4227 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
4230 case OVS_KEY_ATTR_ETHERTYPE
:
4231 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
4233 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
4237 case OVS_KEY_ATTR_IPV4
: {
4238 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
4239 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4241 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
4242 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
4243 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
4245 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
4246 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
4247 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
4252 case OVS_KEY_ATTR_IPV6
: {
4253 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
4254 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4256 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
4258 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
4260 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
4262 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
4264 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
4266 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
4268 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
4273 /* These have the same structure and format. */
4274 case OVS_KEY_ATTR_TCP
:
4275 case OVS_KEY_ATTR_UDP
:
4276 case OVS_KEY_ATTR_SCTP
: {
4277 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
4278 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4280 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
4281 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
4285 case OVS_KEY_ATTR_TCP_FLAGS
:
4287 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
4288 ntohs(nl_attr_get_be16(a
)),
4289 TCP_FLAGS(nl_attr_get_be16(ma
)),
4290 TCP_FLAGS(OVS_BE16_MAX
));
4292 format_flags(ds
, packet_tcp_flag_to_string
,
4293 ntohs(nl_attr_get_be16(a
)), '|');
4297 case OVS_KEY_ATTR_ICMP
: {
4298 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
4299 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4301 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
4302 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
4306 case OVS_KEY_ATTR_ICMPV6
: {
4307 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
4308 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4310 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
4312 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4317 case OVS_KEY_ATTR_ARP
: {
4318 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4319 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4321 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4322 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4323 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4324 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4325 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4329 case OVS_KEY_ATTR_ND
: {
4330 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4331 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4333 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4335 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4336 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4341 case OVS_KEY_ATTR_ND_EXTENSIONS
: {
4342 const struct ovs_key_nd_extensions
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4343 const struct ovs_key_nd_extensions
*key
= nl_attr_get(a
);
4346 format_be32_masked(ds
, &first
, "nd_reserved", key
->nd_reserved
,
4348 ds_put_char(ds
, ',');
4350 format_u8u(ds
, "nd_options_type", key
->nd_options_type
,
4351 MASK(mask
, nd_options_type
), verbose
);
4356 case OVS_KEY_ATTR_NSH
: {
4357 format_odp_nsh_attr(a
, ma
, ds
);
4360 case OVS_KEY_ATTR_UNSPEC
:
4361 case __OVS_KEY_ATTR_MAX
:
4363 format_generic_odp_key(a
, ds
);
4365 ds_put_char(ds
, '/');
4366 format_generic_odp_key(ma
, ds
);
4370 ds_put_char(ds
, ')');
4374 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4375 const struct hmap
*portno_names
, struct ds
*ds
,
4378 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4379 OVS_KEY_ATTR_MAX
, false)) {
4380 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4384 static struct nlattr
*
4385 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4386 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4388 const struct nlattr
*a
;
4390 int type
= nl_attr_type(key
);
4391 int size
= nl_attr_get_size(key
);
4393 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4394 nl_msg_put_unspec_zero(ofp
, type
, size
);
4398 if (tbl
[type
].next
) {
4399 const struct attr_len_tbl
*entry
= &tbl
[type
];
4401 max
= entry
->next_max
;
4404 nested_mask
= nl_msg_start_nested(ofp
, type
);
4405 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4406 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4408 nl_msg_end_nested(ofp
, nested_mask
);
4415 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4416 const ovs_32aligned_u128
*mask
, bool verbose
)
4418 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4419 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4420 ds_put_hex(ds
, &value
, sizeof value
);
4421 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4422 value
= hton128(get_32aligned_u128(mask
));
4423 ds_put_char(ds
, '/');
4424 ds_put_hex(ds
, &value
, sizeof value
);
4429 /* Read the string from 's_' as a 128-bit value. If the string contains
4430 * a "/", the rest of the string will be treated as a 128-bit mask.
4432 * If either the value or mask is larger than 64 bits, the string must
4433 * be in hexadecimal.
4436 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4438 char *s
= CONST_CAST(char *, s_
);
4442 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4443 *value
= ntoh128(be_value
);
4448 if (ovs_scan(s
, "/%n", &n
)) {
4452 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4453 sizeof be_mask
, &s
);
4457 *mask
= ntoh128(be_mask
);
4459 *mask
= OVS_U128_MAX
;
4469 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4473 if (ovs_scan(s
, "ufid:")) {
4476 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4488 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4490 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4493 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4494 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4495 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4496 * non-null, translates odp port number to its name. */
4498 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4499 const struct nlattr
*mask
, size_t mask_len
,
4500 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4503 const struct nlattr
*a
;
4505 bool has_ethtype_key
= false;
4506 bool has_packet_type_key
= false;
4508 bool first_field
= true;
4510 ofpbuf_init(&ofp
, 100);
4511 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4512 int attr_type
= nl_attr_type(a
);
4513 const struct nlattr
*ma
= (mask
&& mask_len
4514 ? nl_attr_find__(mask
, mask_len
,
4517 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4518 OVS_KEY_ATTR_MAX
, false)) {
4522 bool is_nested_attr
;
4523 bool is_wildcard
= false;
4525 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4526 has_ethtype_key
= true;
4527 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4528 has_packet_type_key
= true;
4531 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4532 OVS_KEY_ATTR_MAX
, attr_type
) ==
4535 if (mask
&& mask_len
) {
4536 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4537 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4540 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4541 if (is_wildcard
&& !ma
) {
4542 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4547 ds_put_char(ds
, ',');
4549 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4550 first_field
= false;
4551 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4552 && !has_packet_type_key
) {
4553 /* This special case reflects differences between the kernel
4554 * and userspace datapaths regarding the root type of the
4555 * packet being matched (typically Ethernet but some tunnels
4556 * can encapsulate IPv4 etc.). The kernel datapath does not
4557 * have an explicit way to indicate packet type; instead:
4559 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4560 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4561 * Ethertype encoded in the Ethernet header.
4563 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4564 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4565 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4566 * packet is an IPv4 packet).
4568 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4569 * all-wildcarded, it is important to print it.
4571 * On the other hand, the userspace datapath supports
4572 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4573 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4574 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4576 ds_put_char(ds
, ',');
4578 ds_put_cstr(ds
, "eth()");
4582 ofpbuf_uninit(&ofp
);
4587 if (left
== key_len
) {
4588 ds_put_cstr(ds
, "<empty>");
4590 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4591 for (i
= 0; i
< left
; i
++) {
4592 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4594 ds_put_char(ds
, ')');
4596 if (!has_ethtype_key
) {
4597 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4598 OVS_KEY_ATTR_ETHERTYPE
);
4600 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4601 ntohs(nl_attr_get_be16(ma
)));
4605 ds_put_cstr(ds
, "<empty>");
4609 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4610 * OVS_KEY_ATTR_* attributes in 'key'. */
4612 odp_flow_key_format(const struct nlattr
*key
,
4613 size_t key_len
, struct ds
*ds
)
4615 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4619 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4621 if (!strcasecmp(s
, "no")) {
4622 *type
= OVS_FRAG_TYPE_NONE
;
4623 } else if (!strcasecmp(s
, "first")) {
4624 *type
= OVS_FRAG_TYPE_FIRST
;
4625 } else if (!strcasecmp(s
, "later")) {
4626 *type
= OVS_FRAG_TYPE_LATER
;
4636 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4640 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4641 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4645 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4646 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4649 memset(mask
, 0xff, sizeof *mask
);
4658 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4662 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4666 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4667 IP_SCAN_ARGS(mask
), &n
)) {
4670 *mask
= OVS_BE32_MAX
;
4679 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4682 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4684 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4685 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4689 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4690 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4693 memset(mask
, 0xff, sizeof *mask
);
4702 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4707 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4708 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4713 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4714 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4716 *mask
= htonl(mask_
);
4718 *mask
= htonl(IPV6_LABEL_MASK
);
4727 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4731 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4735 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4747 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4751 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4755 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4767 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4771 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4775 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4787 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4789 uint16_t key_
, mask_
;
4792 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4797 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4799 *mask
= htons(mask_
);
4801 *mask
= OVS_BE16_MAX
;
4810 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4812 uint32_t key_
, mask_
;
4815 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4820 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4822 *mask
= htonl(mask_
);
4824 *mask
= OVS_BE32_MAX
;
4833 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4835 uint64_t key_
, mask_
;
4838 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4841 *key
= htonll(key_
);
4843 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4845 *mask
= htonll(mask_
);
4847 *mask
= OVS_BE64_MAX
;
4856 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4858 uint32_t flags
, fmask
;
4861 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4862 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4863 if (n
>= 0 && s
[n
] == ')') {
4874 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4876 uint32_t flags
, fmask
;
4879 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4880 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4882 *key
= htons(flags
);
4884 *mask
= htons(fmask
);
4892 ovs_to_odp_ct_state(uint8_t state
)
4896 #define CS_STATE(ENUM, INDEX, NAME) \
4897 if (state & CS_##ENUM) { \
4898 odp |= OVS_CS_F_##ENUM; \
4907 odp_to_ovs_ct_state(uint32_t flags
)
4911 #define CS_STATE(ENUM, INDEX, NAME) \
4912 if (flags & OVS_CS_F_##ENUM) { \
4913 state |= CS_##ENUM; \
4922 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4924 uint32_t flags
, fmask
;
4927 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4928 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4929 mask
? &fmask
: NULL
);
4942 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4946 enum ovs_frag_type frag_type
;
4948 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4949 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4962 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4963 const struct simap
*port_names
)
4967 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4971 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4978 } else if (port_names
) {
4979 const struct simap_node
*node
;
4982 len
= strcspn(s
, ")");
4983 node
= simap_find_len(port_names
, s
, len
);
4996 /* Helper for vlan parsing. */
4997 struct ovs_key_vlan__
{
5002 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
5004 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
5006 if (value
>> bits
) {
5010 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
5015 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
5018 uint16_t key_
, mask_
;
5021 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
5024 if (set_be16_bf(key
, bits
, offset
, key_
)) {
5026 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
5029 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
5033 *mask
|= htons(((1U << bits
) - 1) << offset
);
5043 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5045 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
5049 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5051 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
5055 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
5057 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
5062 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
5064 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
5066 if (value
>> bits
) {
5070 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
5075 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
5078 uint32_t key_
, mask_
;
5081 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
5084 if (set_be32_bf(key
, bits
, offset
, key_
)) {
5086 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
5089 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
5093 *mask
|= htonl(((1U << bits
) - 1) << offset
);
5103 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5105 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
5109 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5111 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
5115 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5117 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
5121 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
5123 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
5127 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
5129 const char *s_base
= s
;
5130 ovs_be16 id
= 0, id_mask
= 0;
5131 uint8_t flags
= 0, flags_mask
= 0;
5134 if (!strncmp(s
, "id=", 3)) {
5136 len
= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
5146 if (!strncmp(s
, "flags=", 6)) {
5148 len
= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
5155 if (!strncmp(s
, "))", 2)) {
5158 *key
= (flags
<< 16) | ntohs(id
);
5160 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
5170 scan_gtpu_metadata(const char *s
,
5171 struct gtpu_metadata
*key
,
5172 struct gtpu_metadata
*mask
)
5174 const char *s_base
= s
;
5175 uint8_t flags
, flags_ma
;
5176 uint8_t msgtype
, msgtype_ma
;
5179 if (!strncmp(s
, "flags=", 6)) {
5181 len
= scan_u8(s
, &flags
, mask
? &flags_ma
: NULL
);
5192 if (!strncmp(s
, "msgtype=", 8)) {
5194 len
= scan_u8(s
, &msgtype
, mask
? &msgtype_ma
: NULL
);
5201 if (!strncmp(s
, ")", 1)) {
5204 key
->msgtype
= msgtype
;
5206 mask
->flags
= flags_ma
;
5207 mask
->msgtype
= msgtype_ma
;
5214 scan_erspan_metadata(const char *s
,
5215 struct erspan_metadata
*key
,
5216 struct erspan_metadata
*mask
)
5218 const char *s_base
= s
;
5219 uint32_t idx
= 0, idx_mask
= 0;
5220 uint8_t ver
= 0, dir
= 0, hwid
= 0;
5221 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
5224 if (!strncmp(s
, "ver=", 4)) {
5226 len
= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
5238 if (!strncmp(s
, "idx=", 4)) {
5240 len
= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
5247 if (!strncmp(s
, ")", 1)) {
5250 key
->u
.index
= htonl(idx
);
5252 mask
->u
.index
= htonl(idx_mask
);
5257 } else if (ver
== 2) {
5258 if (!strncmp(s
, "dir=", 4)) {
5260 len
= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
5269 if (!strncmp(s
, "hwid=", 5)) {
5271 len
= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
5278 if (!strncmp(s
, ")", 1)) {
5281 key
->u
.md2
.hwid
= hwid
;
5282 key
->u
.md2
.dir
= dir
;
5284 mask
->u
.md2
.hwid
= hwid_mask
;
5285 mask
->u
.md2
.dir
= dir_mask
;
5295 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
5297 const char *s_base
= s
;
5298 struct geneve_opt
*opt
= key
->d
;
5299 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
5300 int len_remain
= sizeof key
->d
;
5303 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
5307 len_remain
-= sizeof *opt
;
5309 if (!strncmp(s
, "class=", 6)) {
5311 len
= scan_be16(s
, &opt
->opt_class
,
5312 mask
? &opt_mask
->opt_class
: NULL
);
5318 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
5324 if (!strncmp(s
, "type=", 5)) {
5326 len
= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
5332 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5338 if (!strncmp(s
, "len=", 4)) {
5339 uint8_t opt_len
, opt_len_mask
;
5341 len
= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
5347 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
5350 opt
->length
= opt_len
/ 4;
5352 opt_mask
->length
= opt_len_mask
;
5356 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5361 if (parse_int_string(s
, (uint8_t *)(opt
+ 1),
5362 data_len
, (char **)&s
)) {
5369 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
5370 data_len
, (char **)&s
)) {
5381 opt
+= 1 + data_len
/ 4;
5383 opt_mask
+= 1 + data_len
/ 4;
5385 len_remain
-= data_len
;
5392 len
= sizeof key
->d
- len_remain
;
5406 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
5408 const uint16_t *flags
= data_
;
5410 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
5411 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5413 if (*flags
& FLOW_TNL_F_CSUM
) {
5414 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5416 if (*flags
& FLOW_TNL_F_OAM
) {
5417 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5422 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5424 const uint32_t *gbp
= data_
;
5427 size_t vxlan_opts_ofs
;
5429 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5430 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5431 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5436 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5438 const struct geneve_scan
*geneve
= data_
;
5440 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5445 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5447 const struct erspan_metadata
*md
= data_
;
5449 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5454 gtpu_to_attr(struct ofpbuf
*a
, const void *data_
)
5456 const struct gtpu_metadata
*md
= data_
;
5458 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GTPU_OPTS
, md
,
5462 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5464 unsigned long call_fn = (unsigned long)FUNC; \
5466 typedef void (*fn)(struct ofpbuf *, const void *); \
5468 func(BUF, &(DATA)); \
5470 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5474 #define SCAN_IF(NAME) \
5475 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5476 const char *start = s; \
5481 /* Usually no special initialization is needed. */
5482 #define SCAN_BEGIN(NAME, TYPE) \
5485 memset(&skey, 0, sizeof skey); \
5486 memset(&smask, 0, sizeof smask); \
5490 /* Init as fully-masked as mask will not be scanned. */
5491 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5494 memset(&skey, 0, sizeof skey); \
5495 memset(&smask, 0xff, sizeof smask); \
5499 /* VLAN needs special initialization. */
5500 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5502 TYPE skey = KEY_INIT; \
5503 TYPE smask = MASK_INIT; \
5507 /* Scan unnamed entry as 'TYPE' */
5508 #define SCAN_TYPE(TYPE, KEY, MASK) \
5509 len = scan_##TYPE(s, KEY, MASK); \
5515 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5516 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5517 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5518 s += strlen(NAME); \
5519 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5523 #define SCAN_FINISH() \
5524 } while (*s++ == ',' && len != 0); \
5525 if (s[-1] != ')') { \
5529 #define SCAN_FINISH_SINGLE() \
5531 if (*s++ != ')') { \
5535 /* Beginning of nested attribute. */
5536 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5538 size_t key_offset, mask_offset = 0; \
5539 key_offset = nl_msg_start_nested(key, ATTR); \
5541 mask_offset = nl_msg_start_nested(mask, ATTR); \
5546 #define SCAN_END_NESTED() \
5548 nl_msg_end_nested(key, key_offset); \
5550 nl_msg_end_nested(mask, mask_offset); \
5555 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5556 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5558 memset(&skey, 0, sizeof skey); \
5559 memset(&smask, 0xff, sizeof smask); \
5560 s += strlen(NAME); \
5561 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5562 SCAN_PUT(ATTR, FUNC); \
5566 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5567 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5569 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5570 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5572 #define SCAN_PUT(ATTR, FUNC) \
5573 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5575 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5577 #define SCAN_END(ATTR) \
5579 SCAN_PUT(ATTR, NULL); \
5583 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5585 TYPE skey[CNT], smask[CNT]; \
5586 memset(&skey, 0, sizeof skey); \
5587 memset(&smask, 0, sizeof smask); \
5588 int idx = 0, cnt = CNT; \
5589 uint64_t fields = 0; \
5594 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5595 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5596 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5597 if (fields & (1UL << field)) { \
5599 if (++idx == cnt) { \
5603 s += strlen(NAME); \
5604 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5605 fields |= 1UL << field; \
5610 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5611 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5613 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5614 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5616 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5619 #define SCAN_END_ARRAY(ATTR) \
5624 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5628 #define SCAN_END_SINGLE(ATTR) \
5629 SCAN_FINISH_SINGLE(); \
5630 SCAN_PUT(ATTR, NULL); \
5634 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5635 SCAN_BEGIN(NAME, TYPE) { \
5636 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5637 } SCAN_END_SINGLE(ATTR)
5639 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5640 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5641 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5642 } SCAN_END_SINGLE(ATTR)
5644 /* scan_port needs one extra argument. */
5645 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5646 SCAN_BEGIN(NAME, TYPE) { \
5647 len = scan_port(s, &skey, &smask, \
5648 context->port_names); \
5653 } SCAN_END_SINGLE(ATTR)
5656 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5657 struct ofpbuf
*mask
)
5659 if (strncmp(s
, "nsh(", 4) == 0) {
5660 const char *start
= s
;
5662 struct ovs_key_nsh skey
, smask
;
5663 uint32_t spi
= 0, spi_mask
= 0;
5664 uint8_t si
= 0, si_mask
= 0;
5668 memset(&skey
, 0, sizeof skey
);
5669 memset(&smask
, 0, sizeof smask
);
5673 if (strncmp(s
, "flags=", 6) == 0) {
5675 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5683 if (strncmp(s
, "mdtype=", 7) == 0) {
5685 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5693 if (strncmp(s
, "np=", 3) == 0) {
5695 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5703 if (strncmp(s
, "spi=", 4) == 0) {
5705 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5713 if (strncmp(s
, "si=", 3) == 0) {
5715 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5723 if (strncmp(s
, "c1=", 3) == 0) {
5725 len
= scan_be32(s
, &skey
.context
[0],
5726 mask
? &smask
.context
[0] : NULL
);
5734 if (strncmp(s
, "c2=", 3) == 0) {
5736 len
= scan_be32(s
, &skey
.context
[1],
5737 mask
? &smask
.context
[1] : NULL
);
5745 if (strncmp(s
, "c3=", 3) == 0) {
5747 len
= scan_be32(s
, &skey
.context
[2],
5748 mask
? &smask
.context
[2] : NULL
);
5756 if (strncmp(s
, "c4=", 3) == 0) {
5758 len
= scan_be32(s
, &skey
.context
[3],
5759 mask
? &smask
.context
[3] : NULL
);
5766 } while (*s
++ == ',' && len
!= 0);
5771 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5772 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5774 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5776 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5784 parse_odp_key_mask_attr(struct parse_odp_context
*context
, const char *s
,
5785 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5791 if (context
->depth
== MAX_ODP_NESTED
) {
5794 retval
= parse_odp_key_mask_attr__(context
, s
, key
, mask
);
5803 parse_odp_key_mask_attr__(struct parse_odp_context
*context
, const char *s
,
5804 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5806 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5807 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5808 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5809 OVS_KEY_ATTR_RECIRC_ID
);
5810 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5812 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5813 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5814 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5815 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5817 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5818 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5819 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5820 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5821 SCAN_FIELD("tp_src=", be16
, src_port
);
5822 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5823 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5825 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5826 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5827 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5828 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5829 SCAN_FIELD("tp_src=", be16
, src_port
);
5830 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5831 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5833 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5834 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5835 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5836 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5837 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5838 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5839 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5840 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5841 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5842 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5843 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5845 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5846 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5848 SCAN_FIELD_NESTED_FUNC("gtpu(", struct gtpu_metadata
, gtpu_metadata
,
5850 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5851 } SCAN_END_NESTED();
5853 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5855 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5856 SCAN_FIELD("src=", eth
, eth_src
);
5857 SCAN_FIELD("dst=", eth
, eth_dst
);
5858 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5860 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5861 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5862 SCAN_FIELD("vid=", vid
, tci
);
5863 SCAN_FIELD("pcp=", pcp
, tci
);
5864 SCAN_FIELD("cfi=", cfi
, tci
);
5865 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5867 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5869 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5870 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5871 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5872 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5873 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5874 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5876 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5877 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5878 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5879 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5880 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5881 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5882 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5883 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5885 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5886 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5887 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5888 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5889 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5890 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5891 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5892 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5893 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5895 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5896 SCAN_FIELD("src=", be16
, tcp_src
);
5897 SCAN_FIELD("dst=", be16
, tcp_dst
);
5898 } SCAN_END(OVS_KEY_ATTR_TCP
);
5900 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5902 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5903 SCAN_FIELD("src=", be16
, udp_src
);
5904 SCAN_FIELD("dst=", be16
, udp_dst
);
5905 } SCAN_END(OVS_KEY_ATTR_UDP
);
5907 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5908 SCAN_FIELD("src=", be16
, sctp_src
);
5909 SCAN_FIELD("dst=", be16
, sctp_dst
);
5910 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5912 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5913 SCAN_FIELD("type=", u8
, icmp_type
);
5914 SCAN_FIELD("code=", u8
, icmp_code
);
5915 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5917 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5918 SCAN_FIELD("type=", u8
, icmpv6_type
);
5919 SCAN_FIELD("code=", u8
, icmpv6_code
);
5920 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5922 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5923 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5924 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5925 SCAN_FIELD("op=", be16
, arp_op
);
5926 SCAN_FIELD("sha=", eth
, arp_sha
);
5927 SCAN_FIELD("tha=", eth
, arp_tha
);
5928 } SCAN_END(OVS_KEY_ATTR_ARP
);
5930 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5931 SCAN_FIELD("target=", in6_addr
, nd_target
);
5932 SCAN_FIELD("sll=", eth
, nd_sll
);
5933 SCAN_FIELD("tll=", eth
, nd_tll
);
5934 } SCAN_END(OVS_KEY_ATTR_ND
);
5936 SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions
) {
5937 SCAN_FIELD("nd_reserved=", be32
, nd_reserved
);
5938 SCAN_FIELD("nd_options_type=", u8
, nd_options_type
);
5939 } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS
);
5941 struct packet_type
{
5945 SCAN_BEGIN("packet_type(", struct packet_type
) {
5946 SCAN_FIELD("ns=", be16
, ns
);
5947 SCAN_FIELD("id=", be16
, id
);
5948 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5950 /* nsh is nested, it needs special process */
5951 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5958 /* Encap open-coded. */
5959 if (!strncmp(s
, "encap(", 6)) {
5960 const char *start
= s
;
5961 size_t encap
, encap_mask
= 0;
5963 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5965 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5972 s
+= strspn(s
, delimiters
);
5975 } else if (*s
== ')') {
5979 retval
= parse_odp_key_mask_attr(context
, s
, key
, mask
);
5984 if (nl_attr_oversized(key
->size
- encap
- NLA_HDRLEN
)) {
5991 nl_msg_end_nested(key
, encap
);
5993 nl_msg_end_nested(mask
, encap_mask
);
6002 /* Parses the string representation of a datapath flow key, in the format
6003 * output by odp_flow_key_format(). Returns 0 if successful, otherwise a
6004 * positive errno value. On success, stores NULL into '*errorp' and the flow
6005 * key is appended to 'key' as a series of Netlink attributes. On failure,
6006 * stores a malloc()'d error message in '*errorp' without changing the data in
6007 * 'key'. Either way, 'key''s data might be reallocated.
6009 * If 'port_names' is nonnull, it points to an simap that maps from a port name
6010 * to a port number. (Port names may be used instead of port numbers in
6013 * On success, the attributes appended to 'key' are individually syntactically
6014 * valid, but they may not be valid as a sequence. 'key' might, for example,
6015 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
6017 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
6018 struct ofpbuf
*key
, struct ofpbuf
*mask
,
6025 const size_t old_size
= key
->size
;
6026 struct parse_odp_context context
= (struct parse_odp_context
) {
6027 .port_names
= port_names
,
6032 s
+= strspn(s
, delimiters
);
6039 retval
= odp_ufid_from_string(s
, &ufid
);
6042 *errorp
= xasprintf("syntax error at %s", s
);
6044 key
->size
= old_size
;
6046 } else if (retval
> 0) {
6048 s
+= s
[0] == ' ' ? 1 : 0;
6051 retval
= parse_odp_key_mask_attr(&context
, s
, key
, mask
);
6054 *errorp
= xasprintf("syntax error at %s", s
);
6056 key
->size
= old_size
;
6066 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
6069 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
6070 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
6071 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
6072 * must use a zero mask for the netlink frag field, and all ones mask
6074 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
6076 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
6077 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
6078 : OVS_FRAG_TYPE_FIRST
;
6081 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
6082 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
6083 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
6085 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
6087 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
6089 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
6091 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
6092 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
6093 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
6094 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
6095 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
6097 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
6100 /* These share the same layout. */
6102 struct ovs_key_tcp tcp
;
6103 struct ovs_key_udp udp
;
6104 struct ovs_key_sctp sctp
;
6107 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
6108 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
6111 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
6112 bool export_mask
, struct ofpbuf
*buf
)
6114 /* New "struct flow" fields that are visible to the datapath (including all
6115 * data fields) should be translated into equivalent datapath flow fields
6116 * here (you will have to add a OVS_KEY_ATTR_* for them). */
6117 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
6119 struct ovs_key_ethernet
*eth_key
;
6120 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
6122 const struct flow
*flow
= parms
->flow
;
6123 const struct flow
*mask
= parms
->mask
;
6124 const struct flow
*data
= export_mask
? mask
: flow
;
6126 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
6128 if (flow_tnl_dst_is_set(&flow
->tunnel
) ||
6129 flow_tnl_src_is_set(&flow
->tunnel
) || export_mask
) {
6130 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
6131 parms
->key_buf
, NULL
);
6134 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
6136 if (parms
->support
.ct_state
) {
6137 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
6138 ovs_to_odp_ct_state(data
->ct_state
));
6140 if (parms
->support
.ct_zone
) {
6141 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
6143 if (parms
->support
.ct_mark
) {
6144 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
6146 if (parms
->support
.ct_label
) {
6147 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
6148 sizeof(data
->ct_label
));
6150 if (flow
->ct_nw_proto
) {
6151 if (parms
->support
.ct_orig_tuple
6152 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6153 struct ovs_key_ct_tuple_ipv4
*ct
;
6155 /* 'struct ovs_key_ct_tuple_ipv4' has padding, clear it. */
6156 ct
= nl_msg_put_unspec_zero(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6158 ct
->ipv4_src
= data
->ct_nw_src
;
6159 ct
->ipv4_dst
= data
->ct_nw_dst
;
6160 ct
->src_port
= data
->ct_tp_src
;
6161 ct
->dst_port
= data
->ct_tp_dst
;
6162 ct
->ipv4_proto
= data
->ct_nw_proto
;
6163 } else if (parms
->support
.ct_orig_tuple6
6164 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6165 struct ovs_key_ct_tuple_ipv6
*ct
;
6167 /* 'struct ovs_key_ct_tuple_ipv6' has padding, clear it. */
6168 ct
= nl_msg_put_unspec_zero(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6170 ct
->ipv6_src
= data
->ct_ipv6_src
;
6171 ct
->ipv6_dst
= data
->ct_ipv6_dst
;
6172 ct
->src_port
= data
->ct_tp_src
;
6173 ct
->dst_port
= data
->ct_tp_dst
;
6174 ct
->ipv6_proto
= data
->ct_nw_proto
;
6177 if (parms
->support
.recirc
) {
6178 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
6179 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
6182 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
6183 * is not the magical value "ODPP_NONE". */
6184 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
6185 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
6188 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
6190 if (OVS_UNLIKELY(parms
->probe
)) {
6191 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
6193 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
6196 /* Conditionally add L2 attributes for Ethernet packets */
6197 if (flow
->packet_type
== htonl(PT_ETH
)) {
6198 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
6200 get_ethernet_key(data
, eth_key
);
6202 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
6203 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
6205 if (flow
->vlans
[encaps
].tci
== htons(0)) {
6206 if (eth_type_vlan(flow
->dl_type
)) {
6207 /* If VLAN was truncated the tpid is in dl_type */
6208 tpid
= flow
->dl_type
;
6215 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
6217 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
6219 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
6220 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
6221 if (flow
->vlans
[encaps
].tci
== htons(0)) {
6227 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6228 /* For backwards compatibility with kernels that don't support
6229 * wildcarding, the following convention is used to encode the
6230 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
6233 * -------- -------- -------
6234 * >0x5ff 0xffff Specified Ethernet II Ethertype.
6235 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
6236 * <none> 0xffff Any non-Ethernet II frame (except valid
6237 * 802.3 SNAP packet with valid eth_type).
6240 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
6245 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
6247 if (eth_type_vlan(flow
->dl_type
)) {
6251 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6252 struct ovs_key_ipv4
*ipv4_key
;
6254 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
6256 get_ipv4_key(data
, ipv4_key
, export_mask
);
6257 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6258 struct ovs_key_ipv6
*ipv6_key
;
6260 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
6262 get_ipv6_key(data
, ipv6_key
, export_mask
);
6263 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6264 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6265 struct ovs_key_arp
*arp_key
;
6267 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
6269 get_arp_key(data
, arp_key
);
6270 } else if (eth_type_mpls(flow
->dl_type
)) {
6271 struct ovs_key_mpls
*mpls_key
;
6274 n
= flow_count_mpls_labels(flow
, NULL
);
6276 n
= MIN(n
, parms
->support
.max_mpls_depth
);
6278 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
6279 n
* sizeof *mpls_key
);
6280 for (i
= 0; i
< n
; i
++) {
6281 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
6283 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6284 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
6287 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6288 if (flow
->nw_proto
== IPPROTO_TCP
) {
6289 union ovs_key_tp
*tcp_key
;
6291 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
6293 get_tp_key(data
, tcp_key
);
6294 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
6295 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
6297 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
6298 union ovs_key_tp
*udp_key
;
6300 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
6302 get_tp_key(data
, udp_key
);
6303 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
6304 union ovs_key_tp
*sctp_key
;
6306 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
6308 get_tp_key(data
, sctp_key
);
6309 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
6310 && flow
->nw_proto
== IPPROTO_ICMP
) {
6311 struct ovs_key_icmp
*icmp_key
;
6313 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
6315 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
6316 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
6317 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6318 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
6319 struct ovs_key_icmpv6
*icmpv6_key
;
6321 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
6322 sizeof *icmpv6_key
);
6323 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
6324 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
6326 if (is_nd(flow
, NULL
)
6327 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
6328 * type and code are 8 bits wide. Therefore, an exact match
6329 * looks like htons(0xff), not htons(0xffff). See
6330 * xlate_wc_finish() for details. */
6331 && (!export_mask
|| (data
->tp_src
== htons(0xff)
6332 && data
->tp_dst
== htons(0xff)))) {
6333 struct ovs_key_nd
*nd_key
;
6334 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
6336 nd_key
->nd_target
= data
->nd_target
;
6337 nd_key
->nd_sll
= data
->arp_sha
;
6338 nd_key
->nd_tll
= data
->arp_tha
;
6340 /* Add ND Extensions Attr only if supported and reserved field
6341 * or options type is set. */
6342 if (parms
->support
.nd_ext
) {
6343 struct ovs_key_nd_extensions
*nd_ext_key
;
6345 if (data
->igmp_group_ip4
!= 0 || data
->tcp_flags
!= 0) {
6346 nd_ext_key
= nl_msg_put_unspec_uninit(buf
,
6347 OVS_KEY_ATTR_ND_EXTENSIONS
,
6348 sizeof *nd_ext_key
);
6349 nd_ext_key
->nd_reserved
= data
->igmp_group_ip4
;
6350 nd_ext_key
->nd_options_type
= ntohs(data
->tcp_flags
);
6358 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
6359 if (encap
[encaps
]) {
6360 nl_msg_end_nested(buf
, encap
[encaps
]);
6365 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6367 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6368 * capable of being expanded to allow for that much space. */
6370 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
6373 odp_flow_key_from_flow__(parms
, false, buf
);
6376 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6379 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6380 * capable of being expanded to allow for that much space. */
6382 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
6385 odp_flow_key_from_flow__(parms
, true, buf
);
6388 /* Generate ODP flow key from the given packet metadata */
6390 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
6392 const struct pkt_metadata
*md
= &packet
->md
;
6394 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
6397 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, md
->dp_hash
);
6400 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
6401 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
6404 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
6407 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
6408 ovs_to_odp_ct_state(md
->ct_state
));
6410 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
6413 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
6415 if (!ovs_u128_is_zero(md
->ct_label
)) {
6416 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
6417 sizeof(md
->ct_label
));
6419 if (md
->ct_orig_tuple_ipv6
) {
6420 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
6421 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6422 &md
->ct_orig_tuple
.ipv6
,
6423 sizeof md
->ct_orig_tuple
.ipv6
);
6426 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
6427 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6428 &md
->ct_orig_tuple
.ipv4
,
6429 sizeof md
->ct_orig_tuple
.ipv4
);
6434 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6435 * value "ODPP_NONE". */
6436 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
6437 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
6440 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6441 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
6442 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
6443 pt_ns_type_be(packet
->packet_type
));
6447 /* Generate packet metadata from the given ODP flow key. */
6449 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
6450 struct dp_packet
*packet
)
6452 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6453 const struct nlattr
*nla
;
6454 struct pkt_metadata
*md
= &packet
->md
;
6455 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
6456 ovs_be16 ethertype
= 0;
6459 pkt_metadata_init(md
, ODPP_NONE
);
6461 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6462 enum ovs_key_attr type
= nl_attr_type(nla
);
6463 size_t len
= nl_attr_get_size(nla
);
6464 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6465 OVS_KEY_ATTR_MAX
, type
);
6467 if (len
!= expected_len
&& expected_len
>= 0) {
6472 case OVS_KEY_ATTR_RECIRC_ID
:
6473 md
->recirc_id
= nl_attr_get_u32(nla
);
6475 case OVS_KEY_ATTR_DP_HASH
:
6476 md
->dp_hash
= nl_attr_get_u32(nla
);
6478 case OVS_KEY_ATTR_PRIORITY
:
6479 md
->skb_priority
= nl_attr_get_u32(nla
);
6481 case OVS_KEY_ATTR_SKB_MARK
:
6482 md
->pkt_mark
= nl_attr_get_u32(nla
);
6484 case OVS_KEY_ATTR_CT_STATE
:
6485 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
6487 case OVS_KEY_ATTR_CT_ZONE
:
6488 md
->ct_zone
= nl_attr_get_u16(nla
);
6490 case OVS_KEY_ATTR_CT_MARK
:
6491 md
->ct_mark
= nl_attr_get_u32(nla
);
6493 case OVS_KEY_ATTR_CT_LABELS
: {
6494 md
->ct_label
= nl_attr_get_u128(nla
);
6497 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6498 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6499 md
->ct_orig_tuple
.ipv4
= *ct
;
6500 md
->ct_orig_tuple_ipv6
= false;
6503 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6504 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6506 md
->ct_orig_tuple
.ipv6
= *ct
;
6507 md
->ct_orig_tuple_ipv6
= true;
6510 case OVS_KEY_ATTR_TUNNEL
: {
6511 enum odp_key_fitness res
;
6513 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
, NULL
);
6514 if (res
== ODP_FIT_ERROR
) {
6515 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6519 case OVS_KEY_ATTR_IN_PORT
:
6520 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6522 case OVS_KEY_ATTR_ETHERNET
:
6523 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6524 packet_type
= htonl(PT_ETH
);
6526 case OVS_KEY_ATTR_ETHERTYPE
:
6527 ethertype
= nl_attr_get_be16(nla
);
6529 case OVS_KEY_ATTR_UNSPEC
:
6530 case OVS_KEY_ATTR_ENCAP
:
6531 case OVS_KEY_ATTR_VLAN
:
6532 case OVS_KEY_ATTR_IPV4
:
6533 case OVS_KEY_ATTR_IPV6
:
6534 case OVS_KEY_ATTR_TCP
:
6535 case OVS_KEY_ATTR_UDP
:
6536 case OVS_KEY_ATTR_ICMP
:
6537 case OVS_KEY_ATTR_ICMPV6
:
6538 case OVS_KEY_ATTR_ARP
:
6539 case OVS_KEY_ATTR_ND
:
6540 case OVS_KEY_ATTR_ND_EXTENSIONS
:
6541 case OVS_KEY_ATTR_SCTP
:
6542 case OVS_KEY_ATTR_TCP_FLAGS
:
6543 case OVS_KEY_ATTR_MPLS
:
6544 case OVS_KEY_ATTR_PACKET_TYPE
:
6545 case OVS_KEY_ATTR_NSH
:
6546 case __OVS_KEY_ATTR_MAX
:
6552 if (packet_type
== htonl(PT_ETH
)) {
6553 packet
->packet_type
= htonl(PT_ETH
);
6554 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6555 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6558 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6562 /* Places the hash of the 'key_len' bytes starting at 'key' into '*hash'.
6563 * Generated value has format of random UUID. */
6565 odp_flow_key_hash(const void *key
, size_t key_len
, ovs_u128
*hash
)
6567 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
6568 static uint32_t secret
;
6570 if (ovsthread_once_start(&once
)) {
6571 secret
= random_uint32();
6572 ovsthread_once_done(&once
);
6574 hash_bytes128(key
, key_len
, secret
, hash
);
6575 uuid_set_bits_v4((struct uuid
*)hash
);
6579 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6580 uint64_t attrs
, int out_of_range_attr
,
6581 const struct nlattr
*key
, size_t key_len
)
6586 if (VLOG_DROP_DBG(rl
)) {
6591 for (i
= 0; i
< 64; i
++) {
6592 if (attrs
& (UINT64_C(1) << i
)) {
6593 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6595 ds_put_format(&s
, " %s",
6596 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6599 if (out_of_range_attr
) {
6600 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6603 ds_put_cstr(&s
, ": ");
6604 odp_flow_key_format(key
, key_len
, &s
);
6606 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6611 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6613 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6616 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6619 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6620 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6621 return 0xff; /* Error. */
6624 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6625 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6626 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6629 /* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6630 * must have OVS_KEY_ATTR_MAX + 1 elements. Stores each attribute in 'key'
6631 * into the corresponding element of 'attrs'.
6633 * Stores a bitmask of the attributes' indexes found in 'key' into
6634 * '*present_attrsp'.
6636 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6637 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6639 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6640 * error message in '*errorp'. */
6642 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6643 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6644 int *out_of_range_attrp
, char **errorp
)
6646 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6647 const struct nlattr
*nla
;
6648 uint64_t present_attrs
;
6651 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6653 *out_of_range_attrp
= 0;
6654 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6655 uint16_t type
= nl_attr_type(nla
);
6656 size_t len
= nl_attr_get_size(nla
);
6657 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6658 OVS_KEY_ATTR_MAX
, type
);
6660 if (len
!= expected_len
&& expected_len
>= 0) {
6661 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6663 odp_parse_error(&rl
, errorp
, "attribute %s has length %"PRIuSIZE
" "
6664 "but should have length %d",
6665 ovs_key_attr_to_string(type
, namebuf
,
6671 if (type
> OVS_KEY_ATTR_MAX
) {
6672 *out_of_range_attrp
= type
;
6674 if (present_attrs
& (UINT64_C(1) << type
)) {
6675 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6677 odp_parse_error(&rl
, errorp
,
6678 "duplicate %s attribute in flow key",
6679 ovs_key_attr_to_string(type
, namebuf
,
6684 present_attrs
|= UINT64_C(1) << type
;
6689 odp_parse_error(&rl
, errorp
, "trailing garbage in flow key");
6693 *present_attrsp
= present_attrs
;
6697 static enum odp_key_fitness
6698 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6699 uint64_t expected_attrs
,
6700 const struct nlattr
*key
, size_t key_len
)
6702 uint64_t missing_attrs
;
6703 uint64_t extra_attrs
;
6705 missing_attrs
= expected_attrs
& ~present_attrs
;
6706 if (missing_attrs
) {
6707 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6708 log_odp_key_attributes(&rl
, "expected but not present",
6709 missing_attrs
, 0, key
, key_len
);
6710 return ODP_FIT_TOO_LITTLE
;
6713 extra_attrs
= present_attrs
& ~expected_attrs
;
6714 if (extra_attrs
|| out_of_range_attr
) {
6715 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6716 log_odp_key_attributes(&rl
, "present but not expected",
6717 extra_attrs
, out_of_range_attr
, key
, key_len
);
6718 return ODP_FIT_TOO_MUCH
;
6721 return ODP_FIT_PERFECT
;
6724 /* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
6725 * attributes in the bit-mask 'present_attrs' are present. Returns true if
6726 * successful, false on failure.
6728 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6729 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6730 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6731 * previously parsed flow key.
6733 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6734 * error message in '*errorp'. */
6736 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6737 uint64_t present_attrs
, uint64_t *expected_attrs
,
6738 struct flow
*flow
, const struct flow
*src_flow
,
6741 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6742 bool is_mask
= flow
!= src_flow
;
6744 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6745 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6746 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6747 odp_parse_error(&rl
, errorp
,
6748 "invalid Ethertype %"PRIu16
" in flow key",
6749 ntohs(flow
->dl_type
));
6752 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6753 flow
->dl_type
!= htons(0xffff)) {
6754 odp_parse_error(&rl
, errorp
, "can't bitwise match non-Ethernet II "
6755 "\"Ethertype\" %#"PRIx16
" (with mask %#"PRIx16
")",
6756 ntohs(src_flow
->dl_type
), ntohs(flow
->dl_type
));
6759 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6762 /* Default ethertype for well-known L3 packets. */
6763 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6764 flow
->dl_type
= htons(ETH_TYPE_IP
);
6765 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6766 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6767 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6768 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6770 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6772 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6773 /* dl_type is mandatory for non-Ethernet packets */
6774 flow
->dl_type
= htons(0xffff);
6775 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6776 /* See comments in odp_flow_key_from_flow__(). */
6777 odp_parse_error(&rl
, errorp
,
6778 "mask expected for non-Ethernet II frame");
6785 /* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
6786 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
6787 * present. The caller also indicates an out-of-range attribute
6788 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
6789 * cannot be perfect).
6791 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6792 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6793 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6794 * previously parsed flow key.
6796 * Returns fitness based on any discrepancies between present and expected
6797 * attributes, except that a 'need_check' of false overrides this.
6799 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6800 * error message in '*errorp'. 'key' and 'key_len' are just used for error
6801 * reporting in this case. */
6802 static enum odp_key_fitness
6803 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6804 uint64_t present_attrs
, int out_of_range_attr
,
6805 uint64_t *expected_attrs
, struct flow
*flow
,
6806 const struct nlattr
*key
, size_t key_len
,
6807 const struct flow
*src_flow
, bool need_check
, char **errorp
)
6809 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6810 bool is_mask
= src_flow
!= flow
;
6811 const void *check_start
= NULL
;
6812 size_t check_len
= 0;
6813 enum ovs_key_attr expected_bit
= 0xff;
6815 if (eth_type_mpls(src_flow
->dl_type
)) {
6816 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6817 *expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6819 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6820 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6821 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6822 int n
= size
/ sizeof(ovs_be32
);
6825 if (!size
|| size
% sizeof(ovs_be32
)) {
6826 odp_parse_error(&rl
, errorp
,
6827 "MPLS LSEs have invalid length %"PRIuSIZE
,
6829 return ODP_FIT_ERROR
;
6831 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6832 odp_parse_error(&rl
, errorp
,
6833 "unexpected MPLS Ethertype mask %x"PRIx16
,
6834 ntohs(flow
->dl_type
));
6835 return ODP_FIT_ERROR
;
6838 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6839 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6841 if (n
> FLOW_MAX_MPLS_LABELS
) {
6842 return ODP_FIT_TOO_MUCH
;
6846 /* BOS may be set only in the innermost label. */
6847 for (i
= 0; i
< n
- 1; i
++) {
6848 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6849 odp_parse_error(&rl
, errorp
,
6850 "MPLS BOS set in non-innermost label");
6851 return ODP_FIT_ERROR
;
6855 /* BOS must be set in the innermost label. */
6856 if (n
< FLOW_MAX_MPLS_LABELS
6857 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6858 return ODP_FIT_TOO_LITTLE
;
6864 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6866 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6868 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6869 const struct ovs_key_ipv4
*ipv4_key
;
6871 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6872 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6873 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6874 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV4 has invalid "
6875 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6876 return ODP_FIT_ERROR
;
6880 check_start
= ipv4_key
;
6881 check_len
= sizeof *ipv4_key
;
6882 expected_bit
= OVS_KEY_ATTR_IPV4
;
6885 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6887 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6889 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6890 const struct ovs_key_ipv6
*ipv6_key
;
6892 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6893 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6894 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6895 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV6 has invalid "
6896 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6897 return ODP_FIT_ERROR
;
6900 check_start
= ipv6_key
;
6901 check_len
= sizeof *ipv6_key
;
6902 expected_bit
= OVS_KEY_ATTR_IPV6
;
6905 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6906 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6908 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6910 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6911 const struct ovs_key_arp
*arp_key
;
6913 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6914 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6915 odp_parse_error(&rl
, errorp
,
6916 "unsupported ARP opcode %"PRIu16
" in flow "
6917 "key", ntohs(arp_key
->arp_op
));
6918 return ODP_FIT_ERROR
;
6920 put_arp_key(arp_key
, flow
);
6922 check_start
= arp_key
;
6923 check_len
= sizeof *arp_key
;
6924 expected_bit
= OVS_KEY_ATTR_ARP
;
6927 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6929 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6931 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6932 if (odp_nsh_key_from_attr__(attrs
[OVS_KEY_ATTR_NSH
],
6933 is_mask
, &flow
->nsh
,
6934 NULL
, errorp
) == ODP_FIT_ERROR
) {
6935 return ODP_FIT_ERROR
;
6938 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6939 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6940 expected_bit
= OVS_KEY_ATTR_NSH
;
6946 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6947 if (!is_all_zeros(check_start
, check_len
) &&
6948 flow
->dl_type
!= htons(0xffff)) {
6949 odp_parse_error(&rl
, errorp
, "unexpected L3 matching with "
6950 "masked Ethertype %#"PRIx16
"/%#"PRIx16
,
6951 ntohs(src_flow
->dl_type
),
6952 ntohs(flow
->dl_type
));
6953 return ODP_FIT_ERROR
;
6955 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6959 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6960 if (src_flow
->nw_proto
== IPPROTO_TCP
6961 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6962 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6963 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6965 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6967 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6968 const union ovs_key_tp
*tcp_key
;
6970 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6971 put_tp_key(tcp_key
, flow
);
6972 expected_bit
= OVS_KEY_ATTR_TCP
;
6974 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6975 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6976 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6978 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6979 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6980 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6981 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6983 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
6985 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
6986 const union ovs_key_tp
*udp_key
;
6988 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
6989 put_tp_key(udp_key
, flow
);
6990 expected_bit
= OVS_KEY_ATTR_UDP
;
6992 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
6993 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6994 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6995 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6997 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
6999 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
7000 const union ovs_key_tp
*sctp_key
;
7002 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
7003 put_tp_key(sctp_key
, flow
);
7004 expected_bit
= OVS_KEY_ATTR_SCTP
;
7006 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
7007 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
7008 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7010 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
7012 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
7013 const struct ovs_key_icmp
*icmp_key
;
7015 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
7016 flow
->tp_src
= htons(icmp_key
->icmp_type
);
7017 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
7018 expected_bit
= OVS_KEY_ATTR_ICMP
;
7020 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
7021 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
7022 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
7024 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
7026 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
7027 const struct ovs_key_icmpv6
*icmpv6_key
;
7029 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
7030 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
7031 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
7032 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
7033 if (is_nd(src_flow
, NULL
)) {
7035 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
7037 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
7038 const struct ovs_key_nd
*nd_key
;
7040 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
7041 flow
->nd_target
= nd_key
->nd_target
;
7042 flow
->arp_sha
= nd_key
->nd_sll
;
7043 flow
->arp_tha
= nd_key
->nd_tll
;
7045 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7046 * ICMP type and code are 8 bits wide. Therefore, an
7047 * exact match looks like htons(0xff), not
7048 * htons(0xffff). See xlate_wc_finish() for details.
7050 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
7051 (flow
->tp_src
!= htons(0xff) ||
7052 flow
->tp_dst
!= htons(0xff))) {
7053 odp_parse_error(&rl
, errorp
,
7054 "ICMP (src,dst) masks should be "
7055 "(0xff,0xff) but are actually "
7056 "(%#"PRIx16
",%#"PRIx16
")",
7057 ntohs(flow
->tp_src
),
7058 ntohs(flow
->tp_dst
));
7059 return ODP_FIT_ERROR
;
7061 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
7066 (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
)) {
7067 const struct ovs_key_nd_extensions
*nd_ext_key
;
7070 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
7074 nl_attr_get(attrs
[OVS_KEY_ATTR_ND_EXTENSIONS
]);
7075 flow
->igmp_group_ip4
= nd_ext_key
->nd_reserved
;
7076 flow
->tcp_flags
= htons(nd_ext_key
->nd_options_type
);
7079 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
7080 * ICMP type and code are 8 bits wide. Therefore, an
7081 * exact match looks like htons(0xff), not
7082 * htons(0xffff). See xlate_wc_finish() for details.
7084 if (!is_all_zeros(nd_ext_key
, sizeof *nd_ext_key
) &&
7085 (flow
->tp_src
!= htons(0xff) ||
7086 flow
->tp_dst
!= htons(0xff))) {
7087 return ODP_FIT_ERROR
;
7090 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
7096 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
7097 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
7098 /* OVS userspace parses the IGMP type, code, and group, but its
7099 * datapaths do not, so there is always missing information. */
7100 return ODP_FIT_TOO_LITTLE
;
7102 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
7103 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
7104 odp_parse_error(&rl
, errorp
, "flow matches on L4 ports but does "
7105 "not define an L4 protocol");
7106 return ODP_FIT_ERROR
;
7108 *expected_attrs
|= UINT64_C(1) << expected_bit
;
7113 return need_check
? check_expectations(present_attrs
, out_of_range_attr
,
7114 *expected_attrs
, key
, key_len
) : ODP_FIT_PERFECT
;
7117 /* Parse 802.1Q header then encapsulated L3 attributes. */
7118 static enum odp_key_fitness
7119 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
7120 uint64_t present_attrs
, int out_of_range_attr
,
7121 uint64_t expected_attrs
, struct flow
*flow
,
7122 const struct nlattr
*key
, size_t key_len
,
7123 const struct flow
*src_flow
, char **errorp
)
7125 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7126 bool is_mask
= src_flow
!= flow
;
7128 const struct nlattr
*encap
;
7129 enum odp_key_fitness encap_fitness
;
7130 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
7133 while (encaps
< flow_vlan_limit
&&
7135 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
7136 : eth_type_vlan(flow
->dl_type
))) {
7138 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
7139 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
7141 /* Calculate fitness of outer attributes. */
7143 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
7144 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
7146 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
7147 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
7149 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
7150 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
7153 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
7154 expected_attrs
, key
, key_len
);
7157 * Remove the TPID from dl_type since it's not the real Ethertype. */
7158 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
7159 flow
->dl_type
= htons(0);
7160 flow
->vlans
[encaps
].tci
=
7161 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
7162 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
7165 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
7166 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
7167 return ODP_FIT_TOO_LITTLE
;
7168 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
7169 /* Corner case for a truncated 802.1Q header. */
7170 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
7171 return ODP_FIT_TOO_MUCH
;
7174 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
7176 &rl
, errorp
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
7177 "but CFI bit is not set", ntohs(flow
->vlans
[encaps
].tci
));
7178 return ODP_FIT_ERROR
;
7181 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
7186 /* Now parse the encapsulated attributes. */
7187 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
7188 attrs
, &present_attrs
, &out_of_range_attr
,
7190 return ODP_FIT_ERROR
;
7194 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
7195 flow
, src_flow
, errorp
)) {
7196 return ODP_FIT_ERROR
;
7198 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
,
7202 src_flow
, false, errorp
);
7203 if (encap_fitness
!= ODP_FIT_PERFECT
) {
7204 return encap_fitness
;
7209 return check_expectations(present_attrs
, out_of_range_attr
,
7210 expected_attrs
, key
, key_len
);
7213 static enum odp_key_fitness
7214 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
7215 struct flow
*flow
, const struct flow
*src_flow
,
7218 /* New "struct flow" fields that are visible to the datapath (including all
7219 * data fields) should be translated from equivalent datapath flow fields
7220 * here (you will have to add a OVS_KEY_ATTR_* for them). */
7221 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
7223 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
7228 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
7229 uint64_t expected_attrs
;
7230 uint64_t present_attrs
;
7231 int out_of_range_attr
;
7232 bool is_mask
= src_flow
!= flow
;
7234 memset(flow
, 0, sizeof *flow
);
7236 /* Parse attributes. */
7237 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
7238 &out_of_range_attr
, errorp
)) {
7244 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
7245 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
7246 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
7247 } else if (is_mask
) {
7248 /* Always exact match recirc_id if it is not specified. */
7249 flow
->recirc_id
= UINT32_MAX
;
7252 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
7253 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
7254 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
7256 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
7257 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
7258 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
7261 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
7262 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
7263 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
7266 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
7267 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
7269 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
7270 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
7272 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
7273 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
7274 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
7276 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
7277 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
7278 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
7280 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
7281 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
7282 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
7284 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
7285 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
7286 flow
->ct_nw_src
= ct
->ipv4_src
;
7287 flow
->ct_nw_dst
= ct
->ipv4_dst
;
7288 flow
->ct_nw_proto
= ct
->ipv4_proto
;
7289 flow
->ct_tp_src
= ct
->src_port
;
7290 flow
->ct_tp_dst
= ct
->dst_port
;
7291 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
7293 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
7294 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
7296 flow
->ct_ipv6_src
= ct
->ipv6_src
;
7297 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
7298 flow
->ct_nw_proto
= ct
->ipv6_proto
;
7299 flow
->ct_tp_src
= ct
->src_port
;
7300 flow
->ct_tp_dst
= ct
->dst_port
;
7301 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
7304 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
7305 enum odp_key_fitness res
;
7307 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
7308 &flow
->tunnel
, errorp
);
7309 if (res
== ODP_FIT_ERROR
) {
7311 } else if (res
== ODP_FIT_PERFECT
) {
7312 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
7316 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
7317 flow
->in_port
.odp_port
7318 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
7319 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
7320 } else if (!is_mask
) {
7321 flow
->in_port
.odp_port
= ODPP_NONE
;
7324 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
7326 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
7327 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
7328 if (pt_ns(src_flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
7329 flow
->dl_type
= pt_ns_type_be(flow
->packet_type
);
7331 } else if (!is_mask
) {
7332 flow
->packet_type
= htonl(PT_ETH
);
7335 /* Check for Ethernet header. */
7336 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
7337 const struct ovs_key_ethernet
*eth_key
;
7339 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
7340 put_ethernet_key(eth_key
, flow
);
7342 flow
->packet_type
= htonl(PT_ETH
);
7344 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
7346 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
7347 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
7349 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
7352 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
7355 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7356 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
7357 src_flow
, errorp
)) {
7362 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
7363 : eth_type_vlan(src_flow
->dl_type
)) {
7364 fitness
= parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
7365 expected_attrs
, flow
, key
, key_len
,
7369 /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7371 flow
->vlans
[0].tpid
= htons(0xffff);
7372 flow
->vlans
[0].tci
= htons(0xffff);
7373 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
7374 flow
->vlans
[0].tci
= nl_attr_get_be16(
7375 attrs
[OVS_KEY_ATTR_VLAN
]);
7376 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
7379 fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
7380 &expected_attrs
, flow
, key
, key_len
,
7381 src_flow
, true, errorp
);
7385 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7386 if (fitness
== ODP_FIT_ERROR
&& (errorp
|| !VLOG_DROP_WARN(&rl
))) {
7387 struct ds s
= DS_EMPTY_INITIALIZER
;
7389 ds_put_cstr(&s
, "the flow mask in error is: ");
7390 odp_flow_key_format(key
, key_len
, &s
);
7391 ds_put_cstr(&s
, ", for the following flow key: ");
7392 flow_format(&s
, src_flow
, NULL
);
7394 ds_put_cstr(&s
, "the flow key in error is: ");
7395 odp_flow_key_format(key
, key_len
, &s
);
7398 char *old_error
= *errorp
;
7399 *errorp
= xasprintf("%s; %s", old_error
, ds_cstr(&s
));
7402 VLOG_WARN("%s", ds_cstr(&s
));
7409 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7410 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
7411 * 'key' fits our expectations for what a flow key should contain.
7413 * The 'in_port' will be the datapath's understanding of the port. The
7414 * caller will need to translate with odp_port_to_ofp_port() if the
7415 * OpenFlow port is needed.
7417 * This function doesn't take the packet itself as an argument because none of
7418 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
7419 * it is always possible to infer which additional attribute(s) should appear
7420 * by looking at the attributes for lower-level protocols, e.g. if the network
7421 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7422 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7425 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7426 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7427 * '*errorp', otherwise NULL. */
7428 enum odp_key_fitness
7429 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
7430 struct flow
*flow
, char **errorp
)
7432 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
, errorp
);
7435 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7436 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
7437 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7438 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
7439 * well 'key' fits our expectations for what a flow key should contain.
7441 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7442 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7443 * '*errorp', otherwise NULL. */
7444 enum odp_key_fitness
7445 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
7446 struct flow_wildcards
*mask
, const struct flow
*src_flow
,
7450 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
7451 &mask
->masks
, src_flow
, errorp
);
7457 /* A missing mask means that the flow should be exact matched.
7458 * Generate an appropriate exact wildcard for the flow. */
7459 flow_wildcards_init_for_packet(mask
, src_flow
);
7461 return ODP_FIT_PERFECT
;
7465 /* Converts the netlink formated key/mask to match.
7466 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7467 * disagree on the acceptable form of flow */
7469 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
7470 const struct nlattr
*mask
, size_t mask_len
,
7471 struct match
*match
)
7473 enum odp_key_fitness fitness
;
7475 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
, NULL
);
7477 /* This should not happen: it indicates that
7478 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
7479 * the acceptable form of a flow. Log the problem as an error,
7480 * with enough details to enable debugging. */
7481 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7483 if (!VLOG_DROP_ERR(&rl
)) {
7487 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
7488 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
7495 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
,
7498 /* This should not happen: it indicates that
7499 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
7500 * disagree on the acceptable form of a mask. Log the problem
7501 * as an error, with enough details to enable debugging. */
7502 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7504 if (!VLOG_DROP_ERR(&rl
)) {
7508 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
7510 VLOG_ERR("internal error parsing flow mask %s (%s)",
7511 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
7521 /* Returns 'fitness' as a string, for use in debug messages. */
7523 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
7526 case ODP_FIT_PERFECT
:
7528 case ODP_FIT_TOO_MUCH
:
7530 case ODP_FIT_TOO_LITTLE
:
7531 return "too_little";
7539 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7540 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
7541 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
7542 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
7543 * null, then the return value is not meaningful.) */
7545 odp_put_userspace_action(uint32_t pid
,
7546 const void *userdata
, size_t userdata_size
,
7547 odp_port_t tunnel_out_port
,
7548 bool include_actions
,
7549 struct ofpbuf
*odp_actions
)
7551 size_t userdata_ofs
;
7554 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
7555 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
7557 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
7559 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7560 * module before Linux 3.10 required the userdata to be exactly 8 bytes
7563 * - The kernel rejected shorter userdata with -ERANGE.
7565 * - The kernel silently dropped userdata beyond the first 8 bytes.
7567 * Thus, for maximum compatibility, always put at least 8 bytes. (We
7568 * separately disable features that required more than 8 bytes.) */
7569 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
7570 MAX(8, userdata_size
)),
7571 userdata
, userdata_size
);
7575 if (tunnel_out_port
!= ODPP_NONE
) {
7576 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
7579 if (include_actions
) {
7580 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
7582 nl_msg_end_nested(odp_actions
, offset
);
7584 return userdata_ofs
;
7588 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
7590 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
7594 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
7595 const struct eth_addr
*eth_src
,
7596 const struct eth_addr
*eth_dst
)
7598 struct ovs_action_push_eth eth
;
7600 memset(ð
, 0, sizeof eth
);
7602 eth
.addresses
.eth_src
= *eth_src
;
7605 eth
.addresses
.eth_dst
= *eth_dst
;
7608 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
7613 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
7614 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7616 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7617 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
7618 nl_msg_end_nested(odp_actions
, offset
);
7622 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
7623 struct ovs_action_push_tnl
*data
)
7625 int size
= offsetof(struct ovs_action_push_tnl
, header
);
7627 size
+= data
->header_len
;
7628 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
7632 /* The commit_odp_actions() function and its helpers. */
7635 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
7636 const void *key
, size_t key_size
)
7638 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7639 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
7640 nl_msg_end_nested(odp_actions
, offset
);
7643 /* Masked set actions have a mask following the data within the netlink
7644 * attribute. The unmasked bits in the data will be cleared as the data
7645 * is copied to the action. */
7647 commit_masked_set_action(struct ofpbuf
*odp_actions
,
7648 enum ovs_key_attr key_type
,
7649 const void *key_
, const void *mask_
, size_t key_size
)
7651 size_t offset
= nl_msg_start_nested(odp_actions
,
7652 OVS_ACTION_ATTR_SET_MASKED
);
7653 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
7654 const char *key
= key_
, *mask
= mask_
;
7656 memcpy(data
+ key_size
, mask
, key_size
);
7657 /* Clear unmasked bits while copying. */
7658 while (key_size
--) {
7659 *data
++ = *key
++ & *mask
++;
7661 nl_msg_end_nested(odp_actions
, offset
);
7664 /* If any of the flow key data that ODP actions can modify are different in
7665 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7666 * 'odp_actions' that change the flow tunneling information in key from
7667 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7668 * same way. In other words, operates the same as commit_odp_actions(), but
7669 * only on tunneling information. */
7671 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7672 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7674 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7675 * must have non-zero ipv6_dst. */
7676 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7677 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7680 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7681 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7685 struct offsetof_sizeof
{
7690 /* Compares each of the fields in 'key0' and 'key1'. The fields are specified
7691 * in 'offsetof_sizeof_arr', which is an array terminated by a 0-size field.
7692 * Returns true if all of the fields are equal, false if at least one differs.
7693 * As a side effect, for each field that is the same in 'key0' and 'key1',
7694 * zeros the corresponding bytes in 'mask'. */
7696 keycmp_mask(const void *key0
, const void *key1
,
7697 struct offsetof_sizeof
*offsetof_sizeof_arr
, void *mask
)
7699 bool differ
= false;
7701 for (int field
= 0 ; ; field
++) {
7702 int size
= offsetof_sizeof_arr
[field
].size
;
7703 int offset
= offsetof_sizeof_arr
[field
].offset
;
7708 char *pkey0
= ((char *)key0
) + offset
;
7709 char *pkey1
= ((char *)key1
) + offset
;
7710 char *pmask
= ((char *)mask
) + offset
;
7711 if (memcmp(pkey0
, pkey1
, size
) == 0) {
7712 memset(pmask
, 0, size
);
7722 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7723 const void *key
, void *base
, void *mask
, size_t size
,
7724 struct offsetof_sizeof
*offsetof_sizeof_arr
,
7725 struct ofpbuf
*odp_actions
)
7727 if (keycmp_mask(key
, base
, offsetof_sizeof_arr
, mask
)) {
7728 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7730 if (use_masked_set
&& !fully_masked
) {
7731 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7733 if (!fully_masked
) {
7734 memset(mask
, 0xff, size
);
7736 commit_set_action(odp_actions
, attr
, key
, size
);
7738 memcpy(base
, key
, size
);
7741 /* Mask bits are set when we have either read or set the corresponding
7742 * values. Masked bits will be exact-matched, no need to set them
7743 * if the value did not actually change. */
7749 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7751 eth
->eth_src
= flow
->dl_src
;
7752 eth
->eth_dst
= flow
->dl_dst
;
7756 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7758 flow
->dl_src
= eth
->eth_src
;
7759 flow
->dl_dst
= eth
->eth_dst
;
7763 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7764 struct ofpbuf
*odp_actions
,
7765 struct flow_wildcards
*wc
,
7768 struct ovs_key_ethernet key
, base
, mask
;
7769 struct offsetof_sizeof ovs_key_ethernet_offsetof_sizeof_arr
[] =
7770 OVS_KEY_ETHERNET_OFFSETOF_SIZEOF_ARR
;
7771 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7775 get_ethernet_key(flow
, &key
);
7776 get_ethernet_key(base_flow
, &base
);
7777 get_ethernet_key(&wc
->masks
, &mask
);
7779 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7780 &key
, &base
, &mask
, sizeof key
,
7781 ovs_key_ethernet_offsetof_sizeof_arr
, odp_actions
)) {
7782 put_ethernet_key(&base
, base_flow
);
7783 put_ethernet_key(&mask
, &wc
->masks
);
7788 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7789 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7791 int base_n
= flow_count_vlan_headers(base
);
7792 int flow_n
= flow_count_vlan_headers(flow
);
7793 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7795 /* Pop all mismatching vlan of base, push those of flow */
7796 for (; base_n
>= 0; base_n
--) {
7797 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7798 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7801 for (; flow_n
>= 0; flow_n
--) {
7802 struct ovs_action_push_vlan vlan
;
7804 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7805 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7806 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7807 &vlan
, sizeof vlan
);
7809 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7812 /* Wildcarding already done at action translation time. */
7814 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7815 struct ofpbuf
*odp_actions
)
7817 int base_n
= flow_count_mpls_labels(base
, NULL
);
7818 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7819 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7822 while (base_n
> common_n
) {
7823 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7824 /* If there is only one more LSE in base than there are common
7825 * between base and flow; and flow has at least one more LSE than
7826 * is common then the topmost LSE of base may be updated using
7828 struct ovs_key_mpls mpls_key
;
7830 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7831 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7832 &mpls_key
, sizeof mpls_key
);
7833 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7836 /* Otherwise, if there more LSEs in base than are common between
7837 * base and flow then pop the topmost one. */
7839 /* If all the LSEs are to be popped and this is not the outermost
7840 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7841 * POP_MPLS action instead of flow->dl_type.
7843 * This is because the POP_MPLS action requires its ethertype
7844 * argument to be an MPLS ethernet type but in this case
7845 * flow->dl_type will be a non-MPLS ethernet type.
7847 * When the final POP_MPLS action occurs it use flow->dl_type and
7848 * the and the resulting packet will have the desired dl_type. */
7849 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7850 dl_type
= htons(ETH_TYPE_MPLS
);
7852 dl_type
= flow
->dl_type
;
7854 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7855 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7860 /* If, after the above popping and setting, there are more LSEs in flow
7861 * than base then some LSEs need to be pushed. */
7862 while (base_n
< flow_n
) {
7863 struct ovs_action_push_mpls
*mpls
;
7865 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7866 OVS_ACTION_ATTR_PUSH_MPLS
,
7868 mpls
->mpls_ethertype
= flow
->dl_type
;
7869 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7870 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7871 * headers if the flow is restored later due to returning from a patch
7872 * port or group bucket. */
7873 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7874 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7880 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7882 ipv4
->ipv4_src
= flow
->nw_src
;
7883 ipv4
->ipv4_dst
= flow
->nw_dst
;
7884 ipv4
->ipv4_proto
= flow
->nw_proto
;
7885 ipv4
->ipv4_tos
= flow
->nw_tos
;
7886 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7887 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7891 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7893 flow
->nw_src
= ipv4
->ipv4_src
;
7894 flow
->nw_dst
= ipv4
->ipv4_dst
;
7895 flow
->nw_proto
= ipv4
->ipv4_proto
;
7896 flow
->nw_tos
= ipv4
->ipv4_tos
;
7897 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7898 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7902 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7903 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7906 struct ovs_key_ipv4 key
, mask
, base
;
7907 struct offsetof_sizeof ovs_key_ipv4_offsetof_sizeof_arr
[] =
7908 OVS_KEY_IPV4_OFFSETOF_SIZEOF_ARR
;
7910 /* Check that nw_proto and nw_frag remain unchanged. */
7911 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7912 flow
->nw_frag
== base_flow
->nw_frag
);
7914 get_ipv4_key(flow
, &key
, false);
7915 get_ipv4_key(base_flow
, &base
, false);
7916 get_ipv4_key(&wc
->masks
, &mask
, true);
7917 mask
.ipv4_proto
= 0; /* Not writeable. */
7918 mask
.ipv4_frag
= 0; /* Not writable. */
7920 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7921 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7922 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7925 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7926 ovs_key_ipv4_offsetof_sizeof_arr
, odp_actions
)) {
7927 put_ipv4_key(&base
, base_flow
, false);
7928 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
7929 put_ipv4_key(&mask
, &wc
->masks
, true);
7935 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7937 ipv6
->ipv6_src
= flow
->ipv6_src
;
7938 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7939 ipv6
->ipv6_label
= flow
->ipv6_label
;
7940 ipv6
->ipv6_proto
= flow
->nw_proto
;
7941 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7942 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7943 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7947 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7949 flow
->ipv6_src
= ipv6
->ipv6_src
;
7950 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7951 flow
->ipv6_label
= ipv6
->ipv6_label
;
7952 flow
->nw_proto
= ipv6
->ipv6_proto
;
7953 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7954 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7955 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
7959 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
7960 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7963 struct ovs_key_ipv6 key
, mask
, base
;
7964 struct offsetof_sizeof ovs_key_ipv6_offsetof_sizeof_arr
[] =
7965 OVS_KEY_IPV6_OFFSETOF_SIZEOF_ARR
;
7967 /* Check that nw_proto and nw_frag remain unchanged. */
7968 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7969 flow
->nw_frag
== base_flow
->nw_frag
);
7971 get_ipv6_key(flow
, &key
, false);
7972 get_ipv6_key(base_flow
, &base
, false);
7973 get_ipv6_key(&wc
->masks
, &mask
, true);
7974 mask
.ipv6_proto
= 0; /* Not writeable. */
7975 mask
.ipv6_frag
= 0; /* Not writable. */
7976 mask
.ipv6_label
&= htonl(IPV6_LABEL_MASK
); /* Not writable. */
7978 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7979 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7980 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
7983 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7984 ovs_key_ipv6_offsetof_sizeof_arr
, odp_actions
)) {
7985 put_ipv6_key(&base
, base_flow
, false);
7986 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
7987 put_ipv6_key(&mask
, &wc
->masks
, true);
7993 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
7995 /* ARP key has padding, clear it. */
7996 memset(arp
, 0, sizeof *arp
);
7998 arp
->arp_sip
= flow
->nw_src
;
7999 arp
->arp_tip
= flow
->nw_dst
;
8000 arp
->arp_op
= flow
->nw_proto
== UINT8_MAX
?
8001 OVS_BE16_MAX
: htons(flow
->nw_proto
);
8002 arp
->arp_sha
= flow
->arp_sha
;
8003 arp
->arp_tha
= flow
->arp_tha
;
8007 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
8009 flow
->nw_src
= arp
->arp_sip
;
8010 flow
->nw_dst
= arp
->arp_tip
;
8011 flow
->nw_proto
= ntohs(arp
->arp_op
);
8012 flow
->arp_sha
= arp
->arp_sha
;
8013 flow
->arp_tha
= arp
->arp_tha
;
8016 static enum slow_path_reason
8017 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
8018 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
8020 struct ovs_key_arp key
, mask
, base
;
8021 struct offsetof_sizeof ovs_key_arp_offsetof_sizeof_arr
[] =
8022 OVS_KEY_ARP_OFFSETOF_SIZEOF_ARR
;
8024 get_arp_key(flow
, &key
);
8025 get_arp_key(base_flow
, &base
);
8026 get_arp_key(&wc
->masks
, &mask
);
8028 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
8029 ovs_key_arp_offsetof_sizeof_arr
, odp_actions
)) {
8030 put_arp_key(&base
, base_flow
);
8031 put_arp_key(&mask
, &wc
->masks
);
8038 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
8040 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8041 icmp
->icmp_type
= ntohs(flow
->tp_src
);
8042 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
8046 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
8048 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
8049 flow
->tp_src
= htons(icmp
->icmp_type
);
8050 flow
->tp_dst
= htons(icmp
->icmp_code
);
8053 static enum slow_path_reason
8054 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
8055 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
8057 struct ovs_key_icmp key
, mask
, base
;
8058 struct offsetof_sizeof ovs_key_icmp_offsetof_sizeof_arr
[] =
8059 OVS_KEY_ICMP_OFFSETOF_SIZEOF_ARR
;
8060 enum ovs_key_attr attr
;
8062 if (is_icmpv4(flow
, NULL
)) {
8063 attr
= OVS_KEY_ATTR_ICMP
;
8064 } else if (is_icmpv6(flow
, NULL
)) {
8065 attr
= OVS_KEY_ATTR_ICMPV6
;
8070 get_icmp_key(flow
, &key
);
8071 get_icmp_key(base_flow
, &base
);
8072 get_icmp_key(&wc
->masks
, &mask
);
8074 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
,
8075 ovs_key_icmp_offsetof_sizeof_arr
, odp_actions
)) {
8076 put_icmp_key(&base
, base_flow
);
8077 put_icmp_key(&mask
, &wc
->masks
);
8084 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
8086 nd
->nd_target
= flow
->nd_target
;
8087 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8088 nd
->nd_sll
= flow
->arp_sha
;
8089 nd
->nd_tll
= flow
->arp_tha
;
8093 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
8095 flow
->nd_target
= nd
->nd_target
;
8096 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
8097 flow
->arp_sha
= nd
->nd_sll
;
8098 flow
->arp_tha
= nd
->nd_tll
;
8102 get_nd_extensions_key(const struct flow
*flow
,
8103 struct ovs_key_nd_extensions
*nd_ext
)
8105 /* ND Extensions key has padding, clear it. */
8106 memset(nd_ext
, 0, sizeof *nd_ext
);
8107 nd_ext
->nd_reserved
= flow
->igmp_group_ip4
;
8108 nd_ext
->nd_options_type
= ntohs(flow
->tcp_flags
);
8112 put_nd_extensions_key(const struct ovs_key_nd_extensions
*nd_ext
,
8115 flow
->igmp_group_ip4
= nd_ext
->nd_reserved
;
8116 flow
->tcp_flags
= htons(nd_ext
->nd_options_type
);
8119 static enum slow_path_reason
8120 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
8121 struct ofpbuf
*odp_actions
,
8122 struct flow_wildcards
*wc
, bool use_masked
)
8124 struct ovs_key_nd key
, mask
, base
;
8125 struct offsetof_sizeof ovs_key_nd_offsetof_sizeof_arr
[] =
8126 OVS_KEY_ND_OFFSETOF_SIZEOF_ARR
;
8128 get_nd_key(flow
, &key
);
8129 get_nd_key(base_flow
, &base
);
8130 get_nd_key(&wc
->masks
, &mask
);
8132 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8133 ovs_key_nd_offsetof_sizeof_arr
, odp_actions
)) {
8134 put_nd_key(&base
, base_flow
);
8135 put_nd_key(&mask
, &wc
->masks
);
8142 static enum slow_path_reason
8143 commit_set_nd_extensions_action(const struct flow
*flow
,
8144 struct flow
*base_flow
,
8145 struct ofpbuf
*odp_actions
,
8146 struct flow_wildcards
*wc
, bool use_masked
)
8148 struct ovs_key_nd_extensions key
, mask
, base
;
8149 struct offsetof_sizeof ovs_key_nd_extensions_offsetof_sizeof_arr
[] =
8150 OVS_KEY_ND_EXTENSIONS_OFFSETOF_SIZEOF_ARR
;
8152 get_nd_extensions_key(flow
, &key
);
8153 get_nd_extensions_key(base_flow
, &base
);
8154 get_nd_extensions_key(&wc
->masks
, &mask
);
8156 if (commit(OVS_KEY_ATTR_ND_EXTENSIONS
, use_masked
, &key
, &base
, &mask
,
8157 sizeof key
, ovs_key_nd_extensions_offsetof_sizeof_arr
,
8159 put_nd_extensions_key(&base
, base_flow
);
8160 put_nd_extensions_key(&mask
, &wc
->masks
);
8166 static enum slow_path_reason
8167 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
8168 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8173 /* Check if 'flow' really has an L3 header. */
8174 if (!flow
->nw_proto
) {
8178 switch (ntohs(base
->dl_type
)) {
8180 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
8184 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
8185 if (base
->nw_proto
== IPPROTO_ICMPV6
) {
8186 /* Commit extended attrs first to make sure
8187 correct options are added.*/
8188 reason
= commit_set_nd_extensions_action(flow
, base
,
8189 odp_actions
, wc
, use_masked
);
8190 reason
|= commit_set_nd_action(flow
, base
, odp_actions
,
8197 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
8204 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
8208 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
8209 memset(nsh
->context
, 0, sizeof(nsh
->context
));
8215 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
8216 bool is_mask OVS_UNUSED
)
8219 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
8220 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
8225 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
8226 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
8227 struct ovs_key_nsh
*mask
, size_t size
,
8228 struct ofpbuf
*odp_actions
)
8230 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
8232 if (memcmp(key
, base
, size
) == 0) {
8233 /* Mask bits are set when we have either read or set the corresponding
8234 * values. Masked bits will be exact-matched, no need to set them
8235 * if the value did not actually change. */
8239 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
8241 if (use_masked_set
&& !fully_masked
) {
8243 struct ovs_nsh_key_base nsh_base
;
8244 struct ovs_nsh_key_base nsh_base_mask
;
8245 struct ovs_nsh_key_md1 md1
;
8246 struct ovs_nsh_key_md1 md1_mask
;
8247 size_t offset
= nl_msg_start_nested(odp_actions
,
8248 OVS_ACTION_ATTR_SET_MASKED
);
8250 nsh_base
.flags
= key
->flags
;
8251 nsh_base
.ttl
= key
->ttl
;
8252 nsh_base
.mdtype
= key
->mdtype
;
8253 nsh_base
.np
= key
->np
;
8254 nsh_base
.path_hdr
= key
->path_hdr
;
8256 nsh_base_mask
.flags
= mask
->flags
;
8257 nsh_base_mask
.ttl
= mask
->ttl
;
8258 nsh_base_mask
.mdtype
= mask
->mdtype
;
8259 nsh_base_mask
.np
= mask
->np
;
8260 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
8262 /* OVS_KEY_ATTR_NSH keys */
8263 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
8265 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
8266 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
8267 OVS_NSH_KEY_ATTR_BASE
,
8268 2 * sizeof(nsh_base
));
8269 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
8270 size_t lkey_size
= sizeof(nsh_base
);
8272 while (lkey_size
--) {
8273 *data
++ = *lkey
++ & *lmask
++;
8275 lmask
= (char *)&nsh_base_mask
;
8276 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
8278 switch (key
->mdtype
) {
8280 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
8281 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
8283 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
8284 data
= nl_msg_put_unspec_uninit(odp_actions
,
8285 OVS_NSH_KEY_ATTR_MD1
,
8287 lkey
= (char *)&md1
;
8288 lmask
= (char *)&md1_mask
;
8289 lkey_size
= sizeof(md1
);
8291 while (lkey_size
--) {
8292 *data
++ = *lkey
++ & *lmask
++;
8294 lmask
= (char *)&md1_mask
;
8295 memcpy(data
, lmask
, sizeof(md1_mask
));
8299 /* No match support for other MD formats yet. */
8303 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
8305 nl_msg_end_nested(odp_actions
, offset
);
8307 if (!fully_masked
) {
8308 memset(mask
, 0xff, size
);
8310 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
8311 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
8312 nl_msg_end_nested(odp_actions
, offset
);
8314 memcpy(base
, key
, size
);
8319 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
8320 struct ofpbuf
*odp_actions
,
8321 struct flow_wildcards
*wc
,
8324 struct ovs_key_nsh key
, mask
, base
;
8326 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
8327 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
8331 /* Check that mdtype and np remain unchanged. */
8332 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
8333 flow
->nsh
.np
== base_flow
->nsh
.np
);
8335 get_nsh_key(flow
, &key
, false);
8336 get_nsh_key(base_flow
, &base
, false);
8337 get_nsh_key(&wc
->masks
, &mask
, true);
8338 mask
.mdtype
= 0; /* Not writable. */
8339 mask
.np
= 0; /* Not writable. */
8341 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
8342 sizeof key
, odp_actions
)) {
8343 put_nsh_key(&base
, base_flow
, false);
8344 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
8345 put_nsh_key(&mask
, &wc
->masks
, true);
8350 /* TCP, UDP, and SCTP keys have the same layout. */
8351 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
8352 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
8355 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
8357 tp
->tcp
.tcp_src
= flow
->tp_src
;
8358 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
8362 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
8364 flow
->tp_src
= tp
->tcp
.tcp_src
;
8365 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
8369 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
8370 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8373 enum ovs_key_attr key_type
;
8374 union ovs_key_tp key
, mask
, base
;
8375 struct offsetof_sizeof ovs_key_tp_offsetof_sizeof_arr
[] =
8376 OVS_KEY_TCP_OFFSETOF_SIZEOF_ARR
;
8378 /* Check if 'flow' really has an L3 header. */
8379 if (!flow
->nw_proto
) {
8383 if (!is_ip_any(base_flow
)) {
8387 if (flow
->nw_proto
== IPPROTO_TCP
) {
8388 key_type
= OVS_KEY_ATTR_TCP
;
8389 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
8390 key_type
= OVS_KEY_ATTR_UDP
;
8391 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
8392 key_type
= OVS_KEY_ATTR_SCTP
;
8397 get_tp_key(flow
, &key
);
8398 get_tp_key(base_flow
, &base
);
8399 get_tp_key(&wc
->masks
, &mask
);
8401 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8402 ovs_key_tp_offsetof_sizeof_arr
, odp_actions
)) {
8403 put_tp_key(&base
, base_flow
);
8404 put_tp_key(&mask
, &wc
->masks
);
8409 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
8410 struct ofpbuf
*odp_actions
,
8411 struct flow_wildcards
*wc
,
8414 uint32_t key
, mask
, base
;
8415 struct offsetof_sizeof ovs_key_prio_offsetof_sizeof_arr
[] = {
8416 {0, sizeof(uint32_t)},
8420 key
= flow
->skb_priority
;
8421 base
= base_flow
->skb_priority
;
8422 mask
= wc
->masks
.skb_priority
;
8424 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
8425 sizeof key
, ovs_key_prio_offsetof_sizeof_arr
, odp_actions
)) {
8426 base_flow
->skb_priority
= base
;
8427 wc
->masks
.skb_priority
= mask
;
8432 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
8433 struct ofpbuf
*odp_actions
,
8434 struct flow_wildcards
*wc
,
8437 uint32_t key
, mask
, base
;
8438 struct offsetof_sizeof ovs_key_pkt_mark_offsetof_sizeof_arr
[] = {
8439 {0, sizeof(uint32_t)},
8443 key
= flow
->pkt_mark
;
8444 base
= base_flow
->pkt_mark
;
8445 mask
= wc
->masks
.pkt_mark
;
8447 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
8448 sizeof key
, ovs_key_pkt_mark_offsetof_sizeof_arr
,
8450 base_flow
->pkt_mark
= base
;
8451 wc
->masks
.pkt_mark
= mask
;
8456 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
8458 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
8462 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
8463 const struct flow
*flow
,
8464 struct ofpbuf
*encap_data
)
8466 uint8_t * metadata
= NULL
;
8467 uint8_t md_size
= 0;
8469 switch (flow
->nsh
.mdtype
) {
8472 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
8473 metadata
= encap_data
->data
;
8474 md_size
= encap_data
->size
;
8483 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
8484 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
8485 nl_msg_end_nested(odp_actions
, offset
);
8489 commit_encap_decap_action(const struct flow
*flow
,
8490 struct flow
*base_flow
,
8491 struct ofpbuf
*odp_actions
,
8492 struct flow_wildcards
*wc
,
8493 bool pending_encap
, bool pending_decap
,
8494 struct ofpbuf
*encap_data
)
8496 if (pending_encap
) {
8497 switch (ntohl(flow
->packet_type
)) {
8500 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
8502 base_flow
->packet_type
= flow
->packet_type
;
8503 base_flow
->dl_src
= flow
->dl_src
;
8504 base_flow
->dl_dst
= flow
->dl_dst
;
8509 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
8510 base_flow
->packet_type
= flow
->packet_type
;
8511 /* Update all packet headers in base_flow. */
8512 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
8513 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
8516 /* Only the above protocols are supported for encap.
8517 * The check is done at action translation. */
8520 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
8521 /* This is an explicit or implicit decap case. */
8522 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
8523 base_flow
->packet_type
== htonl(PT_ETH
)) {
8524 /* Generate pop_eth and continue without recirculation. */
8525 odp_put_pop_eth_action(odp_actions
);
8526 base_flow
->packet_type
= flow
->packet_type
;
8527 base_flow
->dl_src
= eth_addr_zero
;
8528 base_flow
->dl_dst
= eth_addr_zero
;
8530 /* All other decap cases require recirculation.
8531 * No need to update the base flow here. */
8532 switch (ntohl(base_flow
->packet_type
)) {
8535 odp_put_pop_nsh_action(odp_actions
);
8538 /* Checks are done during translation. */
8544 wc
->masks
.packet_type
= OVS_BE32_MAX
;
8547 /* If any of the flow key data that ODP actions can modify are different in
8548 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8549 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
8550 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
8551 * in addition to this function if needed. Sets fields in 'wc' that are
8552 * used as part of the action.
8554 * In the common case, this function returns 0. If the flow key modification
8555 * requires the flow's packets to be forced into the userspace slow path, this
8556 * function returns SLOW_ACTION. This only happens when there is no ODP action
8557 * to modify some field that was actually modified. For example, there is no
8558 * ODP action to modify any ARP field, so such a modification triggers
8559 * SLOW_ACTION. (When this happens, packets that need such modification get
8560 * flushed to userspace and handled there, which works OK but much more slowly
8561 * than if the datapath handled it directly.) */
8562 enum slow_path_reason
8563 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
8564 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8565 bool use_masked
, bool pending_encap
, bool pending_decap
,
8566 struct ofpbuf
*encap_data
)
8568 /* If you add a field that OpenFlow actions can change, and that is visible
8569 * to the datapath (including all data fields), then you should also add
8570 * code here to commit changes to the field. */
8571 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 42);
8573 enum slow_path_reason slow1
, slow2
;
8574 bool mpls_done
= false;
8576 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
8577 pending_encap
, pending_decap
, encap_data
);
8578 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
8579 /* Make packet a non-MPLS packet before committing L3/4 actions,
8580 * which would otherwise do nothing. */
8581 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
8582 commit_mpls_action(flow
, base
, odp_actions
);
8585 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
8586 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
8587 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
8588 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
8590 commit_mpls_action(flow
, base
, odp_actions
);
8592 commit_vlan_action(flow
, base
, odp_actions
, wc
);
8593 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
8594 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
8596 return slow1
? slow1
: slow2
;