Add packet injector test frame

This commit is contained in:
luwenpeng
2024-05-15 11:40:00 +08:00
parent cda77c6f6f
commit b33dafff53
31 changed files with 458 additions and 140 deletions

View File

@@ -120,16 +120,25 @@ struct packet *packet_dup(const struct packet *pkt)
{
return NULL;
}
memcpy(dup_pkt, pkt, sizeof(struct packet));
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
dup_pkt->origin = PACKET_ORIGIN_USERHEAP;
dup_pkt->origin_ctx = NULL;
// update layers
dup_pkt->layers_used = pkt->layers_used;
dup_pkt->layers_size = pkt->layers_size;
memcpy((char *)dup_pkt->data_ptr, pkt->data_ptr, pkt->data_len);
dup_pkt->data_len = pkt->data_len;
dup_pkt->origin_ctx = NULL;
dup_pkt->origin = PACKET_ORIGIN_USERHEAP;
dup_pkt->action = PACKET_ACTION_DROP;
for (int8_t i = 0; i < pkt->layers_used; i++)
{
dup_pkt->layers[i].type = pkt->layers[i].type;
dup_pkt->layers[i].hdr_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset;
dup_pkt->layers[i].pld_ptr = dup_pkt->data_ptr + pkt->layers[i].hdr_offset + pkt->layers[i].hdr_len;
dup_pkt->layers[i].hdr_offset = pkt->layers[i].hdr_offset;
dup_pkt->layers[i].hdr_len = pkt->layers[i].hdr_len;
dup_pkt->layers[i].pld_len = pkt->layers[i].pld_len;
}
// update frag_layer

View File

@@ -26,6 +26,7 @@ struct dumpfile_io
struct io_stat stat[MAX_THREAD_NUM];
uint64_t io_thread_need_exit;
uint64_t io_thread_is_runing;
uint64_t io_thread_wait_exit;
};
struct pcap_pkt
@@ -34,10 +35,90 @@ struct pcap_pkt
int len;
};
struct pcap_file_hdr
{
unsigned int magic;
unsigned short version_major;
unsigned short version_minor;
unsigned int thiszone; // gmt to local correction
unsigned int sigfigs; // accuracy of timestamps
unsigned int snaplen; // max length saved portion of each pkt
unsigned int linktype; // data link type (LINKTYPE_*)
};
struct pcap_pkt_hdr
{
unsigned int tv_sec; // time stamp
unsigned int tv_usec; // time stamp
unsigned int caplen; // length of portion present
unsigned int len; // length this packet (off wire)
};
struct pcap_file_hdr DEFAULT_PCAP_FILE_HDR =
{
.magic = 0xA1B2C3D4,
.version_major = 0x0002,
.version_minor = 0x0004,
.thiszone = 0,
.sigfigs = 0,
.snaplen = 0xFFFF,
.linktype = 1};
/******************************************************************************
* Private API
******************************************************************************/
static void save_packet(struct packet *pkt, uint64_t idx)
{
int len = 0;
FILE *fp = NULL;
struct tuple6 tuple;
struct timeval ts = {0};
struct pcap_pkt_hdr pcap_hdr = {0};
char file[256] = {0};
char src_addr[INET6_ADDRSTRLEN] = {0};
char dst_addr[INET6_ADDRSTRLEN] = {0};
len = packet_get_len(pkt);
memset(&tuple, 0, sizeof(struct tuple6));
packet_get_innermost_tuple6(pkt, &tuple);
if (tuple.ip_type == IP_TYPE_V4)
{
inet_ntop(AF_INET, &tuple.src_addr.v4, src_addr, INET6_ADDRSTRLEN);
inet_ntop(AF_INET, &tuple.dst_addr.v4, dst_addr, INET6_ADDRSTRLEN);
}
else
{
inet_ntop(AF_INET6, &tuple.src_addr.v6, src_addr, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &tuple.dst_addr.v6, dst_addr, INET6_ADDRSTRLEN);
}
snprintf(file, sizeof(file), "/tmp/inject-%s:%u-%s:%u-%lu.pcap", src_addr, ntohs(tuple.src_port), dst_addr, ntohs(tuple.dst_port), idx);
fp = fopen(file, "w+");
if (fp)
{
gettimeofday(&ts, NULL);
pcap_hdr.tv_sec = ts.tv_sec;
pcap_hdr.tv_usec = ts.tv_usec;
pcap_hdr.caplen = len;
pcap_hdr.len = len;
fwrite(&DEFAULT_PCAP_FILE_HDR, sizeof(DEFAULT_PCAP_FILE_HDR), 1, fp);
fwrite(&pcap_hdr, sizeof(struct pcap_pkt_hdr), 1, fp);
fwrite(packet_get_data(pkt), 1, len, fp);
fflush(fp);
fclose(fp);
PACKET_IO_LOG_DEBUG("save packet to %s", file);
}
else
{
PACKET_IO_LOG_ERROR("unable to write pcap file: %s, %s", file, strerror(errno));
}
}
typedef int file_handle(const char *file, void *arg);
static int scan_directory(const char *dir, file_handle *handler, void *arg)
@@ -165,6 +246,18 @@ static int dumpfile_handler(const char *file, void *arg)
return 0;
}
static int all_packet_processed(struct dumpfile_io *handle)
{
for (uint16_t i = 0; i < handle->nr_threads; i++)
{
if (!lock_free_queue_empty(handle->queue[i]))
{
return 0;
}
}
return 1;
}
static void *dumpfile_thread(void *arg)
{
struct dumpfile_io *handle = (struct dumpfile_io *)arg;
@@ -176,6 +269,11 @@ static void *dumpfile_thread(void *arg)
while (ATOMIC_READ(&handle->io_thread_need_exit) == 0)
{
if (all_packet_processed(handle))
{
ATOMIC_SET(&handle->io_thread_wait_exit, 1);
}
PACKET_IO_LOG_STATE("dumpfile io thread waiting");
sleep(1);
}
@@ -259,6 +357,11 @@ void dumpfile_io_free(struct dumpfile_io *handle)
}
}
int dumpfile_io_wait_exit(struct dumpfile_io *handle)
{
return ATOMIC_READ(&handle->io_thread_wait_exit);
}
int dumpfile_io_init(struct dumpfile_io *handle, uint16_t thr_idx)
{
return 0;
@@ -365,6 +468,8 @@ int dumpfile_io_inject(struct dumpfile_io *handle, uint16_t thr_idx, struct pack
stat->dev_tx_pkts++;
stat->dev_tx_bytes += len;
save_packet(pkt, stat->inject_pkts);
packet_free(pkt);
}

View File

@@ -11,6 +11,7 @@ extern "C"
struct dumpfile_io;
struct dumpfile_io *dumpfile_io_new(const char *directory, uint16_t nr_threads);
void dumpfile_io_free(struct dumpfile_io *handle);
int dumpfile_io_wait_exit(struct dumpfile_io *handle);
int dumpfile_io_init(struct dumpfile_io *handle, uint16_t thr_idx);
int dumpfile_io_ingress(struct dumpfile_io *handle, uint16_t thr_idx, struct packet *pkts, int nr_pkts);

View File

@@ -78,3 +78,9 @@ void lock_free_queue_pop(struct lock_free_queue *queue, void **data)
*data = (void *)read;
queue->head = (queue->head + 1) % queue->size;
}
int lock_free_queue_empty(struct lock_free_queue *queue)
{
uint64_t read = ATOMIC_READ(&queue->queue[queue->head]);
return read == 0;
}

