Skip to content

Commit

Permalink
Clang-format for Pcap++ (#1516)
Browse files Browse the repository at this point in the history
* Formatted Pcap++ headers.

- Includes manual review.

* Format *.cpp files.

* Updated pre-commit.

* Fix doxygen error.

* Fixed indents.

* Documentation fixes.

- Align spaces to bullet points
- Break up long lines.
- Typo fixes.
- Removed IPv6 support todo, as it is already completed.
- Updated IPFilter documentation to include IPv6 support.

* Align more spaces to bullet points.

* Formatted RawSocketDevice.h

* Fix pre-commit line limit fail.

* Replaced <BR> tags with line break in documentation.

* Moved comments above lines for nicer formatting.

* Formatting fixes.

* Cleaned up <BR> tags.

* Fixed uneven quotation marks.

* Lint

---------

Co-authored-by: Liu, An-Chi <phy.tiger@gmail.com>
  • Loading branch information
Dimi1010 and tigercosmos authored Aug 14, 2024
1 parent 6627ab7 commit b27e84f
Show file tree
Hide file tree
Showing 44 changed files with 9,358 additions and 8,670 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: clang-format
args: ["--style=file"] # Use the .clang-format file for configuration
files: ^(Common\+\+|Tests|Examples|Packet\+\+)/.*\.(cpp|h)$
files: ^(Common\+\+|Packet\+\+|Pcap\+\+|Tests|Examples)/.*\.(cpp|h)$
- id: cppcheck
args: ["--std=c++11", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr", "--force"]
- repo: https://github.com/codespell-project/codespell
Expand Down
38 changes: 22 additions & 16 deletions Pcap++/header/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "PcapFilter.h"

/**
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
*/
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
*/
namespace pcpp
{
/** A vector of pointers to RawPacket */
Expand All @@ -26,11 +26,12 @@ namespace pcpp
bool m_DeviceOpened;

// c'tor should not be public
IDevice() : m_DeviceOpened(false) {}
IDevice() : m_DeviceOpened(false)
{}

public:

virtual ~IDevice() {}
virtual ~IDevice()
{}

/**
* Open the device
Expand All @@ -46,10 +47,12 @@ namespace pcpp
/**
* @return True if the file is opened, false otherwise
*/
inline bool isOpened() { return m_DeviceOpened; }
inline bool isOpened()
{
return m_DeviceOpened;
}
};


/**
* @class IFilterableDevice
* An abstract interface representing all devices that have BPF (Berkeley Packet Filter) filtering capabilities,
Expand All @@ -59,16 +62,17 @@ namespace pcpp
class IFilterableDevice
{
protected:

// c'tor should not be public
IFilterableDevice() {}
IFilterableDevice()
{}

public:

virtual ~IFilterableDevice() {}
virtual ~IFilterableDevice()
{}

/**
* Set a filter for the device. When implemented by the device, only packets that match the filter will be received
* Set a filter for the device. When implemented by the device, only packets that match the filter will be
* received
* @param[in] filter The filter to be set in PcapPlusPlus' GeneralFilter format
* @return True if filter set successfully, false otherwise
*/
Expand All @@ -80,8 +84,10 @@ namespace pcpp
}

/**
* Set a filter for the device. When implemented by the device, only packets that match the filter will be received
* @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax (http://biot.com/capstats/bpf.html)
* Set a filter for the device. When implemented by the device, only packets that match the filter will be
* received
* @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax
* (http://biot.com/capstats/bpf.html)
* @return True if filter set successfully, false otherwise
*/
virtual bool setFilter(std::string filterAsString) = 0;
Expand All @@ -92,4 +98,4 @@ namespace pcpp
*/
virtual bool clearFilter() = 0;
};
}
} // namespace pcpp
4 changes: 2 additions & 2 deletions Pcap++/header/DeviceUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace pcpp
* @throws std::runtime_error The system encountered an error fetching the devices.
*/
std::unique_ptr<pcap_if_t, PcapFreeAllDevsDeleter> getAllLocalPcapDevices();
}
} // namespace internal

/// @endcond
}
} // namespace pcpp
698 changes: 396 additions & 302 deletions Pcap++/header/DpdkDevice.h

Large diffs are not rendered by default.

148 changes: 84 additions & 64 deletions Pcap++/header/DpdkDeviceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,49 @@
*/

