aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/pktio/socket_xdp.c
blob: e43e4bf891763695c9d51427da1404bf3b765c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/* Copyright (c) 2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/autoheader_internal.h>

#ifdef _ODP_PKTIO_XDP

#include <odp_posix_extensions.h>
#include <odp/api/debug.h>
#include <odp/api/hints.h>
#include <odp/api/system_info.h>
#include <odp/api/ticketlock.h>

#include <odp_debug_internal.h>
#include <odp_macros_internal.h>
#include <odp_packet_io_internal.h>
#include <odp_packet_internal.h>
#include <odp_parse_internal.h>
#include <odp_classification_internal.h>
#include <odp_socket_common.h>

#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <unistd.h>
#include <poll.h>

#include <xdp/xsk.h>

#define NUM_XDP_DESCS 1024U
#define MIN_FRAME_SIZE 2048U
#define IF_DELIM " "
#define Q_DELIM ':'

typedef struct {
	struct xsk_ring_prod fill_q;
	struct xsk_ring_cons compl_q;
	struct xsk_umem *umem;
	pool_t *pool;
} xdp_umem_info_t;

typedef struct {
	struct xsk_ring_cons rx;
	struct xsk_ring_cons compl_q;
	struct xsk_ring_prod tx;
	struct xsk_ring_prod fill_q;
	xdp_umem_info_t *umem_info;
	struct xsk_socket *xsk;
	int pktio_idx;
	int helper_sock;
	uint32_t mtu;
	uint32_t max_mtu;
} xdp_sock_info_t;

typedef struct {
	odp_ticketlock_t rx_lock ODP_ALIGNED_CACHE;
	odp_ticketlock_t tx_lock ODP_ALIGNED_CACHE;
	xdp_sock_info_t sock_info;
} pkt_xdp_t;

typedef struct {
	odp_packet_hdr_t *pkt_hdr;
	odp_packet_t pkt;
	uint8_t *data;
	uint32_t len;
} pkt_data_t;

ODP_STATIC_ASSERT(PKTIO_PRIVATE_SIZE >= sizeof(pkt_xdp_t),
		  "PKTIO_PRIVATE_SIZE too small");

static odp_bool_t disable_pktio;

static int sock_xdp_init_global(void)
{
	if (getenv("ODP_PKTIO_DISABLE_SOCKET_XDP")) {
		ODP_PRINT("PKTIO: socket xdp skipped,"
			  " enabled export ODP_PKTIO_DISABLE_SOCKET_XDP=1.\n");
		disable_pktio = true;
	} else {
		ODP_PRINT("PKTIO: initialized socket xdp,"
			  " use export ODP_PKTIO_DISABLE_SOCKET_XDP=1 to disable.\n");
	}

	return 0;
}

static inline pkt_xdp_t *pkt_priv(pktio_entry_t *pktio_entry)
{
	return (pkt_xdp_t *)(uintptr_t)(pktio_entry->s.pkt_priv);
}

static void fill_socket_config(struct xsk_socket_config *config)
{
	config->rx_size = NUM_XDP_DESCS;
	config->tx_size = NUM_XDP_DESCS;
	config->libxdp_flags = 0U;
	config->xdp_flags = 0U;
	config->bind_flags = XDP_ZEROCOPY; /* TODO: XDP_COPY */
}

static uint32_t get_bind_queue_index(const char *devname)
{
	const char *param = getenv("ODP_PKTIO_XDP_PARAMS");
	char *tmp_str;
	char *tmp;
	char *if_str;
	int idx = 0;

	if (param == NULL)
		goto out;

	tmp_str = strdup(param);

	if (tmp_str == NULL)
		goto out;

	tmp = strtok(tmp_str, IF_DELIM);

	if (tmp == NULL)
		goto out_str;

	while (tmp) {
		if_str = strchr(tmp, Q_DELIM);

		if (if_str != NULL && if_str != &tmp[strlen(tmp) - 1U]) {
			if (strncmp(devname, tmp, (uint64_t)(uintptr_t)(if_str - tmp)) == 0) {
				idx = _ODP_MAX(atoi(++if_str), 0);
				break;
			}
		}

		tmp = strtok(NULL, IF_DELIM);
	}

out_str:
	free(tmp_str);

out:
	return idx;
}

