5G-LENA nr-v4.0
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
nr-test-entities.cc
1/*
2 * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Manuel Requena <manuel.requena@cttc.es>
7 */
8
9#include "nr-test-entities.h"
10
11#include "ns3/log.h"
12#include "ns3/node.h"
13#include "ns3/nr-pdcp-header.h"
14#include "ns3/nr-rlc-am-header.h"
15#include "ns3/nr-rlc-header.h"
16#include "ns3/simulator.h"
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("NrTestEntities");
22
24
25TypeId
27{
28 static TypeId tid = TypeId("ns3::NrTestRrc").SetParent<Object>().AddConstructor<NrTestRrc>();
29
30 return tid;
31}
32
33NrTestRrc::NrTestRrc()
34{
35 NS_LOG_FUNCTION(this);
36
37 m_txPdus = 0;
38 m_txBytes = 0;
39 m_rxPdus = 0;
40 m_rxBytes = 0;
41 m_txLastTime = Time(0);
42 m_rxLastTime = Time(0);
43
44 m_pdcpSapUser = new NrPdcpSpecificNrPdcpSapUser<NrTestRrc>(this);
45 // Simulator::ScheduleNow (&NrTestRrc::Start, this);
46}
47
48NrTestRrc::~NrTestRrc()
49{
50 NS_LOG_FUNCTION(this);
51}
52
53void
54NrTestRrc::DoDispose()
55{
56 NS_LOG_FUNCTION(this);
57 delete m_pdcpSapUser;
58}
59
60void
61NrTestRrc::SetDevice(Ptr<NetDevice> device)
62{
63 m_device = device;
64}
65
66void
68{
69 m_pdcpSapProvider = s;
70}
71
74{
75 return m_pdcpSapUser;
76}
77
78std::string
80{
81 NS_LOG_FUNCTION(this);
82 return m_receivedData;
83}
84
85// Stats
86uint32_t
88{
89 NS_LOG_FUNCTION(this << m_txPdus);
90 return m_txPdus;
91}
92
93uint32_t
95{
96 NS_LOG_FUNCTION(this << m_txBytes);
97 return m_txBytes;
98}
99
100uint32_t
102{
103 NS_LOG_FUNCTION(this << m_rxPdus);
104 return m_rxPdus;
105}
106
107uint32_t
109{
110 NS_LOG_FUNCTION(this << m_rxBytes);
111 return m_rxBytes;
112}
113
114Time
116{
117 NS_LOG_FUNCTION(this << m_txLastTime);
118 return m_txLastTime;
119}
120
121Time
123{
124 NS_LOG_FUNCTION(this << m_rxLastTime);
125 return m_rxLastTime;
126}
127
128void
130{
131 NS_LOG_FUNCTION(this << arrivalTime);
132 m_arrivalTime = arrivalTime;
133}
134
135void
136NrTestRrc::SetPduSize(uint32_t pduSize)
137{
138 NS_LOG_FUNCTION(this << pduSize);
139 m_pduSize = pduSize;
140}
141
146void
147NrTestRrc::DoReceivePdcpSdu(NrPdcpSapUser::ReceivePdcpSduParameters params)
148{
149 NS_LOG_FUNCTION(this << params.pdcpSdu->GetSize());
150 Ptr<Packet> p = params.pdcpSdu;
151 // NS_LOG_LOGIC ("PDU received = " << (*p));
152
153 uint32_t dataLen = p->GetSize();
154 auto buf = new uint8_t[dataLen];
155
156 // Stats
157 m_rxPdus++;
158 m_rxBytes += dataLen;
159 m_rxLastTime = Simulator::Now();
160
161 p->CopyData(buf, dataLen);
162 m_receivedData = std::string((char*)buf, dataLen);
163
164 // NS_LOG_LOGIC (m_receivedData);
165
166 delete[] buf;
167}
168
173void
175{
176 NS_LOG_FUNCTION(this);
177 NS_ASSERT_MSG(m_arrivalTime != Time(0), "Arrival time must be different from 0");
178
179 // Stats
180 m_txPdus++;
181 m_txBytes += m_pduSize;
182 m_txLastTime = Simulator::Now();
183
185 p.rnti = 1111;
186 p.lcid = 222;
187 p.pdcpSdu = Create<Packet>(m_pduSize);
188
189 bool haveContext = false;
190 Ptr<Node> node;
191 if (m_device)
192 {
193 node = m_device->GetNode();
194 if (node)
195 {
196 haveContext = true;
197 }
198 }
199 if (haveContext)
200 {
201 Simulator::ScheduleWithContext(node->GetId(),
202 Seconds(0),
204 m_pdcpSapProvider,
205 p);
206 }
207 else
208 {
209 Simulator::Schedule(Seconds(0), &NrPdcpSapProvider::TransmitPdcpSdu, m_pdcpSapProvider, p);
210 }
211
212 m_nextPdu = Simulator::Schedule(m_arrivalTime, &NrTestRrc::Start, this);
213 // Simulator::Run ();
214}
215
216void
218{
219 NS_LOG_FUNCTION(this);
220 m_nextPdu.Cancel();
221}
222
223void
224NrTestRrc::SendData(Time at, std::string dataToSend)
225{
226 NS_LOG_FUNCTION(this << at << dataToSend.length() << dataToSend);
227
228 // Stats
229 m_txPdus++;
230 m_txBytes += dataToSend.length();
231
233 p.rnti = 1111;
234 p.lcid = 222;
235
236 NS_LOG_LOGIC("Data(" << dataToSend.length() << ") = " << dataToSend.data());
237 p.pdcpSdu = Create<Packet>((uint8_t*)dataToSend.data(), dataToSend.length());
238
239 NS_LOG_LOGIC("Packet(" << p.pdcpSdu->GetSize() << ")");
240 Simulator::Schedule(at, &NrPdcpSapProvider::TransmitPdcpSdu, m_pdcpSapProvider, p);
241}
242
244
245TypeId
247{
248 static TypeId tid = TypeId("ns3::NrTestPdcp").SetParent<Object>().AddConstructor<NrTestPdcp>();
249
250 return tid;
251}
252
253NrTestPdcp::NrTestPdcp()
254{
255 NS_LOG_FUNCTION(this);
256 m_rlcSapUser = new NrRlcSpecificNrRlcSapUser<NrTestPdcp>(this);
257 Simulator::ScheduleNow(&NrTestPdcp::Start, this);
258}
259
260NrTestPdcp::~NrTestPdcp()
261{
262 NS_LOG_FUNCTION(this);
263}
264
265void
266NrTestPdcp::DoDispose()
267{
268 NS_LOG_FUNCTION(this);
269 delete m_rlcSapUser;
270}
271
272void
274{
275 m_rlcSapProvider = s;
276}
277
280{
281 return m_rlcSapUser;
282}
283
284std::string
286{
287 NS_LOG_FUNCTION(this);
288
289 return m_receivedData;
290}
291
296void
297NrTestPdcp::DoReceivePdcpPdu(Ptr<Packet> p)
298{
299 NS_LOG_FUNCTION(this << p->GetSize());
300 NS_LOG_LOGIC("Data = " << (*p));
301
302 uint32_t dataLen = p->GetSize();
303 auto buf = new uint8_t[dataLen];
304 p->CopyData(buf, dataLen);
305 m_receivedData = std::string((char*)buf, dataLen);
306
307 NS_LOG_LOGIC(m_receivedData);
308
309 delete[] buf;
310}
311
316void
318{
319 NS_LOG_FUNCTION(this);
320}
321
322void
323NrTestPdcp::SendData(Time time, std::string dataToSend)
324{
325 NS_LOG_FUNCTION(this << time << dataToSend.length() << dataToSend);
326
328 p.rnti = 1111;
329 p.lcid = 222;
330
331 NS_LOG_LOGIC("Data(" << dataToSend.length() << ") = " << dataToSend.data());
332 p.pdcpPdu = Create<Packet>((uint8_t*)dataToSend.data(), dataToSend.length());
333
334 NS_LOG_LOGIC("Packet(" << p.pdcpPdu->GetSize() << ")");
335 Simulator::Schedule(time, &NrRlcSapProvider::TransmitPdcpPdu, m_rlcSapProvider, p);
336}
337
339
340TypeId
342{
343 static TypeId tid = TypeId("ns3::NrTestMac").SetParent<Object>().AddConstructor<NrTestMac>();
344
345 return tid;
346}
347
348NrTestMac::NrTestMac()
349{
350 NS_LOG_FUNCTION(this);
351 m_device = nullptr;
352 m_macSapProvider = new GnbMacMemberNrMacSapProvider<NrTestMac>(this);
353 m_macSapUser = nullptr;
354 m_macLoopback = nullptr;
355 m_pdcpHeaderPresent = false;
356 m_rlcHeaderType = UM_RLC_HEADER;
357 m_txOpportunityMode = MANUAL_MODE;
358 m_txOppTime = Seconds(0.001);
359 m_txOppSize = 0;
360
361 m_txPdus = 0;
362 m_txBytes = 0;
363 m_rxPdus = 0;
364 m_rxBytes = 0;
365
366 // m_cmacSapProvider = new GnbMacMemberNrGnbCmacSapProvider (this);
367 // m_schedSapUser = new GnbMacMemberFfMacSchedSapUser (this);
368 // m_cschedSapUser = new GnbMacMemberFfMacCschedSapUser (this);
369 // m_gnbPhySapUser = new GnbMacMemberNrGnbPhySapUser (this);
370}
371
372NrTestMac::~NrTestMac()
373{
374 NS_LOG_FUNCTION(this);
375}
376
377void
378NrTestMac::DoDispose()
379{
380 NS_LOG_FUNCTION(this);
381 delete m_macSapProvider;
382 // delete m_cmacSapProvider;
383 // delete m_schedSapUser;
384 // delete m_cschedSapUser;
385 // delete m_gnbPhySapUser;
386
387 m_device = nullptr;
388}
389
390void
391NrTestMac::SetDevice(Ptr<NetDevice> device)
392{
393 m_device = device;
394}
395
396void
398{
399 m_macSapUser = s;
400}
401
404{
405 return m_macSapProvider;
406}
407
408void
410{
411 m_macLoopback = s;
412}
413
414std::string
416{
417 NS_LOG_FUNCTION(this);
418 return m_receivedData;
419}
420
421// Stats
422uint32_t
424{
425 NS_LOG_FUNCTION(this << m_txPdus);
426 return m_txPdus;
427}
428
429uint32_t
431{
432 NS_LOG_FUNCTION(this << m_txBytes);
433 return m_txBytes;
434}
435
436uint32_t
438{
439 NS_LOG_FUNCTION(this << m_rxPdus);
440 return m_rxPdus;
441}
442
443uint32_t
445{
446 NS_LOG_FUNCTION(this << m_rxBytes);
447 return m_rxBytes;
448}
449
450void
451NrTestMac::SendTxOpportunity(Time time, uint32_t bytes)
452{
453 NS_LOG_FUNCTION(this << time << bytes);
454 bool haveContext = false;
455 Ptr<Node> node;
456 if (m_device)
457 {
458 node = m_device->GetNode();
459 if (node)
460 {
461 haveContext = true;
462 }
463 }
465 txOpParams.bytes = bytes;
466 txOpParams.layer = 0;
467 txOpParams.componentCarrierId = 0;
468 txOpParams.harqId = 0;
469 txOpParams.rnti = 0;
470 txOpParams.lcid = 0;
471
472 if (haveContext)
473 {
474 Simulator::ScheduleWithContext(node->GetId(),
475 time,
477 m_macSapUser,
478 txOpParams);
479 }
480 else
481 {
482 Simulator::Schedule(time, &NrMacSapUser::NotifyTxOpportunity, m_macSapUser, txOpParams);
483 }
484
485 if (m_txOpportunityMode == RANDOM_MODE)
486 {
487 if (m_txOppTime != Seconds(0))
488 {
489 Simulator::Schedule(m_txOppTime,
491 this,
492 m_txOppTime,
493 m_txOppSize);
494 }
495 }
496}
497
498void
500{
501 NS_LOG_FUNCTION(this << present);
502 m_pdcpHeaderPresent = present;
503}
504
505void
506NrTestMac::SetRlcHeaderType(uint8_t rlcHeaderType)
507{
508 NS_LOG_FUNCTION(this << rlcHeaderType);
509 m_rlcHeaderType = rlcHeaderType;
510}
511
512void
514{
515 NS_LOG_FUNCTION(this << (uint32_t)mode);
516 m_txOpportunityMode = mode;
517
518 if (m_txOpportunityMode == RANDOM_MODE)
519 {
520 if (m_txOppTime != Seconds(0.0))
521 {
522 SendTxOpportunity(m_txOppTime, m_txOppSize);
523 }
524 }
525}
526
527void
529{
530 NS_LOG_FUNCTION(this << txOppTime);
531 m_txOppTime = txOppTime;
532}
533
534void
535NrTestMac::SetTxOppSize(uint32_t txOppSize)
536{
537 NS_LOG_FUNCTION(this << txOppSize);
538 m_txOppSize = txOppSize;
539}
540
545void
546NrTestMac::DoTransmitPdu(NrMacSapProvider::TransmitPduParameters params)
547{
548 NS_LOG_FUNCTION(this << params.pdu->GetSize());
549
550 m_txPdus++;
551 m_txBytes += params.pdu->GetSize();
552
554 rxPduParams.p = params.pdu;
555 rxPduParams.rnti = params.rnti;
556 rxPduParams.lcid = params.lcid;
557
558 if (m_device)
559 {
560 m_device->Send(params.pdu, m_device->GetBroadcast(), 0);
561 }
562 else if (m_macLoopback)
563 {
564 Simulator::Schedule(Seconds(0.1),
566 m_macLoopback->m_macSapUser,
567 rxPduParams);
568 }
569 else
570 {
571 NrPdcpHeader pdcpHeader;
572
573 if (m_rlcHeaderType == AM_RLC_HEADER)
574 {
575 // Remove AM RLC header
576 NrRlcAmHeader rlcAmHeader;
577 params.pdu->RemoveHeader(rlcAmHeader);
578 NS_LOG_LOGIC("AM RLC header: " << rlcAmHeader);
579 }
580 else // if (m_rlcHeaderType == UM_RLC_HEADER)
581 {
582 // Remove UM RLC header
583 NrRlcHeader rlcHeader;
584 params.pdu->RemoveHeader(rlcHeader);
585 NS_LOG_LOGIC("UM RLC header: " << rlcHeader);
586 }
587
588 // Remove PDCP header, if present
589 if (m_pdcpHeaderPresent)
590 {
591 params.pdu->RemoveHeader(pdcpHeader);
592 NS_LOG_LOGIC("PDCP header: " << pdcpHeader);
593 }
594
595 // Copy data to a string
596 uint32_t dataLen = params.pdu->GetSize();
597 auto buf = new uint8_t[dataLen];
598 params.pdu->CopyData(buf, dataLen);
599 m_receivedData = std::string((char*)buf, dataLen);
600
601 NS_LOG_LOGIC("Data (" << dataLen << ") = " << m_receivedData);
602 delete[] buf;
603 }
604}
605
606void
607NrTestMac::DoTransmitBufferStatusReport(NrMacSapProvider::BufferStatusReportParameters params)
608{
609 NS_LOG_FUNCTION(this << params.txQueueSize << params.retxQueueSize << params.statusPduSize);
610
611 if (m_txOpportunityMode == AUTOMATIC_MODE)
612 {
613 // cancel all previously scheduled TxOpps
614 for (auto it = m_nextTxOppList.begin(); it != m_nextTxOppList.end(); ++it)
615 {
616 it->Cancel();
617 }
618 m_nextTxOppList.clear();
619
620 int32_t size = params.statusPduSize + params.txQueueSize + params.retxQueueSize;
621 Time time = m_txOppTime;
622 NrMacSapUser::TxOpportunityParameters txOpParams;
623 txOpParams.bytes = m_txOppSize;
624 txOpParams.layer = 0;
625 txOpParams.componentCarrierId = 0;
626 txOpParams.harqId = 0;
627 txOpParams.rnti = params.rnti;
628 txOpParams.lcid = params.lcid;
629 while (size > 0)
630 {
631 EventId e = Simulator::Schedule(time,
633 m_macSapUser,
634 txOpParams);
635 m_nextTxOppList.push_back(e);
636 size -= m_txOppSize;
637 time += m_txOppTime;
638 }
639 }
640}
641
642bool
643NrTestMac::Receive(Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr)
644{
645 NS_LOG_FUNCTION(this << addr << protocol << p->GetSize());
646
647 m_rxPdus++;
648 m_rxBytes += p->GetSize();
649
650 Ptr<Packet> packet = p->Copy();
652 rxPduParams.p = packet;
653 rxPduParams.rnti = 0;
654 rxPduParams.lcid = 0;
655 m_macSapUser->ReceivePdu(rxPduParams);
656 return true;
657}
658
659NS_OBJECT_ENSURE_REGISTERED(NrEpcTestRrc);
660
661NrEpcTestRrc::NrEpcTestRrc()
662 : m_s1SapProvider(nullptr)
663{
664 NS_LOG_FUNCTION(this);
665 m_s1SapUser = new NrMemberEpcGnbS1SapUser<NrEpcTestRrc>(this);
666}
667
668NrEpcTestRrc::~NrEpcTestRrc()
669{
670 NS_LOG_FUNCTION(this);
671}
672
673void
674NrEpcTestRrc::DoDispose()
675{
676 NS_LOG_FUNCTION(this);
677 delete m_s1SapUser;
678}
679
680TypeId
682{
683 NS_LOG_FUNCTION("NrEpcTestRrc::GetTypeId");
684 static TypeId tid =
685 TypeId("ns3::NrEpcTestRrc").SetParent<Object>().AddConstructor<NrEpcTestRrc>();
686 return tid;
687}
688
689void
691{
692 m_s1SapProvider = s;
693}
694
697{
698 return m_s1SapUser;
699}
700
701void
702NrEpcTestRrc::DoInitialContextSetupRequest(
704{
705}
706
707void
708NrEpcTestRrc::DoDataRadioBearerSetupRequest(
709 NrEpcGnbS1SapUser::DataRadioBearerSetupRequestParameters request)
710{
711}
712
713void
714NrEpcTestRrc::DoPathSwitchRequestAcknowledge(
715 NrEpcGnbS1SapUser::PathSwitchRequestAcknowledgeParameters params)
716{
717}
718
719} // namespace ns3
RRC stub providing a testing S1 SAP user to be used with the NrEpcGnbApplication.
NrEpcGnbS1SapUser * GetS1SapUser()
static TypeId GetTypeId()
Get the type ID.
void SetS1SapProvider(NrEpcGnbS1SapProvider *s)
virtual void NotifyTxOpportunity(TxOpportunityParameters params)=0
virtual void ReceivePdu(ReceivePduParameters params)=0
virtual void TransmitPdcpSdu(TransmitPdcpSduParameters params)=0
virtual void TransmitPdcpPdu(TransmitPdcpPduParameters params)=0
void SendTxOpportunity(Time time, uint32_t bytes)
Send transmit opportunity function.
void SetTxOppTime(Time txOppTime)
uint32_t GetRxPdus()
Get the receive PDUs.
void SetNrMacLoopback(Ptr< NrTestMac > s)
Set the other side of the MAC Loopback.
NrMacSapProvider * GetNrMacSapProvider()
Get the MAC SAP provider.
void SetNrMacSapUser(NrMacSapUser *s)
Set the MAC SAP user.
void SetPdcpHeaderPresent(bool present)
Set PDCP header present function.
void SetDevice(Ptr< NetDevice > device)
Set the device function.
void SetTxOpportunityMode(uint8_t mode)
static TypeId GetTypeId()
Get the type ID.
void SetTxOppSize(uint32_t txOppSize)
bool Receive(Ptr< NetDevice > nd, Ptr< const Packet > p, uint16_t protocol, const Address &addr)
the Receive function
uint32_t GetRxBytes()
Get the receive bytes.
friend class GnbMacMemberNrMacSapProvider< NrTestMac >
allow GnbMacMemberNrMacSapProvider<NrTestMac> class friend access
std::string GetDataReceived()
Get data received function.
void SetRlcHeaderType(uint8_t rlcHeaderType)
Set RLC header type.
uint32_t GetTxPdus()
Get the transmit PDUs.
uint32_t GetTxBytes()
Get the transmit bytes.
void Start()
Start function.
void SetNrRlcSapProvider(NrRlcSapProvider *s)
Set the RLC SAP provider.
static TypeId GetTypeId()
Get the type ID.
std::string GetDataReceived()
Get data received function.
friend class NrRlcSpecificNrRlcSapUser< NrTestPdcp >
allow NrRlcSpecificNrRlcSapUser<NrTestPdcp> class friend access
NrRlcSapUser * GetNrRlcSapUser()
Get the RLC SAP user.
void SendData(Time time, std::string dataToSend)
Send data function.
void Stop()
Stop function.
static TypeId GetTypeId()
Get the type ID.
void SetPduSize(uint32_t pduSize)
Set the PDU size.
Time GetRxLastTime()
Get the last receive time.
uint32_t GetTxPdus()
Get the transmit PDUs.
uint32_t GetRxPdus()
Get the receive PDUs.
uint32_t GetTxBytes()
Get the transmit bytes.
void SetNrPdcpSapProvider(NrPdcpSapProvider *s)
Set the PDCP SAP provider.
uint32_t GetRxBytes()
Get the receive bytes.
std::string GetDataReceived()
Get data received function.
NrPdcpSapUser * GetNrPdcpSapUser()
Get the PDCP SAP user.
void SetArrivalTime(Time arrivalTime)
Set the arrival time.
void Start()
Start function.
friend class NrPdcpSpecificNrPdcpSapUser< NrTestRrc >
allow NrPdcpSpecificNrPdcpSapUser<NrTestRrc> class friend access
Time GetTxLastTime()
Get the last transmit time.
void SendData(Time at, std::string dataToSend)
Send data function.
void SetDevice(Ptr< NetDevice > device)
Set the device.