dpdk的实现,udp编程

1.配置多网卡

1.1 查看是否支持多网卡

首先 ifconfig 查看你的网卡名,如ens33

然后cat /proc/interrupts |grep ens33

如果是

则不支持,需要打开配置文件,在虚拟机目录中,找到后缀为vmx文件,并打开

修改或添加两行

修改huge page设置

1.2 配置dpdk环境

首先下载源码。

然后编译

linuxapp和linux版本,如果不修改源码,则使用linuxapp,否则linux

1.3 设置arp

显示为

配置

2. udp编程

2.1.给发送数据打包

1.导入包,定义地址


#include <stdio.h>

#include <rte_eal.h>
#include <rte_ethdev.h>

#include <arpa/inet.h>

int global_portid = 0;


#define NUM_MBUFS  4096
#define BURST_SIZE	128
uint8_t global_smac[RTE_ETHER_ADDR_LEN];
uint8_t global_dmac[RTE_ETHER_ADDR_LEN];


uint32_t global_sip;
uint32_t global_dip;

uint16_t global_sport;
uint16_t global_dport;


#define ENABLE_SEND		1
#define ENABLE_TCP		1

#define TCP_INIT_WINDOWS		14600

2. 打包,分别是以太网,ip层,udp层

int ustack_encode_udp_pkt(uint8_t *msg, uint8_t *data, uint16_t total_len){
	struct rte_ether_hdr* ethhdr = (struct rte_ether_hdr*) msg;
	rte_memcpy(ethhdr->d_addr.addr_bytes, global_dmac, RTE_ETHER_ADDR_LEN);
	rte_memcpy(ethhdr->s_addr.addr_bytes, global_smac, RTE_ETHER_ADDR_LEN);
	ethhdr->ether_type = htons(RTE_ETHER_TYPE_IPV4);

	struct rte_ipv4_hdr* iphdr = (struct rte_ipv4_hdr*)(ethhdr + 1);

	iphdr->version_ihl = 0x45;
	iphdr->type_of_service = 0;
	iphdr->total_length = total_len - sizeof(struct rte_ether_hdr);
	iphdr->packet_id = 0;
	iphdr->fragment_offset = 0;
	iphdr->time_to_live = 64;
	iphdr->next_proto_id = IPPROTO_UDP;
	iphdr->src_addr = global_sip;
	iphdr->dst_addr = global_dip;

	iphdr->hdr_checksum = 0;
	iphdr->hdr_checksum = rte_ipv4_cksum(iphdr);

	struct rte_udp_hdr* udphdr = (struct rte_udp_hdr*)(iphdr + 1);
	udphdr->src_port = global_sport;
	udphdr->dst_port = global_dport;

	int16_t udplen = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);
	udphdr->dgram_len = htons(udplen);
	udphdr->dgram_cksum = 0;
	udphdr->dgram_cksum = rte_ipv4_udptcp_cksum(iphdr, udphdr);
    rte_memcpy((uint8_t*)(udp+1), data, udplen);
	return 0;
	
}

3. 给dpdk绑定网卡信息,初始化

static int unstack_init_port(struct rte_mempool *mbuf_pool){
	uint16_t nb_sys_ports = rte_eth_dev_count_avail();
	printf("num: %d \n", nb_sys_ports);
	if(nb_sys_ports==0){rte_exit(EXIT_FAILURE, "no dev available\n");}
	const int num_rx_queues = 1;
	const int num_tx_queues = 1;
	struct rte_eth_dev_info dev_info;
	rte_eth_dev_info_get(global_portid, &dev_info);
	rte_eth_dev_configure(global_portid, num_rx_queues, num_tx_queues
		, &port_conf_default);
	if (rte_eth_rx_queue_setup(global_portid, 0, 128, rte_eth_dev_socket_id(global_portid), NULL, mbuf_pool) < 0) {
			rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");
	}

	struct rte_eth_txconf txq_conf = dev_info.default_txconf;
	txq_conf.offloads = port_conf_default.rxmode.offloads;

	if(rte_eth_tx_queue_setup(global_portid, 0, 512, rte_eth_dev_socket_id(global_portid),&txq_conf)<0){
		rte_exit(EXIT_FAILURE, "tx not setup\n");

	}
	if (rte_eth_dev_start(global_portid<0)){
		rte_exit(EXIT_FAILURE, "Could not start\n");
	}
		return 0;
}

