Skip to content

Commit a602456

Browse files
tomratbertdavem330
authored andcommitted
udp: Add GRO functions to UDP socket
This patch adds GRO functions (gro_receive and gro_complete) to UDP sockets. udp_gro_receive is changed to perform socket lookup on a packet. If a socket is found the related GRO functions are called. This features obsoletes using UDP offload infrastructure for GRO (udp_offload). This has the advantage of not being limited to provide offload on a per port basis, GRO is now applied to whatever individual UDP sockets are bound to. This also allows the possbility of "application defined GRO"-- that is we can attach something like a BPF program to a UDP socket to perfrom GRO on an application layer protocol. Signed-off-by: Tom Herbert <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6305830 commit a602456

File tree

8 files changed

+54
-42
lines changed

8 files changed

+54
-42
lines changed

include/linux/udp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ struct udp_sock {
7171
*/
7272
int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
7373
void (*encap_destroy)(struct sock *sk);
74+
75+
/* GRO functions for UDP socket */
76+
struct sk_buff ** (*gro_receive)(struct sock *sk,
77+
struct sk_buff **head,
78+
struct sk_buff *skb);
79+
int (*gro_complete)(struct sock *sk,
80+
struct sk_buff *skb,
81+
int nhoff);
7482
};
7583

7684
static inline struct udp_sock *udp_sk(const struct sock *sk)

include/net/udp.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,12 @@ static inline void udp_csum_pull_header(struct sk_buff *skb)
167167
UDP_SKB_CB(skb)->cscov -= sizeof(struct udphdr);
168168
}
169169

170+
typedef struct sock *(*udp_lookup_t)(struct sk_buff *skb, __be16 sport,
171+
__be16 dport);
172+
170173
struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
171-
struct udphdr *uh);
172-
int udp_gro_complete(struct sk_buff *skb, int nhoff);
174+
struct udphdr *uh, udp_lookup_t lookup);
175+
int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup);
173176

174177
static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
175178
{

net/ipv4/udp_offload.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
179179

180180
return segs;
181181
}
182+
EXPORT_SYMBOL(skb_udp_tunnel_segment);
182183

183184
static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
184185
netdev_features_t features)
@@ -304,13 +305,13 @@ void udp_del_offload(struct udp_offload *uo)
304305
EXPORT_SYMBOL(udp_del_offload);
305306

306307
struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
307-
struct udphdr *uh)
308+
struct udphdr *uh, udp_lookup_t lookup)
308309
{
309-
struct udp_offload_priv *uo_priv;
310310
struct sk_buff *p, **pp = NULL;
311311
struct udphdr *uh2;
312312
unsigned int off = skb_gro_offset(skb);
313313
int flush = 1;
314+
struct sock *sk;
314315

315316
if (NAPI_GRO_CB(skb)->encap_mark ||
316317
(skb->ip_summed != CHECKSUM_PARTIAL &&
@@ -322,13 +323,11 @@ struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
322323
NAPI_GRO_CB(skb)->encap_mark = 1;
323324

324325
rcu_read_lock();
325-
uo_priv = rcu_dereference(udp_offload_base);
326-
for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
327-
if (net_eq(read_pnet(&uo_priv->net), dev_net(skb->dev)) &&
328-
uo_priv->offload->port == uh->dest &&
329-
uo_priv->offload->callbacks.gro_receive)
330-
goto unflush;
331-
}
326+
sk = (*lookup)(skb, uh->source, uh->dest);
327+
328+
if (sk && udp_sk(sk)->gro_receive)
329+
goto unflush;
330+
332331
goto out_unlock;
333332

334333
unflush:
@@ -352,16 +351,15 @@ struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
352351

353352
skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
354353
skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
355-
NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
356-
pp = uo_priv->offload->callbacks.gro_receive(head, skb,
357-
uo_priv->offload);
354+
pp = udp_sk(sk)->gro_receive(sk, head, skb);
358355

359356
out_unlock:
360357
rcu_read_unlock();
361358
out:
362359
NAPI_GRO_CB(skb)->flush |= flush;
363360
return pp;
364361
}
362+
EXPORT_SYMBOL(udp_gro_receive);
365363

366364
static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
367365
struct sk_buff *skb)
@@ -383,39 +381,28 @@ static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
383381
inet_gro_compute_pseudo);
384382
skip:
385383
NAPI_GRO_CB(skb)->is_ipv6 = 0;
386-
return udp_gro_receive(head, skb, uh);
384+
return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
387385

388386
flush:
389387
NAPI_GRO_CB(skb)->flush = 1;
390388
return NULL;
391389
}
392390

