Skip to content

Commit 73cd98f

Browse files
JasonXingKernel Patches Daemon
authored andcommitted
xsk: introduce local_cq for each af_xdp socket
This is a prep that will be used to store the addr(s) of descriptors so that each skb going to the end of life can publish corresponding addr(s) in its completion queue that can be read by userspace. Signed-off-by: Jason Xing <kernelxing@tencent.com>
1 parent 5007636 commit 73cd98f

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

include/net/xdp_sock.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct xsk_map {
4545
struct xdp_sock __rcu *xsk_map[];
4646
};
4747

48+
struct local_cq {
49+
u32 prod ____cacheline_aligned_in_smp;
50+
u32 ring_mask ____cacheline_aligned_in_smp;
51+
u64 desc[] ____cacheline_aligned_in_smp;
52+
};
53+
4854
struct xdp_sock {
4955
/* struct sock must be the first member of struct xdp_sock */
5056
struct sock sk;
@@ -89,6 +95,8 @@ struct xdp_sock {
8995
struct mutex mutex;
9096
struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
9197
struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */
98+
/* Maintain addr(s) of descriptors locally */
99+
struct local_cq *lcq;
92100
};
93101

94102
/*

net/xdp/xsk.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,30 @@ static void xsk_delete_from_maps(struct xdp_sock *xs)
12121212
}
12131213
}
12141214

1215+
/* Initialize local compeletion queue for each xsk */
1216+
static int xsk_init_local_cq(struct xdp_sock *xs)
1217+
{
1218+
struct xsk_queue *cq = xs->pool->cq;
1219+
size_t size;
1220+
1221+
if (!cq || !cq->nentries)
1222+
return -EINVAL;
1223+
1224+
size = struct_size_t(struct local_cq, desc, cq->nentries);
1225+
xs->lcq = vmalloc(size);
1226+
if (!xs->lcq)
1227+
return -ENOMEM;
1228+
xs->lcq->ring_mask = cq->nentries - 1;
1229+
xs->lcq->prod = 0;
1230+
1231+
return 0;
1232+
}
1233+
1234+
static void xsk_clear_local_cq(struct xdp_sock *xs)
1235+
{
1236+
vfree(xs->lcq);
1237+
}
1238+
12151239
static int xsk_release(struct socket *sock)
12161240
{
12171241
struct sock *sk = sock->sk;
@@ -1241,6 +1265,7 @@ static int xsk_release(struct socket *sock)
12411265
xskq_destroy(xs->tx);
12421266
xskq_destroy(xs->fq_tmp);
12431267
xskq_destroy(xs->cq_tmp);
1268+
xsk_clear_local_cq(xs);
12441269

12451270
sock_orphan(sk);
12461271
sock->sk = NULL;
@@ -1360,9 +1385,18 @@ static int xsk_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr
13601385
goto out_unlock;
13611386
}
13621387

1388+
err = xsk_init_local_cq(xs);
1389+
if (err) {
1390+
xp_destroy(xs->pool);
1391+
xs->pool = NULL;
1392+
sockfd_put(sock);
1393+
goto out_unlock;
1394+
}
1395+
13631396
err = xp_assign_dev_shared(xs->pool, umem_xs, dev,
13641397
qid);
13651398
if (err) {
1399+
xsk_clear_local_cq(xs);
13661400
xp_destroy(xs->pool);
13671401
xs->pool = NULL;
13681402
sockfd_put(sock);
@@ -1380,13 +1414,21 @@ static int xsk_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr
13801414
xp_get_pool(umem_xs->pool);
13811415
xs->pool = umem_xs->pool;
13821416

1417+
err = xsk_init_local_cq(xs);
1418+
if (err) {
1419+
xp_put_pool(xs->pool);
1420+
xs->pool = NULL;
1421+
sockfd_put(sock);
1422+
goto out_unlock;
1423+
}
13831424
/* If underlying shared umem was created without Tx
13841425
* ring, allocate Tx descs array that Tx batching API
13851426
* utilizes
13861427
*/
13871428
if (xs->tx && !xs->pool->tx_descs) {
13881429
err = xp_alloc_tx_descs(xs->pool, xs);
13891430
if (err) {
1431+
xsk_clear_local_cq(xs);
13901432
xp_put_pool(xs->pool);
13911433
xs->pool = NULL;
13921434
sockfd_put(sock);
@@ -1409,8 +1451,16 @@ static int xsk_bind(struct socket *sock, struct sockaddr_unsized *addr, int addr
14091451
goto out_unlock;
14101452
}
14111453

1454+
err = xsk_init_local_cq(xs);
1455+
if (err) {
1456+
xp_destroy(xs->pool);
1457+
xs->pool = NULL;
1458+
goto out_unlock;
1459+
}
1460+
14121461
err = xp_assign_dev(xs->pool, dev, qid, flags);
14131462
if (err) {
1463+
xsk_clear_local_cq(xs);
14141464
xp_destroy(xs->pool);
14151465
xs->pool = NULL;
14161466
goto out_unlock;

0 commit comments

Comments
 (0)