65b5aea6481780f7cd6e7614e15e60927ea950fc
[cascardo/ovs.git] / datapath / linux / compat / ip_tunnels_core.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
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.
7  *
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.
12  *
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
16  * 02110-1301, USA
17  */
18
19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/if_vlan.h>
23 #include <linux/in.h>
24 #include <linux/in_route.h>
25 #include <linux/inetdevice.h>
26 #include <linux/jhash.h>
27 #include <linux/list.h>
28 #include <linux/kernel.h>
29 #include <linux/version.h>
30 #include <linux/workqueue.h>
31 #include <linux/rculist.h>
32 #include <net/ip_tunnels.h>
33 #include <net/route.h>
34 #include <net/xfrm.h>
35
36 #include "compat.h"
37 #include "gso.h"
38 #include "vport-netdev.h"
39
40 #ifndef USE_UPSTREAM_TUNNEL
41 void rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
42                       __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
43                       __be16 df, bool xnet)
44 {
45         struct net_device *dev = skb->dev;
46         int pkt_len = skb->len - skb_inner_network_offset(skb);
47         struct iphdr *iph;
48         int err;
49
50         skb_scrub_packet(skb, xnet);
51
52         skb_clear_hash(skb);
53         skb_dst_set(skb, &rt->dst);
54
55 #if 0
56         /* Do not clear ovs_skb_cb.  It will be done in gso code. */
57         memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
58 #endif
59
60         /* Push down and install the IP header. */
61         __skb_push(skb, sizeof(struct iphdr));
62         skb_reset_network_header(skb);
63
64         iph = ip_hdr(skb);
65
66         iph->version    =       4;
67         iph->ihl        =       sizeof(struct iphdr) >> 2;
68         iph->frag_off   =       df;
69         iph->protocol   =       proto;
70         iph->tos        =       tos;
71         iph->daddr      =       dst;
72         iph->saddr      =       src;
73         iph->ttl        =       ttl;
74
75 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
76         __ip_select_ident(iph, &rt->dst, (skb_shinfo(skb)->gso_segs ?: 1) - 1);
77 #elif defined(HAVE_IP_SELECT_IDENT_USING_NET)
78         __ip_select_ident(dev_net(rt->dst.dev), iph,
79                           skb_shinfo(skb)->gso_segs ?: 1);
80 #else
81         __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
82 #endif
83
84         err = ip_local_out(skb);
85         if (unlikely(net_xmit_eval(err)))
86                 pkt_len = 0;
87         iptunnel_xmit_stats(dev, pkt_len);
88 }
89 EXPORT_SYMBOL_GPL(rpl_iptunnel_xmit);
90
91 int ovs_iptunnel_handle_offloads(struct sk_buff *skb,
92                                  bool csum_help, int gso_type_mask,
93                                  void (*fix_segment)(struct sk_buff *))
94 {
95         int err;
96
97         if (likely(!skb_is_encapsulated(skb))) {
98                 skb_reset_inner_headers(skb);
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
100                 skb->encapsulation = 1;
101 #endif
102         } else if (skb_is_gso(skb)) {
103                 err = -ENOSYS;
104                 goto error;
105         }
106
107 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
108         if (gso_type_mask)
109                 fix_segment = NULL;
110
111         OVS_GSO_CB(skb)->fix_segment = fix_segment;
112 #endif
113         if (skb_is_gso(skb)) {
114                 err = skb_unclone(skb, GFP_ATOMIC);
115                 if (unlikely(err))
116                         goto error;
117                 skb_shinfo(skb)->gso_type |= gso_type_mask;
118                 return 0;
119         }
120
121 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
122         /* If packet is not gso and we are resolving any partial checksum,
123          * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
124          * on the outer header without confusing devices that implement
125          * NETIF_F_IP_CSUM with encapsulation.
126          */
127         if (csum_help)
128                 skb->encapsulation = 0;
129 #endif
130
131         if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
132                 err = skb_checksum_help(skb);
133                 if (unlikely(err))
134                         goto error;
135         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
136                 skb->ip_summed = CHECKSUM_NONE;
137
138         return 0;
139 error:
140         return err;
141 }
142 EXPORT_SYMBOL_GPL(ovs_iptunnel_handle_offloads);
143
144 int rpl___iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
145                                __be16 inner_proto, bool raw_proto, bool xnet)
146 {
147         if (unlikely(!pskb_may_pull(skb, hdr_len)))
148                 return -ENOMEM;
149
150         skb_pull_rcsum(skb, hdr_len);
151
152         if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
153                 struct ethhdr *eh;
154
155                 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
156                         return -ENOMEM;
157
158                 eh = (struct ethhdr *)skb->data;
159                 if (likely(eth_proto_is_802_3(eh->h_proto)))
160                         skb->protocol = eh->h_proto;
161                 else
162                         skb->protocol = htons(ETH_P_802_2);
163
164         } else {
165                 skb->protocol = inner_proto;
166         }
167
168         skb_clear_hash_if_not_l4(skb);
169         skb->vlan_tci = 0;
170         skb_set_queue_mapping(skb, 0);
171         skb_scrub_packet(skb, xnet);
172
173         return iptunnel_pull_offloads(skb);
174 }
175 EXPORT_SYMBOL_GPL(rpl___iptunnel_pull_header);
176 #endif /* USE_UPSTREAM_TUNNEL */
177
178 bool ovs_skb_is_encapsulated(struct sk_buff *skb)
179 {
180         /* checking for inner protocol should be sufficient on newer kernel, but
181          * old kernel just set encapsulation bit.
182          */
183         return ovs_skb_get_inner_protocol(skb) || skb->encapsulation;
184 }
185 EXPORT_SYMBOL_GPL(ovs_skb_is_encapsulated);
186
187 /* derived from ip_tunnel_rcv(). */
188 void ovs_ip_tunnel_rcv(struct net_device *dev, struct sk_buff *skb,
189                        struct metadata_dst *tun_dst)
190 {
191 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
192         struct pcpu_sw_netstats *tstats;
193
194         tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats);
195         u64_stats_update_begin(&tstats->syncp);
196         tstats->rx_packets++;
197         tstats->rx_bytes += skb->len;
198         u64_stats_update_end(&tstats->syncp);
199 #endif
200
201         skb_reset_mac_header(skb);
202         skb_scrub_packet(skb, false);
203         skb->protocol = eth_type_trans(skb, dev);
204         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
205
206         ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
207
208 #ifndef USE_UPSTREAM_TUNNEL
209         netdev_port_receive(skb, &tun_dst->u.tun_info);
210 #else
211         netif_rx(skb);
212 #endif
213 }
214
215 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
216 #ifndef HAVE_PCPU_SW_NETSTATS
217 #define netdev_stats_to_stats64 rpl_netdev_stats_to_stats64
218 static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
219                                     const struct net_device_stats *netdev_stats)
220 {
221 #if BITS_PER_LONG == 64
222         BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
223         memcpy(stats64, netdev_stats, sizeof(*stats64));
224 #else
225         size_t i, n = sizeof(*stats64) / sizeof(u64);
226         const unsigned long *src = (const unsigned long *)netdev_stats;
227         u64 *dst = (u64 *)stats64;
228
229         BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
230                      sizeof(*stats64) / sizeof(u64));
231         for (i = 0; i < n; i++)
232                 dst[i] = src[i];
233 #endif
234 }
235
236 struct rtnl_link_stats64 *rpl_ip_tunnel_get_stats64(struct net_device *dev,
237                                                 struct rtnl_link_stats64 *tot)
238 {
239         int i;
240
241         netdev_stats_to_stats64(tot, &dev->stats);
242
243         for_each_possible_cpu(i) {
244                 const struct pcpu_sw_netstats *tstats =
245                                                    per_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats, i);
246                 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
247                 unsigned int start;
248
249                 do {
250                         start = u64_stats_fetch_begin_irq(&tstats->syncp);
251                         rx_packets = tstats->rx_packets;
252                         tx_packets = tstats->tx_packets;
253                         rx_bytes = tstats->rx_bytes;
254                         tx_bytes = tstats->tx_bytes;
255                 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
256
257                 tot->rx_packets += rx_packets;
258                 tot->tx_packets += tx_packets;
259                 tot->rx_bytes   += rx_bytes;
260                 tot->tx_bytes   += tx_bytes;
261         }
262
263         return tot;
264 }
265 #endif
266 #endif