version 0.0.1
This commit is contained in:
37
test/Attacker/Attacker_test.cpp
Normal file
37
test/Attacker/Attacker_test.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <catch2/catch.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include <rte_cycles.h>
|
||||
|
||||
TEST_CASE("tsc timer", "[]") {
|
||||
// count and print seconds since test started
|
||||
// stop at 30 seconds
|
||||
// const std::string clear(100, '\n');
|
||||
const uint64_t MAX_SECONDS = 30;
|
||||
uint64_t cycles_old = 0;
|
||||
uint64_t cycles = 0;
|
||||
uint64_t hz = rte_get_tsc_hz();
|
||||
uint64_t seconds = 0;
|
||||
uint64_t delta_t = 0;
|
||||
|
||||
// print initial message
|
||||
std::cout << "cycles : " << cycles << "\t"
|
||||
<< "hz : " << hz << "\t"
|
||||
<< "seconds : " << seconds << "\t" << std::endl;
|
||||
|
||||
while (seconds < MAX_SECONDS) {
|
||||
cycles_old = cycles;
|
||||
cycles = rte_get_tsc_cycles();
|
||||
hz = rte_get_tsc_hz();
|
||||
|
||||
// calculate
|
||||
delta_t = uint64_t(1 / hz * (cycles - cycles_old));
|
||||
seconds += delta_t;
|
||||
|
||||
// print
|
||||
// std::cout << clear;
|
||||
std::cout << "cycles : " << cycles << "\t"
|
||||
<< "hz : " << hz << "\t"
|
||||
<< "seconds : " << seconds << "\t" << std::endl;
|
||||
}
|
||||
}
|
||||
1
test/Attacker/meson.build
Normal file
1
test/Attacker/meson.build
Normal file
@@ -0,0 +1 @@
|
||||
Attacker_sources = ['test/Attacker/Attacker_test.cpp']
|
||||
16
test/Attacker/namespace-alice.sh
Normal file
16
test/Attacker/namespace-alice.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
# create network namespace aegisns
|
||||
ip netns add AEGISNS
|
||||
# assing interface enp4s0f0 to aegisns
|
||||
ip link set enp5s0f0 netns AEGISNS
|
||||
# assing ip to interface
|
||||
ip netns exec AEGISNS ip add add dev enp5s0f0 10.0.0.1/24
|
||||
# bring the link up
|
||||
ip netns exec AEGISNS ip link set dev enp5s0f0 up
|
||||
# add the standard route for the interface
|
||||
ip netns exec AEGISNS route add default gw 10.0.0.2 enp5s0f0
|
||||
# show wether the project has been successful
|
||||
ip netns exec AEGISNS ifconfig
|
||||
ip netns exec AEGISNS route
|
||||
|
||||
|
||||
|
||||
14
test/Attacker/namespace-bob.sh
Executable file
14
test/Attacker/namespace-bob.sh
Executable file
@@ -0,0 +1,14 @@
|
||||
# create network namespace aegisns
|
||||
ip netns add AEGISNS
|
||||
# assing interface enp4s0f0 to aegisns
|
||||
ip link set enp4s0f0 netns AEGISNS
|
||||
# assing ip to interface
|
||||
ip netns exec AEGISNS ip add add dev enp4s0f0 10.0.0.2/24
|
||||
# bring the link up
|
||||
ip netns exec AEGISNS ip link set dev enp4s0f0 up
|
||||
# add the standard route for the interface
|
||||
ip netns exec AEGISNS route add default gw 10.0.0.1 enp4s0f0
|
||||
# show wether the project has been successful
|
||||
ip netns exec AEGISNS ifconfig
|
||||
ip netns exec AEGISNS route
|
||||
|
||||
52
test/ConfigurationManagement/Configurator_test.cpp
Normal file
52
test/ConfigurationManagement/Configurator_test.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "ConfigurationManagement/Configurator.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("Json Datei einlesen", "[]") {
|
||||
|
||||
REQUIRE_NOTHROW(Configurator::instance()->read_config(
|
||||
"../test/ConfigurationManagement/config_test.json"));
|
||||
|
||||
REQUIRE(Configurator::instance()->get_config_as_bool("BOOLEAN") == true);
|
||||
REQUIRE(Configurator::instance()->get_config_as_unsigned_int(
|
||||
"UNSIGNED_INT") == 42);
|
||||
REQUIRE(Configurator::instance()->get_config_as_string("STRING") ==
|
||||
"Hello World.");
|
||||
REQUIRE(Configurator::instance()->get_config_as_float("FLOAT") == 1.337f);
|
||||
REQUIRE(Configurator::instance()->get_config_as_double("DOUBLE") == -3.001);
|
||||
}
|
||||
|
||||
TEST_CASE("nicht existierende Json-Datei", "[]") {
|
||||
REQUIRE_THROWS(Configurator::instance()->read_config("non-existent.json"));
|
||||
REQUIRE_THROWS(Configurator::instance()->read_config("non-existent.json",
|
||||
"typo.json"));
|
||||
}
|
||||
|
||||
TEST_CASE("Boost-Beispiel") {
|
||||
LOG_INFO << "Dies ist eine Info Message" << LOG_END;
|
||||
LOG_WARNING << "Dies ist eine Warn-Nachricht" << LOG_END;
|
||||
}
|
||||
|
||||
TEST_CASE("Entry does (not) exist") {
|
||||
REQUIRE_NOTHROW(Configurator::instance()->read_config(
|
||||
"../test/ConfigurationManagement/config_test.json",
|
||||
"../test/ConfigurationManagement/default_config_test.json"));
|
||||
|
||||
REQUIRE(Configurator::instance()->entry_exists("fhk4bhf1mx0f") == false);
|
||||
REQUIRE(Configurator::instance()->entry_exists("STRING") == true);
|
||||
REQUIRE(Configurator::instance()->entry_exists("X") == true);
|
||||
}
|
||||
|
||||
TEST_CASE("Default Config") {
|
||||
REQUIRE_NOTHROW(Configurator::instance()->read_config(
|
||||
"../test/ConfigurationManagement/config_test.json",
|
||||
"../test/ConfigurationManagement/default_config_test.json"));
|
||||
|
||||
REQUIRE(Configurator::instance()->get_config_as_unsigned_int(
|
||||
"UNSIGNED_INT") == 42);
|
||||
REQUIRE(Configurator::instance()->get_config_as_unsigned_int("UNSIGNED_INT",
|
||||
true) == 666);
|
||||
REQUIRE(Configurator::instance()->get_config_as_string("X") == "80085");
|
||||
}
|
||||
7
test/ConfigurationManagement/config_test.json
Normal file
7
test/ConfigurationManagement/config_test.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"STRING" : "Hello World.",
|
||||
"BOOLEAN" : true,
|
||||
"UNSIGNED_INT" : 42,
|
||||
"FLOAT" : 1.337,
|
||||
"DOUBLE" : -3.001
|
||||
}
|
||||
4
test/ConfigurationManagement/default_config_test.json
Normal file
4
test/ConfigurationManagement/default_config_test.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"UNSIGNED_INT": 666,
|
||||
"X": "80085"
|
||||
}
|
||||
9
test/Initializer/default_config_test.json
Normal file
9
test/Initializer/default_config_test.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"STRING" : "Hello World.",
|
||||
"BOOLEAN" : true,
|
||||
"UNSIGNED_INT" : 666,
|
||||
"FLOAT" : 1.337,
|
||||
"DOUBLE" : -3.001,
|
||||
"X" : "80085",
|
||||
"Y" : "Hi"
|
||||
}
|
||||
49
test/Initializer_test.cpp
Normal file
49
test/Initializer_test.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
#include "ConfigurationManagement/Configurator.hpp"
|
||||
|
||||
TEST_CASE("Calculate number of threads", "[]") {
|
||||
/*
|
||||
Configurator::instance()->read_config("../test/config.json");
|
||||
|
||||
uint16_t nb_worker_threads = 0;
|
||||
// calculate default value of worker threads
|
||||
uint16_t nb_worker_threads_default = 12;
|
||||
|
||||
// get number of threads from config
|
||||
std::string nb = Configurator::instance()->get_config_as_string(
|
||||
"number_of_worker_threads");
|
||||
|
||||
if (nb == "default") {
|
||||
nb_worker_threads = nb_worker_threads_default;
|
||||
}
|
||||
else {
|
||||
|
||||
uint16_t nb_int = std::stoi(nb);
|
||||
|
||||
REQUIRE(nb_int == 11);
|
||||
|
||||
if (nb_int > nb_worker_threads_default) {
|
||||
throw std::runtime_error(
|
||||
"number_of_worker_threads is greater than the maximal possible "
|
||||
"number of worker threads. Either s number_of_worker_threads "
|
||||
"to 'default' or set it to a valu smaller or equal than " +
|
||||
std::to_string(nb_worker_threads_default) + " and bigger than 0.");
|
||||
} else if (nb_int <= 0) {
|
||||
throw std::runtime_error(
|
||||
"number_of_worker_threads is greater than or equal 0. Either "
|
||||
" set number_of_worker_threads "
|
||||
" to 'default' or set it to a value less or equal than " +
|
||||
std::to_string(nb_worker_threads_default) + " and greater than 0.");
|
||||
}
|
||||
|
||||
nb_worker_threads = nb_int;
|
||||
}
|
||||
|
||||
REQUIRE(nb_worker_threads <= nb_worker_threads_default);
|
||||
REQUIRE(nb_worker_threads_default > 0);
|
||||
REQUIRE(nb_worker_threads_default == 11);
|
||||
*/
|
||||
}
|
||||
292
test/PacketDissection/PacketContainer_test.cpp
Normal file
292
test/PacketDissection/PacketContainer_test.cpp
Normal file
@@ -0,0 +1,292 @@
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <rte_mbuf.h>
|
||||
|
||||
#include "Definitions.hpp"
|
||||
#include "PacketDissection/PacketContainer.hpp"
|
||||
#include "PacketDissection/PacketInfo.hpp"
|
||||
#include "PacketDissection/PacketInfoCreator.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Icmp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Tcp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Udp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv6Icmp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv6Tcp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv6Udp.hpp"
|
||||
|
||||
TEST_CASE("PacketContainer", "[]") {
|
||||
// === I N I T === //
|
||||
uint16_t inside_port = 0;
|
||||
uint16_t outside_port = 1;
|
||||
struct rte_mempool mbuf_pool_struct;
|
||||
struct rte_mempool* mbuf_pool = &mbuf_pool_struct;
|
||||
CHECK(mbuf_pool != nullptr);
|
||||
|
||||
PacketContainer* pkt_container =
|
||||
new PacketContainer(mbuf_pool, inside_port, outside_port, 0, 0);
|
||||
CHECK(pkt_container != nullptr);
|
||||
|
||||
// === S E C T I O N S == //
|
||||
SECTION("get_empty_packet", "[]") {
|
||||
|
||||
SECTION("default", "[]") {
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 0);
|
||||
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_info != nullptr);
|
||||
CHECK(pkt_info->get_mbuf() != nullptr);
|
||||
CHECK(pkt_info->get_type() == IPv4TCP);
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
}
|
||||
|
||||
SECTION("IPv4TCP", "[]") {
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 0);
|
||||
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet(IPv4TCP);
|
||||
CHECK(pkt_info != nullptr);
|
||||
CHECK(pkt_info->get_mbuf() != nullptr);
|
||||
CHECK(pkt_info->get_type() == IPv4TCP);
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("create more packets than burst size", "[]") {
|
||||
|
||||
SECTION("fill till BURST_SIZE", "[]") {
|
||||
for (int i = 0; i < BURST_SIZE; ++i) {
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_info != nullptr);
|
||||
}
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() == BURST_SIZE);
|
||||
}
|
||||
|
||||
SECTION("fill till BURST_SIZE + 1", "[]") {
|
||||
for (int i = 0; i < BURST_SIZE + 1; ++i) {
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_info != nullptr);
|
||||
}
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() ==
|
||||
BURST_SIZE + 1);
|
||||
}
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
}
|
||||
|
||||
SECTION("get_packet_at_index", "[]") {
|
||||
|
||||
SECTION("general", "[]") {
|
||||
// add empty packet
|
||||
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
|
||||
PacketInfo* pkt_info_1 = pkt_container->get_packet_at_index(
|
||||
pkt_container->get_total_number_of_packets() - 1);
|
||||
CHECK(pkt_info_0 == pkt_info_1);
|
||||
CHECK(pkt_info_1 != nullptr);
|
||||
CHECK(pkt_info_1->get_mbuf() != nullptr);
|
||||
CHECK(pkt_info_1->get_type() == IPv4TCP);
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK_NOTHROW(pkt_container->get_packet_at_index(
|
||||
pkt_container->get_total_number_of_packets() - 1));
|
||||
CHECK_THROWS(pkt_container->get_packet_at_index(
|
||||
pkt_container->get_total_number_of_packets()));
|
||||
}
|
||||
|
||||
SECTION("test out of bounds error", "[]") {
|
||||
for (int i = 0; i < int(BURST_SIZE / 2); ++i) {
|
||||
pkt_container->get_empty_packet();
|
||||
}
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() ==
|
||||
int(BURST_SIZE / 2));
|
||||
|
||||
for (int i = 0; i < int(BURST_SIZE / 2); ++i) {
|
||||
CHECK_NOTHROW(pkt_container->get_packet_at_index(i));
|
||||
}
|
||||
|
||||
for (int i = int(BURST_SIZE / 2); i < BURST_SIZE; ++i) {
|
||||
CHECK_THROWS(pkt_container->get_packet_at_index(i));
|
||||
}
|
||||
|
||||
CHECK_THROWS(pkt_container->get_packet_at_index(
|
||||
pkt_container->get_total_number_of_packets()));
|
||||
CHECK_NOTHROW(pkt_container->get_packet_at_index(
|
||||
pkt_container->get_total_number_of_packets() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("take_packet and add_packet", "[]") {
|
||||
|
||||
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
|
||||
PacketInfo* pkt_info_1 = pkt_container->take_packet(0);
|
||||
CHECK(pkt_info_0 == pkt_info_1);
|
||||
CHECK(pkt_info_1 != nullptr);
|
||||
CHECK(pkt_info_1->get_mbuf() != nullptr);
|
||||
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
|
||||
pkt_container->add_packet(pkt_info_1);
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 2);
|
||||
CHECK(pkt_container->get_packet_at_index(1) == pkt_info_1);
|
||||
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
|
||||
}
|
||||
|
||||
SECTION("drop_packet", "[]") {
|
||||
|
||||
SECTION("default") {
|
||||
PacketInfo* pkt_info_0 = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
|
||||
pkt_container->drop_packet(0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() >=
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
|
||||
|
||||
CHECK_NOTHROW(pkt_container->drop_packet(0));
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
CHECK(pkt_container->get_packet_at_index(0) == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("poll_packets", "[]") {
|
||||
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 0);
|
||||
|
||||
uint16_t nb_pkts_polled;
|
||||
pkt_container->poll_packets(nb_pkts_polled);
|
||||
CHECK(pkt_container->get_number_of_polled_packets() > 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() ==
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(nb_pkts_polled == pkt_container->get_number_of_polled_packets());
|
||||
|
||||
CHECK_NOTHROW(pkt_container->get_packet_at_index(nb_pkts_polled - 1));
|
||||
PacketInfo* pkt_info =
|
||||
pkt_container->get_packet_at_index(nb_pkts_polled - 1);
|
||||
CHECK(pkt_info != nullptr);
|
||||
CHECK(pkt_info->get_mbuf() != nullptr);
|
||||
}
|
||||
|
||||
SECTION("send_packets", "[]") {
|
||||
|
||||
SECTION("do not drop") {
|
||||
SECTION("send created packets") {
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 1);
|
||||
}
|
||||
|
||||
SECTION("send polled packets") {
|
||||
uint16_t nb_polled_pkts;
|
||||
pkt_container->poll_packets(nb_polled_pkts);
|
||||
|
||||
CHECK(nb_polled_pkts > 0);
|
||||
}
|
||||
|
||||
SECTION("send polled and created packets") {
|
||||
uint16_t nb_polled_pkts;
|
||||
pkt_container->poll_packets(nb_polled_pkts);
|
||||
CHECK(nb_polled_pkts > 0);
|
||||
|
||||
PacketInfo* pkt_info = pkt_container->get_empty_packet();
|
||||
CHECK(pkt_container->get_total_number_of_packets() >
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
}
|
||||
|
||||
pkt_container->send_packets();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 0);
|
||||
}
|
||||
|
||||
SECTION("drop and send created packets") {
|
||||
|
||||
const int PKTS_TO_POLL = 4;
|
||||
int nb_pkts_to_drop = -1;
|
||||
|
||||
for (int i = 0; i < PKTS_TO_POLL; ++i) {
|
||||
pkt_container->get_empty_packet();
|
||||
}
|
||||
|
||||
CHECK(pkt_container->get_total_number_of_packets() >
|
||||
pkt_container->get_number_of_polled_packets());
|
||||
CHECK(pkt_container->get_total_number_of_packets() == PKTS_TO_POLL);
|
||||
|
||||
SECTION("drop first packet") {
|
||||
nb_pkts_to_drop = 1;
|
||||
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
|
||||
PKTS_TO_POLL);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
|
||||
pkt_container->drop_packet(0);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
|
||||
}
|
||||
|
||||
SECTION("drop second packet") {
|
||||
nb_pkts_to_drop = 1;
|
||||
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
|
||||
PKTS_TO_POLL);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
|
||||
pkt_container->drop_packet(1);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
|
||||
}
|
||||
|
||||
SECTION("drop second and third packet") {
|
||||
nb_pkts_to_drop = 2;
|
||||
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
|
||||
PKTS_TO_POLL);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
|
||||
pkt_container->drop_packet(1);
|
||||
pkt_container->drop_packet(2);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 2);
|
||||
}
|
||||
|
||||
SECTION("drop last packet") {
|
||||
nb_pkts_to_drop = 1;
|
||||
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
|
||||
PKTS_TO_POLL);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 0);
|
||||
pkt_container->drop_packet(PKTS_TO_POLL - 1);
|
||||
CHECK(pkt_container->get_nb_pkts_dropped() == 1);
|
||||
}
|
||||
|
||||
pkt_container->reorder_mbuf_arrays();
|
||||
CHECK(pkt_container->get_nb_mbufs_in_mbuf_arr()[0] ==
|
||||
PKTS_TO_POLL - nb_pkts_to_drop);
|
||||
|
||||
pkt_container->send_packets();
|
||||
CHECK(pkt_container->get_number_of_polled_packets() == 0);
|
||||
CHECK(pkt_container->get_total_number_of_packets() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
// === D E L E T E O B J E C T S === //
|
||||
delete pkt_container;
|
||||
pkt_container = nullptr;
|
||||
}
|
||||
132
test/PacketDissection/PacketInfo_test.cpp
Normal file
132
test/PacketDissection/PacketInfo_test.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "ConfigurationManagement/Configurator.hpp"
|
||||
#include "Initializer.hpp"
|
||||
#include "PacketDissection/PacketInfo.hpp"
|
||||
#include "PacketDissection/PacketInfoCreator.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Icmp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Tcp.hpp"
|
||||
#include "PacketDissection/PacketInfoIpv4Udp.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
#include <iostream>
|
||||
|
||||
TEST_CASE("Creation","[]")
|
||||
{
|
||||
|
||||
SECTION("PacketInfo","[]"){
|
||||
PacketInfo pkt_inf;
|
||||
rte_mbuf* mbuf;
|
||||
//REQUIRE_NOTHROW(pkt_inf.set_mbuf(mbuf));
|
||||
//REQUIRE_NOTHROW(nbuf = pkt_inf.get_mbuf());
|
||||
//CHECK(mbuf == nbuf);
|
||||
CHECK(pkt_inf.get_type() == NONE);
|
||||
}
|
||||
|
||||
SECTION("Creation: IPv4ICMP", "[]") {
|
||||
// PacketInfoIpv4Icmp* pkt_inf =
|
||||
// static_cast<PacketInfoIpv4Icmp*>(PacketInfoCreator::create_pkt_info(IPv4ICMP));
|
||||
PacketInfoIpv4Icmp pkt_inf;
|
||||
CHECK(pkt_inf.get_type() == IPv4ICMP);
|
||||
// pkt_inf->set_mbuf(mbuf);
|
||||
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
|
||||
// struct rte_icmp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_icmp_hdr*, 20);
|
||||
// REQUIRE_NOTHROW(pkt_inf->set_icmp_hdr(l4_header)); uint32_t num;
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
|
||||
}
|
||||
|
||||
SECTION("Creation: IPv4TCP", "[]") {
|
||||
// PacketInfoIpv4Tcp* pkt_inf =
|
||||
// static_cast<PacketInfoIpv4Tcp*>(PacketInfoCreator::create_pkt_info(IPv4TCP));
|
||||
PacketInfoIpv4Tcp pkt_inf;
|
||||
CHECK(pkt_inf.get_type() == IPv4TCP);
|
||||
// pkt_inf->set_mbuf(mbuf);
|
||||
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
|
||||
// struct rte_tcp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_tcp_hdr*, 20); REQUIRE_NOTHROW(pkt_inf->set_tcp_hdr(l4_header));
|
||||
// uint32_t num;
|
||||
// uint32_t num2;
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_dst_port());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_src_port());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_flags());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_window_size());
|
||||
// CHECK_NOTHROW(pkt_inf->set_ack_num(num));
|
||||
// CHECK_NOTHROW(num2= pkt_inf->get_ack_num());
|
||||
// CHECK(num == num2);
|
||||
// CHECK_NOTHROW(pkt_inf->set_seq_num(num));
|
||||
// CHECK_NOTHROW(num = pkt_inf->get_seq_num());
|
||||
// CHECK(num == num2);
|
||||
}
|
||||
|
||||
SECTION("Creation: IPv4UDP", "[]") {
|
||||
// PacketInfoIpv4Udp* pkt_inf =
|
||||
// static_cast<PacketInfoIpv4Udp*>(PacketInfoCreator::create_pkt_info(IPv4UDP));
|
||||
PacketInfoIpv4Udp pkt_inf;
|
||||
CHECK(pkt_inf.get_type() == IPv4UDP);
|
||||
// pkt_inf->set_mbuf(mbuf);
|
||||
// struct rte_ipv4_hdr* ip_hdr = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_ipv4_hdr*, 0); REQUIRE_NOTHROW(pkt_inf->set_ip_hdr(ip_hdr));
|
||||
// struct rte_udp_hdr* l4_header = rte_pktmbuf_mtod_offset(mbuf, struct
|
||||
// rte_udp_hdr*, 20); REQUIRE_NOTHROW(pkt_inf->set_udp_hdr(l4_header));
|
||||
// uint32_t num;
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_dst_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_src_ip());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_packet_size());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_payload_size());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_dst_port());
|
||||
// CHECK_NOTHROW(num= pkt_inf->get_src_port());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Transformation", "[]") {
|
||||
|
||||
SECTION("keeping Type", "[]") {
|
||||
PacketInfo* pkt_inf;
|
||||
// pkt_inf = PacketInfoCreator::create_pkt_info(IPv4ICMP);
|
||||
pkt_inf = new PacketInfoIpv4Icmp;
|
||||
PacketInfoIpv4Icmp* pkt_inf_icmp;
|
||||
pkt_inf_icmp = static_cast<PacketInfoIpv4Icmp*>(pkt_inf);
|
||||
CHECK(pkt_inf_icmp->get_type() == IPv4ICMP);
|
||||
|
||||
// PacketInfoCreator::create_pkt_info(IPv4TCP)
|
||||
pkt_inf = new PacketInfoIpv4Tcp;
|
||||
PacketInfoIpv4Tcp* pkt_inf_tcp;
|
||||
pkt_inf_tcp = static_cast<PacketInfoIpv4Tcp*>(pkt_inf);
|
||||
CHECK(pkt_inf_tcp->get_type() == IPv4TCP);
|
||||
|
||||
// PacketInfoCreator::create_pkt_info(IPv4UDP)
|
||||
pkt_inf = new PacketInfoIpv4Udp;
|
||||
PacketInfoIpv4Udp* pkt_inf_udp;
|
||||
pkt_inf_udp = static_cast<PacketInfoIpv4Udp*>(pkt_inf);
|
||||
CHECK(pkt_inf_udp->get_type() == IPv4UDP);
|
||||
|
||||
// PacketInfoCreator::create_pkt_info(NONE)
|
||||
pkt_inf = new PacketInfo;
|
||||
CHECK(pkt_inf->get_type() == NONE);
|
||||
|
||||
PacketInfo* pkt_inf_arr[5];
|
||||
pkt_inf_arr[0] = pkt_inf_icmp;
|
||||
pkt_inf_arr[1] = pkt_inf_tcp;
|
||||
pkt_inf_arr[2] = pkt_inf_udp;
|
||||
pkt_inf_arr[3] = pkt_inf;
|
||||
CHECK(pkt_inf_arr[0]->get_type() == IPv4ICMP);
|
||||
CHECK(pkt_inf_arr[1]->get_type() == IPv4TCP);
|
||||
CHECK(pkt_inf_arr[2]->get_type() == IPv4UDP);
|
||||
CHECK(pkt_inf_arr[3]->get_type() == NONE);
|
||||
|
||||
// clean up
|
||||
delete pkt_inf;
|
||||
delete pkt_inf_icmp;
|
||||
delete pkt_inf_tcp;
|
||||
delete pkt_inf_udp;
|
||||
delete pkt_inf_arr;
|
||||
}
|
||||
}
|
||||
|
||||
456
test/RandomNumberGenerator/RandomNumberGenerator_test.cpp
Normal file
456
test/RandomNumberGenerator/RandomNumberGenerator_test.cpp
Normal file
@@ -0,0 +1,456 @@
|
||||
#include "RandomNumberGenerator.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
#include <iostream>
|
||||
#include <time.h> // is used for testing the time in the 3rd test case
|
||||
|
||||
TEST_CASE("random_number_generator_basic", "[]") {
|
||||
|
||||
// This test was written to check basic functions like whether different
|
||||
// numbers are generated.
|
||||
SECTION("check_whether_different_16", "[]") {
|
||||
// creates a new RNG object
|
||||
RandomNumberGenerator* xor_shift = new RandomNumberGenerator();
|
||||
|
||||
// creates two pseudo random 16 bit numbers
|
||||
u_int16_t test_1 = xor_shift->gen_rdm_16_bit();
|
||||
u_int16_t test_2 = xor_shift->gen_rdm_16_bit();
|
||||
|
||||
// printes these numbers out
|
||||
std::cout << "1st generated 16 bit int: " << test_1 << std::endl;
|
||||
std::cout << "2nd generated 16 bit int: " << test_2 << std::endl;
|
||||
|
||||
// checks whether these numbers are different
|
||||
// test1 == test2 wouldn't means that the test isn't random.
|
||||
// This section just exists to see whether the algorithm basically works
|
||||
// and generates numbers.
|
||||
CHECK(test_1 != test_2);
|
||||
}
|
||||
|
||||
// The same test like above but for the 32 bit algorithm:
|
||||
SECTION("CheckWhetherDifferent32", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int32_t test_1 = xor_shift.gen_rdm_32_bit();
|
||||
u_int32_t test_2 = xor_shift.gen_rdm_32_bit();
|
||||
std::cout << "1st generated 32 bit int: " << test_1 << std::endl;
|
||||
std::cout << "2nd generated 32 bit int: " << test_2 << std::endl;
|
||||
CHECK(test_1 != test_2);
|
||||
}
|
||||
|
||||
// The same test like above but for the 64 bit algorithm:
|
||||
SECTION("CheckWhetherDifferent64", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int64_t test_1 = xor_shift.gen_rdm_64_bit();
|
||||
u_int64_t test_2 = xor_shift.gen_rdm_64_bit();
|
||||
std::cout << "1st generated 64 bit int: " << test_1 << std::endl;
|
||||
std::cout << "2nd generated 64 bit int: " << test_2 << std::endl;
|
||||
CHECK(test_1 != test_2);
|
||||
// empty line for better layout in testlog
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// This test checks the type of the return value.
|
||||
// This particular section is for 16 bit
|
||||
SECTION("CheckSize16", "[]") {
|
||||
// Creates a RNG and generates a number like above
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int16_t test_value = xor_shift.gen_rdm_16_bit();
|
||||
std::cout << "values that are generated for checking the size: "
|
||||
<< std::endl;
|
||||
std::cout << "generated value: " << test_value << std::endl;
|
||||
// checks wheter the size of the return value is 16 bit or 2 byte
|
||||
CHECK(sizeof(test_value) == 2);
|
||||
}
|
||||
|
||||
// The same for 32 bit
|
||||
SECTION("CheckSize32", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int32_t test_value = xor_shift.gen_rdm_32_bit();
|
||||
std::cout << "generated value (32 bit): " << test_value << std::endl;
|
||||
// checks wheter the size of the return value is 32 bit or 4 byte
|
||||
CHECK(sizeof(test_value) == 4);
|
||||
}
|
||||
|
||||
// The same for 64 bit
|
||||
SECTION("CheckSize64", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int64_t test_value = xor_shift.gen_rdm_64_bit();
|
||||
std::cout << "generated value (64 bit): " << test_value << std::endl;
|
||||
// checks wheter the size of the return value is 64 bit or 8 byte
|
||||
CHECK(sizeof(test_value) == 8);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// Checks whether different numbers are generated when using different RNGs
|
||||
SECTION(
|
||||
"Check whether different when using different RNG objects for 16 bit",
|
||||
"[]") {
|
||||
// creating two objects
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
// generating two values of 16 bit
|
||||
std::cout << "16 bit seed 1: " << xor_shift_1._seed_x16 << std::endl;
|
||||
std::cout << "16 bit seed 2: " << xor_shift_2._seed_x16 << std::endl;
|
||||
u_int16_t test_1_16_bit = xor_shift_1.gen_rdm_16_bit();
|
||||
u_int16_t test_2_16_bit = xor_shift_2.gen_rdm_16_bit();
|
||||
CHECK(test_1_16_bit != test_2_16_bit);
|
||||
}
|
||||
|
||||
// the same for 32 bit again:
|
||||
SECTION(
|
||||
"Check whether different when using different RNG objects for 32 bit",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
std::cout << "32 bit seed 1: " << xor_shift_1._seed_x32 << std::endl;
|
||||
std::cout << "32 bit seed 2: " << xor_shift_2._seed_x32 << std::endl;
|
||||
u_int32_t test_1_32_bit = xor_shift_1.gen_rdm_32_bit();
|
||||
u_int32_t test_2_32_bit = xor_shift_2.gen_rdm_32_bit();
|
||||
CHECK(test_1_32_bit != test_2_32_bit);
|
||||
}
|
||||
|
||||
// the same for 64 bit again:
|
||||
SECTION(
|
||||
"Check whether different when using different RNG objects for 64 bit",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
std::cout << "64 bit seed 1: " << xor_shift_1._seed_x64 << std::endl;
|
||||
std::cout << "64 bit seed 2: " << xor_shift_2._seed_x64 << std::endl;
|
||||
u_int64_t test_1_64_bit = xor_shift_1.gen_rdm_64_bit();
|
||||
u_int64_t test_2_64_bit = xor_shift_2.gen_rdm_64_bit();
|
||||
CHECK(test_1_64_bit != test_2_64_bit);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// This test checks whether two RNGs generate the same number after the seed
|
||||
// is set to the same number
|
||||
SECTION("Check whether the same numbers are generated with the same seed "
|
||||
"for 16 bit",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
// set the seed to the same value in both RNGs
|
||||
xor_shift_1._seed_x16 = 30000;
|
||||
xor_shift_2._seed_x16 = 30000;
|
||||
std::cout << "16 bit seed for RNG 1: " << xor_shift_1._seed_x16
|
||||
<< std::endl;
|
||||
std::cout << "16 bit seed for RNG 2: " << xor_shift_2._seed_x16
|
||||
<< std::endl;
|
||||
u_int16_t test_1_16_bit = xor_shift_1.gen_rdm_16_bit();
|
||||
u_int16_t test_2_16_bit = xor_shift_2.gen_rdm_16_bit();
|
||||
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x16
|
||||
<< std::endl;
|
||||
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x16
|
||||
<< std::endl;
|
||||
// check whether the results are the same too
|
||||
CHECK(test_1_16_bit == test_2_16_bit);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// the same test for 32 bit
|
||||
SECTION("Check whether the same numbers are generated with the same seed "
|
||||
"for 32 bit",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
// set the seed to the same value in both RNGs
|
||||
xor_shift_1._seed_x32 = 30000000;
|
||||
xor_shift_2._seed_x32 = 30000000;
|
||||
std::cout << "32 bit seed for RNG 1: " << xor_shift_1._seed_x32
|
||||
<< std::endl;
|
||||
std::cout << "32 bit seed for RNG 2: " << xor_shift_2._seed_x32
|
||||
<< std::endl;
|
||||
u_int32_t test_1_32_bit = xor_shift_1.gen_rdm_32_bit();
|
||||
u_int32_t test_2_32_bit = xor_shift_2.gen_rdm_32_bit();
|
||||
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x32
|
||||
<< std::endl;
|
||||
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x32
|
||||
<< std::endl;
|
||||
// check whether the results are the same too
|
||||
CHECK(test_1_32_bit == test_2_32_bit);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// the same test for 64 bit
|
||||
SECTION("Check whether the same numbers are generated with the same seed "
|
||||
"for 64 bit",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift_1;
|
||||
RandomNumberGenerator xor_shift_2;
|
||||
// set the seed to the same value in both RNGs
|
||||
xor_shift_1._seed_x64 = 30000000000;
|
||||
xor_shift_2._seed_x64 = 30000000000;
|
||||
std::cout << "64 bit seed for RNG 1: " << xor_shift_1._seed_x64
|
||||
<< std::endl;
|
||||
std::cout << "64 bit seed for RNG 2: " << xor_shift_2._seed_x64
|
||||
<< std::endl;
|
||||
u_int64_t test_1_64_bit = xor_shift_1.gen_rdm_64_bit();
|
||||
u_int64_t test_2_64_bit = xor_shift_2.gen_rdm_64_bit();
|
||||
std::cout << "number generated from RNG 1: " << xor_shift_1._seed_x64
|
||||
<< std::endl;
|
||||
std::cout << "number generated from RNG 2: " << xor_shift_2._seed_x64
|
||||
<< std::endl;
|
||||
// check whether the results are the same too
|
||||
CHECK(test_1_64_bit == test_2_64_bit);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
SECTION("Check whether generated numbers are really in the interval",
|
||||
"[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int16_t test_value;
|
||||
int lower_limit = 1024;
|
||||
int upper_limit = 49151;
|
||||
bool no_number_has_been_outside_the_interval = true;
|
||||
// inside the for loop an if statement checks for 1,000,000 generated
|
||||
// numbers whether they are really in the interval
|
||||
for (int i = 0; i < 10000000; i++) {
|
||||
test_value =
|
||||
xor_shift.gen_rdm_16_bit_in_interval(lower_limit, upper_limit);
|
||||
if (test_value < lower_limit || test_value > upper_limit) {
|
||||
no_number_has_been_outside_the_interval = false;
|
||||
}
|
||||
}
|
||||
std::cout << "No generated number has been outside the interval? (1 "
|
||||
"means true) --> "
|
||||
<< no_number_has_been_outside_the_interval << std::endl;
|
||||
std::cout << std::endl;
|
||||
CHECK(no_number_has_been_outside_the_interval == true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("RandomNumberGeneratorStatistics", "[]") {
|
||||
|
||||
// The result of the chi square test shows how uniform the generated numbers
|
||||
// are distributed
|
||||
// A big chi square means that the actual frequencies vary widely from the
|
||||
// theoretical frequencies.
|
||||
SECTION("ChiSquare16", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
// 65536 - 1 = 2 ^ 16 different numbers can be generated
|
||||
int r = 65536 - 1;
|
||||
// 1,000,000 numbers are generated
|
||||
int n = 1000000;
|
||||
u_int16_t t;
|
||||
// this array counts how often each number from 0 to r is returned as a
|
||||
// result
|
||||
int f[r] = {};
|
||||
for (int i = 0; i < r; i++) {
|
||||
f[i] = 0;
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
t = xor_shift.gen_rdm_16_bit_in_interval(1024, 49151);
|
||||
f[t]++;
|
||||
}
|
||||
double chisquare = 0.0;
|
||||
for (int i = 0; i < r; i++) {
|
||||
// chi square is calculated
|
||||
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
|
||||
}
|
||||
std::cout << "+++ chi square test for gen_rdm_16_bit_in_interval() +++"
|
||||
<< std::endl;
|
||||
std::cout << "chi square is: " << chisquare << std::endl;
|
||||
double k = sqrt(chisquare / (n + chisquare));
|
||||
std::cout << "k is: " << k << std::endl;
|
||||
|
||||
// k is in [0; k_max] with k_max ≈ 1
|
||||
// Calculating k_norm wouldn't make sense.
|
||||
// 0 means that every number is generated equally frequent
|
||||
// 1 means not random at all
|
||||
|
||||
// A bad result could be improved by returning _seed_x16 instead of
|
||||
// _seed_x16 % 48128 + 1024 (valid port number) in
|
||||
// RandomNumberGenerator.cpp.
|
||||
|
||||
CHECK(k < 1.0);
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
SECTION("ChiSquare16", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
// 65526 - 1 = 2 ^ 16 different numbers can be generated
|
||||
int r = 65536 - 1;
|
||||
// 1,000,000 numbers are generated
|
||||
int n = 1000000;
|
||||
u_int16_t t;
|
||||
// this array counts how often each number from 0 to r is returned as a
|
||||
// result
|
||||
int f[r] = {};
|
||||
for (int i = 0; i < r; i++) {
|
||||
f[i] = 0;
|
||||
}
|
||||
for (int i = 1; i < n; i++) {
|
||||
t = xor_shift.gen_rdm_16_bit();
|
||||
f[t]++;
|
||||
}
|
||||
double chisquare = 0.0;
|
||||
for (int i = 0; i < r; i++) {
|
||||
// chi square is calculated
|
||||
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
|
||||
}
|
||||
std::cout << "+++ chi square test for gen_rdm_16_bit() +++"
|
||||
<< std::endl;
|
||||
std::cout << "chi square is: " << chisquare << std::endl;
|
||||
double k = sqrt(chisquare / (n + chisquare));
|
||||
std::cout << "k is: " << k << std::endl;
|
||||
|
||||
CHECK(k < 1.0);
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// the following test fails due to an segmentation violation signal
|
||||
// 32 bit seems to be to big
|
||||
|
||||
/*SECTION("ChiSquare32", "[]") {
|
||||
RandomNumberGenerator xor_shift;
|
||||
u_int32_t r = 4294967296 - 1;
|
||||
u_int64_t n = 10000000000;
|
||||
u_int32_t t;
|
||||
int f[r] = {};
|
||||
for (u_int64_t i = 0; i < r; i++) {
|
||||
f[i] = 0;
|
||||
}
|
||||
for (u_int64_t i = 1; i < n; i++) {
|
||||
t = xor_shift.gen_rdm_32_bit();
|
||||
f[t]++;
|
||||
}
|
||||
double chisquare = 0.0;
|
||||
for (int i = 0; i < r; i++) {
|
||||
chisquare = chisquare + ((f[i] - n / r) * (f[i] - n / r) / (n / r));
|
||||
}
|
||||
std::cout << "chi square is: " << chisquare << std::endl;
|
||||
double k = sqrt(chisquare / (n + chisquare));
|
||||
std::cout << "k is: " << k << std::endl;
|
||||
// k is in [0; k_max] with k_max ≈ 1
|
||||
// 0 means independence
|
||||
// 1 means not random at all
|
||||
CHECK(k < 1.0);
|
||||
}*/
|
||||
}
|
||||
|
||||
TEST_CASE("RandomNumberGeneratorTime", "[]") {
|
||||
|
||||
// The following section is calculating the time for generating n
|
||||
// 16 bit numbers with a single RNG object
|
||||
SECTION("TestTime16", "[]") {
|
||||
// the following two lines initialize and start the timer
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
// creating a RNG object
|
||||
RandomNumberGenerator xor_shift;
|
||||
// amount of numbers generated
|
||||
// can be changed if neccessary, but it you will get a segmentation
|
||||
// violation error if it's to big
|
||||
long n = 10000000;
|
||||
// variable to store generated number
|
||||
uint16_t test_value;
|
||||
// generating those numbers
|
||||
for (long i = 0; i < n; i++) {
|
||||
test_value = xor_shift.gen_rdm_16_bit();
|
||||
}
|
||||
// stops the timer and calculates the difference between start and end
|
||||
time1 += clock() - tstart;
|
||||
// prints out the time
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 16 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
|
||||
<< std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 10.0);
|
||||
}
|
||||
|
||||
// This test calculates the time needed to generate a certain amount of 16
|
||||
// bit ints with rand() allowowing a comparison with xorShift
|
||||
SECTION("TestTime16Rand", "[]") {
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
long n = 10000000;
|
||||
uint16_t test_value;
|
||||
for (long i = 0; i < n; i++) {
|
||||
test_value = rand();
|
||||
}
|
||||
time1 += clock() - tstart;
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 16 bit numbers with rand(): " << time1 / CLOCKS_PER_SEC
|
||||
<< " s" << std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 10.0);
|
||||
}
|
||||
|
||||
// the same test for 32 bit numbers
|
||||
SECTION("TestTime32", "[]") {
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
RandomNumberGenerator xor_shift;
|
||||
long n = 10000000;
|
||||
uint32_t test_value;
|
||||
for (long i = 0; i < n; i++) {
|
||||
test_value = xor_shift.gen_rdm_32_bit();
|
||||
}
|
||||
time1 += clock() - tstart;
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 32 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
|
||||
<< std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
|
||||
}
|
||||
|
||||
// true 32 bit numbers with shifting and rand() for comparison to the
|
||||
// section above
|
||||
SECTION("TestTime32Rand", "[]") {
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
long n = 10000000;
|
||||
uint32_t test_value;
|
||||
for (long i = 0; i < n; i++) {
|
||||
test_value = (uint16_t)rand();
|
||||
test_value |= (uint16_t)rand() << 16;
|
||||
}
|
||||
time1 += clock() - tstart;
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 32 bit numbers with rand() and shifting: "
|
||||
<< time1 / CLOCKS_PER_SEC << " s" << std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
|
||||
}
|
||||
|
||||
// the same test for 64 bit numbers
|
||||
SECTION("TestTime64", "[]") {
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
RandomNumberGenerator xor_shift;
|
||||
long n = 10000000;
|
||||
uint64_t test_value;
|
||||
for (long i = 0; i < n; i++) {
|
||||
test_value = xor_shift.gen_rdm_64_bit();
|
||||
}
|
||||
time1 += clock() - tstart;
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 64 bit numbers: " << time1 / CLOCKS_PER_SEC << " s"
|
||||
<< std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
|
||||
}
|
||||
|
||||
// true 64 bit numbers with shifting and rand() for comparison to the
|
||||
// section above
|
||||
SECTION("TestTime64Rand", "[]") {
|
||||
double time1 = 0.0, tstart;
|
||||
tstart = clock();
|
||||
long n = 10000000;
|
||||
uint64_t test_value = 0;
|
||||
for (long i = 0; i < n; i++) {
|
||||
// the following lines have been copied from
|
||||
// Treatment::create_cookie_secret()
|
||||
u_int64_t value1 = (uint16_t)rand();
|
||||
value1 = (value1 << 48);
|
||||
test_value |= value1;
|
||||
u_int64_t value2 = (uint16_t)rand();
|
||||
value2 = (value2 << 32);
|
||||
test_value |= value2;
|
||||
u_int64_t value3 = (uint16_t)rand();
|
||||
test_value |= value3;
|
||||
}
|
||||
time1 += clock() - tstart;
|
||||
std::cout << "time needed to generate " << n
|
||||
<< " 64 bit numbers with rand() and shifting: "
|
||||
<< time1 / CLOCKS_PER_SEC << " s" << std::endl;
|
||||
CHECK(time1 / CLOCKS_PER_SEC < 1.0);
|
||||
}
|
||||
}
|
||||
1302
test/Treatment/Treatment_test.cpp
Normal file
1302
test/Treatment/Treatment_test.cpp
Normal file
File diff suppressed because it is too large
Load Diff
6
test/config_attacker.json
Normal file
6
test/config_attacker.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"attack_type": "syn_flood",
|
||||
"number_of_worker_threads": "4",
|
||||
"number_of_attack_packets_per_thread_per_send_call": 100,
|
||||
"call_send_pkts_every_nth_iteration": 1
|
||||
}
|
||||
32
test/libdpdk_dummy/include/rte_branch_prediction.h
Normal file
32
test/libdpdk_dummy/include/rte_branch_prediction.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef _RTE_BRANCH_PREDICTION_H_
|
||||
#define _RTE_BRANCH_PREDICTION_H_
|
||||
|
||||
/**
|
||||
* Check if a branch is likely to be taken.
|
||||
*
|
||||
* This compiler builtin allows the developer to indicate if a branch is
|
||||
* likely to be taken. Example:
|
||||
*
|
||||
* if (likely(x > 1))
|
||||
* do_stuff();
|
||||
*
|
||||
*/
|
||||
#ifndef likely
|
||||
#define likely(x) __builtin_expect(!!(x), 1)
|
||||
#endif /* likely */
|
||||
|
||||
/**
|
||||
* Check if a branch is unlikely to be taken.
|
||||
*
|
||||
* This compiler builtin allows the developer to indicate if a branch is
|
||||
* unlikely to be taken. Example:
|
||||
*
|
||||
* if (unlikely(x < 1))
|
||||
* do_stuff();
|
||||
*
|
||||
*/
|
||||
#ifndef unlikely
|
||||
#define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
#endif /* unlikely */
|
||||
|
||||
#endif /* _RTE_BRANCH_PREDICTION_H_ */
|
||||
62
test/libdpdk_dummy/include/rte_byteorder.h
Normal file
62
test/libdpdk_dummy/include/rte_byteorder.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define rte_cpu_to_be_16(x) rte_bswap16(x)
|
||||
#define rte_be_to_cpu_16(x) rte_bswap16(x)
|
||||
|
||||
#define rte_cpu_to_be_32(x) rte_bswap32(x)
|
||||
#define rte_be_to_cpu_32(x) rte_bswap32(x)
|
||||
|
||||
#define rte_bswap16(x) \
|
||||
((uint16_t)(__builtin_constant_p(x) ? rte_constant_bswap16(x) \
|
||||
: rte_arch_bswap16(x)))
|
||||
|
||||
#define rte_bswap32(x) \
|
||||
((uint32_t)(__builtin_constant_p(x) ? rte_constant_bswap32(x) \
|
||||
: rte_arch_bswap32(x)))
|
||||
|
||||
typedef uint16_t rte_be16_t;
|
||||
|
||||
typedef uint32_t rte_be32_t;
|
||||
|
||||
/**
|
||||
* An internal function to swap bytes in a
|
||||
* 16-bit value.
|
||||
*
|
||||
* It is used by rte_bswap16() when the
|
||||
* value is constant. Do not use this
|
||||
* function directly; rte_bswap16() is
|
||||
* preferred.
|
||||
*/
|
||||
static inline uint16_t rte_constant_bswap16(uint16_t x) {
|
||||
return (uint16_t)(((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8));
|
||||
}
|
||||
|
||||
/**
|
||||
* An internal function to swap bytes in a 32-bit value.
|
||||
*
|
||||
* It is used by rte_bswap32() when the value is constant. Do not use
|
||||
* this function directly; rte_bswap32() is preferred.
|
||||
*/
|
||||
static inline uint32_t rte_constant_bswap32(uint32_t x) {
|
||||
return ((x & 0x000000ffUL) << 24) | ((x & 0x0000ff00UL) << 8) |
|
||||
((x & 0x00ff0000UL) >> 8) | ((x & 0xff000000UL) >> 24);
|
||||
}
|
||||
|
||||
static inline uint16_t rte_arch_bswap16(uint16_t _x) {
|
||||
uint16_t x = _x;
|
||||
asm volatile("xchgb %b[x1],%h[x2]" : [ x1 ] "=Q"(x) : [ x2 ] "0"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
/*
|
||||
* An architecture-optimized byte swap for a 32-bit value.
|
||||
*
|
||||
* Do not use this function directly. The preferred function is rte_bswap32().
|
||||
*/
|
||||
static inline uint32_t rte_arch_bswap32(uint32_t _x) {
|
||||
uint32_t x = _x;
|
||||
asm volatile("bswap %[x]" : [ x ] "+r"(x));
|
||||
return x;
|
||||
}
|
||||
14
test/libdpdk_dummy/include/rte_common.h
Normal file
14
test/libdpdk_dummy/include/rte_common.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#define RTE_MIN(a, b) \
|
||||
__extension__({ \
|
||||
typeof(a) _a = (a); \
|
||||
typeof(b) _b = (b); \
|
||||
_a < _b ? _a : _b; \
|
||||
})
|
||||
|
||||
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
|
||||
#define RTE_STD_C11 __extension__
|
||||
#else
|
||||
#define RTE_STD_C11
|
||||
#endif
|
||||
3
test/libdpdk_dummy/include/rte_config.h
Normal file
3
test/libdpdk_dummy/include/rte_config.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define RTE_PKTMBUF_HEADROOM 128
|
||||
14
test/libdpdk_dummy/include/rte_cycles.h
Normal file
14
test/libdpdk_dummy/include/rte_cycles.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint64_t rte_get_tsc_cycles() {
|
||||
unsigned int lo, hi;
|
||||
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
|
||||
return ((uint64_t)hi << 32) | lo;
|
||||
}
|
||||
|
||||
uint64_t rte_get_tsc_hz() {
|
||||
// 1.8 GHz
|
||||
return 1800000000;
|
||||
}
|
||||
1
test/libdpdk_dummy/include/rte_eal.h
Normal file
1
test/libdpdk_dummy/include/rte_eal.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
44
test/libdpdk_dummy/include/rte_ethdev.h
Normal file
44
test/libdpdk_dummy/include/rte_ethdev.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_mbuf.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct rte_eth_conf {
|
||||
/*
|
||||
uint32_t link_speeds;
|
||||
struct rte_eth_rxmode rxmode;
|
||||
struct rte_eth_txmode txmode;
|
||||
uint32_t lpbk_mode;
|
||||
struct {
|
||||
struct rte_eth_rss_conf rss_conf;
|
||||
struct rte_eth_vmdq_dcb_conf vmdq_dcb_conf;
|
||||
struct rte_eth_dcb_rx_conf dcb_rx_conf;
|
||||
struct rte_eth_vmdq_rx_conf vmdq_rx_conf;
|
||||
} rx_adv_conf;
|
||||
union {
|
||||
struct rte_eth_vmdq_dcb_tx_conf vmdq_dcb_tx_conf;
|
||||
struct rte_eth_dcb_tx_conf dcb_tx_conf;
|
||||
struct rte_eth_vmdq_tx_conf vmdq_tx_conf;
|
||||
} tx_adv_conf;
|
||||
uint32_t dcb_capability_en;
|
||||
struct rte_fdir_conf fdir_conf;
|
||||
struct rte_intr_conf intr_conf;
|
||||
*/
|
||||
};
|
||||
|
||||
static inline uint16_t rte_eth_tx_burst(uint16_t port_id, uint16_t queue_id,
|
||||
struct rte_mbuf** tx_pkts,
|
||||
uint16_t nb_pkts) {
|
||||
for (int i = 0; i < nb_pkts; ++i) {
|
||||
rte_pktmbuf_free(tx_pkts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id,
|
||||
struct rte_mbuf** rx_pkts,
|
||||
const uint16_t nb_pkts) {
|
||||
rte_mempool* mp;
|
||||
for (int i = 0; i < nb_pkts; ++i) {
|
||||
rx_pkts[i] = rte_pktmbuf_alloc(mp);
|
||||
}
|
||||
}
|
||||
22
test/libdpdk_dummy/include/rte_ether.h
Normal file
22
test/libdpdk_dummy/include/rte_ether.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_byteorder.h>
|
||||
#include <rte_common.h>
|
||||
|
||||
#define RTE_ETHER_ADDR_LEN 6
|
||||
|
||||
struct rte_ether_addr {
|
||||
uint8_t addr_bytes[RTE_ETHER_ADDR_LEN];
|
||||
} __attribute__((__packed__));
|
||||
|
||||
struct rte_ether_hdr {
|
||||
struct rte_ether_addr d_addr;
|
||||
RTE_STD_C11
|
||||
union {
|
||||
struct rte_ether_addr s_addr;
|
||||
struct {
|
||||
struct rte_ether_addr S_addr;
|
||||
} S_un;
|
||||
};
|
||||
rte_be16_t ether_type;
|
||||
} __attribute__((__packed__));
|
||||
12
test/libdpdk_dummy/include/rte_icmp.h
Normal file
12
test/libdpdk_dummy/include/rte_icmp.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_byteorder.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct rte_icmp_hdr {
|
||||
uint8_t icmp_type; /* ICMP packet type. */
|
||||
uint8_t icmp_code; /* ICMP packet code. */
|
||||
rte_be16_t icmp_cksum; /* ICMP packet checksum. */
|
||||
rte_be16_t icmp_ident; /* ICMP packet identifier. */
|
||||
rte_be16_t icmp_seq_nb; /* ICMP packet sequence number. */
|
||||
} __attribute__((__packed__));
|
||||
139
test/libdpdk_dummy/include/rte_ip.h
Normal file
139
test/libdpdk_dummy/include/rte_ip.h
Normal file
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <rte_byteorder.h>
|
||||
#include <rte_mbuf_core.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define RTE_IPV4_HDR_IHL_MASK (0x0f)
|
||||
#define RTE_IPV4_IHL_MULTIPLIER (4)
|
||||
#define IPPROTO_UDP IPPROTO_UDP
|
||||
|
||||
struct rte_ipv4_hdr {
|
||||
uint8_t version_ihl;
|
||||
uint8_t type_of_service;
|
||||
rte_be16_t total_length;
|
||||
rte_be16_t packet_id;
|
||||
rte_be16_t fragment_offset;
|
||||
uint8_t time_to_live;
|
||||
uint8_t next_proto_id;
|
||||
rte_be16_t hdr_checksum;
|
||||
rte_be32_t src_addr;
|
||||
rte_be32_t dst_addr;
|
||||
} __attribute__((__packed__));
|
||||
|
||||
struct rte_ipv6_hdr {
|
||||
rte_be32_t vtc_flow;
|
||||
rte_be16_t payload_len;
|
||||
uint8_t proto;
|
||||
uint8_t hop_limits;
|
||||
uint8_t src_addr[16];
|
||||
uint8_t dst_addr[16];
|
||||
} __attribute__((__packed__));
|
||||
|
||||
static inline uint8_t rte_ipv4_hdr_len(const struct rte_ipv4_hdr* ipv4_hdr) {
|
||||
return (uint8_t)((ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
|
||||
RTE_IPV4_IHL_MULTIPLIER);
|
||||
}
|
||||
|
||||
static inline uint16_t __rte_raw_cksum_reduce(uint32_t sum) {
|
||||
sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
|
||||
sum = ((sum & 0xffff0000) >> 16) + (sum & 0xffff);
|
||||
return (uint16_t)sum;
|
||||
}
|
||||
|
||||
static inline uint32_t __rte_raw_cksum(const void* buf, size_t len,
|
||||
uint32_t sum) {
|
||||
/* workaround gcc strict-aliasing warning */
|
||||
uintptr_t ptr = (uintptr_t)buf;
|
||||
typedef uint16_t __attribute__((__may_alias__)) u16_p;
|
||||
const u16_p* u16_buf = (const u16_p*)ptr;
|
||||
|
||||
while (len >= (sizeof(*u16_buf) * 4)) {
|
||||
sum += u16_buf[0];
|
||||
sum += u16_buf[1];
|
||||
sum += u16_buf[2];
|
||||
sum += u16_buf[3];
|
||||
len -= sizeof(*u16_buf) * 4;
|
||||
u16_buf += 4;
|
||||
}
|
||||
while (len >= sizeof(*u16_buf)) {
|
||||
sum += *u16_buf;
|
||||
len -= sizeof(*u16_buf);
|
||||
u16_buf += 1;
|
||||
}
|
||||
|
||||
/* if length is in odd bytes */
|
||||
if (len == 1)
|
||||
sum += *((const uint8_t*)u16_buf);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
static inline uint16_t rte_raw_cksum(const void* buf, size_t len) {
|
||||
uint32_t sum;
|
||||
|
||||
sum = __rte_raw_cksum(buf, len, 0);
|
||||
return __rte_raw_cksum_reduce(sum);
|
||||
}
|
||||
|
||||
static inline uint16_t rte_ipv4_cksum(const struct rte_ipv4_hdr* ipv4_hdr) {
|
||||
uint16_t cksum;
|
||||
cksum = rte_raw_cksum(ipv4_hdr, rte_ipv4_hdr_len(ipv4_hdr));
|
||||
return (uint16_t)~cksum;
|
||||
}
|
||||
|
||||
static inline uint16_t rte_ipv4_phdr_cksum(const struct rte_ipv4_hdr* ipv4_hdr,
|
||||
uint64_t ol_flags) {
|
||||
struct ipv4_psd_header {
|
||||
uint32_t src_addr; /* IP address of source host. */
|
||||
uint32_t dst_addr; /* IP address of destination host. */
|
||||
uint8_t zero; /* zero. */
|
||||
uint8_t proto; /* L4 protocol type. */
|
||||
uint16_t len; /* L4 length. */
|
||||
} psd_hdr;
|
||||
|
||||
psd_hdr.src_addr = ipv4_hdr->src_addr;
|
||||
psd_hdr.dst_addr = ipv4_hdr->dst_addr;
|
||||
psd_hdr.zero = 0;
|
||||
psd_hdr.proto = ipv4_hdr->next_proto_id;
|
||||
|
||||
if (ol_flags & PKT_TX_TCP_SEG) {
|
||||
psd_hdr.len = 0;
|
||||
} else {
|
||||
psd_hdr.len = rte_cpu_to_be_16(
|
||||
(uint16_t)(rte_be_to_cpu_16(ipv4_hdr->total_length) -
|
||||
sizeof(struct rte_ipv4_hdr)));
|
||||
}
|
||||
|
||||
return rte_raw_cksum(&psd_hdr, sizeof(psd_hdr));
|
||||
}
|
||||
|
||||
static inline uint16_t
|
||||
rte_ipv4_udptcp_cksum(const struct rte_ipv4_hdr* ipv4_hdr, const void* l4_hdr) {
|
||||
uint32_t cksum;
|
||||
uint32_t l3_len, l4_len;
|
||||
uint8_t ip_hdr_len;
|
||||
|
||||
ip_hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
|
||||
l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
|
||||
if (l3_len < ip_hdr_len)
|
||||
return 0;
|
||||
|
||||
l4_len = l3_len - ip_hdr_len;
|
||||
|
||||
cksum = rte_raw_cksum(l4_hdr, l4_len);
|
||||
cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);
|
||||
|
||||
cksum = ((cksum & 0xffff0000) >> 16) + (cksum & 0xffff);
|
||||
cksum = (~cksum) & 0xffff;
|
||||
/*
|
||||
* Per RFC 768:If the computed checksum is zero for UDP,
|
||||
* it is transmitted as all ones
|
||||
* (the equivalent in one's complement arithmetic).
|
||||
*/
|
||||
if (cksum == 0 && ipv4_hdr->next_proto_id == IPPROTO_UDP)
|
||||
cksum = 0xffff;
|
||||
|
||||
return (uint16_t)cksum;
|
||||
}
|
||||
3
test/libdpdk_dummy/include/rte_lcore.h
Normal file
3
test/libdpdk_dummy/include/rte_lcore.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
static unsigned int rte_lcore_id() { return 1; }
|
||||
119
test/libdpdk_dummy/include/rte_mbuf.h
Normal file
119
test/libdpdk_dummy/include/rte_mbuf.h
Normal file
@@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <rte_branch_prediction.h>
|
||||
#include <rte_common.h>
|
||||
#include <rte_config.h>
|
||||
#include <rte_mbuf_core.h>
|
||||
#include <rte_mempool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define __rte_mbuf_sanity_check(m, is_h) \
|
||||
do { \
|
||||
} while (0)
|
||||
|
||||
static inline uint16_t rte_pktmbuf_headroom(const struct rte_mbuf* m) {
|
||||
__rte_mbuf_sanity_check(m, 0);
|
||||
return m->data_off;
|
||||
}
|
||||
|
||||
static inline void rte_pktmbuf_reset_headroom(struct rte_mbuf* m) {
|
||||
m->data_off = (uint16_t)RTE_PKTMBUF_HEADROOM;
|
||||
//(uint16_t)RTE_MIN((uint16_t)RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
|
||||
}
|
||||
|
||||
static inline void rte_pktmbuf_reset(struct rte_mbuf* m) {
|
||||
m->next = NULL;
|
||||
m->pkt_len = 0;
|
||||
m->tx_offload = 0;
|
||||
m->vlan_tci = 0;
|
||||
m->vlan_tci_outer = 0;
|
||||
m->nb_segs = 1;
|
||||
m->port = UINT16_MAX;
|
||||
|
||||
m->ol_flags &= (1ULL << 61);
|
||||
m->packet_type = 0;
|
||||
rte_pktmbuf_reset_headroom(m);
|
||||
|
||||
m->data_len = 0;
|
||||
__rte_mbuf_sanity_check(m, 1);
|
||||
}
|
||||
|
||||
static struct rte_mbuf* rte_pktmbuf_alloc(struct rte_mempool* mp) {
|
||||
|
||||
struct rte_mbuf* m = NULL;
|
||||
m = new struct rte_mbuf;
|
||||
if (m != NULL) {
|
||||
m->refcnt = 1;
|
||||
m->nb_segs = 1;
|
||||
m->next = NULL;
|
||||
rte_pktmbuf_reset(m);
|
||||
|
||||
m->buf_addr = new uint8_t[2000];
|
||||
m->buf_len = 0;
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static inline void rte_pktmbuf_free(struct rte_mbuf* m) {
|
||||
struct rte_mbuf* m_next;
|
||||
|
||||
if (m != NULL)
|
||||
__rte_mbuf_sanity_check(m, 1);
|
||||
|
||||
delete[] m->buf_addr;
|
||||
m->buf_addr = nullptr;
|
||||
|
||||
while (m != NULL) {
|
||||
m_next = m->next;
|
||||
delete m;
|
||||
m = m_next;
|
||||
}
|
||||
}
|
||||
|
||||
static inline char* rte_pktmbuf_prepend(struct rte_mbuf* m, uint16_t len) {
|
||||
__rte_mbuf_sanity_check(m, 1);
|
||||
|
||||
if (unlikely(len > rte_pktmbuf_headroom(m))) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* NB: elaborating the subtraction like this instead of using
|
||||
* -= allows us to ensure the result type is uint16_t
|
||||
* avoiding compiler warnings on gcc 8.1 at least */
|
||||
m->data_off = static_cast<uint16_t>(m->data_off - len);
|
||||
m->data_len = static_cast<uint16_t>(m->data_len + len);
|
||||
m->pkt_len = (m->pkt_len + len);
|
||||
|
||||
return (char*)m->buf_addr + m->data_off;
|
||||
}
|
||||
|
||||
static inline uint16_t rte_pktmbuf_tailroom(const struct rte_mbuf* m) {
|
||||
__rte_mbuf_sanity_check(m, 0);
|
||||
return (uint16_t)(m->buf_len - rte_pktmbuf_headroom(m) - m->data_len);
|
||||
}
|
||||
|
||||
static inline struct rte_mbuf* rte_pktmbuf_lastseg(struct rte_mbuf* m) {
|
||||
__rte_mbuf_sanity_check(m, 1);
|
||||
while (m->next != NULL)
|
||||
m = m->next;
|
||||
return m;
|
||||
}
|
||||
|
||||
static inline char* rte_pktmbuf_append(struct rte_mbuf* m, uint16_t len) {
|
||||
void* tail;
|
||||
struct rte_mbuf* m_last;
|
||||
|
||||
__rte_mbuf_sanity_check(m, 1);
|
||||
|
||||
m_last = rte_pktmbuf_lastseg(m);
|
||||
if (unlikely(len > rte_pktmbuf_tailroom(m_last)))
|
||||
return NULL;
|
||||
|
||||
tail = (char*)m_last->buf_addr + m_last->data_off + m_last->data_len;
|
||||
m_last->data_len = (uint16_t)(m_last->data_len + len);
|
||||
m->pkt_len = (m->pkt_len + len);
|
||||
return (char*)tail;
|
||||
}
|
||||
183
test/libdpdk_dummy/include/rte_mbuf_core.h
Normal file
183
test/libdpdk_dummy/include/rte_mbuf_core.h
Normal file
@@ -0,0 +1,183 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_common.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define rte_pktmbuf_mtod_offset(m, t, o) \
|
||||
((t)((char*)(m)->buf_addr + (m)->data_off + (o)))
|
||||
|
||||
#define rte_pktmbuf_mtod(m, t) rte_pktmbuf_mtod_offset(m, t, 0)
|
||||
|
||||
#define PKT_RX_VLAN (1ULL << 0)
|
||||
|
||||
#define PKT_RX_RSS_HASH (1ULL << 1)
|
||||
|
||||
#define PKT_RX_FDIR (1ULL << 2)
|
||||
|
||||
#define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
|
||||
|
||||
#define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
|
||||
|
||||
#define PKT_RX_OUTER_IP_CKSUM_BAD (1ULL << 5)
|
||||
|
||||
#define PKT_RX_EIP_CKSUM_BAD \
|
||||
RTE_DEPRECATED(PKT_RX_EIP_CKSUM_BAD) PKT_RX_OUTER_IP_CKSUM_BAD
|
||||
|
||||
#define PKT_RX_VLAN_STRIPPED (1ULL << 6)
|
||||
|
||||
#define PKT_RX_IP_CKSUM_MASK ((1ULL << 4) | (1ULL << 7))
|
||||
|
||||
#define PKT_RX_IP_CKSUM_UNKNOWN 0
|
||||
#define PKT_RX_IP_CKSUM_BAD (1ULL << 4)
|
||||
#define PKT_RX_IP_CKSUM_GOOD (1ULL << 7)
|
||||
#define PKT_RX_IP_CKSUM_NONE ((1ULL << 4) | (1ULL << 7))
|
||||
|
||||
#define PKT_RX_L4_CKSUM_MASK ((1ULL << 3) | (1ULL << 8))
|
||||
|
||||
#define PKT_RX_L4_CKSUM_UNKNOWN 0
|
||||
#define PKT_RX_L4_CKSUM_BAD (1ULL << 3)
|
||||
#define PKT_RX_L4_CKSUM_GOOD (1ULL << 8)
|
||||
#define PKT_RX_L4_CKSUM_NONE ((1ULL << 3) | (1ULL << 8))
|
||||
|
||||
#define PKT_RX_IEEE1588_PTP (1ULL << 9)
|
||||
|
||||
#define PKT_RX_IEEE1588_TMST (1ULL << 10)
|
||||
|
||||
#define PKT_RX_FDIR_ID (1ULL << 13)
|
||||
|
||||
#define PKT_RX_FDIR_FLX (1ULL << 14)
|
||||
|
||||
#define PKT_RX_QINQ_STRIPPED (1ULL << 15)
|
||||
|
||||
#define PKT_RX_LRO (1ULL << 16)
|
||||
|
||||
/* There is no flag defined at offset 17. It is free for any future use. */
|
||||
|
||||
#define PKT_RX_SEC_OFFLOAD (1ULL << 18)
|
||||
|
||||
#define PKT_RX_SEC_OFFLOAD_FAILED (1ULL << 19)
|
||||
|
||||
#define PKT_RX_QINQ (1ULL << 20)
|
||||
|
||||
#define PKT_RX_OUTER_L4_CKSUM_MASK ((1ULL << 21) | (1ULL << 22))
|
||||
|
||||
#define PKT_RX_OUTER_L4_CKSUM_UNKNOWN 0
|
||||
#define PKT_RX_OUTER_L4_CKSUM_BAD (1ULL << 21)
|
||||
#define PKT_RX_OUTER_L4_CKSUM_GOOD (1ULL << 22)
|
||||
#define PKT_RX_OUTER_L4_CKSUM_INVALID ((1ULL << 21) | (1ULL << 22))
|
||||
|
||||
/* add new RX flags here, don't forget to update PKT_FIRST_FREE */
|
||||
|
||||
#define PKT_FIRST_FREE (1ULL << 23)
|
||||
#define PKT_LAST_FREE (1ULL << 40)
|
||||
|
||||
/* add new TX flags here, don't forget to update PKT_LAST_FREE */
|
||||
|
||||
#define PKT_TX_OUTER_UDP_CKSUM (1ULL << 41)
|
||||
|
||||
#define PKT_TX_UDP_SEG (1ULL << 42)
|
||||
|
||||
#define PKT_TX_SEC_OFFLOAD (1ULL << 43)
|
||||
|
||||
#define PKT_TX_MACSEC (1ULL << 44)
|
||||
|
||||
#define PKT_TX_TUNNEL_VXLAN (0x1ULL << 45)
|
||||
#define PKT_TX_TUNNEL_GRE (0x2ULL << 45)
|
||||
#define PKT_TX_TUNNEL_IPIP (0x3ULL << 45)
|
||||
#define PKT_TX_TUNNEL_GENEVE (0x4ULL << 45)
|
||||
|
||||
#define PKT_TX_TUNNEL_MPLSINUDP (0x5ULL << 45)
|
||||
#define PKT_TX_TUNNEL_VXLAN_GPE (0x6ULL << 45)
|
||||
#define PKT_TX_TUNNEL_GTP (0x7ULL << 45)
|
||||
|
||||
#define PKT_TX_TUNNEL_IP (0xDULL << 45)
|
||||
|
||||
#define PKT_TX_TUNNEL_UDP (0xEULL << 45)
|
||||
/* add new TX TUNNEL type here */
|
||||
#define PKT_TX_TUNNEL_MASK (0xFULL << 45)
|
||||
|
||||
#define PKT_TX_QINQ (1ULL << 49)
|
||||
|
||||
#define PKT_TX_QINQ_PKT PKT_TX_QINQ
|
||||
|
||||
#define PKT_TX_TCP_SEG (1ULL << 50)
|
||||
|
||||
#define PKT_TX_IEEE1588_TMST (1ULL << 51)
|
||||
|
||||
#define PKT_TX_L4_NO_CKSUM (0ULL << 52)
|
||||
#define PKT_TX_TCP_CKSUM (1ULL << 52)
|
||||
|
||||
#define PKT_TX_SCTP_CKSUM (2ULL << 52)
|
||||
|
||||
#define PKT_TX_UDP_CKSUM (3ULL << 52)
|
||||
|
||||
#define PKT_TX_L4_MASK (3ULL << 52)
|
||||
|
||||
#define PKT_TX_IP_CKSUM (1ULL << 54)
|
||||
|
||||
#define PKT_TX_IPV4 (1ULL << 55)
|
||||
|
||||
#define PKT_TX_IPV6 (1ULL << 56)
|
||||
|
||||
#define PKT_TX_VLAN (1ULL << 57)
|
||||
/* this old name is deprecated */
|
||||
#define PKT_TX_VLAN_PKT PKT_TX_VLAN
|
||||
|
||||
#define PKT_TX_OUTER_IP_CKSUM (1ULL << 58)
|
||||
|
||||
#define PKT_TX_OUTER_IPV4 (1ULL << 59)
|
||||
|
||||
#define PKT_TX_OUTER_IPV6 (1ULL << 60)
|
||||
|
||||
enum {
|
||||
RTE_MBUF_L2_LEN_BITS = 7,
|
||||
RTE_MBUF_L3_LEN_BITS = 9,
|
||||
RTE_MBUF_L4_LEN_BITS = 8,
|
||||
RTE_MBUF_TSO_SEGSZ_BITS = 16,
|
||||
RTE_MBUF_OUTL3_LEN_BITS = 9,
|
||||
RTE_MBUF_OUTL2_LEN_BITS = 7
|
||||
};
|
||||
|
||||
struct rte_mbuf {
|
||||
void* buf_addr;
|
||||
uint16_t buf_len;
|
||||
|
||||
struct rte_mbuf* next;
|
||||
uint32_t pkt_len;
|
||||
uint16_t vlan_tci;
|
||||
uint16_t vlan_tci_outer;
|
||||
uint16_t nb_segs;
|
||||
uint16_t port;
|
||||
uint64_t ol_flags;
|
||||
uint32_t packet_type;
|
||||
uint16_t data_len;
|
||||
uint16_t data_off;
|
||||
uint16_t refcnt;
|
||||
|
||||
/* fields to support TX offloads */
|
||||
RTE_STD_C11
|
||||
union {
|
||||
uint64_t tx_offload;
|
||||
__extension__ struct {
|
||||
uint64_t l2_len : RTE_MBUF_L2_LEN_BITS;
|
||||
uint64_t l3_len : RTE_MBUF_L3_LEN_BITS;
|
||||
uint64_t l4_len : RTE_MBUF_L4_LEN_BITS;
|
||||
uint64_t tso_segsz : RTE_MBUF_TSO_SEGSZ_BITS;
|
||||
/*
|
||||
* Fields for Tx offloading of tunnels.
|
||||
* These are undefined for packets which don't request
|
||||
* any tunnel offloads (outer IP or UDP checksum,
|
||||
* tunnel TSO).
|
||||
*
|
||||
* PMDs should not use these fields unconditionally
|
||||
* when calculating offsets.
|
||||
*
|
||||
* Applications are expected to set appropriate tunnel
|
||||
* offload flags when they fill in these fields.
|
||||
*/
|
||||
uint64_t outer_l3_len : RTE_MBUF_OUTL3_LEN_BITS;
|
||||
uint64_t outer_l2_len : RTE_MBUF_OUTL2_LEN_BITS;
|
||||
/* uint64_t unused:RTE_MBUF_TXOFLD_UNUSED_BITS; */
|
||||
};
|
||||
};
|
||||
};
|
||||
3
test/libdpdk_dummy/include/rte_mempool.h
Normal file
3
test/libdpdk_dummy/include/rte_mempool.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
struct rte_mempool {};
|
||||
16
test/libdpdk_dummy/include/rte_tcp.h
Normal file
16
test/libdpdk_dummy/include/rte_tcp.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_byteorder.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct rte_tcp_hdr {
|
||||
rte_be16_t src_port;
|
||||
rte_be16_t dst_port;
|
||||
rte_be32_t sent_seq;
|
||||
rte_be32_t recv_ack;
|
||||
uint8_t data_off;
|
||||
uint8_t tcp_flags;
|
||||
rte_be16_t rx_win;
|
||||
rte_be16_t cksum;
|
||||
rte_be16_t tcp_urp;
|
||||
} __attribute__((__packed__));
|
||||
10
test/libdpdk_dummy/include/rte_udp.h
Normal file
10
test/libdpdk_dummy/include/rte_udp.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <rte_byteorder.h>
|
||||
|
||||
struct rte_udp_hdr {
|
||||
rte_be16_t src_port;
|
||||
rte_be16_t dst_port;
|
||||
rte_be16_t dgram_len;
|
||||
rte_be16_t dgram_cksum;
|
||||
} __attribute__((__packed__));
|
||||
14
test/libdpdk_dummy/libdpdk_dummy_test.cpp
Normal file
14
test/libdpdk_dummy/libdpdk_dummy_test.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <catch2/catch.hpp>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
#include <rte_mbuf.h>
|
||||
#include <rte_mempool.h>
|
||||
|
||||
TEST_CASE("rte_mbuf", "[]") {
|
||||
struct rte_mbuf* mbuf;
|
||||
struct rte_mempool* mempool = nullptr;
|
||||
|
||||
mbuf = rte_pktmbuf_alloc(mempool);
|
||||
CHECK(mbuf != nullptr);
|
||||
}
|
||||
15
test/meson.build
Normal file
15
test/meson.build
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
# ..each entry will be a test executable
|
||||
# ..the structure of the test directory should be equivalent
|
||||
# ..to the structure in source/ or include/
|
||||
|
||||
# Syntax:
|
||||
# 'Name of the Test (e.g. class name)' : 'Path/to/source/file'
|
||||
test_sources_dict = {
|
||||
#'PacketInfo' : 'test/PacketDissection/PacketInfo_test.cpp',
|
||||
'PacketContainer' : 'test/PacketDissection/PacketContainer_test.cpp',
|
||||
'Configurator' : 'test/ConfigurationManagement/Configurator_test.cpp',
|
||||
'libdpdk_dummy' : 'test/libdpdk_dummy/libdpdk_dummy_test.cpp',
|
||||
'Treatment' : 'test/Treatment/Treatment_test.cpp',
|
||||
'RandomNumberGenerator' : 'test/RandomNumberGenerator/RandomNumberGenerator_test.cpp',
|
||||
}
|
||||
Reference in New Issue
Block a user