393-
int udp_gro_complete(struct sk_buff *skb, int nhoff)
391+
int udp_gro_complete(struct sk_buff *skb, int nhoff,
392+
udp_lookup_t lookup)
394393
{
395-
struct udp_offload_priv *uo_priv;
396394
__be16 newlen = htons(skb->len - nhoff);
397395
struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
398396
int err = -ENOSYS;
397+
struct sock *sk;
399398

400399
uh->len = newlen;
401400

402401
rcu_read_lock();
403-
404-
uo_priv = rcu_dereference(udp_offload_base);
405-
for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
406-
if (net_eq(read_pnet(&uo_priv->net), dev_net(skb->dev)) &&
407-
uo_priv->offload->port == uh->dest &&
408-
uo_priv->offload->callbacks.gro_complete)
409-
break;
410-
}
411-
412-
if (uo_priv) {
413-
NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
414-
err = uo_priv->offload->callbacks.gro_complete(skb,
415-
nhoff + sizeof(struct udphdr),
416-
uo_priv->offload);
417-
}
418-
402+
sk = (*lookup)(skb, uh->source, uh->dest);
403+
if (sk && udp_sk(sk)->gro_complete)
404+
err = udp_sk(sk)->gro_complete(sk, skb,
405+
nhoff + sizeof(struct udphdr));
419406
rcu_read_unlock();
420407

421408
if (skb->remcsum_offload)
@@ -426,6 +413,7 @@ int udp_gro_complete(struct sk_buff *skb, int nhoff)
426413

427414
return err;
428415
}
416+
EXPORT_SYMBOL(udp_gro_complete);
429417

430418
static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
431419
{
@@ -440,7 +428,7 @@ static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
440428
skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
441429
}
442430

443-
return udp_gro_complete(skb, nhoff);
431+
return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
444432
}
445433

446434
static const struct net_offload udpv4_offload = {

net/ipv6/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ ipv6-objs := af_inet6.o anycast.o ip6_output.o ip6_input.o addrconf.o \
88
addrlabel.o \
99
route.o ip6_fib.o ipv6_sockglue.o ndisc.o udp.o udplite.o \
1010
raw.o icmp.o mcast.o reassembly.o tcp_ipv6.o ping.o \
11-
exthdrs.o datagram.o ip6_flowlabel.o inet6_connection_sock.o
11+
exthdrs.o datagram.o ip6_flowlabel.o inet6_connection_sock.o \
12+
udp_offload.o
1213

13-
ipv6-offload := ip6_offload.o tcpv6_offload.o udp_offload.o exthdrs_offload.o
14+
ipv6-offload := ip6_offload.o tcpv6_offload.o exthdrs_offload.o
1415

1516
ipv6-$(CONFIG_SYSCTL) = sysctl_net_ipv6.o
1617
ipv6-$(CONFIG_IPV6_MROUTE) += ip6mr.o

net/ipv6/af_inet6.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
#include <asm/uaccess.h>
6565
#include <linux/mroute6.h>
6666

67+
#include "ip6_offload.h"
68+
6769
MODULE_AUTHOR("Cast of dozens");
6870
MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
6971
MODULE_LICENSE("GPL");
@@ -959,6 +961,10 @@ static int __init inet6_init(void)
959961
if (err)
960962
goto udplitev6_fail;
961963

964+
err = udpv6_offload_init();
965+
if (err)
966+
goto udpv6_offload_fail;
967+
962968
err = tcpv6_init();
963969
if (err)
964970
goto tcpv6_fail;
@@ -988,6 +994,8 @@ static int __init inet6_init(void)
988994
ipv6_packet_fail:
989995
tcpv6_exit();
990996
tcpv6_fail:
997+
udpv6_offload_exit();
998+
udpv6_offload_fail:
991999
udplitev6_exit();
9921000
udplitev6_fail:
9931001
udpv6_exit();

net/ipv6/ip6_offload.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,6 @@ static int __init ipv6_offload_init(void)
325325

326326
if (tcpv6_offload_init() < 0)
327327
pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
328-
if (udp_offload_init() < 0)
329-
pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
330328
if (ipv6_exthdrs_offload_init() < 0)
331329
pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
332330

net/ipv6/ip6_offload.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#define __ip6_offload_h
1313

1414
int ipv6_exthdrs_offload_init(void);
15-
int udp_offload_init(void);
15+
int udpv6_offload_init(void);
16+
int udpv6_offload_exit(void);
1617
int tcpv6_offload_init(void);
1718

1819
#endif

net/ipv6/udp_offload.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static struct sk_buff **udp6_gro_receive(struct sk_buff **head,
153153

154154
skip:
155155
NAPI_GRO_CB(skb)->is_ipv6 = 1;
156-
return udp_gro_receive(head, skb, uh);
156+
return udp_gro_receive(head, skb, uh, udp6_lib_lookup_skb);
157157

158158
flush:
159159
NAPI_GRO_CB(skb)->flush = 1;
@@ -173,7 +173,7 @@ static int udp6_gro_complete(struct sk_buff *skb, int nhoff)
173173
skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
174174
}
175175

176-
return udp_gro_complete(skb, nhoff);
176+
return udp_gro_complete(skb, nhoff, udp6_lib_lookup_skb);
177177
}
178178

179179
static const struct net_offload udpv6_offload = {
@@ -184,7 +184,12 @@ static const struct net_offload udpv6_offload = {
184184
},
185185
};
186186

187-
int __init udp_offload_init(void)
187+
int udpv6_offload_init(void)
188188
{
189189
return inet6_add_offload(&udpv6_offload, IPPROTO_UDP);
190190
}
191+
192+
int udpv6_offload_exit(void)
193+
{
194+
return inet6_del_offload(&udpv6_offload, IPPROTO_UDP);
195+
}

0 commit comments

Comments
 (0)