File: sample_packet_forward.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (143 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download | duplicates (4)
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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Intel Corporation
 */

#include <stdio.h>
#include <string.h>

#include <rte_eth_ring.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_bus_vdev.h>
#include "rte_lcore.h"
#include "rte_mempool.h"
#include "rte_ring.h"

#include "sample_packet_forward.h"

/*
 * heper function: configure and start test device
 */
int
test_dev_start(uint16_t port, struct rte_mempool *mp)
{
	int32_t rc;
	struct rte_eth_conf pconf;

	memset(&pconf, 0, sizeof(pconf));

	rc =  rte_eth_dev_configure(port, NUM_QUEUES, NUM_QUEUES, &pconf);
	if (rc != 0)
		return rc;

	rc = rte_eth_rx_queue_setup(port, 0, RING_SIZE, SOCKET_ID_ANY,
		NULL, mp);
	if (rc != 0)
		return rc;

	rc = rte_eth_tx_queue_setup(port, 0, RING_SIZE, SOCKET_ID_ANY,
		NULL);
	if (rc != 0)
		return rc;

	rc = rte_eth_dev_start(port);
	return rc;
}

/* Sample test to create virtual rings and tx,rx portid from rings */
int
test_ring_setup(struct rte_ring **ring, uint16_t *portid)
{
	*ring = rte_ring_create("R0", RING_SIZE, rte_socket_id(),
				  RING_F_SP_ENQ | RING_F_SC_DEQ);
	if (*ring == NULL) {
		printf("%s() line %u: rte_ring_create R0 failed",
		       __func__, __LINE__);
		return -1;
	}
	*portid = rte_eth_from_rings("net_ringa", ring, NUM_QUEUES,
			ring, NUM_QUEUES, rte_socket_id());

	return 0;
}

/* Sample test to free the mempool */
void
test_mp_free(struct rte_mempool *mp)
{
	rte_mempool_free(mp);
}

/* Sample test to free the virtual rings */
void
test_ring_free(struct rte_ring *rxtx)
{
	rte_ring_free(rxtx);
}

/* Sample test to release the vdev */
void
test_vdev_uninit(const char *vdev)
{
	rte_vdev_uninit(vdev);
}

/* sample test to allocate the mempool */
int
test_get_mempool(struct rte_mempool **mp, char *poolname)
{
	*mp = rte_pktmbuf_pool_create(poolname, NB_MBUF, 32, 0,
			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (*mp == NULL)
		return -1;
	return 0;
}

/* sample test to allocate buffer for pkts */
int
test_get_mbuf_from_pool(struct rte_mempool **mp, struct rte_mbuf **pbuf,
		char *poolname)
{
	int ret = 0;

	ret = test_get_mempool(mp, poolname);
	if (ret < 0)
		return -1;
	if (rte_pktmbuf_alloc_bulk(*mp, pbuf, NUM_PACKETS) != 0) {
		printf("%s() line %u: rte_pktmbuf_alloc_bulk failed", __func__,
		       __LINE__);
		return -1;
	}
	return 0;
}

/* sample test to deallocate the allocated buffers and mempool */
void
test_put_mbuf_to_pool(struct rte_mempool *mp, struct rte_mbuf **pbuf)
{
	int itr = 0;

	for (itr = 0; itr < NUM_PACKETS; itr++)
		rte_pktmbuf_free(pbuf[itr]);
	rte_mempool_free(mp);
}

/* Sample test to forward packets using virtual portids */
int
test_packet_forward(struct rte_mbuf **pbuf, uint16_t portid, uint16_t queue_id)
{
	/* send and receive packet and check for stats update */
	if (rte_eth_tx_burst(portid, queue_id, pbuf, NUM_PACKETS)
			< NUM_PACKETS) {
		printf("%s() line %u: Error sending packet to"
		       " port %d\n", __func__, __LINE__, portid);
		return -1;
	}
	if (rte_eth_rx_burst(portid, queue_id, pbuf, NUM_PACKETS)
			< NUM_PACKETS) {
		printf("%s() line %u: Error receiving packet from"
		       " port %d\n", __func__, __LINE__, portid);
		return -1;
	}
	return 0;
}