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
, "l4(%"PRIu32
")", hash_act
->hash_basis
);
599 } else if (hash_act
->hash_alg
== OVS_HASH_ALG_SYM_L4
) {
600 ds_put_format(ds
, "sym_l4(%"PRIu32
")", hash_act
->hash_basis
);
602 ds_put_format(ds
, "Unknown hash algorithm(%"PRIu32
")",
605 ds_put_format(ds
, ")");
609 format_udp_tnl_push_header(struct ds
*ds
, const struct udp_header
*udp
)
611 ds_put_format(ds
, "udp(src=%"PRIu16
",dst=%"PRIu16
",csum=0x%"PRIx16
"),",
612 ntohs(udp
->udp_src
), ntohs(udp
->udp_dst
),
613 ntohs(udp
->udp_csum
));
619 format_odp_tnl_push_header(struct ds
*ds
, struct ovs_action_push_tnl
*data
)
621 const struct eth_header
*eth
;
624 const struct udp_header
*udp
;
626 eth
= (const struct eth_header
*)data
->header
;
631 ds_put_format(ds
, "header(size=%"PRIu32
",type=%"PRIu32
",eth(dst=",
632 data
->header_len
, data
->tnl_type
);
633 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_dst
));
634 ds_put_format(ds
, ",src=");
635 ds_put_format(ds
, ETH_ADDR_FMT
, ETH_ADDR_ARGS(eth
->eth_src
));
636 ds_put_format(ds
, ",dl_type=0x%04"PRIx16
"),", ntohs(eth
->eth_type
));
638 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
640 const struct ip_header
*ip
= l3
;
641 ds_put_format(ds
, "ipv4(src="IP_FMT
",dst="IP_FMT
",proto=%"PRIu8
642 ",tos=%#"PRIx8
",ttl=%"PRIu8
",frag=0x%"PRIx16
"),",
643 IP_ARGS(get_16aligned_be32(&ip
->ip_src
)),
644 IP_ARGS(get_16aligned_be32(&ip
->ip_dst
)),
645 ip
->ip_proto
, ip
->ip_tos
,
647 ntohs(ip
->ip_frag_off
));
650 const struct ovs_16aligned_ip6_hdr
*ip6
= l3
;
651 struct in6_addr src
, dst
;
652 memcpy(&src
, &ip6
->ip6_src
, sizeof src
);
653 memcpy(&dst
, &ip6
->ip6_dst
, sizeof dst
);
654 uint32_t ipv6_flow
= ntohl(get_16aligned_be32(&ip6
->ip6_flow
));
656 ds_put_format(ds
, "ipv6(src=");
657 ipv6_format_addr(&src
, ds
);
658 ds_put_format(ds
, ",dst=");
659 ipv6_format_addr(&dst
, ds
);
660 ds_put_format(ds
, ",label=%i,proto=%"PRIu8
",tclass=0x%"PRIx32
661 ",hlimit=%"PRIu8
"),",
662 ipv6_flow
& IPV6_LABEL_MASK
, ip6
->ip6_nxt
,
663 (ipv6_flow
>> 20) & 0xff, ip6
->ip6_hlim
);
667 udp
= (const struct udp_header
*) l4
;
669 if (data
->tnl_type
== OVS_VPORT_TYPE_VXLAN
) {
670 const struct vxlanhdr
*vxh
;
672 vxh
= format_udp_tnl_push_header(ds
, udp
);
674 ds_put_format(ds
, "vxlan(flags=0x%"PRIx32
",vni=0x%"PRIx32
")",
675 ntohl(get_16aligned_be32(&vxh
->vx_flags
)),
676 ntohl(get_16aligned_be32(&vxh
->vx_vni
)) >> 8);
677 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GENEVE
) {
678 const struct genevehdr
*gnh
;
680 gnh
= format_udp_tnl_push_header(ds
, udp
);
682 ds_put_format(ds
, "geneve(%s%svni=0x%"PRIx32
,
683 gnh
->oam
? "oam," : "",
684 gnh
->critical
? "crit," : "",
685 ntohl(get_16aligned_be32(&gnh
->vni
)) >> 8);
688 ds_put_cstr(ds
, ",options(");
689 format_geneve_opts(gnh
->options
, NULL
, gnh
->opt_len
* 4,
691 ds_put_char(ds
, ')');
694 ds_put_char(ds
, ')');
695 } else if (data
->tnl_type
== OVS_VPORT_TYPE_GRE
||
696 data
->tnl_type
== OVS_VPORT_TYPE_IP6GRE
) {
697 const struct gre_base_hdr
*greh
;
698 ovs_16aligned_be32
*options
;
700 greh
= (const struct gre_base_hdr
*) l4
;
702 ds_put_format(ds
, "gre((flags=0x%"PRIx16
",proto=0x%"PRIx16
")",
703 ntohs(greh
->flags
), ntohs(greh
->protocol
));
704 options
= (ovs_16aligned_be32
*)(greh
+ 1);
705 if (greh
->flags
& htons(GRE_CSUM
)) {
706 ds_put_format(ds
, ",csum=0x%"PRIx16
, ntohs(*((ovs_be16
*)options
)));
709 if (greh
->flags
& htons(GRE_KEY
)) {
710 ds_put_format(ds
, ",key=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
713 if (greh
->flags
& htons(GRE_SEQ
)) {
714 ds_put_format(ds
, ",seq=0x%"PRIx32
, ntohl(get_16aligned_be32(options
)));
717 ds_put_format(ds
, ")");
718 } else if (data
->tnl_type
== OVS_VPORT_TYPE_ERSPAN
||
719 data
->tnl_type
== OVS_VPORT_TYPE_IP6ERSPAN
) {
720 const struct gre_base_hdr
*greh
;
721 const struct erspan_base_hdr
*ersh
;
723 greh
= (const struct gre_base_hdr
*) l4
;
724 ersh
= ERSPAN_HDR(greh
);
726 if (ersh
->ver
== 1) {
727 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
729 ds_put_format(ds
, "erspan(ver=1,sid=0x%"PRIx16
",idx=0x%"PRIx32
")",
730 get_sid(ersh
), ntohl(get_16aligned_be32(index
)));
731 } else if (ersh
->ver
== 2) {
732 struct erspan_md2
*md2
= ALIGNED_CAST(struct erspan_md2
*,
734 ds_put_format(ds
, "erspan(ver=2,sid=0x%"PRIx16
735 ",dir=%"PRIu8
",hwid=0x%"PRIx8
")",
736 get_sid(ersh
), md2
->dir
, get_hwid(md2
));
738 VLOG_WARN("%s Invalid ERSPAN version %d\n", __func__
, ersh
->ver
);
741 ds_put_format(ds
, ")");
745 format_odp_tnl_push_action(struct ds
*ds
, const struct nlattr
*attr
,
746 const struct hmap
*portno_names
)
748 struct ovs_action_push_tnl
*data
;
750 data
= (struct ovs_action_push_tnl
*) nl_attr_get(attr
);
752 ds_put_cstr(ds
, "tnl_push(tnl_port(");
753 odp_portno_name_format(portno_names
, data
->tnl_port
, ds
);
754 ds_put_cstr(ds
, "),");
755 format_odp_tnl_push_header(ds
, data
);
756 ds_put_format(ds
, ",out_port(");
757 odp_portno_name_format(portno_names
, data
->out_port
, ds
);
758 ds_put_cstr(ds
, "))");
761 static const struct nl_policy ovs_nat_policy
[] = {
762 [OVS_NAT_ATTR_SRC
] = { .type
= NL_A_FLAG
, .optional
= true, },
763 [OVS_NAT_ATTR_DST
] = { .type
= NL_A_FLAG
, .optional
= true, },
764 [OVS_NAT_ATTR_IP_MIN
] = { .type
= NL_A_UNSPEC
, .optional
= true,
765 .min_len
= sizeof(struct in_addr
),
766 .max_len
= sizeof(struct in6_addr
)},
767 [OVS_NAT_ATTR_IP_MAX
] = { .type
= NL_A_UNSPEC
, .optional
= true,
768 .min_len
= sizeof(struct in_addr
),
769 .max_len
= sizeof(struct in6_addr
)},
770 [OVS_NAT_ATTR_PROTO_MIN
] = { .type
= NL_A_U16
, .optional
= true, },
771 [OVS_NAT_ATTR_PROTO_MAX
] = { .type
= NL_A_U16
, .optional
= true, },
772 [OVS_NAT_ATTR_PERSISTENT
] = { .type
= NL_A_FLAG
, .optional
= true, },
773 [OVS_NAT_ATTR_PROTO_HASH
] = { .type
= NL_A_FLAG
, .optional
= true, },
774 [OVS_NAT_ATTR_PROTO_RANDOM
] = { .type
= NL_A_FLAG
, .optional
= true, },
778 format_odp_ct_nat(struct ds
*ds
, const struct nlattr
*attr
)
780 struct nlattr
*a
[ARRAY_SIZE(ovs_nat_policy
)];
782 ovs_be32 ip_min
, ip_max
;
783 struct in6_addr ip6_min
, ip6_max
;
784 uint16_t proto_min
, proto_max
;
786 if (!nl_parse_nested(attr
, ovs_nat_policy
, a
, ARRAY_SIZE(a
))) {
787 ds_put_cstr(ds
, "nat(error: nl_parse_nested() failed.)");
790 /* If no type, then nothing else either. */
791 if (!(a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
])
792 && (a
[OVS_NAT_ATTR_IP_MIN
] || a
[OVS_NAT_ATTR_IP_MAX
]
793 || a
[OVS_NAT_ATTR_PROTO_MIN
] || a
[OVS_NAT_ATTR_PROTO_MAX
]
794 || a
[OVS_NAT_ATTR_PERSISTENT
] || a
[OVS_NAT_ATTR_PROTO_HASH
]
795 || a
[OVS_NAT_ATTR_PROTO_RANDOM
])) {
796 ds_put_cstr(ds
, "nat(error: options allowed only with \"src\" or \"dst\")");
799 /* Both SNAT & DNAT may not be specified. */
800 if (a
[OVS_NAT_ATTR_SRC
] && a
[OVS_NAT_ATTR_DST
]) {
801 ds_put_cstr(ds
, "nat(error: Only one of \"src\" or \"dst\" may be present.)");
804 /* proto may not appear without ip. */
805 if (!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_PROTO_MIN
]) {
806 ds_put_cstr(ds
, "nat(error: proto but no IP.)");
809 /* MAX may not appear without MIN. */
810 if ((!a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
])
811 || (!a
[OVS_NAT_ATTR_PROTO_MIN
] && a
[OVS_NAT_ATTR_PROTO_MAX
])) {
812 ds_put_cstr(ds
, "nat(error: range max without min.)");
815 /* Address sizes must match. */
816 if ((a
[OVS_NAT_ATTR_IP_MIN
]
817 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(ovs_be32
) &&
818 nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) != sizeof(struct in6_addr
)))
819 || (a
[OVS_NAT_ATTR_IP_MIN
] && a
[OVS_NAT_ATTR_IP_MAX
]
820 && (nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
])
821 != nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MAX
])))) {
822 ds_put_cstr(ds
, "nat(error: IP address sizes do not match)");
826 addr_len
= a
[OVS_NAT_ATTR_IP_MIN
]
827 ? nl_attr_get_size(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
828 ip_min
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MIN
]
829 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MIN
]) : 0;
830 ip_max
= addr_len
== sizeof(ovs_be32
) && a
[OVS_NAT_ATTR_IP_MAX
]
831 ? nl_attr_get_be32(a
[OVS_NAT_ATTR_IP_MAX
]) : 0;
832 if (addr_len
== sizeof ip6_min
) {
833 ip6_min
= a
[OVS_NAT_ATTR_IP_MIN
]
834 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MIN
])
836 ip6_max
= a
[OVS_NAT_ATTR_IP_MAX
]
837 ? *(struct in6_addr
*)nl_attr_get(a
[OVS_NAT_ATTR_IP_MAX
])
840 proto_min
= a
[OVS_NAT_ATTR_PROTO_MIN
]
841 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MIN
]) : 0;
842 proto_max
= a
[OVS_NAT_ATTR_PROTO_MAX
]
843 ? nl_attr_get_u16(a
[OVS_NAT_ATTR_PROTO_MAX
]) : 0;
845 if ((addr_len
== sizeof(ovs_be32
)
846 && ip_max
&& ntohl(ip_min
) > ntohl(ip_max
))
847 || (addr_len
== sizeof(struct in6_addr
)
848 && !ipv6_mask_is_any(&ip6_max
)
849 && memcmp(&ip6_min
, &ip6_max
, sizeof ip6_min
) > 0)
850 || (proto_max
&& proto_min
> proto_max
)) {
851 ds_put_cstr(ds
, "nat(range error)");
855 ds_put_cstr(ds
, "nat");
856 if (a
[OVS_NAT_ATTR_SRC
] || a
[OVS_NAT_ATTR_DST
]) {
857 ds_put_char(ds
, '(');
858 if (a
[OVS_NAT_ATTR_SRC
]) {
859 ds_put_cstr(ds
, "src");
860 } else if (a
[OVS_NAT_ATTR_DST
]) {
861 ds_put_cstr(ds
, "dst");
865 ds_put_cstr(ds
, "=");
867 if (addr_len
== sizeof ip_min
) {
868 ds_put_format(ds
, IP_FMT
, IP_ARGS(ip_min
));
870 if (ip_max
&& ip_max
!= ip_min
) {
871 ds_put_format(ds
, "-"IP_FMT
, IP_ARGS(ip_max
));
873 } else if (addr_len
== sizeof ip6_min
) {
874 ipv6_format_addr_bracket(&ip6_min
, ds
, proto_min
);
876 if (!ipv6_mask_is_any(&ip6_max
) &&
877 memcmp(&ip6_max
, &ip6_min
, sizeof ip6_max
) != 0) {
878 ds_put_char(ds
, '-');
879 ipv6_format_addr_bracket(&ip6_max
, ds
, proto_min
);
883 ds_put_format(ds
, ":%"PRIu16
, proto_min
);
885 if (proto_max
&& proto_max
!= proto_min
) {
886 ds_put_format(ds
, "-%"PRIu16
, proto_max
);
890 ds_put_char(ds
, ',');
891 if (a
[OVS_NAT_ATTR_PERSISTENT
]) {
892 ds_put_cstr(ds
, "persistent,");
894 if (a
[OVS_NAT_ATTR_PROTO_HASH
]) {
895 ds_put_cstr(ds
, "hash,");
897 if (a
[OVS_NAT_ATTR_PROTO_RANDOM
]) {
898 ds_put_cstr(ds
, "random,");
901 ds_put_char(ds
, ')');
905 static const struct nl_policy ovs_conntrack_policy
[] = {
906 [OVS_CT_ATTR_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
907 [OVS_CT_ATTR_FORCE_COMMIT
] = { .type
= NL_A_FLAG
, .optional
= true, },
908 [OVS_CT_ATTR_ZONE
] = { .type
= NL_A_U16
, .optional
= true, },
909 [OVS_CT_ATTR_MARK
] = { .type
= NL_A_UNSPEC
, .optional
= true,
910 .min_len
= sizeof(uint32_t) * 2 },
911 [OVS_CT_ATTR_LABELS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
912 .min_len
= sizeof(struct ovs_key_ct_labels
) * 2 },
913 [OVS_CT_ATTR_HELPER
] = { .type
= NL_A_STRING
, .optional
= true,
914 .min_len
= 1, .max_len
= 16 },
915 [OVS_CT_ATTR_NAT
] = { .type
= NL_A_UNSPEC
, .optional
= true },
919 format_odp_conntrack_action(struct ds
*ds
, const struct nlattr
*attr
)
921 struct nlattr
*a
[ARRAY_SIZE(ovs_conntrack_policy
)];
923 ovs_32aligned_u128 value
;
924 ovs_32aligned_u128 mask
;
926 const uint32_t *mark
;
930 const struct nlattr
*nat
;
932 if (!nl_parse_nested(attr
, ovs_conntrack_policy
, a
, ARRAY_SIZE(a
))) {
933 ds_put_cstr(ds
, "ct(error)");
937 commit
= a
[OVS_CT_ATTR_COMMIT
] ? true : false;
938 force
= a
[OVS_CT_ATTR_FORCE_COMMIT
] ? true : false;
939 zone
= a
[OVS_CT_ATTR_ZONE
] ? nl_attr_get_u16(a
[OVS_CT_ATTR_ZONE
]) : 0;
940 mark
= a
[OVS_CT_ATTR_MARK
] ? nl_attr_get(a
[OVS_CT_ATTR_MARK
]) : NULL
;
941 label
= a
[OVS_CT_ATTR_LABELS
] ? nl_attr_get(a
[OVS_CT_ATTR_LABELS
]): NULL
;
942 helper
= a
[OVS_CT_ATTR_HELPER
] ? nl_attr_get(a
[OVS_CT_ATTR_HELPER
]) : NULL
;
943 nat
= a
[OVS_CT_ATTR_NAT
];
945 ds_put_format(ds
, "ct");
946 if (commit
|| force
|| zone
|| mark
|| label
|| helper
|| nat
) {
947 ds_put_cstr(ds
, "(");
949 ds_put_format(ds
, "commit,");
952 ds_put_format(ds
, "force_commit,");
955 ds_put_format(ds
, "zone=%"PRIu16
",", zone
);
958 ds_put_format(ds
, "mark=%#"PRIx32
"/%#"PRIx32
",", *mark
,
962 ds_put_format(ds
, "label=");
963 format_u128(ds
, &label
->value
, &label
->mask
, true);
964 ds_put_char(ds
, ',');
967 ds_put_format(ds
, "helper=%s,", helper
);
970 format_odp_ct_nat(ds
, nat
);
973 ds_put_cstr(ds
, ")");
977 static const struct attr_len_tbl
978 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
979 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= 8 },
980 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= 16 },
981 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= ATTR_LEN_VARIABLE
},
985 format_odp_set_nsh(struct ds
*ds
, const struct nlattr
*attr
)
988 const struct nlattr
*a
;
989 struct ovs_key_nsh nsh
;
990 struct ovs_key_nsh nsh_mask
;
992 memset(&nsh
, 0, sizeof nsh
);
993 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
995 NL_NESTED_FOR_EACH (a
, left
, attr
) {
996 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
997 size_t len
= nl_attr_get_size(a
);
999 if (type
>= OVS_NSH_KEY_ATTR_MAX
) {
1003 int expected_len
= ovs_nsh_key_attr_lens
[type
].len
;
1004 if ((expected_len
!= ATTR_LEN_VARIABLE
) && (len
!= 2 * expected_len
)) {
1009 case OVS_NSH_KEY_ATTR_UNSPEC
:
1011 case OVS_NSH_KEY_ATTR_BASE
: {
1012 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
1013 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1014 memcpy(&nsh
, base
, sizeof(*base
));
1015 memcpy(&nsh_mask
, base_mask
, sizeof(*base_mask
));
1018 case OVS_NSH_KEY_ATTR_MD1
: {
1019 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
1020 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1021 memcpy(&nsh
.context
, &md1
->context
, sizeof(*md1
));
1022 memcpy(&nsh_mask
.context
, &md1_mask
->context
, sizeof(*md1_mask
));
1025 case OVS_NSH_KEY_ATTR_MD2
:
1026 case __OVS_NSH_KEY_ATTR_MAX
:
1028 /* No support for matching other metadata formats yet. */
1033 ds_put_cstr(ds
, "set(nsh(");
1034 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
1035 ds_put_cstr(ds
, "))");
1040 format_odp_action(struct ds
*ds
, const struct nlattr
*a
,
1041 const struct hmap
*portno_names
)
1044 enum ovs_action_attr type
= nl_attr_type(a
);
1047 expected_len
= odp_action_len(nl_attr_type(a
));
1048 if (expected_len
!= ATTR_LEN_VARIABLE
&&
1049 nl_attr_get_size(a
) != expected_len
) {
1050 ds_put_format(ds
, "bad length %"PRIuSIZE
", expected %d for: ",
1051 nl_attr_get_size(a
), expected_len
);
1052 format_generic_odp_action(ds
, a
);
1057 case OVS_ACTION_ATTR_METER
:
1058 ds_put_format(ds
, "meter(%"PRIu32
")", nl_attr_get_u32(a
));
1060 case OVS_ACTION_ATTR_OUTPUT
:
1061 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1063 case OVS_ACTION_ATTR_TRUNC
: {
1064 const struct ovs_action_trunc
*trunc
=
1065 nl_attr_get_unspec(a
, sizeof *trunc
);
1067 ds_put_format(ds
, "trunc(%"PRIu32
")", trunc
->max_len
);
1070 case OVS_ACTION_ATTR_TUNNEL_POP
:
1071 ds_put_cstr(ds
, "tnl_pop(");
1072 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
1073 ds_put_char(ds
, ')');
1075 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1076 format_odp_tnl_push_action(ds
, a
, portno_names
);
1078 case OVS_ACTION_ATTR_USERSPACE
:
1079 format_odp_userspace_action(ds
, a
, portno_names
);
1081 case OVS_ACTION_ATTR_RECIRC
:
1082 format_odp_recirc_action(ds
, nl_attr_get_u32(a
));
1084 case OVS_ACTION_ATTR_HASH
:
1085 format_odp_hash_action(ds
, nl_attr_get(a
));
1087 case OVS_ACTION_ATTR_SET_MASKED
:
1089 /* OVS_KEY_ATTR_NSH is nested attribute, so it needs special process */
1090 if (nl_attr_type(a
) == OVS_KEY_ATTR_NSH
) {
1091 format_odp_set_nsh(ds
, a
);
1094 size
= nl_attr_get_size(a
) / 2;
1095 ds_put_cstr(ds
, "set(");
1097 /* Masked set action not supported for tunnel key, which is bigger. */
1098 if (size
<= sizeof(struct ovs_key_ipv6
)) {
1099 struct nlattr attr
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1100 sizeof(struct nlattr
))];
1101 struct nlattr mask
[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6
),
1102 sizeof(struct nlattr
))];
1104 mask
->nla_type
= attr
->nla_type
= nl_attr_type(a
);
1105 mask
->nla_len
= attr
->nla_len
= NLA_HDRLEN
+ size
;
1106 memcpy(attr
+ 1, (char *)(a
+ 1), size
);
1107 memcpy(mask
+ 1, (char *)(a
+ 1) + size
, size
);
1108 format_odp_key_attr(attr
, mask
, NULL
, ds
, false);
1110 format_odp_key_attr(a
, NULL
, NULL
, ds
, false);
1112 ds_put_cstr(ds
, ")");
1114 case OVS_ACTION_ATTR_SET
:
1115 ds_put_cstr(ds
, "set(");
1116 format_odp_key_attr(nl_attr_get(a
), NULL
, NULL
, ds
, true);
1117 ds_put_cstr(ds
, ")");
1119 case OVS_ACTION_ATTR_PUSH_ETH
: {
1120 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1121 ds_put_format(ds
, "push_eth(src="ETH_ADDR_FMT
",dst="ETH_ADDR_FMT
")",
1122 ETH_ADDR_ARGS(eth
->addresses
.eth_src
),
1123 ETH_ADDR_ARGS(eth
->addresses
.eth_dst
));
1126 case OVS_ACTION_ATTR_POP_ETH
:
1127 ds_put_cstr(ds
, "pop_eth");
1129 case OVS_ACTION_ATTR_PUSH_VLAN
: {
1130 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
1131 ds_put_cstr(ds
, "push_vlan(");
1132 if (vlan
->vlan_tpid
!= htons(ETH_TYPE_VLAN
)) {
1133 ds_put_format(ds
, "tpid=0x%04"PRIx16
",", ntohs(vlan
->vlan_tpid
));
1135 format_vlan_tci(ds
, vlan
->vlan_tci
, OVS_BE16_MAX
, false);
1136 ds_put_char(ds
, ')');
1139 case OVS_ACTION_ATTR_POP_VLAN
:
1140 ds_put_cstr(ds
, "pop_vlan");
1142 case OVS_ACTION_ATTR_PUSH_MPLS
: {
1143 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
1144 ds_put_cstr(ds
, "push_mpls(");
1145 format_mpls_lse(ds
, mpls
->mpls_lse
);
1146 ds_put_format(ds
, ",eth_type=0x%"PRIx16
")", ntohs(mpls
->mpls_ethertype
));
1149 case OVS_ACTION_ATTR_POP_MPLS
: {
1150 ovs_be16 ethertype
= nl_attr_get_be16(a
);
1151 ds_put_format(ds
, "pop_mpls(eth_type=0x%"PRIx16
")", ntohs(ethertype
));
1154 case OVS_ACTION_ATTR_SAMPLE
:
1155 format_odp_sample_action(ds
, a
, portno_names
);
1157 case OVS_ACTION_ATTR_CT
:
1158 format_odp_conntrack_action(ds
, a
);
1160 case OVS_ACTION_ATTR_CT_CLEAR
:
1161 ds_put_cstr(ds
, "ct_clear");
1163 case OVS_ACTION_ATTR_CLONE
:
1164 format_odp_clone_action(ds
, a
, portno_names
);
1166 case OVS_ACTION_ATTR_PUSH_NSH
: {
1167 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1168 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1169 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1170 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1171 format_odp_push_nsh_action(ds
, nsh_hdr
);
1174 case OVS_ACTION_ATTR_POP_NSH
:
1175 ds_put_cstr(ds
, "pop_nsh()");
1177 case OVS_ACTION_ATTR_UNSPEC
:
1178 case __OVS_ACTION_ATTR_MAX
:
1180 format_generic_odp_action(ds
, a
);
1186 format_odp_actions(struct ds
*ds
, const struct nlattr
*actions
,
1187 size_t actions_len
, const struct hmap
*portno_names
)
1190 const struct nlattr
*a
;
1193 NL_ATTR_FOR_EACH (a
, left
, actions
, actions_len
) {
1195 ds_put_char(ds
, ',');
1197 format_odp_action(ds
, a
, portno_names
);
1202 if (left
== actions_len
) {
1203 ds_put_cstr(ds
, "<empty>");
1205 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
1206 for (i
= 0; i
< left
; i
++) {
1207 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
1209 ds_put_char(ds
, ')');
1212 ds_put_cstr(ds
, "drop");
1216 /* Separate out parse_odp_userspace_action() function. */
1218 parse_odp_userspace_action(const char *s
, struct ofpbuf
*actions
)
1221 struct user_action_cookie cookie
;
1223 odp_port_t tunnel_out_port
;
1225 void *user_data
= NULL
;
1226 size_t user_data_size
= 0;
1227 bool include_actions
= false;
1230 if (!ovs_scan(s
, "userspace(pid=%"SCNi32
"%n", &pid
, &n
)) {
1234 ofpbuf_init(&buf
, 16);
1235 memset(&cookie
, 0, sizeof cookie
);
1237 user_data
= &cookie
;
1238 user_data_size
= sizeof cookie
;
1241 uint32_t probability
;
1242 uint32_t collector_set_id
;
1243 uint32_t obs_domain_id
;
1244 uint32_t obs_point_id
;
1246 /* USER_ACTION_COOKIE_CONTROLLER. */
1248 uint8_t continuation
;
1251 uint64_t rule_cookie
;
1252 uint16_t controller_id
;
1257 if (ovs_scan(&s
[n
], ",sFlow(vid=%i,"
1258 "pcp=%i,output=%"SCNi32
")%n",
1259 &vid
, &pcp
, &output
, &n1
)) {
1263 tci
= vid
| (pcp
<< VLAN_PCP_SHIFT
);
1268 cookie
.type
= USER_ACTION_COOKIE_SFLOW
;
1269 cookie
.ofp_in_port
= OFPP_NONE
;
1270 cookie
.ofproto_uuid
= UUID_ZERO
;
1271 cookie
.sflow
.vlan_tci
= htons(tci
);
1272 cookie
.sflow
.output
= output
;
1273 } else if (ovs_scan(&s
[n
], ",slow_path(%n",
1276 cookie
.type
= USER_ACTION_COOKIE_SLOW_PATH
;
1277 cookie
.ofp_in_port
= OFPP_NONE
;
1278 cookie
.ofproto_uuid
= UUID_ZERO
;
1279 cookie
.slow_path
.reason
= 0;
1281 res
= parse_odp_flags(&s
[n
], slow_path_reason_to_string
,
1282 &cookie
.slow_path
.reason
,
1283 SLOW_PATH_REASON_MASK
, NULL
);
1284 if (res
< 0 || s
[n
+ res
] != ')') {
1288 } else if (ovs_scan(&s
[n
], ",flow_sample(probability=%"SCNi32
","
1289 "collector_set_id=%"SCNi32
","
1290 "obs_domain_id=%"SCNi32
","
1291 "obs_point_id=%"SCNi32
","
1292 "output_port=%"SCNi32
"%n",
1293 &probability
, &collector_set_id
,
1294 &obs_domain_id
, &obs_point_id
,
1298 cookie
.type
= USER_ACTION_COOKIE_FLOW_SAMPLE
;
1299 cookie
.ofp_in_port
= OFPP_NONE
;
1300 cookie
.ofproto_uuid
= UUID_ZERO
;
1301 cookie
.flow_sample
.probability
= probability
;
1302 cookie
.flow_sample
.collector_set_id
= collector_set_id
;
1303 cookie
.flow_sample
.obs_domain_id
= obs_domain_id
;
1304 cookie
.flow_sample
.obs_point_id
= obs_point_id
;
1305 cookie
.flow_sample
.output_odp_port
= u32_to_odp(output
);
1307 if (ovs_scan(&s
[n
], ",ingress%n", &n1
)) {
1308 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_INGRESS
;
1310 } else if (ovs_scan(&s
[n
], ",egress%n", &n1
)) {
1311 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_EGRESS
;
1314 cookie
.flow_sample
.direction
= NX_ACTION_SAMPLE_DEFAULT
;
1321 } else if (ovs_scan(&s
[n
], ",ipfix(output_port=%"SCNi32
")%n",
1324 cookie
.type
= USER_ACTION_COOKIE_IPFIX
;
1325 cookie
.ofp_in_port
= OFPP_NONE
;
1326 cookie
.ofproto_uuid
= UUID_ZERO
;
1327 cookie
.ipfix
.output_odp_port
= u32_to_odp(output
);
1328 } else if (ovs_scan(&s
[n
], ",controller(reason=%"SCNu16
1330 ",continuation=%"SCNu8
1331 ",recirc_id=%"SCNu32
1332 ",rule_cookie=%"SCNx64
1333 ",controller_id=%"SCNu16
1334 ",max_len=%"SCNu16
")%n",
1335 &reason
, &dont_send
, &continuation
, &recirc_id
,
1336 &rule_cookie
, &controller_id
, &max_len
, &n1
)) {
1338 cookie
.type
= USER_ACTION_COOKIE_CONTROLLER
;
1339 cookie
.ofp_in_port
= OFPP_NONE
;
1340 cookie
.ofproto_uuid
= UUID_ZERO
;
1341 cookie
.controller
.dont_send
= dont_send
? true : false;
1342 cookie
.controller
.continuation
= continuation
? true : false;
1343 cookie
.controller
.reason
= reason
;
1344 cookie
.controller
.recirc_id
= recirc_id
;
1345 put_32aligned_be64(&cookie
.controller
.rule_cookie
,
1346 htonll(rule_cookie
));
1347 cookie
.controller
.controller_id
= controller_id
;
1348 cookie
.controller
.max_len
= max_len
;
1349 } else if (ovs_scan(&s
[n
], ",userdata(%n", &n1
)) {
1353 end
= ofpbuf_put_hex(&buf
, &s
[n
], NULL
);
1354 if (end
[0] != ')') {
1358 user_data
= buf
.data
;
1359 user_data_size
= buf
.size
;
1366 if (ovs_scan(&s
[n
], ",actions%n", &n1
)) {
1368 include_actions
= true;
1374 if (ovs_scan(&s
[n
], ",tunnel_out_port=%"SCNi32
")%n",
1375 &tunnel_out_port
, &n1
)) {
1376 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1377 tunnel_out_port
, include_actions
, actions
);
1380 } else if (s
[n
] == ')') {
1381 odp_put_userspace_action(pid
, user_data
, user_data_size
,
1382 ODPP_NONE
, include_actions
, actions
);
1389 struct ovs_action_push_eth push
;
1393 if (ovs_scan(&s
[n
], "push_eth(src="ETH_ADDR_SCAN_FMT
","
1394 "dst="ETH_ADDR_SCAN_FMT
",type=%i)%n",
1395 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_src
),
1396 ETH_ADDR_SCAN_ARGS(push
.addresses
.eth_dst
),
1399 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_ETH
,
1400 &push
, sizeof push
);
1407 if (!strncmp(&s
[n
], "pop_eth", 7)) {
1408 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_ETH
);
1415 ofpbuf_uninit(&buf
);
1420 ovs_parse_tnl_push(const char *s
, struct ovs_action_push_tnl
*data
)
1422 struct eth_header
*eth
;
1423 struct ip_header
*ip
;
1424 struct ovs_16aligned_ip6_hdr
*ip6
;
1425 struct udp_header
*udp
;
1426 struct gre_base_hdr
*greh
;
1427 struct erspan_base_hdr
*ersh
;
1428 struct erspan_md2
*md2
;
1429 uint16_t gre_proto
, gre_flags
, dl_type
, udp_src
, udp_dst
, csum
, sid
;
1431 uint32_t tnl_type
= 0, header_len
= 0, ip_len
= 0, erspan_idx
= 0;
1436 if (!ovs_scan_len(s
, &n
, "tnl_push(tnl_port(%"SCNi32
"),", &data
->tnl_port
)) {
1439 eth
= (struct eth_header
*) data
->header
;
1440 l3
= (struct ip_header
*) (eth
+ 1);
1441 ip
= (struct ip_header
*) l3
;
1442 ip6
= (struct ovs_16aligned_ip6_hdr
*) l3
;
1443 if (!ovs_scan_len(s
, &n
, "header(size=%"SCNi32
",type=%"SCNi32
","
1444 "eth(dst="ETH_ADDR_SCAN_FMT
",",
1447 ETH_ADDR_SCAN_ARGS(eth
->eth_dst
))) {
1451 if (!ovs_scan_len(s
, &n
, "src="ETH_ADDR_SCAN_FMT
",",
1452 ETH_ADDR_SCAN_ARGS(eth
->eth_src
))) {
1455 if (!ovs_scan_len(s
, &n
, "dl_type=0x%"SCNx16
"),", &dl_type
)) {
1458 eth
->eth_type
= htons(dl_type
);
1460 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1462 uint16_t ip_frag_off
;
1463 if (!ovs_scan_len(s
, &n
, "ipv4(src="IP_SCAN_FMT
",dst="IP_SCAN_FMT
",proto=%"SCNi8
1464 ",tos=%"SCNi8
",ttl=%"SCNi8
",frag=0x%"SCNx16
"),",
1467 &ip
->ip_proto
, &ip
->ip_tos
,
1468 &ip
->ip_ttl
, &ip_frag_off
)) {
1471 put_16aligned_be32(&ip
->ip_src
, sip
);
1472 put_16aligned_be32(&ip
->ip_dst
, dip
);
1473 ip
->ip_frag_off
= htons(ip_frag_off
);
1474 ip_len
= sizeof *ip
;
1476 char sip6_s
[IPV6_SCAN_LEN
+ 1];
1477 char dip6_s
[IPV6_SCAN_LEN
+ 1];
1478 struct in6_addr sip6
, dip6
;
1481 if (!ovs_scan_len(s
, &n
, "ipv6(src="IPV6_SCAN_FMT
",dst="IPV6_SCAN_FMT
1482 ",label=%i,proto=%"SCNi8
",tclass=0x%"SCNx8
1483 ",hlimit=%"SCNi8
"),",
1484 sip6_s
, dip6_s
, &label
, &ip6
->ip6_nxt
,
1485 &tclass
, &ip6
->ip6_hlim
)
1486 || (label
& ~IPV6_LABEL_MASK
) != 0
1487 || inet_pton(AF_INET6
, sip6_s
, &sip6
) != 1
1488 || inet_pton(AF_INET6
, dip6_s
, &dip6
) != 1) {
1491 put_16aligned_be32(&ip6
->ip6_flow
, htonl(6 << 28) |
1492 htonl(tclass
<< 20) | htonl(label
));
1493 memcpy(&ip6
->ip6_src
, &sip6
, sizeof(ip6
->ip6_src
));
1494 memcpy(&ip6
->ip6_dst
, &dip6
, sizeof(ip6
->ip6_dst
));
1495 ip_len
= sizeof *ip6
;
1499 l4
= ((uint8_t *) l3
+ ip_len
);
1500 udp
= (struct udp_header
*) l4
;
1501 greh
= (struct gre_base_hdr
*) l4
;
1502 if (ovs_scan_len(s
, &n
, "udp(src=%"SCNi16
",dst=%"SCNi16
",csum=0x%"SCNx16
"),",
1503 &udp_src
, &udp_dst
, &csum
)) {
1504 uint32_t vx_flags
, vni
;
1506 udp
->udp_src
= htons(udp_src
);
1507 udp
->udp_dst
= htons(udp_dst
);
1509 udp
->udp_csum
= htons(csum
);
1511 if (ovs_scan_len(s
, &n
, "vxlan(flags=0x%"SCNx32
",vni=0x%"SCNx32
"))",
1513 struct vxlanhdr
*vxh
= (struct vxlanhdr
*) (udp
+ 1);
1515 put_16aligned_be32(&vxh
->vx_flags
, htonl(vx_flags
));
1516 put_16aligned_be32(&vxh
->vx_vni
, htonl(vni
<< 8));
1517 tnl_type
= OVS_VPORT_TYPE_VXLAN
;
1518 header_len
= sizeof *eth
+ ip_len
+
1519 sizeof *udp
+ sizeof *vxh
;
1520 } else if (ovs_scan_len(s
, &n
, "geneve(")) {
1521 struct genevehdr
*gnh
= (struct genevehdr
*) (udp
+ 1);
1523 memset(gnh
, 0, sizeof *gnh
);
1524 header_len
= sizeof *eth
+ ip_len
+
1525 sizeof *udp
+ sizeof *gnh
;
1527 if (ovs_scan_len(s
, &n
, "oam,")) {
1530 if (ovs_scan_len(s
, &n
, "crit,")) {
1533 if (!ovs_scan_len(s
, &n
, "vni=%"SCNi32
, &vni
)) {
1536 if (ovs_scan_len(s
, &n
, ",options(")) {
1537 struct geneve_scan options
;
1540 memset(&options
, 0, sizeof options
);
1541 len
= scan_geneve(s
+ n
, &options
, NULL
);
1546 memcpy(gnh
->options
, options
.d
, options
.len
);
1547 gnh
->opt_len
= options
.len
/ 4;
1548 header_len
+= options
.len
;
1552 if (!ovs_scan_len(s
, &n
, "))")) {
1556 gnh
->proto_type
= htons(ETH_TYPE_TEB
);
1557 put_16aligned_be32(&gnh
->vni
, htonl(vni
<< 8));
1558 tnl_type
= OVS_VPORT_TYPE_GENEVE
;
1562 } else if (ovs_scan_len(s
, &n
, "gre((flags=0x%"SCNx16
",proto=0x%"SCNx16
")",
1563 &gre_flags
, &gre_proto
)){
1565 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1566 tnl_type
= OVS_VPORT_TYPE_GRE
;
1568 tnl_type
= OVS_VPORT_TYPE_IP6GRE
;
1570 greh
->flags
= htons(gre_flags
);
1571 greh
->protocol
= htons(gre_proto
);
1572 ovs_16aligned_be32
*options
= (ovs_16aligned_be32
*) (greh
+ 1);
1574 if (greh
->flags
& htons(GRE_CSUM
)) {
1575 if (!ovs_scan_len(s
, &n
, ",csum=0x%"SCNx16
, &csum
)) {
1579 memset(options
, 0, sizeof *options
);
1580 *((ovs_be16
*)options
) = htons(csum
);
1583 if (greh
->flags
& htons(GRE_KEY
)) {
1586 if (!ovs_scan_len(s
, &n
, ",key=0x%"SCNx32
, &key
)) {
1590 put_16aligned_be32(options
, htonl(key
));
1593 if (greh
->flags
& htons(GRE_SEQ
)) {
1596 if (!ovs_scan_len(s
, &n
, ",seq=0x%"SCNx32
, &seq
)) {
1599 put_16aligned_be32(options
, htonl(seq
));
1603 if (!ovs_scan_len(s
, &n
, "))")) {
1607 header_len
= sizeof *eth
+ ip_len
+
1608 ((uint8_t *) options
- (uint8_t *) greh
);
1609 } else if (ovs_scan_len(s
, &n
, "erspan(ver=1,sid="SCNx16
",idx=0x"SCNx32
")",
1610 &sid
, &erspan_idx
)) {
1611 ersh
= ERSPAN_HDR(greh
);
1612 ovs_16aligned_be32
*index
= ALIGNED_CAST(ovs_16aligned_be32
*,
1615 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1616 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1618 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1621 greh
->flags
= htons(GRE_SEQ
);
1622 greh
->protocol
= htons(ETH_TYPE_ERSPAN1
);
1626 put_16aligned_be32(index
, htonl(erspan_idx
));
1628 if (!ovs_scan_len(s
, &n
, ")")) {
1631 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1632 sizeof *ersh
+ ERSPAN_V1_MDSIZE
;
1634 } else if (ovs_scan_len(s
, &n
, "erspan(ver=2,sid="SCNx16
"dir="SCNu8
1635 ",hwid=0x"SCNx8
")", &sid
, &dir
, &hwid
)) {
1637 ersh
= ERSPAN_HDR(greh
);
1638 md2
= ALIGNED_CAST(struct erspan_md2
*, ersh
+ 1);
1640 if (eth
->eth_type
== htons(ETH_TYPE_IP
)) {
1641 tnl_type
= OVS_VPORT_TYPE_ERSPAN
;
1643 tnl_type
= OVS_VPORT_TYPE_IP6ERSPAN
;
1646 greh
->flags
= htons(GRE_SEQ
);
1647 greh
->protocol
= htons(ETH_TYPE_ERSPAN2
);
1651 set_hwid(md2
, hwid
);
1654 if (!ovs_scan_len(s
, &n
, ")")) {
1658 header_len
= sizeof *eth
+ ip_len
+ ERSPAN_GREHDR_LEN
+
1659 sizeof *ersh
+ ERSPAN_V2_MDSIZE
;
1664 /* check tunnel meta data. */
1665 if (data
->tnl_type
!= tnl_type
) {
1668 if (data
->header_len
!= header_len
) {
1673 if (!ovs_scan_len(s
, &n
, ",out_port(%"SCNi32
"))", &data
->out_port
)) {
1680 struct ct_nat_params
{
1686 struct in6_addr ip6
;
1690 struct in6_addr ip6
;
1700 scan_ct_nat_range(const char *s
, int *n
, struct ct_nat_params
*p
)
1702 if (ovs_scan_len(s
, n
, "=")) {
1703 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
1704 struct in6_addr ipv6
;
1706 if (ovs_scan_len(s
, n
, IP_SCAN_FMT
, IP_SCAN_ARGS(&p
->addr_min
.ip
))) {
1707 p
->addr_len
= sizeof p
->addr_min
.ip
;
1708 if (ovs_scan_len(s
, n
, "-")) {
1709 if (!ovs_scan_len(s
, n
, IP_SCAN_FMT
,
1710 IP_SCAN_ARGS(&p
->addr_max
.ip
))) {
1714 } else if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1715 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1716 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1717 p
->addr_len
= sizeof p
->addr_min
.ip6
;
1718 p
->addr_min
.ip6
= ipv6
;
1719 if (ovs_scan_len(s
, n
, "-")) {
1720 if ((ovs_scan_len(s
, n
, IPV6_SCAN_FMT
, ipv6_s
)
1721 || ovs_scan_len(s
, n
, "["IPV6_SCAN_FMT
"]", ipv6_s
))
1722 && inet_pton(AF_INET6
, ipv6_s
, &ipv6
) == 1) {
1723 p
->addr_max
.ip6
= ipv6
;
1731 if (ovs_scan_len(s
, n
, ":%"SCNu16
, &p
->proto_min
)) {
1732 if (ovs_scan_len(s
, n
, "-")) {
1733 if (!ovs_scan_len(s
, n
, "%"SCNu16
, &p
->proto_max
)) {
1743 scan_ct_nat(const char *s
, struct ct_nat_params
*p
)
1747 if (ovs_scan_len(s
, &n
, "nat")) {
1748 memset(p
, 0, sizeof *p
);
1750 if (ovs_scan_len(s
, &n
, "(")) {
1754 end
= strchr(s
+ n
, ')');
1761 n
+= strspn(s
+ n
, delimiters
);
1762 if (ovs_scan_len(s
, &n
, "src")) {
1763 int err
= scan_ct_nat_range(s
, &n
, p
);
1770 if (ovs_scan_len(s
, &n
, "dst")) {
1771 int err
= scan_ct_nat_range(s
, &n
, p
);
1778 if (ovs_scan_len(s
, &n
, "persistent")) {
1779 p
->persistent
= true;
1782 if (ovs_scan_len(s
, &n
, "hash")) {
1783 p
->proto_hash
= true;
1786 if (ovs_scan_len(s
, &n
, "random")) {
1787 p
->proto_random
= true;
1793 if (p
->snat
&& p
->dnat
) {
1796 if ((p
->addr_len
!= 0 &&
1797 memcmp(&p
->addr_max
, &in6addr_any
, p
->addr_len
) &&
1798 memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) < 0) ||
1799 (p
->proto_max
&& p
->proto_max
< p
->proto_min
)) {
1802 if (p
->proto_hash
&& p
->proto_random
) {
1812 nl_msg_put_ct_nat(struct ct_nat_params
*p
, struct ofpbuf
*actions
)
1814 size_t start
= nl_msg_start_nested(actions
, OVS_CT_ATTR_NAT
);
1817 nl_msg_put_flag(actions
, OVS_NAT_ATTR_SRC
);
1818 } else if (p
->dnat
) {
1819 nl_msg_put_flag(actions
, OVS_NAT_ATTR_DST
);
1823 if (p
->addr_len
!= 0) {
1824 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MIN
, &p
->addr_min
,
1826 if (memcmp(&p
->addr_max
, &p
->addr_min
, p
->addr_len
) > 0) {
1827 nl_msg_put_unspec(actions
, OVS_NAT_ATTR_IP_MAX
, &p
->addr_max
,
1831 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MIN
, p
->proto_min
);
1832 if (p
->proto_max
&& p
->proto_max
> p
->proto_min
) {
1833 nl_msg_put_u16(actions
, OVS_NAT_ATTR_PROTO_MAX
, p
->proto_max
);
1836 if (p
->persistent
) {
1837 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PERSISTENT
);
1839 if (p
->proto_hash
) {
1840 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_HASH
);
1842 if (p
->proto_random
) {
1843 nl_msg_put_flag(actions
, OVS_NAT_ATTR_PROTO_RANDOM
);
1847 nl_msg_end_nested(actions
, start
);
1851 parse_conntrack_action(const char *s_
, struct ofpbuf
*actions
)
1855 if (ovs_scan(s
, "ct")) {
1856 const char *helper
= NULL
;
1857 size_t helper_len
= 0;
1858 bool commit
= false;
1859 bool force_commit
= false;
1864 } ct_mark
= { 0, 0 };
1869 struct ct_nat_params nat_params
;
1870 bool have_nat
= false;
1874 memset(&ct_label
, 0, sizeof(ct_label
));
1877 if (ovs_scan(s
, "(")) {
1880 end
= strchr(s
, ')');
1888 s
+= strspn(s
, delimiters
);
1889 if (ovs_scan(s
, "commit%n", &n
)) {
1894 if (ovs_scan(s
, "force_commit%n", &n
)) {
1895 force_commit
= true;
1899 if (ovs_scan(s
, "zone=%"SCNu16
"%n", &zone
, &n
)) {
1903 if (ovs_scan(s
, "mark=%"SCNx32
"%n", &ct_mark
.value
, &n
)) {
1906 if (ovs_scan(s
, "/%"SCNx32
"%n", &ct_mark
.mask
, &n
)) {
1909 ct_mark
.mask
= UINT32_MAX
;
1913 if (ovs_scan(s
, "label=%n", &n
)) {
1917 retval
= scan_u128(s
, &ct_label
.value
, &ct_label
.mask
);
1924 if (ovs_scan(s
, "helper=%n", &n
)) {
1926 helper_len
= strcspn(s
, delimiters_end
);
1927 if (!helper_len
|| helper_len
> 15) {
1935 n
= scan_ct_nat(s
, &nat_params
);
1940 /* end points to the end of the nested, nat action.
1941 * find the real end. */
1944 /* Nothing matched. */
1949 if (commit
&& force_commit
) {
1953 start
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CT
);
1955 nl_msg_put_flag(actions
, OVS_CT_ATTR_COMMIT
);
1956 } else if (force_commit
) {
1957 nl_msg_put_flag(actions
, OVS_CT_ATTR_FORCE_COMMIT
);
1960 nl_msg_put_u16(actions
, OVS_CT_ATTR_ZONE
, zone
);
1963 nl_msg_put_unspec(actions
, OVS_CT_ATTR_MARK
, &ct_mark
,
1966 if (!ovs_u128_is_zero(ct_label
.mask
)) {
1967 nl_msg_put_unspec(actions
, OVS_CT_ATTR_LABELS
, &ct_label
,
1971 nl_msg_put_string__(actions
, OVS_CT_ATTR_HELPER
, helper
,
1975 nl_msg_put_ct_nat(&nat_params
, actions
);
1977 nl_msg_end_nested(actions
, start
);
1984 nsh_key_to_attr(struct ofpbuf
*buf
, const struct ovs_key_nsh
*nsh
,
1985 uint8_t * metadata
, size_t md_size
,
1989 struct ovs_nsh_key_base base
;
1991 base
.flags
= nsh
->flags
;
1992 base
.ttl
= nsh
->ttl
;
1993 base
.mdtype
= nsh
->mdtype
;
1995 base
.path_hdr
= nsh
->path_hdr
;
1997 nsh_key_ofs
= nl_msg_start_nested(buf
, OVS_KEY_ATTR_NSH
);
1998 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_BASE
, &base
, sizeof base
);
2001 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2002 sizeof nsh
->context
);
2004 switch (nsh
->mdtype
) {
2006 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD1
, nsh
->context
,
2007 sizeof nsh
->context
);
2010 if (metadata
&& md_size
> 0) {
2011 nl_msg_put_unspec(buf
, OVS_NSH_KEY_ATTR_MD2
, metadata
,
2016 /* No match support for other MD formats yet. */
2020 nl_msg_end_nested(buf
, nsh_key_ofs
);
2025 parse_odp_push_nsh_action(const char *s
, struct ofpbuf
*actions
)
2032 struct ovs_key_nsh nsh
;
2033 uint8_t metadata
[NSH_CTX_HDRS_MAX_LEN
];
2034 uint8_t md_size
= 0;
2036 if (!ovs_scan_len(s
, &n
, "push_nsh(")) {
2041 /* The default is NSH_M_TYPE1 */
2044 nsh
.mdtype
= NSH_M_TYPE1
;
2045 nsh
.np
= NSH_P_ETHERNET
;
2046 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(0, 255);
2047 memset(nsh
.context
, 0, NSH_M_TYPE1_MDLEN
);
2050 n
+= strspn(s
+ n
, delimiters
);
2055 if (ovs_scan_len(s
, &n
, "flags=%"SCNi8
, &nsh
.flags
)) {
2058 if (ovs_scan_len(s
, &n
, "ttl=%"SCNi8
, &nsh
.ttl
)) {
2061 if (ovs_scan_len(s
, &n
, "mdtype=%"SCNi8
, &nsh
.mdtype
)) {
2062 switch (nsh
.mdtype
) {
2064 /* This is the default format. */;
2067 /* Length will be updated later. */
2076 if (ovs_scan_len(s
, &n
, "np=%"SCNi8
, &nsh
.np
)) {
2079 if (ovs_scan_len(s
, &n
, "spi=0x%"SCNx32
, &spi
)) {
2082 if (ovs_scan_len(s
, &n
, "si=%"SCNi8
, &si
)) {
2085 if (nsh
.mdtype
== NSH_M_TYPE1
) {
2086 if (ovs_scan_len(s
, &n
, "c1=0x%"SCNx32
, &cd
)) {
2087 nsh
.context
[0] = htonl(cd
);
2090 if (ovs_scan_len(s
, &n
, "c2=0x%"SCNx32
, &cd
)) {
2091 nsh
.context
[1] = htonl(cd
);
2094 if (ovs_scan_len(s
, &n
, "c3=0x%"SCNx32
, &cd
)) {
2095 nsh
.context
[2] = htonl(cd
);
2098 if (ovs_scan_len(s
, &n
, "c4=0x%"SCNx32
, &cd
)) {
2099 nsh
.context
[3] = htonl(cd
);
2103 else if (nsh
.mdtype
== NSH_M_TYPE2
) {
2106 size_t mdlen
, padding
;
2107 if (ovs_scan_len(s
, &n
, "md2=0x%511[0-9a-fA-F]", buf
)) {
2108 ofpbuf_use_stub(&b
, metadata
,
2109 NSH_CTX_HDRS_MAX_LEN
);
2110 ofpbuf_put_hex(&b
, buf
, &mdlen
);
2111 /* Pad metadata to 4 bytes. */
2112 padding
= PAD_SIZE(mdlen
, 4);
2114 ofpbuf_push_zeros(&b
, padding
);
2116 md_size
= mdlen
+ padding
;
2127 nsh
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
2128 size_t offset
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_PUSH_NSH
);
2129 nsh_key_to_attr(actions
, &nsh
, metadata
, md_size
, false);
2130 nl_msg_end_nested(actions
, offset
);
2137 parse_action_list(const char *s
, const struct simap
*port_names
,
2138 struct ofpbuf
*actions
)
2145 n
+= strspn(s
+ n
, delimiters
);
2149 retval
= parse_odp_action(s
+ n
, port_names
, actions
);
2160 parse_odp_action(const char *s
, const struct simap
*port_names
,
2161 struct ofpbuf
*actions
)
2167 if (ovs_scan(s
, "%"SCNi32
"%n", &port
, &n
)) {
2168 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, port
);
2177 if (ovs_scan(s
, "trunc(%"SCNi32
")%n", &max_len
, &n
)) {
2178 struct ovs_action_trunc
*trunc
;
2180 trunc
= nl_msg_put_unspec_uninit(actions
,
2181 OVS_ACTION_ATTR_TRUNC
, sizeof *trunc
);
2182 trunc
->max_len
= max_len
;
2188 int len
= strcspn(s
, delimiters
);
2189 struct simap_node
*node
;
2191 node
= simap_find_len(port_names
, s
, len
);
2193 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_OUTPUT
, node
->data
);
2202 if (ovs_scan(s
, "recirc(%"PRIu32
")%n", &recirc_id
, &n
)) {
2203 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_RECIRC
, recirc_id
);
2208 if (!strncmp(s
, "userspace(", 10)) {
2209 return parse_odp_userspace_action(s
, actions
);
2212 if (!strncmp(s
, "set(", 4)) {
2215 struct nlattr mask
[1024 / sizeof(struct nlattr
)];
2216 struct ofpbuf maskbuf
= OFPBUF_STUB_INITIALIZER(mask
);
2217 struct nlattr
*nested
, *key
;
2220 start_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SET
);
2221 retval
= parse_odp_key_mask_attr(s
+ 4, port_names
, actions
, &maskbuf
);
2223 ofpbuf_uninit(&maskbuf
);
2226 if (s
[retval
+ 4] != ')') {
2227 ofpbuf_uninit(&maskbuf
);
2231 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2234 size
= nl_attr_get_size(mask
);
2235 if (size
== nl_attr_get_size(key
)) {
2236 /* Change to masked set action if not fully masked. */
2237 if (!is_all_ones(mask
+ 1, size
)) {
2238 /* Remove padding of eariler key payload */
2239 actions
->size
-= NLA_ALIGN(key
->nla_len
) - key
->nla_len
;
2241 /* Put mask payload right after key payload */
2242 key
->nla_len
+= size
;
2243 ofpbuf_put(actions
, mask
+ 1, size
);
2245 /* Add new padding as needed */
2246 ofpbuf_put_zeros(actions
, NLA_ALIGN(key
->nla_len
) -
2249 /* 'actions' may have been reallocated by ofpbuf_put(). */
2250 nested
= ofpbuf_at_assert(actions
, start_ofs
, sizeof *nested
);
2251 nested
->nla_type
= OVS_ACTION_ATTR_SET_MASKED
;
2254 ofpbuf_uninit(&maskbuf
);
2256 nl_msg_end_nested(actions
, start_ofs
);
2261 struct ovs_action_push_vlan push
;
2262 int tpid
= ETH_TYPE_VLAN
;
2267 if (ovs_scan(s
, "push_vlan(vid=%i,pcp=%i)%n", &vid
, &pcp
, &n
)
2268 || ovs_scan(s
, "push_vlan(vid=%i,pcp=%i,cfi=%i)%n",
2269 &vid
, &pcp
, &cfi
, &n
)
2270 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i)%n",
2271 &tpid
, &vid
, &pcp
, &n
)
2272 || ovs_scan(s
, "push_vlan(tpid=%i,vid=%i,pcp=%i,cfi=%i)%n",
2273 &tpid
, &vid
, &pcp
, &cfi
, &n
)) {
2274 push
.vlan_tpid
= htons(tpid
);
2275 push
.vlan_tci
= htons((vid
<< VLAN_VID_SHIFT
)
2276 | (pcp
<< VLAN_PCP_SHIFT
)
2277 | (cfi
? VLAN_CFI
: 0));
2278 nl_msg_put_unspec(actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
2279 &push
, sizeof push
);
2285 if (!strncmp(s
, "pop_vlan", 8)) {
2286 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_VLAN
);
2291 unsigned long long int meter_id
;
2294 if (sscanf(s
, "meter(%lli)%n", &meter_id
, &n
) > 0 && n
> 0) {
2295 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_METER
, meter_id
);
2304 if (ovs_scan(s
, "sample(sample=%lf%%,actions(%n", &percentage
, &n
)
2305 && percentage
>= 0. && percentage
<= 100.0) {
2306 size_t sample_ofs
, actions_ofs
;
2309 probability
= floor(UINT32_MAX
* (percentage
/ 100.0) + .5);
2310 sample_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_SAMPLE
);
2311 nl_msg_put_u32(actions
, OVS_SAMPLE_ATTR_PROBABILITY
,
2312 (probability
<= 0 ? 0
2313 : probability
>= UINT32_MAX
? UINT32_MAX
2316 actions_ofs
= nl_msg_start_nested(actions
,
2317 OVS_SAMPLE_ATTR_ACTIONS
);
2318 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2323 nl_msg_end_nested(actions
, actions_ofs
);
2324 nl_msg_end_nested(actions
, sample_ofs
);
2326 return s
[n
+ 1] == ')' ? n
+ 2 : -EINVAL
;
2331 if (!strncmp(s
, "clone(", 6)) {
2335 actions_ofs
= nl_msg_start_nested(actions
, OVS_ACTION_ATTR_CLONE
);
2336 int retval
= parse_action_list(s
+ n
, port_names
, actions
);
2341 nl_msg_end_nested(actions
, actions_ofs
);
2347 if (!strncmp(s
, "push_nsh(", 9)) {
2348 int retval
= parse_odp_push_nsh_action(s
, actions
);
2358 if (ovs_scan(s
, "pop_nsh()%n", &n
)) {
2359 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_POP_NSH
);
2368 if (ovs_scan(s
, "tnl_pop(%"SCNi32
")%n", &port
, &n
)) {
2369 nl_msg_put_u32(actions
, OVS_ACTION_ATTR_TUNNEL_POP
, port
);
2375 if (!strncmp(s
, "ct_clear", 8)) {
2376 nl_msg_put_flag(actions
, OVS_ACTION_ATTR_CT_CLEAR
);
2384 retval
= parse_conntrack_action(s
, actions
);
2391 struct ovs_action_push_tnl data
;
2394 n
= ovs_parse_tnl_push(s
, &data
);
2396 odp_put_tnl_push_action(actions
, &data
);
2405 /* Parses the string representation of datapath actions, in the format output
2406 * by format_odp_action(). Returns 0 if successful, otherwise a positive errno
2407 * value. On success, the ODP actions are appended to 'actions' as a series of
2408 * Netlink attributes. On failure, no data is appended to 'actions'. Either
2409 * way, 'actions''s data might be reallocated. */
2411 odp_actions_from_string(const char *s
, const struct simap
*port_names
,
2412 struct ofpbuf
*actions
)
2416 if (!strcasecmp(s
, "drop")) {
2420 old_size
= actions
->size
;
2424 s
+= strspn(s
, delimiters
);
2429 retval
= parse_odp_action(s
, port_names
, actions
);
2430 if (retval
< 0 || !strchr(delimiters
, s
[retval
])) {
2431 actions
->size
= old_size
;
2440 static const struct attr_len_tbl ovs_vxlan_ext_attr_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
2441 [OVS_VXLAN_EXT_GBP
] = { .len
= 4 },
2444 static const struct attr_len_tbl ovs_tun_key_attr_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
2445 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= 8 },
2446 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= 4 },
2447 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= 4 },
2448 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
2449 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
2450 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
2451 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
2452 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= 2 },
2453 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= 2 },
2454 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
2455 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2456 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= ATTR_LEN_NESTED
,
2457 .next
= ovs_vxlan_ext_attr_lens
,
2458 .next_max
= OVS_VXLAN_EXT_MAX
},
2459 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= 16 },
2460 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= 16 },
2461 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= ATTR_LEN_VARIABLE
},
2464 const struct attr_len_tbl ovs_flow_key_attr_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
2465 [OVS_KEY_ATTR_ENCAP
] = { .len
= ATTR_LEN_NESTED
},
2466 [OVS_KEY_ATTR_PRIORITY
] = { .len
= 4 },
2467 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= 4 },
2468 [OVS_KEY_ATTR_DP_HASH
] = { .len
= 4 },
2469 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= 4 },
2470 [OVS_KEY_ATTR_TUNNEL
] = { .len
= ATTR_LEN_NESTED
,
2471 .next
= ovs_tun_key_attr_lens
,
2472 .next_max
= OVS_TUNNEL_KEY_ATTR_MAX
},
2473 [OVS_KEY_ATTR_IN_PORT
] = { .len
= 4 },
2474 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
2475 [OVS_KEY_ATTR_VLAN
] = { .len
= 2 },
2476 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= 2 },
2477 [OVS_KEY_ATTR_MPLS
] = { .len
= ATTR_LEN_VARIABLE
},
2478 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
2479 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
2480 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
2481 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= 2 },
2482 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
2483 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
2484 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
2485 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
2486 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
2487 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
2488 [OVS_KEY_ATTR_CT_STATE
] = { .len
= 4 },
2489 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= 2 },
2490 [OVS_KEY_ATTR_CT_MARK
] = { .len
= 4 },
2491 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
2492 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
2493 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = { .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
2494 [OVS_KEY_ATTR_PACKET_TYPE
] = { .len
= 4 },
2495 [OVS_KEY_ATTR_NSH
] = { .len
= ATTR_LEN_NESTED
,
2496 .next
= ovs_nsh_key_attr_lens
,
2497 .next_max
= OVS_NSH_KEY_ATTR_MAX
},
2500 /* Returns the correct length of the payload for a flow key attribute of the
2501 * specified 'type', ATTR_LEN_INVALID if 'type' is unknown, ATTR_LEN_VARIABLE
2502 * if the attribute's payload is variable length, or ATTR_LEN_NESTED if the
2503 * payload is a nested type. */
2505 odp_key_attr_len(const struct attr_len_tbl tbl
[], int max_type
, uint16_t type
)
2507 if (type
> max_type
) {
2508 return ATTR_LEN_INVALID
;
2511 return tbl
[type
].len
;
2515 format_generic_odp_key(const struct nlattr
*a
, struct ds
*ds
)
2517 size_t len
= nl_attr_get_size(a
);
2519 const uint8_t *unspec
;
2522 unspec
= nl_attr_get(a
);
2523 for (i
= 0; i
< len
; i
++) {
2525 ds_put_char(ds
, ' ');
2527 ds_put_format(ds
, "%02x", unspec
[i
]);
2533 ovs_frag_type_to_string(enum ovs_frag_type type
)
2536 case OVS_FRAG_TYPE_NONE
:
2538 case OVS_FRAG_TYPE_FIRST
:
2540 case OVS_FRAG_TYPE_LATER
:
2542 case __OVS_FRAG_TYPE_MAX
:
2548 enum odp_key_fitness
2549 odp_nsh_hdr_from_attr(const struct nlattr
*attr
,
2550 struct nsh_hdr
*nsh_hdr
, size_t size
)
2553 const struct nlattr
*a
;
2554 bool unknown
= false;
2558 bool has_md1
= false;
2559 bool has_md2
= false;
2561 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2562 uint16_t type
= nl_attr_type(a
);
2563 size_t len
= nl_attr_get_size(a
);
2564 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2565 OVS_NSH_KEY_ATTR_MAX
, type
);
2567 if (len
!= expected_len
&& expected_len
>= 0) {
2568 return ODP_FIT_ERROR
;
2572 case OVS_NSH_KEY_ATTR_BASE
: {
2573 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2574 nsh_hdr
->next_proto
= base
->np
;
2575 nsh_hdr
->md_type
= base
->mdtype
;
2576 put_16aligned_be32(&nsh_hdr
->path_hdr
, base
->path_hdr
);
2577 flags
= base
->flags
;
2581 case OVS_NSH_KEY_ATTR_MD1
: {
2582 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2583 struct nsh_md1_ctx
*md1_dst
= &nsh_hdr
->md1
;
2585 mdlen
= nl_attr_get_size(a
);
2586 if ((mdlen
+ NSH_BASE_HDR_LEN
!= NSH_M_TYPE1_LEN
) ||
2587 (mdlen
+ NSH_BASE_HDR_LEN
> size
)) {
2588 return ODP_FIT_ERROR
;
2590 memcpy(md1_dst
, md1
, mdlen
);
2593 case OVS_NSH_KEY_ATTR_MD2
: {
2594 struct nsh_md2_tlv
*md2_dst
= &nsh_hdr
->md2
;
2595 const uint8_t *md2
= nl_attr_get(a
);
2597 mdlen
= nl_attr_get_size(a
);
2598 if (mdlen
+ NSH_BASE_HDR_LEN
> size
) {
2599 return ODP_FIT_ERROR
;
2601 memcpy(md2_dst
, md2
, mdlen
);
2605 /* Allow this to show up as unexpected, if there are unknown
2606 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2613 return ODP_FIT_TOO_MUCH
;
2616 if ((has_md1
&& nsh_hdr
->md_type
!= NSH_M_TYPE1
)
2617 || (has_md2
&& nsh_hdr
->md_type
!= NSH_M_TYPE2
)) {
2618 return ODP_FIT_ERROR
;
2621 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
2622 nsh_set_flags_ttl_len(nsh_hdr
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
2624 return ODP_FIT_PERFECT
;
2627 enum odp_key_fitness
2628 odp_nsh_key_from_attr(const struct nlattr
*attr
, struct ovs_key_nsh
*nsh
,
2629 struct ovs_key_nsh
*nsh_mask
)
2632 const struct nlattr
*a
;
2633 bool unknown
= false;
2634 bool has_md1
= false;
2636 NL_NESTED_FOR_EACH (a
, left
, attr
) {
2637 uint16_t type
= nl_attr_type(a
);
2638 size_t len
= nl_attr_get_size(a
);
2639 int expected_len
= odp_key_attr_len(ovs_nsh_key_attr_lens
,
2640 OVS_NSH_KEY_ATTR_MAX
, type
);
2642 /* the attribute can have mask, len is 2 * expected_len for that case.
2644 if ((len
!= expected_len
) && (len
!= 2 * expected_len
) &&
2645 (expected_len
>= 0)) {
2646 return ODP_FIT_ERROR
;
2649 if ((nsh_mask
&& (expected_len
>= 0) && (len
!= 2 * expected_len
)) ||
2650 (!nsh_mask
&& (expected_len
>= 0) && (len
== 2 * expected_len
))) {
2651 return ODP_FIT_ERROR
;
2655 case OVS_NSH_KEY_ATTR_UNSPEC
:
2657 case OVS_NSH_KEY_ATTR_BASE
: {
2658 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
2659 nsh
->flags
= base
->flags
;
2660 nsh
->ttl
= base
->ttl
;
2661 nsh
->mdtype
= base
->mdtype
;
2663 nsh
->path_hdr
= base
->path_hdr
;
2664 if (nsh_mask
&& (len
== 2 * sizeof(*base
))) {
2665 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
2666 nsh_mask
->flags
= base_mask
->flags
;
2667 nsh_mask
->ttl
= base_mask
->ttl
;
2668 nsh_mask
->mdtype
= base_mask
->mdtype
;
2669 nsh_mask
->np
= base_mask
->np
;
2670 nsh_mask
->path_hdr
= base_mask
->path_hdr
;
2674 case OVS_NSH_KEY_ATTR_MD1
: {
2675 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
2677 memcpy(nsh
->context
, md1
->context
, sizeof md1
->context
);
2678 if (len
== 2 * sizeof(*md1
)) {
2679 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
2680 memcpy(nsh_mask
->context
, md1_mask
->context
,
2685 case OVS_NSH_KEY_ATTR_MD2
:
2687 /* Allow this to show up as unexpected, if there are unknown
2688 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2695 return ODP_FIT_TOO_MUCH
;
2698 if (has_md1
&& nsh
->mdtype
!= NSH_M_TYPE1
) {
2699 return ODP_FIT_ERROR
;
2702 return ODP_FIT_PERFECT
;
2705 static enum odp_key_fitness
2706 odp_tun_key_from_attr__(const struct nlattr
*attr
, bool is_mask
,
2707 struct flow_tnl
*tun
)
2710 const struct nlattr
*a
;
2712 bool unknown
= false;
2714 NL_NESTED_FOR_EACH(a
, left
, attr
) {
2715 uint16_t type
= nl_attr_type(a
);
2716 size_t len
= nl_attr_get_size(a
);
2717 int expected_len
= odp_key_attr_len(ovs_tun_key_attr_lens
,
2718 OVS_TUNNEL_ATTR_MAX
, type
);
2720 if (len
!= expected_len
&& expected_len
>= 0) {
2721 return ODP_FIT_ERROR
;
2725 case OVS_TUNNEL_KEY_ATTR_ID
:
2726 tun
->tun_id
= nl_attr_get_be64(a
);
2727 tun
->flags
|= FLOW_TNL_F_KEY
;
2729 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
2730 tun
->ip_src
= nl_attr_get_be32(a
);
2732 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
2733 tun
->ip_dst
= nl_attr_get_be32(a
);
2735 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
2736 tun
->ipv6_src
= nl_attr_get_in6_addr(a
);
2738 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
2739 tun
->ipv6_dst
= nl_attr_get_in6_addr(a
);
2741 case OVS_TUNNEL_KEY_ATTR_TOS
:
2742 tun
->ip_tos
= nl_attr_get_u8(a
);
2744 case OVS_TUNNEL_KEY_ATTR_TTL
:
2745 tun
->ip_ttl
= nl_attr_get_u8(a
);
2748 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
2749 tun
->flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
2751 case OVS_TUNNEL_KEY_ATTR_CSUM
:
2752 tun
->flags
|= FLOW_TNL_F_CSUM
;
2754 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
2755 tun
->tp_src
= nl_attr_get_be16(a
);
2757 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
2758 tun
->tp_dst
= nl_attr_get_be16(a
);
2760 case OVS_TUNNEL_KEY_ATTR_OAM
:
2761 tun
->flags
|= FLOW_TNL_F_OAM
;
2763 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
: {
2764 static const struct nl_policy vxlan_opts_policy
[] = {
2765 [OVS_VXLAN_EXT_GBP
] = { .type
= NL_A_U32
},
2767 struct nlattr
*ext
[ARRAY_SIZE(vxlan_opts_policy
)];
2769 if (!nl_parse_nested(a
, vxlan_opts_policy
, ext
, ARRAY_SIZE(ext
))) {
2770 return ODP_FIT_ERROR
;
2773 if (ext
[OVS_VXLAN_EXT_GBP
]) {
2774 uint32_t gbp
= nl_attr_get_u32(ext
[OVS_VXLAN_EXT_GBP
]);
2776 tun
->gbp_id
= htons(gbp
& 0xFFFF);
2777 tun
->gbp_flags
= (gbp
>> 16) & 0xFF;
2782 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2783 tun_metadata_from_geneve_nlattr(a
, is_mask
, tun
);
2785 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
: {
2786 const struct erspan_metadata
*opts
= nl_attr_get(a
);
2788 tun
->erspan_ver
= opts
->version
;
2789 if (tun
->erspan_ver
== 1) {
2790 tun
->erspan_idx
= ntohl(opts
->u
.index
);
2791 } else if (tun
->erspan_ver
== 2) {
2792 tun
->erspan_dir
= opts
->u
.md2
.dir
;
2793 tun
->erspan_hwid
= get_hwid(&opts
->u
.md2
);
2795 VLOG_WARN("%s invalid erspan version\n", __func__
);
2801 /* Allow this to show up as unexpected, if there are unknown
2802 * tunnel attribute, eventually resulting in ODP_FIT_TOO_MUCH. */
2809 return ODP_FIT_ERROR
;
2812 return ODP_FIT_TOO_MUCH
;
2814 return ODP_FIT_PERFECT
;
2817 enum odp_key_fitness
2818 odp_tun_key_from_attr(const struct nlattr
*attr
, struct flow_tnl
*tun
)
2820 memset(tun
, 0, sizeof *tun
);
2821 return odp_tun_key_from_attr__(attr
, false, tun
);
2825 tun_key_to_attr(struct ofpbuf
*a
, const struct flow_tnl
*tun_key
,
2826 const struct flow_tnl
*tun_flow_key
,
2827 const struct ofpbuf
*key_buf
, const char *tnl_type
)
2831 tun_key_ofs
= nl_msg_start_nested(a
, OVS_KEY_ATTR_TUNNEL
);
2833 /* tun_id != 0 without FLOW_TNL_F_KEY is valid if tun_key is a mask. */
2834 if (tun_key
->tun_id
|| tun_key
->flags
& FLOW_TNL_F_KEY
) {
2835 nl_msg_put_be64(a
, OVS_TUNNEL_KEY_ATTR_ID
, tun_key
->tun_id
);
2837 if (tun_key
->ip_src
) {
2838 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, tun_key
->ip_src
);
2840 if (tun_key
->ip_dst
) {
2841 nl_msg_put_be32(a
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, tun_key
->ip_dst
);
2843 if (ipv6_addr_is_set(&tun_key
->ipv6_src
)) {
2844 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
, &tun_key
->ipv6_src
);
2846 if (ipv6_addr_is_set(&tun_key
->ipv6_dst
)) {
2847 nl_msg_put_in6_addr(a
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
, &tun_key
->ipv6_dst
);
2849 if (tun_key
->ip_tos
) {
2850 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TOS
, tun_key
->ip_tos
);
2852 nl_msg_put_u8(a
, OVS_TUNNEL_KEY_ATTR_TTL
, tun_key
->ip_ttl
);
2853 if (tun_key
->flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
2854 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
2856 if (tun_key
->flags
& FLOW_TNL_F_CSUM
) {
2857 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
2859 if (tun_key
->tp_src
) {
2860 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, tun_key
->tp_src
);
2862 if (tun_key
->tp_dst
) {
2863 nl_msg_put_be16(a
, OVS_TUNNEL_KEY_ATTR_TP_DST
, tun_key
->tp_dst
);
2865 if (tun_key
->flags
& FLOW_TNL_F_OAM
) {
2866 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
2869 /* If tnl_type is set to a particular type of output tunnel,
2870 * only put its relevant tunnel metadata to the nlattr.
2871 * If tnl_type is NULL, put tunnel metadata according to the
2874 if ((!tnl_type
|| !strcmp(tnl_type
, "vxlan")) &&
2875 (tun_key
->gbp_flags
|| tun_key
->gbp_id
)) {
2876 size_t vxlan_opts_ofs
;
2878 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
2879 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
,
2880 (tun_key
->gbp_flags
<< 16) | ntohs(tun_key
->gbp_id
));
2881 nl_msg_end_nested(a
, vxlan_opts_ofs
);
2884 if (!tnl_type
|| !strcmp(tnl_type
, "geneve")) {
2885 tun_metadata_to_geneve_nlattr(tun_key
, tun_flow_key
, key_buf
, a
);
2888 if ((!tnl_type
|| !strcmp(tnl_type
, "erspan") ||
2889 !strcmp(tnl_type
, "ip6erspan")) &&
2890 (tun_key
->erspan_ver
== 1 || tun_key
->erspan_ver
== 2)) {
2891 struct erspan_metadata opts
;
2893 opts
.version
= tun_key
->erspan_ver
;
2894 if (opts
.version
== 1) {
2895 opts
.u
.index
= htonl(tun_key
->erspan_idx
);
2897 opts
.u
.md2
.dir
= tun_key
->erspan_dir
;
2898 set_hwid(&opts
.u
.md2
, tun_key
->erspan_hwid
);
2900 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
2901 &opts
, sizeof(opts
));
2904 nl_msg_end_nested(a
, tun_key_ofs
);
2908 odp_mask_is_constant__(enum ovs_key_attr attr
, const void *mask
, size_t size
,
2911 /* Convert 'constant' to all the widths we need. C conversion rules ensure
2912 * that -1 becomes all-1-bits and 0 does not change. */
2913 ovs_be16 be16
= (OVS_FORCE ovs_be16
) constant
;
2914 uint32_t u32
= constant
;
2915 uint8_t u8
= constant
;
2916 const struct in6_addr
*in6
= constant
? &in6addr_exact
: &in6addr_any
;
2919 case OVS_KEY_ATTR_UNSPEC
:
2920 case OVS_KEY_ATTR_ENCAP
:
2921 case __OVS_KEY_ATTR_MAX
:
2925 case OVS_KEY_ATTR_PRIORITY
:
2926 case OVS_KEY_ATTR_IN_PORT
:
2927 case OVS_KEY_ATTR_ETHERNET
:
2928 case OVS_KEY_ATTR_VLAN
:
2929 case OVS_KEY_ATTR_ETHERTYPE
:
2930 case OVS_KEY_ATTR_IPV4
:
2931 case OVS_KEY_ATTR_TCP
:
2932 case OVS_KEY_ATTR_UDP
:
2933 case OVS_KEY_ATTR_ICMP
:
2934 case OVS_KEY_ATTR_ICMPV6
:
2935 case OVS_KEY_ATTR_ND
:
2936 case OVS_KEY_ATTR_SKB_MARK
:
2937 case OVS_KEY_ATTR_TUNNEL
:
2938 case OVS_KEY_ATTR_SCTP
:
2939 case OVS_KEY_ATTR_DP_HASH
:
2940 case OVS_KEY_ATTR_RECIRC_ID
:
2941 case OVS_KEY_ATTR_MPLS
:
2942 case OVS_KEY_ATTR_CT_STATE
:
2943 case OVS_KEY_ATTR_CT_ZONE
:
2944 case OVS_KEY_ATTR_CT_MARK
:
2945 case OVS_KEY_ATTR_CT_LABELS
:
2946 case OVS_KEY_ATTR_PACKET_TYPE
:
2947 case OVS_KEY_ATTR_NSH
:
2948 return is_all_byte(mask
, size
, u8
);
2950 case OVS_KEY_ATTR_TCP_FLAGS
:
2951 return TCP_FLAGS(*(ovs_be16
*) mask
) == TCP_FLAGS(be16
);
2953 case OVS_KEY_ATTR_IPV6
: {
2954 const struct ovs_key_ipv6
*ipv6_mask
= mask
;
2955 return ((ipv6_mask
->ipv6_label
& htonl(IPV6_LABEL_MASK
))
2956 == htonl(IPV6_LABEL_MASK
& u32
)
2957 && ipv6_mask
->ipv6_proto
== u8
2958 && ipv6_mask
->ipv6_tclass
== u8
2959 && ipv6_mask
->ipv6_hlimit
== u8
2960 && ipv6_mask
->ipv6_frag
== u8
2961 && ipv6_addr_equals(&ipv6_mask
->ipv6_src
, in6
)
2962 && ipv6_addr_equals(&ipv6_mask
->ipv6_dst
, in6
));
2965 case OVS_KEY_ATTR_ARP
:
2966 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_arp
, arp_tha
), u8
);
2968 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
2969 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv4
,
2972 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
2973 return is_all_byte(mask
, OFFSETOFEND(struct ovs_key_ct_tuple_ipv6
,
2978 /* The caller must already have verified that 'ma' has a correct length.
2980 * The main purpose of this function is formatting, to allow code to figure out
2981 * whether the mask can be omitted. It doesn't try hard for attributes that
2982 * contain sub-attributes, etc., because normally those would be broken down
2983 * further for formatting. */
2985 odp_mask_attr_is_wildcard(const struct nlattr
*ma
)
2987 return odp_mask_is_constant__(nl_attr_type(ma
),
2988 nl_attr_get(ma
), nl_attr_get_size(ma
), 0);
2991 /* The caller must already have verified that 'size' is a correct length for
2994 * The main purpose of this function is formatting, to allow code to figure out
2995 * whether the mask can be omitted. It doesn't try hard for attributes that
2996 * contain sub-attributes, etc., because normally those would be broken down
2997 * further for formatting. */
2999 odp_mask_is_exact(enum ovs_key_attr attr
, const void *mask
, size_t size
)
3001 return odp_mask_is_constant__(attr
, mask
, size
, -1);
3004 /* The caller must already have verified that 'ma' has a correct length. */
3006 odp_mask_attr_is_exact(const struct nlattr
*ma
)
3008 enum ovs_key_attr attr
= nl_attr_type(ma
);
3009 return odp_mask_is_exact(attr
, nl_attr_get(ma
), nl_attr_get_size(ma
));
3013 odp_portno_names_set(struct hmap
*portno_names
, odp_port_t port_no
,
3016 struct odp_portno_names
*odp_portno_names
;
3018 odp_portno_names
= xmalloc(sizeof *odp_portno_names
);
3019 odp_portno_names
->port_no
= port_no
;
3020 odp_portno_names
->name
= xstrdup(port_name
);
3021 hmap_insert(portno_names
, &odp_portno_names
->hmap_node
,
3022 hash_odp_port(port_no
));
3026 odp_portno_names_get(const struct hmap
*portno_names
, odp_port_t port_no
)
3029 struct odp_portno_names
*odp_portno_names
;
3031 HMAP_FOR_EACH_IN_BUCKET (odp_portno_names
, hmap_node
,
3032 hash_odp_port(port_no
), portno_names
) {
3033 if (odp_portno_names
->port_no
== port_no
) {
3034 return odp_portno_names
->name
;
3042 odp_portno_names_destroy(struct hmap
*portno_names
)
3044 struct odp_portno_names
*odp_portno_names
;
3046 HMAP_FOR_EACH_POP (odp_portno_names
, hmap_node
, portno_names
) {
3047 free(odp_portno_names
->name
);
3048 free(odp_portno_names
);
3053 odp_portno_name_format(const struct hmap
*portno_names
, odp_port_t port_no
,
3056 const char *name
= odp_portno_names_get(portno_names
, port_no
);
3058 ds_put_cstr(s
, name
);
3060 ds_put_format(s
, "%"PRIu32
, port_no
);
3064 /* Format helpers. */
3067 format_eth(struct ds
*ds
, const char *name
, const struct eth_addr key
,
3068 const struct eth_addr
*mask
, bool verbose
)
3070 bool mask_empty
= mask
&& eth_addr_is_zero(*mask
);
3072 if (verbose
|| !mask_empty
) {
3073 bool mask_full
= !mask
|| eth_mask_is_exact(*mask
);
3076 ds_put_format(ds
, "%s="ETH_ADDR_FMT
",", name
, ETH_ADDR_ARGS(key
));
3078 ds_put_format(ds
, "%s=", name
);
3079 eth_format_masked(key
, mask
, ds
);
3080 ds_put_char(ds
, ',');
3087 format_be64(struct ds
*ds
, const char *name
, ovs_be64 key
,
3088 const ovs_be64
*mask
, bool verbose
)
3090 bool mask_empty
= mask
&& !*mask
;
3092 if (verbose
|| !mask_empty
) {
3093 bool mask_full
= !mask
|| *mask
== OVS_BE64_MAX
;
3095 ds_put_format(ds
, "%s=0x%"PRIx64
, name
, ntohll(key
));
3096 if (!mask_full
) { /* Partially masked. */
3097 ds_put_format(ds
, "/%#"PRIx64
, ntohll(*mask
));
3099 ds_put_char(ds
, ',');
3104 format_ipv4(struct ds
*ds
, const char *name
, ovs_be32 key
,
3105 const ovs_be32
*mask
, bool verbose
)
3107 bool mask_empty
= mask
&& !*mask
;
3109 if (verbose
|| !mask_empty
) {
3110 bool mask_full
= !mask
|| *mask
== OVS_BE32_MAX
;
3112 ds_put_format(ds
, "%s="IP_FMT
, name
, IP_ARGS(key
));
3113 if (!mask_full
) { /* Partially masked. */
3114 ds_put_format(ds
, "/"IP_FMT
, IP_ARGS(*mask
));
3116 ds_put_char(ds
, ',');
3121 format_in6_addr(struct ds
*ds
, const char *name
,
3122 const struct in6_addr
*key
,
3123 const struct in6_addr
*mask
,
3126 char buf
[INET6_ADDRSTRLEN
];
3127 bool mask_empty
= mask
&& ipv6_mask_is_any(mask
);
3129 if (verbose
|| !mask_empty
) {
3130 bool mask_full
= !mask
|| ipv6_mask_is_exact(mask
);
3132 inet_ntop(AF_INET6
, key
, buf
, sizeof buf
);
3133 ds_put_format(ds
, "%s=%s", name
, buf
);
3134 if (!mask_full
) { /* Partially masked. */
3135 inet_ntop(AF_INET6
, mask
, buf
, sizeof buf
);
3136 ds_put_format(ds
, "/%s", buf
);
3138 ds_put_char(ds
, ',');
3143 format_ipv6_label(struct ds
*ds
, const char *name
, ovs_be32 key
,
3144 const ovs_be32
*mask
, bool verbose
)
3146 bool mask_empty
= mask
&& !*mask
;
3148 if (verbose
|| !mask_empty
) {
3149 bool mask_full
= !mask
3150 || (*mask
& htonl(IPV6_LABEL_MASK
)) == htonl(IPV6_LABEL_MASK
);
3152 ds_put_format(ds
, "%s=%#"PRIx32
, name
, ntohl(key
));
3153 if (!mask_full
) { /* Partially masked. */
3154 ds_put_format(ds
, "/%#"PRIx32
, ntohl(*mask
));
3156 ds_put_char(ds
, ',');
3161 format_u8x(struct ds
*ds
, const char *name
, uint8_t key
,
3162 const uint8_t *mask
, bool verbose
)
3164 bool mask_empty
= mask
&& !*mask
;
3166 if (verbose
|| !mask_empty
) {
3167 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3169 ds_put_format(ds
, "%s=%#"PRIx8
, name
, key
);
3170 if (!mask_full
) { /* Partially masked. */
3171 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3173 ds_put_char(ds
, ',');
3178 format_u8u(struct ds
*ds
, const char *name
, uint8_t key
,
3179 const uint8_t *mask
, bool verbose
)
3181 bool mask_empty
= mask
&& !*mask
;
3183 if (verbose
|| !mask_empty
) {
3184 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3186 ds_put_format(ds
, "%s=%"PRIu8
, name
, key
);
3187 if (!mask_full
) { /* Partially masked. */
3188 ds_put_format(ds
, "/%#"PRIx8
, *mask
);
3190 ds_put_char(ds
, ',');
3195 format_be16(struct ds
*ds
, const char *name
, ovs_be16 key
,
3196 const ovs_be16
*mask
, bool verbose
)
3198 bool mask_empty
= mask
&& !*mask
;
3200 if (verbose
|| !mask_empty
) {
3201 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3203 ds_put_format(ds
, "%s=%"PRIu16
, name
, ntohs(key
));
3204 if (!mask_full
) { /* Partially masked. */
3205 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3207 ds_put_char(ds
, ',');
3212 format_be16x(struct ds
*ds
, const char *name
, ovs_be16 key
,
3213 const ovs_be16
*mask
, bool verbose
)
3215 bool mask_empty
= mask
&& !*mask
;
3217 if (verbose
|| !mask_empty
) {
3218 bool mask_full
= !mask
|| *mask
== OVS_BE16_MAX
;
3220 ds_put_format(ds
, "%s=%#"PRIx16
, name
, ntohs(key
));
3221 if (!mask_full
) { /* Partially masked. */
3222 ds_put_format(ds
, "/%#"PRIx16
, ntohs(*mask
));
3224 ds_put_char(ds
, ',');
3229 format_tun_flags(struct ds
*ds
, const char *name
, uint16_t key
,
3230 const uint16_t *mask
, bool verbose
)
3232 bool mask_empty
= mask
&& !*mask
;
3234 if (verbose
|| !mask_empty
) {
3235 ds_put_cstr(ds
, name
);
3236 ds_put_char(ds
, '(');
3238 format_flags_masked(ds
, NULL
, flow_tun_flag_to_string
, key
,
3239 *mask
& FLOW_TNL_F_MASK
, FLOW_TNL_F_MASK
);
3240 } else { /* Fully masked. */
3241 format_flags(ds
, flow_tun_flag_to_string
, key
, '|');
3243 ds_put_cstr(ds
, "),");
3248 check_attr_len(struct ds
*ds
, const struct nlattr
*a
, const struct nlattr
*ma
,
3249 const struct attr_len_tbl tbl
[], int max_type
, bool need_key
)
3253 expected_len
= odp_key_attr_len(tbl
, max_type
, nl_attr_type(a
));
3254 if (expected_len
!= ATTR_LEN_VARIABLE
&&
3255 expected_len
!= ATTR_LEN_NESTED
) {
3257 bool bad_key_len
= nl_attr_get_size(a
) != expected_len
;
3258 bool bad_mask_len
= ma
&& nl_attr_get_size(ma
) != expected_len
;
3260 if (bad_key_len
|| bad_mask_len
) {
3262 ds_put_format(ds
, "key%u", nl_attr_type(a
));
3265 ds_put_format(ds
, "(bad key length %"PRIuSIZE
", expected %d)(",
3266 nl_attr_get_size(a
), expected_len
);
3268 format_generic_odp_key(a
, ds
);
3270 ds_put_char(ds
, '/');
3272 ds_put_format(ds
, "(bad mask length %"PRIuSIZE
", expected %d)(",
3273 nl_attr_get_size(ma
), expected_len
);
3275 format_generic_odp_key(ma
, ds
);
3277 ds_put_char(ds
, ')');
3286 format_unknown_key(struct ds
*ds
, const struct nlattr
*a
,
3287 const struct nlattr
*ma
)
3289 ds_put_format(ds
, "key%u(", nl_attr_type(a
));
3290 format_generic_odp_key(a
, ds
);
3291 if (ma
&& !odp_mask_attr_is_exact(ma
)) {
3292 ds_put_char(ds
, '/');
3293 format_generic_odp_key(ma
, ds
);
3295 ds_put_cstr(ds
, "),");
3299 format_odp_tun_vxlan_opt(const struct nlattr
*attr
,
3300 const struct nlattr
*mask_attr
, struct ds
*ds
,
3304 const struct nlattr
*a
;
3307 ofpbuf_init(&ofp
, 100);
3308 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3309 uint16_t type
= nl_attr_type(a
);
3310 const struct nlattr
*ma
= NULL
;
3313 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3314 nl_attr_get_size(mask_attr
), type
);
3316 ma
= generate_all_wildcard_mask(ovs_vxlan_ext_attr_lens
,
3322 if (!check_attr_len(ds
, a
, ma
, ovs_vxlan_ext_attr_lens
,
3323 OVS_VXLAN_EXT_MAX
, true)) {
3328 case OVS_VXLAN_EXT_GBP
: {
3329 uint32_t key
= nl_attr_get_u32(a
);
3330 ovs_be16 id
, id_mask
;
3331 uint8_t flags
, flags_mask
= 0;
3333 id
= htons(key
& 0xFFFF);
3334 flags
= (key
>> 16) & 0xFF;
3336 uint32_t mask
= nl_attr_get_u32(ma
);
3337 id_mask
= htons(mask
& 0xFFFF);
3338 flags_mask
= (mask
>> 16) & 0xFF;
3341 ds_put_cstr(ds
, "gbp(");
3342 format_be16(ds
, "id", id
, ma
? &id_mask
: NULL
, verbose
);
3343 format_u8x(ds
, "flags", flags
, ma
? &flags_mask
: NULL
, verbose
);
3345 ds_put_cstr(ds
, "),");
3350 format_unknown_key(ds
, a
, ma
);
3356 ofpbuf_uninit(&ofp
);
3360 format_odp_tun_erspan_opt(const struct nlattr
*attr
,
3361 const struct nlattr
*mask_attr
, struct ds
*ds
,
3364 const struct erspan_metadata
*opts
, *mask
;
3365 uint8_t ver
, ver_ma
, dir
, dir_ma
, hwid
, hwid_ma
;
3367 opts
= nl_attr_get(attr
);
3368 mask
= mask_attr
? nl_attr_get(mask_attr
) : NULL
;
3370 ver
= (uint8_t)opts
->version
;
3372 ver_ma
= (uint8_t)mask
->version
;
3375 format_u8u(ds
, "ver", ver
, mask
? &ver_ma
: NULL
, verbose
);
3377 if (opts
->version
== 1) {
3379 ds_put_format(ds
, "idx=%#"PRIx32
"/%#"PRIx32
",",
3380 ntohl(opts
->u
.index
),
3381 ntohl(mask
->u
.index
));
3383 ds_put_format(ds
, "idx=%#"PRIx32
",", ntohl(opts
->u
.index
));
3385 } else if (opts
->version
== 2) {
3386 dir
= opts
->u
.md2
.dir
;
3387 hwid
= opts
->u
.md2
.hwid
;
3389 dir_ma
= mask
->u
.md2
.dir
;
3390 hwid_ma
= mask
->u
.md2
.hwid
;
3393 format_u8u(ds
, "dir", dir
, mask
? &dir_ma
: NULL
, verbose
);
3394 format_u8x(ds
, "hwid", hwid
, mask
? &hwid_ma
: NULL
, verbose
);
3399 #define MASK(PTR, FIELD) PTR ? &PTR->FIELD : NULL
3402 format_geneve_opts(const struct geneve_opt
*opt
,
3403 const struct geneve_opt
*mask
, int opts_len
,
3404 struct ds
*ds
, bool verbose
)
3406 while (opts_len
> 0) {
3408 uint8_t data_len
, data_len_mask
;
3410 if (opts_len
< sizeof *opt
) {
3411 ds_put_format(ds
, "opt len %u less than minimum %"PRIuSIZE
,
3412 opts_len
, sizeof *opt
);
3416 data_len
= opt
->length
* 4;
3418 if (mask
->length
== 0x1f) {
3419 data_len_mask
= UINT8_MAX
;
3421 data_len_mask
= mask
->length
;
3424 len
= sizeof *opt
+ data_len
;
3425 if (len
> opts_len
) {
3426 ds_put_format(ds
, "opt len %u greater than remaining %u",
3431 ds_put_char(ds
, '{');
3432 format_be16x(ds
, "class", opt
->opt_class
, MASK(mask
, opt_class
),
3434 format_u8x(ds
, "type", opt
->type
, MASK(mask
, type
), verbose
);
3435 format_u8u(ds
, "len", data_len
, mask
? &data_len_mask
: NULL
, verbose
);
3437 (verbose
|| !mask
|| !is_all_zeros(mask
+ 1, data_len
))) {
3438 ds_put_hex(ds
, opt
+ 1, data_len
);
3439 if (mask
&& !is_all_ones(mask
+ 1, data_len
)) {
3440 ds_put_char(ds
, '/');
3441 ds_put_hex(ds
, mask
+ 1, data_len
);
3446 ds_put_char(ds
, '}');
3448 opt
+= len
/ sizeof(*opt
);
3450 mask
+= len
/ sizeof(*opt
);
3457 format_odp_tun_geneve(const struct nlattr
*attr
,
3458 const struct nlattr
*mask_attr
, struct ds
*ds
,
3461 int opts_len
= nl_attr_get_size(attr
);
3462 const struct geneve_opt
*opt
= nl_attr_get(attr
);
3463 const struct geneve_opt
*mask
= mask_attr
?
3464 nl_attr_get(mask_attr
) : NULL
;
3466 if (mask
&& nl_attr_get_size(attr
) != nl_attr_get_size(mask_attr
)) {
3467 ds_put_format(ds
, "value len %"PRIuSIZE
" different from mask len %"PRIuSIZE
,
3468 nl_attr_get_size(attr
), nl_attr_get_size(mask_attr
));
3472 format_geneve_opts(opt
, mask
, opts_len
, ds
, verbose
);
3476 format_odp_nsh_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3480 const struct nlattr
*a
;
3481 struct ovs_key_nsh nsh
;
3482 struct ovs_key_nsh nsh_mask
;
3484 memset(&nsh
, 0, sizeof nsh
);
3485 memset(&nsh_mask
, 0xff, sizeof nsh_mask
);
3487 NL_NESTED_FOR_EACH (a
, left
, attr
) {
3488 enum ovs_nsh_key_attr type
= nl_attr_type(a
);
3489 const struct nlattr
*ma
= NULL
;
3492 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3493 nl_attr_get_size(mask_attr
), type
);
3496 if (!check_attr_len(ds
, a
, ma
, ovs_nsh_key_attr_lens
,
3497 OVS_NSH_KEY_ATTR_MAX
, true)) {
3502 case OVS_NSH_KEY_ATTR_UNSPEC
:
3504 case OVS_NSH_KEY_ATTR_BASE
: {
3505 const struct ovs_nsh_key_base
*base
= nl_attr_get(a
);
3506 const struct ovs_nsh_key_base
*base_mask
3507 = ma
? nl_attr_get(ma
) : NULL
;
3508 nsh
.flags
= base
->flags
;
3509 nsh
.ttl
= base
->ttl
;
3510 nsh
.mdtype
= base
->mdtype
;
3512 nsh
.path_hdr
= base
->path_hdr
;
3514 nsh_mask
.flags
= base_mask
->flags
;
3515 nsh_mask
.ttl
= base_mask
->ttl
;
3516 nsh_mask
.mdtype
= base_mask
->mdtype
;
3517 nsh_mask
.np
= base_mask
->np
;
3518 nsh_mask
.path_hdr
= base_mask
->path_hdr
;
3522 case OVS_NSH_KEY_ATTR_MD1
: {
3523 const struct ovs_nsh_key_md1
*md1
= nl_attr_get(a
);
3524 const struct ovs_nsh_key_md1
*md1_mask
3525 = ma
? nl_attr_get(ma
) : NULL
;
3526 memcpy(nsh
.context
, md1
->context
, sizeof md1
->context
);
3528 memcpy(nsh_mask
.context
, md1_mask
->context
,
3529 sizeof md1_mask
->context
);
3533 case OVS_NSH_KEY_ATTR_MD2
:
3534 case __OVS_NSH_KEY_ATTR_MAX
:
3536 /* No support for matching other metadata formats yet. */
3542 format_nsh_key_mask(ds
, &nsh
, &nsh_mask
);
3544 format_nsh_key(ds
, &nsh
);
3549 format_odp_tun_attr(const struct nlattr
*attr
, const struct nlattr
*mask_attr
,
3550 struct ds
*ds
, bool verbose
)
3553 const struct nlattr
*a
;
3555 uint16_t mask_flags
= 0;
3558 ofpbuf_init(&ofp
, 100);
3559 NL_NESTED_FOR_EACH(a
, left
, attr
) {
3560 enum ovs_tunnel_key_attr type
= nl_attr_type(a
);
3561 const struct nlattr
*ma
= NULL
;
3564 ma
= nl_attr_find__(nl_attr_get(mask_attr
),
3565 nl_attr_get_size(mask_attr
), type
);
3567 ma
= generate_all_wildcard_mask(ovs_tun_key_attr_lens
,
3568 OVS_TUNNEL_KEY_ATTR_MAX
,
3573 if (!check_attr_len(ds
, a
, ma
, ovs_tun_key_attr_lens
,
3574 OVS_TUNNEL_KEY_ATTR_MAX
, true)) {
3579 case OVS_TUNNEL_KEY_ATTR_ID
:
3580 format_be64(ds
, "tun_id", nl_attr_get_be64(a
),
3581 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3582 flags
|= FLOW_TNL_F_KEY
;
3584 mask_flags
|= FLOW_TNL_F_KEY
;
3587 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
3588 format_ipv4(ds
, "src", nl_attr_get_be32(a
),
3589 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3591 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
3592 format_ipv4(ds
, "dst", nl_attr_get_be32(a
),
3593 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3595 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
: {
3596 struct in6_addr ipv6_src
;
3597 ipv6_src
= nl_attr_get_in6_addr(a
);
3598 format_in6_addr(ds
, "ipv6_src", &ipv6_src
,
3599 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3602 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
: {
3603 struct in6_addr ipv6_dst
;
3604 ipv6_dst
= nl_attr_get_in6_addr(a
);
3605 format_in6_addr(ds
, "ipv6_dst", &ipv6_dst
,
3606 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3609 case OVS_TUNNEL_KEY_ATTR_TOS
:
3610 format_u8x(ds
, "tos", nl_attr_get_u8(a
),
3611 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3613 case OVS_TUNNEL_KEY_ATTR_TTL
:
3614 format_u8u(ds
, "ttl", nl_attr_get_u8(a
),
3615 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3617 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3618 flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3620 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3621 flags
|= FLOW_TNL_F_CSUM
;
3623 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
3624 format_be16(ds
, "tp_src", nl_attr_get_be16(a
),
3625 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3627 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
3628 format_be16(ds
, "tp_dst", nl_attr_get_be16(a
),
3629 ma
? nl_attr_get(ma
) : NULL
, verbose
);
3631 case OVS_TUNNEL_KEY_ATTR_OAM
:
3632 flags
|= FLOW_TNL_F_OAM
;
3634 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
3635 ds_put_cstr(ds
, "vxlan(");
3636 format_odp_tun_vxlan_opt(a
, ma
, ds
, verbose
);
3637 ds_put_cstr(ds
, "),");
3639 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
3640 ds_put_cstr(ds
, "geneve(");
3641 format_odp_tun_geneve(a
, ma
, ds
, verbose
);
3642 ds_put_cstr(ds
, "),");
3644 case OVS_TUNNEL_KEY_ATTR_PAD
:
3646 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
3647 ds_put_cstr(ds
, "erspan(");
3648 format_odp_tun_erspan_opt(a
, ma
, ds
, verbose
);
3649 ds_put_cstr(ds
, "),");
3651 case __OVS_TUNNEL_KEY_ATTR_MAX
:
3653 format_unknown_key(ds
, a
, ma
);
3658 /* Flags can have a valid mask even if the attribute is not set, so
3659 * we need to collect these separately. */
3661 NL_NESTED_FOR_EACH(a
, left
, mask_attr
) {
3662 switch (nl_attr_type(a
)) {
3663 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
3664 mask_flags
|= FLOW_TNL_F_DONT_FRAGMENT
;
3666 case OVS_TUNNEL_KEY_ATTR_CSUM
:
3667 mask_flags
|= FLOW_TNL_F_CSUM
;
3669 case OVS_TUNNEL_KEY_ATTR_OAM
:
3670 mask_flags
|= FLOW_TNL_F_OAM
;
3676 format_tun_flags(ds
, "flags", flags
, mask_attr
? &mask_flags
: NULL
,
3679 ofpbuf_uninit(&ofp
);
3683 odp_ct_state_to_string(uint32_t flag
)
3686 case OVS_CS_F_REPLY_DIR
:
3688 case OVS_CS_F_TRACKED
:
3692 case OVS_CS_F_ESTABLISHED
:
3694 case OVS_CS_F_RELATED
:
3696 case OVS_CS_F_INVALID
:
3698 case OVS_CS_F_SRC_NAT
:
3700 case OVS_CS_F_DST_NAT
:
3708 format_frag(struct ds
*ds
, const char *name
, uint8_t key
,
3709 const uint8_t *mask
, bool verbose OVS_UNUSED
)
3711 bool mask_empty
= mask
&& !*mask
;
3712 bool mask_full
= !mask
|| *mask
== UINT8_MAX
;
3714 /* ODP frag is an enumeration field; partial masks are not meaningful. */
3715 if (!mask_empty
&& !mask_full
) {
3716 ds_put_format(ds
, "error: partial mask not supported for frag (%#"
3718 } else if (!mask_empty
) {
3719 ds_put_format(ds
, "%s=%s,", name
, ovs_frag_type_to_string(key
));
3724 mask_empty(const struct nlattr
*ma
)
3732 mask
= nl_attr_get(ma
);
3733 n
= nl_attr_get_size(ma
);
3735 return is_all_zeros(mask
, n
);
3738 /* The caller must have already verified that 'a' and 'ma' have correct
3741 format_odp_key_attr__(const struct nlattr
*a
, const struct nlattr
*ma
,
3742 const struct hmap
*portno_names
, struct ds
*ds
,
3745 enum ovs_key_attr attr
= nl_attr_type(a
);
3746 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
3749 is_exact
= ma
? odp_mask_attr_is_exact(ma
) : true;
3751 ds_put_cstr(ds
, ovs_key_attr_to_string(attr
, namebuf
, sizeof namebuf
));
3753 ds_put_char(ds
, '(');
3755 case OVS_KEY_ATTR_ENCAP
:
3756 if (ma
&& nl_attr_get_size(ma
) && nl_attr_get_size(a
)) {
3757 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
),
3758 nl_attr_get(ma
), nl_attr_get_size(ma
), NULL
, ds
,
3760 } else if (nl_attr_get_size(a
)) {
3761 odp_flow_format(nl_attr_get(a
), nl_attr_get_size(a
), NULL
, 0, NULL
,
3766 case OVS_KEY_ATTR_PRIORITY
:
3767 case OVS_KEY_ATTR_SKB_MARK
:
3768 case OVS_KEY_ATTR_DP_HASH
:
3769 case OVS_KEY_ATTR_RECIRC_ID
:
3770 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3772 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3776 case OVS_KEY_ATTR_CT_MARK
:
3777 if (verbose
|| !mask_empty(ma
)) {
3778 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3780 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3785 case OVS_KEY_ATTR_CT_STATE
:
3787 ds_put_format(ds
, "%#"PRIx32
, nl_attr_get_u32(a
));
3789 ds_put_format(ds
, "/%#"PRIx32
,
3790 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
));
3792 } else if (!is_exact
) {
3793 format_flags_masked(ds
, NULL
, odp_ct_state_to_string
,
3795 mask_empty(ma
) ? 0 : nl_attr_get_u32(ma
),
3798 format_flags(ds
, odp_ct_state_to_string
, nl_attr_get_u32(a
), '|');
3802 case OVS_KEY_ATTR_CT_ZONE
:
3803 if (verbose
|| !mask_empty(ma
)) {
3804 ds_put_format(ds
, "%#"PRIx16
, nl_attr_get_u16(a
));
3806 ds_put_format(ds
, "/%#"PRIx16
, nl_attr_get_u16(ma
));
3811 case OVS_KEY_ATTR_CT_LABELS
: {
3812 const ovs_32aligned_u128
*value
= nl_attr_get(a
);
3813 const ovs_32aligned_u128
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3815 format_u128(ds
, value
, mask
, verbose
);
3819 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
3820 const struct ovs_key_ct_tuple_ipv4
*key
= nl_attr_get(a
);
3821 const struct ovs_key_ct_tuple_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3823 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3824 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3825 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3827 format_be16(ds
, "tp_src", key
->src_port
, MASK(mask
, src_port
),
3829 format_be16(ds
, "tp_dst", key
->dst_port
, MASK(mask
, dst_port
),
3835 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
3836 const struct ovs_key_ct_tuple_ipv6
*key
= nl_attr_get(a
);
3837 const struct ovs_key_ct_tuple_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3839 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3841 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3843 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3845 format_be16(ds
, "src_port", key
->src_port
, MASK(mask
, src_port
),
3847 format_be16(ds
, "dst_port", key
->dst_port
, MASK(mask
, dst_port
),
3853 case OVS_KEY_ATTR_TUNNEL
:
3854 format_odp_tun_attr(a
, ma
, ds
, verbose
);
3857 case OVS_KEY_ATTR_IN_PORT
:
3859 odp_portno_name_format(portno_names
, nl_attr_get_odp_port(a
), ds
);
3861 ds_put_format(ds
, "%"PRIu32
, nl_attr_get_u32(a
));
3863 ds_put_format(ds
, "/%#"PRIx32
, nl_attr_get_u32(ma
));
3868 case OVS_KEY_ATTR_PACKET_TYPE
: {
3869 ovs_be32 value
= nl_attr_get_be32(a
);
3870 ovs_be32 mask
= ma
? nl_attr_get_be32(ma
) : OVS_BE32_MAX
;
3872 ovs_be16 ns
= htons(pt_ns(value
));
3873 ovs_be16 ns_mask
= htons(pt_ns(mask
));
3874 format_be16(ds
, "ns", ns
, &ns_mask
, verbose
);
3876 ovs_be16 ns_type
= pt_ns_type_be(value
);
3877 ovs_be16 ns_type_mask
= pt_ns_type_be(mask
);
3878 format_be16x(ds
, "id", ns_type
, &ns_type_mask
, verbose
);
3884 case OVS_KEY_ATTR_ETHERNET
: {
3885 const struct ovs_key_ethernet
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3886 const struct ovs_key_ethernet
*key
= nl_attr_get(a
);
3888 format_eth(ds
, "src", key
->eth_src
, MASK(mask
, eth_src
), verbose
);
3889 format_eth(ds
, "dst", key
->eth_dst
, MASK(mask
, eth_dst
), verbose
);
3893 case OVS_KEY_ATTR_VLAN
:
3894 format_vlan_tci(ds
, nl_attr_get_be16(a
),
3895 ma
? nl_attr_get_be16(ma
) : OVS_BE16_MAX
, verbose
);
3898 case OVS_KEY_ATTR_MPLS
: {
3899 const struct ovs_key_mpls
*mpls_key
= nl_attr_get(a
);
3900 const struct ovs_key_mpls
*mpls_mask
= NULL
;
3901 size_t size
= nl_attr_get_size(a
);
3903 if (!size
|| size
% sizeof *mpls_key
) {
3904 ds_put_format(ds
, "(bad key length %"PRIuSIZE
")", size
);
3908 mpls_mask
= nl_attr_get(ma
);
3909 if (size
!= nl_attr_get_size(ma
)) {
3910 ds_put_format(ds
, "(key length %"PRIuSIZE
" != "
3911 "mask length %"PRIuSIZE
")",
3912 size
, nl_attr_get_size(ma
));
3916 format_mpls(ds
, mpls_key
, mpls_mask
, size
/ sizeof *mpls_key
);
3919 case OVS_KEY_ATTR_ETHERTYPE
:
3920 ds_put_format(ds
, "0x%04"PRIx16
, ntohs(nl_attr_get_be16(a
)));
3922 ds_put_format(ds
, "/0x%04"PRIx16
, ntohs(nl_attr_get_be16(ma
)));
3926 case OVS_KEY_ATTR_IPV4
: {
3927 const struct ovs_key_ipv4
*key
= nl_attr_get(a
);
3928 const struct ovs_key_ipv4
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3930 format_ipv4(ds
, "src", key
->ipv4_src
, MASK(mask
, ipv4_src
), verbose
);
3931 format_ipv4(ds
, "dst", key
->ipv4_dst
, MASK(mask
, ipv4_dst
), verbose
);
3932 format_u8u(ds
, "proto", key
->ipv4_proto
, MASK(mask
, ipv4_proto
),
3934 format_u8x(ds
, "tos", key
->ipv4_tos
, MASK(mask
, ipv4_tos
), verbose
);
3935 format_u8u(ds
, "ttl", key
->ipv4_ttl
, MASK(mask
, ipv4_ttl
), verbose
);
3936 format_frag(ds
, "frag", key
->ipv4_frag
, MASK(mask
, ipv4_frag
),
3941 case OVS_KEY_ATTR_IPV6
: {
3942 const struct ovs_key_ipv6
*key
= nl_attr_get(a
);
3943 const struct ovs_key_ipv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3945 format_in6_addr(ds
, "src", &key
->ipv6_src
, MASK(mask
, ipv6_src
),
3947 format_in6_addr(ds
, "dst", &key
->ipv6_dst
, MASK(mask
, ipv6_dst
),
3949 format_ipv6_label(ds
, "label", key
->ipv6_label
, MASK(mask
, ipv6_label
),
3951 format_u8u(ds
, "proto", key
->ipv6_proto
, MASK(mask
, ipv6_proto
),
3953 format_u8x(ds
, "tclass", key
->ipv6_tclass
, MASK(mask
, ipv6_tclass
),
3955 format_u8u(ds
, "hlimit", key
->ipv6_hlimit
, MASK(mask
, ipv6_hlimit
),
3957 format_frag(ds
, "frag", key
->ipv6_frag
, MASK(mask
, ipv6_frag
),
3962 /* These have the same structure and format. */
3963 case OVS_KEY_ATTR_TCP
:
3964 case OVS_KEY_ATTR_UDP
:
3965 case OVS_KEY_ATTR_SCTP
: {
3966 const struct ovs_key_tcp
*key
= nl_attr_get(a
);
3967 const struct ovs_key_tcp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3969 format_be16(ds
, "src", key
->tcp_src
, MASK(mask
, tcp_src
), verbose
);
3970 format_be16(ds
, "dst", key
->tcp_dst
, MASK(mask
, tcp_dst
), verbose
);
3974 case OVS_KEY_ATTR_TCP_FLAGS
:
3976 format_flags_masked(ds
, NULL
, packet_tcp_flag_to_string
,
3977 ntohs(nl_attr_get_be16(a
)),
3978 TCP_FLAGS(nl_attr_get_be16(ma
)),
3979 TCP_FLAGS(OVS_BE16_MAX
));
3981 format_flags(ds
, packet_tcp_flag_to_string
,
3982 ntohs(nl_attr_get_be16(a
)), '|');
3986 case OVS_KEY_ATTR_ICMP
: {
3987 const struct ovs_key_icmp
*key
= nl_attr_get(a
);
3988 const struct ovs_key_icmp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3990 format_u8u(ds
, "type", key
->icmp_type
, MASK(mask
, icmp_type
), verbose
);
3991 format_u8u(ds
, "code", key
->icmp_code
, MASK(mask
, icmp_code
), verbose
);
3995 case OVS_KEY_ATTR_ICMPV6
: {
3996 const struct ovs_key_icmpv6
*key
= nl_attr_get(a
);
3997 const struct ovs_key_icmpv6
*mask
= ma
? nl_attr_get(ma
) : NULL
;
3999 format_u8u(ds
, "type", key
->icmpv6_type
, MASK(mask
, icmpv6_type
),
4001 format_u8u(ds
, "code", key
->icmpv6_code
, MASK(mask
, icmpv6_code
),
4006 case OVS_KEY_ATTR_ARP
: {
4007 const struct ovs_key_arp
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4008 const struct ovs_key_arp
*key
= nl_attr_get(a
);
4010 format_ipv4(ds
, "sip", key
->arp_sip
, MASK(mask
, arp_sip
), verbose
);
4011 format_ipv4(ds
, "tip", key
->arp_tip
, MASK(mask
, arp_tip
), verbose
);
4012 format_be16(ds
, "op", key
->arp_op
, MASK(mask
, arp_op
), verbose
);
4013 format_eth(ds
, "sha", key
->arp_sha
, MASK(mask
, arp_sha
), verbose
);
4014 format_eth(ds
, "tha", key
->arp_tha
, MASK(mask
, arp_tha
), verbose
);
4018 case OVS_KEY_ATTR_ND
: {
4019 const struct ovs_key_nd
*mask
= ma
? nl_attr_get(ma
) : NULL
;
4020 const struct ovs_key_nd
*key
= nl_attr_get(a
);
4022 format_in6_addr(ds
, "target", &key
->nd_target
, MASK(mask
, nd_target
),
4024 format_eth(ds
, "sll", key
->nd_sll
, MASK(mask
, nd_sll
), verbose
);
4025 format_eth(ds
, "tll", key
->nd_tll
, MASK(mask
, nd_tll
), verbose
);
4030 case OVS_KEY_ATTR_NSH
: {
4031 format_odp_nsh_attr(a
, ma
, ds
);
4034 case OVS_KEY_ATTR_UNSPEC
:
4035 case __OVS_KEY_ATTR_MAX
:
4037 format_generic_odp_key(a
, ds
);
4039 ds_put_char(ds
, '/');
4040 format_generic_odp_key(ma
, ds
);
4044 ds_put_char(ds
, ')');
4048 format_odp_key_attr(const struct nlattr
*a
, const struct nlattr
*ma
,
4049 const struct hmap
*portno_names
, struct ds
*ds
,
4052 if (check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4053 OVS_KEY_ATTR_MAX
, false)) {
4054 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4058 static struct nlattr
*
4059 generate_all_wildcard_mask(const struct attr_len_tbl tbl
[], int max
,
4060 struct ofpbuf
*ofp
, const struct nlattr
*key
)
4062 const struct nlattr
*a
;
4064 int type
= nl_attr_type(key
);
4065 int size
= nl_attr_get_size(key
);
4067 if (odp_key_attr_len(tbl
, max
, type
) != ATTR_LEN_NESTED
) {
4068 nl_msg_put_unspec_zero(ofp
, type
, size
);
4072 if (tbl
[type
].next
) {
4073 const struct attr_len_tbl
*entry
= &tbl
[type
];
4075 max
= entry
->next_max
;
4078 nested_mask
= nl_msg_start_nested(ofp
, type
);
4079 NL_ATTR_FOR_EACH(a
, left
, key
, nl_attr_get_size(key
)) {
4080 generate_all_wildcard_mask(tbl
, max
, ofp
, nl_attr_get(a
));
4082 nl_msg_end_nested(ofp
, nested_mask
);
4089 format_u128(struct ds
*ds
, const ovs_32aligned_u128
*key
,
4090 const ovs_32aligned_u128
*mask
, bool verbose
)
4092 if (verbose
|| (mask
&& !ovs_u128_is_zero(get_32aligned_u128(mask
)))) {
4093 ovs_be128 value
= hton128(get_32aligned_u128(key
));
4094 ds_put_hex(ds
, &value
, sizeof value
);
4095 if (mask
&& !(ovs_u128_is_ones(get_32aligned_u128(mask
)))) {
4096 value
= hton128(get_32aligned_u128(mask
));
4097 ds_put_char(ds
, '/');
4098 ds_put_hex(ds
, &value
, sizeof value
);
4103 /* Read the string from 's_' as a 128-bit value. If the string contains
4104 * a "/", the rest of the string will be treated as a 128-bit mask.
4106 * If either the value or mask is larger than 64 bits, the string must
4107 * be in hexadecimal.
4110 scan_u128(const char *s_
, ovs_u128
*value
, ovs_u128
*mask
)
4112 char *s
= CONST_CAST(char *, s_
);
4116 if (!parse_int_string(s
, (uint8_t *)&be_value
, sizeof be_value
, &s
)) {
4117 *value
= ntoh128(be_value
);
4122 if (ovs_scan(s
, "/%n", &n
)) {
4126 error
= parse_int_string(s
, (uint8_t *)&be_mask
,
4127 sizeof be_mask
, &s
);
4131 *mask
= ntoh128(be_mask
);
4133 *mask
= OVS_U128_MAX
;
4143 odp_ufid_from_string(const char *s_
, ovs_u128
*ufid
)
4147 if (ovs_scan(s
, "ufid:")) {
4150 if (!uuid_from_string_prefix((struct uuid
*)ufid
, s
)) {
4162 odp_format_ufid(const ovs_u128
*ufid
, struct ds
*ds
)
4164 ds_put_format(ds
, "ufid:"UUID_FMT
, UUID_ARGS((struct uuid
*)ufid
));
4167 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4168 * OVS_KEY_ATTR_* attributes in 'key'. If non-null, additionally formats the
4169 * 'mask_len' bytes of 'mask' which apply to 'key'. If 'portno_names' is
4170 * non-null, translates odp port number to its name. */
4172 odp_flow_format(const struct nlattr
*key
, size_t key_len
,
4173 const struct nlattr
*mask
, size_t mask_len
,
4174 const struct hmap
*portno_names
, struct ds
*ds
, bool verbose
)
4177 const struct nlattr
*a
;
4179 bool has_ethtype_key
= false;
4180 bool has_packet_type_key
= false;
4182 bool first_field
= true;
4184 ofpbuf_init(&ofp
, 100);
4185 NL_ATTR_FOR_EACH (a
, left
, key
, key_len
) {
4186 int attr_type
= nl_attr_type(a
);
4187 const struct nlattr
*ma
= (mask
&& mask_len
4188 ? nl_attr_find__(mask
, mask_len
,
4191 if (!check_attr_len(ds
, a
, ma
, ovs_flow_key_attr_lens
,
4192 OVS_KEY_ATTR_MAX
, false)) {
4196 bool is_nested_attr
;
4197 bool is_wildcard
= false;
4199 if (attr_type
== OVS_KEY_ATTR_ETHERTYPE
) {
4200 has_ethtype_key
= true;
4201 } else if (attr_type
== OVS_KEY_ATTR_PACKET_TYPE
) {
4202 has_packet_type_key
= true;
4205 is_nested_attr
= odp_key_attr_len(ovs_flow_key_attr_lens
,
4206 OVS_KEY_ATTR_MAX
, attr_type
) ==
4209 if (mask
&& mask_len
) {
4210 ma
= nl_attr_find__(mask
, mask_len
, nl_attr_type(a
));
4211 is_wildcard
= ma
? odp_mask_attr_is_wildcard(ma
) : true;
4214 if (verbose
|| !is_wildcard
|| is_nested_attr
) {
4215 if (is_wildcard
&& !ma
) {
4216 ma
= generate_all_wildcard_mask(ovs_flow_key_attr_lens
,
4221 ds_put_char(ds
, ',');
4223 format_odp_key_attr__(a
, ma
, portno_names
, ds
, verbose
);
4224 first_field
= false;
4225 } else if (attr_type
== OVS_KEY_ATTR_ETHERNET
4226 && !has_packet_type_key
) {
4227 /* This special case reflects differences between the kernel
4228 * and userspace datapaths regarding the root type of the
4229 * packet being matched (typically Ethernet but some tunnels
4230 * can encapsulate IPv4 etc.). The kernel datapath does not
4231 * have an explicit way to indicate packet type; instead:
4233 * - If OVS_KEY_ATTR_ETHERNET is present, the packet is an
4234 * Ethernet packet and OVS_KEY_ATTR_ETHERTYPE is the
4235 * Ethertype encoded in the Ethernet header.
4237 * - If OVS_KEY_ATTR_ETHERNET is absent, then the packet's
4238 * root type is that encoded in OVS_KEY_ATTR_ETHERTYPE
4239 * (i.e. if OVS_KEY_ATTR_ETHERTYPE is 0x0800 then the
4240 * packet is an IPv4 packet).
4242 * Thus, if OVS_KEY_ATTR_ETHERNET is present, even if it is
4243 * all-wildcarded, it is important to print it.
4245 * On the other hand, the userspace datapath supports
4246 * OVS_KEY_ATTR_PACKET_TYPE and uses it to indicate the packet
4247 * type. Thus, if OVS_KEY_ATTR_PACKET_TYPE is present, we need
4248 * not print an all-wildcarded OVS_KEY_ATTR_ETHERNET. */
4250 ds_put_char(ds
, ',');
4252 ds_put_cstr(ds
, "eth()");
4256 ofpbuf_uninit(&ofp
);
4261 if (left
== key_len
) {
4262 ds_put_cstr(ds
, "<empty>");
4264 ds_put_format(ds
, ",***%u leftover bytes*** (", left
);
4265 for (i
= 0; i
< left
; i
++) {
4266 ds_put_format(ds
, "%02x", ((const uint8_t *) a
)[i
]);
4268 ds_put_char(ds
, ')');
4270 if (!has_ethtype_key
) {
4271 const struct nlattr
*ma
= nl_attr_find__(mask
, mask_len
,
4272 OVS_KEY_ATTR_ETHERTYPE
);
4274 ds_put_format(ds
, ",eth_type(0/0x%04"PRIx16
")",
4275 ntohs(nl_attr_get_be16(ma
)));
4279 ds_put_cstr(ds
, "<empty>");
4283 /* Appends to 'ds' a string representation of the 'key_len' bytes of
4284 * OVS_KEY_ATTR_* attributes in 'key'. */
4286 odp_flow_key_format(const struct nlattr
*key
,
4287 size_t key_len
, struct ds
*ds
)
4289 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, ds
, true);
4293 ovs_frag_type_from_string(const char *s
, enum ovs_frag_type
*type
)
4295 if (!strcasecmp(s
, "no")) {
4296 *type
= OVS_FRAG_TYPE_NONE
;
4297 } else if (!strcasecmp(s
, "first")) {
4298 *type
= OVS_FRAG_TYPE_FIRST
;
4299 } else if (!strcasecmp(s
, "later")) {
4300 *type
= OVS_FRAG_TYPE_LATER
;
4310 scan_eth(const char *s
, struct eth_addr
*key
, struct eth_addr
*mask
)
4314 if (ovs_scan(s
, ETH_ADDR_SCAN_FMT
"%n",
4315 ETH_ADDR_SCAN_ARGS(*key
), &n
)) {
4319 if (ovs_scan(s
+ len
, "/"ETH_ADDR_SCAN_FMT
"%n",
4320 ETH_ADDR_SCAN_ARGS(*mask
), &n
)) {
4323 memset(mask
, 0xff, sizeof *mask
);
4332 scan_ipv4(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4336 if (ovs_scan(s
, IP_SCAN_FMT
"%n", IP_SCAN_ARGS(key
), &n
)) {
4340 if (ovs_scan(s
+ len
, "/"IP_SCAN_FMT
"%n",
4341 IP_SCAN_ARGS(mask
), &n
)) {
4344 *mask
= OVS_BE32_MAX
;
4353 scan_in6_addr(const char *s
, struct in6_addr
*key
, struct in6_addr
*mask
)
4356 char ipv6_s
[IPV6_SCAN_LEN
+ 1];
4358 if (ovs_scan(s
, IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4359 && inet_pton(AF_INET6
, ipv6_s
, key
) == 1) {
4363 if (ovs_scan(s
+ len
, "/"IPV6_SCAN_FMT
"%n", ipv6_s
, &n
)
4364 && inet_pton(AF_INET6
, ipv6_s
, mask
) == 1) {
4367 memset(mask
, 0xff, sizeof *mask
);
4376 scan_ipv6_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4381 if (ovs_scan(s
, "%i%n", &key_
, &n
)
4382 && (key_
& ~IPV6_LABEL_MASK
) == 0) {
4387 if (ovs_scan(s
+ len
, "/%i%n", &mask_
, &n
)
4388 && (mask_
& ~IPV6_LABEL_MASK
) == 0) {
4390 *mask
= htonl(mask_
);
4392 *mask
= htonl(IPV6_LABEL_MASK
);
4401 scan_u8(const char *s
, uint8_t *key
, uint8_t *mask
)
4405 if (ovs_scan(s
, "%"SCNi8
"%n", key
, &n
)) {
4409 if (ovs_scan(s
+ len
, "/%"SCNi8
"%n", mask
, &n
)) {
4421 scan_u16(const char *s
, uint16_t *key
, uint16_t *mask
)
4425 if (ovs_scan(s
, "%"SCNi16
"%n", key
, &n
)) {
4429 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", mask
, &n
)) {
4441 scan_u32(const char *s
, uint32_t *key
, uint32_t *mask
)
4445 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4449 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4461 scan_be16(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4463 uint16_t key_
, mask_
;
4466 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4471 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4473 *mask
= htons(mask_
);
4475 *mask
= OVS_BE16_MAX
;
4484 scan_be32(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4486 uint32_t key_
, mask_
;
4489 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4494 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4496 *mask
= htonl(mask_
);
4498 *mask
= OVS_BE32_MAX
;
4507 scan_be64(const char *s
, ovs_be64
*key
, ovs_be64
*mask
)
4509 uint64_t key_
, mask_
;
4512 if (ovs_scan(s
, "%"SCNi64
"%n", &key_
, &n
)) {
4515 *key
= htonll(key_
);
4517 if (ovs_scan(s
+ len
, "/%"SCNi64
"%n", &mask_
, &n
)) {
4519 *mask
= htonll(mask_
);
4521 *mask
= OVS_BE64_MAX
;
4530 scan_tun_flags(const char *s
, uint16_t *key
, uint16_t *mask
)
4532 uint32_t flags
, fmask
;
4535 n
= parse_odp_flags(s
, flow_tun_flag_to_string
, &flags
,
4536 FLOW_TNL_F_MASK
, mask
? &fmask
: NULL
);
4537 if (n
>= 0 && s
[n
] == ')') {
4548 scan_tcp_flags(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4550 uint32_t flags
, fmask
;
4553 n
= parse_odp_flags(s
, packet_tcp_flag_to_string
, &flags
,
4554 TCP_FLAGS(OVS_BE16_MAX
), mask
? &fmask
: NULL
);
4556 *key
= htons(flags
);
4558 *mask
= htons(fmask
);
4566 ovs_to_odp_ct_state(uint8_t state
)
4570 #define CS_STATE(ENUM, INDEX, NAME) \
4571 if (state & CS_##ENUM) { \
4572 odp |= OVS_CS_F_##ENUM; \
4581 odp_to_ovs_ct_state(uint32_t flags
)
4585 #define CS_STATE(ENUM, INDEX, NAME) \
4586 if (flags & OVS_CS_F_##ENUM) { \
4587 state |= CS_##ENUM; \
4596 scan_ct_state(const char *s
, uint32_t *key
, uint32_t *mask
)
4598 uint32_t flags
, fmask
;
4601 n
= parse_flags(s
, odp_ct_state_to_string
, ')', NULL
, NULL
, &flags
,
4602 ovs_to_odp_ct_state(CS_SUPPORTED_MASK
),
4603 mask
? &fmask
: NULL
);
4616 scan_frag(const char *s
, uint8_t *key
, uint8_t *mask
)
4620 enum ovs_frag_type frag_type
;
4622 if (ovs_scan(s
, "%7[a-z]%n", frag
, &n
)
4623 && ovs_frag_type_from_string(frag
, &frag_type
)) {
4636 scan_port(const char *s
, uint32_t *key
, uint32_t *mask
,
4637 const struct simap
*port_names
)
4641 if (ovs_scan(s
, "%"SCNi32
"%n", key
, &n
)) {
4645 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", mask
, &n
)) {
4652 } else if (port_names
) {
4653 const struct simap_node
*node
;
4656 len
= strcspn(s
, ")");
4657 node
= simap_find_len(port_names
, s
, len
);
4670 /* Helper for vlan parsing. */
4671 struct ovs_key_vlan__
{
4676 set_be16_bf(ovs_be16
*bf
, uint8_t bits
, uint8_t offset
, uint16_t value
)
4678 const uint16_t mask
= ((1U << bits
) - 1) << offset
;
4680 if (value
>> bits
) {
4684 *bf
= htons((ntohs(*bf
) & ~mask
) | (value
<< offset
));
4689 scan_be16_bf(const char *s
, ovs_be16
*key
, ovs_be16
*mask
, uint8_t bits
,
4692 uint16_t key_
, mask_
;
4695 if (ovs_scan(s
, "%"SCNi16
"%n", &key_
, &n
)) {
4698 if (set_be16_bf(key
, bits
, offset
, key_
)) {
4700 if (ovs_scan(s
+ len
, "/%"SCNi16
"%n", &mask_
, &n
)) {
4703 if (!set_be16_bf(mask
, bits
, offset
, mask_
)) {
4707 *mask
|= htons(((1U << bits
) - 1) << offset
);
4717 scan_vid(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4719 return scan_be16_bf(s
, key
, mask
, 12, VLAN_VID_SHIFT
);
4723 scan_pcp(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4725 return scan_be16_bf(s
, key
, mask
, 3, VLAN_PCP_SHIFT
);
4729 scan_cfi(const char *s
, ovs_be16
*key
, ovs_be16
*mask
)
4731 return scan_be16_bf(s
, key
, mask
, 1, VLAN_CFI_SHIFT
);
4736 set_be32_bf(ovs_be32
*bf
, uint8_t bits
, uint8_t offset
, uint32_t value
)
4738 const uint32_t mask
= ((1U << bits
) - 1) << offset
;
4740 if (value
>> bits
) {
4744 *bf
= htonl((ntohl(*bf
) & ~mask
) | (value
<< offset
));
4749 scan_be32_bf(const char *s
, ovs_be32
*key
, ovs_be32
*mask
, uint8_t bits
,
4752 uint32_t key_
, mask_
;
4755 if (ovs_scan(s
, "%"SCNi32
"%n", &key_
, &n
)) {
4758 if (set_be32_bf(key
, bits
, offset
, key_
)) {
4760 if (ovs_scan(s
+ len
, "/%"SCNi32
"%n", &mask_
, &n
)) {
4763 if (!set_be32_bf(mask
, bits
, offset
, mask_
)) {
4767 *mask
|= htonl(((1U << bits
) - 1) << offset
);
4777 scan_mpls_label(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4779 return scan_be32_bf(s
, key
, mask
, 20, MPLS_LABEL_SHIFT
);
4783 scan_mpls_tc(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4785 return scan_be32_bf(s
, key
, mask
, 3, MPLS_TC_SHIFT
);
4789 scan_mpls_ttl(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4791 return scan_be32_bf(s
, key
, mask
, 8, MPLS_TTL_SHIFT
);
4795 scan_mpls_bos(const char *s
, ovs_be32
*key
, ovs_be32
*mask
)
4797 return scan_be32_bf(s
, key
, mask
, 1, MPLS_BOS_SHIFT
);
4801 scan_vxlan_gbp(const char *s
, uint32_t *key
, uint32_t *mask
)
4803 const char *s_base
= s
;
4804 ovs_be16 id
= 0, id_mask
= 0;
4805 uint8_t flags
= 0, flags_mask
= 0;
4807 if (!strncmp(s
, "id=", 3)) {
4809 s
+= scan_be16(s
, &id
, mask
? &id_mask
: NULL
);
4815 if (!strncmp(s
, "flags=", 6)) {
4817 s
+= scan_u8(s
, &flags
, mask
? &flags_mask
: NULL
);
4820 if (!strncmp(s
, "))", 2)) {
4823 *key
= (flags
<< 16) | ntohs(id
);
4825 *mask
= (flags_mask
<< 16) | ntohs(id_mask
);
4835 scan_erspan_metadata(const char *s
,
4836 struct erspan_metadata
*key
,
4837 struct erspan_metadata
*mask
)
4839 const char *s_base
= s
;
4840 uint32_t idx
= 0, idx_mask
= 0;
4841 uint8_t ver
= 0, dir
= 0, hwid
= 0;
4842 uint8_t ver_mask
= 0, dir_mask
= 0, hwid_mask
= 0;
4844 if (!strncmp(s
, "ver=", 4)) {
4846 s
+= scan_u8(s
, &ver
, mask
? &ver_mask
: NULL
);
4854 if (!strncmp(s
, "idx=", 4)) {
4856 s
+= scan_u32(s
, &idx
, mask
? &idx_mask
: NULL
);
4859 if (!strncmp(s
, ")", 1)) {
4862 key
->u
.index
= htonl(idx
);
4864 mask
->u
.index
= htonl(idx_mask
);
4869 } else if (ver
== 2) {
4870 if (!strncmp(s
, "dir=", 4)) {
4872 s
+= scan_u8(s
, &dir
, mask
? &dir_mask
: NULL
);
4877 if (!strncmp(s
, "hwid=", 5)) {
4879 s
+= scan_u8(s
, &hwid
, mask
? &hwid_mask
: NULL
);
4882 if (!strncmp(s
, ")", 1)) {
4885 key
->u
.md2
.hwid
= hwid
;
4886 key
->u
.md2
.dir
= dir
;
4888 mask
->u
.md2
.hwid
= hwid_mask
;
4889 mask
->u
.md2
.dir
= dir_mask
;
4899 scan_geneve(const char *s
, struct geneve_scan
*key
, struct geneve_scan
*mask
)
4901 const char *s_base
= s
;
4902 struct geneve_opt
*opt
= key
->d
;
4903 struct geneve_opt
*opt_mask
= mask
? mask
->d
: NULL
;
4904 int len_remain
= sizeof key
->d
;
4906 while (s
[0] == '{' && len_remain
>= sizeof *opt
) {
4910 len_remain
-= sizeof *opt
;
4912 if (!strncmp(s
, "class=", 6)) {
4914 s
+= scan_be16(s
, &opt
->opt_class
,
4915 mask
? &opt_mask
->opt_class
: NULL
);
4917 memset(&opt_mask
->opt_class
, 0, sizeof opt_mask
->opt_class
);
4923 if (!strncmp(s
, "type=", 5)) {
4925 s
+= scan_u8(s
, &opt
->type
, mask
? &opt_mask
->type
: NULL
);
4927 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4933 if (!strncmp(s
, "len=", 4)) {
4934 uint8_t opt_len
, opt_len_mask
;
4936 s
+= scan_u8(s
, &opt_len
, mask
? &opt_len_mask
: NULL
);
4938 if (opt_len
> 124 || opt_len
% 4 || opt_len
> len_remain
) {
4941 opt
->length
= opt_len
/ 4;
4943 opt_mask
->length
= opt_len_mask
;
4947 memset(&opt_mask
->type
, 0, sizeof opt_mask
->type
);
4953 if (parse_int_string(s
, (uint8_t *)(opt
+ 1), data_len
, (char **)&s
)) {
4960 if (parse_int_string(s
, (uint8_t *)(opt_mask
+ 1),
4961 data_len
, (char **)&s
)) {
4972 opt
+= 1 + data_len
/ 4;
4974 opt_mask
+= 1 + data_len
/ 4;
4976 len_remain
-= data_len
;
4981 int len
= sizeof key
->d
- len_remain
;
4995 tun_flags_to_attr(struct ofpbuf
*a
, const void *data_
)
4997 const uint16_t *flags
= data_
;
4999 if (*flags
& FLOW_TNL_F_DONT_FRAGMENT
) {
5000 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
);
5002 if (*flags
& FLOW_TNL_F_CSUM
) {
5003 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_CSUM
);
5005 if (*flags
& FLOW_TNL_F_OAM
) {
5006 nl_msg_put_flag(a
, OVS_TUNNEL_KEY_ATTR_OAM
);
5011 vxlan_gbp_to_attr(struct ofpbuf
*a
, const void *data_
)
5013 const uint32_t *gbp
= data_
;
5016 size_t vxlan_opts_ofs
;
5018 vxlan_opts_ofs
= nl_msg_start_nested(a
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
5019 nl_msg_put_u32(a
, OVS_VXLAN_EXT_GBP
, *gbp
);
5020 nl_msg_end_nested(a
, vxlan_opts_ofs
);
5025 geneve_to_attr(struct ofpbuf
*a
, const void *data_
)
5027 const struct geneve_scan
*geneve
= data_
;
5029 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
, geneve
->d
,
5034 erspan_to_attr(struct ofpbuf
*a
, const void *data_
)
5036 const struct erspan_metadata
*md
= data_
;
5038 nl_msg_put_unspec(a
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
, md
,
5042 #define SCAN_PUT_ATTR(BUF, ATTR, DATA, FUNC) \
5044 unsigned long call_fn = (unsigned long)FUNC; \
5046 typedef void (*fn)(struct ofpbuf *, const void *); \
5048 func(BUF, &(DATA)); \
5050 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)); \
5054 #define SCAN_IF(NAME) \
5055 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5056 const char *start = s; \
5061 /* Usually no special initialization is needed. */
5062 #define SCAN_BEGIN(NAME, TYPE) \
5065 memset(&skey, 0, sizeof skey); \
5066 memset(&smask, 0, sizeof smask); \
5070 /* Init as fully-masked as mask will not be scanned. */
5071 #define SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) \
5074 memset(&skey, 0, sizeof skey); \
5075 memset(&smask, 0xff, sizeof smask); \
5079 /* VLAN needs special initialization. */
5080 #define SCAN_BEGIN_INIT(NAME, TYPE, KEY_INIT, MASK_INIT) \
5082 TYPE skey = KEY_INIT; \
5083 TYPE smask = MASK_INIT; \
5087 /* Scan unnamed entry as 'TYPE' */
5088 #define SCAN_TYPE(TYPE, KEY, MASK) \
5089 len = scan_##TYPE(s, KEY, MASK); \
5095 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5096 #define SCAN_FIELD(NAME, TYPE, FIELD) \
5097 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5098 s += strlen(NAME); \
5099 SCAN_TYPE(TYPE, &skey.FIELD, mask ? &smask.FIELD : NULL); \
5103 #define SCAN_FINISH() \
5104 } while (*s++ == ',' && len != 0); \
5105 if (s[-1] != ')') { \
5109 #define SCAN_FINISH_SINGLE() \
5111 if (*s++ != ')') { \
5115 /* Beginning of nested attribute. */
5116 #define SCAN_BEGIN_NESTED(NAME, ATTR) \
5118 size_t key_offset, mask_offset; \
5119 key_offset = nl_msg_start_nested(key, ATTR); \
5121 mask_offset = nl_msg_start_nested(mask, ATTR); \
5126 #define SCAN_END_NESTED() \
5128 nl_msg_end_nested(key, key_offset); \
5130 nl_msg_end_nested(mask, mask_offset); \
5135 #define SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, FUNC) \
5136 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5138 memset(&skey, 0, sizeof skey); \
5139 memset(&smask, 0xff, sizeof smask); \
5140 s += strlen(NAME); \
5141 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5142 SCAN_PUT(ATTR, FUNC); \
5146 #define SCAN_FIELD_NESTED(NAME, TYPE, SCAN_AS, ATTR) \
5147 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, ATTR, NULL)
5149 #define SCAN_FIELD_NESTED_FUNC(NAME, TYPE, SCAN_AS, FUNC) \
5150 SCAN_FIELD_NESTED__(NAME, TYPE, SCAN_AS, 0, FUNC)
5152 #define SCAN_PUT(ATTR, FUNC) \
5153 SCAN_PUT_ATTR(key, ATTR, skey, FUNC); \
5155 SCAN_PUT_ATTR(mask, ATTR, smask, FUNC); \
5157 #define SCAN_END(ATTR) \
5159 SCAN_PUT(ATTR, NULL); \
5163 #define SCAN_BEGIN_ARRAY(NAME, TYPE, CNT) \
5165 TYPE skey[CNT], smask[CNT]; \
5166 memset(&skey, 0, sizeof skey); \
5167 memset(&smask, 0, sizeof smask); \
5168 int idx = 0, cnt = CNT; \
5169 uint64_t fields = 0; \
5174 /* Scan named ('NAME') entry 'FIELD' as 'TYPE'. */
5175 #define SCAN_FIELD_ARRAY(NAME, TYPE, FIELD) \
5176 if (strncmp(s, NAME, strlen(NAME)) == 0) { \
5177 if (fields & (1UL << field)) { \
5179 if (++idx == cnt) { \
5183 s += strlen(NAME); \
5184 SCAN_TYPE(TYPE, &skey[idx].FIELD, mask ? &smask[idx].FIELD : NULL); \
5185 fields |= 1UL << field; \
5190 #define SCAN_PUT_ATTR_ARRAY(BUF, ATTR, DATA, CNT) \
5191 nl_msg_put_unspec(BUF, ATTR, &(DATA), sizeof (DATA)[0] * (CNT)); \
5193 #define SCAN_PUT_ARRAY(ATTR, CNT) \
5194 SCAN_PUT_ATTR_ARRAY(key, ATTR, skey, CNT); \
5196 SCAN_PUT_ATTR_ARRAY(mask, ATTR, smask, CNT); \
5199 #define SCAN_END_ARRAY(ATTR) \
5204 SCAN_PUT_ARRAY(ATTR, idx + 1); \
5208 #define SCAN_END_SINGLE(ATTR) \
5209 SCAN_FINISH_SINGLE(); \
5210 SCAN_PUT(ATTR, NULL); \
5214 #define SCAN_SINGLE(NAME, TYPE, SCAN_AS, ATTR) \
5215 SCAN_BEGIN(NAME, TYPE) { \
5216 SCAN_TYPE(SCAN_AS, &skey, &smask); \
5217 } SCAN_END_SINGLE(ATTR)
5219 #define SCAN_SINGLE_FULLY_MASKED(NAME, TYPE, SCAN_AS, ATTR) \
5220 SCAN_BEGIN_FULLY_MASKED(NAME, TYPE) { \
5221 SCAN_TYPE(SCAN_AS, &skey, NULL); \
5222 } SCAN_END_SINGLE(ATTR)
5224 /* scan_port needs one extra argument. */
5225 #define SCAN_SINGLE_PORT(NAME, TYPE, ATTR) \
5226 SCAN_BEGIN(NAME, TYPE) { \
5227 len = scan_port(s, &skey, &smask, port_names); \
5232 } SCAN_END_SINGLE(ATTR)
5235 parse_odp_nsh_key_mask_attr(const char *s
, struct ofpbuf
*key
,
5236 struct ofpbuf
*mask
)
5238 if (strncmp(s
, "nsh(", 4) == 0) {
5239 const char *start
= s
;
5241 struct ovs_key_nsh skey
, smask
;
5242 uint32_t spi
= 0, spi_mask
= 0;
5243 uint8_t si
= 0, si_mask
= 0;
5247 memset(&skey
, 0, sizeof skey
);
5248 memset(&smask
, 0, sizeof smask
);
5252 if (strncmp(s
, "flags=", 6) == 0) {
5254 len
= scan_u8(s
, &skey
.flags
, mask
? &smask
.flags
: NULL
);
5262 if (strncmp(s
, "mdtype=", 7) == 0) {
5264 len
= scan_u8(s
, &skey
.mdtype
, mask
? &smask
.mdtype
: NULL
);
5272 if (strncmp(s
, "np=", 3) == 0) {
5274 len
= scan_u8(s
, &skey
.np
, mask
? &smask
.np
: NULL
);
5282 if (strncmp(s
, "spi=", 4) == 0) {
5284 len
= scan_u32(s
, &spi
, mask
? &spi_mask
: NULL
);
5292 if (strncmp(s
, "si=", 3) == 0) {
5294 len
= scan_u8(s
, &si
, mask
? &si_mask
: NULL
);
5302 if (strncmp(s
, "c1=", 3) == 0) {
5304 len
= scan_be32(s
, &skey
.context
[0],
5305 mask
? &smask
.context
[0] : NULL
);
5313 if (strncmp(s
, "c2=", 3) == 0) {
5315 len
= scan_be32(s
, &skey
.context
[1],
5316 mask
? &smask
.context
[1] : NULL
);
5324 if (strncmp(s
, "c3=", 3) == 0) {
5326 len
= scan_be32(s
, &skey
.context
[2],
5327 mask
? &smask
.context
[2] : NULL
);
5335 if (strncmp(s
, "c4=", 3) == 0) {
5337 len
= scan_be32(s
, &skey
.context
[3],
5338 mask
? &smask
.context
[3] : NULL
);
5345 } while (*s
++ == ',' && len
!= 0);
5350 skey
.path_hdr
= nsh_spi_si_to_path_hdr(spi
, si
);
5351 smask
.path_hdr
= nsh_spi_si_to_path_hdr(spi_mask
, si_mask
);
5353 nsh_key_to_attr(key
, &skey
, NULL
, 0, false);
5355 nsh_key_to_attr(mask
, &smask
, NULL
, 0, true);
5363 parse_odp_key_mask_attr(const char *s
, const struct simap
*port_names
,
5364 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5368 int ufid_len
= odp_ufid_from_string(s
, &ufid
);
5373 SCAN_SINGLE("skb_priority(", uint32_t, u32
, OVS_KEY_ATTR_PRIORITY
);
5374 SCAN_SINGLE("skb_mark(", uint32_t, u32
, OVS_KEY_ATTR_SKB_MARK
);
5375 SCAN_SINGLE_FULLY_MASKED("recirc_id(", uint32_t, u32
,
5376 OVS_KEY_ATTR_RECIRC_ID
);
5377 SCAN_SINGLE("dp_hash(", uint32_t, u32
, OVS_KEY_ATTR_DP_HASH
);
5379 SCAN_SINGLE("ct_state(", uint32_t, ct_state
, OVS_KEY_ATTR_CT_STATE
);
5380 SCAN_SINGLE("ct_zone(", uint16_t, u16
, OVS_KEY_ATTR_CT_ZONE
);
5381 SCAN_SINGLE("ct_mark(", uint32_t, u32
, OVS_KEY_ATTR_CT_MARK
);
5382 SCAN_SINGLE("ct_label(", ovs_u128
, u128
, OVS_KEY_ATTR_CT_LABELS
);
5384 SCAN_BEGIN("ct_tuple4(", struct ovs_key_ct_tuple_ipv4
) {
5385 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5386 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5387 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5388 SCAN_FIELD("tp_src=", be16
, src_port
);
5389 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5390 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
5392 SCAN_BEGIN("ct_tuple6(", struct ovs_key_ct_tuple_ipv6
) {
5393 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5394 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5395 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5396 SCAN_FIELD("tp_src=", be16
, src_port
);
5397 SCAN_FIELD("tp_dst=", be16
, dst_port
);
5398 } SCAN_END(OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
5400 SCAN_BEGIN_NESTED("tunnel(", OVS_KEY_ATTR_TUNNEL
) {
5401 SCAN_FIELD_NESTED("tun_id=", ovs_be64
, be64
, OVS_TUNNEL_KEY_ATTR_ID
);
5402 SCAN_FIELD_NESTED("src=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
);
5403 SCAN_FIELD_NESTED("dst=", ovs_be32
, ipv4
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
);
5404 SCAN_FIELD_NESTED("ipv6_src=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
);
5405 SCAN_FIELD_NESTED("ipv6_dst=", struct in6_addr
, in6_addr
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
);
5406 SCAN_FIELD_NESTED("tos=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TOS
);
5407 SCAN_FIELD_NESTED("ttl=", uint8_t, u8
, OVS_TUNNEL_KEY_ATTR_TTL
);
5408 SCAN_FIELD_NESTED("tp_src=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_SRC
);
5409 SCAN_FIELD_NESTED("tp_dst=", ovs_be16
, be16
, OVS_TUNNEL_KEY_ATTR_TP_DST
);
5410 SCAN_FIELD_NESTED_FUNC("erspan(", struct erspan_metadata
, erspan_metadata
,
5412 SCAN_FIELD_NESTED_FUNC("vxlan(gbp(", uint32_t, vxlan_gbp
, vxlan_gbp_to_attr
);
5413 SCAN_FIELD_NESTED_FUNC("geneve(", struct geneve_scan
, geneve
,
5415 SCAN_FIELD_NESTED_FUNC("flags(", uint16_t, tun_flags
, tun_flags_to_attr
);
5416 } SCAN_END_NESTED();
5418 SCAN_SINGLE_PORT("in_port(", uint32_t, OVS_KEY_ATTR_IN_PORT
);
5420 SCAN_BEGIN("eth(", struct ovs_key_ethernet
) {
5421 SCAN_FIELD("src=", eth
, eth_src
);
5422 SCAN_FIELD("dst=", eth
, eth_dst
);
5423 } SCAN_END(OVS_KEY_ATTR_ETHERNET
);
5425 SCAN_BEGIN_INIT("vlan(", struct ovs_key_vlan__
,
5426 { htons(VLAN_CFI
) }, { htons(VLAN_CFI
) }) {
5427 SCAN_FIELD("vid=", vid
, tci
);
5428 SCAN_FIELD("pcp=", pcp
, tci
);
5429 SCAN_FIELD("cfi=", cfi
, tci
);
5430 } SCAN_END(OVS_KEY_ATTR_VLAN
);
5432 SCAN_SINGLE("eth_type(", ovs_be16
, be16
, OVS_KEY_ATTR_ETHERTYPE
);
5434 SCAN_BEGIN_ARRAY("mpls(", struct ovs_key_mpls
, FLOW_MAX_MPLS_LABELS
) {
5435 SCAN_FIELD_ARRAY("label=", mpls_label
, mpls_lse
);
5436 SCAN_FIELD_ARRAY("tc=", mpls_tc
, mpls_lse
);
5437 SCAN_FIELD_ARRAY("ttl=", mpls_ttl
, mpls_lse
);
5438 SCAN_FIELD_ARRAY("bos=", mpls_bos
, mpls_lse
);
5439 } SCAN_END_ARRAY(OVS_KEY_ATTR_MPLS
);
5441 SCAN_BEGIN("ipv4(", struct ovs_key_ipv4
) {
5442 SCAN_FIELD("src=", ipv4
, ipv4_src
);
5443 SCAN_FIELD("dst=", ipv4
, ipv4_dst
);
5444 SCAN_FIELD("proto=", u8
, ipv4_proto
);
5445 SCAN_FIELD("tos=", u8
, ipv4_tos
);
5446 SCAN_FIELD("ttl=", u8
, ipv4_ttl
);
5447 SCAN_FIELD("frag=", frag
, ipv4_frag
);
5448 } SCAN_END(OVS_KEY_ATTR_IPV4
);
5450 SCAN_BEGIN("ipv6(", struct ovs_key_ipv6
) {
5451 SCAN_FIELD("src=", in6_addr
, ipv6_src
);
5452 SCAN_FIELD("dst=", in6_addr
, ipv6_dst
);
5453 SCAN_FIELD("label=", ipv6_label
, ipv6_label
);
5454 SCAN_FIELD("proto=", u8
, ipv6_proto
);
5455 SCAN_FIELD("tclass=", u8
, ipv6_tclass
);
5456 SCAN_FIELD("hlimit=", u8
, ipv6_hlimit
);
5457 SCAN_FIELD("frag=", frag
, ipv6_frag
);
5458 } SCAN_END(OVS_KEY_ATTR_IPV6
);
5460 SCAN_BEGIN("tcp(", struct ovs_key_tcp
) {
5461 SCAN_FIELD("src=", be16
, tcp_src
);
5462 SCAN_FIELD("dst=", be16
, tcp_dst
);
5463 } SCAN_END(OVS_KEY_ATTR_TCP
);
5465 SCAN_SINGLE("tcp_flags(", ovs_be16
, tcp_flags
, OVS_KEY_ATTR_TCP_FLAGS
);
5467 SCAN_BEGIN("udp(", struct ovs_key_udp
) {
5468 SCAN_FIELD("src=", be16
, udp_src
);
5469 SCAN_FIELD("dst=", be16
, udp_dst
);
5470 } SCAN_END(OVS_KEY_ATTR_UDP
);
5472 SCAN_BEGIN("sctp(", struct ovs_key_sctp
) {
5473 SCAN_FIELD("src=", be16
, sctp_src
);
5474 SCAN_FIELD("dst=", be16
, sctp_dst
);
5475 } SCAN_END(OVS_KEY_ATTR_SCTP
);
5477 SCAN_BEGIN("icmp(", struct ovs_key_icmp
) {
5478 SCAN_FIELD("type=", u8
, icmp_type
);
5479 SCAN_FIELD("code=", u8
, icmp_code
);
5480 } SCAN_END(OVS_KEY_ATTR_ICMP
);
5482 SCAN_BEGIN("icmpv6(", struct ovs_key_icmpv6
) {
5483 SCAN_FIELD("type=", u8
, icmpv6_type
);
5484 SCAN_FIELD("code=", u8
, icmpv6_code
);
5485 } SCAN_END(OVS_KEY_ATTR_ICMPV6
);
5487 SCAN_BEGIN("arp(", struct ovs_key_arp
) {
5488 SCAN_FIELD("sip=", ipv4
, arp_sip
);
5489 SCAN_FIELD("tip=", ipv4
, arp_tip
);
5490 SCAN_FIELD("op=", be16
, arp_op
);
5491 SCAN_FIELD("sha=", eth
, arp_sha
);
5492 SCAN_FIELD("tha=", eth
, arp_tha
);
5493 } SCAN_END(OVS_KEY_ATTR_ARP
);
5495 SCAN_BEGIN("nd(", struct ovs_key_nd
) {
5496 SCAN_FIELD("target=", in6_addr
, nd_target
);
5497 SCAN_FIELD("sll=", eth
, nd_sll
);
5498 SCAN_FIELD("tll=", eth
, nd_tll
);
5499 } SCAN_END(OVS_KEY_ATTR_ND
);
5501 struct packet_type
{
5505 SCAN_BEGIN("packet_type(", struct packet_type
) {
5506 SCAN_FIELD("ns=", be16
, ns
);
5507 SCAN_FIELD("id=", be16
, id
);
5508 } SCAN_END(OVS_KEY_ATTR_PACKET_TYPE
);
5510 /* nsh is nested, it needs special process */
5511 int ret
= parse_odp_nsh_key_mask_attr(s
, key
, mask
);
5518 /* Encap open-coded. */
5519 if (!strncmp(s
, "encap(", 6)) {
5520 const char *start
= s
;
5521 size_t encap
, encap_mask
= 0;
5523 encap
= nl_msg_start_nested(key
, OVS_KEY_ATTR_ENCAP
);
5525 encap_mask
= nl_msg_start_nested(mask
, OVS_KEY_ATTR_ENCAP
);
5532 s
+= strspn(s
, delimiters
);
5535 } else if (*s
== ')') {
5539 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
5547 nl_msg_end_nested(key
, encap
);
5549 nl_msg_end_nested(mask
, encap_mask
);
5558 /* Parses the string representation of a datapath flow key, in the
5559 * format output by odp_flow_key_format(). Returns 0 if successful,
5560 * otherwise a positive errno value. On success, the flow key is
5561 * appended to 'key' as a series of Netlink attributes. On failure, no
5562 * data is appended to 'key'. Either way, 'key''s data might be
5565 * If 'port_names' is nonnull, it points to an simap that maps from a port name
5566 * to a port number. (Port names may be used instead of port numbers in
5569 * On success, the attributes appended to 'key' are individually syntactically
5570 * valid, but they may not be valid as a sequence. 'key' might, for example,
5571 * have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
5573 odp_flow_from_string(const char *s
, const struct simap
*port_names
,
5574 struct ofpbuf
*key
, struct ofpbuf
*mask
)
5576 const size_t old_size
= key
->size
;
5580 s
+= strspn(s
, delimiters
);
5585 retval
= parse_odp_key_mask_attr(s
, port_names
, key
, mask
);
5587 key
->size
= old_size
;
5597 ovs_to_odp_frag(uint8_t nw_frag
, bool is_mask
)
5600 /* Netlink interface 'enum ovs_frag_type' is an 8-bit enumeration type,
5601 * not a set of flags or bitfields. Hence, if the struct flow nw_frag
5602 * mask, which is a set of bits, has the FLOW_NW_FRAG_ANY as zero, we
5603 * must use a zero mask for the netlink frag field, and all ones mask
5605 return (nw_frag
& FLOW_NW_FRAG_ANY
) ? UINT8_MAX
: 0;
5607 return !(nw_frag
& FLOW_NW_FRAG_ANY
) ? OVS_FRAG_TYPE_NONE
5608 : nw_frag
& FLOW_NW_FRAG_LATER
? OVS_FRAG_TYPE_LATER
5609 : OVS_FRAG_TYPE_FIRST
;
5612 static void get_ethernet_key(const struct flow
*, struct ovs_key_ethernet
*);
5613 static void put_ethernet_key(const struct ovs_key_ethernet
*, struct flow
*);
5614 static void get_ipv4_key(const struct flow
*, struct ovs_key_ipv4
*,
5616 static void put_ipv4_key(const struct ovs_key_ipv4
*, struct flow
*,
5618 static void get_ipv6_key(const struct flow
*, struct ovs_key_ipv6
*,
5620 static void put_ipv6_key(const struct ovs_key_ipv6
*, struct flow
*,
5622 static void get_arp_key(const struct flow
*, struct ovs_key_arp
*);
5623 static void put_arp_key(const struct ovs_key_arp
*, struct flow
*);
5624 static void get_nd_key(const struct flow
*, struct ovs_key_nd
*);
5625 static void put_nd_key(const struct ovs_key_nd
*, struct flow
*);
5626 static void get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
,
5628 static void put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
5631 /* These share the same layout. */
5633 struct ovs_key_tcp tcp
;
5634 struct ovs_key_udp udp
;
5635 struct ovs_key_sctp sctp
;
5638 static void get_tp_key(const struct flow
*, union ovs_key_tp
*);
5639 static void put_tp_key(const union ovs_key_tp
*, struct flow
*);
5642 odp_flow_key_from_flow__(const struct odp_flow_key_parms
*parms
,
5643 bool export_mask
, struct ofpbuf
*buf
)
5645 struct ovs_key_ethernet
*eth_key
;
5646 size_t encap
[FLOW_MAX_VLAN_HEADERS
] = {0};
5648 const struct flow
*flow
= parms
->flow
;
5649 const struct flow
*mask
= parms
->mask
;
5650 const struct flow
*data
= export_mask
? mask
: flow
;
5652 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, data
->skb_priority
);
5654 if (flow_tnl_dst_is_set(&flow
->tunnel
) || export_mask
) {
5655 tun_key_to_attr(buf
, &data
->tunnel
, &parms
->flow
->tunnel
,
5656 parms
->key_buf
, NULL
);
5659 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, data
->pkt_mark
);
5661 if (parms
->support
.ct_state
) {
5662 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5663 ovs_to_odp_ct_state(data
->ct_state
));
5665 if (parms
->support
.ct_zone
) {
5666 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, data
->ct_zone
);
5668 if (parms
->support
.ct_mark
) {
5669 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, data
->ct_mark
);
5671 if (parms
->support
.ct_label
) {
5672 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &data
->ct_label
,
5673 sizeof(data
->ct_label
));
5675 if (flow
->ct_nw_proto
) {
5676 if (parms
->support
.ct_orig_tuple
5677 && flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5678 struct ovs_key_ct_tuple_ipv4 ct
= {
5685 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
, &ct
,
5687 } else if (parms
->support
.ct_orig_tuple6
5688 && flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5689 struct ovs_key_ct_tuple_ipv6 ct
= {
5696 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
, &ct
,
5700 if (parms
->support
.recirc
) {
5701 nl_msg_put_u32(buf
, OVS_KEY_ATTR_RECIRC_ID
, data
->recirc_id
);
5702 nl_msg_put_u32(buf
, OVS_KEY_ATTR_DP_HASH
, data
->dp_hash
);
5705 /* Add an ingress port attribute if this is a mask or 'in_port.odp_port'
5706 * is not the magical value "ODPP_NONE". */
5707 if (export_mask
|| flow
->in_port
.odp_port
!= ODPP_NONE
) {
5708 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, data
->in_port
.odp_port
);
5711 nl_msg_put_be32(buf
, OVS_KEY_ATTR_PACKET_TYPE
, data
->packet_type
);
5713 if (OVS_UNLIKELY(parms
->probe
)) {
5714 max_vlans
= FLOW_MAX_VLAN_HEADERS
;
5716 max_vlans
= MIN(parms
->support
.max_vlan_headers
, flow_vlan_limit
);
5719 /* Conditionally add L2 attributes for Ethernet packets */
5720 if (flow
->packet_type
== htonl(PT_ETH
)) {
5721 eth_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ETHERNET
,
5723 get_ethernet_key(data
, eth_key
);
5725 for (int encaps
= 0; encaps
< max_vlans
; encaps
++) {
5726 ovs_be16 tpid
= flow
->vlans
[encaps
].tpid
;
5728 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5729 if (eth_type_vlan(flow
->dl_type
)) {
5730 /* If VLAN was truncated the tpid is in dl_type */
5731 tpid
= flow
->dl_type
;
5738 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5740 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, tpid
);
5742 nl_msg_put_be16(buf
, OVS_KEY_ATTR_VLAN
, data
->vlans
[encaps
].tci
);
5743 encap
[encaps
] = nl_msg_start_nested(buf
, OVS_KEY_ATTR_ENCAP
);
5744 if (flow
->vlans
[encaps
].tci
== htons(0)) {
5750 if (ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
5751 /* For backwards compatibility with kernels that don't support
5752 * wildcarding, the following convention is used to encode the
5753 * OVS_KEY_ATTR_ETHERTYPE for key and mask:
5756 * -------- -------- -------
5757 * >0x5ff 0xffff Specified Ethernet II Ethertype.
5758 * >0x5ff 0 Any Ethernet II or non-Ethernet II frame.
5759 * <none> 0xffff Any non-Ethernet II frame (except valid
5760 * 802.3 SNAP packet with valid eth_type).
5763 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, OVS_BE16_MAX
);
5768 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
, data
->dl_type
);
5770 if (eth_type_vlan(flow
->dl_type
)) {
5774 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
5775 struct ovs_key_ipv4
*ipv4_key
;
5777 ipv4_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV4
,
5779 get_ipv4_key(data
, ipv4_key
, export_mask
);
5780 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
5781 struct ovs_key_ipv6
*ipv6_key
;
5783 ipv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_IPV6
,
5785 get_ipv6_key(data
, ipv6_key
, export_mask
);
5786 } else if (flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
5787 flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
5788 struct ovs_key_arp
*arp_key
;
5790 arp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ARP
,
5792 get_arp_key(data
, arp_key
);
5793 } else if (eth_type_mpls(flow
->dl_type
)) {
5794 struct ovs_key_mpls
*mpls_key
;
5797 n
= flow_count_mpls_labels(flow
, NULL
);
5799 n
= MIN(n
, parms
->support
.max_mpls_depth
);
5801 mpls_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_MPLS
,
5802 n
* sizeof *mpls_key
);
5803 for (i
= 0; i
< n
; i
++) {
5804 mpls_key
[i
].mpls_lse
= data
->mpls_lse
[i
];
5806 } else if (flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
5807 nsh_key_to_attr(buf
, &data
->nsh
, NULL
, 0, export_mask
);
5810 if (is_ip_any(flow
) && !(flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
5811 if (flow
->nw_proto
== IPPROTO_TCP
) {
5812 union ovs_key_tp
*tcp_key
;
5814 tcp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_TCP
,
5816 get_tp_key(data
, tcp_key
);
5817 if (data
->tcp_flags
|| (mask
&& mask
->tcp_flags
)) {
5818 nl_msg_put_be16(buf
, OVS_KEY_ATTR_TCP_FLAGS
, data
->tcp_flags
);
5820 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
5821 union ovs_key_tp
*udp_key
;
5823 udp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_UDP
,
5825 get_tp_key(data
, udp_key
);
5826 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
5827 union ovs_key_tp
*sctp_key
;
5829 sctp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_SCTP
,
5831 get_tp_key(data
, sctp_key
);
5832 } else if (flow
->dl_type
== htons(ETH_TYPE_IP
)
5833 && flow
->nw_proto
== IPPROTO_ICMP
) {
5834 struct ovs_key_icmp
*icmp_key
;
5836 icmp_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMP
,
5838 icmp_key
->icmp_type
= ntohs(data
->tp_src
);
5839 icmp_key
->icmp_code
= ntohs(data
->tp_dst
);
5840 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)
5841 && flow
->nw_proto
== IPPROTO_ICMPV6
) {
5842 struct ovs_key_icmpv6
*icmpv6_key
;
5844 icmpv6_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ICMPV6
,
5845 sizeof *icmpv6_key
);
5846 icmpv6_key
->icmpv6_type
= ntohs(data
->tp_src
);
5847 icmpv6_key
->icmpv6_code
= ntohs(data
->tp_dst
);
5849 if (is_nd(flow
, NULL
)
5850 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide, ICMP
5851 * type and code are 8 bits wide. Therefore, an exact match
5852 * looks like htons(0xff), not htons(0xffff). See
5853 * xlate_wc_finish() for details. */
5854 && (!export_mask
|| (data
->tp_src
== htons(0xff)
5855 && data
->tp_dst
== htons(0xff)))) {
5857 struct ovs_key_nd
*nd_key
;
5859 nd_key
= nl_msg_put_unspec_uninit(buf
, OVS_KEY_ATTR_ND
,
5861 nd_key
->nd_target
= data
->nd_target
;
5862 nd_key
->nd_sll
= data
->arp_sha
;
5863 nd_key
->nd_tll
= data
->arp_tha
;
5869 for (int encaps
= max_vlans
- 1; encaps
>= 0; encaps
--) {
5870 if (encap
[encaps
]) {
5871 nl_msg_end_nested(buf
, encap
[encaps
]);
5876 /* Appends a representation of 'flow' as OVS_KEY_ATTR_* attributes to 'buf'.
5878 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5879 * capable of being expanded to allow for that much space. */
5881 odp_flow_key_from_flow(const struct odp_flow_key_parms
*parms
,
5884 odp_flow_key_from_flow__(parms
, false, buf
);
5887 /* Appends a representation of 'mask' as OVS_KEY_ATTR_* attributes to
5890 * 'buf' must have at least ODPUTIL_FLOW_KEY_BYTES bytes of space, or be
5891 * capable of being expanded to allow for that much space. */
5893 odp_flow_key_from_mask(const struct odp_flow_key_parms
*parms
,
5896 odp_flow_key_from_flow__(parms
, true, buf
);
5899 /* Generate ODP flow key from the given packet metadata */
5901 odp_key_from_dp_packet(struct ofpbuf
*buf
, const struct dp_packet
*packet
)
5903 const struct pkt_metadata
*md
= &packet
->md
;
5905 nl_msg_put_u32(buf
, OVS_KEY_ATTR_PRIORITY
, md
->skb_priority
);
5907 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
5908 tun_key_to_attr(buf
, &md
->tunnel
, &md
->tunnel
, NULL
, NULL
);
5911 nl_msg_put_u32(buf
, OVS_KEY_ATTR_SKB_MARK
, md
->pkt_mark
);
5914 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_STATE
,
5915 ovs_to_odp_ct_state(md
->ct_state
));
5917 nl_msg_put_u16(buf
, OVS_KEY_ATTR_CT_ZONE
, md
->ct_zone
);
5920 nl_msg_put_u32(buf
, OVS_KEY_ATTR_CT_MARK
, md
->ct_mark
);
5922 if (!ovs_u128_is_zero(md
->ct_label
)) {
5923 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_LABELS
, &md
->ct_label
,
5924 sizeof(md
->ct_label
));
5926 if (md
->ct_orig_tuple_ipv6
) {
5927 if (md
->ct_orig_tuple
.ipv6
.ipv6_proto
) {
5928 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
,
5929 &md
->ct_orig_tuple
.ipv6
,
5930 sizeof md
->ct_orig_tuple
.ipv6
);
5933 if (md
->ct_orig_tuple
.ipv4
.ipv4_proto
) {
5934 nl_msg_put_unspec(buf
, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
,
5935 &md
->ct_orig_tuple
.ipv4
,
5936 sizeof md
->ct_orig_tuple
.ipv4
);
5941 /* Add an ingress port attribute if 'odp_in_port' is not the magical
5942 * value "ODPP_NONE". */
5943 if (md
->in_port
.odp_port
!= ODPP_NONE
) {
5944 nl_msg_put_odp_port(buf
, OVS_KEY_ATTR_IN_PORT
, md
->in_port
.odp_port
);
5947 /* Add OVS_KEY_ATTR_ETHERNET for non-Ethernet packets */
5948 if (pt_ns(packet
->packet_type
) == OFPHTN_ETHERTYPE
) {
5949 nl_msg_put_be16(buf
, OVS_KEY_ATTR_ETHERTYPE
,
5950 pt_ns_type_be(packet
->packet_type
));
5954 /* Generate packet metadata from the given ODP flow key. */
5956 odp_key_to_dp_packet(const struct nlattr
*key
, size_t key_len
,
5957 struct dp_packet
*packet
)
5959 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
5960 const struct nlattr
*nla
;
5961 struct pkt_metadata
*md
= &packet
->md
;
5962 ovs_be32 packet_type
= htonl(PT_UNKNOWN
);
5963 ovs_be16 ethertype
= 0;
5966 pkt_metadata_init(md
, ODPP_NONE
);
5968 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
5969 enum ovs_key_attr type
= nl_attr_type(nla
);
5970 size_t len
= nl_attr_get_size(nla
);
5971 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
5972 OVS_KEY_ATTR_MAX
, type
);
5974 if (len
!= expected_len
&& expected_len
>= 0) {
5979 case OVS_KEY_ATTR_RECIRC_ID
:
5980 md
->recirc_id
= nl_attr_get_u32(nla
);
5982 case OVS_KEY_ATTR_DP_HASH
:
5983 md
->dp_hash
= nl_attr_get_u32(nla
);
5985 case OVS_KEY_ATTR_PRIORITY
:
5986 md
->skb_priority
= nl_attr_get_u32(nla
);
5988 case OVS_KEY_ATTR_SKB_MARK
:
5989 md
->pkt_mark
= nl_attr_get_u32(nla
);
5991 case OVS_KEY_ATTR_CT_STATE
:
5992 md
->ct_state
= odp_to_ovs_ct_state(nl_attr_get_u32(nla
));
5994 case OVS_KEY_ATTR_CT_ZONE
:
5995 md
->ct_zone
= nl_attr_get_u16(nla
);
5997 case OVS_KEY_ATTR_CT_MARK
:
5998 md
->ct_mark
= nl_attr_get_u32(nla
);
6000 case OVS_KEY_ATTR_CT_LABELS
: {
6001 md
->ct_label
= nl_attr_get_u128(nla
);
6004 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
: {
6005 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(nla
);
6006 md
->ct_orig_tuple
.ipv4
= *ct
;
6007 md
->ct_orig_tuple_ipv6
= false;
6010 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
: {
6011 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(nla
);
6013 md
->ct_orig_tuple
.ipv6
= *ct
;
6014 md
->ct_orig_tuple_ipv6
= true;
6017 case OVS_KEY_ATTR_TUNNEL
: {
6018 enum odp_key_fitness res
;
6020 res
= odp_tun_key_from_attr(nla
, &md
->tunnel
);
6021 if (res
== ODP_FIT_ERROR
) {
6022 memset(&md
->tunnel
, 0, sizeof md
->tunnel
);
6026 case OVS_KEY_ATTR_IN_PORT
:
6027 md
->in_port
.odp_port
= nl_attr_get_odp_port(nla
);
6029 case OVS_KEY_ATTR_ETHERNET
:
6030 /* Presence of OVS_KEY_ATTR_ETHERNET indicates Ethernet packet. */
6031 packet_type
= htonl(PT_ETH
);
6033 case OVS_KEY_ATTR_ETHERTYPE
:
6034 ethertype
= nl_attr_get_be16(nla
);
6036 case OVS_KEY_ATTR_UNSPEC
:
6037 case OVS_KEY_ATTR_ENCAP
:
6038 case OVS_KEY_ATTR_VLAN
:
6039 case OVS_KEY_ATTR_IPV4
:
6040 case OVS_KEY_ATTR_IPV6
:
6041 case OVS_KEY_ATTR_TCP
:
6042 case OVS_KEY_ATTR_UDP
:
6043 case OVS_KEY_ATTR_ICMP
:
6044 case OVS_KEY_ATTR_ICMPV6
:
6045 case OVS_KEY_ATTR_ARP
:
6046 case OVS_KEY_ATTR_ND
:
6047 case OVS_KEY_ATTR_SCTP
:
6048 case OVS_KEY_ATTR_TCP_FLAGS
:
6049 case OVS_KEY_ATTR_MPLS
:
6050 case OVS_KEY_ATTR_PACKET_TYPE
:
6051 case OVS_KEY_ATTR_NSH
:
6052 case __OVS_KEY_ATTR_MAX
:
6058 if (packet_type
== htonl(PT_ETH
)) {
6059 packet
->packet_type
= htonl(PT_ETH
);
6060 } else if (packet_type
== htonl(PT_UNKNOWN
) && ethertype
!= 0) {
6061 packet
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6064 VLOG_ERR_RL(&rl
, "Packet without ETHERTYPE. Unknown packet_type.");
6069 odp_flow_key_hash(const struct nlattr
*key
, size_t key_len
)
6071 BUILD_ASSERT_DECL(!(NLA_ALIGNTO
% sizeof(uint32_t)));
6072 return hash_bytes32(ALIGNED_CAST(const uint32_t *, key
), key_len
, 0);
6076 log_odp_key_attributes(struct vlog_rate_limit
*rl
, const char *title
,
6077 uint64_t attrs
, int out_of_range_attr
,
6078 const struct nlattr
*key
, size_t key_len
)
6083 if (VLOG_DROP_DBG(rl
)) {
6088 for (i
= 0; i
< 64; i
++) {
6089 if (attrs
& (UINT64_C(1) << i
)) {
6090 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6092 ds_put_format(&s
, " %s",
6093 ovs_key_attr_to_string(i
, namebuf
, sizeof namebuf
));
6096 if (out_of_range_attr
) {
6097 ds_put_format(&s
, " %d (and possibly others)", out_of_range_attr
);
6100 ds_put_cstr(&s
, ": ");
6101 odp_flow_key_format(key
, key_len
, &s
);
6103 VLOG_DBG("%s:%s", title
, ds_cstr(&s
));
6108 odp_to_ovs_frag(uint8_t odp_frag
, bool is_mask
)
6110 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6113 return odp_frag
? FLOW_NW_FRAG_MASK
: 0;
6116 if (odp_frag
> OVS_FRAG_TYPE_LATER
) {
6117 VLOG_ERR_RL(&rl
, "invalid frag %"PRIu8
" in flow key", odp_frag
);
6118 return 0xff; /* Error. */
6121 return (odp_frag
== OVS_FRAG_TYPE_NONE
) ? 0
6122 : (odp_frag
== OVS_FRAG_TYPE_FIRST
) ? FLOW_NW_FRAG_ANY
6123 : FLOW_NW_FRAG_ANY
| FLOW_NW_FRAG_LATER
;
6127 parse_flow_nlattrs(const struct nlattr
*key
, size_t key_len
,
6128 const struct nlattr
*attrs
[], uint64_t *present_attrsp
,
6129 int *out_of_range_attrp
)
6131 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6132 const struct nlattr
*nla
;
6133 uint64_t present_attrs
;
6136 BUILD_ASSERT(OVS_KEY_ATTR_MAX
< CHAR_BIT
* sizeof present_attrs
);
6138 *out_of_range_attrp
= 0;
6139 NL_ATTR_FOR_EACH (nla
, left
, key
, key_len
) {
6140 uint16_t type
= nl_attr_type(nla
);
6141 size_t len
= nl_attr_get_size(nla
);
6142 int expected_len
= odp_key_attr_len(ovs_flow_key_attr_lens
,
6143 OVS_KEY_ATTR_MAX
, type
);
6145 if (len
!= expected_len
&& expected_len
>= 0) {
6146 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6148 VLOG_ERR_RL(&rl
, "attribute %s has length %"PRIuSIZE
" but should have "
6149 "length %d", ovs_key_attr_to_string(type
, namebuf
,
6155 if (type
> OVS_KEY_ATTR_MAX
) {
6156 *out_of_range_attrp
= type
;
6158 if (present_attrs
& (UINT64_C(1) << type
)) {
6159 char namebuf
[OVS_KEY_ATTR_BUFSIZE
];
6161 VLOG_ERR_RL(&rl
, "duplicate %s attribute in flow key",
6162 ovs_key_attr_to_string(type
,
6163 namebuf
, sizeof namebuf
));
6167 present_attrs
|= UINT64_C(1) << type
;
6172 VLOG_ERR_RL(&rl
, "trailing garbage in flow key");
6176 *present_attrsp
= present_attrs
;
6180 static enum odp_key_fitness
6181 check_expectations(uint64_t present_attrs
, int out_of_range_attr
,
6182 uint64_t expected_attrs
,
6183 const struct nlattr
*key
, size_t key_len
)
6185 uint64_t missing_attrs
;
6186 uint64_t extra_attrs
;
6188 missing_attrs
= expected_attrs
& ~present_attrs
;
6189 if (missing_attrs
) {
6190 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6191 log_odp_key_attributes(&rl
, "expected but not present",
6192 missing_attrs
, 0, key
, key_len
);
6193 return ODP_FIT_TOO_LITTLE
;
6196 extra_attrs
= present_attrs
& ~expected_attrs
;
6197 if (extra_attrs
|| out_of_range_attr
) {
6198 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(10, 10);
6199 log_odp_key_attributes(&rl
, "present but not expected",
6200 extra_attrs
, out_of_range_attr
, key
, key_len
);
6201 return ODP_FIT_TOO_MUCH
;
6204 return ODP_FIT_PERFECT
;
6208 parse_ethertype(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6209 uint64_t present_attrs
, uint64_t *expected_attrs
,
6210 struct flow
*flow
, const struct flow
*src_flow
)
6212 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6213 bool is_mask
= flow
!= src_flow
;
6215 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6216 flow
->dl_type
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6217 if (!is_mask
&& ntohs(flow
->dl_type
) < ETH_TYPE_MIN
) {
6218 VLOG_ERR_RL(&rl
, "invalid Ethertype %"PRIu16
" in flow key",
6219 ntohs(flow
->dl_type
));
6222 if (is_mask
&& ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
&&
6223 flow
->dl_type
!= htons(0xffff)) {
6226 *expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6229 /* Default ethertype for well-known L3 packets. */
6230 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6231 flow
->dl_type
= htons(ETH_TYPE_IP
);
6232 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6233 flow
->dl_type
= htons(ETH_TYPE_IPV6
);
6234 } else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6235 flow
->dl_type
= htons(ETH_TYPE_MPLS
);
6237 flow
->dl_type
= htons(FLOW_DL_TYPE_NONE
);
6239 } else if (src_flow
->packet_type
!= htonl(PT_ETH
)) {
6240 /* dl_type is mandatory for non-Ethernet packets */
6241 flow
->dl_type
= htons(0xffff);
6242 } else if (ntohs(src_flow
->dl_type
) < ETH_TYPE_MIN
) {
6243 /* See comments in odp_flow_key_from_flow__(). */
6244 VLOG_ERR_RL(&rl
, "mask expected for non-Ethernet II frame");
6251 static enum odp_key_fitness
6252 parse_l2_5_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6253 uint64_t present_attrs
, int out_of_range_attr
,
6254 uint64_t expected_attrs
, struct flow
*flow
,
6255 const struct nlattr
*key
, size_t key_len
,
6256 const struct flow
*src_flow
)
6258 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6259 bool is_mask
= src_flow
!= flow
;
6260 const void *check_start
= NULL
;
6261 size_t check_len
= 0;
6262 enum ovs_key_attr expected_bit
= 0xff;
6264 if (eth_type_mpls(src_flow
->dl_type
)) {
6265 if (!is_mask
|| present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6266 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_MPLS
);
6268 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_MPLS
)) {
6269 size_t size
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_MPLS
]);
6270 const ovs_be32
*mpls_lse
= nl_attr_get(attrs
[OVS_KEY_ATTR_MPLS
]);
6271 int n
= size
/ sizeof(ovs_be32
);
6274 if (!size
|| size
% sizeof(ovs_be32
)) {
6275 return ODP_FIT_ERROR
;
6277 if (flow
->mpls_lse
[0] && flow
->dl_type
!= htons(0xffff)) {
6278 return ODP_FIT_ERROR
;
6281 for (i
= 0; i
< n
&& i
< FLOW_MAX_MPLS_LABELS
; i
++) {
6282 flow
->mpls_lse
[i
] = mpls_lse
[i
];
6284 if (n
> FLOW_MAX_MPLS_LABELS
) {
6285 return ODP_FIT_TOO_MUCH
;
6289 /* BOS may be set only in the innermost label. */
6290 for (i
= 0; i
< n
- 1; i
++) {
6291 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
6292 return ODP_FIT_ERROR
;
6296 /* BOS must be set in the innermost label. */
6297 if (n
< FLOW_MAX_MPLS_LABELS
6298 && !(flow
->mpls_lse
[n
- 1] & htonl(MPLS_BOS_MASK
))) {
6299 return ODP_FIT_TOO_LITTLE
;
6305 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6307 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV4
;
6309 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV4
)) {
6310 const struct ovs_key_ipv4
*ipv4_key
;
6312 ipv4_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV4
]);
6313 put_ipv4_key(ipv4_key
, flow
, is_mask
);
6314 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6315 return ODP_FIT_ERROR
;
6318 check_start
= ipv4_key
;
6319 check_len
= sizeof *ipv4_key
;
6320 expected_bit
= OVS_KEY_ATTR_IPV4
;
6323 } else if (src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
6325 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IPV6
;
6327 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IPV6
)) {
6328 const struct ovs_key_ipv6
*ipv6_key
;
6330 ipv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_IPV6
]);
6331 put_ipv6_key(ipv6_key
, flow
, is_mask
);
6332 if (flow
->nw_frag
> FLOW_NW_FRAG_MASK
) {
6333 return ODP_FIT_ERROR
;
6336 check_start
= ipv6_key
;
6337 check_len
= sizeof *ipv6_key
;
6338 expected_bit
= OVS_KEY_ATTR_IPV6
;
6341 } else if (src_flow
->dl_type
== htons(ETH_TYPE_ARP
) ||
6342 src_flow
->dl_type
== htons(ETH_TYPE_RARP
)) {
6344 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ARP
;
6346 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ARP
)) {
6347 const struct ovs_key_arp
*arp_key
;
6349 arp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ARP
]);
6350 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
6351 VLOG_ERR_RL(&rl
, "unsupported ARP opcode %"PRIu16
" in flow "
6352 "key", ntohs(arp_key
->arp_op
));
6353 return ODP_FIT_ERROR
;
6355 put_arp_key(arp_key
, flow
);
6357 check_start
= arp_key
;
6358 check_len
= sizeof *arp_key
;
6359 expected_bit
= OVS_KEY_ATTR_ARP
;
6362 } else if (src_flow
->dl_type
== htons(ETH_TYPE_NSH
)) {
6364 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_NSH
;
6366 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_NSH
)) {
6367 odp_nsh_key_from_attr(attrs
[OVS_KEY_ATTR_NSH
], &flow
->nsh
, NULL
);
6369 check_start
= nl_attr_get(attrs
[OVS_KEY_ATTR_NSH
]);
6370 check_len
= nl_attr_get_size(attrs
[OVS_KEY_ATTR_NSH
]);
6371 expected_bit
= OVS_KEY_ATTR_NSH
;
6377 if (check_len
> 0) { /* Happens only when 'is_mask'. */
6378 if (!is_all_zeros(check_start
, check_len
) &&
6379 flow
->dl_type
!= htons(0xffff)) {
6380 return ODP_FIT_ERROR
;
6382 expected_attrs
|= UINT64_C(1) << expected_bit
;
6386 expected_bit
= OVS_KEY_ATTR_UNSPEC
;
6387 if (src_flow
->nw_proto
== IPPROTO_TCP
6388 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6389 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6390 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6392 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP
;
6394 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP
)) {
6395 const union ovs_key_tp
*tcp_key
;
6397 tcp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_TCP
]);
6398 put_tp_key(tcp_key
, flow
);
6399 expected_bit
= OVS_KEY_ATTR_TCP
;
6401 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
)) {
6402 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TCP_FLAGS
;
6403 flow
->tcp_flags
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_TCP_FLAGS
]);
6405 } else if (src_flow
->nw_proto
== IPPROTO_UDP
6406 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6407 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6408 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6410 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_UDP
;
6412 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_UDP
)) {
6413 const union ovs_key_tp
*udp_key
;
6415 udp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_UDP
]);
6416 put_tp_key(udp_key
, flow
);
6417 expected_bit
= OVS_KEY_ATTR_UDP
;
6419 } else if (src_flow
->nw_proto
== IPPROTO_SCTP
6420 && (src_flow
->dl_type
== htons(ETH_TYPE_IP
) ||
6421 src_flow
->dl_type
== htons(ETH_TYPE_IPV6
))
6422 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6424 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SCTP
;
6426 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SCTP
)) {
6427 const union ovs_key_tp
*sctp_key
;
6429 sctp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_SCTP
]);
6430 put_tp_key(sctp_key
, flow
);
6431 expected_bit
= OVS_KEY_ATTR_SCTP
;
6433 } else if (src_flow
->nw_proto
== IPPROTO_ICMP
6434 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)
6435 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6437 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMP
;
6439 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMP
)) {
6440 const struct ovs_key_icmp
*icmp_key
;
6442 icmp_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMP
]);
6443 flow
->tp_src
= htons(icmp_key
->icmp_type
);
6444 flow
->tp_dst
= htons(icmp_key
->icmp_code
);
6445 expected_bit
= OVS_KEY_ATTR_ICMP
;
6447 } else if (src_flow
->nw_proto
== IPPROTO_ICMPV6
6448 && src_flow
->dl_type
== htons(ETH_TYPE_IPV6
)
6449 && !(src_flow
->nw_frag
& FLOW_NW_FRAG_LATER
)) {
6451 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
;
6453 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ICMPV6
)) {
6454 const struct ovs_key_icmpv6
*icmpv6_key
;
6456 icmpv6_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ICMPV6
]);
6457 flow
->tp_src
= htons(icmpv6_key
->icmpv6_type
);
6458 flow
->tp_dst
= htons(icmpv6_key
->icmpv6_code
);
6459 expected_bit
= OVS_KEY_ATTR_ICMPV6
;
6460 if (is_nd(src_flow
, NULL
)) {
6462 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6464 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ND
)) {
6465 const struct ovs_key_nd
*nd_key
;
6467 nd_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ND
]);
6468 flow
->nd_target
= nd_key
->nd_target
;
6469 flow
->arp_sha
= nd_key
->nd_sll
;
6470 flow
->arp_tha
= nd_key
->nd_tll
;
6472 /* Even though 'tp_src' and 'tp_dst' are 16 bits wide,
6473 * ICMP type and code are 8 bits wide. Therefore, an
6474 * exact match looks like htons(0xff), not
6475 * htons(0xffff). See xlate_wc_finish() for details.
6477 if (!is_all_zeros(nd_key
, sizeof *nd_key
) &&
6478 (flow
->tp_src
!= htons(0xff) ||
6479 flow
->tp_dst
!= htons(0xff))) {
6480 return ODP_FIT_ERROR
;
6482 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ND
;
6488 } else if (src_flow
->nw_proto
== IPPROTO_IGMP
6489 && src_flow
->dl_type
== htons(ETH_TYPE_IP
)) {
6490 /* OVS userspace parses the IGMP type, code, and group, but its
6491 * datapaths do not, so there is always missing information. */
6492 return ODP_FIT_TOO_LITTLE
;
6494 if (is_mask
&& expected_bit
!= OVS_KEY_ATTR_UNSPEC
) {
6495 if ((flow
->tp_src
|| flow
->tp_dst
) && flow
->nw_proto
!= 0xff) {
6496 return ODP_FIT_ERROR
;
6498 expected_attrs
|= UINT64_C(1) << expected_bit
;
6503 return check_expectations(present_attrs
, out_of_range_attr
, expected_attrs
,
6507 /* Parse 802.1Q header then encapsulated L3 attributes. */
6508 static enum odp_key_fitness
6509 parse_8021q_onward(const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1],
6510 uint64_t present_attrs
, int out_of_range_attr
,
6511 uint64_t expected_attrs
, struct flow
*flow
,
6512 const struct nlattr
*key
, size_t key_len
,
6513 const struct flow
*src_flow
)
6515 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6516 bool is_mask
= src_flow
!= flow
;
6518 const struct nlattr
*encap
;
6519 enum odp_key_fitness encap_fitness
;
6520 enum odp_key_fitness fitness
= ODP_FIT_ERROR
;
6523 while (encaps
< flow_vlan_limit
&&
6525 ? (src_flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
)) != 0
6526 : eth_type_vlan(flow
->dl_type
))) {
6528 encap
= (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)
6529 ? attrs
[OVS_KEY_ATTR_ENCAP
] : NULL
);
6531 /* Calculate fitness of outer attributes. */
6533 expected_attrs
|= ((UINT64_C(1) << OVS_KEY_ATTR_VLAN
) |
6534 (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
));
6536 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6537 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6539 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
)) {
6540 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
);
6543 fitness
= check_expectations(present_attrs
, out_of_range_attr
,
6544 expected_attrs
, key
, key_len
);
6547 * Remove the TPID from dl_type since it's not the real Ethertype. */
6548 flow
->vlans
[encaps
].tpid
= flow
->dl_type
;
6549 flow
->dl_type
= htons(0);
6550 flow
->vlans
[encaps
].tci
=
6551 (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)
6552 ? nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
])
6555 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) ||
6556 !(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6557 return ODP_FIT_TOO_LITTLE
;
6558 } else if (flow
->vlans
[encaps
].tci
== htons(0)) {
6559 /* Corner case for a truncated 802.1Q header. */
6560 if (fitness
== ODP_FIT_PERFECT
&& nl_attr_get_size(encap
)) {
6561 return ODP_FIT_TOO_MUCH
;
6564 } else if (!(flow
->vlans
[encaps
].tci
& htons(VLAN_CFI
))) {
6565 VLOG_ERR_RL(&rl
, "OVS_KEY_ATTR_VLAN 0x%04"PRIx16
" is nonzero "
6566 "but CFI bit is not set",
6567 ntohs(flow
->vlans
[encaps
].tci
));
6568 return ODP_FIT_ERROR
;
6571 if (!(present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ENCAP
))) {
6576 /* Now parse the encapsulated attributes. */
6577 if (!parse_flow_nlattrs(nl_attr_get(encap
), nl_attr_get_size(encap
),
6578 attrs
, &present_attrs
, &out_of_range_attr
)) {
6579 return ODP_FIT_ERROR
;
6583 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
,
6585 return ODP_FIT_ERROR
;
6591 encap_fitness
= parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
6592 expected_attrs
, flow
, key
, key_len
,
6595 /* The overall fitness is the worse of the outer and inner attributes. */
6596 return MAX(fitness
, encap_fitness
);
6599 static enum odp_key_fitness
6600 odp_flow_key_to_flow__(const struct nlattr
*key
, size_t key_len
,
6601 struct flow
*flow
, const struct flow
*src_flow
)
6603 const struct nlattr
*attrs
[OVS_KEY_ATTR_MAX
+ 1];
6604 uint64_t expected_attrs
;
6605 uint64_t present_attrs
;
6606 int out_of_range_attr
;
6607 bool is_mask
= src_flow
!= flow
;
6609 memset(flow
, 0, sizeof *flow
);
6611 /* Parse attributes. */
6612 if (!parse_flow_nlattrs(key
, key_len
, attrs
, &present_attrs
,
6613 &out_of_range_attr
)) {
6614 return ODP_FIT_ERROR
;
6619 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
)) {
6620 flow
->recirc_id
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_RECIRC_ID
]);
6621 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_RECIRC_ID
;
6622 } else if (is_mask
) {
6623 /* Always exact match recirc_id if it is not specified. */
6624 flow
->recirc_id
= UINT32_MAX
;
6627 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
)) {
6628 flow
->dp_hash
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_DP_HASH
]);
6629 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_DP_HASH
;
6631 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
)) {
6632 flow
->skb_priority
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_PRIORITY
]);
6633 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PRIORITY
;
6636 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
)) {
6637 flow
->pkt_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_SKB_MARK
]);
6638 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_SKB_MARK
;
6641 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
)) {
6642 uint32_t odp_state
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_STATE
]);
6644 flow
->ct_state
= odp_to_ovs_ct_state(odp_state
);
6645 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_STATE
;
6647 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
)) {
6648 flow
->ct_zone
= nl_attr_get_u16(attrs
[OVS_KEY_ATTR_CT_ZONE
]);
6649 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ZONE
;
6651 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
)) {
6652 flow
->ct_mark
= nl_attr_get_u32(attrs
[OVS_KEY_ATTR_CT_MARK
]);
6653 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_MARK
;
6655 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
)) {
6656 flow
->ct_label
= nl_attr_get_u128(attrs
[OVS_KEY_ATTR_CT_LABELS
]);
6657 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_LABELS
;
6659 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
6660 const struct ovs_key_ct_tuple_ipv4
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
6661 flow
->ct_nw_src
= ct
->ipv4_src
;
6662 flow
->ct_nw_dst
= ct
->ipv4_dst
;
6663 flow
->ct_nw_proto
= ct
->ipv4_proto
;
6664 flow
->ct_tp_src
= ct
->src_port
;
6665 flow
->ct_tp_dst
= ct
->dst_port
;
6666 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
6668 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
6669 const struct ovs_key_ct_tuple_ipv6
*ct
= nl_attr_get(attrs
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
6671 flow
->ct_ipv6_src
= ct
->ipv6_src
;
6672 flow
->ct_ipv6_dst
= ct
->ipv6_dst
;
6673 flow
->ct_nw_proto
= ct
->ipv6_proto
;
6674 flow
->ct_tp_src
= ct
->src_port
;
6675 flow
->ct_tp_dst
= ct
->dst_port
;
6676 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
6679 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
)) {
6680 enum odp_key_fitness res
;
6682 res
= odp_tun_key_from_attr__(attrs
[OVS_KEY_ATTR_TUNNEL
], is_mask
,
6684 if (res
== ODP_FIT_ERROR
) {
6685 return ODP_FIT_ERROR
;
6686 } else if (res
== ODP_FIT_PERFECT
) {
6687 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_TUNNEL
;
6691 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
)) {
6692 flow
->in_port
.odp_port
6693 = nl_attr_get_odp_port(attrs
[OVS_KEY_ATTR_IN_PORT
]);
6694 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_IN_PORT
;
6695 } else if (!is_mask
) {
6696 flow
->in_port
.odp_port
= ODPP_NONE
;
6699 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
)) {
6701 = nl_attr_get_be32(attrs
[OVS_KEY_ATTR_PACKET_TYPE
]);
6702 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_PACKET_TYPE
;
6703 } else if (!is_mask
) {
6704 flow
->packet_type
= htonl(PT_ETH
);
6707 /* Check for Ethernet header. */
6708 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
)) {
6709 const struct ovs_key_ethernet
*eth_key
;
6711 eth_key
= nl_attr_get(attrs
[OVS_KEY_ATTR_ETHERNET
]);
6712 put_ethernet_key(eth_key
, flow
);
6714 flow
->packet_type
= htonl(PT_ETH
);
6716 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERNET
;
6718 else if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
)) {
6719 ovs_be16 ethertype
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_ETHERTYPE
]);
6721 flow
->packet_type
= PACKET_TYPE_BE(OFPHTN_ETHERTYPE
,
6724 expected_attrs
|= UINT64_C(1) << OVS_KEY_ATTR_ETHERTYPE
;
6727 /* Get Ethertype or 802.1Q TPID or FLOW_DL_TYPE_NONE. */
6728 if (!parse_ethertype(attrs
, present_attrs
, &expected_attrs
, flow
,
6730 return ODP_FIT_ERROR
;
6734 ? (src_flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0
6735 : eth_type_vlan(src_flow
->dl_type
)) {
6736 return parse_8021q_onward(attrs
, present_attrs
, out_of_range_attr
,
6737 expected_attrs
, flow
, key
, key_len
, src_flow
);
6740 /* A missing VLAN mask means exact match on vlan_tci 0 (== no VLAN). */
6741 flow
->vlans
[0].tpid
= htons(0xffff);
6742 flow
->vlans
[0].tci
= htons(0xffff);
6743 if (present_attrs
& (UINT64_C(1) << OVS_KEY_ATTR_VLAN
)) {
6744 flow
->vlans
[0].tci
= nl_attr_get_be16(attrs
[OVS_KEY_ATTR_VLAN
]);
6745 expected_attrs
|= (UINT64_C(1) << OVS_KEY_ATTR_VLAN
);
6748 return parse_l2_5_onward(attrs
, present_attrs
, out_of_range_attr
,
6749 expected_attrs
, flow
, key
, key_len
, src_flow
);
6752 /* Converts the 'key_len' bytes of OVS_KEY_ATTR_* attributes in 'key' to a flow
6753 * structure in 'flow'. Returns an ODP_FIT_* value that indicates how well
6754 * 'key' fits our expectations for what a flow key should contain.
6756 * The 'in_port' will be the datapath's understanding of the port. The
6757 * caller will need to translate with odp_port_to_ofp_port() if the
6758 * OpenFlow port is needed.
6760 * This function doesn't take the packet itself as an argument because none of
6761 * the currently understood OVS_KEY_ATTR_* attributes require it. Currently,
6762 * it is always possible to infer which additional attribute(s) should appear
6763 * by looking at the attributes for lower-level protocols, e.g. if the network
6764 * protocol in OVS_KEY_ATTR_IPV4 or OVS_KEY_ATTR_IPV6 is IPPROTO_TCP then we
6765 * know that a OVS_KEY_ATTR_TCP attribute must appear and that otherwise it
6766 * must be absent. */
6767 enum odp_key_fitness
6768 odp_flow_key_to_flow(const struct nlattr
*key
, size_t key_len
,
6771 return odp_flow_key_to_flow__(key
, key_len
, flow
, flow
);
6774 /* Converts the 'mask_key_len' bytes of OVS_KEY_ATTR_* attributes in 'mask_key'
6775 * to a mask structure in 'mask'. 'flow' must be a previously translated flow
6776 * corresponding to 'mask' and similarly flow_key/flow_key_len must be the
6777 * attributes from that flow. Returns an ODP_FIT_* value that indicates how
6778 * well 'key' fits our expectations for what a flow key should contain. */
6779 enum odp_key_fitness
6780 odp_flow_key_to_mask(const struct nlattr
*mask_key
, size_t mask_key_len
,
6781 struct flow_wildcards
*mask
, const struct flow
*src_flow
)
6784 return odp_flow_key_to_flow__(mask_key
, mask_key_len
,
6785 &mask
->masks
, src_flow
);
6788 /* A missing mask means that the flow should be exact matched.
6789 * Generate an appropriate exact wildcard for the flow. */
6790 flow_wildcards_init_for_packet(mask
, src_flow
);
6792 return ODP_FIT_PERFECT
;
6796 /* Converts the netlink formated key/mask to match.
6797 * Fails if odp_flow_key_from_key/mask and odp_flow_key_key/mask
6798 * disagree on the acceptable form of flow */
6800 parse_key_and_mask_to_match(const struct nlattr
*key
, size_t key_len
,
6801 const struct nlattr
*mask
, size_t mask_len
,
6802 struct match
*match
)
6804 enum odp_key_fitness fitness
;
6806 fitness
= odp_flow_key_to_flow(key
, key_len
, &match
->flow
);
6808 /* This should not happen: it indicates that
6809 * odp_flow_key_from_flow() and odp_flow_key_to_flow() disagree on
6810 * the acceptable form of a flow. Log the problem as an error,
6811 * with enough details to enable debugging. */
6812 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6814 if (!VLOG_DROP_ERR(&rl
)) {
6818 odp_flow_format(key
, key_len
, NULL
, 0, NULL
, &s
, true);
6819 VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s
));
6826 fitness
= odp_flow_key_to_mask(mask
, mask_len
, &match
->wc
, &match
->flow
);
6828 /* This should not happen: it indicates that
6829 * odp_flow_key_from_mask() and odp_flow_key_to_mask()
6830 * disagree on the acceptable form of a mask. Log the problem
6831 * as an error, with enough details to enable debugging. */
6832 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
6834 if (!VLOG_DROP_ERR(&rl
)) {
6838 odp_flow_format(key
, key_len
, mask
, mask_len
, NULL
, &s
,
6840 VLOG_ERR("internal error parsing flow mask %s (%s)",
6841 ds_cstr(&s
), odp_key_fitness_to_string(fitness
));
6851 /* Returns 'fitness' as a string, for use in debug messages. */
6853 odp_key_fitness_to_string(enum odp_key_fitness fitness
)
6856 case ODP_FIT_PERFECT
:
6858 case ODP_FIT_TOO_MUCH
:
6860 case ODP_FIT_TOO_LITTLE
:
6861 return "too_little";
6869 /* Appends an OVS_ACTION_ATTR_USERSPACE action to 'odp_actions' that specifies
6870 * Netlink PID 'pid'. If 'userdata' is nonnull, adds a userdata attribute
6871 * whose contents are the 'userdata_size' bytes at 'userdata' and returns the
6872 * offset within 'odp_actions' of the start of the cookie. (If 'userdata' is
6873 * null, then the return value is not meaningful.) */
6875 odp_put_userspace_action(uint32_t pid
,
6876 const void *userdata
, size_t userdata_size
,
6877 odp_port_t tunnel_out_port
,
6878 bool include_actions
,
6879 struct ofpbuf
*odp_actions
)
6881 size_t userdata_ofs
;
6884 offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_USERSPACE
);
6885 nl_msg_put_u32(odp_actions
, OVS_USERSPACE_ATTR_PID
, pid
);
6887 userdata_ofs
= odp_actions
->size
+ NLA_HDRLEN
;
6889 /* The OVS kernel module before OVS 1.11 and the upstream Linux kernel
6890 * module before Linux 3.10 required the userdata to be exactly 8 bytes
6893 * - The kernel rejected shorter userdata with -ERANGE.
6895 * - The kernel silently dropped userdata beyond the first 8 bytes.
6897 * Thus, for maximum compatibility, always put at least 8 bytes. (We
6898 * separately disable features that required more than 8 bytes.) */
6899 memcpy(nl_msg_put_unspec_zero(odp_actions
, OVS_USERSPACE_ATTR_USERDATA
,
6900 MAX(8, userdata_size
)),
6901 userdata
, userdata_size
);
6905 if (tunnel_out_port
!= ODPP_NONE
) {
6906 nl_msg_put_odp_port(odp_actions
, OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
,
6909 if (include_actions
) {
6910 nl_msg_put_flag(odp_actions
, OVS_USERSPACE_ATTR_ACTIONS
);
6912 nl_msg_end_nested(odp_actions
, offset
);
6914 return userdata_ofs
;
6918 odp_put_pop_eth_action(struct ofpbuf
*odp_actions
)
6920 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_ETH
);
6924 odp_put_push_eth_action(struct ofpbuf
*odp_actions
,
6925 const struct eth_addr
*eth_src
,
6926 const struct eth_addr
*eth_dst
)
6928 struct ovs_action_push_eth eth
;
6930 memset(ð
, 0, sizeof eth
);
6932 eth
.addresses
.eth_src
= *eth_src
;
6935 eth
.addresses
.eth_dst
= *eth_dst
;
6938 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_ETH
,
6943 odp_put_tunnel_action(const struct flow_tnl
*tunnel
,
6944 struct ofpbuf
*odp_actions
, const char *tnl_type
)
6946 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6947 tun_key_to_attr(odp_actions
, tunnel
, tunnel
, NULL
, tnl_type
);
6948 nl_msg_end_nested(odp_actions
, offset
);
6952 odp_put_tnl_push_action(struct ofpbuf
*odp_actions
,
6953 struct ovs_action_push_tnl
*data
)
6955 int size
= offsetof(struct ovs_action_push_tnl
, header
);
6957 size
+= data
->header_len
;
6958 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_TUNNEL_PUSH
, data
, size
);
6962 /* The commit_odp_actions() function and its helpers. */
6965 commit_set_action(struct ofpbuf
*odp_actions
, enum ovs_key_attr key_type
,
6966 const void *key
, size_t key_size
)
6968 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
6969 nl_msg_put_unspec(odp_actions
, key_type
, key
, key_size
);
6970 nl_msg_end_nested(odp_actions
, offset
);
6973 /* Masked set actions have a mask following the data within the netlink
6974 * attribute. The unmasked bits in the data will be cleared as the data
6975 * is copied to the action. */
6977 commit_masked_set_action(struct ofpbuf
*odp_actions
,
6978 enum ovs_key_attr key_type
,
6979 const void *key_
, const void *mask_
, size_t key_size
)
6981 size_t offset
= nl_msg_start_nested(odp_actions
,
6982 OVS_ACTION_ATTR_SET_MASKED
);
6983 char *data
= nl_msg_put_unspec_uninit(odp_actions
, key_type
, key_size
* 2);
6984 const char *key
= key_
, *mask
= mask_
;
6986 memcpy(data
+ key_size
, mask
, key_size
);
6987 /* Clear unmasked bits while copying. */
6988 while (key_size
--) {
6989 *data
++ = *key
++ & *mask
++;
6991 nl_msg_end_nested(odp_actions
, offset
);
6994 /* If any of the flow key data that ODP actions can modify are different in
6995 * 'base->tunnel' and 'flow->tunnel', appends a set_tunnel ODP action to
6996 * 'odp_actions' that change the flow tunneling information in key from
6997 * 'base->tunnel' into 'flow->tunnel', and then changes 'base->tunnel' in the
6998 * same way. In other words, operates the same as commit_odp_actions(), but
6999 * only on tunneling information. */
7001 commit_odp_tunnel_action(const struct flow
*flow
, struct flow
*base
,
7002 struct ofpbuf
*odp_actions
, const char *tnl_type
)
7004 /* A valid IPV4_TUNNEL must have non-zero ip_dst; a valid IPv6 tunnel
7005 * must have non-zero ipv6_dst. */
7006 if (flow_tnl_dst_is_set(&flow
->tunnel
)) {
7007 if (!memcmp(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
)) {
7010 memcpy(&base
->tunnel
, &flow
->tunnel
, sizeof base
->tunnel
);
7011 odp_put_tunnel_action(&base
->tunnel
, odp_actions
, tnl_type
);
7016 commit(enum ovs_key_attr attr
, bool use_masked_set
,
7017 const void *key
, void *base
, void *mask
, size_t size
,
7018 struct ofpbuf
*odp_actions
)
7020 if (memcmp(key
, base
, size
)) {
7021 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7023 if (use_masked_set
&& !fully_masked
) {
7024 commit_masked_set_action(odp_actions
, attr
, key
, mask
, size
);
7026 if (!fully_masked
) {
7027 memset(mask
, 0xff, size
);
7029 commit_set_action(odp_actions
, attr
, key
, size
);
7031 memcpy(base
, key
, size
);
7034 /* Mask bits are set when we have either read or set the corresponding
7035 * values. Masked bits will be exact-matched, no need to set them
7036 * if the value did not actually change. */
7042 get_ethernet_key(const struct flow
*flow
, struct ovs_key_ethernet
*eth
)
7044 eth
->eth_src
= flow
->dl_src
;
7045 eth
->eth_dst
= flow
->dl_dst
;
7049 put_ethernet_key(const struct ovs_key_ethernet
*eth
, struct flow
*flow
)
7051 flow
->dl_src
= eth
->eth_src
;
7052 flow
->dl_dst
= eth
->eth_dst
;
7056 commit_set_ether_action(const struct flow
*flow
, struct flow
*base_flow
,
7057 struct ofpbuf
*odp_actions
,
7058 struct flow_wildcards
*wc
,
7061 struct ovs_key_ethernet key
, base
, mask
;
7063 if (flow
->packet_type
!= htonl(PT_ETH
)) {
7067 get_ethernet_key(flow
, &key
);
7068 get_ethernet_key(base_flow
, &base
);
7069 get_ethernet_key(&wc
->masks
, &mask
);
7071 if (commit(OVS_KEY_ATTR_ETHERNET
, use_masked
,
7072 &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7073 put_ethernet_key(&base
, base_flow
);
7074 put_ethernet_key(&mask
, &wc
->masks
);
7079 commit_vlan_action(const struct flow
* flow
, struct flow
*base
,
7080 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7082 int base_n
= flow_count_vlan_headers(base
);
7083 int flow_n
= flow_count_vlan_headers(flow
);
7084 flow_skip_common_vlan_headers(base
, &base_n
, flow
, &flow_n
);
7086 /* Pop all mismatching vlan of base, push those of flow */
7087 for (; base_n
>= 0; base_n
--) {
7088 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_VLAN
);
7089 wc
->masks
.vlans
[base_n
].qtag
= OVS_BE32_MAX
;
7092 for (; flow_n
>= 0; flow_n
--) {
7093 struct ovs_action_push_vlan vlan
;
7095 vlan
.vlan_tpid
= flow
->vlans
[flow_n
].tpid
;
7096 vlan
.vlan_tci
= flow
->vlans
[flow_n
].tci
;
7097 nl_msg_put_unspec(odp_actions
, OVS_ACTION_ATTR_PUSH_VLAN
,
7098 &vlan
, sizeof vlan
);
7100 memcpy(base
->vlans
, flow
->vlans
, sizeof(base
->vlans
));
7103 /* Wildcarding already done at action translation time. */
7105 commit_mpls_action(const struct flow
*flow
, struct flow
*base
,
7106 struct ofpbuf
*odp_actions
)
7108 int base_n
= flow_count_mpls_labels(base
, NULL
);
7109 int flow_n
= flow_count_mpls_labels(flow
, NULL
);
7110 int common_n
= flow_count_common_mpls_labels(flow
, flow_n
, base
, base_n
,
7113 while (base_n
> common_n
) {
7114 if (base_n
- 1 == common_n
&& flow_n
> common_n
) {
7115 /* If there is only one more LSE in base than there are common
7116 * between base and flow; and flow has at least one more LSE than
7117 * is common then the topmost LSE of base may be updated using
7119 struct ovs_key_mpls mpls_key
;
7121 mpls_key
.mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
];
7122 commit_set_action(odp_actions
, OVS_KEY_ATTR_MPLS
,
7123 &mpls_key
, sizeof mpls_key
);
7124 flow_set_mpls_lse(base
, 0, mpls_key
.mpls_lse
);
7127 /* Otherwise, if there more LSEs in base than are common between
7128 * base and flow then pop the topmost one. */
7130 /* If all the LSEs are to be popped and this is not the outermost
7131 * LSE then use ETH_TYPE_MPLS as the ethertype parameter of the
7132 * POP_MPLS action instead of flow->dl_type.
7134 * This is because the POP_MPLS action requires its ethertype
7135 * argument to be an MPLS ethernet type but in this case
7136 * flow->dl_type will be a non-MPLS ethernet type.
7138 * When the final POP_MPLS action occurs it use flow->dl_type and
7139 * the and the resulting packet will have the desired dl_type. */
7140 if ((!eth_type_mpls(flow
->dl_type
)) && base_n
> 1) {
7141 dl_type
= htons(ETH_TYPE_MPLS
);
7143 dl_type
= flow
->dl_type
;
7145 nl_msg_put_be16(odp_actions
, OVS_ACTION_ATTR_POP_MPLS
, dl_type
);
7146 ovs_assert(flow_pop_mpls(base
, base_n
, flow
->dl_type
, NULL
));
7151 /* If, after the above popping and setting, there are more LSEs in flow
7152 * than base then some LSEs need to be pushed. */
7153 while (base_n
< flow_n
) {
7154 struct ovs_action_push_mpls
*mpls
;
7156 mpls
= nl_msg_put_unspec_zero(odp_actions
,
7157 OVS_ACTION_ATTR_PUSH_MPLS
,
7159 mpls
->mpls_ethertype
= flow
->dl_type
;
7160 mpls
->mpls_lse
= flow
->mpls_lse
[flow_n
- base_n
- 1];
7161 /* Update base flow's MPLS stack, but do not clear L3. We need the L3
7162 * headers if the flow is restored later due to returning from a patch
7163 * port or group bucket. */
7164 flow_push_mpls(base
, base_n
, mpls
->mpls_ethertype
, NULL
, false);
7165 flow_set_mpls_lse(base
, 0, mpls
->mpls_lse
);
7171 get_ipv4_key(const struct flow
*flow
, struct ovs_key_ipv4
*ipv4
, bool is_mask
)
7173 ipv4
->ipv4_src
= flow
->nw_src
;
7174 ipv4
->ipv4_dst
= flow
->nw_dst
;
7175 ipv4
->ipv4_proto
= flow
->nw_proto
;
7176 ipv4
->ipv4_tos
= flow
->nw_tos
;
7177 ipv4
->ipv4_ttl
= flow
->nw_ttl
;
7178 ipv4
->ipv4_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7182 put_ipv4_key(const struct ovs_key_ipv4
*ipv4
, struct flow
*flow
, bool is_mask
)
7184 flow
->nw_src
= ipv4
->ipv4_src
;
7185 flow
->nw_dst
= ipv4
->ipv4_dst
;
7186 flow
->nw_proto
= ipv4
->ipv4_proto
;
7187 flow
->nw_tos
= ipv4
->ipv4_tos
;
7188 flow
->nw_ttl
= ipv4
->ipv4_ttl
;
7189 flow
->nw_frag
= odp_to_ovs_frag(ipv4
->ipv4_frag
, is_mask
);
7193 commit_set_ipv4_action(const struct flow
*flow
, struct flow
*base_flow
,
7194 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7197 struct ovs_key_ipv4 key
, mask
, base
;
7199 /* Check that nw_proto and nw_frag remain unchanged. */
7200 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7201 flow
->nw_frag
== base_flow
->nw_frag
);
7203 get_ipv4_key(flow
, &key
, false);
7204 get_ipv4_key(base_flow
, &base
, false);
7205 get_ipv4_key(&wc
->masks
, &mask
, true);
7206 mask
.ipv4_proto
= 0; /* Not writeable. */
7207 mask
.ipv4_frag
= 0; /* Not writable. */
7209 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7210 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7211 mask
.ipv4_tos
&= ~IP_ECN_MASK
;
7214 if (commit(OVS_KEY_ATTR_IPV4
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7216 put_ipv4_key(&base
, base_flow
, false);
7217 if (mask
.ipv4_proto
!= 0) { /* Mask was changed by commit(). */
7218 put_ipv4_key(&mask
, &wc
->masks
, true);
7224 get_ipv6_key(const struct flow
*flow
, struct ovs_key_ipv6
*ipv6
, bool is_mask
)
7226 ipv6
->ipv6_src
= flow
->ipv6_src
;
7227 ipv6
->ipv6_dst
= flow
->ipv6_dst
;
7228 ipv6
->ipv6_label
= flow
->ipv6_label
;
7229 ipv6
->ipv6_proto
= flow
->nw_proto
;
7230 ipv6
->ipv6_tclass
= flow
->nw_tos
;
7231 ipv6
->ipv6_hlimit
= flow
->nw_ttl
;
7232 ipv6
->ipv6_frag
= ovs_to_odp_frag(flow
->nw_frag
, is_mask
);
7236 put_ipv6_key(const struct ovs_key_ipv6
*ipv6
, struct flow
*flow
, bool is_mask
)
7238 flow
->ipv6_src
= ipv6
->ipv6_src
;
7239 flow
->ipv6_dst
= ipv6
->ipv6_dst
;
7240 flow
->ipv6_label
= ipv6
->ipv6_label
;
7241 flow
->nw_proto
= ipv6
->ipv6_proto
;
7242 flow
->nw_tos
= ipv6
->ipv6_tclass
;
7243 flow
->nw_ttl
= ipv6
->ipv6_hlimit
;
7244 flow
->nw_frag
= odp_to_ovs_frag(ipv6
->ipv6_frag
, is_mask
);
7248 commit_set_ipv6_action(const struct flow
*flow
, struct flow
*base_flow
,
7249 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7252 struct ovs_key_ipv6 key
, mask
, base
;
7254 /* Check that nw_proto and nw_frag remain unchanged. */
7255 ovs_assert(flow
->nw_proto
== base_flow
->nw_proto
&&
7256 flow
->nw_frag
== base_flow
->nw_frag
);
7258 get_ipv6_key(flow
, &key
, false);
7259 get_ipv6_key(base_flow
, &base
, false);
7260 get_ipv6_key(&wc
->masks
, &mask
, true);
7261 mask
.ipv6_proto
= 0; /* Not writeable. */
7262 mask
.ipv6_frag
= 0; /* Not writable. */
7264 if (flow_tnl_dst_is_set(&base_flow
->tunnel
) &&
7265 ((base_flow
->nw_tos
^ flow
->nw_tos
) & IP_ECN_MASK
) == 0) {
7266 mask
.ipv6_tclass
&= ~IP_ECN_MASK
;
7269 if (commit(OVS_KEY_ATTR_IPV6
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7271 put_ipv6_key(&base
, base_flow
, false);
7272 if (mask
.ipv6_proto
!= 0) { /* Mask was changed by commit(). */
7273 put_ipv6_key(&mask
, &wc
->masks
, true);
7279 get_arp_key(const struct flow
*flow
, struct ovs_key_arp
*arp
)
7281 /* ARP key has padding, clear it. */
7282 memset(arp
, 0, sizeof *arp
);
7284 arp
->arp_sip
= flow
->nw_src
;
7285 arp
->arp_tip
= flow
->nw_dst
;
7286 arp
->arp_op
= htons(flow
->nw_proto
);
7287 arp
->arp_sha
= flow
->arp_sha
;
7288 arp
->arp_tha
= flow
->arp_tha
;
7292 put_arp_key(const struct ovs_key_arp
*arp
, struct flow
*flow
)
7294 flow
->nw_src
= arp
->arp_sip
;
7295 flow
->nw_dst
= arp
->arp_tip
;
7296 flow
->nw_proto
= ntohs(arp
->arp_op
);
7297 flow
->arp_sha
= arp
->arp_sha
;
7298 flow
->arp_tha
= arp
->arp_tha
;
7301 static enum slow_path_reason
7302 commit_set_arp_action(const struct flow
*flow
, struct flow
*base_flow
,
7303 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7305 struct ovs_key_arp key
, mask
, base
;
7307 get_arp_key(flow
, &key
);
7308 get_arp_key(base_flow
, &base
);
7309 get_arp_key(&wc
->masks
, &mask
);
7311 if (commit(OVS_KEY_ATTR_ARP
, true, &key
, &base
, &mask
, sizeof key
,
7313 put_arp_key(&base
, base_flow
);
7314 put_arp_key(&mask
, &wc
->masks
);
7321 get_icmp_key(const struct flow
*flow
, struct ovs_key_icmp
*icmp
)
7323 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7324 icmp
->icmp_type
= ntohs(flow
->tp_src
);
7325 icmp
->icmp_code
= ntohs(flow
->tp_dst
);
7329 put_icmp_key(const struct ovs_key_icmp
*icmp
, struct flow
*flow
)
7331 /* icmp_type and icmp_code are stored in tp_src and tp_dst, respectively */
7332 flow
->tp_src
= htons(icmp
->icmp_type
);
7333 flow
->tp_dst
= htons(icmp
->icmp_code
);
7336 static enum slow_path_reason
7337 commit_set_icmp_action(const struct flow
*flow
, struct flow
*base_flow
,
7338 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
)
7340 struct ovs_key_icmp key
, mask
, base
;
7341 enum ovs_key_attr attr
;
7343 if (is_icmpv4(flow
, NULL
)) {
7344 attr
= OVS_KEY_ATTR_ICMP
;
7345 } else if (is_icmpv6(flow
, NULL
)) {
7346 attr
= OVS_KEY_ATTR_ICMPV6
;
7351 get_icmp_key(flow
, &key
);
7352 get_icmp_key(base_flow
, &base
);
7353 get_icmp_key(&wc
->masks
, &mask
);
7355 if (commit(attr
, false, &key
, &base
, &mask
, sizeof key
, odp_actions
)) {
7356 put_icmp_key(&base
, base_flow
);
7357 put_icmp_key(&mask
, &wc
->masks
);
7364 get_nd_key(const struct flow
*flow
, struct ovs_key_nd
*nd
)
7366 nd
->nd_target
= flow
->nd_target
;
7367 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7368 nd
->nd_sll
= flow
->arp_sha
;
7369 nd
->nd_tll
= flow
->arp_tha
;
7373 put_nd_key(const struct ovs_key_nd
*nd
, struct flow
*flow
)
7375 flow
->nd_target
= nd
->nd_target
;
7376 /* nd_sll and nd_tll are stored in arp_sha and arp_tha, respectively */
7377 flow
->arp_sha
= nd
->nd_sll
;
7378 flow
->arp_tha
= nd
->nd_tll
;
7381 static enum slow_path_reason
7382 commit_set_nd_action(const struct flow
*flow
, struct flow
*base_flow
,
7383 struct ofpbuf
*odp_actions
,
7384 struct flow_wildcards
*wc
, bool use_masked
)
7386 struct ovs_key_nd key
, mask
, base
;
7388 get_nd_key(flow
, &key
);
7389 get_nd_key(base_flow
, &base
);
7390 get_nd_key(&wc
->masks
, &mask
);
7392 if (commit(OVS_KEY_ATTR_ND
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7394 put_nd_key(&base
, base_flow
);
7395 put_nd_key(&mask
, &wc
->masks
);
7402 static enum slow_path_reason
7403 commit_set_nw_action(const struct flow
*flow
, struct flow
*base
,
7404 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7407 /* Check if 'flow' really has an L3 header. */
7408 if (!flow
->nw_proto
) {
7412 switch (ntohs(base
->dl_type
)) {
7414 commit_set_ipv4_action(flow
, base
, odp_actions
, wc
, use_masked
);
7418 commit_set_ipv6_action(flow
, base
, odp_actions
, wc
, use_masked
);
7419 return commit_set_nd_action(flow
, base
, odp_actions
, wc
, use_masked
);
7422 return commit_set_arp_action(flow
, base
, odp_actions
, wc
);
7429 get_nsh_key(const struct flow
*flow
, struct ovs_key_nsh
*nsh
, bool is_mask
)
7433 if (nsh
->mdtype
!= NSH_M_TYPE1
) {
7434 memset(nsh
->context
, 0, sizeof(nsh
->context
));
7440 put_nsh_key(const struct ovs_key_nsh
*nsh
, struct flow
*flow
,
7441 bool is_mask OVS_UNUSED
)
7444 if (flow
->nsh
.mdtype
!= NSH_M_TYPE1
) {
7445 memset(flow
->nsh
.context
, 0, sizeof(flow
->nsh
.context
));
7450 commit_nsh(const struct ovs_key_nsh
* flow_nsh
, bool use_masked_set
,
7451 const struct ovs_key_nsh
*key
, struct ovs_key_nsh
*base
,
7452 struct ovs_key_nsh
*mask
, size_t size
,
7453 struct ofpbuf
*odp_actions
)
7455 enum ovs_key_attr attr
= OVS_KEY_ATTR_NSH
;
7457 if (memcmp(key
, base
, size
) == 0) {
7458 /* Mask bits are set when we have either read or set the corresponding
7459 * values. Masked bits will be exact-matched, no need to set them
7460 * if the value did not actually change. */
7464 bool fully_masked
= odp_mask_is_exact(attr
, mask
, size
);
7466 if (use_masked_set
&& !fully_masked
) {
7468 struct ovs_nsh_key_base nsh_base
;
7469 struct ovs_nsh_key_base nsh_base_mask
;
7470 struct ovs_nsh_key_md1 md1
;
7471 struct ovs_nsh_key_md1 md1_mask
;
7472 size_t offset
= nl_msg_start_nested(odp_actions
,
7473 OVS_ACTION_ATTR_SET_MASKED
);
7475 nsh_base
.flags
= key
->flags
;
7476 nsh_base
.ttl
= key
->ttl
;
7477 nsh_base
.mdtype
= key
->mdtype
;
7478 nsh_base
.np
= key
->np
;
7479 nsh_base
.path_hdr
= key
->path_hdr
;
7481 nsh_base_mask
.flags
= mask
->flags
;
7482 nsh_base_mask
.ttl
= mask
->ttl
;
7483 nsh_base_mask
.mdtype
= mask
->mdtype
;
7484 nsh_base_mask
.np
= mask
->np
;
7485 nsh_base_mask
.path_hdr
= mask
->path_hdr
;
7487 /* OVS_KEY_ATTR_NSH keys */
7488 nsh_key_ofs
= nl_msg_start_nested(odp_actions
, OVS_KEY_ATTR_NSH
);
7490 /* put value and mask for OVS_NSH_KEY_ATTR_BASE */
7491 char *data
= nl_msg_put_unspec_uninit(odp_actions
,
7492 OVS_NSH_KEY_ATTR_BASE
,
7493 2 * sizeof(nsh_base
));
7494 const char *lkey
= (char *)&nsh_base
, *lmask
= (char *)&nsh_base_mask
;
7495 size_t lkey_size
= sizeof(nsh_base
);
7497 while (lkey_size
--) {
7498 *data
++ = *lkey
++ & *lmask
++;
7500 lmask
= (char *)&nsh_base_mask
;
7501 memcpy(data
, lmask
, sizeof(nsh_base_mask
));
7503 switch (key
->mdtype
) {
7505 memcpy(md1
.context
, key
->context
, sizeof key
->context
);
7506 memcpy(md1_mask
.context
, mask
->context
, sizeof mask
->context
);
7508 /* put value and mask for OVS_NSH_KEY_ATTR_MD1 */
7509 data
= nl_msg_put_unspec_uninit(odp_actions
,
7510 OVS_NSH_KEY_ATTR_MD1
,
7512 lkey
= (char *)&md1
;
7513 lmask
= (char *)&md1_mask
;
7514 lkey_size
= sizeof(md1
);
7516 while (lkey_size
--) {
7517 *data
++ = *lkey
++ & *lmask
++;
7519 lmask
= (char *)&md1_mask
;
7520 memcpy(data
, lmask
, sizeof(md1_mask
));
7524 /* No match support for other MD formats yet. */
7528 nl_msg_end_nested(odp_actions
, nsh_key_ofs
);
7530 nl_msg_end_nested(odp_actions
, offset
);
7532 if (!fully_masked
) {
7533 memset(mask
, 0xff, size
);
7535 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_SET
);
7536 nsh_key_to_attr(odp_actions
, flow_nsh
, NULL
, 0, false);
7537 nl_msg_end_nested(odp_actions
, offset
);
7539 memcpy(base
, key
, size
);
7544 commit_set_nsh_action(const struct flow
*flow
, struct flow
*base_flow
,
7545 struct ofpbuf
*odp_actions
,
7546 struct flow_wildcards
*wc
,
7549 struct ovs_key_nsh key
, mask
, base
;
7551 if (flow
->dl_type
!= htons(ETH_TYPE_NSH
) ||
7552 !memcmp(&base_flow
->nsh
, &flow
->nsh
, sizeof base_flow
->nsh
)) {
7556 /* Check that mdtype and np remain unchanged. */
7557 ovs_assert(flow
->nsh
.mdtype
== base_flow
->nsh
.mdtype
&&
7558 flow
->nsh
.np
== base_flow
->nsh
.np
);
7560 get_nsh_key(flow
, &key
, false);
7561 get_nsh_key(base_flow
, &base
, false);
7562 get_nsh_key(&wc
->masks
, &mask
, true);
7563 mask
.mdtype
= 0; /* Not writable. */
7564 mask
.np
= 0; /* Not writable. */
7566 if (commit_nsh(&base_flow
->nsh
, use_masked
, &key
, &base
, &mask
,
7567 sizeof key
, odp_actions
)) {
7568 put_nsh_key(&base
, base_flow
, false);
7569 if (mask
.mdtype
!= 0) { /* Mask was changed by commit(). */
7570 put_nsh_key(&mask
, &wc
->masks
, true);
7575 /* TCP, UDP, and SCTP keys have the same layout. */
7576 BUILD_ASSERT_DECL(sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_udp
) &&
7577 sizeof(struct ovs_key_tcp
) == sizeof(struct ovs_key_sctp
));
7580 get_tp_key(const struct flow
*flow
, union ovs_key_tp
*tp
)
7582 tp
->tcp
.tcp_src
= flow
->tp_src
;
7583 tp
->tcp
.tcp_dst
= flow
->tp_dst
;
7587 put_tp_key(const union ovs_key_tp
*tp
, struct flow
*flow
)
7589 flow
->tp_src
= tp
->tcp
.tcp_src
;
7590 flow
->tp_dst
= tp
->tcp
.tcp_dst
;
7594 commit_set_port_action(const struct flow
*flow
, struct flow
*base_flow
,
7595 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7598 enum ovs_key_attr key_type
;
7599 union ovs_key_tp key
, mask
, base
;
7601 /* Check if 'flow' really has an L3 header. */
7602 if (!flow
->nw_proto
) {
7606 if (!is_ip_any(base_flow
)) {
7610 if (flow
->nw_proto
== IPPROTO_TCP
) {
7611 key_type
= OVS_KEY_ATTR_TCP
;
7612 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
7613 key_type
= OVS_KEY_ATTR_UDP
;
7614 } else if (flow
->nw_proto
== IPPROTO_SCTP
) {
7615 key_type
= OVS_KEY_ATTR_SCTP
;
7620 get_tp_key(flow
, &key
);
7621 get_tp_key(base_flow
, &base
);
7622 get_tp_key(&wc
->masks
, &mask
);
7624 if (commit(key_type
, use_masked
, &key
, &base
, &mask
, sizeof key
,
7626 put_tp_key(&base
, base_flow
);
7627 put_tp_key(&mask
, &wc
->masks
);
7632 commit_set_priority_action(const struct flow
*flow
, struct flow
*base_flow
,
7633 struct ofpbuf
*odp_actions
,
7634 struct flow_wildcards
*wc
,
7637 uint32_t key
, mask
, base
;
7639 key
= flow
->skb_priority
;
7640 base
= base_flow
->skb_priority
;
7641 mask
= wc
->masks
.skb_priority
;
7643 if (commit(OVS_KEY_ATTR_PRIORITY
, use_masked
, &key
, &base
, &mask
,
7644 sizeof key
, odp_actions
)) {
7645 base_flow
->skb_priority
= base
;
7646 wc
->masks
.skb_priority
= mask
;
7651 commit_set_pkt_mark_action(const struct flow
*flow
, struct flow
*base_flow
,
7652 struct ofpbuf
*odp_actions
,
7653 struct flow_wildcards
*wc
,
7656 uint32_t key
, mask
, base
;
7658 key
= flow
->pkt_mark
;
7659 base
= base_flow
->pkt_mark
;
7660 mask
= wc
->masks
.pkt_mark
;
7662 if (commit(OVS_KEY_ATTR_SKB_MARK
, use_masked
, &key
, &base
, &mask
,
7663 sizeof key
, odp_actions
)) {
7664 base_flow
->pkt_mark
= base
;
7665 wc
->masks
.pkt_mark
= mask
;
7670 odp_put_pop_nsh_action(struct ofpbuf
*odp_actions
)
7672 nl_msg_put_flag(odp_actions
, OVS_ACTION_ATTR_POP_NSH
);
7676 odp_put_push_nsh_action(struct ofpbuf
*odp_actions
,
7677 const struct flow
*flow
,
7678 struct ofpbuf
*encap_data
)
7680 uint8_t * metadata
= NULL
;
7681 uint8_t md_size
= 0;
7683 switch (flow
->nsh
.mdtype
) {
7686 ovs_assert(encap_data
->size
< NSH_CTX_HDRS_MAX_LEN
);
7687 metadata
= encap_data
->data
;
7688 md_size
= encap_data
->size
;
7697 size_t offset
= nl_msg_start_nested(odp_actions
, OVS_ACTION_ATTR_PUSH_NSH
);
7698 nsh_key_to_attr(odp_actions
, &flow
->nsh
, metadata
, md_size
, false);
7699 nl_msg_end_nested(odp_actions
, offset
);
7703 commit_encap_decap_action(const struct flow
*flow
,
7704 struct flow
*base_flow
,
7705 struct ofpbuf
*odp_actions
,
7706 struct flow_wildcards
*wc
,
7707 bool pending_encap
, bool pending_decap
,
7708 struct ofpbuf
*encap_data
)
7710 if (pending_encap
) {
7711 switch (ntohl(flow
->packet_type
)) {
7714 odp_put_push_eth_action(odp_actions
, &flow
->dl_src
,
7716 base_flow
->packet_type
= flow
->packet_type
;
7717 base_flow
->dl_src
= flow
->dl_src
;
7718 base_flow
->dl_dst
= flow
->dl_dst
;
7723 odp_put_push_nsh_action(odp_actions
, flow
, encap_data
);
7724 base_flow
->packet_type
= flow
->packet_type
;
7725 /* Update all packet headers in base_flow. */
7726 memcpy(&base_flow
->dl_dst
, &flow
->dl_dst
,
7727 sizeof(*flow
) - offsetof(struct flow
, dl_dst
));
7730 /* Only the above protocols are supported for encap.
7731 * The check is done at action translation. */
7734 } else if (pending_decap
|| flow
->packet_type
!= base_flow
->packet_type
) {
7735 /* This is an explicit or implicit decap case. */
7736 if (pt_ns(flow
->packet_type
) == OFPHTN_ETHERTYPE
&&
7737 base_flow
->packet_type
== htonl(PT_ETH
)) {
7738 /* Generate pop_eth and continue without recirculation. */
7739 odp_put_pop_eth_action(odp_actions
);
7740 base_flow
->packet_type
= flow
->packet_type
;
7741 base_flow
->dl_src
= eth_addr_zero
;
7742 base_flow
->dl_dst
= eth_addr_zero
;
7744 /* All other decap cases require recirculation.
7745 * No need to update the base flow here. */
7746 switch (ntohl(base_flow
->packet_type
)) {
7749 odp_put_pop_nsh_action(odp_actions
);
7752 /* Checks are done during translation. */
7758 wc
->masks
.packet_type
= OVS_BE32_MAX
;
7761 /* If any of the flow key data that ODP actions can modify are different in
7762 * 'base' and 'flow', appends ODP actions to 'odp_actions' that change the flow
7763 * key from 'base' into 'flow', and then changes 'base' the same way. Does not
7764 * commit set_tunnel actions. Users should call commit_odp_tunnel_action()
7765 * in addition to this function if needed. Sets fields in 'wc' that are
7766 * used as part of the action.
7768 * Returns a reason to force processing the flow's packets into the userspace
7769 * slow path, if there is one, otherwise 0. */
7770 enum slow_path_reason
7771 commit_odp_actions(const struct flow
*flow
, struct flow
*base
,
7772 struct ofpbuf
*odp_actions
, struct flow_wildcards
*wc
,
7773 bool use_masked
, bool pending_encap
, bool pending_decap
,
7774 struct ofpbuf
*encap_data
)
7776 enum slow_path_reason slow1
, slow2
;
7777 bool mpls_done
= false;
7779 commit_encap_decap_action(flow
, base
, odp_actions
, wc
,
7780 pending_encap
, pending_decap
, encap_data
);
7781 commit_set_ether_action(flow
, base
, odp_actions
, wc
, use_masked
);
7782 /* Make packet a non-MPLS packet before committing L3/4 actions,
7783 * which would otherwise do nothing. */
7784 if (eth_type_mpls(base
->dl_type
) && !eth_type_mpls(flow
->dl_type
)) {
7785 commit_mpls_action(flow
, base
, odp_actions
);
7788 commit_set_nsh_action(flow
, base
, odp_actions
, wc
, use_masked
);
7789 slow1
= commit_set_nw_action(flow
, base
, odp_actions
, wc
, use_masked
);
7790 commit_set_port_action(flow
, base
, odp_actions
, wc
, use_masked
);
7791 slow2
= commit_set_icmp_action(flow
, base
, odp_actions
, wc
);
7793 commit_mpls_action(flow
, base
, odp_actions
);
7795 commit_vlan_action(flow
, base
, odp_actions
, wc
);
7796 commit_set_priority_action(flow
, base
, odp_actions
, wc
, use_masked
);
7797 commit_set_pkt_mark_action(flow
, base
, odp_actions
, wc
, use_masked
);
7799 return slow1
? slow1
: slow2
;