/**
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
*/
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
*/
namespace pcpp
{

/**
* @class DpdkWorkerThread
* There are two ways to capture packets using DpdkDevice: one of them is using worker threads and the other way is using
* a callback which is invoked on each a burst of packets are captured (see DpdkDevice#startCaptureSingleThread() ). This class
* is a base class for implementing workers. A worker is basically a class that is activated by DpdkDeviceList#startDpdkWorkerThreads()
* and runs on a designated core. When it runs it can do whatever the user wants it to do. The most common use it running in an
* endless loop and receive, analyze and send packets using one or more DpdkDevice instances. It can do all kinds of processing for
* these packets. The only restriction for a worker class is that it must implement the 3 abstract methods stated in this class-interface
* for start running, stop running and get the core ID the worker is running on.
* There are two ways to capture packets using DpdkDevice: one of them is using worker threads and the other way is
* using a callback which is invoked on each a burst of packets are captured (see
* DpdkDevice#startCaptureSingleThread() ). This class is a base class for implementing workers. A worker is
* basically a class that is activated by DpdkDeviceList#startDpdkWorkerThreads() and runs on a designated core.
* When it runs it can do whatever the user wants it to do. The most common use it running in an endless loop and
* receive, analyze and send packets using one or more DpdkDevice instances. It can do all kinds of processing for
* these packets. The only restriction for a worker class is that it must implement the 3 abstract methods stated in
* this class-interface for start running, stop running and get the core ID the worker is running on.
*/
class DpdkWorkerThread
{
public:
/**
* A virtual d'tor. Can be overridden by child class if needed
*/
virtual ~DpdkWorkerThread() {}
virtual ~DpdkWorkerThread()
{}

/**
* An abstract method that must be implemented by child class. It's the indication for the worker to start running
* An abstract method that must be implemented by child class. It's the indication for the worker to start
* running
* @param[in] coreId The core ID the worker is running on (should be returned in getCoreId() )
* @return True if all went well or false otherwise
*/
virtual bool run(uint32_t coreId) = 0;

/**
* An abstract method that must be implemented by child class. It's the indication for the worker to stop running. After
* this method is called the caller expects the worker to stop running as fast as possible
* An abstract method that must be implemented by child class. It's the indication for the worker to stop
* running. After this method is called the caller expects the worker to stop running as fast as possible
*/
virtual void stop() = 0;

/**
* An abstract method that must be implemented by child class. Get the core ID the worker is running on (as sent to the run() method
* as a parameter)
* An abstract method that must be implemented by child class. Get the core ID the worker is running on (as sent
* to the run() method as a parameter)
* @return The core ID the worker is running on
*/
virtual uint32_t getCoreId() const = 0;
Expand All @@ -62,16 +65,17 @@ namespace pcpp

/**
* @class DpdkDeviceList
* A singleton class that encapsulates DPDK initialization and holds the list of DpdkDevice instances. As it's a singleton, it has only
* one active instance doesn't have a public c'tor. This class has several main uses:
* - it contains the initDpdk() static method which initializes the DPDK infrastructure. It should be called once in every application at
* its startup process
* A singleton class that encapsulates DPDK initialization and holds the list of DpdkDevice instances. As it's a
* singleton, it has only one active instance doesn't have a public c'tor. This class has several main uses:
* - it contains the initDpdk() static method which initializes the DPDK infrastructure. It should be called once
* in every application at its startup process
* - it contains the list of DpdkDevice instances and enables access to them
* - it has methods to start and stop worker threads. See more details in startDpdkWorkerThreads()
*/
class DpdkDeviceList
{
friend class KniDeviceList;

private:
bool m_IsInitialized;
static bool m_IsDpdkInitialized;
Expand All @@ -83,18 +87,22 @@ namespace pcpp

DpdkDeviceList();

bool isInitialized() const { return (m_IsInitialized && m_IsDpdkInitialized); }
bool isInitialized() const
{
return (m_IsInitialized && m_IsDpdkInitialized);
}
bool initDpdkDevices(uint32_t mBufPoolSizePerDevice, uint16_t mMbufDataSize);
static bool verifyHugePagesAndDpdkDriver();

static int dpdkWorkerThreadStart(void* ptr);
public:

public:
~DpdkDeviceList();

/**
* As DpdkDeviceList is a singleton, this is the static getter to retrieve its instance. Note that if the static method
* initDpdk() was not called or returned false this instance won't be initialized and DpdkDevices won't be initialized either
* As DpdkDeviceList is a singleton, this is the static getter to retrieve its instance. Note that if the static
* method initDpdk() was not called or returned false this instance won't be initialized and DpdkDevices won't
* be initialized either
* @return The singleton instance of DpdkDeviceList
*/
static DpdkDeviceList& getInstance()
Expand All @@ -107,31 +115,38 @@ namespace pcpp
}

/**
* A static method that has to be called once at the startup of every application that uses DPDK. It does several things:
* - verifies huge-pages are set and DPDK kernel module is loaded, unless specifically asked not to (these are set by
* the setup_dpdk.py external script that has to be run before application is started)
* A static method that has to be called once at the startup of every application that uses DPDK. It does
* several things:
* - verifies huge-pages are set and DPDK kernel module is loaded, unless specifically asked not to (these are
* set by the setup_dpdk.py external script that has to be run before application is started)
* - initializes the DPDK infrastructure
* - creates DpdkDevice instances for all ports available for DPDK
*
* @param[in] coreMask The cores to initialize DPDK with. After initialization, DPDK will only be able to use these cores
* for its work. The core mask should have a bit set for every core to use. For example: if the user want to use cores 1,2
* the core mask should be 6 (binary: 110)
* @param[in] mBufPoolSizePerDevice The mbuf pool size each DpdkDevice will have. This has to be a number which is a power of 2
* minus 1, for example: 1023 (= 2^10-1) or 4,294,967,295 (= 2^32-1), etc. This is a DPDK limitation, not PcapPlusPlus.
* The size of the mbuf pool size dictates how many packets can be handled by the application at the same time. For example: if
* pool size is 1023 it means that no more than 1023 packets can be handled or stored in application memory at every point in time
* @param[in] mBufDataSize The size of data buffer in each mbuf. If this value is less than 1, we will use RTE_MBUF_DEFAULT_BUF_SIZE.
* @param[in] masterCore The core DPDK will use as master to control all worker thread. The default, unless set otherwise, is 0
* @param[in] coreMask The cores to initialize DPDK with. After initialization, DPDK will only be able to use
* these cores for its work. The core mask should have a bit set for every core to use. For example: if the user
* want to use cores 1,2 the core mask should be 6 (binary: 110)
* @param[in] mBufPoolSizePerDevice The mbuf pool size each DpdkDevice will have. This has to be a number which
* is a power of 2 minus 1, for example: 1023 (= 2^10-1) or 4,294,967,295 (= 2^32-1), etc. This is a DPDK
* limitation, not PcapPlusPlus. The size of the mbuf pool size dictates how many packets can be handled by the
* application at the same time. For example: if pool size is 1023 it means that no more than 1023 packets can
* be handled or stored in application memory at every point in time
* @param[in] mBufDataSize The size of data buffer in each mbuf. If this value is less than 1, we will use
* RTE_MBUF_DEFAULT_BUF_SIZE.
* @param[in] masterCore The core DPDK will use as master to control all worker thread. The default, unless set
* otherwise, is 0
* @param[in] initDpdkArgc Number of optional arguments
* @param[in] initDpdkArgv Optional arguments
* @param[in] appName Program name to be provided for the DPDK
* @param[in] verifyHugePagesAndDriver Verify huge-pages are set and DPDK kernel module is loaded. The default value it true
* @return True if initialization succeeded or false if huge-pages or DPDK kernel driver are not loaded, if mBufPoolSizePerDevice
* isn't power of 2 minus 1, if DPDK infra initialization failed or if DpdkDevice initialization failed. Anyway, if this method
* returned false it's impossible to use DPDK with PcapPlusPlus. You can get some more details about mbufs and pools in
* DpdkDevice.h file description or in DPDK web site
* @param[in] verifyHugePagesAndDriver Verify huge-pages are set and DPDK kernel module is loaded. The default
* value it true
* @return True if initialization succeeded or false if huge-pages or DPDK kernel driver are not loaded, if
* mBufPoolSizePerDevice isn't power of 2 minus 1, if DPDK infra initialization failed or if DpdkDevice
* initialization failed. Anyway, if this method returned false it's impossible to use DPDK with PcapPlusPlus.
* You can get some more details about mbufs and pools in DpdkDevice.h file description or in DPDK web site
*/
static bool initDpdk(CoreMask coreMask, uint32_t mBufPoolSizePerDevice, uint16_t mBufDataSize = 0, uint8_t masterCore = 0, uint32_t initDpdkArgc = 0, char **initDpdkArgv = nullptr, const std::string& appName = "pcapplusplusapp", bool verifyHugePagesAndDriver = true);
static bool initDpdk(CoreMask coreMask, uint32_t mBufPoolSizePerDevice, uint16_t mBufDataSize = 0,
uint8_t masterCore = 0, uint32_t initDpdkArgc = 0, char** initDpdkArgv = nullptr,
const std::string& appName = "pcapplusplusapp", bool verifyHugePagesAndDriver = true);

/**
* Get a DpdkDevice by port ID
Expand All @@ -150,7 +165,10 @@ namespace pcpp
/**
* @return A vector of all DpdkDevice instances
*/
const std::vector<DpdkDevice*>& getDpdkDeviceList() const { return m_DpdkDeviceList; }
const std::vector<DpdkDevice*>& getDpdkDeviceList() const
{
return m_DpdkDeviceList;
}

/**
* @return DPDK master core which is the core that initializes the application
Expand All @@ -164,8 +182,8 @@ namespace pcpp
void setDpdkLogLevel(Logger::LogLevel logLevel);

/**
* @return The current DPDK log level. RTE_LOG_NOTICE and lower are considered as Logger#Info. RTE_LOG_INFO or RTE_LOG_DEBUG
* are considered as Logger#Debug
* @return The current DPDK log level. RTE_LOG_NOTICE and lower are considered as Logger#Info. RTE_LOG_INFO or
* RTE_LOG_DEBUG are considered as Logger#Debug
*/
Logger::LogLevel getDpdkLogLevel() const;

Expand All @@ -177,32 +195,34 @@ namespace pcpp
bool writeDpdkLogToFile(FILE* logFile);

/**
* There are two ways to capture packets using DpdkDevice: one of them is using worker threads and the other way is setting
* a callback which is invoked each time a burst of packets is captured (see DpdkDevice#startCaptureSingleThread() ). This
* method implements the first way. See a detailed description of workers in DpdkWorkerThread class description. This method
* gets a vector of workers (classes that implement the DpdkWorkerThread interface) and a core mask and starts a worker thread
* on each core (meaning - call the worker's DpdkWorkerThread#run() method). Workers usually run in an endless loop and will
* be ordered to stop by calling stopDpdkWorkerThreads().<BR>
* Note that number of cores in the core mask must be equal to the number of workers. In addition it's impossible to run a
* worker thread on DPDK master core, so the core mask shouldn't include the master core (you can find the master core by
* calling getDpdkMasterCore() ).
* @param[in] coreMask The bitmask of cores to run worker threads on. This list shouldn't include DPDK master core
* @param[in] workerThreadsVec A vector of worker instances to run (classes who implement the DpdkWorkerThread interface).
* Number of workers in this vector must be equal to the number of cores in the core mask. Notice that the instances of
* DpdkWorkerThread shouldn't be freed until calling stopDpdkWorkerThreads() as these instances are running
* @return True if all worker threads started successfully or false if: DPDK isn't initialized (initDpdk() wasn't called or
* returned false), number of cores differs from number of workers, core mask includes DPDK master core or if one of the
* worker threads couldn't be run
* There are two ways to capture packets using DpdkDevice: one of them is using worker threads and the other way
* is setting a callback which is invoked each time a burst of packets is captured (see
* DpdkDevice#startCaptureSingleThread() ). This method implements the first way. See a detailed description of
* workers in DpdkWorkerThread class description. This method gets a vector of workers (classes that implement
* the DpdkWorkerThread interface) and a core mask and starts a worker thread on each core (meaning - call the
* worker's DpdkWorkerThread#run() method). Workers usually run in an endless loop and will be ordered to stop
* by calling stopDpdkWorkerThreads().<BR> Note that number of cores in the core mask must be equal to the
* number of workers. In addition it's impossible to run a worker thread on DPDK master core, so the core mask
* shouldn't include the master core (you can find the master core by calling getDpdkMasterCore() ).
* @param[in] coreMask The bitmask of cores to run worker threads on. This list shouldn't include DPDK master
* core
* @param[in] workerThreadsVec A vector of worker instances to run (classes who implement the DpdkWorkerThread
* interface). Number of workers in this vector must be equal to the number of cores in the core mask. Notice
* that the instances of DpdkWorkerThread shouldn't be freed until calling stopDpdkWorkerThreads() as these
* instances are running
* @return True if all worker threads started successfully or false if: DPDK isn't initialized (initDpdk()
* wasn't called or returned false), number of cores differs from number of workers, core mask includes DPDK
* master core or if one of the worker threads couldn't be run
*/
bool startDpdkWorkerThreads(CoreMask coreMask, std::vector<DpdkWorkerThread*>& workerThreadsVec);

/**
* Assuming worker threads are running, this method orders them to stop by calling DpdkWorkerThread#stop(). Then it waits until
* they stop running
* Assuming worker threads are running, this method orders them to stop by calling DpdkWorkerThread#stop(). Then
* it waits until they stop running
*/
void stopDpdkWorkerThreads();
};

} // namespace pcpp
} // namespace pcpp

// GCOVR_EXCL_STOP
Loading

0 comments on commit b27e84f

Please sign in to comment.