2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
25 #include <netinet/icmp6.h>
26 #include <netinet/ip6.h>
30 #include "byte-order.h"
33 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/ofpbuf.h"
40 #include "tun-metadata.h"
41 #include "unaligned.h"
44 #include "openvswitch/vlog.h"
45 #include "openvswitch/match.h"
47 VLOG_DEFINE_THIS_MODULE(odp_util
);
49 /* The interface between userspace and kernel uses an "OVS_*" prefix.
50 * Since this is fairly non-specific for the OVS userspace components,
51 * "ODP_*" (Open vSwitch Datapath) is used as the prefix for
52 * interactions with the datapath.
55 /* The set of characters that may separate one action or one key attribute
57 static const char *delimiters
= ", \t\r\n";
58 static const char *delimiters_end
= ", \t\r\n)";
60 static int parse_odp_key_mask_attr(const char *, const struct simap
*port_names
,
61 struct ofpbuf
*, struct ofpbuf
*);
62 static void format_odp_key_attr(const struct nlattr
*a
,
63 const struct nlattr
*ma
,
64 const struct hmap
*portno_names
, struct ds
*ds
,
68 struct geneve_opt d
[63];
72 static int scan_geneve(const char *s
, struct geneve_scan
*key
,
73 struct geneve_scan
*mask
);
74 static void format_geneve_opts(const struct geneve_opt
*opt
,
75 const struct geneve_opt
*mask
, int opts_len
,
76 struct ds
*, bool verbose
);
78 static struct nlattr
*generate_all_wildcard_mask(const struct attr_len_tbl tbl
[],
79 int max
, struct ofpbuf
*,
80 const struct nlattr
*key
);
81 static void format_u128(struct ds
*d
, const ovs_32aligned_u128
*key
,
82 const ovs_32aligned_u128
*mask
, bool verbose
);
83 static int scan_u128(const char *s
, ovs_u128
*value
, ovs_u128
*mask
);
85 static int parse_odp_action(const char *s
, const struct simap
*port_names
,
86 struct ofpbuf
*actions
);
88 /* Returns one the following for the action with the given OVS_ACTION_ATTR_*
91 * - For an action whose argument has a fixed length, returned that
92 * nonnegative length in bytes.
94 * - For an action with a variable-length argument, returns ATTR_LEN_VARIABLE.
96 * - For an invalid 'type', returns ATTR_LEN_INVALID. */
98 odp_action_len(uint16_t type
)
100 if (type
> OVS_ACTION_ATTR_MAX
) {
104 switch ((enum ovs_action_attr
) type
) {
105 case OVS_ACTION_ATTR_OUTPUT
: return sizeof(uint32_t);
106 case OVS_ACTION_ATTR_TRUNC
: return sizeof(struct ovs_action_trunc
);
107 case OVS_ACTION_ATTR_TUNNEL_PUSH
: return ATTR_LEN_VARIABLE
;
108 case OVS_ACTION_ATTR_TUNNEL_POP
: return sizeof(uint32_t);
109 case OVS_ACTION_ATTR_METER
: return sizeof(uint32_t);
110 case OVS_ACTION_ATTR_USERSPACE
: return ATTR_LEN_VARIABLE
;
111 case OVS_ACTION_ATTR_PUSH_VLAN
: return sizeof(struct ovs_action_push_vlan
);
112 case OVS_ACTION_ATTR_POP_VLAN
: return 0;
113 case OVS_ACTION_ATTR_PUSH_MPLS
: return sizeof(struct ovs_action_push_mpls
);
114 case OVS_ACTION_ATTR_POP_MPLS
: return sizeof(ovs_be16
);
115 case OVS_ACTION_ATTR_RECIRC
: return sizeof(uint32_t);
116 case OVS_ACTION_ATTR_HASH
: return sizeof(struct ovs_action_hash
);
117 case OVS_ACTION_ATTR_SET
: return ATTR_LEN_VARIABLE
;
118 case OVS_ACTION_ATTR_SET_MASKED
: return ATTR_LEN_VARIABLE
;
119 case OVS_ACTION_ATTR_SAMPLE
: return ATTR_LEN_VARIABLE
;
120 case OVS_ACTION_ATTR_CT
: return ATTR_LEN_VARIABLE
;
121 case OVS_ACTION_ATTR_CT_CLEAR
: return 0;
122 case OVS_ACTION_ATTR_PUSH_ETH
: return sizeof(struct ovs_action_push_eth
);
123 case OVS_ACTION_ATTR_POP_ETH
: return 0;
124 case OVS_ACTION_ATTR_CLONE
: return ATTR_LEN_VARIABLE
;
125 case OVS_ACTION_ATTR_PUSH_NSH
: return ATTR_LEN_VARIABLE
;
126 case OVS_ACTION_ATTR_POP_NSH
: return 0;
128 case OVS_ACTION_ATTR_UNSPEC
:
129 case __OVS_ACTION_ATTR_MAX
:
130 return ATTR_LEN_INVALID
;
133 return ATTR_LEN_INVALID
;
136 /* Returns a string form of 'attr'. The return value is either a statically
137 * allocated constant string or the 'bufsize'-byte buffer 'namebuf'. 'bufsize'
138 * should be at least OVS_KEY_ATTR_BUFSIZE. */
139 enum { OVS_KEY_ATTR_BUFSIZE
= 3 + INT_STRLEN(unsigned int) + 1 };
141 ovs_key_attr_to_string(enum ovs_key_attr attr
, char *namebuf
, size_t bufsize
)
144 case OVS_KEY_ATTR_UNSPEC
: return "unspec";
145 case OVS_KEY_ATTR_ENCAP
: return "encap";
146 case OVS_KEY_ATTR_PRIORITY
: return "skb_priority";
147 case OVS_KEY_ATTR_SKB_MARK
: return "skb_mark";
148 case OVS_KEY_ATTR_CT_STATE
: return "ct_state";
149 case OVS_KEY_ATTR_CT_ZONE
: return "ct_zone";
150 case OVS_KEY_ATTR_CT_MARK
: return "ct_mark";
151 case OVS_KEY_ATTR_CT_LABELS
: return "ct_label";
152 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: return "ct_tuple4";
153 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: return "ct_tuple6";
154 case OVS_KEY_ATTR_TUNNEL
: return "tunnel";
155 case OVS_KEY_ATTR_IN_PORT
: return "in_port";
156 case OVS_KEY_ATTR_ETHERNET
: return "eth";
157 case OVS_KEY_ATTR_VLAN
: return "vlan";
158 case OVS_KEY_ATTR_ETHERTYPE
: return "eth_type";
159 case OVS_KEY_ATTR_IPV4
: return "ipv4";
160 case OVS_KEY_ATTR_IPV6
: return "ipv6";
161 case OVS_KEY_ATTR_TCP
: return "tcp";
162 case OVS_KEY_ATTR_TCP_FLAGS
: return "tcp_flags";
163 case OVS_KEY_ATTR_UDP
: return "udp";
164 case OVS_KEY_ATTR_SCTP
: return "sctp";
165 case OVS_KEY_ATTR_ICMP
: return "icmp";
166 case OVS_KEY_ATTR_ICMPV6
: return "icmpv6";
167 case OVS_KEY_ATTR_ARP
: return "arp";
168 case OVS_KEY_ATTR_ND
: return "nd";
169 case OVS_KEY_ATTR_MPLS
: return "mpls";
170 case OVS_KEY_ATTR_DP_HASH
: return "dp_hash";
171 case OVS_KEY_ATTR_RECIRC_ID
: return "recirc_id";
172 case OVS_KEY_ATTR_PACKET_TYPE
: return "packet_type";
173 case OVS_KEY_ATTR_NSH
: return "nsh";
175 case __OVS_KEY_ATTR_MAX
:
177 snprintf(namebuf
, bufsize
, "key%u", (unsigned int) attr
);
183 format_generic_odp_action(struct ds
*ds
, const struct nlattr
*a
)
185 size_t len
= nl_attr_get_size(a
);
187 ds_put_format(ds
, "action%d", nl_attr_type(a
));
189 const uint8_t *unspec
;
192 unspec
= nl_attr_get(a
);
193 for (i
= 0; i
< len
; i
++) {
194 ds_put_char(ds
, i
? ' ': '(');
195 ds_put_format(ds
, "%02x", unspec
[i
]);
197 ds_put_char(ds
, ')');
202 format_odp_sample_action(struct ds
*ds
, const struct nlattr
*attr
,
203 const struct hmap
*portno_names
)
205 static const struct nl_policy ovs_sample_policy
[] = {
206 [OVS_SAMPLE_ATTR_PROBABILITY
] = { .type
= NL_A_U32
},
207 [OVS_SAMPLE_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
}
209 struct nlattr
*a
[ARRAY_SIZE(ovs_sample_policy
)];
211 const struct nlattr
*nla_acts
;
214 ds_put_cstr(ds
, "sample");
216 if (!nl_parse_nested(attr
, ovs_sample_policy
, a
, ARRAY_SIZE(a
))) {
217 ds_put_cstr(ds
, "(error)");
221 percentage
= (100.0 * nl_attr_get_u32(a
[OVS_SAMPLE_ATTR_PROBABILITY
])) /
224 ds_put_format(ds
, "(sample=%.1f%%,", percentage
);
226 ds_put_cstr(ds
, "actions(");
227 nla_acts
= nl_attr_get(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
228 len
= nl_attr_get_size(a
[OVS_SAMPLE_ATTR_ACTIONS
]);
229 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
230 ds_put_format(ds
, "))");
234 format_odp_clone_action(struct ds
*ds
, const struct nlattr
*attr
,
235 const struct hmap
*portno_names
)
237 const struct nlattr
*nla_acts
= nl_attr_get(attr
);
238 int len
= nl_attr_get_size(attr
);
240 ds_put_cstr(ds
, "clone");
241 ds_put_format(ds
, "(");
242 format_odp_actions(ds
, nla_acts
, len
, portno_names
);
243 ds_put_format(ds
, ")");
247 format_nsh_key(struct ds
*ds
, const struct ovs_key_nsh
*key
)
249 ds_put_format(ds
, "flags=%d", key
->flags
);
250 ds_put_format(ds
, "ttl=%d", key
->ttl
);
251 ds_put_format(ds
, ",mdtype=%d", key
->mdtype
);
252 ds_put_format(ds
, ",np=%d", key
->np
);
253 ds_put_format(ds
, ",spi=0x%x",
254 nsh_path_hdr_to_spi_uint32(key
->path_hdr
));
255 ds_put_format(ds
, ",si=%d",
256 nsh_path_hdr_to_si(key
->path_hdr
));
258 switch (key
->mdtype
) {
260 for (int i
= 0; i
< 4; i
++) {
261 ds_put_format(ds
, ",c%d=0x%x", i
+ 1, ntohl(key
->context
[i
]));
266 /* No support for matching other metadata formats yet. */
272 format_uint8_masked(struct ds
*s
, bool *first
, const char *name
,
273 uint8_t value
, uint8_t mask
)
279 ds_put_format(s
, "%s=", name
);
280 if (mask
== UINT8_MAX
) {
281 ds_put_format(s
, "%"PRIu8
, value
);
283 ds_put_format(s
, "0x%02"PRIx8
"/0x%02"PRIx8
, value
, mask
);
290 format_be32_masked(struct ds
*s
, bool *first
, const char *name
,
291 ovs_be32 value
, ovs_be32 mask
)
293 if (mask
!= htonl(0)) {
297 ds_put_format(s
, "%s=", name
);
298 if (mask
== OVS_BE32_MAX
) {
299 ds_put_format(s
, "0x%"PRIx32
, ntohl(value
));
301 ds_put_format(s
, "0x%"PRIx32
"/0x%08"PRIx32
,
302 ntohl(value
), ntohl(mask
));
309 format_nsh_key_mask(struct ds
*ds
, const struct ovs_key_nsh
*key
,
310 const struct ovs_key_nsh
*mask
)
313 format_nsh_key(ds
, key
);
316 uint32_t spi
= nsh_path_hdr_to_spi_uint32(key
->path_hdr
);
317 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
->path_hdr
);
318 if (spi_mask
== (NSH_SPI_MASK
>> NSH_SPI_SHIFT
)) {
319 spi_mask
= UINT32_MAX
;
321 uint8_t si
= nsh_path_hdr_to_si(key
->path_hdr
);
322 uint8_t si_mask
= nsh_path_hdr_to_si(mask
->path_hdr
);
324 format_uint8_masked(ds
, &first
, "flags", key
->flags
, mask
->flags
);
325 format_uint8_masked(ds
, &first
, "ttl", key
->ttl
, mask
->ttl
);
326 format_uint8_masked(ds
, &first
, "mdtype", key
->mdtype
, mask
->mdtype
);
327 format_uint8_masked(ds
, &first
, "np", key
->np
, mask
->np
);
328 format_be32_masked(ds
, &first
, "spi", htonl(spi
), htonl(spi_mask
));
329 format_uint8_masked(ds
, &first
, "si", si
, si_mask
);
330 format_be32_masked(ds
, &first
, "c1", key
->context
[0],
332 format_be32_masked(ds
, &first
, "c2", key
->context
[1],
334 format_be32_masked(ds
, &first
, "c3", key
->context
[2],
336 format_be32_masked(ds
, &first
, "c4", key
->context
[3],
342 format_odp_push_nsh_action(struct ds
*ds
,
343 const struct nsh_hdr
*nsh_hdr
)
345 size_t mdlen
= nsh_hdr_len(nsh_hdr
) - NSH_BASE_HDR_LEN
;
346 uint32_t spi
= ntohl(nsh_get_spi(nsh_hdr
));
347 uint8_t si
= nsh_get_si(nsh_hdr
);
348 uint8_t flags
= nsh_get_flags(nsh_hdr
);
349 uint8_t ttl
= nsh_get_ttl(nsh_hdr
);
351 ds_put_cstr(ds
, "push_nsh(");
352 ds_put_format(ds
, "flags=%d", flags
);
353 ds_put_format(ds
, ",ttl=%d", ttl
);
354 ds_put_format(ds
, ",mdtype=%d", nsh_hdr
->md_type
);
355 ds_put_format(ds
, ",np=%d", nsh_hdr
->next_proto
);
356 ds_put_format(ds
, ",spi=0x%x", spi
);
357 ds_put_format(ds
, ",si=%d", si
);
358 switch (nsh_hdr
->md_type
) {
360 const struct nsh_md1_ctx
*md1_ctx
= &nsh_hdr
->md1
;
361 for (int i
= 0; i
< 4; i
++) {
362 ds_put_format(ds
, ",c%d=0x%x", i
+ 1,
363 ntohl(get_16aligned_be32(&md1_ctx
->context
[i
])));
368 const struct nsh_md2_tlv
*md2_ctx
= &nsh_hdr
->md2
;
369 ds_put_cstr(ds
, ",md2=");
370 ds_put_hex(ds
, md2_ctx
, mdlen
);
376 ds_put_format(ds
, ")");
380 slow_path_reason_to_string(uint32_t reason
)
382 switch ((enum slow_path_reason
) reason
) {
383 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return STRING;
392 slow_path_reason_to_explanation(enum slow_path_reason reason
)
395 #define SPR(ENUM, STRING, EXPLANATION) case ENUM: return EXPLANATION;
404 parse_odp_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
405 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
)
407 return parse_flags(s
, bit_to_string
, ')', NULL
, NULL
,
408 res_flags
, allowed
, res_mask
);
412 format_odp_userspace_action(struct ds
*ds
, const struct nlattr
*attr
,
413 const struct hmap
*portno_names
)
415 static const struct nl_policy ovs_userspace_policy
[] = {
416 [OVS_USERSPACE_ATTR_PID
] = { .type
= NL_A_U32
},
417 [OVS_USERSPACE_ATTR_USERDATA
] = { .type
= NL_A_UNSPEC
,
419 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = { .type
= NL_A_U32
,
421 [OVS_USERSPACE_ATTR_ACTIONS
] = { .type
= NL_A_UNSPEC
,
424 struct nlattr
*a
[ARRAY_SIZE(ovs_userspace_policy
)];
425 const struct nlattr
*userdata_attr
;
426 const struct nlattr
*tunnel_out_port_attr
;
428 if (!nl_parse_nested(attr
, ovs_userspace_policy
, a
, ARRAY_SIZE(a
))) {
429 ds_put_cstr(ds
, "userspace(error)");
433 ds_put_format(ds
, "userspace(pid=%"PRIu32
,
434 nl_attr_get_u32(a
[OVS_USERSPACE_ATTR_PID
]));
436 userdata_attr
= a
[OVS_USERSPACE_ATTR_USERDATA
];
439 const uint8_t *userdata
= nl_attr_get(userdata_attr
);
440 size_t userdata_len
= nl_attr_get_size(userdata_attr
);
441 bool userdata_unspec
= true;
442 struct user_action_cookie cookie
;
444 if (userdata_len
== sizeof cookie
) {
445 memcpy(&cookie
, userdata
, sizeof cookie
);
447 userdata_unspec
= false;
449 if (cookie
.type
== USER_ACTION_COOKIE_SFLOW
) {
450 ds_put_format(ds
, ",sFlow("
451 "vid=%"PRIu16
",pcp=%d,output=%"PRIu32
")",
452 vlan_tci_to_vid(cookie
.sflow
.vlan_tci
),
453 vlan_tci_to_pcp(cookie
.sflow
.vlan_tci
),
454 cookie
.sflow
.output
);
455 } else if (cookie
.type
== USER_ACTION_COOKIE_SLOW_PATH
) {
456 ds_put_cstr(ds
, ",slow_path(");
457 format_flags(ds
, slow_path_reason_to_string
,
458 cookie
.slow_path
.reason
, ',');
459 ds_put_format(ds
, ")");
460 } else if (cookie
.type
== USER_ACTION_COOKIE_FLOW_SAMPLE
) {
461 ds_put_format(ds
, ",flow_sample(probability=%"PRIu16
462 ",collector_set_id=%"PRIu32
463 ",obs_domain_id=%"PRIu32
464 ",obs_point_id=%"PRIu32
466 cookie
.flow_sample
.probability
,
467 cookie
.flow_sample
.collector_set_id
,
468 cookie
.flow_sample
.obs_domain_id
,
469 cookie
.flow_sample
.obs_point_id
);
470 odp_portno_name_format(portno_names
,
471 cookie
.flow_sample
.output_odp_port
, ds
);
472 if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_INGRESS
) {
473 ds_put_cstr(ds
, ",ingress");
474 } else if (cookie
.flow_sample
.direction
== NX_ACTION_SAMPLE_EGRESS
) {
475 ds_put_cstr(ds
, ",egress");
477 ds_put_char(ds
, ')');
478 } else if (cookie
.type
== USER_ACTION_COOKIE_IPFIX
) {
479 ds_put_format(ds
, ",ipfix(output_port=");
480 odp_portno_name_format(portno_names
,
481 cookie
.ipfix
.output_odp_port
, ds
);
482 ds_put_char(ds
, ')');
483 } else if (cookie
.type
== USER_ACTION_COOKIE_CONTROLLER
) {
484 ds_put_format(ds
, ",controller(reason=%"PRIu16
488 ",rule_cookie=%#"PRIx64
489 ",controller_id=%"PRIu16
491 cookie
.controller
.reason
,
492 cookie
.controller
.dont_send
? 1 : 0,
493 cookie
.controller
.continuation
? 1 : 0,
494 cookie
.controller
.recirc_id
,
495 ntohll(get_32aligned_be64(
496 &cookie
.controller
.rule_cookie
)),
497 cookie
.controller
.controller_id
,
498 cookie
.controller
.max_len
);
499 ds_put_char(ds
, ')');
501 userdata_unspec
= true;
505 if (userdata_unspec
) {
507 ds_put_format(ds
, ",userdata(");
508 for (i
= 0; i
< userdata_len
; i
++) {
509 ds_put_format(ds
, "%02x", userdata
[i
]);
511 ds_put_char(ds
, ')');
515 if (a
[OVS_USERSPACE_ATTR_ACTIONS
]) {
516 ds_put_cstr(ds
, ",actions");
519 tunnel_out_port_attr
= a
[OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
];
520 if (tunnel_out_port_attr
) {
521 ds_put_format(ds
, ",tunnel_out_port=");
522 odp_portno_name_format(portno_names
,
523 nl_attr_get_odp_port(tunnel_out_port_attr
), ds
);
526 ds_put_char(ds
, ')');
530 format_vlan_tci(struct ds
*ds
, ovs_be16 tci
, ovs_be16 mask
, bool verbose
)
532 if (verbose
|| vlan_tci_to_vid(tci
) || vlan_tci_to_vid(mask
)) {
533 ds_put_format(ds
, "vid=%"PRIu16
, vlan_tci_to_vid(tci
));
534 if (vlan_tci_to_vid(mask
) != VLAN_VID_MASK
) { /* Partially masked. */
535 ds_put_format(ds
, "/0x%"PRIx16
, vlan_tci_to_vid(mask
));
537 ds_put_char(ds
, ',');
539 if (verbose
|| vlan_tci_to_pcp(tci
) || vlan_tci_to_pcp(mask
)) {
540 ds_put_format(ds
, "pcp=%d", vlan_tci_to_pcp(tci
));
541 if (vlan_tci_to_pcp(mask
) != (VLAN_PCP_MASK
>> VLAN_PCP_SHIFT
)) {
542 ds_put_format(ds
, "/0x%x", vlan_tci_to_pcp(mask
));
544 ds_put_char(ds
, ',');
546 if (!(tci
& htons(VLAN_CFI
))) {
547 ds_put_cstr(ds
, "cfi=0");
548 ds_put_char(ds
, ',');
554 format_mpls_lse(struct ds
*ds
, ovs_be32 mpls_lse
)
556 ds_put_format(ds
, "label=%"PRIu32
",tc=%d,ttl=%d,bos=%d",
557 mpls_lse_to_label(mpls_lse
),
558 mpls_lse_to_tc(mpls_lse
),
559 mpls_lse_to_ttl(mpls_lse
),
560 mpls_lse_to_bos(mpls_lse
));
564 format_mpls(struct ds
*ds
, const struct ovs_key_mpls
*mpls_key
,
565 const struct ovs_key_mpls
*mpls_mask
, int n
)
567 for (int i
= 0; i
< n
; i
++) {
568 ovs_be32 key
= mpls_key
[i
].mpls_lse
;
570 if (mpls_mask
== NULL
) {
571 format_mpls_lse(ds
, key
);
573 ovs_be32 mask
= mpls_mask
[i
].mpls_lse
;
575 ds_put_format(ds
, "label=%"PRIu32
"/0x%x,tc=%d/%x,ttl=%d/0x%x,bos=%d/%x",
576 mpls_lse_to_label(key
), mpls_lse_to_label(mask
),
577 mpls_lse_to_tc(key
), mpls_lse_to_tc(mask
),
578 mpls_lse_to_ttl(key
), mpls_lse_to_ttl(mask
),
579 mpls_lse_to_bos(key
), mpls_lse_to_bos(mask
));
581 ds_put_char(ds
, ',');
587 format_odp_recirc_action(struct ds
*ds
, uint32_t recirc_id
)
589 ds_put_format(ds
, "recirc(%#"PRIx32
")", recirc_id
);
593 format_odp_hash_action(struct ds
*ds
, const struct ovs_action_hash
*hash_act
)
595 ds_put_format(ds
, "hash(");
597 if (hash_act
->hash_alg
== OVS_HASH_ALG_L4
) {
598 ds_put_format(ds
, "hash_l4(%"PRIu32
")", hash_act
->hash_basis
);
600 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
603 ds_put_format(ds
, ")");
607 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
609 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
610 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
611 ntohs(udp
->udp_csum
));
617 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
619 const struct eth_header
*eth
;
622 const struct udp_header
*udp
;
624 eth
= (const struct eth_header
*)data
->header
;
629 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
630 data
->header_len
, data
->tnl_type
);
631 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
632 ds_put_format(ds
, ",src=");
633 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
634 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
636 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
638 const struct ip_header
*ip
= l3
;
639 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
640 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
641 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
642 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
643 ip
->ip_proto
, ip
->ip_tos
,
645 ntohs(ip
->ip_frag_off
));
648 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
649 struct in6_addr src
, dst
;
650 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
651 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
652 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
654 ds_put_format(ds
, "ipv6(src=");
655 ipv6_format_addr(&src
, ds
);
656 ds_put_format(ds
, ",dst=");
657 ipv6_format_addr(&dst
, ds
);
658 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
659 ",hlimit=%"PRIu8
"),",
660 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
661 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
665 udp
= (const struct udp_header
*) l4
;
667 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
668 const struct vxlanhdr
*vxh
;
670 vxh
= format_udp_tnl_push_header(ds
, udp
);
672 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
673 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
674 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
675 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
676 const struct genevehdr
*gnh
;
678 gnh
= format_udp_tnl_push_header(ds
, udp
);
680 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
681 gnh
->oam
? "oam," : "",
682 gnh
->critical
? "crit," : "",
683 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
686 ds_put_cstr(ds
, ",options(");
687 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
689 ds_put_char(ds
, ')');
692 ds_put_char(ds
, ')');
693 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
694 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
695 const struct gre_base_hdr
*greh
;
696 ovs_16aligned_be32
*options
;
698 greh
= (const struct gre_base_hdr
*) l4
;
700 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
701 ntohs(greh
->flags
), ntohs(greh
->protocol
));
702 options
= (ovs_16aligned_be32
*)(greh
+ 1);
703 if (greh
->flags
& htons(GRE_CSUM
)) {
704 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
707 if (greh
->flags
& htons(GRE_KEY
)) {
708 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
711 if (greh
->flags
& htons(GRE_SEQ
)) {
712 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
715 ds_put_format(ds
, ")");
716 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
717 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
718 const struct gre_base_hdr
*greh
;
719 const struct erspan_base_hdr
*ersh
;
721 greh
= (const struct gre_base_hdr
*) l4
;
722 ersh
= ERSPAN_HDR(greh
);
724 if (ersh
->ver
== 1) {
725 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
727 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
728 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
729 } else if (ersh
->ver
== 2) {
730 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
732 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
733 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
734 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
736 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
739 ds_put_format(ds
, ")");
743 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
744 const struct hmap
*portno_names
)
746 struct ovs_action_push_tnl
*data
;
748 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
750 ds_put_cstr(ds
, "tnl_push(tnl_port(");
751 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
752 ds_put_cstr(ds
, "),");
753 format_odp_tnl_push_header(ds
, data
);
754 ds_put_format(ds
, ",out_port(");
755 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
756 ds_put_cstr(ds
, "))");
759 static const struct nl_policy ovs_nat_policy
[] = {
760 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
761 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
762 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
763 .min_len
= sizeof(struct in_addr
),
764 .max_len
= sizeof(struct in6_addr
)},
765 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
766 .min_len
= sizeof(struct in_addr
),
767 .max_len
= sizeof(struct in6_addr
)},
768 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
769 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
770 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
771 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
772 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
776 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
778 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
780 ovs_be32 ip_min
, ip_max
;
781 struct in6_addr ip6_min
, ip6_max
;
782 uint16_t proto_min
, proto_max
;
784 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
785 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
788 /* If no type, then nothing else either. */
789 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
790 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
791 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
792 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
793 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
794 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
797 /* Both SNAT & DNAT may not be specified. */
798 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
799 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
802 /* proto may not appear without ip. */
803 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
804 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
807 /* MAX may not appear without MIN. */
808 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
809 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
810 ds_put_cstr(ds
, "nat(error: range max without min.)");
813 /* Address sizes must match. */
814 if ((a
[OVS_NAT_ATTR_IP_MIN
]
815 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
816 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
817 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
818 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
819 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
820 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
824 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
825 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
826 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
827 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
828 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
829 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
830 if (addr_len
== sizeof ip6_min
) {
831 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
832 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
834 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
835 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
838 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
839 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
840 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
841 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
843 if ((addr_len
== sizeof(ovs_be32
)
844 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
845 || (addr_len
== sizeof(struct in6_addr
)
846 && !ipv6_mask_is_any(&ip6_max
)
847 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
848 || (proto_max
&& proto_min
> proto_max
)) {
849 ds_put_cstr(ds
, "nat(range error)");
853 ds_put_cstr(ds
, "nat");
854 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
855 ds_put_char(ds
, '(');
856 if (a
[OVS_NAT_ATTR_SRC
]) {
857 ds_put_cstr(ds
, "src");
858 } else if (a
[OVS_NAT_ATTR_DST
]) {
859 ds_put_cstr(ds
, "dst");
863 ds_put_cstr(ds
, "=");
865 if (addr_len
== sizeof ip_min
) {
866 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
868 if (ip_max
&& ip_max
!= ip_min
) {
869 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
871 } else if (addr_len
== sizeof ip6_min
) {
872 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
874 if (!ipv6_mask_is_any(&ip6_max
) &&
875 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
876 ds_put_char(ds
, '-');
877 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
881 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
883 if (proto_max
&& proto_max
!= proto_min
) {
884 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
888 ds_put_char(ds
, ',');
889 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
890 ds_put_cstr(ds
, "persistent,");
892 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
893 ds_put_cstr(ds
, "hash,");
895 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
896 ds_put_cstr(ds
, "random,");
899 ds_put_char(ds
, ')');
903 static const struct nl_policy ovs_conntrack_policy
[] = {
904 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
905 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
906 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
907 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
908 .min_len
= sizeof(uint32_t) * 2 },
909 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
910 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
911 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
912 .min_len
= 1, .max_len
= 16 },
913 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
917 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
919 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
921 ovs_32aligned_u128 value
;
922 ovs_32aligned_u128 mask
;
924 const uint32_t *mark
;
928 const struct nlattr
*nat
;
930 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
931 ds_put_cstr(ds
, "ct(error)");
935 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
936 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
937 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
938 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
939 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
940 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
941 nat
= a
[OVS_CT_ATTR_NAT
];
943 ds_put_format(ds
, "ct");
944 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
945 ds_put_cstr(ds
, "(");
947 ds_put_format(ds
, "commit,");
950 ds_put_format(ds
, "force_commit,");
953 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
956 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
960 ds_put_format(ds
, "label=");
961 format_u128(ds
, &label
->value
, &label
->mask
, true);
962 ds_put_char(ds
, ',');
965 ds_put_format(ds
, "helper=%s,", helper
);
968 format_odp_ct_nat(ds
, nat
);
971 ds_put_cstr(ds
, ")");
975 static const struct attr_len_tbl
976 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
977 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
978 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
979 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
983 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
986 const struct nlattr
*a
;
987 struct ovs_key_nsh nsh
;
988 struct ovs_key_nsh nsh_mask
;
990 memset(&nsh
, 0, sizeof nsh
);
991 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
993 NL_NESTED_FOR_EACH (a
, left
, attr
) {
994 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
995 size_t len
= nl_attr_get_size(a
);
997 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1001 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1002 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1007 case OVS_NSH_KEY_ATTR_UNSPEC
:
1009 case OVS_NSH_KEY_ATTR_BASE
: {
1010 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1011 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1012 memcpy(&nsh
, base
, sizeof(*base
));
1013 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1016 case OVS_NSH_KEY_ATTR_MD1
: {
1017 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1018 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1019 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1020 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1023 case OVS_NSH_KEY_ATTR_MD2
:
1024 case __OVS_NSH_KEY_ATTR_MAX
:
1026 /* No support for matching other metadata formats yet. */
1031 ds_put_cstr(ds
, "set(nsh(");
1032 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1033 ds_put_cstr(ds
, "))");
1038 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1039 const struct hmap
*portno_names
)
1042 enum ovs_action_attr type
= nl_attr_type(a
);
1045 expected_len
= odp_action_len(nl_attr_type(a
));
1046 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1047 nl_attr_get_size(a
) != expected_len
) {
1048 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1049 nl_attr_get_size(a
), expected_len
);
1050 format_generic_odp_action(ds
, a
);
1055 case OVS_ACTION_ATTR_METER
:
1056 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1058 case OVS_ACTION_ATTR_OUTPUT
:
1059 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1061 case OVS_ACTION_ATTR_TRUNC
: {
1062 const struct ovs_action_trunc
*trunc
=
1063 nl_attr_get_unspec(a
, sizeof *trunc
);
1065 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1068 case OVS_ACTION_ATTR_TUNNEL_POP
:
1069 ds_put_cstr(ds
, "tnl_pop(");
1070 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1071 ds_put_char(ds
, ')');
1073 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1074 format_odp_tnl_push_action(ds
, a
, portno_names
);
1076 case OVS_ACTION_ATTR_USERSPACE
:
1077 format_odp_userspace_action(ds
, a
, portno_names
);
1079 case OVS_ACTION_ATTR_RECIRC
:
1080 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1082 case OVS_ACTION_ATTR_HASH
:
1083 format_odp_hash_action(ds
, nl_attr_get(a
));
1085 case OVS_ACTION_ATTR_SET_MASKED
:
1087 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1088 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1089 format_odp_set_nsh(ds
, a
);
1092 size
= nl_attr_get_size(a
) / 2;
1093 ds_put_cstr(ds
, "set(");
1095 /* Masked set action not supported for tunnel key, which is bigger. */
1096 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1097 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1098 sizeof(struct nlattr
))];
1099 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1100 sizeof(struct nlattr
))];
1102 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1103 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1104 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1105 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1106 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1108 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1110 ds_put_cstr(ds
, ")");
1112 case OVS_ACTION_ATTR_SET
:
1113 ds_put_cstr(ds
, "set(");
1114 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1115 ds_put_cstr(ds
, ")");
1117 case OVS_ACTION_ATTR_PUSH_ETH
: {
1118 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1119 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1120 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1121 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1124 case OVS_ACTION_ATTR_POP_ETH
:
1125 ds_put_cstr(ds
, "pop_eth");
1127 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1128 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1129 ds_put_cstr(ds
, "push_vlan(");
1130 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1131 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1133 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1134 ds_put_char(ds
, ')');
1137 case OVS_ACTION_ATTR_POP_VLAN
:
1138 ds_put_cstr(ds
, "pop_vlan");
1140 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1141 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1142 ds_put_cstr(ds
, "push_mpls(");
1143 format_mpls_lse(ds
, mpls
->mpls_lse
);
1144 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1147 case OVS_ACTION_ATTR_POP_MPLS
: {
1148 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1149 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1152 case OVS_ACTION_ATTR_SAMPLE
:
1153 format_odp_sample_action(ds
, a
, portno_names
);
1155 case OVS_ACTION_ATTR_CT
:
1156 format_odp_conntrack_action(ds
, a
);
1158 case OVS_ACTION_ATTR_CT_CLEAR
:
1159 ds_put_cstr(ds
, "ct_clear");
1161 case OVS_ACTION_ATTR_CLONE
:
1162 format_odp_clone_action(ds
, a
, portno_names
);
1164 case OVS_ACTION_ATTR_PUSH_NSH
: {
1165 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1166 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1167 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1168 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1169 format_odp_push_nsh_action(ds
, nsh_hdr
);
1172 case OVS_ACTION_ATTR_POP_NSH
:
1173 ds_put_cstr(ds
, "pop_nsh()");
1175 case OVS_ACTION_ATTR_UNSPEC
:
1176 case __OVS_ACTION_ATTR_MAX
:
1178 format_generic_odp_action(ds
, a
);
1184 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1185 size_t actions_len
, const struct hmap
*portno_names
)
1188 const struct nlattr
*a
;
1191 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1193 ds_put_char(ds
, ',');
1195 format_odp_action(ds
, a
, portno_names
);
1200 if (left
== actions_len
) {
1201 ds_put_cstr(ds
, "<empty>");
1203 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1204 for (i
= 0; i
< left
; i
++) {
1205 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1207 ds_put_char(ds
, ')');
1210 ds_put_cstr(ds
, "drop");
1214 /* Separate out parse_odp_userspace_action() function. */
1216 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1219 struct user_action_cookie cookie
;
1221 odp_port_t tunnel_out_port
;
1223 void *user_data
= NULL
;
1224 size_t user_data_size
= 0;
1225 bool include_actions
= false;
1228 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1232 ofpbuf_init(&buf
, 16);
1233 memset(&cookie
, 0, sizeof cookie
);
1235 user_data
= &cookie
;
1236 user_data_size
= sizeof cookie
;
1239 uint32_t probability
;
1240 uint32_t collector_set_id
;
1241 uint32_t obs_domain_id
;
1242 uint32_t obs_point_id
;
1244 /* USER_ACTION_COOKIE_CONTROLLER. */
1246 uint8_t continuation
;
1249 uint64_t rule_cookie
;
1250 uint16_t controller_id
;
1255 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1256 "pcp=%i,output=%"SCNi32
")%n",
1257 &vid
, &pcp
, &output
, &n1
)) {
1261 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1266 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1267 cookie
.ofp_in_port
= OFPP_NONE
;
1268 cookie
.ofproto_uuid
= UUID_ZERO
;
1269 cookie
.sflow
.vlan_tci
= htons(tci
);
1270 cookie
.sflow
.output
= output
;
1271 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1274 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1275 cookie
.ofp_in_port
= OFPP_NONE
;
1276 cookie
.ofproto_uuid
= UUID_ZERO
;
1277 cookie
.slow_path
.reason
= 0;
1279 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1280 &cookie
.slow_path
.reason
,
1281 SLOW_PATH_REASON_MASK
, NULL
);
1282 if (res
< 0 || s
[n
+ res
] != ')') {
1286 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1287 "collector_set_id=%"SCNi32
","
1288 "obs_domain_id=%"SCNi32
","
1289 "obs_point_id=%"SCNi32
","
1290 "output_port=%"SCNi32
"%n",
1291 &probability
, &collector_set_id
,
1292 &obs_domain_id
, &obs_point_id
,
1296 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1297 cookie
.ofp_in_port
= OFPP_NONE
;
1298 cookie
.ofproto_uuid
= UUID_ZERO
;
1299 cookie
.flow_sample
.probability
= probability
;
1300 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1301 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1302 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1303 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1305 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1306 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1308 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1309 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1312 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1319 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1322 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1323 cookie
.ofp_in_port
= OFPP_NONE
;
1324 cookie
.ofproto_uuid
= UUID_ZERO
;
1325 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1326 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1328 ",continuation=%"SCNu8
1329 ",recirc_id=%"SCNu32
1330 ",rule_cookie=%"SCNx64
1331 ",controller_id=%"SCNu16
1332 ",max_len=%"SCNu16
")%n",
1333 &reason
, &dont_send
, &continuation
, &recirc_id
,
1334 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1336 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1337 cookie
.ofp_in_port
= OFPP_NONE
;
1338 cookie
.ofproto_uuid
= UUID_ZERO
;
1339 cookie
.controller
.dont_send
= dont_send
? true : false;
1340 cookie
.controller
.continuation
= continuation
? true : false;
1341 cookie
.controller
.reason
= reason
;
1342 cookie
.controller
.recirc_id
= recirc_id
;
1343 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1344 htonll(rule_cookie
));
1345 cookie
.controller
.controller_id
= controller_id
;
1346 cookie
.controller
.max_len
= max_len
;
1347 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1351 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1352 if (end
[0] != ')') {
1356 user_data
= buf
.data
;
1357 user_data_size
= buf
.size
;
1364 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1366 include_actions
= true;
1372 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1373 &tunnel_out_port
, &n1
)) {
1374 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1375 tunnel_out_port
, include_actions
, actions
);
1378 } else if (s
[n
] == ')') {
1379 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1380 ODPP_NONE
, include_actions
, actions
);
1387 struct ovs_action_push_eth push
;
1391 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1392 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1393 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1394 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1397 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1398 &push
, sizeof push
);
1405 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1406 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1413 ofpbuf_uninit(&buf
);
1418 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1420 struct eth_header
*eth
;
1421 struct ip_header
*ip
;
1422 struct ovs_16aligned_ip6_hdr
*ip6
;
1423 struct udp_header
*udp
;
1424 struct gre_base_hdr
*greh
;
1425 struct erspan_base_hdr
*ersh
;
1426 struct erspan_md2
*md2
;
1427 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
, sid
;
1429 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1434 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1437 eth
= (struct eth_header
*) data
->header
;
1438 l3
= (struct ip_header
*) (eth
+ 1);
1439 ip
= (struct ip_header
*) l3
;
1440 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1441 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1442 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1445 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1449 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1450 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1453 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1456 eth
->eth_type
= htons(dl_type
);
1458 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1460 uint16_t ip_frag_off
;
1461 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1462 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1465 &ip
->ip_proto
, &ip
->ip_tos
,
1466 &ip
->ip_ttl
, &ip_frag_off
)) {
1469 put_16aligned_be32(&ip
->ip_src
, sip
);
1470 put_16aligned_be32(&ip
->ip_dst
, dip
);
1471 ip
->ip_frag_off
= htons(ip_frag_off
);
1472 ip_len
= sizeof *ip
;
1474 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1475 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1476 struct in6_addr sip6
, dip6
;
1479 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1480 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1481 ",hlimit=%"SCNi8
"),",
1482 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1483 &tclass
, &ip6
->ip6_hlim
)
1484 || (label
& ~IPV6_LABEL_MASK
) != 0
1485 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1486 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1489 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1490 htonl(tclass
<< 20) | htonl(label
));
1491 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1492 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1493 ip_len
= sizeof *ip6
;
1497 l4
= ((uint8_t *) l3
+ ip_len
);
1498 udp
= (struct udp_header
*) l4
;
1499 greh
= (struct gre_base_hdr
*) l4
;
1500 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1501 &udp_src
, &udp_dst
, &csum
)) {
1502 uint32_t vx_flags
, vni
;
1504 udp
->udp_src
= htons(udp_src
);
1505 udp
->udp_dst
= htons(udp_dst
);
1507 udp
->udp_csum
= htons(csum
);
1509 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1511 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1513 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1514 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1515 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1516 header_len
= sizeof *eth
+ ip_len
+
1517 sizeof *udp
+ sizeof *vxh
;
1518 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1519 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1521 memset(gnh
, 0, sizeof *gnh
);
1522 header_len
= sizeof *eth
+ ip_len
+
1523 sizeof *udp
+ sizeof *gnh
;
1525 if (ovs_scan_len(s
, &n
, "oam,")) {
1528 if (ovs_scan_len(s
, &n
, "crit,")) {
1531 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1534 if (ovs_scan_len(s
, &n
, ",options(")) {
1535 struct geneve_scan options
;
1538 memset(&options
, 0, sizeof options
);
1539 len
= scan_geneve(s
+ n
, &options
, NULL
);
1544 memcpy(gnh
->options
, options
.d
, options
.len
);
1545 gnh
->opt_len
= options
.len
/ 4;
1546 header_len
+= options
.len
;
1550 if (!ovs_scan_len(s
, &n
, "))")) {
1554 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1555 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1556 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1560 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1561 &gre_flags
, &gre_proto
)){
1563 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1564 tnl_type
= OVS_VPORT_TYPE_GRE
;
1566 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1568 greh
->flags
= htons(gre_flags
);
1569 greh
->protocol
= htons(gre_proto
);
1570 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1572 if (greh
->flags
& htons(GRE_CSUM
)) {
1573 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1577 memset(options
, 0, sizeof *options
);
1578 *((ovs_be16
*)options
) = htons(csum
);
1581 if (greh
->flags
& htons(GRE_KEY
)) {
1584 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1588 put_16aligned_be32(options
, htonl(key
));
1591 if (greh
->flags
& htons(GRE_SEQ
)) {
1594 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1597 put_16aligned_be32(options
, htonl(seq
));
1601 if (!ovs_scan_len(s
, &n
, "))")) {
1605 header_len
= sizeof *eth
+ ip_len
+
1606 ((uint8_t *) options
- (uint8_t *) greh
);
1607 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1608 &sid
, &erspan_idx
)) {
1609 ersh
= ERSPAN_HDR(greh
);
1610 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1613 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1614 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1616 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1619 greh
->flags
= htons(GRE_SEQ
);
1620 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1624 put_16aligned_be32(index
, htonl(erspan_idx
));
1626 if (!ovs_scan_len(s
, &n
, ")")) {
1629 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1630 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1632 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1633 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1635 ersh
= ERSPAN_HDR(greh
);
1636 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1638 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1639 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1641 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1644 greh
->flags
= htons(GRE_SEQ
);
1645 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1649 set_hwid(md2
, hwid
);
1652 if (!ovs_scan_len(s
, &n
, ")")) {
1656 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1657 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1662 /* check tunnel meta data. */
1663 if (data
->tnl_type
!= tnl_type
) {
1666 if (data
->header_len
!= header_len
) {
1671 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1678 struct ct_nat_params
{
1684 struct in6_addr ip6
;
1688 struct in6_addr ip6
;
1698 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1700 if (ovs_scan_len(s
, n
, "=")) {
1701 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1702 struct in6_addr ipv6
;
1704 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1705 p
->addr_len
= sizeof p
->addr_min
.ip
;
1706 if (ovs_scan_len(s
, n
, "-")) {
1707 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1708 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1712 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1713 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1714 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1715 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1716 p
->addr_min
.ip6
= ipv6
;
1717 if (ovs_scan_len(s
, n
, "-")) {
1718 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1719 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1720 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1721 p
->addr_max
.ip6
= ipv6
;
1729 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1730 if (ovs_scan_len(s
, n
, "-")) {
1731 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1741 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1745 if (ovs_scan_len(s
, &n
, "nat")) {
1746 memset(p
, 0, sizeof *p
);
1748 if (ovs_scan_len(s
, &n
, "(")) {
1752 end
= strchr(s
+ n
, ')');
1759 n
+= strspn(s
+ n
, delimiters
);
1760 if (ovs_scan_len(s
, &n
, "src")) {
1761 int err
= scan_ct_nat_range(s
, &n
, p
);
1768 if (ovs_scan_len(s
, &n
, "dst")) {
1769 int err
= scan_ct_nat_range(s
, &n
, p
);
1776 if (ovs_scan_len(s
, &n
, "persistent")) {
1777 p
->persistent
= true;
1780 if (ovs_scan_len(s
, &n
, "hash")) {
1781 p
->proto_hash
= true;
1784 if (ovs_scan_len(s
, &n
, "random")) {
1785 p
->proto_random
= true;
1791 if (p
->snat
&& p
->dnat
) {
1794 if ((p
->addr_len
!= 0 &&
1795 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1796 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1797 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1800 if (p
->proto_hash
&& p
->proto_random
) {
1810 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1812 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1815 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1816 } else if (p
->dnat
) {
1817 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1821 if (p
->addr_len
!= 0) {
1822 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1824 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1825 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1829 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1830 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1831 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1834 if (p
->persistent
) {
1835 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1837 if (p
->proto_hash
) {
1838 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1840 if (p
->proto_random
) {
1841 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1845 nl_msg_end_nested(actions
, start
);
1849 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1853 if (ovs_scan(s
, "ct")) {
1854 const char *helper
= NULL
;
1855 size_t helper_len
= 0;
1856 bool commit
= false;
1857 bool force_commit
= false;
1862 } ct_mark
= { 0, 0 };
1867 struct ct_nat_params nat_params
;
1868 bool have_nat
= false;
1872 memset(&ct_label
, 0, sizeof(ct_label
));
1875 if (ovs_scan(s
, "(")) {
1878 end
= strchr(s
, ')');
1886 s
+= strspn(s
, delimiters
);
1887 if (ovs_scan(s
, "commit%n", &n
)) {
1892 if (ovs_scan(s
, "force_commit%n", &n
)) {
1893 force_commit
= true;
1897 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1901 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1904 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1907 ct_mark
.mask
= UINT32_MAX
;
1911 if (ovs_scan(s
, "label=%n", &n
)) {
1915 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1922 if (ovs_scan(s
, "helper=%n", &n
)) {
1924 helper_len
= strcspn(s
, delimiters_end
);
1925 if (!helper_len
|| helper_len
> 15) {
1933 n
= scan_ct_nat(s
, &nat_params
);
1938 /* end points to the end of the nested, nat action.
1939 * find the real end. */
1942 /* Nothing matched. */
1947 if (commit
&& force_commit
) {
1951 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1953 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1954 } else if (force_commit
) {
1955 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1958 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1961 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1964 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1965 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1969 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1973 nl_msg_put_ct_nat(&nat_params
, actions
);
1975 nl_msg_end_nested(actions
, start
);
1982 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
1983 uint8_t * metadata
, size_t md_size
,
1987 struct ovs_nsh_key_base base
;
1989 base
.flags
= nsh
->flags
;
1990 base
.ttl
= nsh
->ttl
;
1991 base
.mdtype
= nsh
->mdtype
;
1993 base
.path_hdr
= nsh
->path_hdr
;
1995 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
1996 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
1999 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2000 sizeof nsh
->context
);
2002 switch (nsh
->mdtype
) {
2004 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2005 sizeof nsh
->context
);
2008 if (metadata
&& md_size
> 0) {
2009 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2014 /* No match support for other MD formats yet. */
2018 nl_msg_end_nested(buf
, nsh_key_ofs
);
2023 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2030 struct ovs_key_nsh nsh
;
2031 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2032 uint8_t md_size
= 0;
2034 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2039 /* The default is NSH_M_TYPE1 */
2042 nsh
.mdtype
= NSH_M_TYPE1
;
2043 nsh
.np
= NSH_P_ETHERNET
;
2044 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2045 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2048 n
+= strspn(s
+ n
, delimiters
);
2053 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2056 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2059 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2060 switch (nsh
.mdtype
) {
2062 /* This is the default format. */;
2065 /* Length will be updated later. */
2074 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2077 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2080 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2083 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2084 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2085 nsh
.context
[0] = htonl(cd
);
2088 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2089 nsh
.context
[1] = htonl(cd
);
2092 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2093 nsh
.context
[2] = htonl(cd
);
2096 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2097 nsh
.context
[3] = htonl(cd
);
2101 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2104 size_t mdlen
, padding
;
2105 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)) {
2106 ofpbuf_use_stub(&b
, metadata
,
2107 NSH_CTX_HDRS_MAX_LEN
);
2108 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2109 /* Pad metadata to 4 bytes. */
2110 padding
= PAD_SIZE(mdlen
, 4);
2112 ofpbuf_push_zeros(&b
, padding
);
2114 md_size
= mdlen
+ padding
;
2125 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2126 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2127 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2128 nl_msg_end_nested(actions
, offset
);
2135 parse_action_list(const char *s
, const struct simap
*port_names
,
2136 struct ofpbuf
*actions
)
2143 n
+= strspn(s
+ n
, delimiters
);
2147 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
2158 parse_odp_action(const char *s
, const struct simap
*port_names
,
2159 struct ofpbuf
*actions
)
2165 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2166 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2175 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2176 struct ovs_action_trunc
*trunc
;
2178 trunc
= nl_msg_put_unspec_uninit(actions
,
2179 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2180 trunc
->max_len
= max_len
;
2186 int len
= strcspn(s
, delimiters
);
2187 struct simap_node
*node
;
2189 node
= simap_find_len(port_names
, s
, len
);
2191 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2200 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2201 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2206 if (!strncmp(s
, "userspace(", 10)) {
2207 return parse_odp_userspace_action(s
, actions
);
2210 if (!strncmp(s
, "set(", 4)) {
2213 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2214 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2215 struct nlattr
*nested
, *key
;
2218 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2219 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
2221 ofpbuf_uninit(&maskbuf
);
2224 if (s
[retval
+ 4] != ')') {
2225 ofpbuf_uninit(&maskbuf
);
2229 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2232 size
= nl_attr_get_size(mask
);
2233 if (size
== nl_attr_get_size(key
)) {
2234 /* Change to masked set action if not fully masked. */
2235 if (!is_all_ones(mask
+ 1, size
)) {
2236 /* Remove padding of eariler key payload */
2237 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2239 /* Put mask payload right after key payload */
2240 key
->nla_len
+= size
;
2241 ofpbuf_put(actions
, mask
+ 1, size
);
2243 /* Add new padding as needed */
2244 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2247 /* 'actions' may have been reallocated by ofpbuf_put(). */
2248 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2249 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2252 ofpbuf_uninit(&maskbuf
);
2254 nl_msg_end_nested(actions
, start_ofs
);
2259 struct ovs_action_push_vlan push
;
2260 int tpid
= ETH_TYPE_VLAN
;
2265 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2266 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2267 &vid
, &pcp
, &cfi
, &n
)
2268 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2269 &tpid
, &vid
, &pcp
, &n
)
2270 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2271 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2272 push
.vlan_tpid
= htons(tpid
);
2273 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2274 | (pcp
<< VLAN_PCP_SHIFT
)
2275 | (cfi
? VLAN_CFI
: 0));
2276 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2277 &push
, sizeof push
);
2283 if (!strncmp(s
, "pop_vlan", 8)) {
2284 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2289 unsigned long long int meter_id
;
2292 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2293 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2302 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2303 && percentage
>= 0. && percentage
<= 100.0) {
2304 size_t sample_ofs
, actions_ofs
;
2307 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2308 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2309 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2310 (probability
<= 0 ? 0
2311 : probability
>= UINT32_MAX
? UINT32_MAX
2314 actions_ofs
= nl_msg_start_nested(actions
,
2315 OVS_SAMPLE_ATTR_ACTIONS
);
2316 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2321 nl_msg_end_nested(actions
, actions_ofs
);
2322 nl_msg_end_nested(actions
, sample_ofs
);
2324 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2329 if (!strncmp(s
, "clone(", 6)) {
2333 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2334 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2339 nl_msg_end_nested(actions
, actions_ofs
);
2345 if (!strncmp(s
, "push_nsh(", 9)) {
2346 int retval
= parse_odp_push_nsh_action(s
, actions
);
2356 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2357 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2366 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2367 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2373 if (!strncmp(s
, "ct_clear", 8)) {
2374 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2382 retval
= parse_conntrack_action(s
, actions
);
2389 struct ovs_action_push_tnl data
;
2392 n
= ovs_parse_tnl_push(s
, &data
);
2394 odp_put_tnl_push_action(actions
, &data
);
2403 /* Parses the string representation of datapath actions, in the format output
2404 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2405 * value. On success, the ODP actions are appended to 'actions' as a series of
2406 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2407 * way, 'actions''s data might be reallocated. */
2409 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2410 struct ofpbuf
*actions
)
2414 if (!strcasecmp(s
, "drop")) {
2418 old_size
= actions
->size
;
2422 s
+= strspn(s
, delimiters
);
2427 retval
= parse_odp_action(s
, port_names
, actions
);
2428 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2429 actions
->size
= old_size
;
2438 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2439 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2442 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2443 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2444 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2445 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2446 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2447 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2448 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2449 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2450 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2451 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2452 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2453 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2454 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2455 .next
= ovs_vxlan_ext_attr_lens
,
2456 .next_max
= OVS_VXLAN_EXT_MAX
},
2457 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2458 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2459 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2462 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2463 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2464 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2465 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2466 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2467 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2468 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2469 .next
= ovs_tun_key_attr_lens
,
2470 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2471 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2472 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2473 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2474 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2475 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2476 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2477 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2478 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2479 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2480 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2481 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2482 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2483 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2484 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2485 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2486 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2487 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2488 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2489 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2490 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2491 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2492 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2493 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2494 .next
= ovs_nsh_key_attr_lens
,
2495 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2498 /* Returns the correct length of the payload for a flow key attribute of the
2499 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2500 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2501 * payload is a nested type. */
2503 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2505 if (type
> max_type
) {
2506 return ATTR_LEN_INVALID
;
2509 return tbl
[type
].len
;
2513 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2515 size_t len
= nl_attr_get_size(a
);
2517 const uint8_t *unspec
;
2520 unspec
= nl_attr_get(a
);
2521 for (i
= 0; i
< len
; i
++) {
2523 ds_put_char(ds
, ' ');
2525 ds_put_format(ds
, "%02x", unspec
[i
]);
2531 ovs_frag_type_to_string(enum ovs_frag_type type
)
2534 case OVS_FRAG_TYPE_NONE
:
2536 case OVS_FRAG_TYPE_FIRST
:
2538 case OVS_FRAG_TYPE_LATER
:
2540 case __OVS_FRAG_TYPE_MAX
:
2546 enum odp_key_fitness
2547 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2548 struct nsh_hdr
*nsh_hdr
, size_t size
)
2551 const struct nlattr
*a
;
2552 bool unknown
= false;
2556 bool has_md1
= false;
2557 bool has_md2
= false;
2559 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2560 uint16_t type
= nl_attr_type(a
);
2561 size_t len
= nl_attr_get_size(a
);
2562 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2563 OVS_NSH_KEY_ATTR_MAX
, type
);
2565 if (len
!= expected_len
&& expected_len
>= 0) {
2566 return ODP_FIT_ERROR
;
2570 case OVS_NSH_KEY_ATTR_BASE
: {
2571 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2572 nsh_hdr
->next_proto
= base
->np
;
2573 nsh_hdr
->md_type
= base
->mdtype
;
2574 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2575 flags
= base
->flags
;
2579 case OVS_NSH_KEY_ATTR_MD1
: {
2580 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2581 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2583 mdlen
= nl_attr_get_size(a
);
2584 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2585 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2586 return ODP_FIT_ERROR
;
2588 memcpy(md1_dst
, md1
, mdlen
);
2591 case OVS_NSH_KEY_ATTR_MD2
: {
2592 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2593 const uint8_t *md2
= nl_attr_get(a
);
2595 mdlen
= nl_attr_get_size(a
);
2596 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2597 return ODP_FIT_ERROR
;
2599 memcpy(md2_dst
, md2
, mdlen
);
2603 /* Allow this to show up as unexpected, if there are unknown
2604 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2611 return ODP_FIT_TOO_MUCH
;
2614 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2615 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2616 return ODP_FIT_ERROR
;
2619 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2620 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2622 return ODP_FIT_PERFECT
;
2625 enum odp_key_fitness
2626 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2627 struct ovs_key_nsh
*nsh_mask
)
2630 const struct nlattr
*a
;
2631 bool unknown
= false;
2632 bool has_md1
= false;
2634 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2635 uint16_t type
= nl_attr_type(a
);
2636 size_t len
= nl_attr_get_size(a
);
2637 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2638 OVS_NSH_KEY_ATTR_MAX
, type
);
2640 /* the attribute can have mask, len is 2 * expected_len for that case.
2642 if ((len
!= expected_len
) && (len
!= 2 * expected_len
) &&
2643 (expected_len
>= 0)) {
2644 return ODP_FIT_ERROR
;
2647 if ((nsh_mask
&& (expected_len
>= 0) && (len
!= 2 * expected_len
)) ||
2648 (!nsh_mask
&& (expected_len
>= 0) && (len
== 2 * expected_len
))) {
2649 return ODP_FIT_ERROR
;
2653 case OVS_NSH_KEY_ATTR_UNSPEC
:
2655 case OVS_NSH_KEY_ATTR_BASE
: {
2656 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2657 nsh
->flags
= base
->flags
;
2658 nsh
->ttl
= base
->ttl
;
2659 nsh
->mdtype
= base
->mdtype
;
2661 nsh
->path_hdr
= base
->path_hdr
;
2662 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2663 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2664 nsh_mask
->flags
= base_mask
->flags
;
2665 nsh_mask
->ttl
= base_mask
->ttl
;
2666 nsh_mask
->mdtype
= base_mask
->mdtype
;
2667 nsh_mask
->np
= base_mask
->np
;
2668 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2672 case OVS_NSH_KEY_ATTR_MD1
: {
2673 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2675 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2676 if (len
== 2 * sizeof(*md1
)) {
2677 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2678 memcpy(nsh_mask
->context
, md1_mask
->context
,
2683 case OVS_NSH_KEY_ATTR_MD2
:
2685 /* Allow this to show up as unexpected, if there are unknown
2686 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2693 return ODP_FIT_TOO_MUCH
;
2696 if (has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
) {
2697 return ODP_FIT_ERROR
;
2700 return ODP_FIT_PERFECT
;
2703 static enum odp_key_fitness
2704 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2705 struct flow_tnl
*tun
)
2708 const struct nlattr
*a
;
2710 bool unknown
= false;
2712 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2713 uint16_t type
= nl_attr_type(a
);
2714 size_t len
= nl_attr_get_size(a
);
2715 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2716 OVS_TUNNEL_ATTR_MAX
, type
);
2718 if (len
!= expected_len
&& expected_len
>= 0) {
2719 return ODP_FIT_ERROR
;
2723 case OVS_TUNNEL_KEY_ATTR_ID
:
2724 tun
->tun_id
= nl_attr_get_be64(a
);
2725 tun
->flags
|= FLOW_TNL_F_KEY
;
2727 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2728 tun
->ip_src
= nl_attr_get_be32(a
);
2730 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2731 tun
->ip_dst
= nl_attr_get_be32(a
);
2733 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2734 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2736 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2737 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2739 case OVS_TUNNEL_KEY_ATTR_TOS
:
2740 tun
->ip_tos
= nl_attr_get_u8(a
);
2742 case OVS_TUNNEL_KEY_ATTR_TTL
:
2743 tun
->ip_ttl
= nl_attr_get_u8(a
);
2746 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2747 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2749 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2750 tun
->flags
|= FLOW_TNL_F_CSUM
;
2752 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2753 tun
->tp_src
= nl_attr_get_be16(a
);
2755 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2756 tun
->tp_dst
= nl_attr_get_be16(a
);
2758 case OVS_TUNNEL_KEY_ATTR_OAM
:
2759 tun
->flags
|= FLOW_TNL_F_OAM
;
2761 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2762 static const struct nl_policy vxlan_opts_policy
[] = {
2763 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2765 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2767 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2768 return ODP_FIT_ERROR
;
2771 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2772 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2774 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2775 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2780 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2781 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2783 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
2784 const struct erspan_metadata
*opts
= nl_attr_get(a
);
2786 tun
->erspan_ver
= opts
->version
;
2787 if (tun
->erspan_ver
== 1) {
2788 tun
->erspan_idx
= ntohl(opts
->u
.index
);
2789 } else if (tun
->erspan_ver
== 2) {
2790 tun
->erspan_dir
= opts
->u
.md2
.dir
;
2791 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
2793 VLOG_WARN("%s invalid erspan version\n", __func__
);
2799 /* Allow this to show up as unexpected, if there are unknown
2800 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2807 return ODP_FIT_ERROR
;
2810 return ODP_FIT_TOO_MUCH
;
2812 return ODP_FIT_PERFECT
;
2815 enum odp_key_fitness
2816 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2818 memset(tun
, 0, sizeof *tun
);
2819 return odp_tun_key_from_attr__(attr
, false, tun
);
2823 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2824 const struct flow_tnl
*tun_flow_key
,
2825 const struct ofpbuf
*key_buf
, const char *tnl_type
)
2829 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2831 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2832 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2833 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2835 if (tun_key
->ip_src
) {
2836 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2838 if (tun_key
->ip_dst
) {
2839 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2841 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2842 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2844 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2845 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2847 if (tun_key
->ip_tos
) {
2848 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2850 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2851 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2852 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2854 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2855 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2857 if (tun_key
->tp_src
) {
2858 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2860 if (tun_key
->tp_dst
) {
2861 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2863 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2864 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2867 /* If tnl_type is set to a particular type of output tunnel,
2868 * only put its relevant tunnel metadata to the nlattr.
2869 * If tnl_type is NULL, put tunnel metadata according to the
2872 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
2873 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
2874 size_t vxlan_opts_ofs
;
2876 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2877 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2878 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2879 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2882 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
2883 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2886 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
2887 !strcmp(tnl_type
, "ip6erspan")) &&
2888 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
2889 struct erspan_metadata opts
;
2891 opts
.version
= tun_key
->erspan_ver
;
2892 if (opts
.version
== 1) {
2893 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
2895 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
2896 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
2898 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
2899 &opts
, sizeof(opts
));
2902 nl_msg_end_nested(a
, tun_key_ofs
);
2906 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2909 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2910 * that -1 becomes all-1-bits and 0 does not change. */
2911 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2912 uint32_t u32
= constant
;
2913 uint8_t u8
= constant
;
2914 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
2917 case OVS_KEY_ATTR_UNSPEC
:
2918 case OVS_KEY_ATTR_ENCAP
:
2919 case __OVS_KEY_ATTR_MAX
:
2923 case OVS_KEY_ATTR_PRIORITY
:
2924 case OVS_KEY_ATTR_IN_PORT
:
2925 case OVS_KEY_ATTR_ETHERNET
:
2926 case OVS_KEY_ATTR_VLAN
:
2927 case OVS_KEY_ATTR_ETHERTYPE
:
2928 case OVS_KEY_ATTR_IPV4
:
2929 case OVS_KEY_ATTR_TCP
:
2930 case OVS_KEY_ATTR_UDP
:
2931 case OVS_KEY_ATTR_ICMP
:
2932 case OVS_KEY_ATTR_ICMPV6
:
2933 case OVS_KEY_ATTR_ND
:
2934 case OVS_KEY_ATTR_SKB_MARK
:
2935 case OVS_KEY_ATTR_TUNNEL
:
2936 case OVS_KEY_ATTR_SCTP
:
2937 case OVS_KEY_ATTR_DP_HASH
:
2938 case OVS_KEY_ATTR_RECIRC_ID
:
2939 case OVS_KEY_ATTR_MPLS
:
2940 case OVS_KEY_ATTR_CT_STATE
:
2941 case OVS_KEY_ATTR_CT_ZONE
:
2942 case OVS_KEY_ATTR_CT_MARK
:
2943 case OVS_KEY_ATTR_CT_LABELS
:
2944 case OVS_KEY_ATTR_PACKET_TYPE
:
2945 case OVS_KEY_ATTR_NSH
:
2946 return is_all_byte(mask
, size
, u8
);
2948 case OVS_KEY_ATTR_TCP_FLAGS
:
2949 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
2951 case OVS_KEY_ATTR_IPV6
: {
2952 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2953 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2954 == htonl(IPV6_LABEL_MASK
& u32
)
2955 && ipv6_mask
->ipv6_proto
== u8
2956 && ipv6_mask
->ipv6_tclass
== u8
2957 && ipv6_mask
->ipv6_hlimit
== u8
2958 && ipv6_mask
->ipv6_frag
== u8
2959 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
2960 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
2963 case OVS_KEY_ATTR_ARP
:
2964 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
2966 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
2967 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
2970 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
2971 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
2976 /* The caller must already have verified that 'ma' has a correct length.
2978 * The main purpose of this function is formatting, to allow code to figure out
2979 * whether the mask can be omitted. It doesn't try hard for attributes that
2980 * contain sub-attributes, etc., because normally those would be broken down
2981 * further for formatting. */
2983 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2985 return odp_mask_is_constant__(nl_attr_type(ma
),
2986 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
2989 /* The caller must already have verified that 'size' is a correct length for
2992 * The main purpose of this function is formatting, to allow code to figure out
2993 * whether the mask can be omitted. It doesn't try hard for attributes that
2994 * contain sub-attributes, etc., because normally those would be broken down
2995 * further for formatting. */
2997 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
2999 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3002 /* The caller must already have verified that 'ma' has a correct length. */
3004 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3006 enum ovs_key_attr attr
= nl_attr_type(ma
);
3007 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3011 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3014 struct odp_portno_names
*odp_portno_names
;
3016 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3017 odp_portno_names
->port_no
= port_no
;
3018 odp_portno_names
->name
= xstrdup(port_name
);
3019 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3020 hash_odp_port(port_no
));
3024 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3027 struct odp_portno_names
*odp_portno_names
;
3029 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3030 hash_odp_port(port_no
), portno_names
) {
3031 if (odp_portno_names
->port_no
== port_no
) {
3032 return odp_portno_names
->name
;
3040 odp_portno_names_destroy(struct hmap
*portno_names
)
3042 struct odp_portno_names
*odp_portno_names
;
3044 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3045 free(odp_portno_names
->name
);
3046 free(odp_portno_names
);
3051 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3054 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3056 ds_put_cstr(s
, name
);
3058 ds_put_format(s
, "%"PRIu32
, port_no
);
3062 /* Format helpers. */
3065 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3066 const struct eth_addr
*mask
, bool verbose
)
3068 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3070 if (verbose
|| !mask_empty
) {
3071 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3074 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3076 ds_put_format(ds
, "%s=", name
);
3077 eth_format_masked(key
, mask
, ds
);
3078 ds_put_char(ds
, ',');
3085 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3086 const ovs_be64
*mask
, bool verbose
)
3088 bool mask_empty
= mask
&& !*mask
;
3090 if (verbose
|| !mask_empty
) {
3091 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3093 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3094 if (!mask_full
) { /* Partially masked. */
3095 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3097 ds_put_char(ds
, ',');
3102 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3103 const ovs_be32
*mask
, bool verbose
)
3105 bool mask_empty
= mask
&& !*mask
;
3107 if (verbose
|| !mask_empty
) {
3108 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3110 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3111 if (!mask_full
) { /* Partially masked. */
3112 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3114 ds_put_char(ds
, ',');
3119 format_in6_addr(struct ds
*ds
, const char *name
,
3120 const struct in6_addr
*key
,
3121 const struct in6_addr
*mask
,
3124 char buf
[INET6_ADDRSTRLEN
];
3125 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3127 if (verbose
|| !mask_empty
) {
3128 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3130 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3131 ds_put_format(ds
, "%s=%s", name
, buf
);
3132 if (!mask_full
) { /* Partially masked. */
3133 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3134 ds_put_format(ds
, "/%s", buf
);
3136 ds_put_char(ds
, ',');
3141 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3142 const ovs_be32
*mask
, bool verbose
)
3144 bool mask_empty
= mask
&& !*mask
;
3146 if (verbose
|| !mask_empty
) {
3147 bool mask_full
= !mask
3148 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3150 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3151 if (!mask_full
) { /* Partially masked. */
3152 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3154 ds_put_char(ds
, ',');
3159 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3160 const uint8_t *mask
, bool verbose
)
3162 bool mask_empty
= mask
&& !*mask
;
3164 if (verbose
|| !mask_empty
) {
3165 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3167 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3168 if (!mask_full
) { /* Partially masked. */
3169 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3171 ds_put_char(ds
, ',');
3176 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3177 const uint8_t *mask
, bool verbose
)
3179 bool mask_empty
= mask
&& !*mask
;
3181 if (verbose
|| !mask_empty
) {
3182 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3184 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3185 if (!mask_full
) { /* Partially masked. */
3186 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3188 ds_put_char(ds
, ',');
3193 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3194 const ovs_be16
*mask
, bool verbose
)
3196 bool mask_empty
= mask
&& !*mask
;
3198 if (verbose
|| !mask_empty
) {
3199 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3201 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3202 if (!mask_full
) { /* Partially masked. */
3203 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3205 ds_put_char(ds
, ',');
3210 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3211 const ovs_be16
*mask
, bool verbose
)
3213 bool mask_empty
= mask
&& !*mask
;
3215 if (verbose
|| !mask_empty
) {
3216 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3218 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3219 if (!mask_full
) { /* Partially masked. */
3220 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3222 ds_put_char(ds
, ',');
3227 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3228 const uint16_t *mask
, bool verbose
)
3230 bool mask_empty
= mask
&& !*mask
;
3232 if (verbose
|| !mask_empty
) {
3233 ds_put_cstr(ds
, name
);
3234 ds_put_char(ds
, '(');
3236 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3237 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3238 } else { /* Fully masked. */
3239 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3241 ds_put_cstr(ds
, "),");
3246 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3247 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3251 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3252 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3253 expected_len
!= ATTR_LEN_NESTED
) {
3255 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3256 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3258 if (bad_key_len
|| bad_mask_len
) {
3260 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3263 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3264 nl_attr_get_size(a
), expected_len
);
3266 format_generic_odp_key(a
, ds
);
3268 ds_put_char(ds
, '/');
3270 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3271 nl_attr_get_size(ma
), expected_len
);
3273 format_generic_odp_key(ma
, ds
);
3275 ds_put_char(ds
, ')');
3284 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3285 const struct nlattr
*ma
)
3287 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3288 format_generic_odp_key(a
, ds
);
3289 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3290 ds_put_char(ds
, '/');
3291 format_generic_odp_key(ma
, ds
);
3293 ds_put_cstr(ds
, "),");
3297 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3298 const struct nlattr
*mask_attr
, struct ds
*ds
,
3302 const struct nlattr
*a
;
3305 ofpbuf_init(&ofp
, 100);
3306 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3307 uint16_t type
= nl_attr_type(a
);
3308 const struct nlattr
*ma
= NULL
;
3311 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3312 nl_attr_get_size(mask_attr
), type
);
3314 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3320 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3321 OVS_VXLAN_EXT_MAX
, true)) {
3326 case OVS_VXLAN_EXT_GBP
: {
3327 uint32_t key
= nl_attr_get_u32(a
);
3328 ovs_be16 id
, id_mask
;
3329 uint8_t flags
, flags_mask
= 0;
3331 id
= htons(key
& 0xFFFF);
3332 flags
= (key
>> 16) & 0xFF;
3334 uint32_t mask
= nl_attr_get_u32(ma
);
3335 id_mask
= htons(mask
& 0xFFFF);
3336 flags_mask
= (mask
>> 16) & 0xFF;
3339 ds_put_cstr(ds
, "gbp(");
3340 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3341 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3343 ds_put_cstr(ds
, "),");
3348 format_unknown_key(ds
, a
, ma
);
3354 ofpbuf_uninit(&ofp
);
3358 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3359 const struct nlattr
*mask_attr
, struct ds
*ds
,
3362 const struct erspan_metadata
*opts
, *mask
;
3363 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3365 opts
= nl_attr_get(attr
);
3366 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3368 ver
= (uint8_t)opts
->version
;
3370 ver_ma
= (uint8_t)mask
->version
;
3373 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3375 if (opts
->version
== 1) {
3377 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3378 ntohl(opts
->u
.index
),
3379 ntohl(mask
->u
.index
));
3381 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3383 } else if (opts
->version
== 2) {
3384 dir
= opts
->u
.md2
.dir
;
3385 hwid
= opts
->u
.md2
.hwid
;
3387 dir_ma
= mask
->u
.md2
.dir
;
3388 hwid_ma
= mask
->u
.md2
.hwid
;
3391 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3392 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3397 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3400 format_geneve_opts(const struct geneve_opt
*opt
,
3401 const struct geneve_opt
*mask
, int opts_len
,
3402 struct ds
*ds
, bool verbose
)
3404 while (opts_len
> 0) {
3406 uint8_t data_len
, data_len_mask
;
3408 if (opts_len
< sizeof *opt
) {
3409 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3410 opts_len
, sizeof *opt
);
3414 data_len
= opt
->length
* 4;
3416 if (mask
->length
== 0x1f) {
3417 data_len_mask
= UINT8_MAX
;
3419 data_len_mask
= mask
->length
;
3422 len
= sizeof *opt
+ data_len
;
3423 if (len
> opts_len
) {
3424 ds_put_format(ds
, "opt len %u greater than remaining %u",
3429 ds_put_char(ds
, '{');
3430 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3432 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3433 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3435 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3436 ds_put_hex(ds
, opt
+ 1, data_len
);
3437 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3438 ds_put_char(ds
, '/');
3439 ds_put_hex(ds
, mask
+ 1, data_len
);
3444 ds_put_char(ds
, '}');
3446 opt
+= len
/ sizeof(*opt
);
3448 mask
+= len
/ sizeof(*opt
);
3455 format_odp_tun_geneve(const struct nlattr
*attr
,
3456 const struct nlattr
*mask_attr
, struct ds
*ds
,
3459 int opts_len
= nl_attr_get_size(attr
);
3460 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3461 const struct geneve_opt
*mask
= mask_attr
?
3462 nl_attr_get(mask_attr
) : NULL
;
3464 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3465 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3466 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3470 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3474 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3478 const struct nlattr
*a
;
3479 struct ovs_key_nsh nsh
;
3480 struct ovs_key_nsh nsh_mask
;
3482 memset(&nsh
, 0, sizeof nsh
);
3483 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3485 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3486 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3487 const struct nlattr
*ma
= NULL
;
3490 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3491 nl_attr_get_size(mask_attr
), type
);
3494 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3495 OVS_NSH_KEY_ATTR_MAX
, true)) {
3500 case OVS_NSH_KEY_ATTR_UNSPEC
:
3502 case OVS_NSH_KEY_ATTR_BASE
: {
3503 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3504 const struct ovs_nsh_key_base
*base_mask
3505 = ma
? nl_attr_get(ma
) : NULL
;
3506 nsh
.flags
= base
->flags
;
3507 nsh
.ttl
= base
->ttl
;
3508 nsh
.mdtype
= base
->mdtype
;
3510 nsh
.path_hdr
= base
->path_hdr
;
3512 nsh_mask
.flags
= base_mask
->flags
;
3513 nsh_mask
.ttl
= base_mask
->ttl
;
3514 nsh_mask
.mdtype
= base_mask
->mdtype
;
3515 nsh_mask
.np
= base_mask
->np
;
3516 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3520 case OVS_NSH_KEY_ATTR_MD1
: {
3521 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3522 const struct ovs_nsh_key_md1
*md1_mask
3523 = ma
? nl_attr_get(ma
) : NULL
;
3524 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3526 memcpy(nsh_mask
.context
, md1_mask
->context
,
3527 sizeof md1_mask
->context
);
3531 case OVS_NSH_KEY_ATTR_MD2
:
3532 case __OVS_NSH_KEY_ATTR_MAX
:
3534 /* No support for matching other metadata formats yet. */
3540 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3542 format_nsh_key(ds
, &nsh
);
3547 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3548 struct ds
*ds
, bool verbose
)
3551 const struct nlattr
*a
;
3553 uint16_t mask_flags
= 0;
3556 ofpbuf_init(&ofp
, 100);
3557 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3558 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3559 const struct nlattr
*ma
= NULL
;
3562 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3563 nl_attr_get_size(mask_attr
), type
);
3565 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3566 OVS_TUNNEL_KEY_ATTR_MAX
,
3571 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3572 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3577 case OVS_TUNNEL_KEY_ATTR_ID
:
3578 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3579 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3580 flags
|= FLOW_TNL_F_KEY
;
3582 mask_flags
|= FLOW_TNL_F_KEY
;
3585 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3586 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3587 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3589 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3590 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3591 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3593 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3594 struct in6_addr ipv6_src
;
3595 ipv6_src
= nl_attr_get_in6_addr(a
);
3596 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3597 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3600 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3601 struct in6_addr ipv6_dst
;
3602 ipv6_dst
= nl_attr_get_in6_addr(a
);
3603 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3604 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3607 case OVS_TUNNEL_KEY_ATTR_TOS
:
3608 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3609 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3611 case OVS_TUNNEL_KEY_ATTR_TTL
:
3612 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3613 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3615 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3616 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3618 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3619 flags
|= FLOW_TNL_F_CSUM
;
3621 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3622 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3623 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3625 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3626 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3627 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3629 case OVS_TUNNEL_KEY_ATTR_OAM
:
3630 flags
|= FLOW_TNL_F_OAM
;
3632 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3633 ds_put_cstr(ds
, "vxlan(");
3634 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3635 ds_put_cstr(ds
, "),");
3637 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3638 ds_put_cstr(ds
, "geneve(");
3639 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3640 ds_put_cstr(ds
, "),");
3642 case OVS_TUNNEL_KEY_ATTR_PAD
:
3644 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3645 ds_put_cstr(ds
, "erspan(");
3646 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3647 ds_put_cstr(ds
, "),");
3649 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3651 format_unknown_key(ds
, a
, ma
);
3656 /* Flags can have a valid mask even if the attribute is not set, so
3657 * we need to collect these separately. */
3659 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3660 switch (nl_attr_type(a
)) {
3661 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3662 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3664 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3665 mask_flags
|= FLOW_TNL_F_CSUM
;
3667 case OVS_TUNNEL_KEY_ATTR_OAM
:
3668 mask_flags
|= FLOW_TNL_F_OAM
;
3674 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3677 ofpbuf_uninit(&ofp
);
3681 odp_ct_state_to_string(uint32_t flag
)
3684 case OVS_CS_F_REPLY_DIR
:
3686 case OVS_CS_F_TRACKED
:
3690 case OVS_CS_F_ESTABLISHED
:
3692 case OVS_CS_F_RELATED
:
3694 case OVS_CS_F_INVALID
:
3696 case OVS_CS_F_SRC_NAT
:
3698 case OVS_CS_F_DST_NAT
:
3706 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
3707 const uint8_t *mask
, bool verbose OVS_UNUSED
)
3709 bool mask_empty
= mask
&& !*mask
;
3710 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3712 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3713 if (!mask_empty
&& !mask_full
) {
3714 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
3716 } else if (!mask_empty
) {
3717 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
3722 mask_empty(const struct nlattr
*ma
)
3730 mask
= nl_attr_get(ma
);
3731 n
= nl_attr_get_size(ma
);
3733 return is_all_zeros(mask
, n
);
3736 /* The caller must have already verified that 'a' and 'ma' have correct
3739 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
3740 const struct hmap
*portno_names
, struct ds
*ds
,
3743 enum ovs_key_attr attr
= nl_attr_type(a
);
3744 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3747 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
3749 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
3751 ds_put_char(ds
, '(');
3753 case OVS_KEY_ATTR_ENCAP
:
3754 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
3755 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
3756 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
3758 } else if (nl_attr_get_size(a
)) {
3759 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
3764 case OVS_KEY_ATTR_PRIORITY
:
3765 case OVS_KEY_ATTR_SKB_MARK
:
3766 case OVS_KEY_ATTR_DP_HASH
:
3767 case OVS_KEY_ATTR_RECIRC_ID
:
3768 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3770 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3774 case OVS_KEY_ATTR_CT_MARK
:
3775 if (verbose
|| !mask_empty(ma
)) {
3776 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3778 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3783 case OVS_KEY_ATTR_CT_STATE
:
3785 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3787 ds_put_format(ds
, "/%#"PRIx32
,
3788 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
3790 } else if (!is_exact
) {
3791 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
3793 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
3796 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
3800 case OVS_KEY_ATTR_CT_ZONE
:
3801 if (verbose
|| !mask_empty(ma
)) {
3802 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
3804 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
3809 case OVS_KEY_ATTR_CT_LABELS
: {
3810 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
3811 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3813 format_u128(ds
, value
, mask
, verbose
);
3817 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
3818 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
3819 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3821 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3822 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3823 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3825 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
3827 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
3833 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
3834 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
3835 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3837 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3839 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3841 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3843 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3845 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3851 case OVS_KEY_ATTR_TUNNEL
:
3852 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3855 case OVS_KEY_ATTR_IN_PORT
:
3857 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3859 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3861 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3866 case OVS_KEY_ATTR_PACKET_TYPE
: {
3867 ovs_be32 value
= nl_attr_get_be32(a
);
3868 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3870 ovs_be16 ns
= htons(pt_ns(value
));
3871 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3872 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3874 ovs_be16 ns_type
= pt_ns_type_be(value
);
3875 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3876 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3882 case OVS_KEY_ATTR_ETHERNET
: {
3883 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3884 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3886 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3887 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3891 case OVS_KEY_ATTR_VLAN
:
3892 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3893 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3896 case OVS_KEY_ATTR_MPLS
: {
3897 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3898 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3899 size_t size
= nl_attr_get_size(a
);
3901 if (!size
|| size
% sizeof *mpls_key
) {
3902 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3906 mpls_mask
= nl_attr_get(ma
);
3907 if (size
!= nl_attr_get_size(ma
)) {
3908 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3909 "mask length %"PRIuSIZE
")",
3910 size
, nl_attr_get_size(ma
));
3914 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
3917 case OVS_KEY_ATTR_ETHERTYPE
:
3918 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
3920 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
3924 case OVS_KEY_ATTR_IPV4
: {
3925 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
3926 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3928 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3929 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3930 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3932 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
3933 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
3934 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
3939 case OVS_KEY_ATTR_IPV6
: {
3940 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
3941 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3943 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3945 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3947 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
3949 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3951 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
3953 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
3955 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
3960 /* These have the same structure and format. */
3961 case OVS_KEY_ATTR_TCP
:
3962 case OVS_KEY_ATTR_UDP
:
3963 case OVS_KEY_ATTR_SCTP
: {
3964 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
3965 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3967 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
3968 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
3972 case OVS_KEY_ATTR_TCP_FLAGS
:
3974 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
3975 ntohs(nl_attr_get_be16(a
)),
3976 TCP_FLAGS(nl_attr_get_be16(ma
)),
3977 TCP_FLAGS(OVS_BE16_MAX
));
3979 format_flags(ds
, packet_tcp_flag_to_string
,
3980 ntohs(nl_attr_get_be16(a
)), '|');
3984 case OVS_KEY_ATTR_ICMP
: {
3985 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
3986 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3988 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
3989 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
3993 case OVS_KEY_ATTR_ICMPV6
: {
3994 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
3995 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3997 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
3999 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4004 case OVS_KEY_ATTR_ARP
: {
4005 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4006 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4008 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4009 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4010 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4011 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4012 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4016 case OVS_KEY_ATTR_ND
: {
4017 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4018 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4020 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4022 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4023 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4028 case OVS_KEY_ATTR_NSH
: {
4029 format_odp_nsh_attr(a
, ma
, ds
);
4032 case OVS_KEY_ATTR_UNSPEC
:
4033 case __OVS_KEY_ATTR_MAX
:
4035 format_generic_odp_key(a
, ds
);
4037 ds_put_char(ds
, '/');
4038 format_generic_odp_key(ma
, ds
);
4042 ds_put_char(ds
, ')');
4046 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4047 const struct hmap
*portno_names
, struct ds
*ds
,
4050 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4051 OVS_KEY_ATTR_MAX
, false)) {
4052 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4056 static struct nlattr
*
4057 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4058 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4060 const struct nlattr
*a
;
4062 int type
= nl_attr_type(key
);
4063 int size
= nl_attr_get_size(key
);
4065 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4066 nl_msg_put_unspec_zero(ofp
, type
, size
);
4070 if (tbl
[type
].next
) {
4071 const struct attr_len_tbl
*entry
= &tbl
[type
];
4073 max
= entry
->next_max
;
4076 nested_mask
= nl_msg_start_nested(ofp
, type
);
4077 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4078 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4080 nl_msg_end_nested(ofp
, nested_mask
);
4087 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4088 const ovs_32aligned_u128
*mask
, bool verbose
)
4090 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4091 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4092 ds_put_hex(ds
, &value
, sizeof value
);
4093 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4094 value
= hton128(get_32aligned_u128(mask
));
4095 ds_put_char(ds
, '/');
4096 ds_put_hex(ds
, &value
, sizeof value
);
4101 /* Read the string from 's_' as a 128-bit value. If the string contains
4102 * a "/", the rest of the string will be treated as a 128-bit mask.
4104 * If either the value or mask is larger than 64 bits, the string must
4105 * be in hexadecimal.
4108 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4110 char *s
= CONST_CAST(char *, s_
);
4114 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4115 *value
= ntoh128(be_value
);
4120 if (ovs_scan(s
, "/%n", &n
)) {
4124 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4125 sizeof be_mask
, &s
);
4129 *mask
= ntoh128(be_mask
);
4131 *mask
= OVS_U128_MAX
;
4141 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4145 if (ovs_scan(s
, "ufid:")) {
4148 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4160 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4162 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4165 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4166 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4167 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4168 * non-null, translates odp port number to its name. */
4170 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4171 const struct nlattr
*mask
, size_t mask_len
,
4172 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4175 const struct nlattr
*a
;
4177 bool has_ethtype_key
= false;
4178 bool has_packet_type_key
= false;
4180 bool first_field
= true;
4182 ofpbuf_init(&ofp
, 100);
4183 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4184 int attr_type
= nl_attr_type(a
);
4185 const struct nlattr
*ma
= (mask
&& mask_len
4186 ? nl_attr_find__(mask
, mask_len
,
4189 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4190 OVS_KEY_ATTR_MAX
, false)) {
4194 bool is_nested_attr
;
4195 bool is_wildcard
= false;
4197 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4198 has_ethtype_key
= true;
4199 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4200 has_packet_type_key
= true;
4203 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4204 OVS_KEY_ATTR_MAX
, attr_type
) ==
4207 if (mask
&& mask_len
) {
4208 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4209 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4212 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4213 if (is_wildcard
&& !ma
) {
4214 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4219 ds_put_char(ds
, ',');
4221 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4222 first_field
= false;
4223 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4224 && !has_packet_type_key
) {
4225 /* This special case reflects differences between the kernel
4226 * and userspace datapaths regarding the root type of the
4227 * packet being matched (typically Ethernet but some tunnels
4228 * can encapsulate IPv4 etc.). The kernel datapath does not
4229 * have an explicit way to indicate packet type; instead:
4231 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4232 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4233 * Ethertype encoded in the Ethernet header.
4235 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4236 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4237 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4238 * packet is an IPv4 packet).
4240 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4241 * all-wildcarded, it is important to print it.
4243 * On the other hand, the userspace datapath supports
4244 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4245 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4246 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4248 ds_put_char(ds
, ',');
4250 ds_put_cstr(ds
, "eth()");
4254 ofpbuf_uninit(&ofp
);
4259 if (left
== key_len
) {
4260 ds_put_cstr(ds
, "<empty>");
4262 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4263 for (i
= 0; i
< left
; i
++) {
4264 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4266 ds_put_char(ds
, ')');
4268 if (!has_ethtype_key
) {
4269 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4270 OVS_KEY_ATTR_ETHERTYPE
);
4272 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4273 ntohs(nl_attr_get_be16(ma
)));
4277 ds_put_cstr(ds
, "<empty>");
4281 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4282 * OVS_KEY_ATTR_* attributes in 'key'. */
4284 odp_flow_key_format(const struct nlattr
*key
,
4285 size_t key_len
, struct ds
*ds
)
4287 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4291 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4293 if (!strcasecmp(s
, "no")) {
4294 *type
= OVS_FRAG_TYPE_NONE
;
4295 } else if (!strcasecmp(s
, "first")) {
4296 *type
= OVS_FRAG_TYPE_FIRST
;
4297 } else if (!strcasecmp(s
, "later")) {
4298 *type
= OVS_FRAG_TYPE_LATER
;
4308 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4312 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4313 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4317 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4318 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4321 memset(mask
, 0xff, sizeof *mask
);
4330 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4334 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4338 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4339 IP_SCAN_ARGS(mask
), &n
)) {
4342 *mask
= OVS_BE32_MAX
;
4351 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4354 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4356 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4357 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4361 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4362 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4365 memset(mask
, 0xff, sizeof *mask
);
4374 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4379 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4380 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4385 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4386 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4388 *mask
= htonl(mask_
);
4390 *mask
= htonl(IPV6_LABEL_MASK
);
4399 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4403 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4407 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4419 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4423 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4427 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4439 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4443 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4447 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4459 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4461 uint16_t key_
, mask_
;
4464 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4469 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4471 *mask
= htons(mask_
);
4473 *mask
= OVS_BE16_MAX
;
4482 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4484 uint32_t key_
, mask_
;
4487 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4492 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4494 *mask
= htonl(mask_
);
4496 *mask
= OVS_BE32_MAX
;
4505 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4507 uint64_t key_
, mask_
;
4510 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4513 *key
= htonll(key_
);
4515 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4517 *mask
= htonll(mask_
);
4519 *mask
= OVS_BE64_MAX
;
4528 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4530 uint32_t flags
, fmask
;
4533 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4534 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4535 if (n
>= 0 && s
[n
] == ')') {
4546 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4548 uint32_t flags
, fmask
;
4551 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4552 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4554 *key
= htons(flags
);
4556 *mask
= htons(fmask
);
4564 ovs_to_odp_ct_state(uint8_t state
)
4568 #define CS_STATE(ENUM, INDEX, NAME) \
4569 if (state & CS_##ENUM) { \
4570 odp |= OVS_CS_F_##ENUM; \
4579 odp_to_ovs_ct_state(uint32_t flags
)
4583 #define CS_STATE(ENUM, INDEX, NAME) \
4584 if (flags & OVS_CS_F_##ENUM) { \
4585 state |= CS_##ENUM; \
4594 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4596 uint32_t flags
, fmask
;
4599 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4600 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4601 mask
? &fmask
: NULL
);
4614 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4618 enum ovs_frag_type frag_type
;
4620 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4621 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4634 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4635 const struct simap
*port_names
)
4639 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4643 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4650 } else if (port_names
) {
4651 const struct simap_node
*node
;
4654 len
= strcspn(s
, ")");
4655 node
= simap_find_len(port_names
, s
, len
);
4668 /* Helper for vlan parsing. */
4669 struct ovs_key_vlan__
{
4674 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
4676 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
4678 if (value
>> bits
) {
4682 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
4687 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
4690 uint16_t key_
, mask_
;
4693 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4696 if (set_be16_bf(key
, bits
, offset
, key_
)) {
4698 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4701 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
4705 *mask
|= htons(((1U << bits
) - 1) << offset
);
4715 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4717 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
4721 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4723 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
4727 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4729 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
4734 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
4736 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
4738 if (value
>> bits
) {
4742 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
4747 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
4750 uint32_t key_
, mask_
;
4753 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4756 if (set_be32_bf(key
, bits
, offset
, key_
)) {
4758 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4761 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
4765 *mask
|= htonl(((1U << bits
) - 1) << offset
);
4775 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4777 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
4781 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4783 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
4787 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4789 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
4793 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4795 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
4799 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
4801 const char *s_base
= s
;
4802 ovs_be16 id
= 0, id_mask
= 0;
4803 uint8_t flags
= 0, flags_mask
= 0;
4805 if (!strncmp(s
, "id=", 3)) {
4807 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
4813 if (!strncmp(s
, "flags=", 6)) {
4815 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
4818 if (!strncmp(s
, "))", 2)) {
4821 *key
= (flags
<< 16) | ntohs(id
);
4823 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
4833 scan_erspan_metadata(const char *s
,
4834 struct erspan_metadata
*key
,
4835 struct erspan_metadata
*mask
)
4837 const char *s_base
= s
;
4838 uint32_t idx
= 0, idx_mask
= 0;
4839 uint8_t ver
= 0, dir
= 0, hwid
= 0;
4840 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
4842 if (!strncmp(s
, "ver=", 4)) {
4844 s
+= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
4852 if (!strncmp(s
, "idx=", 4)) {
4854 s
+= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
4857 if (!strncmp(s
, ")", 1)) {
4860 key
->u
.index
= htonl(idx
);
4862 mask
->u
.index
= htonl(idx_mask
);
4867 } else if (ver
== 2) {
4868 if (!strncmp(s
, "dir=", 4)) {
4870 s
+= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
4875 if (!strncmp(s
, "hwid=", 5)) {
4877 s
+= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
4880 if (!strncmp(s
, ")", 1)) {
4883 key
->u
.md2
.hwid
= hwid
;
4884 key
->u
.md2
.dir
= dir
;
4886 mask
->u
.md2
.hwid
= hwid_mask
;
4887 mask
->u
.md2
.dir
= dir_mask
;
4897 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
4899 const char *s_base
= s
;
4900 struct geneve_opt
*opt
= key
->d
;
4901 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
4902 int len_remain
= sizeof key
->d
;
4904 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
4908 len_remain
-= sizeof *opt
;
4910 if (!strncmp(s
, "class=", 6)) {
4912 s
+= scan_be16(s
, &opt
->opt_class
,
4913 mask
? &opt_mask
->opt_class
: NULL
);
4915 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
4921 if (!strncmp(s
, "type=", 5)) {
4923 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
4925 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4931 if (!strncmp(s
, "len=", 4)) {
4932 uint8_t opt_len
, opt_len_mask
;
4934 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
4936 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
4939 opt
->length
= opt_len
/ 4;
4941 opt_mask
->length
= opt_len_mask
;
4945 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4951 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
4958 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
4959 data_len
, (char **)&s
)) {
4970 opt
+= 1 + data_len
/ 4;
4972 opt_mask
+= 1 + data_len
/ 4;
4974 len_remain
-= data_len
;
4979 int len
= sizeof key
->d
- len_remain
;
4993 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
4995 const uint16_t *flags
= data_
;
4997 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
4998 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5000 if (*flags
& FLOW_TNL_F_CSUM
) {
5001 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5003 if (*flags
& FLOW_TNL_F_OAM
) {
5004 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5009 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5011 const uint32_t *gbp
= data_
;
5014 size_t vxlan_opts_ofs
;
5016 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5017 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5018 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5023 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5025 const struct geneve_scan
*geneve
= data_
;
5027 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5032 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5034 const struct erspan_metadata
*md
= data_
;
5036 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5040 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5042 unsigned long call_fn = (unsigned long)FUNC; \
5044 typedef void (*fn)(struct ofpbuf *, const void *); \
5046 func(BUF, &(DATA)); \
5048 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5052 #define SCAN_IF(NAME) \
5053 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5054 const char *start = s; \
5059 /* Usually no special initialization is needed. */
5060 #define SCAN_BEGIN(NAME, TYPE) \
5063 memset(&skey, 0, sizeof skey); \
5064 memset(&smask, 0, sizeof smask); \
5068 /* Init as fully-masked as mask will not be scanned. */
5069 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5072 memset(&skey, 0, sizeof skey); \
5073 memset(&smask, 0xff, sizeof smask); \
5077 /* VLAN needs special initialization. */
5078 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5080 TYPE skey = KEY_INIT; \
5081 TYPE smask = MASK_INIT; \
5085 /* Scan unnamed entry as 'TYPE' */
5086 #define SCAN_TYPE(TYPE, KEY, MASK) \
5087 len = scan_##TYPE(s, KEY, MASK); \
5093 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5094 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5095 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5096 s += strlen(NAME); \
5097 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5101 #define SCAN_FINISH() \
5102 } while (*s++ == ',' && len != 0); \
5103 if (s[-1] != ')') { \
5107 #define SCAN_FINISH_SINGLE() \
5109 if (*s++ != ')') { \
5113 /* Beginning of nested attribute. */
5114 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5116 size_t key_offset, mask_offset; \
5117 key_offset = nl_msg_start_nested(key, ATTR); \
5119 mask_offset = nl_msg_start_nested(mask, ATTR); \
5124 #define SCAN_END_NESTED() \
5126 nl_msg_end_nested(key, key_offset); \
5128 nl_msg_end_nested(mask, mask_offset); \
5133 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5134 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5136 memset(&skey, 0, sizeof skey); \
5137 memset(&smask, 0xff, sizeof smask); \
5138 s += strlen(NAME); \
5139 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5140 SCAN_PUT(ATTR, FUNC); \
5144 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5145 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5147 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5148 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5150 #define SCAN_PUT(ATTR, FUNC) \
5151 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5153 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5155 #define SCAN_END(ATTR) \
5157 SCAN_PUT(ATTR, NULL); \
5161 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5163 TYPE skey[CNT], smask[CNT]; \
5164 memset(&skey, 0, sizeof skey); \
5165 memset(&smask, 0, sizeof smask); \
5166 int idx = 0, cnt = CNT; \
5167 uint64_t fields = 0; \
5172 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5173 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5174 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5175 if (fields & (1UL << field)) { \
5177 if (++idx == cnt) { \
5181 s += strlen(NAME); \
5182 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5183 fields |= 1UL << field; \
5188 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5189 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5191 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5192 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5194 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5197 #define SCAN_END_ARRAY(ATTR) \
5202 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5206 #define SCAN_END_SINGLE(ATTR) \
5207 SCAN_FINISH_SINGLE(); \
5208 SCAN_PUT(ATTR, NULL); \
5212 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5213 SCAN_BEGIN(NAME, TYPE) { \
5214 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5215 } SCAN_END_SINGLE(ATTR)
5217 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5218 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5219 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5220 } SCAN_END_SINGLE(ATTR)
5222 /* scan_port needs one extra argument. */
5223 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5224 SCAN_BEGIN(NAME, TYPE) { \
5225 len = scan_port(s, &skey, &smask, port_names); \
5230 } SCAN_END_SINGLE(ATTR)
5233 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5234 struct ofpbuf
*mask
)
5236 if (strncmp(s
, "nsh(", 4) == 0) {
5237 const char *start
= s
;
5239 struct ovs_key_nsh skey
, smask
;
5240 uint32_t spi
= 0, spi_mask
= 0;
5241 uint8_t si
= 0, si_mask
= 0;
5245 memset(&skey
, 0, sizeof skey
);
5246 memset(&smask
, 0, sizeof smask
);
5250 if (strncmp(s
, "flags=", 6) == 0) {
5252 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5260 if (strncmp(s
, "mdtype=", 7) == 0) {
5262 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5270 if (strncmp(s
, "np=", 3) == 0) {
5272 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5280 if (strncmp(s
, "spi=", 4) == 0) {
5282 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5290 if (strncmp(s
, "si=", 3) == 0) {
5292 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5300 if (strncmp(s
, "c1=", 3) == 0) {
5302 len
= scan_be32(s
, &skey
.context
[0],
5303 mask
? &smask
.context
[0] : NULL
);
5311 if (strncmp(s
, "c2=", 3) == 0) {
5313 len
= scan_be32(s
, &skey
.context
[1],
5314 mask
? &smask
.context
[1] : NULL
);
5322 if (strncmp(s
, "c3=", 3) == 0) {
5324 len
= scan_be32(s
, &skey
.context
[2],
5325 mask
? &smask
.context
[2] : NULL
);
5333 if (strncmp(s
, "c4=", 3) == 0) {
5335 len
= scan_be32(s
, &skey
.context
[3],
5336 mask
? &smask
.context
[3] : NULL
);
5343 } while (*s
++ == ',' && len
!= 0);
5348 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5349 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5351 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5353 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5361 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
5362 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5366 int ufid_len
= odp_ufid_from_string(s
, &ufid
);
5371 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5372 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5373 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5374 OVS_KEY_ATTR_RECIRC_ID
);
5375 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5377 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5378 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5379 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5380 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5382 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5383 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5384 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5385 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5386 SCAN_FIELD("tp_src=", be16
, src_port
);
5387 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5388 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5390 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5391 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5392 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5393 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5394 SCAN_FIELD("tp_src=", be16
, src_port
);
5395 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5396 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5398 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5399 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5400 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5401 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5402 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5403 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5404 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5405 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5406 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5407 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5408 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5410 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5411 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5413 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5414 } SCAN_END_NESTED();
5416 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5418 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5419 SCAN_FIELD("src=", eth
, eth_src
);
5420 SCAN_FIELD("dst=", eth
, eth_dst
);
5421 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5423 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5424 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5425 SCAN_FIELD("vid=", vid
, tci
);
5426 SCAN_FIELD("pcp=", pcp
, tci
);
5427 SCAN_FIELD("cfi=", cfi
, tci
);
5428 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5430 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5432 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5433 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5434 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5435 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5436 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5437 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5439 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5440 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5441 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5442 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5443 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5444 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5445 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5446 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5448 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5449 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5450 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5451 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5452 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5453 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5454 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5455 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5456 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5458 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5459 SCAN_FIELD("src=", be16
, tcp_src
);
5460 SCAN_FIELD("dst=", be16
, tcp_dst
);
5461 } SCAN_END(OVS_KEY_ATTR_TCP
);
5463 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5465 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5466 SCAN_FIELD("src=", be16
, udp_src
);
5467 SCAN_FIELD("dst=", be16
, udp_dst
);
5468 } SCAN_END(OVS_KEY_ATTR_UDP
);
5470 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5471 SCAN_FIELD("src=", be16
, sctp_src
);
5472 SCAN_FIELD("dst=", be16
, sctp_dst
);
5473 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5475 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5476 SCAN_FIELD("type=", u8
, icmp_type
);
5477 SCAN_FIELD("code=", u8
, icmp_code
);
5478 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5480 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5481 SCAN_FIELD("type=", u8
, icmpv6_type
);
5482 SCAN_FIELD("code=", u8
, icmpv6_code
);
5483 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5485 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5486 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5487 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5488 SCAN_FIELD("op=", be16
, arp_op
);
5489 SCAN_FIELD("sha=", eth
, arp_sha
);
5490 SCAN_FIELD("tha=", eth
, arp_tha
);
5491 } SCAN_END(OVS_KEY_ATTR_ARP
);
5493 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5494 SCAN_FIELD("target=", in6_addr
, nd_target
);
5495 SCAN_FIELD("sll=", eth
, nd_sll
);
5496 SCAN_FIELD("tll=", eth
, nd_tll
);
5497 } SCAN_END(OVS_KEY_ATTR_ND
);
5499 struct packet_type
{
5503 SCAN_BEGIN("packet_type(", struct packet_type
) {
5504 SCAN_FIELD("ns=", be16
, ns
);
5505 SCAN_FIELD("id=", be16
, id
);
5506 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5508 /* nsh is nested, it needs special process */
5509 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5516 /* Encap open-coded. */
5517 if (!strncmp(s
, "encap(", 6)) {
5518 const char *start
= s
;
5519 size_t encap
, encap_mask
= 0;
5521 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5523 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5530 s
+= strspn(s
, delimiters
);
5533 } else if (*s
== ')') {
5537 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
5545 nl_msg_end_nested(key
, encap
);
5547 nl_msg_end_nested(mask
, encap_mask
);
5556 /* Parses the string representation of a datapath flow key, in the
5557 * format output by odp_flow_key_format(). Returns 0 if successful,
5558 * otherwise a positive errno value. On success, the flow key is
5559 * appended to 'key' as a series of Netlink attributes. On failure, no
5560 * data is appended to 'key'. Either way, 'key''s data might be
5563 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5564 * to a port number. (Port names may be used instead of port numbers in
5567 * On success, the attributes appended to 'key' are individually syntactically
5568 * valid, but they may not be valid as a sequence. 'key' might, for example,
5569 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5571 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
5572 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5574 const size_t old_size
= key
->size
;
5578 s
+= strspn(s
, delimiters
);
5583 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
5585 key
->size
= old_size
;
5595 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
5598 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5599 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5600 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5601 * must use a zero mask for the netlink frag field, and all ones mask
5603 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
5605 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
5606 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
5607 : OVS_FRAG_TYPE_FIRST
;
5610 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
5611 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
5612 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
5614 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
5616 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
5618 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
5620 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
5621 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
5622 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
5623 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
5624 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
5626 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
5629 /* These share the same layout. */
5631 struct ovs_key_tcp tcp
;
5632 struct ovs_key_udp udp
;
5633 struct ovs_key_sctp sctp
;
5636 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
5637 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
5640 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
5641 bool export_mask
, struct ofpbuf
*buf
)
5643 struct ovs_key_ethernet
*eth_key
;
5644 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
5646 const struct flow
*flow
= parms
->flow
;
5647 const struct flow
*mask
= parms
->mask
;
5648 const struct flow
*data
= export_mask
? mask
: flow
;
5650 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
5652 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
5653 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
5654 parms
->key_buf
, NULL
);
5657 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
5659 if (parms
->support
.ct_state
) {
5660 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5661 ovs_to_odp_ct_state(data
->ct_state
));
5663 if (parms
->support
.ct_zone
) {
5664 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
5666 if (parms
->support
.ct_mark
) {
5667 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
5669 if (parms
->support
.ct_label
) {
5670 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
5671 sizeof(data
->ct_label
));
5673 if (flow
->ct_nw_proto
) {
5674 if (parms
->support
.ct_orig_tuple
5675 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5676 struct ovs_key_ct_tuple_ipv4 ct
= {
5683 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
5685 } else if (parms
->support
.ct_orig_tuple6
5686 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5687 struct ovs_key_ct_tuple_ipv6 ct
= {
5694 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
5698 if (parms
->support
.recirc
) {
5699 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
5700 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
5703 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
5704 * is not the magical value "ODPP_NONE". */
5705 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
5706 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
5709 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
5711 if (OVS_UNLIKELY(parms
->probe
)) {
5712 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
5714 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
5717 /* Conditionally add L2 attributes for Ethernet packets */
5718 if (flow
->packet_type
== htonl(PT_ETH
)) {
5719 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
5721 get_ethernet_key(data
, eth_key
);
5723 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
5724 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
5726 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5727 if (eth_type_vlan(flow
->dl_type
)) {
5728 /* If VLAN was truncated the tpid is in dl_type */
5729 tpid
= flow
->dl_type
;
5736 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5738 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
5740 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
5741 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
5742 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5748 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5749 /* For backwards compatibility with kernels that don't support
5750 * wildcarding, the following convention is used to encode the
5751 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
5754 * -------- -------- -------
5755 * >0x5ff 0xffff Specified Ethernet II Ethertype.
5756 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
5757 * <none> 0xffff Any non-Ethernet II frame (except valid
5758 * 802.3 SNAP packet with valid eth_type).
5761 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5766 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
5768 if (eth_type_vlan(flow
->dl_type
)) {
5772 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5773 struct ovs_key_ipv4
*ipv4_key
;
5775 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
5777 get_ipv4_key(data
, ipv4_key
, export_mask
);
5778 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5779 struct ovs_key_ipv6
*ipv6_key
;
5781 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
5783 get_ipv6_key(data
, ipv6_key
, export_mask
);
5784 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5785 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5786 struct ovs_key_arp
*arp_key
;
5788 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
5790 get_arp_key(data
, arp_key
);
5791 } else if (eth_type_mpls(flow
->dl_type
)) {
5792 struct ovs_key_mpls
*mpls_key
;
5795 n
= flow_count_mpls_labels(flow
, NULL
);
5797 n
= MIN(n
, parms
->support
.max_mpls_depth
);
5799 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
5800 n
* sizeof *mpls_key
);
5801 for (i
= 0; i
< n
; i
++) {
5802 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
5804 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5805 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
5808 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5809 if (flow
->nw_proto
== IPPROTO_TCP
) {
5810 union ovs_key_tp
*tcp_key
;
5812 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
5814 get_tp_key(data
, tcp_key
);
5815 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
5816 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
5818 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5819 union ovs_key_tp
*udp_key
;
5821 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
5823 get_tp_key(data
, udp_key
);
5824 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5825 union ovs_key_tp
*sctp_key
;
5827 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
5829 get_tp_key(data
, sctp_key
);
5830 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
5831 && flow
->nw_proto
== IPPROTO_ICMP
) {
5832 struct ovs_key_icmp
*icmp_key
;
5834 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
5836 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
5837 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
5838 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5839 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
5840 struct ovs_key_icmpv6
*icmpv6_key
;
5842 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
5843 sizeof *icmpv6_key
);
5844 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
5845 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
5847 if (is_nd(flow
, NULL
)
5848 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5849 * type and code are 8 bits wide. Therefore, an exact match
5850 * looks like htons(0xff), not htons(0xffff). See
5851 * xlate_wc_finish() for details. */
5852 && (!export_mask
|| (data
->tp_src
== htons(0xff)
5853 && data
->tp_dst
== htons(0xff)))) {
5855 struct ovs_key_nd
*nd_key
;
5857 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
5859 nd_key
->nd_target
= data
->nd_target
;
5860 nd_key
->nd_sll
= data
->arp_sha
;
5861 nd_key
->nd_tll
= data
->arp_tha
;
5867 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
5868 if (encap
[encaps
]) {
5869 nl_msg_end_nested(buf
, encap
[encaps
]);
5874 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5876 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5877 * capable of being expanded to allow for that much space. */
5879 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
5882 odp_flow_key_from_flow__(parms
, false, buf
);
5885 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5888 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5889 * capable of being expanded to allow for that much space. */
5891 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
5894 odp_flow_key_from_flow__(parms
, true, buf
);
5897 /* Generate ODP flow key from the given packet metadata */
5899 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
5901 const struct pkt_metadata
*md
= &packet
->md
;
5903 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
5905 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
5906 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
5909 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
5912 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5913 ovs_to_odp_ct_state(md
->ct_state
));
5915 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
5918 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
5920 if (!ovs_u128_is_zero(md
->ct_label
)) {
5921 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
5922 sizeof(md
->ct_label
));
5924 if (md
->ct_orig_tuple_ipv6
) {
5925 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
5926 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
5927 &md
->ct_orig_tuple
.ipv6
,
5928 sizeof md
->ct_orig_tuple
.ipv6
);
5931 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
5932 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
5933 &md
->ct_orig_tuple
.ipv4
,
5934 sizeof md
->ct_orig_tuple
.ipv4
);
5939 /* Add an ingress port attribute if 'odp_in_port' is not the magical
5940 * value "ODPP_NONE". */
5941 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
5942 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
5945 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
5946 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
5947 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
5948 pt_ns_type_be(packet
->packet_type
));
5952 /* Generate packet metadata from the given ODP flow key. */
5954 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
5955 struct dp_packet
*packet
)
5957 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5958 const struct nlattr
*nla
;
5959 struct pkt_metadata
*md
= &packet
->md
;
5960 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
5961 ovs_be16 ethertype
= 0;
5964 pkt_metadata_init(md
, ODPP_NONE
);
5966 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
5967 enum ovs_key_attr type
= nl_attr_type(nla
);
5968 size_t len
= nl_attr_get_size(nla
);
5969 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
5970 OVS_KEY_ATTR_MAX
, type
);
5972 if (len
!= expected_len
&& expected_len
>= 0) {
5977 case OVS_KEY_ATTR_RECIRC_ID
:
5978 md
->recirc_id
= nl_attr_get_u32(nla
);
5980 case OVS_KEY_ATTR_DP_HASH
:
5981 md
->dp_hash
= nl_attr_get_u32(nla
);
5983 case OVS_KEY_ATTR_PRIORITY
:
5984 md
->skb_priority
= nl_attr_get_u32(nla
);
5986 case OVS_KEY_ATTR_SKB_MARK
:
5987 md
->pkt_mark
= nl_attr_get_u32(nla
);
5989 case OVS_KEY_ATTR_CT_STATE
:
5990 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
5992 case OVS_KEY_ATTR_CT_ZONE
:
5993 md
->ct_zone
= nl_attr_get_u16(nla
);
5995 case OVS_KEY_ATTR_CT_MARK
:
5996 md
->ct_mark
= nl_attr_get_u32(nla
);
5998 case OVS_KEY_ATTR_CT_LABELS
: {
5999 md
->ct_label
= nl_attr_get_u128(nla
);
6002 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6003 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6004 md
->ct_orig_tuple
.ipv4
= *ct
;
6005 md
->ct_orig_tuple_ipv6
= false;
6008 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6009 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6011 md
->ct_orig_tuple
.ipv6
= *ct
;
6012 md
->ct_orig_tuple_ipv6
= true;
6015 case OVS_KEY_ATTR_TUNNEL
: {
6016 enum odp_key_fitness res
;
6018 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
6019 if (res
== ODP_FIT_ERROR
) {
6020 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6024 case OVS_KEY_ATTR_IN_PORT
:
6025 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6027 case OVS_KEY_ATTR_ETHERNET
:
6028 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6029 packet_type
= htonl(PT_ETH
);
6031 case OVS_KEY_ATTR_ETHERTYPE
:
6032 ethertype
= nl_attr_get_be16(nla
);
6034 case OVS_KEY_ATTR_UNSPEC
:
6035 case OVS_KEY_ATTR_ENCAP
:
6036 case OVS_KEY_ATTR_VLAN
:
6037 case OVS_KEY_ATTR_IPV4
:
6038 case OVS_KEY_ATTR_IPV6
:
6039 case OVS_KEY_ATTR_TCP
:
6040 case OVS_KEY_ATTR_UDP
:
6041 case OVS_KEY_ATTR_ICMP
:
6042 case OVS_KEY_ATTR_ICMPV6
:
6043 case OVS_KEY_ATTR_ARP
:
6044 case OVS_KEY_ATTR_ND
:
6045 case OVS_KEY_ATTR_SCTP
:
6046 case OVS_KEY_ATTR_TCP_FLAGS
:
6047 case OVS_KEY_ATTR_MPLS
:
6048 case OVS_KEY_ATTR_PACKET_TYPE
:
6049 case OVS_KEY_ATTR_NSH
:
6050 case __OVS_KEY_ATTR_MAX
:
6056 if (packet_type
== htonl(PT_ETH
)) {
6057 packet
->packet_type
= htonl(PT_ETH
);
6058 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6059 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6062 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6067 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
6069 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
6070 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
6074 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6075 uint64_t attrs
, int out_of_range_attr
,
6076 const struct nlattr
*key
, size_t key_len
)
6081 if (VLOG_DROP_DBG(rl
)) {
6086 for (i
= 0; i
< 64; i
++) {
6087 if (attrs
& (UINT64_C(1) << i
)) {
6088 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6090 ds_put_format(&s
, " %s",
6091 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6094 if (out_of_range_attr
) {
6095 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6098 ds_put_cstr(&s
, ": ");
6099 odp_flow_key_format(key
, key_len
, &s
);
6101 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6106 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6108 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6111 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6114 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6115 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6116 return 0xff; /* Error. */
6119 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6120 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6121 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6125 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6126 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6127 int *out_of_range_attrp
)
6129 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6130 const struct nlattr
*nla
;
6131 uint64_t present_attrs
;
6134 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6136 *out_of_range_attrp
= 0;
6137 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6138 uint16_t type
= nl_attr_type(nla
);
6139 size_t len
= nl_attr_get_size(nla
);
6140 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6141 OVS_KEY_ATTR_MAX
, type
);
6143 if (len
!= expected_len
&& expected_len
>= 0) {
6144 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6146 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
6147 "length %d", ovs_key_attr_to_string(type
, namebuf
,
6153 if (type
> OVS_KEY_ATTR_MAX
) {
6154 *out_of_range_attrp
= type
;
6156 if (present_attrs
& (UINT64_C(1) << type
)) {
6157 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6159 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
6160 ovs_key_attr_to_string(type
,
6161 namebuf
, sizeof namebuf
));
6165 present_attrs
|= UINT64_C(1) << type
;
6170 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
6174 *present_attrsp
= present_attrs
;
6178 static enum odp_key_fitness
6179 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6180 uint64_t expected_attrs
,
6181 const struct nlattr
*key
, size_t key_len
)
6183 uint64_t missing_attrs
;
6184 uint64_t extra_attrs
;
6186 missing_attrs
= expected_attrs
& ~present_attrs
;
6187 if (missing_attrs
) {
6188 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6189 log_odp_key_attributes(&rl
, "expected but not present",
6190 missing_attrs
, 0, key
, key_len
);
6191 return ODP_FIT_TOO_LITTLE
;
6194 extra_attrs
= present_attrs
& ~expected_attrs
;
6195 if (extra_attrs
|| out_of_range_attr
) {
6196 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6197 log_odp_key_attributes(&rl
, "present but not expected",
6198 extra_attrs
, out_of_range_attr
, key
, key_len
);
6199 return ODP_FIT_TOO_MUCH
;
6202 return ODP_FIT_PERFECT
;
6206 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6207 uint64_t present_attrs
, uint64_t *expected_attrs
,
6208 struct flow
*flow
, const struct flow
*src_flow
)
6210 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6211 bool is_mask
= flow
!= src_flow
;
6213 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6214 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6215 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6216 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
6217 ntohs(flow
->dl_type
));
6220 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6221 flow
->dl_type
!= htons(0xffff)) {
6224 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6227 /* Default ethertype for well-known L3 packets. */
6228 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6229 flow
->dl_type
= htons(ETH_TYPE_IP
);
6230 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6231 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6232 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6233 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6235 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6237 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6238 /* dl_type is mandatory for non-Ethernet packets */
6239 flow
->dl_type
= htons(0xffff);
6240 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6241 /* See comments in odp_flow_key_from_flow__(). */
6242 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
6249 static enum odp_key_fitness
6250 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6251 uint64_t present_attrs
, int out_of_range_attr
,
6252 uint64_t expected_attrs
, struct flow
*flow
,
6253 const struct nlattr
*key
, size_t key_len
,
6254 const struct flow
*src_flow
)
6256 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6257 bool is_mask
= src_flow
!= flow
;
6258 const void *check_start
= NULL
;
6259 size_t check_len
= 0;
6260 enum ovs_key_attr expected_bit
= 0xff;
6262 if (eth_type_mpls(src_flow
->dl_type
)) {
6263 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6264 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6266 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6267 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6268 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6269 int n
= size
/ sizeof(ovs_be32
);
6272 if (!size
|| size
% sizeof(ovs_be32
)) {
6273 return ODP_FIT_ERROR
;
6275 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6276 return ODP_FIT_ERROR
;
6279 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6280 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6282 if (n
> FLOW_MAX_MPLS_LABELS
) {
6283 return ODP_FIT_TOO_MUCH
;
6287 /* BOS may be set only in the innermost label. */
6288 for (i
= 0; i
< n
- 1; i
++) {
6289 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6290 return ODP_FIT_ERROR
;
6294 /* BOS must be set in the innermost label. */
6295 if (n
< FLOW_MAX_MPLS_LABELS
6296 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6297 return ODP_FIT_TOO_LITTLE
;
6303 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6305 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6307 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6308 const struct ovs_key_ipv4
*ipv4_key
;
6310 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6311 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6312 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6313 return ODP_FIT_ERROR
;
6316 check_start
= ipv4_key
;
6317 check_len
= sizeof *ipv4_key
;
6318 expected_bit
= OVS_KEY_ATTR_IPV4
;
6321 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6323 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6325 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6326 const struct ovs_key_ipv6
*ipv6_key
;
6328 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6329 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6330 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6331 return ODP_FIT_ERROR
;
6334 check_start
= ipv6_key
;
6335 check_len
= sizeof *ipv6_key
;
6336 expected_bit
= OVS_KEY_ATTR_IPV6
;
6339 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6340 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6342 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6344 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6345 const struct ovs_key_arp
*arp_key
;
6347 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6348 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6349 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
6350 "key", ntohs(arp_key
->arp_op
));
6351 return ODP_FIT_ERROR
;
6353 put_arp_key(arp_key
, flow
);
6355 check_start
= arp_key
;
6356 check_len
= sizeof *arp_key
;
6357 expected_bit
= OVS_KEY_ATTR_ARP
;
6360 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6362 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6364 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6365 odp_nsh_key_from_attr(attrs
[OVS_KEY_ATTR_NSH
], &flow
->nsh
, NULL
);
6367 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6368 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6369 expected_bit
= OVS_KEY_ATTR_NSH
;
6375 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6376 if (!is_all_zeros(check_start
, check_len
) &&
6377 flow
->dl_type
!= htons(0xffff)) {
6378 return ODP_FIT_ERROR
;
6380 expected_attrs
|= UINT64_C(1) << expected_bit
;
6384 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6385 if (src_flow
->nw_proto
== IPPROTO_TCP
6386 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6387 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6388 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6390 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6392 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6393 const union ovs_key_tp
*tcp_key
;
6395 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6396 put_tp_key(tcp_key
, flow
);
6397 expected_bit
= OVS_KEY_ATTR_TCP
;
6399 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6400 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6401 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6403 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6404 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6405 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6406 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6408 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
6410 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
6411 const union ovs_key_tp
*udp_key
;
6413 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
6414 put_tp_key(udp_key
, flow
);
6415 expected_bit
= OVS_KEY_ATTR_UDP
;
6417 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
6418 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6419 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6420 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6422 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
6424 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
6425 const union ovs_key_tp
*sctp_key
;
6427 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
6428 put_tp_key(sctp_key
, flow
);
6429 expected_bit
= OVS_KEY_ATTR_SCTP
;
6431 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
6432 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
6433 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6435 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
6437 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
6438 const struct ovs_key_icmp
*icmp_key
;
6440 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
6441 flow
->tp_src
= htons(icmp_key
->icmp_type
);
6442 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
6443 expected_bit
= OVS_KEY_ATTR_ICMP
;
6445 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
6446 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6447 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6449 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
6451 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
6452 const struct ovs_key_icmpv6
*icmpv6_key
;
6454 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
6455 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
6456 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
6457 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
6458 if (is_nd(src_flow
, NULL
)) {
6460 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6462 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
6463 const struct ovs_key_nd
*nd_key
;
6465 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
6466 flow
->nd_target
= nd_key
->nd_target
;
6467 flow
->arp_sha
= nd_key
->nd_sll
;
6468 flow
->arp_tha
= nd_key
->nd_tll
;
6470 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6471 * ICMP type and code are 8 bits wide. Therefore, an
6472 * exact match looks like htons(0xff), not
6473 * htons(0xffff). See xlate_wc_finish() for details.
6475 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
6476 (flow
->tp_src
!= htons(0xff) ||
6477 flow
->tp_dst
!= htons(0xff))) {
6478 return ODP_FIT_ERROR
;
6480 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6486 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
6487 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6488 /* OVS userspace parses the IGMP type, code, and group, but its
6489 * datapaths do not, so there is always missing information. */
6490 return ODP_FIT_TOO_LITTLE
;
6492 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
6493 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
6494 return ODP_FIT_ERROR
;
6496 expected_attrs
|= UINT64_C(1) << expected_bit
;
6501 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
6505 /* Parse 802.1Q header then encapsulated L3 attributes. */
6506 static enum odp_key_fitness
6507 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6508 uint64_t present_attrs
, int out_of_range_attr
,
6509 uint64_t expected_attrs
, struct flow
*flow
,
6510 const struct nlattr
*key
, size_t key_len
,
6511 const struct flow
*src_flow
)
6513 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6514 bool is_mask
= src_flow
!= flow
;
6516 const struct nlattr
*encap
;
6517 enum odp_key_fitness encap_fitness
;
6518 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
6521 while (encaps
< flow_vlan_limit
&&
6523 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
6524 : eth_type_vlan(flow
->dl_type
))) {
6526 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
6527 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
6529 /* Calculate fitness of outer attributes. */
6531 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
6532 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
6534 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6535 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6537 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
6538 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
6541 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
6542 expected_attrs
, key
, key_len
);
6545 * Remove the TPID from dl_type since it's not the real Ethertype. */
6546 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
6547 flow
->dl_type
= htons(0);
6548 flow
->vlans
[encaps
].tci
=
6549 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
6550 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
6553 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
6554 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6555 return ODP_FIT_TOO_LITTLE
;
6556 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
6557 /* Corner case for a truncated 802.1Q header. */
6558 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
6559 return ODP_FIT_TOO_MUCH
;
6562 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
6563 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
6564 "but CFI bit is not set",
6565 ntohs(flow
->vlans
[encaps
].tci
));
6566 return ODP_FIT_ERROR
;
6569 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6574 /* Now parse the encapsulated attributes. */
6575 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
6576 attrs
, &present_attrs
, &out_of_range_attr
)) {
6577 return ODP_FIT_ERROR
;
6581 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
6583 return ODP_FIT_ERROR
;
6589 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
6590 expected_attrs
, flow
, key
, key_len
,
6593 /* The overall fitness is the worse of the outer and inner attributes. */
6594 return MAX(fitness
, encap_fitness
);
6597 static enum odp_key_fitness
6598 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
6599 struct flow
*flow
, const struct flow
*src_flow
)
6601 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
6602 uint64_t expected_attrs
;
6603 uint64_t present_attrs
;
6604 int out_of_range_attr
;
6605 bool is_mask
= src_flow
!= flow
;
6607 memset(flow
, 0, sizeof *flow
);
6609 /* Parse attributes. */
6610 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
6611 &out_of_range_attr
)) {
6612 return ODP_FIT_ERROR
;
6617 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
6618 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
6619 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
6620 } else if (is_mask
) {
6621 /* Always exact match recirc_id if it is not specified. */
6622 flow
->recirc_id
= UINT32_MAX
;
6625 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
6626 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
6627 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
6629 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
6630 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
6631 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
6634 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
6635 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
6636 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
6639 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
6640 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
6642 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
6643 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
6645 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
6646 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
6647 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
6649 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
6650 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
6651 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
6653 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
6654 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
6655 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
6657 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
6658 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
6659 flow
->ct_nw_src
= ct
->ipv4_src
;
6660 flow
->ct_nw_dst
= ct
->ipv4_dst
;
6661 flow
->ct_nw_proto
= ct
->ipv4_proto
;
6662 flow
->ct_tp_src
= ct
->src_port
;
6663 flow
->ct_tp_dst
= ct
->dst_port
;
6664 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
6666 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
6667 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
6669 flow
->ct_ipv6_src
= ct
->ipv6_src
;
6670 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
6671 flow
->ct_nw_proto
= ct
->ipv6_proto
;
6672 flow
->ct_tp_src
= ct
->src_port
;
6673 flow
->ct_tp_dst
= ct
->dst_port
;
6674 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
6677 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
6678 enum odp_key_fitness res
;
6680 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
6682 if (res
== ODP_FIT_ERROR
) {
6683 return ODP_FIT_ERROR
;
6684 } else if (res
== ODP_FIT_PERFECT
) {
6685 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
6689 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
6690 flow
->in_port
.odp_port
6691 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
6692 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
6693 } else if (!is_mask
) {
6694 flow
->in_port
.odp_port
= ODPP_NONE
;
6697 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
6699 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
6700 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
6701 } else if (!is_mask
) {
6702 flow
->packet_type
= htonl(PT_ETH
);
6705 /* Check for Ethernet header. */
6706 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
6707 const struct ovs_key_ethernet
*eth_key
;
6709 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
6710 put_ethernet_key(eth_key
, flow
);
6712 flow
->packet_type
= htonl(PT_ETH
);
6714 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
6716 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6717 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6719 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6722 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6725 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
6726 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
6728 return ODP_FIT_ERROR
;
6732 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
6733 : eth_type_vlan(src_flow
->dl_type
)) {
6734 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
6735 expected_attrs
, flow
, key
, key_len
, src_flow
);
6738 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
6739 flow
->vlans
[0].tpid
= htons(0xffff);
6740 flow
->vlans
[0].tci
= htons(0xffff);
6741 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6742 flow
->vlans
[0].tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
6743 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6746 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
6747 expected_attrs
, flow
, key
, key_len
, src_flow
);
6750 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
6751 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
6752 * 'key' fits our expectations for what a flow key should contain.
6754 * The 'in_port' will be the datapath's understanding of the port. The
6755 * caller will need to translate with odp_port_to_ofp_port() if the
6756 * OpenFlow port is needed.
6758 * This function doesn't take the packet itself as an argument because none of
6759 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
6760 * it is always possible to infer which additional attribute(s) should appear
6761 * by looking at the attributes for lower-level protocols, e.g. if the network
6762 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
6763 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
6764 * must be absent. */
6765 enum odp_key_fitness
6766 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
6769 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
6772 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
6773 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
6774 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
6775 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
6776 * well 'key' fits our expectations for what a flow key should contain. */
6777 enum odp_key_fitness
6778 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
6779 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
6782 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
6783 &mask
->masks
, src_flow
);
6786 /* A missing mask means that the flow should be exact matched.
6787 * Generate an appropriate exact wildcard for the flow. */
6788 flow_wildcards_init_for_packet(mask
, src_flow
);
6790 return ODP_FIT_PERFECT
;
6794 /* Converts the netlink formated key/mask to match.
6795 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
6796 * disagree on the acceptable form of flow */
6798 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
6799 const struct nlattr
*mask
, size_t mask_len
,
6800 struct match
*match
)
6802 enum odp_key_fitness fitness
;
6804 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
);
6806 /* This should not happen: it indicates that
6807 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6808 * the acceptable form of a flow. Log the problem as an error,
6809 * with enough details to enable debugging. */
6810 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6812 if (!VLOG_DROP_ERR(&rl
)) {
6816 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
6817 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
6824 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
);
6826 /* This should not happen: it indicates that
6827 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6828 * disagree on the acceptable form of a mask. Log the problem
6829 * as an error, with enough details to enable debugging. */
6830 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6832 if (!VLOG_DROP_ERR(&rl
)) {
6836 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
6838 VLOG_ERR("internal error parsing flow mask %s (%s)",
6839 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
6849 /* Returns 'fitness' as a string, for use in debug messages. */
6851 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
6854 case ODP_FIT_PERFECT
:
6856 case ODP_FIT_TOO_MUCH
:
6858 case ODP_FIT_TOO_LITTLE
:
6859 return "too_little";
6867 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6868 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6869 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6870 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6871 * null, then the return value is not meaningful.) */
6873 odp_put_userspace_action(uint32_t pid
,
6874 const void *userdata
, size_t userdata_size
,
6875 odp_port_t tunnel_out_port
,
6876 bool include_actions
,
6877 struct ofpbuf
*odp_actions
)
6879 size_t userdata_ofs
;
6882 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
6883 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
6885 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
6887 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6888 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6891 * - The kernel rejected shorter userdata with -ERANGE.
6893 * - The kernel silently dropped userdata beyond the first 8 bytes.
6895 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6896 * separately disable features that required more than 8 bytes.) */
6897 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
6898 MAX(8, userdata_size
)),
6899 userdata
, userdata_size
);
6903 if (tunnel_out_port
!= ODPP_NONE
) {
6904 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
6907 if (include_actions
) {
6908 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
6910 nl_msg_end_nested(odp_actions
, offset
);
6912 return userdata_ofs
;
6916 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
6918 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
6922 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
6923 const struct eth_addr
*eth_src
,
6924 const struct eth_addr
*eth_dst
)
6926 struct ovs_action_push_eth eth
;
6928 memset(ð
, 0, sizeof eth
);
6930 eth
.addresses
.eth_src
= *eth_src
;
6933 eth
.addresses
.eth_dst
= *eth_dst
;
6936 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
6941 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
6942 struct ofpbuf
*odp_actions
, const char *tnl_type
)
6944 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6945 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
6946 nl_msg_end_nested(odp_actions
, offset
);
6950 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
6951 struct ovs_action_push_tnl
*data
)
6953 int size
= offsetof(struct ovs_action_push_tnl
, header
);
6955 size
+= data
->header_len
;
6956 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
6960 /* The commit_odp_actions() function and its helpers. */
6963 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
6964 const void *key
, size_t key_size
)
6966 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6967 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
6968 nl_msg_end_nested(odp_actions
, offset
);
6971 /* Masked set actions have a mask following the data within the netlink
6972 * attribute. The unmasked bits in the data will be cleared as the data
6973 * is copied to the action. */
6975 commit_masked_set_action(struct ofpbuf
*odp_actions
,
6976 enum ovs_key_attr key_type
,
6977 const void *key_
, const void *mask_
, size_t key_size
)
6979 size_t offset
= nl_msg_start_nested(odp_actions
,
6980 OVS_ACTION_ATTR_SET_MASKED
);
6981 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
6982 const char *key
= key_
, *mask
= mask_
;
6984 memcpy(data
+ key_size
, mask
, key_size
);
6985 /* Clear unmasked bits while copying. */
6986 while (key_size
--) {
6987 *data
++ = *key
++ & *mask
++;
6989 nl_msg_end_nested(odp_actions
, offset
);
6992 /* If any of the flow key data that ODP actions can modify are different in
6993 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
6994 * 'odp_actions' that change the flow tunneling information in key from
6995 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
6996 * same way. In other words, operates the same as commit_odp_actions(), but
6997 * only on tunneling information. */
6999 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7000 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7002 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7003 * must have non-zero ipv6_dst. */
7004 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7005 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7008 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7009 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7014 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7015 const void *key
, void *base
, void *mask
, size_t size
,
7016 struct ofpbuf
*odp_actions
)
7018 if (memcmp(key
, base
, size
)) {
7019 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7021 if (use_masked_set
&& !fully_masked
) {
7022 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7024 if (!fully_masked
) {
7025 memset(mask
, 0xff, size
);
7027 commit_set_action(odp_actions
, attr
, key
, size
);
7029 memcpy(base
, key
, size
);
7032 /* Mask bits are set when we have either read or set the corresponding
7033 * values. Masked bits will be exact-matched, no need to set them
7034 * if the value did not actually change. */
7040 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7042 eth
->eth_src
= flow
->dl_src
;
7043 eth
->eth_dst
= flow
->dl_dst
;
7047 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7049 flow
->dl_src
= eth
->eth_src
;
7050 flow
->dl_dst
= eth
->eth_dst
;
7054 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7055 struct ofpbuf
*odp_actions
,
7056 struct flow_wildcards
*wc
,
7059 struct ovs_key_ethernet key
, base
, mask
;
7061 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7065 get_ethernet_key(flow
, &key
);
7066 get_ethernet_key(base_flow
, &base
);
7067 get_ethernet_key(&wc
->masks
, &mask
);
7069 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7070 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7071 put_ethernet_key(&base
, base_flow
);
7072 put_ethernet_key(&mask
, &wc
->masks
);
7077 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7078 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7080 int base_n
= flow_count_vlan_headers(base
);
7081 int flow_n
= flow_count_vlan_headers(flow
);
7082 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7084 /* Pop all mismatching vlan of base, push those of flow */
7085 for (; base_n
>= 0; base_n
--) {
7086 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7087 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7090 for (; flow_n
>= 0; flow_n
--) {
7091 struct ovs_action_push_vlan vlan
;
7093 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7094 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7095 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7096 &vlan
, sizeof vlan
);
7098 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7101 /* Wildcarding already done at action translation time. */
7103 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7104 struct ofpbuf
*odp_actions
)
7106 int base_n
= flow_count_mpls_labels(base
, NULL
);
7107 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7108 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7111 while (base_n
> common_n
) {
7112 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7113 /* If there is only one more LSE in base than there are common
7114 * between base and flow; and flow has at least one more LSE than
7115 * is common then the topmost LSE of base may be updated using
7117 struct ovs_key_mpls mpls_key
;
7119 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7120 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7121 &mpls_key
, sizeof mpls_key
);
7122 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7125 /* Otherwise, if there more LSEs in base than are common between
7126 * base and flow then pop the topmost one. */
7128 /* If all the LSEs are to be popped and this is not the outermost
7129 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7130 * POP_MPLS action instead of flow->dl_type.
7132 * This is because the POP_MPLS action requires its ethertype
7133 * argument to be an MPLS ethernet type but in this case
7134 * flow->dl_type will be a non-MPLS ethernet type.
7136 * When the final POP_MPLS action occurs it use flow->dl_type and
7137 * the and the resulting packet will have the desired dl_type. */
7138 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7139 dl_type
= htons(ETH_TYPE_MPLS
);
7141 dl_type
= flow
->dl_type
;
7143 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7144 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7149 /* If, after the above popping and setting, there are more LSEs in flow
7150 * than base then some LSEs need to be pushed. */
7151 while (base_n
< flow_n
) {
7152 struct ovs_action_push_mpls
*mpls
;
7154 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7155 OVS_ACTION_ATTR_PUSH_MPLS
,
7157 mpls
->mpls_ethertype
= flow
->dl_type
;
7158 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7159 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7160 * headers if the flow is restored later due to returning from a patch
7161 * port or group bucket. */
7162 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7163 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7169 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7171 ipv4
->ipv4_src
= flow
->nw_src
;
7172 ipv4
->ipv4_dst
= flow
->nw_dst
;
7173 ipv4
->ipv4_proto
= flow
->nw_proto
;
7174 ipv4
->ipv4_tos
= flow
->nw_tos
;
7175 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7176 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7180 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7182 flow
->nw_src
= ipv4
->ipv4_src
;
7183 flow
->nw_dst
= ipv4
->ipv4_dst
;
7184 flow
->nw_proto
= ipv4
->ipv4_proto
;
7185 flow
->nw_tos
= ipv4
->ipv4_tos
;
7186 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7187 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7191 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7192 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7195 struct ovs_key_ipv4 key
, mask
, base
;
7197 /* Check that nw_proto and nw_frag remain unchanged. */
7198 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7199 flow
->nw_frag
== base_flow
->nw_frag
);
7201 get_ipv4_key(flow
, &key
, false);
7202 get_ipv4_key(base_flow
, &base
, false);
7203 get_ipv4_key(&wc
->masks
, &mask
, true);
7204 mask
.ipv4_proto
= 0; /* Not writeable. */
7205 mask
.ipv4_frag
= 0; /* Not writable. */
7207 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7208 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7209 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7212 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7214 put_ipv4_key(&base
, base_flow
, false);
7215 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
7216 put_ipv4_key(&mask
, &wc
->masks
, true);
7222 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7224 ipv6
->ipv6_src
= flow
->ipv6_src
;
7225 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7226 ipv6
->ipv6_label
= flow
->ipv6_label
;
7227 ipv6
->ipv6_proto
= flow
->nw_proto
;
7228 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7229 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7230 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7234 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7236 flow
->ipv6_src
= ipv6
->ipv6_src
;
7237 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7238 flow
->ipv6_label
= ipv6
->ipv6_label
;
7239 flow
->nw_proto
= ipv6
->ipv6_proto
;
7240 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7241 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7242 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
7246 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
7247 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7250 struct ovs_key_ipv6 key
, mask
, base
;
7252 /* Check that nw_proto and nw_frag remain unchanged. */
7253 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7254 flow
->nw_frag
== base_flow
->nw_frag
);
7256 get_ipv6_key(flow
, &key
, false);
7257 get_ipv6_key(base_flow
, &base
, false);
7258 get_ipv6_key(&wc
->masks
, &mask
, true);
7259 mask
.ipv6_proto
= 0; /* Not writeable. */
7260 mask
.ipv6_frag
= 0; /* Not writable. */
7262 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7263 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7264 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
7267 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7269 put_ipv6_key(&base
, base_flow
, false);
7270 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
7271 put_ipv6_key(&mask
, &wc
->masks
, true);
7277 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
7279 /* ARP key has padding, clear it. */
7280 memset(arp
, 0, sizeof *arp
);
7282 arp
->arp_sip
= flow
->nw_src
;
7283 arp
->arp_tip
= flow
->nw_dst
;
7284 arp
->arp_op
= htons(flow
->nw_proto
);
7285 arp
->arp_sha
= flow
->arp_sha
;
7286 arp
->arp_tha
= flow
->arp_tha
;
7290 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
7292 flow
->nw_src
= arp
->arp_sip
;
7293 flow
->nw_dst
= arp
->arp_tip
;
7294 flow
->nw_proto
= ntohs(arp
->arp_op
);
7295 flow
->arp_sha
= arp
->arp_sha
;
7296 flow
->arp_tha
= arp
->arp_tha
;
7299 static enum slow_path_reason
7300 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
7301 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7303 struct ovs_key_arp key
, mask
, base
;
7305 get_arp_key(flow
, &key
);
7306 get_arp_key(base_flow
, &base
);
7307 get_arp_key(&wc
->masks
, &mask
);
7309 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
7311 put_arp_key(&base
, base_flow
);
7312 put_arp_key(&mask
, &wc
->masks
);
7319 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
7321 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7322 icmp
->icmp_type
= ntohs(flow
->tp_src
);
7323 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
7327 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
7329 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7330 flow
->tp_src
= htons(icmp
->icmp_type
);
7331 flow
->tp_dst
= htons(icmp
->icmp_code
);
7334 static enum slow_path_reason
7335 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
7336 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7338 struct ovs_key_icmp key
, mask
, base
;
7339 enum ovs_key_attr attr
;
7341 if (is_icmpv4(flow
, NULL
)) {
7342 attr
= OVS_KEY_ATTR_ICMP
;
7343 } else if (is_icmpv6(flow
, NULL
)) {
7344 attr
= OVS_KEY_ATTR_ICMPV6
;
7349 get_icmp_key(flow
, &key
);
7350 get_icmp_key(base_flow
, &base
);
7351 get_icmp_key(&wc
->masks
, &mask
);
7353 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7354 put_icmp_key(&base
, base_flow
);
7355 put_icmp_key(&mask
, &wc
->masks
);
7362 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
7364 nd
->nd_target
= flow
->nd_target
;
7365 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7366 nd
->nd_sll
= flow
->arp_sha
;
7367 nd
->nd_tll
= flow
->arp_tha
;
7371 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
7373 flow
->nd_target
= nd
->nd_target
;
7374 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7375 flow
->arp_sha
= nd
->nd_sll
;
7376 flow
->arp_tha
= nd
->nd_tll
;
7379 static enum slow_path_reason
7380 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
7381 struct ofpbuf
*odp_actions
,
7382 struct flow_wildcards
*wc
, bool use_masked
)
7384 struct ovs_key_nd key
, mask
, base
;
7386 get_nd_key(flow
, &key
);
7387 get_nd_key(base_flow
, &base
);
7388 get_nd_key(&wc
->masks
, &mask
);
7390 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7392 put_nd_key(&base
, base_flow
);
7393 put_nd_key(&mask
, &wc
->masks
);
7400 static enum slow_path_reason
7401 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
7402 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7405 /* Check if 'flow' really has an L3 header. */
7406 if (!flow
->nw_proto
) {
7410 switch (ntohs(base
->dl_type
)) {
7412 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
7416 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
7417 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
7420 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
7427 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
7431 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
7432 memset(nsh
->context
, 0, sizeof(nsh
->context
));
7438 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
7439 bool is_mask OVS_UNUSED
)
7442 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
7443 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
7448 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
7449 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
7450 struct ovs_key_nsh
*mask
, size_t size
,
7451 struct ofpbuf
*odp_actions
)
7453 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
7455 if (memcmp(key
, base
, size
) == 0) {
7456 /* Mask bits are set when we have either read or set the corresponding
7457 * values. Masked bits will be exact-matched, no need to set them
7458 * if the value did not actually change. */
7462 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7464 if (use_masked_set
&& !fully_masked
) {
7466 struct ovs_nsh_key_base nsh_base
;
7467 struct ovs_nsh_key_base nsh_base_mask
;
7468 struct ovs_nsh_key_md1 md1
;
7469 struct ovs_nsh_key_md1 md1_mask
;
7470 size_t offset
= nl_msg_start_nested(odp_actions
,
7471 OVS_ACTION_ATTR_SET_MASKED
);
7473 nsh_base
.flags
= key
->flags
;
7474 nsh_base
.ttl
= key
->ttl
;
7475 nsh_base
.mdtype
= key
->mdtype
;
7476 nsh_base
.np
= key
->np
;
7477 nsh_base
.path_hdr
= key
->path_hdr
;
7479 nsh_base_mask
.flags
= mask
->flags
;
7480 nsh_base_mask
.ttl
= mask
->ttl
;
7481 nsh_base_mask
.mdtype
= mask
->mdtype
;
7482 nsh_base_mask
.np
= mask
->np
;
7483 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
7485 /* OVS_KEY_ATTR_NSH keys */
7486 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
7488 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
7489 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
7490 OVS_NSH_KEY_ATTR_BASE
,
7491 2 * sizeof(nsh_base
));
7492 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
7493 size_t lkey_size
= sizeof(nsh_base
);
7495 while (lkey_size
--) {
7496 *data
++ = *lkey
++ & *lmask
++;
7498 lmask
= (char *)&nsh_base_mask
;
7499 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
7501 switch (key
->mdtype
) {
7503 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
7504 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
7506 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
7507 data
= nl_msg_put_unspec_uninit(odp_actions
,
7508 OVS_NSH_KEY_ATTR_MD1
,
7510 lkey
= (char *)&md1
;
7511 lmask
= (char *)&md1_mask
;
7512 lkey_size
= sizeof(md1
);
7514 while (lkey_size
--) {
7515 *data
++ = *lkey
++ & *lmask
++;
7517 lmask
= (char *)&md1_mask
;
7518 memcpy(data
, lmask
, sizeof(md1_mask
));
7522 /* No match support for other MD formats yet. */
7526 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
7528 nl_msg_end_nested(odp_actions
, offset
);
7530 if (!fully_masked
) {
7531 memset(mask
, 0xff, size
);
7533 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7534 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
7535 nl_msg_end_nested(odp_actions
, offset
);
7537 memcpy(base
, key
, size
);
7542 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
7543 struct ofpbuf
*odp_actions
,
7544 struct flow_wildcards
*wc
,
7547 struct ovs_key_nsh key
, mask
, base
;
7549 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
7550 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
7554 /* Check that mdtype and np remain unchanged. */
7555 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
7556 flow
->nsh
.np
== base_flow
->nsh
.np
);
7558 get_nsh_key(flow
, &key
, false);
7559 get_nsh_key(base_flow
, &base
, false);
7560 get_nsh_key(&wc
->masks
, &mask
, true);
7561 mask
.mdtype
= 0; /* Not writable. */
7562 mask
.np
= 0; /* Not writable. */
7564 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
7565 sizeof key
, odp_actions
)) {
7566 put_nsh_key(&base
, base_flow
, false);
7567 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
7568 put_nsh_key(&mask
, &wc
->masks
, true);
7573 /* TCP, UDP, and SCTP keys have the same layout. */
7574 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
7575 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
7578 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
7580 tp
->tcp
.tcp_src
= flow
->tp_src
;
7581 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
7585 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
7587 flow
->tp_src
= tp
->tcp
.tcp_src
;
7588 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
7592 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
7593 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7596 enum ovs_key_attr key_type
;
7597 union ovs_key_tp key
, mask
, base
;
7599 /* Check if 'flow' really has an L3 header. */
7600 if (!flow
->nw_proto
) {
7604 if (!is_ip_any(base_flow
)) {
7608 if (flow
->nw_proto
== IPPROTO_TCP
) {
7609 key_type
= OVS_KEY_ATTR_TCP
;
7610 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
7611 key_type
= OVS_KEY_ATTR_UDP
;
7612 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
7613 key_type
= OVS_KEY_ATTR_SCTP
;
7618 get_tp_key(flow
, &key
);
7619 get_tp_key(base_flow
, &base
);
7620 get_tp_key(&wc
->masks
, &mask
);
7622 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7624 put_tp_key(&base
, base_flow
);
7625 put_tp_key(&mask
, &wc
->masks
);
7630 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
7631 struct ofpbuf
*odp_actions
,
7632 struct flow_wildcards
*wc
,
7635 uint32_t key
, mask
, base
;
7637 key
= flow
->skb_priority
;
7638 base
= base_flow
->skb_priority
;
7639 mask
= wc
->masks
.skb_priority
;
7641 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
7642 sizeof key
, odp_actions
)) {
7643 base_flow
->skb_priority
= base
;
7644 wc
->masks
.skb_priority
= mask
;
7649 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
7650 struct ofpbuf
*odp_actions
,
7651 struct flow_wildcards
*wc
,
7654 uint32_t key
, mask
, base
;
7656 key
= flow
->pkt_mark
;
7657 base
= base_flow
->pkt_mark
;
7658 mask
= wc
->masks
.pkt_mark
;
7660 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
7661 sizeof key
, odp_actions
)) {
7662 base_flow
->pkt_mark
= base
;
7663 wc
->masks
.pkt_mark
= mask
;
7668 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
7670 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
7674 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
7675 const struct flow
*flow
,
7676 struct ofpbuf
*encap_data
)
7678 uint8_t * metadata
= NULL
;
7679 uint8_t md_size
= 0;
7681 switch (flow
->nsh
.mdtype
) {
7684 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
7685 metadata
= encap_data
->data
;
7686 md_size
= encap_data
->size
;
7695 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
7696 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
7697 nl_msg_end_nested(odp_actions
, offset
);
7701 commit_encap_decap_action(const struct flow
*flow
,
7702 struct flow
*base_flow
,
7703 struct ofpbuf
*odp_actions
,
7704 struct flow_wildcards
*wc
,
7705 bool pending_encap
, bool pending_decap
,
7706 struct ofpbuf
*encap_data
)
7708 if (pending_encap
) {
7709 switch (ntohl(flow
->packet_type
)) {
7712 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
7714 base_flow
->packet_type
= flow
->packet_type
;
7715 base_flow
->dl_src
= flow
->dl_src
;
7716 base_flow
->dl_dst
= flow
->dl_dst
;
7721 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
7722 base_flow
->packet_type
= flow
->packet_type
;
7723 /* Update all packet headers in base_flow. */
7724 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
7725 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
7728 /* Only the above protocols are supported for encap.
7729 * The check is done at action translation. */
7732 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
7733 /* This is an explicit or implicit decap case. */
7734 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
7735 base_flow
->packet_type
== htonl(PT_ETH
)) {
7736 /* Generate pop_eth and continue without recirculation. */
7737 odp_put_pop_eth_action(odp_actions
);
7738 base_flow
->packet_type
= flow
->packet_type
;
7739 base_flow
->dl_src
= eth_addr_zero
;
7740 base_flow
->dl_dst
= eth_addr_zero
;
7742 /* All other decap cases require recirculation.
7743 * No need to update the base flow here. */
7744 switch (ntohl(base_flow
->packet_type
)) {
7747 odp_put_pop_nsh_action(odp_actions
);
7750 /* Checks are done during translation. */
7756 wc
->masks
.packet_type
= OVS_BE32_MAX
;
7759 /* If any of the flow key data that ODP actions can modify are different in
7760 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
7761 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
7762 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
7763 * in addition to this function if needed. Sets fields in 'wc' that are
7764 * used as part of the action.
7766 * Returns a reason to force processing the flow's packets into the userspace
7767 * slow path, if there is one, otherwise 0. */
7768 enum slow_path_reason
7769 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
7770 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7771 bool use_masked
, bool pending_encap
, bool pending_decap
,
7772 struct ofpbuf
*encap_data
)
7774 enum slow_path_reason slow1
, slow2
;
7775 bool mpls_done
= false;
7777 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
7778 pending_encap
, pending_decap
, encap_data
);
7779 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
7780 /* Make packet a non-MPLS packet before committing L3/4 actions,
7781 * which would otherwise do nothing. */
7782 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
7783 commit_mpls_action(flow
, base
, odp_actions
);
7786 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
7787 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
7788 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
7789 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
7791 commit_mpls_action(flow
, base
, odp_actions
);
7793 commit_vlan_action(flow
, base
, odp_actions
, wc
);
7794 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
7795 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
7797 return slow1
? slow1
: slow2
;