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 int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
141 const struct ovs_action_push_mpls *mpls)
143 __be32 *new_mpls_lse;
146 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
147 if (skb->encapsulation)
150 if (skb_cow_head(skb, MPLS_HLEN) < 0)
153 skb_push(skb, MPLS_HLEN);
154 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
156 skb_reset_mac_header(skb);
158 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
159 *new_mpls_lse = mpls->mpls_lse;
161 if (skb->ip_summed == CHECKSUM_COMPLETE)
162 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
166 hdr->h_proto = mpls->mpls_ethertype;
168 if (!skb->inner_protocol)
169 skb_set_inner_protocol(skb, skb->protocol);
170 skb->protocol = mpls->mpls_ethertype;
172 invalidate_flow_key(key);
176 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
177 const __be16 ethertype)
182 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
186 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
188 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
191 __skb_pull(skb, MPLS_HLEN);
192 skb_reset_mac_header(skb);
194 /* skb_mpls_header() is used to locate the ethertype
195 * field correctly in the presence of VLAN tags.
197 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
198 hdr->h_proto = ethertype;
199 if (eth_p_mpls(skb->protocol))
200 skb->protocol = ethertype;
202 invalidate_flow_key(key);
206 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
207 const __be32 *mpls_lse, const __be32 *mask)
213 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
217 stack = (__be32 *)skb_mpls_header(skb);
218 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
219 if (skb->ip_summed == CHECKSUM_COMPLETE) {
220 __be32 diff[] = { ~(*stack), lse };
222 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
227 flow_key->mpls.top_lse = lse;
231 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
235 err = skb_vlan_pop(skb);
236 if (skb_vlan_tag_present(skb))
237 invalidate_flow_key(key);
243 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
244 const struct ovs_action_push_vlan *vlan)
246 if (skb_vlan_tag_present(skb))
247 invalidate_flow_key(key);
249 key->eth.tci = vlan->vlan_tci;
250 return skb_vlan_push(skb, vlan->vlan_tpid,
251 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
254 /* 'src' is already properly masked. */
255 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
257 u16 *dst = (u16 *)dst_;
258 const u16 *src = (const u16 *)src_;
259 const u16 *mask = (const u16 *)mask_;
261 OVS_SET_MASKED(dst[0], src[0], mask[0]);
262 OVS_SET_MASKED(dst[1], src[1], mask[1]);
263 OVS_SET_MASKED(dst[2], src[2], mask[2]);
266 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
267 const struct ovs_key_ethernet *key,
268 const struct ovs_key_ethernet *mask)
272 err = skb_ensure_writable(skb, ETH_HLEN);
276 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
278 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
280 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
283 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
285 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
286 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
290 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
291 __be32 addr, __be32 new_addr)
293 int transport_len = skb->len - skb_transport_offset(skb);
295 if (nh->frag_off & htons(IP_OFFSET))
298 if (nh->protocol == IPPROTO_TCP) {
299 if (likely(transport_len >= sizeof(struct tcphdr)))
300 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
301 addr, new_addr, true);
302 } else if (nh->protocol == IPPROTO_UDP) {
303 if (likely(transport_len >= sizeof(struct udphdr))) {
304 struct udphdr *uh = udp_hdr(skb);
306 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
307 inet_proto_csum_replace4(&uh->check, skb,
308 addr, new_addr, true);
310 uh->check = CSUM_MANGLED_0;
316 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
317 __be32 *addr, __be32 new_addr)
319 update_ip_l4_checksum(skb, nh, *addr, new_addr);
320 csum_replace4(&nh->check, *addr, new_addr);
325 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
326 __be32 addr[4], const __be32 new_addr[4])
328 int transport_len = skb->len - skb_transport_offset(skb);
330 if (l4_proto == NEXTHDR_TCP) {
331 if (likely(transport_len >= sizeof(struct tcphdr)))
332 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
333 addr, new_addr, true);
334 } else if (l4_proto == NEXTHDR_UDP) {
335 if (likely(transport_len >= sizeof(struct udphdr))) {
336 struct udphdr *uh = udp_hdr(skb);
338 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
339 inet_proto_csum_replace16(&uh->check, skb,
340 addr, new_addr, true);
342 uh->check = CSUM_MANGLED_0;
345 } else if (l4_proto == NEXTHDR_ICMP) {
346 if (likely(transport_len >= sizeof(struct icmp6hdr)))
347 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
348 skb, addr, new_addr, true);
352 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
353 const __be32 mask[4], __be32 masked[4])
355 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
356 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
357 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
358 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
361 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
362 __be32 addr[4], const __be32 new_addr[4],
363 bool recalculate_csum)
365 if (recalculate_csum)
366 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
369 memcpy(addr, new_addr, sizeof(__be32[4]));
372 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
374 /* Bits 21-24 are always unmasked, so this retains their values. */
375 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
376 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
377 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
380 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
383 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
385 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
389 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
390 const struct ovs_key_ipv4 *key,
391 const struct ovs_key_ipv4 *mask)
397 err = skb_ensure_writable(skb, skb_network_offset(skb) +
398 sizeof(struct iphdr));
404 /* Setting an IP addresses is typically only a side effect of
405 * matching on them in the current userspace implementation, so it
406 * makes sense to check if the value actually changed.
408 if (mask->ipv4_src) {
409 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
411 if (unlikely(new_addr != nh->saddr)) {
412 set_ip_addr(skb, nh, &nh->saddr, new_addr);
413 flow_key->ipv4.addr.src = new_addr;
416 if (mask->ipv4_dst) {
417 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
419 if (unlikely(new_addr != nh->daddr)) {
420 set_ip_addr(skb, nh, &nh->daddr, new_addr);
421 flow_key->ipv4.addr.dst = new_addr;
424 if (mask->ipv4_tos) {
425 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
426 flow_key->ip.tos = nh->tos;
428 if (mask->ipv4_ttl) {
429 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
430 flow_key->ip.ttl = nh->ttl;
436 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
438 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
441 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
442 const struct ovs_key_ipv6 *key,
443 const struct ovs_key_ipv6 *mask)
448 err = skb_ensure_writable(skb, skb_network_offset(skb) +
449 sizeof(struct ipv6hdr));
455 /* Setting an IP addresses is typically only a side effect of
456 * matching on them in the current userspace implementation, so it
457 * makes sense to check if the value actually changed.
459 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
460 __be32 *saddr = (__be32 *)&nh->saddr;
463 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
465 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
466 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
468 memcpy(&flow_key->ipv6.addr.src, masked,
469 sizeof(flow_key->ipv6.addr.src));
472 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
473 unsigned int offset = 0;
474 int flags = IP6_FH_F_SKIP_RH;
475 bool recalc_csum = true;
476 __be32 *daddr = (__be32 *)&nh->daddr;
479 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
481 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
482 if (ipv6_ext_hdr(nh->nexthdr))
483 recalc_csum = (ipv6_find_hdr(skb, &offset,
488 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
490 memcpy(&flow_key->ipv6.addr.dst, masked,
491 sizeof(flow_key->ipv6.addr.dst));
494 if (mask->ipv6_tclass) {
495 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
496 flow_key->ip.tos = ipv6_get_dsfield(nh);
498 if (mask->ipv6_label) {
499 set_ipv6_fl(nh, ntohl(key->ipv6_label),
500 ntohl(mask->ipv6_label));
501 flow_key->ipv6.label =
502 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
504 if (mask->ipv6_hlimit) {
505 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
507 flow_key->ip.ttl = nh->hop_limit;
512 /* Must follow skb_ensure_writable() since that can move the skb data. */
513 static void set_tp_port(struct sk_buff *skb, __be16 *port,
514 __be16 new_port, __sum16 *check)
516 inet_proto_csum_replace2(check, skb, *port, new_port, false);
520 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
521 const struct ovs_key_udp *key,
522 const struct ovs_key_udp *mask)
528 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
529 sizeof(struct udphdr));
534 /* Either of the masks is non-zero, so do not bother checking them. */
535 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
536 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
538 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
539 if (likely(src != uh->source)) {
540 set_tp_port(skb, &uh->source, src, &uh->check);
541 flow_key->tp.src = src;
543 if (likely(dst != uh->dest)) {
544 set_tp_port(skb, &uh->dest, dst, &uh->check);
545 flow_key->tp.dst = dst;
548 if (unlikely(!uh->check))
549 uh->check = CSUM_MANGLED_0;
553 flow_key->tp.src = src;
554 flow_key->tp.dst = dst;
562 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
563 const struct ovs_key_tcp *key,
564 const struct ovs_key_tcp *mask)
570 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
571 sizeof(struct tcphdr));
576 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
577 if (likely(src != th->source)) {
578 set_tp_port(skb, &th->source, src, &th->check);
579 flow_key->tp.src = src;
581 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
582 if (likely(dst != th->dest)) {
583 set_tp_port(skb, &th->dest, dst, &th->check);
584 flow_key->tp.dst = dst;
591 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
592 const struct ovs_key_sctp *key,
593 const struct ovs_key_sctp *mask)
595 unsigned int sctphoff = skb_transport_offset(skb);
597 __le32 old_correct_csum, new_csum, old_csum;
600 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
605 old_csum = sh->checksum;
606 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
608 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
609 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
611 new_csum = sctp_compute_cksum(skb, sctphoff);
613 /* Carry any checksum errors through. */
614 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
617 flow_key->tp.src = sh->source;
618 flow_key->tp.dst = sh->dest;
623 static int ovs_vport_output(struct sock *sock, struct sk_buff *skb)
625 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
626 struct vport *vport = data->vport;
628 if (skb_cow_head(skb, data->l2_len) < 0) {
633 __skb_dst_copy(skb, data->dst);
634 *OVS_CB(skb) = data->cb;
635 skb->inner_protocol = data->inner_protocol;
636 skb->vlan_tci = data->vlan_tci;
637 skb->vlan_proto = data->vlan_proto;
639 /* Reconstruct the MAC header. */
640 skb_push(skb, data->l2_len);
641 memcpy(skb->data, &data->l2_data, data->l2_len);
642 ovs_skb_postpush_rcsum(skb, skb->data, data->l2_len);
643 skb_reset_mac_header(skb);
645 ovs_vport_send(vport, skb);
650 ovs_dst_get_mtu(const struct dst_entry *dst)
652 return dst->dev->mtu;
655 static struct dst_ops ovs_dst_ops = {
657 .mtu = ovs_dst_get_mtu,
660 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
661 * ovs_vport_output(), which is called once per fragmented packet.
663 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
665 unsigned int hlen = skb_network_offset(skb);
666 struct ovs_frag_data *data;
668 data = this_cpu_ptr(&ovs_frag_data_storage);
669 data->dst = skb->_skb_refdst;
671 data->cb = *OVS_CB(skb);
672 data->inner_protocol = skb->inner_protocol;
673 data->vlan_tci = skb->vlan_tci;
674 data->vlan_proto = skb->vlan_proto;
676 memcpy(&data->l2_data, skb->data, hlen);
678 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
682 static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
685 if (skb_network_offset(skb) > MAX_L2_LEN) {
686 OVS_NLERR(1, "L2 header too long to fragment");
690 if (ethertype == htons(ETH_P_IP)) {
691 struct dst_entry ovs_dst;
692 unsigned long orig_dst;
694 prepare_frag(vport, skb);
695 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
696 DST_OBSOLETE_NONE, DST_NOCOUNT);
697 ovs_dst.dev = vport->dev;
699 orig_dst = skb->_skb_refdst;
700 skb_dst_set_noref(skb, &ovs_dst);
701 IPCB(skb)->frag_max_size = mru;
703 ip_do_fragment(skb->sk, skb, ovs_vport_output);
704 refdst_drop(orig_dst);
705 } else if (ethertype == htons(ETH_P_IPV6)) {
706 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
707 unsigned long orig_dst;
708 struct rt6_info ovs_rt;
715 prepare_frag(vport, skb);
716 memset(&ovs_rt, 0, sizeof(ovs_rt));
717 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
718 DST_OBSOLETE_NONE, DST_NOCOUNT);
719 ovs_rt.dst.dev = vport->dev;
721 orig_dst = skb->_skb_refdst;
722 skb_dst_set_noref(skb, &ovs_rt.dst);
723 IP6CB(skb)->frag_max_size = mru;
725 v6ops->fragment(skb->sk, skb, ovs_vport_output);
726 refdst_drop(orig_dst);
728 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
729 ovs_vport_name(vport), ntohs(ethertype), mru,
735 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
736 struct sw_flow_key *key)
738 struct vport *vport = ovs_vport_rcu(dp, out_port);
741 u16 mru = OVS_CB(skb)->mru;
743 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
744 ovs_vport_send(vport, skb);
745 } else if (mru <= vport->dev->mtu) {
746 __be16 ethertype = key->eth.type;
748 if (!is_flow_key_valid(key)) {
749 if (eth_p_mpls(skb->protocol))
750 ethertype = skb->inner_protocol;
752 ethertype = vlan_get_protocol(skb);
755 ovs_fragment(vport, skb, mru, ethertype);
764 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
765 struct sw_flow_key *key, const struct nlattr *attr,
766 const struct nlattr *actions, int actions_len)
768 struct ip_tunnel_info info;
769 struct dp_upcall_info upcall;
770 const struct nlattr *a;
773 memset(&upcall, 0, sizeof(upcall));
774 upcall.cmd = OVS_PACKET_CMD_ACTION;
775 upcall.mru = OVS_CB(skb)->mru;
777 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
778 a = nla_next(a, &rem)) {
779 switch (nla_type(a)) {
780 case OVS_USERSPACE_ATTR_USERDATA:
784 case OVS_USERSPACE_ATTR_PID:
785 upcall.portid = nla_get_u32(a);
788 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
789 /* Get out tunnel info. */
792 vport = ovs_vport_rcu(dp, nla_get_u32(a));
796 upcall.egress_tun_info = &info;
797 err = ovs_vport_get_egress_tun_info(vport, skb,
800 upcall.egress_tun_info = NULL;
806 case OVS_USERSPACE_ATTR_ACTIONS: {
807 /* Include actions. */
808 upcall.actions = actions;
809 upcall.actions_len = actions_len;
813 } /* End of switch. */
816 return ovs_dp_upcall(dp, skb, key, &upcall);
819 static int sample(struct datapath *dp, struct sk_buff *skb,
820 struct sw_flow_key *key, const struct nlattr *attr,
821 const struct nlattr *actions, int actions_len)
823 const struct nlattr *acts_list = NULL;
824 const struct nlattr *a;
827 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
828 a = nla_next(a, &rem)) {
831 switch (nla_type(a)) {
832 case OVS_SAMPLE_ATTR_PROBABILITY:
833 probability = nla_get_u32(a);
834 if (!probability || prandom_u32() > probability)
838 case OVS_SAMPLE_ATTR_ACTIONS:
844 rem = nla_len(acts_list);
845 a = nla_data(acts_list);
847 /* Actions list is empty, do nothing */
851 /* The only known usage of sample action is having a single user-space
852 * action. Treat this usage as a special case.
853 * The output_userspace() should clone the skb to be sent to the
854 * user space. This skb will be consumed by its caller.
856 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
857 nla_is_last(a, rem)))
858 return output_userspace(dp, skb, key, a, actions, actions_len);
860 skb = skb_clone(skb, GFP_ATOMIC);
862 /* Skip the sample action when out of memory. */
865 if (!add_deferred_actions(skb, key, a)) {
867 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
875 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
876 const struct nlattr *attr)
878 struct ovs_action_hash *hash_act = nla_data(attr);
881 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
882 hash = skb_get_hash(skb);
883 hash = jhash_1word(hash, hash_act->hash_basis);
887 key->ovs_flow_hash = hash;
890 static int execute_set_action(struct sk_buff *skb,
891 struct sw_flow_key *flow_key,
892 const struct nlattr *a)
894 /* Only tunnel set execution is supported without a mask. */
895 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
896 struct ovs_tunnel_info *tun = nla_data(a);
899 dst_hold((struct dst_entry *)tun->tun_dst);
900 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
907 /* Mask is at the midpoint of the data. */
908 #define get_mask(a, type) ((const type)nla_data(a) + 1)
910 static int execute_masked_set_action(struct sk_buff *skb,
911 struct sw_flow_key *flow_key,
912 const struct nlattr *a)
916 switch (nla_type(a)) {
917 case OVS_KEY_ATTR_PRIORITY:
918 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
919 *get_mask(a, u32 *));
920 flow_key->phy.priority = skb->priority;
923 case OVS_KEY_ATTR_SKB_MARK:
924 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
925 flow_key->phy.skb_mark = skb->mark;
928 case OVS_KEY_ATTR_TUNNEL_INFO:
929 /* Masked data not supported for tunnel. */
933 case OVS_KEY_ATTR_ETHERNET:
934 err = set_eth_addr(skb, flow_key, nla_data(a),
935 get_mask(a, struct ovs_key_ethernet *));
938 case OVS_KEY_ATTR_IPV4:
939 err = set_ipv4(skb, flow_key, nla_data(a),
940 get_mask(a, struct ovs_key_ipv4 *));
943 case OVS_KEY_ATTR_IPV6:
944 err = set_ipv6(skb, flow_key, nla_data(a),
945 get_mask(a, struct ovs_key_ipv6 *));
948 case OVS_KEY_ATTR_TCP:
949 err = set_tcp(skb, flow_key, nla_data(a),
950 get_mask(a, struct ovs_key_tcp *));
953 case OVS_KEY_ATTR_UDP:
954 err = set_udp(skb, flow_key, nla_data(a),
955 get_mask(a, struct ovs_key_udp *));
958 case OVS_KEY_ATTR_SCTP:
959 err = set_sctp(skb, flow_key, nla_data(a),
960 get_mask(a, struct ovs_key_sctp *));
963 case OVS_KEY_ATTR_MPLS:
964 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
968 case OVS_KEY_ATTR_CT_STATE:
969 case OVS_KEY_ATTR_CT_ZONE:
970 case OVS_KEY_ATTR_CT_MARK:
971 case OVS_KEY_ATTR_CT_LABEL:
979 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
980 struct sw_flow_key *key,
981 const struct nlattr *a, int rem)
983 struct deferred_action *da;
985 if (!is_flow_key_valid(key)) {
988 err = ovs_flow_key_update(skb, key);
992 BUG_ON(!is_flow_key_valid(key));
994 if (!nla_is_last(a, rem)) {
995 /* Recirc action is the not the last action
996 * of the action list, need to clone the skb.
998 skb = skb_clone(skb, GFP_ATOMIC);
1000 /* Skip the recirc action when out of memory, but
1001 * continue on with the rest of the action list.
1007 da = add_deferred_actions(skb, key, NULL);
1009 da->pkt_key.recirc_id = nla_get_u32(a);
1013 if (net_ratelimit())
1014 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1021 /* Execute a list of actions against 'skb'. */
1022 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1023 struct sw_flow_key *key,
1024 const struct nlattr *attr, int len)
1026 /* Every output action needs a separate clone of 'skb', but the common
1027 * case is just a single output action, so that doing a clone and
1028 * then freeing the original skbuff is wasteful. So the following code
1029 * is slightly obscure just to avoid that.
1032 const struct nlattr *a;
1035 for (a = attr, rem = len; rem > 0;
1036 a = nla_next(a, &rem)) {
1039 if (unlikely(prev_port != -1)) {
1040 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1043 do_output(dp, out_skb, prev_port, key);
1048 switch (nla_type(a)) {
1049 case OVS_ACTION_ATTR_OUTPUT:
1050 prev_port = nla_get_u32(a);
1053 case OVS_ACTION_ATTR_USERSPACE:
1054 output_userspace(dp, skb, key, a, attr, len);
1057 case OVS_ACTION_ATTR_HASH:
1058 execute_hash(skb, key, a);
1061 case OVS_ACTION_ATTR_PUSH_MPLS:
1062 err = push_mpls(skb, key, nla_data(a));
1065 case OVS_ACTION_ATTR_POP_MPLS:
1066 err = pop_mpls(skb, key, nla_get_be16(a));
1069 case OVS_ACTION_ATTR_PUSH_VLAN:
1070 err = push_vlan(skb, key, nla_data(a));
1073 case OVS_ACTION_ATTR_POP_VLAN:
1074 err = pop_vlan(skb, key);
1077 case OVS_ACTION_ATTR_RECIRC:
1078 err = execute_recirc(dp, skb, key, a, rem);
1079 if (nla_is_last(a, rem)) {
1080 /* If this is the last action, the skb has
1081 * been consumed or freed.
1082 * Return immediately.
1088 case OVS_ACTION_ATTR_SET:
1089 err = execute_set_action(skb, key, nla_data(a));
1092 case OVS_ACTION_ATTR_SET_MASKED:
1093 case OVS_ACTION_ATTR_SET_TO_MASKED:
1094 err = execute_masked_set_action(skb, key, nla_data(a));
1097 case OVS_ACTION_ATTR_SAMPLE:
1098 err = sample(dp, skb, key, a, attr, len);
1101 case OVS_ACTION_ATTR_CT:
1102 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1105 /* Hide stolen IP fragments from user space. */
1106 if (err == -EINPROGRESS)
1111 if (unlikely(err)) {
1117 if (prev_port != -1)
1118 do_output(dp, skb, prev_port, key);
1125 static void process_deferred_actions(struct datapath *dp)
1127 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1129 /* Do not touch the FIFO in case there is no deferred actions. */
1130 if (action_fifo_is_empty(fifo))
1133 /* Finishing executing all deferred actions. */
1135 struct deferred_action *da = action_fifo_get(fifo);
1136 struct sk_buff *skb = da->skb;
1137 struct sw_flow_key *key = &da->pkt_key;
1138 const struct nlattr *actions = da->actions;
1141 do_execute_actions(dp, skb, key, actions,
1144 ovs_dp_process_packet(skb, key);
1145 } while (!action_fifo_is_empty(fifo));
1147 /* Reset FIFO for the next packet. */
1148 action_fifo_init(fifo);
1151 /* Execute a list of actions against 'skb'. */
1152 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1153 const struct sw_flow_actions *acts,
1154 struct sw_flow_key *key)
1156 int level = this_cpu_read(exec_actions_level);
1159 this_cpu_inc(exec_actions_level);
1160 err = do_execute_actions(dp, skb, key,
1161 acts->actions, acts->actions_len);
1164 process_deferred_actions(dp);
1166 this_cpu_dec(exec_actions_level);
1170 int action_fifos_init(void)
1172 action_fifos = alloc_percpu(struct action_fifo);
1179 void action_fifos_exit(void)
1181 free_percpu(action_fifos);