From 783022e22d907edabf8999159d2e09d58eededb6 Mon Sep 17 00:00:00 2001 From: Robbie Tu Date: Wed, 4 Dec 2024 00:24:06 -0800 Subject: [PATCH] add changes --- CMakeLists.txt | 2 +- src/daemonManager.cpp | 6 ++++-- src/pcaphandler.cpp | 21 +++++++++++++++++++++ src/pktminerg.cpp | 7 ++++++- src/restful/model/PacketAgentData.cpp | 24 +++++++++++++++++++++++- src/restful/model/PacketAgentData.h | 10 ++++++++++ src/socketvxlan.cpp | 16 +++++++++++++--- src/socketvxlan.h | 3 ++- 8 files changed, 80 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2625ab42..dfb3bca0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ endif () # set PKTMINERG_MAJOR_VERSION, PKTMINERG_MINOR_VERSION, etc. set(PKTMINERG_MAJOR_VERSION "0") set(PKTMINERG_MINOR_VERSION "8") -set(PKTMINERG_PATCH_VERSION "3") +set(PKTMINERG_PATCH_VERSION "4") set(PKTMINERG_VERSION_STRING "${PKTMINERG_MAJOR_VERSION}.${PKTMINERG_MINOR_VERSION}.${PKTMINERG_PATCH_VERSION}") if (WIN32) diff --git a/src/daemonManager.cpp b/src/daemonManager.cpp index fa3fcb667..c60ebb64b 100644 --- a/src/daemonManager.cpp +++ b/src/daemonManager.cpp @@ -445,7 +445,7 @@ std::string DaemonManager::createParams(std::shared_ptraddressIsSet() ? std::string(" -r ") + data->getAddress() : std::string()) % (data->getBpf().empty() @@ -458,6 +458,8 @@ std::string DaemonManager::createParams(std::shared_ptrgetStartup()) : std::string()) % (data->forwardRateLimitIsSet() ? std::string(" --mbps ") + std::to_string(data->getForwardRateLimit()) : std::string()) + % (data->capTimeIsSet() + ? std::string(" --capTime ") + std::to_string(data->getCapTime()) : std::string()) % (dir != "" ? std::string(" --dir ") + dir : std::string())); } @@ -1204,7 +1206,7 @@ void DaemonManager::scanNetworkInterfaceImpl() { interfaces = readInterfaces(ctx_); std::lock_guard lock{mtx_}; - if(interfaces != interfaces_) + if(interfaces_.empty()) { ctx_.log("scanNetworkInterface changed ", log4cpp::Priority::INFO); LOG(INFO) << "scanNetworkInterface changed "; diff --git a/src/pcaphandler.cpp b/src/pcaphandler.cpp index 2b41d42b9..3cb95cea3 100644 --- a/src/pcaphandler.cpp +++ b/src/pcaphandler.cpp @@ -541,6 +541,27 @@ int PcapHandler::openPcap() { } pcapGuard.Dismiss(); +#ifndef _WIN32 + int fd1 = -1; + if (_param.getProcessId() != "") { + pid_t pid = getppid(); + std::string path = "/proc/" + std::to_string(pid) + "/ns/net"; + fd1 = open(path.c_str(), O_RDONLY); /* Get file descriptor for namespace */ + + if (fd1 == -1) { + std::cerr << "Can not open the namespace:" <()->value_name("DIR"), "specify the direction determination expression") + ("capTime", boost::program_options::value()->value_name("CAPTIME"),"set capture time in tranferred packets for VxLan") #ifdef _WIN32 ("macRefreshInterval", boost::program_options::value()->value_name("MACREFRESHINTERVAL"), "specify the interval to refresh mac list of VM and PA") @@ -727,6 +728,7 @@ Allowed options for each interface:"); uint32_t vni; uint8_t vni_version; int vxlan_port; + int capTime = 0; if (zmq_port > 0) { type = exporttype::zmq; @@ -746,6 +748,9 @@ Allowed options for each interface:"); keybit = vm["keybit"].as(); } + if(vm.count("capTime")) { + capTime = vm["capTime"].as(); + } std::string intface = ""; if (vm.count("interface")) { auto strs = vm["interface"].as>(); @@ -945,7 +950,7 @@ Allowed options for each interface:"); case exporttype::vxlan: exportPtr = std::make_shared(remoteips, vni, bind_device, pmtudisc, vxlan_port, - vm["mbps"].as(), vni_version, ctx); + vm["mbps"].as(), vni_version, ctx, capTime); break; default: diff --git a/src/restful/model/PacketAgentData.cpp b/src/restful/model/PacketAgentData.cpp index 28c46ade1..abf62df84 100644 --- a/src/restful/model/PacketAgentData.cpp +++ b/src/restful/model/PacketAgentData.cpp @@ -164,6 +164,11 @@ namespace io { val["forwardRateLimit"] = m_ForwardRateLimit; } + if(m_CapTimeIsSet) + { + val["capTime"] = m_CapTime; + } + if(m_ApiVersionIsSet) { val["apiVersion"] = ModelBase::toJson(m_ApiVersion); @@ -273,7 +278,7 @@ namespace io { MB_FSET(dumpDir, DumpDir) MB_FSET(dumpInterval, DumpInterval) MB_FSET(forwardRateLimit, ForwardRateLimit) - + MB_FSET(capTime, CapTime) MB_FSET(apiVersion, ApiVersion) { m_ObservationDomainIds.clear(); @@ -535,6 +540,23 @@ namespace io { m_ForwardRateLimitIsSet = false; } + int32_t PacketAgentData::getCapTime() const { + return m_CapTime; + } + + void PacketAgentData::setCapTime(int32_t value) { + m_CapTime = value; + m_CapTimeIsSet = true; + } + + bool PacketAgentData::capTimeIsSet() const { + return m_CapTimeIsSet; + } + + void PacketAgentData::unsetCapTime() { + m_CapTimeIsSet = false; + } + std::string PacketAgentData::getApiVersion() const { return m_ApiVersion; } diff --git a/src/restful/model/PacketAgentData.h b/src/restful/model/PacketAgentData.h index 5831e2a88..6fcb708dc 100644 --- a/src/restful/model/PacketAgentData.h +++ b/src/restful/model/PacketAgentData.h @@ -224,6 +224,14 @@ namespace io { void unsetForwardRateLimit(); + int32_t getCapTime() const; + + void setCapTime(int32_t value); + + bool capTimeIsSet() const; + + void unsetCapTime(); + std::string getApiVersion() const; void setApiVersion(std::string value); @@ -305,6 +313,8 @@ namespace io { bool m_ExtensionFlagIsSet; std::vector m_ObservationPointIds; bool m_ObservationPointIdsIsSet; + int32_t m_CapTime; + bool m_CapTimeIsSet; }; } } diff --git a/src/socketvxlan.cpp b/src/socketvxlan.cpp index 1eb1cdc30..de0051542 100644 --- a/src/socketvxlan.cpp +++ b/src/socketvxlan.cpp @@ -1,4 +1,5 @@ #include "socketvxlan.h" +#include #include #include #include "statislog.h" @@ -6,7 +7,7 @@ const int INVALIDE_SOCKET_FD = -1; PcapExportVxlan::PcapExportVxlan(const std::vector& remoteips, uint32_t vni, const std::string& bind_device, const int pmtudisc, const int vxlan_port, double mbps, uint8_t vni_version, - LogFileContext& ctx) : + LogFileContext& ctx, int capTime) : _remoteips(remoteips), _vni(vni), _vni_version(vni_version), @@ -16,7 +17,8 @@ PcapExportVxlan::PcapExportVxlan(const std::vector& remoteips, uint _socketfds(remoteips.size()), _remote_addrs(remoteips.size()), _vxlanbuffers(remoteips.size()), - _ctx(ctx) { + _ctx(ctx), + _capTime(capTime){ setExportTypeAndMbps(exporttype::vxlan, mbps); for (size_t i = 0; i < remoteips.size(); ++i) { _socketfds[i] = INVALIDE_SOCKET_FD; @@ -129,11 +131,19 @@ int PcapExportVxlan::exportPacket(size_t index, const struct pcap_pkthdr* header auto& remote_addr = _remote_addrs[index]; size_t length = (size_t) (header->caplen <= 65535 ? header->caplen : 65535); - + vxlan_hdr_t* hdr; hdr = (vxlan_hdr_t*)&*vxlanbuffer.begin(); std::memcpy(reinterpret_cast(&(vxlanbuffer[sizeof(vxlan_hdr_t)])), reinterpret_cast(pkt_data), length); + uint32_t tv_sec = htonl(header->ts.tv_sec); + uint32_t tv_usec = htonl(header->ts.tv_usec); + if (_capTime == 1) { + memcpy(reinterpret_cast(&(vxlanbuffer[sizeof(vxlan_hdr_t)]) + length), &tv_sec, sizeof(uint32_t)); + length += 4; + memcpy(reinterpret_cast(&(vxlanbuffer[sizeof(vxlan_hdr_t)]) + length), &tv_usec, sizeof(uint32_t)); + length += 4; + } if (_vni_version == 1) { hdr->vx_vni = htonl(_vni<<8); if (direct != 0) { diff --git a/src/socketvxlan.h b/src/socketvxlan.h index ab7599a64..e9a181746 100644 --- a/src/socketvxlan.h +++ b/src/socketvxlan.h @@ -21,6 +21,7 @@ class PcapExportVxlan : public PcapExportBase { std::vector> _vxlanbuffers; LogFileContext _ctx; std::string output_buffer; + int _capTime; private: int initSockets(size_t index, uint32_t vni); @@ -28,7 +29,7 @@ class PcapExportVxlan : public PcapExportBase { public: PcapExportVxlan(const std::vector& remoteips, uint32_t vni, const std::string& bind_device, - const int pmtudisc, const int vxlan_port, double mbps,uint8_t vni_version, LogFileContext& ctx); + const int pmtudisc, const int vxlan_port, double mbps,uint8_t vni_version, LogFileContext& ctx, int capTime); ~PcapExportVxlan(); int initExport(); int exportPacket(const struct pcap_pkthdr *header, const uint8_t *pkt_data, int direct);