Skip to content

Commit 57c67ff

Browse files
Tom Herbertdavem330
authored andcommitted
udp: additional GRO support
Implement GRO for UDPv6. Add UDP checksum verification in gro_receive for both UDP4 and UDP6 calling skb_gro_checksum_validate_zero_check. Signed-off-by: Tom Herbert <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 149d077 commit 57c67ff

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

include/net/udp.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ static inline __sum16 udp_v4_check(int len, __be32 saddr,
158158
void udp_set_csum(bool nocheck, struct sk_buff *skb,
159159
__be32 saddr, __be32 daddr, int len);
160160

161+
struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
162+
struct udphdr *uh);
163+
int udp_gro_complete(struct sk_buff *skb, int nhoff);
164+
165+
static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
166+
{
167+
struct udphdr *uh;
168+
unsigned int hlen, off;
169+
170+
off = skb_gro_offset(skb);
171+
hlen = off + sizeof(*uh);
172+
uh = skb_gro_header_fast(skb, off);
173+
if (skb_gro_header_hard(skb, hlen))
174+
uh = skb_gro_header_slow(skb, hlen, off);
175+
176+
return uh;
177+
}
178+
161179
/* hash routines shared between UDPv4/6 and UDP-Litev4/6 */
162180
static inline void udp_lib_hash(struct sock *sk)
163181
{

net/ipv4/udp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#include <linux/slab.h>
100100
#include <net/tcp_states.h>
101101
#include <linux/skbuff.h>
102+
#include <linux/netdevice.h>
102103
#include <linux/proc_fs.h>
103104
#include <linux/seq_file.h>
104105
#include <net/net_namespace.h>

net/ipv4/udp_offload.c

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,22 @@ void udp_del_offload(struct udp_offload *uo)
228228
}
229229
EXPORT_SYMBOL(udp_del_offload);
230230

231-
static struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
231+
struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
232+
struct udphdr *uh)
232233
{
233234
struct udp_offload_priv *uo_priv;
234235
struct sk_buff *p, **pp = NULL;
235-
struct udphdr *uh, *uh2;
236-
unsigned int hlen, off;
236+
struct udphdr *uh2;
237+
unsigned int off = skb_gro_offset(skb);
237238
int flush = 1;
238239

239240
if (NAPI_GRO_CB(skb)->udp_mark ||
240-
(!skb->encapsulation && skb->ip_summed != CHECKSUM_COMPLETE))
241+
(!skb->encapsulation && !NAPI_GRO_CB(skb)->csum_valid))
241242
goto out;
242243

243244
/* mark that this skb passed once through the udp gro layer */
244245
NAPI_GRO_CB(skb)->udp_mark = 1;
245-
246-
off = skb_gro_offset(skb);
247-
hlen = off + sizeof(*uh);
248-
uh = skb_gro_header_fast(skb, off);
249-
if (skb_gro_header_hard(skb, hlen)) {
250-
uh = skb_gro_header_slow(skb, hlen, off);
251-
if (unlikely(!uh))
252-
goto out;
253-
}
246+
NAPI_GRO_CB(skb)->encapsulation++;
254247

255248
rcu_read_lock();
256249
uo_priv = rcu_dereference(udp_offload_base);
@@ -269,7 +262,12 @@ static struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *s
269262
continue;
270263

271264
uh2 = (struct udphdr *)(p->data + off);
272-
if ((*(u32 *)&uh->source != *(u32 *)&uh2->source)) {
265+
266+
/* Match ports and either checksums are either both zero
267+
* or nonzero.
268+
*/
269+
if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
270+
(!uh->check ^ !uh2->check)) {
273271
NAPI_GRO_CB(p)->same_flow = 0;
274272
continue;
275273
}
@@ -286,7 +284,24 @@ static struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *s
286284
return pp;
287285
}
288286

289-
static int udp_gro_complete(struct sk_buff *skb, int nhoff)
287+
static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
288+
struct sk_buff *skb)
289+
{
290+
struct udphdr *uh = udp_gro_udphdr(skb);
291+
292+
/* Don't bother verifying checksum if we're going to flush anyway. */
293+
if (unlikely(!uh) ||
294+
(!NAPI_GRO_CB(skb)->flush &&
295+
skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
296+
inet_gro_compute_pseudo))) {
297+
NAPI_GRO_CB(skb)->flush = 1;
298+
return NULL;
299+
}
300+
301+
return udp_gro_receive(head, skb, uh);
302+
}
303+
304+
int udp_gro_complete(struct sk_buff *skb, int nhoff)
290305
{
291306
struct udp_offload_priv *uo_priv;
292307
__be16 newlen = htons(skb->len - nhoff);
@@ -311,12 +326,24 @@ static int udp_gro_complete(struct sk_buff *skb, int nhoff)
311326
return err;
312327
}
313328

329+
int udp4_gro_complete(struct sk_buff *skb, int nhoff)
330+
{
331+
const struct iphdr *iph = ip_hdr(skb);
332+
struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
333+
334+
if (uh->check)
335+
uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
336+
iph->daddr, 0);
337+
338+
return udp_gro_complete(skb, nhoff);
339+
}
340+
314341
static const struct net_offload udpv4_offload = {
315342
.callbacks = {
316343
.gso_send_check = udp4_ufo_send_check,
317344
.gso_segment = udp4_ufo_fragment,
318-
.gro_receive = udp_gro_receive,
319-
.gro_complete = udp_gro_complete,
345+
.gro_receive = udp4_gro_receive,
346+
.gro_complete = udp4_gro_complete,
320347
},
321348
};
322349

net/ipv6/udp_offload.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* UDPv6 GSO support
1111
*/
1212
#include <linux/skbuff.h>
13+
#include <linux/netdevice.h>
1314
#include <net/protocol.h>
1415
#include <net/ipv6.h>
1516
#include <net/udp.h>
@@ -127,10 +128,42 @@ static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
127128
out:
128129
return segs;
129130
}
131+
132+
static struct sk_buff **udp6_gro_receive(struct sk_buff **head,
133+
struct sk_buff *skb)
134+
{
135+
struct udphdr *uh = udp_gro_udphdr(skb);
136+
137+
/* Don't bother verifying checksum if we're going to flush anyway. */
138+
if (unlikely(!uh) ||
139+
(!NAPI_GRO_CB(skb)->flush &&
140+
skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
141+
ip6_gro_compute_pseudo))) {
142+
NAPI_GRO_CB(skb)->flush = 1;
143+
return NULL;
144+
}
145+
146+
return udp_gro_receive(head, skb, uh);
147+
}
148+
149+
int udp6_gro_complete(struct sk_buff *skb, int nhoff)
150+
{
151+
const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
152+
struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
153+
154+
if (uh->check)
155+
uh->check = ~udp_v6_check(skb->len - nhoff, &ipv6h->saddr,
156+
&ipv6h->daddr, 0);
157+
158+
return udp_gro_complete(skb, nhoff);
159+
}
160+
130161
static const struct net_offload udpv6_offload = {
131162
.callbacks = {
132163
.gso_send_check = udp6_ufo_send_check,
133164
.gso_segment = udp6_ufo_fragment,
165+
.gro_receive = udp6_gro_receive,
166+
.gro_complete = udp6_gro_complete,
134167
},
135168
};
136169

0 commit comments

Comments
 (0)