Skip to content

Commit

Permalink
refactor: drop dataplane's report server
Browse files Browse the repository at this point in the history
It's useless anyway.
Add a bit of clarification comments, also drop no longer used includes.

Resolves #115.
  • Loading branch information
3Hren committed Feb 8, 2024
1 parent 535ec75 commit 8c5b411
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 138 deletions.
8 changes: 0 additions & 8 deletions dataplane/dataplane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ eResult cDataPlane::init(const std::string& binaryPath,
return result;
}

result = report.init();
if (result != eResult::success)
{
return result;
}

result = controlPlane->init(config.use_kernel_interface);
if (result != eResult::success)
{
Expand Down Expand Up @@ -1265,7 +1259,6 @@ void cDataPlane::start()
timestamp_thread();
});

report.run();
bus.run();

/// run forwarding plane and control plane
Expand All @@ -1276,7 +1269,6 @@ void cDataPlane::join()
{
rte_eal_mp_wait_lcore();

report.join();
bus.join();
}

Expand Down
115 changes: 2 additions & 113 deletions dataplane/report.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <rte_ethdev.h>
#include <rte_mempool.h>

#include "common.h"
#include "dataplane.h"
#include "report.h"
#include "worker.h"
Expand Down Expand Up @@ -55,59 +50,10 @@ nlohmann::json convertHashtable(const hashtable_mod_T& hashtable, const stats_T&
} // namespace

cReport::cReport(cDataPlane* dataPlane) :
dataPlane(dataPlane),
serverSocket(-1)
dataPlane(dataPlane)
{
}

eResult cReport::init()
{
serverSocket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket < 0)
{
serverSocket = -1;
return eResult::errorSocket;
}

if (fcntl(serverSocket, F_GETFL) & O_NONBLOCK)
{
if (fcntl(serverSocket, F_SETFL, fcntl(serverSocket, F_GETFL) & (~O_NONBLOCK)) < 0)
{
return eResult::errorSocket;
}
}

int one = 1;
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
{
return eResult::errorSocket;
}

return eResult::success;
}

void cReport::run()
{
thread = std::thread([this] { mainLoop(); });
}

void cReport::stop()
{
if (serverSocket != -1)
{
shutdown(serverSocket, SHUT_RDWR);
close(serverSocket);
}
}

void cReport::join()
{
if (thread.joinable())
{
thread.join();
}
}

nlohmann::json cReport::getReport()
{
nlohmann::json jsonReport;
Expand Down Expand Up @@ -164,63 +110,6 @@ nlohmann::json cReport::getReport()
return jsonReport;
}

static void sendAll(int socket, const char* buffer, size_t bufferSize)
{
size_t total = 0;

while (total < bufferSize)
{
int sendSize = send(socket, buffer + total, bufferSize - total, MSG_NOSIGNAL);
if (sendSize <= 0)
{
return;
}

total += sendSize;
}
}

void cReport::mainLoop()
{
sockaddr_in6 address;
memset((char*)&address, 0, sizeof(address));
address.sin6_family = AF_INET6;
address.sin6_addr = in6addr_any;
address.sin6_port = htons(29929); ///< @todo

if (bind(serverSocket, (struct sockaddr*)&address, sizeof(address)) < 0)
{
YADECAP_LOG_ERROR("bind()\n");
return;
}

if (listen(serverSocket, 64) < 0)
{
YADECAP_LOG_ERROR("listen()\n");
return;
}

for (;;)
{
struct sockaddr_in6 address;
socklen_t addressLength = sizeof(address);
int clientSocket = accept(serverSocket, (struct sockaddr*)&address, &addressLength);
if (clientSocket < 0)
{
continue;
}

nlohmann::json jsonReport = getReport();

std::string dump = jsonReport.dump();
sendAll(clientSocket, dump.data(), dump.size());

close(clientSocket);
}

serverSocket = -1;
}

std::string pointerToHex(const void* pointer)
{
char buffer[128];
Expand Down Expand Up @@ -309,7 +198,7 @@ nlohmann::json cReport::convertWorker(const cWorker* worker)
counterId < CONFIG_YADECAP_COUNTERS_SIZE;
counterId++)
{
json["counters"].emplace_back(worker->counters[counterId]);
json["counters"].emplace_back(worker->counters[counterId]);
}
*/

Expand Down
28 changes: 11 additions & 17 deletions dataplane/report.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
#pragma once

#include <thread>

#include <nlohmann/json.hpp>

#include "common/result.h"
#include "common/type.h"

#include "type.h"

/// Provides various reports about the current state of the dataplane in JSON format.
class cReport
{
public:
cReport(cDataPlane* dataPlane);

eResult init();
void run();
void stop();
void join();

explicit cReport(cDataPlane* dataPlane);

/// Returns the current state of the dataplane.
///
/// This function is not marked as const, because eventually it calls DPDK
/// functions, which in its turn call driver's functions, which can
/// modify its own state.
///
/// As a result, this method itself is not thread-safe. Be careful calling
/// DPDK functions in multiple threads parallel with this method.
nlohmann::json getReport();

protected:
void mainLoop();

nlohmann::json convertWorker(const cWorker* worker);
nlohmann::json convertWorkerGC(const worker_gc_t* worker);
nlohmann::json convertMempool(const rte_mempool* mempool);
Expand All @@ -35,7 +32,4 @@ class cReport

protected:
cDataPlane* dataPlane;

std::thread thread;
int serverSocket;
};

0 comments on commit 8c5b411

Please sign in to comment.