2 * Copyright (c) 2007-2015 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/skbuff.h>
24 #include <linux/openvswitch.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/in6.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_vlan.h>
36 #include <net/checksum.h>
37 #include <net/dsfield.h>
39 #include <net/sctp/checksum.h>
42 #include "conntrack.h"
46 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
47 struct sw_flow_key *key,
48 const struct nlattr *attr, int len);
50 struct deferred_action {
52 const struct nlattr *actions;
54 /* Store pkt_key clone when creating deferred action. */
55 struct sw_flow_key pkt_key;
58 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
59 struct ovs_frag_data {
63 __be16 inner_protocol;
67 u8 l2_data[MAX_L2_LEN];
70 static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
72 #define DEFERRED_ACTION_FIFO_SIZE 10
76 /* Deferred action fifo queue storage. */
77 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
80 static struct action_fifo __percpu *action_fifos;
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 queue entry 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),
150 hdr->h_proto = ethertype;
153 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
154 const struct ovs_action_push_mpls *mpls)
156 __be32 *new_mpls_lse;
158 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
159 if (skb->encapsulation)
162 if (skb_cow_head(skb, MPLS_HLEN) < 0)
165 skb_push(skb, MPLS_HLEN);
166 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
168 skb_reset_mac_header(skb);
170 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
171 *new_mpls_lse = mpls->mpls_lse;
173 skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
175 update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
176 if (!ovs_skb_get_inner_protocol(skb))
177 ovs_skb_set_inner_protocol(skb, skb->protocol);
178 skb->protocol = mpls->mpls_ethertype;
180 invalidate_flow_key(key);
184 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
185 const __be16 ethertype)
190 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
194 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
196 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
199 __skb_pull(skb, MPLS_HLEN);
200 skb_reset_mac_header(skb);
202 /* skb_mpls_header() is used to locate the ethertype
203 * field correctly in the presence of VLAN tags.
205 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
206 update_ethertype(skb, hdr, ethertype);
207 if (eth_p_mpls(skb->protocol))
208 skb->protocol = ethertype;
210 invalidate_flow_key(key);
214 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
215 const __be32 *mpls_lse, const __be32 *mask)
221 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
225 stack = (__be32 *)skb_mpls_header(skb);
226 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
227 if (skb->ip_summed == CHECKSUM_COMPLETE) {
228 __be32 diff[] = { ~(*stack), lse };
230 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
235 flow_key->mpls.top_lse = lse;
239 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
243 err = skb_vlan_pop(skb);
244 if (skb_vlan_tag_present(skb))
245 invalidate_flow_key(key);
251 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
252 const struct ovs_action_push_vlan *vlan)
254 if (skb_vlan_tag_present(skb))
255 invalidate_flow_key(key);
257 key->eth.tci = vlan->vlan_tci;
258 return skb_vlan_push(skb, vlan->vlan_tpid,
259 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
262 /* 'src' is already properly masked. */
263 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
265 u16 *dst = (u16 *)dst_;
266 const u16 *src = (const u16 *)src_;
267 const u16 *mask = (const u16 *)mask_;
269 OVS_SET_MASKED(dst[0], src[0], mask[0]);
270 OVS_SET_MASKED(dst[1], src[1], mask[1]);
271 OVS_SET_MASKED(dst[2], src[2], mask[2]);
274 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
275 const struct ovs_key_ethernet *key,
276 const struct ovs_key_ethernet *mask)
280 err = skb_ensure_writable(skb, ETH_HLEN);
284 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
286 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
288 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
291 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
293 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
294 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
298 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
299 __be32 addr, __be32 new_addr)
301 int transport_len = skb->len - skb_transport_offset(skb);
303 if (nh->frag_off & htons(IP_OFFSET))
306 if (nh->protocol == IPPROTO_TCP) {
307 if (likely(transport_len >= sizeof(struct tcphdr)))
308 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
309 addr, new_addr, true);
310 } else if (nh->protocol == IPPROTO_UDP) {
311 if (likely(transport_len >= sizeof(struct udphdr))) {
312 struct udphdr *uh = udp_hdr(skb);
314 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
315 inet_proto_csum_replace4(&uh->check, skb,
316 addr, new_addr, true);
318 uh->check = CSUM_MANGLED_0;
325 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
326 __be32 *addr, __be32 new_addr)
328 update_ip_l4_checksum(skb, nh, *addr, new_addr);
329 csum_replace4(&nh->check, *addr, new_addr);
334 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
335 __be32 addr[4], const __be32 new_addr[4])
337 int transport_len = skb->len - skb_transport_offset(skb);
339 if (l4_proto == NEXTHDR_TCP) {
340 if (likely(transport_len >= sizeof(struct tcphdr)))
341 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
342 addr, new_addr, true);
343 } else if (l4_proto == NEXTHDR_UDP) {
344 if (likely(transport_len >= sizeof(struct udphdr))) {
345 struct udphdr *uh = udp_hdr(skb);
347 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
348 inet_proto_csum_replace16(&uh->check, skb,
349 addr, new_addr, true);
351 uh->check = CSUM_MANGLED_0;
354 } else if (l4_proto == NEXTHDR_ICMP) {
355 if (likely(transport_len >= sizeof(struct icmp6hdr)))
356 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
357 skb, addr, new_addr, true);
361 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
362 const __be32 mask[4], __be32 masked[4])
364 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
365 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
366 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
367 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
370 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
371 __be32 addr[4], const __be32 new_addr[4],
372 bool recalculate_csum)
374 if (likely(recalculate_csum))
375 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
378 memcpy(addr, new_addr, sizeof(__be32[4]));
381 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
383 /* Bits 21-24 are always unmasked, so this retains their values. */
384 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
385 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
386 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
389 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
392 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
394 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
398 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
399 const struct ovs_key_ipv4 *key,
400 const struct ovs_key_ipv4 *mask)
406 err = skb_ensure_writable(skb, skb_network_offset(skb) +
407 sizeof(struct iphdr));
413 /* Setting an IP addresses is typically only a side effect of
414 * matching on them in the current userspace implementation, so it
415 * makes sense to check if the value actually changed.
417 if (mask->ipv4_src) {
418 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
420 if (unlikely(new_addr != nh->saddr)) {
421 set_ip_addr(skb, nh, &nh->saddr, new_addr);
422 flow_key->ipv4.addr.src = new_addr;
425 if (mask->ipv4_dst) {
426 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
428 if (unlikely(new_addr != nh->daddr)) {
429 set_ip_addr(skb, nh, &nh->daddr, new_addr);
430 flow_key->ipv4.addr.dst = new_addr;
433 if (mask->ipv4_tos) {
434 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
435 flow_key->ip.tos = nh->tos;
437 if (mask->ipv4_ttl) {
438 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
439 flow_key->ip.ttl = nh->ttl;
445 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
447 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
450 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
451 const struct ovs_key_ipv6 *key,
452 const struct ovs_key_ipv6 *mask)
457 err = skb_ensure_writable(skb, skb_network_offset(skb) +
458 sizeof(struct ipv6hdr));
464 /* Setting an IP addresses is typically only a side effect of
465 * matching on them in the current userspace implementation, so it
466 * makes sense to check if the value actually changed.
468 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
469 __be32 *saddr = (__be32 *)&nh->saddr;
472 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
474 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
475 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
477 memcpy(&flow_key->ipv6.addr.src, masked,
478 sizeof(flow_key->ipv6.addr.src));
481 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
482 unsigned int offset = 0;
483 int flags = IP6_FH_F_SKIP_RH;
484 bool recalc_csum = true;
485 __be32 *daddr = (__be32 *)&nh->daddr;
488 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
490 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
491 if (ipv6_ext_hdr(nh->nexthdr))
492 recalc_csum = (ipv6_find_hdr(skb, &offset,
497 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
499 memcpy(&flow_key->ipv6.addr.dst, masked,
500 sizeof(flow_key->ipv6.addr.dst));
503 if (mask->ipv6_tclass) {
504 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
505 flow_key->ip.tos = ipv6_get_dsfield(nh);
507 if (mask->ipv6_label) {
508 set_ipv6_fl(nh, ntohl(key->ipv6_label),
509 ntohl(mask->ipv6_label));
510 flow_key->ipv6.label =
511 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
513 if (mask->ipv6_hlimit) {
514 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
516 flow_key->ip.ttl = nh->hop_limit;
521 /* Must follow skb_ensure_writable() since that can move the skb data. */
522 static void set_tp_port(struct sk_buff *skb, __be16 *port,
523 __be16 new_port, __sum16 *check)
525 inet_proto_csum_replace2(check, skb, *port, new_port, false);
529 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
530 const struct ovs_key_udp *key,
531 const struct ovs_key_udp *mask)
537 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
538 sizeof(struct udphdr));
543 /* Either of the masks is non-zero, so do not bother checking them. */
544 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
545 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
547 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
548 if (likely(src != uh->source)) {
549 set_tp_port(skb, &uh->source, src, &uh->check);
550 flow_key->tp.src = src;
552 if (likely(dst != uh->dest)) {
553 set_tp_port(skb, &uh->dest, dst, &uh->check);
554 flow_key->tp.dst = dst;
557 if (unlikely(!uh->check))
558 uh->check = CSUM_MANGLED_0;
562 flow_key->tp.src = src;
563 flow_key->tp.dst = dst;
571 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
572 const struct ovs_key_tcp *key,
573 const struct ovs_key_tcp *mask)
579 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
580 sizeof(struct tcphdr));
585 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
586 if (likely(src != th->source)) {
587 set_tp_port(skb, &th->source, src, &th->check);
588 flow_key->tp.src = src;
590 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
591 if (likely(dst != th->dest)) {
592 set_tp_port(skb, &th->dest, dst, &th->check);
593 flow_key->tp.dst = dst;
600 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
601 const struct ovs_key_sctp *key,
602 const struct ovs_key_sctp *mask)
604 unsigned int sctphoff = skb_transport_offset(skb);
606 __le32 old_correct_csum, new_csum, old_csum;
609 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
614 old_csum = sh->checksum;
615 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
617 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
618 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
620 new_csum = sctp_compute_cksum(skb, sctphoff);
622 /* Carry any checksum errors through. */
623 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
626 flow_key->tp.src = sh->source;
627 flow_key->tp.dst = sh->dest;
632 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
634 struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
635 struct vport *vport = data->vport;
637 if (skb_cow_head(skb, data->l2_len) < 0) {
642 __skb_dst_copy(skb, data->dst);
643 *OVS_GSO_CB(skb) = data->cb;
644 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
645 skb->vlan_tci = data->vlan_tci;
646 skb->vlan_proto = data->vlan_proto;
648 /* Reconstruct the MAC header. */
649 skb_push(skb, data->l2_len);
650 memcpy(skb->data, &data->l2_data, data->l2_len);
651 skb_postpush_rcsum(skb, skb->data, data->l2_len);
652 skb_reset_mac_header(skb);
654 ovs_vport_send(vport, skb);
659 ovs_dst_get_mtu(const struct dst_entry *dst)
661 return dst->dev->mtu;
664 static struct dst_ops ovs_dst_ops = {
666 .mtu = ovs_dst_get_mtu,
669 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
670 * ovs_vport_output(), which is called once per fragmented packet.
672 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
674 unsigned int hlen = skb_network_offset(skb);
675 struct ovs_frag_data *data;
677 data = this_cpu_ptr(&ovs_frag_data_storage);
678 data->dst = (unsigned long) skb_dst(skb);
680 data->cb = *OVS_GSO_CB(skb);
681 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
682 data->vlan_tci = skb->vlan_tci;
683 data->vlan_proto = skb->vlan_proto;
685 memcpy(&data->l2_data, skb->data, hlen);
687 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
691 static void ovs_fragment(struct net *net, struct vport *vport,
692 struct sk_buff *skb, u16 mru, __be16 ethertype)
694 if (skb_network_offset(skb) > MAX_L2_LEN) {
695 OVS_NLERR(1, "L2 header too long to fragment");
699 if (ethertype == htons(ETH_P_IP)) {
700 struct dst_entry ovs_dst;
701 unsigned long orig_dst;
703 prepare_frag(vport, skb);
704 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
705 DST_OBSOLETE_NONE, DST_NOCOUNT);
706 ovs_dst.dev = vport->dev;
708 orig_dst = (unsigned long) skb_dst(skb);
709 skb_dst_set_noref(skb, &ovs_dst);
710 IPCB(skb)->frag_max_size = mru;
712 ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
713 refdst_drop(orig_dst);
714 } else if (ethertype == htons(ETH_P_IPV6)) {
715 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
716 unsigned long orig_dst;
717 struct rt6_info ovs_rt;
723 prepare_frag(vport, skb);
724 memset(&ovs_rt, 0, sizeof(ovs_rt));
725 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
726 DST_OBSOLETE_NONE, DST_NOCOUNT);
727 ovs_rt.dst.dev = vport->dev;
729 orig_dst = (unsigned long) skb_dst(skb);
730 skb_dst_set_noref(skb, &ovs_rt.dst);
731 IP6CB(skb)->frag_max_size = mru;
732 #ifdef HAVE_IP_LOCAL_OUT_TAKES_NET
733 v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
735 v6ops->fragment(skb->sk, skb, ovs_vport_output);
737 refdst_drop(orig_dst);
739 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
740 ovs_vport_name(vport), ntohs(ethertype), mru,
750 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
751 struct sw_flow_key *key)
753 struct vport *vport = ovs_vport_rcu(dp, out_port);
756 u16 mru = OVS_CB(skb)->mru;
757 u32 cutlen = OVS_CB(skb)->cutlen;
759 if (unlikely(cutlen > 0)) {
760 if (skb->len - cutlen > ETH_HLEN)
761 pskb_trim(skb, skb->len - cutlen);
763 pskb_trim(skb, ETH_HLEN);
766 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
767 ovs_vport_send(vport, skb);
768 } else if (mru <= vport->dev->mtu) {
769 struct net *net = ovs_dp_get_net(dp);
770 __be16 ethertype = key->eth.type;
772 if (!is_flow_key_valid(key)) {
773 if (eth_p_mpls(skb->protocol))
774 ethertype = ovs_skb_get_inner_protocol(skb);
776 ethertype = vlan_get_protocol(skb);
779 ovs_fragment(net, vport, skb, mru, ethertype);
781 OVS_NLERR(true, "Cannot fragment IP frames");
789 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
790 struct sw_flow_key *key, const struct nlattr *attr,
791 const struct nlattr *actions, int actions_len,
794 struct dp_upcall_info upcall;
795 const struct nlattr *a;
798 memset(&upcall, 0, sizeof(upcall));
799 upcall.cmd = OVS_PACKET_CMD_ACTION;
800 upcall.mru = OVS_CB(skb)->mru;
802 SKB_INIT_FILL_METADATA_DST(skb);
803 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
804 a = nla_next(a, &rem)) {
805 switch (nla_type(a)) {
806 case OVS_USERSPACE_ATTR_USERDATA:
810 case OVS_USERSPACE_ATTR_PID:
811 upcall.portid = nla_get_u32(a);
814 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
815 /* Get out tunnel info. */
818 vport = ovs_vport_rcu(dp, nla_get_u32(a));
820 err = dev_fill_metadata_dst(vport->dev, skb);
822 upcall.egress_tun_info = skb_tunnel_info(skb);
828 case OVS_USERSPACE_ATTR_ACTIONS: {
829 /* Include actions. */
830 upcall.actions = actions;
831 upcall.actions_len = actions_len;
835 } /* End of switch. */
838 err = ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
839 SKB_RESTORE_FILL_METADATA_DST(skb);
843 static int sample(struct datapath *dp, struct sk_buff *skb,
844 struct sw_flow_key *key, const struct nlattr *attr,
845 const struct nlattr *actions, int actions_len)
847 const struct nlattr *acts_list = NULL;
848 const struct nlattr *a;
852 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
853 a = nla_next(a, &rem)) {
856 switch (nla_type(a)) {
857 case OVS_SAMPLE_ATTR_PROBABILITY:
858 probability = nla_get_u32(a);
859 if (!probability || prandom_u32() > probability)
863 case OVS_SAMPLE_ATTR_ACTIONS:
869 rem = nla_len(acts_list);
870 a = nla_data(acts_list);
872 /* Actions list is empty, do nothing */
876 /* The only known usage of sample action is having a single user-space
877 * action, or having a truncate action followed by a single user-space
878 * action. Treat this usage as a special case.
879 * The output_userspace() should clone the skb to be sent to the
880 * user space. This skb will be consumed by its caller.
882 if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
883 struct ovs_action_trunc *trunc = nla_data(a);
885 if (skb->len > trunc->max_len)
886 cutlen = skb->len - trunc->max_len;
888 a = nla_next(a, &rem);
891 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
892 nla_is_last(a, rem)))
893 return output_userspace(dp, skb, key, a, actions,
894 actions_len, cutlen);
896 skb = skb_clone(skb, GFP_ATOMIC);
898 /* Skip the sample action when out of memory. */
901 if (!add_deferred_actions(skb, key, a)) {
903 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
911 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
912 const struct nlattr *attr)
914 struct ovs_action_hash *hash_act = nla_data(attr);
917 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
918 hash = skb_get_hash(skb);
919 hash = jhash_1word(hash, hash_act->hash_basis);
923 key->ovs_flow_hash = hash;
926 static int execute_set_action(struct sk_buff *skb,
927 struct sw_flow_key *flow_key,
928 const struct nlattr *a)
930 /* Only tunnel set execution is supported without a mask. */
931 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
932 struct ovs_tunnel_info *tun = nla_data(a);
934 ovs_skb_dst_drop(skb);
935 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
936 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
943 /* Mask is at the midpoint of the data. */
944 #define get_mask(a, type) ((const type)nla_data(a) + 1)
946 static int execute_masked_set_action(struct sk_buff *skb,
947 struct sw_flow_key *flow_key,
948 const struct nlattr *a)
952 switch (nla_type(a)) {
953 case OVS_KEY_ATTR_PRIORITY:
954 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
955 *get_mask(a, u32 *));
956 flow_key->phy.priority = skb->priority;
959 case OVS_KEY_ATTR_SKB_MARK:
960 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
961 flow_key->phy.skb_mark = skb->mark;
964 case OVS_KEY_ATTR_TUNNEL_INFO:
965 /* Masked data not supported for tunnel. */
969 case OVS_KEY_ATTR_ETHERNET:
970 err = set_eth_addr(skb, flow_key, nla_data(a),
971 get_mask(a, struct ovs_key_ethernet *));
974 case OVS_KEY_ATTR_IPV4:
975 err = set_ipv4(skb, flow_key, nla_data(a),
976 get_mask(a, struct ovs_key_ipv4 *));
979 case OVS_KEY_ATTR_IPV6:
980 err = set_ipv6(skb, flow_key, nla_data(a),
981 get_mask(a, struct ovs_key_ipv6 *));
984 case OVS_KEY_ATTR_TCP:
985 err = set_tcp(skb, flow_key, nla_data(a),
986 get_mask(a, struct ovs_key_tcp *));
989 case OVS_KEY_ATTR_UDP:
990 err = set_udp(skb, flow_key, nla_data(a),
991 get_mask(a, struct ovs_key_udp *));
994 case OVS_KEY_ATTR_SCTP:
995 err = set_sctp(skb, flow_key, nla_data(a),
996 get_mask(a, struct ovs_key_sctp *));
999 case OVS_KEY_ATTR_MPLS:
1000 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
1004 case OVS_KEY_ATTR_CT_STATE:
1005 case OVS_KEY_ATTR_CT_ZONE:
1006 case OVS_KEY_ATTR_CT_MARK:
1007 case OVS_KEY_ATTR_CT_LABELS:
1015 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
1016 struct sw_flow_key *key,
1017 const struct nlattr *a, int rem)
1019 struct deferred_action *da;
1021 if (!is_flow_key_valid(key)) {
1024 err = ovs_flow_key_update(skb, key);
1028 BUG_ON(!is_flow_key_valid(key));
1030 if (!nla_is_last(a, rem)) {
1031 /* Recirc action is the not the last action
1032 * of the action list, need to clone the skb.
1034 skb = skb_clone(skb, GFP_ATOMIC);
1036 /* Skip the recirc action when out of memory, but
1037 * continue on with the rest of the action list.
1043 da = add_deferred_actions(skb, key, NULL);
1045 da->pkt_key.recirc_id = nla_get_u32(a);
1049 if (net_ratelimit())
1050 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1057 /* Execute a list of actions against 'skb'. */
1058 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1059 struct sw_flow_key *key,
1060 const struct nlattr *attr, int len)
1062 /* Every output action needs a separate clone of 'skb', but the common
1063 * case is just a single output action, so that doing a clone and
1064 * then freeing the original skbuff is wasteful. So the following code
1065 * is slightly obscure just to avoid that.
1068 const struct nlattr *a;
1071 for (a = attr, rem = len; rem > 0;
1072 a = nla_next(a, &rem)) {
1075 if (unlikely(prev_port != -1)) {
1076 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1079 do_output(dp, out_skb, prev_port, key);
1081 OVS_CB(skb)->cutlen = 0;
1085 switch (nla_type(a)) {
1086 case OVS_ACTION_ATTR_OUTPUT:
1087 prev_port = nla_get_u32(a);
1090 case OVS_ACTION_ATTR_TRUNC: {
1091 struct ovs_action_trunc *trunc = nla_data(a);
1093 if (skb->len > trunc->max_len)
1094 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1098 case OVS_ACTION_ATTR_USERSPACE:
1099 output_userspace(dp, skb, key, a, attr,
1100 len, OVS_CB(skb)->cutlen);
1101 OVS_CB(skb)->cutlen = 0;
1104 case OVS_ACTION_ATTR_HASH:
1105 execute_hash(skb, key, a);
1108 case OVS_ACTION_ATTR_PUSH_MPLS:
1109 err = push_mpls(skb, key, nla_data(a));
1112 case OVS_ACTION_ATTR_POP_MPLS:
1113 err = pop_mpls(skb, key, nla_get_be16(a));
1116 case OVS_ACTION_ATTR_PUSH_VLAN:
1117 err = push_vlan(skb, key, nla_data(a));
1120 case OVS_ACTION_ATTR_POP_VLAN:
1121 err = pop_vlan(skb, key);
1124 case OVS_ACTION_ATTR_RECIRC:
1125 err = execute_recirc(dp, skb, key, a, rem);
1126 if (nla_is_last(a, rem)) {
1127 /* If this is the last action, the skb has
1128 * been consumed or freed.
1129 * Return immediately.
1135 case OVS_ACTION_ATTR_SET:
1136 err = execute_set_action(skb, key, nla_data(a));
1139 case OVS_ACTION_ATTR_SET_MASKED:
1140 case OVS_ACTION_ATTR_SET_TO_MASKED:
1141 err = execute_masked_set_action(skb, key, nla_data(a));
1144 case OVS_ACTION_ATTR_SAMPLE:
1145 err = sample(dp, skb, key, a, attr, len);
1148 case OVS_ACTION_ATTR_CT:
1149 if (!is_flow_key_valid(key)) {
1150 err = ovs_flow_key_update(skb, key);
1155 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1158 /* Hide stolen IP fragments from user space. */
1160 return err == -EINPROGRESS ? 0 : err;
1164 if (unlikely(err)) {
1170 if (prev_port != -1)
1171 do_output(dp, skb, prev_port, key);
1178 static void process_deferred_actions(struct datapath *dp)
1180 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1182 /* Do not touch the FIFO in case there is no deferred actions. */
1183 if (action_fifo_is_empty(fifo))
1186 /* Finishing executing all deferred actions. */
1188 struct deferred_action *da = action_fifo_get(fifo);
1189 struct sk_buff *skb = da->skb;
1190 struct sw_flow_key *key = &da->pkt_key;
1191 const struct nlattr *actions = da->actions;
1194 do_execute_actions(dp, skb, key, actions,
1197 ovs_dp_process_packet(skb, key);
1198 } while (!action_fifo_is_empty(fifo));
1200 /* Reset FIFO for the next packet. */
1201 action_fifo_init(fifo);
1204 /* Execute a list of actions against 'skb'. */
1205 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1206 const struct sw_flow_actions *acts,
1207 struct sw_flow_key *key)
1209 static const int ovs_recursion_limit = 4;
1212 level = __this_cpu_inc_return(exec_actions_level);
1213 if (unlikely(level > ovs_recursion_limit)) {
1214 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
1221 err = do_execute_actions(dp, skb, key,
1222 acts->actions, acts->actions_len);
1225 process_deferred_actions(dp);
1228 __this_cpu_dec(exec_actions_level);
1232 int action_fifos_init(void)
1234 action_fifos = alloc_percpu(struct action_fifo);
1241 void action_fifos_exit(void)
1243 free_percpu(action_fifos);