1.DPDK是什么?
DPDK是一款性能极高的网络驱动组件,可以开辟一条可以不经过传统的数据流处理流程“通路”,可以直接接管网卡从网络环境里接受的数据。DPDK内部封装了一系列的方法,可以将数据处理完成后重新写入协议栈,也可以写到VFS模块和POSIX,实现直接和应用层其他app进行数据的交互。
2.DPDK能够做什么?
DPDK设计的初衷是提高网络的吞吐量。可以做的方面包括,对网络吞吐量要求高的所有的地方:防火墙、云主机、泛洪测试工具、CDN等等
3.DPDK的server端代码
#include<unistd.h>
#include<stdio.h>
#include<rte_eal.h>
#include<rte_ethdev.h>
#include<arpa/inet.h>
int gDpdkdPortID=0;
#define MEMPOOLNUMBER 1024
#define BURST_SIZE 128
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,"EORROR with EAL init\n");
}
uint16_t nb_sys_ports=rte_eth_dev_count_avail();
if(nb_sys_ports==0){
rte_exit(EXIT_FAILURE,"NO SPPORT ETH found\n");
}
printf("nb_sys_ports:%d\n",nb_sys_ports);
struct rte_mempool *mbuf_pool=rte_pktmbuf_pool_create("mbufpool",MEMPOOLNUMBER,0,0,RTE_MBUF_DEFAULT_BUF_SIZE,rte_socket_id());
if(!mbuf_pool){
rte_exit(EXIT_FAILURE,"mbuf pool create failed\n");
}
struct rte_eth_dev_info dev_info;
rte_eth_dev_info_get(gDpdkdPortID,&dev_info);
const int num_rx_queues=1;
const int num_tx_queues=0;
struct rte_eth_conf port_conf =port_conf_default;
rte_eth_dev_configure(gDpdkdPortID,num_rx_queues,num_tx_queues,&port_conf);
if(rte_eth_rx_queue_setup(gDpdkdPortID,0,128,rte_eth_dev_socket_id(gDpdkdPortID),NULL,mbuf_pool)<0){
rte_exit(EXIT_FAILURE,"rte_eth_rx_queue_setup create failed\n");
}
if(rte_eth_dev_start(gDpdkdPortID)<0){
rte_exit(EXIT_FAILURE,"not start \n");
}
printf("hello!\n");
while(1){
struct rte_mbuf *mbufs[BURST_SIZE];
unsigned nb_recvd=rte_eth_rx_burst(gDpdkdPortID,0,mbufs,BURST_SIZE);
if(nb_recvd>BURST_SIZE){
rte_exit(EXIT_FAILURE,"EORROR WITH rte_eth");
}
unsigned i=0;
for(i=0;i<nb_recvd;i++){
struct rte_ether_hdr *ehdr=rte_pktmbuf_mtod(mbufs[i],struct rte_ether_hdr *);
if(ehdr->ether_type!=rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)){
continue;
}
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 =udphdr->dgram_len;
printf("length :%d,context: %s\n",length,(char*)udphdr+1);
}
}
}
return 0;
}
后续将持续更新新的代码,此部分代码实现UDP数据的接收。