i want to build application upd server and upd client for test, but i only have one dpdk interface, i’m new to dpdk, here is my upd server/client demo, and i got error when i run udp client app.
sudo ./dpdk_udp_client --proc-type=secondary
EAL: Detected CPU lcores: 2
EAL: Detected NUMA nodes: 1
EAL: Detected shared linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket_11301_143edb614d0e
EAL: Selected IOVA mode 'PA'
EAL: VFIO support initialized
EAL: Using IOMMU type 8 (No-IOMMU)
EAL: Probe PCI driver: net_ena (1d0f:ec20) device: 0000:28:00.0 (socket 0)
EAL: Error - exiting with code: 1
Cause: Cannot create mbuf pool
=============== upd client demo ================
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_udp.h>
#include <rte_ip.h>
#include <rte_ether.h>
#include <rte_malloc.h>
#include <rte_lcore.h>
#include <rte_cycles.h>
#include <rte_errno.h>
#include <iostream>
#include <cstring>
#define MBUF_CACHE_SIZE 250
#define NUM_MBUFS (4096-1)
#define IPv4(a, b, c, d) ((uint32_t)(((a) << 24) | ((b) << 16) | ((c) << 8) | (d)))
struct Data {
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
uint64_t e;
uint64_t f;
};
static struct rte_mempool *mbuf_pool;
int dpdk_init(int argc, char *argv[]) {
int ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Error with EAL initializationn");
// mbuf_pool = rte_mempool_lookup("MBUF_POOL");
// if (mbuf_pool == NULL)
// rte_exit(EXIT_FAILURE, "Cannot find mbuf pooln");
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * rte_lcore_count(),
MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
if (mbuf_pool == NULL)
rte_exit(EXIT_FAILURE, "Cannot create mbuf pooln");
return ret;
}
void udp_client_send(struct rte_mempool *mbuf_pool, uint16_t port_id, const struct Data *data) {
struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
if (mbuf == NULL)
rte_exit(EXIT_FAILURE, "Cannot allocate mbufn");
struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
struct rte_ipv4_hdr *ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
struct rte_udp_hdr *udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
struct Data *payload = (struct Data *)(udp_hdr + 1);
memset(eth_hdr, 0, sizeof(struct rte_ether_hdr));
rte_eth_macaddr_get(port_id, ð_hdr->src_addr);
eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
ip_hdr->version_ihl = RTE_IPV4_VHL_DEF;
ip_hdr->type_of_service = 0;
ip_hdr->total_length = rte_cpu_to_be_16(sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_udp_hdr) + sizeof(struct Data));
ip_hdr->packet_id = 0;
ip_hdr->fragment_offset = 0;
ip_hdr->time_to_live = 64;
ip_hdr->next_proto_id = IPPROTO_UDP;
ip_hdr->hdr_checksum = 0;
ip_hdr->src_addr = rte_cpu_to_be_32(IPv4(127, 0, 0, 1));
ip_hdr->dst_addr = rte_cpu_to_be_32(IPv4(127, 0, 0, 1));
udp_hdr->src_port = rte_cpu_to_be_16(1234);
udp_hdr->dst_port = rte_cpu_to_be_16(5678);
udp_hdr->dgram_len = rte_cpu_to_be_16(sizeof(struct rte_udp_hdr) + sizeof(struct Data));
udp_hdr->dgram_cksum = 0;
*payload = *data;
mbuf->l2_len = sizeof(struct rte_ether_hdr);
mbuf->l3_len = sizeof(struct rte_ipv4_hdr);
mbuf->data_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_udp_hdr) + sizeof(struct Data);
mbuf->pkt_len = mbuf->data_len;
uint16_t nb_tx = rte_eth_tx_burst(port_id, 0, &mbuf, 1);
if (nb_tx < 1) {
rte_pktmbuf_free(mbuf);
std::cerr << "Failed to send packetn";
}
}
int main(int argc, char *argv[]) {
dpdk_init(argc, argv);
uint16_t port_id = 0;
if (rte_eth_dev_count_avail() < 1)
rte_exit(EXIT_FAILURE, "No Ethernet ports - byen");
rte_eth_dev_start(port_id); // rte_eth_dev_configure(port_id, 1, 1, NULL); //rte_eth_dev_start(port_id); //
rte_eth_rx_queue_setup(port_id, 0, 128, rte_eth_dev_socket_id(port_id), NULL, mbuf_pool);
rte_eth_tx_queue_setup(port_id, 0, 128, rte_eth_dev_socket_id(port_id), NULL);
// rte_eth_dev_start(port_id);
rte_eth_promiscuous_enable(port_id);
struct Data data = {1, 2, 3, 4, 5, 6};
udp_client_send(mbuf_pool, port_id, &data);
return 0;
}
============= upd server ==============
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include <rte_udp.h>
#include <rte_ip.h>
#include <rte_ether.h>
#include <rte_malloc.h>
#include <rte_lcore.h>
#include <rte_cycles.h>
#include <rte_errno.h>
#include <iostream>
#include <cstring>
#define MBUF_CACHE_SIZE 250
#define NUM_MBUFS (4096-1)
#define IPv4(a, b, c, d) ((uint32_t)(((a) << 24) | ((b) << 16) | ((c) << 8) | (d)))
struct Data {
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
uint64_t e;
uint64_t f;
};
static struct rte_mempool *mbuf_pool;
int dpdk_init(int argc, char *argv[]) {
int ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Error with EAL initializationn");
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * rte_lcore_count(),
MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
if (mbuf_pool == NULL)
rte_exit(EXIT_FAILURE, "Cannot create mbuf pooln");
return ret;
}
void udp_server_receive(uint16_t port_id) {
struct rte_mbuf *bufs[32];
uint16_t nb_rx;
while (true) {
nb_rx = rte_eth_rx_burst(port_id, 0, bufs, 32);
if (nb_rx > 0) {
for (uint16_t i = 0; i < nb_rx; i++) {
struct rte_mbuf *mbuf = bufs[i];
struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
struct rte_ipv4_hdr *ip_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
struct rte_udp_hdr *udp_hdr = (struct rte_udp_hdr *)(ip_hdr + 1);
struct Data *data = (struct Data *)(udp_hdr + 1);
// 处理数据包
std::cout << "Received data: " << data->a << ", " << data->b << ", " << data->c << ", "
<< data->d << ", " << data->e << ", " << data->f << std::endl;
rte_pktmbuf_free(mbuf);
}
}
}
}
int main(int argc, char *argv[]) {
dpdk_init(argc, argv);
uint16_t port_id = 0;
if (rte_eth_dev_count_avail() < 1)
rte_exit(EXIT_FAILURE, "No Ethernet ports - byen");
struct rte_eth_conf port_conf = {};
rte_eth_dev_configure(port_id, 1, 1, &port_conf);
rte_eth_rx_queue_setup(port_id, 0, 128, rte_eth_dev_socket_id(port_id), NULL, mbuf_pool);
rte_eth_tx_queue_setup(port_id, 0, 128, rte_eth_dev_socket_id(port_id), NULL);
rte_eth_dev_start(port_id);
rte_eth_promiscuous_enable(port_id);
if (rte_lcore_id() == 0) {
udp_server_receive(port_id);
}
return 0;
}
tks a lot….
fix my pr / show some new way / gave me some advice / share doc to me