static odp_bool_t reserve_fill_queue_elements(xdp_sock_info_t *sock_info, int num)
{
	pool_t *pool;
	odp_packet_t packets[num];
	int count;
	struct xsk_ring_prod *fill_q;
	uint32_t start_idx;
	int pktio_idx;
	uint32_t block_size;
	odp_packet_hdr_t *pkt_hdr;

	pool = sock_info->umem_info->pool;
	count = odp_packet_alloc_multi(pool->pool_hdl, sock_info->mtu, packets, num);

	if (count <= 0)
		return false;

	fill_q = &sock_info->fill_q;

	if (xsk_ring_prod__reserve(fill_q, count, &start_idx) == 0U) {
		odp_packet_free_multi(packets, count);
		return false;
	}

	pktio_idx = sock_info->pktio_idx;
	block_size = pool->block_size;

	for (int i = 0; i < count; ++i) {
		pkt_hdr = packet_hdr(packets[i]);
		pkt_hdr->ms_pktio_idx = pktio_idx;
		*xsk_ring_prod__fill_addr(fill_q, start_idx++) =
			pkt_hdr->event_hdr.index.event * block_size;
	}

	xsk_ring_prod__submit(&sock_info->fill_q, count);

	return true;
}

static int sock_xdp_open(odp_pktio_t pktio, pktio_entry_t *pktio_entry, const char *devname,
			 odp_pool_t pool_hdl)
{
	pkt_xdp_t *priv;
	pool_t *pool;
	struct xsk_socket_config config;
	uint32_t bind_q;
	int ret;

	if (disable_pktio)
		return -1;

	priv = pkt_priv(pktio_entry);
	memset(priv, 0, sizeof(pkt_xdp_t));
	pool = pool_entry_from_hdl(pool_hdl);
	priv->sock_info.umem_info = (xdp_umem_info_t *)pool->mem_src_data;
	priv->sock_info.xsk = NULL;
	/* Mark transitory kernel-owned packets with the pktio index, so that they can be freed on
	 * close. */
	priv->sock_info.pktio_idx = 1 + odp_pktio_index(pktio);
	fill_socket_config(&config);
	bind_q = get_bind_queue_index(devname);
	/* With xsk_socket__create_shared(), as only one bind queue index can
	 * be passed, NIC in use needs to be configured accordingly to have
	 * only a single combined TX-RX queue, otherwise traffic may not end up
	 * on the socket. For now, always bind to the first queue (overridable
	 * with environment variable). */
	ret = xsk_socket__create_shared(&priv->sock_info.xsk, devname, bind_q,
					priv->sock_info.umem_info->umem, &priv->sock_info.rx,
					&priv->sock_info.tx, &priv->sock_info.fill_q,
					&priv->sock_info.compl_q, &config);

	if (ret) {
		ODP_ERR("Error creating xdp socket for bind queue %u: %d\n", bind_q, ret);
		goto xsk_err;
	}

	/* Ring setup/clean up routines seem to be asynchronous with some drivers and might not be
	 * ready yet after xsk_socket__create_shared(). */
	sleep(1U);

	/* Querying with ioctl() via AF_XDP socket doesn't seem to work, so
	 * create a helper socket for this. */
	priv->sock_info.helper_sock = -1;
	ret = socket(AF_INET, SOCK_DGRAM, 0);

	if (ret == -1) {
		ODP_ERR("Error creating helper socket for xdp: %s\n", strerror(errno));
		goto sock_err;
	}

	priv->sock_info.helper_sock = ret;
	priv->sock_info.mtu = _odp_mtu_get_fd(priv->sock_info.helper_sock, devname);

	if (priv->sock_info.mtu == 0U)
		goto res_err;

	priv->sock_info.max_mtu = pool->seg_len;

	if (!reserve_fill_queue_elements(&priv->sock_info, config.rx_size)) {
		ODP_ERR("Unable to reserve fill queue descriptors.\n");
		goto res_err;
	}

	odp_ticketlock_init(&priv->rx_lock);
	odp_ticketlock_init(&priv->tx_lock);

	return 0;

res_err:
	close(priv->sock_info.helper_sock);
	priv->sock_info.helper_sock = -1;

sock_err:
	xsk_socket__delete(priv->sock_info.xsk);
	priv->sock_info.xsk = NULL;

xsk_err:
	return -1;
}

