Skip to content

Commit

Permalink
libxdp: Assign default values for fields in xsk_umem_opts
Browse files Browse the repository at this point in the history
Use 0 as default(or unset) value for fd when creating umem,
also, set fields to their default values if passed in by 0.

Signed-off-by: Muyang Tian <[email protected]>
  • Loading branch information
tacslon committed Nov 11, 2024
1 parent fb8a8f5 commit 0b7b4a3
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions lib/libxdp/xsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ struct xsk_umem *xsk_umem__create_opts(void *umem_area,
err = -EINVAL;
goto err;
}
fd = OPTS_GET(opts, fd, -1);
fd = OPTS_GET(opts, fd, 0);
size = OPTS_GET(opts, size, 0);

if (!size && !xsk_page_aligned(umem_area)) {
Expand All @@ -330,7 +330,7 @@ struct xsk_umem *xsk_umem__create_opts(void *umem_area,
goto err;
}

umem->fd = fd < 0 ? socket(AF_XDP, SOCK_RAW, 0) : fd;
umem->fd = fd > 0 ? fd : socket(AF_XDP, SOCK_RAW, 0);
if (umem->fd < 0) {
err = -errno;
goto out_umem_alloc;
Expand Down Expand Up @@ -379,26 +379,37 @@ int xsk_umem__create_with_fd(struct xsk_umem **umem_ptr, int fd,
struct xsk_ring_cons *comp,
const struct xsk_umem_config *usr_config)
{
__u32 fill_size, comp_size, frame_size, frame_headroom, flags;
struct xsk_umem *umem;

if(!umem_ptr)
return -EFAULT;

if(!usr_config) {
fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM;
flags = XSK_UMEM__DEFAULT_FLAGS;
} else {
/* Set the following fields to their default values if they are passed in as 0 */
fill_size = usr_config->fill_size ? usr_config->fill_size : XSK_RING_PROD__DEFAULT_NUM_DESCS;
comp_size = usr_config->comp_size ? usr_config->comp_size : XSK_RING_CONS__DEFAULT_NUM_DESCS;
frame_size = usr_config->frame_size ? usr_config->frame_size : XSK_UMEM__DEFAULT_FRAME_SIZE;
/* The following fields are 0 by default, just keep them as what they are passed in */
frame_headroom = usr_config->frame_headroom;
flags = usr_config->flags;
}

DECLARE_LIBXDP_OPTS(xsk_umem_opts, opts,
.fd = fd,
.size = size,
.fill_size = usr_config ? usr_config->fill_size
: XSK_RING_PROD__DEFAULT_NUM_DESCS,
.comp_size = usr_config ? usr_config->comp_size
: XSK_RING_CONS__DEFAULT_NUM_DESCS,
.frame_size = usr_config ? usr_config->frame_size
: XSK_UMEM__DEFAULT_FRAME_SIZE,
.frame_headroom = usr_config ? usr_config->frame_headroom
: XSK_UMEM__DEFAULT_FRAME_HEADROOM,
.flags = usr_config ? usr_config->flags
: XSK_UMEM__DEFAULT_FLAGS,
.fill_size = fill_size,
.comp_size = comp_size,
.frame_size = frame_size,
.frame_headroom = frame_headroom,
.flags = flags,
);

umem = xsk_umem__create_opts(umem_area, fill, comp, &opts);
if(!umem)
return errno;
Expand All @@ -412,7 +423,7 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area,
struct xsk_ring_cons *comp,
const struct xsk_umem_config *usr_config)
{
return xsk_umem__create_with_fd(umem_ptr, -1, umem_area, size,
return xsk_umem__create_with_fd(umem_ptr, 0, umem_area, size,
fill, comp, usr_config);
}

Expand Down

0 comments on commit 0b7b4a3

Please sign in to comment.