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