5G-LENA nr-v3.3-81-g75c7590d
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
nr-gnb-net-device.cc
1// Copyright (c) 2019 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
2//
3// SPDX-License-Identifier: GPL-2.0-only
4
5#include "nr-gnb-net-device.h"
6
7#include "bandwidth-part-gnb.h"
8#include "bwp-manager-gnb.h"
9#include "nr-gnb-component-carrier-manager.h"
10#include "nr-gnb-mac.h"
11#include "nr-gnb-phy.h"
12#include "nr-gnb-rrc.h"
13
14#include <ns3/abort.h>
15#include <ns3/ipv4-l3-protocol.h>
16#include <ns3/ipv6-l3-protocol.h>
17#include <ns3/log.h>
18#include <ns3/object-map.h>
19#include <ns3/pointer.h>
20
21namespace ns3
22{
23
24NS_LOG_COMPONENT_DEFINE("NrGnbNetDevice");
25
26NS_OBJECT_ENSURE_REGISTERED(NrGnbNetDevice);
27
28TypeId
29NrGnbNetDevice::GetTypeId()
30{
31 static TypeId tid =
32 TypeId("ns3::NrGnbNetDevice")
33 .SetParent<NrNetDevice>()
34 .AddConstructor<NrGnbNetDevice>()
35 .AddAttribute("NrGnbComponentCarrierManager",
36 "The component carrier manager associated to this GnbNetDevice",
37 PointerValue(),
38 MakePointerAccessor(&NrGnbNetDevice::m_componentCarrierManager),
39 MakePointerChecker<NrGnbComponentCarrierManager>())
40 .AddAttribute("BandwidthPartMap",
41 "List of Bandwidth Part container.",
42 ObjectMapValue(),
43 MakeObjectMapAccessor(&NrGnbNetDevice::m_ccMap),
44 MakeObjectMapChecker<BandwidthPartGnb>())
45 .AddAttribute("NrGnbRrc",
46 "The RRC layer associated with the gNB",
47 PointerValue(),
48 MakePointerAccessor(&NrGnbNetDevice::m_rrc),
49 MakePointerChecker<NrGnbRrc>());
50 return tid;
51}
52
53NrGnbNetDevice::NrGnbNetDevice()
54 : m_cellId(0)
55{
56 NS_LOG_FUNCTION(this);
57}
58
59NrGnbNetDevice::~NrGnbNetDevice()
60{
61 NS_LOG_FUNCTION(this);
62}
63
64Ptr<NrMacScheduler>
65NrGnbNetDevice::GetScheduler(uint8_t index) const
66{
67 NS_LOG_FUNCTION(this);
68 return m_ccMap.at(index)->GetScheduler();
69}
70
71void
72NrGnbNetDevice::SetCcMap(const std::map<uint8_t, Ptr<BandwidthPartGnb>>& ccm)
73{
74 NS_ABORT_IF(!m_ccMap.empty());
75 m_ccMap = ccm;
76}
77
78uint32_t
79NrGnbNetDevice::GetCcMapSize() const
80{
81 return static_cast<uint32_t>(m_ccMap.size());
82}
83
84void
85NrGnbNetDevice::SetNrFhControl(Ptr<NrFhControl> nrFh)
86{
87 NS_LOG_FUNCTION(this);
88 m_nrFhControl = nrFh;
89}
90
91Ptr<NrFhControl>
92NrGnbNetDevice::GetNrFhControl()
93{
94 NS_LOG_FUNCTION(this);
95 return m_nrFhControl;
96}
97
98void
99NrGnbNetDevice::RouteIngoingCtrlMsgs(const std::list<Ptr<NrControlMessage>>& msgList,
100 uint8_t sourceBwpId)
101{
102 NS_LOG_FUNCTION(this);
103
104 for (const auto& msg : msgList)
105 {
106 uint8_t bwpId = DynamicCast<BwpManagerGnb>(m_componentCarrierManager)
107 ->RouteIngoingCtrlMsgs(msg, sourceBwpId);
108 m_ccMap.at(bwpId)->GetPhy()->PhyCtrlMessagesReceived(msg);
109 }
110}
111
112void
113NrGnbNetDevice::RouteOutgoingCtrlMsgs(const std::list<Ptr<NrControlMessage>>& msgList,
114 uint8_t sourceBwpId)
115{
116 NS_LOG_FUNCTION(this);
117
118 for (const auto& msg : msgList)
119 {
120 uint8_t bwpId = DynamicCast<BwpManagerGnb>(m_componentCarrierManager)
121 ->RouteOutgoingCtrlMsg(msg, sourceBwpId);
122 NS_ASSERT_MSG(m_ccMap.size() > bwpId,
123 "Returned bwp " << +bwpId << " is not present. Check your configuration");
124 NS_ASSERT_MSG(
125 m_ccMap.at(bwpId)->GetPhy()->HasDlSlot(),
126 "Returned bwp "
127 << +bwpId
128 << " has no DL slot, so the message can't go out. Check your configuration");
129 m_ccMap.at(bwpId)->GetPhy()->EncodeCtrlMsg(msg);
130 }
131}
132
133void
134NrGnbNetDevice::DoInitialize()
135{
136 NS_LOG_FUNCTION(this);
137 if (!m_isCellConfigured)
138 {
139 ConfigureCell();
140 }
141}
142
143void
144NrGnbNetDevice::DoDispose()
145{
146 NS_LOG_FUNCTION(this);
147
148 m_rrc->Dispose();
149 m_rrc = nullptr;
150 for (const auto& it : m_ccMap)
151 {
152 it.second->Dispose();
153 }
154 m_ccMap.clear();
155 m_componentCarrierManager->Dispose();
156 m_componentCarrierManager = nullptr;
157 NrNetDevice::DoDispose();
158}
159
160Ptr<NrGnbMac>
161NrGnbNetDevice::GetMac(uint8_t index) const
162{
163 return m_ccMap.at(index)->GetMac();
164}
165
166Ptr<NrGnbPhy>
167NrGnbNetDevice::GetPhy(uint8_t index) const
168{
169 NS_LOG_FUNCTION(this);
170 return m_ccMap.at(index)->GetPhy();
171}
172
173Ptr<BwpManagerGnb>
174NrGnbNetDevice::GetBwpManager() const
175{
176 return DynamicCast<BwpManagerGnb>(m_componentCarrierManager);
177}
178
179uint16_t
180NrGnbNetDevice::GetCellId() const
181{
182 NS_LOG_FUNCTION(this);
183 return m_cellId;
184}
185
186std::vector<uint16_t>
187NrGnbNetDevice::GetCellIds() const
188{
189 std::vector<uint16_t> cellIds;
190
191 cellIds.reserve(m_ccMap.size());
192 for (auto& it : m_ccMap)
193 {
194 cellIds.push_back(it.second->GetCellId());
195 }
196 return cellIds;
197}
198
199void
200NrGnbNetDevice::SetCellId(uint16_t cellId)
201{
202 NS_LOG_FUNCTION(this);
203 m_cellId = cellId;
204}
205
206uint16_t
207NrGnbNetDevice::GetBwpId(uint8_t index) const
208{
209 NS_LOG_FUNCTION(this);
210 return m_ccMap.at(index)->GetCellId();
211}
212
213uint16_t
214NrGnbNetDevice::GetEarfcn(uint8_t index) const
215{
216 NS_LOG_FUNCTION(this);
217 return m_ccMap.at(index)->GetDlEarfcn(); // Ul or Dl doesn't matter, they are the same
218}
219
220void
221NrGnbNetDevice::SetRrc(Ptr<NrGnbRrc> rrc)
222{
223 m_rrc = rrc;
224}
225
226Ptr<NrGnbRrc>
227NrGnbNetDevice::GetRrc()
228{
229 return m_rrc;
230}
231
232bool
233NrGnbNetDevice::DoSend(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
234{
235 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
236 NS_ABORT_MSG_IF(protocolNumber != Ipv4L3Protocol::PROT_NUMBER &&
237 protocolNumber != Ipv6L3Protocol::PROT_NUMBER,
238 "unsupported protocol " << protocolNumber
239 << ", only IPv4 and IPv6 are supported");
240
241 NS_LOG_INFO("Forward received packet to RRC Layer");
242 m_txTrace(packet, dest);
243
244 return m_rrc->SendData(packet);
245}
246
247void
248NrGnbNetDevice::UpdateConfig()
249{
250 NS_LOG_FUNCTION(this);
251 // No longer does anything; replaced by ConfigureCell()
252}
253
254void
255NrGnbNetDevice::ConfigureCell()
256{
257 NS_LOG_FUNCTION(this);
258 NS_ASSERT_MSG(!m_isCellConfigured, "ConfigureCell() has already been called");
259 NS_ASSERT_MSG(!m_ccMap.empty(), "Component carrier map is empty");
260 m_isCellConfigured = true;
261 m_rrc->ConfigureCell(m_ccMap);
262}
263
264bool
265NrGnbNetDevice::IsCellConfigured() const
266{
267 return m_isCellConfigured;
268}
269
270uint16_t
271NrGnbNetDevice::GetCellIdDlBandwidth(uint16_t cellId) const
272{
273 NS_ASSERT_MSG(m_rrc->HasCellId(cellId), "Unknown cellId");
274 if (m_rrc->HasCellId(cellId))
275 {
276 for (const auto& [key, cc] : m_ccMap)
277 {
278 if (cc->GetCellId() == cellId)
279 {
280 return cc->GetDlBandwidth();
281 }
282 }
283 }
284 return 0;
285}
286
287uint16_t
288NrGnbNetDevice::GetCellIdUlBandwidth(uint16_t cellId) const
289{
290 NS_ASSERT_MSG(m_rrc->HasCellId(cellId), "Unknown cellId");
291 if (m_rrc->HasCellId(cellId))
292 {
293 for (const auto& [key, cc] : m_ccMap)
294 {
295 if (cc->GetCellId() == cellId)
296 {
297 return cc->GetUlBandwidth();
298 }
299 }
300 }
301 return 0;
302}
303
304uint32_t
305NrGnbNetDevice::GetCellIdDlEarfcn(uint16_t cellId) const
306{
307 NS_ASSERT_MSG(m_rrc->HasCellId(cellId), "Unknown cellId");
308 if (m_rrc->HasCellId(cellId))
309 {
310 for (const auto& [key, cc] : m_ccMap)
311 {
312 if (cc->GetCellId() == cellId)
313 {
314 return cc->GetDlEarfcn();
315 }
316 }
317 }
318 return 0;
319}
320
321uint32_t
322NrGnbNetDevice::GetCellIdUlEarfcn(uint16_t cellId) const
323{
324 NS_ASSERT_MSG(m_rrc->HasCellId(cellId), "Unknown cellId");
325 if (m_rrc->HasCellId(cellId))
326 {
327 for (const auto& [key, cc] : m_ccMap)
328 {
329 if (cc->GetCellId() == cellId)
330 {
331 return cc->GetUlEarfcn();
332 }
333 }
334 }
335 return 0;
336}
337
338} // namespace ns3
NrNetDevice()
NrNetDevice.