aegis-dos-protection/source/main.cpp

109 lines
2.9 KiB
C++
Raw Permalink Normal View History

2021-10-23 14:53:40 +00:00
#include <signal.h>
2021-10-24 10:51:25 +00:00
#include "Configurator.hpp"
2021-10-23 14:53:40 +00:00
#include "Initializer.hpp"
#include "PacketDissection/PacketContainer.hpp"
#include "Threads/DefenseThread.hpp"
2021-10-24 10:51:25 +00:00
#include "Threads/StatisticsThread.hpp"
2021-10-23 14:53:40 +00:00
2021-10-24 10:51:25 +00:00
int main(int argc, char** argv);
2021-10-23 14:53:40 +00:00
void handle_quit(int signum);
bool quit = false;
int main(int argc, char** argv) {
// ===== INITIALIZE ===== //
// Register signal and signal handler
signal(SIGINT, handle_quit);
Configurator::instance()->read_config("../config.json");
Initializer* init = new Initializer();
unsigned int lcore_id;
2021-10-24 10:51:25 +00:00
uint16_t inside_port = 0;
uint16_t outside_port = 1;
2021-10-23 14:53:40 +00:00
uint16_t nb_worker_threads = 0;
struct rte_mempool* mbuf_pool =
init->init_dpdk(argc, argv, nb_worker_threads);
// create thread objects
2021-10-24 10:51:25 +00:00
StatisticsThread* stat_thread = new StatisticsThread();
DefenseThread* thread_arr[nb_worker_threads];
PacketContainer* pkt_containers_to_outside[nb_worker_threads];
PacketContainer* pkt_containers_to_inside[nb_worker_threads];
2021-10-23 14:53:40 +00:00
for (int i = 0; i < nb_worker_threads; i++) {
pkt_containers_to_outside[i] =
2021-10-24 10:51:25 +00:00
new PacketContainer(mbuf_pool, inside_port, outside_port, i, i);
pkt_containers_to_inside[i] =
new PacketContainer(mbuf_pool, outside_port, inside_port, i, i);
2021-10-23 14:53:40 +00:00
2021-10-24 10:51:25 +00:00
thread_arr[i] = new DefenseThread(pkt_containers_to_outside[i],
pkt_containers_to_inside[i], stat_thread);
2021-10-23 14:53:40 +00:00
}
2021-10-24 10:51:25 +00:00
// start DefenseThreads
// ..start each thread on an own lcore
lcore_id = rte_lcore_id();
2021-10-23 14:53:40 +00:00
for (int i = 0; i < nb_worker_threads; ++i) {
lcore_id = rte_get_next_lcore(lcore_id, true, true);
rte_eal_remote_launch(
static_cast<lcore_function_t*>(DefenseThread::s_run), thread_arr[i],
lcore_id);
}
2021-10-24 10:51:25 +00:00
uint64_t hz = rte_get_tsc_hz();
u_int64_t cycles = rte_get_tsc_cycles();
u_int64_t old_cycles = cycles;
while (likely(quit == false)) {
cycles = rte_get_tsc_cycles();
if (cycles - old_cycles > 64 * hz) {
Treatment::s_increment_timestamp();
old_cycles = cycles;
2021-10-23 14:53:40 +00:00
}
}
// ===== TERMINATE ===== //
2021-10-24 10:51:25 +00:00
std::cout << "\nterminating..." << std::endl;
for (int i = 0; i < nb_worker_threads; ++i) {
thread_arr[i]->quit();
}
stat_thread->quit();
2021-10-23 14:53:40 +00:00
// destruct objects on heap
for (int i = 0; i < nb_worker_threads; ++i) {
delete thread_arr[i];
thread_arr[i] = nullptr;
delete pkt_containers_to_inside[i];
pkt_containers_to_inside[i] = nullptr;
2021-10-24 10:51:25 +00:00
delete pkt_containers_to_outside[i];
pkt_containers_to_outside[i] = nullptr;
2021-10-23 14:53:40 +00:00
}
2021-10-24 10:51:25 +00:00
delete stat_thread;
stat_thread = nullptr;
2021-10-23 14:53:40 +00:00
delete init;
init = nullptr;
// cleanup eal
rte_eal_mp_wait_lcore();
rte_eal_cleanup();
// YOU ARE TERMINATED
}
void handle_quit(int signum) {
2021-10-24 10:51:25 +00:00
//
quit = true;
2021-10-23 14:53:40 +00:00
}
2021-10-24 10:51:25 +00:00
//