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"
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 #define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
83 * looping by the network stack
85 static DEFINE_PER_CPU(int, exec_actions_level);
87 static void action_fifo_init(struct action_fifo *fifo)
93 static bool action_fifo_is_empty(const struct action_fifo *fifo)
95 return (fifo->head == fifo->tail);
98 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
100 if (action_fifo_is_empty(fifo))
103 return &fifo->fifo[fifo->tail++];
106 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
108 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
111 return &fifo->fifo[fifo->head++];
114 /* Return queue entry if fifo is not full */
115 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
116 const struct sw_flow_key *key,
117 const struct nlattr *attr)
119 struct action_fifo *fifo;
120 struct deferred_action *da;
122 fifo = this_cpu_ptr(action_fifos);
123 da = action_fifo_put(fifo);
133 static void invalidate_flow_key(struct sw_flow_key *key)
135 key->eth.type = htons(0);
138 static bool is_flow_key_valid(const struct sw_flow_key *key)
140 return !!key->eth.type;
143 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
144 const struct ovs_action_push_mpls *mpls)
146 __be32 *new_mpls_lse;
149 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
150 if (skb_encapsulation(skb))
153 if (skb_cow_head(skb, MPLS_HLEN) < 0)
156 skb_push(skb, MPLS_HLEN);
157 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
159 skb_reset_mac_header(skb);
161 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
162 *new_mpls_lse = mpls->mpls_lse;
164 if (skb->ip_summed == CHECKSUM_COMPLETE)
165 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
169 hdr->h_proto = mpls->mpls_ethertype;
170 if (!ovs_skb_get_inner_protocol(skb))
171 ovs_skb_set_inner_protocol(skb, skb->protocol);
172 skb->protocol = mpls->mpls_ethertype;
174 invalidate_flow_key(key);
178 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
179 const __be16 ethertype)
184 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
188 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
190 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
193 __skb_pull(skb, MPLS_HLEN);
194 skb_reset_mac_header(skb);
196 /* skb_mpls_header() is used to locate the ethertype
197 * field correctly in the presence of VLAN tags.
199 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
200 hdr->h_proto = ethertype;
201 if (eth_p_mpls(skb->protocol))
202 skb->protocol = ethertype;
204 invalidate_flow_key(key);
208 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
209 const __be32 *mpls_lse, const __be32 *mask)
215 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
219 stack = (__be32 *)skb_mpls_header(skb);
220 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
221 if (skb->ip_summed == CHECKSUM_COMPLETE) {
222 __be32 diff[] = { ~(*stack), lse };
224 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
229 flow_key->mpls.top_lse = lse;
233 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
237 err = skb_vlan_pop(skb);
238 if (skb_vlan_tag_present(skb))
239 invalidate_flow_key(key);
245 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
246 const struct ovs_action_push_vlan *vlan)
248 if (skb_vlan_tag_present(skb))
249 invalidate_flow_key(key);
251 key->eth.tci = vlan->vlan_tci;
252 return skb_vlan_push(skb, vlan->vlan_tpid,
253 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
256 /* 'src' is already properly masked. */
257 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
259 u16 *dst = (u16 *)dst_;
260 const u16 *src = (const u16 *)src_;
261 const u16 *mask = (const u16 *)mask_;
263 OVS_SET_MASKED(dst[0], src[0], mask[0]);
264 OVS_SET_MASKED(dst[1], src[1], mask[1]);
265 OVS_SET_MASKED(dst[2], src[2], mask[2]);
268 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
269 const struct ovs_key_ethernet *key,
270 const struct ovs_key_ethernet *mask)
274 err = skb_ensure_writable(skb, ETH_HLEN);
278 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
280 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
282 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
285 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
287 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
288 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
292 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
293 __be32 addr, __be32 new_addr)
295 int transport_len = skb->len - skb_transport_offset(skb);
297 if (nh->frag_off & htons(IP_OFFSET))
300 if (nh->protocol == IPPROTO_TCP) {
301 if (likely(transport_len >= sizeof(struct tcphdr)))
302 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
304 } else if (nh->protocol == IPPROTO_UDP) {
305 if (likely(transport_len >= sizeof(struct udphdr))) {
306 struct udphdr *uh = udp_hdr(skb);
308 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
309 inet_proto_csum_replace4(&uh->check, skb,
312 uh->check = CSUM_MANGLED_0;
319 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
320 __be32 *addr, __be32 new_addr)
322 update_ip_l4_checksum(skb, nh, *addr, new_addr);
323 csum_replace4(&nh->check, *addr, new_addr);
328 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
329 __be32 addr[4], const __be32 new_addr[4])
331 int transport_len = skb->len - skb_transport_offset(skb);
333 if (l4_proto == NEXTHDR_TCP) {
334 if (likely(transport_len >= sizeof(struct tcphdr)))
335 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
337 } else if (l4_proto == NEXTHDR_UDP) {
338 if (likely(transport_len >= sizeof(struct udphdr))) {
339 struct udphdr *uh = udp_hdr(skb);
341 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
342 inet_proto_csum_replace16(&uh->check, skb,
345 uh->check = CSUM_MANGLED_0;
348 } else if (l4_proto == NEXTHDR_ICMP) {
349 if (likely(transport_len >= sizeof(struct icmp6hdr)))
350 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
351 skb, addr, new_addr, 1);
355 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
356 const __be32 mask[4], __be32 masked[4])
358 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
359 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
360 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
361 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
364 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
365 __be32 addr[4], const __be32 new_addr[4],
366 bool recalculate_csum)
368 if (likely(recalculate_csum))
369 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
372 memcpy(addr, new_addr, sizeof(__be32[4]));
375 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
377 /* Bits 21-24 are always unmasked, so this retains their values. */
378 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
379 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
380 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
383 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
386 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
388 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
392 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
393 const struct ovs_key_ipv4 *key,
394 const struct ovs_key_ipv4 *mask)
400 err = skb_ensure_writable(skb, skb_network_offset(skb) +
401 sizeof(struct iphdr));
407 /* Setting an IP addresses is typically only a side effect of
408 * matching on them in the current userspace implementation, so it
409 * makes sense to check if the value actually changed.
411 if (mask->ipv4_src) {
412 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
414 if (unlikely(new_addr != nh->saddr)) {
415 set_ip_addr(skb, nh, &nh->saddr, new_addr);
416 flow_key->ipv4.addr.src = new_addr;
419 if (mask->ipv4_dst) {
420 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
422 if (unlikely(new_addr != nh->daddr)) {
423 set_ip_addr(skb, nh, &nh->daddr, new_addr);
424 flow_key->ipv4.addr.dst = new_addr;
427 if (mask->ipv4_tos) {
428 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
429 flow_key->ip.tos = nh->tos;
431 if (mask->ipv4_ttl) {
432 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
433 flow_key->ip.ttl = nh->ttl;
439 static bool is_ipv6_mask_nonzero(const __be32 addr[4])
441 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
444 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
445 const struct ovs_key_ipv6 *key,
446 const struct ovs_key_ipv6 *mask)
451 err = skb_ensure_writable(skb, skb_network_offset(skb) +
452 sizeof(struct ipv6hdr));
458 /* Setting an IP addresses is typically only a side effect of
459 * matching on them in the current userspace implementation, so it
460 * makes sense to check if the value actually changed.
462 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
463 __be32 *saddr = (__be32 *)&nh->saddr;
466 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
468 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
469 set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
471 memcpy(&flow_key->ipv6.addr.src, masked,
472 sizeof(flow_key->ipv6.addr.src));
475 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
476 unsigned int offset = 0;
477 int flags = IP6_FH_F_SKIP_RH;
478 bool recalc_csum = true;
479 __be32 *daddr = (__be32 *)&nh->daddr;
482 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
484 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
485 if (ipv6_ext_hdr(nh->nexthdr))
486 recalc_csum = (ipv6_find_hdr(skb, &offset,
491 set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
493 memcpy(&flow_key->ipv6.addr.dst, masked,
494 sizeof(flow_key->ipv6.addr.dst));
497 if (mask->ipv6_tclass) {
498 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
499 flow_key->ip.tos = ipv6_get_dsfield(nh);
501 if (mask->ipv6_label) {
502 set_ipv6_fl(nh, ntohl(key->ipv6_label),
503 ntohl(mask->ipv6_label));
504 flow_key->ipv6.label =
505 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
507 if (mask->ipv6_hlimit) {
508 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
510 flow_key->ip.ttl = nh->hop_limit;
515 /* Must follow skb_ensure_writable() since that can move the skb data. */
516 static void set_tp_port(struct sk_buff *skb, __be16 *port,
517 __be16 new_port, __sum16 *check)
519 inet_proto_csum_replace2(check, skb, *port, new_port, 0);
523 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
524 const struct ovs_key_udp *key,
525 const struct ovs_key_udp *mask)
531 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
532 sizeof(struct udphdr));
537 /* Either of the masks is non-zero, so do not bother checking them. */
538 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
539 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
541 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
542 if (likely(src != uh->source)) {
543 set_tp_port(skb, &uh->source, src, &uh->check);
544 flow_key->tp.src = src;
546 if (likely(dst != uh->dest)) {
547 set_tp_port(skb, &uh->dest, dst, &uh->check);
548 flow_key->tp.dst = dst;
551 if (unlikely(!uh->check))
552 uh->check = CSUM_MANGLED_0;
556 flow_key->tp.src = src;
557 flow_key->tp.dst = dst;
565 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
566 const struct ovs_key_tcp *key,
567 const struct ovs_key_tcp *mask)
573 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
574 sizeof(struct tcphdr));
579 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
580 if (likely(src != th->source)) {
581 set_tp_port(skb, &th->source, src, &th->check);
582 flow_key->tp.src = src;
584 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
585 if (likely(dst != th->dest)) {
586 set_tp_port(skb, &th->dest, dst, &th->check);
587 flow_key->tp.dst = dst;
594 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
595 const struct ovs_key_sctp *key,
596 const struct ovs_key_sctp *mask)
598 unsigned int sctphoff = skb_transport_offset(skb);
600 __le32 old_correct_csum, new_csum, old_csum;
603 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
608 old_csum = sh->checksum;
609 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
611 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
612 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
614 new_csum = sctp_compute_cksum(skb, sctphoff);
616 /* Carry any checksum errors through. */
617 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
620 flow_key->tp.src = sh->source;
621 flow_key->tp.dst = sh->dest;
626 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,9,0)
627 static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
629 struct ovs_frag_data *data = get_pcpu_ptr(ovs_frag_data_storage);
630 struct vport *vport = data->vport;
632 if (skb_cow_head(skb, data->l2_len) < 0) {
637 __skb_dst_copy(skb, data->dst);
638 *OVS_CB(skb) = data->cb;
639 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
640 skb->vlan_tci = data->vlan_tci;
641 skb->vlan_proto = data->vlan_proto;
643 /* Reconstruct the MAC header. */
644 skb_push(skb, data->l2_len);
645 memcpy(skb->data, &data->l2_data, data->l2_len);
646 ovs_skb_postpush_rcsum(skb, skb->data, data->l2_len);
647 skb_reset_mac_header(skb);
649 ovs_vport_send(vport, skb);
654 ovs_dst_get_mtu(const struct dst_entry *dst)
656 return dst->dev->mtu;
659 static struct dst_ops ovs_dst_ops = {
661 .mtu = ovs_dst_get_mtu,
664 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
665 * ovs_vport_output(), which is called once per fragmented packet.
667 static void prepare_frag(struct vport *vport, struct sk_buff *skb)
669 unsigned int hlen = skb_network_offset(skb);
670 struct ovs_frag_data *data;
672 data = get_pcpu_ptr(ovs_frag_data_storage);
673 data->dst = (unsigned long) skb_dst(skb);
675 data->cb = *OVS_CB(skb);
676 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
677 data->vlan_tci = skb->vlan_tci;
678 data->vlan_proto = skb->vlan_proto;
680 memcpy(&data->l2_data, skb->data, hlen);
682 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
686 static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
689 if (skb_network_offset(skb) > MAX_L2_LEN) {
690 OVS_NLERR(1, "L2 header too long to fragment");
694 if (ethertype == htons(ETH_P_IP)) {
695 struct dst_entry ovs_dst;
696 unsigned long orig_dst;
698 prepare_frag(vport, skb);
699 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
700 DST_OBSOLETE_NONE, DST_NOCOUNT);
701 ovs_dst.dev = vport->dev;
703 orig_dst = (unsigned long) skb_dst(skb);
704 skb_dst_set_noref(skb, &ovs_dst);
705 IPCB(skb)->frag_max_size = mru;
707 ip_do_fragment(skb->sk, skb, ovs_vport_output);
708 refdst_drop(orig_dst);
709 } else if (ethertype == htons(ETH_P_IPV6)) {
710 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
711 unsigned long orig_dst;
712 struct rt6_info ovs_rt;
719 prepare_frag(vport, skb);
720 memset(&ovs_rt, 0, sizeof(ovs_rt));
721 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
722 DST_OBSOLETE_NONE, DST_NOCOUNT);
723 ovs_rt.dst.dev = vport->dev;
725 orig_dst = (unsigned long) skb_dst(skb);
726 skb_dst_set_noref(skb, &ovs_rt.dst);
727 IP6CB(skb)->frag_max_size = mru;
729 v6ops->fragment(skb->sk, skb, ovs_vport_output);
730 refdst_drop(orig_dst);
732 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
733 ovs_vport_name(vport), ntohs(ethertype), mru,
739 static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
742 WARN_ONCE(1, "Fragment unavailable ->%s: eth=%04x, MRU=%d, MTU=%d.",
743 ovs_vport_name(vport), ntohs(ethertype), mru,
749 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
750 struct sw_flow_key *key)
752 struct vport *vport = ovs_vport_rcu(dp, out_port);
755 u16 mru = OVS_CB(skb)->mru;
757 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
758 ovs_vport_send(vport, skb);
759 } else if (mru <= vport->dev->mtu) {
760 __be16 ethertype = key->eth.type;
762 if (!is_flow_key_valid(key)) {
763 if (eth_p_mpls(skb->protocol))
764 ethertype = ovs_skb_get_inner_protocol(skb);
766 ethertype = vlan_get_protocol(skb);
769 ovs_fragment(vport, skb, mru, ethertype);
771 OVS_NLERR(true, "Cannot fragment IP frames");
778 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
779 struct sw_flow_key *key, const struct nlattr *attr,
780 const struct nlattr *actions, int actions_len)
782 struct ip_tunnel_info info;
783 struct dp_upcall_info upcall;
784 const struct nlattr *a;
787 memset(&upcall, 0, sizeof(upcall));
788 upcall.cmd = OVS_PACKET_CMD_ACTION;
789 upcall.mru = OVS_CB(skb)->mru;
791 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
792 a = nla_next(a, &rem)) {
793 switch (nla_type(a)) {
794 case OVS_USERSPACE_ATTR_USERDATA:
798 case OVS_USERSPACE_ATTR_PID:
799 upcall.portid = nla_get_u32(a);
802 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
803 /* Get out tunnel info. */
806 vport = ovs_vport_rcu(dp, nla_get_u32(a));
810 upcall.egress_tun_info = &info;
811 err = ovs_vport_get_egress_tun_info(vport, skb,
814 upcall.egress_tun_info = NULL;
820 case OVS_USERSPACE_ATTR_ACTIONS: {
821 /* Include actions. */
822 upcall.actions = actions;
823 upcall.actions_len = actions_len;
827 } /* End of switch. */
830 return ovs_dp_upcall(dp, skb, key, &upcall);
833 static int sample(struct datapath *dp, struct sk_buff *skb,
834 struct sw_flow_key *key, const struct nlattr *attr,
835 const struct nlattr *actions, int actions_len)
837 const struct nlattr *acts_list = NULL;
838 const struct nlattr *a;
841 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
842 a = nla_next(a, &rem)) {
845 switch (nla_type(a)) {
846 case OVS_SAMPLE_ATTR_PROBABILITY:
847 probability = nla_get_u32(a);
848 if (!probability || prandom_u32() > probability)
852 case OVS_SAMPLE_ATTR_ACTIONS:
858 rem = nla_len(acts_list);
859 a = nla_data(acts_list);
861 /* Actions list is empty, do nothing */
865 /* The only known usage of sample action is having a single user-space
866 * action. Treat this usage as a special case.
867 * The output_userspace() should clone the skb to be sent to the
868 * user space. This skb will be consumed by its caller.
870 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
871 nla_is_last(a, rem)))
872 return output_userspace(dp, skb, key, a, actions, actions_len);
874 skb = skb_clone(skb, GFP_ATOMIC);
876 /* Skip the sample action when out of memory. */
879 if (!add_deferred_actions(skb, key, a)) {
881 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
889 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
890 const struct nlattr *attr)
892 struct ovs_action_hash *hash_act = nla_data(attr);
895 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
896 hash = skb_get_hash(skb);
897 hash = jhash_1word(hash, hash_act->hash_basis);
901 key->ovs_flow_hash = hash;
904 static int execute_set_action(struct sk_buff *skb,
905 struct sw_flow_key *flow_key,
906 const struct nlattr *a)
908 /* Only tunnel set execution is supported without a mask. */
909 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
910 struct ovs_tunnel_info *tun = nla_data(a);
912 ovs_skb_dst_drop(skb);
913 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
914 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
921 /* Mask is at the midpoint of the data. */
922 #define get_mask(a, type) ((const type)nla_data(a) + 1)
924 static int execute_masked_set_action(struct sk_buff *skb,
925 struct sw_flow_key *flow_key,
926 const struct nlattr *a)
930 switch (nla_type(a)) {
931 case OVS_KEY_ATTR_PRIORITY:
932 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
933 *get_mask(a, u32 *));
934 flow_key->phy.priority = skb->priority;
937 case OVS_KEY_ATTR_SKB_MARK:
938 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
939 flow_key->phy.skb_mark = skb->mark;
942 case OVS_KEY_ATTR_TUNNEL_INFO:
943 /* Masked data not supported for tunnel. */
947 case OVS_KEY_ATTR_ETHERNET:
948 err = set_eth_addr(skb, flow_key, nla_data(a),
949 get_mask(a, struct ovs_key_ethernet *));
952 case OVS_KEY_ATTR_IPV4:
953 err = set_ipv4(skb, flow_key, nla_data(a),
954 get_mask(a, struct ovs_key_ipv4 *));
957 case OVS_KEY_ATTR_IPV6:
958 err = set_ipv6(skb, flow_key, nla_data(a),
959 get_mask(a, struct ovs_key_ipv6 *));
962 case OVS_KEY_ATTR_TCP:
963 err = set_tcp(skb, flow_key, nla_data(a),
964 get_mask(a, struct ovs_key_tcp *));
967 case OVS_KEY_ATTR_UDP:
968 err = set_udp(skb, flow_key, nla_data(a),
969 get_mask(a, struct ovs_key_udp *));
972 case OVS_KEY_ATTR_SCTP:
973 err = set_sctp(skb, flow_key, nla_data(a),
974 get_mask(a, struct ovs_key_sctp *));
977 case OVS_KEY_ATTR_MPLS:
978 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
982 case OVS_KEY_ATTR_CT_STATE:
983 case OVS_KEY_ATTR_CT_ZONE:
991 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
992 struct sw_flow_key *key,
993 const struct nlattr *a, int rem)
995 struct deferred_action *da;
997 if (!is_flow_key_valid(key)) {
1000 err = ovs_flow_key_update(skb, key);
1004 BUG_ON(!is_flow_key_valid(key));
1006 if (!nla_is_last(a, rem)) {
1007 /* Recirc action is the not the last action
1008 * of the action list, need to clone the skb.
1010 skb = skb_clone(skb, GFP_ATOMIC);
1012 /* Skip the recirc action when out of memory, but
1013 * continue on with the rest of the action list.
1019 da = add_deferred_actions(skb, key, NULL);
1021 da->pkt_key.recirc_id = nla_get_u32(a);
1025 if (net_ratelimit())
1026 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1033 /* Execute a list of actions against 'skb'. */
1034 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
1035 struct sw_flow_key *key,
1036 const struct nlattr *attr, int len)
1038 /* Every output action needs a separate clone of 'skb', but the common
1039 * case is just a single output action, so that doing a clone and
1040 * then freeing the original skbuff is wasteful. So the following code
1041 * is slightly obscure just to avoid that.
1044 const struct nlattr *a;
1047 for (a = attr, rem = len; rem > 0;
1048 a = nla_next(a, &rem)) {
1051 if (unlikely(prev_port != -1)) {
1052 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1055 do_output(dp, out_skb, prev_port, key);
1060 switch (nla_type(a)) {
1061 case OVS_ACTION_ATTR_OUTPUT:
1062 prev_port = nla_get_u32(a);
1065 case OVS_ACTION_ATTR_USERSPACE:
1066 output_userspace(dp, skb, key, a, attr, len);
1069 case OVS_ACTION_ATTR_HASH:
1070 execute_hash(skb, key, a);
1073 case OVS_ACTION_ATTR_PUSH_MPLS:
1074 err = push_mpls(skb, key, nla_data(a));
1077 case OVS_ACTION_ATTR_POP_MPLS:
1078 err = pop_mpls(skb, key, nla_get_be16(a));
1081 case OVS_ACTION_ATTR_PUSH_VLAN:
1082 err = push_vlan(skb, key, nla_data(a));
1085 case OVS_ACTION_ATTR_POP_VLAN:
1086 err = pop_vlan(skb, key);
1089 case OVS_ACTION_ATTR_RECIRC:
1090 err = execute_recirc(dp, skb, key, a, rem);
1091 if (nla_is_last(a, rem)) {
1092 /* If this is the last action, the skb has
1093 * been consumed or freed.
1094 * Return immediately.
1100 case OVS_ACTION_ATTR_SET:
1101 err = execute_set_action(skb, key, nla_data(a));
1104 case OVS_ACTION_ATTR_SET_MASKED:
1105 case OVS_ACTION_ATTR_SET_TO_MASKED:
1106 err = execute_masked_set_action(skb, key, nla_data(a));
1109 case OVS_ACTION_ATTR_SAMPLE:
1110 err = sample(dp, skb, key, a, attr, len);
1113 case OVS_ACTION_ATTR_CT:
1114 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1117 /* Hide stolen IP fragments from user space. */
1118 if (err == -EINPROGRESS)
1123 if (unlikely(err)) {
1129 if (prev_port != -1)
1130 do_output(dp, skb, prev_port, key);
1137 static void process_deferred_actions(struct datapath *dp)
1139 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1141 /* Do not touch the FIFO in case there is no deferred actions. */
1142 if (action_fifo_is_empty(fifo))
1145 /* Finishing executing all deferred actions. */
1147 struct deferred_action *da = action_fifo_get(fifo);
1148 struct sk_buff *skb = da->skb;
1149 struct sw_flow_key *key = &da->pkt_key;
1150 const struct nlattr *actions = da->actions;
1153 do_execute_actions(dp, skb, key, actions,
1156 ovs_dp_process_packet(skb, key);
1157 } while (!action_fifo_is_empty(fifo));
1159 /* Reset FIFO for the next packet. */
1160 action_fifo_init(fifo);
1163 /* Execute a list of actions against 'skb'. */
1164 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
1165 const struct sw_flow_actions *acts,
1166 struct sw_flow_key *key)
1168 int level = this_cpu_read(exec_actions_level);
1171 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
1172 if (net_ratelimit())
1173 pr_warn("%s: packet loop detected, dropping.\n",
1180 this_cpu_inc(exec_actions_level);
1181 err = do_execute_actions(dp, skb, key,
1182 acts->actions, acts->actions_len);
1185 process_deferred_actions(dp);
1187 this_cpu_dec(exec_actions_level);
1189 /* This return status currently does not reflect the errors
1190 * encounted during deferred actions execution. Probably needs to
1191 * be fixed in the future.
1196 int action_fifos_init(void)
1198 action_fifos = alloc_percpu(struct action_fifo);
1205 void action_fifos_exit(void)
1207 free_percpu(action_fifos);