|
| 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 */ |
0 commit comments