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
;
67 u8 l2_data
[MAX_L2_LEN
];
70 static DEFINE_PER_CPU(struct ovs_frag_data
, ovs_frag_data_storage
);
72 #define DEFERRED_ACTION_FIFO_SIZE 10
76 /* Deferred action fifo queue storage. */
77 struct deferred_action fifo
[DEFERRED_ACTION_FIFO_SIZE
];
80 static struct action_fifo __percpu
*action_fifos
;
81 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
82 * looping by the network stack
84 static DEFINE_PER_CPU(int, exec_actions_level
);
86 static void action_fifo_init(struct action_fifo
*fifo
)
92 static bool action_fifo_is_empty(const struct action_fifo
*fifo
)
94 return (fifo
->head
== fifo
->tail
);
97 static struct deferred_action
*action_fifo_get(struct action_fifo
*fifo
)
99 if (action_fifo_is_empty(fifo
))
102 return &fifo
->fifo
[fifo
->tail
++];
105 static struct deferred_action
*action_fifo_put(struct action_fifo
*fifo
)
107 if (fifo
->head
>= DEFERRED_ACTION_FIFO_SIZE
- 1)
110 return &fifo
->fifo
[fifo
->head
++];
113 /* Return queue entry if fifo is not full */
114 static struct deferred_action
*add_deferred_actions(struct sk_buff
*skb
,
115 const struct sw_flow_key
*key
,
116 const struct nlattr
*attr
)
118 struct action_fifo
*fifo
;
119 struct deferred_action
*da
;
121 fifo
= this_cpu_ptr(action_fifos
);
122 da
= action_fifo_put(fifo
);
132 static void invalidate_flow_key(struct sw_flow_key
*key
)
134 key
->eth
.type
= htons(0);
137 static bool is_flow_key_valid(const struct sw_flow_key
*key
)
139 return !!key
->eth
.type
;
142 static int push_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
143 const struct ovs_action_push_mpls
*mpls
)
145 __be32
*new_mpls_lse
;
148 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
149 if (skb
->encapsulation
)
152 if (skb_cow_head(skb
, MPLS_HLEN
) < 0)
155 skb_push(skb
, MPLS_HLEN
);
156 memmove(skb_mac_header(skb
) - MPLS_HLEN
, skb_mac_header(skb
),
158 skb_reset_mac_header(skb
);
160 new_mpls_lse
= (__be32
*)skb_mpls_header(skb
);
161 *new_mpls_lse
= mpls
->mpls_lse
;
163 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
164 skb
->csum
= csum_add(skb
->csum
, csum_partial(new_mpls_lse
,
168 hdr
->h_proto
= mpls
->mpls_ethertype
;
169 if (!ovs_skb_get_inner_protocol(skb
))
170 ovs_skb_set_inner_protocol(skb
, skb
->protocol
);
171 skb
->protocol
= mpls
->mpls_ethertype
;
173 invalidate_flow_key(key
);
177 static int pop_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
178 const __be16 ethertype
)
183 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
187 skb_postpull_rcsum(skb
, skb_mpls_header(skb
), MPLS_HLEN
);
189 memmove(skb_mac_header(skb
) + MPLS_HLEN
, skb_mac_header(skb
),
192 __skb_pull(skb
, MPLS_HLEN
);
193 skb_reset_mac_header(skb
);
195 /* skb_mpls_header() is used to locate the ethertype
196 * field correctly in the presence of VLAN tags.
198 hdr
= (struct ethhdr
*)(skb_mpls_header(skb
) - ETH_HLEN
);
199 hdr
->h_proto
= ethertype
;
200 if (eth_p_mpls(skb
->protocol
))
201 skb
->protocol
= ethertype
;
203 invalidate_flow_key(key
);
207 static int set_mpls(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
208 const __be32
*mpls_lse
, const __be32
*mask
)
214 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
218 stack
= (__be32
*)skb_mpls_header(skb
);
219 lse
= OVS_MASKED(*stack
, *mpls_lse
, *mask
);
220 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
221 __be32 diff
[] = { ~(*stack
), lse
};
223 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
228 flow_key
->mpls
.top_lse
= lse
;
232 static int pop_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
236 err
= skb_vlan_pop(skb
);
237 if (skb_vlan_tag_present(skb
))
238 invalidate_flow_key(key
);
244 static int push_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
,
245 const struct ovs_action_push_vlan
*vlan
)
247 if (skb_vlan_tag_present(skb
))
248 invalidate_flow_key(key
);
250 key
->eth
.tci
= vlan
->vlan_tci
;
251 return skb_vlan_push(skb
, vlan
->vlan_tpid
,
252 ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
255 /* 'src' is already properly masked. */
256 static void ether_addr_copy_masked(u8
*dst_
, const u8
*src_
, const u8
*mask_
)
258 u16
*dst
= (u16
*)dst_
;
259 const u16
*src
= (const u16
*)src_
;
260 const u16
*mask
= (const u16
*)mask_
;
262 OVS_SET_MASKED(dst
[0], src
[0], mask
[0]);
263 OVS_SET_MASKED(dst
[1], src
[1], mask
[1]);
264 OVS_SET_MASKED(dst
[2], src
[2], mask
[2]);
267 static int set_eth_addr(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
268 const struct ovs_key_ethernet
*key
,
269 const struct ovs_key_ethernet
*mask
)
273 err
= skb_ensure_writable(skb
, ETH_HLEN
);
277 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
279 ether_addr_copy_masked(eth_hdr(skb
)->h_source
, key
->eth_src
,
281 ether_addr_copy_masked(eth_hdr(skb
)->h_dest
, key
->eth_dst
,
284 ovs_skb_postpush_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
286 ether_addr_copy(flow_key
->eth
.src
, eth_hdr(skb
)->h_source
);
287 ether_addr_copy(flow_key
->eth
.dst
, eth_hdr(skb
)->h_dest
);
291 static void update_ip_l4_checksum(struct sk_buff
*skb
, struct iphdr
*nh
,
292 __be32 addr
, __be32 new_addr
)
294 int transport_len
= skb
->len
- skb_transport_offset(skb
);
296 if (nh
->frag_off
& htons(IP_OFFSET
))
299 if (nh
->protocol
== IPPROTO_TCP
) {
300 if (likely(transport_len
>= sizeof(struct tcphdr
)))
301 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
302 addr
, new_addr
, true);
303 } else if (nh
->protocol
== IPPROTO_UDP
) {
304 if (likely(transport_len
>= sizeof(struct udphdr
))) {
305 struct udphdr
*uh
= udp_hdr(skb
);
307 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
308 inet_proto_csum_replace4(&uh
->check
, skb
,
309 addr
, new_addr
, true);
311 uh
->check
= CSUM_MANGLED_0
;
318 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
319 __be32
*addr
, __be32 new_addr
)
321 update_ip_l4_checksum(skb
, nh
, *addr
, new_addr
);
322 csum_replace4(&nh
->check
, *addr
, new_addr
);
327 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
328 __be32 addr
[4], const __be32 new_addr
[4])
330 int transport_len
= skb
->len
- skb_transport_offset(skb
);
332 if (l4_proto
== NEXTHDR_TCP
) {
333 if (likely(transport_len
>= sizeof(struct tcphdr
)))
334 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
335 addr
, new_addr
, true);
336 } else if (l4_proto
== NEXTHDR_UDP
) {
337 if (likely(transport_len
>= sizeof(struct udphdr
))) {
338 struct udphdr
*uh
= udp_hdr(skb
);
340 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
341 inet_proto_csum_replace16(&uh
->check
, skb
,
342 addr
, new_addr
, true);
344 uh
->check
= CSUM_MANGLED_0
;
347 } else if (l4_proto
== NEXTHDR_ICMP
) {
348 if (likely(transport_len
>= sizeof(struct icmp6hdr
)))
349 inet_proto_csum_replace16(&icmp6_hdr(skb
)->icmp6_cksum
,
350 skb
, addr
, new_addr
, true);
354 static void mask_ipv6_addr(const __be32 old
[4], const __be32 addr
[4],
355 const __be32 mask
[4], __be32 masked
[4])
357 masked
[0] = OVS_MASKED(old
[0], addr
[0], mask
[0]);
358 masked
[1] = OVS_MASKED(old
[1], addr
[1], mask
[1]);
359 masked
[2] = OVS_MASKED(old
[2], addr
[2], mask
[2]);
360 masked
[3] = OVS_MASKED(old
[3], addr
[3], mask
[3]);
363 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
364 __be32 addr
[4], const __be32 new_addr
[4],
365 bool recalculate_csum
)
367 if (likely(recalculate_csum
))
368 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
371 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
374 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
, u32 mask
)
376 /* Bits 21-24 are always unmasked, so this retains their values. */
377 OVS_SET_MASKED(nh
->flow_lbl
[0], (u8
)(fl
>> 16), (u8
)(mask
>> 16));
378 OVS_SET_MASKED(nh
->flow_lbl
[1], (u8
)(fl
>> 8), (u8
)(mask
>> 8));
379 OVS_SET_MASKED(nh
->flow_lbl
[2], (u8
)fl
, (u8
)mask
);
382 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
,
385 new_ttl
= OVS_MASKED(nh
->ttl
, new_ttl
, mask
);
387 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
391 static int set_ipv4(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
392 const struct ovs_key_ipv4
*key
,
393 const struct ovs_key_ipv4
*mask
)
399 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
400 sizeof(struct iphdr
));
406 /* Setting an IP addresses is typically only a side effect of
407 * matching on them in the current userspace implementation, so it
408 * makes sense to check if the value actually changed.
410 if (mask
->ipv4_src
) {
411 new_addr
= OVS_MASKED(nh
->saddr
, key
->ipv4_src
, mask
->ipv4_src
);
413 if (unlikely(new_addr
!= nh
->saddr
)) {
414 set_ip_addr(skb
, nh
, &nh
->saddr
, new_addr
);
415 flow_key
->ipv4
.addr
.src
= new_addr
;
418 if (mask
->ipv4_dst
) {
419 new_addr
= OVS_MASKED(nh
->daddr
, key
->ipv4_dst
, mask
->ipv4_dst
);
421 if (unlikely(new_addr
!= nh
->daddr
)) {
422 set_ip_addr(skb
, nh
, &nh
->daddr
, new_addr
);
423 flow_key
->ipv4
.addr
.dst
= new_addr
;
426 if (mask
->ipv4_tos
) {
427 ipv4_change_dsfield(nh
, ~mask
->ipv4_tos
, key
->ipv4_tos
);
428 flow_key
->ip
.tos
= nh
->tos
;
430 if (mask
->ipv4_ttl
) {
431 set_ip_ttl(skb
, nh
, key
->ipv4_ttl
, mask
->ipv4_ttl
);
432 flow_key
->ip
.ttl
= nh
->ttl
;
438 static bool is_ipv6_mask_nonzero(const __be32 addr
[4])
440 return !!(addr
[0] | addr
[1] | addr
[2] | addr
[3]);
443 static int set_ipv6(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
444 const struct ovs_key_ipv6
*key
,
445 const struct ovs_key_ipv6
*mask
)
450 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
451 sizeof(struct ipv6hdr
));
457 /* Setting an IP addresses is typically only a side effect of
458 * matching on them in the current userspace implementation, so it
459 * makes sense to check if the value actually changed.
461 if (is_ipv6_mask_nonzero(mask
->ipv6_src
)) {
462 __be32
*saddr
= (__be32
*)&nh
->saddr
;
465 mask_ipv6_addr(saddr
, key
->ipv6_src
, mask
->ipv6_src
, masked
);
467 if (unlikely(memcmp(saddr
, masked
, sizeof(masked
)))) {
468 set_ipv6_addr(skb
, flow_key
->ip
.proto
, saddr
, masked
,
470 memcpy(&flow_key
->ipv6
.addr
.src
, masked
,
471 sizeof(flow_key
->ipv6
.addr
.src
));
474 if (is_ipv6_mask_nonzero(mask
->ipv6_dst
)) {
475 unsigned int offset
= 0;
476 int flags
= IP6_FH_F_SKIP_RH
;
477 bool recalc_csum
= true;
478 __be32
*daddr
= (__be32
*)&nh
->daddr
;
481 mask_ipv6_addr(daddr
, key
->ipv6_dst
, mask
->ipv6_dst
, masked
);
483 if (unlikely(memcmp(daddr
, masked
, sizeof(masked
)))) {
484 if (ipv6_ext_hdr(nh
->nexthdr
))
485 recalc_csum
= (ipv6_find_hdr(skb
, &offset
,
490 set_ipv6_addr(skb
, flow_key
->ip
.proto
, daddr
, masked
,
492 memcpy(&flow_key
->ipv6
.addr
.dst
, masked
,
493 sizeof(flow_key
->ipv6
.addr
.dst
));
496 if (mask
->ipv6_tclass
) {
497 ipv6_change_dsfield(nh
, ~mask
->ipv6_tclass
, key
->ipv6_tclass
);
498 flow_key
->ip
.tos
= ipv6_get_dsfield(nh
);
500 if (mask
->ipv6_label
) {
501 set_ipv6_fl(nh
, ntohl(key
->ipv6_label
),
502 ntohl(mask
->ipv6_label
));
503 flow_key
->ipv6
.label
=
504 *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
506 if (mask
->ipv6_hlimit
) {
507 OVS_SET_MASKED(nh
->hop_limit
, key
->ipv6_hlimit
,
509 flow_key
->ip
.ttl
= nh
->hop_limit
;
514 /* Must follow skb_ensure_writable() since that can move the skb data. */
515 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
516 __be16 new_port
, __sum16
*check
)
518 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, false);
522 static int set_udp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
523 const struct ovs_key_udp
*key
,
524 const struct ovs_key_udp
*mask
)
530 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
531 sizeof(struct udphdr
));
536 /* Either of the masks is non-zero, so do not bother checking them. */
537 src
= OVS_MASKED(uh
->source
, key
->udp_src
, mask
->udp_src
);
538 dst
= OVS_MASKED(uh
->dest
, key
->udp_dst
, mask
->udp_dst
);
540 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
541 if (likely(src
!= uh
->source
)) {
542 set_tp_port(skb
, &uh
->source
, src
, &uh
->check
);
543 flow_key
->tp
.src
= src
;
545 if (likely(dst
!= uh
->dest
)) {
546 set_tp_port(skb
, &uh
->dest
, dst
, &uh
->check
);
547 flow_key
->tp
.dst
= dst
;
550 if (unlikely(!uh
->check
))
551 uh
->check
= CSUM_MANGLED_0
;
555 flow_key
->tp
.src
= src
;
556 flow_key
->tp
.dst
= dst
;
564 static int set_tcp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
565 const struct ovs_key_tcp
*key
,
566 const struct ovs_key_tcp
*mask
)
572 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
573 sizeof(struct tcphdr
));
578 src
= OVS_MASKED(th
->source
, key
->tcp_src
, mask
->tcp_src
);
579 if (likely(src
!= th
->source
)) {
580 set_tp_port(skb
, &th
->source
, src
, &th
->check
);
581 flow_key
->tp
.src
= src
;
583 dst
= OVS_MASKED(th
->dest
, key
->tcp_dst
, mask
->tcp_dst
);
584 if (likely(dst
!= th
->dest
)) {
585 set_tp_port(skb
, &th
->dest
, dst
, &th
->check
);
586 flow_key
->tp
.dst
= dst
;
593 static int set_sctp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
594 const struct ovs_key_sctp
*key
,
595 const struct ovs_key_sctp
*mask
)
597 unsigned int sctphoff
= skb_transport_offset(skb
);
599 __le32 old_correct_csum
, new_csum
, old_csum
;
602 err
= skb_ensure_writable(skb
, sctphoff
+ sizeof(struct sctphdr
));
607 old_csum
= sh
->checksum
;
608 old_correct_csum
= sctp_compute_cksum(skb
, sctphoff
);
610 sh
->source
= OVS_MASKED(sh
->source
, key
->sctp_src
, mask
->sctp_src
);
611 sh
->dest
= OVS_MASKED(sh
->dest
, key
->sctp_dst
, mask
->sctp_dst
);
613 new_csum
= sctp_compute_cksum(skb
, sctphoff
);
615 /* Carry any checksum errors through. */
616 sh
->checksum
= old_csum
^ old_correct_csum
^ new_csum
;
619 flow_key
->tp
.src
= sh
->source
;
620 flow_key
->tp
.dst
= sh
->dest
;
625 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS
)
627 struct ovs_frag_data
*data
= get_pcpu_ptr(ovs_frag_data_storage
);
628 struct vport
*vport
= data
->vport
;
630 if (skb_cow_head(skb
, data
->l2_len
) < 0) {
635 __skb_dst_copy(skb
, data
->dst
);
636 *OVS_GSO_CB(skb
) = data
->cb
;
637 ovs_skb_set_inner_protocol(skb
, data
->inner_protocol
);
638 skb
->vlan_tci
= data
->vlan_tci
;
639 skb
->vlan_proto
= data
->vlan_proto
;
641 /* Reconstruct the MAC header. */
642 skb_push(skb
, data
->l2_len
);
643 memcpy(skb
->data
, &data
->l2_data
, data
->l2_len
);
644 ovs_skb_postpush_rcsum(skb
, skb
->data
, data
->l2_len
);
645 skb_reset_mac_header(skb
);
647 ovs_vport_send(vport
, skb
);
652 ovs_dst_get_mtu(const struct dst_entry
*dst
)
654 return dst
->dev
->mtu
;
657 static struct dst_ops ovs_dst_ops
= {
659 .mtu
= ovs_dst_get_mtu
,
662 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
663 * ovs_vport_output(), which is called once per fragmented packet.
665 static void prepare_frag(struct vport
*vport
, struct sk_buff
*skb
)
667 unsigned int hlen
= skb_network_offset(skb
);
668 struct ovs_frag_data
*data
;
670 data
= get_pcpu_ptr(ovs_frag_data_storage
);
671 data
->dst
= (unsigned long) skb_dst(skb
);
673 data
->cb
= *OVS_GSO_CB(skb
);
674 data
->inner_protocol
= ovs_skb_get_inner_protocol(skb
);
675 data
->vlan_tci
= skb
->vlan_tci
;
676 data
->vlan_proto
= skb
->vlan_proto
;
678 memcpy(&data
->l2_data
, skb
->data
, hlen
);
680 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
684 static void ovs_fragment(struct net
*net
, struct vport
*vport
,
685 struct sk_buff
*skb
, u16 mru
, __be16 ethertype
)
687 if (skb_network_offset(skb
) > MAX_L2_LEN
) {
688 OVS_NLERR(1, "L2 header too long to fragment");
692 if (ethertype
== htons(ETH_P_IP
)) {
693 struct dst_entry ovs_dst
;
694 unsigned long orig_dst
;
696 prepare_frag(vport
, skb
);
697 dst_init(&ovs_dst
, &ovs_dst_ops
, NULL
, 1,
698 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
699 ovs_dst
.dev
= vport
->dev
;
701 orig_dst
= (unsigned long) skb_dst(skb
);
702 skb_dst_set_noref(skb
, &ovs_dst
);
703 IPCB(skb
)->frag_max_size
= mru
;
705 ip_do_fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
706 refdst_drop(orig_dst
);
707 } else if (ethertype
== htons(ETH_P_IPV6
)) {
708 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
709 unsigned long orig_dst
;
710 struct rt6_info ovs_rt
;
716 prepare_frag(vport
, skb
);
717 memset(&ovs_rt
, 0, sizeof(ovs_rt
));
718 dst_init(&ovs_rt
.dst
, &ovs_dst_ops
, NULL
, 1,
719 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
720 ovs_rt
.dst
.dev
= vport
->dev
;
722 orig_dst
= (unsigned long) skb_dst(skb
);
723 skb_dst_set_noref(skb
, &ovs_rt
.dst
);
724 IP6CB(skb
)->frag_max_size
= mru
;
726 v6ops
->fragment(skb
->sk
, skb
, ovs_vport_output
);
727 refdst_drop(orig_dst
);
729 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
730 ovs_vport_name(vport
), ntohs(ethertype
), mru
,
740 static void do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
,
741 struct sw_flow_key
*key
)
743 struct vport
*vport
= ovs_vport_rcu(dp
, out_port
);
746 u16 mru
= OVS_CB(skb
)->mru
;
747 u32 cutlen
= OVS_CB(skb
)->cutlen
;
749 if (unlikely(cutlen
> 0)) {
750 if (skb
->len
- cutlen
> ETH_HLEN
)
751 pskb_trim(skb
, skb
->len
- cutlen
);
753 pskb_trim(skb
, ETH_HLEN
);
756 if (likely(!mru
|| (skb
->len
<= mru
+ ETH_HLEN
))) {
757 ovs_vport_send(vport
, skb
);
758 } else if (mru
<= vport
->dev
->mtu
) {
759 struct net
*net
= ovs_dp_get_net(dp
);
760 __be16 ethertype
= key
->eth
.type
;
762 if (!is_flow_key_valid(key
)) {
763 if (eth_p_mpls(skb
->protocol
))
764 ethertype
= ovs_skb_get_inner_protocol(skb
);
766 ethertype
= vlan_get_protocol(skb
);
769 ovs_fragment(net
, vport
, skb
, mru
, ethertype
);
771 OVS_NLERR(true, "Cannot fragment IP frames");
779 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
780 struct sw_flow_key
*key
, const struct nlattr
*attr
,
781 const struct nlattr
*actions
, int actions_len
,
784 struct dp_upcall_info upcall
;
785 const struct nlattr
*a
;
788 memset(&upcall
, 0, sizeof(upcall
));
789 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
790 upcall
.mru
= OVS_CB(skb
)->mru
;
792 SKB_INIT_FILL_METADATA_DST(skb
);
793 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
794 a
= nla_next(a
, &rem
)) {
795 switch (nla_type(a
)) {
796 case OVS_USERSPACE_ATTR_USERDATA
:
800 case OVS_USERSPACE_ATTR_PID
:
801 upcall
.portid
= nla_get_u32(a
);
804 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
: {
805 /* Get out tunnel info. */
808 vport
= ovs_vport_rcu(dp
, nla_get_u32(a
));
812 err
= dev_fill_metadata_dst(vport
->dev
, skb
);
814 upcall
.egress_tun_info
= skb_tunnel_info(skb
);
820 case OVS_USERSPACE_ATTR_ACTIONS
: {
821 /* Include actions. */
822 upcall
.actions
= actions
;
823 upcall
.actions_len
= actions_len
;
827 } /* End of switch. */
830 err
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, cutlen
);
831 SKB_RESTORE_FILL_METADATA_DST(skb
);
835 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
836 struct sw_flow_key
*key
, const struct nlattr
*attr
,
837 const struct nlattr
*actions
, int actions_len
)
839 const struct nlattr
*acts_list
= NULL
;
840 const struct nlattr
*a
;
844 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
845 a
= nla_next(a
, &rem
)) {
848 switch (nla_type(a
)) {
849 case OVS_SAMPLE_ATTR_PROBABILITY
:
850 probability
= nla_get_u32(a
);
851 if (!probability
|| prandom_u32() > probability
)
855 case OVS_SAMPLE_ATTR_ACTIONS
:
861 rem
= nla_len(acts_list
);
862 a
= nla_data(acts_list
);
864 /* Actions list is empty, do nothing */
868 /* The only known usage of sample action is having a single user-space
869 * action, or having a truncate action followed by a single user-space
870 * action. Treat this usage as a special case.
871 * The output_userspace() should clone the skb to be sent to the
872 * user space. This skb will be consumed by its caller.
874 if (unlikely(nla_type(a
) == OVS_ACTION_ATTR_TRUNC
)) {
875 struct ovs_action_trunc
*trunc
= nla_data(a
);
877 if (skb
->len
> trunc
->max_len
)
878 cutlen
= skb
->len
- trunc
->max_len
;
880 a
= nla_next(a
, &rem
);
883 if (likely(nla_type(a
) == OVS_ACTION_ATTR_USERSPACE
&&
884 nla_is_last(a
, rem
)))
885 return output_userspace(dp
, skb
, key
, a
, actions
,
886 actions_len
, cutlen
);
888 skb
= skb_clone(skb
, GFP_ATOMIC
);
890 /* Skip the sample action when out of memory. */
893 if (!add_deferred_actions(skb
, key
, a
)) {
895 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
903 static void execute_hash(struct sk_buff
*skb
, struct sw_flow_key
*key
,
904 const struct nlattr
*attr
)
906 struct ovs_action_hash
*hash_act
= nla_data(attr
);
909 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
910 hash
= skb_get_hash(skb
);
911 hash
= jhash_1word(hash
, hash_act
->hash_basis
);
915 key
->ovs_flow_hash
= hash
;
918 static int execute_set_action(struct sk_buff
*skb
,
919 struct sw_flow_key
*flow_key
,
920 const struct nlattr
*a
)
922 /* Only tunnel set execution is supported without a mask. */
923 if (nla_type(a
) == OVS_KEY_ATTR_TUNNEL_INFO
) {
924 struct ovs_tunnel_info
*tun
= nla_data(a
);
926 ovs_skb_dst_drop(skb
);
927 ovs_dst_hold((struct dst_entry
*)tun
->tun_dst
);
928 ovs_skb_dst_set(skb
, (struct dst_entry
*)tun
->tun_dst
);
935 /* Mask is at the midpoint of the data. */
936 #define get_mask(a, type) ((const type)nla_data(a) + 1)
938 static int execute_masked_set_action(struct sk_buff
*skb
,
939 struct sw_flow_key
*flow_key
,
940 const struct nlattr
*a
)
944 switch (nla_type(a
)) {
945 case OVS_KEY_ATTR_PRIORITY
:
946 OVS_SET_MASKED(skb
->priority
, nla_get_u32(a
),
947 *get_mask(a
, u32
*));
948 flow_key
->phy
.priority
= skb
->priority
;
951 case OVS_KEY_ATTR_SKB_MARK
:
952 OVS_SET_MASKED(skb
->mark
, nla_get_u32(a
), *get_mask(a
, u32
*));
953 flow_key
->phy
.skb_mark
= skb
->mark
;
956 case OVS_KEY_ATTR_TUNNEL_INFO
:
957 /* Masked data not supported for tunnel. */
961 case OVS_KEY_ATTR_ETHERNET
:
962 err
= set_eth_addr(skb
, flow_key
, nla_data(a
),
963 get_mask(a
, struct ovs_key_ethernet
*));
966 case OVS_KEY_ATTR_IPV4
:
967 err
= set_ipv4(skb
, flow_key
, nla_data(a
),
968 get_mask(a
, struct ovs_key_ipv4
*));
971 case OVS_KEY_ATTR_IPV6
:
972 err
= set_ipv6(skb
, flow_key
, nla_data(a
),
973 get_mask(a
, struct ovs_key_ipv6
*));
976 case OVS_KEY_ATTR_TCP
:
977 err
= set_tcp(skb
, flow_key
, nla_data(a
),
978 get_mask(a
, struct ovs_key_tcp
*));
981 case OVS_KEY_ATTR_UDP
:
982 err
= set_udp(skb
, flow_key
, nla_data(a
),
983 get_mask(a
, struct ovs_key_udp
*));
986 case OVS_KEY_ATTR_SCTP
:
987 err
= set_sctp(skb
, flow_key
, nla_data(a
),
988 get_mask(a
, struct ovs_key_sctp
*));
991 case OVS_KEY_ATTR_MPLS
:
992 err
= set_mpls(skb
, flow_key
, nla_data(a
), get_mask(a
,
996 case OVS_KEY_ATTR_CT_STATE
:
997 case OVS_KEY_ATTR_CT_ZONE
:
998 case OVS_KEY_ATTR_CT_MARK
:
999 case OVS_KEY_ATTR_CT_LABELS
:
1007 static int execute_recirc(struct datapath
*dp
, struct sk_buff
*skb
,
1008 struct sw_flow_key
*key
,
1009 const struct nlattr
*a
, int rem
)
1011 struct deferred_action
*da
;
1013 if (!is_flow_key_valid(key
)) {
1016 err
= ovs_flow_key_update(skb
, key
);
1020 BUG_ON(!is_flow_key_valid(key
));
1022 if (!nla_is_last(a
, rem
)) {
1023 /* Recirc action is the not the last action
1024 * of the action list, need to clone the skb.
1026 skb
= skb_clone(skb
, GFP_ATOMIC
);
1028 /* Skip the recirc action when out of memory, but
1029 * continue on with the rest of the action list.
1035 da
= add_deferred_actions(skb
, key
, NULL
);
1037 da
->pkt_key
.recirc_id
= nla_get_u32(a
);
1041 if (net_ratelimit())
1042 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1049 /* Execute a list of actions against 'skb'. */
1050 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1051 struct sw_flow_key
*key
,
1052 const struct nlattr
*attr
, int len
)
1054 /* Every output action needs a separate clone of 'skb', but the common
1055 * case is just a single output action, so that doing a clone and
1056 * then freeing the original skbuff is wasteful. So the following code
1057 * is slightly obscure just to avoid that.
1060 const struct nlattr
*a
;
1063 for (a
= attr
, rem
= len
; rem
> 0;
1064 a
= nla_next(a
, &rem
)) {
1067 if (unlikely(prev_port
!= -1)) {
1068 struct sk_buff
*out_skb
= skb_clone(skb
, GFP_ATOMIC
);
1071 do_output(dp
, out_skb
, prev_port
, key
);
1073 OVS_CB(skb
)->cutlen
= 0;
1077 switch (nla_type(a
)) {
1078 case OVS_ACTION_ATTR_OUTPUT
:
1079 prev_port
= nla_get_u32(a
);
1082 case OVS_ACTION_ATTR_TRUNC
: {
1083 struct ovs_action_trunc
*trunc
= nla_data(a
);
1085 if (skb
->len
> trunc
->max_len
)
1086 OVS_CB(skb
)->cutlen
= skb
->len
- trunc
->max_len
;
1090 case OVS_ACTION_ATTR_USERSPACE
:
1091 output_userspace(dp
, skb
, key
, a
, attr
,
1092 len
, OVS_CB(skb
)->cutlen
);
1093 OVS_CB(skb
)->cutlen
= 0;
1096 case OVS_ACTION_ATTR_HASH
:
1097 execute_hash(skb
, key
, a
);
1100 case OVS_ACTION_ATTR_PUSH_MPLS
:
1101 err
= push_mpls(skb
, key
, nla_data(a
));
1104 case OVS_ACTION_ATTR_POP_MPLS
:
1105 err
= pop_mpls(skb
, key
, nla_get_be16(a
));
1108 case OVS_ACTION_ATTR_PUSH_VLAN
:
1109 err
= push_vlan(skb
, key
, nla_data(a
));
1112 case OVS_ACTION_ATTR_POP_VLAN
:
1113 err
= pop_vlan(skb
, key
);
1116 case OVS_ACTION_ATTR_RECIRC
:
1117 err
= execute_recirc(dp
, skb
, key
, a
, rem
);
1118 if (nla_is_last(a
, rem
)) {
1119 /* If this is the last action, the skb has
1120 * been consumed or freed.
1121 * Return immediately.
1127 case OVS_ACTION_ATTR_SET
:
1128 err
= execute_set_action(skb
, key
, nla_data(a
));
1131 case OVS_ACTION_ATTR_SET_MASKED
:
1132 case OVS_ACTION_ATTR_SET_TO_MASKED
:
1133 err
= execute_masked_set_action(skb
, key
, nla_data(a
));
1136 case OVS_ACTION_ATTR_SAMPLE
:
1137 err
= sample(dp
, skb
, key
, a
, attr
, len
);
1140 case OVS_ACTION_ATTR_CT
:
1141 if (!is_flow_key_valid(key
)) {
1142 err
= ovs_flow_key_update(skb
, key
);
1147 err
= ovs_ct_execute(ovs_dp_get_net(dp
), skb
, key
,
1150 /* Hide stolen IP fragments from user space. */
1152 return err
== -EINPROGRESS
? 0 : err
;
1156 if (unlikely(err
)) {
1162 if (prev_port
!= -1)
1163 do_output(dp
, skb
, prev_port
, key
);
1170 static void process_deferred_actions(struct datapath
*dp
)
1172 struct action_fifo
*fifo
= this_cpu_ptr(action_fifos
);
1174 /* Do not touch the FIFO in case there is no deferred actions. */
1175 if (action_fifo_is_empty(fifo
))
1178 /* Finishing executing all deferred actions. */
1180 struct deferred_action
*da
= action_fifo_get(fifo
);
1181 struct sk_buff
*skb
= da
->skb
;
1182 struct sw_flow_key
*key
= &da
->pkt_key
;
1183 const struct nlattr
*actions
= da
->actions
;
1186 do_execute_actions(dp
, skb
, key
, actions
,
1189 ovs_dp_process_packet(skb
, key
);
1190 } while (!action_fifo_is_empty(fifo
));
1192 /* Reset FIFO for the next packet. */
1193 action_fifo_init(fifo
);
1196 /* Execute a list of actions against 'skb'. */
1197 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1198 const struct sw_flow_actions
*acts
,
1199 struct sw_flow_key
*key
)
1201 int level
= this_cpu_read(exec_actions_level
);
1204 if (unlikely(level
>= EXEC_ACTIONS_LEVEL_LIMIT
)) {
1205 if (net_ratelimit())
1206 pr_warn("%s: packet loop detected, dropping.\n",
1213 this_cpu_inc(exec_actions_level
);
1214 err
= do_execute_actions(dp
, skb
, key
,
1215 acts
->actions
, acts
->actions_len
);
1218 process_deferred_actions(dp
);
1220 this_cpu_dec(exec_actions_level
);
1222 /* This return status currently does not reflect the errors
1223 * encounted during deferred actions execution. Probably needs to
1224 * be fixed in the future.
1229 int action_fifos_init(void)
1231 action_fifos
= alloc_percpu(struct action_fifo
);
1238 void action_fifos_exit(void)
1240 free_percpu(action_fifos
);