Add files via upload

This commit is contained in:
abu330 2024-12-27 22:27:57 +08:00 committed by GitHub
parent f20ad1a420
commit 0c57070d72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 160 additions and 0 deletions

31
custom_stack/Makefile Normal file
View File

@ -0,0 +1,31 @@
APP = dpdklib
SRCDIR = src
INCDIR = include
OBJDIR = build
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJS = $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS))
RTE_SDK = /root/task10/dpdk-22.07
RTE_TARGET = x86_64-native-linuxapp-gcc
DPDK_INC = $(RTE_SDK)/$(RTE_TARGET)/include
DPDK_LIB = $(RTE_SDK)/$(RTE_TARGET)/lib
CC = gcc
CFLAGS = -O3 -g -Wall -I$(INCDIR) -I$(DPDK_INC)
LDFLAGS = -L$(DPDK_LIB) -Wl,-rpath,$(DPDK_LIB) -ldpdk
all: $(APP)
$(APP): $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR) $(APP)
.PHONY: all clean

View File

@ -0,0 +1,7 @@
#ifndef DPDK_INIT_H
#define DPDK_INIT_H
void init_dpdk(void);
void receive_packets(void);
#endif // DPDK_INIT_H

View File

@ -0,0 +1,8 @@
#ifndef PACKET_PARSER_H
#define PACKET_PARSER_H
void process_packet(struct rte_mbuf *mbuf);
void process_packet_with_boundaries(struct rte_mbuf *mbuf);
void save_packet_to_file(struct rte_mbuf *mbuf);
#endif // PACKET_PARSER_H

View File

@ -0,0 +1,55 @@
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mempool.h>
#include <stdio.h>
#include "dpdk_init.h"
#define NUM_PORTS 1
#define RX_QUEUE_SIZE 1024
void init_dpdk() {
int ret = rte_eal_init(0, NULL);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "DPDK initialization failed\n");
}
uint16_t port_id = 0;
if (!rte_eth_dev_is_valid_port(port_id)) {
rte_exit(EXIT_FAILURE, "Invalid port id\n");
}
struct rte_eth_conf port_conf = {0};
ret = rte_eth_dev_configure(port_id, 1, 1, &port_conf);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "Failed to configure the port\n");
}
ret = rte_eth_rx_queue_setup(port_id, 0, RX_QUEUE_SIZE, rte_eth_dev_socket_id(port_id), NULL, NULL);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "Failed to setup RX queue\n");
}
ret = rte_eth_tx_queue_setup(port_id, 0, RX_QUEUE_SIZE, rte_eth_dev_socket_id(port_id), NULL);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "Failed to setup TX queue\n");
}
ret = rte_eth_dev_start(port_id);
if (ret < 0) {
rte_exit(EXIT_FAILURE, "Failed to start the port\n");
}
rte_eth_promiscuous_enable(port_id);
}
void receive_packets() {
struct rte_mbuf *pkts_burst[32];
uint16_t port_id = 0;
int ret = rte_eth_rx_burst(port_id, 0, pkts_burst, 32);
if (ret > 0) {
for (int i = 0; i < ret; i++) {
process_packet(pkts_burst[i]);
rte_pktmbuf_free(pkts_burst[i]);
}
}
}

15
custom_stack/src/main.c Normal file
View File

@ -0,0 +1,15 @@
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>
#include "dpdk_init.h"
#include "packet_parser.h"
int main() {
init_dpdk();
while (1) {
receive_packets();
}
return 0;
}

View File

@ -0,0 +1,44 @@
#include <rte_mbuf.h>
#include <rte_ip.h>
#include <rte_ether.h>
#include <stdio.h>
#include "packet_parser.h"
#define PACKET_START_MAGIC 0x12345678
#define PACKET_END_MAGIC 0x87654321
void process_packet(struct rte_mbuf *mbuf) {
struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
struct ipv4_hdr *ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
if (eth_hdr->ether_type == rte_be_to_cpu_16(ETHERTYPE_IP)) {
printf("Received IP packet, Src IP: %d.%d.%d.%d, Dst IP: %d.%d.%d.%d\n",
(ip_hdr->src_addr >> 24) & 0xFF, (ip_hdr->src_addr >> 16) & 0xFF,
(ip_hdr->src_addr >> 8) & 0xFF, ip_hdr->src_addr & 0xFF,
(ip_hdr->dst_addr >> 24) & 0xFF, (ip_hdr->dst_addr >> 16) & 0xFF,
(ip_hdr->dst_addr >> 8) & 0xFF, ip_hdr->dst_addr & 0xFF);
}
}
void process_packet_with_boundaries(struct rte_mbuf *mbuf) {
struct ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf, struct ether_hdr *);
struct ipv4_hdr *ip_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
if (ip_hdr->src_addr == PACKET_START_MAGIC) {
printf("Packet Start Detected\n");
}
if (ip_hdr->dst_addr == PACKET_END_MAGIC) {
printf("Packet End Detected\n");
}
}
void save_packet_to_file(struct rte_mbuf *mbuf) {
FILE *file = fopen("received_packets.bin", "ab");
if (!file) {
perror("Failed to open file");
return;
}
fwrite(rte_pktmbuf_mtod(mbuf, void *), rte_pktmbuf_data_len(mbuf), 1, file);
fclose(file);
}