3.tcp编程

直接全部给出



#include <stdio.h>

#include <rte_eal.h>
#include <rte_ethdev.h>

#include <arpa/inet.h>

int global_portid = 0;


#define NUM_MBUFS  4096
#define BURST_SIZE	128
uint8_t global_smac[RTE_ETHER_ADDR_LEN];
uint8_t global_dmac[RTE_ETHER_ADDR_LEN];


uint32_t global_sip;
uint32_t global_dip;

uint16_t global_sport;
uint16_t global_dport;


uint8_t global_flags;
uint32_t global_seqnum;
uint32_t global_acknum;

#define ENABLE_SEND		1
#define ENABLE_TCP		1

#define TCP_INIT_WINDOWS		14600
typedef enum __USTACK_TCP_STATUS {

	USTACK_TCP_STATUS_CLOSED = 0,
	USTACK_TCP_STATUS_LISTEN,
	USTACK_TCP_STATUS_SYN_RCVD,
	USTACK_TCP_STATUS_SYN_SENT,
	USTACK_TCP_STATUS_ESTABLISHED,
	
	USTACK_TCP_STATUS_FIN_WAIT_1,
	USTACK_TCP_STATUS_FIN_WAIT_2,
	USTACK_TCP_STATUS_CLOSING,
	USTACK_TCP_STATUS_TIMEWAIT,
	USTACK_TCP_STATUS_CLOSE_WAIT,
	USTACK_TCP_STATUS_LAST_ACK
	
} USTACK_TCP_STATUS;

uint8_t tcp_status = USTACK_TCP_STATUS_LISTEN;

static const struct rte_eth_conf port_conf_default = {
	.rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN}
};
static int unstack_init_port(struct rte_mempool *mbuf_pool){
	uint16_t nb_sys_ports = rte_eth_dev_count_avail();
	printf("num: %d \n", nb_sys_ports);
	if(nb_sys_ports==0){rte_exit(EXIT_FAILURE, "no dev available\n");}
	const int num_rx_queues = 1;
	const int num_tx_queues = 1;
	struct rte_eth_dev_info dev_info;
	rte_eth_dev_info_get(global_portid, &dev_info);
	rte_eth_dev_configure(global_portid, num_rx_queues, num_tx_queues
		, &port_conf_default);
	if (rte_eth_rx_queue_setup(global_portid, 0, 128, rte_eth_dev_socket_id(global_portid), NULL, mbuf_pool) < 0) {
			rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");
	}

	struct rte_eth_txconf txq_conf = dev_info.default_txconf;
	txq_conf.offloads = port_conf_default.rxmode.offloads;

	if(rte_eth_tx_queue_setup(global_portid, 0, 512, rte_eth_dev_socket_id(global_portid),&txq_conf)<0){
		rte_exit(EXIT_FAILURE, "tx not setup\n");

	}
	if (rte_eth_dev_start(global_portid)<0){
		rte_exit(EXIT_FAILURE, "Could not start\n");
	}
		return 0;
}
int ustack_encode_tcp_pkt(uint8_t *msg,uint16_t total_len){
	struct rte_ether_hdr* ethhdr = (struct rte_ether_hdr*) msg;
	rte_memcpy(ethhdr->d_addr.addr_bytes, global_dmac, RTE_ETHER_ADDR_LEN);
	rte_memcpy(ethhdr->s_addr.addr_bytes, global_smac, RTE_ETHER_ADDR_LEN);
	ethhdr->ether_type = htons(RTE_ETHER_TYPE_IPV4);

	struct rte_ipv4_hdr* iphdr = (struct rte_ipv4_hdr*)(ethhdr + 1);

	iphdr->version_ihl = 0x45;
	iphdr->type_of_service = 0;
	iphdr->total_length = htons(total_len - sizeof(struct rte_ether_hdr));
	iphdr->packet_id = 0;
	iphdr->fragment_offset = 0;
	iphdr->time_to_live = 64;
	iphdr->next_proto_id = IPPROTO_TCP;
	iphdr->src_addr = global_sip;
	iphdr->dst_addr = global_dip;

	iphdr->hdr_checksum = 0;
	iphdr->hdr_checksum = rte_ipv4_cksum(iphdr);

	struct rte_tcp_hdr* tcphdr = (struct rte_tcp_hdr*)(iphdr+1);
	tcphdr->src_port = global_sport;
	tcphdr->dst_port = global_dport;
	tcp->sent_seq = htonl(12345);
	tcp->recv_ack = htonl(global_seqnum + 1); 
	tcp->data_off = 0x50;
	tcp->tcp_flags = RTE_TCP_SYN_FLAG | RTE_TCP_ACK_FLAG; //0x1 << 1;

	tcphdr->rx_win = htons(4096);
	tcphdr->cksum=0;
	tcphdr->cksum = rte_ipv4_udptcp_cksum(iphdr, tcphdr);
	
	return 0;
}

