Skip to content

Commit

Permalink
fix pcap crash
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Wiles <[email protected]>
  • Loading branch information
KeithWiles committed Oct 30, 2024
1 parent 73420d7 commit 1b8d706
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 48 deletions.
12 changes: 7 additions & 5 deletions app/pktgen-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ pktgen_script_save(char *path)
pcap_info_t *pcap = l2p_get_pcap(pinfo->pid);
if (pcap) {
fprintf(fd, "#\n# PCAP port %d\n", i);
fprintf(fd, "# Packet count: %d\n", pcap->pkt_count);
fprintf(fd, "# Packet count: %d, max size: %d\n", pcap->pkt_count,
pcap->max_pkt_size);
fprintf(fd, "# Filename : %s\n", pcap->filename);
}
fprintf(fd, "\n");
Expand Down Expand Up @@ -723,7 +724,8 @@ pktgen_lua_save(char *path)
pcap_info_t *pcap = l2p_get_pcap(pinfo->pid);
if (pcap) {
fprintf(fd, "--\n-- PCAP port %d\n", i);
fprintf(fd, "-- Packet count: %d\n", pcap->pkt_count);
fprintf(fd, "-- Packet count: %d, max size: %d\n", pcap->pkt_count,
pcap->max_pkt_size);
fprintf(fd, "-- Filename : %s\n", pcap->filename);
}
fprintf(fd, "\n");
Expand Down Expand Up @@ -1538,7 +1540,7 @@ enable_pcap(port_info_t *pinfo, uint32_t state)
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_PCAP_PKTS);
} else {
pktgen_clr_port_flags(pinfo, SEND_PCAP_PKTS);
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_SINGLE_PKTS);
}
pinfo->tx_cycles = 0;
Expand Down Expand Up @@ -2559,7 +2561,7 @@ pktgen_set_port_seqCnt(port_info_t *pinfo, uint32_t cnt)
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_SEQ_PKTS);
} else {
pktgen_clr_port_flags(pinfo, SEND_SEQ_PKTS);
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_SINGLE_PKTS);
}
}
Expand Down Expand Up @@ -3124,7 +3126,7 @@ enable_range(port_info_t *pinfo, uint32_t state)
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_RANGE_PKTS);
} else {
pktgen_clr_port_flags(pinfo, SEND_RANGE_PKTS);
pktgen_clr_port_flags(pinfo, EXCLUSIVE_MODES);
pktgen_set_port_flags(pinfo, SEND_SINGLE_PKTS);
}

Expand Down
26 changes: 19 additions & 7 deletions app/pktgen-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pktgen_pcap_info(pcap_info_t *pcap, uint16_t port, int flag)
printf(" network: %d", pcap->info.network);
printf(" Convert Endian: %s\n", pcap->convert ? "Yes" : "No");
if (flag)
printf(" Packet count: %d\n", pcap->pkt_count);
printf(" Packet count: %d, max size %d\n", pcap->pkt_count, pcap->max_pkt_size);
fflush(stdout);
}

Expand Down Expand Up @@ -64,9 +64,9 @@ pcap_get_info(pcap_info_t *pcap)
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)
if (pcap->info.magic_number == PCAP_MAGIC_NUMBER)
pcap->convert = 0;
else if (pcap->info.magic_number != ntohl(PCAP_MAGIC_NUMBER))
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__,
Expand Down Expand Up @@ -112,8 +112,11 @@ mbuf_iterate_cb(struct rte_mempool *mp, void *opaque, void *obj, unsigned obj_id
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__);
if (fread(&hdr, 1, sizeof(pcap_record_hdr_t), pcap->fp) != sizeof(hdr)) {
pcap_rewind(pcap);
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__);
}

pcap_convert(pcap, &hdr); /* Convert the packet header to the correct format. */

Expand Down Expand Up @@ -161,6 +164,7 @@ pktgen_pcap_open(void)
struct rte_mempool *mp;
char name[64] = {0};
uint16_t sid;
uint32_t pkt_count;

