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"
47 VLOG_DEFINE_THIS_MODULE(odp_util
);
49 /* The interface between userspace and kernel uses an "OVS_*" prefix.
50 * Since this is fairly non-specific for the OVS userspace components,
51 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
52 * interactions with the datapath.
55 /* The set of characters that may separate one action or one key attribute
57 static const char *delimiters
= ", \t\r\n";
58 static const char *delimiters_end
= ", \t\r\n)";
60 #define MAX_ODP_NESTED 32
62 struct parse_odp_context
{
63 const struct simap
*port_names
;
64 int depth
; /* Current nested depth of odp string. */
67 static int parse_odp_key_mask_attr(struct parse_odp_context
*, const char *,
68 struct ofpbuf
*, struct ofpbuf
*);
69 static void format_odp_key_attr(const struct nlattr
*a
,
70 const struct nlattr
*ma
,
71 const struct hmap
*portno_names
, struct ds
*ds
,
75 struct geneve_opt d
[63];
79 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
80 struct geneve_scan
*mask
);
81 static void format_geneve_opts(const struct geneve_opt
*opt
,
82 const struct geneve_opt
*mask
, int opts_len
,
83 struct ds
*, bool verbose
);
85 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
86 int max
, struct ofpbuf
*,
87 const struct nlattr
*key
);
88 static void format_u128(struct ds
*d
, const ovs_32aligned_u128
*key
,
89 const ovs_32aligned_u128
*mask
, bool verbose
);
90 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
92 static int parse_odp_action(const char *s
, const struct simap
*port_names
,
93 struct ofpbuf
*actions
);
95 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
98 * - For an action whose argument has a fixed length, returned that
99 * nonnegative length in bytes.
101 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
103 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
105 odp_action_len(uint16_t type
)
107 if (type
> OVS_ACTION_ATTR_MAX
) {
111 switch ((enum ovs_action_attr
) type
) {
112 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
113 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
114 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
115 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
116 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
117 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
118 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
119 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
120 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
121 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
122 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
123 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
124 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
125 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
126 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
127 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
128 case OVS_ACTION_ATTR_CT_CLEAR
: return 0;
129 case OVS_ACTION_ATTR_PUSH_ETH
: return sizeof(struct ovs_action_push_eth
);
130 case OVS_ACTION_ATTR_POP_ETH
: return 0;
131 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
132 case OVS_ACTION_ATTR_PUSH_NSH
: return ATTR_LEN_VARIABLE
;
133 case OVS_ACTION_ATTR_POP_NSH
: return 0;
135 case OVS_ACTION_ATTR_UNSPEC
:
136 case __OVS_ACTION_ATTR_MAX
:
137 return ATTR_LEN_INVALID
;
140 return ATTR_LEN_INVALID
;
143 /* Returns a string form of 'attr'. The return value is either a statically
144 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
145 * should be at least OVS_KEY_ATTR_BUFSIZE. */
146 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
148 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
151 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
152 case OVS_KEY_ATTR_ENCAP
: return "encap";
153 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
154 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
155 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
156 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
157 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
158 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
159 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: return "ct_tuple4";
160 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: return "ct_tuple6";
161 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
162 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
163 case OVS_KEY_ATTR_ETHERNET
: return "eth";
164 case OVS_KEY_ATTR_VLAN
: return "vlan";
165 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
166 case OVS_KEY_ATTR_IPV4
: return "ipv4";
167 case OVS_KEY_ATTR_IPV6
: return "ipv6";
168 case OVS_KEY_ATTR_TCP
: return "tcp";
169 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
170 case OVS_KEY_ATTR_UDP
: return "udp";
171 case OVS_KEY_ATTR_SCTP
: return "sctp";
172 case OVS_KEY_ATTR_ICMP
: return "icmp";
173 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
174 case OVS_KEY_ATTR_ARP
: return "arp";
175 case OVS_KEY_ATTR_ND
: return "nd";
176 case OVS_KEY_ATTR_ND_EXTENSIONS
: return "nd_ext";
177 case OVS_KEY_ATTR_MPLS
: return "mpls";
178 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
179 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
180 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
181 case OVS_KEY_ATTR_NSH
: return "nsh";
183 case __OVS_KEY_ATTR_MAX
:
185 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
191 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
193 size_t len
= nl_attr_get_size(a
);
195 ds_put_format(ds
, "action%d", nl_attr_type(a
));
197 const uint8_t *unspec
;
200 unspec
= nl_attr_get(a
);
201 for (i
= 0; i
< len
; i
++) {
202 ds_put_char(ds
, i
? ' ': '(');
203 ds_put_format(ds
, "%02x", unspec
[i
]);
205 ds_put_char(ds
, ')');
210 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
211 const struct hmap
*portno_names
)
213 static const struct nl_policy ovs_sample_policy
[] = {
214 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
215 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
217 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
219 const struct nlattr
*nla_acts
;
222 ds_put_cstr(ds
, "sample");
224 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
225 ds_put_cstr(ds
, "(error)");
229 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
232 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
234 ds_put_cstr(ds
, "actions(");
235 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
236 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
237 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
238 ds_put_format(ds
, "))");
242 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
243 const struct hmap
*portno_names
)
245 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
246 int len
= nl_attr_get_size(attr
);
248 ds_put_cstr(ds
, "clone");
249 ds_put_format(ds
, "(");
250 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
251 ds_put_format(ds
, ")");
255 format_nsh_key(struct ds
*ds
, const struct ovs_key_nsh
*key
)
257 ds_put_format(ds
, "flags=%d", key
->flags
);
258 ds_put_format(ds
, "ttl=%d", key
->ttl
);
259 ds_put_format(ds
, ",mdtype=%d", key
->mdtype
);
260 ds_put_format(ds
, ",np=%d", key
->np
);
261 ds_put_format(ds
, ",spi=0x%x",
262 nsh_path_hdr_to_spi_uint32(key
->path_hdr
));
263 ds_put_format(ds
, ",si=%d",
264 nsh_path_hdr_to_si(key
->path_hdr
));
266 switch (key
->mdtype
) {
268 for (int i
= 0; i
< 4; i
++) {
269 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->context
[i
]));
274 /* No support for matching other metadata formats yet. */
280 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
281 uint8_t value
, uint8_t mask
)
287 ds_put_format(s
, "%s=", name
);
288 if (mask
== UINT8_MAX
) {
289 ds_put_format(s
, "%"PRIu8
, value
);
291 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
298 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
299 ovs_be32 value
, ovs_be32 mask
)
301 if (mask
!= htonl(0)) {
305 ds_put_format(s
, "%s=", name
);
306 if (mask
== OVS_BE32_MAX
) {
307 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
309 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
310 ntohl(value
), ntohl(mask
));
317 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
318 const struct ovs_key_nsh
*mask
)
321 format_nsh_key(ds
, key
);
324 uint32_t spi
= nsh_path_hdr_to_spi_uint32(key
->path_hdr
);
325 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
->path_hdr
);
326 if (spi_mask
== (NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
327 spi_mask
= UINT32_MAX
;
329 uint8_t si
= nsh_path_hdr_to_si(key
->path_hdr
);
330 uint8_t si_mask
= nsh_path_hdr_to_si(mask
->path_hdr
);
332 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
333 format_uint8_masked(ds
, &first
, "ttl", key
->ttl
, mask
->ttl
);
334 format_uint8_masked(ds
, &first
, "mdtype", key
->mdtype
, mask
->mdtype
);
335 format_uint8_masked(ds
, &first
, "np", key
->np
, mask
->np
);
336 format_be32_masked(ds
, &first
, "spi", htonl(spi
), htonl(spi_mask
));
337 format_uint8_masked(ds
, &first
, "si", si
, si_mask
);
338 format_be32_masked(ds
, &first
, "c1", key
->context
[0],
340 format_be32_masked(ds
, &first
, "c2", key
->context
[1],
342 format_be32_masked(ds
, &first
, "c3", key
->context
[2],
344 format_be32_masked(ds
, &first
, "c4", key
->context
[3],
350 format_odp_push_nsh_action(struct ds
*ds
,
351 const struct nsh_hdr
*nsh_hdr
)
353 size_t mdlen
= nsh_hdr_len(nsh_hdr
) - NSH_BASE_HDR_LEN
;
354 uint32_t spi
= ntohl(nsh_get_spi(nsh_hdr
));
355 uint8_t si
= nsh_get_si(nsh_hdr
);
356 uint8_t flags
= nsh_get_flags(nsh_hdr
);
357 uint8_t ttl
= nsh_get_ttl(nsh_hdr
);
359 ds_put_cstr(ds
, "push_nsh(");
360 ds_put_format(ds
, "flags=%d", flags
);
361 ds_put_format(ds
, ",ttl=%d", ttl
);
362 ds_put_format(ds
, ",mdtype=%d", nsh_hdr
->md_type
);
363 ds_put_format(ds
, ",np=%d", nsh_hdr
->next_proto
);
364 ds_put_format(ds
, ",spi=0x%x", spi
);
365 ds_put_format(ds
, ",si=%d", si
);
366 switch (nsh_hdr
->md_type
) {
368 const struct nsh_md1_ctx
*md1_ctx
= &nsh_hdr
->md1
;
369 for (int i
= 0; i
< 4; i
++) {
370 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
371 ntohl(get_16aligned_be32(&md1_ctx
->context
[i
])));
376 const struct nsh_md2_tlv
*md2_ctx
= &nsh_hdr
->md2
;
377 ds_put_cstr(ds
, ",md2=");
378 ds_put_hex(ds
, md2_ctx
, mdlen
);
384 ds_put_format(ds
, ")");
388 slow_path_reason_to_string(uint32_t reason
)
390 switch ((enum slow_path_reason
) reason
) {
391 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
400 slow_path_reason_to_explanation(enum slow_path_reason reason
)
403 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
412 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
413 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
415 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
416 res_flags
, allowed
, res_mask
);
420 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
421 const struct hmap
*portno_names
)
423 static const struct nl_policy ovs_userspace_policy
[] = {
424 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
425 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
427 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
429 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
432 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
433 const struct nlattr
*userdata_attr
;
434 const struct nlattr
*tunnel_out_port_attr
;
436 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
437 ds_put_cstr(ds
, "userspace(error)");
441 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
442 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
444 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
447 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
448 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
449 bool userdata_unspec
= true;
450 struct user_action_cookie cookie
;
452 if (userdata_len
== sizeof cookie
) {
453 memcpy(&cookie
, userdata
, sizeof cookie
);
455 userdata_unspec
= false;
457 if (cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
458 ds_put_format(ds
, ",sFlow("
459 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
460 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
461 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
462 cookie
.sflow
.output
);
463 } else if (cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
464 ds_put_cstr(ds
, ",slow_path(");
465 format_flags(ds
, slow_path_reason_to_string
,
466 cookie
.slow_path
.reason
, ',');
467 ds_put_format(ds
, ")");
468 } else if (cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
469 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
470 ",collector_set_id=%"PRIu32
471 ",obs_domain_id=%"PRIu32
472 ",obs_point_id=%"PRIu32
474 cookie
.flow_sample
.probability
,
475 cookie
.flow_sample
.collector_set_id
,
476 cookie
.flow_sample
.obs_domain_id
,
477 cookie
.flow_sample
.obs_point_id
);
478 odp_portno_name_format(portno_names
,
479 cookie
.flow_sample
.output_odp_port
, ds
);
480 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
481 ds_put_cstr(ds
, ",ingress");
482 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
483 ds_put_cstr(ds
, ",egress");
485 ds_put_char(ds
, ')');
486 } else if (cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
487 ds_put_format(ds
, ",ipfix(output_port=");
488 odp_portno_name_format(portno_names
,
489 cookie
.ipfix
.output_odp_port
, ds
);
490 ds_put_char(ds
, ')');
491 } else if (cookie
.type
== USER_ACTION_COOKIE_CONTROLLER
) {
492 ds_put_format(ds
, ",controller(reason=%"PRIu16
496 ",rule_cookie=%#"PRIx64
497 ",controller_id=%"PRIu16
499 cookie
.controller
.reason
,
500 !!cookie
.controller
.dont_send
,
501 !!cookie
.controller
.continuation
,
502 cookie
.controller
.recirc_id
,
503 ntohll(get_32aligned_be64(
504 &cookie
.controller
.rule_cookie
)),
505 cookie
.controller
.controller_id
,
506 cookie
.controller
.max_len
);
507 ds_put_char(ds
, ')');
509 userdata_unspec
= true;
513 if (userdata_unspec
) {
515 ds_put_format(ds
, ",userdata(");
516 for (i
= 0; i
< userdata_len
; i
++) {
517 ds_put_format(ds
, "%02x", userdata
[i
]);
519 ds_put_char(ds
, ')');
523 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
524 ds_put_cstr(ds
, ",actions");
527 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
528 if (tunnel_out_port_attr
) {
529 ds_put_format(ds
, ",tunnel_out_port=");
530 odp_portno_name_format(portno_names
,
531 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
534 ds_put_char(ds
, ')');
538 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
540 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
541 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
542 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
543 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
545 ds_put_char(ds
, ',');
547 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
548 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
549 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
550 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
552 ds_put_char(ds
, ',');
554 if (!(tci
& htons(VLAN_CFI
))) {
555 ds_put_cstr(ds
, "cfi=0");
556 ds_put_char(ds
, ',');
562 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
564 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
565 mpls_lse_to_label(mpls_lse
),
566 mpls_lse_to_tc(mpls_lse
),
567 mpls_lse_to_ttl(mpls_lse
),
568 mpls_lse_to_bos(mpls_lse
));
572 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
573 const struct ovs_key_mpls
*mpls_mask
, int n
)
575 for (int i
= 0; i
< n
; i
++) {
576 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
578 if (mpls_mask
== NULL
) {
579 format_mpls_lse(ds
, key
);
581 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
583 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
584 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
585 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
586 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
587 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
589 ds_put_char(ds
, ',');
595 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
597 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
601 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
603 ds_put_format(ds
, "hash(");
605 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
606 ds_put_format(ds
, "l4(%"PRIu32
")", hash_act
->hash_basis
);
607 } else if (hash_act
->hash_alg
== OVS_HASH_ALG_SYM_L4
) {
608 ds_put_format(ds
, "sym_l4(%"PRIu32
")", hash_act
->hash_basis
);
610 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
613 ds_put_format(ds
, ")");
617 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
619 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
620 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
621 ntohs(udp
->udp_csum
));
627 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
629 const struct eth_header
*eth
;
632 const struct udp_header
*udp
;
634 eth
= (const struct eth_header
*)data
->header
;
639 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
640 data
->header_len
, data
->tnl_type
);
641 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
642 ds_put_format(ds
, ",src=");
643 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
644 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
646 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
648 const struct ip_header
*ip
= l3
;
649 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
650 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
651 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
652 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
653 ip
->ip_proto
, ip
->ip_tos
,
655 ntohs(ip
->ip_frag_off
));
658 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
659 struct in6_addr src
, dst
;
660 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
661 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
662 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
664 ds_put_format(ds
, "ipv6(src=");
665 ipv6_format_addr(&src
, ds
);
666 ds_put_format(ds
, ",dst=");
667 ipv6_format_addr(&dst
, ds
);
668 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
669 ",hlimit=%"PRIu8
"),",
670 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
671 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
675 udp
= (const struct udp_header
*) l4
;
677 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
678 const struct vxlanhdr
*vxh
;
680 vxh
= format_udp_tnl_push_header(ds
, udp
);
682 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
683 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
684 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
685 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
686 const struct genevehdr
*gnh
;
688 gnh
= format_udp_tnl_push_header(ds
, udp
);
690 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
691 gnh
->oam
? "oam," : "",
692 gnh
->critical
? "crit," : "",
693 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
696 ds_put_cstr(ds
, ",options(");
697 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
699 ds_put_char(ds
, ')');
702 ds_put_char(ds
, ')');
703 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
704 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
705 const struct gre_base_hdr
*greh
;
706 ovs_16aligned_be32
*options
;
708 greh
= (const struct gre_base_hdr
*) l4
;
710 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
711 ntohs(greh
->flags
), ntohs(greh
->protocol
));
712 options
= (ovs_16aligned_be32
*)(greh
+ 1);
713 if (greh
->flags
& htons(GRE_CSUM
)) {
714 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
717 if (greh
->flags
& htons(GRE_KEY
)) {
718 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
721 if (greh
->flags
& htons(GRE_SEQ
)) {
722 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
725 ds_put_format(ds
, ")");
726 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
727 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
728 const struct gre_base_hdr
*greh
;
729 const struct erspan_base_hdr
*ersh
;
731 greh
= (const struct gre_base_hdr
*) l4
;
732 ersh
= ERSPAN_HDR(greh
);
734 if (ersh
->ver
== 1) {
735 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
737 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
738 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
739 } else if (ersh
->ver
== 2) {
740 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
742 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
743 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
744 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
746 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
749 ds_put_format(ds
, ")");
753 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
754 const struct hmap
*portno_names
)
756 struct ovs_action_push_tnl
*data
;
758 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
760 ds_put_cstr(ds
, "tnl_push(tnl_port(");
761 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
762 ds_put_cstr(ds
, "),");
763 format_odp_tnl_push_header(ds
, data
);
764 ds_put_format(ds
, ",out_port(");
765 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
766 ds_put_cstr(ds
, "))");
769 static const struct nl_policy ovs_nat_policy
[] = {
770 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
771 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
772 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
773 .min_len
= sizeof(struct in_addr
),
774 .max_len
= sizeof(struct in6_addr
)},
775 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
776 .min_len
= sizeof(struct in_addr
),
777 .max_len
= sizeof(struct in6_addr
)},
778 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
779 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
780 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
781 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
782 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
786 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
788 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
790 ovs_be32 ip_min
, ip_max
;
791 struct in6_addr ip6_min
, ip6_max
;
792 uint16_t proto_min
, proto_max
;
794 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
795 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
798 /* If no type, then nothing else either. */
799 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
800 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
801 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
802 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
803 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
804 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
807 /* Both SNAT & DNAT may not be specified. */
808 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
809 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
812 /* proto may not appear without ip. */
813 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
814 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
817 /* MAX may not appear without MIN. */
818 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
819 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
820 ds_put_cstr(ds
, "nat(error: range max without min.)");
823 /* Address sizes must match. */
824 if ((a
[OVS_NAT_ATTR_IP_MIN
]
825 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
826 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
827 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
828 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
829 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
830 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
834 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
835 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
836 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
837 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
838 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
839 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
840 if (addr_len
== sizeof ip6_min
) {
841 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
842 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
844 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
845 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
848 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
849 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
850 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
851 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
853 if ((addr_len
== sizeof(ovs_be32
)
854 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
855 || (addr_len
== sizeof(struct in6_addr
)
856 && !ipv6_mask_is_any(&ip6_max
)
857 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
858 || (proto_max
&& proto_min
> proto_max
)) {
859 ds_put_cstr(ds
, "nat(range error)");
863 ds_put_cstr(ds
, "nat");
864 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
865 ds_put_char(ds
, '(');
866 if (a
[OVS_NAT_ATTR_SRC
]) {
867 ds_put_cstr(ds
, "src");
868 } else if (a
[OVS_NAT_ATTR_DST
]) {
869 ds_put_cstr(ds
, "dst");
873 ds_put_cstr(ds
, "=");
875 if (addr_len
== sizeof ip_min
) {
876 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
878 if (ip_max
&& ip_max
!= ip_min
) {
879 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
881 } else if (addr_len
== sizeof ip6_min
) {
882 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
884 if (!ipv6_mask_is_any(&ip6_max
) &&
885 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
886 ds_put_char(ds
, '-');
887 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
891 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
893 if (proto_max
&& proto_max
!= proto_min
) {
894 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
898 ds_put_char(ds
, ',');
899 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
900 ds_put_cstr(ds
, "persistent,");
902 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
903 ds_put_cstr(ds
, "hash,");
905 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
906 ds_put_cstr(ds
, "random,");
909 ds_put_char(ds
, ')');
913 static const struct nl_policy ovs_conntrack_policy
[] = {
914 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
915 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
916 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
917 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
918 .min_len
= sizeof(uint32_t) * 2 },
919 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
920 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
921 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
922 .min_len
= 1, .max_len
= 16 },
923 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
927 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
929 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
931 ovs_32aligned_u128 value
;
932 ovs_32aligned_u128 mask
;
934 const uint32_t *mark
;
938 const struct nlattr
*nat
;
940 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
941 ds_put_cstr(ds
, "ct(error)");
945 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
946 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
947 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
948 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
949 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
950 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
951 nat
= a
[OVS_CT_ATTR_NAT
];
953 ds_put_format(ds
, "ct");
954 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
955 ds_put_cstr(ds
, "(");
957 ds_put_format(ds
, "commit,");
960 ds_put_format(ds
, "force_commit,");
963 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
966 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
970 ds_put_format(ds
, "label=");
971 format_u128(ds
, &label
->value
, &label
->mask
, true);
972 ds_put_char(ds
, ',');
975 ds_put_format(ds
, "helper=%s,", helper
);
978 format_odp_ct_nat(ds
, nat
);
981 ds_put_cstr(ds
, ")");
985 static const struct attr_len_tbl
986 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
987 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
988 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
989 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
993 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
996 const struct nlattr
*a
;
997 struct ovs_key_nsh nsh
;
998 struct ovs_key_nsh nsh_mask
;
1000 memset(&nsh
, 0, sizeof nsh
);
1001 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
1003 NL_NESTED_FOR_EACH (a
, left
, attr
) {
1004 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
1005 size_t len
= nl_attr_get_size(a
);
1007 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1011 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1012 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1017 case OVS_NSH_KEY_ATTR_UNSPEC
:
1019 case OVS_NSH_KEY_ATTR_BASE
: {
1020 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1021 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1022 memcpy(&nsh
, base
, sizeof(*base
));
1023 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1026 case OVS_NSH_KEY_ATTR_MD1
: {
1027 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1028 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1029 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1030 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1033 case OVS_NSH_KEY_ATTR_MD2
:
1034 case __OVS_NSH_KEY_ATTR_MAX
:
1036 /* No support for matching other metadata formats yet. */
1041 ds_put_cstr(ds
, "set(nsh(");
1042 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1043 ds_put_cstr(ds
, "))");
1048 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1049 const struct hmap
*portno_names
)
1052 enum ovs_action_attr type
= nl_attr_type(a
);
1055 expected_len
= odp_action_len(nl_attr_type(a
));
1056 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1057 nl_attr_get_size(a
) != expected_len
) {
1058 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1059 nl_attr_get_size(a
), expected_len
);
1060 format_generic_odp_action(ds
, a
);
1065 case OVS_ACTION_ATTR_METER
:
1066 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1068 case OVS_ACTION_ATTR_OUTPUT
:
1069 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1071 case OVS_ACTION_ATTR_TRUNC
: {
1072 const struct ovs_action_trunc
*trunc
=
1073 nl_attr_get_unspec(a
, sizeof *trunc
);
1075 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1078 case OVS_ACTION_ATTR_TUNNEL_POP
:
1079 ds_put_cstr(ds
, "tnl_pop(");
1080 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1081 ds_put_char(ds
, ')');
1083 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1084 format_odp_tnl_push_action(ds
, a
, portno_names
);
1086 case OVS_ACTION_ATTR_USERSPACE
:
1087 format_odp_userspace_action(ds
, a
, portno_names
);
1089 case OVS_ACTION_ATTR_RECIRC
:
1090 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1092 case OVS_ACTION_ATTR_HASH
:
1093 format_odp_hash_action(ds
, nl_attr_get(a
));
1095 case OVS_ACTION_ATTR_SET_MASKED
:
1097 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1098 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1099 format_odp_set_nsh(ds
, a
);
1102 size
= nl_attr_get_size(a
) / 2;
1103 ds_put_cstr(ds
, "set(");
1105 /* Masked set action not supported for tunnel key, which is bigger. */
1106 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1107 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1108 sizeof(struct nlattr
))];
1109 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1110 sizeof(struct nlattr
))];
1112 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1113 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1114 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1115 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1116 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1118 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1120 ds_put_cstr(ds
, ")");
1122 case OVS_ACTION_ATTR_SET
:
1123 ds_put_cstr(ds
, "set(");
1124 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1125 ds_put_cstr(ds
, ")");
1127 case OVS_ACTION_ATTR_PUSH_ETH
: {
1128 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1129 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1130 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1131 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1134 case OVS_ACTION_ATTR_POP_ETH
:
1135 ds_put_cstr(ds
, "pop_eth");
1137 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1138 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1139 ds_put_cstr(ds
, "push_vlan(");
1140 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1141 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1143 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1144 ds_put_char(ds
, ')');
1147 case OVS_ACTION_ATTR_POP_VLAN
:
1148 ds_put_cstr(ds
, "pop_vlan");
1150 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1151 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1152 ds_put_cstr(ds
, "push_mpls(");
1153 format_mpls_lse(ds
, mpls
->mpls_lse
);
1154 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1157 case OVS_ACTION_ATTR_POP_MPLS
: {
1158 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1159 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1162 case OVS_ACTION_ATTR_SAMPLE
:
1163 format_odp_sample_action(ds
, a
, portno_names
);
1165 case OVS_ACTION_ATTR_CT
:
1166 format_odp_conntrack_action(ds
, a
);
1168 case OVS_ACTION_ATTR_CT_CLEAR
:
1169 ds_put_cstr(ds
, "ct_clear");
1171 case OVS_ACTION_ATTR_CLONE
:
1172 format_odp_clone_action(ds
, a
, portno_names
);
1174 case OVS_ACTION_ATTR_PUSH_NSH
: {
1175 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1176 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1177 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1178 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1179 format_odp_push_nsh_action(ds
, nsh_hdr
);
1182 case OVS_ACTION_ATTR_POP_NSH
:
1183 ds_put_cstr(ds
, "pop_nsh()");
1185 case OVS_ACTION_ATTR_UNSPEC
:
1186 case __OVS_ACTION_ATTR_MAX
:
1188 format_generic_odp_action(ds
, a
);
1194 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1195 size_t actions_len
, const struct hmap
*portno_names
)
1198 const struct nlattr
*a
;
1201 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1203 ds_put_char(ds
, ',');
1205 format_odp_action(ds
, a
, portno_names
);
1210 if (left
== actions_len
) {
1211 ds_put_cstr(ds
, "<empty>");
1213 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1214 for (i
= 0; i
< left
; i
++) {
1215 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1217 ds_put_char(ds
, ')');
1220 ds_put_cstr(ds
, "drop");
1224 /* Separate out parse_odp_userspace_action() function. */
1226 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1229 struct user_action_cookie cookie
;
1231 odp_port_t tunnel_out_port
;
1233 void *user_data
= NULL
;
1234 size_t user_data_size
= 0;
1235 bool include_actions
= false;
1238 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1242 ofpbuf_init(&buf
, 16);
1243 memset(&cookie
, 0, sizeof cookie
);
1245 user_data
= &cookie
;
1246 user_data_size
= sizeof cookie
;
1249 uint32_t probability
;
1250 uint32_t collector_set_id
;
1251 uint32_t obs_domain_id
;
1252 uint32_t obs_point_id
;
1254 /* USER_ACTION_COOKIE_CONTROLLER. */
1256 uint8_t continuation
;
1259 uint64_t rule_cookie
;
1260 uint16_t controller_id
;
1265 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1266 "pcp=%i,output=%"SCNi32
")%n",
1267 &vid
, &pcp
, &output
, &n1
)) {
1271 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1276 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1277 cookie
.ofp_in_port
= OFPP_NONE
;
1278 cookie
.ofproto_uuid
= UUID_ZERO
;
1279 cookie
.sflow
.vlan_tci
= htons(tci
);
1280 cookie
.sflow
.output
= output
;
1281 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1284 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1285 cookie
.ofp_in_port
= OFPP_NONE
;
1286 cookie
.ofproto_uuid
= UUID_ZERO
;
1287 cookie
.slow_path
.reason
= 0;
1289 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1290 &cookie
.slow_path
.reason
,
1291 SLOW_PATH_REASON_MASK
, NULL
);
1292 if (res
< 0 || s
[n
+ res
] != ')') {
1296 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1297 "collector_set_id=%"SCNi32
","
1298 "obs_domain_id=%"SCNi32
","
1299 "obs_point_id=%"SCNi32
","
1300 "output_port=%"SCNi32
"%n",
1301 &probability
, &collector_set_id
,
1302 &obs_domain_id
, &obs_point_id
,
1306 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1307 cookie
.ofp_in_port
= OFPP_NONE
;
1308 cookie
.ofproto_uuid
= UUID_ZERO
;
1309 cookie
.flow_sample
.probability
= probability
;
1310 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1311 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1312 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1313 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1315 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1316 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1318 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1319 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1322 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1329 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1332 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1333 cookie
.ofp_in_port
= OFPP_NONE
;
1334 cookie
.ofproto_uuid
= UUID_ZERO
;
1335 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1336 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1338 ",continuation=%"SCNu8
1339 ",recirc_id=%"SCNu32
1340 ",rule_cookie=%"SCNx64
1341 ",controller_id=%"SCNu16
1342 ",max_len=%"SCNu16
")%n",
1343 &reason
, &dont_send
, &continuation
, &recirc_id
,
1344 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1346 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1347 cookie
.ofp_in_port
= OFPP_NONE
;
1348 cookie
.ofproto_uuid
= UUID_ZERO
;
1349 cookie
.controller
.dont_send
= dont_send
? true : false;
1350 cookie
.controller
.continuation
= continuation
? true : false;
1351 cookie
.controller
.reason
= reason
;
1352 cookie
.controller
.recirc_id
= recirc_id
;
1353 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1354 htonll(rule_cookie
));
1355 cookie
.controller
.controller_id
= controller_id
;
1356 cookie
.controller
.max_len
= max_len
;
1357 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1361 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1362 if (end
[0] != ')') {
1366 user_data
= buf
.data
;
1367 user_data_size
= buf
.size
;
1374 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1376 include_actions
= true;
1382 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1383 &tunnel_out_port
, &n1
)) {
1384 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1385 tunnel_out_port
, include_actions
, actions
);
1388 } else if (s
[n
] == ')') {
1389 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1390 ODPP_NONE
, include_actions
, actions
);
1397 struct ovs_action_push_eth push
;
1401 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1402 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1403 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1404 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1407 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1408 &push
, sizeof push
);
1415 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1416 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1423 ofpbuf_uninit(&buf
);
1428 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1430 struct eth_header
*eth
;
1431 struct ip_header
*ip
;
1432 struct ovs_16aligned_ip6_hdr
*ip6
;
1433 struct udp_header
*udp
;
1434 struct gre_base_hdr
*greh
;
1435 struct erspan_base_hdr
*ersh
;
1436 struct erspan_md2
*md2
;
1437 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
, sid
;
1439 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1444 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1447 eth
= (struct eth_header
*) data
->header
;
1448 l3
= (struct ip_header
*) (eth
+ 1);
1449 ip
= (struct ip_header
*) l3
;
1450 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1451 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1452 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1455 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1459 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1460 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1463 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1466 eth
->eth_type
= htons(dl_type
);
1468 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1470 uint16_t ip_frag_off
;
1471 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1472 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1475 &ip
->ip_proto
, &ip
->ip_tos
,
1476 &ip
->ip_ttl
, &ip_frag_off
)) {
1479 put_16aligned_be32(&ip
->ip_src
, sip
);
1480 put_16aligned_be32(&ip
->ip_dst
, dip
);
1481 ip
->ip_frag_off
= htons(ip_frag_off
);
1482 ip_len
= sizeof *ip
;
1484 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1485 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1486 struct in6_addr sip6
, dip6
;
1489 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1490 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1491 ",hlimit=%"SCNi8
"),",
1492 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1493 &tclass
, &ip6
->ip6_hlim
)
1494 || (label
& ~IPV6_LABEL_MASK
) != 0
1495 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1496 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1499 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1500 htonl(tclass
<< 20) | htonl(label
));
1501 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1502 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1503 ip_len
= sizeof *ip6
;
1507 l4
= ((uint8_t *) l3
+ ip_len
);
1508 udp
= (struct udp_header
*) l4
;
1509 greh
= (struct gre_base_hdr
*) l4
;
1510 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1511 &udp_src
, &udp_dst
, &csum
)) {
1512 uint32_t vx_flags
, vni
;
1514 udp
->udp_src
= htons(udp_src
);
1515 udp
->udp_dst
= htons(udp_dst
);
1517 udp
->udp_csum
= htons(csum
);
1519 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1521 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1523 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1524 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1525 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1526 header_len
= sizeof *eth
+ ip_len
+
1527 sizeof *udp
+ sizeof *vxh
;
1528 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1529 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1531 memset(gnh
, 0, sizeof *gnh
);
1532 header_len
= sizeof *eth
+ ip_len
+
1533 sizeof *udp
+ sizeof *gnh
;
1535 if (ovs_scan_len(s
, &n
, "oam,")) {
1538 if (ovs_scan_len(s
, &n
, "crit,")) {
1541 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1544 if (ovs_scan_len(s
, &n
, ",options(")) {
1545 struct geneve_scan options
;
1548 memset(&options
, 0, sizeof options
);
1549 len
= scan_geneve(s
+ n
, &options
, NULL
);
1554 memcpy(gnh
->options
, options
.d
, options
.len
);
1555 gnh
->opt_len
= options
.len
/ 4;
1556 header_len
+= options
.len
;
1560 if (!ovs_scan_len(s
, &n
, "))")) {
1564 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1565 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1566 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1570 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1571 &gre_flags
, &gre_proto
)){
1573 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1574 tnl_type
= OVS_VPORT_TYPE_GRE
;
1576 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1578 greh
->flags
= htons(gre_flags
);
1579 greh
->protocol
= htons(gre_proto
);
1580 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1582 if (greh
->flags
& htons(GRE_CSUM
)) {
1583 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1587 memset(options
, 0, sizeof *options
);
1588 *((ovs_be16
*)options
) = htons(csum
);
1591 if (greh
->flags
& htons(GRE_KEY
)) {
1594 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1598 put_16aligned_be32(options
, htonl(key
));
1601 if (greh
->flags
& htons(GRE_SEQ
)) {
1604 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1607 put_16aligned_be32(options
, htonl(seq
));
1611 if (!ovs_scan_len(s
, &n
, "))")) {
1615 header_len
= sizeof *eth
+ ip_len
+
1616 ((uint8_t *) options
- (uint8_t *) greh
);
1617 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1618 &sid
, &erspan_idx
)) {
1619 ersh
= ERSPAN_HDR(greh
);
1620 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1623 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1624 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1626 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1629 greh
->flags
= htons(GRE_SEQ
);
1630 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1634 put_16aligned_be32(index
, htonl(erspan_idx
));
1636 if (!ovs_scan_len(s
, &n
, ")")) {
1639 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1640 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1642 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1643 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1645 ersh
= ERSPAN_HDR(greh
);
1646 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1648 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1649 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1651 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1654 greh
->flags
= htons(GRE_SEQ
);
1655 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1659 set_hwid(md2
, hwid
);
1662 if (!ovs_scan_len(s
, &n
, ")")) {
1666 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1667 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1672 /* check tunnel meta data. */
1673 if (data
->tnl_type
!= tnl_type
) {
1676 if (data
->header_len
!= header_len
) {
1681 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1688 struct ct_nat_params
{
1694 struct in6_addr ip6
;
1698 struct in6_addr ip6
;
1708 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1710 if (ovs_scan_len(s
, n
, "=")) {
1711 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1712 struct in6_addr ipv6
;
1714 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1715 p
->addr_len
= sizeof p
->addr_min
.ip
;
1716 if (ovs_scan_len(s
, n
, "-")) {
1717 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1718 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1722 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1723 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1724 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1725 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1726 p
->addr_min
.ip6
= ipv6
;
1727 if (ovs_scan_len(s
, n
, "-")) {
1728 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1729 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1730 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1731 p
->addr_max
.ip6
= ipv6
;
1739 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1740 if (ovs_scan_len(s
, n
, "-")) {
1741 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1751 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1755 if (ovs_scan_len(s
, &n
, "nat")) {
1756 memset(p
, 0, sizeof *p
);
1758 if (ovs_scan_len(s
, &n
, "(")) {
1762 end
= strchr(s
+ n
, ')');
1769 n
+= strspn(s
+ n
, delimiters
);
1770 if (ovs_scan_len(s
, &n
, "src")) {
1771 int err
= scan_ct_nat_range(s
, &n
, p
);
1778 if (ovs_scan_len(s
, &n
, "dst")) {
1779 int err
= scan_ct_nat_range(s
, &n
, p
);
1786 if (ovs_scan_len(s
, &n
, "persistent")) {
1787 p
->persistent
= true;
1790 if (ovs_scan_len(s
, &n
, "hash")) {
1791 p
->proto_hash
= true;
1794 if (ovs_scan_len(s
, &n
, "random")) {
1795 p
->proto_random
= true;
1801 if (p
->snat
&& p
->dnat
) {
1804 if ((p
->addr_len
!= 0 &&
1805 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1806 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1807 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1810 if (p
->proto_hash
&& p
->proto_random
) {
1820 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1822 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1825 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1826 } else if (p
->dnat
) {
1827 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1831 if (p
->addr_len
!= 0) {
1832 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1834 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1835 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1839 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1840 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1841 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1844 if (p
->persistent
) {
1845 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1847 if (p
->proto_hash
) {
1848 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1850 if (p
->proto_random
) {
1851 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1855 nl_msg_end_nested(actions
, start
);
1859 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1863 if (ovs_scan(s
, "ct")) {
1864 const char *helper
= NULL
;
1865 size_t helper_len
= 0;
1866 bool commit
= false;
1867 bool force_commit
= false;
1872 } ct_mark
= { 0, 0 };
1877 struct ct_nat_params nat_params
;
1878 bool have_nat
= false;
1882 memset(&ct_label
, 0, sizeof(ct_label
));
1885 if (ovs_scan(s
, "(")) {
1888 end
= strchr(s
, ')');
1896 s
+= strspn(s
, delimiters
);
1897 if (ovs_scan(s
, "commit%n", &n
)) {
1902 if (ovs_scan(s
, "force_commit%n", &n
)) {
1903 force_commit
= true;
1907 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1911 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1914 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1917 ct_mark
.mask
= UINT32_MAX
;
1921 if (ovs_scan(s
, "label=%n", &n
)) {
1925 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1932 if (ovs_scan(s
, "helper=%n", &n
)) {
1934 helper_len
= strcspn(s
, delimiters_end
);
1935 if (!helper_len
|| helper_len
> 15) {
1943 n
= scan_ct_nat(s
, &nat_params
);
1948 /* end points to the end of the nested, nat action.
1949 * find the real end. */
1952 /* Nothing matched. */
1957 if (commit
&& force_commit
) {
1961 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1963 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1964 } else if (force_commit
) {
1965 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1968 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1971 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1974 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1975 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1979 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1983 nl_msg_put_ct_nat(&nat_params
, actions
);
1985 nl_msg_end_nested(actions
, start
);
1992 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
1993 uint8_t * metadata
, size_t md_size
,
1997 struct ovs_nsh_key_base base
;
1999 base
.flags
= nsh
->flags
;
2000 base
.ttl
= nsh
->ttl
;
2001 base
.mdtype
= nsh
->mdtype
;
2003 base
.path_hdr
= nsh
->path_hdr
;
2005 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
2006 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
2009 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2010 sizeof nsh
->context
);
2012 switch (nsh
->mdtype
) {
2014 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2015 sizeof nsh
->context
);
2018 if (metadata
&& md_size
> 0) {
2019 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2024 /* No match support for other MD formats yet. */
2028 nl_msg_end_nested(buf
, nsh_key_ofs
);
2033 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2040 struct ovs_key_nsh nsh
;
2041 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2042 uint8_t md_size
= 0;
2044 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2049 /* The default is NSH_M_TYPE1 */
2052 nsh
.mdtype
= NSH_M_TYPE1
;
2053 nsh
.np
= NSH_P_ETHERNET
;
2054 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2055 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2058 n
+= strspn(s
+ n
, delimiters
);
2063 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2066 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2069 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2070 switch (nsh
.mdtype
) {
2072 /* This is the default format. */;
2075 /* Length will be updated later. */
2084 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2087 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2090 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2093 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2094 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2095 nsh
.context
[0] = htonl(cd
);
2098 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2099 nsh
.context
[1] = htonl(cd
);
2102 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2103 nsh
.context
[2] = htonl(cd
);
2106 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2107 nsh
.context
[3] = htonl(cd
);
2111 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2114 size_t mdlen
, padding
;
2115 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)
2116 && n
/2 <= sizeof metadata
) {
2117 ofpbuf_use_stub(&b
, metadata
, sizeof metadata
);
2118 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2119 /* Pad metadata to 4 bytes. */
2120 padding
= PAD_SIZE(mdlen
, 4);
2122 ofpbuf_put_zeros(&b
, padding
);
2124 md_size
= mdlen
+ padding
;
2135 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2136 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2137 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2138 nl_msg_end_nested(actions
, offset
);
2145 parse_action_list(const char *s
, const struct simap
*port_names
,
2146 struct ofpbuf
*actions
)
2153 n
+= strspn(s
+ n
, delimiters
);
2157 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
2164 if (actions
->size
> UINT16_MAX
) {
2172 parse_odp_action(const char *s
, const struct simap
*port_names
,
2173 struct ofpbuf
*actions
)
2179 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2180 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2189 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2190 struct ovs_action_trunc
*trunc
;
2192 trunc
= nl_msg_put_unspec_uninit(actions
,
2193 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2194 trunc
->max_len
= max_len
;
2200 int len
= strcspn(s
, delimiters
);
2201 struct simap_node
*node
;
2203 node
= simap_find_len(port_names
, s
, len
);
2205 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2214 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2215 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2220 if (!strncmp(s
, "userspace(", 10)) {
2221 return parse_odp_userspace_action(s
, actions
);
2224 if (!strncmp(s
, "set(", 4)) {
2227 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2228 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2229 struct nlattr
*nested
, *key
;
2231 struct parse_odp_context context
= (struct parse_odp_context
) {
2232 .port_names
= port_names
,
2235 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2236 retval
= parse_odp_key_mask_attr(&context
, s
+ 4, actions
, &maskbuf
);
2238 ofpbuf_uninit(&maskbuf
);
2241 if (s
[retval
+ 4] != ')') {
2242 ofpbuf_uninit(&maskbuf
);
2246 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2249 size
= nl_attr_get_size(mask
);
2250 if (size
== nl_attr_get_size(key
)) {
2251 /* Change to masked set action if not fully masked. */
2252 if (!is_all_ones(mask
+ 1, size
)) {
2253 /* Remove padding of eariler key payload */
2254 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2256 /* Put mask payload right after key payload */
2257 key
->nla_len
+= size
;
2258 ofpbuf_put(actions
, mask
+ 1, size
);
2260 /* 'actions' may have been reallocated by ofpbuf_put(). */
2261 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2262 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2265 /* Add new padding as needed */
2266 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2270 ofpbuf_uninit(&maskbuf
);
2272 nl_msg_end_nested(actions
, start_ofs
);
2277 struct ovs_action_push_vlan push
;
2278 int tpid
= ETH_TYPE_VLAN
;
2283 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2284 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2285 &vid
, &pcp
, &cfi
, &n
)
2286 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2287 &tpid
, &vid
, &pcp
, &n
)
2288 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2289 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2290 if ((vid
& ~(VLAN_VID_MASK
>> VLAN_VID_SHIFT
)) != 0
2291 || (pcp
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) != 0) {
2294 push
.vlan_tpid
= htons(tpid
);
2295 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2296 | (pcp
<< VLAN_PCP_SHIFT
)
2297 | (cfi
? VLAN_CFI
: 0));
2298 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2299 &push
, sizeof push
);
2305 if (!strncmp(s
, "pop_vlan", 8)) {
2306 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2311 unsigned long long int meter_id
;
2314 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2315 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2324 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2325 && percentage
>= 0. && percentage
<= 100.0) {
2326 size_t sample_ofs
, actions_ofs
;
2329 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2330 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2331 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2332 (probability
<= 0 ? 0
2333 : probability
>= UINT32_MAX
? UINT32_MAX
2336 actions_ofs
= nl_msg_start_nested(actions
,
2337 OVS_SAMPLE_ATTR_ACTIONS
);
2338 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2343 nl_msg_end_nested(actions
, actions_ofs
);
2344 nl_msg_end_nested(actions
, sample_ofs
);
2346 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2351 if (!strncmp(s
, "clone(", 6)) {
2355 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2356 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2361 nl_msg_end_nested(actions
, actions_ofs
);
2367 if (!strncmp(s
, "push_nsh(", 9)) {
2368 int retval
= parse_odp_push_nsh_action(s
, actions
);
2378 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2379 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2388 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2389 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2395 if (!strncmp(s
, "ct_clear", 8)) {
2396 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2404 retval
= parse_conntrack_action(s
, actions
);
2411 struct ovs_action_push_tnl data
;
2414 n
= ovs_parse_tnl_push(s
, &data
);
2416 odp_put_tnl_push_action(actions
, &data
);
2425 /* Parses the string representation of datapath actions, in the format output
2426 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2427 * value. On success, the ODP actions are appended to 'actions' as a series of
2428 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2429 * way, 'actions''s data might be reallocated. */
2431 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2432 struct ofpbuf
*actions
)
2436 if (!strcasecmp(s
, "drop")) {
2440 old_size
= actions
->size
;
2444 s
+= strspn(s
, delimiters
);
2449 retval
= parse_odp_action(s
, port_names
, actions
);
2450 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2451 actions
->size
= old_size
;
2460 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2461 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2464 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2465 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2466 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2467 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2468 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2469 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2470 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2471 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2472 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2473 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2474 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2475 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2476 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2477 .next
= ovs_vxlan_ext_attr_lens
,
2478 .next_max
= OVS_VXLAN_EXT_MAX
},
2479 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2480 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2481 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2484 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2485 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2486 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2487 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2488 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2489 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2490 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2491 .next
= ovs_tun_key_attr_lens
,
2492 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2493 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2494 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2495 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2496 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2497 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2498 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2499 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2500 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2501 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2502 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2503 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2504 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2505 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2506 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2507 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2508 [OVS_KEY_ATTR_ND_EXTENSIONS
] = { .len
= sizeof(struct ovs_key_nd_extensions
) },
2509 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2510 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2511 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2512 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2513 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2514 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2515 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2516 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2517 .next
= ovs_nsh_key_attr_lens
,
2518 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2521 /* Returns the correct length of the payload for a flow key attribute of the
2522 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2523 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2524 * payload is a nested type. */
2526 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2528 if (type
> max_type
) {
2529 return ATTR_LEN_INVALID
;
2532 return tbl
[type
].len
;
2536 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2538 size_t len
= nl_attr_get_size(a
);
2540 const uint8_t *unspec
;
2543 unspec
= nl_attr_get(a
);
2544 for (i
= 0; i
< len
; i
++) {
2546 ds_put_char(ds
, ' ');
2548 ds_put_format(ds
, "%02x", unspec
[i
]);
2554 ovs_frag_type_to_string(enum ovs_frag_type type
)
2557 case OVS_FRAG_TYPE_NONE
:
2559 case OVS_FRAG_TYPE_FIRST
:
2561 case OVS_FRAG_TYPE_LATER
:
2563 case __OVS_FRAG_TYPE_MAX
:
2569 enum odp_key_fitness
2570 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2571 struct nsh_hdr
*nsh_hdr
, size_t size
)
2574 const struct nlattr
*a
;
2575 bool unknown
= false;
2579 bool has_md1
= false;
2580 bool has_md2
= false;
2582 memset(nsh_hdr
, 0, size
);
2584 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2585 uint16_t type
= nl_attr_type(a
);
2586 size_t len
= nl_attr_get_size(a
);
2587 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2588 OVS_NSH_KEY_ATTR_MAX
, type
);
2590 if (len
!= expected_len
&& expected_len
>= 0) {
2591 return ODP_FIT_ERROR
;
2595 case OVS_NSH_KEY_ATTR_BASE
: {
2596 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2597 nsh_hdr
->next_proto
= base
->np
;
2598 nsh_hdr
->md_type
= base
->mdtype
;
2599 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2600 flags
= base
->flags
;
2604 case OVS_NSH_KEY_ATTR_MD1
: {
2605 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2606 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2608 mdlen
= nl_attr_get_size(a
);
2609 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2610 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2611 return ODP_FIT_ERROR
;
2613 memcpy(md1_dst
, md1
, mdlen
);
2616 case OVS_NSH_KEY_ATTR_MD2
: {
2617 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2618 const uint8_t *md2
= nl_attr_get(a
);
2620 mdlen
= nl_attr_get_size(a
);
2621 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2622 return ODP_FIT_ERROR
;
2624 memcpy(md2_dst
, md2
, mdlen
);
2628 /* Allow this to show up as unexpected, if there are unknown
2629 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2636 return ODP_FIT_TOO_MUCH
;
2639 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2640 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2641 return ODP_FIT_ERROR
;
2644 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2645 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2647 return ODP_FIT_PERFECT
;
2650 /* Reports the error 'msg', which is formatted as with printf().
2652 * If 'errorp' is nonnull, then some the wants the error report to come
2653 * directly back to it, so the function stores the error message into '*errorp'
2654 * (after first freeing it in case there's something there already).
2656 * Otherwise, logs the message at WARN level, rate-limited. */
2657 static void OVS_PRINTF_FORMAT(3, 4)
2658 odp_parse_error(struct vlog_rate_limit
*rl
, char **errorp
,
2659 const char *msg
, ...)
2661 if (OVS_UNLIKELY(errorp
)) {
2665 va_start(args
, msg
);
2666 *errorp
= xvasprintf(msg
, args
);
2668 } else if (!VLOG_DROP_WARN(rl
)) {
2670 va_start(args
, msg
);
2671 char *error
= xvasprintf(msg
, args
);
2674 VLOG_WARN("%s", error
);
2680 /* Parses OVS_KEY_ATTR_NSH attribute 'attr' into 'nsh' and 'nsh_mask' and
2681 * returns fitness. If 'errorp' is nonnull and the function returns
2682 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
2683 enum odp_key_fitness
2684 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2685 struct ovs_key_nsh
*nsh_mask
, char **errorp
)
2687 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2693 const struct nlattr
*a
;
2694 bool unknown
= false;
2695 bool has_md1
= false;
2697 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2698 uint16_t type
= nl_attr_type(a
);
2699 size_t len
= nl_attr_get_size(a
);
2700 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2701 OVS_NSH_KEY_ATTR_MAX
, type
);
2706 if (len
!= expected_len
) {
2707 odp_parse_error(&rl
, errorp
, "NSH %s attribute %"PRIu16
" "
2708 "should have length %d but actually has "
2710 nsh_mask
? "mask" : "key",
2711 type
, expected_len
, len
);
2712 return ODP_FIT_ERROR
;
2717 case OVS_NSH_KEY_ATTR_UNSPEC
:
2719 case OVS_NSH_KEY_ATTR_BASE
: {
2720 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2721 nsh
->flags
= base
->flags
;
2722 nsh
->ttl
= base
->ttl
;
2723 nsh
->mdtype
= base
->mdtype
;
2725 nsh
->path_hdr
= base
->path_hdr
;
2726 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2727 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2728 nsh_mask
->flags
= base_mask
->flags
;
2729 nsh_mask
->ttl
= base_mask
->ttl
;
2730 nsh_mask
->mdtype
= base_mask
->mdtype
;
2731 nsh_mask
->np
= base_mask
->np
;
2732 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2736 case OVS_NSH_KEY_ATTR_MD1
: {
2737 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2739 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2740 if (len
== 2 * sizeof(*md1
)) {
2741 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2742 memcpy(nsh_mask
->context
, md1_mask
->context
,
2747 case OVS_NSH_KEY_ATTR_MD2
:
2749 /* Allow this to show up as unexpected, if there are unknown
2750 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2757 return ODP_FIT_TOO_MUCH
;
2760 if (has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
&& !nsh_mask
) {
2761 odp_parse_error(&rl
, errorp
, "OVS_NSH_KEY_ATTR_MD1 present but "
2762 "declared mdtype %"PRIu8
" is not %d (NSH_M_TYPE1)",
2763 nsh
->mdtype
, NSH_M_TYPE1
);
2764 return ODP_FIT_ERROR
;
2767 return ODP_FIT_PERFECT
;
2770 /* Parses OVS_KEY_ATTR_TUNNEL attribute 'attr' into 'tun' and returns fitness.
2771 * If the attribute is a key, 'is_mask' should be false; if it is a mask,
2772 * 'is_mask' should be true. If 'errorp' is nonnull and the function returns
2773 * ODP_FIT_ERROR, stores a malloc()'d error message in '*errorp'. */
2774 static enum odp_key_fitness
2775 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2776 struct flow_tnl
*tun
, char **errorp
)
2778 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
2780 const struct nlattr
*a
;
2782 bool unknown
= false;
2784 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2785 uint16_t type
= nl_attr_type(a
);
2786 size_t len
= nl_attr_get_size(a
);
2787 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2788 OVS_TUNNEL_ATTR_MAX
, type
);
2790 if (len
!= expected_len
&& expected_len
>= 0) {
2791 odp_parse_error(&rl
, errorp
, "tunnel key attribute %"PRIu16
" "
2792 "should have length %d but actually has %"PRIuSIZE
,
2793 type
, expected_len
, len
);
2794 return ODP_FIT_ERROR
;
2798 case OVS_TUNNEL_KEY_ATTR_ID
:
2799 tun
->tun_id
= nl_attr_get_be64(a
);
2800 tun
->flags
|= FLOW_TNL_F_KEY
;
2802 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2803 tun
->ip_src
= nl_attr_get_be32(a
);
2805 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2806 tun
->ip_dst
= nl_attr_get_be32(a
);
2808 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2809 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2811 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2812 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2814 case OVS_TUNNEL_KEY_ATTR_TOS
:
2815 tun
->ip_tos
= nl_attr_get_u8(a
);
2817 case OVS_TUNNEL_KEY_ATTR_TTL
:
2818 tun
->ip_ttl
= nl_attr_get_u8(a
);
2821 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2822 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2824 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2825 tun
->flags
|= FLOW_TNL_F_CSUM
;
2827 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2828 tun
->tp_src
= nl_attr_get_be16(a
);
2830 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2831 tun
->tp_dst
= nl_attr_get_be16(a
);
2833 case OVS_TUNNEL_KEY_ATTR_OAM
:
2834 tun
->flags
|= FLOW_TNL_F_OAM
;
2836 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2837 static const struct nl_policy vxlan_opts_policy
[] = {
2838 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2840 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2842 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2843 odp_parse_error(&rl
, errorp
, "error parsing VXLAN options");
2844 return ODP_FIT_ERROR
;
2847 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2848 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2850 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2851 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2856 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2857 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2859 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
2860 const struct erspan_metadata
*opts
= nl_attr_get(a
);
2862 tun
->erspan_ver
= opts
->version
;
2863 if (tun
->erspan_ver
== 1) {
2864 tun
->erspan_idx
= ntohl(opts
->u
.index
);
2865 } else if (tun
->erspan_ver
== 2) {
2866 tun
->erspan_dir
= opts
->u
.md2
.dir
;
2867 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
2869 VLOG_WARN("%s invalid erspan version\n", __func__
);
2875 /* Allow this to show up as unexpected, if there are unknown
2876 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2883 odp_parse_error(&rl
, errorp
, "tunnel options missing TTL");
2884 return ODP_FIT_ERROR
;
2887 return ODP_FIT_TOO_MUCH
;
2889 return ODP_FIT_PERFECT
;
2892 /* Parses OVS_KEY_ATTR_TUNNEL key attribute 'attr' into 'tun' and returns
2893 * fitness. The attribute should be a key (not a mask). If 'errorp' is
2894 * nonnull, stores NULL into '*errorp' on success, otherwise a malloc()'d error
2896 enum odp_key_fitness
2897 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
,
2903 memset(tun
, 0, sizeof *tun
);
2904 return odp_tun_key_from_attr__(attr
, false, tun
, errorp
);
2908 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2909 const struct flow_tnl
*tun_flow_key
,
2910 const struct ofpbuf
*key_buf
, const char *tnl_type
)
2914 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2916 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2917 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2918 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2920 if (tun_key
->ip_src
) {
2921 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2923 if (tun_key
->ip_dst
) {
2924 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2926 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2927 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2929 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2930 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2932 if (tun_key
->ip_tos
) {
2933 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2935 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2936 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2937 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2939 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2940 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2942 if (tun_key
->tp_src
) {
2943 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2945 if (tun_key
->tp_dst
) {
2946 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2948 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2949 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2952 /* If tnl_type is set to a particular type of output tunnel,
2953 * only put its relevant tunnel metadata to the nlattr.
2954 * If tnl_type is NULL, put tunnel metadata according to the
2957 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
2958 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
2959 size_t vxlan_opts_ofs
;
2961 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2962 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2963 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2964 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2967 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
2968 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2971 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
2972 !strcmp(tnl_type
, "ip6erspan")) &&
2973 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
2974 struct erspan_metadata opts
;
2976 opts
.version
= tun_key
->erspan_ver
;
2977 if (opts
.version
== 1) {
2978 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
2980 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
2981 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
2983 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
2984 &opts
, sizeof(opts
));
2987 nl_msg_end_nested(a
, tun_key_ofs
);
2991 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2994 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2995 * that -1 becomes all-1-bits and 0 does not change. */
2996 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2997 uint32_t u32
= constant
;
2998 uint8_t u8
= constant
;
2999 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
3002 case OVS_KEY_ATTR_UNSPEC
:
3003 case OVS_KEY_ATTR_ENCAP
:
3004 case __OVS_KEY_ATTR_MAX
:
3008 case OVS_KEY_ATTR_PRIORITY
:
3009 case OVS_KEY_ATTR_IN_PORT
:
3010 case OVS_KEY_ATTR_ETHERNET
:
3011 case OVS_KEY_ATTR_VLAN
:
3012 case OVS_KEY_ATTR_ETHERTYPE
:
3013 case OVS_KEY_ATTR_IPV4
:
3014 case OVS_KEY_ATTR_TCP
:
3015 case OVS_KEY_ATTR_UDP
:
3016 case OVS_KEY_ATTR_ICMP
:
3017 case OVS_KEY_ATTR_ICMPV6
:
3018 case OVS_KEY_ATTR_ND
:
3019 case OVS_KEY_ATTR_ND_EXTENSIONS
:
3020 case OVS_KEY_ATTR_SKB_MARK
:
3021 case OVS_KEY_ATTR_TUNNEL
:
3022 case OVS_KEY_ATTR_SCTP
:
3023 case OVS_KEY_ATTR_DP_HASH
:
3024 case OVS_KEY_ATTR_RECIRC_ID
:
3025 case OVS_KEY_ATTR_MPLS
:
3026 case OVS_KEY_ATTR_CT_STATE
:
3027 case OVS_KEY_ATTR_CT_ZONE
:
3028 case OVS_KEY_ATTR_CT_MARK
:
3029 case OVS_KEY_ATTR_CT_LABELS
:
3030 case OVS_KEY_ATTR_PACKET_TYPE
:
3031 case OVS_KEY_ATTR_NSH
:
3032 return is_all_byte(mask
, size
, u8
);
3034 case OVS_KEY_ATTR_TCP_FLAGS
:
3035 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
3037 case OVS_KEY_ATTR_IPV6
: {
3038 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
3039 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
3040 == htonl(IPV6_LABEL_MASK
& u32
)
3041 && ipv6_mask
->ipv6_proto
== u8
3042 && ipv6_mask
->ipv6_tclass
== u8
3043 && ipv6_mask
->ipv6_hlimit
== u8
3044 && ipv6_mask
->ipv6_frag
== u8
3045 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
3046 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
3049 case OVS_KEY_ATTR_ARP
:
3050 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
3052 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
3053 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
3056 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
3057 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
3062 /* The caller must already have verified that 'ma' has a correct length.
3064 * The main purpose of this function is formatting, to allow code to figure out
3065 * whether the mask can be omitted. It doesn't try hard for attributes that
3066 * contain sub-attributes, etc., because normally those would be broken down
3067 * further for formatting. */
3069 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
3071 return odp_mask_is_constant__(nl_attr_type(ma
),
3072 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
3075 /* The caller must already have verified that 'size' is a correct length for
3078 * The main purpose of this function is formatting, to allow code to figure out
3079 * whether the mask can be omitted. It doesn't try hard for attributes that
3080 * contain sub-attributes, etc., because normally those would be broken down
3081 * further for formatting. */
3083 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
3085 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3088 /* The caller must already have verified that 'ma' has a correct length. */
3090 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3092 enum ovs_key_attr attr
= nl_attr_type(ma
);
3093 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3097 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3100 struct odp_portno_names
*odp_portno_names
;
3102 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3103 odp_portno_names
->port_no
= port_no
;
3104 odp_portno_names
->name
= xstrdup(port_name
);
3105 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3106 hash_odp_port(port_no
));
3110 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3113 struct odp_portno_names
*odp_portno_names
;
3115 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3116 hash_odp_port(port_no
), portno_names
) {
3117 if (odp_portno_names
->port_no
== port_no
) {
3118 return odp_portno_names
->name
;
3126 odp_portno_names_destroy(struct hmap
*portno_names
)
3128 struct odp_portno_names
*odp_portno_names
;
3130 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3131 free(odp_portno_names
->name
);
3132 free(odp_portno_names
);
3137 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3140 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3142 ds_put_cstr(s
, name
);
3144 ds_put_format(s
, "%"PRIu32
, port_no
);
3148 /* Format helpers. */
3151 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3152 const struct eth_addr
*mask
, bool verbose
)
3154 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3156 if (verbose
|| !mask_empty
) {
3157 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3160 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3162 ds_put_format(ds
, "%s=", name
);
3163 eth_format_masked(key
, mask
, ds
);
3164 ds_put_char(ds
, ',');
3171 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3172 const ovs_be64
*mask
, bool verbose
)
3174 bool mask_empty
= mask
&& !*mask
;
3176 if (verbose
|| !mask_empty
) {
3177 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3179 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3180 if (!mask_full
) { /* Partially masked. */
3181 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3183 ds_put_char(ds
, ',');
3188 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3189 const ovs_be32
*mask
, bool verbose
)
3191 bool mask_empty
= mask
&& !*mask
;
3193 if (verbose
|| !mask_empty
) {
3194 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3196 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3197 if (!mask_full
) { /* Partially masked. */
3198 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3200 ds_put_char(ds
, ',');
3205 format_in6_addr(struct ds
*ds
, const char *name
,
3206 const struct in6_addr
*key
,
3207 const struct in6_addr
*mask
,
3210 char buf
[INET6_ADDRSTRLEN
];
3211 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3213 if (verbose
|| !mask_empty
) {
3214 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3216 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3217 ds_put_format(ds
, "%s=%s", name
, buf
);
3218 if (!mask_full
) { /* Partially masked. */
3219 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3220 ds_put_format(ds
, "/%s", buf
);
3222 ds_put_char(ds
, ',');
3227 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3228 const ovs_be32
*mask
, bool verbose
)
3230 bool mask_empty
= mask
&& !*mask
;
3232 if (verbose
|| !mask_empty
) {
3233 bool mask_full
= !mask
3234 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3236 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3237 if (!mask_full
) { /* Partially masked. */
3238 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3240 ds_put_char(ds
, ',');
3245 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3246 const uint8_t *mask
, bool verbose
)
3248 bool mask_empty
= mask
&& !*mask
;
3250 if (verbose
|| !mask_empty
) {
3251 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3253 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3254 if (!mask_full
) { /* Partially masked. */
3255 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3257 ds_put_char(ds
, ',');
3262 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3263 const uint8_t *mask
, bool verbose
)
3265 bool mask_empty
= mask
&& !*mask
;
3267 if (verbose
|| !mask_empty
) {
3268 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3270 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3271 if (!mask_full
) { /* Partially masked. */
3272 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3274 ds_put_char(ds
, ',');
3279 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3280 const ovs_be16
*mask
, bool verbose
)
3282 bool mask_empty
= mask
&& !*mask
;
3284 if (verbose
|| !mask_empty
) {
3285 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3287 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3288 if (!mask_full
) { /* Partially masked. */
3289 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3291 ds_put_char(ds
, ',');
3296 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3297 const ovs_be16
*mask
, bool verbose
)
3299 bool mask_empty
= mask
&& !*mask
;
3301 if (verbose
|| !mask_empty
) {
3302 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3304 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3305 if (!mask_full
) { /* Partially masked. */
3306 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3308 ds_put_char(ds
, ',');
3313 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3314 const uint16_t *mask
, bool verbose
)
3316 bool mask_empty
= mask
&& !*mask
;
3318 if (verbose
|| !mask_empty
) {
3319 ds_put_cstr(ds
, name
);
3320 ds_put_char(ds
, '(');
3322 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3323 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3324 } else { /* Fully masked. */
3325 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3327 ds_put_cstr(ds
, "),");
3332 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3333 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3337 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3338 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3339 expected_len
!= ATTR_LEN_NESTED
) {
3341 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3342 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3344 if (bad_key_len
|| bad_mask_len
) {
3346 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3349 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3350 nl_attr_get_size(a
), expected_len
);
3352 format_generic_odp_key(a
, ds
);
3354 ds_put_char(ds
, '/');
3356 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3357 nl_attr_get_size(ma
), expected_len
);
3359 format_generic_odp_key(ma
, ds
);
3361 ds_put_char(ds
, ')');
3370 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3371 const struct nlattr
*ma
)
3373 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3374 format_generic_odp_key(a
, ds
);
3375 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3376 ds_put_char(ds
, '/');
3377 format_generic_odp_key(ma
, ds
);
3379 ds_put_cstr(ds
, "),");
3383 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3384 const struct nlattr
*mask_attr
, struct ds
*ds
,
3388 const struct nlattr
*a
;
3391 ofpbuf_init(&ofp
, 100);
3392 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3393 uint16_t type
= nl_attr_type(a
);
3394 const struct nlattr
*ma
= NULL
;
3397 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3398 nl_attr_get_size(mask_attr
), type
);
3400 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3406 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3407 OVS_VXLAN_EXT_MAX
, true)) {
3412 case OVS_VXLAN_EXT_GBP
: {
3413 uint32_t key
= nl_attr_get_u32(a
);
3414 ovs_be16 id
, id_mask
;
3415 uint8_t flags
, flags_mask
= 0;
3417 id
= htons(key
& 0xFFFF);
3418 flags
= (key
>> 16) & 0xFF;
3420 uint32_t mask
= nl_attr_get_u32(ma
);
3421 id_mask
= htons(mask
& 0xFFFF);
3422 flags_mask
= (mask
>> 16) & 0xFF;
3425 ds_put_cstr(ds
, "gbp(");
3426 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3427 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3429 ds_put_cstr(ds
, "),");
3434 format_unknown_key(ds
, a
, ma
);
3440 ofpbuf_uninit(&ofp
);
3444 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3445 const struct nlattr
*mask_attr
, struct ds
*ds
,
3448 const struct erspan_metadata
*opts
, *mask
;
3449 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3451 opts
= nl_attr_get(attr
);
3452 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3454 ver
= (uint8_t)opts
->version
;
3456 ver_ma
= (uint8_t)mask
->version
;
3459 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3461 if (opts
->version
== 1) {
3463 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3464 ntohl(opts
->u
.index
),
3465 ntohl(mask
->u
.index
));
3467 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3469 } else if (opts
->version
== 2) {
3470 dir
= opts
->u
.md2
.dir
;
3471 hwid
= opts
->u
.md2
.hwid
;
3473 dir_ma
= mask
->u
.md2
.dir
;
3474 hwid_ma
= mask
->u
.md2
.hwid
;
3477 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3478 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3483 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3486 format_geneve_opts(const struct geneve_opt
*opt
,
3487 const struct geneve_opt
*mask
, int opts_len
,
3488 struct ds
*ds
, bool verbose
)
3490 while (opts_len
> 0) {
3492 uint8_t data_len
, data_len_mask
;
3494 if (opts_len
< sizeof *opt
) {
3495 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3496 opts_len
, sizeof *opt
);
3500 data_len
= opt
->length
* 4;
3502 if (mask
->length
== 0x1f) {
3503 data_len_mask
= UINT8_MAX
;
3505 data_len_mask
= mask
->length
;
3508 len
= sizeof *opt
+ data_len
;
3509 if (len
> opts_len
) {
3510 ds_put_format(ds
, "opt len %u greater than remaining %u",
3515 ds_put_char(ds
, '{');
3516 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3518 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3519 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3521 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3522 ds_put_hex(ds
, opt
+ 1, data_len
);
3523 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3524 ds_put_char(ds
, '/');
3525 ds_put_hex(ds
, mask
+ 1, data_len
);
3530 ds_put_char(ds
, '}');
3532 opt
+= len
/ sizeof(*opt
);
3534 mask
+= len
/ sizeof(*opt
);
3541 format_odp_tun_geneve(const struct nlattr
*attr
,
3542 const struct nlattr
*mask_attr
, struct ds
*ds
,
3545 int opts_len
= nl_attr_get_size(attr
);
3546 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3547 const struct geneve_opt
*mask
= mask_attr
?
3548 nl_attr_get(mask_attr
) : NULL
;
3550 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3551 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3552 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3556 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3560 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3564 const struct nlattr
*a
;
3565 struct ovs_key_nsh nsh
;
3566 struct ovs_key_nsh nsh_mask
;
3568 memset(&nsh
, 0, sizeof nsh
);
3569 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3571 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3572 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3573 const struct nlattr
*ma
= NULL
;
3576 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3577 nl_attr_get_size(mask_attr
), type
);
3580 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3581 OVS_NSH_KEY_ATTR_MAX
, true)) {
3586 case OVS_NSH_KEY_ATTR_UNSPEC
:
3588 case OVS_NSH_KEY_ATTR_BASE
: {
3589 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3590 const struct ovs_nsh_key_base
*base_mask
3591 = ma
? nl_attr_get(ma
) : NULL
;
3592 nsh
.flags
= base
->flags
;
3593 nsh
.ttl
= base
->ttl
;
3594 nsh
.mdtype
= base
->mdtype
;
3596 nsh
.path_hdr
= base
->path_hdr
;
3598 nsh_mask
.flags
= base_mask
->flags
;
3599 nsh_mask
.ttl
= base_mask
->ttl
;
3600 nsh_mask
.mdtype
= base_mask
->mdtype
;
3601 nsh_mask
.np
= base_mask
->np
;
3602 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3606 case OVS_NSH_KEY_ATTR_MD1
: {
3607 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3608 const struct ovs_nsh_key_md1
*md1_mask
3609 = ma
? nl_attr_get(ma
) : NULL
;
3610 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3612 memcpy(nsh_mask
.context
, md1_mask
->context
,
3613 sizeof md1_mask
->context
);
3617 case OVS_NSH_KEY_ATTR_MD2
:
3618 case __OVS_NSH_KEY_ATTR_MAX
:
3620 /* No support for matching other metadata formats yet. */
3626 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3628 format_nsh_key(ds
, &nsh
);
3633 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3634 struct ds
*ds
, bool verbose
)
3637 const struct nlattr
*a
;
3639 uint16_t mask_flags
= 0;
3642 ofpbuf_init(&ofp
, 100);
3643 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3644 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3645 const struct nlattr
*ma
= NULL
;
3648 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3649 nl_attr_get_size(mask_attr
), type
);
3651 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3652 OVS_TUNNEL_KEY_ATTR_MAX
,
3657 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3658 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3663 case OVS_TUNNEL_KEY_ATTR_ID
:
3664 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3665 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3666 flags
|= FLOW_TNL_F_KEY
;
3668 mask_flags
|= FLOW_TNL_F_KEY
;
3671 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3672 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3673 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3675 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3676 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3677 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3679 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3680 struct in6_addr ipv6_src
;
3681 ipv6_src
= nl_attr_get_in6_addr(a
);
3682 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3683 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3686 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3687 struct in6_addr ipv6_dst
;
3688 ipv6_dst
= nl_attr_get_in6_addr(a
);
3689 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3690 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3693 case OVS_TUNNEL_KEY_ATTR_TOS
:
3694 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3695 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3697 case OVS_TUNNEL_KEY_ATTR_TTL
:
3698 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3699 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3701 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3702 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3704 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3705 flags
|= FLOW_TNL_F_CSUM
;
3707 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3708 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3709 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3711 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3712 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3713 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3715 case OVS_TUNNEL_KEY_ATTR_OAM
:
3716 flags
|= FLOW_TNL_F_OAM
;
3718 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3719 ds_put_cstr(ds
, "vxlan(");
3720 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3721 ds_put_cstr(ds
, "),");
3723 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3724 ds_put_cstr(ds
, "geneve(");
3725 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3726 ds_put_cstr(ds
, "),");
3728 case OVS_TUNNEL_KEY_ATTR_PAD
:
3730 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3731 ds_put_cstr(ds
, "erspan(");
3732 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3733 ds_put_cstr(ds
, "),");
3735 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3737 format_unknown_key(ds
, a
, ma
);
3742 /* Flags can have a valid mask even if the attribute is not set, so
3743 * we need to collect these separately. */
3745 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3746 switch (nl_attr_type(a
)) {
3747 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3748 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3750 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3751 mask_flags
|= FLOW_TNL_F_CSUM
;
3753 case OVS_TUNNEL_KEY_ATTR_OAM
:
3754 mask_flags
|= FLOW_TNL_F_OAM
;
3760 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3763 ofpbuf_uninit(&ofp
);
3767 odp_ct_state_to_string(uint32_t flag
)
3770 case OVS_CS_F_REPLY_DIR
:
3772 case OVS_CS_F_TRACKED
:
3776 case OVS_CS_F_ESTABLISHED
:
3778 case OVS_CS_F_RELATED
:
3780 case OVS_CS_F_INVALID
:
3782 case OVS_CS_F_SRC_NAT
:
3784 case OVS_CS_F_DST_NAT
:
3792 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
3793 const uint8_t *mask
, bool verbose OVS_UNUSED
)
3795 bool mask_empty
= mask
&& !*mask
;
3796 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3798 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3799 if (!mask_empty
&& !mask_full
) {
3800 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
3802 } else if (!mask_empty
) {
3803 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
3808 mask_empty(const struct nlattr
*ma
)
3816 mask
= nl_attr_get(ma
);
3817 n
= nl_attr_get_size(ma
);
3819 return is_all_zeros(mask
, n
);
3822 /* The caller must have already verified that 'a' and 'ma' have correct
3825 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
3826 const struct hmap
*portno_names
, struct ds
*ds
,
3829 enum ovs_key_attr attr
= nl_attr_type(a
);
3830 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3833 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
3835 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
3837 ds_put_char(ds
, '(');
3839 case OVS_KEY_ATTR_ENCAP
:
3840 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
3841 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
3842 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
3844 } else if (nl_attr_get_size(a
)) {
3845 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
3850 case OVS_KEY_ATTR_PRIORITY
:
3851 case OVS_KEY_ATTR_SKB_MARK
:
3852 case OVS_KEY_ATTR_DP_HASH
:
3853 case OVS_KEY_ATTR_RECIRC_ID
:
3854 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3856 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3860 case OVS_KEY_ATTR_CT_MARK
:
3861 if (verbose
|| !mask_empty(ma
)) {
3862 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3864 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3869 case OVS_KEY_ATTR_CT_STATE
:
3871 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3873 ds_put_format(ds
, "/%#"PRIx32
,
3874 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
3876 } else if (!is_exact
) {
3877 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
3879 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
3882 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
3886 case OVS_KEY_ATTR_CT_ZONE
:
3887 if (verbose
|| !mask_empty(ma
)) {
3888 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
3890 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
3895 case OVS_KEY_ATTR_CT_LABELS
: {
3896 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
3897 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3899 format_u128(ds
, value
, mask
, verbose
);
3903 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
3904 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
3905 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3907 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3908 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3909 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3911 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
3913 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
3919 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
3920 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
3921 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3923 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3925 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3927 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3929 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3931 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3937 case OVS_KEY_ATTR_TUNNEL
:
3938 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3941 case OVS_KEY_ATTR_IN_PORT
:
3943 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3945 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3947 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3952 case OVS_KEY_ATTR_PACKET_TYPE
: {
3953 ovs_be32 value
= nl_attr_get_be32(a
);
3954 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3956 ovs_be16 ns
= htons(pt_ns(value
));
3957 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3958 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3960 ovs_be16 ns_type
= pt_ns_type_be(value
);
3961 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3962 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3968 case OVS_KEY_ATTR_ETHERNET
: {
3969 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3970 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3972 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3973 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3977 case OVS_KEY_ATTR_VLAN
:
3978 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3979 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3982 case OVS_KEY_ATTR_MPLS
: {
3983 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3984 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3985 size_t size
= nl_attr_get_size(a
);
3987 if (!size
|| size
% sizeof *mpls_key
) {
3988 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3992 mpls_mask
= nl_attr_get(ma
);
3993 if (size
!= nl_attr_get_size(ma
)) {
3994 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3995 "mask length %"PRIuSIZE
")",
3996 size
, nl_attr_get_size(ma
));
4000 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
4003 case OVS_KEY_ATTR_ETHERTYPE
:
4004 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
4006 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
4010 case OVS_KEY_ATTR_IPV4
: {
4011 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
4012 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4014 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
4015 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
4016 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
4018 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
4019 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
4020 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
4025 case OVS_KEY_ATTR_IPV6
: {
4026 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
4027 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4029 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
4031 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
4033 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
4035 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
4037 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
4039 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
4041 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
4046 /* These have the same structure and format. */
4047 case OVS_KEY_ATTR_TCP
:
4048 case OVS_KEY_ATTR_UDP
:
4049 case OVS_KEY_ATTR_SCTP
: {
4050 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
4051 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4053 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
4054 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
4058 case OVS_KEY_ATTR_TCP_FLAGS
:
4060 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
4061 ntohs(nl_attr_get_be16(a
)),
4062 TCP_FLAGS(nl_attr_get_be16(ma
)),
4063 TCP_FLAGS(OVS_BE16_MAX
));
4065 format_flags(ds
, packet_tcp_flag_to_string
,
4066 ntohs(nl_attr_get_be16(a
)), '|');
4070 case OVS_KEY_ATTR_ICMP
: {
4071 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
4072 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4074 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
4075 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
4079 case OVS_KEY_ATTR_ICMPV6
: {
4080 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
4081 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4083 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
4085 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4090 case OVS_KEY_ATTR_ARP
: {
4091 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4092 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4094 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4095 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4096 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4097 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4098 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4102 case OVS_KEY_ATTR_ND
: {
4103 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4104 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4106 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4108 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4109 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4114 case OVS_KEY_ATTR_ND_EXTENSIONS
: {
4115 const struct ovs_key_nd_extensions
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4116 const struct ovs_key_nd_extensions
*key
= nl_attr_get(a
);
4119 format_be32_masked(ds
, &first
, "nd_reserved", key
->nd_reserved
,
4121 ds_put_char(ds
, ',');
4123 format_u8u(ds
, "nd_options_type", key
->nd_options_type
,
4124 MASK(mask
, nd_options_type
), verbose
);
4129 case OVS_KEY_ATTR_NSH
: {
4130 format_odp_nsh_attr(a
, ma
, ds
);
4133 case OVS_KEY_ATTR_UNSPEC
:
4134 case __OVS_KEY_ATTR_MAX
:
4136 format_generic_odp_key(a
, ds
);
4138 ds_put_char(ds
, '/');
4139 format_generic_odp_key(ma
, ds
);
4143 ds_put_char(ds
, ')');
4147 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4148 const struct hmap
*portno_names
, struct ds
*ds
,
4151 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4152 OVS_KEY_ATTR_MAX
, false)) {
4153 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4157 static struct nlattr
*
4158 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4159 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4161 const struct nlattr
*a
;
4163 int type
= nl_attr_type(key
);
4164 int size
= nl_attr_get_size(key
);
4166 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4167 nl_msg_put_unspec_zero(ofp
, type
, size
);
4171 if (tbl
[type
].next
) {
4172 const struct attr_len_tbl
*entry
= &tbl
[type
];
4174 max
= entry
->next_max
;
4177 nested_mask
= nl_msg_start_nested(ofp
, type
);
4178 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4179 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4181 nl_msg_end_nested(ofp
, nested_mask
);
4188 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4189 const ovs_32aligned_u128
*mask
, bool verbose
)
4191 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4192 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4193 ds_put_hex(ds
, &value
, sizeof value
);
4194 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4195 value
= hton128(get_32aligned_u128(mask
));
4196 ds_put_char(ds
, '/');
4197 ds_put_hex(ds
, &value
, sizeof value
);
4202 /* Read the string from 's_' as a 128-bit value. If the string contains
4203 * a "/", the rest of the string will be treated as a 128-bit mask.
4205 * If either the value or mask is larger than 64 bits, the string must
4206 * be in hexadecimal.
4209 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4211 char *s
= CONST_CAST(char *, s_
);
4215 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4216 *value
= ntoh128(be_value
);
4221 if (ovs_scan(s
, "/%n", &n
)) {
4225 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4226 sizeof be_mask
, &s
);
4230 *mask
= ntoh128(be_mask
);
4232 *mask
= OVS_U128_MAX
;
4242 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4246 if (ovs_scan(s
, "ufid:")) {
4249 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4261 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4263 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4266 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4267 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4268 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4269 * non-null, translates odp port number to its name. */
4271 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4272 const struct nlattr
*mask
, size_t mask_len
,
4273 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4276 const struct nlattr
*a
;
4278 bool has_ethtype_key
= false;
4279 bool has_packet_type_key
= false;
4281 bool first_field
= true;
4283 ofpbuf_init(&ofp
, 100);
4284 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4285 int attr_type
= nl_attr_type(a
);
4286 const struct nlattr
*ma
= (mask
&& mask_len
4287 ? nl_attr_find__(mask
, mask_len
,
4290 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4291 OVS_KEY_ATTR_MAX
, false)) {
4295 bool is_nested_attr
;
4296 bool is_wildcard
= false;
4298 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4299 has_ethtype_key
= true;
4300 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4301 has_packet_type_key
= true;
4304 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4305 OVS_KEY_ATTR_MAX
, attr_type
) ==
4308 if (mask
&& mask_len
) {
4309 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4310 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4313 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4314 if (is_wildcard
&& !ma
) {
4315 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4320 ds_put_char(ds
, ',');
4322 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4323 first_field
= false;
4324 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4325 && !has_packet_type_key
) {
4326 /* This special case reflects differences between the kernel
4327 * and userspace datapaths regarding the root type of the
4328 * packet being matched (typically Ethernet but some tunnels
4329 * can encapsulate IPv4 etc.). The kernel datapath does not
4330 * have an explicit way to indicate packet type; instead:
4332 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4333 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4334 * Ethertype encoded in the Ethernet header.
4336 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4337 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4338 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4339 * packet is an IPv4 packet).
4341 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4342 * all-wildcarded, it is important to print it.
4344 * On the other hand, the userspace datapath supports
4345 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4346 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4347 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4349 ds_put_char(ds
, ',');
4351 ds_put_cstr(ds
, "eth()");
4355 ofpbuf_uninit(&ofp
);
4360 if (left
== key_len
) {
4361 ds_put_cstr(ds
, "<empty>");
4363 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4364 for (i
= 0; i
< left
; i
++) {
4365 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4367 ds_put_char(ds
, ')');
4369 if (!has_ethtype_key
) {
4370 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4371 OVS_KEY_ATTR_ETHERTYPE
);
4373 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4374 ntohs(nl_attr_get_be16(ma
)));
4378 ds_put_cstr(ds
, "<empty>");
4382 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4383 * OVS_KEY_ATTR_* attributes in 'key'. */
4385 odp_flow_key_format(const struct nlattr
*key
,
4386 size_t key_len
, struct ds
*ds
)
4388 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4392 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4394 if (!strcasecmp(s
, "no")) {
4395 *type
= OVS_FRAG_TYPE_NONE
;
4396 } else if (!strcasecmp(s
, "first")) {
4397 *type
= OVS_FRAG_TYPE_FIRST
;
4398 } else if (!strcasecmp(s
, "later")) {
4399 *type
= OVS_FRAG_TYPE_LATER
;
4409 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4413 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4414 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4418 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4419 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4422 memset(mask
, 0xff, sizeof *mask
);
4431 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4435 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4439 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4440 IP_SCAN_ARGS(mask
), &n
)) {
4443 *mask
= OVS_BE32_MAX
;
4452 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4455 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4457 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4458 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4462 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4463 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4466 memset(mask
, 0xff, sizeof *mask
);
4475 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4480 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4481 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4486 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4487 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4489 *mask
= htonl(mask_
);
4491 *mask
= htonl(IPV6_LABEL_MASK
);
4500 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4504 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4508 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4520 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4524 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4528 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4540 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4544 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4548 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4560 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4562 uint16_t key_
, mask_
;
4565 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4570 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4572 *mask
= htons(mask_
);
4574 *mask
= OVS_BE16_MAX
;
4583 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4585 uint32_t key_
, mask_
;
4588 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4593 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4595 *mask
= htonl(mask_
);
4597 *mask
= OVS_BE32_MAX
;
4606 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4608 uint64_t key_
, mask_
;
4611 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4614 *key
= htonll(key_
);
4616 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4618 *mask
= htonll(mask_
);
4620 *mask
= OVS_BE64_MAX
;
4629 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4631 uint32_t flags
, fmask
;
4634 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4635 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4636 if (n
>= 0 && s
[n
] == ')') {
4647 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4649 uint32_t flags
, fmask
;
4652 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4653 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4655 *key
= htons(flags
);
4657 *mask
= htons(fmask
);
4665 ovs_to_odp_ct_state(uint8_t state
)
4669 #define CS_STATE(ENUM, INDEX, NAME) \
4670 if (state & CS_##ENUM) { \
4671 odp |= OVS_CS_F_##ENUM; \
4680 odp_to_ovs_ct_state(uint32_t flags
)
4684 #define CS_STATE(ENUM, INDEX, NAME) \
4685 if (flags & OVS_CS_F_##ENUM) { \
4686 state |= CS_##ENUM; \
4695 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4697 uint32_t flags
, fmask
;
4700 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4701 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4702 mask
? &fmask
: NULL
);
4715 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4719 enum ovs_frag_type frag_type
;
4721 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4722 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4735 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4736 const struct simap
*port_names
)
4740 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4744 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4751 } else if (port_names
) {
4752 const struct simap_node
*node
;
4755 len
= strcspn(s
, ")");
4756 node
= simap_find_len(port_names
, s
, len
);
4769 /* Helper for vlan parsing. */
4770 struct ovs_key_vlan__
{
4775 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
4777 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
4779 if (value
>> bits
) {
4783 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
4788 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
4791 uint16_t key_
, mask_
;
4794 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4797 if (set_be16_bf(key
, bits
, offset
, key_
)) {
4799 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4802 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
4806 *mask
|= htons(((1U << bits
) - 1) << offset
);
4816 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4818 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
4822 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4824 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
4828 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4830 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
4835 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
4837 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
4839 if (value
>> bits
) {
4843 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
4848 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
4851 uint32_t key_
, mask_
;
4854 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4857 if (set_be32_bf(key
, bits
, offset
, key_
)) {
4859 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4862 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
4866 *mask
|= htonl(((1U << bits
) - 1) << offset
);
4876 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4878 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
4882 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4884 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
4888 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4890 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
4894 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4896 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
4900 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
4902 const char *s_base
= s
;
4903 ovs_be16 id
= 0, id_mask
= 0;
4904 uint8_t flags
= 0, flags_mask
= 0;
4907 if (!strncmp(s
, "id=", 3)) {
4909 len
= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
4919 if (!strncmp(s
, "flags=", 6)) {
4921 len
= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
4928 if (!strncmp(s
, "))", 2)) {
4931 *key
= (flags
<< 16) | ntohs(id
);
4933 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
4943 scan_erspan_metadata(const char *s
,
4944 struct erspan_metadata
*key
,
4945 struct erspan_metadata
*mask
)
4947 const char *s_base
= s
;
4948 uint32_t idx
= 0, idx_mask
= 0;
4949 uint8_t ver
= 0, dir
= 0, hwid
= 0;
4950 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
4953 if (!strncmp(s
, "ver=", 4)) {
4955 len
= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
4967 if (!strncmp(s
, "idx=", 4)) {
4969 len
= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
4976 if (!strncmp(s
, ")", 1)) {
4979 key
->u
.index
= htonl(idx
);
4981 mask
->u
.index
= htonl(idx_mask
);
4986 } else if (ver
== 2) {
4987 if (!strncmp(s
, "dir=", 4)) {
4989 len
= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
4998 if (!strncmp(s
, "hwid=", 5)) {
5000 len
= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
5007 if (!strncmp(s
, ")", 1)) {
5010 key
->u
.md2
.hwid
= hwid
;
5011 key
->u
.md2
.dir
= dir
;
5013 mask
->u
.md2
.hwid
= hwid_mask
;
5014 mask
->u
.md2
.dir
= dir_mask
;
5024 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
5026 const char *s_base
= s
;
5027 struct geneve_opt
*opt
= key
->d
;
5028 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
5029 int len_remain
= sizeof key
->d
;
5032 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
5036 len_remain
-= sizeof *opt
;
5038 if (!strncmp(s
, "class=", 6)) {
5040 len
= scan_be16(s
, &opt
->opt_class
,
5041 mask
? &opt_mask
->opt_class
: NULL
);
5047 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
5053 if (!strncmp(s
, "type=", 5)) {
5055 len
= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
5061 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5067 if (!strncmp(s
, "len=", 4)) {
5068 uint8_t opt_len
, opt_len_mask
;
5070 len
= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
5076 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
5079 opt
->length
= opt_len
/ 4;
5081 opt_mask
->length
= opt_len_mask
;
5085 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5090 if (parse_int_string(s
, (uint8_t *)(opt
+ 1),
5091 data_len
, (char **)&s
)) {
5098 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
5099 data_len
, (char **)&s
)) {
5110 opt
+= 1 + data_len
/ 4;
5112 opt_mask
+= 1 + data_len
/ 4;
5114 len_remain
-= data_len
;
5121 len
= sizeof key
->d
- len_remain
;
5135 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
5137 const uint16_t *flags
= data_
;
5139 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
5140 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5142 if (*flags
& FLOW_TNL_F_CSUM
) {
5143 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5145 if (*flags
& FLOW_TNL_F_OAM
) {
5146 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5151 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5153 const uint32_t *gbp
= data_
;
5156 size_t vxlan_opts_ofs
;
5158 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5159 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5160 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5165 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5167 const struct geneve_scan
*geneve
= data_
;
5169 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5174 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5176 const struct erspan_metadata
*md
= data_
;
5178 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5182 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5184 unsigned long call_fn = (unsigned long)FUNC; \
5186 typedef void (*fn)(struct ofpbuf *, const void *); \
5188 func(BUF, &(DATA)); \
5190 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5194 #define SCAN_IF(NAME) \
5195 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5196 const char *start = s; \
5201 /* Usually no special initialization is needed. */
5202 #define SCAN_BEGIN(NAME, TYPE) \
5205 memset(&skey, 0, sizeof skey); \
5206 memset(&smask, 0, sizeof smask); \
5210 /* Init as fully-masked as mask will not be scanned. */
5211 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5214 memset(&skey, 0, sizeof skey); \
5215 memset(&smask, 0xff, sizeof smask); \
5219 /* VLAN needs special initialization. */
5220 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5222 TYPE skey = KEY_INIT; \
5223 TYPE smask = MASK_INIT; \
5227 /* Scan unnamed entry as 'TYPE' */
5228 #define SCAN_TYPE(TYPE, KEY, MASK) \
5229 len = scan_##TYPE(s, KEY, MASK); \
5235 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5236 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5237 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5238 s += strlen(NAME); \
5239 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5243 #define SCAN_FINISH() \
5244 } while (*s++ == ',' && len != 0); \
5245 if (s[-1] != ')') { \
5249 #define SCAN_FINISH_SINGLE() \
5251 if (*s++ != ')') { \
5255 /* Beginning of nested attribute. */
5256 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5258 size_t key_offset, mask_offset = 0; \
5259 key_offset = nl_msg_start_nested(key, ATTR); \
5261 mask_offset = nl_msg_start_nested(mask, ATTR); \
5266 #define SCAN_END_NESTED() \
5268 nl_msg_end_nested(key, key_offset); \
5270 nl_msg_end_nested(mask, mask_offset); \
5275 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5276 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5278 memset(&skey, 0, sizeof skey); \
5279 memset(&smask, 0xff, sizeof smask); \
5280 s += strlen(NAME); \
5281 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5282 SCAN_PUT(ATTR, FUNC); \
5286 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5287 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5289 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5290 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5292 #define SCAN_PUT(ATTR, FUNC) \
5293 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5295 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5297 #define SCAN_END(ATTR) \
5299 SCAN_PUT(ATTR, NULL); \
5303 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5305 TYPE skey[CNT], smask[CNT]; \
5306 memset(&skey, 0, sizeof skey); \
5307 memset(&smask, 0, sizeof smask); \
5308 int idx = 0, cnt = CNT; \
5309 uint64_t fields = 0; \
5314 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5315 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5316 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5317 if (fields & (1UL << field)) { \
5319 if (++idx == cnt) { \
5323 s += strlen(NAME); \
5324 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5325 fields |= 1UL << field; \
5330 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5331 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5333 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5334 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5336 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5339 #define SCAN_END_ARRAY(ATTR) \
5344 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5348 #define SCAN_END_SINGLE(ATTR) \
5349 SCAN_FINISH_SINGLE(); \
5350 SCAN_PUT(ATTR, NULL); \
5354 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5355 SCAN_BEGIN(NAME, TYPE) { \
5356 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5357 } SCAN_END_SINGLE(ATTR)
5359 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5360 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5361 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5362 } SCAN_END_SINGLE(ATTR)
5364 /* scan_port needs one extra argument. */
5365 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5366 SCAN_BEGIN(NAME, TYPE) { \
5367 len = scan_port(s, &skey, &smask, \
5368 context->port_names); \
5373 } SCAN_END_SINGLE(ATTR)
5376 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5377 struct ofpbuf
*mask
)
5379 if (strncmp(s
, "nsh(", 4) == 0) {
5380 const char *start
= s
;
5382 struct ovs_key_nsh skey
, smask
;
5383 uint32_t spi
= 0, spi_mask
= 0;
5384 uint8_t si
= 0, si_mask
= 0;
5388 memset(&skey
, 0, sizeof skey
);
5389 memset(&smask
, 0, sizeof smask
);
5393 if (strncmp(s
, "flags=", 6) == 0) {
5395 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5403 if (strncmp(s
, "mdtype=", 7) == 0) {
5405 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5413 if (strncmp(s
, "np=", 3) == 0) {
5415 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5423 if (strncmp(s
, "spi=", 4) == 0) {
5425 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5433 if (strncmp(s
, "si=", 3) == 0) {
5435 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5443 if (strncmp(s
, "c1=", 3) == 0) {
5445 len
= scan_be32(s
, &skey
.context
[0],
5446 mask
? &smask
.context
[0] : NULL
);
5454 if (strncmp(s
, "c2=", 3) == 0) {
5456 len
= scan_be32(s
, &skey
.context
[1],
5457 mask
? &smask
.context
[1] : NULL
);
5465 if (strncmp(s
, "c3=", 3) == 0) {
5467 len
= scan_be32(s
, &skey
.context
[2],
5468 mask
? &smask
.context
[2] : NULL
);
5476 if (strncmp(s
, "c4=", 3) == 0) {
5478 len
= scan_be32(s
, &skey
.context
[3],
5479 mask
? &smask
.context
[3] : NULL
);
5486 } while (*s
++ == ',' && len
!= 0);
5491 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5492 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5494 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5496 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5504 parse_odp_key_mask_attr(struct parse_odp_context
*context
, const char *s
,
5505 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5507 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5508 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5509 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5510 OVS_KEY_ATTR_RECIRC_ID
);
5511 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5513 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5514 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5515 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5516 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5518 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5519 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5520 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5521 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5522 SCAN_FIELD("tp_src=", be16
, src_port
);
5523 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5524 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5526 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5527 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5528 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5529 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5530 SCAN_FIELD("tp_src=", be16
, src_port
);
5531 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5532 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5534 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5535 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5536 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5537 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5538 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5539 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5540 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5541 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5542 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5543 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5544 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5546 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5547 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5549 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5550 } SCAN_END_NESTED();
5552 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5554 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5555 SCAN_FIELD("src=", eth
, eth_src
);
5556 SCAN_FIELD("dst=", eth
, eth_dst
);
5557 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5559 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5560 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5561 SCAN_FIELD("vid=", vid
, tci
);
5562 SCAN_FIELD("pcp=", pcp
, tci
);
5563 SCAN_FIELD("cfi=", cfi
, tci
);
5564 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5566 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5568 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5569 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5570 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5571 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5572 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5573 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5575 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5576 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5577 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5578 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5579 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5580 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5581 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5582 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5584 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5585 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5586 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5587 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5588 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5589 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5590 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5591 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5592 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5594 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5595 SCAN_FIELD("src=", be16
, tcp_src
);
5596 SCAN_FIELD("dst=", be16
, tcp_dst
);
5597 } SCAN_END(OVS_KEY_ATTR_TCP
);
5599 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5601 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5602 SCAN_FIELD("src=", be16
, udp_src
);
5603 SCAN_FIELD("dst=", be16
, udp_dst
);
5604 } SCAN_END(OVS_KEY_ATTR_UDP
);
5606 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5607 SCAN_FIELD("src=", be16
, sctp_src
);
5608 SCAN_FIELD("dst=", be16
, sctp_dst
);
5609 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5611 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5612 SCAN_FIELD("type=", u8
, icmp_type
);
5613 SCAN_FIELD("code=", u8
, icmp_code
);
5614 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5616 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5617 SCAN_FIELD("type=", u8
, icmpv6_type
);
5618 SCAN_FIELD("code=", u8
, icmpv6_code
);
5619 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5621 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5622 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5623 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5624 SCAN_FIELD("op=", be16
, arp_op
);
5625 SCAN_FIELD("sha=", eth
, arp_sha
);
5626 SCAN_FIELD("tha=", eth
, arp_tha
);
5627 } SCAN_END(OVS_KEY_ATTR_ARP
);
5629 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5630 SCAN_FIELD("target=", in6_addr
, nd_target
);
5631 SCAN_FIELD("sll=", eth
, nd_sll
);
5632 SCAN_FIELD("tll=", eth
, nd_tll
);
5633 } SCAN_END(OVS_KEY_ATTR_ND
);
5635 SCAN_BEGIN("nd_ext(", struct ovs_key_nd_extensions
) {
5636 SCAN_FIELD("nd_reserved=", be32
, nd_reserved
);
5637 SCAN_FIELD("nd_options_type=", u8
, nd_options_type
);
5638 } SCAN_END(OVS_KEY_ATTR_ND_EXTENSIONS
);
5640 struct packet_type
{
5644 SCAN_BEGIN("packet_type(", struct packet_type
) {
5645 SCAN_FIELD("ns=", be16
, ns
);
5646 SCAN_FIELD("id=", be16
, id
);
5647 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5649 /* nsh is nested, it needs special process */
5650 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5657 /* Encap open-coded. */
5658 if (!strncmp(s
, "encap(", 6)) {
5659 const char *start
= s
;
5660 size_t encap
, encap_mask
= 0;
5662 if (context
->depth
+ 1 == MAX_ODP_NESTED
) {
5667 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5669 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5676 s
+= strspn(s
, delimiters
);
5680 } else if (*s
== ')') {
5684 retval
= parse_odp_key_mask_attr(context
, s
, key
, mask
);
5690 if (nl_attr_oversized(key
->size
- encap
- NLA_HDRLEN
)) {
5697 nl_msg_end_nested(key
, encap
);
5699 nl_msg_end_nested(mask
, encap_mask
);
5709 /* Parses the string representation of a datapath flow key, in the format
5710 * output by odp_flow_key_format(). Returns 0 if successful, otherwise a
5711 * positive errno value. On success, stores NULL into '*errorp' and the flow
5712 * key is appended to 'key' as a series of Netlink attributes. On failure,
5713 * stores a malloc()'d error message in '*errorp' without changing the data in
5714 * 'key'. Either way, 'key''s data might be reallocated.
5716 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5717 * to a port number. (Port names may be used instead of port numbers in
5720 * On success, the attributes appended to 'key' are individually syntactically
5721 * valid, but they may not be valid as a sequence. 'key' might, for example,
5722 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5724 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
5725 struct ofpbuf
*key
, struct ofpbuf
*mask
,
5732 const size_t old_size
= key
->size
;
5733 struct parse_odp_context context
= (struct parse_odp_context
) {
5734 .port_names
= port_names
,
5739 s
+= strspn(s
, delimiters
);
5746 retval
= odp_ufid_from_string(s
, &ufid
);
5749 *errorp
= xasprintf("syntax error at %s", s
);
5751 key
->size
= old_size
;
5753 } else if (retval
> 0) {
5755 s
+= s
[0] == ' ' ? 1 : 0;
5758 retval
= parse_odp_key_mask_attr(&context
, s
, key
, mask
);
5761 *errorp
= xasprintf("syntax error at %s", s
);
5763 key
->size
= old_size
;
5773 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
5776 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5777 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5778 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5779 * must use a zero mask for the netlink frag field, and all ones mask
5781 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
5783 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
5784 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
5785 : OVS_FRAG_TYPE_FIRST
;
5788 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
5789 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
5790 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
5792 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
5794 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
5796 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
5798 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
5799 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
5800 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
5801 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
5802 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
5804 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
5807 /* These share the same layout. */
5809 struct ovs_key_tcp tcp
;
5810 struct ovs_key_udp udp
;
5811 struct ovs_key_sctp sctp
;
5814 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
5815 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
5818 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
5819 bool export_mask
, struct ofpbuf
*buf
)
5821 struct ovs_key_ethernet
*eth_key
;
5822 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
5824 const struct flow
*flow
= parms
->flow
;
5825 const struct flow
*mask
= parms
->mask
;
5826 const struct flow
*data
= export_mask
? mask
: flow
;
5828 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
5830 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
5831 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
5832 parms
->key_buf
, NULL
);
5835 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
5837 if (parms
->support
.ct_state
) {
5838 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5839 ovs_to_odp_ct_state(data
->ct_state
));
5841 if (parms
->support
.ct_zone
) {
5842 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
5844 if (parms
->support
.ct_mark
) {
5845 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
5847 if (parms
->support
.ct_label
) {
5848 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
5849 sizeof(data
->ct_label
));
5851 if (flow
->ct_nw_proto
) {
5852 if (parms
->support
.ct_orig_tuple
5853 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5854 struct ovs_key_ct_tuple_ipv4 ct
= {
5861 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
5863 } else if (parms
->support
.ct_orig_tuple6
5864 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5865 struct ovs_key_ct_tuple_ipv6 ct
= {
5872 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
5876 if (parms
->support
.recirc
) {
5877 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
5878 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
5881 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
5882 * is not the magical value "ODPP_NONE". */
5883 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
5884 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
5887 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
5889 if (OVS_UNLIKELY(parms
->probe
)) {
5890 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
5892 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
5895 /* Conditionally add L2 attributes for Ethernet packets */
5896 if (flow
->packet_type
== htonl(PT_ETH
)) {
5897 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
5899 get_ethernet_key(data
, eth_key
);
5901 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
5902 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
5904 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5905 if (eth_type_vlan(flow
->dl_type
)) {
5906 /* If VLAN was truncated the tpid is in dl_type */
5907 tpid
= flow
->dl_type
;
5914 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5916 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
5918 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
5919 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
5920 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5926 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5927 /* For backwards compatibility with kernels that don't support
5928 * wildcarding, the following convention is used to encode the
5929 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
5932 * -------- -------- -------
5933 * >0x5ff 0xffff Specified Ethernet II Ethertype.
5934 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
5935 * <none> 0xffff Any non-Ethernet II frame (except valid
5936 * 802.3 SNAP packet with valid eth_type).
5939 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5944 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
5946 if (eth_type_vlan(flow
->dl_type
)) {
5950 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5951 struct ovs_key_ipv4
*ipv4_key
;
5953 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
5955 get_ipv4_key(data
, ipv4_key
, export_mask
);
5956 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5957 struct ovs_key_ipv6
*ipv6_key
;
5959 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
5961 get_ipv6_key(data
, ipv6_key
, export_mask
);
5962 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5963 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5964 struct ovs_key_arp
*arp_key
;
5966 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
5968 get_arp_key(data
, arp_key
);
5969 } else if (eth_type_mpls(flow
->dl_type
)) {
5970 struct ovs_key_mpls
*mpls_key
;
5973 n
= flow_count_mpls_labels(flow
, NULL
);
5975 n
= MIN(n
, parms
->support
.max_mpls_depth
);
5977 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
5978 n
* sizeof *mpls_key
);
5979 for (i
= 0; i
< n
; i
++) {
5980 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
5982 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5983 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
5986 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5987 if (flow
->nw_proto
== IPPROTO_TCP
) {
5988 union ovs_key_tp
*tcp_key
;
5990 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
5992 get_tp_key(data
, tcp_key
);
5993 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
5994 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
5996 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5997 union ovs_key_tp
*udp_key
;
5999 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
6001 get_tp_key(data
, udp_key
);
6002 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
6003 union ovs_key_tp
*sctp_key
;
6005 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
6007 get_tp_key(data
, sctp_key
);
6008 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
6009 && flow
->nw_proto
== IPPROTO_ICMP
) {
6010 struct ovs_key_icmp
*icmp_key
;
6012 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
6014 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
6015 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
6016 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6017 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
6018 struct ovs_key_icmpv6
*icmpv6_key
;
6020 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
6021 sizeof *icmpv6_key
);
6022 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
6023 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
6025 if (is_nd(flow
, NULL
)
6026 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
6027 * type and code are 8 bits wide. Therefore, an exact match
6028 * looks like htons(0xff), not htons(0xffff). See
6029 * xlate_wc_finish() for details. */
6030 && (!export_mask
|| (data
->tp_src
== htons(0xff)
6031 && data
->tp_dst
== htons(0xff)))) {
6032 struct ovs_key_nd
*nd_key
;
6033 struct ovs_key_nd_extensions
*nd_ext_key
;
6034 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
6036 nd_key
->nd_target
= data
->nd_target
;
6037 nd_key
->nd_sll
= data
->arp_sha
;
6038 nd_key
->nd_tll
= data
->arp_tha
;
6040 /* Add ND Extensions Attr only if reserved field
6041 * or options type is set. */
6042 if (data
->igmp_group_ip4
!= 0 ||
6043 data
->tcp_flags
!= 0) {
6045 nl_msg_put_unspec_uninit(buf
,
6046 OVS_KEY_ATTR_ND_EXTENSIONS
,
6047 sizeof *nd_ext_key
);
6048 nd_ext_key
->nd_reserved
= data
->igmp_group_ip4
;
6049 nd_ext_key
->nd_options_type
= ntohs(data
->tcp_flags
);
6056 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
6057 if (encap
[encaps
]) {
6058 nl_msg_end_nested(buf
, encap
[encaps
]);
6063 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
6065 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6066 * capable of being expanded to allow for that much space. */
6068 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
6071 odp_flow_key_from_flow__(parms
, false, buf
);
6074 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
6077 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
6078 * capable of being expanded to allow for that much space. */
6080 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
6083 odp_flow_key_from_flow__(parms
, true, buf
);
6086 /* Generate ODP flow key from the given packet metadata */
6088 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
6090 const struct pkt_metadata
*md
= &packet
->md
;
6092 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
6094 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
6095 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
6098 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
6101 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
6102 ovs_to_odp_ct_state(md
->ct_state
));
6104 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
6107 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
6109 if (!ovs_u128_is_zero(md
->ct_label
)) {
6110 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
6111 sizeof(md
->ct_label
));
6113 if (md
->ct_orig_tuple_ipv6
) {
6114 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
6115 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6116 &md
->ct_orig_tuple
.ipv6
,
6117 sizeof md
->ct_orig_tuple
.ipv6
);
6120 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
6121 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6122 &md
->ct_orig_tuple
.ipv4
,
6123 sizeof md
->ct_orig_tuple
.ipv4
);
6128 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6129 * value "ODPP_NONE". */
6130 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
6131 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
6134 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6135 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
6136 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
6137 pt_ns_type_be(packet
->packet_type
));
6141 /* Generate packet metadata from the given ODP flow key. */
6143 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
6144 struct dp_packet
*packet
)
6146 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6147 const struct nlattr
*nla
;
6148 struct pkt_metadata
*md
= &packet
->md
;
6149 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
6150 ovs_be16 ethertype
= 0;
6153 pkt_metadata_init(md
, ODPP_NONE
);
6155 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6156 enum ovs_key_attr type
= nl_attr_type(nla
);
6157 size_t len
= nl_attr_get_size(nla
);
6158 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6159 OVS_KEY_ATTR_MAX
, type
);
6161 if (len
!= expected_len
&& expected_len
>= 0) {
6166 case OVS_KEY_ATTR_RECIRC_ID
:
6167 md
->recirc_id
= nl_attr_get_u32(nla
);
6169 case OVS_KEY_ATTR_DP_HASH
:
6170 md
->dp_hash
= nl_attr_get_u32(nla
);
6172 case OVS_KEY_ATTR_PRIORITY
:
6173 md
->skb_priority
= nl_attr_get_u32(nla
);
6175 case OVS_KEY_ATTR_SKB_MARK
:
6176 md
->pkt_mark
= nl_attr_get_u32(nla
);
6178 case OVS_KEY_ATTR_CT_STATE
:
6179 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
6181 case OVS_KEY_ATTR_CT_ZONE
:
6182 md
->ct_zone
= nl_attr_get_u16(nla
);
6184 case OVS_KEY_ATTR_CT_MARK
:
6185 md
->ct_mark
= nl_attr_get_u32(nla
);
6187 case OVS_KEY_ATTR_CT_LABELS
: {
6188 md
->ct_label
= nl_attr_get_u128(nla
);
6191 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6192 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6193 md
->ct_orig_tuple
.ipv4
= *ct
;
6194 md
->ct_orig_tuple_ipv6
= false;
6197 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6198 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6200 md
->ct_orig_tuple
.ipv6
= *ct
;
6201 md
->ct_orig_tuple_ipv6
= true;
6204 case OVS_KEY_ATTR_TUNNEL
: {
6205 enum odp_key_fitness res
;
6207 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
, NULL
);
6208 if (res
== ODP_FIT_ERROR
) {
6209 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6213 case OVS_KEY_ATTR_IN_PORT
:
6214 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6216 case OVS_KEY_ATTR_ETHERNET
:
6217 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6218 packet_type
= htonl(PT_ETH
);
6220 case OVS_KEY_ATTR_ETHERTYPE
:
6221 ethertype
= nl_attr_get_be16(nla
);
6223 case OVS_KEY_ATTR_UNSPEC
:
6224 case OVS_KEY_ATTR_ENCAP
:
6225 case OVS_KEY_ATTR_VLAN
:
6226 case OVS_KEY_ATTR_IPV4
:
6227 case OVS_KEY_ATTR_IPV6
:
6228 case OVS_KEY_ATTR_TCP
:
6229 case OVS_KEY_ATTR_UDP
:
6230 case OVS_KEY_ATTR_ICMP
:
6231 case OVS_KEY_ATTR_ICMPV6
:
6232 case OVS_KEY_ATTR_ARP
:
6233 case OVS_KEY_ATTR_ND
:
6234 case OVS_KEY_ATTR_ND_EXTENSIONS
:
6235 case OVS_KEY_ATTR_SCTP
:
6236 case OVS_KEY_ATTR_TCP_FLAGS
:
6237 case OVS_KEY_ATTR_MPLS
:
6238 case OVS_KEY_ATTR_PACKET_TYPE
:
6239 case OVS_KEY_ATTR_NSH
:
6240 case __OVS_KEY_ATTR_MAX
:
6246 if (packet_type
== htonl(PT_ETH
)) {
6247 packet
->packet_type
= htonl(PT_ETH
);
6248 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6249 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6252 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6257 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
6259 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
6260 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
6264 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6265 uint64_t attrs
, int out_of_range_attr
,
6266 const struct nlattr
*key
, size_t key_len
)
6271 if (VLOG_DROP_DBG(rl
)) {
6276 for (i
= 0; i
< 64; i
++) {
6277 if (attrs
& (UINT64_C(1) << i
)) {
6278 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6280 ds_put_format(&s
, " %s",
6281 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6284 if (out_of_range_attr
) {
6285 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6288 ds_put_cstr(&s
, ": ");
6289 odp_flow_key_format(key
, key_len
, &s
);
6291 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6296 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6298 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6301 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6304 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6305 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6306 return 0xff; /* Error. */
6309 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6310 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6311 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6314 /* Parses the attributes in the 'key_len' bytes of 'key' into 'attrs', which
6315 * must have OVS_KEY_ATTR_MAX + 1 elements. Stores each attribute in 'key'
6316 * into the corresponding element of 'attrs'.
6318 * Stores a bitmask of the attributes' indexes found in 'key' into
6319 * '*present_attrsp'.
6321 * If an attribute beyond OVS_KEY_ATTR_MAX is found, stores its attribute type
6322 * (or one of them, if more than one) into '*out_of_range_attrp', otherwise 0.
6324 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6325 * error message in '*errorp'. */
6327 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6328 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6329 int *out_of_range_attrp
, char **errorp
)
6331 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6332 const struct nlattr
*nla
;
6333 uint64_t present_attrs
;
6336 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6338 *out_of_range_attrp
= 0;
6339 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6340 uint16_t type
= nl_attr_type(nla
);
6341 size_t len
= nl_attr_get_size(nla
);
6342 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6343 OVS_KEY_ATTR_MAX
, type
);
6345 if (len
!= expected_len
&& expected_len
>= 0) {
6346 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6348 odp_parse_error(&rl
, errorp
, "attribute %s has length %"PRIuSIZE
" "
6349 "but should have length %d",
6350 ovs_key_attr_to_string(type
, namebuf
,
6356 if (type
> OVS_KEY_ATTR_MAX
) {
6357 *out_of_range_attrp
= type
;
6359 if (present_attrs
& (UINT64_C(1) << type
)) {
6360 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6362 odp_parse_error(&rl
, errorp
,
6363 "duplicate %s attribute in flow key",
6364 ovs_key_attr_to_string(type
, namebuf
,
6369 present_attrs
|= UINT64_C(1) << type
;
6374 odp_parse_error(&rl
, errorp
, "trailing garbage in flow key");
6378 *present_attrsp
= present_attrs
;
6382 static enum odp_key_fitness
6383 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6384 uint64_t expected_attrs
,
6385 const struct nlattr
*key
, size_t key_len
)
6387 uint64_t missing_attrs
;
6388 uint64_t extra_attrs
;
6390 missing_attrs
= expected_attrs
& ~present_attrs
;
6391 if (missing_attrs
) {
6392 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6393 log_odp_key_attributes(&rl
, "expected but not present",
6394 missing_attrs
, 0, key
, key_len
);
6395 return ODP_FIT_TOO_LITTLE
;
6398 extra_attrs
= present_attrs
& ~expected_attrs
;
6399 if (extra_attrs
|| out_of_range_attr
) {
6400 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6401 log_odp_key_attributes(&rl
, "present but not expected",
6402 extra_attrs
, out_of_range_attr
, key
, key_len
);
6403 return ODP_FIT_TOO_MUCH
;
6406 return ODP_FIT_PERFECT
;
6409 /* Initializes 'flow->dl_type' based on the attributes in 'attrs', in which the
6410 * attributes in the bit-mask 'present_attrs' are present. Returns true if
6411 * successful, false on failure.
6413 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6414 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6415 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6416 * previously parsed flow key.
6418 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6419 * error message in '*errorp'. */
6421 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6422 uint64_t present_attrs
, uint64_t *expected_attrs
,
6423 struct flow
*flow
, const struct flow
*src_flow
,
6426 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6427 bool is_mask
= flow
!= src_flow
;
6429 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6430 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6431 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6432 odp_parse_error(&rl
, errorp
,
6433 "invalid Ethertype %"PRIu16
" in flow key",
6434 ntohs(flow
->dl_type
));
6437 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6438 flow
->dl_type
!= htons(0xffff)) {
6439 odp_parse_error(&rl
, errorp
, "can't bitwise match non-Ethernet II "
6440 "\"Ethertype\" %#"PRIx16
" (with mask %#"PRIx16
")",
6441 ntohs(src_flow
->dl_type
), ntohs(flow
->dl_type
));
6444 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6447 /* Default ethertype for well-known L3 packets. */
6448 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6449 flow
->dl_type
= htons(ETH_TYPE_IP
);
6450 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6451 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6452 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6453 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6455 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6457 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6458 /* dl_type is mandatory for non-Ethernet packets */
6459 flow
->dl_type
= htons(0xffff);
6460 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6461 /* See comments in odp_flow_key_from_flow__(). */
6462 odp_parse_error(&rl
, errorp
,
6463 "mask expected for non-Ethernet II frame");
6470 /* Initializes MPLS, L3, and L4 fields in 'flow' based on the attributes in
6471 * 'attrs', in which the attributes in the bit-mask 'present_attrs' are
6472 * present. The caller also indicates an out-of-range attribute
6473 * 'out_of_range_attr' if one was present when parsing (if so, the fitness
6474 * cannot be perfect).
6476 * Sets 1-bits in '*expected_attrs' for the attributes in 'attrs' that were
6477 * consulted. 'flow' is assumed to be a flow key unless 'src_flow' is nonnull,
6478 * in which case 'flow' is a flow mask and 'src_flow' is its corresponding
6479 * previously parsed flow key.
6481 * Returns fitness based on any discrepancies between present and expected
6482 * attributes, except that a 'need_check' of false overrides this.
6484 * If 'errorp' is nonnull and the function returns false, stores a malloc()'d
6485 * error message in '*errorp'. 'key' and 'key_len' are just used for error
6486 * reporting in this case. */
6487 static enum odp_key_fitness
6488 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6489 uint64_t present_attrs
, int out_of_range_attr
,
6490 uint64_t *expected_attrs
, struct flow
*flow
,
6491 const struct nlattr
*key
, size_t key_len
,
6492 const struct flow
*src_flow
, bool need_check
, char **errorp
)
6494 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6495 bool is_mask
= src_flow
!= flow
;
6496 const void *check_start
= NULL
;
6497 size_t check_len
= 0;
6498 enum ovs_key_attr expected_bit
= 0xff;
6500 if (eth_type_mpls(src_flow
->dl_type
)) {
6501 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6502 *expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6504 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6505 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6506 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6507 int n
= size
/ sizeof(ovs_be32
);
6510 if (!size
|| size
% sizeof(ovs_be32
)) {
6511 odp_parse_error(&rl
, errorp
,
6512 "MPLS LSEs have invalid length %"PRIuSIZE
,
6514 return ODP_FIT_ERROR
;
6516 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6517 odp_parse_error(&rl
, errorp
,
6518 "unexpected MPLS Ethertype mask %x"PRIx16
,
6519 ntohs(flow
->dl_type
));
6520 return ODP_FIT_ERROR
;
6523 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6524 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6526 if (n
> FLOW_MAX_MPLS_LABELS
) {
6527 return ODP_FIT_TOO_MUCH
;
6531 /* BOS may be set only in the innermost label. */
6532 for (i
= 0; i
< n
- 1; i
++) {
6533 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6534 odp_parse_error(&rl
, errorp
,
6535 "MPLS BOS set in non-innermost label");
6536 return ODP_FIT_ERROR
;
6540 /* BOS must be set in the innermost label. */
6541 if (n
< FLOW_MAX_MPLS_LABELS
6542 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6543 return ODP_FIT_TOO_LITTLE
;
6549 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6551 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6553 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6554 const struct ovs_key_ipv4
*ipv4_key
;
6556 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6557 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6558 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6559 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV4 has invalid "
6560 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6561 return ODP_FIT_ERROR
;
6565 check_start
= ipv4_key
;
6566 check_len
= sizeof *ipv4_key
;
6567 expected_bit
= OVS_KEY_ATTR_IPV4
;
6570 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6572 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6574 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6575 const struct ovs_key_ipv6
*ipv6_key
;
6577 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6578 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6579 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6580 odp_parse_error(&rl
, errorp
, "OVS_KEY_ATTR_IPV6 has invalid "
6581 "nw_frag %#"PRIx8
, flow
->nw_frag
);
6582 return ODP_FIT_ERROR
;
6585 check_start
= ipv6_key
;
6586 check_len
= sizeof *ipv6_key
;
6587 expected_bit
= OVS_KEY_ATTR_IPV6
;
6590 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6591 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6593 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6595 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6596 const struct ovs_key_arp
*arp_key
;
6598 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6599 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6600 odp_parse_error(&rl
, errorp
,
6601 "unsupported ARP opcode %"PRIu16
" in flow "
6602 "key", ntohs(arp_key
->arp_op
));
6603 return ODP_FIT_ERROR
;
6605 put_arp_key(arp_key
, flow
);
6607 check_start
= arp_key
;
6608 check_len
= sizeof *arp_key
;
6609 expected_bit
= OVS_KEY_ATTR_ARP
;
6612 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6614 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6616 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6617 if (odp_nsh_key_from_attr(attrs
[OVS_KEY_ATTR_NSH
], &flow
->nsh
,
6618 NULL
, errorp
) == ODP_FIT_ERROR
) {
6619 return ODP_FIT_ERROR
;
6622 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6623 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6624 expected_bit
= OVS_KEY_ATTR_NSH
;
6630 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6631 if (!is_all_zeros(check_start
, check_len
) &&
6632 flow
->dl_type
!= htons(0xffff)) {
6633 odp_parse_error(&rl
, errorp
, "unexpected L3 matching with "
6634 "masked Ethertype %#"PRIx16
"/%#"PRIx16
,
6635 ntohs(src_flow
->dl_type
),
6636 ntohs(flow
->dl_type
));
6637 return ODP_FIT_ERROR
;
6639 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6643 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6644 if (src_flow
->nw_proto
== IPPROTO_TCP
6645 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6646 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6647 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6649 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6651 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6652 const union ovs_key_tp
*tcp_key
;
6654 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6655 put_tp_key(tcp_key
, flow
);
6656 expected_bit
= OVS_KEY_ATTR_TCP
;
6658 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6659 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6660 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6662 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6663 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6664 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6665 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6667 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
6669 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
6670 const union ovs_key_tp
*udp_key
;
6672 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
6673 put_tp_key(udp_key
, flow
);
6674 expected_bit
= OVS_KEY_ATTR_UDP
;
6676 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
6677 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6678 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6679 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6681 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
6683 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
6684 const union ovs_key_tp
*sctp_key
;
6686 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
6687 put_tp_key(sctp_key
, flow
);
6688 expected_bit
= OVS_KEY_ATTR_SCTP
;
6690 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
6691 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
6692 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6694 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
6696 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
6697 const struct ovs_key_icmp
*icmp_key
;
6699 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
6700 flow
->tp_src
= htons(icmp_key
->icmp_type
);
6701 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
6702 expected_bit
= OVS_KEY_ATTR_ICMP
;
6704 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
6705 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6706 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6708 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
6710 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
6711 const struct ovs_key_icmpv6
*icmpv6_key
;
6713 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
6714 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
6715 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
6716 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
6717 if (is_nd(src_flow
, NULL
)) {
6719 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6721 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
6722 const struct ovs_key_nd
*nd_key
;
6724 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
6725 flow
->nd_target
= nd_key
->nd_target
;
6726 flow
->arp_sha
= nd_key
->nd_sll
;
6727 flow
->arp_tha
= nd_key
->nd_tll
;
6729 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6730 * ICMP type and code are 8 bits wide. Therefore, an
6731 * exact match looks like htons(0xff), not
6732 * htons(0xffff). See xlate_wc_finish() for details.
6734 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
6735 (flow
->tp_src
!= htons(0xff) ||
6736 flow
->tp_dst
!= htons(0xff))) {
6737 odp_parse_error(&rl
, errorp
,
6738 "ICMP (src,dst) masks should be "
6739 "(0xff,0xff) but are actually "
6740 "(%#"PRIx16
",%#"PRIx16
")",
6741 ntohs(flow
->tp_src
),
6742 ntohs(flow
->tp_dst
));
6743 return ODP_FIT_ERROR
;
6745 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6750 (UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
)) {
6751 const struct ovs_key_nd_extensions
*nd_ext_key
;
6754 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
6758 nl_attr_get(attrs
[OVS_KEY_ATTR_ND_EXTENSIONS
]);
6759 flow
->igmp_group_ip4
= nd_ext_key
->nd_reserved
;
6760 flow
->tcp_flags
= htons(nd_ext_key
->nd_options_type
);
6763 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6764 * ICMP type and code are 8 bits wide. Therefore, an
6765 * exact match looks like htons(0xff), not
6766 * htons(0xffff). See xlate_wc_finish() for details.
6768 if (!is_all_zeros(nd_ext_key
, sizeof *nd_ext_key
) &&
6769 (flow
->tp_src
!= htons(0xff) ||
6770 flow
->tp_dst
!= htons(0xff))) {
6771 return ODP_FIT_ERROR
;
6774 UINT64_C(1) << OVS_KEY_ATTR_ND_EXTENSIONS
;
6780 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
6781 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6782 /* OVS userspace parses the IGMP type, code, and group, but its
6783 * datapaths do not, so there is always missing information. */
6784 return ODP_FIT_TOO_LITTLE
;
6786 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
6787 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
6788 odp_parse_error(&rl
, errorp
, "flow matches on L4 ports but does "
6789 "not define an L4 protocol");
6790 return ODP_FIT_ERROR
;
6792 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6797 return need_check
? check_expectations(present_attrs
, out_of_range_attr
,
6798 *expected_attrs
, key
, key_len
) : ODP_FIT_PERFECT
;
6801 /* Parse 802.1Q header then encapsulated L3 attributes. */
6802 static enum odp_key_fitness
6803 parse_8021q_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
, char **errorp
)
6809 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6810 bool is_mask
= src_flow
!= flow
;
6812 const struct nlattr
*encap
;
6813 enum odp_key_fitness encap_fitness
;
6814 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
6817 while (encaps
< flow_vlan_limit
&&
6819 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
6820 : eth_type_vlan(flow
->dl_type
))) {
6822 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
6823 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
6825 /* Calculate fitness of outer attributes. */
6827 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
6828 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
6830 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6831 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6833 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
6834 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
6837 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
6838 expected_attrs
, key
, key_len
);
6841 * Remove the TPID from dl_type since it's not the real Ethertype. */
6842 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
6843 flow
->dl_type
= htons(0);
6844 flow
->vlans
[encaps
].tci
=
6845 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
6846 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
6849 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
6850 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6851 return ODP_FIT_TOO_LITTLE
;
6852 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
6853 /* Corner case for a truncated 802.1Q header. */
6854 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
6855 return ODP_FIT_TOO_MUCH
;
6858 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
6860 &rl
, errorp
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
6861 "but CFI bit is not set", ntohs(flow
->vlans
[encaps
].tci
));
6862 return ODP_FIT_ERROR
;
6865 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6870 /* Now parse the encapsulated attributes. */
6871 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
6872 attrs
, &present_attrs
, &out_of_range_attr
,
6874 return ODP_FIT_ERROR
;
6878 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
6879 flow
, src_flow
, errorp
)) {
6880 return ODP_FIT_ERROR
;
6882 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
,
6886 src_flow
, false, errorp
);
6887 if (encap_fitness
!= ODP_FIT_PERFECT
) {
6888 return encap_fitness
;
6893 return check_expectations(present_attrs
, out_of_range_attr
,
6894 expected_attrs
, key
, key_len
);
6897 static enum odp_key_fitness
6898 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
6899 struct flow
*flow
, const struct flow
*src_flow
,
6902 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
6907 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
6908 uint64_t expected_attrs
;
6909 uint64_t present_attrs
;
6910 int out_of_range_attr
;
6911 bool is_mask
= src_flow
!= flow
;
6913 memset(flow
, 0, sizeof *flow
);
6915 /* Parse attributes. */
6916 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
6917 &out_of_range_attr
, errorp
)) {
6923 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
6924 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
6925 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
6926 } else if (is_mask
) {
6927 /* Always exact match recirc_id if it is not specified. */
6928 flow
->recirc_id
= UINT32_MAX
;
6931 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
6932 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
6933 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
6935 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
6936 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
6937 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
6940 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
6941 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
6942 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
6945 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
6946 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
6948 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
6949 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
6951 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
6952 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
6953 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
6955 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
6956 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
6957 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
6959 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
6960 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
6961 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
6963 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
6964 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
6965 flow
->ct_nw_src
= ct
->ipv4_src
;
6966 flow
->ct_nw_dst
= ct
->ipv4_dst
;
6967 flow
->ct_nw_proto
= ct
->ipv4_proto
;
6968 flow
->ct_tp_src
= ct
->src_port
;
6969 flow
->ct_tp_dst
= ct
->dst_port
;
6970 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
6972 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
6973 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
6975 flow
->ct_ipv6_src
= ct
->ipv6_src
;
6976 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
6977 flow
->ct_nw_proto
= ct
->ipv6_proto
;
6978 flow
->ct_tp_src
= ct
->src_port
;
6979 flow
->ct_tp_dst
= ct
->dst_port
;
6980 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
6983 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
6984 enum odp_key_fitness res
;
6986 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
6987 &flow
->tunnel
, errorp
);
6988 if (res
== ODP_FIT_ERROR
) {
6990 } else if (res
== ODP_FIT_PERFECT
) {
6991 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
6995 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
6996 flow
->in_port
.odp_port
6997 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
6998 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
6999 } else if (!is_mask
) {
7000 flow
->in_port
.odp_port
= ODPP_NONE
;
7003 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
7005 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
7006 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
7007 if (pt_ns(src_flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
7008 flow
->dl_type
= pt_ns_type_be(flow
->packet_type
);
7010 } else if (!is_mask
) {
7011 flow
->packet_type
= htonl(PT_ETH
);
7014 /* Check for Ethernet header. */
7015 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
7016 const struct ovs_key_ethernet
*eth_key
;
7018 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
7019 put_ethernet_key(eth_key
, flow
);
7021 flow
->packet_type
= htonl(PT_ETH
);
7023 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
7025 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
7026 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
7028 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
7031 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
7034 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
7035 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
7036 src_flow
, errorp
)) {
7041 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
7042 : eth_type_vlan(src_flow
->dl_type
)) {
7043 fitness
= parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
7044 expected_attrs
, flow
, key
, key_len
,
7048 /* A missing VLAN mask means exact match on vlan_tci 0 (== no
7050 flow
->vlans
[0].tpid
= htons(0xffff);
7051 flow
->vlans
[0].tci
= htons(0xffff);
7052 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
7053 flow
->vlans
[0].tci
= nl_attr_get_be16(
7054 attrs
[OVS_KEY_ATTR_VLAN
]);
7055 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
7058 fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
7059 &expected_attrs
, flow
, key
, key_len
,
7060 src_flow
, true, errorp
);
7064 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7065 if (fitness
== ODP_FIT_ERROR
&& (errorp
|| !VLOG_DROP_WARN(&rl
))) {
7066 struct ds s
= DS_EMPTY_INITIALIZER
;
7068 ds_put_cstr(&s
, "the flow mask in error is: ");
7069 odp_flow_key_format(key
, key_len
, &s
);
7070 ds_put_cstr(&s
, ", for the following flow key: ");
7071 flow_format(&s
, src_flow
, NULL
);
7073 ds_put_cstr(&s
, "the flow key in error is: ");
7074 odp_flow_key_format(key
, key_len
, &s
);
7077 char *old_error
= *errorp
;
7078 *errorp
= xasprintf("%s; %s", old_error
, ds_cstr(&s
));
7081 VLOG_WARN("%s", ds_cstr(&s
));
7088 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
7089 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
7090 * 'key' fits our expectations for what a flow key should contain.
7092 * The 'in_port' will be the datapath's understanding of the port. The
7093 * caller will need to translate with odp_port_to_ofp_port() if the
7094 * OpenFlow port is needed.
7096 * This function doesn't take the packet itself as an argument because none of
7097 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
7098 * it is always possible to infer which additional attribute(s) should appear
7099 * by looking at the attributes for lower-level protocols, e.g. if the network
7100 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
7101 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
7104 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7105 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7106 * '*errorp', otherwise NULL. */
7107 enum odp_key_fitness
7108 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
7109 struct flow
*flow
, char **errorp
)
7111 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
, errorp
);
7114 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
7115 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
7116 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
7117 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
7118 * well 'key' fits our expectations for what a flow key should contain.
7120 * If 'errorp' is nonnull, this function uses it for detailed error reports: if
7121 * the return value is ODP_FIT_ERROR, it stores a malloc()'d error string in
7122 * '*errorp', otherwise NULL. */
7123 enum odp_key_fitness
7124 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
7125 struct flow_wildcards
*mask
, const struct flow
*src_flow
,
7129 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
7130 &mask
->masks
, src_flow
, errorp
);
7136 /* A missing mask means that the flow should be exact matched.
7137 * Generate an appropriate exact wildcard for the flow. */
7138 flow_wildcards_init_for_packet(mask
, src_flow
);
7140 return ODP_FIT_PERFECT
;
7144 /* Converts the netlink formated key/mask to match.
7145 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
7146 * disagree on the acceptable form of flow */
7148 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
7149 const struct nlattr
*mask
, size_t mask_len
,
7150 struct match
*match
)
7152 enum odp_key_fitness fitness
;
7154 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
, NULL
);
7156 /* This should not happen: it indicates that
7157 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
7158 * the acceptable form of a flow. Log the problem as an error,
7159 * with enough details to enable debugging. */
7160 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7162 if (!VLOG_DROP_ERR(&rl
)) {
7166 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
7167 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
7174 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
,
7177 /* This should not happen: it indicates that
7178 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
7179 * disagree on the acceptable form of a mask. Log the problem
7180 * as an error, with enough details to enable debugging. */
7181 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
7183 if (!VLOG_DROP_ERR(&rl
)) {
7187 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
7189 VLOG_ERR("internal error parsing flow mask %s (%s)",
7190 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
7200 /* Returns 'fitness' as a string, for use in debug messages. */
7202 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
7205 case ODP_FIT_PERFECT
:
7207 case ODP_FIT_TOO_MUCH
:
7209 case ODP_FIT_TOO_LITTLE
:
7210 return "too_little";
7218 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
7219 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
7220 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
7221 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
7222 * null, then the return value is not meaningful.) */
7224 odp_put_userspace_action(uint32_t pid
,
7225 const void *userdata
, size_t userdata_size
,
7226 odp_port_t tunnel_out_port
,
7227 bool include_actions
,
7228 struct ofpbuf
*odp_actions
)
7230 size_t userdata_ofs
;
7233 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
7234 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
7236 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
7238 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
7239 * module before Linux 3.10 required the userdata to be exactly 8 bytes
7242 * - The kernel rejected shorter userdata with -ERANGE.
7244 * - The kernel silently dropped userdata beyond the first 8 bytes.
7246 * Thus, for maximum compatibility, always put at least 8 bytes. (We
7247 * separately disable features that required more than 8 bytes.) */
7248 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
7249 MAX(8, userdata_size
)),
7250 userdata
, userdata_size
);
7254 if (tunnel_out_port
!= ODPP_NONE
) {
7255 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
7258 if (include_actions
) {
7259 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
7261 nl_msg_end_nested(odp_actions
, offset
);
7263 return userdata_ofs
;
7267 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
7269 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
7273 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
7274 const struct eth_addr
*eth_src
,
7275 const struct eth_addr
*eth_dst
)
7277 struct ovs_action_push_eth eth
;
7279 memset(ð
, 0, sizeof eth
);
7281 eth
.addresses
.eth_src
= *eth_src
;
7284 eth
.addresses
.eth_dst
= *eth_dst
;
7287 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
7292 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
7293 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7295 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7296 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
7297 nl_msg_end_nested(odp_actions
, offset
);
7301 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
7302 struct ovs_action_push_tnl
*data
)
7304 int size
= offsetof(struct ovs_action_push_tnl
, header
);
7306 size
+= data
->header_len
;
7307 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
7311 /* The commit_odp_actions() function and its helpers. */
7314 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
7315 const void *key
, size_t key_size
)
7317 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7318 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
7319 nl_msg_end_nested(odp_actions
, offset
);
7322 /* Masked set actions have a mask following the data within the netlink
7323 * attribute. The unmasked bits in the data will be cleared as the data
7324 * is copied to the action. */
7326 commit_masked_set_action(struct ofpbuf
*odp_actions
,
7327 enum ovs_key_attr key_type
,
7328 const void *key_
, const void *mask_
, size_t key_size
)
7330 size_t offset
= nl_msg_start_nested(odp_actions
,
7331 OVS_ACTION_ATTR_SET_MASKED
);
7332 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
7333 const char *key
= key_
, *mask
= mask_
;
7335 memcpy(data
+ key_size
, mask
, key_size
);
7336 /* Clear unmasked bits while copying. */
7337 while (key_size
--) {
7338 *data
++ = *key
++ & *mask
++;
7340 nl_msg_end_nested(odp_actions
, offset
);
7343 /* If any of the flow key data that ODP actions can modify are different in
7344 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7345 * 'odp_actions' that change the flow tunneling information in key from
7346 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7347 * same way. In other words, operates the same as commit_odp_actions(), but
7348 * only on tunneling information. */
7350 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7351 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7353 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7354 * must have non-zero ipv6_dst. */
7355 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7356 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7359 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7360 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7365 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7366 const void *key
, void *base
, void *mask
, size_t size
,
7367 struct ofpbuf
*odp_actions
)
7369 if (memcmp(key
, base
, size
)) {
7370 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7372 if (use_masked_set
&& !fully_masked
) {
7373 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7375 if (!fully_masked
) {
7376 memset(mask
, 0xff, size
);
7378 commit_set_action(odp_actions
, attr
, key
, size
);
7380 memcpy(base
, key
, size
);
7383 /* Mask bits are set when we have either read or set the corresponding
7384 * values. Masked bits will be exact-matched, no need to set them
7385 * if the value did not actually change. */
7391 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7393 eth
->eth_src
= flow
->dl_src
;
7394 eth
->eth_dst
= flow
->dl_dst
;
7398 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7400 flow
->dl_src
= eth
->eth_src
;
7401 flow
->dl_dst
= eth
->eth_dst
;
7405 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7406 struct ofpbuf
*odp_actions
,
7407 struct flow_wildcards
*wc
,
7410 struct ovs_key_ethernet key
, base
, mask
;
7412 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7416 get_ethernet_key(flow
, &key
);
7417 get_ethernet_key(base_flow
, &base
);
7418 get_ethernet_key(&wc
->masks
, &mask
);
7420 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7421 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7422 put_ethernet_key(&base
, base_flow
);
7423 put_ethernet_key(&mask
, &wc
->masks
);
7428 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7429 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7431 int base_n
= flow_count_vlan_headers(base
);
7432 int flow_n
= flow_count_vlan_headers(flow
);
7433 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7435 /* Pop all mismatching vlan of base, push those of flow */
7436 for (; base_n
>= 0; base_n
--) {
7437 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7438 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7441 for (; flow_n
>= 0; flow_n
--) {
7442 struct ovs_action_push_vlan vlan
;
7444 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7445 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7446 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7447 &vlan
, sizeof vlan
);
7449 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7452 /* Wildcarding already done at action translation time. */
7454 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7455 struct ofpbuf
*odp_actions
)
7457 int base_n
= flow_count_mpls_labels(base
, NULL
);
7458 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7459 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7462 while (base_n
> common_n
) {
7463 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7464 /* If there is only one more LSE in base than there are common
7465 * between base and flow; and flow has at least one more LSE than
7466 * is common then the topmost LSE of base may be updated using
7468 struct ovs_key_mpls mpls_key
;
7470 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7471 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7472 &mpls_key
, sizeof mpls_key
);
7473 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7476 /* Otherwise, if there more LSEs in base than are common between
7477 * base and flow then pop the topmost one. */
7479 /* If all the LSEs are to be popped and this is not the outermost
7480 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7481 * POP_MPLS action instead of flow->dl_type.
7483 * This is because the POP_MPLS action requires its ethertype
7484 * argument to be an MPLS ethernet type but in this case
7485 * flow->dl_type will be a non-MPLS ethernet type.
7487 * When the final POP_MPLS action occurs it use flow->dl_type and
7488 * the and the resulting packet will have the desired dl_type. */
7489 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7490 dl_type
= htons(ETH_TYPE_MPLS
);
7492 dl_type
= flow
->dl_type
;
7494 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7495 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7500 /* If, after the above popping and setting, there are more LSEs in flow
7501 * than base then some LSEs need to be pushed. */
7502 while (base_n
< flow_n
) {
7503 struct ovs_action_push_mpls
*mpls
;
7505 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7506 OVS_ACTION_ATTR_PUSH_MPLS
,
7508 mpls
->mpls_ethertype
= flow
->dl_type
;
7509 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7510 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7511 * headers if the flow is restored later due to returning from a patch
7512 * port or group bucket. */
7513 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7514 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7520 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7522 ipv4
->ipv4_src
= flow
->nw_src
;
7523 ipv4
->ipv4_dst
= flow
->nw_dst
;
7524 ipv4
->ipv4_proto
= flow
->nw_proto
;
7525 ipv4
->ipv4_tos
= flow
->nw_tos
;
7526 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7527 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7531 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7533 flow
->nw_src
= ipv4
->ipv4_src
;
7534 flow
->nw_dst
= ipv4
->ipv4_dst
;
7535 flow
->nw_proto
= ipv4
->ipv4_proto
;
7536 flow
->nw_tos
= ipv4
->ipv4_tos
;
7537 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7538 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7542 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7543 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7546 struct ovs_key_ipv4 key
, mask
, base
;
7548 /* Check that nw_proto and nw_frag remain unchanged. */
7549 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7550 flow
->nw_frag
== base_flow
->nw_frag
);
7552 get_ipv4_key(flow
, &key
, false);
7553 get_ipv4_key(base_flow
, &base
, false);
7554 get_ipv4_key(&wc
->masks
, &mask
, true);
7555 mask
.ipv4_proto
= 0; /* Not writeable. */
7556 mask
.ipv4_frag
= 0; /* Not writable. */
7558 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7559 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7560 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7563 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7565 put_ipv4_key(&base
, base_flow
, false);
7566 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
7567 put_ipv4_key(&mask
, &wc
->masks
, true);
7573 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7575 ipv6
->ipv6_src
= flow
->ipv6_src
;
7576 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7577 ipv6
->ipv6_label
= flow
->ipv6_label
;
7578 ipv6
->ipv6_proto
= flow
->nw_proto
;
7579 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7580 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7581 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7585 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7587 flow
->ipv6_src
= ipv6
->ipv6_src
;
7588 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7589 flow
->ipv6_label
= ipv6
->ipv6_label
;
7590 flow
->nw_proto
= ipv6
->ipv6_proto
;
7591 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7592 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7593 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
7597 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
7598 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7601 struct ovs_key_ipv6 key
, mask
, base
;
7603 /* Check that nw_proto and nw_frag remain unchanged. */
7604 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7605 flow
->nw_frag
== base_flow
->nw_frag
);
7607 get_ipv6_key(flow
, &key
, false);
7608 get_ipv6_key(base_flow
, &base
, false);
7609 get_ipv6_key(&wc
->masks
, &mask
, true);
7610 mask
.ipv6_proto
= 0; /* Not writeable. */
7611 mask
.ipv6_frag
= 0; /* Not writable. */
7612 mask
.ipv6_label
&= htonl(IPV6_LABEL_MASK
); /* Not writable. */
7614 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7615 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7616 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
7619 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7621 put_ipv6_key(&base
, base_flow
, false);
7622 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
7623 put_ipv6_key(&mask
, &wc
->masks
, true);
7629 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
7631 /* ARP key has padding, clear it. */
7632 memset(arp
, 0, sizeof *arp
);
7634 arp
->arp_sip
= flow
->nw_src
;
7635 arp
->arp_tip
= flow
->nw_dst
;
7636 arp
->arp_op
= htons(flow
->nw_proto
);
7637 arp
->arp_sha
= flow
->arp_sha
;
7638 arp
->arp_tha
= flow
->arp_tha
;
7642 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
7644 flow
->nw_src
= arp
->arp_sip
;
7645 flow
->nw_dst
= arp
->arp_tip
;
7646 flow
->nw_proto
= ntohs(arp
->arp_op
);
7647 flow
->arp_sha
= arp
->arp_sha
;
7648 flow
->arp_tha
= arp
->arp_tha
;
7651 static enum slow_path_reason
7652 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
7653 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7655 struct ovs_key_arp key
, mask
, base
;
7657 get_arp_key(flow
, &key
);
7658 get_arp_key(base_flow
, &base
);
7659 get_arp_key(&wc
->masks
, &mask
);
7661 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
7663 put_arp_key(&base
, base_flow
);
7664 put_arp_key(&mask
, &wc
->masks
);
7671 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
7673 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7674 icmp
->icmp_type
= ntohs(flow
->tp_src
);
7675 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
7679 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
7681 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7682 flow
->tp_src
= htons(icmp
->icmp_type
);
7683 flow
->tp_dst
= htons(icmp
->icmp_code
);
7686 static enum slow_path_reason
7687 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
7688 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7690 struct ovs_key_icmp key
, mask
, base
;
7691 enum ovs_key_attr attr
;
7693 if (is_icmpv4(flow
, NULL
)) {
7694 attr
= OVS_KEY_ATTR_ICMP
;
7695 } else if (is_icmpv6(flow
, NULL
)) {
7696 attr
= OVS_KEY_ATTR_ICMPV6
;
7701 get_icmp_key(flow
, &key
);
7702 get_icmp_key(base_flow
, &base
);
7703 get_icmp_key(&wc
->masks
, &mask
);
7705 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7706 put_icmp_key(&base
, base_flow
);
7707 put_icmp_key(&mask
, &wc
->masks
);
7714 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
7716 nd
->nd_target
= flow
->nd_target
;
7717 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7718 nd
->nd_sll
= flow
->arp_sha
;
7719 nd
->nd_tll
= flow
->arp_tha
;
7723 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
7725 flow
->nd_target
= nd
->nd_target
;
7726 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7727 flow
->arp_sha
= nd
->nd_sll
;
7728 flow
->arp_tha
= nd
->nd_tll
;
7732 get_nd_extensions_key(const struct flow
*flow
,
7733 struct ovs_key_nd_extensions
*nd_ext
)
7735 /* ND Extensions key has padding, clear it. */
7736 memset(nd_ext
, 0, sizeof *nd_ext
);
7737 nd_ext
->nd_reserved
= flow
->igmp_group_ip4
;
7738 nd_ext
->nd_options_type
= ntohs(flow
->tcp_flags
);
7742 put_nd_extensions_key(const struct ovs_key_nd_extensions
*nd_ext
,
7745 flow
->igmp_group_ip4
= nd_ext
->nd_reserved
;
7746 flow
->tcp_flags
= htons(nd_ext
->nd_options_type
);
7749 static enum slow_path_reason
7750 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
7751 struct ofpbuf
*odp_actions
,
7752 struct flow_wildcards
*wc
, bool use_masked
)
7754 struct ovs_key_nd key
, mask
, base
;
7756 get_nd_key(flow
, &key
);
7757 get_nd_key(base_flow
, &base
);
7758 get_nd_key(&wc
->masks
, &mask
);
7760 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7762 put_nd_key(&base
, base_flow
);
7763 put_nd_key(&mask
, &wc
->masks
);
7770 static enum slow_path_reason
7771 commit_set_nd_extensions_action(const struct flow
*flow
,
7772 struct flow
*base_flow
,
7773 struct ofpbuf
*odp_actions
,
7774 struct flow_wildcards
*wc
, bool use_masked
)
7776 struct ovs_key_nd_extensions key
, mask
, base
;
7778 get_nd_extensions_key(flow
, &key
);
7779 get_nd_extensions_key(base_flow
, &base
);
7780 get_nd_extensions_key(&wc
->masks
, &mask
);
7782 if (commit(OVS_KEY_ATTR_ND_EXTENSIONS
, use_masked
, &key
,
7783 &base
, &mask
, sizeof key
, odp_actions
)) {
7784 put_nd_extensions_key(&base
, base_flow
);
7785 put_nd_extensions_key(&mask
, &wc
->masks
);
7791 static enum slow_path_reason
7792 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
7793 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7798 /* Check if 'flow' really has an L3 header. */
7799 if (!flow
->nw_proto
) {
7803 switch (ntohs(base
->dl_type
)) {
7805 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
7809 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
7810 if (base
->nw_proto
== IPPROTO_ICMPV6
) {
7811 /* Commit extended attrs first to make sure
7812 correct options are added.*/
7813 reason
= commit_set_nd_extensions_action(flow
, base
,
7814 odp_actions
, wc
, use_masked
);
7815 reason
|= commit_set_nd_action(flow
, base
, odp_actions
,
7822 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
7829 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
7833 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
7834 memset(nsh
->context
, 0, sizeof(nsh
->context
));
7840 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
7841 bool is_mask OVS_UNUSED
)
7844 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
7845 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
7850 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
7851 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
7852 struct ovs_key_nsh
*mask
, size_t size
,
7853 struct ofpbuf
*odp_actions
)
7855 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
7857 if (memcmp(key
, base
, size
) == 0) {
7858 /* Mask bits are set when we have either read or set the corresponding
7859 * values. Masked bits will be exact-matched, no need to set them
7860 * if the value did not actually change. */
7864 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7866 if (use_masked_set
&& !fully_masked
) {
7868 struct ovs_nsh_key_base nsh_base
;
7869 struct ovs_nsh_key_base nsh_base_mask
;
7870 struct ovs_nsh_key_md1 md1
;
7871 struct ovs_nsh_key_md1 md1_mask
;
7872 size_t offset
= nl_msg_start_nested(odp_actions
,
7873 OVS_ACTION_ATTR_SET_MASKED
);
7875 nsh_base
.flags
= key
->flags
;
7876 nsh_base
.ttl
= key
->ttl
;
7877 nsh_base
.mdtype
= key
->mdtype
;
7878 nsh_base
.np
= key
->np
;
7879 nsh_base
.path_hdr
= key
->path_hdr
;
7881 nsh_base_mask
.flags
= mask
->flags
;
7882 nsh_base_mask
.ttl
= mask
->ttl
;
7883 nsh_base_mask
.mdtype
= mask
->mdtype
;
7884 nsh_base_mask
.np
= mask
->np
;
7885 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
7887 /* OVS_KEY_ATTR_NSH keys */
7888 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
7890 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
7891 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
7892 OVS_NSH_KEY_ATTR_BASE
,
7893 2 * sizeof(nsh_base
));
7894 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
7895 size_t lkey_size
= sizeof(nsh_base
);
7897 while (lkey_size
--) {
7898 *data
++ = *lkey
++ & *lmask
++;
7900 lmask
= (char *)&nsh_base_mask
;
7901 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
7903 switch (key
->mdtype
) {
7905 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
7906 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
7908 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
7909 data
= nl_msg_put_unspec_uninit(odp_actions
,
7910 OVS_NSH_KEY_ATTR_MD1
,
7912 lkey
= (char *)&md1
;
7913 lmask
= (char *)&md1_mask
;
7914 lkey_size
= sizeof(md1
);
7916 while (lkey_size
--) {
7917 *data
++ = *lkey
++ & *lmask
++;
7919 lmask
= (char *)&md1_mask
;
7920 memcpy(data
, lmask
, sizeof(md1_mask
));
7924 /* No match support for other MD formats yet. */
7928 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
7930 nl_msg_end_nested(odp_actions
, offset
);
7932 if (!fully_masked
) {
7933 memset(mask
, 0xff, size
);
7935 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7936 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
7937 nl_msg_end_nested(odp_actions
, offset
);
7939 memcpy(base
, key
, size
);
7944 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
7945 struct ofpbuf
*odp_actions
,
7946 struct flow_wildcards
*wc
,
7949 struct ovs_key_nsh key
, mask
, base
;
7951 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
7952 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
7956 /* Check that mdtype and np remain unchanged. */
7957 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
7958 flow
->nsh
.np
== base_flow
->nsh
.np
);
7960 get_nsh_key(flow
, &key
, false);
7961 get_nsh_key(base_flow
, &base
, false);
7962 get_nsh_key(&wc
->masks
, &mask
, true);
7963 mask
.mdtype
= 0; /* Not writable. */
7964 mask
.np
= 0; /* Not writable. */
7966 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
7967 sizeof key
, odp_actions
)) {
7968 put_nsh_key(&base
, base_flow
, false);
7969 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
7970 put_nsh_key(&mask
, &wc
->masks
, true);
7975 /* TCP, UDP, and SCTP keys have the same layout. */
7976 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
7977 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
7980 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
7982 tp
->tcp
.tcp_src
= flow
->tp_src
;
7983 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
7987 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
7989 flow
->tp_src
= tp
->tcp
.tcp_src
;
7990 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
7994 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
7995 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7998 enum ovs_key_attr key_type
;
7999 union ovs_key_tp key
, mask
, base
;
8001 /* Check if 'flow' really has an L3 header. */
8002 if (!flow
->nw_proto
) {
8006 if (!is_ip_any(base_flow
)) {
8010 if (flow
->nw_proto
== IPPROTO_TCP
) {
8011 key_type
= OVS_KEY_ATTR_TCP
;
8012 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
8013 key_type
= OVS_KEY_ATTR_UDP
;
8014 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
8015 key_type
= OVS_KEY_ATTR_SCTP
;
8020 get_tp_key(flow
, &key
);
8021 get_tp_key(base_flow
, &base
);
8022 get_tp_key(&wc
->masks
, &mask
);
8024 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
8026 put_tp_key(&base
, base_flow
);
8027 put_tp_key(&mask
, &wc
->masks
);
8032 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
8033 struct ofpbuf
*odp_actions
,
8034 struct flow_wildcards
*wc
,
8037 uint32_t key
, mask
, base
;
8039 key
= flow
->skb_priority
;
8040 base
= base_flow
->skb_priority
;
8041 mask
= wc
->masks
.skb_priority
;
8043 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
8044 sizeof key
, odp_actions
)) {
8045 base_flow
->skb_priority
= base
;
8046 wc
->masks
.skb_priority
= mask
;
8051 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
8052 struct ofpbuf
*odp_actions
,
8053 struct flow_wildcards
*wc
,
8056 uint32_t key
, mask
, base
;
8058 key
= flow
->pkt_mark
;
8059 base
= base_flow
->pkt_mark
;
8060 mask
= wc
->masks
.pkt_mark
;
8062 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
8063 sizeof key
, odp_actions
)) {
8064 base_flow
->pkt_mark
= base
;
8065 wc
->masks
.pkt_mark
= mask
;
8070 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
8072 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
8076 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
8077 const struct flow
*flow
,
8078 struct ofpbuf
*encap_data
)
8080 uint8_t * metadata
= NULL
;
8081 uint8_t md_size
= 0;
8083 switch (flow
->nsh
.mdtype
) {
8086 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
8087 metadata
= encap_data
->data
;
8088 md_size
= encap_data
->size
;
8097 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
8098 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
8099 nl_msg_end_nested(odp_actions
, offset
);
8103 commit_encap_decap_action(const struct flow
*flow
,
8104 struct flow
*base_flow
,
8105 struct ofpbuf
*odp_actions
,
8106 struct flow_wildcards
*wc
,
8107 bool pending_encap
, bool pending_decap
,
8108 struct ofpbuf
*encap_data
)
8110 if (pending_encap
) {
8111 switch (ntohl(flow
->packet_type
)) {
8114 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
8116 base_flow
->packet_type
= flow
->packet_type
;
8117 base_flow
->dl_src
= flow
->dl_src
;
8118 base_flow
->dl_dst
= flow
->dl_dst
;
8123 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
8124 base_flow
->packet_type
= flow
->packet_type
;
8125 /* Update all packet headers in base_flow. */
8126 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
8127 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
8130 /* Only the above protocols are supported for encap.
8131 * The check is done at action translation. */
8134 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
8135 /* This is an explicit or implicit decap case. */
8136 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
8137 base_flow
->packet_type
== htonl(PT_ETH
)) {
8138 /* Generate pop_eth and continue without recirculation. */
8139 odp_put_pop_eth_action(odp_actions
);
8140 base_flow
->packet_type
= flow
->packet_type
;
8141 base_flow
->dl_src
= eth_addr_zero
;
8142 base_flow
->dl_dst
= eth_addr_zero
;
8144 /* All other decap cases require recirculation.
8145 * No need to update the base flow here. */
8146 switch (ntohl(base_flow
->packet_type
)) {
8149 odp_put_pop_nsh_action(odp_actions
);
8152 /* Checks are done during translation. */
8158 wc
->masks
.packet_type
= OVS_BE32_MAX
;
8161 /* If any of the flow key data that ODP actions can modify are different in
8162 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
8163 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
8164 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
8165 * in addition to this function if needed. Sets fields in 'wc' that are
8166 * used as part of the action.
8168 * Returns a reason to force processing the flow's packets into the userspace
8169 * slow path, if there is one, otherwise 0. */
8170 enum slow_path_reason
8171 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
8172 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
8173 bool use_masked
, bool pending_encap
, bool pending_decap
,
8174 struct ofpbuf
*encap_data
)
8176 enum slow_path_reason slow1
, slow2
;
8177 bool mpls_done
= false;
8179 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
8180 pending_encap
, pending_decap
, encap_data
);
8181 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
8182 /* Make packet a non-MPLS packet before committing L3/4 actions,
8183 * which would otherwise do nothing. */
8184 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
8185 commit_mpls_action(flow
, base
, odp_actions
);
8188 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
8189 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
8190 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
8191 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
8193 commit_mpls_action(flow
, base
, odp_actions
);
8195 commit_vlan_action(flow
, base
, odp_actions
, wc
);
8196 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
8197 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
8199 return slow1
? slow1
: slow2
;