Skip to content

Commit

Permalink
add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
robbietu committed Dec 4, 2024
1 parent 4e23a9e commit 783022e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/daemonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ std::string DaemonManager::createParams(std::shared_ptr<io::swagger::server::mod
}
}
return boost::str(boost::format("%1%%2% --control %3%%4%%5%%6%%7%%8%")
return boost::str(boost::format("%1%%2% --control %3%%4%%5%%6%%7%%8%%9%")
% (data->addressIsSet()
? std::string(" -r ") + data->getAddress() : std::string())
% (data->getBpf().empty()
Expand All @@ -458,6 +458,8 @@ std::string DaemonManager::createParams(std::shared_ptr<io::swagger::server::mod
boost::format(" %1%") % data->getStartup()) : 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()));
}
Expand Down Expand Up @@ -1204,7 +1206,7 @@ void DaemonManager::scanNetworkInterfaceImpl() {
interfaces = readInterfaces(ctx_);

std::lock_guard<std::recursive_mutex> lock{mtx_};
if(interfaces != interfaces_)
if(interfaces_.empty())
{
ctx_.log("scanNetworkInterface changed ", log4cpp::Priority::INFO);
LOG(INFO) << "scanNetworkInterface changed ";
Expand Down
21 changes: 21 additions & 0 deletions src/pcaphandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:" <<path<<std::endl;
return -1;
}

if (setns(fd1, 0) == -1) {
std::cerr << "Can not set the namespace:" <<path<<std::endl;
return -1;
}
if (fd1 != -1) {
close(fd1);
}
}
#endif
_pcap_handle = pcap_handle;

return 0;
Expand Down
7 changes: 6 additions & 1 deletion src/pktminerg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ Allowed options for each interface:");
"specify a floating point value for the Mbps rate that pktmg should send packets at.")
("dir", boost::program_options::value<std::string>()->value_name("DIR"),
"specify the direction determination expression")
("capTime", boost::program_options::value<int>()->value_name("CAPTIME"),"set capture time in tranferred packets for VxLan")
#ifdef _WIN32
("macRefreshInterval", boost::program_options::value<int>()->value_name("MACREFRESHINTERVAL"),
"specify the interval to refresh mac list of VM and PA")
Expand Down Expand Up @@ -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;
Expand All @@ -746,6 +748,9 @@ Allowed options for each interface:");
keybit = vm["keybit"].as<uint32_t>();
}

if(vm.count("capTime")) {
capTime = vm["capTime"].as<int>();
}
std::string intface = "";
if (vm.count("interface")) {
auto strs = vm["interface"].as<std::vector<std::string>>();
Expand Down Expand Up @@ -945,7 +950,7 @@ Allowed options for each interface:");

case exporttype::vxlan:
exportPtr = std::make_shared<PcapExportVxlan>(remoteips, vni, bind_device, pmtudisc, vxlan_port,
vm["mbps"].as<double>(), vni_version, ctx);
vm["mbps"].as<double>(), vni_version, ctx, capTime);
break;

default:
Expand Down
24 changes: 23 additions & 1 deletion src/restful/model/PacketAgentData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/restful/model/PacketAgentData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -305,6 +313,8 @@ namespace io {
bool m_ExtensionFlagIsSet;
std::vector<uint8_t> m_ObservationPointIds;
bool m_ObservationPointIdsIsSet;
int32_t m_CapTime;
bool m_CapTimeIsSet;
};
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/socketvxlan.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "socketvxlan.h"
#include <cstdint>
#include <cstring>
#include <iostream>
#include "statislog.h"
const int INVALIDE_SOCKET_FD = -1;

PcapExportVxlan::PcapExportVxlan(const std::vector<std::string>& 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),
Expand All @@ -16,7 +17,8 @@ PcapExportVxlan::PcapExportVxlan(const std::vector<std::string>& 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;
Expand Down Expand Up @@ -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<void*>(&(vxlanbuffer[sizeof(vxlan_hdr_t)])),
reinterpret_cast<const void*>(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<void*>(&(vxlanbuffer[sizeof(vxlan_hdr_t)]) + length), &tv_sec, sizeof(uint32_t));
length += 4;
memcpy(reinterpret_cast<void*>(&(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) {
Expand Down
3 changes: 2 additions & 1 deletion src/socketvxlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class PcapExportVxlan : public PcapExportBase {
std::vector<std::vector<char>> _vxlanbuffers;
LogFileContext _ctx;
std::string output_buffer;
int _capTime;

private:
int initSockets(size_t index, uint32_t vni);
int exportPacket(size_t index, const struct pcap_pkthdr *header, const uint8_t *pkt_data, int direct);

public:
PcapExportVxlan(const std::vector<std::string>& 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);
Expand Down

0 comments on commit 783022e

Please sign in to comment.