2 * Copyright (c) 2007-2014 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/ip6_fib.h>
37 #include <net/checksum.h>
38 #include <net/dsfield.h>
40 #include <net/sctp/checksum.h>
44 #include "conntrack.h"
47 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
48 struct sw_flow_key *key,
49 const struct nlattr *attr, int len);
51 struct deferred_action {
53 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;
68 u8 l2_data[MAX_L2_LEN];
71 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
73 #define DEFERRED_ACTION_FIFO_SIZE 10
77 /* Deferred action fifo queue storage. */
78 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
81 static struct action_fifo __percpu *action_fifos;
82 static DEFINE_PER_CPU(int, exec_actions_level);
84 static void action_fifo_init(struct action_fifo *fifo)
90 static bool action_fifo_is_empty(const struct action_fifo *fifo)
92 return (fifo->head == fifo->tail);
95 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
97 if (action_fifo_is_empty(fifo))
100 return &fifo->fifo[fifo->tail++];
103 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
105 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
108 return &fifo->fifo[fifo->head++];
111 /* Return true if fifo is not full */
112 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
113 const struct sw_flow_key *key,
114 const struct nlattr *attr)
116 struct action_fifo *fifo;
117 struct deferred_action *da;
119 fifo = this_cpu_ptr(action_fifos);
120 da = action_fifo_put(fifo);
130 static void invalidate_flow_key(struct sw_flow_key *key)
132 key->eth.type = htons(0);
135 static bool is_flow_key_valid(const struct sw_flow_key *key)
137 return !!key->eth.type;
140 static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
143 if (skb->ip_summed == CHECKSUM_COMPLETE) {
144 __be16 diff[] = { ~(hdr->h_proto), ethertype };
146 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
149 hdr->h_proto = ethertype;
152 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
153 const struct ovs_action_push_mpls *mpls)
155 __be32 *new_mpls_lse;
157 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
158 if (skb->encapsulation)
161 if (skb_cow_head(skb, MPLS_HLEN) < 0)
164 skb_push(skb, MPLS_HLEN);
165 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
167 skb_reset_mac_header(skb);
169 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
170 *new_mpls_lse = mpls->mpls_lse;
172 skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
174 update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
175 if (!skb->inner_protocol)
176 skb_set_inner_protocol(skb, skb->protocol);
177 skb->protocol = mpls->mpls_ethertype;
179 invalidate_flow_key(key);
183 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
184 const __be16 ethertype)
189 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
193 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
195 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
198 __skb_pull(skb, MPLS_HLEN);
199 skb_reset_mac_header(skb);
201 /* skb_mpls_header() is used to locate the ethertype
202 * field correctly in the presence of VLAN tags.
204 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
205 update_ethertype(skb, hdr, ethertype);
206 if (eth_p_mpls(skb->protocol))
207 skb->protocol = ethertype;
209 invalidate_flow_key(key);
213 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
214 const __be32 *mpls_lse, const __be32 *mask)
220 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
224 stack = (__be32 *)skb_mpls_header(skb);
225 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
226 if (skb->ip_summed == CHECKSUM_COMPLETE) {
227 __be32 diff[] = { ~(*stack), lse };
229 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
233 flow_key->mpls.top_lse = lse;
237 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
241 err = skb_vlan_pop(skb);
242 if (skb_vlan_tag_present(skb))
243 invalidate_flow_key(key);
249 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
250 const struct ovs_action_push_vlan *vlan)
252 if (skb_vlan_tag_present(skb))
253 invalidate_flow_key(key);
255 key->eth.tci = vlan->vlan_tci;
256 return skb_vlan_push(skb, vlan->vlan_tpid,
257 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
260 /* 'src' is already properly masked. */
261 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
263 u16 *dst = (u16 *)dst_;
264 const u16 *src = (const u16 *)src_;
265 const u16 *mask = (const u16 *)mask_;
267 OVS_SET_MASKED(dst[0], src[0], mask[0]);
268 OVS_SET_MASKED(dst[1], src[1], mask[1]);
269 OVS_SET_MASKED(dst[2], src[2], mask[2]);
272 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
273 const struct ovs_key_ethernet *key,
274 const struct ovs_key_ethernet *mask)
278 err = skb_ensure_writable(skb, ETH_HLEN);
282 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
284 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
286 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
289 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
291 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
292 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
296 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
297 __be32 addr, __be32 new_addr)
299 int transport_len = skb->len - skb_transport_offset(skb);
301 if (nh->frag_off & htons(IP_OFFSET))
304 if (nh->protocol == IPPROTO_TCP) {
305 if (likely(transport_len >= sizeof(struct tcphdr)))
306 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
307 addr, new_addr, true);
308 } else if (nh->protocol == IPPROTO_UDP) {
309 if (likely(transport_len >= sizeof(struct udphdr))) {
310 struct udphdr *uh = udp_hdr(skb);
312 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
313 inet_proto_csum_replace4(&uh->check, skb,
314 addr, new_addr, true);
316 uh->check = CSUM_MANGLED_0;
322 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
323 __be32 *addr, __be32 new_addr)
325 update_ip_l4_checksum(skb, nh, *addr, new_addr);
326 csum_replace4(&nh->check, *addr, new_addr);
331 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
332 __be32 addr[4], const __be32 new_addr[4])
334 int transport_len = skb->len - skb_transport_offset(skb);
336 if (l4_proto == NEXTHDR_TCP) {
337 if (likely(transport_len >= sizeof(struct tcphdr)))
338 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
339 addr, new_addr, true);
340 } else if (l4_proto == NEXTHDR_UDP) {
341 if (likely(transport_len >= sizeof(struct udphdr))) {
342 struct udphdr *uh = udp_hdr(skb);
344 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
345 inet_proto_csum_replace16(&uh->check, skb,
346 addr, new_addr, true);
348 uh->check = CSUM_MANGLED_0;
351 } else if (l4_proto == NEXTHDR_ICMP) {
352 if (likely(transport_len >= sizeof(struct icmp6hdr)))
353 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
354 skb, addr, new_addr, true);
358 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
359 const __be32 mask[4], __be32 masked[4])
361 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
362 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
363 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
364 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
367 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
368 __be32 addr[4], const __be32 new_addr[4],
369 bool recalculate_csum)
371 if (recalculate_csum)
372 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
375 memcpy(addr, new_addr, sizeof(__be32[4]));
378 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
380 /* Bits 21-24 are always unmasked, so this retains their values. */
381 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
382 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
383 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
386 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
389 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
391 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
395 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
396 const struct ovs_key_ipv4 *key,
397 const struct ovs_key_ipv4 *mask)
403 err = skb_ensure_writable(skb, skb_network_offset(skb) +
404 sizeof(struct iphdr));
410 /* Setting an IP addresses is typically only a side effect of
411 * matching on them in the current userspace implementation, so it
412 * makes sense to check if the value actually changed.
414 if (mask->ipv4_src) {
415 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
417 if (unlikely(new_addr != nh->saddr)) {
418 set_ip_addr(skb, nh, &nh->saddr, new_addr);
419 flow_key->ipv4.addr.src = new_addr;
422 if (mask->ipv4_dst) {
423 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
425 if (unlikely(new_addr != nh->daddr)) {
426 set_ip_addr(skb, nh, &nh->daddr, new_addr);
427 flow_key->ipv4.addr.dst = new_addr;
430 if (mask->ipv4_tos) {
431 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
432 flow_key->ip.tos = nh->tos;
434 if (mask->ipv4_ttl) {
435 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
436 flow_key->ip.ttl = nh->ttl;
442 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
444 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
447 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
448 const struct ovs_key_ipv6 *key,
449 const struct ovs_key_ipv6 *mask)
454 err = skb_ensure_writable(skb, skb_network_offset(skb) +
455 sizeof(struct ipv6hdr));
461 /* Setting an IP addresses is typically only a side effect of
462 * matching on them in the current userspace implementation, so it
463 * makes sense to check if the value actually changed.
465 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
466 __be32 *saddr = (__be32 *)&nh->saddr;
469 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
471 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
472 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
474 memcpy(&flow_key->ipv6.addr.src, masked,
475 sizeof(flow_key->ipv6.addr.src));
478 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
479 unsigned int offset = 0;
480 int flags = IP6_FH_F_SKIP_RH;
481 bool recalc_csum = true;
482 __be32 *daddr = (__be32 *)&nh->daddr;
485 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
487 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
488 if (ipv6_ext_hdr(nh->nexthdr))
489 recalc_csum = (ipv6_find_hdr(skb, &offset,
494 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
496 memcpy(&flow_key->ipv6.addr.dst, masked,
497 sizeof(flow_key->ipv6.addr.dst));
500 if (mask->ipv6_tclass) {
501 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
502 flow_key->ip.tos = ipv6_get_dsfield(nh);
504 if (mask->ipv6_label) {
505 set_ipv6_fl(nh, ntohl(key->ipv6_label),
506 ntohl(mask->ipv6_label));
507 flow_key->ipv6.label =
508 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
510 if (mask->ipv6_hlimit) {
511 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
513 flow_key->ip.ttl = nh->hop_limit;
518 /* Must follow skb_ensure_writable() since that can move the skb data. */
519 static void set_tp_port(struct sk_buff *skb, __be16 *port,
520 __be16 new_port, __sum16 *check)
522 inet_proto_csum_replace2(check, skb, *port, new_port, false);
526 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
527 const struct ovs_key_udp *key,
528 const struct ovs_key_udp *mask)
534 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
535 sizeof(struct udphdr));
540 /* Either of the masks is non-zero, so do not bother checking them. */
541 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
542 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
544 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
545 if (likely(src != uh->source)) {
546 set_tp_port(skb, &uh->source, src, &uh->check);
547 flow_key->tp.src = src;
549 if (likely(dst != uh->dest)) {
550 set_tp_port(skb, &uh->dest, dst, &uh->check);
551 flow_key->tp.dst = dst;
554 if (unlikely(!uh->check))
555 uh->check = CSUM_MANGLED_0;
559 flow_key->tp.src = src;
560 flow_key->tp.dst = dst;
568 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
569 const struct ovs_key_tcp *key,
570 const struct ovs_key_tcp *mask)
576 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
577 sizeof(struct tcphdr));
582 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
583 if (likely(src != th->source)) {
584 set_tp_port(skb, &th->source, src, &th->check);
585 flow_key->tp.src = src;
587 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
588 if (likely(dst != th->dest)) {
589 set_tp_port(skb, &th->dest, dst, &th->check);
590 flow_key->tp.dst = dst;
597 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
598 const struct ovs_key_sctp *key,
599 const struct ovs_key_sctp *mask)
601 unsigned int sctphoff = skb_transport_offset(skb);
603 __le32 old_correct_csum, new_csum, old_csum;
606 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
611 old_csum = sh->checksum;
612 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
614 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
615 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
617 new_csum = sctp_compute_cksum(skb, sctphoff);
619 /* Carry any checksum errors through. */
620 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
623 flow_key->tp.src = sh->source;
624 flow_key->tp.dst = sh->dest;
629 static int ovs_vport_output(struct net *net, struct sock *sk, struct sk_buff *skb)
631 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
632 struct vport *vport = data->vport;
634 if (skb_cow_head(skb, data->l2_len) < 0) {
639 __skb_dst_copy(skb, data->dst);
640 *OVS_CB(skb) = data->cb;
641 skb->inner_protocol = data->inner_protocol;
642 skb->vlan_tci = data->vlan_tci;
643 skb->vlan_proto = data->vlan_proto;
645 /* Reconstruct the MAC header. */
646 skb_push(skb, data->l2_len);
647 memcpy(skb->data, &data->l2_data, data->l2_len);
648 skb_postpush_rcsum(skb, skb->data, data->l2_len);
649 skb_reset_mac_header(skb);
651 ovs_vport_send(vport, skb);
656 ovs_dst_get_mtu(const struct dst_entry *dst)
658 return dst->dev->mtu;
661 static struct dst_ops ovs_dst_ops = {
663 .mtu = ovs_dst_get_mtu,
666 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
667 * ovs_vport_output(), which is called once per fragmented packet.
669 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
671 unsigned int hlen = skb_network_offset(skb);
672 struct ovs_frag_data *data;
674 data = this_cpu_ptr(&ovs_frag_data_storage);
675 data->dst = skb->_skb_refdst;
677 data->cb = *OVS_CB(skb);
678 data->inner_protocol = skb->inner_protocol;
679 data->vlan_tci = skb->vlan_tci;
680 data->vlan_proto = skb->vlan_proto;
682 memcpy(&data->l2_data, skb->data, hlen);
684 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
688 static void ovs_fragment(struct net *net, struct vport *vport,
689 struct sk_buff *skb, u16 mru, __be16 ethertype)
691 if (skb_network_offset(skb) > MAX_L2_LEN) {
692 OVS_NLERR(1, "L2 header too long to fragment");
696 if (ethertype == htons(ETH_P_IP)) {
697 struct rtable ovs_rt = { 0 };
698 unsigned long orig_dst;
700 prepare_frag(vport, skb);
701 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
702 DST_OBSOLETE_NONE, DST_NOCOUNT);
703 ovs_rt.dst.dev = vport->dev;
705 orig_dst = skb->_skb_refdst;
706 skb_dst_set_noref(skb, &ovs_rt.dst);
707 IPCB(skb)->frag_max_size = mru;
709 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
710 refdst_drop(orig_dst);
711 } else if (ethertype == htons(ETH_P_IPV6)) {
712 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
713 unsigned long orig_dst;
714 struct rt6_info ovs_rt;
720 prepare_frag(vport, skb);
721 memset(&ovs_rt, 0, sizeof(ovs_rt));
722 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
723 DST_OBSOLETE_NONE, DST_NOCOUNT);
724 ovs_rt.dst.dev = vport->dev;
726 orig_dst = skb->_skb_refdst;
727 skb_dst_set_noref(skb, &ovs_rt.dst);
728 IP6CB(skb)->frag_max_size = mru;
730 v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
731 refdst_drop(orig_dst);
733 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
734 ovs_vport_name(vport), ntohs(ethertype), mru,
744 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
745 struct sw_flow_key *key)
747 struct vport *vport = ovs_vport_rcu(dp, out_port);
750 u16 mru = OVS_CB(skb)->mru;
752 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
753 ovs_vport_send(vport, skb);
754 } else if (mru <= vport->dev->mtu) {
755 struct net *net = read_pnet(&dp->net);
756 __be16 ethertype = key->eth.type;
758 if (!is_flow_key_valid(key)) {
759 if (eth_p_mpls(skb->protocol))
760 ethertype = skb->inner_protocol;
762 ethertype = vlan_get_protocol(skb);
765 ovs_fragment(net, vport, skb, mru, ethertype);
774 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
775 struct sw_flow_key *key, const struct nlattr *attr,
776 const struct nlattr *actions, int actions_len)
778 struct dp_upcall_info upcall;
779 const struct nlattr *a;
782 memset(&upcall, 0, sizeof(upcall));
783 upcall.cmd = OVS_PACKET_CMD_ACTION;
784 upcall.mru = OVS_CB(skb)->mru;
786 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
787 a = nla_next(a, &rem)) {
788 switch (nla_type(a)) {
789 case OVS_USERSPACE_ATTR_USERDATA:
793 case OVS_USERSPACE_ATTR_PID:
794 upcall.portid = nla_get_u32(a);
797 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
798 /* Get out tunnel info. */
801 vport = ovs_vport_rcu(dp, nla_get_u32(a));
805 err = dev_fill_metadata_dst(vport->dev, skb);
807 upcall.egress_tun_info = skb_tunnel_info(skb);
813 case OVS_USERSPACE_ATTR_ACTIONS: {
814 /* Include actions. */
815 upcall.actions = actions;
816 upcall.actions_len = actions_len;
820 } /* End of switch. */
823 return ovs_dp_upcall(dp, skb, key, &upcall);
826 static int sample(struct datapath *dp, struct sk_buff *skb,
827 struct sw_flow_key *key, const struct nlattr *attr,
828 const struct nlattr *actions, int actions_len)
830 const struct nlattr *acts_list = NULL;
831 const struct nlattr *a;
834 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
835 a = nla_next(a, &rem)) {
838 switch (nla_type(a)) {
839 case OVS_SAMPLE_ATTR_PROBABILITY:
840 probability = nla_get_u32(a);
841 if (!probability || prandom_u32() > probability)
845 case OVS_SAMPLE_ATTR_ACTIONS:
851 rem = nla_len(acts_list);
852 a = nla_data(acts_list);
854 /* Actions list is empty, do nothing */
858 /* The only known usage of sample action is having a single user-space
859 * action. Treat this usage as a special case.
860 * The output_userspace() should clone the skb to be sent to the
861 * user space. This skb will be consumed by its caller.
863 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
864 nla_is_last(a, rem)))
865 return output_userspace(dp, skb, key, a, actions, actions_len);
867 skb = skb_clone(skb, GFP_ATOMIC);
869 /* Skip the sample action when out of memory. */
872 if (!add_deferred_actions(skb, key, a)) {
874 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
882 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
883 const struct nlattr *attr)
885 struct ovs_action_hash *hash_act = nla_data(attr);
888 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
889 hash = skb_get_hash(skb);
890 hash = jhash_1word(hash, hash_act->hash_basis);
894 key->ovs_flow_hash = hash;
897 static int execute_set_action(struct sk_buff *skb,
898 struct sw_flow_key *flow_key,
899 const struct nlattr *a)
901 /* Only tunnel set execution is supported without a mask. */
902 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
903 struct ovs_tunnel_info *tun = nla_data(a);
906 dst_hold((struct dst_entry *)tun->tun_dst);
907 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
914 /* Mask is at the midpoint of the data. */
915 #define get_mask(a, type) ((const type)nla_data(a) + 1)
917 static int execute_masked_set_action(struct sk_buff *skb,
918 struct sw_flow_key *flow_key,
919 const struct nlattr *a)
923 switch (nla_type(a)) {
924 case OVS_KEY_ATTR_PRIORITY:
925 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
926 *get_mask(a, u32 *));
927 flow_key->phy.priority = skb->priority;
930 case OVS_KEY_ATTR_SKB_MARK:
931 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
932 flow_key->phy.skb_mark = skb->mark;
935 case OVS_KEY_ATTR_TUNNEL_INFO:
936 /* Masked data not supported for tunnel. */
940 case OVS_KEY_ATTR_ETHERNET:
941 err = set_eth_addr(skb, flow_key, nla_data(a),
942 get_mask(a, struct ovs_key_ethernet *));
945 case OVS_KEY_ATTR_IPV4:
946 err = set_ipv4(skb, flow_key, nla_data(a),
947 get_mask(a, struct ovs_key_ipv4 *));
950 case OVS_KEY_ATTR_IPV6:
951 err = set_ipv6(skb, flow_key, nla_data(a),
952 get_mask(a, struct ovs_key_ipv6 *));
955 case OVS_KEY_ATTR_TCP:
956 err = set_tcp(skb, flow_key, nla_data(a),
957 get_mask(a, struct ovs_key_tcp *));
960 case OVS_KEY_ATTR_UDP:
961 err = set_udp(skb, flow_key, nla_data(a),
962 get_mask(a, struct ovs_key_udp *));
965 case OVS_KEY_ATTR_SCTP:
966 err = set_sctp(skb, flow_key, nla_data(a),
967 get_mask(a, struct ovs_key_sctp *));
970 case OVS_KEY_ATTR_MPLS:
971 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
975 case OVS_KEY_ATTR_CT_STATE:
976 case OVS_KEY_ATTR_CT_ZONE:
977 case OVS_KEY_ATTR_CT_MARK:
978 case OVS_KEY_ATTR_CT_LABELS:
986 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
987 struct sw_flow_key *key,
988 const struct nlattr *a, int rem)
990 struct deferred_action *da;
992 if (!is_flow_key_valid(key)) {
995 err = ovs_flow_key_update(skb, key);
999 BUG_ON(!is_flow_key_valid(key));
1001 if (!nla_is_last(a, rem)) {
1002 /* Recirc action is the not the last action
1003 * of the action list, need to clone the skb.
1005 skb = skb_clone(skb, GFP_ATOMIC);
1007 /* Skip the recirc action when out of memory, but
1008 * continue on with the rest of the action list.
1014 da = add_deferred_actions(skb, key, NULL);
1016 da->pkt_key.recirc_id = nla_get_u32(a);
1020 if (net_ratelimit())
1021 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1028 /* Execute a list of actions against 'skb'. */
1029 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1030 struct sw_flow_key *key,
1031 const struct nlattr *attr, int len)
1033 /* Every output action needs a separate clone of 'skb', but the common
1034 * case is just a single output action, so that doing a clone and
1035 * then freeing the original skbuff is wasteful. So the following code
1036 * is slightly obscure just to avoid that.
1039 const struct nlattr *a;
1042 for (a = attr, rem = len; rem > 0;
1043 a = nla_next(a, &rem)) {
1046 if (unlikely(prev_port != -1)) {
1047 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1050 do_output(dp, out_skb, prev_port, key);
1055 switch (nla_type(a)) {
1056 case OVS_ACTION_ATTR_OUTPUT:
1057 prev_port = nla_get_u32(a);
1060 case OVS_ACTION_ATTR_USERSPACE:
1061 output_userspace(dp, skb, key, a, attr, len);
1064 case OVS_ACTION_ATTR_HASH:
1065 execute_hash(skb, key, a);
1068 case OVS_ACTION_ATTR_PUSH_MPLS:
1069 err = push_mpls(skb, key, nla_data(a));
1072 case OVS_ACTION_ATTR_POP_MPLS:
1073 err = pop_mpls(skb, key, nla_get_be16(a));
1076 case OVS_ACTION_ATTR_PUSH_VLAN:
1077 err = push_vlan(skb, key, nla_data(a));
1080 case OVS_ACTION_ATTR_POP_VLAN:
1081 err = pop_vlan(skb, key);
1084 case OVS_ACTION_ATTR_RECIRC:
1085 err = execute_recirc(dp, skb, key, a, rem);
1086 if (nla_is_last(a, rem)) {
1087 /* If this is the last action, the skb has
1088 * been consumed or freed.
1089 * Return immediately.
1095 case OVS_ACTION_ATTR_SET:
1096 err = execute_set_action(skb, key, nla_data(a));
1099 case OVS_ACTION_ATTR_SET_MASKED:
1100 case OVS_ACTION_ATTR_SET_TO_MASKED:
1101 err = execute_masked_set_action(skb, key, nla_data(a));
1104 case OVS_ACTION_ATTR_SAMPLE:
1105 err = sample(dp, skb, key, a, attr, len);
1108 case OVS_ACTION_ATTR_CT:
1109 if (!is_flow_key_valid(key)) {
1110 err = ovs_flow_key_update(skb, key);
1115 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1118 /* Hide stolen IP fragments from user space. */
1120 return err == -EINPROGRESS ? 0 : err;
1124 if (unlikely(err)) {
1130 if (prev_port != -1)
1131 do_output(dp, skb, prev_port, key);
1138 static void process_deferred_actions(struct datapath *dp)
1140 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1142 /* Do not touch the FIFO in case there is no deferred actions. */
1143 if (action_fifo_is_empty(fifo))
1146 /* Finishing executing all deferred actions. */
1148 struct deferred_action *da = action_fifo_get(fifo);
1149 struct sk_buff *skb = da->skb;
1150 struct sw_flow_key *key = &da->pkt_key;
1151 const struct nlattr *actions = da->actions;
1154 do_execute_actions(dp, skb, key, actions,
1157 ovs_dp_process_packet(skb, key);
1158 } while (!action_fifo_is_empty(fifo));
1160 /* Reset FIFO for the next packet. */
1161 action_fifo_init(fifo);
1164 /* Execute a list of actions against 'skb'. */
1165 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1166 const struct sw_flow_actions *acts,
1167 struct sw_flow_key *key)
1169 int level = this_cpu_read(exec_actions_level);
1172 this_cpu_inc(exec_actions_level);
1173 err = do_execute_actions(dp, skb, key,
1174 acts->actions, acts->actions_len);
1177 process_deferred_actions(dp);
1179 this_cpu_dec(exec_actions_level);
1183 int action_fifos_init(void)
1185 action_fifos = alloc_percpu(struct action_fifo);
1192 void action_fifos_exit(void)
1194 free_percpu(action_fifos);