2 * Copyright (c) 2007-2015 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
;
54 /* Store pkt_key clone when creating deferred action. */
55 struct sw_flow_key pkt_key
;
58 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
59 struct ovs_frag_data
{
63 __be16 inner_protocol
;
68 u8 l2_data
[MAX_L2_LEN
];
71 static DEFINE_PER_CPU(struct ovs_frag_data
, ovs_frag_data_storage
);
73 #define DEFERRED_ACTION_FIFO_SIZE 10
74 #define OVS_RECURSION_LIMIT 4
75 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
79 /* Deferred action fifo queue storage. */
80 struct deferred_action fifo
[DEFERRED_ACTION_FIFO_SIZE
];
84 struct sw_flow_key key
[OVS_DEFERRED_ACTION_THRESHOLD
];
87 static struct action_fifo __percpu
*action_fifos
;
88 static struct recirc_keys __percpu
*recirc_keys
;
89 static DEFINE_PER_CPU(int, exec_actions_level
);
91 static void action_fifo_init(struct action_fifo
*fifo
)
97 static bool action_fifo_is_empty(const struct action_fifo
*fifo
)
99 return (fifo
->head
== fifo
->tail
);
102 static struct deferred_action
*action_fifo_get(struct action_fifo
*fifo
)
104 if (action_fifo_is_empty(fifo
))
107 return &fifo
->fifo
[fifo
->tail
++];
110 static struct deferred_action
*action_fifo_put(struct action_fifo
*fifo
)
112 if (fifo
->head
>= DEFERRED_ACTION_FIFO_SIZE
- 1)
115 return &fifo
->fifo
[fifo
->head
++];
118 /* Return queue entry if fifo is not full */
119 static struct deferred_action
*add_deferred_actions(struct sk_buff
*skb
,
120 const struct sw_flow_key
*key
,
121 const struct nlattr
*attr
)
123 struct action_fifo
*fifo
;
124 struct deferred_action
*da
;
126 fifo
= this_cpu_ptr(action_fifos
);
127 da
= action_fifo_put(fifo
);
137 static void invalidate_flow_key(struct sw_flow_key
*key
)
139 key
->mac_proto
|= SW_FLOW_KEY_INVALID
;
142 static bool is_flow_key_valid(const struct sw_flow_key
*key
)
144 return !(key
->mac_proto
& SW_FLOW_KEY_INVALID
);
147 static void update_ethertype(struct sk_buff
*skb
, struct ethhdr
*hdr
,
150 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
151 __be16 diff
[] = { ~(hdr
->h_proto
), ethertype
};
153 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
157 hdr
->h_proto
= ethertype
;
160 static int push_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
161 const struct ovs_action_push_mpls
*mpls
)
163 __be32
*new_mpls_lse
;
165 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
166 if (skb
->encapsulation
)
169 if (skb_cow_head(skb
, MPLS_HLEN
) < 0)
172 skb_push(skb
, MPLS_HLEN
);
173 memmove(skb_mac_header(skb
) - MPLS_HLEN
, skb_mac_header(skb
),
175 skb_reset_mac_header(skb
);
177 new_mpls_lse
= (__be32
*)skb_mpls_header(skb
);
178 *new_mpls_lse
= mpls
->mpls_lse
;
180 skb_postpush_rcsum(skb
, new_mpls_lse
, MPLS_HLEN
);
182 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
)
183 update_ethertype(skb
, eth_hdr(skb
), mpls
->mpls_ethertype
);
184 if (!ovs_skb_get_inner_protocol(skb
))
185 ovs_skb_set_inner_protocol(skb
, skb
->protocol
);
186 skb
->protocol
= mpls
->mpls_ethertype
;
188 invalidate_flow_key(key
);
192 static int pop_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
193 const __be16 ethertype
)
197 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
201 skb_postpull_rcsum(skb
, skb_mpls_header(skb
), MPLS_HLEN
);
203 memmove(skb_mac_header(skb
) + MPLS_HLEN
, skb_mac_header(skb
),
206 __skb_pull(skb
, MPLS_HLEN
);
207 skb_reset_mac_header(skb
);
209 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
) {
212 /* skb_mpls_header() is used to locate the ethertype
213 * field correctly in the presence of VLAN tags.
215 hdr
= (struct ethhdr
*)(skb_mpls_header(skb
) - ETH_HLEN
);
216 update_ethertype(skb
, hdr
, ethertype
);
218 if (eth_p_mpls(skb
->protocol
))
219 skb
->protocol
= ethertype
;
221 invalidate_flow_key(key
);
225 static int set_mpls(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
226 const __be32
*mpls_lse
, const __be32
*mask
)
232 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
236 stack
= (__be32
*)skb_mpls_header(skb
);
237 lse
= OVS_MASKED(*stack
, *mpls_lse
, *mask
);
238 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
239 __be32 diff
[] = { ~(*stack
), lse
};
241 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
246 flow_key
->mpls
.top_lse
= lse
;
250 static int pop_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
254 err
= skb_vlan_pop(skb
);
255 if (skb_vlan_tag_present(skb
)) {
256 invalidate_flow_key(key
);
258 key
->eth
.vlan
.tci
= 0;
259 key
->eth
.vlan
.tpid
= 0;
264 static int push_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
,
265 const struct ovs_action_push_vlan
*vlan
)
267 if (skb_vlan_tag_present(skb
)) {
268 invalidate_flow_key(key
);
270 key
->eth
.vlan
.tci
= vlan
->vlan_tci
;
271 key
->eth
.vlan
.tpid
= vlan
->vlan_tpid
;
273 return skb_vlan_push(skb
, vlan
->vlan_tpid
,
274 ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
277 /* 'src' is already properly masked. */
278 static void ether_addr_copy_masked(u8
*dst_
, const u8
*src_
, const u8
*mask_
)
280 u16
*dst
= (u16
*)dst_
;
281 const u16
*src
= (const u16
*)src_
;
282 const u16
*mask
= (const u16
*)mask_
;
284 OVS_SET_MASKED(dst
[0], src
[0], mask
[0]);
285 OVS_SET_MASKED(dst
[1], src
[1], mask
[1]);
286 OVS_SET_MASKED(dst
[2], src
[2], mask
[2]);
289 static int set_eth_addr(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
290 const struct ovs_key_ethernet
*key
,
291 const struct ovs_key_ethernet
*mask
)
295 err
= skb_ensure_writable(skb
, ETH_HLEN
);
299 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
301 ether_addr_copy_masked(eth_hdr(skb
)->h_source
, key
->eth_src
,
303 ether_addr_copy_masked(eth_hdr(skb
)->h_dest
, key
->eth_dst
,
306 skb_postpush_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
308 ether_addr_copy(flow_key
->eth
.src
, eth_hdr(skb
)->h_source
);
309 ether_addr_copy(flow_key
->eth
.dst
, eth_hdr(skb
)->h_dest
);
313 /* pop_eth does not support VLAN packets as this action is never called
316 static int pop_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
)
318 skb_pull_rcsum(skb
, ETH_HLEN
);
319 skb_reset_mac_header(skb
);
320 skb_reset_mac_len(skb
);
322 /* safe right before invalidate_flow_key */
323 key
->mac_proto
= MAC_PROTO_NONE
;
324 invalidate_flow_key(key
);
328 static int push_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
,
329 const struct ovs_action_push_eth
*ethh
)
333 /* Add the new Ethernet header */
334 if (skb_cow_head(skb
, ETH_HLEN
) < 0)
337 skb_push(skb
, ETH_HLEN
);
338 skb_reset_mac_header(skb
);
339 skb_reset_mac_len(skb
);
342 ether_addr_copy(hdr
->h_source
, ethh
->addresses
.eth_src
);
343 ether_addr_copy(hdr
->h_dest
, ethh
->addresses
.eth_dst
);
344 hdr
->h_proto
= skb
->protocol
;
346 skb_postpush_rcsum(skb
, hdr
, ETH_HLEN
);
348 /* safe right before invalidate_flow_key */
349 key
->mac_proto
= MAC_PROTO_ETHERNET
;
350 invalidate_flow_key(key
);
354 static void update_ip_l4_checksum(struct sk_buff
*skb
, struct iphdr
*nh
,
355 __be32 addr
, __be32 new_addr
)
357 int transport_len
= skb
->len
- skb_transport_offset(skb
);
359 if (nh
->frag_off
& htons(IP_OFFSET
))
362 if (nh
->protocol
== IPPROTO_TCP
) {
363 if (likely(transport_len
>= sizeof(struct tcphdr
)))
364 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
365 addr
, new_addr
, true);
366 } else if (nh
->protocol
== IPPROTO_UDP
) {
367 if (likely(transport_len
>= sizeof(struct udphdr
))) {
368 struct udphdr
*uh
= udp_hdr(skb
);
370 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
371 inet_proto_csum_replace4(&uh
->check
, skb
,
372 addr
, new_addr
, true);
374 uh
->check
= CSUM_MANGLED_0
;
381 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
382 __be32
*addr
, __be32 new_addr
)
384 update_ip_l4_checksum(skb
, nh
, *addr
, new_addr
);
385 csum_replace4(&nh
->check
, *addr
, new_addr
);
390 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
391 __be32 addr
[4], const __be32 new_addr
[4])
393 int transport_len
= skb
->len
- skb_transport_offset(skb
);
395 if (l4_proto
== NEXTHDR_TCP
) {
396 if (likely(transport_len
>= sizeof(struct tcphdr
)))
397 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
398 addr
, new_addr
, true);
399 } else if (l4_proto
== NEXTHDR_UDP
) {
400 if (likely(transport_len
>= sizeof(struct udphdr
))) {
401 struct udphdr
*uh
= udp_hdr(skb
);
403 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
404 inet_proto_csum_replace16(&uh
->check
, skb
,
405 addr
, new_addr
, true);
407 uh
->check
= CSUM_MANGLED_0
;
410 } else if (l4_proto
== NEXTHDR_ICMP
) {
411 if (likely(transport_len
>= sizeof(struct icmp6hdr
)))
412 inet_proto_csum_replace16(&icmp6_hdr(skb
)->icmp6_cksum
,
413 skb
, addr
, new_addr
, true);
417 static void mask_ipv6_addr(const __be32 old
[4], const __be32 addr
[4],
418 const __be32 mask
[4], __be32 masked
[4])
420 masked
[0] = OVS_MASKED(old
[0], addr
[0], mask
[0]);
421 masked
[1] = OVS_MASKED(old
[1], addr
[1], mask
[1]);
422 masked
[2] = OVS_MASKED(old
[2], addr
[2], mask
[2]);
423 masked
[3] = OVS_MASKED(old
[3], addr
[3], mask
[3]);
426 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
427 __be32 addr
[4], const __be32 new_addr
[4],
428 bool recalculate_csum
)
430 if (likely(recalculate_csum
))
431 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
434 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
437 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
, u32 mask
)
439 /* Bits 21-24 are always unmasked, so this retains their values. */
440 OVS_SET_MASKED(nh
->flow_lbl
[0], (u8
)(fl
>> 16), (u8
)(mask
>> 16));
441 OVS_SET_MASKED(nh
->flow_lbl
[1], (u8
)(fl
>> 8), (u8
)(mask
>> 8));
442 OVS_SET_MASKED(nh
->flow_lbl
[2], (u8
)fl
, (u8
)mask
);
445 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
,
448 new_ttl
= OVS_MASKED(nh
->ttl
, new_ttl
, mask
);
450 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
454 static int set_ipv4(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
455 const struct ovs_key_ipv4
*key
,
456 const struct ovs_key_ipv4
*mask
)
462 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
463 sizeof(struct iphdr
));
469 /* Setting an IP addresses is typically only a side effect of
470 * matching on them in the current userspace implementation, so it
471 * makes sense to check if the value actually changed.
473 if (mask
->ipv4_src
) {
474 new_addr
= OVS_MASKED(nh
->saddr
, key
->ipv4_src
, mask
->ipv4_src
);
476 if (unlikely(new_addr
!= nh
->saddr
)) {
477 set_ip_addr(skb
, nh
, &nh
->saddr
, new_addr
);
478 flow_key
->ipv4
.addr
.src
= new_addr
;
481 if (mask
->ipv4_dst
) {
482 new_addr
= OVS_MASKED(nh
->daddr
, key
->ipv4_dst
, mask
->ipv4_dst
);
484 if (unlikely(new_addr
!= nh
->daddr
)) {
485 set_ip_addr(skb
, nh
, &nh
->daddr
, new_addr
);
486 flow_key
->ipv4
.addr
.dst
= new_addr
;
489 if (mask
->ipv4_tos
) {
490 ipv4_change_dsfield(nh
, ~mask
->ipv4_tos
, key
->ipv4_tos
);
491 flow_key
->ip
.tos
= nh
->tos
;
493 if (mask
->ipv4_ttl
) {
494 set_ip_ttl(skb
, nh
, key
->ipv4_ttl
, mask
->ipv4_ttl
);
495 flow_key
->ip
.ttl
= nh
->ttl
;
501 static bool is_ipv6_mask_nonzero(const __be32 addr
[4])
503 return !!(addr
[0] | addr
[1] | addr
[2] | addr
[3]);
506 static int set_ipv6(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
507 const struct ovs_key_ipv6
*key
,
508 const struct ovs_key_ipv6
*mask
)
513 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
514 sizeof(struct ipv6hdr
));
520 /* Setting an IP addresses is typically only a side effect of
521 * matching on them in the current userspace implementation, so it
522 * makes sense to check if the value actually changed.
524 if (is_ipv6_mask_nonzero(mask
->ipv6_src
)) {
525 __be32
*saddr
= (__be32
*)&nh
->saddr
;
528 mask_ipv6_addr(saddr
, key
->ipv6_src
, mask
->ipv6_src
, masked
);
530 if (unlikely(memcmp(saddr
, masked
, sizeof(masked
)))) {
531 set_ipv6_addr(skb
, flow_key
->ip
.proto
, saddr
, masked
,
533 memcpy(&flow_key
->ipv6
.addr
.src
, masked
,
534 sizeof(flow_key
->ipv6
.addr
.src
));
537 if (is_ipv6_mask_nonzero(mask
->ipv6_dst
)) {
538 unsigned int offset
= 0;
539 int flags
= IP6_FH_F_SKIP_RH
;
540 bool recalc_csum
= true;
541 __be32
*daddr
= (__be32
*)&nh
->daddr
;
544 mask_ipv6_addr(daddr
, key
->ipv6_dst
, mask
->ipv6_dst
, masked
);
546 if (unlikely(memcmp(daddr
, masked
, sizeof(masked
)))) {
547 if (ipv6_ext_hdr(nh
->nexthdr
))
548 recalc_csum
= (ipv6_find_hdr(skb
, &offset
,
553 set_ipv6_addr(skb
, flow_key
->ip
.proto
, daddr
, masked
,
555 memcpy(&flow_key
->ipv6
.addr
.dst
, masked
,
556 sizeof(flow_key
->ipv6
.addr
.dst
));
559 if (mask
->ipv6_tclass
) {
560 ipv6_change_dsfield(nh
, ~mask
->ipv6_tclass
, key
->ipv6_tclass
);
561 flow_key
->ip
.tos
= ipv6_get_dsfield(nh
);
563 if (mask
->ipv6_label
) {
564 set_ipv6_fl(nh
, ntohl(key
->ipv6_label
),
565 ntohl(mask
->ipv6_label
));
566 flow_key
->ipv6
.label
=
567 *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
569 if (mask
->ipv6_hlimit
) {
570 OVS_SET_MASKED(nh
->hop_limit
, key
->ipv6_hlimit
,
572 flow_key
->ip
.ttl
= nh
->hop_limit
;
577 /* Must follow skb_ensure_writable() since that can move the skb data. */
578 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
579 __be16 new_port
, __sum16
*check
)
581 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, false);
585 static int set_udp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
586 const struct ovs_key_udp
*key
,
587 const struct ovs_key_udp
*mask
)
593 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
594 sizeof(struct udphdr
));
599 /* Either of the masks is non-zero, so do not bother checking them. */
600 src
= OVS_MASKED(uh
->source
, key
->udp_src
, mask
->udp_src
);
601 dst
= OVS_MASKED(uh
->dest
, key
->udp_dst
, mask
->udp_dst
);
603 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
604 if (likely(src
!= uh
->source
)) {
605 set_tp_port(skb
, &uh
->source
, src
, &uh
->check
);
606 flow_key
->tp
.src
= src
;
608 if (likely(dst
!= uh
->dest
)) {
609 set_tp_port(skb
, &uh
->dest
, dst
, &uh
->check
);
610 flow_key
->tp
.dst
= dst
;
613 if (unlikely(!uh
->check
))
614 uh
->check
= CSUM_MANGLED_0
;
618 flow_key
->tp
.src
= src
;
619 flow_key
->tp
.dst
= dst
;
627 static int set_tcp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
628 const struct ovs_key_tcp
*key
,
629 const struct ovs_key_tcp
*mask
)
635 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
636 sizeof(struct tcphdr
));
641 src
= OVS_MASKED(th
->source
, key
->tcp_src
, mask
->tcp_src
);
642 if (likely(src
!= th
->source
)) {
643 set_tp_port(skb
, &th
->source
, src
, &th
->check
);
644 flow_key
->tp
.src
= src
;
646 dst
= OVS_MASKED(th
->dest
, key
->tcp_dst
, mask
->tcp_dst
);
647 if (likely(dst
!= th
->dest
)) {
648 set_tp_port(skb
, &th
->dest
, dst
, &th
->check
);
649 flow_key
->tp
.dst
= dst
;
656 static int set_sctp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
657 const struct ovs_key_sctp
*key
,
658 const struct ovs_key_sctp
*mask
)
660 unsigned int sctphoff
= skb_transport_offset(skb
);
662 __le32 old_correct_csum
, new_csum
, old_csum
;
665 err
= skb_ensure_writable(skb
, sctphoff
+ sizeof(struct sctphdr
));
670 old_csum
= sh
->checksum
;
671 old_correct_csum
= sctp_compute_cksum(skb
, sctphoff
);
673 sh
->source
= OVS_MASKED(sh
->source
, key
->sctp_src
, mask
->sctp_src
);
674 sh
->dest
= OVS_MASKED(sh
->dest
, key
->sctp_dst
, mask
->sctp_dst
);
676 new_csum
= sctp_compute_cksum(skb
, sctphoff
);
678 /* Carry any checksum errors through. */
679 sh
->checksum
= old_csum
^ old_correct_csum
^ new_csum
;
682 flow_key
->tp
.src
= sh
->source
;
683 flow_key
->tp
.dst
= sh
->dest
;
688 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS
)
690 struct ovs_frag_data
*data
= this_cpu_ptr(&ovs_frag_data_storage
);
691 struct vport
*vport
= data
->vport
;
693 if (skb_cow_head(skb
, data
->l2_len
) < 0) {
698 __skb_dst_copy(skb
, data
->dst
);
699 *OVS_GSO_CB(skb
) = data
->cb
;
700 ovs_skb_set_inner_protocol(skb
, data
->inner_protocol
);
701 skb
->vlan_tci
= data
->vlan_tci
;
702 skb
->vlan_proto
= data
->vlan_proto
;
704 /* Reconstruct the MAC header. */
705 skb_push(skb
, data
->l2_len
);
706 memcpy(skb
->data
, &data
->l2_data
, data
->l2_len
);
707 skb_postpush_rcsum(skb
, skb
->data
, data
->l2_len
);
708 skb_reset_mac_header(skb
);
710 ovs_vport_send(vport
, skb
, data
->mac_proto
);
715 ovs_dst_get_mtu(const struct dst_entry
*dst
)
717 return dst
->dev
->mtu
;
720 static struct dst_ops ovs_dst_ops
= {
722 .mtu
= ovs_dst_get_mtu
,
725 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
726 * ovs_vport_output(), which is called once per fragmented packet.
728 static void prepare_frag(struct vport
*vport
, struct sk_buff
*skb
,
731 unsigned int hlen
= skb_network_offset(skb
);
732 struct ovs_frag_data
*data
;
734 data
= this_cpu_ptr(&ovs_frag_data_storage
);
735 data
->dst
= (unsigned long) skb_dst(skb
);
737 data
->cb
= *OVS_GSO_CB(skb
);
738 data
->inner_protocol
= ovs_skb_get_inner_protocol(skb
);
739 data
->vlan_tci
= skb
->vlan_tci
;
740 data
->vlan_proto
= skb
->vlan_proto
;
741 data
->mac_proto
= mac_proto
;
743 memcpy(&data
->l2_data
, skb
->data
, hlen
);
745 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
749 static void ovs_fragment(struct net
*net
, struct vport
*vport
,
750 struct sk_buff
*skb
, u16 mru
,
751 struct sw_flow_key
*key
)
753 if (skb_network_offset(skb
) > MAX_L2_LEN
) {
754 OVS_NLERR(1, "L2 header too long to fragment");
758 if (key
->eth
.type
== htons(ETH_P_IP
)) {
759 struct dst_entry ovs_dst
;
760 unsigned long orig_dst
;
762 prepare_frag(vport
, skb
, ovs_key_mac_proto(key
));
763 dst_init(&ovs_dst
, &ovs_dst_ops
, NULL
, 1,
764 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
765 ovs_dst
.dev
= vport
->dev
;
767 orig_dst
= (unsigned long) skb_dst(skb
);
768 skb_dst_set_noref(skb
, &ovs_dst
);
769 IPCB(skb
)->frag_max_size
= mru
;
771 ip_do_fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
772 refdst_drop(orig_dst
);
773 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
774 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
775 unsigned long orig_dst
;
776 struct rt6_info ovs_rt
;
781 prepare_frag(vport
, skb
,
782 ovs_key_mac_proto(key
));
783 memset(&ovs_rt
, 0, sizeof(ovs_rt
));
784 dst_init(&ovs_rt
.dst
, &ovs_dst_ops
, NULL
, 1,
785 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
786 ovs_rt
.dst
.dev
= vport
->dev
;
788 orig_dst
= (unsigned long) skb_dst(skb
);
789 skb_dst_set_noref(skb
, &ovs_rt
.dst
);
790 IP6CB(skb
)->frag_max_size
= mru
;
791 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
792 v6ops
->fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
794 v6ops
->fragment(skb
->sk
, skb
, ovs_vport_output
);
796 refdst_drop(orig_dst
);
798 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
799 ovs_vport_name(vport
), ntohs(key
->eth
.type
), mru
,
809 static void do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
,
810 struct sw_flow_key
*key
)
812 struct vport
*vport
= ovs_vport_rcu(dp
, out_port
);
815 u16 mru
= OVS_CB(skb
)->mru
;
816 u32 cutlen
= OVS_CB(skb
)->cutlen
;
818 if (unlikely(cutlen
> 0)) {
819 if (skb
->len
- cutlen
> ovs_mac_header_len(key
))
820 pskb_trim(skb
, skb
->len
- cutlen
);
822 pskb_trim(skb
, ovs_mac_header_len(key
));
826 (skb
->len
<= mru
+ vport
->dev
->hard_header_len
))) {
827 ovs_vport_send(vport
, skb
, ovs_key_mac_proto(key
));
828 } else if (mru
<= vport
->dev
->mtu
) {
829 struct net
*net
= ovs_dp_get_net(dp
);
831 ovs_fragment(net
, vport
, skb
, mru
, key
);
833 OVS_NLERR(true, "Cannot fragment IP frames");
841 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
842 struct sw_flow_key
*key
, const struct nlattr
*attr
,
843 const struct nlattr
*actions
, int actions_len
,
846 struct dp_upcall_info upcall
;
847 const struct nlattr
*a
;
850 memset(&upcall
, 0, sizeof(upcall
));
851 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
852 upcall
.mru
= OVS_CB(skb
)->mru
;
854 SKB_INIT_FILL_METADATA_DST(skb
);
855 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
856 a
= nla_next(a
, &rem
)) {
857 switch (nla_type(a
)) {
858 case OVS_USERSPACE_ATTR_USERDATA
:
862 case OVS_USERSPACE_ATTR_PID
:
863 upcall
.portid
= nla_get_u32(a
);
866 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
: {
867 /* Get out tunnel info. */
870 vport
= ovs_vport_rcu(dp
, nla_get_u32(a
));
872 err
= dev_fill_metadata_dst(vport
->dev
, skb
);
874 upcall
.egress_tun_info
= skb_tunnel_info(skb
);
880 case OVS_USERSPACE_ATTR_ACTIONS
: {
881 /* Include actions. */
882 upcall
.actions
= actions
;
883 upcall
.actions_len
= actions_len
;
887 } /* End of switch. */
890 err
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, cutlen
);
891 SKB_RESTORE_FILL_METADATA_DST(skb
);
895 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
896 struct sw_flow_key
*key
, const struct nlattr
*attr
,
897 const struct nlattr
*actions
, int actions_len
)
899 const struct nlattr
*acts_list
= NULL
;
900 const struct nlattr
*a
;
904 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
905 a
= nla_next(a
, &rem
)) {
908 switch (nla_type(a
)) {
909 case OVS_SAMPLE_ATTR_PROBABILITY
:
910 probability
= nla_get_u32(a
);
911 if (!probability
|| prandom_u32() > probability
)
915 case OVS_SAMPLE_ATTR_ACTIONS
:
921 rem
= nla_len(acts_list
);
922 a
= nla_data(acts_list
);
924 /* Actions list is empty, do nothing */
928 /* The only known usage of sample action is having a single user-space
929 * action, or having a truncate action followed by a single user-space
930 * action. Treat this usage as a special case.
931 * The output_userspace() should clone the skb to be sent to the
932 * user space. This skb will be consumed by its caller.
934 if (unlikely(nla_type(a
) == OVS_ACTION_ATTR_TRUNC
)) {
935 struct ovs_action_trunc
*trunc
= nla_data(a
);
937 if (skb
->len
> trunc
->max_len
)
938 cutlen
= skb
->len
- trunc
->max_len
;
940 a
= nla_next(a
, &rem
);
943 if (likely(nla_type(a
) == OVS_ACTION_ATTR_USERSPACE
&&
944 nla_is_last(a
, rem
)))
945 return output_userspace(dp
, skb
, key
, a
, actions
,
946 actions_len
, cutlen
);
948 skb
= skb_clone(skb
, GFP_ATOMIC
);
950 /* Skip the sample action when out of memory. */
953 if (!add_deferred_actions(skb
, key
, a
)) {
955 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
963 static void execute_hash(struct sk_buff
*skb
, struct sw_flow_key
*key
,
964 const struct nlattr
*attr
)
966 struct ovs_action_hash
*hash_act
= nla_data(attr
);
969 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
970 hash
= skb_get_hash(skb
);
971 hash
= jhash_1word(hash
, hash_act
->hash_basis
);
975 key
->ovs_flow_hash
= hash
;
978 static int execute_set_action(struct sk_buff
*skb
,
979 struct sw_flow_key
*flow_key
,
980 const struct nlattr
*a
)
982 /* Only tunnel set execution is supported without a mask. */
983 if (nla_type(a
) == OVS_KEY_ATTR_TUNNEL_INFO
) {
984 struct ovs_tunnel_info
*tun
= nla_data(a
);
986 ovs_skb_dst_drop(skb
);
987 ovs_dst_hold((struct dst_entry
*)tun
->tun_dst
);
988 ovs_skb_dst_set(skb
, (struct dst_entry
*)tun
->tun_dst
);
995 /* Mask is at the midpoint of the data. */
996 #define get_mask(a, type) ((const type)nla_data(a) + 1)
998 static int execute_masked_set_action(struct sk_buff
*skb
,
999 struct sw_flow_key
*flow_key
,
1000 const struct nlattr
*a
)
1004 switch (nla_type(a
)) {
1005 case OVS_KEY_ATTR_PRIORITY
:
1006 OVS_SET_MASKED(skb
->priority
, nla_get_u32(a
),
1007 *get_mask(a
, u32
*));
1008 flow_key
->phy
.priority
= skb
->priority
;
1011 case OVS_KEY_ATTR_SKB_MARK
:
1012 OVS_SET_MASKED(skb
->mark
, nla_get_u32(a
), *get_mask(a
, u32
*));
1013 flow_key
->phy
.skb_mark
= skb
->mark
;
1016 case OVS_KEY_ATTR_TUNNEL_INFO
:
1017 /* Masked data not supported for tunnel. */
1021 case OVS_KEY_ATTR_ETHERNET
:
1022 err
= set_eth_addr(skb
, flow_key
, nla_data(a
),
1023 get_mask(a
, struct ovs_key_ethernet
*));
1026 case OVS_KEY_ATTR_IPV4
:
1027 err
= set_ipv4(skb
, flow_key
, nla_data(a
),
1028 get_mask(a
, struct ovs_key_ipv4
*));
1031 case OVS_KEY_ATTR_IPV6
:
1032 err
= set_ipv6(skb
, flow_key
, nla_data(a
),
1033 get_mask(a
, struct ovs_key_ipv6
*));
1036 case OVS_KEY_ATTR_TCP
:
1037 err
= set_tcp(skb
, flow_key
, nla_data(a
),
1038 get_mask(a
, struct ovs_key_tcp
*));
1041 case OVS_KEY_ATTR_UDP
:
1042 err
= set_udp(skb
, flow_key
, nla_data(a
),
1043 get_mask(a
, struct ovs_key_udp
*));
1046 case OVS_KEY_ATTR_SCTP
:
1047 err
= set_sctp(skb
, flow_key
, nla_data(a
),
1048 get_mask(a
, struct ovs_key_sctp
*));
1051 case OVS_KEY_ATTR_MPLS
:
1052 err
= set_mpls(skb
, flow_key
, nla_data(a
), get_mask(a
,
1056 case OVS_KEY_ATTR_CT_STATE
:
1057 case OVS_KEY_ATTR_CT_ZONE
:
1058 case OVS_KEY_ATTR_CT_MARK
:
1059 case OVS_KEY_ATTR_CT_LABELS
:
1060 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
1061 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
1069 static int execute_recirc(struct datapath
*dp
, struct sk_buff
*skb
,
1070 struct sw_flow_key
*key
,
1071 const struct nlattr
*a
, int rem
)
1073 struct deferred_action
*da
;
1076 if (!is_flow_key_valid(key
)) {
1079 err
= ovs_flow_key_update(skb
, key
);
1083 BUG_ON(!is_flow_key_valid(key
));
1085 if (!nla_is_last(a
, rem
)) {
1086 /* Recirc action is the not the last action
1087 * of the action list, need to clone the skb.
1089 skb
= skb_clone(skb
, GFP_ATOMIC
);
1091 /* Skip the recirc action when out of memory, but
1092 * continue on with the rest of the action list.
1098 level
= this_cpu_read(exec_actions_level
);
1099 if (level
<= OVS_DEFERRED_ACTION_THRESHOLD
) {
1100 struct recirc_keys
*rks
= this_cpu_ptr(recirc_keys
);
1101 struct sw_flow_key
*recirc_key
= &rks
->key
[level
- 1];
1104 recirc_key
->recirc_id
= nla_get_u32(a
);
1105 ovs_dp_process_packet(skb
, recirc_key
);
1110 da
= add_deferred_actions(skb
, key
, NULL
);
1112 da
->pkt_key
.recirc_id
= nla_get_u32(a
);
1116 if (net_ratelimit())
1117 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1124 /* Execute a list of actions against 'skb'. */
1125 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1126 struct sw_flow_key
*key
,
1127 const struct nlattr
*attr
, int len
)
1129 const struct nlattr
*a
;
1132 for (a
= attr
, rem
= len
; rem
> 0;
1133 a
= nla_next(a
, &rem
)) {
1136 switch (nla_type(a
)) {
1137 case OVS_ACTION_ATTR_OUTPUT
: {
1138 int port
= nla_get_u32(a
);
1139 struct sk_buff
*clone
;
1141 /* Every output action needs a separate clone
1142 * of 'skb', In case the output action is the
1143 * last action, cloning can be avoided.
1145 if (nla_is_last(a
, rem
)) {
1146 do_output(dp
, skb
, port
, key
);
1147 /* 'skb' has been used for output.
1152 clone
= skb_clone(skb
, GFP_ATOMIC
);
1154 do_output(dp
, clone
, port
, key
);
1155 OVS_CB(skb
)->cutlen
= 0;
1159 case OVS_ACTION_ATTR_TRUNC
: {
1160 struct ovs_action_trunc
*trunc
= nla_data(a
);
1162 if (skb
->len
> trunc
->max_len
)
1163 OVS_CB(skb
)->cutlen
= skb
->len
- trunc
->max_len
;
1167 case OVS_ACTION_ATTR_USERSPACE
:
1168 output_userspace(dp
, skb
, key
, a
, attr
,
1169 len
, OVS_CB(skb
)->cutlen
);
1170 OVS_CB(skb
)->cutlen
= 0;
1173 case OVS_ACTION_ATTR_HASH
:
1174 execute_hash(skb
, key
, a
);
1177 case OVS_ACTION_ATTR_PUSH_MPLS
:
1178 err
= push_mpls(skb
, key
, nla_data(a
));
1181 case OVS_ACTION_ATTR_POP_MPLS
:
1182 err
= pop_mpls(skb
, key
, nla_get_be16(a
));
1185 case OVS_ACTION_ATTR_PUSH_VLAN
:
1186 err
= push_vlan(skb
, key
, nla_data(a
));
1189 case OVS_ACTION_ATTR_POP_VLAN
:
1190 err
= pop_vlan(skb
, key
);
1193 case OVS_ACTION_ATTR_RECIRC
:
1194 err
= execute_recirc(dp
, skb
, key
, a
, rem
);
1195 if (nla_is_last(a
, rem
)) {
1196 /* If this is the last action, the skb has
1197 * been consumed or freed.
1198 * Return immediately.
1204 case OVS_ACTION_ATTR_SET
:
1205 err
= execute_set_action(skb
, key
, nla_data(a
));
1208 case OVS_ACTION_ATTR_SET_MASKED
:
1209 case OVS_ACTION_ATTR_SET_TO_MASKED
:
1210 err
= execute_masked_set_action(skb
, key
, nla_data(a
));
1213 case OVS_ACTION_ATTR_SAMPLE
:
1214 err
= sample(dp
, skb
, key
, a
, attr
, len
);
1217 case OVS_ACTION_ATTR_CT
:
1218 if (!is_flow_key_valid(key
)) {
1219 err
= ovs_flow_key_update(skb
, key
);
1224 err
= ovs_ct_execute(ovs_dp_get_net(dp
), skb
, key
,
1227 /* Hide stolen IP fragments from user space. */
1229 return err
== -EINPROGRESS
? 0 : err
;
1232 case OVS_ACTION_ATTR_PUSH_ETH
:
1233 err
= push_eth(skb
, key
, nla_data(a
));
1236 case OVS_ACTION_ATTR_POP_ETH
:
1237 err
= pop_eth(skb
, key
);
1241 if (unlikely(err
)) {
1251 static void process_deferred_actions(struct datapath
*dp
)
1253 struct action_fifo
*fifo
= this_cpu_ptr(action_fifos
);
1255 /* Do not touch the FIFO in case there is no deferred actions. */
1256 if (action_fifo_is_empty(fifo
))
1259 /* Finishing executing all deferred actions. */
1261 struct deferred_action
*da
= action_fifo_get(fifo
);
1262 struct sk_buff
*skb
= da
->skb
;
1263 struct sw_flow_key
*key
= &da
->pkt_key
;
1264 const struct nlattr
*actions
= da
->actions
;
1267 do_execute_actions(dp
, skb
, key
, actions
,
1270 ovs_dp_process_packet(skb
, key
);
1271 } while (!action_fifo_is_empty(fifo
));
1273 /* Reset FIFO for the next packet. */
1274 action_fifo_init(fifo
);
1277 /* Execute a list of actions against 'skb'. */
1278 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1279 const struct sw_flow_actions
*acts
,
1280 struct sw_flow_key
*key
)
1284 level
= __this_cpu_inc_return(exec_actions_level
);
1285 if (unlikely(level
> OVS_RECURSION_LIMIT
)) {
1286 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1293 err
= do_execute_actions(dp
, skb
, key
,
1294 acts
->actions
, acts
->actions_len
);
1297 process_deferred_actions(dp
);
1300 __this_cpu_dec(exec_actions_level
);
1304 int action_fifos_init(void)
1306 action_fifos
= alloc_percpu(struct action_fifo
);
1310 recirc_keys
= alloc_percpu(struct recirc_keys
);
1312 free_percpu(action_fifos
);
1319 void action_fifos_exit(void)
1321 free_percpu(action_fifos
);
1322 free_percpu(recirc_keys
);