@@ -152,22 +152,13 @@ static bool xsk_page_aligned(void *buffer)
152
152
}
153
153
154
154
static void xsk_set_umem_config (struct xsk_umem_config * cfg ,
155
- const struct xsk_umem_config * usr_cfg )
155
+ const struct xsk_umem_opts * opts )
156
156
{
157
- if (!usr_cfg ) {
158
- cfg -> fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS ;
159
- cfg -> comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS ;
160
- cfg -> frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE ;
161
- cfg -> frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM ;
162
- cfg -> flags = XSK_UMEM__DEFAULT_FLAGS ;
163
- return ;
164
- }
165
-
166
- cfg -> fill_size = usr_cfg -> fill_size ;
167
- cfg -> comp_size = usr_cfg -> comp_size ;
168
- cfg -> frame_size = usr_cfg -> frame_size ;
169
- cfg -> frame_headroom = usr_cfg -> frame_headroom ;
170
- cfg -> flags = usr_cfg -> flags ;
157
+ cfg -> fill_size = OPTS_GET (opts , fill_size , XSK_RING_PROD__DEFAULT_NUM_DESCS );
158
+ cfg -> comp_size = OPTS_GET (opts , comp_size , XSK_RING_CONS__DEFAULT_NUM_DESCS );
159
+ cfg -> frame_size = OPTS_GET (opts , frame_size , XSK_UMEM__DEFAULT_FRAME_SIZE );
160
+ cfg -> frame_headroom = OPTS_GET (opts , frame_headroom , XSK_UMEM__DEFAULT_FRAME_HEADROOM );
161
+ cfg -> flags = OPTS_GET (opts , flags , XSK_UMEM__DEFAULT_FLAGS );
171
162
}
172
163
173
164
static int xsk_set_xdp_socket_config (struct xsk_socket_config * cfg ,
@@ -306,24 +297,38 @@ static int xsk_create_umem_rings(struct xsk_umem *umem, int fd,
306
297
return err ;
307
298
}
308
299
309
- int xsk_umem__create_with_fd (struct xsk_umem * * umem_ptr , int fd ,
310
- void * umem_area , __u64 size ,
311
- struct xsk_ring_prod * fill ,
312
- struct xsk_ring_cons * comp ,
313
- const struct xsk_umem_config * usr_config )
314
- {
300
+ struct xsk_umem * xsk_umem__create_opts (void * umem_area ,
301
+ struct xsk_ring_prod * fill ,
302
+ struct xsk_ring_cons * comp ,
303
+ struct xsk_umem_opts * opts ) {
315
304
struct xdp_umem_reg mr ;
316
305
struct xsk_umem * umem ;
317
- int err ;
306
+ size_t mr_size ;
307
+ int err , fd ;
308
+ __u64 size ;
309
+
310
+ if (!umem_area || !fill || !comp ) {
311
+ err = - EFAULT ;
312
+ goto err ;
313
+ }
318
314
319
- if (!umem_area || !umem_ptr || !fill || !comp )
320
- return - EFAULT ;
321
- if (!size && !xsk_page_aligned (umem_area ))
322
- return - EINVAL ;
315
+ if (!OPTS_VALID (opts , xsk_umem_opts )) {
316
+ err = - EINVAL ;
317
+ goto err ;
318
+ }
319
+ fd = OPTS_GET (opts , fd , -1 );
320
+ size = OPTS_GET (opts , size , 0 );
321
+
322
+ if (!size && !xsk_page_aligned (umem_area )) {
323
+ err = - EINVAL ;
324
+ goto err ;
325
+ }
323
326
324
327
umem = calloc (1 , sizeof (* umem ));
325
- if (!umem )
326
- return - ENOMEM ;
328
+ if (!umem ) {
329
+ err = - ENOMEM ;
330
+ goto err ;
331
+ }
327
332
328
333
umem -> fd = fd < 0 ? socket (AF_XDP , SOCK_RAW , 0 ) : fd ;
329
334
if (umem -> fd < 0 ) {
@@ -333,16 +338,21 @@ int xsk_umem__create_with_fd(struct xsk_umem **umem_ptr, int fd,
333
338
334
339
umem -> umem_area = umem_area ;
335
340
INIT_LIST_HEAD (& umem -> ctx_list );
336
- xsk_set_umem_config (& umem -> config , usr_config );
341
+ xsk_set_umem_config (& umem -> config , opts );
337
342
338
343
memset (& mr , 0 , sizeof (mr ));
339
344
mr .addr = (uintptr_t )umem_area ;
340
345
mr .len = size ;
341
346
mr .chunk_size = umem -> config .frame_size ;
342
347
mr .headroom = umem -> config .frame_headroom ;
343
348
mr .flags = umem -> config .flags ;
344
-
345
- err = setsockopt (umem -> fd , SOL_XDP , XDP_UMEM_REG , & mr , sizeof (mr ));
349
+ mr .tx_metadata_len = OPTS_GET (opts , tx_metadata_len , XSK_UMEM__DEFAULT_TX_METADATA_LEN );
350
+
351
+ mr_size = sizeof (mr );
352
+ /* Older kernels don't support tx_metadata_len, skip if we are not setting a value */
353
+ if (!mr .tx_metadata_len )
354
+ mr_size = offsetof(struct xdp_umem_reg , tx_metadata_len );
355
+ err = setsockopt (umem -> fd , SOL_XDP , XDP_UMEM_REG , & mr , mr_size );
346
356
if (err ) {
347
357
err = - errno ;
348
358
goto out_socket ;
@@ -354,14 +364,47 @@ int xsk_umem__create_with_fd(struct xsk_umem **umem_ptr, int fd,
354
364
355
365
umem -> fill_save = fill ;
356
366
umem -> comp_save = comp ;
357
- * umem_ptr = umem ;
358
- return 0 ;
359
-
367
+ return umem ;
360
368
out_socket :
361
369
close (umem -> fd );
362
370
out_umem_alloc :
363
371
free (umem );
364
- return err ;
372
+ err :
373
+ return libxdp_err_ptr (err , true);
374
+ }
375
+
376
+ int xsk_umem__create_with_fd (struct xsk_umem * * umem_ptr , int fd ,
377
+ void * umem_area , __u64 size ,
378
+ struct xsk_ring_prod * fill ,
379
+ struct xsk_ring_cons * comp ,
380
+ const struct xsk_umem_config * usr_config )
381
+ {
382
+ struct xsk_umem * umem ;
383
+
384
+ if (!umem_ptr )
385
+ return - EFAULT ;
386
+
387
+ DECLARE_LIBXDP_OPTS (xsk_umem_opts , opts ,
388
+ .fd = fd ,
389
+ .size = size ,
390
+ .fill_size = usr_config ? usr_config -> fill_size
391
+ : XSK_RING_PROD__DEFAULT_NUM_DESCS ,
392
+ .comp_size = usr_config ? usr_config -> comp_size
393
+ : XSK_RING_CONS__DEFAULT_NUM_DESCS ,
394
+ .frame_size = usr_config ? usr_config -> frame_size
395
+ : XSK_UMEM__DEFAULT_FRAME_SIZE ,
396
+ .frame_headroom = usr_config ? usr_config -> frame_headroom
397
+ : XSK_UMEM__DEFAULT_FRAME_HEADROOM ,
398
+ .flags = usr_config ? usr_config -> flags
399
+ : XSK_UMEM__DEFAULT_FLAGS ,
400
+ );
401
+
402
+ umem = xsk_umem__create_opts (umem_area , fill , comp , & opts );
403
+ if (!umem )
404
+ return errno ;
405
+
406
+ * umem_ptr = umem ;
407
+ return 0 ;
365
408
}
366
409
367
410
int xsk_umem__create (struct xsk_umem * * umem_ptr , void * umem_area ,
0 commit comments