int ustack_encode_udp_pkt(uint8_t *msg,  uint8_t *data, uint16_t total_len){
	struct rte_ether_hdr* ethhdr = (struct rte_ether_hdr*) msg;
	rte_memcpy(ethhdr->d_addr.addr_bytes, global_dmac, RTE_ETHER_ADDR_LEN);
	rte_memcpy(ethhdr->s_addr.addr_bytes, global_smac, RTE_ETHER_ADDR_LEN);
	ethhdr->ether_type = htons(RTE_ETHER_TYPE_IPV4);

	struct rte_ipv4_hdr* iphdr = (struct rte_ipv4_hdr*)(ethhdr + 1);

	iphdr->version_ihl = 0x45;
	iphdr->type_of_service = 0;
	iphdr->total_length = htons(total_len - sizeof(struct rte_ether_hdr));
	iphdr->packet_id = 0;
	iphdr->fragment_offset = 0;
	iphdr->time_to_live = 64;
	iphdr->next_proto_id = IPPROTO_UDP;
	iphdr->src_addr = global_sip;
	iphdr->dst_addr = global_dip;

	iphdr->hdr_checksum = 0;
	iphdr->hdr_checksum = rte_ipv4_cksum(iphdr);

	struct rte_udp_hdr* udphdr = (struct rte_udp_hdr*)(iphdr + 1);
	udphdr->src_port = global_sport;
	udphdr->dst_port = global_dport;

	int16_t udplen = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);
	udphdr->dgram_len = htons(udplen);
	udphdr->dgram_cksum = 0;
	udphdr->dgram_cksum = rte_ipv4_udptcp_cksum(iphdr, udphdr);
	rte_memcpy((uint8_t*)(udphdr+1), data, udplen);

	return 0;
	
}

