Merge pull request #475 from jinhao2/dev

modify dump codes.
dev
johnjiang 2020-01-10 14:36:47 +08:00 committed by GitHub
commit 4ad0b6400b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 33 deletions

View File

@ -61,6 +61,14 @@ nb_vdev=0
# Number of bond.
nb_bond=0
# Each core write into own pcap file, which is open one time, close one time if enough.
# Support dump the first snaplen bytes of each packet.
# if pcap file is lager than savelen bytes, it will be closed and next file was dumped into.
[pcap]
enable = 0
snaplen= 96
savelen= 16777216
# Port config section
# Correspond to dpdk.port_list's index: port0, port1...
[port0]

View File

@ -615,6 +615,16 @@ ini_parse_handler(void* user, const char* section, const char* name,
return vdev_cfg_handler(pconfig, section, name, value);
} else if (strncmp(section, "bond", 4) == 0) {
return bond_cfg_handler(pconfig, section, name, value);
} else if (strcmp(section, "pcap") == 0) {
if (strcmp(name, "snaplen") == 0) {
pconfig->pcap.snap_len = (uint16_t)atoi(value);
} else if (strcmp(name, "savelen") == 0) {
pconfig->pcap.save_len = (uint32_t)atoi(value);
} else if (strcmp(name, "enable") == 0) {
pconfig->pcap.enable = (uint16_t)atoi(value);
} else if (strcmp(name, "savepath") == 0) {
pconfig->pcap.save_path = strdup(value);
}
}
return 1;
@ -804,6 +814,13 @@ ff_check_config(struct ff_config *cfg)
}
}
if ( cfg->pcap.save_len < PCAP_SAVE_MINLEN )
cfg->pcap.save_len = PCAP_SAVE_MINLEN;
if (cfg->pcap.snap_len < PCAP_SNAP_MINLEN)
cfg->pcap.snap_len = PCAP_SNAP_MINLEN;
if ( cfg->pcap.save_path==NULL || strlen(cfg->pcap.save_path) ==0)
cfg->pcap.save_path = strdup(".");
#define CHECK_VALID(n) \
do { \
if (!pc->n) { \

View File

@ -35,6 +35,8 @@ extern "C" {
#define DPDK_CONFIG_NUM 16
#define DPDK_CONFIG_MAXLEN 256
#define DPDK_MAX_LCORE 128
#define PCAP_SNAP_MINLEN 94
#define PCAP_SAVE_MINLEN (2<<22)
extern int dpdk_argc;
extern char *dpdk_argv[DPDK_CONFIG_NUM + 1];
@ -60,6 +62,8 @@ struct ff_port_cfg {
char *broadcast;
char *gateway;
char *pcap;
uint16_t snaplen;
uint32_t savelen;
int nb_lcores;
int nb_slaves;
@ -163,6 +167,13 @@ struct ff_config {
int fd_reserve;
int mem_size;
} freebsd;
struct {
uint16_t enable;
uint16_t snap_len;
uint32_t save_len;
char* save_path;
} pcap;
};
extern struct ff_config ff_global_cfg;

View File

@ -274,7 +274,11 @@ init_lcore_conf(void)
lcore_conf.tx_port_id[lcore_conf.nb_tx_port] = port_id;
lcore_conf.nb_tx_port++;
lcore_conf.pcap[port_id] = pconf->pcap;
/* Enable pcap dump */
if (ff_global_cfg.pcap.enable) {
ff_enable_pcap(ff_global_cfg.pcap.save_path, ff_global_cfg.pcap.snap_len);
}
lcore_conf.nb_queue_list[port_id] = pconf->nb_lcores;
}
@ -773,11 +777,6 @@ init_port_start(void)
printf("set port %u to promiscuous mode error\n", port_id);
}
}
/* Enable pcap dump */
if (pconf->pcap) {
ff_enable_pcap(pconf->pcap);
}
}
}
@ -1017,9 +1016,9 @@ process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs,
for (i = 0; i < count; i++) {
struct rte_mbuf *rtem = bufs[i];
if (unlikely(qconf->pcap[port_id] != NULL)) {
if (unlikely( ff_global_cfg.pcap.enable)) {
if (!pkts_from_ring) {
ff_dump_packets(qconf->pcap[port_id], rtem);
ff_dump_packets( ff_global_cfg.pcap.save_path, rtem, ff_global_cfg.pcap.snap_len, ff_global_cfg.pcap.save_len);
}
}
@ -1326,10 +1325,11 @@ send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
queueid = qconf->tx_queue_id[port];
m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
if (unlikely(qconf->pcap[port] != NULL)) {
if (unlikely(ff_global_cfg.pcap.enable)) {
uint16_t i;
for (i = 0; i < n; i++) {
ff_dump_packets(qconf->pcap[port], m_table[i]);
ff_dump_packets( ff_global_cfg.pcap.save_path, m_table[i],
ff_global_cfg.pcap.snap_len, ff_global_cfg.pcap.save_len);
}
}

View File

@ -26,8 +26,11 @@
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include "ff_dpdk_pcap.h"
#define FILE_PATH_LEN 64
#define PCAP_FILE_NUM 10
struct pcap_file_header {
uint32_t magic;
@ -46,14 +49,21 @@ struct pcap_pkthdr {
uint32_t len; /* length this packet (off wire) */
};
int
ff_enable_pcap(const char* dump_path)
static __thread FILE* g_pcap_fp = NULL;
static __thread uint32_t seq = 0;
static __thread uint32_t g_flen = 0;
int ff_enable_pcap(const char* dump_path, uint16_t snap_len)
{
FILE* fp = fopen(dump_path, "w");
if (fp == NULL) {
rte_exit(EXIT_FAILURE, "Cannot open pcap dump path: %s\n", dump_path);
char pcap_f_path[FILE_PATH_LEN] = {0};
snprintf(pcap_f_path, FILE_PATH_LEN, "%s/cpu%d_%d.pcap", dump_path==NULL?".":dump_path, rte_lcore_id(), seq);
g_pcap_fp = fopen(pcap_f_path, "w+");
if (g_pcap_fp == NULL) {
rte_exit(EXIT_FAILURE, "Cannot open pcap dump path: %s, errno %d.\n", pcap_f_path, errno);
return -1;
}
g_flen = 0;
struct pcap_file_header pcap_file_hdr;
void* file_hdr = &pcap_file_hdr;
@ -63,40 +73,52 @@ ff_enable_pcap(const char* dump_path)
pcap_file_hdr.version_minor = 0x0004;
pcap_file_hdr.thiszone = 0x00000000;
pcap_file_hdr.sigfigs = 0x00000000;
pcap_file_hdr.snaplen = 0x0000FFFF; //65535
pcap_file_hdr.snaplen = snap_len; //0x0000FFFF; //65535
pcap_file_hdr.linktype = 0x00000001; //DLT_EN10MB, Ethernet (10Mb)
fwrite(file_hdr, sizeof(struct pcap_file_header), 1, fp);
fclose(fp);
fwrite(file_hdr, sizeof(struct pcap_file_header), 1, g_pcap_fp);
g_flen += sizeof(struct pcap_file_header);
return 0;
}
int
ff_dump_packets(const char* dump_path, struct rte_mbuf* pkt)
ff_dump_packets(const char* dump_path, struct rte_mbuf* pkt, uint16_t snap_len, uint32_t f_maxlen)
{
FILE* fp = fopen(dump_path, "a");
if (fp == NULL) {
return -1;
}
unsigned int out_len = 0, wr_len = 0;
struct pcap_pkthdr pcap_hdr;
void* hdr = &pcap_hdr;
struct timeval ts;
char pcap_f_path[FILE_PATH_LEN] = {0};
if (g_pcap_fp == NULL) {
return -1;
}
snap_len = pkt->pkt_len < snap_len ? pkt->pkt_len : snap_len;
gettimeofday(&ts, NULL);
pcap_hdr.sec = ts.tv_sec;
pcap_hdr.usec = ts.tv_usec;
pcap_hdr.caplen = pkt->pkt_len;
pcap_hdr.caplen = snap_len;
pcap_hdr.len = pkt->pkt_len;
fwrite(hdr, sizeof(struct pcap_pkthdr), 1, fp);
fwrite(hdr, sizeof(struct pcap_pkthdr), 1, g_pcap_fp);
g_flen += sizeof(struct pcap_pkthdr);
while(pkt != NULL) {
fwrite(rte_pktmbuf_mtod(pkt, char*), pkt->data_len, 1, fp);
while(pkt != NULL && out_len <= snap_len) {
wr_len = snap_len - out_len;
wr_len = wr_len > pkt->data_len ? pkt->data_len : wr_len ;
fwrite(rte_pktmbuf_mtod(pkt, char*), wr_len, 1, g_pcap_fp);
out_len += wr_len;
pkt = pkt->next;
}
g_flen += out_len;
fclose(fp);
if ( g_flen >= f_maxlen ){
fclose(g_pcap_fp);
if ( ++seq >= PCAP_FILE_NUM )
seq = 0;
ff_enable_pcap(dump_path, snap_len);
}
return 0;
}

View File

@ -30,8 +30,8 @@
#include <rte_config.h>
#include <rte_mbuf.h>
int ff_enable_pcap(const char* dump_path);
int ff_dump_packets(const char* dump_path, struct rte_mbuf *pkt);
int ff_enable_pcap(const char* dump_path, uint16_t snap_len);
int ff_dump_packets(const char* dump_path, struct rte_mbuf *pkt, uint16_t snap_len, uint32_t f_maxlen);
#endif /* ifndef _FSTACK_DPDK_PCAP_H */

View File

@ -89,7 +89,7 @@ struct lcore_conf {
uint16_t tx_port_id[RTE_MAX_ETHPORTS];
uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
char *pcap[RTE_MAX_ETHPORTS];
//char *pcap[RTE_MAX_ETHPORTS];
} __rte_cache_aligned;
#ifdef FF_USE_PAGE_ARRAY