arping ip and interface from client interface

This commit is contained in:
maliakal_d 2022-02-01 14:03:48 +01:00
parent c236cbf17b
commit bf83c9b3e2
6 changed files with 67 additions and 12 deletions

View File

@ -1399,6 +1399,11 @@ sls::MacAddr ClientInterface::setUdpIp(sls::IpAddr arg) {
if (detType == EIGER) { if (detType == EIGER) {
impl()->setEthernetInterface2(eth); impl()->setEthernetInterface2(eth);
} }
// update locally to use for arping
udpips.clear();
udpips.push_back(arg.str());
// get mac address // get mac address
auto retval = sls::InterfaceNameToMac(eth); auto retval = sls::InterfaceNameToMac(eth);
if (retval == 0) { if (retval == 0) {
@ -1431,6 +1436,9 @@ sls::MacAddr ClientInterface::setUdpIp2(sls::IpAddr arg) {
} }
impl()->setEthernetInterface2(eth); impl()->setEthernetInterface2(eth);
// update locally to use for arping
udpips.push_back(arg.str());
// get mac address // get mac address
auto retval = sls::InterfaceNameToMac(eth); auto retval = sls::InterfaceNameToMac(eth);
if (retval == 0) { if (retval == 0) {
@ -1712,6 +1720,6 @@ int ClientInterface::set_arping(Interface &socket) {
} }
verifyIdle(socket); verifyIdle(socket);
LOG(logDEBUG1) << "Starting/ Killing arping thread:" << value; LOG(logDEBUG1) << "Starting/ Killing arping thread:" << value;
impl()->setArping(value); impl()->setArping(value, udpips);
return socket.Send(OK); return socket.Send(OK);
} }

View File

@ -192,4 +192,5 @@ class ClientInterface : private virtual slsDetectorDefs {
pid_t parentThreadId{0}; pid_t parentThreadId{0};
pid_t tcpThreadId{0}; pid_t tcpThreadId{0};
std::vector<std::string> udpips;
}; };

View File

