A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "simple-net-device.h"
20
21#include "error-model.h"
22#include "queue.h"
23#include "simple-channel.h"
24
25#include "ns3/boolean.h"
26#include "ns3/log.h"
27#include "ns3/node.h"
28#include "ns3/packet.h"
29#include "ns3/pointer.h"
30#include "ns3/simulator.h"
31#include "ns3/string.h"
32#include "ns3/tag.h"
33#include "ns3/trace-source-accessor.h"
34
35namespace ns3
36{
37
38NS_LOG_COMPONENT_DEFINE("SimpleNetDevice");
39
43class SimpleTag : public Tag
44{
45 public:
50 static TypeId GetTypeId();
51 TypeId GetInstanceTypeId() const override;
52
53 uint32_t GetSerializedSize() const override;
54 void Serialize(TagBuffer i) const override;
55 void Deserialize(TagBuffer i) override;
56
61 void SetSrc(Mac48Address src);
66 Mac48Address GetSrc() const;
67
72 void SetDst(Mac48Address dst);
77 Mac48Address GetDst() const;
78
83 void SetProto(uint16_t proto);
88 uint16_t GetProto() const;
89
90 void Print(std::ostream& os) const override;
91
92 private:
96};
97
99
100TypeId
102{
103 static TypeId tid = TypeId("ns3::SimpleTag")
104 .SetParent<Tag>()
105 .SetGroupName("Network")
106 .AddConstructor<SimpleTag>();
107 return tid;
108}
109
110TypeId
112{
113 return GetTypeId();
114}
115
118{
119 return 8 + 8 + 2;
120}
121
122void
124{
125 uint8_t mac[6];
126 m_src.CopyTo(mac);
127 i.Write(mac, 6);
128 m_dst.CopyTo(mac);
129 i.Write(mac, 6);
131}
132
133void
135{
136 uint8_t mac[6];
137 i.Read(mac, 6);
138 m_src.CopyFrom(mac);
139 i.Read(mac, 6);
140 m_dst.CopyFrom(mac);
142}
143
144void
146{
147 m_src = src;
148}
149
152{
153 return m_src;
154}
155
156void
158{
159 m_dst = dst;
160}
161
164{
165 return m_dst;
166}
167
168void
169SimpleTag::SetProto(uint16_t proto)
170{
171 m_protocolNumber = proto;
172}
173
174uint16_t
176{
177 return m_protocolNumber;
178}
179
180void
181SimpleTag::Print(std::ostream& os) const
182{
183 os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
184}
185
187
188TypeId
190{
191 static TypeId tid =
192 TypeId("ns3::SimpleNetDevice")
194 .SetGroupName("Network")
195 .AddConstructor<SimpleNetDevice>()
196 .AddAttribute("ReceiveErrorModel",
197 "The receiver error model used to simulate packet loss",
198 PointerValue(),
200 MakePointerChecker<ErrorModel>())
201 .AddAttribute("PointToPointMode",
202 "The device is configured in Point to Point mode",
203 BooleanValue(false),
206 .AddAttribute("TxQueue",
207 "A queue to use as the transmit queue in the device.",
208 StringValue("ns3::DropTailQueue<Packet>"),
210 MakePointerChecker<Queue<Packet>>())
211 .AddAttribute("DataRate",
212 "The default data rate for point to point links. Zero means infinite",
213 DataRateValue(DataRate("0b/s")),
216 .AddTraceSource("PhyRxDrop",
217 "Trace source indicating a packet has been dropped "
218 "by the device during reception",
220 "ns3::Packet::TracedCallback");
221 return tid;
222}
223
225 : m_channel(nullptr),
226 m_node(nullptr),
227 m_mtu(0xffff),
228 m_ifIndex(0),
229 m_linkUp(false)
230{
231 NS_LOG_FUNCTION(this);
232}
233
234void
236{
237 NS_LOG_FUNCTION(this << packet << protocol << to << from);
238 NetDevice::PacketType packetType;
239
240 if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt(packet))
241 {
242 m_phyRxDropTrace(packet);
243 return;
244 }
245
246 if (to == m_address)
247 {
248 packetType = NetDevice::PACKET_HOST;
249 }
250 else if (to.IsBroadcast())
251 {
252 packetType = NetDevice::PACKET_BROADCAST;
253 }
254 else if (to.IsGroup())
255 {
256 packetType = NetDevice::PACKET_MULTICAST;
257 }
258 else
259 {
260 packetType = NetDevice::PACKET_OTHERHOST;
261 }
262
263 if (packetType != NetDevice::PACKET_OTHERHOST)
264 {
265 m_rxCallback(this, packet, protocol, from);
266 }
267
269 {
270 m_promiscCallback(this, packet, protocol, from, to, packetType);
271 }
272}
273
274void
276{
277 NS_LOG_FUNCTION(this << channel);
278 m_channel = channel;
279 m_channel->Add(this);
280 m_linkUp = true;
282}
283
286{
287 NS_LOG_FUNCTION(this);
288 return m_queue;
289}
290
291void
293{
294 NS_LOG_FUNCTION(this << q);
295 m_queue = q;
296}
297
298void
300{
301 NS_LOG_FUNCTION(this << em);
303}
304
305void
307{
308 NS_LOG_FUNCTION(this << index);
309 m_ifIndex = index;
310}
311
314{
315 NS_LOG_FUNCTION(this);
316 return m_ifIndex;
317}
318
321{
322 NS_LOG_FUNCTION(this);
323 return m_channel;
324}
325
326void
328{
329 NS_LOG_FUNCTION(this << address);
331}
332
335{
336 //
337 // Implicit conversion from Mac48Address to Address
338 //
339 NS_LOG_FUNCTION(this);
340 return m_address;
341}
342
343bool
344SimpleNetDevice::SetMtu(const uint16_t mtu)
345{
346 NS_LOG_FUNCTION(this << mtu);
347 m_mtu = mtu;
348 return true;
349}
350
351uint16_t
353{
354 NS_LOG_FUNCTION(this);
355 return m_mtu;
356}
357
358bool
360{
361 NS_LOG_FUNCTION(this);
362 return m_linkUp;
363}
364
365void
367{
368 NS_LOG_FUNCTION(this << &callback);
370}
371
372bool
374{
375 NS_LOG_FUNCTION(this);
376 return !m_pointToPointMode;
377}
378
381{
382 NS_LOG_FUNCTION(this);
384}
385
386bool
388{
389 NS_LOG_FUNCTION(this);
390 return !m_pointToPointMode;
391}
392
395{
396 NS_LOG_FUNCTION(this << multicastGroup);
397 return Mac48Address::GetMulticast(multicastGroup);
398}
399
402{
403 NS_LOG_FUNCTION(this << addr);
404 return Mac48Address::GetMulticast(addr);
405}
406
407bool
409{
410 NS_LOG_FUNCTION(this);
411 return m_pointToPointMode;
412}
413
414bool
416{
417 NS_LOG_FUNCTION(this);
418 return false;
419}
420
421bool
422SimpleNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
423{
424 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
425
426 return SendFrom(packet, m_address, dest, protocolNumber);
427}
428
429bool
431 const Address& source,
432 const Address& dest,
433 uint16_t protocolNumber)
434{
435 NS_LOG_FUNCTION(this << p << source << dest << protocolNumber);
436 if (p->GetSize() > GetMtu())
437 {
438 return false;
439 }
440
443
444 SimpleTag tag;
445 tag.SetSrc(from);
446 tag.SetDst(to);
447 tag.SetProto(protocolNumber);
448
449 p->AddPacketTag(tag);
450
451 if (m_queue->Enqueue(p))
452 {
453 if (m_queue->GetNPackets() == 1 && !FinishTransmissionEvent.IsRunning())
454 {
456 }
457 return true;
458 }
459
460 return false;
461}
462
463void
465{
466 if (m_queue->GetNPackets() == 0)
467 {
468 return;
469 }
471 "Tried to transmit a packet while another transmission was in progress");
472 Ptr<Packet> packet = m_queue->Dequeue();
473
485 Time txTime = Time(0);
486 if (m_bps > DataRate(0))
487 {
488 txTime = m_bps.CalculateBytesTxTime(packet->GetSize());
489 }
492}
493
494void
496{
497 NS_LOG_FUNCTION(this);
498
499 SimpleTag tag;
500 packet->RemovePacketTag(tag);
501
502 Mac48Address src = tag.GetSrc();
503 Mac48Address dst = tag.GetDst();
504 uint16_t proto = tag.GetProto();
505
506 m_channel->Send(packet, proto, dst, src, this);
507
509}
510
513{
514 NS_LOG_FUNCTION(this);
515 return m_node;
516}
517
518void
520{
521 NS_LOG_FUNCTION(this << node);
522 m_node = node;
523}
524
525bool
527{
528 NS_LOG_FUNCTION(this);
529 return !m_pointToPointMode;
530}
531
532void
534{
535 NS_LOG_FUNCTION(this << &cb);
536 m_rxCallback = cb;
537}
538
539void
541{
542 NS_LOG_FUNCTION(this);
543 m_channel = nullptr;
544 m_node = nullptr;
545 m_receiveErrorModel = nullptr;
546 m_queue->Dispose();
548 {
550 }
552}
553
554void
556{
557 NS_LOG_FUNCTION(this << &cb);
559}
560
561bool
563{
564 NS_LOG_FUNCTION(this);
565 return true;
566}
567
568} // namespace ns3
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Callback template class.
Definition: callback.h:438
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Class for representing data rates.
Definition: data-rate.h:89
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:291
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
an EUI-48 address
Definition: mac48-address.h:46
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup() const
void CopyFrom(const uint8_t buffer[6])
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address GetBroadcast()
void CopyTo(uint8_t buffer[6]) const
bool IsBroadcast() const
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_HOST
Packet addressed to us.
Definition: net-device.h:301
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:303
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:305
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Template class for packet Queues.
Definition: queue.h:268
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
bool NeedsArp() const override
void DoDispose() override
Destructor implementation.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
The trace source fired when the phy layer drops a packet it has received due to the error model being...
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
void SetNode(Ptr< Node > node) override
void SetAddress(Address address) override
Set the address of this interface.
void SetIfIndex(const uint32_t index) override
void SetQueue(Ptr< Queue< Packet > > queue)
Attach a queue to the SimpleNetDevice.
bool SetMtu(const uint16_t mtu) override
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
static TypeId GetTypeId()
Get the type ID.
DataRate m_bps
The device nominal Data rate.
Ptr< Queue< Packet > > m_queue
The Queue for outgoing packets.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
Ptr< Channel > GetChannel() const override
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the SimpleNetDevice.
uint16_t GetMtu() const override
EventId FinishTransmissionEvent
the Tx Complete event
bool m_linkUp
Flag indicating whether or not the link is up.
void AddLinkChangeCallback(Callback< void > callback) override
bool m_pointToPointMode
Flag indicating whether or not the NetDevice is a Point to Point model.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
void FinishTransmission(Ptr< Packet > packet)
The FinishTransmission method is used internally to finish the process of sending a packet out on the...
uint32_t GetIfIndex() const override
Ptr< ErrorModel > m_receiveErrorModel
Receive error model.
bool IsMulticast() const override
Ptr< SimpleChannel > m_channel
the channel the device is connected to
Ptr< Node > m_node
Node this netDevice is associated to.
uint32_t m_ifIndex
Interface index.
Mac48Address m_address
MAC address.
Ptr< Queue< Packet > > GetQueue() const
Get a copy of the attached Queue.
Ptr< Node > GetNode() const override
bool IsLinkUp() const override
void StartTransmission()
The StartTransmission method is used internally to start the process of sending a packet out on the c...
Address GetBroadcast() const override
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool IsBroadcast() const override
bool SupportsSendFrom() const override
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Address GetAddress() const override
SimpleNetDevice tag to store source, destination and protocol of each packet.
Mac48Address GetDst() const
Get the destination address.
Mac48Address GetSrc() const
Get the source address.
Mac48Address m_dst
destination address
void Deserialize(TagBuffer i) override
static TypeId GetTypeId()
Get the type ID.
uint16_t m_protocolNumber
protocol number
void Print(std::ostream &os) const override
void SetSrc(Mac48Address src)
Set the source address.
uint32_t GetSerializedSize() const override
void SetProto(uint16_t proto)
Set the protocol number.
Mac48Address m_src
source address
uint16_t GetProto() const
Get the protocol number.
void Serialize(TagBuffer i) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetDst(Mac48Address dst)
Set the destination address.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
Hold variables of type string.
Definition: string.h:56
read and write tag data
Definition: tag-buffer.h:52
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:183
TAG_BUFFER_INLINE uint16_t ReadU16()
Definition: tag-buffer.h:206
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:129
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
tag a set of bytes in a packet
Definition: tag.h:39
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Definition: data-rate.h:296
Ptr< const AttributeChecker > MakeDataRateChecker()
Definition: data-rate.cc:31
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.