A Discrete-Event Network Simulator
API
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 "simple-channel.h"
22 
23 #include "ns3/boolean.h"
24 #include "ns3/error-model.h"
25 #include "ns3/log.h"
26 #include "ns3/node.h"
27 #include "ns3/packet.h"
28 #include "ns3/pointer.h"
29 #include "ns3/queue.h"
30 #include "ns3/simulator.h"
31 #include "ns3/string.h"
32 #include "ns3/tag.h"
33 #include "ns3/trace-source-accessor.h"
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("SimpleNetDevice");
39 
43 class 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:
95  uint16_t m_protocolNumber;
96 };
97 
99 
100 TypeId
102 {
103  static TypeId tid = TypeId("ns3::SimpleTag")
104  .SetParent<Tag>()
105  .SetGroupName("Network")
106  .AddConstructor<SimpleTag>();
107  return tid;
108 }
109 
110 TypeId
112 {
113  return GetTypeId();
114 }
115 
116 uint32_t
118 {
119  return 8 + 8 + 2;
120 }
121 
122 void
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 
133 void
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 
144 void
146 {
147  m_src = src;
148 }
149 
152 {
153  return m_src;
154 }
155 
156 void
158 {
159  m_dst = dst;
160 }
161 
164 {
165  return m_dst;
166 }
167 
168 void
169 SimpleTag::SetProto(uint16_t proto)
170 {
171  m_protocolNumber = proto;
172 }
173 
174 uint16_t
176 {
177  return m_protocolNumber;
178 }
179 
180 void
181 SimpleTag::Print(std::ostream& os) const
182 {
183  os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
184 }
185 
187 
188 TypeId
190 {
191  static TypeId tid =
192  TypeId("ns3::SimpleNetDevice")
193  .SetParent<NetDevice>()
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")),
214  MakeDataRateAccessor(&SimpleNetDevice::m_bps),
215  MakeDataRateChecker())
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 
234 void
235 SimpleNetDevice::Receive(Ptr<Packet> packet, uint16_t protocol, Mac48Address to, Mac48Address from)
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 
268  if (!m_promiscCallback.IsNull())
269  {
270  m_promiscCallback(this, packet, protocol, from, to, packetType);
271  }
272 }
273 
274 void
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 
291 void
293 {
294  NS_LOG_FUNCTION(this << q);
295  m_queue = q;
296 }
297 
298 void
300 {
301  NS_LOG_FUNCTION(this << em);
302  m_receiveErrorModel = em;
303 }
304 
305 void
306 SimpleNetDevice::SetIfIndex(const uint32_t index)
307 {
308  NS_LOG_FUNCTION(this << index);
309  m_ifIndex = index;
310 }
311 
312 uint32_t
314 {
315  NS_LOG_FUNCTION(this);
316  return m_ifIndex;
317 }
318 
321 {
322  NS_LOG_FUNCTION(this);
323  return m_channel;
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION(this << address);
331 }
332 
333 Address
335 {
336  //
337  // Implicit conversion from Mac48Address to Address
338  //
339  NS_LOG_FUNCTION(this);
340  return m_address;
341 }
342 
343 bool
344 SimpleNetDevice::SetMtu(const uint16_t mtu)
345 {
346  NS_LOG_FUNCTION(this << mtu);
347  m_mtu = mtu;
348  return true;
349 }
350 
351 uint16_t
353 {
354  NS_LOG_FUNCTION(this);
355  return m_mtu;
356 }
357 
358 bool
360 {
361  NS_LOG_FUNCTION(this);
362  return m_linkUp;
363 }
364 
365 void
367 {
368  NS_LOG_FUNCTION(this << &callback);
370 }
371 
372 bool
374 {
375  NS_LOG_FUNCTION(this);
376  if (m_pointToPointMode)
377  {
378  return false;
379  }
380  return true;
381 }
382 
383 Address
385 {
386  NS_LOG_FUNCTION(this);
387  return Mac48Address("ff:ff:ff:ff:ff:ff");
388 }
389 
390 bool
392 {
393  NS_LOG_FUNCTION(this);
394  if (m_pointToPointMode)
395  {
396  return false;
397  }
398  return true;
399 }
400 
401 Address
403 {
404  NS_LOG_FUNCTION(this << multicastGroup);
405  return Mac48Address::GetMulticast(multicastGroup);
406 }
407 
408 Address
410 {
411  NS_LOG_FUNCTION(this << addr);
412  return Mac48Address::GetMulticast(addr);
413 }
414 
415 bool
417 {
418  NS_LOG_FUNCTION(this);
419  if (m_pointToPointMode)
420  {
421  return true;
422  }
423  return false;
424 }
425 
426 bool
428 {
429  NS_LOG_FUNCTION(this);
430  return false;
431 }
432 
433 bool
434 SimpleNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
435 {
436  NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
437 
438  return SendFrom(packet, m_address, dest, protocolNumber);
439 }
440 
441 bool
443  const Address& source,
444  const Address& dest,
445  uint16_t protocolNumber)
446 {
447  NS_LOG_FUNCTION(this << p << source << dest << protocolNumber);
448  if (p->GetSize() > GetMtu())
449  {
450  return false;
451  }
452 
455 
456  SimpleTag tag;
457  tag.SetSrc(from);
458  tag.SetDst(to);
459  tag.SetProto(protocolNumber);
460 
461  p->AddPacketTag(tag);
462 
463  if (m_queue->Enqueue(p))
464  {
465  if (m_queue->GetNPackets() == 1 && !FinishTransmissionEvent.IsRunning())
466  {
468  }
469  return true;
470  }
471 
472  return false;
473 }
474 
475 void
477 {
478  if (m_queue->GetNPackets() == 0)
479  {
480  return;
481  }
483  "Tried to transmit a packet while another transmission was in progress");
484  Ptr<Packet> packet = m_queue->Dequeue();
485 
497  Time txTime = Time(0);
498  if (m_bps > DataRate(0))
499  {
500  txTime = m_bps.CalculateBytesTxTime(packet->GetSize());
501  }
504 }
505 
506 void
508 {
509  NS_LOG_FUNCTION(this);
510 
511  SimpleTag tag;
512  packet->RemovePacketTag(tag);
513 
514  Mac48Address src = tag.GetSrc();
515  Mac48Address dst = tag.GetDst();
516  uint16_t proto = tag.GetProto();
517 
518  m_channel->Send(packet, proto, dst, src, this);
519 
521 }
522 
523 Ptr<Node>
525 {
526  NS_LOG_FUNCTION(this);
527  return m_node;
528 }
529 
530 void
532 {
533  NS_LOG_FUNCTION(this << node);
534  m_node = node;
535 }
536 
537 bool
539 {
540  NS_LOG_FUNCTION(this);
541  if (m_pointToPointMode)
542  {
543  return false;
544  }
545  return true;
546 }
547 
548 void
550 {
551  NS_LOG_FUNCTION(this << &cb);
552  m_rxCallback = cb;
553 }
554 
555 void
557 {
558  NS_LOG_FUNCTION(this);
559  m_channel = nullptr;
560  m_node = nullptr;
561  m_receiveErrorModel = nullptr;
562  m_queue->Dispose();
564  {
566  }
568 }
569 
570 void
572 {
573  NS_LOG_FUNCTION(this << &cb);
574  m_promiscCallback = cb;
575 }
576 
577 bool
579 {
580  NS_LOG_FUNCTION(this);
581  return true;
582 }
583 
584 } // namespace ns3
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Boolean.
Definition: boolean.h:37
bool IsNull() const
Check for null implementation.
Definition: callback.h:572
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:291
AttributeValue implementation for DataRate.
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:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
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)
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:353
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:986
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:979
Hold objects of type Ptr<T>.
Definition: pointer.h:37
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
void SetQueue(Ptr< Queue< Packet >> queue)
Attach a queue to the SimpleNetDevice.
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
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:568
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:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#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)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: pointer.h:231
#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 ",...
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
#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.
address
Definition: first.py:40
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:81
mac
Definition: third.py:85