mirror of https://github.com/pvnis/srsRAN_4G.git
Merge branch 'next' into agpl_next
commit
5fe9a14aa5
@ -0,0 +1,116 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSRAN_CACHED_ALLOC_H
|
||||||
|
#define SRSRAN_CACHED_ALLOC_H
|
||||||
|
|
||||||
|
#include "../intrusive_list.h"
|
||||||
|
#include "memblock_cache.h"
|
||||||
|
#include <deque>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
namespace srsran {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom Allocator that caches deallocated memory blocks in a stack to be reused in future allocations.
|
||||||
|
* This minimizes the number of new/delete calls, when the rate of insertions/removals match (e.g. a queue)
|
||||||
|
* This allocator is not thread-safe. It assumes the container is being used in a single-threaded environment,
|
||||||
|
* or being mutexed when altered, which is a reasonable assumption
|
||||||
|
* @tparam T object type
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
class cached_alloc : public std::allocator<T>
|
||||||
|
{
|
||||||
|
struct memblock_t : public intrusive_double_linked_list_element<> {
|
||||||
|
explicit memblock_t(size_t sz) : block_size(sz) {}
|
||||||
|
size_t block_size;
|
||||||
|
};
|
||||||
|
const size_t min_n = (sizeof(memblock_t) + sizeof(T) - 1) / sizeof(T);
|
||||||
|
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
~cached_alloc()
|
||||||
|
{
|
||||||
|
while (not free_list.empty()) {
|
||||||
|
memblock_t& b = free_list.front();
|
||||||
|
free_list.pop_front();
|
||||||
|
size_t n = b.block_size;
|
||||||
|
b.~memblock_t();
|
||||||
|
std::allocator<T>::deallocate(reinterpret_cast<T*>(&b), n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cached_alloc() = default;
|
||||||
|
cached_alloc(cached_alloc<T>&& other) noexcept = default;
|
||||||
|
cached_alloc(const cached_alloc<T>& other) noexcept : cached_alloc() {}
|
||||||
|
template <typename U>
|
||||||
|
explicit cached_alloc(const cached_alloc<U>& other) noexcept : cached_alloc()
|
||||||
|
{
|
||||||
|
// start empty, as cached blocks cannot be copied
|
||||||
|
}
|
||||||
|
cached_alloc& operator=(const cached_alloc<T>& other) noexcept { return *this; }
|
||||||
|
cached_alloc& operator=(cached_alloc&& other) noexcept = default;
|
||||||
|
|
||||||
|
T* allocate(size_t n, const void* ptr = nullptr)
|
||||||
|
{
|
||||||
|
size_t req_n = std::max(n, min_n);
|
||||||
|
for (memblock_t& b : free_list) {
|
||||||
|
if (b.block_size == req_n) {
|
||||||
|
free_list.pop(&b);
|
||||||
|
b.~memblock_t();
|
||||||
|
return reinterpret_cast<T*>(&b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::allocator<T>::allocate(req_n, ptr);
|
||||||
|
}
|
||||||
|
void deallocate(T* p, size_t n) noexcept
|
||||||
|
{
|
||||||
|
size_t req_n = std::max(n, min_n);
|
||||||
|
auto* block = reinterpret_cast<memblock_t*>(p);
|
||||||
|
new (block) memblock_t(req_n);
|
||||||
|
free_list.push_front(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
struct rebind {
|
||||||
|
using other = cached_alloc<U>;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
intrusive_double_linked_list<memblock_t> free_list;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace srsran
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
bool operator==(const srsran::cached_alloc<T1>& lhs, const srsran::cached_alloc<T2>& rhs) noexcept
|
||||||
|
{
|
||||||
|
return &lhs == &rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T1, typename T2>
|
||||||
|
bool operator!=(const srsran::cached_alloc<T1>& lhs, const srsran::cached_alloc<T2>& rhs) noexcept
|
||||||
|
{
|
||||||
|
return not(lhs == rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace srsran {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using deque = std::deque<T, cached_alloc<T> >;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using queue = std::queue<T, srsran::deque<T> >;
|
||||||
|
|
||||||
|
} // namespace srsran
|
||||||
|
|
||||||
|
#endif // SRSRAN_CACHED_ALLOC_H
|
@ -0,0 +1,31 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSRAN_NGAP_UTILS_H
|
||||||
|
#define SRSRAN_NGAP_UTILS_H
|
||||||
|
|
||||||
|
#include "asn1_utils.h"
|
||||||
|
#include "ngap.h"
|
||||||
|
/************************
|
||||||
|
* Forward declarations
|
||||||
|
***********************/
|
||||||
|
|
||||||
|
namespace asn1 {
|
||||||
|
namespace ngap_nr {
|
||||||
|
struct rrcestablishment_cause_opts;
|
||||||
|
struct cause_radio_network_opts;
|
||||||
|
using rrcestablishment_cause_e = enumerated<rrcestablishment_cause_opts, true, 1>;
|
||||||
|
using cause_radio_network_e = enumerated<cause_radio_network_opts, true, 2>;
|
||||||
|
} // namespace ngap
|
||||||
|
} // namespace asn1
|
||||||
|
|
||||||
|
#endif // SRSRAN_NGAP_UTILS_H
|
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSRAN_GNB_NGAP_INTERFACES_H
|
||||||
|
#define SRSRAN_GNB_NGAP_INTERFACES_H
|
||||||
|
|
||||||
|
#include "srsran/asn1/ngap_utils.h"
|
||||||
|
|
||||||
|
namespace srsenb {
|
||||||
|
|
||||||
|
struct ngap_args_t {
|
||||||
|
uint32_t gnb_id; // 20-bit id (lsb bits)
|
||||||
|
uint8_t cell_id; // 8-bit cell id
|
||||||
|
uint16_t tac; // 16-bit tac
|
||||||
|
uint16_t mcc; // BCD-coded with 0xF filler
|
||||||
|
uint16_t mnc; // BCD-coded with 0xF filler
|
||||||
|
std::string amf_addr;
|
||||||
|
std::string gtp_bind_addr;
|
||||||
|
std::string gtp_advertise_addr;
|
||||||
|
std::string ngc_bind_addr;
|
||||||
|
std::string gnb_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
// NGAP interface for RRC
|
||||||
|
class ngap_interface_rrc_nr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void initial_ue(uint16_t rnti,
|
||||||
|
uint32_t gnb_cc_idx,
|
||||||
|
asn1::ngap_nr::rrcestablishment_cause_e cause,
|
||||||
|
srsran::unique_byte_buffer_t pdu) = 0;
|
||||||
|
virtual void initial_ue(uint16_t rnti,
|
||||||
|
uint32_t gnb_cc_idx,
|
||||||
|
asn1::ngap_nr::rrcestablishment_cause_e cause,
|
||||||
|
srsran::unique_byte_buffer_t pdu,
|
||||||
|
uint32_t m_tmsi,
|
||||||
|
uint8_t mmec) = 0;
|
||||||
|
|
||||||
|
virtual void write_pdu(uint16_t rnti, srsran::unique_byte_buffer_t pdu) = 0;
|
||||||
|
virtual bool user_exists(uint16_t rnti) = 0;
|
||||||
|
virtual void user_mod(uint16_t old_rnti, uint16_t new_rnti) = 0;
|
||||||
|
virtual bool user_release(uint16_t rnti, asn1::ngap_nr::cause_radio_network_e cause_radio) = 0;
|
||||||
|
virtual bool is_amf_connected() = 0;
|
||||||
|
|
||||||
|
/// TS 36.413, 8.3.1 - Initial Context Setup
|
||||||
|
virtual void ue_ctxt_setup_complete(uint16_t rnti) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace srsenb
|
||||||
|
|
||||||
|
#endif // SRSRAN_GNB_NGAP_INTERFACES_H
|
@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSRAN_GNB_RRC_NR_INTERFACES_H
|
||||||
|
#define SRSRAN_GNB_RRC_NR_INTERFACES_H
|
||||||
|
|
||||||
|
namespace srsenb {
|
||||||
|
|
||||||
|
class rrc_interface_ngap_nr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace srsenb
|
||||||
|
|
||||||
|
#endif // SRSRAN_GNB_RRC_NR_INTERFACES_H
|
@ -0,0 +1,104 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 "srsran/adt/pool/cached_alloc.h"
|
||||||
|
#include "srsran/common/test_common.h"
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
void test_cached_deque_basic_operations()
|
||||||
|
{
|
||||||
|
srsran::deque<int> my_deque;
|
||||||
|
TESTASSERT(my_deque.empty() and my_deque.size() == 0);
|
||||||
|
my_deque.push_front(0);
|
||||||
|
my_deque.push_back(1);
|
||||||
|
TESTASSERT(my_deque.front() == 0 and my_deque.back() == 1);
|
||||||
|
TESTASSERT(my_deque.size() == 2);
|
||||||
|
|
||||||
|
srsran::deque<int> my_deque2(my_deque);
|
||||||
|
TESTASSERT(my_deque == my_deque2);
|
||||||
|
my_deque.clear();
|
||||||
|
TESTASSERT(my_deque != my_deque2);
|
||||||
|
TESTASSERT(my_deque2.size() == 2 and my_deque2.back() == 1);
|
||||||
|
TESTASSERT(my_deque.empty());
|
||||||
|
|
||||||
|
my_deque = my_deque2;
|
||||||
|
TESTASSERT(my_deque == my_deque2);
|
||||||
|
my_deque2.clear();
|
||||||
|
TESTASSERT(my_deque2.empty());
|
||||||
|
|
||||||
|
my_deque2 = std::move(my_deque);
|
||||||
|
TESTASSERT(my_deque.empty() and my_deque2.size() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct C {
|
||||||
|
C() = default;
|
||||||
|
C(C&&) noexcept = default;
|
||||||
|
C(const C&) = delete;
|
||||||
|
C& operator=(C&&) noexcept = default;
|
||||||
|
C& operator=(const C&) = delete;
|
||||||
|
|
||||||
|
bool operator==(const C& other) { return true; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void test_cached_queue_basic_operations()
|
||||||
|
{
|
||||||
|
srsran::queue<C> my_queue;
|
||||||
|
TESTASSERT(my_queue.empty());
|
||||||
|
my_queue.push(C{});
|
||||||
|
TESTASSERT(my_queue.size() == 1);
|
||||||
|
|
||||||
|
srsran::queue<C> my_queue2(std::move(my_queue));
|
||||||
|
TESTASSERT(my_queue2.size() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cached_deque_benchmark()
|
||||||
|
{
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
|
using std::chrono::microseconds;
|
||||||
|
|
||||||
|
srsran::queue<int> my_deque;
|
||||||
|
std::queue<int> std_deque;
|
||||||
|
high_resolution_clock::time_point tp;
|
||||||
|
|
||||||
|
size_t N = 10000000, n_elems = 10;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < n_elems; ++i) {
|
||||||
|
my_deque.push(i);
|
||||||
|
std_deque.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: this benchmark doesnt account for when memory is fragmented
|
||||||
|
tp = high_resolution_clock::now();
|
||||||
|
for (size_t i = n_elems; i < N; ++i) {
|
||||||
|
std_deque.push(i);
|
||||||
|
std_deque.pop();
|
||||||
|
}
|
||||||
|
microseconds t_std = std::chrono::duration_cast<microseconds>(high_resolution_clock::now() - tp);
|
||||||
|
|
||||||
|
tp = high_resolution_clock::now();
|
||||||
|
for (size_t i = n_elems; i < N; ++i) {
|
||||||
|
my_deque.push(i);
|
||||||
|
my_deque.pop();
|
||||||
|
}
|
||||||
|
microseconds t_cached = std::chrono::duration_cast<microseconds>(high_resolution_clock::now() - tp);
|
||||||
|
|
||||||
|
fmt::print("Time elapsed: cached alloc={} usec, std alloc={} usec\n", t_cached.count(), t_std.count());
|
||||||
|
fmt::print("queue sizes: {} {}\n", my_deque.size(), std_deque.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
test_cached_deque_basic_operations();
|
||||||
|
test_cached_queue_basic_operations();
|
||||||
|
cached_deque_benchmark();
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,137 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 "srsran/srslog/srslog.h"
|
||||||
|
#include <atomic>
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
using namespace srslog;
|
||||||
|
|
||||||
|
static constexpr unsigned num_iterations = 4000;
|
||||||
|
static constexpr unsigned num_entries_per_iter = 40;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
/// This helper class checks if there has been context switches between its construction and destruction for the caller
|
||||||
|
/// thread.
|
||||||
|
class context_switch_checker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit context_switch_checker(std::atomic<unsigned>& counter) : counter(counter)
|
||||||
|
{
|
||||||
|
::getrusage(RUSAGE_THREAD, &before);
|
||||||
|
}
|
||||||
|
|
||||||
|
~context_switch_checker()
|
||||||
|
{
|
||||||
|
::rusage after{};
|
||||||
|
::getrusage(RUSAGE_THREAD, &after);
|
||||||
|
unsigned diff = (after.ru_nvcsw - before.ru_nvcsw) + (after.ru_nivcsw - before.ru_nivcsw);
|
||||||
|
if (diff) {
|
||||||
|
counter.fetch_add(diff, std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
::rusage before{};
|
||||||
|
std::atomic<unsigned>& counter;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
/// Busy waits in the calling thread for the specified amount of time.
|
||||||
|
static void busy_wait(std::chrono::milliseconds interval)
|
||||||
|
{
|
||||||
|
auto begin = std::chrono::steady_clock::now();
|
||||||
|
auto end = begin + interval;
|
||||||
|
|
||||||
|
while (std::chrono::steady_clock::now() < end) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Worker function used for each thread of the benchmark to generate and measure the time taken for each log entry.
|
||||||
|
static void run_thread(log_channel& c, std::vector<uint64_t>& results, std::atomic<unsigned>& ctx_counter)
|
||||||
|
{
|
||||||
|
for (unsigned iter = 0; iter != num_iterations; ++iter) {
|
||||||
|
context_switch_checker ctx_checker(ctx_counter);
|
||||||
|
|
||||||
|
auto begin = std::chrono::steady_clock::now();
|
||||||
|
for (unsigned entry_num = 0; entry_num != num_entries_per_iter; ++entry_num) {
|
||||||
|
double d = entry_num;
|
||||||
|
c("SRSLOG latency benchmark: int: %u, double: %f, string: %s", iter, d, "test");
|
||||||
|
}
|
||||||
|
auto end = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
|
results.push_back(std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() / num_entries_per_iter);
|
||||||
|
|
||||||
|
busy_wait(std::chrono::milliseconds(4));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function runs the latency benchmark generating log entries using the specified number of threads.
|
||||||
|
static void benchmark(unsigned num_threads)
|
||||||
|
{
|
||||||
|
std::vector<std::vector<uint64_t> > thread_results;
|
||||||
|
thread_results.resize(num_threads);
|
||||||
|
for (auto& v : thread_results) {
|
||||||
|
v.reserve(num_iterations);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& s = srslog::fetch_file_sink("srslog_latency_benchmark.txt");
|
||||||
|
auto& channel = srslog::fetch_log_channel("bench", s, {});
|
||||||
|
|
||||||
|
srslog::init();
|
||||||
|
|
||||||
|
std::vector<std::thread> workers;
|
||||||
|
workers.reserve(num_threads);
|
||||||
|
|
||||||
|
std::atomic<unsigned> ctx_counter(0);
|
||||||
|
for (unsigned i = 0; i != num_threads; ++i) {
|
||||||
|
workers.emplace_back(run_thread, std::ref(channel), std::ref(thread_results[i]), std::ref(ctx_counter));
|
||||||
|
}
|
||||||
|
for (auto& w : workers) {
|
||||||
|
w.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint64_t> results;
|
||||||
|
results.reserve(num_threads * num_iterations);
|
||||||
|
for (const auto& v : thread_results) {
|
||||||
|
results.insert(results.end(), v.begin(), v.end());
|
||||||
|
}
|
||||||
|
std::sort(results.begin(), results.end());
|
||||||
|
|
||||||
|
fmt::print("SRSLOG Frontend Latency Benchmark - logging with {} thread{}\n"
|
||||||
|
"All values in nanoseconds\n"
|
||||||
|
"Percentiles: | 50th | 75th | 90th | 99th | 99.9th | Worst |\n"
|
||||||
|
" |{:6}|{:6}|{:6}|{:6}|{:8}|{:7}|\n"
|
||||||
|
"Context switches: {} in {} of generated entries\n\n",
|
||||||
|
num_threads,
|
||||||
|
(num_threads > 1) ? "s" : "",
|
||||||
|
results[static_cast<size_t>(results.size() * 0.5)],
|
||||||
|
results[static_cast<size_t>(results.size() * 0.75)],
|
||||||
|
results[static_cast<size_t>(results.size() * 0.9)],
|
||||||
|
results[static_cast<size_t>(results.size() * 0.99)],
|
||||||
|
results[static_cast<size_t>(results.size() * 0.999)],
|
||||||
|
results.back(),
|
||||||
|
ctx_counter,
|
||||||
|
num_threads * num_iterations * num_entries_per_iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
for (auto n : {1, 2, 4}) {
|
||||||
|
benchmark(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -0,0 +1,234 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSRAN_RRC_PAGING_H
|
||||||
|
#define SRSRAN_RRC_PAGING_H
|
||||||
|
|
||||||
|
#include "srsran/adt/span.h"
|
||||||
|
#include "srsran/asn1/rrc/paging.h"
|
||||||
|
#include "srsran/common/tti_point.h"
|
||||||
|
|
||||||
|
namespace srsenb {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class that handles the buffering of paging records and encoding of PCCH messages. It's thread-safe
|
||||||
|
*/
|
||||||
|
class paging_manager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
paging_manager(uint32_t default_paging_cycle_, uint32_t nb_) :
|
||||||
|
T(default_paging_cycle_),
|
||||||
|
Nb(T * nb_),
|
||||||
|
N(std::min(T, Nb)),
|
||||||
|
Ns(std::max(nb_, 1u)),
|
||||||
|
pending_paging(T),
|
||||||
|
logger(srslog::fetch_basic_logger("RRC"))
|
||||||
|
{
|
||||||
|
for (auto& sfn_pcch_msgs : pending_paging) {
|
||||||
|
for (pcch_info& pcch : sfn_pcch_msgs) {
|
||||||
|
pcch.pcch_msg.msg.set_c1().paging().paging_record_list_present = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// add new IMSI paging record
|
||||||
|
bool add_imsi_paging(uint32_t ueid, srsran::const_byte_span imsi);
|
||||||
|
|
||||||
|
/// add new TMSI paging record
|
||||||
|
bool add_tmsi_paging(uint32_t ueid, uint8_t mmec, srsran::const_byte_span m_tmsi);
|
||||||
|
|
||||||
|
/// Get how many bytes are required to fit the pending PCCH message.
|
||||||
|
size_t pending_pcch_bytes(tti_point tti_tx_dl);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke "callable" for PCCH indexed by tti_tx_dl in a mutexed context.
|
||||||
|
* Callable signature is bool(const_byte_span pdu, const pcch_msg& msg, bool is_first_tx)
|
||||||
|
* - "is_first_tx" tells if the PDU hasn't been transmitted yet.
|
||||||
|
* - the return should be true if the PDU is being transmitted, and false otherwise
|
||||||
|
*/
|
||||||
|
template <typename Callable>
|
||||||
|
bool read_pdu_pcch(tti_point tti_tx_dl, const Callable& callable);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct pcch_info {
|
||||||
|
tti_point tti_tx_dl;
|
||||||
|
asn1::rrc::pcch_msg_s pcch_msg;
|
||||||
|
srsran::unique_byte_buffer_t pdu;
|
||||||
|
|
||||||
|
bool is_tx() const { return tti_tx_dl.is_valid(); }
|
||||||
|
bool empty() const { return pdu == nullptr; }
|
||||||
|
void clear()
|
||||||
|
{
|
||||||
|
tti_tx_dl = tti_point();
|
||||||
|
pcch_msg.msg.c1().paging().paging_record_list.clear();
|
||||||
|
pdu.reset();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const static size_t nof_paging_subframes = 4;
|
||||||
|
|
||||||
|
bool add_paging_record(uint32_t ueid, const asn1::rrc::paging_record_s& paging_record);
|
||||||
|
pcch_info& get_pcch_info(tti_point tti_tx_dl)
|
||||||
|
{
|
||||||
|
return pending_paging[tti_tx_dl.sfn() % T][get_sf_idx_key(tti_tx_dl.sf_idx())];
|
||||||
|
}
|
||||||
|
const pcch_info& get_pcch_info(tti_point tti_tx_dl) const
|
||||||
|
{
|
||||||
|
return pending_paging[tti_tx_dl.sfn() % T][get_sf_idx_key(tti_tx_dl.sf_idx())];
|
||||||
|
}
|
||||||
|
static int get_sf_idx_key(uint32_t sf_idx)
|
||||||
|
{
|
||||||
|
switch (sf_idx) {
|
||||||
|
case 0:
|
||||||
|
return 0;
|
||||||
|
case 4:
|
||||||
|
return 1;
|
||||||
|
case 5:
|
||||||
|
return 2;
|
||||||
|
case 9:
|
||||||
|
return 3;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// args
|
||||||
|
uint32_t T;
|
||||||
|
uint32_t Nb;
|
||||||
|
uint32_t N;
|
||||||
|
uint32_t Ns;
|
||||||
|
srslog::basic_logger& logger;
|
||||||
|
|
||||||
|
mutable std::array<std::mutex, nof_paging_subframes> sf_idx_mutex;
|
||||||
|
std::vector<std::array<pcch_info, nof_paging_subframes> > pending_paging;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool paging_manager::add_imsi_paging(uint32_t ueid, srsran::const_byte_span imsi)
|
||||||
|
{
|
||||||
|
asn1::rrc::paging_record_s paging_elem;
|
||||||
|
paging_elem.ue_id.set_imsi().resize(imsi.size());
|
||||||
|
std::copy(imsi.begin(), imsi.end(), paging_elem.ue_id.imsi().begin());
|
||||||
|
paging_elem.cn_domain = asn1::rrc::paging_record_s::cn_domain_e_::ps;
|
||||||
|
return add_paging_record(ueid, paging_elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool paging_manager::add_tmsi_paging(uint32_t ueid, uint8_t mmec, srsran::const_byte_span m_tmsi)
|
||||||
|
{
|
||||||
|
asn1::rrc::paging_record_s paging_elem;
|
||||||
|
paging_elem.ue_id.set_s_tmsi().mmec.from_number(mmec);
|
||||||
|
uint32_t m_tmsi_val = 0;
|
||||||
|
for (uint32_t i = 0; i < m_tmsi.size(); i++) {
|
||||||
|
m_tmsi_val |= m_tmsi[i] << (8u * (m_tmsi.size() - i - 1u));
|
||||||
|
}
|
||||||
|
paging_elem.ue_id.s_tmsi().m_tmsi.from_number(m_tmsi_val);
|
||||||
|
paging_elem.cn_domain = asn1::rrc::paging_record_s::cn_domain_e_::ps;
|
||||||
|
return add_paging_record(ueid, paging_elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \remark See TS 36.304, Section 7
|
||||||
|
bool paging_manager::add_paging_record(uint32_t ueid, const asn1::rrc::paging_record_s& paging_record)
|
||||||
|
{
|
||||||
|
constexpr static const int sf_pattern[4][4] = {{9, 4, -1, 0}, {-1, 9, -1, 4}, {-1, -1, -1, 5}, {-1, -1, -1, 9}};
|
||||||
|
|
||||||
|
ueid = ((uint32_t)ueid) % 1024;
|
||||||
|
uint32_t i_s = (ueid / N) % Ns;
|
||||||
|
|
||||||
|
int sf_idx = sf_pattern[i_s % 4][(Ns - 1) % 4];
|
||||||
|
if (sf_idx < 0) {
|
||||||
|
logger.error("SF pattern is N/A for Ns=%d, i_s=%d, imsi_decimal=%d", Ns, i_s, ueid);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t sfn_cycle_idx = (T / N) * (ueid % N);
|
||||||
|
pcch_info& pending_pcch = pending_paging[sfn_cycle_idx][get_sf_idx_key(sf_idx)];
|
||||||
|
auto& record_list = pending_pcch.pcch_msg.msg.c1().paging().paging_record_list;
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(sf_idx_mutex[get_sf_idx_key(sf_idx)]);
|
||||||
|
|
||||||
|
if (record_list.size() >= ASN1_RRC_MAX_PAGE_REC) {
|
||||||
|
logger.warning("Failed to add new paging record for ueid=%d. Cause: no paging record space left.", ueid);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pending_pcch.pdu == nullptr) {
|
||||||
|
pending_pcch.pdu = srsran::make_byte_buffer();
|
||||||
|
if (pending_pcch.pdu == nullptr) {
|
||||||
|
logger.warning("Failed to add new paging record for ueid=%d. Cause: No buffers available", ueid);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record_list.push_back(paging_record);
|
||||||
|
|
||||||
|
asn1::bit_ref bref(pending_pcch.pdu->msg, pending_pcch.pdu->get_tailroom());
|
||||||
|
if (pending_pcch.pcch_msg.pack(bref) == asn1::SRSASN_ERROR_ENCODE_FAIL) {
|
||||||
|
logger.error("Failed to pack PCCH message");
|
||||||
|
pending_pcch.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pending_pcch.pdu->N_bytes = (uint32_t)bref.distance_bytes();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t paging_manager::pending_pcch_bytes(tti_point tti_tx_dl)
|
||||||
|
{
|
||||||
|
int sf_key = get_sf_idx_key(tti_tx_dl.sf_idx());
|
||||||
|
if (sf_key < 0) {
|
||||||
|
// tti_tx_dl is not in a paging subframe
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(sf_idx_mutex[sf_key]);
|
||||||
|
|
||||||
|
// clear old PCCH that has been transmitted at this point
|
||||||
|
pcch_info& old_pcch = get_pcch_info(tti_tx_dl - SRSRAN_NOF_SF_X_FRAME);
|
||||||
|
if (not old_pcch.empty()) {
|
||||||
|
old_pcch.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
const pcch_info& pending_pcch = get_pcch_info(tti_tx_dl);
|
||||||
|
if (pending_pcch.empty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return pending_pcch.pdu->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Callable>
|
||||||
|
bool paging_manager::read_pdu_pcch(tti_point tti_tx_dl, const Callable& func)
|
||||||
|
{
|
||||||
|
int sf_key = get_sf_idx_key(tti_tx_dl.sf_idx());
|
||||||
|
if (sf_key < 0) {
|
||||||
|
logger.warning("%s was called for tti=%d, which is not a paging subframe index", __FUNCTION__, tti_tx_dl.to_uint());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pcch_info& pending_pcch = get_pcch_info(tti_tx_dl);
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(sf_idx_mutex[get_sf_idx_key(tti_tx_dl.sf_idx())]);
|
||||||
|
|
||||||
|
if (pending_pcch.empty()) {
|
||||||
|
logger.warning("read_pdu_pdcch(...) called for tti=%d, but there is no pending pcch message", tti_tx_dl.to_uint());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call callable for existing PCCH pdu
|
||||||
|
if (func(*pending_pcch.pdu, pending_pcch.pcch_msg, pending_pcch.tti_tx_dl.is_valid())) {
|
||||||
|
pending_pcch.tti_tx_dl = tti_tx_dl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace srsenb
|
||||||
|
|
||||||
|
#endif // SRSRAN_RRC_PAGING_H
|
@ -0,0 +1,134 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 SRSENB_NGAP_H
|
||||||
|
#define SRSENB_NGAP_H
|
||||||
|
|
||||||
|
#include "srsenb/hdr/common/common_enb.h"
|
||||||
|
#include "srsran/adt/circular_map.h"
|
||||||
|
#include "srsran/asn1/asn1_utils.h"
|
||||||
|
#include "srsran/asn1/ngap.h"
|
||||||
|
#include "srsran/common/bcd_helpers.h"
|
||||||
|
#include "srsran/common/buffer_pool.h"
|
||||||
|
#include "srsran/common/common.h"
|
||||||
|
#include "srsran/common/network_utils.h"
|
||||||
|
#include "srsran/common/stack_procedure.h"
|
||||||
|
#include "srsran/common/standard_streams.h"
|
||||||
|
#include "srsran/common/task_scheduler.h"
|
||||||
|
#include "srsran/common/threads.h"
|
||||||
|
#include "srsran/interfaces/gnb_ngap_interfaces.h"
|
||||||
|
#include "srsran/interfaces/gnb_rrc_nr_interfaces.h"
|
||||||
|
#include "srsran/srslog/srslog.h"
|
||||||
|
|
||||||
|
namespace srsenb {
|
||||||
|
class ngap : public ngap_interface_rrc_nr
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ngap(srsran::task_sched_handle task_sched_,
|
||||||
|
srslog::basic_logger& logger,
|
||||||
|
srsran::socket_manager_itf* rx_socket_handler);
|
||||||
|
int init(ngap_args_t args_, rrc_interface_ngap_nr* rrc_);
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
// RRC NR interface
|
||||||
|
void initial_ue(uint16_t rnti,
|
||||||
|
uint32_t gnb_cc_idx,
|
||||||
|
asn1::ngap_nr::rrcestablishment_cause_e cause,
|
||||||
|
srsran::unique_byte_buffer_t pdu){};
|
||||||
|
void initial_ue(uint16_t rnti,
|
||||||
|
uint32_t gnb_cc_idx,
|
||||||
|
asn1::ngap_nr::rrcestablishment_cause_e cause,
|
||||||
|
srsran::unique_byte_buffer_t pdu,
|
||||||
|
uint32_t m_tmsi,
|
||||||
|
uint8_t mmec){};
|
||||||
|
void write_pdu(uint16_t rnti, srsran::unique_byte_buffer_t pdu){};
|
||||||
|
bool user_exists(uint16_t rnti) { return true; };
|
||||||
|
void user_mod(uint16_t old_rnti, uint16_t new_rnti){};
|
||||||
|
bool user_release(uint16_t rnti, asn1::ngap_nr::cause_radio_network_e cause_radio) { return true; };
|
||||||
|
bool is_amf_connected();
|
||||||
|
|
||||||
|
/// TS 36.413, 8.3.1 - Initial Context Setup
|
||||||
|
void ue_ctxt_setup_complete(uint16_t rnti){};
|
||||||
|
|
||||||
|
// Stack interface
|
||||||
|
bool
|
||||||
|
handle_amf_rx_msg(srsran::unique_byte_buffer_t pdu, const sockaddr_in& from, const sctp_sndrcvinfo& sri, int flags);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const int AMF_PORT = 38412;
|
||||||
|
static const int ADDR_FAMILY = AF_INET;
|
||||||
|
static const int SOCK_TYPE = SOCK_STREAM;
|
||||||
|
static const int PROTO = IPPROTO_SCTP;
|
||||||
|
static const int PPID = 60;
|
||||||
|
static const int NONUE_STREAM_ID = 0;
|
||||||
|
|
||||||
|
// args
|
||||||
|
rrc_interface_ngap_nr* rrc = nullptr;
|
||||||
|
ngap_args_t args;
|
||||||
|
srslog::basic_logger& logger;
|
||||||
|
srsran::task_sched_handle task_sched;
|
||||||
|
srsran::task_queue_handle amf_task_queue;
|
||||||
|
srsran::socket_manager_itf* rx_socket_handler;
|
||||||
|
|
||||||
|
srsran::unique_socket amf_socket;
|
||||||
|
struct sockaddr_in amf_addr = {}; // AMF address
|
||||||
|
bool amf_connected = false;
|
||||||
|
bool running = false;
|
||||||
|
uint32_t next_enb_ue_ngap_id = 1; // Next ENB-side UE identifier
|
||||||
|
uint16_t next_ue_stream_id = 1; // Next UE SCTP stream identifier
|
||||||
|
srsran::unique_timer amf_connect_timer, ngsetup_timeout;
|
||||||
|
|
||||||
|
// Protocol IEs sent with every UL NGAP message
|
||||||
|
asn1::ngap_nr::tai_s tai;
|
||||||
|
asn1::ngap_nr::nr_cgi_s nr_cgi;
|
||||||
|
|
||||||
|
asn1::ngap_nr::ng_setup_resp_s ngsetupresponse;
|
||||||
|
|
||||||
|
// procedures
|
||||||
|
class ng_setup_proc_t
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
struct ngsetupresult {
|
||||||
|
bool success = false;
|
||||||
|
enum class cause_t { timeout, failure } cause;
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit ng_setup_proc_t(ngap* ngap_) : ngap_ptr(ngap_) {}
|
||||||
|
srsran::proc_outcome_t init();
|
||||||
|
srsran::proc_outcome_t step() { return srsran::proc_outcome_t::yield; }
|
||||||
|
srsran::proc_outcome_t react(const ngsetupresult& event);
|
||||||
|
void then(const srsran::proc_state_t& result) const;
|
||||||
|
const char* name() const { return "AMF Connection"; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
srsran::proc_outcome_t start_amf_connection();
|
||||||
|
|
||||||
|
ngap* ngap_ptr = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
void build_tai_cgi();
|
||||||
|
bool connect_amf();
|
||||||
|
bool setup_ng();
|
||||||
|
bool sctp_send_ngap_pdu(const asn1::ngap_nr::ngap_pdu_c& tx_pdu, uint32_t rnti, const char* procedure_name);
|
||||||
|
|
||||||
|
bool handle_ngap_rx_pdu(srsran::byte_buffer_t* pdu);
|
||||||
|
bool handle_successfuloutcome(const asn1::ngap_nr::successful_outcome_s& msg);
|
||||||
|
|
||||||
|
bool handle_ngsetupresponse(const asn1::ngap_nr::ng_setup_resp_s& msg);
|
||||||
|
|
||||||
|
srsran::proc_t<ng_setup_proc_t> ngsetup_proc;
|
||||||
|
|
||||||
|
std::string get_cause(const asn1::ngap_nr::cause_c& c);
|
||||||
|
void log_ngap_msg(const asn1::ngap_nr::ngap_pdu_c& msg, srsran::const_span<uint8_t> sdu, bool is_rx);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace srsenb
|
||||||
|
#endif
|
@ -0,0 +1,405 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* \section COPYRIGHT
|
||||||
|
*
|
||||||
|
* Copyright 2013-2021 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 "srsenb/hdr/stack/upper/ngap.h"
|
||||||
|
|
||||||
|
#define procError(fmt, ...) ngap_ptr->logger.error("Proc \"%s\" - " fmt, name(), ##__VA_ARGS__)
|
||||||
|
#define procWarning(fmt, ...) ngap_ptr->logger.warning("Proc \"%s\" - " fmt, name(), ##__VA_ARGS__)
|
||||||
|
#define procInfo(fmt, ...) ngap_ptr->logger.info("Proc \"%s\" - " fmt, name(), ##__VA_ARGS__)
|
||||||
|
|
||||||
|
using namespace asn1::ngap_nr;
|
||||||
|
|
||||||
|
namespace srsenb {
|
||||||
|
/*********************************************************
|
||||||
|
* AMF Connection
|
||||||
|
*********************************************************/
|
||||||
|
|
||||||
|
srsran::proc_outcome_t ngap::ng_setup_proc_t::init()
|
||||||
|
{
|
||||||
|
procInfo("Starting new AMF connection.");
|
||||||
|
return start_amf_connection();
|
||||||
|
}
|
||||||
|
|
||||||
|
srsran::proc_outcome_t ngap::ng_setup_proc_t::start_amf_connection()
|
||||||
|
{
|
||||||
|
if (not ngap_ptr->running) {
|
||||||
|
procInfo("NGAP is not running anymore.");
|
||||||
|
return srsran::proc_outcome_t::error;
|
||||||
|
}
|
||||||
|
if (ngap_ptr->amf_connected) {
|
||||||
|
procInfo("gNB NGAP is already connected to AMF");
|
||||||
|
return srsran::proc_outcome_t::success;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (not ngap_ptr->connect_amf()) {
|
||||||
|
procInfo("Could not connect to AMF");
|
||||||
|
return srsran::proc_outcome_t::error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (not ngap_ptr->setup_ng()) {
|
||||||
|
procError("NG setup failed. Exiting...");
|
||||||
|
srsran::console("NG setup failed\n");
|
||||||
|
ngap_ptr->running = false;
|
||||||
|
return srsran::proc_outcome_t::error;
|
||||||
|
}
|
||||||
|
|
||||||
|
ngap_ptr->ngsetup_timeout.run();
|
||||||
|
procInfo("NGSetupRequest sent. Waiting for response...");
|
||||||
|
return srsran::proc_outcome_t::yield;
|
||||||
|
}
|
||||||
|
|
||||||
|
srsran::proc_outcome_t ngap::ng_setup_proc_t::react(const srsenb::ngap::ng_setup_proc_t::ngsetupresult& event)
|
||||||
|
{
|
||||||
|
if (ngap_ptr->ngsetup_timeout.is_running()) {
|
||||||
|
ngap_ptr->ngsetup_timeout.stop();
|
||||||
|
}
|
||||||
|
if (event.success) {
|
||||||
|
procInfo("NGSetup procedure completed successfully");
|
||||||
|
return srsran::proc_outcome_t::success;
|
||||||
|
}
|
||||||
|
procError("NGSetup failed.");
|
||||||
|
srsran::console("NGsetup failed\n");
|
||||||
|
return srsran::proc_outcome_t::error;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ngap::ng_setup_proc_t::then(const srsran::proc_state_t& result) const
|
||||||
|
{
|
||||||
|
if (result.is_error()) {
|
||||||
|
procInfo("Failed to initiate NG connection. Attempting reconnection in %d seconds",
|
||||||
|
ngap_ptr->amf_connect_timer.duration() / 1000);
|
||||||
|
srsran::console("Failed to initiate NG connection. Attempting reconnection in %d seconds\n",
|
||||||
|
ngap_ptr->amf_connect_timer.duration() / 1000);
|
||||||
|
ngap_ptr->rx_socket_handler->remove_socket(ngap_ptr->amf_socket.get_socket());
|
||||||
|
ngap_ptr->amf_socket.close();
|
||||||
|
procInfo("NGAP socket closed.");
|
||||||
|
ngap_ptr->amf_connect_timer.run();
|
||||||
|
// Try again with in 10 seconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************
|
||||||
|
* NGAP class
|
||||||
|
*********************************************************/
|
||||||
|
|
||||||
|
ngap::ngap(srsran::task_sched_handle task_sched_,
|
||||||
|
srslog::basic_logger& logger,
|
||||||
|
srsran::socket_manager_itf* rx_socket_handler_) :
|
||||||
|
ngsetup_proc(this), logger(logger), task_sched(task_sched_), rx_socket_handler(rx_socket_handler_)
|
||||||
|
{
|
||||||
|
amf_task_queue = task_sched.make_task_queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
int ngap::init(ngap_args_t args_, rrc_interface_ngap_nr* rrc_)
|
||||||
|
{
|
||||||
|
rrc = rrc_;
|
||||||
|
args = args_;
|
||||||
|
|
||||||
|
build_tai_cgi();
|
||||||
|
|
||||||
|
// Setup AMF reconnection timer
|
||||||
|
amf_connect_timer = task_sched.get_unique_timer();
|
||||||
|
auto amf_connect_run = [this](uint32_t tid) {
|
||||||
|
if (ngsetup_proc.is_busy()) {
|
||||||
|
logger.error("Failed to initiate NGSetup procedure.");
|
||||||
|
}
|
||||||
|
ngsetup_proc.launch();
|
||||||
|
};
|
||||||
|
amf_connect_timer.set(10000, amf_connect_run);
|
||||||
|
// Setup NGSetup timeout
|
||||||
|
ngsetup_timeout = task_sched.get_unique_timer();
|
||||||
|
uint32_t ngsetup_timeout_val = 5000;
|
||||||
|
ngsetup_timeout.set(ngsetup_timeout_val, [this](uint32_t tid) {
|
||||||
|
ng_setup_proc_t::ngsetupresult res;
|
||||||
|
res.success = false;
|
||||||
|
res.cause = ng_setup_proc_t::ngsetupresult::cause_t::timeout;
|
||||||
|
ngsetup_proc.trigger(res);
|
||||||
|
});
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
// starting AMF connection
|
||||||
|
if (not ngsetup_proc.launch()) {
|
||||||
|
logger.error("Failed to initiate NGSetup procedure.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return SRSRAN_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ngap::stop()
|
||||||
|
{
|
||||||
|
running = false;
|
||||||
|
amf_socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ngap::is_amf_connected()
|
||||||
|
{
|
||||||
|
return amf_connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate common NGAP protocol IEs from config args
|
||||||
|
void ngap::build_tai_cgi()
|
||||||
|
{
|
||||||
|
uint32_t plmn;
|
||||||
|
|
||||||
|
// TAI
|
||||||
|
srsran::s1ap_mccmnc_to_plmn(args.mcc, args.mnc, &plmn);
|
||||||
|
tai.plmn_id.from_number(plmn);
|
||||||
|
|
||||||
|
tai.tac.from_number(args.tac);
|
||||||
|
|
||||||
|
// nr_cgi
|
||||||
|
nr_cgi.plmn_id.from_number(plmn);
|
||||||
|
// TODO Check how to build nr cell id
|
||||||
|
nr_cgi.nrcell_id.from_number((uint32_t)(args.gnb_id << 8) | args.cell_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
/* NGAP message handlers
|
||||||
|
********************************************************************************/
|
||||||
|
bool ngap::handle_amf_rx_msg(srsran::unique_byte_buffer_t pdu,
|
||||||
|
const sockaddr_in& from,
|
||||||
|
const sctp_sndrcvinfo& sri,
|
||||||
|
int flags)
|
||||||
|
{
|
||||||
|
// Handle Notification Case
|
||||||
|
if (flags & MSG_NOTIFICATION) {
|
||||||
|
// Received notification
|
||||||
|
union sctp_notification* notification = (union sctp_notification*)pdu->msg;
|
||||||
|
logger.debug("SCTP Notification %d", notification->sn_header.sn_type);
|
||||||
|
if (notification->sn_header.sn_type == SCTP_SHUTDOWN_EVENT) {
|
||||||
|
logger.info("SCTP Association Shutdown. Association: %d", sri.sinfo_assoc_id);
|
||||||
|
srsran::console("SCTP Association Shutdown. Association: %d\n", sri.sinfo_assoc_id);
|
||||||
|
rx_socket_handler->remove_socket(amf_socket.get_socket());
|
||||||
|
amf_socket.close();
|
||||||
|
} else if (notification->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE &&
|
||||||
|
notification->sn_paddr_change.spc_state == SCTP_ADDR_UNREACHABLE) {
|
||||||
|
logger.info("SCTP peer addres unreachable. Association: %d", sri.sinfo_assoc_id);
|
||||||
|
srsran::console("SCTP peer address unreachable. Association: %d\n", sri.sinfo_assoc_id);
|
||||||
|
rx_socket_handler->remove_socket(amf_socket.get_socket());
|
||||||
|
amf_socket.close();
|
||||||
|
}
|
||||||
|
} else if (pdu->N_bytes == 0) {
|
||||||
|
logger.error("SCTP return 0 bytes. Closing socket");
|
||||||
|
amf_socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart MME connection procedure if we lost connection
|
||||||
|
if (not amf_socket.is_open()) {
|
||||||
|
amf_connected = false;
|
||||||
|
if (ngsetup_proc.is_busy()) {
|
||||||
|
logger.error("Failed to initiate MME connection procedure, as it is already running.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ngsetup_proc.launch();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_ngap_rx_pdu(pdu.get());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ngap::handle_ngap_rx_pdu(srsran::byte_buffer_t* pdu)
|
||||||
|
{
|
||||||
|
// Save message to PCAP
|
||||||
|
// if (pcap != nullptr) {
|
||||||
|
// pcap->write_ngap(pdu->msg, pdu->N_bytes);
|
||||||
|
// }
|
||||||
|
|
||||||
|
ngap_pdu_c rx_pdu;
|
||||||
|
asn1::cbit_ref bref(pdu->msg, pdu->N_bytes);
|
||||||
|
|
||||||
|
if (rx_pdu.unpack(bref) != asn1::SRSASN_SUCCESS) {
|
||||||
|
logger.error(pdu->msg, pdu->N_bytes, "Failed to unpack received PDU");
|
||||||
|
cause_c cause;
|
||||||
|
cause.set_protocol().value = cause_protocol_opts::transfer_syntax_error;
|
||||||
|
// send_error_indication(cause);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// log_ngap_msg(rx_pdu, srsran::make_span(*pdu), true);
|
||||||
|
|
||||||
|
switch (rx_pdu.type().value) {
|
||||||
|
// case ngap_pdu_c::types_opts::init_msg:
|
||||||
|
// return handle_initiatingmessage(rx_pdu.init_msg());
|
||||||
|
case ngap_pdu_c::types_opts::successful_outcome:
|
||||||
|
return handle_successfuloutcome(rx_pdu.successful_outcome());
|
||||||
|
// case ngap_pdu_c::types_opts::unsuccessful_outcome:
|
||||||
|
// return handle_unsuccessfuloutcome(rx_pdu.unsuccessful_outcome());
|
||||||
|
default:
|
||||||
|
logger.error("Unhandled PDU type %d", rx_pdu.type().value);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ngap::handle_successfuloutcome(const successful_outcome_s& msg)
|
||||||
|
{
|
||||||
|
switch (msg.value.type().value) {
|
||||||
|
case ngap_elem_procs_o::successful_outcome_c::types_opts::ng_setup_resp:
|
||||||
|
return handle_ngsetupresponse(msg.value.ng_setup_resp());
|
||||||
|
default:
|
||||||
|
logger.error("Unhandled successful outcome message: %s", msg.value.type().to_string());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ngap::handle_ngsetupresponse(const asn1::ngap_nr::ng_setup_resp_s& msg)
|
||||||
|
{
|
||||||
|
ngsetupresponse = msg;
|
||||||
|
amf_connected = true;
|
||||||
|
ng_setup_proc_t::ngsetupresult res;
|
||||||
|
res.success = true;
|
||||||
|
ngsetup_proc.trigger(res);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
/* NGAP connection helpers
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
bool ngap::connect_amf()
|
||||||
|
{
|
||||||
|
using namespace srsran::net_utils;
|
||||||
|
logger.info("Connecting to AMF %s:%d", args.amf_addr.c_str(), int(AMF_PORT));
|
||||||
|
|
||||||
|
// Init SCTP socket and bind it
|
||||||
|
if (not sctp_init_client(&amf_socket, socket_type::seqpacket, args.ngc_bind_addr.c_str())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
logger.info("SCTP socket opened. fd=%d", amf_socket.fd());
|
||||||
|
|
||||||
|
// Connect to the AMF address
|
||||||
|
if (not amf_socket.connect_to(args.amf_addr.c_str(), AMF_PORT, &amf_addr)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
logger.info("SCTP socket connected with AMF. fd=%d", amf_socket.fd());
|
||||||
|
|
||||||
|
// Assign a handler to rx AMF packets
|
||||||
|
auto rx_callback =
|
||||||
|
[this](srsran::unique_byte_buffer_t pdu, const sockaddr_in& from, const sctp_sndrcvinfo& sri, int flags) {
|
||||||
|
// Defer the handling of AMF packet to eNB stack main thread
|
||||||
|
handle_amf_rx_msg(std::move(pdu), from, sri, flags);
|
||||||
|
};
|
||||||
|
rx_socket_handler->add_socket_handler(amf_socket.fd(),
|
||||||
|
srsran::make_sctp_sdu_handler(logger, amf_task_queue, rx_callback));
|
||||||
|
|
||||||
|
logger.info("SCTP socket established with AMF");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ngap::setup_ng()
|
||||||
|
{
|
||||||
|
uint32_t tmp32;
|
||||||
|
uint16_t tmp16;
|
||||||
|
|
||||||
|
uint32_t plmn;
|
||||||
|
srsran::s1ap_mccmnc_to_plmn(args.mcc, args.mnc, &plmn);
|
||||||
|
plmn = htonl(plmn);
|
||||||
|
|
||||||
|
ngap_pdu_c pdu;
|
||||||
|
pdu.set_init_msg().load_info_obj(ASN1_NGAP_NR_ID_NG_SETUP);
|
||||||
|
ng_setup_request_ies_container& container = pdu.init_msg().value.ng_setup_request().protocol_ies;
|
||||||
|
global_gnb_id_s& global_gnb_id = container.global_ran_node_id.value.set_global_gnb_id();
|
||||||
|
global_gnb_id.plmn_id = tai.plmn_id;
|
||||||
|
// TODO: when ASN1 is fixed
|
||||||
|
// global_gnb_id.gnb_id.set_gnb_id().from_number(args.gnb_id);
|
||||||
|
|
||||||
|
// container.ran_node_name_present = true;
|
||||||
|
// container.ran_node_name.value.from_string(args.gnb_name);
|
||||||
|
|
||||||
|
asn1::bounded_bitstring<22, 32, false, true>& gnb_str = global_gnb_id.gnb_id.set_gnb_id();
|
||||||
|
gnb_str.resize(32);
|
||||||
|
uint8_t buffer[4];
|
||||||
|
asn1::bit_ref bref(&buffer[0], sizeof(buffer));
|
||||||
|
bref.pack(args.gnb_id, 8);
|
||||||
|
memcpy(gnb_str.data(), &buffer[0], bref.distance_bytes());
|
||||||
|
|
||||||
|
// .from_number(args.gnb_id);
|
||||||
|
|
||||||
|
container.ran_node_name_present = true;
|
||||||
|
if (args.gnb_name.length() >= 150) {
|
||||||
|
args.gnb_name.resize(150);
|
||||||
|
}
|
||||||
|
// container.ran_node_name.value.from_string(args.enb_name);
|
||||||
|
container.ran_node_name.value.resize(args.gnb_name.size());
|
||||||
|
memcpy(&container.ran_node_name.value[0], &args.gnb_name[0], args.gnb_name.size());
|
||||||
|
|
||||||
|
container.supported_ta_list.value.resize(1);
|
||||||
|
container.supported_ta_list.value[0].tac = tai.tac;
|
||||||
|
container.supported_ta_list.value[0].broadcast_plmn_list.resize(1);
|
||||||
|
container.supported_ta_list.value[0].broadcast_plmn_list[0].plmn_id = tai.plmn_id;
|
||||||
|
container.supported_ta_list.value[0].broadcast_plmn_list[0].tai_slice_support_list.resize(1);
|
||||||
|
container.supported_ta_list.value[0].broadcast_plmn_list[0].tai_slice_support_list[0].s_nssai.sst.from_number(1);
|
||||||
|
|
||||||
|
container.default_paging_drx.value.value = asn1::ngap_nr::paging_drx_opts::v256; // Todo: add to args, config file
|
||||||
|
|
||||||
|
return sctp_send_ngap_pdu(pdu, 0, "ngSetupRequest");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
/* General helpers
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
bool ngap::sctp_send_ngap_pdu(const asn1::ngap_nr::ngap_pdu_c& tx_pdu, uint32_t rnti, const char* procedure_name)
|
||||||
|
{
|
||||||
|
if (not amf_connected and rnti != SRSRAN_INVALID_RNTI) {
|
||||||
|
logger.error("Aborting %s for rnti=0x%x. Cause: AMF is not connected.", procedure_name, rnti);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
srsran::unique_byte_buffer_t buf = srsran::make_byte_buffer();
|
||||||
|
if (buf == nullptr) {
|
||||||
|
logger.error("Fatal Error: Couldn't allocate buffer for %s.", procedure_name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
asn1::bit_ref bref(buf->msg, buf->get_tailroom());
|
||||||
|
if (tx_pdu.pack(bref) != asn1::SRSASN_SUCCESS) {
|
||||||
|
logger.error("Failed to pack TX PDU %s", procedure_name);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
buf->N_bytes = bref.distance_bytes();
|
||||||
|
|
||||||
|
// TODO: when we got pcap support
|
||||||
|
// Save message to PCAP
|
||||||
|
// if (pcap != nullptr) {
|
||||||
|
// pcap->write_s1ap(buf->msg, buf->N_bytes);
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (rnti != SRSRAN_INVALID_RNTI) {
|
||||||
|
logger.info(buf->msg, buf->N_bytes, "Tx S1AP SDU, %s, rnti=0x%x", procedure_name, rnti);
|
||||||
|
} else {
|
||||||
|
logger.info(buf->msg, buf->N_bytes, "Tx S1AP SDU, %s", procedure_name);
|
||||||
|
}
|
||||||
|
// TODO: when user list is ready
|
||||||
|
// uint16_t streamid = rnti == SRSRAN_INVALID_RNTI ? NONUE_STREAM_ID : users.find_ue_rnti(rnti)->stream_id;
|
||||||
|
uint16_t streamid = 0;
|
||||||
|
ssize_t n_sent = sctp_sendmsg(amf_socket.fd(),
|
||||||
|
buf->msg,
|
||||||
|
buf->N_bytes,
|
||||||
|
(struct sockaddr*)&amf_addr,
|
||||||
|
sizeof(struct sockaddr_in),
|
||||||
|
htonl(PPID),
|
||||||
|
0,
|
||||||
|
streamid,
|
||||||
|
0,
|
||||||
|
0);
|
||||||
|
if (n_sent == -1) {
|
||||||
|
if (rnti != SRSRAN_INVALID_RNTI) {
|
||||||
|
logger.error("Error: Failure at Tx S1AP SDU, %s, rnti=0x%x", procedure_name, rnti);
|
||||||
|
} else {
|
||||||
|
logger.error("Error: Failure at Tx S1AP SDU, %s", procedure_name);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace srsenb
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue