2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 * Copyright (c) 2013 Simon Horman
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "odp-execute.h"
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
29 #include "dp-packet.h"
32 #include "odp-netlink.h"
36 #include "unaligned.h"
39 #include "conntrack.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(odp_execute
);
43 COVERAGE_DEFINE(datapath_drop_sample_error
);
44 COVERAGE_DEFINE(datapath_drop_nsh_decap_error
);
45 COVERAGE_DEFINE(drop_action_of_pipeline
);
46 COVERAGE_DEFINE(drop_action_bridge_not_found
);
47 COVERAGE_DEFINE(drop_action_recursion_too_deep
);
48 COVERAGE_DEFINE(drop_action_too_many_resubmit
);
49 COVERAGE_DEFINE(drop_action_stack_too_deep
);
50 COVERAGE_DEFINE(drop_action_no_recirculation_context
);
51 COVERAGE_DEFINE(drop_action_recirculation_conflict
);
52 COVERAGE_DEFINE(drop_action_too_many_mpls_labels
);
53 COVERAGE_DEFINE(drop_action_invalid_tunnel_metadata
);
54 COVERAGE_DEFINE(drop_action_unsupported_packet_type
);
55 COVERAGE_DEFINE(drop_action_congestion
);
56 COVERAGE_DEFINE(drop_action_forwarding_disabled
);
59 dp_update_drop_action_counter(enum xlate_error drop_reason
,
62 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(1, 5);
64 switch (drop_reason
) {
66 COVERAGE_ADD(drop_action_of_pipeline
, delta
);
68 case XLATE_BRIDGE_NOT_FOUND
:
69 COVERAGE_ADD(drop_action_bridge_not_found
, delta
);
71 case XLATE_RECURSION_TOO_DEEP
:
72 COVERAGE_ADD(drop_action_recursion_too_deep
, delta
);
74 case XLATE_TOO_MANY_RESUBMITS
:
75 COVERAGE_ADD(drop_action_too_many_resubmit
, delta
);
77 case XLATE_STACK_TOO_DEEP
:
78 COVERAGE_ADD(drop_action_stack_too_deep
, delta
);
80 case XLATE_NO_RECIRCULATION_CONTEXT
:
81 COVERAGE_ADD(drop_action_no_recirculation_context
, delta
);
83 case XLATE_RECIRCULATION_CONFLICT
:
84 COVERAGE_ADD(drop_action_recirculation_conflict
, delta
);
86 case XLATE_TOO_MANY_MPLS_LABELS
:
87 COVERAGE_ADD(drop_action_too_many_mpls_labels
, delta
);
89 case XLATE_INVALID_TUNNEL_METADATA
:
90 COVERAGE_ADD(drop_action_invalid_tunnel_metadata
, delta
);
92 case XLATE_UNSUPPORTED_PACKET_TYPE
:
93 COVERAGE_ADD(drop_action_unsupported_packet_type
, delta
);
95 case XLATE_CONGESTION_DROP
:
96 COVERAGE_ADD(drop_action_congestion
, delta
);
98 case XLATE_FORWARDING_DISABLED
:
99 COVERAGE_ADD(drop_action_forwarding_disabled
, delta
);
103 VLOG_ERR_RL(&rl
, "Invalid Drop reason type: %d", drop_reason
);
107 /* Masked copy of an ethernet address. 'src' is already properly masked. */
109 ether_addr_copy_masked(struct eth_addr
*dst
, const struct eth_addr src
,
110 const struct eth_addr mask
)
114 for (i
= 0; i
< ARRAY_SIZE(dst
->be16
); i
++) {
115 dst
->be16
[i
] = src
.be16
[i
] | (dst
->be16
[i
] & ~mask
.be16
[i
]);
120 odp_eth_set_addrs(struct dp_packet
*packet
, const struct ovs_key_ethernet
*key
,
121 const struct ovs_key_ethernet
*mask
)
123 struct eth_header
*eh
= dp_packet_eth(packet
);
127 eh
->eth_src
= key
->eth_src
;
128 eh
->eth_dst
= key
->eth_dst
;
130 ether_addr_copy_masked(&eh
->eth_src
, key
->eth_src
, mask
->eth_src
);
131 ether_addr_copy_masked(&eh
->eth_dst
, key
->eth_dst
, mask
->eth_dst
);
137 odp_set_ipv4(struct dp_packet
*packet
, const struct ovs_key_ipv4
*key
,
138 const struct ovs_key_ipv4
*mask
)
140 struct ip_header
*nh
= dp_packet_l3(packet
);
148 if (mask
->ipv4_src
) {
149 ip_src_nh
= get_16aligned_be32(&nh
->ip_src
);
150 new_ip_src
= key
->ipv4_src
| (ip_src_nh
& ~mask
->ipv4_src
);
152 if (ip_src_nh
!= new_ip_src
) {
153 packet_set_ipv4_addr(packet
, &nh
->ip_src
, new_ip_src
);
157 if (mask
->ipv4_dst
) {
158 ip_dst_nh
= get_16aligned_be32(&nh
->ip_dst
);
159 new_ip_dst
= key
->ipv4_dst
| (ip_dst_nh
& ~mask
->ipv4_dst
);
161 if (ip_dst_nh
!= new_ip_dst
) {
162 packet_set_ipv4_addr(packet
, &nh
->ip_dst
, new_ip_dst
);
166 if (mask
->ipv4_tos
) {
167 new_tos
= key
->ipv4_tos
| (nh
->ip_tos
& ~mask
->ipv4_tos
);
169 if (nh
->ip_tos
!= new_tos
) {
170 nh
->ip_csum
= recalc_csum16(nh
->ip_csum
,
171 htons((uint16_t) nh
->ip_tos
),
172 htons((uint16_t) new_tos
));
173 nh
->ip_tos
= new_tos
;
177 if (OVS_LIKELY(mask
->ipv4_ttl
)) {
178 new_ttl
= key
->ipv4_ttl
| (nh
->ip_ttl
& ~mask
->ipv4_ttl
);
180 if (OVS_LIKELY(nh
->ip_ttl
!= new_ttl
)) {
181 nh
->ip_csum
= recalc_csum16(nh
->ip_csum
, htons(nh
->ip_ttl
<< 8),
182 htons(new_ttl
<< 8));
183 nh
->ip_ttl
= new_ttl
;
188 static struct in6_addr
*
189 mask_ipv6_addr(const ovs_16aligned_be32
*old
, const struct in6_addr
*addr
,
190 const struct in6_addr
*mask
, struct in6_addr
*masked
)
193 for (int i
= 0; i
< 4; i
++) {
194 masked
->s6_addr32
[i
] = addr
->s6_addr32
[i
]
195 | (get_16aligned_be32(&old
[i
]) & ~mask
->s6_addr32
[i
]);
198 const uint8_t *old8
= (const uint8_t *)old
;
199 for (int i
= 0; i
< 16; i
++) {
200 masked
->s6_addr
[i
] = addr
->s6_addr
[i
] | (old8
[i
] & ~mask
->s6_addr
[i
]);
207 odp_set_ipv6(struct dp_packet
*packet
, const struct ovs_key_ipv6
*key
,
208 const struct ovs_key_ipv6
*mask
)
210 struct ovs_16aligned_ip6_hdr
*nh
= dp_packet_l3(packet
);
211 struct in6_addr sbuf
, dbuf
;
212 uint8_t old_tc
= ntohl(get_16aligned_be32(&nh
->ip6_flow
)) >> 20;
213 ovs_be32 old_fl
= get_16aligned_be32(&nh
->ip6_flow
) & htonl(0xfffff);
217 mask_ipv6_addr(nh
->ip6_src
.be32
, &key
->ipv6_src
, &mask
->ipv6_src
,
219 mask_ipv6_addr(nh
->ip6_dst
.be32
, &key
->ipv6_dst
, &mask
->ipv6_dst
,
221 key
->ipv6_tclass
| (old_tc
& ~mask
->ipv6_tclass
),
222 key
->ipv6_label
| (old_fl
& ~mask
->ipv6_label
),
223 key
->ipv6_hlimit
| (nh
->ip6_hlim
& ~mask
->ipv6_hlimit
));
227 odp_set_tcp(struct dp_packet
*packet
, const struct ovs_key_tcp
*key
,
228 const struct ovs_key_tcp
*mask
)
230 struct tcp_header
*th
= dp_packet_l4(packet
);
232 if (OVS_LIKELY(th
&& dp_packet_get_tcp_payload(packet
))) {
233 packet_set_tcp_port(packet
,
234 key
->tcp_src
| (th
->tcp_src
& ~mask
->tcp_src
),
235 key
->tcp_dst
| (th
->tcp_dst
& ~mask
->tcp_dst
));
240 odp_set_udp(struct dp_packet
*packet
, const struct ovs_key_udp
*key
,
241 const struct ovs_key_udp
*mask
)
243 struct udp_header
*uh
= dp_packet_l4(packet
);
245 if (OVS_LIKELY(uh
&& dp_packet_get_udp_payload(packet
))) {
246 packet_set_udp_port(packet
,
247 key
->udp_src
| (uh
->udp_src
& ~mask
->udp_src
),
248 key
->udp_dst
| (uh
->udp_dst
& ~mask
->udp_dst
));
253 odp_set_sctp(struct dp_packet
*packet
, const struct ovs_key_sctp
*key
,
254 const struct ovs_key_sctp
*mask
)
256 struct sctp_header
*sh
= dp_packet_l4(packet
);
258 if (OVS_LIKELY(sh
&& dp_packet_get_sctp_payload(packet
))) {
259 packet_set_sctp_port(packet
,
260 key
->sctp_src
| (sh
->sctp_src
& ~mask
->sctp_src
),
261 key
->sctp_dst
| (sh
->sctp_dst
& ~mask
->sctp_dst
));
266 odp_set_tunnel_action(const struct nlattr
*a
, struct flow_tnl
*tun_key
)
268 ovs_assert(odp_tun_key_from_attr(a
, tun_key
, NULL
) != ODP_FIT_ERROR
);
272 set_arp(struct dp_packet
*packet
, const struct ovs_key_arp
*key
,
273 const struct ovs_key_arp
*mask
)
275 struct arp_eth_header
*arp
= dp_packet_l3(packet
);
278 arp
->ar_op
= key
->arp_op
;
279 arp
->ar_sha
= key
->arp_sha
;
280 put_16aligned_be32(&arp
->ar_spa
, key
->arp_sip
);
281 arp
->ar_tha
= key
->arp_tha
;
282 put_16aligned_be32(&arp
->ar_tpa
, key
->arp_tip
);
284 ovs_be32 ar_spa
= get_16aligned_be32(&arp
->ar_spa
);
285 ovs_be32 ar_tpa
= get_16aligned_be32(&arp
->ar_tpa
);
287 arp
->ar_op
= key
->arp_op
| (arp
->ar_op
& ~mask
->arp_op
);
288 ether_addr_copy_masked(&arp
->ar_sha
, key
->arp_sha
, mask
->arp_sha
);
289 put_16aligned_be32(&arp
->ar_spa
,
290 key
->arp_sip
| (ar_spa
& ~mask
->arp_sip
));
291 ether_addr_copy_masked(&arp
->ar_tha
, key
->arp_tha
, mask
->arp_tha
);
292 put_16aligned_be32(&arp
->ar_tpa
,
293 key
->arp_tip
| (ar_tpa
& ~mask
->arp_tip
));
298 odp_set_nd_ext(struct dp_packet
*packet
, const struct ovs_key_nd_extensions
299 *key
, const struct ovs_key_nd_extensions
*mask
)
301 const struct ovs_nd_msg
*ns
= dp_packet_l4(packet
);
302 ovs_16aligned_be32 reserved
= ns
->rso_flags
;
303 uint8_t opt_type
= ns
->options
[0].type
;
305 if (mask
->nd_reserved
) {
306 put_16aligned_be32(&reserved
, key
->nd_reserved
);
308 if (mask
->nd_options_type
) {
309 opt_type
= key
->nd_options_type
;
311 packet_set_nd_ext(packet
, reserved
, opt_type
);
315 odp_set_nd(struct dp_packet
*packet
, const struct ovs_key_nd
*key
,
316 const struct ovs_key_nd
*mask
)
318 const struct ovs_nd_msg
*ns
= dp_packet_l4(packet
);
319 const struct ovs_nd_lla_opt
*lla_opt
= dp_packet_get_nd_payload(packet
);
321 if (OVS_LIKELY(ns
&& lla_opt
)) {
322 int bytes_remain
= dp_packet_l4_size(packet
) - sizeof(*ns
);
323 struct in6_addr tgt_buf
;
324 struct eth_addr sll_buf
= eth_addr_zero
;
325 struct eth_addr tll_buf
= eth_addr_zero
;
327 while (bytes_remain
>= ND_LLA_OPT_LEN
&& lla_opt
->len
!= 0) {
328 if (lla_opt
->type
== ND_OPT_SOURCE_LINKADDR
329 && lla_opt
->len
== 1) {
330 sll_buf
= lla_opt
->mac
;
331 ether_addr_copy_masked(&sll_buf
, key
->nd_sll
, mask
->nd_sll
);
333 /* A packet can only contain one SLL or TLL option */
335 } else if (lla_opt
->type
== ND_OPT_TARGET_LINKADDR
336 && lla_opt
->len
== 1) {
337 tll_buf
= lla_opt
->mac
;
338 ether_addr_copy_masked(&tll_buf
, key
->nd_tll
, mask
->nd_tll
);
340 /* A packet can only contain one SLL or TLL option */
344 lla_opt
+= lla_opt
->len
;
345 bytes_remain
-= lla_opt
->len
* ND_LLA_OPT_LEN
;
348 packet_set_nd(packet
,
349 mask_ipv6_addr(ns
->target
.be32
, &key
->nd_target
,
350 &mask
->nd_target
, &tgt_buf
),
356 /* Set the NSH header. Assumes the NSH header is present and matches the
357 * MD format of the key. The slow path must take case of that. */
359 odp_set_nsh(struct dp_packet
*packet
, const struct nlattr
*a
, bool has_mask
)
361 struct ovs_key_nsh key
, mask
;
362 struct nsh_hdr
*nsh
= dp_packet_l3(packet
);
363 uint8_t mdtype
= nsh_md_type(nsh
);
367 odp_nsh_key_from_attr(a
, &key
, &mask
, NULL
);
369 odp_nsh_key_from_attr(a
, &key
, NULL
, NULL
);
373 nsh_set_flags_and_ttl(nsh
, key
.flags
, key
.ttl
);
374 put_16aligned_be32(&nsh
->path_hdr
, key
.path_hdr
);
377 for (int i
= 0; i
< 4; i
++) {
378 put_16aligned_be32(&nsh
->md1
.context
[i
], key
.context
[i
]);
383 /* No support for setting any other metadata format yet. */
387 uint8_t flags
= nsh_get_flags(nsh
);
388 uint8_t ttl
= nsh_get_ttl(nsh
);
390 flags
= key
.flags
| (flags
& ~mask
.flags
);
391 ttl
= key
.ttl
| (ttl
& ~mask
.ttl
);
392 nsh_set_flags_and_ttl(nsh
, flags
, ttl
);
394 uint32_t spi
= ntohl(nsh_get_spi(nsh
));
395 uint8_t si
= nsh_get_si(nsh
);
396 uint32_t spi_mask
= nsh_path_hdr_to_spi_uint32(mask
.path_hdr
);
397 uint8_t si_mask
= nsh_path_hdr_to_si(mask
.path_hdr
);
398 if (spi_mask
== 0x00ffffff) {
399 spi_mask
= UINT32_MAX
;
401 spi
= nsh_path_hdr_to_spi_uint32(key
.path_hdr
) | (spi
& ~spi_mask
);
402 si
= nsh_path_hdr_to_si(key
.path_hdr
) | (si
& ~si_mask
);
403 path_hdr
= nsh_get_path_hdr(nsh
);
404 nsh_path_hdr_set_spi(&path_hdr
, htonl(spi
));
405 nsh_path_hdr_set_si(&path_hdr
, si
);
406 put_16aligned_be32(&nsh
->path_hdr
, path_hdr
);
409 for (int i
= 0; i
< 4; i
++) {
410 ovs_be32 p
= get_16aligned_be32(&nsh
->md1
.context
[i
]);
411 ovs_be32 k
= key
.context
[i
];
412 ovs_be32 m
= mask
.context
[i
];
413 put_16aligned_be32(&nsh
->md1
.context
[i
], k
| (p
& ~m
));
418 /* No support for setting any other metadata format yet. */
425 odp_execute_set_action(struct dp_packet
*packet
, const struct nlattr
*a
)
427 enum ovs_key_attr type
= nl_attr_type(a
);
428 const struct ovs_key_ipv4
*ipv4_key
;
429 const struct ovs_key_ipv6
*ipv6_key
;
430 struct pkt_metadata
*md
= &packet
->md
;
433 case OVS_KEY_ATTR_PRIORITY
:
434 md
->skb_priority
= nl_attr_get_u32(a
);
437 case OVS_KEY_ATTR_TUNNEL
:
438 odp_set_tunnel_action(a
, &md
->tunnel
);
441 case OVS_KEY_ATTR_SKB_MARK
:
442 md
->pkt_mark
= nl_attr_get_u32(a
);
445 case OVS_KEY_ATTR_ETHERNET
:
446 odp_eth_set_addrs(packet
, nl_attr_get(a
), NULL
);
449 case OVS_KEY_ATTR_NSH
: {
450 odp_set_nsh(packet
, a
, false);
454 case OVS_KEY_ATTR_IPV4
:
455 ipv4_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_ipv4
));
456 packet_set_ipv4(packet
, ipv4_key
->ipv4_src
,
457 ipv4_key
->ipv4_dst
, ipv4_key
->ipv4_tos
,
461 case OVS_KEY_ATTR_IPV6
:
462 ipv6_key
= nl_attr_get_unspec(a
, sizeof(struct ovs_key_ipv6
));
463 packet_set_ipv6(packet
, &ipv6_key
->ipv6_src
, &ipv6_key
->ipv6_dst
,
464 ipv6_key
->ipv6_tclass
, ipv6_key
->ipv6_label
,
465 ipv6_key
->ipv6_hlimit
);
468 case OVS_KEY_ATTR_TCP
:
469 if (OVS_LIKELY(dp_packet_get_tcp_payload(packet
))) {
470 const struct ovs_key_tcp
*tcp_key
471 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_tcp
));
473 packet_set_tcp_port(packet
, tcp_key
->tcp_src
,
478 case OVS_KEY_ATTR_UDP
:
479 if (OVS_LIKELY(dp_packet_get_udp_payload(packet
))) {
480 const struct ovs_key_udp
*udp_key
481 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_udp
));
483 packet_set_udp_port(packet
, udp_key
->udp_src
,
488 case OVS_KEY_ATTR_SCTP
:
489 if (OVS_LIKELY(dp_packet_get_sctp_payload(packet
))) {
490 const struct ovs_key_sctp
*sctp_key
491 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_sctp
));
493 packet_set_sctp_port(packet
, sctp_key
->sctp_src
,
498 case OVS_KEY_ATTR_MPLS
:
499 set_mpls_lse(packet
, nl_attr_get_be32(a
));
502 case OVS_KEY_ATTR_ARP
:
503 set_arp(packet
, nl_attr_get(a
), NULL
);
506 case OVS_KEY_ATTR_ICMP
:
507 case OVS_KEY_ATTR_ICMPV6
:
508 if (OVS_LIKELY(dp_packet_get_icmp_payload(packet
))) {
509 const struct ovs_key_icmp
*icmp_key
510 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_icmp
));
512 packet_set_icmp(packet
, icmp_key
->icmp_type
, icmp_key
->icmp_code
);
516 case OVS_KEY_ATTR_ND
:
517 if (OVS_LIKELY(dp_packet_get_nd_payload(packet
))) {
518 const struct ovs_key_nd
*nd_key
519 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_nd
));
520 packet_set_nd(packet
, &nd_key
->nd_target
, nd_key
->nd_sll
,
525 case OVS_KEY_ATTR_ND_EXTENSIONS
:
526 if (OVS_LIKELY(dp_packet_get_nd_payload(packet
))) {
527 const struct ovs_key_nd_extensions
*nd_ext_key
528 = nl_attr_get_unspec(a
, sizeof(struct ovs_key_nd_extensions
));
529 ovs_16aligned_be32 rso_flags
;
530 put_16aligned_be32(&rso_flags
, nd_ext_key
->nd_reserved
);
531 packet_set_nd_ext(packet
, rso_flags
, nd_ext_key
->nd_options_type
);
535 case OVS_KEY_ATTR_DP_HASH
:
536 md
->dp_hash
= nl_attr_get_u32(a
);
539 case OVS_KEY_ATTR_RECIRC_ID
:
540 md
->recirc_id
= nl_attr_get_u32(a
);
543 case OVS_KEY_ATTR_UNSPEC
:
544 case OVS_KEY_ATTR_PACKET_TYPE
:
545 case OVS_KEY_ATTR_ENCAP
:
546 case OVS_KEY_ATTR_ETHERTYPE
:
547 case OVS_KEY_ATTR_IN_PORT
:
548 case OVS_KEY_ATTR_VLAN
:
549 case OVS_KEY_ATTR_TCP_FLAGS
:
550 case OVS_KEY_ATTR_CT_STATE
:
551 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
552 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
553 case OVS_KEY_ATTR_CT_ZONE
:
554 case OVS_KEY_ATTR_CT_MARK
:
555 case OVS_KEY_ATTR_CT_LABELS
:
556 case __OVS_KEY_ATTR_MAX
:
562 #define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
565 odp_execute_masked_set_action(struct dp_packet
*packet
,
566 const struct nlattr
*a
)
568 struct pkt_metadata
*md
= &packet
->md
;
569 enum ovs_key_attr type
= nl_attr_type(a
);
573 case OVS_KEY_ATTR_PRIORITY
:
574 md
->skb_priority
= nl_attr_get_u32(a
)
575 | (md
->skb_priority
& ~*get_mask(a
, uint32_t));
578 case OVS_KEY_ATTR_SKB_MARK
:
579 md
->pkt_mark
= nl_attr_get_u32(a
)
580 | (md
->pkt_mark
& ~*get_mask(a
, uint32_t));
583 case OVS_KEY_ATTR_ETHERNET
:
584 odp_eth_set_addrs(packet
, nl_attr_get(a
),
585 get_mask(a
, struct ovs_key_ethernet
));
588 case OVS_KEY_ATTR_NSH
: {
589 odp_set_nsh(packet
, a
, true);
593 case OVS_KEY_ATTR_IPV4
:
594 odp_set_ipv4(packet
, nl_attr_get(a
),
595 get_mask(a
, struct ovs_key_ipv4
));
598 case OVS_KEY_ATTR_IPV6
:
599 odp_set_ipv6(packet
, nl_attr_get(a
),
600 get_mask(a
, struct ovs_key_ipv6
));
603 case OVS_KEY_ATTR_TCP
:
604 odp_set_tcp(packet
, nl_attr_get(a
),
605 get_mask(a
, struct ovs_key_tcp
));
608 case OVS_KEY_ATTR_UDP
:
609 odp_set_udp(packet
, nl_attr_get(a
),
610 get_mask(a
, struct ovs_key_udp
));
613 case OVS_KEY_ATTR_SCTP
:
614 odp_set_sctp(packet
, nl_attr_get(a
),
615 get_mask(a
, struct ovs_key_sctp
));
618 case OVS_KEY_ATTR_MPLS
:
619 mh
= dp_packet_l2_5(packet
);
621 put_16aligned_be32(&mh
->mpls_lse
, nl_attr_get_be32(a
)
622 | (get_16aligned_be32(&mh
->mpls_lse
)
623 & ~*get_mask(a
, ovs_be32
)));
627 case OVS_KEY_ATTR_ARP
:
628 set_arp(packet
, nl_attr_get(a
),
629 get_mask(a
, struct ovs_key_arp
));
632 case OVS_KEY_ATTR_ND
:
633 odp_set_nd(packet
, nl_attr_get(a
),
634 get_mask(a
, struct ovs_key_nd
));
637 case OVS_KEY_ATTR_ND_EXTENSIONS
:
638 odp_set_nd_ext(packet
, nl_attr_get(a
),
639 get_mask(a
, struct ovs_key_nd_extensions
));
642 case OVS_KEY_ATTR_DP_HASH
:
643 md
->dp_hash
= nl_attr_get_u32(a
)
644 | (md
->dp_hash
& ~*get_mask(a
, uint32_t));
647 case OVS_KEY_ATTR_RECIRC_ID
:
648 md
->recirc_id
= nl_attr_get_u32(a
)
649 | (md
->recirc_id
& ~*get_mask(a
, uint32_t));
652 case OVS_KEY_ATTR_TUNNEL
: /* Masked data not supported for tunnel. */
653 case OVS_KEY_ATTR_PACKET_TYPE
:
654 case OVS_KEY_ATTR_UNSPEC
:
655 case OVS_KEY_ATTR_CT_STATE
:
656 case OVS_KEY_ATTR_CT_ZONE
:
657 case OVS_KEY_ATTR_CT_MARK
:
658 case OVS_KEY_ATTR_CT_LABELS
:
659 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
660 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
661 case OVS_KEY_ATTR_ENCAP
:
662 case OVS_KEY_ATTR_ETHERTYPE
:
663 case OVS_KEY_ATTR_IN_PORT
:
664 case OVS_KEY_ATTR_VLAN
:
665 case OVS_KEY_ATTR_ICMP
:
666 case OVS_KEY_ATTR_ICMPV6
:
667 case OVS_KEY_ATTR_TCP_FLAGS
:
668 case __OVS_KEY_ATTR_MAX
:
675 odp_execute_sample(void *dp
, struct dp_packet
*packet
, bool steal
,
676 const struct nlattr
*action
,
677 odp_execute_cb dp_execute_action
)
679 const struct nlattr
*subactions
= NULL
;
680 const struct nlattr
*a
;
681 struct dp_packet_batch pb
;
684 NL_NESTED_FOR_EACH_UNSAFE (a
, left
, action
) {
685 int type
= nl_attr_type(a
);
687 switch ((enum ovs_sample_attr
) type
) {
688 case OVS_SAMPLE_ATTR_PROBABILITY
:
689 if (random_uint32() >= nl_attr_get_u32(a
)) {
691 COVERAGE_INC(datapath_drop_sample_error
);
692 dp_packet_delete(packet
);
698 case OVS_SAMPLE_ATTR_ACTIONS
:
702 case OVS_SAMPLE_ATTR_UNSPEC
:
703 case __OVS_SAMPLE_ATTR_MAX
:
710 /* The 'subactions' may modify the packet, but the modification
711 * should not propagate beyond this sample action. Make a copy
712 * the packet in case we don't own the packet, so that the
713 * 'subactions' are only applid to the clone. 'odp_execute_actions'
714 * will free the clone. */
715 packet
= dp_packet_clone(packet
);
717 dp_packet_batch_init_packet(&pb
, packet
);
718 odp_execute_actions(dp
, &pb
, true, nl_attr_get(subactions
),
719 nl_attr_get_size(subactions
), dp_execute_action
);
723 odp_execute_clone(void *dp
, struct dp_packet_batch
*batch
, bool steal
,
724 const struct nlattr
*actions
,
725 odp_execute_cb dp_execute_action
)
728 /* The 'actions' may modify the packet, but the modification
729 * should not propagate beyond this clone action. Make a copy
730 * the packet in case we don't own the packet, so that the
731 * 'actions' are only applied to the clone. 'odp_execute_actions'
732 * will free the clone. */
733 struct dp_packet_batch clone_pkt_batch
;
734 dp_packet_batch_clone(&clone_pkt_batch
, batch
);
735 dp_packet_batch_reset_cutlen(batch
);
736 odp_execute_actions(dp
, &clone_pkt_batch
, true, nl_attr_get(actions
),
737 nl_attr_get_size(actions
), dp_execute_action
);
740 odp_execute_actions(dp
, batch
, true, nl_attr_get(actions
),
741 nl_attr_get_size(actions
), dp_execute_action
);
746 odp_execute_check_pkt_len(void *dp
, struct dp_packet
*packet
, bool steal
,
747 const struct nlattr
*action
,
748 odp_execute_cb dp_execute_action
)
750 static const struct nl_policy ovs_cpl_policy
[] = {
751 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] = { .type
= NL_A_U16
},
752 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] = { .type
= NL_A_NESTED
},
753 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
]
754 = { .type
= NL_A_NESTED
},
756 struct nlattr
*attrs
[ARRAY_SIZE(ovs_cpl_policy
)];
758 if (!nl_parse_nested(action
, ovs_cpl_policy
, attrs
, ARRAY_SIZE(attrs
))) {
762 const struct nlattr
*a
;
763 struct dp_packet_batch pb
;
764 uint32_t size
= dp_packet_get_send_len(packet
)
765 - dp_packet_l2_pad_size(packet
);
767 a
= attrs
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
];
768 if (size
> nl_attr_get_u16(a
)) {
769 a
= attrs
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
];
771 a
= attrs
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
];
775 /* The 'subactions' may modify the packet, but the modification
776 * should not propagate beyond this action. Make a copy
777 * the packet in case we don't own the packet, so that the
778 * 'subactions' are only applid to check_pkt_len. 'odp_execute_actions'
779 * will free the clone. */
780 packet
= dp_packet_clone(packet
);
782 /* If nl_attr_get(a) is NULL, the packet will be freed by
783 * odp_execute_actions. */
784 dp_packet_batch_init_packet(&pb
, packet
);
785 odp_execute_actions(dp
, &pb
, true, nl_attr_get(a
), nl_attr_get_size(a
),
790 requires_datapath_assistance(const struct nlattr
*a
)
792 enum ovs_action_attr type
= nl_attr_type(a
);
795 /* These only make sense in the context of a datapath. */
796 case OVS_ACTION_ATTR_OUTPUT
:
797 case OVS_ACTION_ATTR_LB_OUTPUT
:
798 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
799 case OVS_ACTION_ATTR_TUNNEL_POP
:
800 case OVS_ACTION_ATTR_USERSPACE
:
801 case OVS_ACTION_ATTR_RECIRC
:
802 case OVS_ACTION_ATTR_CT
:
803 case OVS_ACTION_ATTR_METER
:
806 case OVS_ACTION_ATTR_SET
:
807 case OVS_ACTION_ATTR_SET_MASKED
:
808 case OVS_ACTION_ATTR_PUSH_VLAN
:
809 case OVS_ACTION_ATTR_POP_VLAN
:
810 case OVS_ACTION_ATTR_SAMPLE
:
811 case OVS_ACTION_ATTR_HASH
:
812 case OVS_ACTION_ATTR_PUSH_MPLS
:
813 case OVS_ACTION_ATTR_POP_MPLS
:
814 case OVS_ACTION_ATTR_TRUNC
:
815 case OVS_ACTION_ATTR_PUSH_ETH
:
816 case OVS_ACTION_ATTR_POP_ETH
:
817 case OVS_ACTION_ATTR_CLONE
:
818 case OVS_ACTION_ATTR_PUSH_NSH
:
819 case OVS_ACTION_ATTR_POP_NSH
:
820 case OVS_ACTION_ATTR_CT_CLEAR
:
821 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
822 case OVS_ACTION_ATTR_DROP
:
825 case OVS_ACTION_ATTR_UNSPEC
:
826 case __OVS_ACTION_ATTR_MAX
:
833 /* Executes all of the 'actions_len' bytes of datapath actions in 'actions' on
834 * the packets in 'batch'. If 'steal' is true, possibly modifies and
835 * definitely free the packets in 'batch', otherwise leaves 'batch' unchanged.
837 * Some actions (e.g. output actions) can only be executed by a datapath. This
838 * function implements those actions by passing the action and the packets to
839 * 'dp_execute_action' (along with 'dp'). If 'dp_execute_action' is passed a
840 * true 'steal' parameter then it must definitely free the packets passed into
841 * it. The packet can be modified whether 'steal' is false or true. If a
842 * packet is removed from the batch, then the fate of the packet is determined
843 * by the code that does this removal, irrespective of the value of 'steal'.
844 * Otherwise, if the packet is not removed from the batch and 'steal' is false
845 * then the packet could either be cloned or not. */
847 odp_execute_actions(void *dp
, struct dp_packet_batch
*batch
, bool steal
,
848 const struct nlattr
*actions
, size_t actions_len
,
849 odp_execute_cb dp_execute_action
)
851 struct dp_packet
*packet
;
852 const struct nlattr
*a
;
855 NL_ATTR_FOR_EACH_UNSAFE (a
, left
, actions
, actions_len
) {
856 int type
= nl_attr_type(a
);
857 bool last_action
= (left
<= NLA_ALIGN(a
->nla_len
));
859 if (requires_datapath_assistance(a
)) {
860 if (dp_execute_action
) {
861 /* Allow 'dp_execute_action' to steal the packet data if we do
862 * not need it any more. */
863 bool should_steal
= steal
&& last_action
;
865 dp_execute_action(dp
, batch
, a
, should_steal
);
867 if (last_action
|| dp_packet_batch_is_empty(batch
)) {
868 /* We do not need to free the packets.
869 * Either dp_execute_actions() has stolen them
870 * or the batch is freed due to errors. In either
871 * case we do not need to execute further actions.
879 switch ((enum ovs_action_attr
) type
) {
881 case OVS_ACTION_ATTR_HASH
: {
882 const struct ovs_action_hash
*hash_act
= nl_attr_get(a
);
884 /* Calculate a hash value directly. This might not match the
885 * value computed by the datapath, but it is much less expensive,
886 * and the current use case (bonding) does not require a strict
887 * match to work properly. */
888 switch (hash_act
->hash_alg
) {
889 case OVS_HASH_ALG_L4
: {
893 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
894 /* RSS hash can be used here instead of 5tuple for
895 * performance reasons. */
896 if (dp_packet_rss_valid(packet
)) {
897 hash
= dp_packet_get_rss_hash(packet
);
898 hash
= hash_int(hash
, hash_act
->hash_basis
);
900 flow_extract(packet
, &flow
);
901 hash
= flow_hash_5tuple(&flow
, hash_act
->hash_basis
);
903 packet
->md
.dp_hash
= hash
;
907 case OVS_HASH_ALG_SYM_L4
: {
911 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
912 flow_extract(packet
, &flow
);
913 hash
= flow_hash_symmetric_l3l4(&flow
,
914 hash_act
->hash_basis
,
916 packet
->md
.dp_hash
= hash
;
921 /* Assert on unknown hash algorithm. */
927 case OVS_ACTION_ATTR_PUSH_VLAN
: {
928 const struct ovs_action_push_vlan
*vlan
= nl_attr_get(a
);
930 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
931 eth_push_vlan(packet
, vlan
->vlan_tpid
, vlan
->vlan_tci
);
936 case OVS_ACTION_ATTR_POP_VLAN
:
937 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
938 eth_pop_vlan(packet
);
942 case OVS_ACTION_ATTR_PUSH_MPLS
: {
943 const struct ovs_action_push_mpls
*mpls
= nl_attr_get(a
);
945 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
946 push_mpls(packet
, mpls
->mpls_ethertype
, mpls
->mpls_lse
);
951 case OVS_ACTION_ATTR_POP_MPLS
:
952 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
953 pop_mpls(packet
, nl_attr_get_be16(a
));
957 case OVS_ACTION_ATTR_SET
:
958 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
959 odp_execute_set_action(packet
, nl_attr_get(a
));
963 case OVS_ACTION_ATTR_SET_MASKED
:
964 DP_PACKET_BATCH_FOR_EACH(i
, packet
, batch
) {
965 odp_execute_masked_set_action(packet
, nl_attr_get(a
));
969 case OVS_ACTION_ATTR_SAMPLE
:
970 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
971 odp_execute_sample(dp
, packet
, steal
&& last_action
, a
,
976 /* We do not need to free the packets. odp_execute_sample() has
982 case OVS_ACTION_ATTR_TRUNC
: {
983 const struct ovs_action_trunc
*trunc
=
984 nl_attr_get_unspec(a
, sizeof *trunc
);
987 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
988 dp_packet_set_cutlen(packet
, trunc
->max_len
);
993 case OVS_ACTION_ATTR_CLONE
:
994 odp_execute_clone(dp
, batch
, steal
&& last_action
, a
,
997 /* We do not need to free the packets. odp_execute_clone() has
1002 case OVS_ACTION_ATTR_METER
:
1003 /* Not implemented yet. */
1005 case OVS_ACTION_ATTR_PUSH_ETH
: {
1006 const struct ovs_action_push_eth
*eth
= nl_attr_get(a
);
1008 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
1009 push_eth(packet
, ð
->addresses
.eth_dst
,
1010 ð
->addresses
.eth_src
);
1015 case OVS_ACTION_ATTR_POP_ETH
:
1016 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
1021 case OVS_ACTION_ATTR_PUSH_NSH
: {
1022 uint32_t buffer
[NSH_HDR_MAX_LEN
/ 4];
1023 struct nsh_hdr
*nsh_hdr
= ALIGNED_CAST(struct nsh_hdr
*, buffer
);
1024 nsh_reset_ver_flags_ttl_len(nsh_hdr
);
1025 odp_nsh_hdr_from_attr(nl_attr_get(a
), nsh_hdr
, NSH_HDR_MAX_LEN
);
1026 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
1027 push_nsh(packet
, nsh_hdr
);
1031 case OVS_ACTION_ATTR_POP_NSH
: {
1033 const size_t num
= dp_packet_batch_size(batch
);
1035 DP_PACKET_BATCH_REFILL_FOR_EACH (i
, num
, packet
, batch
) {
1036 if (pop_nsh(packet
)) {
1037 dp_packet_batch_refill(batch
, packet
, i
);
1039 COVERAGE_INC(datapath_drop_nsh_decap_error
);
1040 dp_packet_delete(packet
);
1045 case OVS_ACTION_ATTR_CT_CLEAR
:
1046 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
1047 conntrack_clear(packet
);
1051 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
1052 DP_PACKET_BATCH_FOR_EACH (i
, packet
, batch
) {
1053 odp_execute_check_pkt_len(dp
, packet
, steal
&& last_action
, a
,
1058 /* We do not need to free the packets.
1059 * odp_execute_check_pkt_len() has stolen them. */
1064 case OVS_ACTION_ATTR_DROP
:{
1065 const enum xlate_error
*drop_reason
= nl_attr_get(a
);
1067 dp_update_drop_action_counter(*drop_reason
,
1068 dp_packet_batch_size(batch
));
1069 dp_packet_delete_batch(batch
, steal
);
1072 case OVS_ACTION_ATTR_OUTPUT
:
1073 case OVS_ACTION_ATTR_LB_OUTPUT
:
1074 case OVS_ACTION_ATTR_TUNNEL_PUSH
:
1075 case OVS_ACTION_ATTR_TUNNEL_POP
:
1076 case OVS_ACTION_ATTR_USERSPACE
:
1077 case OVS_ACTION_ATTR_RECIRC
:
1078 case OVS_ACTION_ATTR_CT
:
1079 case OVS_ACTION_ATTR_UNSPEC
:
1080 case __OVS_ACTION_ATTR_MAX
:
1085 dp_packet_delete_batch(batch
, steal
);