Agora  1.2.0
Agora project
mac_thread_basestation.h
Go to the documentation of this file.
1 
5 #ifndef MAC_THREAD_H_
6 #define MAC_THREAD_H_
7 
8 #include <queue>
9 
10 #include "concurrentqueue.h"
11 #include "config.h"
12 #include "crc.h"
13 #include "gettime.h"
14 #include "message.h"
15 #include "ran_config.h"
16 #include "symbols.h"
17 #include "udp_comm.h"
18 
28  public:
29  // Default log file for MAC layer outputs
30  static constexpr char kDefaultLogFilename[] = "data/mac_log_server";
31 
32  // Maximum number of outstanding UDP packets per UE that we allocate recv()
33  // buffer space for
34  static constexpr size_t kMaxPktsPerUE = 64;
35 
36  // Length of SNR moving average window
37  // TODO: map this to time?
38  static constexpr size_t kSNRWindowSize = 100;
39 
41  Config* const cfg, size_t core_offset,
43  Table<int8_t>* dl_bits_buffer, Table<int8_t>* dl_bits_buffer_status,
46  const std::string& log_filename = "");
47 
49 
50  // The main MAC thread event loop. It receives uplink data bits from the
51  // master thread and sends them to remote applications.
52  void RunEventLoop();
53 
54  private:
55  // Receive events from Agora PHY master thread. Forwards
56  // to appropriate function in MAC.
57  void ProcessRxFromPhy();
58 
59  // Receive decoded codeblocks from the PHY master thread. Send
60  // fully-received frames for UE #i to kRemoteHostname::(kBaseRemotePort + i)
62 
63  // Receive SNR report from PHY master thread. Use for RB scheduling.
64  // TODO: process CQI report here as well.
66 
67  // Push RAN config update to PHY master thread.
68  void SendRanConfigUpdate(EventData event);
69 
70  // Send control information over (out-of-band) control channel
71  // from server to client
73 
74  // Receive user data bits (downlink bits at the MAC thread running at the
75  // server, uplink bits at the MAC thread running at the client) and forward
76  // them to the PHY.
78  void ProcessUdpPacketsFromAppsBs(const char* payload);
79 
80  Config* const cfg_;
81 
82  const double freq_ghz_; // RDTSC frequency in GHz
83  // We check for new MAC packets from applications every [tsc_delta_]
84  // clock ticks
85  const size_t tsc_delta_;
86 
87  const size_t core_offset_; // The CPU core on which this thread runs
88 
89  FILE* log_file_; // Log file used to store MAC layer outputs
90  std::string log_filename_;
91 
92  // UDP endpoint used for sending messages
93  std::unique_ptr<UDPComm> udp_comm_;
94 
95  // A preallocated buffer to store UDP packets received via recv()
96  std::vector<std::byte> udp_pkt_buf_;
97 
98  // The timestamp at which we last received a UDP packet from an application
100 
101  // The frame ID of the next MAC packet we'll hand over to the PHY
102  size_t next_tx_frame_id_ = 0;
103 
104  // The radio ID of the next MAC packet we'll hand over to the PHY
105  size_t next_radio_id_ = 0;
106 
107  // The frame ID of the next TTI that the scheduler plans for
109 
111 
112  // Server-only members
113  struct {
114  // Staging buffers to accumulate decoded uplink code blocks for each UE
115  std::array<std::vector<std::byte>, kMaxUEs> frame_data_;
116 
117  // n_filled_in_frame_[i] is the number of bytes received in the current
118  // frame for UE #i
119  std::array<size_t, kMaxUEs> n_filled_in_frame_;
120 
121  // snr_[i] contains a moving window of SNR measurement for UE #i
122  std::array<std::queue<float>, kMaxUEs> snr_;
123 
124  // Placing at the end because it is variable size based on configuration
125  std::vector<std::vector<size_t>> data_size_;
126 
127  } server_;
128 
129  // TODO: decoded_buffer_ is used by only the server, so it should be moved
130  // to server_ for clarity.
132 
133  struct {
134  std::array<size_t, kMaxUEs> dl_bits_buffer_id_;
135 
138  } client_;
139  // FIFO queue for receiving messages from the master thread
141 
142  // FIFO queue for sending messages to the master thread
144 
145  // CRC
146  std::unique_ptr<DoCRC> crc_obj_;
147 };
148 
149 #endif // MAC_THREAD_H_
fmt::v8::detail::byte
byte
Definition: core.h:388
udp_comm.h
Declaration file for the UDPComm class. This class is used to send messages and receive messages from...
FrameStats::GetDLSymbolIdx
size_t GetDLSymbolIdx(size_t symbol_number) const
Definition: framestats.cc:152
kMacRemoteHostname
static constexpr char kMacRemoteHostname[]
Definition: symbols.h:324
MacThreadBaseStation::client_
struct MacThreadBaseStation::@8 client_
Config::Running
void Running(bool value)
Definition: config.h:33
Config::UeAntNum
size_t UeAntNum() const
Definition: config.h:41
EventType::kRANUpdate
@ kRANUpdate
PinToCoreWithOffset
void PinToCoreWithOffset(ThreadType thread_type, size_t core_offset, size_t thread_id, bool allow_reuse, bool verbose)
Definition: utils.cc:157
MacThreadBaseStation::MacThreadBaseStation
MacThreadBaseStation(Config *const cfg, size_t core_offset, PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t > &decoded_buffer, Table< int8_t > *dl_bits_buffer, Table< int8_t > *dl_bits_buffer_status, moodycamel::ConcurrentQueue< EventData > *rx_queue, moodycamel::ConcurrentQueue< EventData > *tx_queue, const std::string &log_filename="")
Definition: mac_thread_basestation.cc:12
MacThreadBaseStation::next_radio_id_
size_t next_radio_id_
Definition: mac_thread_basestation.h:105
Config::MacPacketsPerframe
size_t MacPacketsPerframe(Direction dir) const
Definition: config.h:276
MacThreadBaseStation::ProcessUdpPacketsFromAppsBs
void ProcessUdpPacketsFromAppsBs(const char *payload)
Definition: mac_thread_basestation.cc:374
gen_tag_t::symbol_id_
uint16_t symbol_id_
Definition: message.h:33
RanConfig::frame_id_
size_t frame_id_
modulation type (number of bits)
Definition: ran_config.h:18
kFrameWnd
static constexpr size_t kFrameWnd
Definition: symbols.h:18
EventType::kPacketFromMac
@ kPacketFromMac
MacPacketPacked::Ue
uint16_t Ue() const
Definition: message.h:280
mac_thread_basestation.h
RanConfig
The struct that contains the RAN configuration that Agora must apply for a particular frame.
Definition: ran_config.h:14
RBIndicator
The packet that contains the control information (DCI) that tells each UE which uplink resource block...
Definition: ran_config.h:30
FrameStats::GetULSymbolIdx
size_t GetULSymbolIdx(size_t symbol_number) const
Definition: framestats.cc:156
crc.h
MacThreadBaseStation::cfg_
Config *const cfg_
Definition: mac_thread_basestation.h:80
MacThreadBaseStation::udp_comm_
std::unique_ptr< UDPComm > udp_comm_
Definition: mac_thread_basestation.h:93
Config::BsAntNum
size_t BsAntNum() const
Definition: config.h:35
Config::MacDataBytesNumPerframe
size_t MacDataBytesNumPerframe(Direction dir) const
Definition: config.h:259
MacThreadBaseStation::core_offset_
const size_t core_offset_
Definition: mac_thread_basestation.h:87
fclose
fclose(fileID)
EventData::tags_
std::array< size_t, kMaxTags > tags_
Definition: message.h:146
AGORA_LOG_ERROR
#define AGORA_LOG_ERROR(...)
Definition: logger.h:39
MacThreadBaseStation::ProcessUdpPacketsFromApps
void ProcessUdpPacketsFromApps()
Definition: mac_thread_basestation.cc:270
MacThreadBaseStation::decoded_buffer_
PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t > & decoded_buffer_
Definition: mac_thread_basestation.h:131
Direction::kUplink
@ kUplink
Config::Frame
const FrameStats & Frame() const
Definition: config.h:340
MacThreadBaseStation::tsc_delta_
const size_t tsc_delta_
Definition: mac_thread_basestation.h:85
AGORA_LOG_TRACE
#define AGORA_LOG_TRACE(...)
Definition: logger.h:92
MacThreadBaseStation::rx_queue_
moodycamel::ConcurrentQueue< EventData > * rx_queue_
Definition: mac_thread_basestation.h:140
gen_tag_t::ue_id_
uint16_t ue_id_
Definition: message.h:37
MacThreadBaseStation::data_size_
std::vector< std::vector< size_t > > data_size_
Definition: mac_thread_basestation.h:125
EventData::event_type_
EventType event_type_
Definition: message.h:144
MacThreadBaseStation::n_filled_in_frame_
std::array< size_t, kMaxUEs > n_filled_in_frame_
Definition: mac_thread_basestation.h:119
Table< int8_t >
MacThreadBaseStation::log_file_
FILE * log_file_
Definition: mac_thread_basestation.h:89
MacThreadBaseStation::~MacThreadBaseStation
~MacThreadBaseStation()
Definition: mac_thread_basestation.cc:71
snr
snr
Definition: inspect_agora_results.m:118
gen_tag_t
Definition: message.h:22
MacPacketPacked::kHeaderSize
static constexpr size_t kHeaderSize
Definition: message.h:276
EventType::kSNRReport
@ kSNRReport
EventType::kPacketToMac
@ kPacketToMac
EventData
Definition: message.h:142
CommsLib::kQaM16
@ kQaM16
Definition: comms-lib.h:43
MacThreadBaseStation::dl_bits_buffer_
Table< int8_t > * dl_bits_buffer_
Definition: mac_thread_basestation.h:136
EventData::num_tags_
uint32_t num_tags_
Definition: message.h:145
message.h
Self defined functions for message storage and passing.
kUdpRxBufferPadding
static constexpr size_t kUdpRxBufferPadding
Definition: mac_thread_basestation.cc:10
MacPacketHeaderPacked
Definition: message.h:244
MacPacketPacked::Set
void Set(const uint16_t &f, const uint16_t &s, const uint16_t &u, const uint16_t &data_size)
Definition: message.h:286
FrameStats::ClientUlPilotSymbols
size_t ClientUlPilotSymbols() const
Definition: framestats.h:73
MacThreadBaseStation::last_mac_pkt_rx_tsc_
size_t last_mac_pkt_rx_tsc_
Definition: mac_thread_basestation.h:99
MacThreadBaseStation::scheduler_next_frame_id_
size_t scheduler_next_frame_id_
Definition: mac_thread_basestation.h:108
i
for i
Definition: generate_data.m:107
GetTime
Definition: gettime.h:11
MacThreadBaseStation::RunEventLoop
void RunEventLoop()
Definition: mac_thread_basestation.cc:526
MacThreadBaseStation::udp_pkt_buf_
std::vector< std::byte > udp_pkt_buf_
Definition: mac_thread_basestation.h:96
Config::MacPayloadMaxLength
size_t MacPayloadMaxLength(Direction dir) const
Definition: config.h:272
MacThreadBaseStation::crc_obj_
std::unique_ptr< DoCRC > crc_obj_
Definition: mac_thread_basestation.h:146
moodycamel::ConcurrentQueue::enqueue
bool enqueue(T const &item)
Definition: concurrentqueue.h:974
MacThreadBaseStation::SendRanConfigUpdate
void SendRanConfigUpdate(EventData event)
Definition: mac_thread_basestation.cc:102
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
GetTime::Rdtsc
static size_t Rdtsc()
Return the TSC.
Definition: gettime.h:25
MacThreadBaseStation
The MAC thread that runs alongside the PHY processing at the Agora server or client.
Definition: mac_thread_basestation.h:27
MacThreadBaseStation::SendControlInformation
void SendControlInformation()
Definition: mac_thread_basestation.cc:258
fmt::v8::fprintf
auto fprintf(std::FILE *f, const S &fmt, const T &... args) -> int
Definition: printf.h:607
Config::BsServerAddr
std::string BsServerAddr() const
Definition: config.h:299
ThreadType::kWorkerMacTXRX
@ kWorkerMacTXRX
RanConfig::n_antennas_
size_t n_antennas_
Definition: ran_config.h:16
Direction::kDownlink
@ kDownlink
Config::GetFrameDurationSec
double GetFrameDurationSec() const
Return the frame duration in seconds.
Definition: config.h:464
Config::FramesToTest
size_t FramesToTest() const
Definition: config.h:309
MacThreadBaseStation::tx_queue_
moodycamel::ConcurrentQueue< EventData > * tx_queue_
Definition: mac_thread_basestation.h:143
MacThreadBaseStation::frame_data_
std::array< std::vector< std::byte >, kMaxUEs > frame_data_
Definition: mac_thread_basestation.h:115
Config::MacPacketLength
size_t MacPacketLength(Direction dir) const
Definition: config.h:268
Config::BsMacRxPort
size_t BsMacRxPort() const
Definition: config.h:320
symbols.h
RBIndicator::ue_id_
size_t ue_id_
Definition: ran_config.h:32
MacThreadBaseStation::kMaxPktsPerUE
static constexpr size_t kMaxPktsPerUE
Definition: mac_thread_basestation.h:34
ran_config.h
FrameStats::ClientDlPilotSymbols
size_t ClientDlPilotSymbols() const
Definition: framestats.h:76
MacThreadBaseStation::fast_rand_
FastRand fast_rand_
Definition: mac_thread_basestation.h:110
MacThreadBaseStation::freq_ghz_
const double freq_ghz_
Definition: mac_thread_basestation.h:82
moodycamel::ConcurrentQueue< EventData >
MacThreadBaseStation::snr_
std::array< std::queue< float >, kMaxUEs > snr_
Definition: mac_thread_basestation.h:122
Config::UeAntTotal
size_t UeAntTotal() const
Definition: config.h:43
GetTime::MeasureRdtscFreq
static double MeasureRdtscFreq()
Definition: gettime.h:51
MacThreadBaseStation::log_filename_
std::string log_filename_
Definition: mac_thread_basestation.h:90
MacThreadBaseStation::ProcessRxFromPhy
void ProcessRxFromPhy()
Definition: mac_thread_basestation.cc:76
Config
Definition: config.h:26
FastRand
Definition: utils.h:179
AGORA_LOG_INFO
#define AGORA_LOG_INFO(...)
Definition: logger.h:62
MacThreadBaseStation::server_
struct MacThreadBaseStation::@7 server_
AGORA_LOG_FRAME
#define AGORA_LOG_FRAME(...)
Definition: logger.h:72
MacPacketPacked::PayloadLength
uint16_t PayloadLength() const
Definition: message.h:282
RBIndicator::mod_order_bits_
size_t mod_order_bits_
UE ID.
Definition: ran_config.h:33
RtAssert
static void RtAssert(bool condition, const char *throw_str)
Definition: utils.h:104
kLogMacPackets
static constexpr bool kLogMacPackets
Definition: symbols.h:211
FrameStats::GetULSymbolLast
size_t GetULSymbolLast() const
Definition: framestats.h:47
to_string
std::string to_string() const
Definition: eth_common.h:64
config.h
Declaration file for the configuration class which importants json configuration values into class va...
MacThreadBaseStation::next_tx_frame_id_
size_t next_tx_frame_id_
Definition: mac_thread_basestation.h:102
moodycamel::ConcurrentQueue::try_dequeue
bool try_dequeue(U &item)
Definition: concurrentqueue.h:1104
MacPacketPacked
Definition: message.h:274
MacThreadBaseStation::kSNRWindowSize
static constexpr size_t kSNRWindowSize
Definition: mac_thread_basestation.h:38
PtrCube< kFrameWnd, kMaxSymbols, kMaxUEs, int8_t >
gettime.h
kMaxUEs
static constexpr size_t kMaxUEs
Definition: symbols.h:289
FrameStats::NumUlDataSyms
size_t NumUlDataSyms() const
Definition: framestats.h:80
gen_tag_t::frame_id_
uint32_t frame_id_
Definition: message.h:32
MacThreadBaseStation::dl_bits_buffer_status_
Table< int8_t > * dl_bits_buffer_status_
Definition: mac_thread_basestation.h:137
rx_mac_tag_t
Definition: message.h:303
MacThreadBaseStation::ProcessSnrReportFromPhy
void ProcessSnrReportFromPhy(EventData event)
Definition: mac_thread_basestation.cc:91
MacThreadBaseStation::ProcessCodeblocksFromPhy
void ProcessCodeblocksFromPhy(EventData event)
Definition: mac_thread_basestation.cc:122
Config::ModOrderBits
size_t ModOrderBits(Direction dir) const
Definition: config.h:247
RanConfig::mod_order_bits_
size_t mod_order_bits_
Number of active antennas at the base station.
Definition: ran_config.h:17
FrameStats::GetULSymbol
size_t GetULSymbol(size_t location) const
Definition: framestats.cc:114
concurrentqueue.h
MacThreadBaseStation::dl_bits_buffer_id_
std::array< size_t, kMaxUEs > dl_bits_buffer_id_
Definition: mac_thread_basestation.h:134
kMacBaseClientPort
static constexpr size_t kMacBaseClientPort
Definition: symbols.h:337
Config::UeServerAddr
std::string UeServerAddr() const
Definition: config.h:298
Config::BsMacTxPort
size_t BsMacTxPort() const
Definition: config.h:321
utils_ldpc.h
MacThreadBaseStation::kDefaultLogFilename
static constexpr char kDefaultLogFilename[]
Definition: mac_thread_basestation.h:30