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 <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
29 #include "byte-order.h"
32 #include "openvswitch/dynamic-string.h"
35 #include "openvswitch/ofpbuf.h"
39 #include "tun-metadata.h"
40 #include "unaligned.h"
43 #include "openvswitch/vlog.h"
44 #include "openvswitch/match.h"
46 VLOG_DEFINE_THIS_MODULE(odp_util
);
48 /* The interface between userspace and kernel uses an "OVS_*" prefix.
49 * Since this is fairly non-specific for the OVS userspace components,
50 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
51 * interactions with the datapath.
54 /* The set of characters that may separate one action or one key attribute
56 static const char *delimiters
= ", \t\r\n";
57 static const char *delimiters_end
= ", \t\r\n)";
61 const struct attr_len_tbl
*next
;
64 #define ATTR_LEN_INVALID -1
65 #define ATTR_LEN_VARIABLE -2
66 #define ATTR_LEN_NESTED -3
68 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
69 struct ofpbuf
*, struct ofpbuf
*);
70 static void format_odp_key_attr(const struct nlattr
*a
,
71 const struct nlattr
*ma
,
72 const struct hmap
*portno_names
, struct ds
*ds
,
76 struct geneve_opt d
[63];
80 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
81 struct geneve_scan
*mask
);
82 static void format_geneve_opts(const struct geneve_opt
*opt
,
83 const struct geneve_opt
*mask
, int opts_len
,
84 struct ds
*, bool verbose
);
86 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
87 int max
, struct ofpbuf
*,
88 const struct nlattr
*key
);
89 static void format_u128(struct ds
*d
, const ovs_32aligned_u128
*key
,
90 const ovs_32aligned_u128
*mask
, bool verbose
);
91 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
93 static int parse_odp_action(const char *s
, const struct simap
*port_names
,
94 struct ofpbuf
*actions
);
96 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
99 * - For an action whose argument has a fixed length, returned that
100 * nonnegative length in bytes.
102 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
104 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
106 odp_action_len(uint16_t type
)
108 if (type
> OVS_ACTION_ATTR_MAX
) {
112 switch ((enum ovs_action_attr
) type
) {
113 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
114 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
115 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
116 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
117 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
118 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
119 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
120 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
121 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
122 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
123 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
124 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
125 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
126 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
127 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
128 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
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_ENCAP_NSH
: return ATTR_LEN_VARIABLE
;
133 case OVS_ACTION_ATTR_DECAP_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
, ",mdtype=%d", key
->mdtype
);
258 ds_put_format(ds
, ",np=%d", key
->np
);
259 ds_put_format(ds
, ",spi=0x%x",
260 (ntohl(key
->path_hdr
) & NSH_SPI_MASK
) >> NSH_SPI_SHIFT
);
261 ds_put_format(ds
, ",si=%d",
262 (ntohl(key
->path_hdr
) & NSH_SI_MASK
) >> NSH_SI_SHIFT
);
264 switch (key
->mdtype
) {
266 for (int i
= 0; i
< 4; i
++) {
267 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->c
[i
]));
272 /* No support for matching other metadata formats yet. */
278 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
279 uint8_t value
, uint8_t mask
)
285 ds_put_format(s
, "%s=", name
);
286 if (mask
== UINT8_MAX
) {
287 ds_put_format(s
, "%"PRIu8
, value
);
289 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
296 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
297 ovs_be32 value
, ovs_be32 mask
)
299 if (mask
!= htonl(0)) {
303 ds_put_format(s
, "%s=", name
);
304 if (mask
== OVS_BE32_MAX
) {
305 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
307 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
308 ntohl(value
), ntohl(mask
));
315 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
316 const struct ovs_key_nsh
*mask
)
319 format_nsh_key(ds
, key
);
322 uint32_t spi
= (ntohl(key
->path_hdr
) & NSH_SPI_MASK
) >> NSH_SPI_SHIFT
;
323 uint32_t spi_mask
= (ntohl(mask
->path_hdr
) & NSH_SPI_MASK
) >>
325 if (spi_mask
== 0x00ffffff) {
326 spi_mask
= UINT32_MAX
;
328 uint8_t si
= (ntohl(key
->path_hdr
) & NSH_SI_MASK
) >> NSH_SI_SHIFT
;
329 uint8_t si_mask
= (ntohl(mask
->path_hdr
) & NSH_SI_MASK
) >>
332 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
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
->c
[0], mask
->c
[0]);
338 format_be32_masked(ds
, &first
, "c2", key
->c
[1], mask
->c
[1]);
339 format_be32_masked(ds
, &first
, "c3", key
->c
[2], mask
->c
[2]);
340 format_be32_masked(ds
, &first
, "c4", key
->c
[3], mask
->c
[3]);
345 format_odp_encap_nsh_action(struct ds
*ds
,
346 const struct ovs_action_encap_nsh
*encap_nsh
)
348 uint32_t path_hdr
= ntohl(encap_nsh
->path_hdr
);
349 uint32_t spi
= (path_hdr
& NSH_SPI_MASK
) >> NSH_SPI_SHIFT
;
350 uint8_t si
= (path_hdr
& NSH_SI_MASK
) >> NSH_SI_SHIFT
;
352 ds_put_cstr(ds
, "encap_nsh(");
353 ds_put_format(ds
, "flags=%d", encap_nsh
->flags
);
354 ds_put_format(ds
, ",mdtype=%d", encap_nsh
->mdtype
);
355 ds_put_format(ds
, ",np=%d", encap_nsh
->np
);
356 ds_put_format(ds
, ",spi=0x%x", spi
);
357 ds_put_format(ds
, ",si=%d", si
);
358 switch (encap_nsh
->mdtype
) {
360 struct nsh_md1_ctx
*md1_ctx
=
361 ALIGNED_CAST(struct nsh_md1_ctx
*, encap_nsh
->metadata
);
362 for (int i
= 0; i
< 4; i
++) {
363 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
364 ntohl(get_16aligned_be32(&md1_ctx
->c
[i
])));
369 ds_put_cstr(ds
, ",md2=");
370 ds_put_hex(ds
, encap_nsh
->metadata
, encap_nsh
->mdlen
);
375 ds_put_format(ds
, ")");
379 slow_path_reason_to_string(uint32_t reason
)
381 switch ((enum slow_path_reason
) reason
) {
382 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
391 slow_path_reason_to_explanation(enum slow_path_reason reason
)
394 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
403 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
404 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
406 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
407 res_flags
, allowed
, res_mask
);
411 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
412 const struct hmap
*portno_names
)
414 static const struct nl_policy ovs_userspace_policy
[] = {
415 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
416 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
418 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
420 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
423 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
424 const struct nlattr
*userdata_attr
;
425 const struct nlattr
*tunnel_out_port_attr
;
427 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
428 ds_put_cstr(ds
, "userspace(error)");
432 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
433 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
435 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
438 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
439 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
440 bool userdata_unspec
= true;
441 union user_action_cookie cookie
;
443 if (userdata_len
>= sizeof cookie
.type
444 && userdata_len
<= sizeof cookie
) {
446 memset(&cookie
, 0, sizeof cookie
);
447 memcpy(&cookie
, userdata
, userdata_len
);
449 userdata_unspec
= false;
451 if (userdata_len
== sizeof cookie
.sflow
452 && cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
453 ds_put_format(ds
, ",sFlow("
454 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
455 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
456 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
457 cookie
.sflow
.output
);
458 } else if (userdata_len
== sizeof cookie
.slow_path
459 && cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
460 ds_put_cstr(ds
, ",slow_path(");
461 format_flags(ds
, slow_path_reason_to_string
,
462 cookie
.slow_path
.reason
, ',');
463 ds_put_format(ds
, ")");
464 } else if (userdata_len
== sizeof cookie
.flow_sample
465 && cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
466 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
467 ",collector_set_id=%"PRIu32
468 ",obs_domain_id=%"PRIu32
469 ",obs_point_id=%"PRIu32
471 cookie
.flow_sample
.probability
,
472 cookie
.flow_sample
.collector_set_id
,
473 cookie
.flow_sample
.obs_domain_id
,
474 cookie
.flow_sample
.obs_point_id
);
475 odp_portno_name_format(portno_names
,
476 cookie
.flow_sample
.output_odp_port
, ds
);
477 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
478 ds_put_cstr(ds
, ",ingress");
479 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
480 ds_put_cstr(ds
, ",egress");
482 ds_put_char(ds
, ')');
483 } else if (userdata_len
>= sizeof cookie
.ipfix
484 && cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
485 ds_put_format(ds
, ",ipfix(output_port=");
486 odp_portno_name_format(portno_names
,
487 cookie
.ipfix
.output_odp_port
, ds
);
488 ds_put_char(ds
, ')');
490 userdata_unspec
= true;
494 if (userdata_unspec
) {
496 ds_put_format(ds
, ",userdata(");
497 for (i
= 0; i
< userdata_len
; i
++) {
498 ds_put_format(ds
, "%02x", userdata
[i
]);
500 ds_put_char(ds
, ')');
504 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
505 ds_put_cstr(ds
, ",actions");
508 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
509 if (tunnel_out_port_attr
) {
510 ds_put_format(ds
, ",tunnel_out_port=");
511 odp_portno_name_format(portno_names
,
512 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
515 ds_put_char(ds
, ')');
519 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
521 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
522 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
523 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
524 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
526 ds_put_char(ds
, ',');
528 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
529 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
530 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
531 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
533 ds_put_char(ds
, ',');
535 if (!(tci
& htons(VLAN_CFI
))) {
536 ds_put_cstr(ds
, "cfi=0");
537 ds_put_char(ds
, ',');
543 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
545 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
546 mpls_lse_to_label(mpls_lse
),
547 mpls_lse_to_tc(mpls_lse
),
548 mpls_lse_to_ttl(mpls_lse
),
549 mpls_lse_to_bos(mpls_lse
));
553 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
554 const struct ovs_key_mpls
*mpls_mask
, int n
)
556 for (int i
= 0; i
< n
; i
++) {
557 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
559 if (mpls_mask
== NULL
) {
560 format_mpls_lse(ds
, key
);
562 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
564 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
565 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
566 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
567 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
568 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
570 ds_put_char(ds
, ',');
576 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
578 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
582 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
584 ds_put_format(ds
, "hash(");
586 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
587 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
589 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
592 ds_put_format(ds
, ")");
596 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
598 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
599 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
600 ntohs(udp
->udp_csum
));
606 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
608 const struct eth_header
*eth
;
611 const struct udp_header
*udp
;
613 eth
= (const struct eth_header
*)data
->header
;
618 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
619 data
->header_len
, data
->tnl_type
);
620 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
621 ds_put_format(ds
, ",src=");
622 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
623 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
625 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
627 const struct ip_header
*ip
= l3
;
628 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
629 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
630 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
631 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
632 ip
->ip_proto
, ip
->ip_tos
,
634 ntohs(ip
->ip_frag_off
));
637 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
638 struct in6_addr src
, dst
;
639 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
640 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
641 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
643 ds_put_format(ds
, "ipv6(src=");
644 ipv6_format_addr(&src
, ds
);
645 ds_put_format(ds
, ",dst=");
646 ipv6_format_addr(&dst
, ds
);
647 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
648 ",hlimit=%"PRIu8
"),",
649 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
650 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
654 udp
= (const struct udp_header
*) l4
;
656 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
657 const struct vxlanhdr
*vxh
;
659 vxh
= format_udp_tnl_push_header(ds
, udp
);
661 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
662 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
663 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
664 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
665 const struct genevehdr
*gnh
;
667 gnh
= format_udp_tnl_push_header(ds
, udp
);
669 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
670 gnh
->oam
? "oam," : "",
671 gnh
->critical
? "crit," : "",
672 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
675 ds_put_cstr(ds
, ",options(");
676 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
678 ds_put_char(ds
, ')');
681 ds_put_char(ds
, ')');
682 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
) {
683 const struct gre_base_hdr
*greh
;
684 ovs_16aligned_be32
*options
;
686 greh
= (const struct gre_base_hdr
*) l4
;
688 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
689 ntohs(greh
->flags
), ntohs(greh
->protocol
));
690 options
= (ovs_16aligned_be32
*)(greh
+ 1);
691 if (greh
->flags
& htons(GRE_CSUM
)) {
692 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
695 if (greh
->flags
& htons(GRE_KEY
)) {
696 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
699 if (greh
->flags
& htons(GRE_SEQ
)) {
700 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
703 ds_put_format(ds
, ")");
705 ds_put_format(ds
, ")");
709 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
710 const struct hmap
*portno_names
)
712 struct ovs_action_push_tnl
*data
;
714 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
716 ds_put_cstr(ds
, "tnl_push(tnl_port(");
717 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
718 ds_put_cstr(ds
, "),");
719 format_odp_tnl_push_header(ds
, data
);
720 ds_put_format(ds
, ",out_port(");
721 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
722 ds_put_cstr(ds
, "))");
725 static const struct nl_policy ovs_nat_policy
[] = {
726 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
727 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
728 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
729 .min_len
= sizeof(struct in_addr
),
730 .max_len
= sizeof(struct in6_addr
)},
731 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
732 .min_len
= sizeof(struct in_addr
),
733 .max_len
= sizeof(struct in6_addr
)},
734 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
735 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
736 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
737 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
738 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
742 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
744 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
746 ovs_be32 ip_min
, ip_max
;
747 struct in6_addr ip6_min
, ip6_max
;
748 uint16_t proto_min
, proto_max
;
750 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
751 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
754 /* If no type, then nothing else either. */
755 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
756 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
757 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
758 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
759 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
760 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
763 /* Both SNAT & DNAT may not be specified. */
764 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
765 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
768 /* proto may not appear without ip. */
769 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
770 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
773 /* MAX may not appear without MIN. */
774 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
775 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
776 ds_put_cstr(ds
, "nat(error: range max without min.)");
779 /* Address sizes must match. */
780 if ((a
[OVS_NAT_ATTR_IP_MIN
]
781 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
782 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
783 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
784 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
785 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
786 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
790 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
791 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
792 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
793 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
794 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
795 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
796 if (addr_len
== sizeof ip6_min
) {
797 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
798 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
800 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
801 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
804 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
805 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
806 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
807 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
809 if ((addr_len
== sizeof(ovs_be32
)
810 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
811 || (addr_len
== sizeof(struct in6_addr
)
812 && !ipv6_mask_is_any(&ip6_max
)
813 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
814 || (proto_max
&& proto_min
> proto_max
)) {
815 ds_put_cstr(ds
, "nat(range error)");
819 ds_put_cstr(ds
, "nat");
820 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
821 ds_put_char(ds
, '(');
822 if (a
[OVS_NAT_ATTR_SRC
]) {
823 ds_put_cstr(ds
, "src");
824 } else if (a
[OVS_NAT_ATTR_DST
]) {
825 ds_put_cstr(ds
, "dst");
829 ds_put_cstr(ds
, "=");
831 if (addr_len
== sizeof ip_min
) {
832 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
834 if (ip_max
&& ip_max
!= ip_min
) {
835 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
837 } else if (addr_len
== sizeof ip6_min
) {
838 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
840 if (!ipv6_mask_is_any(&ip6_max
) &&
841 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
842 ds_put_char(ds
, '-');
843 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
847 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
849 if (proto_max
&& proto_max
!= proto_min
) {
850 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
854 ds_put_char(ds
, ',');
855 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
856 ds_put_cstr(ds
, "persistent,");
858 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
859 ds_put_cstr(ds
, "hash,");
861 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
862 ds_put_cstr(ds
, "random,");
865 ds_put_char(ds
, ')');
869 static const struct nl_policy ovs_conntrack_policy
[] = {
870 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
871 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
872 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
873 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
874 .min_len
= sizeof(uint32_t) * 2 },
875 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
876 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
877 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
878 .min_len
= 1, .max_len
= 16 },
879 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
883 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
885 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
887 ovs_32aligned_u128 value
;
888 ovs_32aligned_u128 mask
;
890 const uint32_t *mark
;
894 const struct nlattr
*nat
;
896 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
897 ds_put_cstr(ds
, "ct(error)");
901 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
902 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
903 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
904 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
905 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
906 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
907 nat
= a
[OVS_CT_ATTR_NAT
];
909 ds_put_format(ds
, "ct");
910 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
911 ds_put_cstr(ds
, "(");
913 ds_put_format(ds
, "commit,");
916 ds_put_format(ds
, "force_commit,");
919 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
922 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
926 ds_put_format(ds
, "label=");
927 format_u128(ds
, &label
->value
, &label
->mask
, true);
928 ds_put_char(ds
, ',');
931 ds_put_format(ds
, "helper=%s,", helper
);
934 format_odp_ct_nat(ds
, nat
);
937 ds_put_cstr(ds
, ")");
942 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
943 const struct hmap
*portno_names
)
946 enum ovs_action_attr type
= nl_attr_type(a
);
949 expected_len
= odp_action_len(nl_attr_type(a
));
950 if (expected_len
!= ATTR_LEN_VARIABLE
&&
951 nl_attr_get_size(a
) != expected_len
) {
952 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
953 nl_attr_get_size(a
), expected_len
);
954 format_generic_odp_action(ds
, a
);
959 case OVS_ACTION_ATTR_METER
:
960 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
962 case OVS_ACTION_ATTR_OUTPUT
:
963 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
965 case OVS_ACTION_ATTR_TRUNC
: {
966 const struct ovs_action_trunc
*trunc
=
967 nl_attr_get_unspec(a
, sizeof *trunc
);
969 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
972 case OVS_ACTION_ATTR_TUNNEL_POP
:
973 ds_put_cstr(ds
, "tnl_pop(");
974 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
975 ds_put_char(ds
, ')');
977 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
978 format_odp_tnl_push_action(ds
, a
, portno_names
);
980 case OVS_ACTION_ATTR_USERSPACE
:
981 format_odp_userspace_action(ds
, a
, portno_names
);
983 case OVS_ACTION_ATTR_RECIRC
:
984 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
986 case OVS_ACTION_ATTR_HASH
:
987 format_odp_hash_action(ds
, nl_attr_get(a
));
989 case OVS_ACTION_ATTR_SET_MASKED
:
991 size
= nl_attr_get_size(a
) / 2;
992 ds_put_cstr(ds
, "set(");
994 /* Masked set action not supported for tunnel key, which is bigger. */
995 if (size
<= sizeof(struct ovs_key_ipv6
)) {
996 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
997 sizeof(struct nlattr
))];
998 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
999 sizeof(struct nlattr
))];
1001 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1002 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1003 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1004 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1005 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1007 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1009 ds_put_cstr(ds
, ")");
1011 case OVS_ACTION_ATTR_SET
:
1012 ds_put_cstr(ds
, "set(");
1013 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1014 ds_put_cstr(ds
, ")");
1016 case OVS_ACTION_ATTR_PUSH_ETH
: {
1017 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1018 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1019 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1020 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1023 case OVS_ACTION_ATTR_POP_ETH
:
1024 ds_put_cstr(ds
, "pop_eth");
1026 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1027 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1028 ds_put_cstr(ds
, "push_vlan(");
1029 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1030 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1032 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1033 ds_put_char(ds
, ')');
1036 case OVS_ACTION_ATTR_POP_VLAN
:
1037 ds_put_cstr(ds
, "pop_vlan");
1039 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1040 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1041 ds_put_cstr(ds
, "push_mpls(");
1042 format_mpls_lse(ds
, mpls
->mpls_lse
);
1043 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1046 case OVS_ACTION_ATTR_POP_MPLS
: {
1047 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1048 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1051 case OVS_ACTION_ATTR_SAMPLE
:
1052 format_odp_sample_action(ds
, a
, portno_names
);
1054 case OVS_ACTION_ATTR_CT
:
1055 format_odp_conntrack_action(ds
, a
);
1057 case OVS_ACTION_ATTR_CLONE
:
1058 format_odp_clone_action(ds
, a
, portno_names
);
1060 case OVS_ACTION_ATTR_ENCAP_NSH
:
1061 format_odp_encap_nsh_action(ds
, nl_attr_get(a
));
1063 case OVS_ACTION_ATTR_DECAP_NSH
:
1064 ds_put_cstr(ds
, "decap_nsh()");
1066 case OVS_ACTION_ATTR_UNSPEC
:
1067 case __OVS_ACTION_ATTR_MAX
:
1069 format_generic_odp_action(ds
, a
);
1075 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1076 size_t actions_len
, const struct hmap
*portno_names
)
1079 const struct nlattr
*a
;
1082 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1084 ds_put_char(ds
, ',');
1086 format_odp_action(ds
, a
, portno_names
);
1091 if (left
== actions_len
) {
1092 ds_put_cstr(ds
, "<empty>");
1094 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1095 for (i
= 0; i
< left
; i
++) {
1096 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1098 ds_put_char(ds
, ')');
1101 ds_put_cstr(ds
, "drop");
1105 /* Separate out parse_odp_userspace_action() function. */
1107 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1110 union user_action_cookie cookie
;
1112 odp_port_t tunnel_out_port
;
1114 void *user_data
= NULL
;
1115 size_t user_data_size
= 0;
1116 bool include_actions
= false;
1119 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1123 ofpbuf_init(&buf
, 16);
1127 uint32_t probability
;
1128 uint32_t collector_set_id
;
1129 uint32_t obs_domain_id
;
1130 uint32_t obs_point_id
;
1133 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1134 "pcp=%i,output=%"SCNi32
")%n",
1135 &vid
, &pcp
, &output
, &n1
)) {
1139 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1144 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1145 cookie
.sflow
.vlan_tci
= htons(tci
);
1146 cookie
.sflow
.output
= output
;
1147 user_data
= &cookie
;
1148 user_data_size
= sizeof cookie
.sflow
;
1149 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1152 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1153 cookie
.slow_path
.unused
= 0;
1154 cookie
.slow_path
.reason
= 0;
1156 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1157 &cookie
.slow_path
.reason
,
1158 SLOW_PATH_REASON_MASK
, NULL
);
1159 if (res
< 0 || s
[n
+ res
] != ')') {
1164 user_data
= &cookie
;
1165 user_data_size
= sizeof cookie
.slow_path
;
1166 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1167 "collector_set_id=%"SCNi32
","
1168 "obs_domain_id=%"SCNi32
","
1169 "obs_point_id=%"SCNi32
","
1170 "output_port=%"SCNi32
"%n",
1171 &probability
, &collector_set_id
,
1172 &obs_domain_id
, &obs_point_id
,
1176 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1177 cookie
.flow_sample
.probability
= probability
;
1178 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1179 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1180 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1181 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1182 user_data
= &cookie
;
1183 user_data_size
= sizeof cookie
.flow_sample
;
1185 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1186 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1188 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1189 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1192 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1199 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1202 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1203 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1204 user_data
= &cookie
;
1205 user_data_size
= sizeof cookie
.ipfix
;
1206 } else if (ovs_scan(&s
[n
], ",userdata(%n",
1211 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1212 if (end
[0] != ')') {
1216 user_data
= buf
.data
;
1217 user_data_size
= buf
.size
;
1224 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1226 include_actions
= true;
1232 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1233 &tunnel_out_port
, &n1
)) {
1234 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1235 tunnel_out_port
, include_actions
, actions
);
1238 } else if (s
[n
] == ')') {
1239 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1240 ODPP_NONE
, include_actions
, actions
);
1247 struct ovs_action_push_eth push
;
1251 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1252 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1253 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1254 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1257 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1258 &push
, sizeof push
);
1265 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1266 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1273 ofpbuf_uninit(&buf
);
1278 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1280 struct eth_header
*eth
;
1281 struct ip_header
*ip
;
1282 struct ovs_16aligned_ip6_hdr
*ip6
;
1283 struct udp_header
*udp
;
1284 struct gre_base_hdr
*greh
;
1285 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
;
1287 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0;
1291 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1294 eth
= (struct eth_header
*) data
->header
;
1295 l3
= (struct ip_header
*) (eth
+ 1);
1296 ip
= (struct ip_header
*) l3
;
1297 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1298 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1299 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1302 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1306 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1307 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1310 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1313 eth
->eth_type
= htons(dl_type
);
1315 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1317 uint16_t ip_frag_off
;
1318 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1319 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1322 &ip
->ip_proto
, &ip
->ip_tos
,
1323 &ip
->ip_ttl
, &ip_frag_off
)) {
1326 put_16aligned_be32(&ip
->ip_src
, sip
);
1327 put_16aligned_be32(&ip
->ip_dst
, dip
);
1328 ip
->ip_frag_off
= htons(ip_frag_off
);
1329 ip_len
= sizeof *ip
;
1331 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1332 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1333 struct in6_addr sip6
, dip6
;
1336 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1337 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1338 ",hlimit=%"SCNi8
"),",
1339 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1340 &tclass
, &ip6
->ip6_hlim
)
1341 || (label
& ~IPV6_LABEL_MASK
) != 0
1342 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1343 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1346 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1347 htonl(tclass
<< 20) | htonl(label
));
1348 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1349 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1350 ip_len
= sizeof *ip6
;
1354 l4
= ((uint8_t *) l3
+ ip_len
);
1355 udp
= (struct udp_header
*) l4
;
1356 greh
= (struct gre_base_hdr
*) l4
;
1357 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1358 &udp_src
, &udp_dst
, &csum
)) {
1359 uint32_t vx_flags
, vni
;
1361 udp
->udp_src
= htons(udp_src
);
1362 udp
->udp_dst
= htons(udp_dst
);
1364 udp
->udp_csum
= htons(csum
);
1366 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1368 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1370 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1371 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1372 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1373 header_len
= sizeof *eth
+ ip_len
+
1374 sizeof *udp
+ sizeof *vxh
;
1375 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1376 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1378 memset(gnh
, 0, sizeof *gnh
);
1379 header_len
= sizeof *eth
+ ip_len
+
1380 sizeof *udp
+ sizeof *gnh
;
1382 if (ovs_scan_len(s
, &n
, "oam,")) {
1385 if (ovs_scan_len(s
, &n
, "crit,")) {
1388 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1391 if (ovs_scan_len(s
, &n
, ",options(")) {
1392 struct geneve_scan options
;
1395 memset(&options
, 0, sizeof options
);
1396 len
= scan_geneve(s
+ n
, &options
, NULL
);
1401 memcpy(gnh
->options
, options
.d
, options
.len
);
1402 gnh
->opt_len
= options
.len
/ 4;
1403 header_len
+= options
.len
;
1407 if (!ovs_scan_len(s
, &n
, "))")) {
1411 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1412 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1413 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1417 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1418 &gre_flags
, &gre_proto
)){
1420 tnl_type
= OVS_VPORT_TYPE_GRE
;
1421 greh
->flags
= htons(gre_flags
);
1422 greh
->protocol
= htons(gre_proto
);
1423 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1425 if (greh
->flags
& htons(GRE_CSUM
)) {
1426 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1430 memset(options
, 0, sizeof *options
);
1431 *((ovs_be16
*)options
) = htons(csum
);
1434 if (greh
->flags
& htons(GRE_KEY
)) {
1437 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1441 put_16aligned_be32(options
, htonl(key
));
1444 if (greh
->flags
& htons(GRE_SEQ
)) {
1447 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1450 put_16aligned_be32(options
, htonl(seq
));
1454 if (!ovs_scan_len(s
, &n
, "))")) {
1458 header_len
= sizeof *eth
+ ip_len
+
1459 ((uint8_t *) options
- (uint8_t *) greh
);
1464 /* check tunnel meta data. */
1465 if (data
->tnl_type
!= tnl_type
) {
1468 if (data
->header_len
!= header_len
) {
1473 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1480 struct ct_nat_params
{
1486 struct in6_addr ip6
;
1490 struct in6_addr ip6
;
1500 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1502 if (ovs_scan_len(s
, n
, "=")) {
1503 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1504 struct in6_addr ipv6
;
1506 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1507 p
->addr_len
= sizeof p
->addr_min
.ip
;
1508 if (ovs_scan_len(s
, n
, "-")) {
1509 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1510 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1514 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1515 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1516 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1517 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1518 p
->addr_min
.ip6
= ipv6
;
1519 if (ovs_scan_len(s
, n
, "-")) {
1520 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1521 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1522 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1523 p
->addr_max
.ip6
= ipv6
;
1531 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1532 if (ovs_scan_len(s
, n
, "-")) {
1533 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1543 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1547 if (ovs_scan_len(s
, &n
, "nat")) {
1548 memset(p
, 0, sizeof *p
);
1550 if (ovs_scan_len(s
, &n
, "(")) {
1554 end
= strchr(s
+ n
, ')');
1561 n
+= strspn(s
+ n
, delimiters
);
1562 if (ovs_scan_len(s
, &n
, "src")) {
1563 int err
= scan_ct_nat_range(s
, &n
, p
);
1570 if (ovs_scan_len(s
, &n
, "dst")) {
1571 int err
= scan_ct_nat_range(s
, &n
, p
);
1578 if (ovs_scan_len(s
, &n
, "persistent")) {
1579 p
->persistent
= true;
1582 if (ovs_scan_len(s
, &n
, "hash")) {
1583 p
->proto_hash
= true;
1586 if (ovs_scan_len(s
, &n
, "random")) {
1587 p
->proto_random
= true;
1593 if (p
->snat
&& p
->dnat
) {
1596 if ((p
->addr_len
!= 0 &&
1597 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1598 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1599 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1602 if (p
->proto_hash
&& p
->proto_random
) {
1612 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1614 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1617 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1618 } else if (p
->dnat
) {
1619 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1623 if (p
->addr_len
!= 0) {
1624 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1626 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1627 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1631 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1632 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1633 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1636 if (p
->persistent
) {
1637 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1639 if (p
->proto_hash
) {
1640 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1642 if (p
->proto_random
) {
1643 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1647 nl_msg_end_nested(actions
, start
);
1651 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1655 if (ovs_scan(s
, "ct")) {
1656 const char *helper
= NULL
;
1657 size_t helper_len
= 0;
1658 bool commit
= false;
1659 bool force_commit
= false;
1664 } ct_mark
= { 0, 0 };
1669 struct ct_nat_params nat_params
;
1670 bool have_nat
= false;
1674 memset(&ct_label
, 0, sizeof(ct_label
));
1677 if (ovs_scan(s
, "(")) {
1680 end
= strchr(s
, ')');
1688 s
+= strspn(s
, delimiters
);
1689 if (ovs_scan(s
, "commit%n", &n
)) {
1694 if (ovs_scan(s
, "force_commit%n", &n
)) {
1695 force_commit
= true;
1699 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1703 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1706 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1709 ct_mark
.mask
= UINT32_MAX
;
1713 if (ovs_scan(s
, "label=%n", &n
)) {
1717 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1724 if (ovs_scan(s
, "helper=%n", &n
)) {
1726 helper_len
= strcspn(s
, delimiters_end
);
1727 if (!helper_len
|| helper_len
> 15) {
1735 n
= scan_ct_nat(s
, &nat_params
);
1740 /* end points to the end of the nested, nat action.
1741 * find the real end. */
1744 /* Nothing matched. */
1749 if (commit
&& force_commit
) {
1753 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1755 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1756 } else if (force_commit
) {
1757 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1760 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1763 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1766 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1767 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1771 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1775 nl_msg_put_ct_nat(&nat_params
, actions
);
1777 nl_msg_end_nested(actions
, start
);
1784 parse_odp_encap_nsh_action(const char *s
, struct ofpbuf
*actions
)
1788 struct ovs_action_encap_nsh encap_nsh
;
1793 if (!ovs_scan_len(s
, &n
, "encap_nsh(")) {
1798 /* The default is NSH_M_TYPE1 */
1799 encap_nsh
.flags
= 0;
1800 encap_nsh
.mdtype
= NSH_M_TYPE1
;
1801 encap_nsh
.mdlen
= NSH_M_TYPE1_MDLEN
;
1802 encap_nsh
.path_hdr
= htonl(255);
1803 memset(encap_nsh
.metadata
, 0, NSH_M_TYPE1_MDLEN
);
1806 n
+= strspn(s
+ n
, delimiters
);
1811 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &encap_nsh
.flags
)) {
1814 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &encap_nsh
.mdtype
)) {
1815 switch (encap_nsh
.mdtype
) {
1817 /* This is the default format. */;
1820 /* Length will be updated later. */
1821 encap_nsh
.mdlen
= 0;
1829 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &encap_nsh
.np
)) {
1832 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
1833 encap_nsh
.path_hdr
=
1834 htonl(((spi
<< NSH_SPI_SHIFT
) & NSH_SPI_MASK
) |
1835 (ntohl(encap_nsh
.path_hdr
) & ~NSH_SPI_MASK
));
1838 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
1839 encap_nsh
.path_hdr
=
1840 htonl((si
<< NSH_SI_SHIFT
) |
1841 (ntohl(encap_nsh
.path_hdr
) & ~NSH_SI_MASK
));
1844 if (encap_nsh
.mdtype
== NSH_M_TYPE1
) {
1845 struct nsh_md1_ctx
*md1
=
1846 ALIGNED_CAST(struct nsh_md1_ctx
*, encap_nsh
.metadata
);
1847 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
1848 put_16aligned_be32(&md1
->c
[0], htonl(cd
));
1851 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
1852 put_16aligned_be32(&md1
->c
[1], htonl(cd
));
1855 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
1856 put_16aligned_be32(&md1
->c
[2], htonl(cd
));
1859 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
1860 put_16aligned_be32(&md1
->c
[3], htonl(cd
));
1864 else if (encap_nsh
.mdtype
== NSH_M_TYPE2
) {
1868 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)) {
1869 ofpbuf_use_stub(&b
, encap_nsh
.metadata
,
1870 OVS_ENCAP_NSH_MAX_MD_LEN
);
1871 ofpbuf_put_hex(&b
, buf
, &mdlen
);
1872 encap_nsh
.mdlen
= mdlen
;
1882 size_t size
= offsetof(struct ovs_action_encap_nsh
, metadata
)
1883 + ROUND_UP(encap_nsh
.mdlen
, 4);
1884 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_ENCAP_NSH
,
1891 parse_action_list(const char *s
, const struct simap
*port_names
,
1892 struct ofpbuf
*actions
)
1899 n
+= strspn(s
+ n
, delimiters
);
1903 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
1914 parse_odp_action(const char *s
, const struct simap
*port_names
,
1915 struct ofpbuf
*actions
)
1921 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
1922 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
1931 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
1932 struct ovs_action_trunc
*trunc
;
1934 trunc
= nl_msg_put_unspec_uninit(actions
,
1935 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
1936 trunc
->max_len
= max_len
;
1942 int len
= strcspn(s
, delimiters
);
1943 struct simap_node
*node
;
1945 node
= simap_find_len(port_names
, s
, len
);
1947 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
1956 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
1957 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
1962 if (!strncmp(s
, "userspace(", 10)) {
1963 return parse_odp_userspace_action(s
, actions
);
1966 if (!strncmp(s
, "set(", 4)) {
1969 struct nlattr mask
[128 / sizeof(struct nlattr
)];
1970 struct ofpbuf maskbuf
;
1971 struct nlattr
*nested
, *key
;
1974 /* 'mask' is big enough to hold any key. */
1975 ofpbuf_use_stack(&maskbuf
, mask
, sizeof mask
);
1977 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
1978 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
1982 if (s
[retval
+ 4] != ')') {
1986 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
1989 size
= nl_attr_get_size(mask
);
1990 if (size
== nl_attr_get_size(key
)) {
1991 /* Change to masked set action if not fully masked. */
1992 if (!is_all_ones(mask
+ 1, size
)) {
1993 /* Remove padding of eariler key payload */
1994 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
1996 /* Put mask payload right after key payload */
1997 key
->nla_len
+= size
;
1998 ofpbuf_put(actions
, mask
+ 1, size
);
2000 /* Add new padding as needed */
2001 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2004 /* 'actions' may have been reallocated by ofpbuf_put(). */
2005 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2006 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2010 nl_msg_end_nested(actions
, start_ofs
);
2015 struct ovs_action_push_vlan push
;
2016 int tpid
= ETH_TYPE_VLAN
;
2021 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2022 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2023 &vid
, &pcp
, &cfi
, &n
)
2024 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2025 &tpid
, &vid
, &pcp
, &n
)
2026 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2027 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2028 push
.vlan_tpid
= htons(tpid
);
2029 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2030 | (pcp
<< VLAN_PCP_SHIFT
)
2031 | (cfi
? VLAN_CFI
: 0));
2032 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2033 &push
, sizeof push
);
2039 if (!strncmp(s
, "pop_vlan", 8)) {
2040 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2045 unsigned long long int meter_id
;
2048 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2049 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2058 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2059 && percentage
>= 0. && percentage
<= 100.0) {
2060 size_t sample_ofs
, actions_ofs
;
2063 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2064 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2065 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2066 (probability
<= 0 ? 0
2067 : probability
>= UINT32_MAX
? UINT32_MAX
2070 actions_ofs
= nl_msg_start_nested(actions
,
2071 OVS_SAMPLE_ATTR_ACTIONS
);
2072 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2077 nl_msg_end_nested(actions
, actions_ofs
);
2078 nl_msg_end_nested(actions
, sample_ofs
);
2080 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2085 if (!strncmp(s
, "clone(", 6)) {
2089 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2090 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2095 nl_msg_end_nested(actions
, actions_ofs
);
2101 if (!strncmp(s
, "encap_nsh(", 10)) {
2102 int retval
= parse_odp_encap_nsh_action(s
, actions
);
2112 if (ovs_scan(s
, "decap_nsh()%n", &n
)) {
2113 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_DECAP_NSH
);
2122 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2123 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2131 retval
= parse_conntrack_action(s
, actions
);
2138 struct ovs_action_push_tnl data
;
2141 n
= ovs_parse_tnl_push(s
, &data
);
2143 odp_put_tnl_push_action(actions
, &data
);
2152 /* Parses the string representation of datapath actions, in the format output
2153 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2154 * value. On success, the ODP actions are appended to 'actions' as a series of
2155 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2156 * way, 'actions''s data might be reallocated. */
2158 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2159 struct ofpbuf
*actions
)
2163 if (!strcasecmp(s
, "drop")) {
2167 old_size
= actions
->size
;
2171 s
+= strspn(s
, delimiters
);
2176 retval
= parse_odp_action(s
, port_names
, actions
);
2177 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2178 actions
->size
= old_size
;
2187 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2188 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2191 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2192 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2193 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2194 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2195 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2196 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2197 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2198 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2199 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2200 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2201 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2202 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2203 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2204 .next
= ovs_vxlan_ext_attr_lens
,
2205 .next_max
= OVS_VXLAN_EXT_MAX
},
2206 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2207 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2210 static const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2211 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2212 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2213 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2214 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2215 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2216 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2217 .next
= ovs_tun_key_attr_lens
,
2218 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2219 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2220 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2221 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2222 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2223 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2224 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2225 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2226 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2227 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2228 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2229 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2230 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2231 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2232 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2233 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2234 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2235 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2236 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2237 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2238 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2239 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2240 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2241 [OVS_KEY_ATTR_NSH
] = { .len
= sizeof(struct ovs_key_nsh
) },
2244 /* Returns the correct length of the payload for a flow key attribute of the
2245 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2246 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2247 * payload is a nested type. */
2249 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2251 if (type
> max_type
) {
2252 return ATTR_LEN_INVALID
;
2255 return tbl
[type
].len
;
2259 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2261 size_t len
= nl_attr_get_size(a
);
2263 const uint8_t *unspec
;
2266 unspec
= nl_attr_get(a
);
2267 for (i
= 0; i
< len
; i
++) {
2269 ds_put_char(ds
, ' ');
2271 ds_put_format(ds
, "%02x", unspec
[i
]);
2277 ovs_frag_type_to_string(enum ovs_frag_type type
)
2280 case OVS_FRAG_TYPE_NONE
:
2282 case OVS_FRAG_TYPE_FIRST
:
2284 case OVS_FRAG_TYPE_LATER
:
2286 case __OVS_FRAG_TYPE_MAX
:
2292 static enum odp_key_fitness
2293 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2294 struct flow_tnl
*tun
)
2297 const struct nlattr
*a
;
2299 bool unknown
= false;
2301 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2302 uint16_t type
= nl_attr_type(a
);
2303 size_t len
= nl_attr_get_size(a
);
2304 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2305 OVS_TUNNEL_ATTR_MAX
, type
);
2307 if (len
!= expected_len
&& expected_len
>= 0) {
2308 return ODP_FIT_ERROR
;
2312 case OVS_TUNNEL_KEY_ATTR_ID
:
2313 tun
->tun_id
= nl_attr_get_be64(a
);
2314 tun
->flags
|= FLOW_TNL_F_KEY
;
2316 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2317 tun
->ip_src
= nl_attr_get_be32(a
);
2319 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2320 tun
->ip_dst
= nl_attr_get_be32(a
);
2322 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2323 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2325 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2326 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2328 case OVS_TUNNEL_KEY_ATTR_TOS
:
2329 tun
->ip_tos
= nl_attr_get_u8(a
);
2331 case OVS_TUNNEL_KEY_ATTR_TTL
:
2332 tun
->ip_ttl
= nl_attr_get_u8(a
);
2335 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2336 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2338 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2339 tun
->flags
|= FLOW_TNL_F_CSUM
;
2341 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2342 tun
->tp_src
= nl_attr_get_be16(a
);
2344 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2345 tun
->tp_dst
= nl_attr_get_be16(a
);
2347 case OVS_TUNNEL_KEY_ATTR_OAM
:
2348 tun
->flags
|= FLOW_TNL_F_OAM
;
2350 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2351 static const struct nl_policy vxlan_opts_policy
[] = {
2352 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2354 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2356 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2357 return ODP_FIT_ERROR
;
2360 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2361 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2363 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2364 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2369 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2370 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2374 /* Allow this to show up as unexpected, if there are unknown
2375 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2382 return ODP_FIT_ERROR
;
2385 return ODP_FIT_TOO_MUCH
;
2387 return ODP_FIT_PERFECT
;
2390 enum odp_key_fitness
2391 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2393 memset(tun
, 0, sizeof *tun
);
2394 return odp_tun_key_from_attr__(attr
, false, tun
);
2398 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2399 const struct flow_tnl
*tun_flow_key
,
2400 const struct ofpbuf
*key_buf
)
2404 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2406 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2407 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2408 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2410 if (tun_key
->ip_src
) {
2411 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2413 if (tun_key
->ip_dst
) {
2414 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2416 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2417 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2419 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2420 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2422 if (tun_key
->ip_tos
) {
2423 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2425 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2426 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2427 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2429 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2430 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2432 if (tun_key
->tp_src
) {
2433 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2435 if (tun_key
->tp_dst
) {
2436 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2438 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2439 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2441 if (tun_key
->gbp_flags
|| tun_key
->gbp_id
) {
2442 size_t vxlan_opts_ofs
;
2444 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2445 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2446 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2447 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2449 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2451 nl_msg_end_nested(a
, tun_key_ofs
);
2455 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2458 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2459 * that -1 becomes all-1-bits and 0 does not change. */
2460 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2461 uint32_t u32
= constant
;
2462 uint8_t u8
= constant
;
2463 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
2466 case OVS_KEY_ATTR_UNSPEC
:
2467 case OVS_KEY_ATTR_ENCAP
:
2468 case __OVS_KEY_ATTR_MAX
:
2472 case OVS_KEY_ATTR_PRIORITY
:
2473 case OVS_KEY_ATTR_IN_PORT
:
2474 case OVS_KEY_ATTR_ETHERNET
:
2475 case OVS_KEY_ATTR_VLAN
:
2476 case OVS_KEY_ATTR_ETHERTYPE
:
2477 case OVS_KEY_ATTR_IPV4
:
2478 case OVS_KEY_ATTR_TCP
:
2479 case OVS_KEY_ATTR_UDP
:
2480 case OVS_KEY_ATTR_ICMP
:
2481 case OVS_KEY_ATTR_ICMPV6
:
2482 case OVS_KEY_ATTR_ND
:
2483 case OVS_KEY_ATTR_SKB_MARK
:
2484 case OVS_KEY_ATTR_TUNNEL
:
2485 case OVS_KEY_ATTR_SCTP
:
2486 case OVS_KEY_ATTR_DP_HASH
:
2487 case OVS_KEY_ATTR_RECIRC_ID
:
2488 case OVS_KEY_ATTR_MPLS
:
2489 case OVS_KEY_ATTR_CT_STATE
:
2490 case OVS_KEY_ATTR_CT_ZONE
:
2491 case OVS_KEY_ATTR_CT_MARK
:
2492 case OVS_KEY_ATTR_CT_LABELS
:
2493 case OVS_KEY_ATTR_PACKET_TYPE
:
2494 case OVS_KEY_ATTR_NSH
:
2495 return is_all_byte(mask
, size
, u8
);
2497 case OVS_KEY_ATTR_TCP_FLAGS
:
2498 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
2500 case OVS_KEY_ATTR_IPV6
: {
2501 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2502 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2503 == htonl(IPV6_LABEL_MASK
& u32
)
2504 && ipv6_mask
->ipv6_proto
== u8
2505 && ipv6_mask
->ipv6_tclass
== u8
2506 && ipv6_mask
->ipv6_hlimit
== u8
2507 && ipv6_mask
->ipv6_frag
== u8
2508 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
2509 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
2512 case OVS_KEY_ATTR_ARP
:
2513 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
2515 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
2516 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
2519 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
2520 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
2525 /* The caller must already have verified that 'ma' has a correct length.
2527 * The main purpose of this function is formatting, to allow code to figure out
2528 * whether the mask can be omitted. It doesn't try hard for attributes that
2529 * contain sub-attributes, etc., because normally those would be broken down
2530 * further for formatting. */
2532 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2534 return odp_mask_is_constant__(nl_attr_type(ma
),
2535 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
2538 /* The caller must already have verified that 'size' is a correct length for
2541 * The main purpose of this function is formatting, to allow code to figure out
2542 * whether the mask can be omitted. It doesn't try hard for attributes that
2543 * contain sub-attributes, etc., because normally those would be broken down
2544 * further for formatting. */
2546 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2548 return odp_mask_is_constant__(attr
, mask
, size
, -1);
2551 /* The caller must already have verified that 'ma' has a correct length. */
2553 odp_mask_attr_is_exact(const struct nlattr
*ma
)
2555 enum ovs_key_attr attr
= nl_attr_type(ma
);
2556 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
2560 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
2563 struct odp_portno_names
*odp_portno_names
;
2565 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
2566 odp_portno_names
->port_no
= port_no
;
2567 odp_portno_names
->name
= xstrdup(port_name
);
2568 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
2569 hash_odp_port(port_no
));
2573 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
2576 struct odp_portno_names
*odp_portno_names
;
2578 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
2579 hash_odp_port(port_no
), portno_names
) {
2580 if (odp_portno_names
->port_no
== port_no
) {
2581 return odp_portno_names
->name
;
2589 odp_portno_names_destroy(struct hmap
*portno_names
)
2591 struct odp_portno_names
*odp_portno_names
;
2593 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
2594 free(odp_portno_names
->name
);
2595 free(odp_portno_names
);
2600 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
2603 const char *name
= odp_portno_names_get(portno_names
, port_no
);
2605 ds_put_cstr(s
, name
);
2607 ds_put_format(s
, "%"PRIu32
, port_no
);
2611 /* Format helpers. */
2614 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
2615 const struct eth_addr
*mask
, bool verbose
)
2617 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
2619 if (verbose
|| !mask_empty
) {
2620 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
2623 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
2625 ds_put_format(ds
, "%s=", name
);
2626 eth_format_masked(key
, mask
, ds
);
2627 ds_put_char(ds
, ',');
2634 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
2635 const ovs_be64
*mask
, bool verbose
)
2637 bool mask_empty
= mask
&& !*mask
;
2639 if (verbose
|| !mask_empty
) {
2640 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
2642 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
2643 if (!mask_full
) { /* Partially masked. */
2644 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
2646 ds_put_char(ds
, ',');
2651 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
2652 const ovs_be32
*mask
, bool verbose
)
2654 bool mask_empty
= mask
&& !*mask
;
2656 if (verbose
|| !mask_empty
) {
2657 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
2659 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
2660 if (!mask_full
) { /* Partially masked. */
2661 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
2663 ds_put_char(ds
, ',');
2668 format_in6_addr(struct ds
*ds
, const char *name
,
2669 const struct in6_addr
*key
,
2670 const struct in6_addr
*mask
,
2673 char buf
[INET6_ADDRSTRLEN
];
2674 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
2676 if (verbose
|| !mask_empty
) {
2677 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
2679 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
2680 ds_put_format(ds
, "%s=%s", name
, buf
);
2681 if (!mask_full
) { /* Partially masked. */
2682 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
2683 ds_put_format(ds
, "/%s", buf
);
2685 ds_put_char(ds
, ',');
2690 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
2691 const ovs_be32
*mask
, bool verbose
)
2693 bool mask_empty
= mask
&& !*mask
;
2695 if (verbose
|| !mask_empty
) {
2696 bool mask_full
= !mask
2697 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
2699 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
2700 if (!mask_full
) { /* Partially masked. */
2701 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
2703 ds_put_char(ds
, ',');
2708 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
2709 const uint8_t *mask
, bool verbose
)
2711 bool mask_empty
= mask
&& !*mask
;
2713 if (verbose
|| !mask_empty
) {
2714 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2716 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
2717 if (!mask_full
) { /* Partially masked. */
2718 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2720 ds_put_char(ds
, ',');
2725 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
2726 const uint8_t *mask
, bool verbose
)
2728 bool mask_empty
= mask
&& !*mask
;
2730 if (verbose
|| !mask_empty
) {
2731 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
2733 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
2734 if (!mask_full
) { /* Partially masked. */
2735 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
2737 ds_put_char(ds
, ',');
2742 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
2743 const ovs_be16
*mask
, bool verbose
)
2745 bool mask_empty
= mask
&& !*mask
;
2747 if (verbose
|| !mask_empty
) {
2748 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2750 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
2751 if (!mask_full
) { /* Partially masked. */
2752 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2754 ds_put_char(ds
, ',');
2759 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
2760 const ovs_be16
*mask
, bool verbose
)
2762 bool mask_empty
= mask
&& !*mask
;
2764 if (verbose
|| !mask_empty
) {
2765 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
2767 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
2768 if (!mask_full
) { /* Partially masked. */
2769 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
2771 ds_put_char(ds
, ',');
2776 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
2777 const uint16_t *mask
, bool verbose
)
2779 bool mask_empty
= mask
&& !*mask
;
2781 if (verbose
|| !mask_empty
) {
2782 ds_put_cstr(ds
, name
);
2783 ds_put_char(ds
, '(');
2785 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
2786 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
2787 } else { /* Fully masked. */
2788 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
2790 ds_put_cstr(ds
, "),");
2795 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
2796 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
2800 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
2801 if (expected_len
!= ATTR_LEN_VARIABLE
&&
2802 expected_len
!= ATTR_LEN_NESTED
) {
2804 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
2805 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
2807 if (bad_key_len
|| bad_mask_len
) {
2809 ds_put_format(ds
, "key%u", nl_attr_type(a
));
2812 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
2813 nl_attr_get_size(a
), expected_len
);
2815 format_generic_odp_key(a
, ds
);
2817 ds_put_char(ds
, '/');
2819 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
2820 nl_attr_get_size(ma
), expected_len
);
2822 format_generic_odp_key(ma
, ds
);
2824 ds_put_char(ds
, ')');
2833 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
2834 const struct nlattr
*ma
)
2836 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
2837 format_generic_odp_key(a
, ds
);
2838 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
2839 ds_put_char(ds
, '/');
2840 format_generic_odp_key(ma
, ds
);
2842 ds_put_cstr(ds
, "),");
2846 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
2847 const struct nlattr
*mask_attr
, struct ds
*ds
,
2851 const struct nlattr
*a
;
2854 ofpbuf_init(&ofp
, 100);
2855 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2856 uint16_t type
= nl_attr_type(a
);
2857 const struct nlattr
*ma
= NULL
;
2860 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2861 nl_attr_get_size(mask_attr
), type
);
2863 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
2869 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
2870 OVS_VXLAN_EXT_MAX
, true)) {
2875 case OVS_VXLAN_EXT_GBP
: {
2876 uint32_t key
= nl_attr_get_u32(a
);
2877 ovs_be16 id
, id_mask
;
2878 uint8_t flags
, flags_mask
= 0;
2880 id
= htons(key
& 0xFFFF);
2881 flags
= (key
>> 16) & 0xFF;
2883 uint32_t mask
= nl_attr_get_u32(ma
);
2884 id_mask
= htons(mask
& 0xFFFF);
2885 flags_mask
= (mask
>> 16) & 0xFF;
2888 ds_put_cstr(ds
, "gbp(");
2889 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
2890 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
2892 ds_put_cstr(ds
, "),");
2897 format_unknown_key(ds
, a
, ma
);
2903 ofpbuf_uninit(&ofp
);
2906 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
2909 format_geneve_opts(const struct geneve_opt
*opt
,
2910 const struct geneve_opt
*mask
, int opts_len
,
2911 struct ds
*ds
, bool verbose
)
2913 while (opts_len
> 0) {
2915 uint8_t data_len
, data_len_mask
;
2917 if (opts_len
< sizeof *opt
) {
2918 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
2919 opts_len
, sizeof *opt
);
2923 data_len
= opt
->length
* 4;
2925 if (mask
->length
== 0x1f) {
2926 data_len_mask
= UINT8_MAX
;
2928 data_len_mask
= mask
->length
;
2931 len
= sizeof *opt
+ data_len
;
2932 if (len
> opts_len
) {
2933 ds_put_format(ds
, "opt len %u greater than remaining %u",
2938 ds_put_char(ds
, '{');
2939 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
2941 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
2942 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
2944 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
2945 ds_put_hex(ds
, opt
+ 1, data_len
);
2946 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
2947 ds_put_char(ds
, '/');
2948 ds_put_hex(ds
, mask
+ 1, data_len
);
2953 ds_put_char(ds
, '}');
2955 opt
+= len
/ sizeof(*opt
);
2957 mask
+= len
/ sizeof(*opt
);
2964 format_odp_tun_geneve(const struct nlattr
*attr
,
2965 const struct nlattr
*mask_attr
, struct ds
*ds
,
2968 int opts_len
= nl_attr_get_size(attr
);
2969 const struct geneve_opt
*opt
= nl_attr_get(attr
);
2970 const struct geneve_opt
*mask
= mask_attr
?
2971 nl_attr_get(mask_attr
) : NULL
;
2973 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
2974 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
2975 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
2979 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
2983 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
2984 struct ds
*ds
, bool verbose
)
2987 const struct nlattr
*a
;
2989 uint16_t mask_flags
= 0;
2992 ofpbuf_init(&ofp
, 100);
2993 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2994 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
2995 const struct nlattr
*ma
= NULL
;
2998 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
2999 nl_attr_get_size(mask_attr
), type
);
3001 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3002 OVS_TUNNEL_KEY_ATTR_MAX
,
3007 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3008 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3013 case OVS_TUNNEL_KEY_ATTR_ID
:
3014 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3015 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3016 flags
|= FLOW_TNL_F_KEY
;
3018 mask_flags
|= FLOW_TNL_F_KEY
;
3021 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3022 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3023 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3025 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3026 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3027 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3029 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3030 struct in6_addr ipv6_src
;
3031 ipv6_src
= nl_attr_get_in6_addr(a
);
3032 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3033 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3036 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3037 struct in6_addr ipv6_dst
;
3038 ipv6_dst
= nl_attr_get_in6_addr(a
);
3039 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3040 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3043 case OVS_TUNNEL_KEY_ATTR_TOS
:
3044 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3045 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3047 case OVS_TUNNEL_KEY_ATTR_TTL
:
3048 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3049 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3051 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3052 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3054 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3055 flags
|= FLOW_TNL_F_CSUM
;
3057 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3058 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3059 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3061 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3062 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3063 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3065 case OVS_TUNNEL_KEY_ATTR_OAM
:
3066 flags
|= FLOW_TNL_F_OAM
;
3068 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3069 ds_put_cstr(ds
, "vxlan(");
3070 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3071 ds_put_cstr(ds
, "),");
3073 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3074 ds_put_cstr(ds
, "geneve(");
3075 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3076 ds_put_cstr(ds
, "),");
3078 case OVS_TUNNEL_KEY_ATTR_PAD
:
3080 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3082 format_unknown_key(ds
, a
, ma
);
3087 /* Flags can have a valid mask even if the attribute is not set, so
3088 * we need to collect these separately. */
3090 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3091 switch (nl_attr_type(a
)) {
3092 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3093 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3095 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3096 mask_flags
|= FLOW_TNL_F_CSUM
;
3098 case OVS_TUNNEL_KEY_ATTR_OAM
:
3099 mask_flags
|= FLOW_TNL_F_OAM
;
3105 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3108 ofpbuf_uninit(&ofp
);
3112 odp_ct_state_to_string(uint32_t flag
)
3115 case OVS_CS_F_REPLY_DIR
:
3117 case OVS_CS_F_TRACKED
:
3121 case OVS_CS_F_ESTABLISHED
:
3123 case OVS_CS_F_RELATED
:
3125 case OVS_CS_F_INVALID
:
3127 case OVS_CS_F_SRC_NAT
:
3129 case OVS_CS_F_DST_NAT
:
3137 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
3138 const uint8_t *mask
, bool verbose OVS_UNUSED
)
3140 bool mask_empty
= mask
&& !*mask
;
3141 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3143 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3144 if (!mask_empty
&& !mask_full
) {
3145 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
3147 } else if (!mask_empty
) {
3148 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
3153 mask_empty(const struct nlattr
*ma
)
3161 mask
= nl_attr_get(ma
);
3162 n
= nl_attr_get_size(ma
);
3164 return is_all_zeros(mask
, n
);
3167 /* The caller must have already verified that 'a' and 'ma' have correct
3170 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
3171 const struct hmap
*portno_names
, struct ds
*ds
,
3174 enum ovs_key_attr attr
= nl_attr_type(a
);
3175 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3178 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
3180 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
3182 ds_put_char(ds
, '(');
3184 case OVS_KEY_ATTR_ENCAP
:
3185 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
3186 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
3187 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
3189 } else if (nl_attr_get_size(a
)) {
3190 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
3195 case OVS_KEY_ATTR_PRIORITY
:
3196 case OVS_KEY_ATTR_SKB_MARK
:
3197 case OVS_KEY_ATTR_DP_HASH
:
3198 case OVS_KEY_ATTR_RECIRC_ID
:
3199 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3201 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3205 case OVS_KEY_ATTR_CT_MARK
:
3206 if (verbose
|| !mask_empty(ma
)) {
3207 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3209 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3214 case OVS_KEY_ATTR_CT_STATE
:
3216 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3218 ds_put_format(ds
, "/%#"PRIx32
,
3219 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
3221 } else if (!is_exact
) {
3222 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
3224 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
3227 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
3231 case OVS_KEY_ATTR_CT_ZONE
:
3232 if (verbose
|| !mask_empty(ma
)) {
3233 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
3235 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
3240 case OVS_KEY_ATTR_CT_LABELS
: {
3241 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
3242 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3244 format_u128(ds
, value
, mask
, verbose
);
3248 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
3249 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
3250 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3252 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3253 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3254 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3256 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
3258 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
3264 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
3265 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
3266 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3268 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3270 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3272 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3274 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3276 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3282 case OVS_KEY_ATTR_TUNNEL
:
3283 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3286 case OVS_KEY_ATTR_IN_PORT
:
3288 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3290 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3292 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3297 case OVS_KEY_ATTR_PACKET_TYPE
: {
3298 ovs_be32 value
= nl_attr_get_be32(a
);
3299 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3301 ovs_be16 ns
= htons(pt_ns(value
));
3302 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3303 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3305 ovs_be16 ns_type
= pt_ns_type_be(value
);
3306 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3307 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3313 case OVS_KEY_ATTR_ETHERNET
: {
3314 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3315 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3317 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3318 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3322 case OVS_KEY_ATTR_VLAN
:
3323 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3324 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3327 case OVS_KEY_ATTR_MPLS
: {
3328 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3329 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3330 size_t size
= nl_attr_get_size(a
);
3332 if (!size
|| size
% sizeof *mpls_key
) {
3333 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3337 mpls_mask
= nl_attr_get(ma
);
3338 if (size
!= nl_attr_get_size(ma
)) {
3339 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3340 "mask length %"PRIuSIZE
")",
3341 size
, nl_attr_get_size(ma
));
3345 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
3348 case OVS_KEY_ATTR_ETHERTYPE
:
3349 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
3351 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
3355 case OVS_KEY_ATTR_IPV4
: {
3356 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
3357 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3359 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3360 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3361 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3363 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
3364 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
3365 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
3370 case OVS_KEY_ATTR_IPV6
: {
3371 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
3372 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3374 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3376 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3378 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
3380 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3382 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
3384 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
3386 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
3391 /* These have the same structure and format. */
3392 case OVS_KEY_ATTR_TCP
:
3393 case OVS_KEY_ATTR_UDP
:
3394 case OVS_KEY_ATTR_SCTP
: {
3395 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
3396 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3398 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
3399 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
3403 case OVS_KEY_ATTR_TCP_FLAGS
:
3405 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
3406 ntohs(nl_attr_get_be16(a
)),
3407 TCP_FLAGS(nl_attr_get_be16(ma
)),
3408 TCP_FLAGS(OVS_BE16_MAX
));
3410 format_flags(ds
, packet_tcp_flag_to_string
,
3411 ntohs(nl_attr_get_be16(a
)), '|');
3415 case OVS_KEY_ATTR_ICMP
: {
3416 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
3417 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3419 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
3420 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
3424 case OVS_KEY_ATTR_ICMPV6
: {
3425 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
3426 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3428 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
3430 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
3435 case OVS_KEY_ATTR_ARP
: {
3436 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3437 const struct ovs_key_arp
*key
= nl_attr_get(a
);
3439 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
3440 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
3441 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
3442 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
3443 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
3447 case OVS_KEY_ATTR_ND
: {
3448 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3449 const struct ovs_key_nd
*key
= nl_attr_get(a
);
3451 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
3453 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
3454 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
3459 case OVS_KEY_ATTR_NSH
: {
3460 const struct ovs_key_nsh
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3461 const struct ovs_key_nsh
*key
= nl_attr_get(a
);
3462 format_nsh_key_mask(ds
, key
, mask
);
3465 case OVS_KEY_ATTR_UNSPEC
:
3466 case __OVS_KEY_ATTR_MAX
:
3468 format_generic_odp_key(a
, ds
);
3470 ds_put_char(ds
, '/');
3471 format_generic_odp_key(ma
, ds
);
3475 ds_put_char(ds
, ')');
3479 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
3480 const struct hmap
*portno_names
, struct ds
*ds
,
3483 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
3484 OVS_KEY_ATTR_MAX
, false)) {
3485 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
3489 static struct nlattr
*
3490 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
3491 struct ofpbuf
*ofp
, const struct nlattr
*key
)
3493 const struct nlattr
*a
;
3495 int type
= nl_attr_type(key
);
3496 int size
= nl_attr_get_size(key
);
3498 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
3499 nl_msg_put_unspec_zero(ofp
, type
, size
);
3503 if (tbl
[type
].next
) {
3504 tbl
= tbl
[type
].next
;
3505 max
= tbl
[type
].next_max
;
3508 nested_mask
= nl_msg_start_nested(ofp
, type
);
3509 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
3510 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
3512 nl_msg_end_nested(ofp
, nested_mask
);
3519 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
3520 const ovs_32aligned_u128
*mask
, bool verbose
)
3522 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
3523 ovs_be128 value
= hton128(get_32aligned_u128(key
));
3524 ds_put_hex(ds
, &value
, sizeof value
);
3525 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
3526 value
= hton128(get_32aligned_u128(mask
));
3527 ds_put_char(ds
, '/');
3528 ds_put_hex(ds
, &value
, sizeof value
);
3533 /* Read the string from 's_' as a 128-bit value. If the string contains
3534 * a "/", the rest of the string will be treated as a 128-bit mask.
3536 * If either the value or mask is larger than 64 bits, the string must
3537 * be in hexadecimal.
3540 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
3542 char *s
= CONST_CAST(char *, s_
);
3546 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
3547 *value
= ntoh128(be_value
);
3552 if (ovs_scan(s
, "/%n", &n
)) {
3556 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
3557 sizeof be_mask
, &s
);
3561 *mask
= ntoh128(be_mask
);
3563 *mask
= OVS_U128_MAX
;
3573 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
3577 if (ovs_scan(s
, "ufid:")) {
3580 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
3592 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
3594 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
3597 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3598 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
3599 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
3600 * non-null, translates odp port number to its name. */
3602 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
3603 const struct nlattr
*mask
, size_t mask_len
,
3604 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
3607 const struct nlattr
*a
;
3609 bool has_ethtype_key
= false;
3611 bool first_field
= true;
3613 ofpbuf_init(&ofp
, 100);
3614 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
3615 int attr_type
= nl_attr_type(a
);
3616 const struct nlattr
*ma
= (mask
&& mask_len
3617 ? nl_attr_find__(mask
, mask_len
,
3620 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
3621 OVS_KEY_ATTR_MAX
, false)) {
3625 bool is_nested_attr
;
3626 bool is_wildcard
= false;
3628 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
3629 has_ethtype_key
= true;
3632 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
3633 OVS_KEY_ATTR_MAX
, attr_type
) ==
3636 if (mask
&& mask_len
) {
3637 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
3638 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
3641 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
3642 if (is_wildcard
&& !ma
) {
3643 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
3648 ds_put_char(ds
, ',');
3650 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
3651 first_field
= false;
3655 ofpbuf_uninit(&ofp
);
3660 if (left
== key_len
) {
3661 ds_put_cstr(ds
, "<empty>");
3663 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
3664 for (i
= 0; i
< left
; i
++) {
3665 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
3667 ds_put_char(ds
, ')');
3669 if (!has_ethtype_key
) {
3670 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
3671 OVS_KEY_ATTR_ETHERTYPE
);
3673 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
3674 ntohs(nl_attr_get_be16(ma
)));
3678 ds_put_cstr(ds
, "<empty>");
3682 /* Appends to 'ds' a string representation of the 'key_len' bytes of
3683 * OVS_KEY_ATTR_* attributes in 'key'. */
3685 odp_flow_key_format(const struct nlattr
*key
,
3686 size_t key_len
, struct ds
*ds
)
3688 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
3692 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
3694 if (!strcasecmp(s
, "no")) {
3695 *type
= OVS_FRAG_TYPE_NONE
;
3696 } else if (!strcasecmp(s
, "first")) {
3697 *type
= OVS_FRAG_TYPE_FIRST
;
3698 } else if (!strcasecmp(s
, "later")) {
3699 *type
= OVS_FRAG_TYPE_LATER
;
3709 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
3713 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
3714 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
3718 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
3719 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
3722 memset(mask
, 0xff, sizeof *mask
);
3731 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3735 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
3739 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
3740 IP_SCAN_ARGS(mask
), &n
)) {
3743 *mask
= OVS_BE32_MAX
;
3752 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
3755 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
3757 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3758 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
3762 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
3763 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
3766 memset(mask
, 0xff, sizeof *mask
);
3775 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3780 if (ovs_scan(s
, "%i%n", &key_
, &n
)
3781 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
3786 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
3787 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
3789 *mask
= htonl(mask_
);
3791 *mask
= htonl(IPV6_LABEL_MASK
);
3800 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
3804 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
3808 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
3820 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
3824 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
3828 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
3840 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
3844 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
3848 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
3860 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3862 uint16_t key_
, mask_
;
3865 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
3870 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
3872 *mask
= htons(mask_
);
3874 *mask
= OVS_BE16_MAX
;
3883 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
3885 uint32_t key_
, mask_
;
3888 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
3893 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
3895 *mask
= htonl(mask_
);
3897 *mask
= OVS_BE32_MAX
;
3906 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
3908 uint64_t key_
, mask_
;
3911 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
3914 *key
= htonll(key_
);
3916 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
3918 *mask
= htonll(mask_
);
3920 *mask
= OVS_BE64_MAX
;
3929 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
3931 uint32_t flags
, fmask
;
3934 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
3935 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
3936 if (n
>= 0 && s
[n
] == ')') {
3947 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
3949 uint32_t flags
, fmask
;
3952 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
3953 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
3955 *key
= htons(flags
);
3957 *mask
= htons(fmask
);
3965 ovs_to_odp_ct_state(uint8_t state
)
3969 #define CS_STATE(ENUM, INDEX, NAME) \
3970 if (state & CS_##ENUM) { \
3971 odp |= OVS_CS_F_##ENUM; \
3980 odp_to_ovs_ct_state(uint32_t flags
)
3984 #define CS_STATE(ENUM, INDEX, NAME) \
3985 if (flags & OVS_CS_F_##ENUM) { \
3986 state |= CS_##ENUM; \
3995 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
3997 uint32_t flags
, fmask
;
4000 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4001 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4002 mask
? &fmask
: NULL
);
4015 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4019 enum ovs_frag_type frag_type
;
4021 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4022 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4035 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4036 const struct simap
*port_names
)
4040 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4044 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4051 } else if (port_names
) {
4052 const struct simap_node
*node
;
4055 len
= strcspn(s
, ")");
4056 node
= simap_find_len(port_names
, s
, len
);
4069 /* Helper for vlan parsing. */
4070 struct ovs_key_vlan__
{
4075 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
4077 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
4079 if (value
>> bits
) {
4083 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
4088 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
4091 uint16_t key_
, mask_
;
4094 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4097 if (set_be16_bf(key
, bits
, offset
, key_
)) {
4099 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4102 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
4106 *mask
|= htons(((1U << bits
) - 1) << offset
);
4116 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4118 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
4122 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4124 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
4128 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4130 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
4135 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
4137 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
4139 if (value
>> bits
) {
4143 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
4148 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
4151 uint32_t key_
, mask_
;
4154 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4157 if (set_be32_bf(key
, bits
, offset
, key_
)) {
4159 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4162 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
4166 *mask
|= htonl(((1U << bits
) - 1) << offset
);
4176 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4178 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
4182 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4184 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
4188 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4190 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
4194 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4196 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
4200 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
4202 const char *s_base
= s
;
4203 ovs_be16 id
= 0, id_mask
= 0;
4204 uint8_t flags
= 0, flags_mask
= 0;
4206 if (!strncmp(s
, "id=", 3)) {
4208 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
4214 if (!strncmp(s
, "flags=", 6)) {
4216 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
4219 if (!strncmp(s
, "))", 2)) {
4222 *key
= (flags
<< 16) | ntohs(id
);
4224 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
4234 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
4236 const char *s_base
= s
;
4237 struct geneve_opt
*opt
= key
->d
;
4238 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
4239 int len_remain
= sizeof key
->d
;
4241 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
4245 len_remain
-= sizeof *opt
;
4247 if (!strncmp(s
, "class=", 6)) {
4249 s
+= scan_be16(s
, &opt
->opt_class
,
4250 mask
? &opt_mask
->opt_class
: NULL
);
4252 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
4258 if (!strncmp(s
, "type=", 5)) {
4260 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
4262 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4268 if (!strncmp(s
, "len=", 4)) {
4269 uint8_t opt_len
, opt_len_mask
;
4271 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
4273 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
4276 opt
->length
= opt_len
/ 4;
4278 opt_mask
->length
= opt_len_mask
;
4282 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4288 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
4295 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
4296 data_len
, (char **)&s
)) {
4307 opt
+= 1 + data_len
/ 4;
4309 opt_mask
+= 1 + data_len
/ 4;
4311 len_remain
-= data_len
;
4316 int len
= sizeof key
->d
- len_remain
;
4330 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
4332 const uint16_t *flags
= data_
;
4334 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
4335 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
4337 if (*flags
& FLOW_TNL_F_CSUM
) {
4338 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
4340 if (*flags
& FLOW_TNL_F_OAM
) {
4341 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
4346 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
4348 const uint32_t *gbp
= data_
;
4351 size_t vxlan_opts_ofs
;
4353 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
4354 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
4355 nl_msg_end_nested(a
, vxlan_opts_ofs
);
4360 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
4362 const struct geneve_scan
*geneve
= data_
;
4364 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
4368 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
4370 unsigned long call_fn = (unsigned long)FUNC; \
4372 typedef void (*fn)(struct ofpbuf *, const void *); \
4374 func(BUF, &(DATA)); \
4376 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
4380 #define SCAN_IF(NAME) \
4381 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4382 const char *start = s; \
4387 /* Usually no special initialization is needed. */
4388 #define SCAN_BEGIN(NAME, TYPE) \
4391 memset(&skey, 0, sizeof skey); \
4392 memset(&smask, 0, sizeof smask); \
4396 /* Init as fully-masked as mask will not be scanned. */
4397 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
4400 memset(&skey, 0, sizeof skey); \
4401 memset(&smask, 0xff, sizeof smask); \
4405 /* VLAN needs special initialization. */
4406 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
4408 TYPE skey = KEY_INIT; \
4409 TYPE smask = MASK_INIT; \
4413 /* Scan unnamed entry as 'TYPE' */
4414 #define SCAN_TYPE(TYPE, KEY, MASK) \
4415 len = scan_##TYPE(s, KEY, MASK); \
4421 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4422 #define SCAN_FIELD(NAME, TYPE, FIELD) \
4423 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4424 s += strlen(NAME); \
4425 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
4429 #define SCAN_FINISH() \
4430 } while (*s++ == ',' && len != 0); \
4431 if (s[-1] != ')') { \
4435 #define SCAN_FINISH_SINGLE() \
4437 if (*s++ != ')') { \
4441 /* Beginning of nested attribute. */
4442 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
4444 size_t key_offset, mask_offset; \
4445 key_offset = nl_msg_start_nested(key, ATTR); \
4447 mask_offset = nl_msg_start_nested(mask, ATTR); \
4452 #define SCAN_END_NESTED() \
4454 nl_msg_end_nested(key, key_offset); \
4456 nl_msg_end_nested(mask, mask_offset); \
4461 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
4462 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4464 memset(&skey, 0, sizeof skey); \
4465 memset(&smask, 0xff, sizeof smask); \
4466 s += strlen(NAME); \
4467 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4468 SCAN_PUT(ATTR, FUNC); \
4472 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
4473 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
4475 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
4476 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
4478 #define SCAN_PUT(ATTR, FUNC) \
4479 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
4481 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
4483 #define SCAN_END(ATTR) \
4485 SCAN_PUT(ATTR, NULL); \
4489 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
4491 TYPE skey[CNT], smask[CNT]; \
4492 memset(&skey, 0, sizeof skey); \
4493 memset(&smask, 0, sizeof smask); \
4494 int idx = 0, cnt = CNT; \
4495 uint64_t fields = 0; \
4500 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
4501 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
4502 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
4503 if (fields & (1UL << field)) { \
4505 if (++idx == cnt) { \
4509 s += strlen(NAME); \
4510 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
4511 fields |= 1UL << field; \
4516 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
4517 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
4519 #define SCAN_PUT_ARRAY(ATTR, CNT) \
4520 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
4522 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
4525 #define SCAN_END_ARRAY(ATTR) \
4530 SCAN_PUT_ARRAY(ATTR, idx + 1); \
4534 #define SCAN_END_SINGLE(ATTR) \
4535 SCAN_FINISH_SINGLE(); \
4536 SCAN_PUT(ATTR, NULL); \
4540 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
4541 SCAN_BEGIN(NAME, TYPE) { \
4542 SCAN_TYPE(SCAN_AS, &skey, &smask); \
4543 } SCAN_END_SINGLE(ATTR)
4545 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
4546 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
4547 SCAN_TYPE(SCAN_AS, &skey, NULL); \
4548 } SCAN_END_SINGLE(ATTR)
4550 /* scan_port needs one extra argument. */
4551 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
4552 SCAN_BEGIN(NAME, TYPE) { \
4553 len = scan_port(s, &skey, &smask, port_names); \
4558 } SCAN_END_SINGLE(ATTR)
4561 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
4562 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4566 int ufid_len
= odp_ufid_from_string(s
, &ufid
);
4571 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
4572 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
4573 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
4574 OVS_KEY_ATTR_RECIRC_ID
);
4575 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
4577 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
4578 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
4579 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
4580 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
4582 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
4583 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4584 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4585 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4586 SCAN_FIELD("tp_src=", be16
, src_port
);
4587 SCAN_FIELD("tp_dst=", be16
, dst_port
);
4588 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
4590 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
4591 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4592 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4593 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4594 SCAN_FIELD("tp_src=", be16
, src_port
);
4595 SCAN_FIELD("tp_dst=", be16
, dst_port
);
4596 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
4598 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
4599 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
4600 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
4601 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
4602 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
4603 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
4604 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
4605 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
4606 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
4607 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
4608 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
4609 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
4611 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
4612 } SCAN_END_NESTED();
4614 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
4616 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
4617 SCAN_FIELD("src=", eth
, eth_src
);
4618 SCAN_FIELD("dst=", eth
, eth_dst
);
4619 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
4621 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
4622 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
4623 SCAN_FIELD("vid=", vid
, tci
);
4624 SCAN_FIELD("pcp=", pcp
, tci
);
4625 SCAN_FIELD("cfi=", cfi
, tci
);
4626 } SCAN_END(OVS_KEY_ATTR_VLAN
);
4628 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
4630 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
4631 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
4632 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
4633 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
4634 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
4635 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
4637 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
4638 SCAN_FIELD("src=", ipv4
, ipv4_src
);
4639 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
4640 SCAN_FIELD("proto=", u8
, ipv4_proto
);
4641 SCAN_FIELD("tos=", u8
, ipv4_tos
);
4642 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
4643 SCAN_FIELD("frag=", frag
, ipv4_frag
);
4644 } SCAN_END(OVS_KEY_ATTR_IPV4
);
4646 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
4647 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
4648 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
4649 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
4650 SCAN_FIELD("proto=", u8
, ipv6_proto
);
4651 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
4652 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
4653 SCAN_FIELD("frag=", frag
, ipv6_frag
);
4654 } SCAN_END(OVS_KEY_ATTR_IPV6
);
4656 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
4657 SCAN_FIELD("src=", be16
, tcp_src
);
4658 SCAN_FIELD("dst=", be16
, tcp_dst
);
4659 } SCAN_END(OVS_KEY_ATTR_TCP
);
4661 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
4663 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
4664 SCAN_FIELD("src=", be16
, udp_src
);
4665 SCAN_FIELD("dst=", be16
, udp_dst
);
4666 } SCAN_END(OVS_KEY_ATTR_UDP
);
4668 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
4669 SCAN_FIELD("src=", be16
, sctp_src
);
4670 SCAN_FIELD("dst=", be16
, sctp_dst
);
4671 } SCAN_END(OVS_KEY_ATTR_SCTP
);
4673 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
4674 SCAN_FIELD("type=", u8
, icmp_type
);
4675 SCAN_FIELD("code=", u8
, icmp_code
);
4676 } SCAN_END(OVS_KEY_ATTR_ICMP
);
4678 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
4679 SCAN_FIELD("type=", u8
, icmpv6_type
);
4680 SCAN_FIELD("code=", u8
, icmpv6_code
);
4681 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
4683 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
4684 SCAN_FIELD("sip=", ipv4
, arp_sip
);
4685 SCAN_FIELD("tip=", ipv4
, arp_tip
);
4686 SCAN_FIELD("op=", be16
, arp_op
);
4687 SCAN_FIELD("sha=", eth
, arp_sha
);
4688 SCAN_FIELD("tha=", eth
, arp_tha
);
4689 } SCAN_END(OVS_KEY_ATTR_ARP
);
4691 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
4692 SCAN_FIELD("target=", in6_addr
, nd_target
);
4693 SCAN_FIELD("sll=", eth
, nd_sll
);
4694 SCAN_FIELD("tll=", eth
, nd_tll
);
4695 } SCAN_END(OVS_KEY_ATTR_ND
);
4697 struct packet_type
{
4701 SCAN_BEGIN("packet_type(", struct packet_type
) {
4702 SCAN_FIELD("ns=", be16
, ns
);
4703 SCAN_FIELD("id=", be16
, id
);
4704 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
4706 SCAN_BEGIN("nsh(", struct ovs_key_nsh
) {
4707 SCAN_FIELD("flags=", u8
, flags
);
4708 SCAN_FIELD("mdtype=", u8
, mdtype
);
4709 SCAN_FIELD("np=", u8
, np
);
4710 SCAN_FIELD("path_hdr=", be32
, path_hdr
);
4711 SCAN_FIELD("c1=", be32
, c
[0]);
4712 SCAN_FIELD("c2=", be32
, c
[1]);
4713 SCAN_FIELD("c3=", be32
, c
[2]);
4714 SCAN_FIELD("c4=", be32
, c
[2]);
4715 } SCAN_END(OVS_KEY_ATTR_NSH
);
4717 /* Encap open-coded. */
4718 if (!strncmp(s
, "encap(", 6)) {
4719 const char *start
= s
;
4720 size_t encap
, encap_mask
= 0;
4722 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
4724 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
4731 s
+= strspn(s
, delimiters
);
4734 } else if (*s
== ')') {
4738 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4746 nl_msg_end_nested(key
, encap
);
4748 nl_msg_end_nested(mask
, encap_mask
);
4757 /* Parses the string representation of a datapath flow key, in the
4758 * format output by odp_flow_key_format(). Returns 0 if successful,
4759 * otherwise a positive errno value. On success, the flow key is
4760 * appended to 'key' as a series of Netlink attributes. On failure, no
4761 * data is appended to 'key'. Either way, 'key''s data might be
4764 * If 'port_names' is nonnull, it points to an simap that maps from a port name
4765 * to a port number. (Port names may be used instead of port numbers in
4768 * On success, the attributes appended to 'key' are individually syntactically
4769 * valid, but they may not be valid as a sequence. 'key' might, for example,
4770 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
4772 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
4773 struct ofpbuf
*key
, struct ofpbuf
*mask
)
4775 const size_t old_size
= key
->size
;
4779 s
+= strspn(s
, delimiters
);
4784 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
4786 key
->size
= old_size
;
4796 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
4799 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
4800 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
4801 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
4802 * must use a zero mask for the netlink frag field, and all ones mask
4804 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
4806 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
4807 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
4808 : OVS_FRAG_TYPE_FIRST
;
4811 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
4812 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
4813 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
4815 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
4817 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
4819 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
4821 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
4822 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
4823 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
4824 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
4825 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
4827 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
4830 /* These share the same layout. */
4832 struct ovs_key_tcp tcp
;
4833 struct ovs_key_udp udp
;
4834 struct ovs_key_sctp sctp
;
4837 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
4838 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
4841 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
4842 bool export_mask
, struct ofpbuf
*buf
)
4844 struct ovs_key_ethernet
*eth_key
;
4845 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
4847 const struct flow
*flow
= parms
->flow
;
4848 const struct flow
*mask
= parms
->mask
;
4849 const struct flow
*data
= export_mask
? mask
: flow
;
4851 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
4853 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
4854 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
4858 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
4860 if (parms
->support
.ct_state
) {
4861 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
4862 ovs_to_odp_ct_state(data
->ct_state
));
4864 if (parms
->support
.ct_zone
) {
4865 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
4867 if (parms
->support
.ct_mark
) {
4868 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
4870 if (parms
->support
.ct_label
) {
4871 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
4872 sizeof(data
->ct_label
));
4874 if (flow
->ct_nw_proto
) {
4875 if (parms
->support
.ct_orig_tuple
4876 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4877 struct ovs_key_ct_tuple_ipv4 ct
= {
4884 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
4886 } else if (parms
->support
.ct_orig_tuple6
4887 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4888 struct ovs_key_ct_tuple_ipv6 ct
= {
4895 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
4899 if (parms
->support
.recirc
) {
4900 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
4901 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
4904 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
4905 * is not the magical value "ODPP_NONE". */
4906 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
4907 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
4910 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
4912 if (OVS_UNLIKELY(parms
->probe
)) {
4913 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
4915 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
4918 /* Conditionally add L2 attributes for Ethernet packets */
4919 if (flow
->packet_type
== htonl(PT_ETH
)) {
4920 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
4922 get_ethernet_key(data
, eth_key
);
4924 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
4925 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
4927 if (flow
->vlans
[encaps
].tci
== htons(0)) {
4928 if (eth_type_vlan(flow
->dl_type
)) {
4929 /* If VLAN was truncated the tpid is in dl_type */
4930 tpid
= flow
->dl_type
;
4937 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4939 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
4941 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
4942 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
4943 if (flow
->vlans
[encaps
].tci
== htons(0)) {
4949 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
4950 /* For backwards compatibility with kernels that don't support
4951 * wildcarding, the following convention is used to encode the
4952 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
4955 * -------- -------- -------
4956 * >0x5ff 0xffff Specified Ethernet II Ethertype.
4957 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
4958 * <none> 0xffff Any non-Ethernet II frame (except valid
4959 * 802.3 SNAP packet with valid eth_type).
4962 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
4967 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
4969 if (eth_type_vlan(flow
->dl_type
)) {
4973 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
4974 struct ovs_key_ipv4
*ipv4_key
;
4976 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
4978 get_ipv4_key(data
, ipv4_key
, export_mask
);
4979 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
4980 struct ovs_key_ipv6
*ipv6_key
;
4982 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
4984 get_ipv6_key(data
, ipv6_key
, export_mask
);
4985 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
4986 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
4987 struct ovs_key_arp
*arp_key
;
4989 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
4991 get_arp_key(data
, arp_key
);
4992 } else if (eth_type_mpls(flow
->dl_type
)) {
4993 struct ovs_key_mpls
*mpls_key
;
4996 n
= flow_count_mpls_labels(flow
, NULL
);
4998 n
= MIN(n
, parms
->support
.max_mpls_depth
);
5000 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
5001 n
* sizeof *mpls_key
);
5002 for (i
= 0; i
< n
; i
++) {
5003 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
5005 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5006 struct ovs_key_nsh
*nsh_key
;
5008 nsh_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_NSH
,
5010 get_nsh_key(data
, nsh_key
, export_mask
);
5013 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5014 if (flow
->nw_proto
== IPPROTO_TCP
) {
5015 union ovs_key_tp
*tcp_key
;
5017 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
5019 get_tp_key(data
, tcp_key
);
5020 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
5021 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
5023 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5024 union ovs_key_tp
*udp_key
;
5026 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
5028 get_tp_key(data
, udp_key
);
5029 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5030 union ovs_key_tp
*sctp_key
;
5032 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
5034 get_tp_key(data
, sctp_key
);
5035 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
5036 && flow
->nw_proto
== IPPROTO_ICMP
) {
5037 struct ovs_key_icmp
*icmp_key
;
5039 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
5041 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
5042 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
5043 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5044 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
5045 struct ovs_key_icmpv6
*icmpv6_key
;
5047 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
5048 sizeof *icmpv6_key
);
5049 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
5050 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
5052 if (is_nd(flow
, NULL
)
5053 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5054 * type and code are 8 bits wide. Therefore, an exact match
5055 * looks like htons(0xff), not htons(0xffff). See
5056 * xlate_wc_finish() for details. */
5057 && (!export_mask
|| (data
->tp_src
== htons(0xff)
5058 && data
->tp_dst
== htons(0xff)))) {
5060 struct ovs_key_nd
*nd_key
;
5062 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
5064 nd_key
->nd_target
= data
->nd_target
;
5065 nd_key
->nd_sll
= data
->arp_sha
;
5066 nd_key
->nd_tll
= data
->arp_tha
;
5072 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
5073 if (encap
[encaps
]) {
5074 nl_msg_end_nested(buf
, encap
[encaps
]);
5079 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5081 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5082 * capable of being expanded to allow for that much space. */
5084 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
5087 odp_flow_key_from_flow__(parms
, false, buf
);
5090 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5093 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5094 * capable of being expanded to allow for that much space. */
5096 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
5099 odp_flow_key_from_flow__(parms
, true, buf
);
5102 /* Generate ODP flow key from the given packet metadata */
5104 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
5106 const struct pkt_metadata
*md
= &packet
->md
;
5108 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
5110 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
5111 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
);
5114 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
5117 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5118 ovs_to_odp_ct_state(md
->ct_state
));
5120 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
5123 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
5125 if (!ovs_u128_is_zero(md
->ct_label
)) {
5126 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
5127 sizeof(md
->ct_label
));
5129 if (md
->ct_orig_tuple_ipv6
) {
5130 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
5131 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
5132 &md
->ct_orig_tuple
.ipv6
,
5133 sizeof md
->ct_orig_tuple
.ipv6
);
5136 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
5137 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
5138 &md
->ct_orig_tuple
.ipv4
,
5139 sizeof md
->ct_orig_tuple
.ipv4
);
5144 /* Add an ingress port attribute if 'odp_in_port' is not the magical
5145 * value "ODPP_NONE". */
5146 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
5147 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
5150 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
5151 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
5152 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
5153 pt_ns_type_be(packet
->packet_type
));
5157 /* Generate packet metadata from the given ODP flow key. */
5159 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
5160 struct dp_packet
*packet
)
5162 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5163 const struct nlattr
*nla
;
5164 struct pkt_metadata
*md
= &packet
->md
;
5165 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
5166 ovs_be16 ethertype
= 0;
5169 pkt_metadata_init(md
, ODPP_NONE
);
5171 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
5172 enum ovs_key_attr type
= nl_attr_type(nla
);
5173 size_t len
= nl_attr_get_size(nla
);
5174 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
5175 OVS_KEY_ATTR_MAX
, type
);
5177 if (len
!= expected_len
&& expected_len
>= 0) {
5182 case OVS_KEY_ATTR_RECIRC_ID
:
5183 md
->recirc_id
= nl_attr_get_u32(nla
);
5185 case OVS_KEY_ATTR_DP_HASH
:
5186 md
->dp_hash
= nl_attr_get_u32(nla
);
5188 case OVS_KEY_ATTR_PRIORITY
:
5189 md
->skb_priority
= nl_attr_get_u32(nla
);
5191 case OVS_KEY_ATTR_SKB_MARK
:
5192 md
->pkt_mark
= nl_attr_get_u32(nla
);
5194 case OVS_KEY_ATTR_CT_STATE
:
5195 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
5197 case OVS_KEY_ATTR_CT_ZONE
:
5198 md
->ct_zone
= nl_attr_get_u16(nla
);
5200 case OVS_KEY_ATTR_CT_MARK
:
5201 md
->ct_mark
= nl_attr_get_u32(nla
);
5203 case OVS_KEY_ATTR_CT_LABELS
: {
5204 md
->ct_label
= nl_attr_get_u128(nla
);
5207 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
5208 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
5209 md
->ct_orig_tuple
.ipv4
= *ct
;
5210 md
->ct_orig_tuple_ipv6
= false;
5213 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
5214 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
5216 md
->ct_orig_tuple
.ipv6
= *ct
;
5217 md
->ct_orig_tuple_ipv6
= true;
5220 case OVS_KEY_ATTR_TUNNEL
: {
5221 enum odp_key_fitness res
;
5223 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
5224 if (res
== ODP_FIT_ERROR
) {
5225 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
5229 case OVS_KEY_ATTR_IN_PORT
:
5230 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
5232 case OVS_KEY_ATTR_ETHERNET
:
5233 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
5234 packet_type
= htonl(PT_ETH
);
5236 case OVS_KEY_ATTR_ETHERTYPE
:
5237 ethertype
= nl_attr_get_be16(nla
);
5239 case OVS_KEY_ATTR_UNSPEC
:
5240 case OVS_KEY_ATTR_ENCAP
:
5241 case OVS_KEY_ATTR_VLAN
:
5242 case OVS_KEY_ATTR_IPV4
:
5243 case OVS_KEY_ATTR_IPV6
:
5244 case OVS_KEY_ATTR_TCP
:
5245 case OVS_KEY_ATTR_UDP
:
5246 case OVS_KEY_ATTR_ICMP
:
5247 case OVS_KEY_ATTR_ICMPV6
:
5248 case OVS_KEY_ATTR_ARP
:
5249 case OVS_KEY_ATTR_ND
:
5250 case OVS_KEY_ATTR_SCTP
:
5251 case OVS_KEY_ATTR_TCP_FLAGS
:
5252 case OVS_KEY_ATTR_MPLS
:
5253 case OVS_KEY_ATTR_PACKET_TYPE
:
5254 case OVS_KEY_ATTR_NSH
:
5255 case __OVS_KEY_ATTR_MAX
:
5261 if (packet_type
== htonl(PT_ETH
)) {
5262 packet
->packet_type
= htonl(PT_ETH
);
5263 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
5264 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
5267 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
5272 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
5274 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
5275 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
5279 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
5280 uint64_t attrs
, int out_of_range_attr
,
5281 const struct nlattr
*key
, size_t key_len
)
5286 if (VLOG_DROP_DBG(rl
)) {
5291 for (i
= 0; i
< 64; i
++) {
5292 if (attrs
& (UINT64_C(1) << i
)) {
5293 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
5295 ds_put_format(&s
, " %s",
5296 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
5299 if (out_of_range_attr
) {
5300 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
5303 ds_put_cstr(&s
, ": ");
5304 odp_flow_key_format(key
, key_len
, &s
);
5306 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
5311 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
5313 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5316 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
5319 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
5320 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
5321 return 0xff; /* Error. */
5324 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
5325 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
5326 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
5330 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
5331 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
5332 int *out_of_range_attrp
)
5334 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5335 const struct nlattr
*nla
;
5336 uint64_t present_attrs
;
5339 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
5341 *out_of_range_attrp
= 0;
5342 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
5343 uint16_t type
= nl_attr_type(nla
);
5344 size_t len
= nl_attr_get_size(nla
);
5345 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
5346 OVS_KEY_ATTR_MAX
, type
);
5348 if (len
!= expected_len
&& expected_len
>= 0) {
5349 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
5351 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
5352 "length %d", ovs_key_attr_to_string(type
, namebuf
,
5358 if (type
> OVS_KEY_ATTR_MAX
) {
5359 *out_of_range_attrp
= type
;
5361 if (present_attrs
& (UINT64_C(1) << type
)) {
5362 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
5364 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
5365 ovs_key_attr_to_string(type
,
5366 namebuf
, sizeof namebuf
));
5370 present_attrs
|= UINT64_C(1) << type
;
5375 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
5379 *present_attrsp
= present_attrs
;
5383 static enum odp_key_fitness
5384 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
5385 uint64_t expected_attrs
,
5386 const struct nlattr
*key
, size_t key_len
)
5388 uint64_t missing_attrs
;
5389 uint64_t extra_attrs
;
5391 missing_attrs
= expected_attrs
& ~present_attrs
;
5392 if (missing_attrs
) {
5393 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5394 log_odp_key_attributes(&rl
, "expected but not present",
5395 missing_attrs
, 0, key
, key_len
);
5396 return ODP_FIT_TOO_LITTLE
;
5399 extra_attrs
= present_attrs
& ~expected_attrs
;
5400 if (extra_attrs
|| out_of_range_attr
) {
5401 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
5402 log_odp_key_attributes(&rl
, "present but not expected",
5403 extra_attrs
, out_of_range_attr
, key
, key_len
);
5404 return ODP_FIT_TOO_MUCH
;
5407 return ODP_FIT_PERFECT
;
5411 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5412 uint64_t present_attrs
, uint64_t *expected_attrs
,
5413 struct flow
*flow
, const struct flow
*src_flow
)
5415 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5416 bool is_mask
= flow
!= src_flow
;
5418 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
5419 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
5420 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5421 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
5422 ntohs(flow
->dl_type
));
5425 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
5426 flow
->dl_type
!= htons(0xffff)) {
5429 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
5432 /* Default ethertype for well-known L3 packets. */
5433 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
5434 flow
->dl_type
= htons(ETH_TYPE_IP
);
5435 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
5436 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
5437 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5438 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
5440 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
5442 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
5443 /* dl_type is mandatory for non-Ethernet packets */
5444 flow
->dl_type
= htons(0xffff);
5445 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
5446 /* See comments in odp_flow_key_from_flow__(). */
5447 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
5454 static enum odp_key_fitness
5455 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5456 uint64_t present_attrs
, int out_of_range_attr
,
5457 uint64_t expected_attrs
, struct flow
*flow
,
5458 const struct nlattr
*key
, size_t key_len
,
5459 const struct flow
*src_flow
)
5461 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5462 bool is_mask
= src_flow
!= flow
;
5463 const void *check_start
= NULL
;
5464 size_t check_len
= 0;
5465 enum ovs_key_attr expected_bit
= 0xff;
5467 if (eth_type_mpls(src_flow
->dl_type
)) {
5468 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5469 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
5471 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
5472 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
5473 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
5474 int n
= size
/ sizeof(ovs_be32
);
5477 if (!size
|| size
% sizeof(ovs_be32
)) {
5478 return ODP_FIT_ERROR
;
5480 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
5481 return ODP_FIT_ERROR
;
5484 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
5485 flow
->mpls_lse
[i
] = mpls_lse
[i
];
5487 if (n
> FLOW_MAX_MPLS_LABELS
) {
5488 return ODP_FIT_TOO_MUCH
;
5492 /* BOS may be set only in the innermost label. */
5493 for (i
= 0; i
< n
- 1; i
++) {
5494 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
5495 return ODP_FIT_ERROR
;
5499 /* BOS must be set in the innermost label. */
5500 if (n
< FLOW_MAX_MPLS_LABELS
5501 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
5502 return ODP_FIT_TOO_LITTLE
;
5508 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5510 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
5512 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
5513 const struct ovs_key_ipv4
*ipv4_key
;
5515 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
5516 put_ipv4_key(ipv4_key
, flow
, is_mask
);
5517 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
5518 return ODP_FIT_ERROR
;
5521 check_start
= ipv4_key
;
5522 check_len
= sizeof *ipv4_key
;
5523 expected_bit
= OVS_KEY_ATTR_IPV4
;
5526 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5528 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
5530 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
5531 const struct ovs_key_ipv6
*ipv6_key
;
5533 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
5534 put_ipv6_key(ipv6_key
, flow
, is_mask
);
5535 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
5536 return ODP_FIT_ERROR
;
5539 check_start
= ipv6_key
;
5540 check_len
= sizeof *ipv6_key
;
5541 expected_bit
= OVS_KEY_ATTR_IPV6
;
5544 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5545 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5547 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
5549 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
5550 const struct ovs_key_arp
*arp_key
;
5552 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
5553 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
5554 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
5555 "key", ntohs(arp_key
->arp_op
));
5556 return ODP_FIT_ERROR
;
5558 put_arp_key(arp_key
, flow
);
5560 check_start
= arp_key
;
5561 check_len
= sizeof *arp_key
;
5562 expected_bit
= OVS_KEY_ATTR_ARP
;
5565 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5567 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
5569 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
5570 const struct ovs_key_nsh
*nsh_key
;
5572 nsh_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
5573 put_nsh_key(nsh_key
, flow
, false);
5575 check_start
= nsh_key
;
5576 check_len
= sizeof *nsh_key
;
5577 expected_bit
= OVS_KEY_ATTR_NSH
;
5583 if (check_len
> 0) { /* Happens only when 'is_mask'. */
5584 if (!is_all_zeros(check_start
, check_len
) &&
5585 flow
->dl_type
!= htons(0xffff)) {
5586 return ODP_FIT_ERROR
;
5588 expected_attrs
|= UINT64_C(1) << expected_bit
;
5592 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
5593 if (src_flow
->nw_proto
== IPPROTO_TCP
5594 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5595 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5596 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5598 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
5600 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
5601 const union ovs_key_tp
*tcp_key
;
5603 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
5604 put_tp_key(tcp_key
, flow
);
5605 expected_bit
= OVS_KEY_ATTR_TCP
;
5607 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
5608 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
5609 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
5611 } else if (src_flow
->nw_proto
== IPPROTO_UDP
5612 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5613 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5614 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5616 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
5618 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
5619 const union ovs_key_tp
*udp_key
;
5621 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
5622 put_tp_key(udp_key
, flow
);
5623 expected_bit
= OVS_KEY_ATTR_UDP
;
5625 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
5626 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
5627 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
5628 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5630 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
5632 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
5633 const union ovs_key_tp
*sctp_key
;
5635 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
5636 put_tp_key(sctp_key
, flow
);
5637 expected_bit
= OVS_KEY_ATTR_SCTP
;
5639 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
5640 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
5641 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5643 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
5645 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
5646 const struct ovs_key_icmp
*icmp_key
;
5648 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
5649 flow
->tp_src
= htons(icmp_key
->icmp_type
);
5650 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
5651 expected_bit
= OVS_KEY_ATTR_ICMP
;
5653 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
5654 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5655 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5657 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
5659 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
5660 const struct ovs_key_icmpv6
*icmpv6_key
;
5662 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
5663 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
5664 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
5665 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
5666 if (is_nd(src_flow
, NULL
)) {
5668 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5670 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
5671 const struct ovs_key_nd
*nd_key
;
5673 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
5674 flow
->nd_target
= nd_key
->nd_target
;
5675 flow
->arp_sha
= nd_key
->nd_sll
;
5676 flow
->arp_tha
= nd_key
->nd_tll
;
5678 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
5679 * ICMP type and code are 8 bits wide. Therefore, an
5680 * exact match looks like htons(0xff), not
5681 * htons(0xffff). See xlate_wc_finish() for details.
5683 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
5684 (flow
->tp_src
!= htons(0xff) ||
5685 flow
->tp_dst
!= htons(0xff))) {
5686 return ODP_FIT_ERROR
;
5688 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
5695 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
5696 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
5697 return ODP_FIT_ERROR
;
5699 expected_attrs
|= UINT64_C(1) << expected_bit
;
5704 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
5708 /* Parse 802.1Q header then encapsulated L3 attributes. */
5709 static enum odp_key_fitness
5710 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
5711 uint64_t present_attrs
, int out_of_range_attr
,
5712 uint64_t expected_attrs
, struct flow
*flow
,
5713 const struct nlattr
*key
, size_t key_len
,
5714 const struct flow
*src_flow
)
5716 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5717 bool is_mask
= src_flow
!= flow
;
5719 const struct nlattr
*encap
;
5720 enum odp_key_fitness encap_fitness
;
5721 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
5724 while (encaps
< flow_vlan_limit
&&
5726 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
5727 : eth_type_vlan(flow
->dl_type
))) {
5729 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
5730 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
5732 /* Calculate fitness of outer attributes. */
5734 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
5735 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
5737 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5738 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5740 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
5741 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
5744 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
5745 expected_attrs
, key
, key_len
);
5748 * Remove the TPID from dl_type since it's not the real Ethertype. */
5749 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
5750 flow
->dl_type
= htons(0);
5751 flow
->vlans
[encaps
].tci
=
5752 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
5753 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
5756 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
5757 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5758 return ODP_FIT_TOO_LITTLE
;
5759 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
5760 /* Corner case for a truncated 802.1Q header. */
5761 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
5762 return ODP_FIT_TOO_MUCH
;
5765 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
5766 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
5767 "but CFI bit is not set",
5768 ntohs(flow
->vlans
[encaps
].tci
));
5769 return ODP_FIT_ERROR
;
5772 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
5777 /* Now parse the encapsulated attributes. */
5778 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
5779 attrs
, &present_attrs
, &out_of_range_attr
)) {
5780 return ODP_FIT_ERROR
;
5784 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
5786 return ODP_FIT_ERROR
;
5792 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5793 expected_attrs
, flow
, key
, key_len
,
5796 /* The overall fitness is the worse of the outer and inner attributes. */
5797 return MAX(fitness
, encap_fitness
);
5800 static enum odp_key_fitness
5801 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
5802 struct flow
*flow
, const struct flow
*src_flow
)
5804 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
5805 uint64_t expected_attrs
;
5806 uint64_t present_attrs
;
5807 int out_of_range_attr
;
5808 bool is_mask
= src_flow
!= flow
;
5810 memset(flow
, 0, sizeof *flow
);
5812 /* Parse attributes. */
5813 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
5814 &out_of_range_attr
)) {
5815 return ODP_FIT_ERROR
;
5820 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
5821 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
5822 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
5823 } else if (is_mask
) {
5824 /* Always exact match recirc_id if it is not specified. */
5825 flow
->recirc_id
= UINT32_MAX
;
5828 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
5829 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
5830 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
5832 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
5833 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
5834 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
5837 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
5838 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
5839 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
5842 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
5843 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
5845 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
5846 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
5848 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
5849 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
5850 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
5852 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
5853 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
5854 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
5856 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
5857 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
5858 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
5860 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
5861 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
5862 flow
->ct_nw_src
= ct
->ipv4_src
;
5863 flow
->ct_nw_dst
= ct
->ipv4_dst
;
5864 flow
->ct_nw_proto
= ct
->ipv4_proto
;
5865 flow
->ct_tp_src
= ct
->src_port
;
5866 flow
->ct_tp_dst
= ct
->dst_port
;
5867 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
5869 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
5870 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
5872 flow
->ct_ipv6_src
= ct
->ipv6_src
;
5873 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
5874 flow
->ct_nw_proto
= ct
->ipv6_proto
;
5875 flow
->ct_tp_src
= ct
->src_port
;
5876 flow
->ct_tp_dst
= ct
->dst_port
;
5877 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
5880 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
5881 enum odp_key_fitness res
;
5883 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
5885 if (res
== ODP_FIT_ERROR
) {
5886 return ODP_FIT_ERROR
;
5887 } else if (res
== ODP_FIT_PERFECT
) {
5888 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
5892 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
5893 flow
->in_port
.odp_port
5894 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
5895 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
5896 } else if (!is_mask
) {
5897 flow
->in_port
.odp_port
= ODPP_NONE
;
5900 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
5902 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
5903 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
5904 } else if (!is_mask
) {
5905 flow
->packet_type
= htonl(PT_ETH
);
5908 /* Check for Ethernet header. */
5909 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
5910 const struct ovs_key_ethernet
*eth_key
;
5912 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
5913 put_ethernet_key(eth_key
, flow
);
5915 flow
->packet_type
= htonl(PT_ETH
);
5917 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
5919 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
5920 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
5922 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
5925 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
5928 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
5929 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
5931 return ODP_FIT_ERROR
;
5935 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
5936 : eth_type_vlan(src_flow
->dl_type
)) {
5937 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
5938 expected_attrs
, flow
, key
, key_len
, src_flow
);
5941 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
5942 flow
->vlans
[0].tpid
= htons(0xffff);
5943 flow
->vlans
[0].tci
= htons(0xffff);
5944 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
5945 flow
->vlans
[0].tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
5946 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
5949 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
5950 expected_attrs
, flow
, key
, key_len
, src_flow
);
5953 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
5954 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
5955 * 'key' fits our expectations for what a flow key should contain.
5957 * The 'in_port' will be the datapath's understanding of the port. The
5958 * caller will need to translate with odp_port_to_ofp_port() if the
5959 * OpenFlow port is needed.
5961 * This function doesn't take the packet itself as an argument because none of
5962 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
5963 * it is always possible to infer which additional attribute(s) should appear
5964 * by looking at the attributes for lower-level protocols, e.g. if the network
5965 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
5966 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
5967 * must be absent. */
5968 enum odp_key_fitness
5969 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
5972 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
5975 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
5976 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
5977 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
5978 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
5979 * well 'key' fits our expectations for what a flow key should contain. */
5980 enum odp_key_fitness
5981 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
5982 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
5985 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
5986 &mask
->masks
, src_flow
);
5989 /* A missing mask means that the flow should be exact matched.
5990 * Generate an appropriate exact wildcard for the flow. */
5991 flow_wildcards_init_for_packet(mask
, src_flow
);
5993 return ODP_FIT_PERFECT
;
5997 /* Converts the netlink formated key/mask to match.
5998 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
5999 * disagree on the acceptable form of flow */
6001 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
6002 const struct nlattr
*mask
, size_t mask_len
,
6003 struct match
*match
)
6005 enum odp_key_fitness fitness
;
6007 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
);
6009 /* This should not happen: it indicates that
6010 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6011 * the acceptable form of a flow. Log the problem as an error,
6012 * with enough details to enable debugging. */
6013 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6015 if (!VLOG_DROP_ERR(&rl
)) {
6019 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
6020 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
6027 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
);
6029 /* This should not happen: it indicates that
6030 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6031 * disagree on the acceptable form of a mask. Log the problem
6032 * as an error, with enough details to enable debugging. */
6033 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6035 if (!VLOG_DROP_ERR(&rl
)) {
6039 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
6041 VLOG_ERR("internal error parsing flow mask %s (%s)",
6042 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
6052 /* Returns 'fitness' as a string, for use in debug messages. */
6054 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
6057 case ODP_FIT_PERFECT
:
6059 case ODP_FIT_TOO_MUCH
:
6061 case ODP_FIT_TOO_LITTLE
:
6062 return "too_little";
6070 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6071 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6072 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6073 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6074 * null, then the return value is not meaningful.) */
6076 odp_put_userspace_action(uint32_t pid
,
6077 const void *userdata
, size_t userdata_size
,
6078 odp_port_t tunnel_out_port
,
6079 bool include_actions
,
6080 struct ofpbuf
*odp_actions
)
6082 size_t userdata_ofs
;
6085 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
6086 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
6088 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
6090 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6091 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6094 * - The kernel rejected shorter userdata with -ERANGE.
6096 * - The kernel silently dropped userdata beyond the first 8 bytes.
6098 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6099 * separately disable features that required more than 8 bytes.) */
6100 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
6101 MAX(8, userdata_size
)),
6102 userdata
, userdata_size
);
6106 if (tunnel_out_port
!= ODPP_NONE
) {
6107 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
6110 if (include_actions
) {
6111 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
6113 nl_msg_end_nested(odp_actions
, offset
);
6115 return userdata_ofs
;
6119 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
6121 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
6125 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
6126 const struct eth_addr
*eth_src
,
6127 const struct eth_addr
*eth_dst
)
6129 struct ovs_action_push_eth eth
;
6131 memset(ð
, 0, sizeof eth
);
6133 eth
.addresses
.eth_src
= *eth_src
;
6136 eth
.addresses
.eth_dst
= *eth_dst
;
6139 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
6144 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
6145 struct ofpbuf
*odp_actions
)
6147 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6148 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
);
6149 nl_msg_end_nested(odp_actions
, offset
);
6153 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
6154 struct ovs_action_push_tnl
*data
)
6156 int size
= offsetof(struct ovs_action_push_tnl
, header
);
6158 size
+= data
->header_len
;
6159 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
6163 /* The commit_odp_actions() function and its helpers. */
6166 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
6167 const void *key
, size_t key_size
)
6169 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6170 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
6171 nl_msg_end_nested(odp_actions
, offset
);
6174 /* Masked set actions have a mask following the data within the netlink
6175 * attribute. The unmasked bits in the data will be cleared as the data
6176 * is copied to the action. */
6178 commit_masked_set_action(struct ofpbuf
*odp_actions
,
6179 enum ovs_key_attr key_type
,
6180 const void *key_
, const void *mask_
, size_t key_size
)
6182 size_t offset
= nl_msg_start_nested(odp_actions
,
6183 OVS_ACTION_ATTR_SET_MASKED
);
6184 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
6185 const char *key
= key_
, *mask
= mask_
;
6187 memcpy(data
+ key_size
, mask
, key_size
);
6188 /* Clear unmasked bits while copying. */
6189 while (key_size
--) {
6190 *data
++ = *key
++ & *mask
++;
6192 nl_msg_end_nested(odp_actions
, offset
);
6195 /* If any of the flow key data that ODP actions can modify are different in
6196 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
6197 * 'odp_actions' that change the flow tunneling information in key from
6198 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
6199 * same way. In other words, operates the same as commit_odp_actions(), but
6200 * only on tunneling information. */
6202 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
6203 struct ofpbuf
*odp_actions
)
6205 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
6206 * must have non-zero ipv6_dst. */
6207 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
6208 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
6211 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
6212 odp_put_tunnel_action(&base
->tunnel
, odp_actions
);
6217 commit(enum ovs_key_attr attr
, bool use_masked_set
,
6218 const void *key
, void *base
, void *mask
, size_t size
,
6219 struct ofpbuf
*odp_actions
)
6221 if (memcmp(key
, base
, size
)) {
6222 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
6224 if (use_masked_set
&& !fully_masked
) {
6225 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
6227 if (!fully_masked
) {
6228 memset(mask
, 0xff, size
);
6230 commit_set_action(odp_actions
, attr
, key
, size
);
6232 memcpy(base
, key
, size
);
6235 /* Mask bits are set when we have either read or set the corresponding
6236 * values. Masked bits will be exact-matched, no need to set them
6237 * if the value did not actually change. */
6243 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
6245 eth
->eth_src
= flow
->dl_src
;
6246 eth
->eth_dst
= flow
->dl_dst
;
6250 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
6252 flow
->dl_src
= eth
->eth_src
;
6253 flow
->dl_dst
= eth
->eth_dst
;
6257 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
6258 struct ofpbuf
*odp_actions
,
6259 struct flow_wildcards
*wc
,
6262 struct ovs_key_ethernet key
, base
, mask
;
6264 if (flow
->packet_type
!= htonl(PT_ETH
)) {
6268 get_ethernet_key(flow
, &key
);
6269 get_ethernet_key(base_flow
, &base
);
6270 get_ethernet_key(&wc
->masks
, &mask
);
6272 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
6273 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
6274 put_ethernet_key(&base
, base_flow
);
6275 put_ethernet_key(&mask
, &wc
->masks
);
6280 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
6281 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
6283 int base_n
= flow_count_vlan_headers(base
);
6284 int flow_n
= flow_count_vlan_headers(flow
);
6285 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
6287 /* Pop all mismatching vlan of base, push those of flow */
6288 for (; base_n
>= 0; base_n
--) {
6289 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
6290 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
6293 for (; flow_n
>= 0; flow_n
--) {
6294 struct ovs_action_push_vlan vlan
;
6296 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
6297 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
6298 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
6299 &vlan
, sizeof vlan
);
6301 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
6304 /* Wildcarding already done at action translation time. */
6306 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
6307 struct ofpbuf
*odp_actions
)
6309 int base_n
= flow_count_mpls_labels(base
, NULL
);
6310 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
6311 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
6314 while (base_n
> common_n
) {
6315 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
6316 /* If there is only one more LSE in base than there are common
6317 * between base and flow; and flow has at least one more LSE than
6318 * is common then the topmost LSE of base may be updated using
6320 struct ovs_key_mpls mpls_key
;
6322 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
6323 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
6324 &mpls_key
, sizeof mpls_key
);
6325 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
6328 /* Otherwise, if there more LSEs in base than are common between
6329 * base and flow then pop the topmost one. */
6333 /* If all the LSEs are to be popped and this is not the outermost
6334 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
6335 * POP_MPLS action instead of flow->dl_type.
6337 * This is because the POP_MPLS action requires its ethertype
6338 * argument to be an MPLS ethernet type but in this case
6339 * flow->dl_type will be a non-MPLS ethernet type.
6341 * When the final POP_MPLS action occurs it use flow->dl_type and
6342 * the and the resulting packet will have the desired dl_type. */
6343 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
6344 dl_type
= htons(ETH_TYPE_MPLS
);
6346 dl_type
= flow
->dl_type
;
6348 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
6349 popped
= flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
);
6355 /* If, after the above popping and setting, there are more LSEs in flow
6356 * than base then some LSEs need to be pushed. */
6357 while (base_n
< flow_n
) {
6358 struct ovs_action_push_mpls
*mpls
;
6360 mpls
= nl_msg_put_unspec_zero(odp_actions
,
6361 OVS_ACTION_ATTR_PUSH_MPLS
,
6363 mpls
->mpls_ethertype
= flow
->dl_type
;
6364 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
6365 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
6366 * headers if the flow is restored later due to returning from a patch
6367 * port or group bucket. */
6368 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
6369 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
6375 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
6377 ipv4
->ipv4_src
= flow
->nw_src
;
6378 ipv4
->ipv4_dst
= flow
->nw_dst
;
6379 ipv4
->ipv4_proto
= flow
->nw_proto
;
6380 ipv4
->ipv4_tos
= flow
->nw_tos
;
6381 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
6382 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
6386 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
6388 flow
->nw_src
= ipv4
->ipv4_src
;
6389 flow
->nw_dst
= ipv4
->ipv4_dst
;
6390 flow
->nw_proto
= ipv4
->ipv4_proto
;
6391 flow
->nw_tos
= ipv4
->ipv4_tos
;
6392 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
6393 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
6397 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
6398 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6401 struct ovs_key_ipv4 key
, mask
, base
;
6403 /* Check that nw_proto and nw_frag remain unchanged. */
6404 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
6405 flow
->nw_frag
== base_flow
->nw_frag
);
6407 get_ipv4_key(flow
, &key
, false);
6408 get_ipv4_key(base_flow
, &base
, false);
6409 get_ipv4_key(&wc
->masks
, &mask
, true);
6410 mask
.ipv4_proto
= 0; /* Not writeable. */
6411 mask
.ipv4_frag
= 0; /* Not writable. */
6413 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6415 put_ipv4_key(&base
, base_flow
, false);
6416 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
6417 put_ipv4_key(&mask
, &wc
->masks
, true);
6423 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
6425 ipv6
->ipv6_src
= flow
->ipv6_src
;
6426 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
6427 ipv6
->ipv6_label
= flow
->ipv6_label
;
6428 ipv6
->ipv6_proto
= flow
->nw_proto
;
6429 ipv6
->ipv6_tclass
= flow
->nw_tos
;
6430 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
6431 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
6435 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
6437 flow
->ipv6_src
= ipv6
->ipv6_src
;
6438 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
6439 flow
->ipv6_label
= ipv6
->ipv6_label
;
6440 flow
->nw_proto
= ipv6
->ipv6_proto
;
6441 flow
->nw_tos
= ipv6
->ipv6_tclass
;
6442 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
6443 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
6447 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
6448 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6451 struct ovs_key_ipv6 key
, mask
, base
;
6453 /* Check that nw_proto and nw_frag remain unchanged. */
6454 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
6455 flow
->nw_frag
== base_flow
->nw_frag
);
6457 get_ipv6_key(flow
, &key
, false);
6458 get_ipv6_key(base_flow
, &base
, false);
6459 get_ipv6_key(&wc
->masks
, &mask
, true);
6460 mask
.ipv6_proto
= 0; /* Not writeable. */
6461 mask
.ipv6_frag
= 0; /* Not writable. */
6463 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6465 put_ipv6_key(&base
, base_flow
, false);
6466 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
6467 put_ipv6_key(&mask
, &wc
->masks
, true);
6473 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
6475 /* ARP key has padding, clear it. */
6476 memset(arp
, 0, sizeof *arp
);
6478 arp
->arp_sip
= flow
->nw_src
;
6479 arp
->arp_tip
= flow
->nw_dst
;
6480 arp
->arp_op
= htons(flow
->nw_proto
);
6481 arp
->arp_sha
= flow
->arp_sha
;
6482 arp
->arp_tha
= flow
->arp_tha
;
6486 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
6488 flow
->nw_src
= arp
->arp_sip
;
6489 flow
->nw_dst
= arp
->arp_tip
;
6490 flow
->nw_proto
= ntohs(arp
->arp_op
);
6491 flow
->arp_sha
= arp
->arp_sha
;
6492 flow
->arp_tha
= arp
->arp_tha
;
6495 static enum slow_path_reason
6496 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
6497 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
6499 struct ovs_key_arp key
, mask
, base
;
6501 get_arp_key(flow
, &key
);
6502 get_arp_key(base_flow
, &base
);
6503 get_arp_key(&wc
->masks
, &mask
);
6505 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
6507 put_arp_key(&base
, base_flow
);
6508 put_arp_key(&mask
, &wc
->masks
);
6515 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
6517 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6518 icmp
->icmp_type
= ntohs(flow
->tp_src
);
6519 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
6523 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
6525 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
6526 flow
->tp_src
= htons(icmp
->icmp_type
);
6527 flow
->tp_dst
= htons(icmp
->icmp_code
);
6530 static enum slow_path_reason
6531 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
6532 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
6534 struct ovs_key_icmp key
, mask
, base
;
6535 enum ovs_key_attr attr
;
6537 if (is_icmpv4(flow
, NULL
)) {
6538 attr
= OVS_KEY_ATTR_ICMP
;
6539 } else if (is_icmpv6(flow
, NULL
)) {
6540 attr
= OVS_KEY_ATTR_ICMPV6
;
6545 get_icmp_key(flow
, &key
);
6546 get_icmp_key(base_flow
, &base
);
6547 get_icmp_key(&wc
->masks
, &mask
);
6549 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
6550 put_icmp_key(&base
, base_flow
);
6551 put_icmp_key(&mask
, &wc
->masks
);
6558 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
6560 nd
->nd_target
= flow
->nd_target
;
6561 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6562 nd
->nd_sll
= flow
->arp_sha
;
6563 nd
->nd_tll
= flow
->arp_tha
;
6567 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
6569 flow
->nd_target
= nd
->nd_target
;
6570 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
6571 flow
->arp_sha
= nd
->nd_sll
;
6572 flow
->arp_tha
= nd
->nd_tll
;
6575 static enum slow_path_reason
6576 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
6577 struct ofpbuf
*odp_actions
,
6578 struct flow_wildcards
*wc
, bool use_masked
)
6580 struct ovs_key_nd key
, mask
, base
;
6582 get_nd_key(flow
, &key
);
6583 get_nd_key(base_flow
, &base
);
6584 get_nd_key(&wc
->masks
, &mask
);
6586 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6588 put_nd_key(&base
, base_flow
);
6589 put_nd_key(&mask
, &wc
->masks
);
6596 static enum slow_path_reason
6597 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
6598 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6601 /* Check if 'flow' really has an L3 header. */
6602 if (!flow
->nw_proto
) {
6606 switch (ntohs(base
->dl_type
)) {
6608 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
6612 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
6613 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
6616 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
6623 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
6625 nsh
->flags
= flow
->nsh
.flags
;
6626 nsh
->mdtype
= flow
->nsh
.mdtype
;
6627 nsh
->np
= flow
->nsh
.np
;
6628 nsh
->path_hdr
= htonl((ntohl(flow
->nsh
.spi
) << NSH_SPI_SHIFT
) |
6631 for (int i
= 0; i
< 4; i
++) {
6632 nsh
->c
[i
] = flow
->nsh
.c
[i
];
6635 switch (nsh
->mdtype
) {
6637 for (int i
= 0; i
< 4; i
++) {
6638 nsh
->c
[i
] = flow
->nsh
.c
[i
];
6643 /* No match support for other MD formats yet. */
6650 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
6651 bool is_mask OVS_UNUSED
)
6653 flow
->nsh
.flags
= nsh
->flags
;
6654 flow
->nsh
.mdtype
= nsh
->mdtype
;
6655 flow
->nsh
.np
= nsh
->np
;
6656 flow
->nsh
.spi
= htonl((ntohl(nsh
->path_hdr
) & NSH_SPI_MASK
) >>
6658 flow
->nsh
.si
= (ntohl(nsh
->path_hdr
) & NSH_SI_MASK
) >> NSH_SI_SHIFT
;
6659 switch (nsh
->mdtype
) {
6661 for (int i
= 0; i
< 4; i
++) {
6662 flow
->nsh
.c
[i
] = nsh
->c
[i
];
6667 /* No match support for other MD formats yet. */
6668 memset(flow
->nsh
.c
, 0, sizeof flow
->nsh
.c
);
6674 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
6675 struct ofpbuf
*odp_actions
,
6676 struct flow_wildcards
*wc
,
6679 struct ovs_key_nsh key
, mask
, base
;
6681 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
6682 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
6686 /* Check that mdtype and np remain unchanged. */
6687 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
6688 flow
->nsh
.np
== base_flow
->nsh
.np
);
6690 get_nsh_key(flow
, &key
, false);
6691 get_nsh_key(base_flow
, &base
, false);
6692 get_nsh_key(&wc
->masks
, &mask
, true);
6693 mask
.mdtype
= 0; /* Not writable. */
6694 mask
.np
= 0; /* Not writable. */
6696 if (commit(OVS_KEY_ATTR_NSH
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6698 put_nsh_key(&base
, base_flow
, false);
6699 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
6700 put_nsh_key(&mask
, &wc
->masks
, true);
6705 /* TCP, UDP, and SCTP keys have the same layout. */
6706 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
6707 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
6710 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
6712 tp
->tcp
.tcp_src
= flow
->tp_src
;
6713 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
6717 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
6719 flow
->tp_src
= tp
->tcp
.tcp_src
;
6720 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
6724 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
6725 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6728 enum ovs_key_attr key_type
;
6729 union ovs_key_tp key
, mask
, base
;
6731 /* Check if 'flow' really has an L3 header. */
6732 if (!flow
->nw_proto
) {
6736 if (!is_ip_any(base_flow
)) {
6740 if (flow
->nw_proto
== IPPROTO_TCP
) {
6741 key_type
= OVS_KEY_ATTR_TCP
;
6742 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
6743 key_type
= OVS_KEY_ATTR_UDP
;
6744 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
6745 key_type
= OVS_KEY_ATTR_SCTP
;
6750 get_tp_key(flow
, &key
);
6751 get_tp_key(base_flow
, &base
);
6752 get_tp_key(&wc
->masks
, &mask
);
6754 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
6756 put_tp_key(&base
, base_flow
);
6757 put_tp_key(&mask
, &wc
->masks
);
6762 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
6763 struct ofpbuf
*odp_actions
,
6764 struct flow_wildcards
*wc
,
6767 uint32_t key
, mask
, base
;
6769 key
= flow
->skb_priority
;
6770 base
= base_flow
->skb_priority
;
6771 mask
= wc
->masks
.skb_priority
;
6773 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
6774 sizeof key
, odp_actions
)) {
6775 base_flow
->skb_priority
= base
;
6776 wc
->masks
.skb_priority
= mask
;
6781 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
6782 struct ofpbuf
*odp_actions
,
6783 struct flow_wildcards
*wc
,
6786 uint32_t key
, mask
, base
;
6788 key
= flow
->pkt_mark
;
6789 base
= base_flow
->pkt_mark
;
6790 mask
= wc
->masks
.pkt_mark
;
6792 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
6793 sizeof key
, odp_actions
)) {
6794 base_flow
->pkt_mark
= base
;
6795 wc
->masks
.pkt_mark
= mask
;
6800 odp_put_decap_nsh_action(struct ofpbuf
*odp_actions
)
6802 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_DECAP_NSH
);
6806 odp_put_encap_nsh_action(struct ofpbuf
*odp_actions
,
6807 const struct flow
*flow
,
6808 struct ofpbuf
*encap_data
)
6810 struct ovs_action_encap_nsh encap_nsh
;
6812 encap_nsh
.flags
= flow
->nsh
.flags
;
6813 encap_nsh
.mdtype
= flow
->nsh
.mdtype
;
6814 encap_nsh
.np
= flow
->nsh
.np
;
6815 encap_nsh
.path_hdr
= htonl((ntohl(flow
->nsh
.spi
) << NSH_SPI_SHIFT
) |
6818 switch (encap_nsh
.mdtype
) {
6820 struct nsh_md1_ctx
*md1
=
6821 ALIGNED_CAST(struct nsh_md1_ctx
*, encap_nsh
.metadata
);
6822 encap_nsh
.mdlen
= NSH_M_TYPE1_MDLEN
;
6823 for (int i
= 0; i
< 4; i
++) {
6824 put_16aligned_be32(&md1
->c
[i
], flow
->nsh
.c
[i
]);
6830 ovs_assert(encap_data
->size
< OVS_ENCAP_NSH_MAX_MD_LEN
);
6831 encap_nsh
.mdlen
= encap_data
->size
;
6832 memcpy(encap_nsh
.metadata
, encap_data
->data
, encap_data
->size
);
6834 encap_nsh
.mdlen
= 0;
6838 encap_nsh
.mdlen
= 0;
6841 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_ENCAP_NSH
,
6842 &encap_nsh
, sizeof(encap_nsh
));
6846 commit_packet_type_change(const struct flow
*flow
,
6847 struct flow
*base_flow
,
6848 struct ofpbuf
*odp_actions
,
6849 struct flow_wildcards
*wc
,
6851 struct ofpbuf
*encap_data
)
6853 if (flow
->packet_type
== base_flow
->packet_type
) {
6857 if (pending_encap
) {
6858 switch (ntohl(flow
->packet_type
)) {
6861 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
6863 base_flow
->packet_type
= flow
->packet_type
;
6864 base_flow
->dl_src
= flow
->dl_src
;
6865 base_flow
->dl_dst
= flow
->dl_dst
;
6870 odp_put_encap_nsh_action(odp_actions
, flow
, encap_data
);
6871 base_flow
->packet_type
= flow
->packet_type
;
6872 /* Update all packet headers in base_flow. */
6873 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
6874 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
6877 /* Only the above protocols are supported for encap.
6878 * The check is done at action translation. */
6882 /* This is an explicit or implicit decap case. */
6883 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
6884 base_flow
->packet_type
== htonl(PT_ETH
)) {
6885 /* Generate pop_eth and continue without recirculation. */
6886 odp_put_pop_eth_action(odp_actions
);
6887 base_flow
->packet_type
= flow
->packet_type
;
6888 base_flow
->dl_src
= eth_addr_zero
;
6889 base_flow
->dl_dst
= eth_addr_zero
;
6891 /* All other decap cases require recirculation.
6892 * No need to update the base flow here. */
6893 switch (ntohl(base_flow
->packet_type
)) {
6896 odp_put_decap_nsh_action(odp_actions
);
6899 /* Checks are done during translation. */
6905 wc
->masks
.packet_type
= OVS_BE32_MAX
;
6908 /* If any of the flow key data that ODP actions can modify are different in
6909 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
6910 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
6911 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
6912 * in addition to this function if needed. Sets fields in 'wc' that are
6913 * used as part of the action.
6915 * Returns a reason to force processing the flow's packets into the userspace
6916 * slow path, if there is one, otherwise 0. */
6917 enum slow_path_reason
6918 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
6919 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
6920 bool use_masked
, bool pending_encap
,
6921 struct ofpbuf
*encap_data
)
6923 enum slow_path_reason slow1
, slow2
;
6924 bool mpls_done
= false;
6926 commit_packet_type_change(flow
, base
, odp_actions
, wc
,
6927 pending_encap
, encap_data
);
6928 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
6929 /* Make packet a non-MPLS packet before committing L3/4 actions,
6930 * which would otherwise do nothing. */
6931 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
6932 commit_mpls_action(flow
, base
, odp_actions
);
6935 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
6936 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
6937 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
6938 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
6940 commit_mpls_action(flow
, base
, odp_actions
);
6942 commit_vlan_action(flow
, base
, odp_actions
, wc
);
6943 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
6944 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
6946 return slow1
? slow1
: slow2
;