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 void update_ethertype(struct sk_buff
*skb
, struct ethhdr
*hdr
,
170 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
171 __be16 diff
[] = { ~(hdr
->h_proto
), ethertype
};
173 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
177 hdr
->h_proto
= ethertype
;
180 static int push_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
181 const struct ovs_action_push_mpls
*mpls
)
183 __be32
*new_mpls_lse
;
185 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
186 if (skb
->encapsulation
)
189 if (skb_cow_head(skb
, MPLS_HLEN
) < 0)
192 skb_push(skb
, MPLS_HLEN
);
193 memmove(skb_mac_header(skb
) - MPLS_HLEN
, skb_mac_header(skb
),
195 skb_reset_mac_header(skb
);
197 new_mpls_lse
= (__be32
*)skb_mpls_header(skb
);
198 *new_mpls_lse
= mpls
->mpls_lse
;
200 skb_postpush_rcsum(skb
, new_mpls_lse
, MPLS_HLEN
);
202 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
)
203 update_ethertype(skb
, eth_hdr(skb
), mpls
->mpls_ethertype
);
204 if (!ovs_skb_get_inner_protocol(skb
))
205 ovs_skb_set_inner_protocol(skb
, skb
->protocol
);
206 skb
->protocol
= mpls
->mpls_ethertype
;
208 invalidate_flow_key(key
);
212 static int pop_mpls(struct sk_buff
*skb
, struct sw_flow_key
*key
,
213 const __be16 ethertype
)
217 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
221 skb_postpull_rcsum(skb
, skb_mpls_header(skb
), MPLS_HLEN
);
223 memmove(skb_mac_header(skb
) + MPLS_HLEN
, skb_mac_header(skb
),
226 __skb_pull(skb
, MPLS_HLEN
);
227 skb_reset_mac_header(skb
);
229 if (ovs_key_mac_proto(key
) == MAC_PROTO_ETHERNET
) {
232 /* skb_mpls_header() is used to locate the ethertype
233 * field correctly in the presence of VLAN tags.
235 hdr
= (struct ethhdr
*)(skb_mpls_header(skb
) - ETH_HLEN
);
236 update_ethertype(skb
, hdr
, ethertype
);
238 if (eth_p_mpls(skb
->protocol
))
239 skb
->protocol
= ethertype
;
241 invalidate_flow_key(key
);
245 static int set_mpls(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
246 const __be32
*mpls_lse
, const __be32
*mask
)
252 err
= skb_ensure_writable(skb
, skb
->mac_len
+ MPLS_HLEN
);
256 stack
= (__be32
*)skb_mpls_header(skb
);
257 lse
= OVS_MASKED(*stack
, *mpls_lse
, *mask
);
258 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
259 __be32 diff
[] = { ~(*stack
), lse
};
261 skb
->csum
= ~csum_partial((char *)diff
, sizeof(diff
),
266 flow_key
->mpls
.top_lse
= lse
;
270 static int pop_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
)
274 err
= skb_vlan_pop(skb
);
275 if (skb_vlan_tag_present(skb
)) {
276 invalidate_flow_key(key
);
278 key
->eth
.vlan
.tci
= 0;
279 key
->eth
.vlan
.tpid
= 0;
284 static int push_vlan(struct sk_buff
*skb
, struct sw_flow_key
*key
,
285 const struct ovs_action_push_vlan
*vlan
)
287 if (skb_vlan_tag_present(skb
)) {
288 invalidate_flow_key(key
);
290 key
->eth
.vlan
.tci
= vlan
->vlan_tci
;
291 key
->eth
.vlan
.tpid
= vlan
->vlan_tpid
;
293 return skb_vlan_push(skb
, vlan
->vlan_tpid
,
294 ntohs(vlan
->vlan_tci
) & ~VLAN_TAG_PRESENT
);
297 /* 'src' is already properly masked. */
298 static void ether_addr_copy_masked(u8
*dst_
, const u8
*src_
, const u8
*mask_
)
300 u16
*dst
= (u16
*)dst_
;
301 const u16
*src
= (const u16
*)src_
;
302 const u16
*mask
= (const u16
*)mask_
;
304 OVS_SET_MASKED(dst
[0], src
[0], mask
[0]);
305 OVS_SET_MASKED(dst
[1], src
[1], mask
[1]);
306 OVS_SET_MASKED(dst
[2], src
[2], mask
[2]);
309 static int set_eth_addr(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
310 const struct ovs_key_ethernet
*key
,
311 const struct ovs_key_ethernet
*mask
)
315 err
= skb_ensure_writable(skb
, ETH_HLEN
);
319 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
321 ether_addr_copy_masked(eth_hdr(skb
)->h_source
, key
->eth_src
,
323 ether_addr_copy_masked(eth_hdr(skb
)->h_dest
, key
->eth_dst
,
326 skb_postpush_rcsum(skb
, eth_hdr(skb
), ETH_ALEN
* 2);
328 ether_addr_copy(flow_key
->eth
.src
, eth_hdr(skb
)->h_source
);
329 ether_addr_copy(flow_key
->eth
.dst
, eth_hdr(skb
)->h_dest
);
333 /* pop_eth does not support VLAN packets as this action is never called
336 static int pop_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
)
338 skb_pull_rcsum(skb
, ETH_HLEN
);
339 skb_reset_mac_header(skb
);
340 skb_reset_mac_len(skb
);
342 /* safe right before invalidate_flow_key */
343 key
->mac_proto
= MAC_PROTO_NONE
;
344 invalidate_flow_key(key
);
348 static int push_eth(struct sk_buff
*skb
, struct sw_flow_key
*key
,
349 const struct ovs_action_push_eth
*ethh
)
353 /* Add the new Ethernet header */
354 if (skb_cow_head(skb
, ETH_HLEN
) < 0)
357 skb_push(skb
, ETH_HLEN
);
358 skb_reset_mac_header(skb
);
359 skb_reset_mac_len(skb
);
362 ether_addr_copy(hdr
->h_source
, ethh
->addresses
.eth_src
);
363 ether_addr_copy(hdr
->h_dest
, ethh
->addresses
.eth_dst
);
364 hdr
->h_proto
= skb
->protocol
;
366 skb_postpush_rcsum(skb
, hdr
, ETH_HLEN
);
368 /* safe right before invalidate_flow_key */
369 key
->mac_proto
= MAC_PROTO_ETHERNET
;
370 invalidate_flow_key(key
);
374 static void update_ip_l4_checksum(struct sk_buff
*skb
, struct iphdr
*nh
,
375 __be32 addr
, __be32 new_addr
)
377 int transport_len
= skb
->len
- skb_transport_offset(skb
);
379 if (nh
->frag_off
& htons(IP_OFFSET
))
382 if (nh
->protocol
== IPPROTO_TCP
) {
383 if (likely(transport_len
>= sizeof(struct tcphdr
)))
384 inet_proto_csum_replace4(&tcp_hdr(skb
)->check
, skb
,
385 addr
, new_addr
, true);
386 } else if (nh
->protocol
== IPPROTO_UDP
) {
387 if (likely(transport_len
>= sizeof(struct udphdr
))) {
388 struct udphdr
*uh
= udp_hdr(skb
);
390 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
391 inet_proto_csum_replace4(&uh
->check
, skb
,
392 addr
, new_addr
, true);
394 uh
->check
= CSUM_MANGLED_0
;
401 static void set_ip_addr(struct sk_buff
*skb
, struct iphdr
*nh
,
402 __be32
*addr
, __be32 new_addr
)
404 update_ip_l4_checksum(skb
, nh
, *addr
, new_addr
);
405 csum_replace4(&nh
->check
, *addr
, new_addr
);
410 static void update_ipv6_checksum(struct sk_buff
*skb
, u8 l4_proto
,
411 __be32 addr
[4], const __be32 new_addr
[4])
413 int transport_len
= skb
->len
- skb_transport_offset(skb
);
415 if (l4_proto
== NEXTHDR_TCP
) {
416 if (likely(transport_len
>= sizeof(struct tcphdr
)))
417 inet_proto_csum_replace16(&tcp_hdr(skb
)->check
, skb
,
418 addr
, new_addr
, true);
419 } else if (l4_proto
== NEXTHDR_UDP
) {
420 if (likely(transport_len
>= sizeof(struct udphdr
))) {
421 struct udphdr
*uh
= udp_hdr(skb
);
423 if (uh
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
424 inet_proto_csum_replace16(&uh
->check
, skb
,
425 addr
, new_addr
, true);
427 uh
->check
= CSUM_MANGLED_0
;
430 } else if (l4_proto
== NEXTHDR_ICMP
) {
431 if (likely(transport_len
>= sizeof(struct icmp6hdr
)))
432 inet_proto_csum_replace16(&icmp6_hdr(skb
)->icmp6_cksum
,
433 skb
, addr
, new_addr
, true);
437 static void mask_ipv6_addr(const __be32 old
[4], const __be32 addr
[4],
438 const __be32 mask
[4], __be32 masked
[4])
440 masked
[0] = OVS_MASKED(old
[0], addr
[0], mask
[0]);
441 masked
[1] = OVS_MASKED(old
[1], addr
[1], mask
[1]);
442 masked
[2] = OVS_MASKED(old
[2], addr
[2], mask
[2]);
443 masked
[3] = OVS_MASKED(old
[3], addr
[3], mask
[3]);
446 static void set_ipv6_addr(struct sk_buff
*skb
, u8 l4_proto
,
447 __be32 addr
[4], const __be32 new_addr
[4],
448 bool recalculate_csum
)
450 if (likely(recalculate_csum
))
451 update_ipv6_checksum(skb
, l4_proto
, addr
, new_addr
);
454 memcpy(addr
, new_addr
, sizeof(__be32
[4]));
457 static void set_ipv6_fl(struct ipv6hdr
*nh
, u32 fl
, u32 mask
)
459 /* Bits 21-24 are always unmasked, so this retains their values. */
460 OVS_SET_MASKED(nh
->flow_lbl
[0], (u8
)(fl
>> 16), (u8
)(mask
>> 16));
461 OVS_SET_MASKED(nh
->flow_lbl
[1], (u8
)(fl
>> 8), (u8
)(mask
>> 8));
462 OVS_SET_MASKED(nh
->flow_lbl
[2], (u8
)fl
, (u8
)mask
);
465 static void set_ip_ttl(struct sk_buff
*skb
, struct iphdr
*nh
, u8 new_ttl
,
468 new_ttl
= OVS_MASKED(nh
->ttl
, new_ttl
, mask
);
470 csum_replace2(&nh
->check
, htons(nh
->ttl
<< 8), htons(new_ttl
<< 8));
474 static int set_ipv4(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
475 const struct ovs_key_ipv4
*key
,
476 const struct ovs_key_ipv4
*mask
)
482 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
483 sizeof(struct iphdr
));
489 /* Setting an IP addresses is typically only a side effect of
490 * matching on them in the current userspace implementation, so it
491 * makes sense to check if the value actually changed.
493 if (mask
->ipv4_src
) {
494 new_addr
= OVS_MASKED(nh
->saddr
, key
->ipv4_src
, mask
->ipv4_src
);
496 if (unlikely(new_addr
!= nh
->saddr
)) {
497 set_ip_addr(skb
, nh
, &nh
->saddr
, new_addr
);
498 flow_key
->ipv4
.addr
.src
= new_addr
;
501 if (mask
->ipv4_dst
) {
502 new_addr
= OVS_MASKED(nh
->daddr
, key
->ipv4_dst
, mask
->ipv4_dst
);
504 if (unlikely(new_addr
!= nh
->daddr
)) {
505 set_ip_addr(skb
, nh
, &nh
->daddr
, new_addr
);
506 flow_key
->ipv4
.addr
.dst
= new_addr
;
509 if (mask
->ipv4_tos
) {
510 ipv4_change_dsfield(nh
, ~mask
->ipv4_tos
, key
->ipv4_tos
);
511 flow_key
->ip
.tos
= nh
->tos
;
513 if (mask
->ipv4_ttl
) {
514 set_ip_ttl(skb
, nh
, key
->ipv4_ttl
, mask
->ipv4_ttl
);
515 flow_key
->ip
.ttl
= nh
->ttl
;
521 static bool is_ipv6_mask_nonzero(const __be32 addr
[4])
523 return !!(addr
[0] | addr
[1] | addr
[2] | addr
[3]);
526 static int set_ipv6(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
527 const struct ovs_key_ipv6
*key
,
528 const struct ovs_key_ipv6
*mask
)
533 err
= skb_ensure_writable(skb
, skb_network_offset(skb
) +
534 sizeof(struct ipv6hdr
));
540 /* Setting an IP addresses is typically only a side effect of
541 * matching on them in the current userspace implementation, so it
542 * makes sense to check if the value actually changed.
544 if (is_ipv6_mask_nonzero(mask
->ipv6_src
)) {
545 __be32
*saddr
= (__be32
*)&nh
->saddr
;
548 mask_ipv6_addr(saddr
, key
->ipv6_src
, mask
->ipv6_src
, masked
);
550 if (unlikely(memcmp(saddr
, masked
, sizeof(masked
)))) {
551 set_ipv6_addr(skb
, flow_key
->ip
.proto
, saddr
, masked
,
553 memcpy(&flow_key
->ipv6
.addr
.src
, masked
,
554 sizeof(flow_key
->ipv6
.addr
.src
));
557 if (is_ipv6_mask_nonzero(mask
->ipv6_dst
)) {
558 unsigned int offset
= 0;
559 int flags
= IP6_FH_F_SKIP_RH
;
560 bool recalc_csum
= true;
561 __be32
*daddr
= (__be32
*)&nh
->daddr
;
564 mask_ipv6_addr(daddr
, key
->ipv6_dst
, mask
->ipv6_dst
, masked
);
566 if (unlikely(memcmp(daddr
, masked
, sizeof(masked
)))) {
567 if (ipv6_ext_hdr(nh
->nexthdr
))
568 recalc_csum
= (ipv6_find_hdr(skb
, &offset
,
573 set_ipv6_addr(skb
, flow_key
->ip
.proto
, daddr
, masked
,
575 memcpy(&flow_key
->ipv6
.addr
.dst
, masked
,
576 sizeof(flow_key
->ipv6
.addr
.dst
));
579 if (mask
->ipv6_tclass
) {
580 ipv6_change_dsfield(nh
, ~mask
->ipv6_tclass
, key
->ipv6_tclass
);
581 flow_key
->ip
.tos
= ipv6_get_dsfield(nh
);
583 if (mask
->ipv6_label
) {
584 set_ipv6_fl(nh
, ntohl(key
->ipv6_label
),
585 ntohl(mask
->ipv6_label
));
586 flow_key
->ipv6
.label
=
587 *(__be32
*)nh
& htonl(IPV6_FLOWINFO_FLOWLABEL
);
589 if (mask
->ipv6_hlimit
) {
590 OVS_SET_MASKED(nh
->hop_limit
, key
->ipv6_hlimit
,
592 flow_key
->ip
.ttl
= nh
->hop_limit
;
597 /* Must follow skb_ensure_writable() since that can move the skb data. */
598 static void set_tp_port(struct sk_buff
*skb
, __be16
*port
,
599 __be16 new_port
, __sum16
*check
)
601 inet_proto_csum_replace2(check
, skb
, *port
, new_port
, false);
605 static int set_udp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
606 const struct ovs_key_udp
*key
,
607 const struct ovs_key_udp
*mask
)
613 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
614 sizeof(struct udphdr
));
619 /* Either of the masks is non-zero, so do not bother checking them. */
620 src
= OVS_MASKED(uh
->source
, key
->udp_src
, mask
->udp_src
);
621 dst
= OVS_MASKED(uh
->dest
, key
->udp_dst
, mask
->udp_dst
);
623 if (uh
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
624 if (likely(src
!= uh
->source
)) {
625 set_tp_port(skb
, &uh
->source
, src
, &uh
->check
);
626 flow_key
->tp
.src
= src
;
628 if (likely(dst
!= uh
->dest
)) {
629 set_tp_port(skb
, &uh
->dest
, dst
, &uh
->check
);
630 flow_key
->tp
.dst
= dst
;
633 if (unlikely(!uh
->check
))
634 uh
->check
= CSUM_MANGLED_0
;
638 flow_key
->tp
.src
= src
;
639 flow_key
->tp
.dst
= dst
;
647 static int set_tcp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
648 const struct ovs_key_tcp
*key
,
649 const struct ovs_key_tcp
*mask
)
655 err
= skb_ensure_writable(skb
, skb_transport_offset(skb
) +
656 sizeof(struct tcphdr
));
661 src
= OVS_MASKED(th
->source
, key
->tcp_src
, mask
->tcp_src
);
662 if (likely(src
!= th
->source
)) {
663 set_tp_port(skb
, &th
->source
, src
, &th
->check
);
664 flow_key
->tp
.src
= src
;
666 dst
= OVS_MASKED(th
->dest
, key
->tcp_dst
, mask
->tcp_dst
);
667 if (likely(dst
!= th
->dest
)) {
668 set_tp_port(skb
, &th
->dest
, dst
, &th
->check
);
669 flow_key
->tp
.dst
= dst
;
676 static int set_sctp(struct sk_buff
*skb
, struct sw_flow_key
*flow_key
,
677 const struct ovs_key_sctp
*key
,
678 const struct ovs_key_sctp
*mask
)
680 unsigned int sctphoff
= skb_transport_offset(skb
);
682 __le32 old_correct_csum
, new_csum
, old_csum
;
685 err
= skb_ensure_writable(skb
, sctphoff
+ sizeof(struct sctphdr
));
690 old_csum
= sh
->checksum
;
691 old_correct_csum
= sctp_compute_cksum(skb
, sctphoff
);
693 sh
->source
= OVS_MASKED(sh
->source
, key
->sctp_src
, mask
->sctp_src
);
694 sh
->dest
= OVS_MASKED(sh
->dest
, key
->sctp_dst
, mask
->sctp_dst
);
696 new_csum
= sctp_compute_cksum(skb
, sctphoff
);
698 /* Carry any checksum errors through. */
699 sh
->checksum
= old_csum
^ old_correct_csum
^ new_csum
;
702 flow_key
->tp
.src
= sh
->source
;
703 flow_key
->tp
.dst
= sh
->dest
;
708 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS
)
710 struct ovs_frag_data
*data
= this_cpu_ptr(&ovs_frag_data_storage
);
711 struct vport
*vport
= data
->vport
;
713 if (skb_cow_head(skb
, data
->l2_len
) < 0) {
718 __skb_dst_copy(skb
, data
->dst
);
719 *OVS_GSO_CB(skb
) = data
->cb
;
720 ovs_skb_set_inner_protocol(skb
, data
->inner_protocol
);
721 skb
->vlan_tci
= data
->vlan_tci
;
722 skb
->vlan_proto
= data
->vlan_proto
;
724 /* Reconstruct the MAC header. */
725 skb_push(skb
, data
->l2_len
);
726 memcpy(skb
->data
, &data
->l2_data
, data
->l2_len
);
727 skb_postpush_rcsum(skb
, skb
->data
, data
->l2_len
);
728 skb_reset_mac_header(skb
);
730 ovs_vport_send(vport
, skb
, data
->mac_proto
);
735 ovs_dst_get_mtu(const struct dst_entry
*dst
)
737 return dst
->dev
->mtu
;
740 static struct dst_ops ovs_dst_ops
= {
742 .mtu
= ovs_dst_get_mtu
,
745 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
746 * ovs_vport_output(), which is called once per fragmented packet.
748 static void prepare_frag(struct vport
*vport
, struct sk_buff
*skb
,
751 unsigned int hlen
= skb_network_offset(skb
);
752 struct ovs_frag_data
*data
;
754 data
= this_cpu_ptr(&ovs_frag_data_storage
);
755 data
->dst
= (unsigned long) skb_dst(skb
);
757 data
->cb
= *OVS_GSO_CB(skb
);
758 data
->inner_protocol
= ovs_skb_get_inner_protocol(skb
);
759 data
->vlan_tci
= skb
->vlan_tci
;
760 data
->vlan_proto
= skb
->vlan_proto
;
761 data
->mac_proto
= mac_proto
;
763 memcpy(&data
->l2_data
, skb
->data
, hlen
);
765 memset(IPCB(skb
), 0, sizeof(struct inet_skb_parm
));
769 static void ovs_fragment(struct net
*net
, struct vport
*vport
,
770 struct sk_buff
*skb
, u16 mru
,
771 struct sw_flow_key
*key
)
773 if (skb_network_offset(skb
) > MAX_L2_LEN
) {
774 OVS_NLERR(1, "L2 header too long to fragment");
778 if (key
->eth
.type
== htons(ETH_P_IP
)) {
779 struct dst_entry ovs_dst
;
780 unsigned long orig_dst
;
782 prepare_frag(vport
, skb
, ovs_key_mac_proto(key
));
783 dst_init(&ovs_dst
, &ovs_dst_ops
, NULL
, 1,
784 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
785 ovs_dst
.dev
= vport
->dev
;
787 orig_dst
= (unsigned long) skb_dst(skb
);
788 skb_dst_set_noref(skb
, &ovs_dst
);
789 IPCB(skb
)->frag_max_size
= mru
;
791 ip_do_fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
792 refdst_drop(orig_dst
);
793 } else if (key
->eth
.type
== htons(ETH_P_IPV6
)) {
794 const struct nf_ipv6_ops
*v6ops
= nf_get_ipv6_ops();
795 unsigned long orig_dst
;
796 struct rt6_info ovs_rt
;
801 prepare_frag(vport
, skb
,
802 ovs_key_mac_proto(key
));
803 memset(&ovs_rt
, 0, sizeof(ovs_rt
));
804 dst_init(&ovs_rt
.dst
, &ovs_dst_ops
, NULL
, 1,
805 DST_OBSOLETE_NONE
, DST_NOCOUNT
);
806 ovs_rt
.dst
.dev
= vport
->dev
;
808 orig_dst
= (unsigned long) skb_dst(skb
);
809 skb_dst_set_noref(skb
, &ovs_rt
.dst
);
810 IP6CB(skb
)->frag_max_size
= mru
;
811 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
812 v6ops
->fragment(net
, skb
->sk
, skb
, ovs_vport_output
);
814 v6ops
->fragment(skb
->sk
, skb
, ovs_vport_output
);
816 refdst_drop(orig_dst
);
818 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
819 ovs_vport_name(vport
), ntohs(key
->eth
.type
), mru
,
829 static void do_output(struct datapath
*dp
, struct sk_buff
*skb
, int out_port
,
830 struct sw_flow_key
*key
)
832 struct vport
*vport
= ovs_vport_rcu(dp
, out_port
);
835 u16 mru
= OVS_CB(skb
)->mru
;
836 u32 cutlen
= OVS_CB(skb
)->cutlen
;
838 if (unlikely(cutlen
> 0)) {
839 if (skb
->len
- cutlen
> ovs_mac_header_len(key
))
840 pskb_trim(skb
, skb
->len
- cutlen
);
842 pskb_trim(skb
, ovs_mac_header_len(key
));
846 (skb
->len
<= mru
+ vport
->dev
->hard_header_len
))) {
847 ovs_vport_send(vport
, skb
, ovs_key_mac_proto(key
));
848 } else if (mru
<= vport
->dev
->mtu
) {
849 struct net
*net
= ovs_dp_get_net(dp
);
851 ovs_fragment(net
, vport
, skb
, mru
, key
);
853 OVS_NLERR(true, "Cannot fragment IP frames");
861 static int output_userspace(struct datapath
*dp
, struct sk_buff
*skb
,
862 struct sw_flow_key
*key
, const struct nlattr
*attr
,
863 const struct nlattr
*actions
, int actions_len
,
866 struct dp_upcall_info upcall
;
867 const struct nlattr
*a
;
870 memset(&upcall
, 0, sizeof(upcall
));
871 upcall
.cmd
= OVS_PACKET_CMD_ACTION
;
872 upcall
.mru
= OVS_CB(skb
)->mru
;
874 SKB_INIT_FILL_METADATA_DST(skb
);
875 for (a
= nla_data(attr
), rem
= nla_len(attr
); rem
> 0;
876 a
= nla_next(a
, &rem
)) {
877 switch (nla_type(a
)) {
878 case OVS_USERSPACE_ATTR_USERDATA
:
882 case OVS_USERSPACE_ATTR_PID
:
883 upcall
.portid
= nla_get_u32(a
);
886 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
: {
887 /* Get out tunnel info. */
890 vport
= ovs_vport_rcu(dp
, nla_get_u32(a
));
892 err
= dev_fill_metadata_dst(vport
->dev
, skb
);
894 upcall
.egress_tun_info
= skb_tunnel_info(skb
);
900 case OVS_USERSPACE_ATTR_ACTIONS
: {
901 /* Include actions. */
902 upcall
.actions
= actions
;
903 upcall
.actions_len
= actions_len
;
907 } /* End of switch. */
910 err
= ovs_dp_upcall(dp
, skb
, key
, &upcall
, cutlen
);
911 SKB_RESTORE_FILL_METADATA_DST(skb
);
915 /* When 'last' is true, sample() should always consume the 'skb'.
916 * Otherwise, sample() should keep 'skb' intact regardless what
917 * actions are executed within sample().
919 static int sample(struct datapath
*dp
, struct sk_buff
*skb
,
920 struct sw_flow_key
*key
, const struct nlattr
*attr
,
923 struct nlattr
*actions
;
924 struct nlattr
*sample_arg
;
925 struct sw_flow_key
*orig_key
= key
;
926 int rem
= nla_len(attr
);
928 const struct sample_arg
*arg
;
930 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
931 sample_arg
= nla_data(attr
);
932 arg
= nla_data(sample_arg
);
933 actions
= nla_next(sample_arg
, &rem
);
935 if ((arg
->probability
!= U32_MAX
) &&
936 (!arg
->probability
|| prandom_u32() > arg
->probability
)) {
942 /* Unless the last action, sample works on the clone of SKB. */
943 skb
= last
? skb
: skb_clone(skb
, GFP_ATOMIC
);
945 /* Out of memory, skip this sample action.
950 /* In case the sample actions won't change 'key',
951 * it can be used directly to execute sample actions.
952 * Otherwise, allocate a new key from the
953 * next recursion level of 'flow_keys'. If
954 * successful, execute the sample actions without
957 * Defer the sample actions if the recursion
958 * limit has been reached.
961 __this_cpu_inc(exec_actions_level
);
962 key
= clone_key(key
);
966 err
= do_execute_actions(dp
, skb
, key
, actions
, rem
);
967 } else if (!add_deferred_actions(skb
, orig_key
, actions
, rem
)) {
970 pr_warn("%s: deferred action limit reached, drop sample action\n",
976 __this_cpu_dec(exec_actions_level
);
981 static void execute_hash(struct sk_buff
*skb
, struct sw_flow_key
*key
,
982 const struct nlattr
*attr
)
984 struct ovs_action_hash
*hash_act
= nla_data(attr
);
987 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
988 hash
= skb_get_hash(skb
);
989 hash
= jhash_1word(hash
, hash_act
->hash_basis
);
993 key
->ovs_flow_hash
= hash
;
996 static int execute_set_action(struct sk_buff
*skb
,
997 struct sw_flow_key
*flow_key
,
998 const struct nlattr
*a
)
1000 /* Only tunnel set execution is supported without a mask. */
1001 if (nla_type(a
) == OVS_KEY_ATTR_TUNNEL_INFO
) {
1002 struct ovs_tunnel_info
*tun
= nla_data(a
);
1004 ovs_skb_dst_drop(skb
);
1005 ovs_dst_hold((struct dst_entry
*)tun
->tun_dst
);
1006 ovs_skb_dst_set(skb
, (struct dst_entry
*)tun
->tun_dst
);
1013 /* Mask is at the midpoint of the data. */
1014 #define get_mask(a, type) ((const type)nla_data(a) + 1)
1016 static int execute_masked_set_action(struct sk_buff
*skb
,
1017 struct sw_flow_key
*flow_key
,
1018 const struct nlattr
*a
)
1022 switch (nla_type(a
)) {
1023 case OVS_KEY_ATTR_PRIORITY
:
1024 OVS_SET_MASKED(skb
->priority
, nla_get_u32(a
),
1025 *get_mask(a
, u32
*));
1026 flow_key
->phy
.priority
= skb
->priority
;
1029 case OVS_KEY_ATTR_SKB_MARK
:
1030 OVS_SET_MASKED(skb
->mark
, nla_get_u32(a
), *get_mask(a
, u32
*));
1031 flow_key
->phy
.skb_mark
= skb
->mark
;
1034 case OVS_KEY_ATTR_TUNNEL_INFO
:
1035 /* Masked data not supported for tunnel. */
1039 case OVS_KEY_ATTR_ETHERNET
:
1040 err
= set_eth_addr(skb
, flow_key
, nla_data(a
),
1041 get_mask(a
, struct ovs_key_ethernet
*));
1044 case OVS_KEY_ATTR_IPV4
:
1045 err
= set_ipv4(skb
, flow_key
, nla_data(a
),
1046 get_mask(a
, struct ovs_key_ipv4
*));
1049 case OVS_KEY_ATTR_IPV6
:
1050 err
= set_ipv6(skb
, flow_key
, nla_data(a
),
1051 get_mask(a
, struct ovs_key_ipv6
*));
1054 case OVS_KEY_ATTR_TCP
:
1055 err
= set_tcp(skb
, flow_key
, nla_data(a
),
1056 get_mask(a
, struct ovs_key_tcp
*));
1059 case OVS_KEY_ATTR_UDP
:
1060 err
= set_udp(skb
, flow_key
, nla_data(a
),
1061 get_mask(a
, struct ovs_key_udp
*));
1064 case OVS_KEY_ATTR_SCTP
:
1065 err
= set_sctp(skb
, flow_key
, nla_data(a
),
1066 get_mask(a
, struct ovs_key_sctp
*));
1069 case OVS_KEY_ATTR_MPLS
:
1070 err
= set_mpls(skb
, flow_key
, nla_data(a
), get_mask(a
,
1074 case OVS_KEY_ATTR_CT_STATE
:
1075 case OVS_KEY_ATTR_CT_ZONE
:
1076 case OVS_KEY_ATTR_CT_MARK
:
1077 case OVS_KEY_ATTR_CT_LABELS
:
1078 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
:
1079 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
:
1087 static int execute_recirc(struct datapath
*dp
, struct sk_buff
*skb
,
1088 struct sw_flow_key
*key
,
1089 const struct nlattr
*a
, int rem
)
1091 struct sw_flow_key
*recirc_key
;
1092 struct deferred_action
*da
;
1094 if (!is_flow_key_valid(key
)) {
1097 err
= ovs_flow_key_update(skb
, key
);
1101 BUG_ON(!is_flow_key_valid(key
));
1103 if (!nla_is_last(a
, rem
)) {
1104 /* Recirc action is the not the last action
1105 * of the action list, need to clone the skb.
1107 skb
= skb_clone(skb
, GFP_ATOMIC
);
1109 /* Skip the recirc action when out of memory, but
1110 * continue on with the rest of the action list.
1116 /* If within the limit of 'OVS_DEFERRED_ACTION_THRESHOLD',
1117 * recirc immediately, otherwise, defer it for later execution.
1119 recirc_key
= clone_key(key
);
1121 recirc_key
->recirc_id
= nla_get_u32(a
);
1122 ovs_dp_process_packet(skb
, recirc_key
);
1124 da
= add_deferred_actions(skb
, key
, NULL
, 0);
1126 recirc_key
= &da
->pkt_key
;
1127 recirc_key
->recirc_id
= nla_get_u32(a
);
1129 /* Log an error in case action fifo is full. */
1131 if (net_ratelimit())
1132 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1139 /* Execute a list of actions against 'skb'. */
1140 static int do_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1141 struct sw_flow_key
*key
,
1142 const struct nlattr
*attr
, int len
)
1144 const struct nlattr
*a
;
1147 for (a
= attr
, rem
= len
; rem
> 0;
1148 a
= nla_next(a
, &rem
)) {
1151 switch (nla_type(a
)) {
1152 case OVS_ACTION_ATTR_OUTPUT
: {
1153 int port
= nla_get_u32(a
);
1154 struct sk_buff
*clone
;
1156 /* Every output action needs a separate clone
1157 * of 'skb', In case the output action is the
1158 * last action, cloning can be avoided.
1160 if (nla_is_last(a
, rem
)) {
1161 do_output(dp
, skb
, port
, key
);
1162 /* 'skb' has been used for output.
1167 clone
= skb_clone(skb
, GFP_ATOMIC
);
1169 do_output(dp
, clone
, port
, key
);
1170 OVS_CB(skb
)->cutlen
= 0;
1174 case OVS_ACTION_ATTR_TRUNC
: {
1175 struct ovs_action_trunc
*trunc
= nla_data(a
);
1177 if (skb
->len
> trunc
->max_len
)
1178 OVS_CB(skb
)->cutlen
= skb
->len
- trunc
->max_len
;
1182 case OVS_ACTION_ATTR_USERSPACE
:
1183 output_userspace(dp
, skb
, key
, a
, attr
,
1184 len
, OVS_CB(skb
)->cutlen
);
1185 OVS_CB(skb
)->cutlen
= 0;
1188 case OVS_ACTION_ATTR_HASH
:
1189 execute_hash(skb
, key
, a
);
1192 case OVS_ACTION_ATTR_PUSH_MPLS
:
1193 err
= push_mpls(skb
, key
, nla_data(a
));
1196 case OVS_ACTION_ATTR_POP_MPLS
:
1197 err
= pop_mpls(skb
, key
, nla_get_be16(a
));
1200 case OVS_ACTION_ATTR_PUSH_VLAN
:
1201 err
= push_vlan(skb
, key
, nla_data(a
));
1204 case OVS_ACTION_ATTR_POP_VLAN
:
1205 err
= pop_vlan(skb
, key
);
1208 case OVS_ACTION_ATTR_RECIRC
:
1209 err
= execute_recirc(dp
, skb
, key
, a
, rem
);
1210 if (nla_is_last(a
, rem
)) {
1211 /* If this is the last action, the skb has
1212 * been consumed or freed.
1213 * Return immediately.
1219 case OVS_ACTION_ATTR_SET
:
1220 err
= execute_set_action(skb
, key
, nla_data(a
));
1223 case OVS_ACTION_ATTR_SET_MASKED
:
1224 case OVS_ACTION_ATTR_SET_TO_MASKED
:
1225 err
= execute_masked_set_action(skb
, key
, nla_data(a
));
1228 case OVS_ACTION_ATTR_SAMPLE
: {
1229 bool last
= nla_is_last(a
, rem
);
1231 err
= sample(dp
, skb
, key
, a
, last
);
1238 case OVS_ACTION_ATTR_CT
:
1239 if (!is_flow_key_valid(key
)) {
1240 err
= ovs_flow_key_update(skb
, key
);
1245 err
= ovs_ct_execute(ovs_dp_get_net(dp
), skb
, key
,
1248 /* Hide stolen IP fragments from user space. */
1250 return err
== -EINPROGRESS
? 0 : err
;
1253 case OVS_ACTION_ATTR_PUSH_ETH
:
1254 err
= push_eth(skb
, key
, nla_data(a
));
1257 case OVS_ACTION_ATTR_POP_ETH
:
1258 err
= pop_eth(skb
, key
);
1262 if (unlikely(err
)) {
1272 static void process_deferred_actions(struct datapath
*dp
)
1274 struct action_fifo
*fifo
= this_cpu_ptr(action_fifos
);
1276 /* Do not touch the FIFO in case there is no deferred actions. */
1277 if (action_fifo_is_empty(fifo
))
1280 /* Finishing executing all deferred actions. */
1282 struct deferred_action
*da
= action_fifo_get(fifo
);
1283 struct sk_buff
*skb
= da
->skb
;
1284 struct sw_flow_key
*key
= &da
->pkt_key
;
1285 const struct nlattr
*actions
= da
->actions
;
1286 int actions_len
= da
->actions_len
;
1289 do_execute_actions(dp
, skb
, key
, actions
, actions_len
);
1291 ovs_dp_process_packet(skb
, key
);
1292 } while (!action_fifo_is_empty(fifo
));
1294 /* Reset FIFO for the next packet. */
1295 action_fifo_init(fifo
);
1298 /* Execute a list of actions against 'skb'. */
1299 int ovs_execute_actions(struct datapath
*dp
, struct sk_buff
*skb
,
1300 const struct sw_flow_actions
*acts
,
1301 struct sw_flow_key
*key
)
1305 level
= __this_cpu_inc_return(exec_actions_level
);
1306 if (unlikely(level
> OVS_RECURSION_LIMIT
)) {
1307 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1314 err
= do_execute_actions(dp
, skb
, key
,
1315 acts
->actions
, acts
->actions_len
);
1318 process_deferred_actions(dp
);
1321 __this_cpu_dec(exec_actions_level
);
1325 int action_fifos_init(void)
1327 action_fifos
= alloc_percpu(struct action_fifo
);
1331 flow_keys
= alloc_percpu(struct action_flow_keys
);
1333 free_percpu(action_fifos
);
1340 void action_fifos_exit(void)
1342 free_percpu(action_fifos
);
1343 free_percpu(flow_keys
);