static int sock_xdp_close(pktio_entry_t *pktio_entry)
{
	pkt_xdp_t *priv = pkt_priv(pktio_entry);
	pool_t *pool = priv->sock_info.umem_info->pool;
	odp_packet_hdr_t *pkt_hdr;

	if (priv->sock_info.helper_sock != -1)
		close(priv->sock_info.helper_sock);

	if (priv->sock_info.xsk != NULL)
		xsk_socket__delete(priv->sock_info.xsk);

	/* Ring setup/clean up routines seem to be asynchronous with some drivers and might not be
	 * ready yet after xsk_socket__delete(). */
	sleep(1U);

	/* Free all packets that were in fill or completion queues at the time of closing. */
	for (uint32_t i = 0U; i < pool->num + pool->skipped_blocks; ++i) {
		pkt_hdr = packet_hdr(packet_from_event_hdr(event_hdr_from_index(pool, i)));

		if (pkt_hdr->ms_pktio_idx == priv->sock_info.pktio_idx) {
			pkt_hdr->ms_pktio_idx = 0U;
			odp_packet_free(packet_handle(pkt_hdr));
		}
	}

	return 0;
}

static inline void extract_data(const struct xdp_desc *rx_desc, uint8_t *pool_base_addr,
				pkt_data_t *pkt_data)
{
	uint64_t frame_off;
	uint64_t pkt_off;

	/* UMEM "addresses" are offsets from start of a registered UMEM area.
	 * Additionally, the packet data offset (where received packet data
	 * starts within a UMEM frame) is encoded to the UMEM address with
	 * XSK_UNALIGNED_BUF_OFFSET_SHIFT left bitshift when XDP_ZEROCOPY and
	 * XDP_UMEM_UNALIGNED_CHUNK_FLAG are enabled. */
	frame_off = rx_desc->addr;
	pkt_off = xsk_umem__add_offset_to_addr(frame_off);
	frame_off = xsk_umem__extract_addr(frame_off);
	pkt_data->pkt_hdr = xsk_umem__get_data(pool_base_addr, frame_off);
	pkt_data->pkt = packet_handle(pkt_data->pkt_hdr);
	pkt_data->data = xsk_umem__get_data(pool_base_addr, pkt_off);
	pkt_data->len = rx_desc->len;
}

static uint32_t process_received(pktio_entry_t *pktio_entry, xdp_sock_info_t *sock_info,
				 uint32_t start_idx, odp_packet_t packets[], int num)
{
	pkt_data_t pkt_data;
	struct xsk_ring_cons *rx = &sock_info->rx;
	uint8_t *base_addr = sock_info->umem_info->pool->base_addr;
	const odp_proto_layer_t layer = pktio_entry->s.parse_layer;
	const odp_proto_chksums_t in_chksums = pktio_entry->s.in_chksums;
	const odp_pktin_config_opt_t opt = pktio_entry->s.config.pktin;
	uint64_t l4_part_sum = 0U;
	odp_pool_t *pool_hdl = &sock_info->umem_info->pool->pool_hdl;
	odp_pktio_t pktio_hdl = pktio_entry->s.handle;
	uint32_t num_rx = 0U;

	for (int i = 0; i < num; ++i) {
		extract_data(xsk_ring_cons__rx_desc(rx, start_idx++), base_addr, &pkt_data);
		pkt_data.pkt_hdr->ms_pktio_idx = 0U;
		packet_init(pkt_data.pkt_hdr, pkt_data.len);

		if (layer) {
			if (_odp_packet_parse_common(&pkt_data.pkt_hdr->p, pkt_data.data,
						     pkt_data.len, pkt_data.len,
						     layer, in_chksums, &l4_part_sum, opt) < 0) {
				odp_packet_free(pkt_data.pkt);
				continue;
			}

			if (pktio_cls_enabled(pktio_entry) &&
			    _odp_cls_classify_packet(pktio_entry, pkt_data.data, pool_hdl,
						     pkt_data.pkt_hdr)) {
				odp_packet_free(pkt_data.pkt);
				continue;
			}
		}

		pkt_data.pkt_hdr->seg_data = pkt_data.data;
		pkt_data.pkt_hdr->event_hdr.base_data = pkt_data.data;
		pkt_data.pkt_hdr->input = pktio_hdl;
		packets[num_rx++] = pkt_data.pkt;
	}

	return num_rx;
}

