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
| ##include <stdio.h> ##include <rte_eal.h> ##include <rte_mbuf.h> ##include <rte_ethdev.h> ##include <arpa/inet.h>
##define MBUF_COUNT 4096 ##define BURST_SIZE 32
int gDpdkPortId = 0;
static const struct rte_eth_conf port_conf_default = { .rxmode = { .max_rx_pkt_len = RTE_ETHER_MAX_LEN } };
int main(int argc, char *argv[]) { if(rte_eal_init(argc, argv) < 0){ rte_exit(EXIT_FAILURE, "Failed to init EAL\n"); }
struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbufpool", MBUF_COUNT, 0, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); struct rte_eth_conf port_conf = port_conf_default; int num_rx_queues = 1; int num_tx_queues = 0; rte_eth_dev_configure(gDpdkPortId, num_rx_queues, num_tx_queues, &port_conf );
rte_eth_rx_queue_setup(gDpdkPortId, 0, 128, rte_eth_dev_socket_id(gDpdkPortId), NULL, mbuf_pool); rte_eth_dev_start(gDpdkPortId);
while(1) { struct rte_mbuf *mbufs[BURST_SIZE]; unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE); if(num_recvd > BURST_SIZE){ rte_exit(EXIT_FAILURE, "Failed to rte_eth_rx_burst\n"); } unsigned i = 0; for(; i < num_recvd ; i++){ struct rte_ether_hdr *ehdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr *); if(ehdr->ether_type == htons(RTE_ETHER_TYPE_IPV4)) {
struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr)); if(iphdr->next_proto_id == IPPROTO_UDP) {
struct rte_udp_hdr *udphdr = (struct rte_udp_hdr *)(iphdr + 1); uint16_t length = ntohs(udphdr->dgram_len); *((char*)udphdr + length) = '\0';
printf("data : %s\n", (char*)(udphdr + 1)); } } } } }
|