mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-18 22:14:16 +00:00
net: xsk: introduce XDP_MAX_TX_SKB_BUDGET setsockopt
This patch provides a setsockopt method to let applications leverage to adjust how many descs to be handled at most in one send syscall. It mitigates the situation where the default value (32) that is too small leads to higher frequency of triggering send syscall. Considering the prosperity/complexity the applications have, there is no absolutely ideal suggestion fitting all cases. So keep 32 as its default value like before. The patch does the following things: - Add XDP_MAX_TX_SKB_BUDGET socket option. - Set max_tx_budget to 32 by default in the initialization phase as a per-socket granular control. - Set the range of max_tx_budget as [32, xs->tx->nentries]. The idea behind this comes out of real workloads in production. We use a user-level stack with xsk support to accelerate sending packets and minimize triggering syscalls. When the packets are aggregated, it's not hard to hit the upper bound (namely, 32). The moment user-space stack fetches the -EAGAIN error number passed from sendto(), it will loop to try again until all the expected descs from tx ring are sent out to the driver. Enlarging the XDP_MAX_TX_SKB_BUDGET value contributes to less frequency of sendto() and higher throughput/PPS. Here is what I did in production, along with some numbers as follows: For one application I saw lately, I suggested using 128 as max_tx_budget because I saw two limitations without changing any default configuration: 1) XDP_MAX_TX_SKB_BUDGET, 2) socket sndbuf which is 212992 decided by net.core.wmem_default. As to XDP_MAX_TX_SKB_BUDGET, the scenario behind this was I counted how many descs are transmitted to the driver at one time of sendto() based on [1] patch and then I calculated the possibility of hitting the upper bound. Finally I chose 128 as a suitable value because 1) it covers most of the cases, 2) a higher number would not bring evident results. After twisting the parameters, a stable improvement of around 4% for both PPS and throughput and less resources consumption were found to be observed by strace -c -p xxx: 1) %time was decreased by 7.8% 2) error counter was decreased from 18367 to 572 [1]: https://lore.kernel.org/all/20250619093641.70700-1-kerneljasonxing@gmail.com/ Signed-off-by: Jason Xing <kernelxing@tencent.com> Acked-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Link: https://patch.msgid.link/20250704160138.48677-1-kerneljasonxing@gmail.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
This commit is contained in:
parent
c65d34296b
commit
45e359be1c
5 changed files with 31 additions and 2 deletions
|
@ -438,6 +438,15 @@ is created by a privileged process and passed to a non-privileged one.
|
|||
Once the option is set, kernel will refuse attempts to bind that socket
|
||||
to a different interface. Updating the value requires CAP_NET_RAW.
|
||||
|
||||
XDP_MAX_TX_SKB_BUDGET setsockopt
|
||||
--------------------------------
|
||||
|
||||
This setsockopt sets the maximum number of descriptors that can be handled
|
||||
and passed to the driver at one send syscall. It is applied in the copy
|
||||
mode to allow application to tune the per-socket maximum iteration for
|
||||
better throughput and less frequency of send syscall.
|
||||
Allowed range is [32, xs->tx->nentries].
|
||||
|
||||
XDP_STATISTICS getsockopt
|
||||
-------------------------
|
||||
|
||||
|
|
|
@ -84,6 +84,7 @@ struct xdp_sock {
|
|||
struct list_head map_list;
|
||||
/* Protects map_list */
|
||||
spinlock_t map_list_lock;
|
||||
u32 max_tx_budget;
|
||||
/* Protects multiple processes in the control path */
|
||||
struct mutex mutex;
|
||||
struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
|
||||
|
|
|
@ -79,6 +79,7 @@ struct xdp_mmap_offsets {
|
|||
#define XDP_UMEM_COMPLETION_RING 6
|
||||
#define XDP_STATISTICS 7
|
||||
#define XDP_OPTIONS 8
|
||||
#define XDP_MAX_TX_SKB_BUDGET 9
|
||||
|
||||
struct xdp_umem_reg {
|
||||
__u64 addr; /* Start of packet data area */
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
#include "xsk.h"
|
||||
|
||||
#define TX_BATCH_SIZE 32
|
||||
#define MAX_PER_SOCKET_BUDGET (TX_BATCH_SIZE)
|
||||
#define MAX_PER_SOCKET_BUDGET 32
|
||||
|
||||
void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
|
||||
{
|
||||
|
@ -783,10 +783,10 @@ free_err:
|
|||
static int __xsk_generic_xmit(struct sock *sk)
|
||||
{
|
||||
struct xdp_sock *xs = xdp_sk(sk);
|
||||
u32 max_batch = TX_BATCH_SIZE;
|
||||
bool sent_frame = false;
|
||||
struct xdp_desc desc;
|
||||
struct sk_buff *skb;
|
||||
u32 max_batch;
|
||||
int err = 0;
|
||||
|
||||
mutex_lock(&xs->mutex);
|
||||
|
@ -800,6 +800,7 @@ static int __xsk_generic_xmit(struct sock *sk)
|
|||
if (xs->queue_id >= xs->dev->real_num_tx_queues)
|
||||
goto out;
|
||||
|
||||
max_batch = READ_ONCE(xs->max_tx_budget);
|
||||
while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
|
||||
if (max_batch-- == 0) {
|
||||
err = -EAGAIN;
|
||||
|
@ -1440,6 +1441,21 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
|
|||
mutex_unlock(&xs->mutex);
|
||||
return err;
|
||||
}
|
||||
case XDP_MAX_TX_SKB_BUDGET:
|
||||
{
|
||||
unsigned int budget;
|
||||
|
||||
if (optlen != sizeof(budget))
|
||||
return -EINVAL;
|
||||
if (copy_from_sockptr(&budget, optval, sizeof(budget)))
|
||||
return -EFAULT;
|
||||
if (!xs->tx ||
|
||||
budget < TX_BATCH_SIZE || budget > xs->tx->nentries)
|
||||
return -EACCES;
|
||||
|
||||
WRITE_ONCE(xs->max_tx_budget, budget);
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -1737,6 +1753,7 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
|
|||
|
||||
xs = xdp_sk(sk);
|
||||
xs->state = XSK_READY;
|
||||
xs->max_tx_budget = TX_BATCH_SIZE;
|
||||
mutex_init(&xs->mutex);
|
||||
|
||||
INIT_LIST_HEAD(&xs->map_list);
|
||||
|
|
|
@ -79,6 +79,7 @@ struct xdp_mmap_offsets {
|
|||
#define XDP_UMEM_COMPLETION_RING 6
|
||||
#define XDP_STATISTICS 7
|
||||
#define XDP_OPTIONS 8
|
||||
#define XDP_MAX_TX_SKB_BUDGET 9
|
||||
|
||||
struct xdp_umem_reg {
|
||||
__u64 addr; /* Start of packet data area */
|
||||
|
|
Loading…
Add table
Reference in a new issue