View File

@@ -15,6 +15,7 @@ struct lock_free_queue;
struct lock_free_queue *lock_free_queue_new(uint32_t size);
void lock_free_queue_free(struct lock_free_queue *queue);
int lock_free_queue_empty(struct lock_free_queue *queue);
int lock_free_queue_push(struct lock_free_queue *queue, void *data);
void lock_free_queue_pop(struct lock_free_queue *queue, void **data);

View File

@@ -58,6 +58,18 @@ void packet_io_free(struct packet_io *packet_io)
}
}
int packet_io_wait_exit(struct packet_io *packet_io) // used for dumpfile mode
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
{
return 0;
}
else
{
return dumpfile_io_wait_exit(packet_io->dumpfile);
}
}
int packet_io_init(struct packet_io *packet_io, uint16_t thr_idx)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))

View File

@@ -83,6 +83,7 @@ struct inject_packet_meta
struct packet_io;
struct packet_io *packet_io_new(struct packet_io_options *opts);
void packet_io_free(struct packet_io *packet_io);
int packet_io_wait_exit(struct packet_io *packet_io); // used for dumpfile mode
int packet_io_init(struct packet_io *packet_io, uint16_t thr_idx);
int packet_io_ingress(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, int nr_pkts);

View File

@@ -177,7 +177,7 @@ void session_get_route_ctx(const struct session *sess, enum flow_direction dir,
void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt)
{
sess->first_pkt[dir] = packet_dup(pkt);
sess->first_pkt[dir] = pkt;
}
const struct packet *session_get_first_packet(const struct session *sess, enum flow_direction dir)

View File

@@ -571,7 +571,7 @@ static void session_update(struct session *sess, enum session_state next_state,
packet_get_route_ctx(pkt, &ctx);
packet_get_sid_list(pkt, &list);
session_set_first_packet(sess, dir, pkt);
session_set_first_packet(sess, dir, packet_dup(pkt));
session_set_route_ctx(sess, dir, &ctx);
session_set_sid_list(sess, dir, &list);
}

View File

@@ -115,6 +115,11 @@ int main(int argc, char **argv)
stellar_stat_output(runtime->stat);
}
usleep(1000); // 1ms
if (packet_io_wait_exit(runtime->packet_io))
{
ATOMIC_SET(&runtime->need_exit, 1);
}
}
error_out:

View File

@@ -123,6 +123,7 @@ static void *work_thread(void *arg)
while (ATOMIC_READ(&runtime->need_exit) == 0)
{
now = timestamp_get_msec();
memset(packets, 0, sizeof(packets));
nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX);
if (nr_recv == 0)
{