2 * Copyright (c) 2007-2013 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
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/if_vlan.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/ip6_tunnel.h>
34 #include <net/route.h>
39 #include "vport-netdev.h"
41 #ifndef USE_UPSTREAM_TUNNEL
42 void rpl_iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
43 __be32 src, __be32 dst, __u8 proto, __u8 tos, __u8 ttl,
46 struct net_device *dev = skb->dev;
47 int pkt_len = skb->len - skb_inner_network_offset(skb);
51 skb_scrub_packet(skb, xnet);
54 skb_dst_set(skb, &rt->dst);
57 /* Do not clear ovs_skb_cb. It will be done in gso code. */
58 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
61 /* Push down and install the IP header. */
62 __skb_push(skb, sizeof(struct iphdr));
63 skb_reset_network_header(skb);
68 iph->ihl = sizeof(struct iphdr) >> 2;
70 iph->protocol = proto;
76 #ifdef HAVE_IP_SELECT_IDENT_USING_DST_ENTRY
77 __ip_select_ident(iph, &rt->dst, (skb_shinfo(skb)->gso_segs ?: 1) - 1);
78 #elif defined(HAVE_IP_SELECT_IDENT_USING_NET)
79 __ip_select_ident(dev_net(rt->dst.dev), iph,
80 skb_shinfo(skb)->gso_segs ?: 1);
82 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
85 err = ip_local_out(dev_net(rt->dst.dev), sk, skb);
86 if (unlikely(net_xmit_eval(err)))
88 iptunnel_xmit_stats(dev, pkt_len);
90 EXPORT_SYMBOL_GPL(rpl_iptunnel_xmit);
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 *))
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;
103 } else if (skb_is_gso(skb)) {
108 if (skb_is_gso(skb)) {
109 err = skb_unclone(skb, GFP_ATOMIC);
112 skb_shinfo(skb)->gso_type |= gso_type_mask;
114 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
118 OVS_GSO_CB(skb)->fix_segment = fix_segment;
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.
130 skb->encapsulation = 0;
133 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
134 err = skb_checksum_help(skb);
137 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
138 skb->ip_summed = CHECKSUM_NONE;
144 EXPORT_SYMBOL_GPL(ovs_iptunnel_handle_offloads);
146 int rpl___iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
147 __be16 inner_proto, bool raw_proto, bool xnet)
149 if (unlikely(!pskb_may_pull(skb, hdr_len)))
152 skb_pull_rcsum(skb, hdr_len);
154 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
157 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
160 eh = (struct ethhdr *)skb->data;
161 if (likely(eth_proto_is_802_3(eh->h_proto)))
162 skb->protocol = eh->h_proto;
164 skb->protocol = htons(ETH_P_802_2);
167 skb->protocol = inner_proto;
170 skb_clear_hash_if_not_l4(skb);
172 skb_set_queue_mapping(skb, 0);
173 skb_scrub_packet(skb, xnet);
175 return iptunnel_pull_offloads(skb);
177 EXPORT_SYMBOL_GPL(rpl___iptunnel_pull_header);
178 #endif /* USE_UPSTREAM_TUNNEL */
180 bool ovs_skb_is_encapsulated(struct sk_buff *skb)
182 /* checking for inner protocol should be sufficient on newer kernel, but
183 * old kernel just set encapsulation bit.
185 return ovs_skb_get_inner_protocol(skb) || skb->encapsulation;
187 EXPORT_SYMBOL_GPL(ovs_skb_is_encapsulated);
189 /* derived from ip_tunnel_rcv(). */
190 void ovs_ip_tunnel_rcv(struct net_device *dev, struct sk_buff *skb,
191 struct metadata_dst *tun_dst)
193 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
194 struct pcpu_sw_netstats *tstats;
196 tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats);
197 u64_stats_update_begin(&tstats->syncp);
198 tstats->rx_packets++;
199 tstats->rx_bytes += skb->len;
200 u64_stats_update_end(&tstats->syncp);
203 skb_reset_mac_header(skb);
204 skb_scrub_packet(skb, false);
205 skb->protocol = eth_type_trans(skb, dev);
206 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
208 ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
210 #ifndef USE_UPSTREAM_TUNNEL
211 netdev_port_receive(skb, &tun_dst->u.tun_info);
217 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
218 #ifndef HAVE_PCPU_SW_NETSTATS
219 #define netdev_stats_to_stats64 rpl_netdev_stats_to_stats64
220 static void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
221 const struct net_device_stats *netdev_stats)
223 #if BITS_PER_LONG == 64
224 BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
225 memcpy(stats64, netdev_stats, sizeof(*stats64));
227 size_t i, n = sizeof(*stats64) / sizeof(u64);
228 const unsigned long *src = (const unsigned long *)netdev_stats;
229 u64 *dst = (u64 *)stats64;
231 BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
232 sizeof(*stats64) / sizeof(u64));
233 for (i = 0; i < n; i++)
238 struct rtnl_link_stats64 *rpl_ip_tunnel_get_stats64(struct net_device *dev,
239 struct rtnl_link_stats64 *tot)
243 netdev_stats_to_stats64(tot, &dev->stats);
245 for_each_possible_cpu(i) {
246 const struct pcpu_sw_netstats *tstats =
247 per_cpu_ptr((struct pcpu_sw_netstats __percpu *)dev->tstats, i);
248 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
252 start = u64_stats_fetch_begin_irq(&tstats->syncp);
253 rx_packets = tstats->rx_packets;
254 tx_packets = tstats->tx_packets;
255 rx_bytes = tstats->rx_bytes;
256 tx_bytes = tstats->tx_bytes;
257 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
259 tot->rx_packets += rx_packets;
260 tot->tx_packets += tx_packets;
261 tot->rx_bytes += rx_bytes;
262 tot->tx_bytes += tx_bytes;
269 void rpl_ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
270 struct net_device *dev)
274 pkt_len = skb->len - skb_inner_network_offset(skb);
275 #ifdef HAVE_IP6_LOCAL_OUT_SK
276 err = ip6_local_out_sk(sk, skb);
278 err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
280 if (net_xmit_eval(err))
283 iptunnel_xmit_stats(dev, pkt_len);
285 EXPORT_SYMBOL_GPL(rpl_ip6tunnel_xmit);