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
;
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
];
85 struct sw_flow_key key
[OVS_DEFERRED_ACTION_THRESHOLD
];
88 static struct action_fifo __percpu
*action_fifos
;
89 static struct recirc_keys __percpu
*recirc_keys
;
90 static DEFINE_PER_CPU(int, exec_actions_level
);
92 static void action_fifo_init(struct action_fifo
*fifo
)
98 static bool action_fifo_is_empty(const struct action_fifo
*fifo
)
100 return (fifo
->head
== fifo
->tail
);
103 static struct deferred_action
*action_fifo_get(struct action_fifo
*fifo
)
105 if (action_fifo_is_empty(fifo
))
108 return &fifo
->fifo
[fifo
->tail
++];
111 static struct deferred_action
*action_fifo_put(struct action_fifo
*fifo
)
113 if (fifo
->head
>= DEFERRED_ACTION_FIFO_SIZE
- 1)
116 return &fifo
->fifo
[fifo
->head
++];
119 /* Return queue entry if fifo is not full */
120 static struct deferred_action
*add_deferred_actions(struct sk_buff
*skb
,
121 const struct sw_flow_key
*key
,
122 const struct nlattr
*actions
,
123 const int actions_len
)
125 struct action_fifo
*fifo
;
126 struct deferred_action
*da
;
128 fifo
= this_cpu_ptr(action_fifos
);
129 da
= action_fifo_put(fifo
);
132 da
->actions
= actions
;
133 da
->actions_len
= actions_len
;
140 static void invalidate_flow_key(struct sw_flow_key
*key
)
142 key
->mac_proto
|= SW_FLOW_KEY_INVALID
;
145 static bool is_flow_key_valid(const struct sw_flow_key
*key
)
147 return !(key
->mac_proto
& SW_FLOW_KEY_INVALID
);
150 static void update_ethertype(struct sk_buff
*skb
, struct ethhdr
*hdr
,
153 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
154 __be16 diff
[] = { ~(hdr
->h_proto
), ethertype
};
156 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
160 hdr
->h_proto
= ethertype
;
163 static int push_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
164 const struct ovs_action_push_mpls
*mpls
)
166 __be32
*new_mpls_lse
;
168 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
169 if (skb
->encapsulation
)
172 if (skb_cow_head(skb
, MPLS_HLEN
) < 0)
175 skb_push(skb
, MPLS_HLEN
);
176 memmove(skb_mac_header(skb
) - MPLS_HLEN
, skb_mac_header(skb
),
178 skb_reset_mac_header(skb
);
180 new_mpls_lse
= (__be32
*)skb_mpls_header(skb
);
181 *new_mpls_lse
= mpls
->mpls_lse
;
183 skb_postpush_rcsum(skb
, new_mpls_lse
, MPLS_HLEN
);
185 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
)
186 update_ethertype(skb
, eth_hdr(skb
), mpls
->mpls_ethertype
);
187 if (!ovs_skb_get_inner_protocol(skb
))
188 ovs_skb_set_inner_protocol(skb
, skb
->protocol
);
189 skb
->protocol
= mpls
->mpls_ethertype
;
191 invalidate_flow_key(key
);
195 static int pop_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
196 const __be16 ethertype
)
200 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
204 skb_postpull_rcsum(skb
, skb_mpls_header(skb
), MPLS_HLEN
);
206 memmove(skb_mac_header(skb
) + MPLS_HLEN
, skb_mac_header(skb
),
209 __skb_pull(skb
, MPLS_HLEN
);
210 skb_reset_mac_header(skb
);
212 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
) {
215 /* skb_mpls_header() is used to locate the ethertype
216 * field correctly in the presence of VLAN tags.
218 hdr
= (struct ethhdr
*)(skb_mpls_header(skb
) - ETH_HLEN
);
219 update_ethertype(skb
, hdr
, ethertype
);
221 if (eth_p_mpls(skb
->protocol
))
222 skb
->protocol
= ethertype
;
224 invalidate_flow_key(key
);
228 static int set_mpls(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
229 const __be32
*mpls_lse
, const __be32
*mask
)
235 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
239 stack
= (__be32
*)skb_mpls_header(skb
);
240 lse
= OVS_MASKED(*stack
, *mpls_lse
, *mask
);
241 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
242 __be32 diff
[] = { ~(*stack
), lse
};
244 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
249 flow_key
->mpls
.top_lse
= lse
;
253 static int pop_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
257 err
= skb_vlan_pop(skb
);
258 if (skb_vlan_tag_present(skb
)) {
259 invalidate_flow_key(key
);
261 key
->eth
.vlan
.tci
= 0;
262 key
->eth
.vlan
.tpid
= 0;
267 static int push_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
,
268 const struct ovs_action_push_vlan
*vlan
)
270 if (skb_vlan_tag_present(skb
)) {
271 invalidate_flow_key(key
);
273 key
->eth
.vlan
.tci
= vlan
->vlan_tci
;
274 key
->eth
.vlan
.tpid
= vlan
->vlan_tpid
;
276 return skb_vlan_push(skb
, vlan
->vlan_tpid
,
277 ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
280 /* 'src' is already properly masked. */
281 static void ether_addr_copy_masked(u8
*dst_
, const u8
*src_
, const u8
*mask_
)
283 u16
*dst
= (u16
*)dst_
;
284 const u16
*src
= (const u16
*)src_
;
285 const u16
*mask
= (const u16
*)mask_
;
287 OVS_SET_MASKED(dst
[0], src
[0], mask
[0]);
288 OVS_SET_MASKED(dst
[1], src
[1], mask
[1]);
289 OVS_SET_MASKED(dst
[2], src
[2], mask
[2]);
292 static int set_eth_addr(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
293 const struct ovs_key_ethernet
*key
,
294 const struct ovs_key_ethernet
*mask
)
298 err
= skb_ensure_writable(skb
, ETH_HLEN
);
302 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
304 ether_addr_copy_masked(eth_hdr(skb
)->h_source
, key
->eth_src
,
306 ether_addr_copy_masked(eth_hdr(skb
)->h_dest
, key
->eth_dst
,
309 skb_postpush_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
311 ether_addr_copy(flow_key
->eth
.src
, eth_hdr(skb
)->h_source
);
312 ether_addr_copy(flow_key
->eth
.dst
, eth_hdr(skb
)->h_dest
);
316 /* pop_eth does not support VLAN packets as this action is never called
319 static int pop_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
)
321 skb_pull_rcsum(skb
, ETH_HLEN
);
322 skb_reset_mac_header(skb
);
323 skb_reset_mac_len(skb
);
325 /* safe right before invalidate_flow_key */
326 key
->mac_proto
= MAC_PROTO_NONE
;
327 invalidate_flow_key(key
);
331 static int push_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
,
332 const struct ovs_action_push_eth
*ethh
)
336 /* Add the new Ethernet header */
337 if (skb_cow_head(skb
, ETH_HLEN
) < 0)
340 skb_push(skb
, ETH_HLEN
);
341 skb_reset_mac_header(skb
);
342 skb_reset_mac_len(skb
);
345 ether_addr_copy(hdr
->h_source
, ethh
->addresses
.eth_src
);
346 ether_addr_copy(hdr
->h_dest
, ethh
->addresses
.eth_dst
);
347 hdr
->h_proto
= skb
->protocol
;
349 skb_postpush_rcsum(skb
, hdr
, ETH_HLEN
);
351 /* safe right before invalidate_flow_key */
352 key
->mac_proto
= MAC_PROTO_ETHERNET
;
353 invalidate_flow_key(key
);
357 static void update_ip_l4_checksum(struct sk_buff
*skb
, struct iphdr
*nh
,
358 __be32 addr
, __be32 new_addr
)
360 int transport_len
= skb
->len
- skb_transport_offset(skb
);
362 if (nh
->frag_off
& htons(IP_OFFSET
))
365 if (nh
->protocol
== IPPROTO_TCP
) {
366 if (likely(transport_len
>= sizeof(struct tcphdr
)))
367 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
368 addr
, new_addr
, true);
369 } else if (nh
->protocol
== IPPROTO_UDP
) {
370 if (likely(transport_len
>= sizeof(struct udphdr
))) {
371 struct udphdr
*uh
= udp_hdr(skb
);
373 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
374 inet_proto_csum_replace4(&uh
->check
, skb
,
375 addr
, new_addr
, true);
377 uh
->check
= CSUM_MANGLED_0
;
384 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
385 __be32
*addr
, __be32 new_addr
)
387 update_ip_l4_checksum(skb
, nh
, *addr
, new_addr
);
388 csum_replace4(&nh
->check
, *addr
, new_addr
);
393 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
394 __be32 addr
[4], const __be32 new_addr
[4])
396 int transport_len
= skb
->len
- skb_transport_offset(skb
);
398 if (l4_proto
== NEXTHDR_TCP
) {
399 if (likely(transport_len
>= sizeof(struct tcphdr
)))
400 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
401 addr
, new_addr
, true);
402 } else if (l4_proto
== NEXTHDR_UDP
) {
403 if (likely(transport_len
>= sizeof(struct udphdr
))) {
404 struct udphdr
*uh
= udp_hdr(skb
);
406 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
407 inet_proto_csum_replace16(&uh
->check
, skb
,
408 addr
, new_addr
, true);
410 uh
->check
= CSUM_MANGLED_0
;
413 } else if (l4_proto
== NEXTHDR_ICMP
) {
414 if (likely(transport_len
>= sizeof(struct icmp6hdr
)))
415 inet_proto_csum_replace16(&icmp6_hdr(skb
)->icmp6_cksum
,
416 skb
, addr
, new_addr
, true);
420 static void mask_ipv6_addr(const __be32 old
[4], const __be32 addr
[4],
421 const __be32 mask
[4], __be32 masked
[4])
423 masked
[0] = OVS_MASKED(old
[0], addr
[0], mask
[0]);
424 masked
[1] = OVS_MASKED(old
[1], addr
[1], mask
[1]);
425 masked
[2] = OVS_MASKED(old
[2], addr
[2], mask
[2]);
426 masked
[3] = OVS_MASKED(old
[3], addr
[3], mask
[3]);
429 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
430 __be32 addr
[4], const __be32 new_addr
[4],
431 bool recalculate_csum
)
433 if (likely(recalculate_csum
))
434 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
437 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
440 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
, u32 mask
)
442 /* Bits 21-24 are always unmasked, so this retains their values. */
443 OVS_SET_MASKED(nh
->flow_lbl
[0], (u8
)(fl
>> 16), (u8
)(mask
>> 16));
444 OVS_SET_MASKED(nh
->flow_lbl
[1], (u8
)(fl
>> 8), (u8
)(mask
>> 8));
445 OVS_SET_MASKED(nh
->flow_lbl
[2], (u8
)fl
, (u8
)mask
);
448 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
,
451 new_ttl
= OVS_MASKED(nh
->ttl
, new_ttl
, mask
);
453 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
457 static int set_ipv4(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
458 const struct ovs_key_ipv4
*key
,
459 const struct ovs_key_ipv4
*mask
)
465 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
466 sizeof(struct iphdr
));
472 /* Setting an IP addresses is typically only a side effect of
473 * matching on them in the current userspace implementation, so it
474 * makes sense to check if the value actually changed.
476 if (mask
->ipv4_src
) {
477 new_addr
= OVS_MASKED(nh
->saddr
, key
->ipv4_src
, mask
->ipv4_src
);
479 if (unlikely(new_addr
!= nh
->saddr
)) {
480 set_ip_addr(skb
, nh
, &nh
->saddr
, new_addr
);
481 flow_key
->ipv4
.addr
.src
= new_addr
;
484 if (mask
->ipv4_dst
) {
485 new_addr
= OVS_MASKED(nh
->daddr
, key
->ipv4_dst
, mask
->ipv4_dst
);
487 if (unlikely(new_addr
!= nh
->daddr
)) {
488 set_ip_addr(skb
, nh
, &nh
->daddr
, new_addr
);
489 flow_key
->ipv4
.addr
.dst
= new_addr
;
492 if (mask
->ipv4_tos
) {
493 ipv4_change_dsfield(nh
, ~mask
->ipv4_tos
, key
->ipv4_tos
);
494 flow_key
->ip
.tos
= nh
->tos
;
496 if (mask
->ipv4_ttl
) {
497 set_ip_ttl(skb
, nh
, key
->ipv4_ttl
, mask
->ipv4_ttl
);
498 flow_key
->ip
.ttl
= nh
->ttl
;
504 static bool is_ipv6_mask_nonzero(const __be32 addr
[4])
506 return !!(addr
[0] | addr
[1] | addr
[2] | addr
[3]);
509 static int set_ipv6(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
510 const struct ovs_key_ipv6
*key
,
511 const struct ovs_key_ipv6
*mask
)
516 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
517 sizeof(struct ipv6hdr
));
523 /* Setting an IP addresses is typically only a side effect of
524 * matching on them in the current userspace implementation, so it
525 * makes sense to check if the value actually changed.
527 if (is_ipv6_mask_nonzero(mask
->ipv6_src
)) {
528 __be32
*saddr
= (__be32
*)&nh
->saddr
;
531 mask_ipv6_addr(saddr
, key
->ipv6_src
, mask
->ipv6_src
, masked
);
533 if (unlikely(memcmp(saddr
, masked
, sizeof(masked
)))) {
534 set_ipv6_addr(skb
, flow_key
->ip
.proto
, saddr
, masked
,
536 memcpy(&flow_key
->ipv6
.addr
.src
, masked
,
537 sizeof(flow_key
->ipv6
.addr
.src
));
540 if (is_ipv6_mask_nonzero(mask
->ipv6_dst
)) {
541 unsigned int offset
= 0;
542 int flags
= IP6_FH_F_SKIP_RH
;
543 bool recalc_csum
= true;
544 __be32
*daddr
= (__be32
*)&nh
->daddr
;
547 mask_ipv6_addr(daddr
, key
->ipv6_dst
, mask
->ipv6_dst
, masked
);
549 if (unlikely(memcmp(daddr
, masked
, sizeof(masked
)))) {
550 if (ipv6_ext_hdr(nh
->nexthdr
))
551 recalc_csum
= (ipv6_find_hdr(skb
, &offset
,
556 set_ipv6_addr(skb
, flow_key
->ip
.proto
, daddr
, masked
,
558 memcpy(&flow_key
->ipv6
.addr
.dst
, masked
,
559 sizeof(flow_key
->ipv6
.addr
.dst
));
562 if (mask
->ipv6_tclass
) {
563 ipv6_change_dsfield(nh
, ~mask
->ipv6_tclass
, key
->ipv6_tclass
);
564 flow_key
->ip
.tos
= ipv6_get_dsfield(nh
);
566 if (mask
->ipv6_label
) {
567 set_ipv6_fl(nh
, ntohl(key
->ipv6_label
),
568 ntohl(mask
->ipv6_label
));
569 flow_key
->ipv6
.label
=
570 *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
572 if (mask
->ipv6_hlimit
) {
573 OVS_SET_MASKED(nh
->hop_limit
, key
->ipv6_hlimit
,
575 flow_key
->ip
.ttl
= nh
->hop_limit
;
580 /* Must follow skb_ensure_writable() since that can move the skb data. */
581 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
582 __be16 new_port
, __sum16
*check
)
584 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, false);
588 static int set_udp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
589 const struct ovs_key_udp
*key
,
590 const struct ovs_key_udp
*mask
)
596 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
597 sizeof(struct udphdr
));
602 /* Either of the masks is non-zero, so do not bother checking them. */
603 src
= OVS_MASKED(uh
->source
, key
->udp_src
, mask
->udp_src
);
604 dst
= OVS_MASKED(uh
->dest
, key
->udp_dst
, mask
->udp_dst
);
606 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
607 if (likely(src
!= uh
->source
)) {
608 set_tp_port(skb
, &uh
->source
, src
, &uh
->check
);
609 flow_key
->tp
.src
= src
;
611 if (likely(dst
!= uh
->dest
)) {
612 set_tp_port(skb
, &uh
->dest
, dst
, &uh
->check
);
613 flow_key
->tp
.dst
= dst
;
616 if (unlikely(!uh
->check
))
617 uh
->check
= CSUM_MANGLED_0
;
621 flow_key
->tp
.src
= src
;
622 flow_key
->tp
.dst
= dst
;
630 static int set_tcp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
631 const struct ovs_key_tcp
*key
,
632 const struct ovs_key_tcp
*mask
)
638 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
639 sizeof(struct tcphdr
));
644 src
= OVS_MASKED(th
->source
, key
->tcp_src
, mask
->tcp_src
);
645 if (likely(src
!= th
->source
)) {
646 set_tp_port(skb
, &th
->source
, src
, &th
->check
);
647 flow_key
->tp
.src
= src
;
649 dst
= OVS_MASKED(th
->dest
, key
->tcp_dst
, mask
->tcp_dst
);
650 if (likely(dst
!= th
->dest
)) {
651 set_tp_port(skb
, &th
->dest
, dst
, &th
->check
);
652 flow_key
->tp
.dst
= dst
;
659 static int set_sctp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
660 const struct ovs_key_sctp
*key
,
661 const struct ovs_key_sctp
*mask
)
663 unsigned int sctphoff
= skb_transport_offset(skb
);
665 __le32 old_correct_csum
, new_csum
, old_csum
;
668 err
= skb_ensure_writable(skb
, sctphoff
+ sizeof(struct sctphdr
));
673 old_csum
= sh
->checksum
;
674 old_correct_csum
= sctp_compute_cksum(skb
, sctphoff
);
676 sh
->source
= OVS_MASKED(sh
->source
, key
->sctp_src
, mask
->sctp_src
);
677 sh
->dest
= OVS_MASKED(sh
->dest
, key
->sctp_dst
, mask
->sctp_dst
);
679 new_csum
= sctp_compute_cksum(skb
, sctphoff
);
681 /* Carry any checksum errors through. */
682 sh
->checksum
= old_csum
^ old_correct_csum
^ new_csum
;
685 flow_key
->tp
.src
= sh
->source
;
686 flow_key
->tp
.dst
= sh
->dest
;
691 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS
)
693 struct ovs_frag_data
*data
= this_cpu_ptr(&ovs_frag_data_storage
);
694 struct vport
*vport
= data
->vport
;
696 if (skb_cow_head(skb
, data
->l2_len
) < 0) {
701 __skb_dst_copy(skb
, data
->dst
);
702 *OVS_GSO_CB(skb
) = data
->cb
;
703 ovs_skb_set_inner_protocol(skb
, data
->inner_protocol
);
704 skb
->vlan_tci
= data
->vlan_tci
;
705 skb
->vlan_proto
= data
->vlan_proto
;
707 /* Reconstruct the MAC header. */
708 skb_push(skb
, data
->l2_len
);
709 memcpy(skb
->data
, &data
->l2_data
, data
->l2_len
);
710 skb_postpush_rcsum(skb
, skb
->data
, data
->l2_len
);
711 skb_reset_mac_header(skb
);
713 ovs_vport_send(vport
, skb
, data
->mac_proto
);
718 ovs_dst_get_mtu(const struct dst_entry
*dst
)
720 return dst
->dev
->mtu
;
723 static struct dst_ops ovs_dst_ops
= {
725 .mtu
= ovs_dst_get_mtu
,
728 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
729 * ovs_vport_output(), which is called once per fragmented packet.
731 static void prepare_frag(struct vport
*vport
, struct sk_buff
*skb
,
734 unsigned int hlen
= skb_network_offset(skb
);
735 struct ovs_frag_data
*data
;
737 data
= this_cpu_ptr(&ovs_frag_data_storage
);
738 data
->dst
= (unsigned long) skb_dst(skb
);
740 data
->cb
= *OVS_GSO_CB(skb
);
741 data
->inner_protocol
= ovs_skb_get_inner_protocol(skb
);
742 data
->vlan_tci
= skb
->vlan_tci
;
743 data
->vlan_proto
= skb
->vlan_proto
;
744 data
->mac_proto
= mac_proto
;
746 memcpy(&data
->l2_data
, skb
->data
, hlen
);
748 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
752 static void ovs_fragment(struct net
*net
, struct vport
*vport
,
753 struct sk_buff
*skb
, u16 mru
,
754 struct sw_flow_key
*key
)
756 if (skb_network_offset(skb
) > MAX_L2_LEN
) {
757 OVS_NLERR(1, "L2 header too long to fragment");
761 if (key
->eth
.type
== htons(ETH_P_IP
)) {
762 struct dst_entry ovs_dst
;
763 unsigned long orig_dst
;
765 prepare_frag(vport
, skb
, ovs_key_mac_proto(key
));
766 dst_init(&ovs_dst
, &ovs_dst_ops
, NULL
, 1,
767 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
768 ovs_dst
.dev
= vport
->dev
;
770 orig_dst
= (unsigned long) skb_dst(skb
);
771 skb_dst_set_noref(skb
, &ovs_dst
);
772 IPCB(skb
)->frag_max_size
= mru
;
774 ip_do_fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
775 refdst_drop(orig_dst
);
776 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
777 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
778 unsigned long orig_dst
;
779 struct rt6_info ovs_rt
;
784 prepare_frag(vport
, skb
,
785 ovs_key_mac_proto(key
));
786 memset(&ovs_rt
, 0, sizeof(ovs_rt
));
787 dst_init(&ovs_rt
.dst
, &ovs_dst_ops
, NULL
, 1,
788 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
789 ovs_rt
.dst
.dev
= vport
->dev
;
791 orig_dst
= (unsigned long) skb_dst(skb
);
792 skb_dst_set_noref(skb
, &ovs_rt
.dst
);
793 IP6CB(skb
)->frag_max_size
= mru
;
794 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
795 v6ops
->fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
797 v6ops
->fragment(skb
->sk
, skb
, ovs_vport_output
);
799 refdst_drop(orig_dst
);
801 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
802 ovs_vport_name(vport
), ntohs(key
->eth
.type
), mru
,
812 static void do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
,
813 struct sw_flow_key
*key
)
815 struct vport
*vport
= ovs_vport_rcu(dp
, out_port
);
818 u16 mru
= OVS_CB(skb
)->mru
;
819 u32 cutlen
= OVS_CB(skb
)->cutlen
;
821 if (unlikely(cutlen
> 0)) {
822 if (skb
->len
- cutlen
> ovs_mac_header_len(key
))
823 pskb_trim(skb
, skb
->len
- cutlen
);
825 pskb_trim(skb
, ovs_mac_header_len(key
));
829 (skb
->len
<= mru
+ vport
->dev
->hard_header_len
))) {
830 ovs_vport_send(vport
, skb
, ovs_key_mac_proto(key
));
831 } else if (mru
<= vport
->dev
->mtu
) {
832 struct net
*net
= ovs_dp_get_net(dp
);
834 ovs_fragment(net
, vport
, skb
, mru
, key
);
836 OVS_NLERR(true, "Cannot fragment IP frames");
844 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
845 struct sw_flow_key
*key
, const struct nlattr
*attr
,
846 const struct nlattr
*actions
, int actions_len
,
849 struct dp_upcall_info upcall
;
850 const struct nlattr
*a
;
853 memset(&upcall
, 0, sizeof(upcall
));
854 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
855 upcall
.mru
= OVS_CB(skb
)->mru
;
857 SKB_INIT_FILL_METADATA_DST(skb
);
858 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
859 a
= nla_next(a
, &rem
)) {
860 switch (nla_type(a
)) {
861 case OVS_USERSPACE_ATTR_USERDATA
:
865 case OVS_USERSPACE_ATTR_PID
:
866 upcall
.portid
= nla_get_u32(a
);
869 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
: {
870 /* Get out tunnel info. */
873 vport
= ovs_vport_rcu(dp
, nla_get_u32(a
));
875 err
= dev_fill_metadata_dst(vport
->dev
, skb
);
877 upcall
.egress_tun_info
= skb_tunnel_info(skb
);
883 case OVS_USERSPACE_ATTR_ACTIONS
: {
884 /* Include actions. */
885 upcall
.actions
= actions
;
886 upcall
.actions_len
= actions_len
;
890 } /* End of switch. */
893 err
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, cutlen
);
894 SKB_RESTORE_FILL_METADATA_DST(skb
);
898 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
899 struct sw_flow_key
*key
, const struct nlattr
*attr
,
900 const struct nlattr
*actions
, int actions_len
)
902 const struct nlattr
*acts_list
= NULL
;
903 const struct nlattr
*a
;
907 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
908 a
= nla_next(a
, &rem
)) {
911 switch (nla_type(a
)) {
912 case OVS_SAMPLE_ATTR_PROBABILITY
:
913 probability
= nla_get_u32(a
);
914 if (!probability
|| prandom_u32() > probability
)
918 case OVS_SAMPLE_ATTR_ACTIONS
:
924 rem
= nla_len(acts_list
);
925 a
= nla_data(acts_list
);
927 /* Actions list is empty, do nothing */
931 /* The only known usage of sample action is having a single user-space
932 * action, or having a truncate action followed by a single user-space
933 * action. Treat this usage as a special case.
934 * The output_userspace() should clone the skb to be sent to the
935 * user space. This skb will be consumed by its caller.
937 if (unlikely(nla_type(a
) == OVS_ACTION_ATTR_TRUNC
)) {
938 struct ovs_action_trunc
*trunc
= nla_data(a
);
940 if (skb
->len
> trunc
->max_len
)
941 cutlen
= skb
->len
- trunc
->max_len
;
943 a
= nla_next(a
, &rem
);
946 if (likely(nla_type(a
) == OVS_ACTION_ATTR_USERSPACE
&&
947 nla_is_last(a
, rem
)))
948 return output_userspace(dp
, skb
, key
, a
, actions
,
949 actions_len
, cutlen
);
951 skb
= skb_clone(skb
, GFP_ATOMIC
);
953 /* Skip the sample action when out of memory. */
956 if (!add_deferred_actions(skb
, key
, nla_data(acts_list
),
957 nla_len(acts_list
))) {
959 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
967 static void execute_hash(struct sk_buff
*skb
, struct sw_flow_key
*key
,
968 const struct nlattr
*attr
)
970 struct ovs_action_hash
*hash_act
= nla_data(attr
);
973 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
974 hash
= skb_get_hash(skb
);
975 hash
= jhash_1word(hash
, hash_act
->hash_basis
);
979 key
->ovs_flow_hash
= hash
;
982 static int execute_set_action(struct sk_buff
*skb
,
983 struct sw_flow_key
*flow_key
,
984 const struct nlattr
*a
)
986 /* Only tunnel set execution is supported without a mask. */
987 if (nla_type(a
) == OVS_KEY_ATTR_TUNNEL_INFO
) {
988 struct ovs_tunnel_info
*tun
= nla_data(a
);
990 ovs_skb_dst_drop(skb
);
991 ovs_dst_hold((struct dst_entry
*)tun
->tun_dst
);
992 ovs_skb_dst_set(skb
, (struct dst_entry
*)tun
->tun_dst
);
999 /* Mask is at the midpoint of the data. */
1000 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1002 static int execute_masked_set_action(struct sk_buff
*skb
,
1003 struct sw_flow_key
*flow_key
,
1004 const struct nlattr
*a
)
1008 switch (nla_type(a
)) {
1009 case OVS_KEY_ATTR_PRIORITY
:
1010 OVS_SET_MASKED(skb
->priority
, nla_get_u32(a
),
1011 *get_mask(a
, u32
*));
1012 flow_key
->phy
.priority
= skb
->priority
;
1015 case OVS_KEY_ATTR_SKB_MARK
:
1016 OVS_SET_MASKED(skb
->mark
, nla_get_u32(a
), *get_mask(a
, u32
*));
1017 flow_key
->phy
.skb_mark
= skb
->mark
;
1020 case OVS_KEY_ATTR_TUNNEL_INFO
:
1021 /* Masked data not supported for tunnel. */
1025 case OVS_KEY_ATTR_ETHERNET
:
1026 err
= set_eth_addr(skb
, flow_key
, nla_data(a
),
1027 get_mask(a
, struct ovs_key_ethernet
*));
1030 case OVS_KEY_ATTR_IPV4
:
1031 err
= set_ipv4(skb
, flow_key
, nla_data(a
),
1032 get_mask(a
, struct ovs_key_ipv4
*));
1035 case OVS_KEY_ATTR_IPV6
:
1036 err
= set_ipv6(skb
, flow_key
, nla_data(a
),
1037 get_mask(a
, struct ovs_key_ipv6
*));
1040 case OVS_KEY_ATTR_TCP
:
1041 err
= set_tcp(skb
, flow_key
, nla_data(a
),
1042 get_mask(a
, struct ovs_key_tcp
*));
1045 case OVS_KEY_ATTR_UDP
:
1046 err
= set_udp(skb
, flow_key
, nla_data(a
),
1047 get_mask(a
, struct ovs_key_udp
*));
1050 case OVS_KEY_ATTR_SCTP
:
1051 err
= set_sctp(skb
, flow_key
, nla_data(a
),
1052 get_mask(a
, struct ovs_key_sctp
*));
1055 case OVS_KEY_ATTR_MPLS
:
1056 err
= set_mpls(skb
, flow_key
, nla_data(a
), get_mask(a
,
1060 case OVS_KEY_ATTR_CT_STATE
:
1061 case OVS_KEY_ATTR_CT_ZONE
:
1062 case OVS_KEY_ATTR_CT_MARK
:
1063 case OVS_KEY_ATTR_CT_LABELS
:
1064 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
1065 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
1073 static int execute_recirc(struct datapath
*dp
, struct sk_buff
*skb
,
1074 struct sw_flow_key
*key
,
1075 const struct nlattr
*a
, int rem
)
1077 struct deferred_action
*da
;
1080 if (!is_flow_key_valid(key
)) {
1083 err
= ovs_flow_key_update(skb
, key
);
1087 BUG_ON(!is_flow_key_valid(key
));
1089 if (!nla_is_last(a
, rem
)) {
1090 /* Recirc action is the not the last action
1091 * of the action list, need to clone the skb.
1093 skb
= skb_clone(skb
, GFP_ATOMIC
);
1095 /* Skip the recirc action when out of memory, but
1096 * continue on with the rest of the action list.
1102 level
= this_cpu_read(exec_actions_level
);
1103 if (level
<= OVS_DEFERRED_ACTION_THRESHOLD
) {
1104 struct recirc_keys
*rks
= this_cpu_ptr(recirc_keys
);
1105 struct sw_flow_key
*recirc_key
= &rks
->key
[level
- 1];
1108 recirc_key
->recirc_id
= nla_get_u32(a
);
1109 ovs_dp_process_packet(skb
, recirc_key
);
1114 da
= add_deferred_actions(skb
, key
, NULL
, 0);
1116 da
->pkt_key
.recirc_id
= nla_get_u32(a
);
1120 if (net_ratelimit())
1121 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1128 /* Execute a list of actions against 'skb'. */
1129 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1130 struct sw_flow_key
*key
,
1131 const struct nlattr
*attr
, int len
)
1133 const struct nlattr
*a
;
1136 for (a
= attr
, rem
= len
; rem
> 0;
1137 a
= nla_next(a
, &rem
)) {
1140 switch (nla_type(a
)) {
1141 case OVS_ACTION_ATTR_OUTPUT
: {
1142 int port
= nla_get_u32(a
);
1143 struct sk_buff
*clone
;
1145 /* Every output action needs a separate clone
1146 * of 'skb', In case the output action is the
1147 * last action, cloning can be avoided.
1149 if (nla_is_last(a
, rem
)) {
1150 do_output(dp
, skb
, port
, key
);
1151 /* 'skb' has been used for output.
1156 clone
= skb_clone(skb
, GFP_ATOMIC
);
1158 do_output(dp
, clone
, port
, key
);
1159 OVS_CB(skb
)->cutlen
= 0;
1163 case OVS_ACTION_ATTR_TRUNC
: {
1164 struct ovs_action_trunc
*trunc
= nla_data(a
);
1166 if (skb
->len
> trunc
->max_len
)
1167 OVS_CB(skb
)->cutlen
= skb
->len
- trunc
->max_len
;
1171 case OVS_ACTION_ATTR_USERSPACE
:
1172 output_userspace(dp
, skb
, key
, a
, attr
,
1173 len
, OVS_CB(skb
)->cutlen
);
1174 OVS_CB(skb
)->cutlen
= 0;
1177 case OVS_ACTION_ATTR_HASH
:
1178 execute_hash(skb
, key
, a
);
1181 case OVS_ACTION_ATTR_PUSH_MPLS
:
1182 err
= push_mpls(skb
, key
, nla_data(a
));
1185 case OVS_ACTION_ATTR_POP_MPLS
:
1186 err
= pop_mpls(skb
, key
, nla_get_be16(a
));
1189 case OVS_ACTION_ATTR_PUSH_VLAN
:
1190 err
= push_vlan(skb
, key
, nla_data(a
));
1193 case OVS_ACTION_ATTR_POP_VLAN
:
1194 err
= pop_vlan(skb
, key
);
1197 case OVS_ACTION_ATTR_RECIRC
:
1198 err
= execute_recirc(dp
, skb
, key
, a
, rem
);
1199 if (nla_is_last(a
, rem
)) {
1200 /* If this is the last action, the skb has
1201 * been consumed or freed.
1202 * Return immediately.
1208 case OVS_ACTION_ATTR_SET
:
1209 err
= execute_set_action(skb
, key
, nla_data(a
));
1212 case OVS_ACTION_ATTR_SET_MASKED
:
1213 case OVS_ACTION_ATTR_SET_TO_MASKED
:
1214 err
= execute_masked_set_action(skb
, key
, nla_data(a
));
1217 case OVS_ACTION_ATTR_SAMPLE
:
1218 err
= sample(dp
, skb
, key
, a
, attr
, len
);
1221 case OVS_ACTION_ATTR_CT
:
1222 if (!is_flow_key_valid(key
)) {
1223 err
= ovs_flow_key_update(skb
, key
);
1228 err
= ovs_ct_execute(ovs_dp_get_net(dp
), skb
, key
,
1231 /* Hide stolen IP fragments from user space. */
1233 return err
== -EINPROGRESS
? 0 : err
;
1236 case OVS_ACTION_ATTR_PUSH_ETH
:
1237 err
= push_eth(skb
, key
, nla_data(a
));
1240 case OVS_ACTION_ATTR_POP_ETH
:
1241 err
= pop_eth(skb
, key
);
1245 if (unlikely(err
)) {
1255 static void process_deferred_actions(struct datapath
*dp
)
1257 struct action_fifo
*fifo
= this_cpu_ptr(action_fifos
);
1259 /* Do not touch the FIFO in case there is no deferred actions. */
1260 if (action_fifo_is_empty(fifo
))
1263 /* Finishing executing all deferred actions. */
1265 struct deferred_action
*da
= action_fifo_get(fifo
);
1266 struct sk_buff
*skb
= da
->skb
;
1267 struct sw_flow_key
*key
= &da
->pkt_key
;
1268 const struct nlattr
*actions
= da
->actions
;
1269 int actions_len
= da
->actions_len
;
1272 do_execute_actions(dp
, skb
, key
, actions
, actions_len
);
1274 ovs_dp_process_packet(skb
, key
);
1275 } while (!action_fifo_is_empty(fifo
));
1277 /* Reset FIFO for the next packet. */
1278 action_fifo_init(fifo
);
1281 /* Execute a list of actions against 'skb'. */
1282 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1283 const struct sw_flow_actions
*acts
,
1284 struct sw_flow_key
*key
)
1288 level
= __this_cpu_inc_return(exec_actions_level
);
1289 if (unlikely(level
> OVS_RECURSION_LIMIT
)) {
1290 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1297 err
= do_execute_actions(dp
, skb
, key
,
1298 acts
->actions
, acts
->actions_len
);
1301 process_deferred_actions(dp
);
1304 __this_cpu_dec(exec_actions_level
);
1308 int action_fifos_init(void)
1310 action_fifos
= alloc_percpu(struct action_fifo
);
1314 recirc_keys
= alloc_percpu(struct recirc_keys
);
1316 free_percpu(action_fifos
);
1323 void action_fifos_exit(void)
1325 free_percpu(action_fifos
);
1326 free_percpu(recirc_keys
);