@ -330,13 +330,21 @@ std::array<pid_t, NUM_RX_THREAD_IDS> Implementation::getThreadIds() const {
bool Implementation::getArping() const { return threadArping->IsRunning(); } bool Implementation::getArping() const { return threadArping->IsRunning(); }
void Implementation::setArping(const bool i) { pid_t Implementation::getArpingThreadId() const {
return threadArping->GetThreadId();
}
void Implementation::setArping(const bool i,
const std::vector<std::string> ips) {
if (i != threadArping->IsRunning()) { if (i != threadArping->IsRunning()) {
if (!i) { if (!i) {
threadArping->StopRunning(); threadArping->StopRunning();
} else { } else {
threadArping->ClearIpsAndInterfaces(); threadArping->ClearIpsAndInterfaces();
threadArping->AddIpsAndInterfaces(eth[0], "10.0.0.1"); threadArping->AddIpsAndInterfaces(eth[0], ips[0]);
if (numUDPInterfaces == 2) {
threadArping->AddIpsAndInterfaces(eth[1], ips[1]);
}
threadArping->StartRunning(); threadArping->StartRunning();
} }
} }

View File

@ -51,7 +51,8 @@ class Implementation : private virtual slsDetectorDefs {
void setThreadIds(const pid_t parentTid, const pid_t tcpTid); void setThreadIds(const pid_t parentTid, const pid_t tcpTid);
std::array<pid_t, NUM_RX_THREAD_IDS> getThreadIds() const; std::array<pid_t, NUM_RX_THREAD_IDS> getThreadIds() const;
bool getArping() const; bool getArping() const;
void setArping(const bool i); pid_t getArpingThreadId() const;
void setArping(const bool i, const std::vector<std::string> ips);
/************************************************** /**************************************************
* * * *

View File

@ -11,6 +11,8 @@ ThreadArping::ThreadArping() {}
ThreadArping::~ThreadArping() { StopRunning(); } ThreadArping::~ThreadArping() { StopRunning(); }
pid_t ThreadArping::GetThreadId() const { return threadId; }
bool ThreadArping::IsRunning() const { return runningFlag; } bool ThreadArping::IsRunning() const { return runningFlag; }
void ThreadArping::StartRunning() { void ThreadArping::StartRunning() {
@ -33,9 +35,8 @@ void ThreadArping::StartRunning() {
void ThreadArping::StopRunning() { void ThreadArping::StopRunning() {
pthread_cancel(threadObject); pthread_cancel(threadObject);
LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << "]"; LOG(logINFOBLUE) << "Killing [ Arping Thread, Tid: " << threadId << " ]";
} runningFlag = false;
runningFlag = false;
} }
void ThreadArping::ClearIpsAndInterfaces() { arpInterfaceIp.clear(); } void ThreadArping::ClearIpsAndInterfaces() { arpInterfaceIp.clear(); }
@ -47,14 +48,48 @@ void ThreadArping::AddIpsAndInterfaces(std::string interface, std::string ip) {
void ThreadArping::RunningThread() { void ThreadArping::RunningThread() {
threadId = syscall(SYS_gettid); threadId = syscall(SYS_gettid);
LOG(logINFOBLUE) << "Created [ Arping Thread, Tid: " << threadId << "]"; {
std::ostringstream os;
os << "Created [ Arping Thread, Tid: " << threadId << " ] for ";
for (auto ethip : arpInterfaceIp) {
os << "\n\t[ " << ethip.first << ", " << ethip.second << " ]";
}
LOG(logINFOBLUE) << os.str();
}
// create the commands to ping necessary interfaces
std::vector<std::string> commands;
for (auto ethip : arpInterfaceIp) {
std::ostringstream os;
os << "arping -c 1 -U -I " << ethip.first << " " << ethip.second;
// to read error messages
os << " 2>&1";
std::string cmd = os.str();
commands.push_back(cmd);
}
while (IsRunning()) { while (IsRunning()) {
LOG(logINFOBLUE) << "Going to sleep";
// arping
for (auto cmd : commands) {
LOG(logDEBUG) << "Executing Arping Command: " << cmd;
// execute command and check for errors
FILE *sysFile = popen(cmd.c_str(), "r");
char output[MAX_STR_LENGTH] = {0};
fgets(output, sizeof(output), sysFile);
output[sizeof(output) - 1] = '\0';
if (pclose(sysFile)) {
LOG(logERROR) << "Executing cmd[" << cmd
<< "]\n\tError Message : " << output;
} else {
LOG(logDEBUG) << output;
}
}
// wait for 60s // wait for 60s
usleep(60 * 1000 * 1000); usleep(60 * 1000 * 1000);
} }
LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << "]"; LOG(logINFOBLUE) << "Exiting [ Arping Thread, Tid: " << threadId << " ]";
} }

View File

@ -22,11 +22,13 @@ class ThreadArping : private virtual slsDetectorDefs {
pthread_t threadObject; pthread_t threadObject;
std::vector<std::pair<std::string, std::string>> arpInterfaceIp; std::vector<std::pair<std::string, std::string>> arpInterfaceIp;
pid_t threadIds; std::vector<std::string> commands;
pid_t threadId;
public: public:
ThreadArping(); ThreadArping();
virtual ~ThreadArping(); virtual ~ThreadArping();
pid_t GetThreadId() const;
bool IsRunning() const; bool IsRunning() const;
void StartRunning(); void StartRunning();
void StopRunning(); void StopRunning();
@ -39,5 +41,5 @@ class ThreadArping : private virtual slsDetectorDefs {
* RunningMask is satisfied Then it exits the thread on its own if * RunningMask is satisfied Then it exits the thread on its own if
* killThread is true * killThread is true
*/ */
void RunningThread(std::string interface, std::string ip); void RunningThread();
}; };