update 0.0.2

This commit is contained in:
2021-10-24 12:51:25 +02:00
parent d80e285bb4
commit c2c321eb9b
30 changed files with 1689 additions and 1333 deletions

View File

@@ -1,28 +1,20 @@
#include <iostream>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>
#include "Definitions.hpp"
#include "Threads/DefenseThread.hpp"
#include "Threads/StatisticsThread.hpp"
// ===== PUBLIC ===== //
DefenseThread::DefenseThread(
MbufContainerReceiving* mbuf_container_from_inside,
MbufContainerReceiving* mbuf_container_from_outside,
MbufContainerTransmitting* mbuf_container_to_inside,
MbufContainerTransmitting* mbuf_container_to_outside)
: Thread()
, _mbuf_container_from_inside(mbuf_container_from_inside)
, _mbuf_container_from_outside(mbuf_container_from_outside)
, _mbuf_container_to_inside(mbuf_container_to_inside)
, _mbuf_container_to_outside(mbuf_container_to_outside)
, _do_treat(false) {
_treatment =
new Treatment(_mbuf_container_to_outside, _mbuf_container_to_inside, 0);
}
DefenseThread::DefenseThread(PacketContainer* pkt_container_to_inside,
PacketContainer* pkt_container_to_outside,
StatisticsThread* stat_thread)
: ForwardingThread(pkt_container_to_inside, pkt_container_to_outside),
_treatment(pkt_container_to_inside, pkt_container_to_outside),
_statistics_thread(stat_thread) {}
int DefenseThread::s_run(void* thread_vptr) {
DefenseThread* thread = static_cast<DefenseThread*>(thread_vptr);
@@ -33,94 +25,54 @@ int DefenseThread::s_run(void* thread_vptr) {
// ===== PRIVATE ===== //
void DefenseThread::run() {
/*
LOG_INFO << "\nRunning on lcore " << rte_lcore_id() << ". [Ctrl+C to quit]"
<< LOG_END;
*/
uint16_t nb_pkts_to_inside;
uint16_t nb_pkts_to_outside;
BOOST_LOG_TRIVIAL(info)
<< "\nRunning on lcore " << rte_lcore_id() << ". [Ctrl+C to quit]\n";
// stats for StatisticThread_testing
Stats* thread_statistics = new Stats();
thread_statistics->attacks = rte_lcore_id();
thread_statistics->bytes = rte_lcore_id();
thread_statistics->dropped = rte_lcore_id();
thread_statistics->packets = rte_lcore_id();
thread_statistics->work_time = rte_lcore_id();
// Run until the application is quit or killed.
while (likely(_quit == false)) {
_statistics_thread->enqueue_statistics(rte_lcore_id(),
thread_statistics);
// std::cout << _do_treat << std::endl;
// ===== ALICE --[DAVE]--> BOB ===== //
// continue if no packets are received
int mbufs_from_inside = _mbuf_container_from_inside->poll_mbufs();
/* if (mbufs_from_inside != 0) {
BOOST_LOG_TRIVIAL(info)
<< "Number of packets received from inside: "
<< mbufs_from_inside;
} */
bool keep = true;
for (int i = 0; i < mbufs_from_inside; ++i) {
rte_mbuf* mbuf = _mbuf_container_from_inside->get_mbuf_at_index(i);
_pkt_container_to_inside->poll_packets(nb_pkts_to_inside);
if (likely(nb_pkts_to_inside > 0)) {
if (PacketInfoCreator::is_ipv4_tcp(mbuf) == true &&
_do_treat == true) {
PacketInfoIpv4Tcp* pkt_info = new PacketInfoIpv4Tcp(mbuf);
keep = _treatment->treat_packets_to_outside(pkt_info);
if (keep == false) {
// delete PacketInfo
delete pkt_info;
}
} else {
// fwd
// std::cout << _do_treat;
_mbuf_container_to_outside->add_mbuf(mbuf);
}
/// TODO: implement pipeline
_treatment.treat_packets_to_inside();
_pkt_container_to_inside->send_packets();
_pkt_container_to_outside->send_packets();
}
_mbuf_container_to_inside->send_mbufs();
_mbuf_container_to_outside->send_mbufs();
// _mbuf_container_to_inside->send_mbufs();
// _mbuf_container_to_outside->send_mbufs();
// for mbuf in pktsFromOutside
// pktInfoCreator::determinePacketType without creating a pktinfo
// create packetInfo with obtained type
// if tcp: treat
// else: fwd
// destroy pktInfo
// Pakete von innen, auch die selbsterzeugten bevorzugen
// ===== ALICE <--[DAVE]-- BOB ===== //
// continue if no packets are received
_pkt_container_to_outside->poll_packets(nb_pkts_to_outside);
if (likely(nb_pkts_to_outside > 0)) {
int mbufs_from_outside = _mbuf_container_from_outside->poll_mbufs();
/* if (mbufs_from_outside != 0) {
BOOST_LOG_TRIVIAL(info)
<< "Number of packets received from outside: "
<< mbufs_from_outside;
} */
for (int i = 0; i < mbufs_from_outside; ++i) {
rte_mbuf* mbuf = _mbuf_container_from_outside->get_mbuf_at_index(i);
if (PacketInfoCreator::is_ipv4_tcp(mbuf) == true &&
_do_treat == true) {
PacketInfoIpv4Tcp* pkt_info = new PacketInfoIpv4Tcp(mbuf);
keep = _treatment->treat_packets_to_inside(
static_cast<PacketInfoIpv4Tcp*>(pkt_info));
if (keep == false) {
// delete PacketInfo
delete pkt_info;
}
} else {
// std::cout << _do_treat;
_mbuf_container_to_inside->add_mbuf(mbuf);
}
/// TODO: implement pipeline
_treatment.treat_packets_to_outside();
_pkt_container_to_inside->send_packets();
_pkt_container_to_outside->send_packets();
}
/* if (_mbuf_container_to_inside->get_number_of_mbufs() > 0) {
BOOST_LOG_TRIVIAL(info)
<< "Number of packets sent to inside: "
<< _mbuf_container_to_inside->get_number_of_mbufs();
}
if (_mbuf_container_to_outside->get_number_of_mbufs() > 0) {
BOOST_LOG_TRIVIAL(info)
<< "Number of packets sent to outside: "
<< _mbuf_container_to_outside->get_number_of_mbufs();
} */
_mbuf_container_to_inside->send_mbufs();
_mbuf_container_to_outside->send_mbufs();
if (likely(nb_pkts_to_inside != 0 || nb_pkts_to_outside != 0)) {
BOOST_LOG_TRIVIAL(info)
<< "pkts_to_inside = " << nb_pkts_to_inside
<< "\tpkts_to_outside = " << nb_pkts_to_outside << "\n";
}
}
_running = false;

View File

@@ -1,9 +1,58 @@
#include "Threads/StatisticsThread.hpp"
#include <iostream>
//-------------------------------------------------------------------------
//-------------------------- StatisticsThread -----------------------------
//-------------------------------------------------------------------------
StatisticsThread::StatisticsThread() {
std::string name;
unsigned int lcore_id = rte_lcore_id();
for (int i = 0; i < 16; ++i) {
lcore_id = rte_get_next_lcore(lcore_id, true, true);
name = "RTE_RING_THREAD_";
name += i + 1;
const char* c =
name.c_str(); // wrap string to char pointer //TODO: good name
lcore_id = rte_get_next_lcore(lcore_id, true, true);
_s_queue[i] = rte_ring_create(c, 4, rte_lcore_to_socket_id(lcore_id),
RING_F_SP_ENQ | RING_F_SC_DEQ);
// checking for errors in creating rte rings
if (_s_queue[i] == NULL) {
if (rte_errno == E_RTE_NO_CONFIG) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = E_RTE_NO_CONFIG" << std::endl;
}
if (rte_errno == E_RTE_SECONDARY) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = E_RTE_SECONDARY" << std::endl;
}
if (rte_errno == EINVAL) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = EINVAL" << std::endl;
}
if (rte_errno == ENOSPC) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = ENOSPC" << std::endl;
}
if (rte_errno == EEXIST) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = EEXIST" << std::endl;
}
if (rte_errno == ENOMEM) {
std::cout << "Error during RTE_RING creation at Ring " << i + 1
<< ", return Value = ENOMEM" << std::endl;
}
}
} // rte_lcore_to_socket_id
_s_stats_monitor = new StatsMonitor;
_s_stats = new Stats;
_s_stats_monitor->total_time = 0;
}
rte_ring* StatisticsThread::_s_queue[16];
int StatisticsThread::s_run(void* thread_vptr) {
@@ -14,42 +63,97 @@ int StatisticsThread::s_run(void* thread_vptr) {
void StatisticsThread::run() {
// Check for new statistics in each rte_ring
while (_quit != false) {
for (rte_ring* ring : _s_queue) {
// Get stats from queue and update them
std::cout << "\n StatisticThread running on lcore " << rte_lcore_id()
<< ". [Ctrl+C to quit]\n"
<< std::endl;
cycles_old = rte_get_tsc_cycles();
while (likely(_quit == false)) {
// Timer -> update_statistics_monitor
if (timer_get_seconds() >=
1) { // every second updating the stats monitor
timer_reset();
update_statistics_monitor();
}
for (struct rte_ring* ring : _s_queue) {
// get stats from each Thread and put them into _s_stats
Stats* out;
rte_ring_dequeue(ring, (void**)&out);
if (rte_ring_dequeue(ring, (void**)&out) ==
0) { // taking existing stats from ring
*_s_stats += *out; // updating _s_stats
if (true) { // \TODO test if empty
update_statistics(out);
/*delete out;
out = nullptr;*/
}
}
}
}
void StatisticsThread::enqueue_statistics(int& id, Stats* new_stats) {
// enqueue statistics
rte_ring* ring = _s_queue[id];
rte_ring_enqueue(ring, new_stats);
void StatisticsThread::enqueue_statistics(const unsigned int& id,
Stats* new_stats) {
// enqueue statistics (checked and working)
rte_ring_enqueue(_s_queue[id], (void*)new_stats);
}
Stats* StatisticsThread::_s_stats;
void StatisticsThread::update_statistics_monitor() {
// called once per second
_s_stats_monitor->attacks_per_second = _s_stats->attacks;
_s_stats_monitor->attacks_percent =
((double)_s_stats->attacks / (double)_s_stats->packets) * 100;
_s_stats_monitor->bytes_per_second = _s_stats->bytes;
_s_stats_monitor->dropped_per_second = _s_stats->dropped;
_s_stats_monitor->attacks_percent =
((double)_s_stats->dropped / (double)_s_stats->packets) * 100;
_s_stats_monitor->packets_per_second = _s_stats->packets;
//_s_stats_monitor->process_speed = ;
++_s_stats_monitor->total_time;
void StatisticsThread::update_statistics(Stats* new_stats) {
//_s_stats += new_stats;
// delete old _s_stats
_s_stats->reset_stats();
}
void StatisticsThread::print_stats_monitor(){
std::cout << "Total runtime = " << _s_stats_monitor->total_time << "s" << std::endl;
std::cout << "Bytes per second = " << _s_stats_monitor->bytes_per_second << std::endl;
std::cout << "Packets per second = " << _s_stats_monitor->packets_per_second << std::endl;
std::cout << "Attacks per Second = " << _s_stats_monitor->attacks_per_second << std::endl;
std::cout << "Attacks in percent = " << _s_stats_monitor->attacks_percent << "%" << std::endl;
std::cout << "Dropped packets per second = " << _s_stats_monitor->dropped_per_second << std::endl;
std::cout << "Dropped packets in percent = " << _s_stats_monitor->dropped_percent << "%" << std::endl;
std::cout << "Prozess speed = " << _s_stats_monitor->process_speed << std::endl;
}
uint64_t StatisticsThread::timer_get_seconds() {
cycles = rte_get_tsc_cycles();
hz = rte_get_tsc_hz();
//calculate
seconds = (cycles - cycles_old) / hz;
return seconds;
}
void StatisticsThread::timer_reset() {
seconds = 0;
cycles_old = cycles;
}
//-------------------------------------------------------------------------
//-------------------------- Stats ----------------------------------------
//------------------------------- Stats -----------------------------------
//-------------------------------------------------------------------------
Stats* Stats::operator+=(const Stats* new_stats) {
this->attacks += new_stats->attacks;
this->bytes += new_stats->bytes;
this->dropped += new_stats->dropped;
this->packets += new_stats->packets;
this->work_time += new_stats->work_time;
this->syn_level += new_stats->syn_level;
return this;
Stats& Stats::operator+=(const Stats& new_stats) {
attacks += new_stats.attacks;
bytes += new_stats.bytes;
dropped += new_stats.dropped;
packets += new_stats.packets;
work_time += new_stats.work_time;
}
void Stats::reset_stats() {
attacks = 0;
bytes = 0;
dropped = 0;
packets = 0;
work_time = 0;
}