for (int pid = 0; pid < RTE_MAX_ETHPORTS; pid++) {
if ((pcap = pcap_info_list[pid]) == NULL)
Expand All @@ -177,9 +181,17 @@ pktgen_pcap_open(void)

pcap_get_info(pcap);

pkt_count = pcap->pkt_count;
if (pkt_count == 0) {
fclose(pcap->fp);
rte_exit(EXIT_FAILURE, "%s: PCAP file is empty: %s\n", __func__, pcap->filename);
}
if (pkt_count < (DEFAULT_TX_DESC * 4))
pkt_count = (DEFAULT_TX_DESC * 4);

snprintf(name, sizeof(name), "pcap-%d", pid);
mp = rte_pktmbuf_pool_create(name, pcap->pkt_count, 0, DEFAULT_PRIV_SIZE,
pcap->max_pkt_size, sid);
mp =
rte_pktmbuf_pool_create(name, pkt_count, 0, DEFAULT_PRIV_SIZE, pcap->max_pkt_size, sid);
if (mp == NULL)
rte_exit(EXIT_FAILURE,
"Cannot create mbuf pool (%s) port %d, nb_mbufs %d, socket_id %d: %s", name,
Expand Down
63 changes: 34 additions & 29 deletions app/pktgen-port-cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,55 @@ extern "C" {
50108 // Max 101000?, limited by max allowed size of latsamp_stats_t.data[]
#define MAX_LATENCY_QUEUES 10

// clang-format off
enum { /* Per port flag bits */
/* Supported packet modes non-exclusive */
SEND_ARP_REQUEST = (1 << 0), /**< Send a ARP request */
SEND_GRATUITOUS_ARP = (1 << 1), /**< Send a Gratuitous ARP */
ICMP_ECHO_ENABLE_FLAG = (1 << 2), /**< Enable ICMP Echo support */
BONDING_TX_PACKETS = (1 << 3), /**< Bonding driver send zero pkts */
SEND_ARP_REQUEST = (1 << 0), /**< Send a ARP request */
SEND_GRATUITOUS_ARP = (1 << 1), /**< Send a Gratuitous ARP */
ICMP_ECHO_ENABLE_FLAG = (1 << 2), /**< Enable ICMP Echo support */
BONDING_TX_PACKETS = (1 << 3), /**< Bonding driver send zero pkts */

PROCESS_INPUT_PKTS = (1 << 4), /**< Process input packets */
CAPTURE_PKTS = (1 << 5), /**< Capture received packets */
SEND_PING4_REQUEST = (1 << 6), /**< Send a IPv4 Ping request */
SEND_PING6_REQUEST = (1 << 7), /**< Send a IPv6 Ping request */
/* Receive packet modes */
PROCESS_INPUT_PKTS = (1 << 4), /**< Process input packets */
CAPTURE_PKTS = (1 << 5), /**< Capture received packets */
SAMPLING_LATENCIES = (1 << 6), /**< Sampling latency measurements */

SEND_PING4_REQUEST = (1 << 8), /**< Send a IPv4 Ping request */
SEND_PING6_REQUEST = (1 << 9), /**< Send a IPv6 Ping request */

/* Exclusive Packet sending modes */
SEND_SINGLE_PKTS = (1 << 12), /**< Send single packets */
SEND_PCAP_PKTS = (1 << 13), /**< Send a pcap file of packets */
SEND_RANGE_PKTS = (1 << 14), /**< Send range of packets */
SEND_SEQ_PKTS = (1 << 15), /**< Send sequence of packets */
SEND_RANDOM_PKTS = (1 << 16), /**< Send random bitfields in packets */
SEND_SINGLE_PKTS = (1 << 12), /**< Send single packets */
SEND_PCAP_PKTS = (1 << 13), /**< Send a pcap file of packets */
SEND_RANGE_PKTS = (1 << 14), /**< Send range of packets */
SEND_SEQ_PKTS = (1 << 15), /**< Send sequence of packets */

/* Exclusive Packet type modes */
SEND_VLAN_ID = (1 << 19), /**< Send packets with VLAN ID */
SEND_MPLS_LABEL = (1 << 20), /**< Send MPLS label */
SEND_Q_IN_Q_IDS = (1 << 21), /**< Send packets with Q-in-Q */
SEND_GRE_IPv4_HEADER = (1 << 22), /**< Encapsulate IPv4 in GRE */
SEND_GRE_ETHER_HEADER = (1 << 23), /**< Encapsulate Ethernet frame in GRE */
SEND_VXLAN_PACKETS = (1 << 24), /**< Send VxLAN Packets */
SAMPLING_LATENCIES = (1 << 25), /**< Sampling latency measurements> */
SEND_LATENCY_PKTS = (1 << 26), /**< Send latency packets in any mode */
SEND_RANDOM_PKTS = (1 << 16), /**< Send random bitfields in packets */
SEND_VLAN_ID = (1 << 17), /**< Send packets with VLAN ID */
SEND_MPLS_LABEL = (1 << 18), /**< Send MPLS label */
SEND_Q_IN_Q_IDS = (1 << 19), /**< Send packets with Q-in-Q */

SEND_GRE_IPv4_HEADER = (1 << 20), /**< Encapsulate IPv4 in GRE */
SEND_GRE_ETHER_HEADER = (1 << 21), /**< Encapsulate Ethernet frame in GRE */
SEND_VXLAN_PACKETS = (1 << 22), /**< Send VxLAN Packets */
SEND_LATENCY_PKTS = (1 << 23), /**< Send latency packets in any mode */

/* Sending flags */
SETUP_TRANSMIT_PKTS = (1 << 28), /**< Need to setup transmit packets */
STOP_RECEIVING_PACKETS = (1 << 29), /**< Stop receiving packet */
SENDING_PACKETS = (1 << 30), /**< sending packets on this port */
SEND_FOREVER = (1 << 31), /**< Send packets forever */
SETUP_TRANSMIT_PKTS = (1 << 28), /**< Need to setup transmit packets */
STOP_RECEIVING_PACKETS = (1 << 29), /**< Stop receiving packet */
SENDING_PACKETS = (1 << 30), /**< sending packets on this port */
SEND_FOREVER = (1 << 31), /**< Send packets forever */

SEND_ARP_PING_REQUESTS =
SEND_ARP_PING_REQUESTS =
(SEND_ARP_REQUEST | SEND_GRATUITOUS_ARP | SEND_PING4_REQUEST | SEND_PING6_REQUEST)
};
// clang-format on

#define EXCLUSIVE_MODES (SEND_SINGLE_PKTS | SEND_PCAP_PKTS | SEND_RANGE_PKTS | SEND_SEQ_PKTS)

#define EXCLUSIVE_PKT_MODES \
(SEND_VLAN_ID | SEND_VXLAN_PACKETS | SEND_MPLS_LABEL | SEND_Q_IN_Q_IDS | \
SEND_GRE_IPv4_HEADER | SEND_GRE_ETHER_HEADER)
#define EXCLUSIVE_PKT_MODES \
(SEND_RANDOM_PKTS | SEND_VLAN_ID | SEND_MPLS_LABEL | SEND_Q_IN_Q_IDS | SEND_GRE_IPv4_HEADER | \
SEND_GRE_ETHER_HEADER | SEND_VXLAN_PACKETS | SEND_LATENCY_PKTS)

#define RTE_PMD_PARAM_UNSET -1

Expand Down
7 changes: 0 additions & 7 deletions app/pktgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,9 +1042,6 @@ pktgen_main_transmit(port_info_t *pinfo, uint16_t qid)

pktgen_setup_packets(pinfo->pid);

if (pktgen_tst_port_flags(pinfo, SEND_PCAP_PKTS))
mp = l2p_get_pcap_mp(pinfo->pid);

pinfo->qcnt[qid]++; /* Count the number of times queue is sending */

if (pktgen_tst_port_flags(pinfo, SEND_PCAP_PKTS))
Expand Down Expand Up @@ -1151,8 +1148,6 @@ pktgen_main_rxtx_loop(void)
if (curr_tsc >= tx_next_cycle) {
tx_next_cycle = curr_tsc + pinfo->tx_cycles;

if (unlikely(pinfo->tx_cycles == 0))
continue;
pktgen_main_transmit(pinfo, tx_qid);
}
if (curr_tsc >= tx_bond_cycle) {
Expand Down Expand Up @@ -1207,8 +1202,6 @@ pktgen_main_tx_loop(void)
if (curr_tsc >= tx_next_cycle) {
tx_next_cycle = curr_tsc + pinfo->tx_cycles;

if (unlikely(pinfo->tx_cycles == 0))
continue;
pktgen_main_transmit(pinfo, tx_qid);
}
if (curr_tsc >= tx_bond_cycle) {
Expand Down
3 changes: 3 additions & 0 deletions cfg/cnet-fwd.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ run = {
'[8:9-10].2',
'[11:12-13].3',
),
# 'pcap': (
# '1:pcap/test1.pcap',
# ),

'theme': 'themes/black-yellow.theme',
}
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Pktgen-DPDK - Traffic Generator powered by DPDK

**=== Modifications ===**
- 24.10.0 - fix a number of bugs for pcap crash, tcp flags, socket crash, random and startup.
PCAP crash memory overrun was found and fixed.
- 24.07.1 - add back support for Lua socket support.
- 24.07.0 - fix TX errors of packets greater then 100 bytes as MTU size was wrong.
Add support for UDP/TCP packets in pktperf application.
Expand Down

0 comments on commit 1b8d706

Please sign in to comment.