Skip to content

Commit

Permalink
fix pcap crash
Browse files Browse the repository at this point in the history
The allocation size of the mbufs were incorrect and did not account
for the MBUF header size.

Signed-off-by: Keith Wiles <keith.wiles@intel.com>
  • Loading branch information
KeithWiles committed Aug 9, 2024
1 parent 2c8819d commit 1c56e97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/pktgen-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ convert_bitfield(bf_spec_t *bf)
}

/**
* pktgen_save - Save a configuration as a startup script
* pktgen_script_save - Save a configuration as a startup script
*
* DESCRIPTION
* Save a configuration as a startup script
Expand Down Expand Up @@ -464,7 +464,7 @@ pktgen_lua_save(char *path)
fprintf(fd, "pktgen.mac_from_arp(\"%s\");\n\n",
(pktgen.flags & MAC_FROM_ARP_FLAG) ? "enable" : "disable");

for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
for (i = 0; i < pktgen.nb_ports; i++) {
pinfo = l2p_get_port_pinfo(i);
pkt = &pinfo->seq_pkt[SINGLE_PKT];
range = &pinfo->range;
Expand Down
28 changes: 15 additions & 13 deletions app/pktgen-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pktgen_pcap_info(pcap_info_t *pcap, uint16_t port, int flag)
printf(" snaplen: %d,", pcap->info.snaplen);
printf(" sigfigs: %d,", pcap->info.sigfigs);
printf(" network: %d", pcap->info.network);
printf(" Endian: %s\n", pcap->convert ? "Big" : "Little");
printf(" Convert Endian: %s\n", pcap->convert ? "Yes" : "No");
if (flag)
printf(" Packet count: %d\n", pcap->pkt_count);
printf("\n");
Expand Down Expand Up @@ -58,17 +58,14 @@ static void
pcap_get_info(pcap_info_t *pcap)
{
pcap_record_hdr_t hdr;

if (fread(&pcap->info, 1, sizeof(pcap_hdr_t), pcap->fp) != sizeof(pcap_hdr_t))
rte_exit(EXIT_FAILURE, "%s: failed to read pcap header\n", __func__);

/* Make sure we have a valid PCAP file for Big or Little Endian formats. */
if (pcap->info.magic_number != PCAP_MAGIC_NUMBER)
pcap->convert = 0;
else if (pcap->info.magic_number != ntohl(PCAP_MAGIC_NUMBER))
pcap->convert = 1;
else
rte_exit(EXIT_FAILURE, "%s: invalid magic number 0x%08x\n", __func__,
pcap->info.magic_number);
pcap->convert = (PCAP_MAGIC_NUMBER == pcap->info.magic_number) ? 0 : 1;

printf("PCAP: MAGIC_NUMBER 0x%08x == 0x%08x, Convert: %s\n", PCAP_MAGIC_NUMBER,
pcap->info.magic_number, (pcap->convert) ? "Yes" : "No");

if (pcap->convert) {
pcap->info.magic_number = ntohl(pcap->info.magic_number);
Expand All @@ -80,6 +77,7 @@ pcap_get_info(pcap_info_t *pcap)
pcap->info.network = ntohl(pcap->info.network);
}

pcap->max_pkt_size = 0;
/* count the number of packets and get the largest size packet */
for (;;) {
if (fread(&hdr, 1, sizeof(pcap_record_hdr_t), pcap->fp) != sizeof(hdr))
Expand All @@ -95,15 +93,19 @@ pcap_get_info(pcap_info_t *pcap)
if (hdr.incl_len > pcap->max_pkt_size)
pcap->max_pkt_size = hdr.incl_len;
}
pcap->max_pkt_size += RTE_PKTMBUF_HEADROOM;
pcap->max_pkt_size = RTE_ALIGN_CEIL(pcap->max_pkt_size, RTE_CACHE_LINE_SIZE);
printf("PCAP: Max Packet Size: %d\n", pcap->max_pkt_size);

pcap_rewind(pcap);
}

static __inline__ void
mbuf_iterate_cb(struct rte_mempool *mp, void *opaque, void *obj, unsigned obj_idx __rte_unused)
{
pcap_info_t *pcap = (pcap_info_t *)opaque;
struct rte_mbuf *m = (struct rte_mbuf *)obj;
pcap_record_hdr_t hdr;
pcap_info_t *pcap = (pcap_info_t *)opaque;
struct rte_mbuf *m = (struct rte_mbuf *)obj;
pcap_record_hdr_t hdr = {0};

if (fread(&hdr, 1, sizeof(pcap_record_hdr_t), pcap->fp) != sizeof(hdr))
rte_exit(EXIT_FAILURE, "%s: failed to read pcap header\n", __func__);
Expand Down Expand Up @@ -569,4 +571,4 @@ pktgen_write_mbuf_to_pcap_file(FILE *fp, struct rte_mbuf *mbuf)
fflush(fp);

return 0;
}
}

0 comments on commit 1c56e97

Please sign in to comment.