static int sock_xdp_recv(pktio_entry_t *pktio_entry, int index ODP_UNUSED, odp_packet_t packets[],
			 int num)
{
	pkt_xdp_t *priv;
	struct pollfd fd;
	uint32_t start_idx = 0U, recvd, procd;

	priv = pkt_priv(pktio_entry);
	odp_ticketlock_lock(&priv->rx_lock);

	if (odp_unlikely(xsk_ring_prod__needs_wakeup(&priv->sock_info.fill_q))) {
		fd.fd = xsk_socket__fd(priv->sock_info.xsk);
		fd.events = POLLIN;
		(void)poll(&fd, 1U, 0);
	}

	recvd = xsk_ring_cons__peek(&priv->sock_info.rx, num, &start_idx);

	if (recvd == 0U) {
		odp_ticketlock_unlock(&priv->rx_lock);
		return 0;
	}

	procd = process_received(pktio_entry, &priv->sock_info, start_idx, packets, recvd);
	xsk_ring_cons__release(&priv->sock_info.rx, recvd);
	(void)reserve_fill_queue_elements(&priv->sock_info, recvd);
	odp_ticketlock_unlock(&priv->rx_lock);

	return procd;
}

static inline void populate_tx_desc(pool_t *pool, odp_packet_hdr_t *pkt_hdr,
				    struct xdp_desc *tx_desc)
{
	uint64_t frame_off;
	uint64_t pkt_off;

	frame_off = pkt_hdr->event_hdr.index.event * pool->block_size;
	pkt_off = (uint64_t)(uintptr_t)pkt_hdr->event_hdr.base_data
		  - (uint64_t)(uintptr_t)pool->base_addr - frame_off;
	pkt_off <<= XSK_UNALIGNED_BUF_OFFSET_SHIFT;
	tx_desc->addr = frame_off | pkt_off;
	tx_desc->len = pkt_hdr->frame_len;
}

static void handle_pending_tx(xdp_sock_info_t *sock_info, int num)
{
	struct xsk_ring_cons *compl_q;
	uint32_t sent;
	uint8_t *base_addr;
	uint32_t start_idx;
	uint64_t frame_off;
	odp_packet_t pkt;

	if (odp_unlikely(xsk_ring_prod__needs_wakeup(&sock_info->tx)))
		(void)sendto(xsk_socket__fd(sock_info->xsk), NULL, 0U, MSG_DONTWAIT, NULL, 0U);

	compl_q = &sock_info->compl_q;
	sent = xsk_ring_cons__peek(compl_q, num, &start_idx);
	base_addr = sock_info->umem_info->pool->base_addr;

	odp_packet_t packets[sent];

	if (sent) {
		for (uint32_t i = 0U; i < sent; ++i) {
			frame_off = *xsk_ring_cons__comp_addr(compl_q, start_idx++);
			frame_off = xsk_umem__extract_addr(frame_off);
			pkt = xsk_umem__get_data(base_addr, frame_off);
			packets[i] = pkt;
			packet_hdr(packets[i])->ms_pktio_idx = 0U;
		}

		odp_packet_free_multi(packets, sent);
		xsk_ring_cons__release(compl_q, sent);
	}
}

