From 8e13f04684592ea1561b76af6d60c096e4f6c427 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Wed, 3 Feb 2021 14:22:01 +0100 Subject: [PATCH] mac_pcap: refactor MAC PCAP and include NR RAT into same object after adding the thread-safe PCAP writer functionality to the EUTRA MAC object it became clear that we don't wont to replicate the same for the NR object. This patch therefore refactors the class that now supports both EUTRA and NR rats. The old mac_nr_pcap.[h/cc] has been deleted. All test-cases and usages now use the new object that needs to pass the RAT type in the ctor. this patch addresses the last open point of #2160 --- lib/include/srslte/common/mac_nr_pcap.h | 54 ------------ lib/include/srslte/common/mac_pcap.h | 38 +++++++-- lib/src/common/CMakeLists.txt | 2 +- lib/src/common/mac_nr_pcap.cc | 108 ----------------------- lib/src/common/mac_pcap.cc | 109 +++++++++++++++++++++--- lib/test/mac/mac_pcap_test.cc | 57 ++++++++++++- lib/test/mac/mac_pdu_nr_test.cc | 42 ++++----- srsenb/hdr/stack/mac/mac_nr.h | 4 +- srsenb/src/stack/enb_stack_lte.cc | 3 +- srsenb/src/stack/mac/mac_nr.cc | 16 ++-- srsue/hdr/stack/mac_nr/mac_nr.h | 4 +- srsue/hdr/stack/ue_stack_nr.h | 4 +- srsue/src/stack/mac_nr/mac_nr.cc | 6 +- srsue/src/stack/ue_stack_lte.cc | 10 ++- srsue/src/stack/ue_stack_nr.cc | 4 - 15 files changed, 230 insertions(+), 231 deletions(-) delete mode 100644 lib/include/srslte/common/mac_nr_pcap.h delete mode 100644 lib/src/common/mac_nr_pcap.cc diff --git a/lib/include/srslte/common/mac_nr_pcap.h b/lib/include/srslte/common/mac_nr_pcap.h deleted file mode 100644 index 01515737a..000000000 --- a/lib/include/srslte/common/mac_nr_pcap.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * - * \section COPYRIGHT - * - * Copyright 2013-2020 Software Radio Systems Limited - * - * By using this file, you agree to the terms and conditions set - * forth in the LICENSE file which can be found at the top level of - * the distribution. - * - */ - -#ifndef SRSLTE_MAC_NR_PCAP_H -#define SRSLTE_MAC_NR_PCAP_H - -#include - -namespace srslte { - -class mac_nr_pcap -{ -public: - mac_nr_pcap(); - ~mac_nr_pcap(); - void enable(const bool& enable_); - void open(const std::string& filename, const uint16_t& ue_id = 0); - void close(); - - void set_ue_id(const uint16_t& ue_id); - - void write_dl_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t crnti, uint8_t harqid, uint32_t tti); - void write_ul_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); - void write_dl_ra_rnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); - void write_dl_bch(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); - void write_dl_pch(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); - void write_dl_si_rnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); - -private: - bool enable_write = false; - std::string filename; - FILE* pcap_file = nullptr; - uint32_t ue_id = 0; - void pack_and_write(uint8_t* pdu, - uint32_t pdu_len_bytes, - uint32_t tti, - uint16_t crnti_, - uint8_t harqid, - uint8_t direction, - uint8_t rnti_type); -}; - -} // namespace srslte - -#endif // SRSLTE_MAC_NR_PCAP_H diff --git a/lib/include/srslte/common/mac_pcap.h b/lib/include/srslte/common/mac_pcap.h index 737f2709b..cacc96ec7 100644 --- a/lib/include/srslte/common/mac_pcap.h +++ b/lib/include/srslte/common/mac_pcap.h @@ -18,6 +18,7 @@ #include "srslte/common/common.h" #include "srslte/common/pcap.h" #include "srslte/common/threads.h" +#include "srslte/srslog/srslog.h" #include #include #include @@ -26,14 +27,15 @@ namespace srslte { class mac_pcap : srslte::thread { public: - mac_pcap(); + mac_pcap(srslte_rat_t rat); ~mac_pcap(); - void enable(bool en); - uint32_t open(const char* filename, uint32_t ue_id = 0); + void enable(bool enable); + uint32_t open(std::string filename, uint32_t ue_id = 0); uint32_t close(); void set_ue_id(uint16_t ue_id); + // EUTRA void write_ul_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t crnti, uint32_t reTX, uint32_t tti, uint8_t cc_idx); void write_dl_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t crnti, bool crc_ok, uint32_t tti, uint8_t cc_idx); @@ -50,14 +52,25 @@ public: // Sidelink void write_sl_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint32_t reTX, uint32_t tti, uint8_t cc_idx); + // NR + void write_dl_crnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t crnti, uint8_t harqid, uint32_t tti); + void write_ul_crnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); + void write_dl_ra_rnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); + void write_dl_bch_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); + void write_dl_pch_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); + void write_dl_si_rnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti); + private: srslte::byte_buffer_pool* pool = nullptr; srslog::basic_logger& logger; - bool running = false; + bool running = false; + srslte_rat_t rat = srslte_rat_t::nulltype; + uint32_t dlt = 0; // The DLT used for the PCAP file + std::string filename; FILE* pcap_file = nullptr; uint32_t ue_id = 0; - void pack_and_queue(uint8_t* pdu, - uint32_t pdu_len_bytes, + void pack_and_queue(uint8_t* payload, + uint32_t payload_len, uint32_t reTX, bool crc_ok, uint8_t cc_idx, @@ -65,10 +78,19 @@ private: uint16_t crnti_, uint8_t direction, uint8_t rnti_type); + void pack_and_queue_nr(uint8_t* payload, + uint32_t payload_len, + uint32_t tti, + uint16_t crnti, + uint8_t harqid, + uint8_t direction, + uint8_t rnti_type); typedef struct { - MAC_Context_Info_t context; - unique_byte_buffer_t pdu; + // Different PCAP context for both RATs + MAC_Context_Info_t context; + mac_nr_context_info_t context_nr; + unique_byte_buffer_t pdu; } pcap_pdu_t; block_queue queue; std::mutex mutex; diff --git a/lib/src/common/CMakeLists.txt b/lib/src/common/CMakeLists.txt index e6137e15a..6bb7c17d3 100644 --- a/lib/src/common/CMakeLists.txt +++ b/lib/src/common/CMakeLists.txt @@ -35,7 +35,7 @@ set(SOURCES arch_select.cc s3g.cc) if (ENABLE_5GNR) - set(SOURCES ${SOURCES} mac_nr_pcap.cc basic_vnf.cc) + set(SOURCES ${SOURCES} basic_vnf.cc) endif(ENABLE_5GNR) # Avoid warnings caused by libmbedtls about deprecated functions diff --git a/lib/src/common/mac_nr_pcap.cc b/lib/src/common/mac_nr_pcap.cc deleted file mode 100644 index 010e86dd6..000000000 --- a/lib/src/common/mac_nr_pcap.cc +++ /dev/null @@ -1,108 +0,0 @@ -/** - * - * \section COPYRIGHT - * - * Copyright 2013-2020 Software Radio Systems Limited - * - * By using this file, you agree to the terms and conditions set - * forth in the LICENSE file which can be found at the top level of - * the distribution. - * - */ - -#include "srslte/common/mac_nr_pcap.h" -#include "srslte/common/pcap.h" -#include - -namespace srslte { - -mac_nr_pcap::mac_nr_pcap() {} - -mac_nr_pcap::~mac_nr_pcap() -{ - if (pcap_file) { - close(); - } -} - -void mac_nr_pcap::enable(const bool& enable_) -{ - enable_write = enable_; -} -void mac_nr_pcap::open(const std::string& filename_, const uint16_t& ue_id_) -{ - fprintf(stdout, "Opening MAC-NR PCAP with DLT=%d\n", UDP_DLT); - filename = filename_; - pcap_file = LTE_PCAP_Open(UDP_DLT, filename.c_str()); - ue_id = ue_id_; - enable_write = true; -} -void mac_nr_pcap::close() -{ - enable_write = false; - fprintf(stdout, "Saving MAC-NR PCAP to %s\n", filename.c_str()); - LTE_PCAP_Close(pcap_file); - pcap_file = nullptr; -} - -void mac_nr_pcap::set_ue_id(const uint16_t& ue_id_) -{ - ue_id = ue_id_; -} - -void mac_nr_pcap::pack_and_write(uint8_t* pdu, - uint32_t pdu_len_bytes, - uint32_t tti, - uint16_t crnti, - uint8_t harqid, - uint8_t direction, - uint8_t rnti_type) -{ - if (enable_write) { - mac_nr_context_info_t context = {}; - context.radioType = FDD_RADIO; - context.direction = direction; - context.rntiType = rnti_type; - context.rnti = crnti; - context.ueid = ue_id; - context.harqid = harqid; - context.system_frame_number = tti / 10; - context.sub_frame_number = tti % 10; - - if (pdu) { - NR_PCAP_MAC_WritePDU(pcap_file, &context, pdu, pdu_len_bytes); - } - } -} - -void mac_nr_pcap::write_dl_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, C_RNTI); -} - -void mac_nr_pcap::write_ul_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_UPLINK, C_RNTI); -} - -void mac_nr_pcap::write_dl_ra_rnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, RA_RNTI); -} - -void mac_nr_pcap::write_dl_bch(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, NO_RNTI); -} - -void mac_nr_pcap::write_dl_pch(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, P_RNTI); -} - -void mac_nr_pcap::write_dl_si_rnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) -{ - pack_and_write(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, SI_RNTI); -} - -} // namespace srslte diff --git a/lib/src/common/mac_pcap.cc b/lib/src/common/mac_pcap.cc index a607be7cf..3fefe85b6 100644 --- a/lib/src/common/mac_pcap.cc +++ b/lib/src/common/mac_pcap.cc @@ -17,8 +17,11 @@ namespace srslte { -mac_pcap::mac_pcap() : - pool(srslte::byte_buffer_pool::get_instance()), logger(srslog::fetch_basic_logger("MAC")), thread("PCAP_WRITER") +mac_pcap::mac_pcap(srslte_rat_t rat_) : + pool(srslte::byte_buffer_pool::get_instance()), + logger(srslog::fetch_basic_logger("MAC")), + thread("PCAP_WRITER_" + to_string(rat_)), + rat(rat_) {} mac_pcap::~mac_pcap() @@ -32,22 +35,36 @@ void mac_pcap::enable(bool enable_) running = enable_; } -uint32_t mac_pcap::open(const char* filename, uint32_t ue_id_) +uint32_t mac_pcap::open(std::string filename_, uint32_t ue_id_) { std::lock_guard lock(mutex); if (pcap_file != nullptr) { - logger.error("PCAP writer already running. Close first."); + logger.error("PCAP writer for %s already running. Close first.", filename_.c_str()); return SRSLTE_ERROR; } - pcap_file = LTE_PCAP_Open(MAC_LTE_DLT, filename); + // set DLT for selected RAT + switch (rat) { + case srslte_rat_t::lte: + dlt = MAC_LTE_DLT; + break; + case srslte_rat_t::nr: + dlt = UDP_DLT; + break; + default: + logger.error("Error opening PCAP. Unsupported RAT selected."); + return SRSLTE_ERROR; + } + + pcap_file = LTE_PCAP_Open(dlt, filename_.c_str()); if (pcap_file == nullptr) { - logger.error("Couldn't open file to write PCAP"); + logger.error("Couldn't open %s to write PCAP", filename_.c_str()); return SRSLTE_ERROR; } - ue_id = ue_id_; - running = true; + filename = filename_; + ue_id = ue_id_; + running = true; // start writer thread start(); @@ -74,7 +91,7 @@ uint32_t mac_pcap::close() // close file handle { std::lock_guard lock(mutex); - srslte::console("Saving MAC PCAP file\n"); + srslte::console("Saving %s MAC PCAP (DLT=%d) to %s\n", to_string(rat).c_str(), dlt, filename.c_str()); LTE_PCAP_Close(pcap_file); pcap_file = nullptr; } @@ -85,7 +102,16 @@ uint32_t mac_pcap::close() void mac_pcap::write_pdu(pcap_pdu_t& pdu) { if (pdu.pdu != nullptr) { - LTE_PCAP_MAC_WritePDU(pcap_file, &pdu.context, pdu.pdu->msg, pdu.pdu->N_bytes); + switch (rat) { + case srslte_rat_t::lte: + LTE_PCAP_MAC_WritePDU(pcap_file, &pdu.context, pdu.pdu->msg, pdu.pdu->N_bytes); + break; + case srslte_rat_t::nr: + NR_PCAP_MAC_WritePDU(pcap_file, &pdu.context_nr, pdu.pdu->msg, pdu.pdu->N_bytes); + break; + default: + logger.error("Error writing PDU to PCAP. Unsupported RAT selected."); + } } } @@ -151,6 +177,39 @@ void mac_pcap::pack_and_queue(uint8_t* payload, } } +// Function called from PHY worker context, locking not needed as PDU queue is thread-safe +void mac_pcap::pack_and_queue_nr(uint8_t* payload, + uint32_t payload_len, + uint32_t tti, + uint16_t crnti, + uint8_t harqid, + uint8_t direction, + uint8_t rnti_type) +{ + if (running && payload != nullptr) { + pcap_pdu_t pdu = {}; + pdu.context_nr.radioType = FDD_RADIO; + pdu.context_nr.direction = direction; + pdu.context_nr.rntiType = rnti_type; + pdu.context_nr.rnti = crnti; + pdu.context_nr.ueid = ue_id; + pdu.context_nr.harqid = harqid; + pdu.context_nr.system_frame_number = tti / 10; + pdu.context_nr.sub_frame_number = tti % 10; + + // try to allocate PDU buffer + pdu.pdu = srslte::allocate_unique_buffer(*pool); + if (pdu.pdu != nullptr && pdu.pdu->get_tailroom() >= payload_len) { + // copy payload into PDU buffer + memcpy(pdu.pdu->msg, payload, payload_len); + pdu.pdu->N_bytes = payload_len; + queue.push(std::move(pdu)); + } else { + logger.info("Dropping PDU in NR PCAP. No buffer available or not enough space (pdu_len=%d).", payload_len); + } + } +} + void mac_pcap::write_dl_crnti(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, @@ -206,6 +265,36 @@ void mac_pcap::write_dl_sirnti(uint8_t* pdu, uint32_t pdu_len_bytes, bool crc_ok pack_and_queue(pdu, pdu_len_bytes, 0, crc_ok, cc_idx, tti, SRSLTE_SIRNTI, DIRECTION_DOWNLINK, SI_RNTI); } +void mac_pcap::write_dl_crnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, C_RNTI); +} + +void mac_pcap::write_ul_crnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_UPLINK, C_RNTI); +} + +void mac_pcap::write_dl_ra_rnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, RA_RNTI); +} + +void mac_pcap::write_dl_bch_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, NO_RNTI); +} + +void mac_pcap::write_dl_pch_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, P_RNTI); +} + +void mac_pcap::write_dl_si_rnti_nr(uint8_t* pdu, uint32_t pdu_len_bytes, uint16_t rnti, uint8_t harqid, uint32_t tti) +{ + pack_and_queue_nr(pdu, pdu_len_bytes, tti, rnti, harqid, DIRECTION_DOWNLINK, SI_RNTI); +} + void mac_pcap::write_ul_rrc_pdu(const uint8_t* input, const int32_t input_len) { uint8_t pdu[1024]; diff --git a/lib/test/mac/mac_pcap_test.cc b/lib/test/mac/mac_pcap_test.cc index 6992154d1..2a4ef6ee0 100644 --- a/lib/test/mac/mac_pcap_test.cc +++ b/lib/test/mac/mac_pcap_test.cc @@ -16,7 +16,10 @@ #include #include -void write_pcap_thread_function(srslte::mac_pcap* pcap_handle, const std::array& pdu, uint32_t num_pdus) +// Write #num_pdus UL MAC PDUs using PCAP handle +void write_pcap_eutra_thread_function(srslte::mac_pcap* pcap_handle, + const std::array& pdu, + uint32_t num_pdus) { for (uint32_t i = 0; i < num_pdus; i++) { pcap_handle->write_ul_crnti(const_cast(pdu.data()), pdu.size(), 0x1001, true, 1, 0); @@ -25,7 +28,17 @@ void write_pcap_thread_function(srslte::mac_pcap* pcap_handle, const std::array< std::cout << "Finished thread " << std::this_thread::get_id() << "\n"; } -int main() +// Write #num_pdus DL MAC NR PDUs using PCAP handle +void write_pcap_nr_thread_function(srslte::mac_pcap* pcap_handle, const std::array& pdu, uint32_t num_pdus) +{ + for (uint32_t i = 0; i < num_pdus; i++) { + pcap_handle->write_dl_crnti_nr(const_cast(pdu.data()), pdu.size(), 0x1001, 0, 1); + } + + std::cout << "Finished thread " << std::this_thread::get_id() << "\n"; +} + +int mac_pcap_eutra_test() { std::array tv = { 0x21, 0x08, 0x22, 0x80, 0x82, 0x1f, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, @@ -40,14 +53,15 @@ int main() uint32_t num_threads = 10; uint32_t num_pdus_per_thread = 100; - std::unique_ptr pcap_handle = std::unique_ptr(new srslte::mac_pcap()); + std::unique_ptr pcap_handle = + std::unique_ptr(new srslte::mac_pcap(srslte::srslte_rat_t::lte)); TESTASSERT(pcap_handle->open("mac_pcap_test.pcap") == SRSLTE_SUCCESS); TESTASSERT(pcap_handle->open("mac_pcap_test.pcap") != SRSLTE_SUCCESS); // open again will fail std::vector writer_threads; for (uint32_t i = 0; i < num_threads; i++) { - writer_threads.push_back(std::thread(write_pcap_thread_function, pcap_handle.get(), tv, num_pdus_per_thread)); + writer_threads.push_back(std::thread(write_pcap_eutra_thread_function, pcap_handle.get(), tv, num_pdus_per_thread)); } // wait for threads to finish @@ -60,3 +74,38 @@ int main() return SRSLTE_SUCCESS; } + +int mac_pcap_nr_test() +{ + std::array tv = {0x42, 0x00, 0x08, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; + + uint32_t num_threads = 10; + uint32_t num_pdus_per_thread = 100; + + std::unique_ptr pcap_handle = + std::unique_ptr(new srslte::mac_pcap(srslte::srslte_rat_t::nr)); + TESTASSERT(pcap_handle->open("mac_pcap_nr_test.pcap") == SRSLTE_SUCCESS); + TESTASSERT(pcap_handle->open("mac_pcap_nr_test.pcap") != SRSLTE_SUCCESS); // open again will fail + + std::vector writer_threads; + + for (uint32_t i = 0; i < num_threads; i++) { + writer_threads.push_back(std::thread(write_pcap_nr_thread_function, pcap_handle.get(), tv, num_pdus_per_thread)); + } + + // wait for threads to finish + for (std::thread& thread : writer_threads) { + thread.join(); + } + + TESTASSERT(pcap_handle->close() == SRSLTE_SUCCESS); + TESTASSERT(pcap_handle->close() != 0); // closing twice will fail + + return SRSLTE_SUCCESS; +} + +int main() +{ + TESTASSERT(mac_pcap_eutra_test() == SRSLTE_SUCCESS); + TESTASSERT(mac_pcap_nr_test() == SRSLTE_SUCCESS); +} diff --git a/lib/test/mac/mac_pdu_nr_test.cc b/lib/test/mac/mac_pdu_nr_test.cc index 677215b24..094371693 100644 --- a/lib/test/mac/mac_pdu_nr_test.cc +++ b/lib/test/mac/mac_pdu_nr_test.cc @@ -11,7 +11,7 @@ */ #include "srslte/common/log_filter.h" -#include "srslte/common/mac_nr_pcap.h" +#include "srslte/common/mac_pcap.h" #include "srslte/common/test_common.h" #include "srslte/config.h" #include "srslte/mac/mac_rar_pdu_nr.h" @@ -28,7 +28,7 @@ using namespace srslte; -static std::unique_ptr pcap_handle = nullptr; +static std::unique_ptr pcap_handle = nullptr; int mac_dl_sch_pdu_unpack_and_pack_test1() { @@ -42,7 +42,7 @@ int mac_dl_sch_pdu_unpack_and_pack_test1() uint8_t mac_dl_sch_pdu_1[] = {0x00, 0x08, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; if (pcap_handle) { - pcap_handle->write_dl_crnti(mac_dl_sch_pdu_1, sizeof(mac_dl_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(mac_dl_sch_pdu_1, sizeof(mac_dl_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu; @@ -68,7 +68,7 @@ int mac_dl_sch_pdu_unpack_and_pack_test1() TESTASSERT(memcmp(tx_buffer.msg, mac_dl_sch_pdu_1, tx_buffer.N_bytes) == 0); if (pcap_handle) { - pcap_handle->write_dl_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -94,7 +94,7 @@ int mac_dl_sch_pdu_unpack_test2() uint8_t mac_dl_sch_pdu_2[] = {0x42, 0x00, 0x08, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; if (pcap_handle) { - pcap_handle->write_dl_crnti(mac_dl_sch_pdu_2, sizeof(mac_dl_sch_pdu_2), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(mac_dl_sch_pdu_2, sizeof(mac_dl_sch_pdu_2), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu; @@ -137,7 +137,7 @@ int mac_dl_sch_pdu_pack_test3() TESTASSERT(tx_buffer.N_bytes == 515); if (pcap_handle) { - pcap_handle->write_dl_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -176,7 +176,7 @@ int mac_dl_sch_pdu_pack_test4() TESTASSERT(memcmp(tx_buffer.msg, tv, tx_buffer.N_bytes) == 0); if (pcap_handle) { - pcap_handle->write_dl_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -221,7 +221,7 @@ int mac_dl_sch_pdu_pack_test5() TESTASSERT(memcmp(tx_buffer.msg, tv, tx_buffer.N_bytes) == 0); if (pcap_handle) { - pcap_handle->write_dl_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -244,7 +244,7 @@ int mac_dl_sch_pdu_unpack_test6() uint8_t mac_dl_sch_pdu_2[] = {0x2e, 0x04, 0x11, 0x22, 0x33, 0x44}; if (pcap_handle) { - pcap_handle->write_dl_crnti(mac_dl_sch_pdu_2, sizeof(mac_dl_sch_pdu_2), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_dl_crnti_nr(mac_dl_sch_pdu_2, sizeof(mac_dl_sch_pdu_2), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu; @@ -282,7 +282,7 @@ int mac_rar_pdu_unpack_test7() uint8_t mac_dl_rar_pdu[] = {0x40, 0x05, 0xa0, 0x00, 0x11, 0x46, 0x46, 0x16, 0x00, 0x00, 0x00}; if (pcap_handle) { - pcap_handle->write_dl_ra_rnti(mac_dl_rar_pdu, sizeof(mac_dl_rar_pdu), 0x0016, true, PCAP_TTI); + pcap_handle->write_dl_ra_rnti_nr(mac_dl_rar_pdu, sizeof(mac_dl_rar_pdu), 0x0016, true, PCAP_TTI); } srslte::mac_rar_pdu_nr pdu; @@ -317,7 +317,7 @@ int mac_rar_pdu_unpack_test8() uint8_t mac_dl_rar_pdu[] = {0x40, 0x05}; if (pcap_handle) { - pcap_handle->write_dl_ra_rnti(mac_dl_rar_pdu, sizeof(mac_dl_rar_pdu), 0x0016, true, PCAP_TTI); + pcap_handle->write_dl_ra_rnti_nr(mac_dl_rar_pdu, sizeof(mac_dl_rar_pdu), 0x0016, true, PCAP_TTI); } // unpacking should fail @@ -355,7 +355,7 @@ int mac_ul_sch_pdu_unpack_test1() uint8_t mac_ul_sch_pdu_1[] = {0x3a, 0x11, 0x22, 0x43, 0x00, 0x04, 0x11, 0x22, 0x33, 0x44}; if (pcap_handle) { - pcap_handle->write_ul_crnti(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu(true); @@ -389,7 +389,7 @@ int mac_ul_sch_pdu_unpack_and_pack_test2() uint8_t mac_ul_sch_pdu_1[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; if (pcap_handle) { - pcap_handle->write_ul_crnti(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu(true); @@ -415,7 +415,7 @@ int mac_ul_sch_pdu_unpack_and_pack_test2() TESTASSERT(memcmp(tx_buffer.msg, mac_ul_sch_pdu_1, tx_buffer.N_bytes) == 0); if (pcap_handle) { - pcap_handle->write_ul_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -438,7 +438,7 @@ int mac_ul_sch_pdu_unpack_and_pack_test3() uint8_t mac_ul_sch_pdu_1[] = {0x02, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa}; if (pcap_handle) { - pcap_handle->write_ul_crnti(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu(true); @@ -464,7 +464,7 @@ int mac_ul_sch_pdu_unpack_and_pack_test3() TESTASSERT(memcmp(tx_buffer.msg, mac_ul_sch_pdu_1, tx_buffer.N_bytes) == 0); if (pcap_handle) { - pcap_handle->write_ul_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -504,7 +504,7 @@ int mac_ul_sch_pdu_pack_test4() TESTASSERT(tx_buffer.N_bytes == sizeof(sdu) + 3); if (pcap_handle) { - pcap_handle->write_ul_crnti(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(tx_buffer.msg, tx_buffer.N_bytes, PCAP_CRNTI, true, PCAP_TTI); } srslte::log_filter log("MAC"); @@ -527,7 +527,7 @@ int mac_ul_sch_pdu_unpack_test5() uint8_t mac_ul_sch_pdu_1[] = {0x21, 0x0a, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa}; if (pcap_handle) { - pcap_handle->write_ul_crnti(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); + pcap_handle->write_ul_crnti_nr(mac_ul_sch_pdu_1, sizeof(mac_ul_sch_pdu_1), PCAP_CRNTI, true, PCAP_TTI); } srslte::mac_sch_pdu_nr pdu(true); @@ -540,7 +540,7 @@ int mac_ul_sch_pdu_unpack_test5() int main(int argc, char** argv) { #if PCAP - pcap_handle = std::unique_ptr(new srslte::mac_nr_pcap()); + pcap_handle = std::unique_ptr(new srslte::mac_pcap(srslte::srslte_rat_t::nr)); pcap_handle->open("mac_nr_pdu_test.pcap"); #endif @@ -609,5 +609,9 @@ int main(int argc, char** argv) return SRSLTE_ERROR; } + if (pcap_handle) { + pcap_handle->close(); + } + return SRSLTE_SUCCESS; } diff --git a/srsenb/hdr/stack/mac/mac_nr.h b/srsenb/hdr/stack/mac/mac_nr.h index 7b5fd167b..ca14f921f 100644 --- a/srsenb/hdr/stack/mac/mac_nr.h +++ b/srsenb/hdr/stack/mac/mac_nr.h @@ -15,7 +15,7 @@ #include "srslte/common/block_queue.h" #include "srslte/common/logmap.h" -#include "srslte/common/mac_nr_pcap.h" +#include "srslte/common/mac_pcap.h" #include "srslte/mac/mac_sch_pdu_nr.h" #include "srsenb/hdr/stack/enb_stack_base.h" @@ -81,7 +81,7 @@ private: rlc_interface_mac_nr* rlc_h = nullptr; rrc_interface_mac_nr* rrc_h = nullptr; - std::unique_ptr pcap = nullptr; + std::unique_ptr pcap = nullptr; srslte::log_ref log_h; srslte::byte_buffer_pool* pool = nullptr; mac_nr_args_t args = {}; diff --git a/srsenb/src/stack/enb_stack_lte.cc b/srsenb/src/stack/enb_stack_lte.cc index 73011879b..4665dd591 100644 --- a/srsenb/src/stack/enb_stack_lte.cc +++ b/srsenb/src/stack/enb_stack_lte.cc @@ -36,7 +36,8 @@ enb_stack_lte::enb_stack_lte(srslte::logger* logger_, srslog::sink& log_sink) : gtpu(gtpu_logger), s1ap(&task_sched, s1ap_logger), rrc(&task_sched), - logger(logger_) + logger(logger_), + mac_pcap(srslte_rat_t::lte) { enb_task_queue = task_sched.make_task_queue(); mme_task_queue = task_sched.make_task_queue(); diff --git a/srsenb/src/stack/mac/mac_nr.cc b/srsenb/src/stack/mac/mac_nr.cc index b6afdd932..96dda2980 100644 --- a/srsenb/src/stack/mac/mac_nr.cc +++ b/srsenb/src/stack/mac/mac_nr.cc @@ -54,7 +54,7 @@ int mac_nr::init(const mac_nr_args_t& args_, log_h->set_hex_limit(args.log_hex_limit); if (args.pcap.enable) { - pcap = std::unique_ptr(new srslte::mac_nr_pcap()); + pcap = std::unique_ptr(new srslte::mac_pcap(srslte::srslte_rat_t::nr)); pcap->open(args.pcap.filename); } @@ -113,7 +113,7 @@ void mac_nr::get_dl_config(const uint32_t tti, tx_request.pdus[tx_request.nof_pdus].index = tx_request.nof_pdus; if (pcap) { - pcap->write_dl_si_rnti(sib.payload->msg, sib.payload->N_bytes, 0xffff, 0, tti); + pcap->write_dl_si_rnti_nr(sib.payload->msg, sib.payload->N_bytes, 0xffff, 0, tti); } tx_request.nof_pdus++; @@ -152,11 +152,11 @@ void mac_nr::get_dl_config(const uint32_t tti, tx_request.pdus[tx_request.nof_pdus].index = tx_request.nof_pdus; if (pcap) { - pcap->write_dl_crnti(tx_request.pdus[tx_request.nof_pdus].data[0], - tx_request.pdus[tx_request.nof_pdus].length, - args.rnti, - buffer_index, - tti); + pcap->write_dl_crnti_nr(tx_request.pdus[tx_request.nof_pdus].data[0], + tx_request.pdus[tx_request.nof_pdus].length, + args.rnti, + buffer_index, + tti); } tx_request.nof_pdus++; @@ -191,7 +191,7 @@ int mac_nr::rx_data_indication(stack_interface_phy_nr::rx_data_ind_t& rx_data) // push received PDU on queue if (rx_data.tb != nullptr) { if (pcap) { - pcap->write_ul_crnti(rx_data.tb->msg, rx_data.tb->N_bytes, rx_data.rnti, true, rx_data.tti); + pcap->write_ul_crnti_nr(rx_data.tb->msg, rx_data.tb->N_bytes, rx_data.rnti, true, rx_data.tti); } ue_rx_pdu_queue.push(std::move(rx_data.tb)); } diff --git a/srsue/hdr/stack/mac_nr/mac_nr.h b/srsue/hdr/stack/mac_nr/mac_nr.h index 875d63710..9520afd36 100644 --- a/srsue/hdr/stack/mac_nr/mac_nr.h +++ b/srsue/hdr/stack/mac_nr/mac_nr.h @@ -14,7 +14,7 @@ #define SRSUE_MAC_NR_H #include "srslte/common/block_queue.h" -#include "srslte/common/mac_nr_pcap.h" +#include "srslte/common/mac_pcap.h" #include "srslte/interfaces/mac_interface_types.h" #include "srslte/interfaces/ue_nr_interfaces.h" #include "srslte/mac/mac_sch_pdu_nr.h" @@ -85,7 +85,7 @@ private: rlc_interface_mac* rlc = nullptr; srslte::ext_task_sched_handle task_sched; - std::unique_ptr pcap = nullptr; + std::unique_ptr pcap = nullptr; srslog::basic_logger& logger; srslte::byte_buffer_pool* pool = nullptr; mac_nr_args_t args = {}; diff --git a/srsue/hdr/stack/ue_stack_nr.h b/srsue/hdr/stack/ue_stack_nr.h index ba75623f4..4007dc74b 100644 --- a/srsue/hdr/stack/ue_stack_nr.h +++ b/srsue/hdr/stack/ue_stack_nr.h @@ -28,7 +28,7 @@ #include "srslte/common/buffer_pool.h" #include "srslte/common/log_filter.h" -#include "srslte/common/mac_nr_pcap.h" +#include "srslte/common/mac_pcap.h" #include "srslte/common/multiqueue.h" #include "srslte/common/thread_pool.h" #include "srslte/interfaces/ue_nr_interfaces.h" @@ -112,8 +112,6 @@ private: std::unique_ptr rlc; std::unique_ptr pdcp; - std::unique_ptr mac_pcap; - // RAT-specific interfaces phy_interface_stack_nr* phy = nullptr; gw_interface_stack* gw = nullptr; diff --git a/srsue/src/stack/mac_nr/mac_nr.cc b/srsue/src/stack/mac_nr/mac_nr.cc index 596c8289e..178907831 100644 --- a/srsue/src/stack/mac_nr/mac_nr.cc +++ b/srsue/src/stack/mac_nr/mac_nr.cc @@ -37,7 +37,7 @@ int mac_nr::init(const mac_nr_args_t& args_, phy_interface_mac_nr* phy_, rlc_int // Set up pcap if (args.pcap.enable) { - pcap.reset(new srslte::mac_nr_pcap()); + pcap.reset(new srslte::mac_pcap(srslte::srslte_rat_t::nr)); pcap->open(args.pcap.filename.c_str()); } @@ -164,7 +164,7 @@ void mac_nr::tb_decoded(const uint32_t cc_idx, mac_nr_grant_dl_t& grant) for (uint32_t i = 0; i < SRSLTE_MAX_CODEWORDS; ++i) { if (grant.tb[i] != nullptr) { if (pcap) { - pcap->write_dl_crnti(grant.tb[i]->msg, grant.tb[i]->N_bytes, grant.rnti, true, grant.tti); + pcap->write_dl_crnti_nr(grant.tb[i]->msg, grant.tb[i]->N_bytes, grant.rnti, true, grant.tti); } pdu_queue.push(std::move(grant.tb[i])); } @@ -224,7 +224,7 @@ void mac_nr::get_ul_data(const mac_nr_grant_ul_t& grant, phy_interface_stack_nr: tx_request->tb_len = tx_buffer->N_bytes; if (pcap) { - pcap->write_ul_crnti(tx_request->data, tx_request->tb_len, grant.rnti, grant.pid, grant.tti); + pcap->write_ul_crnti_nr(tx_request->data, tx_request->tb_len, grant.rnti, grant.pid, grant.tti); } } diff --git a/srsue/src/stack/ue_stack_lte.cc b/srsue/src/stack/ue_stack_lte.cc index f7a2b262b..2e364e048 100644 --- a/srsue/src/stack/ue_stack_lte.cc +++ b/srsue/src/stack/ue_stack_lte.cc @@ -46,7 +46,8 @@ ue_stack_lte::ue_stack_lte(srslog::sink& log_sink) : nas(&task_sched), thread("STACK"), task_sched(512, 2, 64), - tti_tprof("tti_tprof", "STCK", TTI_STAT_PERIOD) + tti_tprof("tti_tprof", "STCK", TTI_STAT_PERIOD), + mac_pcap(srslte_rat_t::lte) { ue_task_queue = task_sched.make_task_queue(); gw_queue_id = task_sched.make_task_queue(); @@ -118,8 +119,9 @@ int ue_stack_lte::init(const stack_args_t& args_, srslte::logger* logger_) // Set up pcap if (args.pcap.enable) { - mac_pcap.open(args.pcap.filename.c_str()); - mac.start_pcap(&mac_pcap); + if (mac_pcap.open(args.pcap.filename.c_str()) == SRSLTE_SUCCESS) { + mac.start_pcap(&mac_pcap); + } } if (args.pcap.nas_enable) { nas_pcap.open(args.pcap.nas_filename.c_str()); @@ -141,7 +143,7 @@ int ue_stack_lte::init(const stack_args_t& args_, srslte::logger* logger_) pdcp.init(&rlc, &rrc, gw); nas.init(usim.get(), &rrc, gw, args.nas); #ifdef HAVE_5GNR - mac_nr_args_t mac_nr_args; + mac_nr_args_t mac_nr_args = {}; mac_nr.init(mac_nr_args, nullptr, &rlc); rrc_nr.init( nullptr, &mac_nr, &rlc, &pdcp, gw, &rrc, usim.get(), task_sched.get_timer_handler(), nullptr, args.rrc_nr); diff --git a/srsue/src/stack/ue_stack_nr.cc b/srsue/src/stack/ue_stack_nr.cc index 8ab61b03f..80381b9c2 100644 --- a/srsue/src/stack/ue_stack_nr.cc +++ b/srsue/src/stack/ue_stack_nr.cc @@ -103,10 +103,6 @@ void ue_stack_nr::stop_impl() rlc->stop(); pdcp->stop(); mac->stop(); - - if (mac_pcap != nullptr) { - mac_pcap.reset(); - } } bool ue_stack_nr::switch_on()