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