static int sock_xdp_send(pktio_entry_t *pktio_entry, int index ODP_UNUSED,
			 const odp_packet_t packets[], int num)
{
	pkt_xdp_t *priv;
	xdp_sock_info_t *sock_info;
	pool_t *pool;
	odp_pool_t pool_hdl;
	int pktio_idx, i;
	struct xsk_ring_prod *tx;
	odp_packet_t pkt;
	odp_packet_hdr_t *pkt_hdr;
	uint32_t start_idx;

	if (odp_unlikely(num == 0))
		return 0;

	priv = pkt_priv(pktio_entry);
	odp_ticketlock_lock(&priv->tx_lock);
	sock_info = &priv->sock_info;
	pool = sock_info->umem_info->pool;
	pool_hdl = pool->pool_hdl;
	pktio_idx = sock_info->pktio_idx;
	tx = &sock_info->tx;

	for (i = 0; i < num; ++i) {
		pkt = ODP_PACKET_INVALID;

		if (odp_unlikely(odp_packet_num_segs(packets[i])) > 1) {
			/* TODO: handle segmented packets */
			ODP_ERR("Only single-segment packets supported\n");
			break;
		}

		pkt_hdr = packet_hdr(packets[i]);

		if (pkt_hdr->event_hdr.pool_ptr != pool) {
			pkt = odp_packet_copy(packets[i], pool_hdl);

			if (odp_unlikely(pkt == ODP_PACKET_INVALID))
				break;

			pkt_hdr = packet_hdr(pkt);
		}

		if (xsk_ring_prod__reserve(tx, 1U, &start_idx) == 0U) {
			handle_pending_tx(sock_info, NUM_XDP_DESCS);

			if (xsk_ring_prod__reserve(tx, 1U, &start_idx) == 0U) {
				if (pkt != ODP_PACKET_INVALID)
					odp_packet_free(pkt);

				break;
			}
		}

		if (pkt != ODP_PACKET_INVALID)
			odp_packet_free(packets[i]);

		pkt_hdr->ms_pktio_idx = pktio_idx;
		populate_tx_desc(pool, pkt_hdr, xsk_ring_prod__tx_desc(tx, start_idx));
	}

	xsk_ring_prod__submit(tx, i);
	handle_pending_tx(sock_info, NUM_XDP_DESCS);
	odp_ticketlock_unlock(&priv->tx_lock);

	return i;
}

static uint32_t sock_xdp_mtu_get(pktio_entry_t *pktio_entry)
{
	return pkt_priv(pktio_entry)->sock_info.mtu;
}

static int sock_xdp_mtu_set(pktio_entry_t *pktio_entry, uint32_t maxlen_input,
			    uint32_t maxlen_output ODP_UNUSED)
{
	pkt_xdp_t *priv = pkt_priv(pktio_entry);
	int ret;

	ret = _odp_mtu_set_fd(priv->sock_info.helper_sock, pktio_entry->s.name, maxlen_input);
	if (ret)
		return ret;

	priv->sock_info.mtu = maxlen_input;

	return 0;
}

static int sock_xdp_promisc_mode_set(pktio_entry_t *pktio_entry,  int enable)
{
	return _odp_promisc_mode_set_fd(pkt_priv(pktio_entry)->sock_info.helper_sock,
					pktio_entry->s.name, enable);
}

static int sock_xdp_promisc_mode_get(pktio_entry_t *pktio_entry)
{
	return _odp_promisc_mode_get_fd(pkt_priv(pktio_entry)->sock_info.helper_sock,
					pktio_entry->s.name);
}

static int sock_xdp_mac_addr_get(pktio_entry_t *pktio_entry ODP_UNUSED, void *mac_addr)
{
	return _odp_mac_addr_get_fd(pkt_priv(pktio_entry)->sock_info.helper_sock,
				    pktio_entry->s.name, mac_addr) ? -1 : ETH_ALEN;
}

static int sock_xdp_link_status(pktio_entry_t *pktio_entry)
{
	return _odp_link_status_fd(pkt_priv(pktio_entry)->sock_info.helper_sock,
				   pktio_entry->s.name);
}

static int sock_xdp_link_info(pktio_entry_t *pktio_entry, odp_pktio_link_info_t *info)
{
	return _odp_link_info_fd(pkt_priv(pktio_entry)->sock_info.helper_sock,
				 pktio_entry->s.name, info);
}

static int sock_xdp_capability(pktio_entry_t *pktio_entry, odp_pktio_capability_t *capa)
{
	pkt_xdp_t *priv = pkt_priv(pktio_entry);

	memset(capa, 0, sizeof(odp_pktio_capability_t));
	capa->max_input_queues  = 1U;
	capa->max_output_queues = 1U;
	capa->set_op.op.promisc_mode = 1U;
	capa->set_op.op.maxlen = 1U;

	capa->maxlen.equal = true;
	capa->maxlen.min_input = _ODP_SOCKET_MTU_MIN;
	capa->maxlen.max_input = priv->sock_info.max_mtu;
	capa->maxlen.min_output = _ODP_SOCKET_MTU_MIN;
	capa->maxlen.max_output = priv->sock_info.max_mtu;

	capa->config.parser.layer = ODP_PROTO_LAYER_ALL;

	capa->stats.pktio.all_counters = 0U;
	capa->stats.pktin_queue.all_counters = 0U;
	capa->stats.pktout_queue.all_counters = 0U;

	return 0;
}

