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