2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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_MPLS
: return "mpls";
177 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
178 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
179 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
180 case OVS_KEY_ATTR_NSH
: return "nsh";
182 case __OVS_KEY_ATTR_MAX
:
184 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
190 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
192 size_t len
= nl_attr_get_size(a
);
194 ds_put_format(ds
, "action%d", nl_attr_type(a
));
196 const uint8_t *unspec
;
199 unspec
= nl_attr_get(a
);
200 for (i
= 0; i
< len
; i
++) {
201 ds_put_char(ds
, i
? ' ': '(');
202 ds_put_format(ds
, "%02x", unspec
[i
]);
204 ds_put_char(ds
, ')');
209 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
210 const struct hmap
*portno_names
)
212 static const struct nl_policy ovs_sample_policy
[] = {
213 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
214 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
216 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
218 const struct nlattr
*nla_acts
;
221 ds_put_cstr(ds
, "sample");
223 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
224 ds_put_cstr(ds
, "(error)");
228 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
231 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
233 ds_put_cstr(ds
, "actions(");
234 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
235 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
236 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
237 ds_put_format(ds
, "))");
241 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
242 const struct hmap
*portno_names
)
244 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
245 int len
= nl_attr_get_size(attr
);
247 ds_put_cstr(ds
, "clone");
248 ds_put_format(ds
, "(");
249 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
250 ds_put_format(ds
, ")");
254 format_nsh_key(struct ds
*ds
, const struct ovs_key_nsh
*key
)
256 ds_put_format(ds
, "flags=%d", key
->flags
);
257 ds_put_format(ds
, "ttl=%d", key
->ttl
);
258 ds_put_format(ds
, ",mdtype=%d", key
->mdtype
);
259 ds_put_format(ds
, ",np=%d", key
->np
);
260 ds_put_format(ds
, ",spi=0x%x",
261 nsh_path_hdr_to_spi_uint32(key
->path_hdr
));
262 ds_put_format(ds
, ",si=%d",
263 nsh_path_hdr_to_si(key
->path_hdr
));
265 switch (key
->mdtype
) {
267 for (int i
= 0; i
< 4; i
++) {
268 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->context
[i
]));
273 /* No support for matching other metadata formats yet. */
279 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
280 uint8_t value
, uint8_t mask
)
286 ds_put_format(s
, "%s=", name
);
287 if (mask
== UINT8_MAX
) {
288 ds_put_format(s
, "%"PRIu8
, value
);
290 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
297 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
298 ovs_be32 value
, ovs_be32 mask
)
300 if (mask
!= htonl(0)) {
304 ds_put_format(s
, "%s=", name
);
305 if (mask
== OVS_BE32_MAX
) {
306 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
308 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
309 ntohl(value
), ntohl(mask
));
316 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
317 const struct ovs_key_nsh
*mask
)
320 format_nsh_key(ds
, key
);
323 uint32_t spi
= nsh_path_hdr_to_spi_uint32(key
->path_hdr
);
324 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
->path_hdr
);
325 if (spi_mask
== (NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
326 spi_mask
= UINT32_MAX
;
328 uint8_t si
= nsh_path_hdr_to_si(key
->path_hdr
);
329 uint8_t si_mask
= nsh_path_hdr_to_si(mask
->path_hdr
);
331 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
332 format_uint8_masked(ds
, &first
, "ttl", key
->ttl
, mask
->ttl
);
333 format_uint8_masked(ds
, &first
, "mdtype", key
->mdtype
, mask
->mdtype
);
334 format_uint8_masked(ds
, &first
, "np", key
->np
, mask
->np
);
335 format_be32_masked(ds
, &first
, "spi", htonl(spi
), htonl(spi_mask
));
336 format_uint8_masked(ds
, &first
, "si", si
, si_mask
);
337 format_be32_masked(ds
, &first
, "c1", key
->context
[0],
339 format_be32_masked(ds
, &first
, "c2", key
->context
[1],
341 format_be32_masked(ds
, &first
, "c3", key
->context
[2],
343 format_be32_masked(ds
, &first
, "c4", key
->context
[3],
349 format_odp_push_nsh_action(struct ds
*ds
,
350 const struct nsh_hdr
*nsh_hdr
)
352 size_t mdlen
= nsh_hdr_len(nsh_hdr
) - NSH_BASE_HDR_LEN
;
353 uint32_t spi
= ntohl(nsh_get_spi(nsh_hdr
));
354 uint8_t si
= nsh_get_si(nsh_hdr
);
355 uint8_t flags
= nsh_get_flags(nsh_hdr
);
356 uint8_t ttl
= nsh_get_ttl(nsh_hdr
);
358 ds_put_cstr(ds
, "push_nsh(");
359 ds_put_format(ds
, "flags=%d", flags
);
360 ds_put_format(ds
, ",ttl=%d", ttl
);
361 ds_put_format(ds
, ",mdtype=%d", nsh_hdr
->md_type
);
362 ds_put_format(ds
, ",np=%d", nsh_hdr
->next_proto
);
363 ds_put_format(ds
, ",spi=0x%x", spi
);
364 ds_put_format(ds
, ",si=%d", si
);
365 switch (nsh_hdr
->md_type
) {
367 const struct nsh_md1_ctx
*md1_ctx
= &nsh_hdr
->md1
;
368 for (int i
= 0; i
< 4; i
++) {
369 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
370 ntohl(get_16aligned_be32(&md1_ctx
->context
[i
])));
375 const struct nsh_md2_tlv
*md2_ctx
= &nsh_hdr
->md2
;
376 ds_put_cstr(ds
, ",md2=");
377 ds_put_hex(ds
, md2_ctx
, mdlen
);
383 ds_put_format(ds
, ")");
387 slow_path_reason_to_string(uint32_t reason
)
389 switch ((enum slow_path_reason
) reason
) {
390 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
399 slow_path_reason_to_explanation(enum slow_path_reason reason
)
402 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
411 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
412 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
414 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
415 res_flags
, allowed
, res_mask
);
419 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
420 const struct hmap
*portno_names
)
422 static const struct nl_policy ovs_userspace_policy
[] = {
423 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
424 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
426 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
428 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
431 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
432 const struct nlattr
*userdata_attr
;
433 const struct nlattr
*tunnel_out_port_attr
;
435 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
436 ds_put_cstr(ds
, "userspace(error)");
440 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
441 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
443 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
446 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
447 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
448 bool userdata_unspec
= true;
449 struct user_action_cookie cookie
;
451 if (userdata_len
== sizeof cookie
) {
452 memcpy(&cookie
, userdata
, sizeof cookie
);
454 userdata_unspec
= false;
456 if (cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
457 ds_put_format(ds
, ",sFlow("
458 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
459 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
460 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
461 cookie
.sflow
.output
);
462 } else if (cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
463 ds_put_cstr(ds
, ",slow_path(");
464 format_flags(ds
, slow_path_reason_to_string
,
465 cookie
.slow_path
.reason
, ',');
466 ds_put_format(ds
, ")");
467 } else if (cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
468 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
469 ",collector_set_id=%"PRIu32
470 ",obs_domain_id=%"PRIu32
471 ",obs_point_id=%"PRIu32
473 cookie
.flow_sample
.probability
,
474 cookie
.flow_sample
.collector_set_id
,
475 cookie
.flow_sample
.obs_domain_id
,
476 cookie
.flow_sample
.obs_point_id
);
477 odp_portno_name_format(portno_names
,
478 cookie
.flow_sample
.output_odp_port
, ds
);
479 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
480 ds_put_cstr(ds
, ",ingress");
481 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
482 ds_put_cstr(ds
, ",egress");
484 ds_put_char(ds
, ')');
485 } else if (cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
486 ds_put_format(ds
, ",ipfix(output_port=");
487 odp_portno_name_format(portno_names
,
488 cookie
.ipfix
.output_odp_port
, ds
);
489 ds_put_char(ds
, ')');
490 } else if (cookie
.type
== USER_ACTION_COOKIE_CONTROLLER
) {
491 ds_put_format(ds
, ",controller(reason=%"PRIu16
495 ",rule_cookie=%#"PRIx64
496 ",controller_id=%"PRIu16
498 cookie
.controller
.reason
,
499 !!cookie
.controller
.dont_send
,
500 !!cookie
.controller
.continuation
,
501 cookie
.controller
.recirc_id
,
502 ntohll(get_32aligned_be64(
503 &cookie
.controller
.rule_cookie
)),
504 cookie
.controller
.controller_id
,
505 cookie
.controller
.max_len
);
506 ds_put_char(ds
, ')');
508 userdata_unspec
= true;
512 if (userdata_unspec
) {
514 ds_put_format(ds
, ",userdata(");
515 for (i
= 0; i
< userdata_len
; i
++) {
516 ds_put_format(ds
, "%02x", userdata
[i
]);
518 ds_put_char(ds
, ')');
522 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
523 ds_put_cstr(ds
, ",actions");
526 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
527 if (tunnel_out_port_attr
) {
528 ds_put_format(ds
, ",tunnel_out_port=");
529 odp_portno_name_format(portno_names
,
530 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
533 ds_put_char(ds
, ')');
537 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
539 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
540 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
541 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
542 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
544 ds_put_char(ds
, ',');
546 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
547 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
548 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
549 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
551 ds_put_char(ds
, ',');
553 if (!(tci
& htons(VLAN_CFI
))) {
554 ds_put_cstr(ds
, "cfi=0");
555 ds_put_char(ds
, ',');
561 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
563 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
564 mpls_lse_to_label(mpls_lse
),
565 mpls_lse_to_tc(mpls_lse
),
566 mpls_lse_to_ttl(mpls_lse
),
567 mpls_lse_to_bos(mpls_lse
));
571 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
572 const struct ovs_key_mpls
*mpls_mask
, int n
)
574 for (int i
= 0; i
< n
; i
++) {
575 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
577 if (mpls_mask
== NULL
) {
578 format_mpls_lse(ds
, key
);
580 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
582 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
583 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
584 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
585 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
586 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
588 ds_put_char(ds
, ',');
594 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
596 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
600 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
602 ds_put_format(ds
, "hash(");
604 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
605 ds_put_format(ds
, "l4(%"PRIu32
")", hash_act
->hash_basis
);
606 } else if (hash_act
->hash_alg
== OVS_HASH_ALG_SYM_L4
) {
607 ds_put_format(ds
, "sym_l4(%"PRIu32
")", hash_act
->hash_basis
);
609 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
612 ds_put_format(ds
, ")");
616 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
618 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
619 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
620 ntohs(udp
->udp_csum
));
626 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
628 const struct eth_header
*eth
;
631 const struct udp_header
*udp
;
633 eth
= (const struct eth_header
*)data
->header
;
638 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
639 data
->header_len
, data
->tnl_type
);
640 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
641 ds_put_format(ds
, ",src=");
642 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
643 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
645 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
647 const struct ip_header
*ip
= l3
;
648 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
649 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
650 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
651 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
652 ip
->ip_proto
, ip
->ip_tos
,
654 ntohs(ip
->ip_frag_off
));
657 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
658 struct in6_addr src
, dst
;
659 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
660 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
661 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
663 ds_put_format(ds
, "ipv6(src=");
664 ipv6_format_addr(&src
, ds
);
665 ds_put_format(ds
, ",dst=");
666 ipv6_format_addr(&dst
, ds
);
667 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
668 ",hlimit=%"PRIu8
"),",
669 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
670 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
674 udp
= (const struct udp_header
*) l4
;
676 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
677 const struct vxlanhdr
*vxh
;
679 vxh
= format_udp_tnl_push_header(ds
, udp
);
681 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
682 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
683 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
684 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
685 const struct genevehdr
*gnh
;
687 gnh
= format_udp_tnl_push_header(ds
, udp
);
689 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
690 gnh
->oam
? "oam," : "",
691 gnh
->critical
? "crit," : "",
692 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
695 ds_put_cstr(ds
, ",options(");
696 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
698 ds_put_char(ds
, ')');
701 ds_put_char(ds
, ')');
702 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
703 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
704 const struct gre_base_hdr
*greh
;
705 ovs_16aligned_be32
*options
;
707 greh
= (const struct gre_base_hdr
*) l4
;
709 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
710 ntohs(greh
->flags
), ntohs(greh
->protocol
));
711 options
= (ovs_16aligned_be32
*)(greh
+ 1);
712 if (greh
->flags
& htons(GRE_CSUM
)) {
713 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
716 if (greh
->flags
& htons(GRE_KEY
)) {
717 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
720 if (greh
->flags
& htons(GRE_SEQ
)) {
721 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
724 ds_put_format(ds
, ")");
725 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
726 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
727 const struct gre_base_hdr
*greh
;
728 const struct erspan_base_hdr
*ersh
;
730 greh
= (const struct gre_base_hdr
*) l4
;
731 ersh
= ERSPAN_HDR(greh
);
733 if (ersh
->ver
== 1) {
734 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
736 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
737 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
738 } else if (ersh
->ver
== 2) {
739 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
741 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
742 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
743 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
745 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
748 ds_put_format(ds
, ")");
752 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
753 const struct hmap
*portno_names
)
755 struct ovs_action_push_tnl
*data
;
757 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
759 ds_put_cstr(ds
, "tnl_push(tnl_port(");
760 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
761 ds_put_cstr(ds
, "),");
762 format_odp_tnl_push_header(ds
, data
);
763 ds_put_format(ds
, ",out_port(");
764 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
765 ds_put_cstr(ds
, "))");
768 static const struct nl_policy ovs_nat_policy
[] = {
769 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
770 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
771 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
772 .min_len
= sizeof(struct in_addr
),
773 .max_len
= sizeof(struct in6_addr
)},
774 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
775 .min_len
= sizeof(struct in_addr
),
776 .max_len
= sizeof(struct in6_addr
)},
777 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
778 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
779 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
780 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
781 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
785 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
787 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
789 ovs_be32 ip_min
, ip_max
;
790 struct in6_addr ip6_min
, ip6_max
;
791 uint16_t proto_min
, proto_max
;
793 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
794 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
797 /* If no type, then nothing else either. */
798 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
799 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
800 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
801 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
802 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
803 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
806 /* Both SNAT & DNAT may not be specified. */
807 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
808 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
811 /* proto may not appear without ip. */
812 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
813 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
816 /* MAX may not appear without MIN. */
817 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
818 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
819 ds_put_cstr(ds
, "nat(error: range max without min.)");
822 /* Address sizes must match. */
823 if ((a
[OVS_NAT_ATTR_IP_MIN
]
824 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
825 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
826 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
827 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
828 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
829 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
833 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
834 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
835 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
836 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
837 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
838 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
839 if (addr_len
== sizeof ip6_min
) {
840 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
841 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
843 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
844 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
847 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
848 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
849 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
850 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
852 if ((addr_len
== sizeof(ovs_be32
)
853 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
854 || (addr_len
== sizeof(struct in6_addr
)
855 && !ipv6_mask_is_any(&ip6_max
)
856 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
857 || (proto_max
&& proto_min
> proto_max
)) {
858 ds_put_cstr(ds
, "nat(range error)");
862 ds_put_cstr(ds
, "nat");
863 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
864 ds_put_char(ds
, '(');
865 if (a
[OVS_NAT_ATTR_SRC
]) {
866 ds_put_cstr(ds
, "src");
867 } else if (a
[OVS_NAT_ATTR_DST
]) {
868 ds_put_cstr(ds
, "dst");
872 ds_put_cstr(ds
, "=");
874 if (addr_len
== sizeof ip_min
) {
875 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
877 if (ip_max
&& ip_max
!= ip_min
) {
878 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
880 } else if (addr_len
== sizeof ip6_min
) {
881 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
883 if (!ipv6_mask_is_any(&ip6_max
) &&
884 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
885 ds_put_char(ds
, '-');
886 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
890 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
892 if (proto_max
&& proto_max
!= proto_min
) {
893 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
897 ds_put_char(ds
, ',');
898 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
899 ds_put_cstr(ds
, "persistent,");
901 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
902 ds_put_cstr(ds
, "hash,");
904 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
905 ds_put_cstr(ds
, "random,");
908 ds_put_char(ds
, ')');
912 static const struct nl_policy ovs_conntrack_policy
[] = {
913 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
914 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
915 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
916 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
917 .min_len
= sizeof(uint32_t) * 2 },
918 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
919 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
920 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
921 .min_len
= 1, .max_len
= 16 },
922 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
926 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
928 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
930 ovs_32aligned_u128 value
;
931 ovs_32aligned_u128 mask
;
933 const uint32_t *mark
;
937 const struct nlattr
*nat
;
939 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
940 ds_put_cstr(ds
, "ct(error)");
944 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
945 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
946 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
947 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
948 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
949 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
950 nat
= a
[OVS_CT_ATTR_NAT
];
952 ds_put_format(ds
, "ct");
953 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
954 ds_put_cstr(ds
, "(");
956 ds_put_format(ds
, "commit,");
959 ds_put_format(ds
, "force_commit,");
962 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
965 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
969 ds_put_format(ds
, "label=");
970 format_u128(ds
, &label
->value
, &label
->mask
, true);
971 ds_put_char(ds
, ',');
974 ds_put_format(ds
, "helper=%s,", helper
);
977 format_odp_ct_nat(ds
, nat
);
980 ds_put_cstr(ds
, ")");
984 static const struct attr_len_tbl
985 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
986 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
987 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
988 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
992 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
995 const struct nlattr
*a
;
996 struct ovs_key_nsh nsh
;
997 struct ovs_key_nsh nsh_mask
;
999 memset(&nsh
, 0, sizeof nsh
);
1000 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
1002 NL_NESTED_FOR_EACH (a
, left
, attr
) {
1003 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
1004 size_t len
= nl_attr_get_size(a
);
1006 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1010 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1011 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1016 case OVS_NSH_KEY_ATTR_UNSPEC
:
1018 case OVS_NSH_KEY_ATTR_BASE
: {
1019 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1020 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1021 memcpy(&nsh
, base
, sizeof(*base
));
1022 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1025 case OVS_NSH_KEY_ATTR_MD1
: {
1026 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1027 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1028 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1029 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1032 case OVS_NSH_KEY_ATTR_MD2
:
1033 case __OVS_NSH_KEY_ATTR_MAX
:
1035 /* No support for matching other metadata formats yet. */
1040 ds_put_cstr(ds
, "set(nsh(");
1041 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1042 ds_put_cstr(ds
, "))");
1047 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1048 const struct hmap
*portno_names
)
1051 enum ovs_action_attr type
= nl_attr_type(a
);
1054 expected_len
= odp_action_len(nl_attr_type(a
));
1055 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1056 nl_attr_get_size(a
) != expected_len
) {
1057 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1058 nl_attr_get_size(a
), expected_len
);
1059 format_generic_odp_action(ds
, a
);
1064 case OVS_ACTION_ATTR_METER
:
1065 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1067 case OVS_ACTION_ATTR_OUTPUT
:
1068 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1070 case OVS_ACTION_ATTR_TRUNC
: {
1071 const struct ovs_action_trunc
*trunc
=
1072 nl_attr_get_unspec(a
, sizeof *trunc
);
1074 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1077 case OVS_ACTION_ATTR_TUNNEL_POP
:
1078 ds_put_cstr(ds
, "tnl_pop(");
1079 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1080 ds_put_char(ds
, ')');
1082 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1083 format_odp_tnl_push_action(ds
, a
, portno_names
);
1085 case OVS_ACTION_ATTR_USERSPACE
:
1086 format_odp_userspace_action(ds
, a
, portno_names
);
1088 case OVS_ACTION_ATTR_RECIRC
:
1089 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1091 case OVS_ACTION_ATTR_HASH
:
1092 format_odp_hash_action(ds
, nl_attr_get(a
));
1094 case OVS_ACTION_ATTR_SET_MASKED
:
1096 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1097 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1098 format_odp_set_nsh(ds
, a
);
1101 size
= nl_attr_get_size(a
) / 2;
1102 ds_put_cstr(ds
, "set(");
1104 /* Masked set action not supported for tunnel key, which is bigger. */
1105 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1106 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1107 sizeof(struct nlattr
))];
1108 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1109 sizeof(struct nlattr
))];
1111 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1112 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1113 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1114 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1115 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1117 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1119 ds_put_cstr(ds
, ")");
1121 case OVS_ACTION_ATTR_SET
:
1122 ds_put_cstr(ds
, "set(");
1123 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1124 ds_put_cstr(ds
, ")");
1126 case OVS_ACTION_ATTR_PUSH_ETH
: {
1127 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1128 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1129 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1130 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1133 case OVS_ACTION_ATTR_POP_ETH
:
1134 ds_put_cstr(ds
, "pop_eth");
1136 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1137 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1138 ds_put_cstr(ds
, "push_vlan(");
1139 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1140 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1142 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1143 ds_put_char(ds
, ')');
1146 case OVS_ACTION_ATTR_POP_VLAN
:
1147 ds_put_cstr(ds
, "pop_vlan");
1149 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1150 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1151 ds_put_cstr(ds
, "push_mpls(");
1152 format_mpls_lse(ds
, mpls
->mpls_lse
);
1153 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1156 case OVS_ACTION_ATTR_POP_MPLS
: {
1157 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1158 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1161 case OVS_ACTION_ATTR_SAMPLE
:
1162 format_odp_sample_action(ds
, a
, portno_names
);
1164 case OVS_ACTION_ATTR_CT
:
1165 format_odp_conntrack_action(ds
, a
);
1167 case OVS_ACTION_ATTR_CT_CLEAR
:
1168 ds_put_cstr(ds
, "ct_clear");
1170 case OVS_ACTION_ATTR_CLONE
:
1171 format_odp_clone_action(ds
, a
, portno_names
);
1173 case OVS_ACTION_ATTR_PUSH_NSH
: {
1174 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1175 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1176 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1177 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1178 format_odp_push_nsh_action(ds
, nsh_hdr
);
1181 case OVS_ACTION_ATTR_POP_NSH
:
1182 ds_put_cstr(ds
, "pop_nsh()");
1184 case OVS_ACTION_ATTR_UNSPEC
:
1185 case __OVS_ACTION_ATTR_MAX
:
1187 format_generic_odp_action(ds
, a
);
1193 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1194 size_t actions_len
, const struct hmap
*portno_names
)
1197 const struct nlattr
*a
;
1200 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1202 ds_put_char(ds
, ',');
1204 format_odp_action(ds
, a
, portno_names
);
1209 if (left
== actions_len
) {
1210 ds_put_cstr(ds
, "<empty>");
1212 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1213 for (i
= 0; i
< left
; i
++) {
1214 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1216 ds_put_char(ds
, ')');
1219 ds_put_cstr(ds
, "drop");
1223 /* Separate out parse_odp_userspace_action() function. */
1225 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1228 struct user_action_cookie cookie
;
1230 odp_port_t tunnel_out_port
;
1232 void *user_data
= NULL
;
1233 size_t user_data_size
= 0;
1234 bool include_actions
= false;
1237 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1241 ofpbuf_init(&buf
, 16);
1242 memset(&cookie
, 0, sizeof cookie
);
1244 user_data
= &cookie
;
1245 user_data_size
= sizeof cookie
;
1248 uint32_t probability
;
1249 uint32_t collector_set_id
;
1250 uint32_t obs_domain_id
;
1251 uint32_t obs_point_id
;
1253 /* USER_ACTION_COOKIE_CONTROLLER. */
1255 uint8_t continuation
;
1258 uint64_t rule_cookie
;
1259 uint16_t controller_id
;
1264 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1265 "pcp=%i,output=%"SCNi32
")%n",
1266 &vid
, &pcp
, &output
, &n1
)) {
1270 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1275 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1276 cookie
.ofp_in_port
= OFPP_NONE
;
1277 cookie
.ofproto_uuid
= UUID_ZERO
;
1278 cookie
.sflow
.vlan_tci
= htons(tci
);
1279 cookie
.sflow
.output
= output
;
1280 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1283 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1284 cookie
.ofp_in_port
= OFPP_NONE
;
1285 cookie
.ofproto_uuid
= UUID_ZERO
;
1286 cookie
.slow_path
.reason
= 0;
1288 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1289 &cookie
.slow_path
.reason
,
1290 SLOW_PATH_REASON_MASK
, NULL
);
1291 if (res
< 0 || s
[n
+ res
] != ')') {
1295 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1296 "collector_set_id=%"SCNi32
","
1297 "obs_domain_id=%"SCNi32
","
1298 "obs_point_id=%"SCNi32
","
1299 "output_port=%"SCNi32
"%n",
1300 &probability
, &collector_set_id
,
1301 &obs_domain_id
, &obs_point_id
,
1305 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1306 cookie
.ofp_in_port
= OFPP_NONE
;
1307 cookie
.ofproto_uuid
= UUID_ZERO
;
1308 cookie
.flow_sample
.probability
= probability
;
1309 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1310 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1311 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1312 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1314 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1315 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1317 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1318 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1321 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1328 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1331 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1332 cookie
.ofp_in_port
= OFPP_NONE
;
1333 cookie
.ofproto_uuid
= UUID_ZERO
;
1334 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1335 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1337 ",continuation=%"SCNu8
1338 ",recirc_id=%"SCNu32
1339 ",rule_cookie=%"SCNx64
1340 ",controller_id=%"SCNu16
1341 ",max_len=%"SCNu16
")%n",
1342 &reason
, &dont_send
, &continuation
, &recirc_id
,
1343 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1345 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1346 cookie
.ofp_in_port
= OFPP_NONE
;
1347 cookie
.ofproto_uuid
= UUID_ZERO
;
1348 cookie
.controller
.dont_send
= dont_send
? true : false;
1349 cookie
.controller
.continuation
= continuation
? true : false;
1350 cookie
.controller
.reason
= reason
;
1351 cookie
.controller
.recirc_id
= recirc_id
;
1352 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1353 htonll(rule_cookie
));
1354 cookie
.controller
.controller_id
= controller_id
;
1355 cookie
.controller
.max_len
= max_len
;
1356 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1360 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1361 if (end
[0] != ')') {
1365 user_data
= buf
.data
;
1366 user_data_size
= buf
.size
;
1373 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1375 include_actions
= true;
1381 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1382 &tunnel_out_port
, &n1
)) {
1383 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1384 tunnel_out_port
, include_actions
, actions
);
1387 } else if (s
[n
] == ')') {
1388 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1389 ODPP_NONE
, include_actions
, actions
);
1396 struct ovs_action_push_eth push
;
1400 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1401 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1402 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1403 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1406 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1407 &push
, sizeof push
);
1414 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1415 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1422 ofpbuf_uninit(&buf
);
1427 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1429 struct eth_header
*eth
;
1430 struct ip_header
*ip
;
1431 struct ovs_16aligned_ip6_hdr
*ip6
;
1432 struct udp_header
*udp
;
1433 struct gre_base_hdr
*greh
;
1434 struct erspan_base_hdr
*ersh
;
1435 struct erspan_md2
*md2
;
1436 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
, sid
;
1438 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1443 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1446 eth
= (struct eth_header
*) data
->header
;
1447 l3
= (struct ip_header
*) (eth
+ 1);
1448 ip
= (struct ip_header
*) l3
;
1449 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1450 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1451 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1454 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1458 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1459 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1462 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1465 eth
->eth_type
= htons(dl_type
);
1467 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1469 uint16_t ip_frag_off
;
1470 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1471 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1474 &ip
->ip_proto
, &ip
->ip_tos
,
1475 &ip
->ip_ttl
, &ip_frag_off
)) {
1478 put_16aligned_be32(&ip
->ip_src
, sip
);
1479 put_16aligned_be32(&ip
->ip_dst
, dip
);
1480 ip
->ip_frag_off
= htons(ip_frag_off
);
1481 ip_len
= sizeof *ip
;
1483 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1484 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1485 struct in6_addr sip6
, dip6
;
1488 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1489 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1490 ",hlimit=%"SCNi8
"),",
1491 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1492 &tclass
, &ip6
->ip6_hlim
)
1493 || (label
& ~IPV6_LABEL_MASK
) != 0
1494 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1495 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1498 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1499 htonl(tclass
<< 20) | htonl(label
));
1500 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1501 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1502 ip_len
= sizeof *ip6
;
1506 l4
= ((uint8_t *) l3
+ ip_len
);
1507 udp
= (struct udp_header
*) l4
;
1508 greh
= (struct gre_base_hdr
*) l4
;
1509 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1510 &udp_src
, &udp_dst
, &csum
)) {
1511 uint32_t vx_flags
, vni
;
1513 udp
->udp_src
= htons(udp_src
);
1514 udp
->udp_dst
= htons(udp_dst
);
1516 udp
->udp_csum
= htons(csum
);
1518 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1520 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1522 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1523 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1524 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1525 header_len
= sizeof *eth
+ ip_len
+
1526 sizeof *udp
+ sizeof *vxh
;
1527 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1528 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1530 memset(gnh
, 0, sizeof *gnh
);
1531 header_len
= sizeof *eth
+ ip_len
+
1532 sizeof *udp
+ sizeof *gnh
;
1534 if (ovs_scan_len(s
, &n
, "oam,")) {
1537 if (ovs_scan_len(s
, &n
, "crit,")) {
1540 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1543 if (ovs_scan_len(s
, &n
, ",options(")) {
1544 struct geneve_scan options
;
1547 memset(&options
, 0, sizeof options
);
1548 len
= scan_geneve(s
+ n
, &options
, NULL
);
1553 memcpy(gnh
->options
, options
.d
, options
.len
);
1554 gnh
->opt_len
= options
.len
/ 4;
1555 header_len
+= options
.len
;
1559 if (!ovs_scan_len(s
, &n
, "))")) {
1563 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1564 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1565 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1569 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1570 &gre_flags
, &gre_proto
)){
1572 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1573 tnl_type
= OVS_VPORT_TYPE_GRE
;
1575 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1577 greh
->flags
= htons(gre_flags
);
1578 greh
->protocol
= htons(gre_proto
);
1579 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1581 if (greh
->flags
& htons(GRE_CSUM
)) {
1582 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1586 memset(options
, 0, sizeof *options
);
1587 *((ovs_be16
*)options
) = htons(csum
);
1590 if (greh
->flags
& htons(GRE_KEY
)) {
1593 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1597 put_16aligned_be32(options
, htonl(key
));
1600 if (greh
->flags
& htons(GRE_SEQ
)) {
1603 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1606 put_16aligned_be32(options
, htonl(seq
));
1610 if (!ovs_scan_len(s
, &n
, "))")) {
1614 header_len
= sizeof *eth
+ ip_len
+
1615 ((uint8_t *) options
- (uint8_t *) greh
);
1616 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1617 &sid
, &erspan_idx
)) {
1618 ersh
= ERSPAN_HDR(greh
);
1619 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1622 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1623 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1625 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1628 greh
->flags
= htons(GRE_SEQ
);
1629 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1633 put_16aligned_be32(index
, htonl(erspan_idx
));
1635 if (!ovs_scan_len(s
, &n
, ")")) {
1638 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1639 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1641 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1642 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1644 ersh
= ERSPAN_HDR(greh
);
1645 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1647 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1648 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1650 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1653 greh
->flags
= htons(GRE_SEQ
);
1654 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1658 set_hwid(md2
, hwid
);
1661 if (!ovs_scan_len(s
, &n
, ")")) {
1665 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1666 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1671 /* check tunnel meta data. */
1672 if (data
->tnl_type
!= tnl_type
) {
1675 if (data
->header_len
!= header_len
) {
1680 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1687 struct ct_nat_params
{
1693 struct in6_addr ip6
;
1697 struct in6_addr ip6
;
1707 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1709 if (ovs_scan_len(s
, n
, "=")) {
1710 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1711 struct in6_addr ipv6
;
1713 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1714 p
->addr_len
= sizeof p
->addr_min
.ip
;
1715 if (ovs_scan_len(s
, n
, "-")) {
1716 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1717 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1721 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1722 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1723 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1724 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1725 p
->addr_min
.ip6
= ipv6
;
1726 if (ovs_scan_len(s
, n
, "-")) {
1727 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1728 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1729 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1730 p
->addr_max
.ip6
= ipv6
;
1738 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1739 if (ovs_scan_len(s
, n
, "-")) {
1740 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1750 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1754 if (ovs_scan_len(s
, &n
, "nat")) {
1755 memset(p
, 0, sizeof *p
);
1757 if (ovs_scan_len(s
, &n
, "(")) {
1761 end
= strchr(s
+ n
, ')');
1768 n
+= strspn(s
+ n
, delimiters
);
1769 if (ovs_scan_len(s
, &n
, "src")) {
1770 int err
= scan_ct_nat_range(s
, &n
, p
);
1777 if (ovs_scan_len(s
, &n
, "dst")) {
1778 int err
= scan_ct_nat_range(s
, &n
, p
);
1785 if (ovs_scan_len(s
, &n
, "persistent")) {
1786 p
->persistent
= true;
1789 if (ovs_scan_len(s
, &n
, "hash")) {
1790 p
->proto_hash
= true;
1793 if (ovs_scan_len(s
, &n
, "random")) {
1794 p
->proto_random
= true;
1800 if (p
->snat
&& p
->dnat
) {
1803 if ((p
->addr_len
!= 0 &&
1804 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1805 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1806 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1809 if (p
->proto_hash
&& p
->proto_random
) {
1819 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1821 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1824 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1825 } else if (p
->dnat
) {
1826 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1830 if (p
->addr_len
!= 0) {
1831 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1833 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1834 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1838 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1839 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1840 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1843 if (p
->persistent
) {
1844 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1846 if (p
->proto_hash
) {
1847 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1849 if (p
->proto_random
) {
1850 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1854 nl_msg_end_nested(actions
, start
);
1858 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1862 if (ovs_scan(s
, "ct")) {
1863 const char *helper
= NULL
;
1864 size_t helper_len
= 0;
1865 bool commit
= false;
1866 bool force_commit
= false;
1871 } ct_mark
= { 0, 0 };
1876 struct ct_nat_params nat_params
;
1877 bool have_nat
= false;
1881 memset(&ct_label
, 0, sizeof(ct_label
));
1884 if (ovs_scan(s
, "(")) {
1887 end
= strchr(s
, ')');
1895 s
+= strspn(s
, delimiters
);
1896 if (ovs_scan(s
, "commit%n", &n
)) {
1901 if (ovs_scan(s
, "force_commit%n", &n
)) {
1902 force_commit
= true;
1906 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1910 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1913 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1916 ct_mark
.mask
= UINT32_MAX
;
1920 if (ovs_scan(s
, "label=%n", &n
)) {
1924 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1931 if (ovs_scan(s
, "helper=%n", &n
)) {
1933 helper_len
= strcspn(s
, delimiters_end
);
1934 if (!helper_len
|| helper_len
> 15) {
1942 n
= scan_ct_nat(s
, &nat_params
);
1947 /* end points to the end of the nested, nat action.
1948 * find the real end. */
1951 /* Nothing matched. */
1956 if (commit
&& force_commit
) {
1960 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1962 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1963 } else if (force_commit
) {
1964 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1967 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1970 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1973 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1974 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1978 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1982 nl_msg_put_ct_nat(&nat_params
, actions
);
1984 nl_msg_end_nested(actions
, start
);
1991 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
1992 uint8_t * metadata
, size_t md_size
,
1996 struct ovs_nsh_key_base base
;
1998 base
.flags
= nsh
->flags
;
1999 base
.ttl
= nsh
->ttl
;
2000 base
.mdtype
= nsh
->mdtype
;
2002 base
.path_hdr
= nsh
->path_hdr
;
2004 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
2005 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
2008 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2009 sizeof nsh
->context
);
2011 switch (nsh
->mdtype
) {
2013 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2014 sizeof nsh
->context
);
2017 if (metadata
&& md_size
> 0) {
2018 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2023 /* No match support for other MD formats yet. */
2027 nl_msg_end_nested(buf
, nsh_key_ofs
);
2032 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2039 struct ovs_key_nsh nsh
;
2040 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2041 uint8_t md_size
= 0;
2043 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2048 /* The default is NSH_M_TYPE1 */
2051 nsh
.mdtype
= NSH_M_TYPE1
;
2052 nsh
.np
= NSH_P_ETHERNET
;
2053 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2054 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2057 n
+= strspn(s
+ n
, delimiters
);
2062 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2065 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2068 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2069 switch (nsh
.mdtype
) {
2071 /* This is the default format. */;
2074 /* Length will be updated later. */
2083 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2086 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2089 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2092 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2093 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2094 nsh
.context
[0] = htonl(cd
);
2097 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2098 nsh
.context
[1] = htonl(cd
);
2101 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2102 nsh
.context
[2] = htonl(cd
);
2105 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2106 nsh
.context
[3] = htonl(cd
);
2110 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2113 size_t mdlen
, padding
;
2114 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)
2115 && n
/2 <= sizeof metadata
) {
2116 ofpbuf_use_stub(&b
, metadata
, sizeof metadata
);
2117 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2118 /* Pad metadata to 4 bytes. */
2119 padding
= PAD_SIZE(mdlen
, 4);
2121 ofpbuf_put_zeros(&b
, padding
);
2123 md_size
= mdlen
+ padding
;
2134 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2135 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2136 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2137 nl_msg_end_nested(actions
, offset
);
2144 parse_action_list(const char *s
, const struct simap
*port_names
,
2145 struct ofpbuf
*actions
)
2152 n
+= strspn(s
+ n
, delimiters
);
2156 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
2167 parse_odp_action(const char *s
, const struct simap
*port_names
,
2168 struct ofpbuf
*actions
)
2174 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2175 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2184 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2185 struct ovs_action_trunc
*trunc
;
2187 trunc
= nl_msg_put_unspec_uninit(actions
,
2188 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2189 trunc
->max_len
= max_len
;
2195 int len
= strcspn(s
, delimiters
);
2196 struct simap_node
*node
;
2198 node
= simap_find_len(port_names
, s
, len
);
2200 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2209 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2210 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2215 if (!strncmp(s
, "userspace(", 10)) {
2216 return parse_odp_userspace_action(s
, actions
);
2219 if (!strncmp(s
, "set(", 4)) {
2222 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2223 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2224 struct nlattr
*nested
, *key
;
2226 struct parse_odp_context context
= (struct parse_odp_context
) {
2227 .port_names
= port_names
,
2230 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2231 retval
= parse_odp_key_mask_attr(&context
, s
+ 4, actions
, &maskbuf
);
2233 ofpbuf_uninit(&maskbuf
);
2236 if (s
[retval
+ 4] != ')') {
2237 ofpbuf_uninit(&maskbuf
);
2241 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2244 size
= nl_attr_get_size(mask
);
2245 if (size
== nl_attr_get_size(key
)) {
2246 /* Change to masked set action if not fully masked. */
2247 if (!is_all_ones(mask
+ 1, size
)) {
2248 /* Remove padding of eariler key payload */
2249 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2251 /* Put mask payload right after key payload */
2252 key
->nla_len
+= size
;
2253 ofpbuf_put(actions
, mask
+ 1, size
);
2255 /* 'actions' may have been reallocated by ofpbuf_put(). */
2256 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2257 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2260 /* Add new padding as needed */
2261 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2265 ofpbuf_uninit(&maskbuf
);
2267 nl_msg_end_nested(actions
, start_ofs
);
2272 struct ovs_action_push_vlan push
;
2273 int tpid
= ETH_TYPE_VLAN
;
2278 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2279 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2280 &vid
, &pcp
, &cfi
, &n
)
2281 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2282 &tpid
, &vid
, &pcp
, &n
)
2283 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2284 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2285 if ((vid
& ~(VLAN_VID_MASK
>> VLAN_VID_SHIFT
)) != 0
2286 || (pcp
& ~(VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) != 0) {
2289 push
.vlan_tpid
= htons(tpid
);
2290 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2291 | (pcp
<< VLAN_PCP_SHIFT
)
2292 | (cfi
? VLAN_CFI
: 0));
2293 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2294 &push
, sizeof push
);
2300 if (!strncmp(s
, "pop_vlan", 8)) {
2301 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2306 unsigned long long int meter_id
;
2309 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2310 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2319 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2320 && percentage
>= 0. && percentage
<= 100.0) {
2321 size_t sample_ofs
, actions_ofs
;
2324 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2325 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2326 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2327 (probability
<= 0 ? 0
2328 : probability
>= UINT32_MAX
? UINT32_MAX
2331 actions_ofs
= nl_msg_start_nested(actions
,
2332 OVS_SAMPLE_ATTR_ACTIONS
);
2333 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2338 nl_msg_end_nested(actions
, actions_ofs
);
2339 nl_msg_end_nested(actions
, sample_ofs
);
2341 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2346 if (!strncmp(s
, "clone(", 6)) {
2350 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2351 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2356 nl_msg_end_nested(actions
, actions_ofs
);
2362 if (!strncmp(s
, "push_nsh(", 9)) {
2363 int retval
= parse_odp_push_nsh_action(s
, actions
);
2373 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2374 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2383 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2384 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2390 if (!strncmp(s
, "ct_clear", 8)) {
2391 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2399 retval
= parse_conntrack_action(s
, actions
);
2406 struct ovs_action_push_tnl data
;
2409 n
= ovs_parse_tnl_push(s
, &data
);
2411 odp_put_tnl_push_action(actions
, &data
);
2420 /* Parses the string representation of datapath actions, in the format output
2421 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2422 * value. On success, the ODP actions are appended to 'actions' as a series of
2423 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2424 * way, 'actions''s data might be reallocated. */
2426 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2427 struct ofpbuf
*actions
)
2431 if (!strcasecmp(s
, "drop")) {
2435 old_size
= actions
->size
;
2439 s
+= strspn(s
, delimiters
);
2444 retval
= parse_odp_action(s
, port_names
, actions
);
2445 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2446 actions
->size
= old_size
;
2455 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2456 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2459 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2460 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2461 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2462 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2463 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2464 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2465 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2466 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2467 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2468 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2469 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2470 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2471 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2472 .next
= ovs_vxlan_ext_attr_lens
,
2473 .next_max
= OVS_VXLAN_EXT_MAX
},
2474 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2475 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2476 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2479 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2480 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2481 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2482 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2483 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2484 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2485 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2486 .next
= ovs_tun_key_attr_lens
,
2487 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2488 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2489 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2490 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2491 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2492 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2493 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2494 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2495 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2496 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2497 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2498 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2499 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2500 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2501 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2502 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2503 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2504 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2505 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2506 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2507 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2508 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2509 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2510 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2511 .next
= ovs_nsh_key_attr_lens
,
2512 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2515 /* Returns the correct length of the payload for a flow key attribute of the
2516 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2517 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2518 * payload is a nested type. */
2520 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2522 if (type
> max_type
) {
2523 return ATTR_LEN_INVALID
;
2526 return tbl
[type
].len
;
2530 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2532 size_t len
= nl_attr_get_size(a
);
2534 const uint8_t *unspec
;
2537 unspec
= nl_attr_get(a
);
2538 for (i
= 0; i
< len
; i
++) {
2540 ds_put_char(ds
, ' ');
2542 ds_put_format(ds
, "%02x", unspec
[i
]);
2548 ovs_frag_type_to_string(enum ovs_frag_type type
)
2551 case OVS_FRAG_TYPE_NONE
:
2553 case OVS_FRAG_TYPE_FIRST
:
2555 case OVS_FRAG_TYPE_LATER
:
2557 case __OVS_FRAG_TYPE_MAX
:
2563 enum odp_key_fitness
2564 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2565 struct nsh_hdr
*nsh_hdr
, size_t size
)
2568 const struct nlattr
*a
;
2569 bool unknown
= false;
2573 bool has_md1
= false;
2574 bool has_md2
= false;
2576 memset(nsh_hdr
, 0, size
);
2578 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2579 uint16_t type
= nl_attr_type(a
);
2580 size_t len
= nl_attr_get_size(a
);
2581 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2582 OVS_NSH_KEY_ATTR_MAX
, type
);
2584 if (len
!= expected_len
&& expected_len
>= 0) {
2585 return ODP_FIT_ERROR
;
2589 case OVS_NSH_KEY_ATTR_BASE
: {
2590 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2591 nsh_hdr
->next_proto
= base
->np
;
2592 nsh_hdr
->md_type
= base
->mdtype
;
2593 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2594 flags
= base
->flags
;
2598 case OVS_NSH_KEY_ATTR_MD1
: {
2599 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2600 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2602 mdlen
= nl_attr_get_size(a
);
2603 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2604 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2605 return ODP_FIT_ERROR
;
2607 memcpy(md1_dst
, md1
, mdlen
);
2610 case OVS_NSH_KEY_ATTR_MD2
: {
2611 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2612 const uint8_t *md2
= nl_attr_get(a
);
2614 mdlen
= nl_attr_get_size(a
);
2615 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2616 return ODP_FIT_ERROR
;
2618 memcpy(md2_dst
, md2
, mdlen
);
2622 /* Allow this to show up as unexpected, if there are unknown
2623 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2630 return ODP_FIT_TOO_MUCH
;
2633 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2634 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2635 return ODP_FIT_ERROR
;
2638 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2639 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2641 return ODP_FIT_PERFECT
;
2644 enum odp_key_fitness
2645 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2646 struct ovs_key_nsh
*nsh_mask
)
2649 const struct nlattr
*a
;
2650 bool unknown
= false;
2651 bool has_md1
= false;
2653 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2654 uint16_t type
= nl_attr_type(a
);
2655 size_t len
= nl_attr_get_size(a
);
2656 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2657 OVS_NSH_KEY_ATTR_MAX
, type
);
2659 /* the attribute can have mask, len is 2 * expected_len for that case.
2661 if ((len
!= expected_len
) && (len
!= 2 * expected_len
) &&
2662 (expected_len
>= 0)) {
2663 return ODP_FIT_ERROR
;
2666 if ((nsh_mask
&& (expected_len
>= 0) && (len
!= 2 * expected_len
)) ||
2667 (!nsh_mask
&& (expected_len
>= 0) && (len
== 2 * expected_len
))) {
2668 return ODP_FIT_ERROR
;
2672 case OVS_NSH_KEY_ATTR_UNSPEC
:
2674 case OVS_NSH_KEY_ATTR_BASE
: {
2675 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2676 nsh
->flags
= base
->flags
;
2677 nsh
->ttl
= base
->ttl
;
2678 nsh
->mdtype
= base
->mdtype
;
2680 nsh
->path_hdr
= base
->path_hdr
;
2681 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2682 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2683 nsh_mask
->flags
= base_mask
->flags
;
2684 nsh_mask
->ttl
= base_mask
->ttl
;
2685 nsh_mask
->mdtype
= base_mask
->mdtype
;
2686 nsh_mask
->np
= base_mask
->np
;
2687 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2691 case OVS_NSH_KEY_ATTR_MD1
: {
2692 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2694 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2695 if (len
== 2 * sizeof(*md1
)) {
2696 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2697 memcpy(nsh_mask
->context
, md1_mask
->context
,
2702 case OVS_NSH_KEY_ATTR_MD2
:
2704 /* Allow this to show up as unexpected, if there are unknown
2705 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2712 return ODP_FIT_TOO_MUCH
;
2715 if (has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
&& !nsh_mask
) {
2716 return ODP_FIT_ERROR
;
2719 return ODP_FIT_PERFECT
;
2722 static enum odp_key_fitness
2723 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2724 struct flow_tnl
*tun
)
2727 const struct nlattr
*a
;
2729 bool unknown
= false;
2731 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2732 uint16_t type
= nl_attr_type(a
);
2733 size_t len
= nl_attr_get_size(a
);
2734 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2735 OVS_TUNNEL_ATTR_MAX
, type
);
2737 if (len
!= expected_len
&& expected_len
>= 0) {
2738 return ODP_FIT_ERROR
;
2742 case OVS_TUNNEL_KEY_ATTR_ID
:
2743 tun
->tun_id
= nl_attr_get_be64(a
);
2744 tun
->flags
|= FLOW_TNL_F_KEY
;
2746 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2747 tun
->ip_src
= nl_attr_get_be32(a
);
2749 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2750 tun
->ip_dst
= nl_attr_get_be32(a
);
2752 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2753 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2755 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2756 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2758 case OVS_TUNNEL_KEY_ATTR_TOS
:
2759 tun
->ip_tos
= nl_attr_get_u8(a
);
2761 case OVS_TUNNEL_KEY_ATTR_TTL
:
2762 tun
->ip_ttl
= nl_attr_get_u8(a
);
2765 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2766 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2768 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2769 tun
->flags
|= FLOW_TNL_F_CSUM
;
2771 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2772 tun
->tp_src
= nl_attr_get_be16(a
);
2774 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2775 tun
->tp_dst
= nl_attr_get_be16(a
);
2777 case OVS_TUNNEL_KEY_ATTR_OAM
:
2778 tun
->flags
|= FLOW_TNL_F_OAM
;
2780 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2781 static const struct nl_policy vxlan_opts_policy
[] = {
2782 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2784 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2786 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2787 return ODP_FIT_ERROR
;
2790 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2791 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2793 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2794 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2799 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2800 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2802 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
2803 const struct erspan_metadata
*opts
= nl_attr_get(a
);
2805 tun
->erspan_ver
= opts
->version
;
2806 if (tun
->erspan_ver
== 1) {
2807 tun
->erspan_idx
= ntohl(opts
->u
.index
);
2808 } else if (tun
->erspan_ver
== 2) {
2809 tun
->erspan_dir
= opts
->u
.md2
.dir
;
2810 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
2812 VLOG_WARN("%s invalid erspan version\n", __func__
);
2818 /* Allow this to show up as unexpected, if there are unknown
2819 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2826 return ODP_FIT_ERROR
;
2829 return ODP_FIT_TOO_MUCH
;
2831 return ODP_FIT_PERFECT
;
2834 enum odp_key_fitness
2835 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2837 memset(tun
, 0, sizeof *tun
);
2838 return odp_tun_key_from_attr__(attr
, false, tun
);
2842 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2843 const struct flow_tnl
*tun_flow_key
,
2844 const struct ofpbuf
*key_buf
, const char *tnl_type
)
2848 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2850 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2851 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2852 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2854 if (tun_key
->ip_src
) {
2855 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2857 if (tun_key
->ip_dst
) {
2858 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2860 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2861 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2863 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2864 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2866 if (tun_key
->ip_tos
) {
2867 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2869 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2870 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2871 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2873 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2874 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2876 if (tun_key
->tp_src
) {
2877 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2879 if (tun_key
->tp_dst
) {
2880 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2882 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2883 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2886 /* If tnl_type is set to a particular type of output tunnel,
2887 * only put its relevant tunnel metadata to the nlattr.
2888 * If tnl_type is NULL, put tunnel metadata according to the
2891 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
2892 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
2893 size_t vxlan_opts_ofs
;
2895 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2896 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2897 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2898 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2901 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
2902 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2905 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
2906 !strcmp(tnl_type
, "ip6erspan")) &&
2907 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
2908 struct erspan_metadata opts
;
2910 opts
.version
= tun_key
->erspan_ver
;
2911 if (opts
.version
== 1) {
2912 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
2914 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
2915 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
2917 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
2918 &opts
, sizeof(opts
));
2921 nl_msg_end_nested(a
, tun_key_ofs
);
2925 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2928 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2929 * that -1 becomes all-1-bits and 0 does not change. */
2930 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2931 uint32_t u32
= constant
;
2932 uint8_t u8
= constant
;
2933 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
2936 case OVS_KEY_ATTR_UNSPEC
:
2937 case OVS_KEY_ATTR_ENCAP
:
2938 case __OVS_KEY_ATTR_MAX
:
2942 case OVS_KEY_ATTR_PRIORITY
:
2943 case OVS_KEY_ATTR_IN_PORT
:
2944 case OVS_KEY_ATTR_ETHERNET
:
2945 case OVS_KEY_ATTR_VLAN
:
2946 case OVS_KEY_ATTR_ETHERTYPE
:
2947 case OVS_KEY_ATTR_IPV4
:
2948 case OVS_KEY_ATTR_TCP
:
2949 case OVS_KEY_ATTR_UDP
:
2950 case OVS_KEY_ATTR_ICMP
:
2951 case OVS_KEY_ATTR_ICMPV6
:
2952 case OVS_KEY_ATTR_ND
:
2953 case OVS_KEY_ATTR_SKB_MARK
:
2954 case OVS_KEY_ATTR_TUNNEL
:
2955 case OVS_KEY_ATTR_SCTP
:
2956 case OVS_KEY_ATTR_DP_HASH
:
2957 case OVS_KEY_ATTR_RECIRC_ID
:
2958 case OVS_KEY_ATTR_MPLS
:
2959 case OVS_KEY_ATTR_CT_STATE
:
2960 case OVS_KEY_ATTR_CT_ZONE
:
2961 case OVS_KEY_ATTR_CT_MARK
:
2962 case OVS_KEY_ATTR_CT_LABELS
:
2963 case OVS_KEY_ATTR_PACKET_TYPE
:
2964 case OVS_KEY_ATTR_NSH
:
2965 return is_all_byte(mask
, size
, u8
);
2967 case OVS_KEY_ATTR_TCP_FLAGS
:
2968 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
2970 case OVS_KEY_ATTR_IPV6
: {
2971 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2972 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2973 == htonl(IPV6_LABEL_MASK
& u32
)
2974 && ipv6_mask
->ipv6_proto
== u8
2975 && ipv6_mask
->ipv6_tclass
== u8
2976 && ipv6_mask
->ipv6_hlimit
== u8
2977 && ipv6_mask
->ipv6_frag
== u8
2978 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
2979 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
2982 case OVS_KEY_ATTR_ARP
:
2983 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
2985 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
2986 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
2989 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
2990 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
2995 /* The caller must already have verified that 'ma' has a correct length.
2997 * The main purpose of this function is formatting, to allow code to figure out
2998 * whether the mask can be omitted. It doesn't try hard for attributes that
2999 * contain sub-attributes, etc., because normally those would be broken down
3000 * further for formatting. */
3002 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
3004 return odp_mask_is_constant__(nl_attr_type(ma
),
3005 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
3008 /* The caller must already have verified that 'size' is a correct length for
3011 * The main purpose of this function is formatting, to allow code to figure out
3012 * whether the mask can be omitted. It doesn't try hard for attributes that
3013 * contain sub-attributes, etc., because normally those would be broken down
3014 * further for formatting. */
3016 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
3018 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3021 /* The caller must already have verified that 'ma' has a correct length. */
3023 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3025 enum ovs_key_attr attr
= nl_attr_type(ma
);
3026 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3030 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3033 struct odp_portno_names
*odp_portno_names
;
3035 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3036 odp_portno_names
->port_no
= port_no
;
3037 odp_portno_names
->name
= xstrdup(port_name
);
3038 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3039 hash_odp_port(port_no
));
3043 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3046 struct odp_portno_names
*odp_portno_names
;
3048 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3049 hash_odp_port(port_no
), portno_names
) {
3050 if (odp_portno_names
->port_no
== port_no
) {
3051 return odp_portno_names
->name
;
3059 odp_portno_names_destroy(struct hmap
*portno_names
)
3061 struct odp_portno_names
*odp_portno_names
;
3063 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3064 free(odp_portno_names
->name
);
3065 free(odp_portno_names
);
3070 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3073 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3075 ds_put_cstr(s
, name
);
3077 ds_put_format(s
, "%"PRIu32
, port_no
);
3081 /* Format helpers. */
3084 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3085 const struct eth_addr
*mask
, bool verbose
)
3087 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3089 if (verbose
|| !mask_empty
) {
3090 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3093 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3095 ds_put_format(ds
, "%s=", name
);
3096 eth_format_masked(key
, mask
, ds
);
3097 ds_put_char(ds
, ',');
3104 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3105 const ovs_be64
*mask
, bool verbose
)
3107 bool mask_empty
= mask
&& !*mask
;
3109 if (verbose
|| !mask_empty
) {
3110 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3112 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3113 if (!mask_full
) { /* Partially masked. */
3114 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3116 ds_put_char(ds
, ',');
3121 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3122 const ovs_be32
*mask
, bool verbose
)
3124 bool mask_empty
= mask
&& !*mask
;
3126 if (verbose
|| !mask_empty
) {
3127 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3129 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3130 if (!mask_full
) { /* Partially masked. */
3131 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3133 ds_put_char(ds
, ',');
3138 format_in6_addr(struct ds
*ds
, const char *name
,
3139 const struct in6_addr
*key
,
3140 const struct in6_addr
*mask
,
3143 char buf
[INET6_ADDRSTRLEN
];
3144 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3146 if (verbose
|| !mask_empty
) {
3147 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3149 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3150 ds_put_format(ds
, "%s=%s", name
, buf
);
3151 if (!mask_full
) { /* Partially masked. */
3152 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3153 ds_put_format(ds
, "/%s", buf
);
3155 ds_put_char(ds
, ',');
3160 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3161 const ovs_be32
*mask
, bool verbose
)
3163 bool mask_empty
= mask
&& !*mask
;
3165 if (verbose
|| !mask_empty
) {
3166 bool mask_full
= !mask
3167 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3169 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3170 if (!mask_full
) { /* Partially masked. */
3171 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3173 ds_put_char(ds
, ',');
3178 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3179 const uint8_t *mask
, bool verbose
)
3181 bool mask_empty
= mask
&& !*mask
;
3183 if (verbose
|| !mask_empty
) {
3184 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3186 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3187 if (!mask_full
) { /* Partially masked. */
3188 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3190 ds_put_char(ds
, ',');
3195 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3196 const uint8_t *mask
, bool verbose
)
3198 bool mask_empty
= mask
&& !*mask
;
3200 if (verbose
|| !mask_empty
) {
3201 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3203 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3204 if (!mask_full
) { /* Partially masked. */
3205 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3207 ds_put_char(ds
, ',');
3212 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3213 const ovs_be16
*mask
, bool verbose
)
3215 bool mask_empty
= mask
&& !*mask
;
3217 if (verbose
|| !mask_empty
) {
3218 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3220 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3221 if (!mask_full
) { /* Partially masked. */
3222 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3224 ds_put_char(ds
, ',');
3229 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3230 const ovs_be16
*mask
, bool verbose
)
3232 bool mask_empty
= mask
&& !*mask
;
3234 if (verbose
|| !mask_empty
) {
3235 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3237 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3238 if (!mask_full
) { /* Partially masked. */
3239 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3241 ds_put_char(ds
, ',');
3246 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3247 const uint16_t *mask
, bool verbose
)
3249 bool mask_empty
= mask
&& !*mask
;
3251 if (verbose
|| !mask_empty
) {
3252 ds_put_cstr(ds
, name
);
3253 ds_put_char(ds
, '(');
3255 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3256 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3257 } else { /* Fully masked. */
3258 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3260 ds_put_cstr(ds
, "),");
3265 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3266 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3270 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3271 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3272 expected_len
!= ATTR_LEN_NESTED
) {
3274 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3275 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3277 if (bad_key_len
|| bad_mask_len
) {
3279 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3282 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3283 nl_attr_get_size(a
), expected_len
);
3285 format_generic_odp_key(a
, ds
);
3287 ds_put_char(ds
, '/');
3289 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3290 nl_attr_get_size(ma
), expected_len
);
3292 format_generic_odp_key(ma
, ds
);
3294 ds_put_char(ds
, ')');
3303 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3304 const struct nlattr
*ma
)
3306 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3307 format_generic_odp_key(a
, ds
);
3308 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3309 ds_put_char(ds
, '/');
3310 format_generic_odp_key(ma
, ds
);
3312 ds_put_cstr(ds
, "),");
3316 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3317 const struct nlattr
*mask_attr
, struct ds
*ds
,
3321 const struct nlattr
*a
;
3324 ofpbuf_init(&ofp
, 100);
3325 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3326 uint16_t type
= nl_attr_type(a
);
3327 const struct nlattr
*ma
= NULL
;
3330 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3331 nl_attr_get_size(mask_attr
), type
);
3333 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3339 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3340 OVS_VXLAN_EXT_MAX
, true)) {
3345 case OVS_VXLAN_EXT_GBP
: {
3346 uint32_t key
= nl_attr_get_u32(a
);
3347 ovs_be16 id
, id_mask
;
3348 uint8_t flags
, flags_mask
= 0;
3350 id
= htons(key
& 0xFFFF);
3351 flags
= (key
>> 16) & 0xFF;
3353 uint32_t mask
= nl_attr_get_u32(ma
);
3354 id_mask
= htons(mask
& 0xFFFF);
3355 flags_mask
= (mask
>> 16) & 0xFF;
3358 ds_put_cstr(ds
, "gbp(");
3359 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3360 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3362 ds_put_cstr(ds
, "),");
3367 format_unknown_key(ds
, a
, ma
);
3373 ofpbuf_uninit(&ofp
);
3377 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3378 const struct nlattr
*mask_attr
, struct ds
*ds
,
3381 const struct erspan_metadata
*opts
, *mask
;
3382 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3384 opts
= nl_attr_get(attr
);
3385 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3387 ver
= (uint8_t)opts
->version
;
3389 ver_ma
= (uint8_t)mask
->version
;
3392 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3394 if (opts
->version
== 1) {
3396 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3397 ntohl(opts
->u
.index
),
3398 ntohl(mask
->u
.index
));
3400 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3402 } else if (opts
->version
== 2) {
3403 dir
= opts
->u
.md2
.dir
;
3404 hwid
= opts
->u
.md2
.hwid
;
3406 dir_ma
= mask
->u
.md2
.dir
;
3407 hwid_ma
= mask
->u
.md2
.hwid
;
3410 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3411 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3416 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3419 format_geneve_opts(const struct geneve_opt
*opt
,
3420 const struct geneve_opt
*mask
, int opts_len
,
3421 struct ds
*ds
, bool verbose
)
3423 while (opts_len
> 0) {
3425 uint8_t data_len
, data_len_mask
;
3427 if (opts_len
< sizeof *opt
) {
3428 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3429 opts_len
, sizeof *opt
);
3433 data_len
= opt
->length
* 4;
3435 if (mask
->length
== 0x1f) {
3436 data_len_mask
= UINT8_MAX
;
3438 data_len_mask
= mask
->length
;
3441 len
= sizeof *opt
+ data_len
;
3442 if (len
> opts_len
) {
3443 ds_put_format(ds
, "opt len %u greater than remaining %u",
3448 ds_put_char(ds
, '{');
3449 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3451 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3452 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3454 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3455 ds_put_hex(ds
, opt
+ 1, data_len
);
3456 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3457 ds_put_char(ds
, '/');
3458 ds_put_hex(ds
, mask
+ 1, data_len
);
3463 ds_put_char(ds
, '}');
3465 opt
+= len
/ sizeof(*opt
);
3467 mask
+= len
/ sizeof(*opt
);
3474 format_odp_tun_geneve(const struct nlattr
*attr
,
3475 const struct nlattr
*mask_attr
, struct ds
*ds
,
3478 int opts_len
= nl_attr_get_size(attr
);
3479 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3480 const struct geneve_opt
*mask
= mask_attr
?
3481 nl_attr_get(mask_attr
) : NULL
;
3483 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3484 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3485 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3489 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3493 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3497 const struct nlattr
*a
;
3498 struct ovs_key_nsh nsh
;
3499 struct ovs_key_nsh nsh_mask
;
3501 memset(&nsh
, 0, sizeof nsh
);
3502 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3504 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3505 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3506 const struct nlattr
*ma
= NULL
;
3509 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3510 nl_attr_get_size(mask_attr
), type
);
3513 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3514 OVS_NSH_KEY_ATTR_MAX
, true)) {
3519 case OVS_NSH_KEY_ATTR_UNSPEC
:
3521 case OVS_NSH_KEY_ATTR_BASE
: {
3522 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3523 const struct ovs_nsh_key_base
*base_mask
3524 = ma
? nl_attr_get(ma
) : NULL
;
3525 nsh
.flags
= base
->flags
;
3526 nsh
.ttl
= base
->ttl
;
3527 nsh
.mdtype
= base
->mdtype
;
3529 nsh
.path_hdr
= base
->path_hdr
;
3531 nsh_mask
.flags
= base_mask
->flags
;
3532 nsh_mask
.ttl
= base_mask
->ttl
;
3533 nsh_mask
.mdtype
= base_mask
->mdtype
;
3534 nsh_mask
.np
= base_mask
->np
;
3535 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3539 case OVS_NSH_KEY_ATTR_MD1
: {
3540 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3541 const struct ovs_nsh_key_md1
*md1_mask
3542 = ma
? nl_attr_get(ma
) : NULL
;
3543 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3545 memcpy(nsh_mask
.context
, md1_mask
->context
,
3546 sizeof md1_mask
->context
);
3550 case OVS_NSH_KEY_ATTR_MD2
:
3551 case __OVS_NSH_KEY_ATTR_MAX
:
3553 /* No support for matching other metadata formats yet. */
3559 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3561 format_nsh_key(ds
, &nsh
);
3566 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3567 struct ds
*ds
, bool verbose
)
3570 const struct nlattr
*a
;
3572 uint16_t mask_flags
= 0;
3575 ofpbuf_init(&ofp
, 100);
3576 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3577 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3578 const struct nlattr
*ma
= NULL
;
3581 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3582 nl_attr_get_size(mask_attr
), type
);
3584 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3585 OVS_TUNNEL_KEY_ATTR_MAX
,
3590 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3591 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3596 case OVS_TUNNEL_KEY_ATTR_ID
:
3597 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3598 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3599 flags
|= FLOW_TNL_F_KEY
;
3601 mask_flags
|= FLOW_TNL_F_KEY
;
3604 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3605 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3606 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3608 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3609 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3610 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3612 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3613 struct in6_addr ipv6_src
;
3614 ipv6_src
= nl_attr_get_in6_addr(a
);
3615 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3616 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3619 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3620 struct in6_addr ipv6_dst
;
3621 ipv6_dst
= nl_attr_get_in6_addr(a
);
3622 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3623 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3626 case OVS_TUNNEL_KEY_ATTR_TOS
:
3627 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3628 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3630 case OVS_TUNNEL_KEY_ATTR_TTL
:
3631 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3632 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3634 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3635 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3637 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3638 flags
|= FLOW_TNL_F_CSUM
;
3640 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3641 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3642 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3644 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3645 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3646 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3648 case OVS_TUNNEL_KEY_ATTR_OAM
:
3649 flags
|= FLOW_TNL_F_OAM
;
3651 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3652 ds_put_cstr(ds
, "vxlan(");
3653 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3654 ds_put_cstr(ds
, "),");
3656 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3657 ds_put_cstr(ds
, "geneve(");
3658 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3659 ds_put_cstr(ds
, "),");
3661 case OVS_TUNNEL_KEY_ATTR_PAD
:
3663 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3664 ds_put_cstr(ds
, "erspan(");
3665 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3666 ds_put_cstr(ds
, "),");
3668 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3670 format_unknown_key(ds
, a
, ma
);
3675 /* Flags can have a valid mask even if the attribute is not set, so
3676 * we need to collect these separately. */
3678 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3679 switch (nl_attr_type(a
)) {
3680 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3681 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3683 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3684 mask_flags
|= FLOW_TNL_F_CSUM
;
3686 case OVS_TUNNEL_KEY_ATTR_OAM
:
3687 mask_flags
|= FLOW_TNL_F_OAM
;
3693 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3696 ofpbuf_uninit(&ofp
);
3700 odp_ct_state_to_string(uint32_t flag
)
3703 case OVS_CS_F_REPLY_DIR
:
3705 case OVS_CS_F_TRACKED
:
3709 case OVS_CS_F_ESTABLISHED
:
3711 case OVS_CS_F_RELATED
:
3713 case OVS_CS_F_INVALID
:
3715 case OVS_CS_F_SRC_NAT
:
3717 case OVS_CS_F_DST_NAT
:
3725 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
3726 const uint8_t *mask
, bool verbose OVS_UNUSED
)
3728 bool mask_empty
= mask
&& !*mask
;
3729 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3731 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3732 if (!mask_empty
&& !mask_full
) {
3733 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
3735 } else if (!mask_empty
) {
3736 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
3741 mask_empty(const struct nlattr
*ma
)
3749 mask
= nl_attr_get(ma
);
3750 n
= nl_attr_get_size(ma
);
3752 return is_all_zeros(mask
, n
);
3755 /* The caller must have already verified that 'a' and 'ma' have correct
3758 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
3759 const struct hmap
*portno_names
, struct ds
*ds
,
3762 enum ovs_key_attr attr
= nl_attr_type(a
);
3763 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3766 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
3768 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
3770 ds_put_char(ds
, '(');
3772 case OVS_KEY_ATTR_ENCAP
:
3773 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
3774 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
3775 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
3777 } else if (nl_attr_get_size(a
)) {
3778 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
3783 case OVS_KEY_ATTR_PRIORITY
:
3784 case OVS_KEY_ATTR_SKB_MARK
:
3785 case OVS_KEY_ATTR_DP_HASH
:
3786 case OVS_KEY_ATTR_RECIRC_ID
:
3787 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3789 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3793 case OVS_KEY_ATTR_CT_MARK
:
3794 if (verbose
|| !mask_empty(ma
)) {
3795 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3797 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3802 case OVS_KEY_ATTR_CT_STATE
:
3804 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3806 ds_put_format(ds
, "/%#"PRIx32
,
3807 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
3809 } else if (!is_exact
) {
3810 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
3812 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
3815 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
3819 case OVS_KEY_ATTR_CT_ZONE
:
3820 if (verbose
|| !mask_empty(ma
)) {
3821 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
3823 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
3828 case OVS_KEY_ATTR_CT_LABELS
: {
3829 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
3830 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3832 format_u128(ds
, value
, mask
, verbose
);
3836 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
3837 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
3838 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3840 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3841 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3842 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3844 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
3846 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
3852 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
3853 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
3854 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3856 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3858 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3860 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3862 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3864 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3870 case OVS_KEY_ATTR_TUNNEL
:
3871 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3874 case OVS_KEY_ATTR_IN_PORT
:
3876 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3878 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3880 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3885 case OVS_KEY_ATTR_PACKET_TYPE
: {
3886 ovs_be32 value
= nl_attr_get_be32(a
);
3887 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3889 ovs_be16 ns
= htons(pt_ns(value
));
3890 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3891 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3893 ovs_be16 ns_type
= pt_ns_type_be(value
);
3894 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3895 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3901 case OVS_KEY_ATTR_ETHERNET
: {
3902 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3903 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3905 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3906 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3910 case OVS_KEY_ATTR_VLAN
:
3911 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3912 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3915 case OVS_KEY_ATTR_MPLS
: {
3916 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3917 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3918 size_t size
= nl_attr_get_size(a
);
3920 if (!size
|| size
% sizeof *mpls_key
) {
3921 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3925 mpls_mask
= nl_attr_get(ma
);
3926 if (size
!= nl_attr_get_size(ma
)) {
3927 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3928 "mask length %"PRIuSIZE
")",
3929 size
, nl_attr_get_size(ma
));
3933 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
3936 case OVS_KEY_ATTR_ETHERTYPE
:
3937 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
3939 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
3943 case OVS_KEY_ATTR_IPV4
: {
3944 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
3945 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3947 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3948 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3949 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3951 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
3952 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
3953 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
3958 case OVS_KEY_ATTR_IPV6
: {
3959 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
3960 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3962 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3964 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3966 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
3968 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3970 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
3972 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
3974 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
3979 /* These have the same structure and format. */
3980 case OVS_KEY_ATTR_TCP
:
3981 case OVS_KEY_ATTR_UDP
:
3982 case OVS_KEY_ATTR_SCTP
: {
3983 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
3984 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3986 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
3987 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
3991 case OVS_KEY_ATTR_TCP_FLAGS
:
3993 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
3994 ntohs(nl_attr_get_be16(a
)),
3995 TCP_FLAGS(nl_attr_get_be16(ma
)),
3996 TCP_FLAGS(OVS_BE16_MAX
));
3998 format_flags(ds
, packet_tcp_flag_to_string
,
3999 ntohs(nl_attr_get_be16(a
)), '|');
4003 case OVS_KEY_ATTR_ICMP
: {
4004 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
4005 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4007 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
4008 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
4012 case OVS_KEY_ATTR_ICMPV6
: {
4013 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
4014 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4016 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
4018 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4023 case OVS_KEY_ATTR_ARP
: {
4024 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4025 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4027 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4028 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4029 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4030 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4031 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4035 case OVS_KEY_ATTR_ND
: {
4036 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4037 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4039 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4041 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4042 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4047 case OVS_KEY_ATTR_NSH
: {
4048 format_odp_nsh_attr(a
, ma
, ds
);
4051 case OVS_KEY_ATTR_UNSPEC
:
4052 case __OVS_KEY_ATTR_MAX
:
4054 format_generic_odp_key(a
, ds
);
4056 ds_put_char(ds
, '/');
4057 format_generic_odp_key(ma
, ds
);
4061 ds_put_char(ds
, ')');
4065 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4066 const struct hmap
*portno_names
, struct ds
*ds
,
4069 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4070 OVS_KEY_ATTR_MAX
, false)) {
4071 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4075 static struct nlattr
*
4076 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4077 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4079 const struct nlattr
*a
;
4081 int type
= nl_attr_type(key
);
4082 int size
= nl_attr_get_size(key
);
4084 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4085 nl_msg_put_unspec_zero(ofp
, type
, size
);
4089 if (tbl
[type
].next
) {
4090 const struct attr_len_tbl
*entry
= &tbl
[type
];
4092 max
= entry
->next_max
;
4095 nested_mask
= nl_msg_start_nested(ofp
, type
);
4096 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4097 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4099 nl_msg_end_nested(ofp
, nested_mask
);
4106 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4107 const ovs_32aligned_u128
*mask
, bool verbose
)
4109 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4110 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4111 ds_put_hex(ds
, &value
, sizeof value
);
4112 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4113 value
= hton128(get_32aligned_u128(mask
));
4114 ds_put_char(ds
, '/');
4115 ds_put_hex(ds
, &value
, sizeof value
);
4120 /* Read the string from 's_' as a 128-bit value. If the string contains
4121 * a "/", the rest of the string will be treated as a 128-bit mask.
4123 * If either the value or mask is larger than 64 bits, the string must
4124 * be in hexadecimal.
4127 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4129 char *s
= CONST_CAST(char *, s_
);
4133 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4134 *value
= ntoh128(be_value
);
4139 if (ovs_scan(s
, "/%n", &n
)) {
4143 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4144 sizeof be_mask
, &s
);
4148 *mask
= ntoh128(be_mask
);
4150 *mask
= OVS_U128_MAX
;
4160 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4164 if (ovs_scan(s
, "ufid:")) {
4167 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4179 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4181 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4184 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4185 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4186 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4187 * non-null, translates odp port number to its name. */
4189 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4190 const struct nlattr
*mask
, size_t mask_len
,
4191 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4194 const struct nlattr
*a
;
4196 bool has_ethtype_key
= false;
4197 bool has_packet_type_key
= false;
4199 bool first_field
= true;
4201 ofpbuf_init(&ofp
, 100);
4202 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4203 int attr_type
= nl_attr_type(a
);
4204 const struct nlattr
*ma
= (mask
&& mask_len
4205 ? nl_attr_find__(mask
, mask_len
,
4208 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4209 OVS_KEY_ATTR_MAX
, false)) {
4213 bool is_nested_attr
;
4214 bool is_wildcard
= false;
4216 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4217 has_ethtype_key
= true;
4218 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4219 has_packet_type_key
= true;
4222 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4223 OVS_KEY_ATTR_MAX
, attr_type
) ==
4226 if (mask
&& mask_len
) {
4227 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4228 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4231 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4232 if (is_wildcard
&& !ma
) {
4233 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4238 ds_put_char(ds
, ',');
4240 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4241 first_field
= false;
4242 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4243 && !has_packet_type_key
) {
4244 /* This special case reflects differences between the kernel
4245 * and userspace datapaths regarding the root type of the
4246 * packet being matched (typically Ethernet but some tunnels
4247 * can encapsulate IPv4 etc.). The kernel datapath does not
4248 * have an explicit way to indicate packet type; instead:
4250 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4251 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4252 * Ethertype encoded in the Ethernet header.
4254 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4255 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4256 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4257 * packet is an IPv4 packet).
4259 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4260 * all-wildcarded, it is important to print it.
4262 * On the other hand, the userspace datapath supports
4263 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4264 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4265 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4267 ds_put_char(ds
, ',');
4269 ds_put_cstr(ds
, "eth()");
4273 ofpbuf_uninit(&ofp
);
4278 if (left
== key_len
) {
4279 ds_put_cstr(ds
, "<empty>");
4281 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4282 for (i
= 0; i
< left
; i
++) {
4283 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4285 ds_put_char(ds
, ')');
4287 if (!has_ethtype_key
) {
4288 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4289 OVS_KEY_ATTR_ETHERTYPE
);
4291 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4292 ntohs(nl_attr_get_be16(ma
)));
4296 ds_put_cstr(ds
, "<empty>");
4300 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4301 * OVS_KEY_ATTR_* attributes in 'key'. */
4303 odp_flow_key_format(const struct nlattr
*key
,
4304 size_t key_len
, struct ds
*ds
)
4306 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4310 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4312 if (!strcasecmp(s
, "no")) {
4313 *type
= OVS_FRAG_TYPE_NONE
;
4314 } else if (!strcasecmp(s
, "first")) {
4315 *type
= OVS_FRAG_TYPE_FIRST
;
4316 } else if (!strcasecmp(s
, "later")) {
4317 *type
= OVS_FRAG_TYPE_LATER
;
4327 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4331 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4332 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4336 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4337 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4340 memset(mask
, 0xff, sizeof *mask
);
4349 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4353 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4357 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4358 IP_SCAN_ARGS(mask
), &n
)) {
4361 *mask
= OVS_BE32_MAX
;
4370 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4373 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4375 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4376 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4380 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4381 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4384 memset(mask
, 0xff, sizeof *mask
);
4393 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4398 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4399 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4404 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4405 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4407 *mask
= htonl(mask_
);
4409 *mask
= htonl(IPV6_LABEL_MASK
);
4418 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4422 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4426 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4438 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4442 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4446 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4458 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4462 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4466 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4478 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4480 uint16_t key_
, mask_
;
4483 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4488 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4490 *mask
= htons(mask_
);
4492 *mask
= OVS_BE16_MAX
;
4501 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4503 uint32_t key_
, mask_
;
4506 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4511 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4513 *mask
= htonl(mask_
);
4515 *mask
= OVS_BE32_MAX
;
4524 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4526 uint64_t key_
, mask_
;
4529 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4532 *key
= htonll(key_
);
4534 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4536 *mask
= htonll(mask_
);
4538 *mask
= OVS_BE64_MAX
;
4547 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4549 uint32_t flags
, fmask
;
4552 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4553 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4554 if (n
>= 0 && s
[n
] == ')') {
4565 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4567 uint32_t flags
, fmask
;
4570 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4571 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4573 *key
= htons(flags
);
4575 *mask
= htons(fmask
);
4583 ovs_to_odp_ct_state(uint8_t state
)
4587 #define CS_STATE(ENUM, INDEX, NAME) \
4588 if (state & CS_##ENUM) { \
4589 odp |= OVS_CS_F_##ENUM; \
4598 odp_to_ovs_ct_state(uint32_t flags
)
4602 #define CS_STATE(ENUM, INDEX, NAME) \
4603 if (flags & OVS_CS_F_##ENUM) { \
4604 state |= CS_##ENUM; \
4613 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4615 uint32_t flags
, fmask
;
4618 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4619 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4620 mask
? &fmask
: NULL
);
4633 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4637 enum ovs_frag_type frag_type
;
4639 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4640 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4653 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4654 const struct simap
*port_names
)
4658 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4662 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4669 } else if (port_names
) {
4670 const struct simap_node
*node
;
4673 len
= strcspn(s
, ")");
4674 node
= simap_find_len(port_names
, s
, len
);
4687 /* Helper for vlan parsing. */
4688 struct ovs_key_vlan__
{
4693 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
4695 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
4697 if (value
>> bits
) {
4701 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
4706 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
4709 uint16_t key_
, mask_
;
4712 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4715 if (set_be16_bf(key
, bits
, offset
, key_
)) {
4717 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4720 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
4724 *mask
|= htons(((1U << bits
) - 1) << offset
);
4734 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4736 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
4740 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4742 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
4746 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4748 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
4753 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
4755 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
4757 if (value
>> bits
) {
4761 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
4766 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
4769 uint32_t key_
, mask_
;
4772 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4775 if (set_be32_bf(key
, bits
, offset
, key_
)) {
4777 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4780 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
4784 *mask
|= htonl(((1U << bits
) - 1) << offset
);
4794 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4796 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
4800 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4802 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
4806 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4808 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
4812 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4814 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
4818 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
4820 const char *s_base
= s
;
4821 ovs_be16 id
= 0, id_mask
= 0;
4822 uint8_t flags
= 0, flags_mask
= 0;
4825 if (!strncmp(s
, "id=", 3)) {
4827 len
= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
4837 if (!strncmp(s
, "flags=", 6)) {
4839 len
= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
4846 if (!strncmp(s
, "))", 2)) {
4849 *key
= (flags
<< 16) | ntohs(id
);
4851 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
4861 scan_erspan_metadata(const char *s
,
4862 struct erspan_metadata
*key
,
4863 struct erspan_metadata
*mask
)
4865 const char *s_base
= s
;
4866 uint32_t idx
= 0, idx_mask
= 0;
4867 uint8_t ver
= 0, dir
= 0, hwid
= 0;
4868 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
4871 if (!strncmp(s
, "ver=", 4)) {
4873 len
= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
4885 if (!strncmp(s
, "idx=", 4)) {
4887 len
= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
4894 if (!strncmp(s
, ")", 1)) {
4897 key
->u
.index
= htonl(idx
);
4899 mask
->u
.index
= htonl(idx_mask
);
4904 } else if (ver
== 2) {
4905 if (!strncmp(s
, "dir=", 4)) {
4907 len
= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
4916 if (!strncmp(s
, "hwid=", 5)) {
4918 len
= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
4925 if (!strncmp(s
, ")", 1)) {
4928 key
->u
.md2
.hwid
= hwid
;
4929 key
->u
.md2
.dir
= dir
;
4931 mask
->u
.md2
.hwid
= hwid_mask
;
4932 mask
->u
.md2
.dir
= dir_mask
;
4942 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
4944 const char *s_base
= s
;
4945 struct geneve_opt
*opt
= key
->d
;
4946 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
4947 int len_remain
= sizeof key
->d
;
4950 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
4954 len_remain
-= sizeof *opt
;
4956 if (!strncmp(s
, "class=", 6)) {
4958 len
= scan_be16(s
, &opt
->opt_class
,
4959 mask
? &opt_mask
->opt_class
: NULL
);
4965 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
4971 if (!strncmp(s
, "type=", 5)) {
4973 len
= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
4979 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4985 if (!strncmp(s
, "len=", 4)) {
4986 uint8_t opt_len
, opt_len_mask
;
4988 len
= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
4994 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
4997 opt
->length
= opt_len
/ 4;
4999 opt_mask
->length
= opt_len_mask
;
5003 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
5008 if (parse_int_string(s
, (uint8_t *)(opt
+ 1),
5009 data_len
, (char **)&s
)) {
5016 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
5017 data_len
, (char **)&s
)) {
5028 opt
+= 1 + data_len
/ 4;
5030 opt_mask
+= 1 + data_len
/ 4;
5032 len_remain
-= data_len
;
5039 len
= sizeof key
->d
- len_remain
;
5053 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
5055 const uint16_t *flags
= data_
;
5057 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
5058 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5060 if (*flags
& FLOW_TNL_F_CSUM
) {
5061 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5063 if (*flags
& FLOW_TNL_F_OAM
) {
5064 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5069 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5071 const uint32_t *gbp
= data_
;
5074 size_t vxlan_opts_ofs
;
5076 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5077 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5078 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5083 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5085 const struct geneve_scan
*geneve
= data_
;
5087 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5092 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5094 const struct erspan_metadata
*md
= data_
;
5096 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5100 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5102 unsigned long call_fn = (unsigned long)FUNC; \
5104 typedef void (*fn)(struct ofpbuf *, const void *); \
5106 func(BUF, &(DATA)); \
5108 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5112 #define SCAN_IF(NAME) \
5113 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5114 const char *start = s; \
5119 /* Usually no special initialization is needed. */
5120 #define SCAN_BEGIN(NAME, TYPE) \
5123 memset(&skey, 0, sizeof skey); \
5124 memset(&smask, 0, sizeof smask); \
5128 /* Init as fully-masked as mask will not be scanned. */
5129 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5132 memset(&skey, 0, sizeof skey); \
5133 memset(&smask, 0xff, sizeof smask); \
5137 /* VLAN needs special initialization. */
5138 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5140 TYPE skey = KEY_INIT; \
5141 TYPE smask = MASK_INIT; \
5145 /* Scan unnamed entry as 'TYPE' */
5146 #define SCAN_TYPE(TYPE, KEY, MASK) \
5147 len = scan_##TYPE(s, KEY, MASK); \
5153 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5154 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5155 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5156 s += strlen(NAME); \
5157 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5161 #define SCAN_FINISH() \
5162 } while (*s++ == ',' && len != 0); \
5163 if (s[-1] != ')') { \
5167 #define SCAN_FINISH_SINGLE() \
5169 if (*s++ != ')') { \
5173 /* Beginning of nested attribute. */
5174 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5176 size_t key_offset, mask_offset = 0; \
5177 key_offset = nl_msg_start_nested(key, ATTR); \
5179 mask_offset = nl_msg_start_nested(mask, ATTR); \
5184 #define SCAN_END_NESTED() \
5186 nl_msg_end_nested(key, key_offset); \
5188 nl_msg_end_nested(mask, mask_offset); \
5193 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5194 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5196 memset(&skey, 0, sizeof skey); \
5197 memset(&smask, 0xff, sizeof smask); \
5198 s += strlen(NAME); \
5199 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5200 SCAN_PUT(ATTR, FUNC); \
5204 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5205 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5207 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5208 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5210 #define SCAN_PUT(ATTR, FUNC) \
5211 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5213 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5215 #define SCAN_END(ATTR) \
5217 SCAN_PUT(ATTR, NULL); \
5221 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5223 TYPE skey[CNT], smask[CNT]; \
5224 memset(&skey, 0, sizeof skey); \
5225 memset(&smask, 0, sizeof smask); \
5226 int idx = 0, cnt = CNT; \
5227 uint64_t fields = 0; \
5232 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5233 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5234 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5235 if (fields & (1UL << field)) { \
5237 if (++idx == cnt) { \
5241 s += strlen(NAME); \
5242 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5243 fields |= 1UL << field; \
5248 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5249 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5251 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5252 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5254 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5257 #define SCAN_END_ARRAY(ATTR) \
5262 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5266 #define SCAN_END_SINGLE(ATTR) \
5267 SCAN_FINISH_SINGLE(); \
5268 SCAN_PUT(ATTR, NULL); \
5272 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5273 SCAN_BEGIN(NAME, TYPE) { \
5274 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5275 } SCAN_END_SINGLE(ATTR)
5277 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5278 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5279 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5280 } SCAN_END_SINGLE(ATTR)
5282 /* scan_port needs one extra argument. */
5283 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5284 SCAN_BEGIN(NAME, TYPE) { \
5285 len = scan_port(s, &skey, &smask, \
5286 context->port_names); \
5291 } SCAN_END_SINGLE(ATTR)
5294 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5295 struct ofpbuf
*mask
)
5297 if (strncmp(s
, "nsh(", 4) == 0) {
5298 const char *start
= s
;
5300 struct ovs_key_nsh skey
, smask
;
5301 uint32_t spi
= 0, spi_mask
= 0;
5302 uint8_t si
= 0, si_mask
= 0;
5306 memset(&skey
, 0, sizeof skey
);
5307 memset(&smask
, 0, sizeof smask
);
5311 if (strncmp(s
, "flags=", 6) == 0) {
5313 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5321 if (strncmp(s
, "mdtype=", 7) == 0) {
5323 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5331 if (strncmp(s
, "np=", 3) == 0) {
5333 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5341 if (strncmp(s
, "spi=", 4) == 0) {
5343 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5351 if (strncmp(s
, "si=", 3) == 0) {
5353 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5361 if (strncmp(s
, "c1=", 3) == 0) {
5363 len
= scan_be32(s
, &skey
.context
[0],
5364 mask
? &smask
.context
[0] : NULL
);
5372 if (strncmp(s
, "c2=", 3) == 0) {
5374 len
= scan_be32(s
, &skey
.context
[1],
5375 mask
? &smask
.context
[1] : NULL
);
5383 if (strncmp(s
, "c3=", 3) == 0) {
5385 len
= scan_be32(s
, &skey
.context
[2],
5386 mask
? &smask
.context
[2] : NULL
);
5394 if (strncmp(s
, "c4=", 3) == 0) {
5396 len
= scan_be32(s
, &skey
.context
[3],
5397 mask
? &smask
.context
[3] : NULL
);
5404 } while (*s
++ == ',' && len
!= 0);
5409 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5410 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5412 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5414 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5422 parse_odp_key_mask_attr(struct parse_odp_context
*context
, const char *s
,
5423 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5425 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5426 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5427 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5428 OVS_KEY_ATTR_RECIRC_ID
);
5429 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5431 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5432 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5433 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5434 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5436 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5437 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5438 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5439 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5440 SCAN_FIELD("tp_src=", be16
, src_port
);
5441 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5442 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5444 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5445 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5446 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5447 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5448 SCAN_FIELD("tp_src=", be16
, src_port
);
5449 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5450 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5452 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5453 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5454 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5455 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5456 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5457 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5458 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5459 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5460 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5461 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5462 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5464 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5465 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5467 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5468 } SCAN_END_NESTED();
5470 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5472 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5473 SCAN_FIELD("src=", eth
, eth_src
);
5474 SCAN_FIELD("dst=", eth
, eth_dst
);
5475 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5477 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5478 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5479 SCAN_FIELD("vid=", vid
, tci
);
5480 SCAN_FIELD("pcp=", pcp
, tci
);
5481 SCAN_FIELD("cfi=", cfi
, tci
);
5482 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5484 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5486 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5487 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5488 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5489 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5490 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5491 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5493 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5494 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5495 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5496 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5497 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5498 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5499 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5500 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5502 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5503 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5504 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5505 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5506 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5507 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5508 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5509 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5510 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5512 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5513 SCAN_FIELD("src=", be16
, tcp_src
);
5514 SCAN_FIELD("dst=", be16
, tcp_dst
);
5515 } SCAN_END(OVS_KEY_ATTR_TCP
);
5517 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5519 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5520 SCAN_FIELD("src=", be16
, udp_src
);
5521 SCAN_FIELD("dst=", be16
, udp_dst
);
5522 } SCAN_END(OVS_KEY_ATTR_UDP
);
5524 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5525 SCAN_FIELD("src=", be16
, sctp_src
);
5526 SCAN_FIELD("dst=", be16
, sctp_dst
);
5527 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5529 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5530 SCAN_FIELD("type=", u8
, icmp_type
);
5531 SCAN_FIELD("code=", u8
, icmp_code
);
5532 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5534 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5535 SCAN_FIELD("type=", u8
, icmpv6_type
);
5536 SCAN_FIELD("code=", u8
, icmpv6_code
);
5537 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5539 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5540 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5541 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5542 SCAN_FIELD("op=", be16
, arp_op
);
5543 SCAN_FIELD("sha=", eth
, arp_sha
);
5544 SCAN_FIELD("tha=", eth
, arp_tha
);
5545 } SCAN_END(OVS_KEY_ATTR_ARP
);
5547 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5548 SCAN_FIELD("target=", in6_addr
, nd_target
);
5549 SCAN_FIELD("sll=", eth
, nd_sll
);
5550 SCAN_FIELD("tll=", eth
, nd_tll
);
5551 } SCAN_END(OVS_KEY_ATTR_ND
);
5553 struct packet_type
{
5557 SCAN_BEGIN("packet_type(", struct packet_type
) {
5558 SCAN_FIELD("ns=", be16
, ns
);
5559 SCAN_FIELD("id=", be16
, id
);
5560 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5562 /* nsh is nested, it needs special process */
5563 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5570 /* Encap open-coded. */
5571 if (!strncmp(s
, "encap(", 6)) {
5572 const char *start
= s
;
5573 size_t encap
, encap_mask
= 0;
5575 if (context
->depth
+ 1 == MAX_ODP_NESTED
) {
5580 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5582 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5589 s
+= strspn(s
, delimiters
);
5593 } else if (*s
== ')') {
5597 retval
= parse_odp_key_mask_attr(context
, s
, key
, mask
);
5603 if (nl_attr_oversized(key
->size
- encap
- NLA_HDRLEN
)) {
5610 nl_msg_end_nested(key
, encap
);
5612 nl_msg_end_nested(mask
, encap_mask
);
5622 /* Parses the string representation of a datapath flow key, in the
5623 * format output by odp_flow_key_format(). Returns 0 if successful,
5624 * otherwise a positive errno value. On success, the flow key is
5625 * appended to 'key' as a series of Netlink attributes. On failure, no
5626 * data is appended to 'key'. Either way, 'key''s data might be
5629 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5630 * to a port number. (Port names may be used instead of port numbers in
5633 * On success, the attributes appended to 'key' are individually syntactically
5634 * valid, but they may not be valid as a sequence. 'key' might, for example,
5635 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5637 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
5638 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5640 const size_t old_size
= key
->size
;
5641 struct parse_odp_context context
= (struct parse_odp_context
) {
5642 .port_names
= port_names
,
5647 s
+= strspn(s
, delimiters
);
5654 retval
= odp_ufid_from_string(s
, &ufid
);
5656 key
->size
= old_size
;
5658 } else if (retval
> 0) {
5660 s
+= s
[0] == ' ' ? 1 : 0;
5663 retval
= parse_odp_key_mask_attr(&context
, s
, key
, mask
);
5665 key
->size
= old_size
;
5675 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
5678 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5679 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5680 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5681 * must use a zero mask for the netlink frag field, and all ones mask
5683 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
5685 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
5686 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
5687 : OVS_FRAG_TYPE_FIRST
;
5690 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
5691 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
5692 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
5694 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
5696 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
5698 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
5700 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
5701 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
5702 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
5703 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
5704 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
5706 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
5709 /* These share the same layout. */
5711 struct ovs_key_tcp tcp
;
5712 struct ovs_key_udp udp
;
5713 struct ovs_key_sctp sctp
;
5716 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
5717 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
5720 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
5721 bool export_mask
, struct ofpbuf
*buf
)
5723 struct ovs_key_ethernet
*eth_key
;
5724 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
5726 const struct flow
*flow
= parms
->flow
;
5727 const struct flow
*mask
= parms
->mask
;
5728 const struct flow
*data
= export_mask
? mask
: flow
;
5730 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
5732 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
5733 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
5734 parms
->key_buf
, NULL
);
5737 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
5739 if (parms
->support
.ct_state
) {
5740 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5741 ovs_to_odp_ct_state(data
->ct_state
));
5743 if (parms
->support
.ct_zone
) {
5744 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
5746 if (parms
->support
.ct_mark
) {
5747 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
5749 if (parms
->support
.ct_label
) {
5750 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
5751 sizeof(data
->ct_label
));
5753 if (flow
->ct_nw_proto
) {
5754 if (parms
->support
.ct_orig_tuple
5755 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5756 struct ovs_key_ct_tuple_ipv4 ct
= {
5763 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
5765 } else if (parms
->support
.ct_orig_tuple6
5766 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5767 struct ovs_key_ct_tuple_ipv6 ct
= {
5774 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
5778 if (parms
->support
.recirc
) {
5779 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
5780 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
5783 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
5784 * is not the magical value "ODPP_NONE". */
5785 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
5786 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
5789 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
5791 if (OVS_UNLIKELY(parms
->probe
)) {
5792 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
5794 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
5797 /* Conditionally add L2 attributes for Ethernet packets */
5798 if (flow
->packet_type
== htonl(PT_ETH
)) {
5799 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
5801 get_ethernet_key(data
, eth_key
);
5803 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
5804 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
5806 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5807 if (eth_type_vlan(flow
->dl_type
)) {
5808 /* If VLAN was truncated the tpid is in dl_type */
5809 tpid
= flow
->dl_type
;
5816 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5818 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
5820 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
5821 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
5822 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5828 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5829 /* For backwards compatibility with kernels that don't support
5830 * wildcarding, the following convention is used to encode the
5831 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
5834 * -------- -------- -------
5835 * >0x5ff 0xffff Specified Ethernet II Ethertype.
5836 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
5837 * <none> 0xffff Any non-Ethernet II frame (except valid
5838 * 802.3 SNAP packet with valid eth_type).
5841 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5846 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
5848 if (eth_type_vlan(flow
->dl_type
)) {
5852 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5853 struct ovs_key_ipv4
*ipv4_key
;
5855 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
5857 get_ipv4_key(data
, ipv4_key
, export_mask
);
5858 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5859 struct ovs_key_ipv6
*ipv6_key
;
5861 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
5863 get_ipv6_key(data
, ipv6_key
, export_mask
);
5864 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5865 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5866 struct ovs_key_arp
*arp_key
;
5868 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
5870 get_arp_key(data
, arp_key
);
5871 } else if (eth_type_mpls(flow
->dl_type
)) {
5872 struct ovs_key_mpls
*mpls_key
;
5875 n
= flow_count_mpls_labels(flow
, NULL
);
5877 n
= MIN(n
, parms
->support
.max_mpls_depth
);
5879 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
5880 n
* sizeof *mpls_key
);
5881 for (i
= 0; i
< n
; i
++) {
5882 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
5884 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5885 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
5888 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5889 if (flow
->nw_proto
== IPPROTO_TCP
) {
5890 union ovs_key_tp
*tcp_key
;
5892 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
5894 get_tp_key(data
, tcp_key
);
5895 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
5896 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
5898 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5899 union ovs_key_tp
*udp_key
;
5901 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
5903 get_tp_key(data
, udp_key
);
5904 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5905 union ovs_key_tp
*sctp_key
;
5907 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
5909 get_tp_key(data
, sctp_key
);
5910 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
5911 && flow
->nw_proto
== IPPROTO_ICMP
) {
5912 struct ovs_key_icmp
*icmp_key
;
5914 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
5916 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
5917 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
5918 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5919 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
5920 struct ovs_key_icmpv6
*icmpv6_key
;
5922 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
5923 sizeof *icmpv6_key
);
5924 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
5925 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
5927 if (is_nd(flow
, NULL
)
5928 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5929 * type and code are 8 bits wide. Therefore, an exact match
5930 * looks like htons(0xff), not htons(0xffff). See
5931 * xlate_wc_finish() for details. */
5932 && (!export_mask
|| (data
->tp_src
== htons(0xff)
5933 && data
->tp_dst
== htons(0xff)))) {
5935 struct ovs_key_nd
*nd_key
;
5937 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
5939 nd_key
->nd_target
= data
->nd_target
;
5940 nd_key
->nd_sll
= data
->arp_sha
;
5941 nd_key
->nd_tll
= data
->arp_tha
;
5947 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
5948 if (encap
[encaps
]) {
5949 nl_msg_end_nested(buf
, encap
[encaps
]);
5954 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5956 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5957 * capable of being expanded to allow for that much space. */
5959 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
5962 odp_flow_key_from_flow__(parms
, false, buf
);
5965 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5968 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5969 * capable of being expanded to allow for that much space. */
5971 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
5974 odp_flow_key_from_flow__(parms
, true, buf
);
5977 /* Generate ODP flow key from the given packet metadata */
5979 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
5981 const struct pkt_metadata
*md
= &packet
->md
;
5983 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
5985 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
5986 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
5989 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
5992 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5993 ovs_to_odp_ct_state(md
->ct_state
));
5995 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
5998 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
6000 if (!ovs_u128_is_zero(md
->ct_label
)) {
6001 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
6002 sizeof(md
->ct_label
));
6004 if (md
->ct_orig_tuple_ipv6
) {
6005 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
6006 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
6007 &md
->ct_orig_tuple
.ipv6
,
6008 sizeof md
->ct_orig_tuple
.ipv6
);
6011 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
6012 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
6013 &md
->ct_orig_tuple
.ipv4
,
6014 sizeof md
->ct_orig_tuple
.ipv4
);
6019 /* Add an ingress port attribute if 'odp_in_port' is not the magical
6020 * value "ODPP_NONE". */
6021 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
6022 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
6025 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
6026 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
6027 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
6028 pt_ns_type_be(packet
->packet_type
));
6032 /* Generate packet metadata from the given ODP flow key. */
6034 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
6035 struct dp_packet
*packet
)
6037 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6038 const struct nlattr
*nla
;
6039 struct pkt_metadata
*md
= &packet
->md
;
6040 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
6041 ovs_be16 ethertype
= 0;
6044 pkt_metadata_init(md
, ODPP_NONE
);
6046 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6047 enum ovs_key_attr type
= nl_attr_type(nla
);
6048 size_t len
= nl_attr_get_size(nla
);
6049 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6050 OVS_KEY_ATTR_MAX
, type
);
6052 if (len
!= expected_len
&& expected_len
>= 0) {
6057 case OVS_KEY_ATTR_RECIRC_ID
:
6058 md
->recirc_id
= nl_attr_get_u32(nla
);
6060 case OVS_KEY_ATTR_DP_HASH
:
6061 md
->dp_hash
= nl_attr_get_u32(nla
);
6063 case OVS_KEY_ATTR_PRIORITY
:
6064 md
->skb_priority
= nl_attr_get_u32(nla
);
6066 case OVS_KEY_ATTR_SKB_MARK
:
6067 md
->pkt_mark
= nl_attr_get_u32(nla
);
6069 case OVS_KEY_ATTR_CT_STATE
:
6070 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
6072 case OVS_KEY_ATTR_CT_ZONE
:
6073 md
->ct_zone
= nl_attr_get_u16(nla
);
6075 case OVS_KEY_ATTR_CT_MARK
:
6076 md
->ct_mark
= nl_attr_get_u32(nla
);
6078 case OVS_KEY_ATTR_CT_LABELS
: {
6079 md
->ct_label
= nl_attr_get_u128(nla
);
6082 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6083 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6084 md
->ct_orig_tuple
.ipv4
= *ct
;
6085 md
->ct_orig_tuple_ipv6
= false;
6088 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6089 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6091 md
->ct_orig_tuple
.ipv6
= *ct
;
6092 md
->ct_orig_tuple_ipv6
= true;
6095 case OVS_KEY_ATTR_TUNNEL
: {
6096 enum odp_key_fitness res
;
6098 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
6099 if (res
== ODP_FIT_ERROR
) {
6100 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6104 case OVS_KEY_ATTR_IN_PORT
:
6105 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6107 case OVS_KEY_ATTR_ETHERNET
:
6108 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6109 packet_type
= htonl(PT_ETH
);
6111 case OVS_KEY_ATTR_ETHERTYPE
:
6112 ethertype
= nl_attr_get_be16(nla
);
6114 case OVS_KEY_ATTR_UNSPEC
:
6115 case OVS_KEY_ATTR_ENCAP
:
6116 case OVS_KEY_ATTR_VLAN
:
6117 case OVS_KEY_ATTR_IPV4
:
6118 case OVS_KEY_ATTR_IPV6
:
6119 case OVS_KEY_ATTR_TCP
:
6120 case OVS_KEY_ATTR_UDP
:
6121 case OVS_KEY_ATTR_ICMP
:
6122 case OVS_KEY_ATTR_ICMPV6
:
6123 case OVS_KEY_ATTR_ARP
:
6124 case OVS_KEY_ATTR_ND
:
6125 case OVS_KEY_ATTR_SCTP
:
6126 case OVS_KEY_ATTR_TCP_FLAGS
:
6127 case OVS_KEY_ATTR_MPLS
:
6128 case OVS_KEY_ATTR_PACKET_TYPE
:
6129 case OVS_KEY_ATTR_NSH
:
6130 case __OVS_KEY_ATTR_MAX
:
6136 if (packet_type
== htonl(PT_ETH
)) {
6137 packet
->packet_type
= htonl(PT_ETH
);
6138 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6139 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6142 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6147 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
6149 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
6150 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
6154 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6155 uint64_t attrs
, int out_of_range_attr
,
6156 const struct nlattr
*key
, size_t key_len
)
6161 if (VLOG_DROP_DBG(rl
)) {
6166 for (i
= 0; i
< 64; i
++) {
6167 if (attrs
& (UINT64_C(1) << i
)) {
6168 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6170 ds_put_format(&s
, " %s",
6171 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6174 if (out_of_range_attr
) {
6175 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6178 ds_put_cstr(&s
, ": ");
6179 odp_flow_key_format(key
, key_len
, &s
);
6181 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6186 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6188 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6191 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6194 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6195 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6196 return 0xff; /* Error. */
6199 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6200 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6201 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6205 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6206 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6207 int *out_of_range_attrp
)
6209 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6210 const struct nlattr
*nla
;
6211 uint64_t present_attrs
;
6214 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6216 *out_of_range_attrp
= 0;
6217 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6218 uint16_t type
= nl_attr_type(nla
);
6219 size_t len
= nl_attr_get_size(nla
);
6220 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6221 OVS_KEY_ATTR_MAX
, type
);
6223 if (len
!= expected_len
&& expected_len
>= 0) {
6224 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6226 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
6227 "length %d", ovs_key_attr_to_string(type
, namebuf
,
6233 if (type
> OVS_KEY_ATTR_MAX
) {
6234 *out_of_range_attrp
= type
;
6236 if (present_attrs
& (UINT64_C(1) << type
)) {
6237 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6239 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
6240 ovs_key_attr_to_string(type
,
6241 namebuf
, sizeof namebuf
));
6245 present_attrs
|= UINT64_C(1) << type
;
6250 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
6254 *present_attrsp
= present_attrs
;
6258 static enum odp_key_fitness
6259 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6260 uint64_t expected_attrs
,
6261 const struct nlattr
*key
, size_t key_len
)
6263 uint64_t missing_attrs
;
6264 uint64_t extra_attrs
;
6266 missing_attrs
= expected_attrs
& ~present_attrs
;
6267 if (missing_attrs
) {
6268 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6269 log_odp_key_attributes(&rl
, "expected but not present",
6270 missing_attrs
, 0, key
, key_len
);
6271 return ODP_FIT_TOO_LITTLE
;
6274 extra_attrs
= present_attrs
& ~expected_attrs
;
6275 if (extra_attrs
|| out_of_range_attr
) {
6276 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6277 log_odp_key_attributes(&rl
, "present but not expected",
6278 extra_attrs
, out_of_range_attr
, key
, key_len
);
6279 return ODP_FIT_TOO_MUCH
;
6282 return ODP_FIT_PERFECT
;
6286 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6287 uint64_t present_attrs
, uint64_t *expected_attrs
,
6288 struct flow
*flow
, const struct flow
*src_flow
)
6290 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6291 bool is_mask
= flow
!= src_flow
;
6293 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6294 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6295 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6296 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
6297 ntohs(flow
->dl_type
));
6300 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6301 flow
->dl_type
!= htons(0xffff)) {
6304 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6307 /* Default ethertype for well-known L3 packets. */
6308 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6309 flow
->dl_type
= htons(ETH_TYPE_IP
);
6310 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6311 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6312 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6313 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6315 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6317 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6318 /* dl_type is mandatory for non-Ethernet packets */
6319 flow
->dl_type
= htons(0xffff);
6320 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6321 /* See comments in odp_flow_key_from_flow__(). */
6322 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
6329 static enum odp_key_fitness
6330 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6331 uint64_t present_attrs
, int out_of_range_attr
,
6332 uint64_t *expected_attrs
, struct flow
*flow
,
6333 const struct nlattr
*key
, size_t key_len
,
6334 const struct flow
*src_flow
, bool need_check
)
6336 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6337 bool is_mask
= src_flow
!= flow
;
6338 const void *check_start
= NULL
;
6339 size_t check_len
= 0;
6340 enum ovs_key_attr expected_bit
= 0xff;
6342 if (eth_type_mpls(src_flow
->dl_type
)) {
6343 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6344 *expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6346 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6347 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6348 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6349 int n
= size
/ sizeof(ovs_be32
);
6352 if (!size
|| size
% sizeof(ovs_be32
)) {
6353 return ODP_FIT_ERROR
;
6355 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6356 return ODP_FIT_ERROR
;
6359 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6360 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6362 if (n
> FLOW_MAX_MPLS_LABELS
) {
6363 return ODP_FIT_TOO_MUCH
;
6367 /* BOS may be set only in the innermost label. */
6368 for (i
= 0; i
< n
- 1; i
++) {
6369 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6370 return ODP_FIT_ERROR
;
6374 /* BOS must be set in the innermost label. */
6375 if (n
< FLOW_MAX_MPLS_LABELS
6376 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6377 return ODP_FIT_TOO_LITTLE
;
6383 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6385 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6387 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6388 const struct ovs_key_ipv4
*ipv4_key
;
6390 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6391 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6392 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6393 return ODP_FIT_ERROR
;
6396 check_start
= ipv4_key
;
6397 check_len
= sizeof *ipv4_key
;
6398 expected_bit
= OVS_KEY_ATTR_IPV4
;
6401 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6403 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6405 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6406 const struct ovs_key_ipv6
*ipv6_key
;
6408 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6409 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6410 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6411 return ODP_FIT_ERROR
;
6414 check_start
= ipv6_key
;
6415 check_len
= sizeof *ipv6_key
;
6416 expected_bit
= OVS_KEY_ATTR_IPV6
;
6419 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6420 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6422 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6424 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6425 const struct ovs_key_arp
*arp_key
;
6427 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6428 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6429 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
6430 "key", ntohs(arp_key
->arp_op
));
6431 return ODP_FIT_ERROR
;
6433 put_arp_key(arp_key
, flow
);
6435 check_start
= arp_key
;
6436 check_len
= sizeof *arp_key
;
6437 expected_bit
= OVS_KEY_ATTR_ARP
;
6440 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6442 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6444 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6445 odp_nsh_key_from_attr(attrs
[OVS_KEY_ATTR_NSH
], &flow
->nsh
, NULL
);
6447 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6448 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6449 expected_bit
= OVS_KEY_ATTR_NSH
;
6455 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6456 if (!is_all_zeros(check_start
, check_len
) &&
6457 flow
->dl_type
!= htons(0xffff)) {
6458 return ODP_FIT_ERROR
;
6460 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6464 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6465 if (src_flow
->nw_proto
== IPPROTO_TCP
6466 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6467 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6468 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6470 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6472 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6473 const union ovs_key_tp
*tcp_key
;
6475 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6476 put_tp_key(tcp_key
, flow
);
6477 expected_bit
= OVS_KEY_ATTR_TCP
;
6479 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6480 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6481 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6483 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6484 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6485 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6486 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6488 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
6490 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
6491 const union ovs_key_tp
*udp_key
;
6493 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
6494 put_tp_key(udp_key
, flow
);
6495 expected_bit
= OVS_KEY_ATTR_UDP
;
6497 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
6498 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6499 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6500 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6502 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
6504 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
6505 const union ovs_key_tp
*sctp_key
;
6507 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
6508 put_tp_key(sctp_key
, flow
);
6509 expected_bit
= OVS_KEY_ATTR_SCTP
;
6511 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
6512 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
6513 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6515 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
6517 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
6518 const struct ovs_key_icmp
*icmp_key
;
6520 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
6521 flow
->tp_src
= htons(icmp_key
->icmp_type
);
6522 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
6523 expected_bit
= OVS_KEY_ATTR_ICMP
;
6525 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
6526 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6527 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6529 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
6531 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
6532 const struct ovs_key_icmpv6
*icmpv6_key
;
6534 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
6535 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
6536 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
6537 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
6538 if (is_nd(src_flow
, NULL
)) {
6540 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6542 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
6543 const struct ovs_key_nd
*nd_key
;
6545 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
6546 flow
->nd_target
= nd_key
->nd_target
;
6547 flow
->arp_sha
= nd_key
->nd_sll
;
6548 flow
->arp_tha
= nd_key
->nd_tll
;
6550 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6551 * ICMP type and code are 8 bits wide. Therefore, an
6552 * exact match looks like htons(0xff), not
6553 * htons(0xffff). See xlate_wc_finish() for details.
6555 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
6556 (flow
->tp_src
!= htons(0xff) ||
6557 flow
->tp_dst
!= htons(0xff))) {
6558 return ODP_FIT_ERROR
;
6560 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6566 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
6567 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6568 /* OVS userspace parses the IGMP type, code, and group, but its
6569 * datapaths do not, so there is always missing information. */
6570 return ODP_FIT_TOO_LITTLE
;
6572 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
6573 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
6574 return ODP_FIT_ERROR
;
6576 *expected_attrs
|= UINT64_C(1) << expected_bit
;
6581 return need_check
? check_expectations(present_attrs
, out_of_range_attr
,
6582 *expected_attrs
, key
, key_len
) : ODP_FIT_PERFECT
;
6585 /* Parse 802.1Q header then encapsulated L3 attributes. */
6586 static enum odp_key_fitness
6587 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6588 uint64_t present_attrs
, int out_of_range_attr
,
6589 uint64_t expected_attrs
, struct flow
*flow
,
6590 const struct nlattr
*key
, size_t key_len
,
6591 const struct flow
*src_flow
)
6593 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6594 bool is_mask
= src_flow
!= flow
;
6596 const struct nlattr
*encap
;
6597 enum odp_key_fitness encap_fitness
;
6598 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
6601 while (encaps
< flow_vlan_limit
&&
6603 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
6604 : eth_type_vlan(flow
->dl_type
))) {
6606 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
6607 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
6609 /* Calculate fitness of outer attributes. */
6611 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
6612 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
6614 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6615 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6617 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
6618 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
6621 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
6622 expected_attrs
, key
, key_len
);
6625 * Remove the TPID from dl_type since it's not the real Ethertype. */
6626 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
6627 flow
->dl_type
= htons(0);
6628 flow
->vlans
[encaps
].tci
=
6629 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
6630 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
6633 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
6634 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6635 return ODP_FIT_TOO_LITTLE
;
6636 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
6637 /* Corner case for a truncated 802.1Q header. */
6638 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
6639 return ODP_FIT_TOO_MUCH
;
6642 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
6643 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
6644 "but CFI bit is not set",
6645 ntohs(flow
->vlans
[encaps
].tci
));
6646 return ODP_FIT_ERROR
;
6649 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6654 /* Now parse the encapsulated attributes. */
6655 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
6656 attrs
, &present_attrs
, &out_of_range_attr
)) {
6657 return ODP_FIT_ERROR
;
6661 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
6663 return ODP_FIT_ERROR
;
6665 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
,
6670 if (encap_fitness
!= ODP_FIT_PERFECT
) {
6671 return encap_fitness
;
6676 return check_expectations(present_attrs
, out_of_range_attr
,
6677 expected_attrs
, key
, key_len
);
6680 static enum odp_key_fitness
6681 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
6682 struct flow
*flow
, const struct flow
*src_flow
)
6684 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
6685 uint64_t expected_attrs
;
6686 uint64_t present_attrs
;
6687 int out_of_range_attr
;
6688 bool is_mask
= src_flow
!= flow
;
6690 memset(flow
, 0, sizeof *flow
);
6692 /* Parse attributes. */
6693 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
6694 &out_of_range_attr
)) {
6695 return ODP_FIT_ERROR
;
6700 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
6701 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
6702 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
6703 } else if (is_mask
) {
6704 /* Always exact match recirc_id if it is not specified. */
6705 flow
->recirc_id
= UINT32_MAX
;
6708 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
6709 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
6710 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
6712 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
6713 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
6714 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
6717 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
6718 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
6719 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
6722 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
6723 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
6725 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
6726 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
6728 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
6729 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
6730 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
6732 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
6733 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
6734 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
6736 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
6737 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
6738 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
6740 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
6741 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
6742 flow
->ct_nw_src
= ct
->ipv4_src
;
6743 flow
->ct_nw_dst
= ct
->ipv4_dst
;
6744 flow
->ct_nw_proto
= ct
->ipv4_proto
;
6745 flow
->ct_tp_src
= ct
->src_port
;
6746 flow
->ct_tp_dst
= ct
->dst_port
;
6747 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
6749 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
6750 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
6752 flow
->ct_ipv6_src
= ct
->ipv6_src
;
6753 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
6754 flow
->ct_nw_proto
= ct
->ipv6_proto
;
6755 flow
->ct_tp_src
= ct
->src_port
;
6756 flow
->ct_tp_dst
= ct
->dst_port
;
6757 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
6760 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
6761 enum odp_key_fitness res
;
6763 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
6765 if (res
== ODP_FIT_ERROR
) {
6766 return ODP_FIT_ERROR
;
6767 } else if (res
== ODP_FIT_PERFECT
) {
6768 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
6772 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
6773 flow
->in_port
.odp_port
6774 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
6775 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
6776 } else if (!is_mask
) {
6777 flow
->in_port
.odp_port
= ODPP_NONE
;
6780 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
6782 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
6783 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
6784 if (pt_ns(src_flow
->packet_type
) == OFPHTN_ETHERTYPE
) {
6785 flow
->dl_type
= pt_ns_type_be(flow
->packet_type
);
6787 } else if (!is_mask
) {
6788 flow
->packet_type
= htonl(PT_ETH
);
6791 /* Check for Ethernet header. */
6792 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
6793 const struct ovs_key_ethernet
*eth_key
;
6795 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
6796 put_ethernet_key(eth_key
, flow
);
6798 flow
->packet_type
= htonl(PT_ETH
);
6800 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
6802 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6803 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6805 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6808 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6811 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
6812 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
6814 return ODP_FIT_ERROR
;
6818 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
6819 : eth_type_vlan(src_flow
->dl_type
)) {
6820 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
6821 expected_attrs
, flow
, key
, key_len
, src_flow
);
6824 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
6825 flow
->vlans
[0].tpid
= htons(0xffff);
6826 flow
->vlans
[0].tci
= htons(0xffff);
6827 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6828 flow
->vlans
[0].tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
6829 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6832 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
6833 &expected_attrs
, flow
, key
, key_len
,
6837 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
6838 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
6839 * 'key' fits our expectations for what a flow key should contain.
6841 * The 'in_port' will be the datapath's understanding of the port. The
6842 * caller will need to translate with odp_port_to_ofp_port() if the
6843 * OpenFlow port is needed.
6845 * This function doesn't take the packet itself as an argument because none of
6846 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
6847 * it is always possible to infer which additional attribute(s) should appear
6848 * by looking at the attributes for lower-level protocols, e.g. if the network
6849 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
6850 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
6851 * must be absent. */
6852 enum odp_key_fitness
6853 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
6856 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
6859 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
6860 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
6861 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
6862 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
6863 * well 'key' fits our expectations for what a flow key should contain. */
6864 enum odp_key_fitness
6865 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
6866 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
6869 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
6870 &mask
->masks
, src_flow
);
6873 /* A missing mask means that the flow should be exact matched.
6874 * Generate an appropriate exact wildcard for the flow. */
6875 flow_wildcards_init_for_packet(mask
, src_flow
);
6877 return ODP_FIT_PERFECT
;
6881 /* Converts the netlink formated key/mask to match.
6882 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
6883 * disagree on the acceptable form of flow */
6885 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
6886 const struct nlattr
*mask
, size_t mask_len
,
6887 struct match
*match
)
6889 enum odp_key_fitness fitness
;
6891 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
);
6893 /* This should not happen: it indicates that
6894 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6895 * the acceptable form of a flow. Log the problem as an error,
6896 * with enough details to enable debugging. */
6897 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6899 if (!VLOG_DROP_ERR(&rl
)) {
6903 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
6904 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
6911 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
);
6913 /* This should not happen: it indicates that
6914 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6915 * disagree on the acceptable form of a mask. Log the problem
6916 * as an error, with enough details to enable debugging. */
6917 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6919 if (!VLOG_DROP_ERR(&rl
)) {
6923 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
6925 VLOG_ERR("internal error parsing flow mask %s (%s)",
6926 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
6936 /* Returns 'fitness' as a string, for use in debug messages. */
6938 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
6941 case ODP_FIT_PERFECT
:
6943 case ODP_FIT_TOO_MUCH
:
6945 case ODP_FIT_TOO_LITTLE
:
6946 return "too_little";
6954 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6955 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6956 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6957 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6958 * null, then the return value is not meaningful.) */
6960 odp_put_userspace_action(uint32_t pid
,
6961 const void *userdata
, size_t userdata_size
,
6962 odp_port_t tunnel_out_port
,
6963 bool include_actions
,
6964 struct ofpbuf
*odp_actions
)
6966 size_t userdata_ofs
;
6969 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
6970 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
6972 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
6974 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6975 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6978 * - The kernel rejected shorter userdata with -ERANGE.
6980 * - The kernel silently dropped userdata beyond the first 8 bytes.
6982 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6983 * separately disable features that required more than 8 bytes.) */
6984 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
6985 MAX(8, userdata_size
)),
6986 userdata
, userdata_size
);
6990 if (tunnel_out_port
!= ODPP_NONE
) {
6991 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
6994 if (include_actions
) {
6995 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
6997 nl_msg_end_nested(odp_actions
, offset
);
6999 return userdata_ofs
;
7003 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
7005 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
7009 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
7010 const struct eth_addr
*eth_src
,
7011 const struct eth_addr
*eth_dst
)
7013 struct ovs_action_push_eth eth
;
7015 memset(ð
, 0, sizeof eth
);
7017 eth
.addresses
.eth_src
= *eth_src
;
7020 eth
.addresses
.eth_dst
= *eth_dst
;
7023 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
7028 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
7029 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7031 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7032 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
7033 nl_msg_end_nested(odp_actions
, offset
);
7037 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
7038 struct ovs_action_push_tnl
*data
)
7040 int size
= offsetof(struct ovs_action_push_tnl
, header
);
7042 size
+= data
->header_len
;
7043 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
7047 /* The commit_odp_actions() function and its helpers. */
7050 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
7051 const void *key
, size_t key_size
)
7053 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7054 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
7055 nl_msg_end_nested(odp_actions
, offset
);
7058 /* Masked set actions have a mask following the data within the netlink
7059 * attribute. The unmasked bits in the data will be cleared as the data
7060 * is copied to the action. */
7062 commit_masked_set_action(struct ofpbuf
*odp_actions
,
7063 enum ovs_key_attr key_type
,
7064 const void *key_
, const void *mask_
, size_t key_size
)
7066 size_t offset
= nl_msg_start_nested(odp_actions
,
7067 OVS_ACTION_ATTR_SET_MASKED
);
7068 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
7069 const char *key
= key_
, *mask
= mask_
;
7071 memcpy(data
+ key_size
, mask
, key_size
);
7072 /* Clear unmasked bits while copying. */
7073 while (key_size
--) {
7074 *data
++ = *key
++ & *mask
++;
7076 nl_msg_end_nested(odp_actions
, offset
);
7079 /* If any of the flow key data that ODP actions can modify are different in
7080 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
7081 * 'odp_actions' that change the flow tunneling information in key from
7082 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
7083 * same way. In other words, operates the same as commit_odp_actions(), but
7084 * only on tunneling information. */
7086 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7087 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7089 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7090 * must have non-zero ipv6_dst. */
7091 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7092 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7095 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7096 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7101 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7102 const void *key
, void *base
, void *mask
, size_t size
,
7103 struct ofpbuf
*odp_actions
)
7105 if (memcmp(key
, base
, size
)) {
7106 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7108 if (use_masked_set
&& !fully_masked
) {
7109 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7111 if (!fully_masked
) {
7112 memset(mask
, 0xff, size
);
7114 commit_set_action(odp_actions
, attr
, key
, size
);
7116 memcpy(base
, key
, size
);
7119 /* Mask bits are set when we have either read or set the corresponding
7120 * values. Masked bits will be exact-matched, no need to set them
7121 * if the value did not actually change. */
7127 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7129 eth
->eth_src
= flow
->dl_src
;
7130 eth
->eth_dst
= flow
->dl_dst
;
7134 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7136 flow
->dl_src
= eth
->eth_src
;
7137 flow
->dl_dst
= eth
->eth_dst
;
7141 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7142 struct ofpbuf
*odp_actions
,
7143 struct flow_wildcards
*wc
,
7146 struct ovs_key_ethernet key
, base
, mask
;
7148 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7152 get_ethernet_key(flow
, &key
);
7153 get_ethernet_key(base_flow
, &base
);
7154 get_ethernet_key(&wc
->masks
, &mask
);
7156 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7157 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7158 put_ethernet_key(&base
, base_flow
);
7159 put_ethernet_key(&mask
, &wc
->masks
);
7164 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7165 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7167 int base_n
= flow_count_vlan_headers(base
);
7168 int flow_n
= flow_count_vlan_headers(flow
);
7169 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7171 /* Pop all mismatching vlan of base, push those of flow */
7172 for (; base_n
>= 0; base_n
--) {
7173 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7174 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7177 for (; flow_n
>= 0; flow_n
--) {
7178 struct ovs_action_push_vlan vlan
;
7180 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7181 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7182 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7183 &vlan
, sizeof vlan
);
7185 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7188 /* Wildcarding already done at action translation time. */
7190 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7191 struct ofpbuf
*odp_actions
)
7193 int base_n
= flow_count_mpls_labels(base
, NULL
);
7194 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7195 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7198 while (base_n
> common_n
) {
7199 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7200 /* If there is only one more LSE in base than there are common
7201 * between base and flow; and flow has at least one more LSE than
7202 * is common then the topmost LSE of base may be updated using
7204 struct ovs_key_mpls mpls_key
;
7206 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7207 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7208 &mpls_key
, sizeof mpls_key
);
7209 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7212 /* Otherwise, if there more LSEs in base than are common between
7213 * base and flow then pop the topmost one. */
7215 /* If all the LSEs are to be popped and this is not the outermost
7216 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7217 * POP_MPLS action instead of flow->dl_type.
7219 * This is because the POP_MPLS action requires its ethertype
7220 * argument to be an MPLS ethernet type but in this case
7221 * flow->dl_type will be a non-MPLS ethernet type.
7223 * When the final POP_MPLS action occurs it use flow->dl_type and
7224 * the and the resulting packet will have the desired dl_type. */
7225 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7226 dl_type
= htons(ETH_TYPE_MPLS
);
7228 dl_type
= flow
->dl_type
;
7230 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7231 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7236 /* If, after the above popping and setting, there are more LSEs in flow
7237 * than base then some LSEs need to be pushed. */
7238 while (base_n
< flow_n
) {
7239 struct ovs_action_push_mpls
*mpls
;
7241 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7242 OVS_ACTION_ATTR_PUSH_MPLS
,
7244 mpls
->mpls_ethertype
= flow
->dl_type
;
7245 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7246 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7247 * headers if the flow is restored later due to returning from a patch
7248 * port or group bucket. */
7249 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7250 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7256 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7258 ipv4
->ipv4_src
= flow
->nw_src
;
7259 ipv4
->ipv4_dst
= flow
->nw_dst
;
7260 ipv4
->ipv4_proto
= flow
->nw_proto
;
7261 ipv4
->ipv4_tos
= flow
->nw_tos
;
7262 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7263 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7267 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7269 flow
->nw_src
= ipv4
->ipv4_src
;
7270 flow
->nw_dst
= ipv4
->ipv4_dst
;
7271 flow
->nw_proto
= ipv4
->ipv4_proto
;
7272 flow
->nw_tos
= ipv4
->ipv4_tos
;
7273 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7274 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7278 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7279 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7282 struct ovs_key_ipv4 key
, mask
, base
;
7284 /* Check that nw_proto and nw_frag remain unchanged. */
7285 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7286 flow
->nw_frag
== base_flow
->nw_frag
);
7288 get_ipv4_key(flow
, &key
, false);
7289 get_ipv4_key(base_flow
, &base
, false);
7290 get_ipv4_key(&wc
->masks
, &mask
, true);
7291 mask
.ipv4_proto
= 0; /* Not writeable. */
7292 mask
.ipv4_frag
= 0; /* Not writable. */
7294 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7295 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7296 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7299 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7301 put_ipv4_key(&base
, base_flow
, false);
7302 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
7303 put_ipv4_key(&mask
, &wc
->masks
, true);
7309 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7311 ipv6
->ipv6_src
= flow
->ipv6_src
;
7312 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7313 ipv6
->ipv6_label
= flow
->ipv6_label
;
7314 ipv6
->ipv6_proto
= flow
->nw_proto
;
7315 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7316 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7317 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7321 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7323 flow
->ipv6_src
= ipv6
->ipv6_src
;
7324 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7325 flow
->ipv6_label
= ipv6
->ipv6_label
;
7326 flow
->nw_proto
= ipv6
->ipv6_proto
;
7327 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7328 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7329 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
7333 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
7334 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7337 struct ovs_key_ipv6 key
, mask
, base
;
7339 /* Check that nw_proto and nw_frag remain unchanged. */
7340 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7341 flow
->nw_frag
== base_flow
->nw_frag
);
7343 get_ipv6_key(flow
, &key
, false);
7344 get_ipv6_key(base_flow
, &base
, false);
7345 get_ipv6_key(&wc
->masks
, &mask
, true);
7346 mask
.ipv6_proto
= 0; /* Not writeable. */
7347 mask
.ipv6_frag
= 0; /* Not writable. */
7348 mask
.ipv6_label
&= htonl(IPV6_LABEL_MASK
); /* Not writable. */
7350 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7351 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7352 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
7355 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7357 put_ipv6_key(&base
, base_flow
, false);
7358 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
7359 put_ipv6_key(&mask
, &wc
->masks
, true);
7365 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
7367 /* ARP key has padding, clear it. */
7368 memset(arp
, 0, sizeof *arp
);
7370 arp
->arp_sip
= flow
->nw_src
;
7371 arp
->arp_tip
= flow
->nw_dst
;
7372 arp
->arp_op
= htons(flow
->nw_proto
);
7373 arp
->arp_sha
= flow
->arp_sha
;
7374 arp
->arp_tha
= flow
->arp_tha
;
7378 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
7380 flow
->nw_src
= arp
->arp_sip
;
7381 flow
->nw_dst
= arp
->arp_tip
;
7382 flow
->nw_proto
= ntohs(arp
->arp_op
);
7383 flow
->arp_sha
= arp
->arp_sha
;
7384 flow
->arp_tha
= arp
->arp_tha
;
7387 static enum slow_path_reason
7388 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
7389 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7391 struct ovs_key_arp key
, mask
, base
;
7393 get_arp_key(flow
, &key
);
7394 get_arp_key(base_flow
, &base
);
7395 get_arp_key(&wc
->masks
, &mask
);
7397 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
7399 put_arp_key(&base
, base_flow
);
7400 put_arp_key(&mask
, &wc
->masks
);
7407 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
7409 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7410 icmp
->icmp_type
= ntohs(flow
->tp_src
);
7411 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
7415 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
7417 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7418 flow
->tp_src
= htons(icmp
->icmp_type
);
7419 flow
->tp_dst
= htons(icmp
->icmp_code
);
7422 static enum slow_path_reason
7423 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
7424 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7426 struct ovs_key_icmp key
, mask
, base
;
7427 enum ovs_key_attr attr
;
7429 if (is_icmpv4(flow
, NULL
)) {
7430 attr
= OVS_KEY_ATTR_ICMP
;
7431 } else if (is_icmpv6(flow
, NULL
)) {
7432 attr
= OVS_KEY_ATTR_ICMPV6
;
7437 get_icmp_key(flow
, &key
);
7438 get_icmp_key(base_flow
, &base
);
7439 get_icmp_key(&wc
->masks
, &mask
);
7441 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7442 put_icmp_key(&base
, base_flow
);
7443 put_icmp_key(&mask
, &wc
->masks
);
7450 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
7452 nd
->nd_target
= flow
->nd_target
;
7453 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7454 nd
->nd_sll
= flow
->arp_sha
;
7455 nd
->nd_tll
= flow
->arp_tha
;
7459 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
7461 flow
->nd_target
= nd
->nd_target
;
7462 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7463 flow
->arp_sha
= nd
->nd_sll
;
7464 flow
->arp_tha
= nd
->nd_tll
;
7467 static enum slow_path_reason
7468 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
7469 struct ofpbuf
*odp_actions
,
7470 struct flow_wildcards
*wc
, bool use_masked
)
7472 struct ovs_key_nd key
, mask
, base
;
7474 get_nd_key(flow
, &key
);
7475 get_nd_key(base_flow
, &base
);
7476 get_nd_key(&wc
->masks
, &mask
);
7478 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7480 put_nd_key(&base
, base_flow
);
7481 put_nd_key(&mask
, &wc
->masks
);
7488 static enum slow_path_reason
7489 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
7490 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7493 /* Check if 'flow' really has an L3 header. */
7494 if (!flow
->nw_proto
) {
7498 switch (ntohs(base
->dl_type
)) {
7500 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
7504 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
7505 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
7508 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
7515 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
7519 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
7520 memset(nsh
->context
, 0, sizeof(nsh
->context
));
7526 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
7527 bool is_mask OVS_UNUSED
)
7530 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
7531 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
7536 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
7537 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
7538 struct ovs_key_nsh
*mask
, size_t size
,
7539 struct ofpbuf
*odp_actions
)
7541 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
7543 if (memcmp(key
, base
, size
) == 0) {
7544 /* Mask bits are set when we have either read or set the corresponding
7545 * values. Masked bits will be exact-matched, no need to set them
7546 * if the value did not actually change. */
7550 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7552 if (use_masked_set
&& !fully_masked
) {
7554 struct ovs_nsh_key_base nsh_base
;
7555 struct ovs_nsh_key_base nsh_base_mask
;
7556 struct ovs_nsh_key_md1 md1
;
7557 struct ovs_nsh_key_md1 md1_mask
;
7558 size_t offset
= nl_msg_start_nested(odp_actions
,
7559 OVS_ACTION_ATTR_SET_MASKED
);
7561 nsh_base
.flags
= key
->flags
;
7562 nsh_base
.ttl
= key
->ttl
;
7563 nsh_base
.mdtype
= key
->mdtype
;
7564 nsh_base
.np
= key
->np
;
7565 nsh_base
.path_hdr
= key
->path_hdr
;
7567 nsh_base_mask
.flags
= mask
->flags
;
7568 nsh_base_mask
.ttl
= mask
->ttl
;
7569 nsh_base_mask
.mdtype
= mask
->mdtype
;
7570 nsh_base_mask
.np
= mask
->np
;
7571 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
7573 /* OVS_KEY_ATTR_NSH keys */
7574 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
7576 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
7577 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
7578 OVS_NSH_KEY_ATTR_BASE
,
7579 2 * sizeof(nsh_base
));
7580 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
7581 size_t lkey_size
= sizeof(nsh_base
);
7583 while (lkey_size
--) {
7584 *data
++ = *lkey
++ & *lmask
++;
7586 lmask
= (char *)&nsh_base_mask
;
7587 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
7589 switch (key
->mdtype
) {
7591 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
7592 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
7594 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
7595 data
= nl_msg_put_unspec_uninit(odp_actions
,
7596 OVS_NSH_KEY_ATTR_MD1
,
7598 lkey
= (char *)&md1
;
7599 lmask
= (char *)&md1_mask
;
7600 lkey_size
= sizeof(md1
);
7602 while (lkey_size
--) {
7603 *data
++ = *lkey
++ & *lmask
++;
7605 lmask
= (char *)&md1_mask
;
7606 memcpy(data
, lmask
, sizeof(md1_mask
));
7610 /* No match support for other MD formats yet. */
7614 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
7616 nl_msg_end_nested(odp_actions
, offset
);
7618 if (!fully_masked
) {
7619 memset(mask
, 0xff, size
);
7621 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7622 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
7623 nl_msg_end_nested(odp_actions
, offset
);
7625 memcpy(base
, key
, size
);
7630 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
7631 struct ofpbuf
*odp_actions
,
7632 struct flow_wildcards
*wc
,
7635 struct ovs_key_nsh key
, mask
, base
;
7637 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
7638 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
7642 /* Check that mdtype and np remain unchanged. */
7643 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
7644 flow
->nsh
.np
== base_flow
->nsh
.np
);
7646 get_nsh_key(flow
, &key
, false);
7647 get_nsh_key(base_flow
, &base
, false);
7648 get_nsh_key(&wc
->masks
, &mask
, true);
7649 mask
.mdtype
= 0; /* Not writable. */
7650 mask
.np
= 0; /* Not writable. */
7652 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
7653 sizeof key
, odp_actions
)) {
7654 put_nsh_key(&base
, base_flow
, false);
7655 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
7656 put_nsh_key(&mask
, &wc
->masks
, true);
7661 /* TCP, UDP, and SCTP keys have the same layout. */
7662 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
7663 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
7666 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
7668 tp
->tcp
.tcp_src
= flow
->tp_src
;
7669 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
7673 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
7675 flow
->tp_src
= tp
->tcp
.tcp_src
;
7676 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
7680 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
7681 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7684 enum ovs_key_attr key_type
;
7685 union ovs_key_tp key
, mask
, base
;
7687 /* Check if 'flow' really has an L3 header. */
7688 if (!flow
->nw_proto
) {
7692 if (!is_ip_any(base_flow
)) {
7696 if (flow
->nw_proto
== IPPROTO_TCP
) {
7697 key_type
= OVS_KEY_ATTR_TCP
;
7698 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
7699 key_type
= OVS_KEY_ATTR_UDP
;
7700 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
7701 key_type
= OVS_KEY_ATTR_SCTP
;
7706 get_tp_key(flow
, &key
);
7707 get_tp_key(base_flow
, &base
);
7708 get_tp_key(&wc
->masks
, &mask
);
7710 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7712 put_tp_key(&base
, base_flow
);
7713 put_tp_key(&mask
, &wc
->masks
);
7718 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
7719 struct ofpbuf
*odp_actions
,
7720 struct flow_wildcards
*wc
,
7723 uint32_t key
, mask
, base
;
7725 key
= flow
->skb_priority
;
7726 base
= base_flow
->skb_priority
;
7727 mask
= wc
->masks
.skb_priority
;
7729 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
7730 sizeof key
, odp_actions
)) {
7731 base_flow
->skb_priority
= base
;
7732 wc
->masks
.skb_priority
= mask
;
7737 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
7738 struct ofpbuf
*odp_actions
,
7739 struct flow_wildcards
*wc
,
7742 uint32_t key
, mask
, base
;
7744 key
= flow
->pkt_mark
;
7745 base
= base_flow
->pkt_mark
;
7746 mask
= wc
->masks
.pkt_mark
;
7748 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
7749 sizeof key
, odp_actions
)) {
7750 base_flow
->pkt_mark
= base
;
7751 wc
->masks
.pkt_mark
= mask
;
7756 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
7758 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
7762 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
7763 const struct flow
*flow
,
7764 struct ofpbuf
*encap_data
)
7766 uint8_t * metadata
= NULL
;
7767 uint8_t md_size
= 0;
7769 switch (flow
->nsh
.mdtype
) {
7772 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
7773 metadata
= encap_data
->data
;
7774 md_size
= encap_data
->size
;
7783 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
7784 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
7785 nl_msg_end_nested(odp_actions
, offset
);
7789 commit_encap_decap_action(const struct flow
*flow
,
7790 struct flow
*base_flow
,
7791 struct ofpbuf
*odp_actions
,
7792 struct flow_wildcards
*wc
,
7793 bool pending_encap
, bool pending_decap
,
7794 struct ofpbuf
*encap_data
)
7796 if (pending_encap
) {
7797 switch (ntohl(flow
->packet_type
)) {
7800 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
7802 base_flow
->packet_type
= flow
->packet_type
;
7803 base_flow
->dl_src
= flow
->dl_src
;
7804 base_flow
->dl_dst
= flow
->dl_dst
;
7809 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
7810 base_flow
->packet_type
= flow
->packet_type
;
7811 /* Update all packet headers in base_flow. */
7812 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
7813 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
7816 /* Only the above protocols are supported for encap.
7817 * The check is done at action translation. */
7820 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
7821 /* This is an explicit or implicit decap case. */
7822 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
7823 base_flow
->packet_type
== htonl(PT_ETH
)) {
7824 /* Generate pop_eth and continue without recirculation. */
7825 odp_put_pop_eth_action(odp_actions
);
7826 base_flow
->packet_type
= flow
->packet_type
;
7827 base_flow
->dl_src
= eth_addr_zero
;
7828 base_flow
->dl_dst
= eth_addr_zero
;
7830 /* All other decap cases require recirculation.
7831 * No need to update the base flow here. */
7832 switch (ntohl(base_flow
->packet_type
)) {
7835 odp_put_pop_nsh_action(odp_actions
);
7838 /* Checks are done during translation. */
7844 wc
->masks
.packet_type
= OVS_BE32_MAX
;
7847 /* If any of the flow key data that ODP actions can modify are different in
7848 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
7849 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
7850 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
7851 * in addition to this function if needed. Sets fields in 'wc' that are
7852 * used as part of the action.
7854 * Returns a reason to force processing the flow's packets into the userspace
7855 * slow path, if there is one, otherwise 0. */
7856 enum slow_path_reason
7857 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
7858 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7859 bool use_masked
, bool pending_encap
, bool pending_decap
,
7860 struct ofpbuf
*encap_data
)
7862 enum slow_path_reason slow1
, slow2
;
7863 bool mpls_done
= false;
7865 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
7866 pending_encap
, pending_decap
, encap_data
);
7867 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
7868 /* Make packet a non-MPLS packet before committing L3/4 actions,
7869 * which would otherwise do nothing. */
7870 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
7871 commit_mpls_action(flow
, base
, odp_actions
);
7874 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
7875 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
7876 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
7877 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
7879 commit_mpls_action(flow
, base
, odp_actions
);
7881 commit_vlan_action(flow
, base
, odp_actions
, wc
);
7882 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
7883 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
7885 return slow1
? slow1
: slow2
;