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
19 #include <linux/version.h>
21 #include <linux/module.h>
23 #include <linux/if_tunnel.h>
24 #include <linux/if_vlan.h>
25 #include <linux/icmp.h>
28 #include <linux/kernel.h>
29 #include <linux/kmod.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
37 #include <net/protocol.h>
38 #include <net/route.h>
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) && \
45 !defined(HAVE_VLAN_BUG_WORKAROUND)
46 #include <linux/module.h>
48 static int vlan_tso __read_mostly;
49 module_param(vlan_tso, int, 0644);
50 MODULE_PARM_DESC(vlan_tso, "Enable TSO for VLAN packets");
55 #ifdef OVS_USE_COMPAT_GSO_SEGMENTATION
56 static bool dev_supports_vlan_tx(struct net_device *dev)
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37)
60 #elif defined(HAVE_VLAN_BUG_WORKAROUND)
61 return dev->features & NETIF_F_HW_VLAN_TX;
63 /* Assume that the driver is buggy. */
68 /* Strictly this is not needed and will be optimised out
69 * as this code is guarded by if LINUX_VERSION_CODE < KERNEL_VERSION(3,19,0).
70 * It is here to make things explicit should the compatibility
71 * code be extended in some way prior extending its life-span
74 static bool supports_mpls_gso(void)
76 /* MPLS GSO was introduced in v3.11, however it was not correctly
77 * activated using mpls_features until v3.19. */
78 #ifdef OVS_USE_COMPAT_GSO_SEGMENTATION
85 int rpl_dev_queue_xmit(struct sk_buff *skb)
93 /* Avoid traversing any VLAN tags that are present to determine if
94 * the ethtype is MPLS. Instead compare the mac_len (end of L2) and
95 * skb_network_offset() (beginning of L3) whose inequality will
96 * indicate the presence of an MPLS label stack. */
97 if (skb->mac_len != skb_network_offset(skb) && !supports_mpls_gso())
100 if (skb_vlan_tag_present(skb) && !dev_supports_vlan_tx(skb->dev))
106 features = netif_skb_features(skb);
110 features &= ~(NETIF_F_TSO | NETIF_F_TSO6 |
111 NETIF_F_UFO | NETIF_F_FSO);
113 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
114 skb_vlan_tag_get(skb));
117 vlan_set_tci(skb, 0);
120 /* As of v3.11 the kernel provides an mpls_features field in
121 * struct net_device which allows devices to advertise which
122 * features its supports for MPLS. This value defaults to
123 * NETIF_F_SG and as of v3.19.
125 * This compatibility code is intended for kernels older
126 * than v3.19 that do not support MPLS GSO and do not
127 * use mpls_features. Thus this code uses NETIF_F_SG
128 * directly in place of mpls_features.
131 features &= NETIF_F_SG;
133 if (netif_needs_gso(skb->dev, skb, features)) {
134 struct sk_buff *nskb;
136 nskb = skb_gso_segment(skb, features);
138 if (unlikely(skb_cloned(skb) &&
139 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
142 skb_shinfo(skb)->gso_type &= ~SKB_GSO_DODGY;
156 err = dev_queue_xmit(skb);
164 return dev_queue_xmit(skb);
170 EXPORT_SYMBOL_GPL(rpl_dev_queue_xmit);
171 #endif /* OVS_USE_COMPAT_GSO_SEGMENTATION */
173 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
174 static __be16 __skb_network_protocol(struct sk_buff *skb)
176 __be16 type = skb->protocol;
177 int vlan_depth = ETH_HLEN;
179 while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) {
182 if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN)))
185 vh = (struct vlan_hdr *)(skb->data + vlan_depth);
186 type = vh->h_vlan_encapsulated_proto;
187 vlan_depth += VLAN_HLEN;
190 if (eth_p_mpls(type))
191 type = ovs_skb_get_inner_protocol(skb);
196 static struct sk_buff *tnl_skb_gso_segment(struct sk_buff *skb,
197 netdev_features_t features,
200 struct iphdr *iph = ip_hdr(skb);
201 int pkt_hlen = skb_inner_network_offset(skb); /* inner l2 + tunnel hdr. */
202 int mac_offset = skb_inner_mac_offset(skb);
203 struct sk_buff *skb1 = skb;
204 struct sk_buff *segs;
205 __be16 proto = skb->protocol;
206 char cb[sizeof(skb->cb)];
208 /* setup whole inner packet to get protocol. */
209 __skb_pull(skb, mac_offset);
210 skb->protocol = __skb_network_protocol(skb);
212 /* setup l3 packet to gso, to get around segmentation bug on older kernel.*/
213 __skb_pull(skb, (pkt_hlen - mac_offset));
214 skb_reset_mac_header(skb);
215 skb_reset_network_header(skb);
216 skb_reset_transport_header(skb);
218 /* From 3.9 kernel skb->cb is used by skb gso. Therefore
219 * make copy of it to restore it back. */
220 memcpy(cb, skb->cb, sizeof(cb));
222 /* We are handling offloads by segmenting l3 packet, so
223 * no need to call OVS compat segmentation function. */
225 #ifdef HAVE___SKB_GSO_SEGMENT
226 #undef __skb_gso_segment
227 segs = __skb_gso_segment(skb, 0, tx_path);
229 #undef skb_gso_segment
230 segs = skb_gso_segment(skb, 0);
233 if (!segs || IS_ERR(segs))
238 __skb_push(skb, pkt_hlen);
239 skb_reset_mac_header(skb);
240 skb_reset_network_header(skb);
241 skb_set_transport_header(skb, sizeof(struct iphdr));
244 memcpy(ip_hdr(skb), iph, pkt_hlen);
245 memcpy(skb->cb, cb, sizeof(cb));
246 OVS_GSO_CB(skb)->fix_segment(skb);
248 skb->protocol = proto;
256 static int output_ip(struct sk_buff *skb)
258 int ret = NETDEV_TX_OK;
261 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
264 err = ip_local_out(skb);
265 if (unlikely(net_xmit_eval(err)))
271 int rpl_ip_local_out(struct sk_buff *skb)
273 int ret = NETDEV_TX_OK;
276 if (!OVS_GSO_CB(skb)->fix_segment)
277 return output_ip(skb);
279 if (skb_is_gso(skb)) {
284 skb = tnl_skb_gso_segment(skb, 0, false);
285 if (!skb || IS_ERR(skb))
287 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
290 err = skb_checksum_help(skb);
296 struct sk_buff *next_skb = skb->next;
303 iph->id = htons(id++);
305 ret = output_ip(skb);
310 EXPORT_SYMBOL_GPL(rpl_ip_local_out);