Skip to content

Commit

Permalink
Port tutorial codes to C++11, fixing issue seladb#977 partially
Browse files Browse the repository at this point in the history
  • Loading branch information
merttozer committed Apr 17, 2024
1 parent 0c75603 commit b543e31
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 202 deletions.
66 changes: 20 additions & 46 deletions Examples/Tutorials/Tutorial-DpdkL2Fwd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,31 @@
#include "SystemUtils.h"
#include "DpdkDeviceList.h"
#include "TablePrinter.h"

#include "WorkerThread.h"


#define MBUF_POOL_SIZE 16*1024-1
#define DEVICE_ID_1 0
#define DEVICE_ID_2 1

#define COLLECT_STATS_EVERY_SEC 2


// Keep running flag
bool keepRunning = true;

void onApplicationInterrupted(void* cookie)
{
keepRunning = false;
std::cout << std::endl << "Shutting down..." << std::endl;
std::cout << "\nShutting down...\n";
}


void printStats(pcpp::DpdkDevice* rxDevice, pcpp::DpdkDevice* txDevice)
{
pcpp::DpdkDevice::DpdkDeviceStats rxStats;
pcpp::DpdkDevice::DpdkDeviceStats txStats;
pcpp::DpdkDevice::DpdkDeviceStats rxStats, txStats;
rxDevice->getStatistics(rxStats);
txDevice->getStatistics(txStats);

std::vector<std::string> columnNames;
columnNames.push_back(" ");
columnNames.push_back("Total Packets");
columnNames.push_back("Packets/sec");
columnNames.push_back("Bytes");
columnNames.push_back("Bits/sec");

std::vector<int> columnLengths;
columnLengths.push_back(10);
columnLengths.push_back(15);
columnLengths.push_back(15);
columnLengths.push_back(15);
columnLengths.push_back(15);
std::vector<std::string> columnNames = {" ", "Total Packets", "Packets/sec", "Bytes", "Bits/sec"};
std::vector<int> columnLengths = {10, 15, 15, 15, 15};

pcpp::TablePrinter printer(columnNames, columnLengths);

Expand All @@ -68,49 +52,46 @@ int main(int argc, char* argv[])
pcpp::DpdkDeviceList::initDpdk(coreMaskToUse, MBUF_POOL_SIZE);

// Find DPDK devices
pcpp::DpdkDevice* device1 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_1);
if (device1 == NULL)
auto* device1 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_1);
if (device1 == nullptr)
{
std::cerr << "Cannot find device1 with port '" << DEVICE_ID_1 << "'" << std::endl;
std::cerr << "Cannot find device1 with port '" << DEVICE_ID_1 << "'\n";
return 1;
}

pcpp::DpdkDevice* device2 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_2);
if (device2 == NULL)
auto* device2 = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(DEVICE_ID_2);
if (device2 == nullptr)
{
std::cerr << "Cannot find device2 with port '" << DEVICE_ID_2 << "'" << std::endl;
std::cerr << "Cannot find device2 with port '" << DEVICE_ID_2 << "'\n";
return 1;
}

// Open DPDK devices
if (!device1->openMultiQueues(1, 1))
{
std::cerr << "Couldn't open device1 #" << device1->getDeviceId() << ", PMD '" << device1->getPMDName() << "'" << std::endl;
std::cerr << "Couldn't open device1 #" << device1->getDeviceId() << ", PMD '" << device1->getPMDName() << "'\n";
return 1;
}

if (!device2->openMultiQueues(1, 1))
{
std::cerr << "Couldn't open device2 #" << device2->getDeviceId() << ", PMD '" << device2->getPMDName() << "'" << std::endl;
std::cerr << "Couldn't open device2 #" << device2->getDeviceId() << ", PMD '" << device2->getPMDName() << "'\n";
return 1;
}

// Create worker threads
std::vector<pcpp::DpdkWorkerThread*> workers;
workers.push_back(new L2FwdWorkerThread(device1, device2));
workers.push_back(new L2FwdWorkerThread(device2, device1));
// Constructs a DpdkWorkerThread* directly within the vector's storage
workers.emplace_back(new L2FwdWorkerThread(device1, device2));
workers.emplace_back(new L2FwdWorkerThread(device2, device1));

// Create core mask - use core 1 and 2 for the two threads
int workersCoreMask = 0;
for (int i = 1; i <= 2; i++)
{
workersCoreMask = workersCoreMask | (1 << i);
}
int workersCoreMask = 0x06; // Binary 110, using cores 1 and 2

// Start capture in async mode
if (!pcpp::DpdkDeviceList::getInstance().startDpdkWorkerThreads(workersCoreMask, workers))
{
std::cerr << "Couldn't start worker threads" << std::endl;
std::cerr << "Couldn't start worker threads\n";
return 1;
}

Expand All @@ -129,21 +110,14 @@ int main(int argc, char* argv[])
// Clear screen and move to top left
std::cout << "\033[2J\033[1;1H";

std::cout
<< "Stats #" << statsCounter++ << std::endl
<< "==========" << std::endl
<< std::endl;
std::cout << "Stats #" << statsCounter++ << "\n==========\n\n";

// Print stats of traffic going from Device1 to Device2
std::cout << std::endl
<< "Device1->Device2 stats:" << std::endl
<< std::endl;
std::cout << "\nDevice1->Device2 stats:\n\n";
printStats(device1, device2);

// Print stats of traffic going from Device2 to Device1
std::cout << std::endl
<< "Device2->Device1 stats:" << std::endl
<< std::endl;
std::cout << "\nDevice2->Device1 stats:\n\n";
printStats(device2, device1);
}
counter++;
Expand Down
11 changes: 5 additions & 6 deletions Examples/Tutorials/Tutorial-HelloWorld/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ int main(int argc, char* argv[])
pcpp::PcapFileReaderDevice reader("1_packet.pcap");
if (!reader.open())
{
std::cerr << "Error opening the pcap file" << std::endl;
std::cerr << "Error opening the pcap file\n";
return 1;
}

// read the first (and only) packet from the file
pcpp::RawPacket rawPacket;
if (!reader.getNextPacket(rawPacket))
{
std::cerr << "Couldn't read the first packet in the file" << std::endl;
std::cerr << "Couldn't read the first packet in the file\n";
return 1;
}

Expand All @@ -28,14 +28,13 @@ int main(int argc, char* argv[])
if (parsedPacket.isPacketOfType(pcpp::IPv4))
{
// extract source and dest IPs
pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();
auto srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
auto destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();

// print source and dest IPs
std::cout
<< "Source IP is '" << srcIP << "'; "
<< "Dest IP is '" << destIP << "'"
<< std::endl;
<< "Dest IP is '" << destIP << "'\n";
}

// close the file
Expand Down
Loading

0 comments on commit b543e31

Please sign in to comment.