2 * Copyright (c) 2007-2017 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/skbuff.h>
24 #include <linux/openvswitch.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/in6.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_vlan.h>
36 #include <net/checksum.h>
37 #include <net/dsfield.h>
39 #include <net/sctp/checksum.h>
42 #include "conntrack.h"
46 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
47 struct sw_flow_key
*key
,
48 const struct nlattr
*attr
, int len
);
50 struct deferred_action
{
52 const struct nlattr
*actions
;
55 /* Store pkt_key clone when creating deferred action. */
56 struct sw_flow_key pkt_key
;
59 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
60 struct ovs_frag_data
{
64 __be16 inner_protocol
;
69 u8 l2_data
[MAX_L2_LEN
];
72 static DEFINE_PER_CPU(struct ovs_frag_data
, ovs_frag_data_storage
);
74 #define DEFERRED_ACTION_FIFO_SIZE 10
75 #define OVS_RECURSION_LIMIT 4
76 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
80 /* Deferred action fifo queue storage. */
81 struct deferred_action fifo
[DEFERRED_ACTION_FIFO_SIZE
];
84 struct action_flow_keys
{
85 struct sw_flow_key key
[OVS_DEFERRED_ACTION_THRESHOLD
];
88 static struct action_fifo __percpu
*action_fifos
;
89 static struct action_flow_keys __percpu
*flow_keys
;
90 static DEFINE_PER_CPU(int, exec_actions_level
);
92 /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys'
93 * space. Return NULL if out of key spaces.
95 static struct sw_flow_key
*clone_key(const struct sw_flow_key
*key_
)
97 struct action_flow_keys
*keys
= this_cpu_ptr(flow_keys
);
98 int level
= this_cpu_read(exec_actions_level
);
99 struct sw_flow_key
*key
= NULL
;
101 if (level
<= OVS_DEFERRED_ACTION_THRESHOLD
) {
102 key
= &keys
->key
[level
- 1];
109 static void action_fifo_init(struct action_fifo
*fifo
)
115 static bool action_fifo_is_empty(const struct action_fifo
*fifo
)
117 return (fifo
->head
== fifo
->tail
);
120 static struct deferred_action
*action_fifo_get(struct action_fifo
*fifo
)
122 if (action_fifo_is_empty(fifo
))
125 return &fifo
->fifo
[fifo
->tail
++];
128 static struct deferred_action
*action_fifo_put(struct action_fifo
*fifo
)
130 if (fifo
->head
>= DEFERRED_ACTION_FIFO_SIZE
- 1)
133 return &fifo
->fifo
[fifo
->head
++];
136 /* Return queue entry if fifo is not full */
137 static struct deferred_action
*add_deferred_actions(struct sk_buff
*skb
,
138 const struct sw_flow_key
*key
,
139 const struct nlattr
*actions
,
140 const int actions_len
)
142 struct action_fifo
*fifo
;
143 struct deferred_action
*da
;
145 fifo
= this_cpu_ptr(action_fifos
);
146 da
= action_fifo_put(fifo
);
149 da
->actions
= actions
;
150 da
->actions_len
= actions_len
;
157 static void invalidate_flow_key(struct sw_flow_key
*key
)
159 key
->mac_proto
|= SW_FLOW_KEY_INVALID
;
162 static bool is_flow_key_valid(const struct sw_flow_key
*key
)
164 return !(key
->mac_proto
& SW_FLOW_KEY_INVALID
);
167 static int clone_execute(struct datapath
*dp
, struct sk_buff
*skb
,
168 struct sw_flow_key
*key
,
170 const struct nlattr
*actions
, int len
,
171 bool last
, bool clone_flow_key
);
173 static void update_ethertype(struct sk_buff
*skb
, struct ethhdr
*hdr
,
176 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
177 __be16 diff
[] = { ~(hdr
->h_proto
), ethertype
};
179 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
183 hdr
->h_proto
= ethertype
;
186 static int push_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
187 const struct ovs_action_push_mpls
*mpls
)
189 __be32
*new_mpls_lse
;
191 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
192 if (skb
->encapsulation
)
195 if (skb_cow_head(skb
, MPLS_HLEN
) < 0)
198 skb_push(skb
, MPLS_HLEN
);
199 memmove(skb_mac_header(skb
) - MPLS_HLEN
, skb_mac_header(skb
),
201 skb_reset_mac_header(skb
);
203 new_mpls_lse
= (__be32
*)skb_mpls_header(skb
);
204 *new_mpls_lse
= mpls
->mpls_lse
;
206 skb_postpush_rcsum(skb
, new_mpls_lse
, MPLS_HLEN
);
208 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
)
209 update_ethertype(skb
, eth_hdr(skb
), mpls
->mpls_ethertype
);
210 if (!ovs_skb_get_inner_protocol(skb
))
211 ovs_skb_set_inner_protocol(skb
, skb
->protocol
);
212 skb
->protocol
= mpls
->mpls_ethertype
;
214 invalidate_flow_key(key
);
218 static int pop_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
219 const __be16 ethertype
)
223 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
227 skb_postpull_rcsum(skb
, skb_mpls_header(skb
), MPLS_HLEN
);
229 memmove(skb_mac_header(skb
) + MPLS_HLEN
, skb_mac_header(skb
),
232 __skb_pull(skb
, MPLS_HLEN
);
233 skb_reset_mac_header(skb
);
235 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
) {
238 /* skb_mpls_header() is used to locate the ethertype
239 * field correctly in the presence of VLAN tags.
241 hdr
= (struct ethhdr
*)(skb_mpls_header(skb
) - ETH_HLEN
);
242 update_ethertype(skb
, hdr
, ethertype
);
244 if (eth_p_mpls(skb
->protocol
))
245 skb
->protocol
= ethertype
;
247 invalidate_flow_key(key
);
251 static int set_mpls(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
252 const __be32
*mpls_lse
, const __be32
*mask
)
258 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
262 stack
= (__be32
*)skb_mpls_header(skb
);
263 lse
= OVS_MASKED(*stack
, *mpls_lse
, *mask
);
264 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
265 __be32 diff
[] = { ~(*stack
), lse
};
267 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
272 flow_key
->mpls
.top_lse
= lse
;
276 static int pop_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
280 err
= skb_vlan_pop(skb
);
281 if (skb_vlan_tag_present(skb
)) {
282 invalidate_flow_key(key
);
284 key
->eth
.vlan
.tci
= 0;
285 key
->eth
.vlan
.tpid
= 0;
290 static int push_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
,
291 const struct ovs_action_push_vlan
*vlan
)
293 if (skb_vlan_tag_present(skb
)) {
294 invalidate_flow_key(key
);
296 key
->eth
.vlan
.tci
= vlan
->vlan_tci
;
297 key
->eth
.vlan
.tpid
= vlan
->vlan_tpid
;
299 return skb_vlan_push(skb
, vlan
->vlan_tpid
,
300 ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
303 /* 'src' is already properly masked. */
304 static void ether_addr_copy_masked(u8
*dst_
, const u8
*src_
, const u8
*mask_
)
306 u16
*dst
= (u16
*)dst_
;
307 const u16
*src
= (const u16
*)src_
;
308 const u16
*mask
= (const u16
*)mask_
;
310 OVS_SET_MASKED(dst
[0], src
[0], mask
[0]);
311 OVS_SET_MASKED(dst
[1], src
[1], mask
[1]);
312 OVS_SET_MASKED(dst
[2], src
[2], mask
[2]);
315 static int set_eth_addr(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
316 const struct ovs_key_ethernet
*key
,
317 const struct ovs_key_ethernet
*mask
)
321 err
= skb_ensure_writable(skb
, ETH_HLEN
);
325 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
327 ether_addr_copy_masked(eth_hdr(skb
)->h_source
, key
->eth_src
,
329 ether_addr_copy_masked(eth_hdr(skb
)->h_dest
, key
->eth_dst
,
332 skb_postpush_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
334 ether_addr_copy(flow_key
->eth
.src
, eth_hdr(skb
)->h_source
);
335 ether_addr_copy(flow_key
->eth
.dst
, eth_hdr(skb
)->h_dest
);
339 /* pop_eth does not support VLAN packets as this action is never called
342 static int pop_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
)
344 skb_pull_rcsum(skb
, ETH_HLEN
);
345 skb_reset_mac_header(skb
);
346 skb_reset_mac_len(skb
);
348 /* safe right before invalidate_flow_key */
349 key
->mac_proto
= MAC_PROTO_NONE
;
350 invalidate_flow_key(key
);
354 static int push_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
,
355 const struct ovs_action_push_eth
*ethh
)
359 /* Add the new Ethernet header */
360 if (skb_cow_head(skb
, ETH_HLEN
) < 0)
363 skb_push(skb
, ETH_HLEN
);
364 skb_reset_mac_header(skb
);
365 skb_reset_mac_len(skb
);
368 ether_addr_copy(hdr
->h_source
, ethh
->addresses
.eth_src
);
369 ether_addr_copy(hdr
->h_dest
, ethh
->addresses
.eth_dst
);
370 hdr
->h_proto
= skb
->protocol
;
372 skb_postpush_rcsum(skb
, hdr
, ETH_HLEN
);
374 /* safe right before invalidate_flow_key */
375 key
->mac_proto
= MAC_PROTO_ETHERNET
;
376 invalidate_flow_key(key
);
380 static void update_ip_l4_checksum(struct sk_buff
*skb
, struct iphdr
*nh
,
381 __be32 addr
, __be32 new_addr
)
383 int transport_len
= skb
->len
- skb_transport_offset(skb
);
385 if (nh
->frag_off
& htons(IP_OFFSET
))
388 if (nh
->protocol
== IPPROTO_TCP
) {
389 if (likely(transport_len
>= sizeof(struct tcphdr
)))
390 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
391 addr
, new_addr
, true);
392 } else if (nh
->protocol
== IPPROTO_UDP
) {
393 if (likely(transport_len
>= sizeof(struct udphdr
))) {
394 struct udphdr
*uh
= udp_hdr(skb
);
396 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
397 inet_proto_csum_replace4(&uh
->check
, skb
,
398 addr
, new_addr
, true);
400 uh
->check
= CSUM_MANGLED_0
;
407 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
408 __be32
*addr
, __be32 new_addr
)
410 update_ip_l4_checksum(skb
, nh
, *addr
, new_addr
);
411 csum_replace4(&nh
->check
, *addr
, new_addr
);
416 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
417 __be32 addr
[4], const __be32 new_addr
[4])
419 int transport_len
= skb
->len
- skb_transport_offset(skb
);
421 if (l4_proto
== NEXTHDR_TCP
) {
422 if (likely(transport_len
>= sizeof(struct tcphdr
)))
423 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
424 addr
, new_addr
, true);
425 } else if (l4_proto
== NEXTHDR_UDP
) {
426 if (likely(transport_len
>= sizeof(struct udphdr
))) {
427 struct udphdr
*uh
= udp_hdr(skb
);
429 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
430 inet_proto_csum_replace16(&uh
->check
, skb
,
431 addr
, new_addr
, true);
433 uh
->check
= CSUM_MANGLED_0
;
436 } else if (l4_proto
== NEXTHDR_ICMP
) {
437 if (likely(transport_len
>= sizeof(struct icmp6hdr
)))
438 inet_proto_csum_replace16(&icmp6_hdr(skb
)->icmp6_cksum
,
439 skb
, addr
, new_addr
, true);
443 static void mask_ipv6_addr(const __be32 old
[4], const __be32 addr
[4],
444 const __be32 mask
[4], __be32 masked
[4])
446 masked
[0] = OVS_MASKED(old
[0], addr
[0], mask
[0]);
447 masked
[1] = OVS_MASKED(old
[1], addr
[1], mask
[1]);
448 masked
[2] = OVS_MASKED(old
[2], addr
[2], mask
[2]);
449 masked
[3] = OVS_MASKED(old
[3], addr
[3], mask
[3]);
452 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
453 __be32 addr
[4], const __be32 new_addr
[4],
454 bool recalculate_csum
)
456 if (likely(recalculate_csum
))
457 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
460 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
463 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
, u32 mask
)
465 /* Bits 21-24 are always unmasked, so this retains their values. */
466 OVS_SET_MASKED(nh
->flow_lbl
[0], (u8
)(fl
>> 16), (u8
)(mask
>> 16));
467 OVS_SET_MASKED(nh
->flow_lbl
[1], (u8
)(fl
>> 8), (u8
)(mask
>> 8));
468 OVS_SET_MASKED(nh
->flow_lbl
[2], (u8
)fl
, (u8
)mask
);
471 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
,
474 new_ttl
= OVS_MASKED(nh
->ttl
, new_ttl
, mask
);
476 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
480 static int set_ipv4(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
481 const struct ovs_key_ipv4
*key
,
482 const struct ovs_key_ipv4
*mask
)
488 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
489 sizeof(struct iphdr
));
495 /* Setting an IP addresses is typically only a side effect of
496 * matching on them in the current userspace implementation, so it
497 * makes sense to check if the value actually changed.
499 if (mask
->ipv4_src
) {
500 new_addr
= OVS_MASKED(nh
->saddr
, key
->ipv4_src
, mask
->ipv4_src
);
502 if (unlikely(new_addr
!= nh
->saddr
)) {
503 set_ip_addr(skb
, nh
, &nh
->saddr
, new_addr
);
504 flow_key
->ipv4
.addr
.src
= new_addr
;
507 if (mask
->ipv4_dst
) {
508 new_addr
= OVS_MASKED(nh
->daddr
, key
->ipv4_dst
, mask
->ipv4_dst
);
510 if (unlikely(new_addr
!= nh
->daddr
)) {
511 set_ip_addr(skb
, nh
, &nh
->daddr
, new_addr
);
512 flow_key
->ipv4
.addr
.dst
= new_addr
;
515 if (mask
->ipv4_tos
) {
516 ipv4_change_dsfield(nh
, ~mask
->ipv4_tos
, key
->ipv4_tos
);
517 flow_key
->ip
.tos
= nh
->tos
;
519 if (mask
->ipv4_ttl
) {
520 set_ip_ttl(skb
, nh
, key
->ipv4_ttl
, mask
->ipv4_ttl
);
521 flow_key
->ip
.ttl
= nh
->ttl
;
527 static bool is_ipv6_mask_nonzero(const __be32 addr
[4])
529 return !!(addr
[0] | addr
[1] | addr
[2] | addr
[3]);
532 static int set_ipv6(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
533 const struct ovs_key_ipv6
*key
,
534 const struct ovs_key_ipv6
*mask
)
539 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
540 sizeof(struct ipv6hdr
));
546 /* Setting an IP addresses is typically only a side effect of
547 * matching on them in the current userspace implementation, so it
548 * makes sense to check if the value actually changed.
550 if (is_ipv6_mask_nonzero(mask
->ipv6_src
)) {
551 __be32
*saddr
= (__be32
*)&nh
->saddr
;
554 mask_ipv6_addr(saddr
, key
->ipv6_src
, mask
->ipv6_src
, masked
);
556 if (unlikely(memcmp(saddr
, masked
, sizeof(masked
)))) {
557 set_ipv6_addr(skb
, flow_key
->ip
.proto
, saddr
, masked
,
559 memcpy(&flow_key
->ipv6
.addr
.src
, masked
,
560 sizeof(flow_key
->ipv6
.addr
.src
));
563 if (is_ipv6_mask_nonzero(mask
->ipv6_dst
)) {
564 unsigned int offset
= 0;
565 int flags
= IP6_FH_F_SKIP_RH
;
566 bool recalc_csum
= true;
567 __be32
*daddr
= (__be32
*)&nh
->daddr
;
570 mask_ipv6_addr(daddr
, key
->ipv6_dst
, mask
->ipv6_dst
, masked
);
572 if (unlikely(memcmp(daddr
, masked
, sizeof(masked
)))) {
573 if (ipv6_ext_hdr(nh
->nexthdr
))
574 recalc_csum
= (ipv6_find_hdr(skb
, &offset
,
579 set_ipv6_addr(skb
, flow_key
->ip
.proto
, daddr
, masked
,
581 memcpy(&flow_key
->ipv6
.addr
.dst
, masked
,
582 sizeof(flow_key
->ipv6
.addr
.dst
));
585 if (mask
->ipv6_tclass
) {
586 ipv6_change_dsfield(nh
, ~mask
->ipv6_tclass
, key
->ipv6_tclass
);
587 flow_key
->ip
.tos
= ipv6_get_dsfield(nh
);
589 if (mask
->ipv6_label
) {
590 set_ipv6_fl(nh
, ntohl(key
->ipv6_label
),
591 ntohl(mask
->ipv6_label
));
592 flow_key
->ipv6
.label
=
593 *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
595 if (mask
->ipv6_hlimit
) {
596 OVS_SET_MASKED(nh
->hop_limit
, key
->ipv6_hlimit
,
598 flow_key
->ip
.ttl
= nh
->hop_limit
;
603 /* Must follow skb_ensure_writable() since that can move the skb data. */
604 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
605 __be16 new_port
, __sum16
*check
)
607 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, false);
611 static int set_udp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
612 const struct ovs_key_udp
*key
,
613 const struct ovs_key_udp
*mask
)
619 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
620 sizeof(struct udphdr
));
625 /* Either of the masks is non-zero, so do not bother checking them. */
626 src
= OVS_MASKED(uh
->source
, key
->udp_src
, mask
->udp_src
);
627 dst
= OVS_MASKED(uh
->dest
, key
->udp_dst
, mask
->udp_dst
);
629 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
630 if (likely(src
!= uh
->source
)) {
631 set_tp_port(skb
, &uh
->source
, src
, &uh
->check
);
632 flow_key
->tp
.src
= src
;
634 if (likely(dst
!= uh
->dest
)) {
635 set_tp_port(skb
, &uh
->dest
, dst
, &uh
->check
);
636 flow_key
->tp
.dst
= dst
;
639 if (unlikely(!uh
->check
))
640 uh
->check
= CSUM_MANGLED_0
;
644 flow_key
->tp
.src
= src
;
645 flow_key
->tp
.dst
= dst
;
653 static int set_tcp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
654 const struct ovs_key_tcp
*key
,
655 const struct ovs_key_tcp
*mask
)
661 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
662 sizeof(struct tcphdr
));
667 src
= OVS_MASKED(th
->source
, key
->tcp_src
, mask
->tcp_src
);
668 if (likely(src
!= th
->source
)) {
669 set_tp_port(skb
, &th
->source
, src
, &th
->check
);
670 flow_key
->tp
.src
= src
;
672 dst
= OVS_MASKED(th
->dest
, key
->tcp_dst
, mask
->tcp_dst
);
673 if (likely(dst
!= th
->dest
)) {
674 set_tp_port(skb
, &th
->dest
, dst
, &th
->check
);
675 flow_key
->tp
.dst
= dst
;
682 static int set_sctp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
683 const struct ovs_key_sctp
*key
,
684 const struct ovs_key_sctp
*mask
)
686 unsigned int sctphoff
= skb_transport_offset(skb
);
688 __le32 old_correct_csum
, new_csum
, old_csum
;
691 err
= skb_ensure_writable(skb
, sctphoff
+ sizeof(struct sctphdr
));
696 old_csum
= sh
->checksum
;
697 old_correct_csum
= sctp_compute_cksum(skb
, sctphoff
);
699 sh
->source
= OVS_MASKED(sh
->source
, key
->sctp_src
, mask
->sctp_src
);
700 sh
->dest
= OVS_MASKED(sh
->dest
, key
->sctp_dst
, mask
->sctp_dst
);
702 new_csum
= sctp_compute_cksum(skb
, sctphoff
);
704 /* Carry any checksum errors through. */
705 sh
->checksum
= old_csum
^ old_correct_csum
^ new_csum
;
708 flow_key
->tp
.src
= sh
->source
;
709 flow_key
->tp
.dst
= sh
->dest
;
714 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS
)
716 struct ovs_frag_data
*data
= this_cpu_ptr(&ovs_frag_data_storage
);
717 struct vport
*vport
= data
->vport
;
719 if (skb_cow_head(skb
, data
->l2_len
) < 0) {
724 __skb_dst_copy(skb
, data
->dst
);
725 *OVS_GSO_CB(skb
) = data
->cb
;
726 ovs_skb_set_inner_protocol(skb
, data
->inner_protocol
);
727 skb
->vlan_tci
= data
->vlan_tci
;
728 skb
->vlan_proto
= data
->vlan_proto
;
730 /* Reconstruct the MAC header. */
731 skb_push(skb
, data
->l2_len
);
732 memcpy(skb
->data
, &data
->l2_data
, data
->l2_len
);
733 skb_postpush_rcsum(skb
, skb
->data
, data
->l2_len
);
734 skb_reset_mac_header(skb
);
736 ovs_vport_send(vport
, skb
, data
->mac_proto
);
741 ovs_dst_get_mtu(const struct dst_entry
*dst
)
743 return dst
->dev
->mtu
;
746 static struct dst_ops ovs_dst_ops
= {
748 .mtu
= ovs_dst_get_mtu
,
751 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
752 * ovs_vport_output(), which is called once per fragmented packet.
754 static void prepare_frag(struct vport
*vport
, struct sk_buff
*skb
,
757 unsigned int hlen
= skb_network_offset(skb
);
758 struct ovs_frag_data
*data
;
760 data
= this_cpu_ptr(&ovs_frag_data_storage
);
761 data
->dst
= (unsigned long) skb_dst(skb
);
763 data
->cb
= *OVS_GSO_CB(skb
);
764 data
->inner_protocol
= ovs_skb_get_inner_protocol(skb
);
765 data
->vlan_tci
= skb
->vlan_tci
;
766 data
->vlan_proto
= skb
->vlan_proto
;
767 data
->mac_proto
= mac_proto
;
769 memcpy(&data
->l2_data
, skb
->data
, hlen
);
771 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
775 static void ovs_fragment(struct net
*net
, struct vport
*vport
,
776 struct sk_buff
*skb
, u16 mru
,
777 struct sw_flow_key
*key
)
779 if (skb_network_offset(skb
) > MAX_L2_LEN
) {
780 OVS_NLERR(1, "L2 header too long to fragment");
784 if (key
->eth
.type
== htons(ETH_P_IP
)) {
785 struct dst_entry ovs_dst
;
786 unsigned long orig_dst
;
788 prepare_frag(vport
, skb
, ovs_key_mac_proto(key
));
789 dst_init(&ovs_dst
, &ovs_dst_ops
, NULL
, 1,
790 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
791 ovs_dst
.dev
= vport
->dev
;
793 orig_dst
= (unsigned long) skb_dst(skb
);
794 skb_dst_set_noref(skb
, &ovs_dst
);
795 IPCB(skb
)->frag_max_size
= mru
;
797 ip_do_fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
798 refdst_drop(orig_dst
);
799 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
800 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
801 unsigned long orig_dst
;
802 struct rt6_info ovs_rt
;
807 prepare_frag(vport
, skb
,
808 ovs_key_mac_proto(key
));
809 memset(&ovs_rt
, 0, sizeof(ovs_rt
));
810 dst_init(&ovs_rt
.dst
, &ovs_dst_ops
, NULL
, 1,
811 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
812 ovs_rt
.dst
.dev
= vport
->dev
;
814 orig_dst
= (unsigned long) skb_dst(skb
);
815 skb_dst_set_noref(skb
, &ovs_rt
.dst
);
816 IP6CB(skb
)->frag_max_size
= mru
;
817 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
818 v6ops
->fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
820 v6ops
->fragment(skb
->sk
, skb
, ovs_vport_output
);
822 refdst_drop(orig_dst
);
824 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
825 ovs_vport_name(vport
), ntohs(key
->eth
.type
), mru
,
835 static void do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
,
836 struct sw_flow_key
*key
)
838 struct vport
*vport
= ovs_vport_rcu(dp
, out_port
);
841 u16 mru
= OVS_CB(skb
)->mru
;
842 u32 cutlen
= OVS_CB(skb
)->cutlen
;
844 if (unlikely(cutlen
> 0)) {
845 if (skb
->len
- cutlen
> ovs_mac_header_len(key
))
846 pskb_trim(skb
, skb
->len
- cutlen
);
848 pskb_trim(skb
, ovs_mac_header_len(key
));
852 (skb
->len
<= mru
+ vport
->dev
->hard_header_len
))) {
853 ovs_vport_send(vport
, skb
, ovs_key_mac_proto(key
));
854 } else if (mru
<= vport
->dev
->mtu
) {
855 struct net
*net
= ovs_dp_get_net(dp
);
857 ovs_fragment(net
, vport
, skb
, mru
, key
);
859 OVS_NLERR(true, "Cannot fragment IP frames");
867 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
868 struct sw_flow_key
*key
, const struct nlattr
*attr
,
869 const struct nlattr
*actions
, int actions_len
,
872 struct dp_upcall_info upcall
;
873 const struct nlattr
*a
;
876 memset(&upcall
, 0, sizeof(upcall
));
877 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
878 upcall
.mru
= OVS_CB(skb
)->mru
;
880 SKB_INIT_FILL_METADATA_DST(skb
);
881 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
882 a
= nla_next(a
, &rem
)) {
883 switch (nla_type(a
)) {
884 case OVS_USERSPACE_ATTR_USERDATA
:
888 case OVS_USERSPACE_ATTR_PID
:
889 upcall
.portid
= nla_get_u32(a
);
892 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
: {
893 /* Get out tunnel info. */
896 vport
= ovs_vport_rcu(dp
, nla_get_u32(a
));
898 err
= dev_fill_metadata_dst(vport
->dev
, skb
);
900 upcall
.egress_tun_info
= skb_tunnel_info(skb
);
906 case OVS_USERSPACE_ATTR_ACTIONS
: {
907 /* Include actions. */
908 upcall
.actions
= actions
;
909 upcall
.actions_len
= actions_len
;
913 } /* End of switch. */
916 err
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, cutlen
);
917 SKB_RESTORE_FILL_METADATA_DST(skb
);
921 /* When 'last' is true, sample() should always consume the 'skb'.
922 * Otherwise, sample() should keep 'skb' intact regardless what
923 * actions are executed within sample().
925 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
926 struct sw_flow_key
*key
, const struct nlattr
*attr
,
929 struct nlattr
*actions
;
930 struct nlattr
*sample_arg
;
931 int rem
= nla_len(attr
);
932 const struct sample_arg
*arg
;
935 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
936 sample_arg
= nla_data(attr
);
937 arg
= nla_data(sample_arg
);
938 actions
= nla_next(sample_arg
, &rem
);
940 if ((arg
->probability
!= U32_MAX
) &&
941 (!arg
->probability
|| prandom_u32() > arg
->probability
)) {
947 clone_flow_key
= !arg
->exec
;
948 return clone_execute(dp
, skb
, key
, 0, actions
, rem
, last
,
952 static void execute_hash(struct sk_buff
*skb
, struct sw_flow_key
*key
,
953 const struct nlattr
*attr
)
955 struct ovs_action_hash
*hash_act
= nla_data(attr
);
958 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
959 hash
= skb_get_hash(skb
);
960 hash
= jhash_1word(hash
, hash_act
->hash_basis
);
964 key
->ovs_flow_hash
= hash
;
967 static int execute_set_action(struct sk_buff
*skb
,
968 struct sw_flow_key
*flow_key
,
969 const struct nlattr
*a
)
971 /* Only tunnel set execution is supported without a mask. */
972 if (nla_type(a
) == OVS_KEY_ATTR_TUNNEL_INFO
) {
973 struct ovs_tunnel_info
*tun
= nla_data(a
);
975 ovs_skb_dst_drop(skb
);
976 ovs_dst_hold((struct dst_entry
*)tun
->tun_dst
);
977 ovs_skb_dst_set(skb
, (struct dst_entry
*)tun
->tun_dst
);
984 /* Mask is at the midpoint of the data. */
985 #define get_mask(a, type) ((const type)nla_data(a) + 1)
987 static int execute_masked_set_action(struct sk_buff
*skb
,
988 struct sw_flow_key
*flow_key
,
989 const struct nlattr
*a
)
993 switch (nla_type(a
)) {
994 case OVS_KEY_ATTR_PRIORITY
:
995 OVS_SET_MASKED(skb
->priority
, nla_get_u32(a
),
996 *get_mask(a
, u32
*));
997 flow_key
->phy
.priority
= skb
->priority
;
1000 case OVS_KEY_ATTR_SKB_MARK
:
1001 OVS_SET_MASKED(skb
->mark
, nla_get_u32(a
), *get_mask(a
, u32
*));
1002 flow_key
->phy
.skb_mark
= skb
->mark
;
1005 case OVS_KEY_ATTR_TUNNEL_INFO
:
1006 /* Masked data not supported for tunnel. */
1010 case OVS_KEY_ATTR_ETHERNET
:
1011 err
= set_eth_addr(skb
, flow_key
, nla_data(a
),
1012 get_mask(a
, struct ovs_key_ethernet
*));
1015 case OVS_KEY_ATTR_IPV4
:
1016 err
= set_ipv4(skb
, flow_key
, nla_data(a
),
1017 get_mask(a
, struct ovs_key_ipv4
*));
1020 case OVS_KEY_ATTR_IPV6
:
1021 err
= set_ipv6(skb
, flow_key
, nla_data(a
),
1022 get_mask(a
, struct ovs_key_ipv6
*));
1025 case OVS_KEY_ATTR_TCP
:
1026 err
= set_tcp(skb
, flow_key
, nla_data(a
),
1027 get_mask(a
, struct ovs_key_tcp
*));
1030 case OVS_KEY_ATTR_UDP
:
1031 err
= set_udp(skb
, flow_key
, nla_data(a
),
1032 get_mask(a
, struct ovs_key_udp
*));
1035 case OVS_KEY_ATTR_SCTP
:
1036 err
= set_sctp(skb
, flow_key
, nla_data(a
),
1037 get_mask(a
, struct ovs_key_sctp
*));
1040 case OVS_KEY_ATTR_MPLS
:
1041 err
= set_mpls(skb
, flow_key
, nla_data(a
), get_mask(a
,
1045 case OVS_KEY_ATTR_CT_STATE
:
1046 case OVS_KEY_ATTR_CT_ZONE
:
1047 case OVS_KEY_ATTR_CT_MARK
:
1048 case OVS_KEY_ATTR_CT_LABELS
:
1049 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
1050 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
1058 static int execute_recirc(struct datapath
*dp
, struct sk_buff
*skb
,
1059 struct sw_flow_key
*key
,
1060 const struct nlattr
*a
, bool last
)
1064 if (!is_flow_key_valid(key
)) {
1067 err
= ovs_flow_key_update(skb
, key
);
1071 BUG_ON(!is_flow_key_valid(key
));
1073 recirc_id
= nla_get_u32(a
);
1074 return clone_execute(dp
, skb
, key
, recirc_id
, NULL
, 0, last
, true);
1077 /* Execute a list of actions against 'skb'. */
1078 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1079 struct sw_flow_key
*key
,
1080 const struct nlattr
*attr
, int len
)
1082 const struct nlattr
*a
;
1085 for (a
= attr
, rem
= len
; rem
> 0;
1086 a
= nla_next(a
, &rem
)) {
1089 switch (nla_type(a
)) {
1090 case OVS_ACTION_ATTR_OUTPUT
: {
1091 int port
= nla_get_u32(a
);
1092 struct sk_buff
*clone
;
1094 /* Every output action needs a separate clone
1095 * of 'skb', In case the output action is the
1096 * last action, cloning can be avoided.
1098 if (nla_is_last(a
, rem
)) {
1099 do_output(dp
, skb
, port
, key
);
1100 /* 'skb' has been used for output.
1105 clone
= skb_clone(skb
, GFP_ATOMIC
);
1107 do_output(dp
, clone
, port
, key
);
1108 OVS_CB(skb
)->cutlen
= 0;
1112 case OVS_ACTION_ATTR_TRUNC
: {
1113 struct ovs_action_trunc
*trunc
= nla_data(a
);
1115 if (skb
->len
> trunc
->max_len
)
1116 OVS_CB(skb
)->cutlen
= skb
->len
- trunc
->max_len
;
1120 case OVS_ACTION_ATTR_USERSPACE
:
1121 output_userspace(dp
, skb
, key
, a
, attr
,
1122 len
, OVS_CB(skb
)->cutlen
);
1123 OVS_CB(skb
)->cutlen
= 0;
1126 case OVS_ACTION_ATTR_HASH
:
1127 execute_hash(skb
, key
, a
);
1130 case OVS_ACTION_ATTR_PUSH_MPLS
:
1131 err
= push_mpls(skb
, key
, nla_data(a
));
1134 case OVS_ACTION_ATTR_POP_MPLS
:
1135 err
= pop_mpls(skb
, key
, nla_get_be16(a
));
1138 case OVS_ACTION_ATTR_PUSH_VLAN
:
1139 err
= push_vlan(skb
, key
, nla_data(a
));
1142 case OVS_ACTION_ATTR_POP_VLAN
:
1143 err
= pop_vlan(skb
, key
);
1146 case OVS_ACTION_ATTR_RECIRC
: {
1147 bool last
= nla_is_last(a
, rem
);
1149 err
= execute_recirc(dp
, skb
, key
, a
, last
);
1151 /* If this is the last action, the skb has
1152 * been consumed or freed.
1153 * Return immediately.
1160 case OVS_ACTION_ATTR_SET
:
1161 err
= execute_set_action(skb
, key
, nla_data(a
));
1164 case OVS_ACTION_ATTR_SET_MASKED
:
1165 case OVS_ACTION_ATTR_SET_TO_MASKED
:
1166 err
= execute_masked_set_action(skb
, key
, nla_data(a
));
1169 case OVS_ACTION_ATTR_SAMPLE
: {
1170 bool last
= nla_is_last(a
, rem
);
1172 err
= sample(dp
, skb
, key
, a
, last
);
1179 case OVS_ACTION_ATTR_CT
:
1180 if (!is_flow_key_valid(key
)) {
1181 err
= ovs_flow_key_update(skb
, key
);
1186 err
= ovs_ct_execute(ovs_dp_get_net(dp
), skb
, key
,
1189 /* Hide stolen IP fragments from user space. */
1191 return err
== -EINPROGRESS
? 0 : err
;
1194 case OVS_ACTION_ATTR_PUSH_ETH
:
1195 err
= push_eth(skb
, key
, nla_data(a
));
1198 case OVS_ACTION_ATTR_POP_ETH
:
1199 err
= pop_eth(skb
, key
);
1203 if (unlikely(err
)) {
1213 /* Execute the actions on the clone of the packet. The effect of the
1214 * execution does not affect the original 'skb' nor the original 'key'.
1216 * The execution may be deferred in case the actions can not be executed
1219 static int clone_execute(struct datapath
*dp
, struct sk_buff
*skb
,
1220 struct sw_flow_key
*key
, u32 recirc_id
,
1221 const struct nlattr
*actions
, int len
,
1222 bool last
, bool clone_flow_key
)
1224 struct deferred_action
*da
;
1225 struct sw_flow_key
*clone
;
1227 skb
= last
? skb
: skb_clone(skb
, GFP_ATOMIC
);
1229 /* Out of memory, skip this action.
1234 /* When clone_flow_key is false, the 'key' will not be change
1235 * by the actions, then the 'key' can be used directly.
1236 * Otherwise, try to clone key from the next recursion level of
1237 * 'flow_keys'. If clone is successful, execute the actions
1238 * without deferring.
1240 clone
= clone_flow_key
? clone_key(key
) : key
;
1244 if (actions
) { /* Sample action */
1246 __this_cpu_inc(exec_actions_level
);
1248 err
= do_execute_actions(dp
, skb
, clone
,
1252 __this_cpu_dec(exec_actions_level
);
1253 } else { /* Recirc action */
1254 clone
->recirc_id
= recirc_id
;
1255 ovs_dp_process_packet(skb
, clone
);
1260 /* Out of 'flow_keys' space. Defer actions */
1261 da
= add_deferred_actions(skb
, key
, actions
, len
);
1263 if (!actions
) { /* Recirc action */
1265 key
->recirc_id
= recirc_id
;
1268 /* Out of per CPU action FIFO space. Drop the 'skb' and
1273 if (net_ratelimit()) {
1274 if (actions
) { /* Sample action */
1275 pr_warn("%s: deferred action limit reached, drop sample action\n",
1277 } else { /* Recirc action */
1278 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1286 static void process_deferred_actions(struct datapath
*dp
)
1288 struct action_fifo
*fifo
= this_cpu_ptr(action_fifos
);
1290 /* Do not touch the FIFO in case there is no deferred actions. */
1291 if (action_fifo_is_empty(fifo
))
1294 /* Finishing executing all deferred actions. */
1296 struct deferred_action
*da
= action_fifo_get(fifo
);
1297 struct sk_buff
*skb
= da
->skb
;
1298 struct sw_flow_key
*key
= &da
->pkt_key
;
1299 const struct nlattr
*actions
= da
->actions
;
1300 int actions_len
= da
->actions_len
;
1303 do_execute_actions(dp
, skb
, key
, actions
, actions_len
);
1305 ovs_dp_process_packet(skb
, key
);
1306 } while (!action_fifo_is_empty(fifo
));
1308 /* Reset FIFO for the next packet. */
1309 action_fifo_init(fifo
);
1312 /* Execute a list of actions against 'skb'. */
1313 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1314 const struct sw_flow_actions
*acts
,
1315 struct sw_flow_key
*key
)
1319 level
= __this_cpu_inc_return(exec_actions_level
);
1320 if (unlikely(level
> OVS_RECURSION_LIMIT
)) {
1321 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1328 err
= do_execute_actions(dp
, skb
, key
,
1329 acts
->actions
, acts
->actions_len
);
1332 process_deferred_actions(dp
);
1335 __this_cpu_dec(exec_actions_level
);
1339 int action_fifos_init(void)
1341 action_fifos
= alloc_percpu(struct action_fifo
);
1345 flow_keys
= alloc_percpu(struct action_flow_keys
);
1347 free_percpu(action_fifos
);
1354 void action_fifos_exit(void)
1356 free_percpu(action_fifos
);
1357 free_percpu(flow_keys
);