int main(int argc, char *argv[]){
	if(rte_eal_init(argc, argv) < 0){
		rte_exit(EXIT_FAILURE, "Error eal init\n");
	}
	struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbuf pool", NUM_MBUFS,0,0,RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
	if (mbuf_pool == NULL) {
			rte_exit(EXIT_FAILURE, "Could not create mbuf pool\n");
		}

	unstack_init_port(mbuf_pool);
	while(1){
		struct rte_mbuf *mbufs[BURST_SIZE] = {0};
		uint16_t num_recvd = rte_eth_rx_burst(global_portid, 0, mbufs, BURST_SIZE);
		if(num_recvd >BURST_SIZE){
			rte_exit(EXIT_FAILURE, "recv error\n");
		}
		for(int i =0;i<num_recvd;i++){
			struct rte_ether_hdr *ethhdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);

			if(ntohs(ethhdr->ether_type) != 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 (ntohs(iphdr->next_proto_id) == IPPROTO_UDP){
				struct rte_udp_hdr *udphdr = (struct rte_udp_hdr*)(iphdr+1);
				rte_memcpy(global_smac, ethhdr->d_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
				rte_memcpy(global_dmac, ethhdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);

				rte_memcpy(&global_sip, &iphdr->dst_addr, sizeof(int32_t));
				rte_memcpy(&global_dip, &iphdr->src_addr, sizeof(int32_t));

				rte_memcpy(&global_sport, &udphdr->dst_port, sizeof(int16_t));
				rte_memcpy(&global_dport, &udphdr->src_port, sizeof(int16_t));

				struct in_addr saddr;
				saddr.s_addr = iphdr->src_addr;
				printf("接受ip为 %s %d\n", inet_ntoa(saddr), ntohs(udphdr->src_port));

				saddr.s_addr = iphdr->dst_addr;
				printf("目的ip为 %s %d\n", inet_ntoa(saddr), ntohs(udphdr->dst_port));
				

				uint16_t total_length = ntohs(udphdr->dgram_len) + sizeof(iphdr) + sizeof(ethhdr);
				struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
				mbuf->pkt_len = total_length;
				mbuf->data_len = total_length;

				uint8_t *msg = rte_pktmbuf_mtod(mbuf, uint8_t*);
				ustack_encode_udp_pkt(msg, (uint8_t*)(udphdr+1), total_length);
				rte_eth_tx_burst(global_portid, 0,mbufs, 1);
				printf("udp:%s\n",(char*)(udphdr+1));
			}
			else if (iphdr->next_proto_id == IPPROTO_TCP) {
			
				struct rte_tcp_hdr *tcphdr = (struct rte_tcp_hdr *)(iphdr + 1);

				rte_memcpy(global_smac, ethhdr->d_addr.addr_bytes, RTE_ETHER_ADDR_LEN);
				rte_memcpy(global_dmac, ethhdr->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);

				rte_memcpy(&global_sip, &iphdr->dst_addr, sizeof(uint32_t));
				rte_memcpy(&global_dip, &iphdr->src_addr, sizeof(uint32_t));

				rte_memcpy(&global_sport, &tcphdr->dst_port, sizeof(uint16_t));
				rte_memcpy(&global_dport, &tcphdr->src_port, sizeof(uint16_t));

				uint16_t total_length = sizeof(tcphdr) + sizeof(iphdr) + sizeof(ethhdr);
				struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
				mbuf->pkt_len = total_length;
				mbuf->data_len = total_length;

				uint8_t *msg = rte_pktmbuf_mtod(mbuf, uint8_t*);
				ustack_encode_tcp_pkt(msg,  total_length);

				
				global_flags = tcphdr->tcp_flags;
				global_seqnum = ntohl(tcphdr->sent_seq);
				global_acknum = ntohl(tcphdr->recv_ack);

				struct in_addr addr;
				addr.s_addr = iphdr->src_addr;
				printf("tcp pkt sip %s:%d --> ", inet_ntoa(addr), ntohs(tcphdr->src_port));

				addr.s_addr = iphdr->dst_addr;
				printf("dip %s:%d , flags: %x, seqnum: %d, acknum: %d\n", inet_ntoa(addr), ntohs(tcphdr->dst_port), 
					global_flags, global_seqnum, global_acknum);


				if(global_flags & RTE_TCP_SYN_FLAG){

					if (tcp_status == USTACK_TCP_STATUS_LISTEN){
						tcp_status = USTACK_TCP_STATUS_SYN_RCVD;
						uint16_t lenth = sizeof(ethhdr) + sizeof(iphdr) + sizeof(tcphdr);

						struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
						mbuf->pkt_len = lenth;
						mbuf->data_len = lenth;

						uint8_t *msg = rte_pktmbuf_mtod(mbuf, uint8_t *);
						
						ustack_encode_tcp_pkt(msg, lenth);
						rte_eth_tx_burst(global_portid, 0, &mbuf, 1);
						tcp_status = USTACK_TCP_STATUS_SYN_RCVD;
					}
				}

				if (global_flags & RTE_TCP_ACK_FLAG){
					if (tcp_status == USTACK_TCP_STATUS_SYN_RCVD){
						printf("link is established. \n");
						tcp_status = USTACK_TCP_STATUS_ESTABLISHED;
					}
				}

				if (global_flags & RTE_TCP_PUSH_FLAG){
					if (tcp_status == USTACK_TCP_STATUS_ESTABLISHED){
						print("recv message.\n");
						uint8_t hdr_len = (tcphdr->dataoff>>4) * sizeof(uint32_t);
						uint8_t data = ((uint8_t*)tcphdr + hdr_len);
						print("data: %s \n", data);
					}
					
					
				}
				
				}
		}
			}
	

} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值