Skip to content

Commit c86826a

Browse files
committed
CTT - add CTT commander
1 parent 792f50a commit c86826a

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

storage/cttController.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* \file
3+
* \author Damir Zainullin <[email protected]>
4+
* \brief CttController implementation.
5+
*/
6+
/*
7+
* Copyright (C) 2023 CESNET
8+
*
9+
* LICENSE TERMS
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
* 3. Neither the name of the Company nor the names of its contributors
21+
* may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*/
24+
25+
#include "cttController.hpp"
26+
27+
#ifdef WITH_CTT
28+
29+
namespace ipxp {
30+
31+
CttController::CttController(const std::string& nfb_dev, unsigned ctt_comp_index) {
32+
m_commander = std::make_unique<ctt::AsyncCommander>(ctt::NfbParamsFast{nfb_dev, ctt_comp_index});
33+
try {
34+
// Get UserInfo to determine key, state, and state_mask sizes
35+
ctt::UserInfo user_info = m_commander->get_user_info();
36+
key_size_bytes = (user_info.key_bit_width + 7) / 8;
37+
state_size_bytes = (user_info.state_bit_width + 7) / 8;
38+
state_mask_size_bytes = (user_info.state_mask_bit_width + 7) / 8;
39+
40+
// Enable the CTT
41+
std::future<void> enable_future = m_commander->enable(true);
42+
enable_future.wait();
43+
}
44+
catch (const std::exception& e) {
45+
throw;
46+
}
47+
}
48+
49+
void CttController::create_record(uint64_t flow_hash_ctt, const struct timeval& ts)
50+
{
51+
try {
52+
std::vector<std::byte> key = assemble_key(flow_hash_ctt);
53+
std::vector<std::byte> state = assemble_state(
54+
OffloadMode::PACKET_OFFLOAD,
55+
MetaType::FULL,
56+
ts);
57+
m_commander->write_record(std::move(key), std::move(state));
58+
}
59+
catch (const std::exception& e) {
60+
throw;
61+
}
62+
}
63+
64+
void CttController::export_record(uint64_t flow_hash_ctt)
65+
{
66+
try {
67+
std::vector<std::byte> key = assemble_key(flow_hash_ctt);
68+
m_commander->export_and_delete_record(std::move(key));
69+
}
70+
catch (const std::exception& e) {
71+
throw;
72+
}
73+
}
74+
75+
std::vector<std::byte> CttController::assemble_key(uint64_t flow_hash_ctt)
76+
{
77+
std::vector<std::byte> key(key_size_bytes, std::byte(0));
78+
for (size_t i = 0; i < sizeof(flow_hash_ctt) && i < key_size_bytes; ++i) {
79+
key[i] = static_cast<std::byte>((flow_hash_ctt >> (8 * i)) & 0xFF);
80+
}
81+
return key;
82+
}
83+
84+
std::vector<std::byte> CttController::assemble_state(
85+
OffloadMode offload_mode, MetaType meta_type, const struct timeval& ts)
86+
{
87+
std::vector<std::byte> state(state_size_bytes, std::byte(0));
88+
std::vector<std::byte> state_mask(state_mask_size_bytes, std::byte(0));
89+
90+
state[0] = static_cast<std::byte>(offload_mode);
91+
state[1] = static_cast<std::byte>(meta_type);
92+
93+
// timestamp in sec/ns format, 32+32 bits - 64 bits in total
94+
for (size_t i = 0; i < sizeof(ts.tv_sec) && i < 4; ++i) {
95+
state[2 + i] = static_cast<std::byte>((ts.tv_sec >> (8 * i)) & 0xFF);
96+
}
97+
for (size_t i = 0; i < sizeof(ts.tv_usec) && i < 4; ++i) {
98+
state[6 + i] = static_cast<std::byte>((ts.tv_usec >> (8 * i)) & 0xFF);
99+
}
100+
return state;
101+
}
102+
103+
CttController::~CttController() noexcept
104+
{
105+
if (!m_commander) {
106+
return;
107+
}
108+
std::future<void> enable_future = m_commander->enable(false);
109+
enable_future.wait();
110+
m_commander.reset();
111+
}
112+
113+
} // ipxp
114+
115+
#endif /* WITH_CTT */

storage/cttController.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* \file
3+
* \author Damir Zainullin <[email protected]>
4+
* \brief CttController declaration.
5+
*/
6+
/*
7+
* Copyright (C) 2023 CESNET
8+
*
9+
* LICENSE TERMS
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
* 3. Neither the name of the Company nor the names of its contributors
21+
* may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*/
24+
25+
#pragma once
26+
27+
#include <config.h>
28+
#ifdef WITH_CTT
29+
#include <sys/time.h>
30+
#include <ctt_async.hpp>
31+
#include <ctt_factory.hpp>
32+
#include <ctt_exceptions.hpp>
33+
#include <ctt_modes.hpp>
34+
#include <ctt.hpp>
35+
#include <queue>
36+
#include <tuple>
37+
38+
namespace ipxp {
39+
40+
class CttController {
41+
public:
42+
enum class OffloadMode : uint8_t {
43+
NO_OFFLOAD = 0x0,
44+
PACKET_OFFLOAD = 0x1,
45+
META_EXPORT = 0x2,
46+
PACKET_OFFLOAD_WITH_EXPORT = 0x3
47+
};
48+
enum class MetaType : uint8_t {
49+
FULL = 0x0,
50+
HALF = 0x1,
51+
TS_ONLY = 0x2,
52+
NO_META = 0x3
53+
};
54+
/**
55+
* @brief init the CTT.
56+
*
57+
* @param nfb_dev The NFB device file (e.g., "/dev/nfb0").
58+
* @param ctt_comp_index The index of the CTT component.
59+
*/
60+
CttController(const std::string& nfb_dev, unsigned ctt_comp_index);
61+
62+
/**
63+
* @brief Command: mark a flow for offload.
64+
*
65+
* @param flow_hash_ctt The flow hash to be offloaded.
66+
*/
67+
void create_record(uint64_t flow_hash_ctt, const struct timeval& timestamp_first);
68+
69+
/**
70+
* @brief Command: export a flow from the CTT.
71+
*
72+
* @param flow_hash_ctt The flow hash to be exported.
73+
*/
74+
void export_record(uint64_t flow_hash_ctt);
75+
76+
~CttController() noexcept;
77+
78+
private:
79+
std::unique_ptr<ctt::AsyncCommander> m_commander;
80+
size_t key_size_bytes;
81+
size_t state_size_bytes;
82+
size_t state_mask_size_bytes;
83+
84+
/**
85+
* @brief Assembles the state vector from the given values.
86+
*
87+
* @param offload_mode The offload mode.
88+
* @param meta_type The metadata type.
89+
* @param timestamp_first The first timestamp of the flow.
90+
* @return A byte vector representing the assembled state vector.
91+
*/
92+
std::vector<std::byte> assemble_state(
93+
OffloadMode offload_mode, MetaType meta_type,
94+
const struct timeval& timestamp_first);
95+
96+
/**
97+
* @brief Assembles the key vector from the given flow hash.
98+
*
99+
* @param flow_hash_ctt The flow hash.
100+
* @return A byte vector representing the assembled key vector.
101+
*/
102+
std::vector<std::byte> assemble_key(uint64_t flow_hash_ctt);
103+
};
104+
105+
} // ipxp
106+
107+
#endif /* WITH_CTT */

0 commit comments

Comments
 (0)