const pktio_if_ops_t _odp_sock_xdp_pktio_ops = {
	/* TODO: at least stats */
	.name = "socket_xdp",
	.print = NULL,
	.init_global = sock_xdp_init_global,
	.init_local = NULL,
	.term = NULL,
	.open = sock_xdp_open,
	.close = sock_xdp_close,
	.start = NULL,
	.stop = NULL,
	.stats = NULL,
	.stats_reset = NULL,
	.pktin_queue_stats = NULL,
	.pktout_queue_stats = NULL,
	.extra_stat_info = NULL,
	.extra_stats = NULL,
	.extra_stat_counter = NULL,
	.pktio_ts_res = NULL,
	.pktio_ts_from_ns = NULL,
	.pktio_time = NULL,
	.recv = sock_xdp_recv,
	.recv_tmo = NULL,
	.recv_mq_tmo = NULL,
	.fd_set = NULL,
	.send = sock_xdp_send,
	.maxlen_get = sock_xdp_mtu_get,
	.maxlen_set = sock_xdp_mtu_set,
	.promisc_mode_set = sock_xdp_promisc_mode_set,
	.promisc_mode_get = sock_xdp_promisc_mode_get,
	.mac_get = sock_xdp_mac_addr_get,
	.mac_set = NULL,
	.link_status = sock_xdp_link_status,
	.link_info = sock_xdp_link_info,
	.capability = sock_xdp_capability,
	.config = NULL,
	.input_queues_config = NULL,
	.output_queues_config = NULL
};

static odp_bool_t sock_xdp_is_mem_src_active(void)
{
	return !disable_pktio;
}

static void sock_xdp_force_mem_src_disable(void)
{
	disable_pktio = true;
}

static void sock_xdp_adjust_block_size(uint8_t *data ODP_UNUSED, uint32_t *block_size,
				       uint32_t *block_offset ODP_UNUSED, uint32_t *flags)
{
	const uint32_t size = *block_size + XDP_PACKET_HEADROOM;
	const uint64_t ps = odp_sys_page_size();
	/* AF_XDP requires frames to be between 2kB and page size, so with
	 * XDP_ZEROCOPY, if block size is less than 2kB, adjust it to 2kB, if
	 * it is larger than page size, make pool creation fail. */
	if (disable_pktio)
		return;

	if (size > ps) {
		ODP_ERR("Adjusted pool block size larger than page size: %u > %" PRIu64 "\n",
			size, ps);
		*block_size = 0U;
	}

	*flags |= ODP_SHM_HP;
	*block_size = _ODP_MAX(size, MIN_FRAME_SIZE);
}

static int sock_xdp_umem_create(uint8_t *data, pool_t *pool)
{
	struct xsk_umem_config cfg;
	xdp_umem_info_t *umem_info = (xdp_umem_info_t *)data;

	umem_info->pool = pool;
	/* Fill queue size is recommended to be >= HW RX ring size + AF_XDP RX
	 * ring size, so use size twice the size of AF_XDP RX ring. */
	cfg.fill_size = NUM_XDP_DESCS * 2U; /* TODO: num descs vs pool size */
	cfg.comp_size = NUM_XDP_DESCS;
	cfg.frame_size = pool->block_size;
	cfg.frame_headroom = sizeof(odp_packet_hdr_t) + pool->headroom;
	cfg.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;

	return xsk_umem__create(&umem_info->umem, pool->base_addr, pool->shm_size,
				&umem_info->fill_q, &umem_info->compl_q, &cfg);
}

static void sock_xdp_umem_delete(uint8_t *data)
{
	xdp_umem_info_t *umem_info = (xdp_umem_info_t *)data;

	while (xsk_umem__delete(umem_info->umem) == -EBUSY)
		continue;
}

const _odp_pool_mem_src_ops_t _odp_pool_sock_xdp_mem_src_ops = {
	.name = "xdp_zc",
	.is_active = sock_xdp_is_mem_src_active,
	.force_disable = sock_xdp_force_mem_src_disable,
	.adjust_size = sock_xdp_adjust_block_size,
	.bind = sock_xdp_umem_create,
	.unbind = sock_xdp_umem_delete
};

#else
/* Avoid warning about empty translation unit */
typedef int _odp_dummy;
#endif