5G-LENA nr-v3.3-159-ga6832aa7
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
nr-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-net-device.h"
6
7#include "ns3/channel.h"
8#include "ns3/error-model.h"
9#include "ns3/ipv4-l3-protocol.h"
10#include "ns3/ipv6-header.h"
11#include "ns3/ipv6-l3-protocol.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/pointer.h"
15#include "ns3/uinteger.h"
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("NrNetDevice");
21
22NS_OBJECT_ENSURE_REGISTERED(NrNetDevice);
23
24TypeId
26{
27 static TypeId tid =
28 TypeId("ns3::NrNetDevice")
29 .SetParent<NetDevice>()
30 .AddAttribute("Mtu",
31 "The MAC-level Maximum Transmission Unit",
32 UintegerValue(30000),
33 MakeUintegerAccessor(&NrNetDevice::SetMtu, &NrNetDevice::GetMtu),
34 MakeUintegerChecker<uint16_t>())
35 .AddAttribute("ReceiveErrorModel",
36 "An optional packet error model to simulate packet loss",
37 PointerValue(),
38 MakePointerAccessor(&NrNetDevice::m_receiveErrorModel),
39 MakePointerChecker<ErrorModel>())
40 .AddTraceSource("Tx",
41 "A packet has been transmitted with the Address as the recipient",
42 MakeTraceSourceAccessor(&NrNetDevice::m_txTrace),
43 "ns3::Packet::AddressTracedCallback")
44 .AddTraceSource("Rx",
45 "A packet has been received",
46 MakeTraceSourceAccessor(&NrNetDevice::m_rxTrace),
47 "ns3::Packet::TracedCallback")
48 .AddTraceSource("Drop",
49 "A packet has been dropped by the receive error model",
50 MakeTraceSourceAccessor(&NrNetDevice::m_dropTrace),
51 "ns3::Packet::TracedCallback");
52
53 return tid;
54}
55
57{
58 NS_LOG_FUNCTION(this);
59}
60
62{
63 NS_LOG_FUNCTION(this);
64}
65
66void
67NrNetDevice::DoDispose()
68{
69 m_node = nullptr;
70 NetDevice::DoDispose();
71}
72
73void
74NrNetDevice::SetIfIndex(const uint32_t index)
75{
76 m_ifIndex = index;
77}
78
79uint32_t
80NrNetDevice::GetIfIndex() const
81{
82 return m_ifIndex;
83}
84
85Ptr<Channel>
86NrNetDevice::GetChannel() const
87{
88 return nullptr;
89}
90
91void
92NrNetDevice::SetAddress(Address address)
93{
94 NS_LOG_FUNCTION(this << address);
95 m_macaddress = Mac48Address::ConvertFrom(address);
96}
97
98Address
99NrNetDevice::GetAddress() const
100{
101 NS_LOG_FUNCTION(this);
102 return m_macaddress;
103}
104
105bool
106NrNetDevice::SetMtu(const uint16_t mtu)
107{
108 m_mtu = mtu;
109 return true;
110}
111
112uint16_t
113NrNetDevice::GetMtu() const
114{
115 return m_mtu;
116}
117
118bool
119NrNetDevice::IsLinkUp() const
120{
121 return m_linkUp;
122}
123
124void
125NrNetDevice::AddLinkChangeCallback([[maybe_unused]] Callback<void> callback)
126{
127}
128
129bool
130NrNetDevice::IsBroadcast() const
131{
132 return false;
133}
134
135Address
136NrNetDevice::GetBroadcast() const
137{
138 return Mac48Address::GetBroadcast();
139}
140
141bool
142NrNetDevice::IsMulticast() const
143{
144 return false;
145}
146
147Address
148NrNetDevice::GetMulticast([[maybe_unused]] Ipv4Address multicastGroup) const
149{
150 return Mac48Address("01:00:5e:00:00:00");
151}
152
153bool
154NrNetDevice::IsBridge() const
155{
156 return false;
157}
158
159bool
160NrNetDevice::IsPointToPoint() const
161{
162 return false;
163}
164
165bool
166NrNetDevice::SendFrom([[maybe_unused]] Ptr<Packet> packet,
167 [[maybe_unused]] const Address& source,
168 [[maybe_unused]] const Address& dest,
169 [[maybe_unused]] uint16_t protocolNumber)
170{
171 NS_FATAL_ERROR("Send from not supported");
172 return false;
173}
174
175Ptr<Node>
176NrNetDevice::GetNode() const
177{
178 return m_node;
179}
180
181void
182NrNetDevice::SetNode(Ptr<Node> node)
183{
184 m_node = node;
185}
186
187bool
188NrNetDevice::NeedsArp() const
189{
190 return false;
191}
192
193Address
194NrNetDevice::GetMulticast([[maybe_unused]] Ipv6Address addr) const
195{
196 return Address();
197}
198
199void
200NrNetDevice::SetReceiveCallback(ReceiveCallback cb)
201{
202 NS_LOG_FUNCTION(this);
203 m_rxCallback = cb;
204}
205
206void
207NrNetDevice::SetPromiscReceiveCallback([[maybe_unused]] PromiscReceiveCallback cb)
208{
209}
210
211bool
212NrNetDevice::SupportsSendFrom() const
213{
214 return false;
215}
216
217void
218NrNetDevice::Receive(Ptr<Packet> p)
219{
220 NS_LOG_FUNCTION(this << p);
221
222 Ipv4Header ipv4Header;
223 Ipv6Header ipv6Header;
224
225 if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt(p))
226 {
227 NS_LOG_INFO("Dropping " << p->GetSize() << " bytes on " << m_macaddress);
228 m_dropTrace(p);
229 return;
230 }
231 if (p->PeekHeader(ipv4Header) != 0)
232 {
233 NS_LOG_INFO("Received " << p->GetSize() << " bytes on " << m_macaddress
234 << ". IPv4 packet from " << ipv4Header.GetSource() << " to "
235 << ipv4Header.GetDestination());
236 m_rxTrace(p);
237 m_rxCallback(this, p, Ipv4L3Protocol::PROT_NUMBER, Address());
238 }
239 else if (p->PeekHeader(ipv6Header) != 0)
240 {
241 NS_LOG_INFO("Received " << p->GetSize() << " bytes on " << m_macaddress
242 << ". IPv6 packet from " << ipv6Header.GetSource() << " to "
243 << ipv6Header.GetDestination());
244 m_rxTrace(p);
245 m_rxCallback(this, p, Ipv6L3Protocol::PROT_NUMBER, Address());
246 }
247 else
248 {
249 NS_ABORT_MSG("Unknown IP type");
250 }
251}
252
253bool
254NrNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
255{
256 bool ret = DoSend(packet, dest, protocolNumber);
257 m_txTrace(packet, dest);
258 return ret;
259}
260
261} // namespace ns3
TracedCallback< Ptr< const Packet > > m_dropTrace
Traced Callback for dropped packets.
~NrNetDevice() override
~NrNetDevice
TracedCallback< Ptr< const Packet >, const Address & > m_txTrace
Traced Callback for transmitted packets.
TracedCallback< Ptr< const Packet > > m_rxTrace
Traced Callback for received packets.
NrNetDevice()
NrNetDevice.
static TypeId GetTypeId()
GetTypeId.