A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
simple-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "simple-net-device.h"
21 #include "simple-channel.h"
22 #include "ns3/node.h"
23 #include "ns3/packet.h"
24 #include "ns3/log.h"
25 #include "ns3/pointer.h"
26 #include "ns3/error-model.h"
27 #include "ns3/trace-source-accessor.h"
28 #include "ns3/boolean.h"
29 #include "ns3/tag.h"
30 #include "ns3/simulator.h"
31 #include "ns3/drop-tail-queue.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("SimpleNetDevice");
34 
35 namespace ns3 {
36 
40 class SimpleTag : public Tag {
41 public:
42  static TypeId GetTypeId (void);
43  virtual TypeId GetInstanceTypeId (void) const;
44 
45  virtual uint32_t GetSerializedSize (void) const;
46  virtual void Serialize (TagBuffer i) const;
47  virtual void Deserialize (TagBuffer i);
48 
49  void SetSrc (Mac48Address src);
50  Mac48Address GetSrc (void) const;
51 
52  void SetDst (Mac48Address dst);
53  Mac48Address GetDst (void) const;
54 
55  void SetProto (uint16_t proto);
56  uint16_t GetProto (void) const;
57 
58  void Print (std::ostream &os) const;
59 
60 private:
63  uint16_t m_protocolNumber;
64 };
65 
66 
68 
69 TypeId
71 {
72  static TypeId tid = TypeId ("SimpleTag")
73  .SetParent<Tag> ()
74  .AddConstructor<SimpleTag> ()
75  ;
76  return tid;
77 }
78 TypeId
80 {
81  return GetTypeId ();
82 }
83 
84 uint32_t
86 {
87  return 8+8+2;
88 }
89 void
91 {
92  uint8_t mac[6];
93  m_src.CopyTo (mac);
94  i.Write (mac, 6);
95  m_dst.CopyTo (mac);
96  i.Write (mac, 6);
98 }
99 void
101 {
102  uint8_t mac[6];
103  i.Read (mac, 6);
104  m_src.CopyFrom (mac);
105  i.Read (mac, 6);
106  m_dst.CopyFrom (mac);
107  m_protocolNumber = i.ReadU16 ();
108 }
109 
110 void
112 {
113  m_src = src;
114 }
115 
117 SimpleTag::GetSrc (void) const
118 {
119  return m_src;
120 }
121 
122 void
124 {
125  m_dst = dst;
126 }
127 
129 SimpleTag::GetDst (void) const
130 {
131  return m_dst;
132 }
133 
134 void
135 SimpleTag::SetProto (uint16_t proto)
136 {
137  m_protocolNumber = proto;
138 }
139 
140 uint16_t
142 {
143  return m_protocolNumber;
144 }
145 
146 void
147 SimpleTag::Print (std::ostream &os) const
148 {
149  os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
150 }
151 
152 
153 
155 
156 TypeId
158 {
159  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
160  .SetParent<NetDevice> ()
161  .AddConstructor<SimpleNetDevice> ()
162  .AddAttribute ("ReceiveErrorModel",
163  "The receiver error model used to simulate packet loss",
164  PointerValue (),
165  MakePointerAccessor (&SimpleNetDevice::m_receiveErrorModel),
166  MakePointerChecker<ErrorModel> ())
167  .AddAttribute ("PointToPointMode",
168  "The device is configured in Point to Point mode",
169  BooleanValue (false),
170  MakeBooleanAccessor (&SimpleNetDevice::m_pointToPointMode),
171  MakeBooleanChecker ())
172  .AddAttribute ("TxQueue",
173  "A queue to use as the transmit queue in the device.",
174  PointerValue (CreateObject<DropTailQueue> ()),
175  MakePointerAccessor (&SimpleNetDevice::m_queue),
176  MakePointerChecker<Queue> ())
177  .AddAttribute ("DataRate",
178  "The default data rate for point to point links. Zero means infinite",
179  DataRateValue (DataRate ("0b/s")),
180  MakeDataRateAccessor (&SimpleNetDevice::m_bps),
181  MakeDataRateChecker ())
182  .AddTraceSource ("PhyRxDrop",
183  "Trace source indicating a packet has been dropped by the device during reception",
185  ;
186  return tid;
187 }
188 
190  : m_channel (0),
191  m_node (0),
192  m_mtu (0xffff),
193  m_ifIndex (0),
194  m_linkUp (false)
195 {
196  NS_LOG_FUNCTION (this);
197 }
198 
199 void
200 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
201  Mac48Address to, Mac48Address from)
202 {
203  NS_LOG_FUNCTION (this << packet << protocol << to << from);
204  NetDevice::PacketType packetType;
205 
206  if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
207  {
208  m_phyRxDropTrace (packet);
209  return;
210  }
211 
212  if (to == m_address)
213  {
214  packetType = NetDevice::PACKET_HOST;
215  }
216  else if (to.IsBroadcast ())
217  {
218  packetType = NetDevice::PACKET_BROADCAST;
219  }
220  else if (to.IsGroup ())
221  {
222  packetType = NetDevice::PACKET_MULTICAST;
223  }
224  else
225  {
226  packetType = NetDevice::PACKET_OTHERHOST;
227  }
228 
229  if (packetType != NetDevice::PACKET_OTHERHOST)
230  {
231  m_rxCallback (this, packet, protocol, from);
232  }
233 
234  if (!m_promiscCallback.IsNull ())
235  {
236  m_promiscCallback (this, packet, protocol, from, to, packetType);
237  }
238 }
239 
240 void
242 {
243  NS_LOG_FUNCTION (this << channel);
244  m_channel = channel;
245  m_channel->Add (this);
246  m_linkUp = true;
248 }
249 
252 {
253  NS_LOG_FUNCTION (this);
254  return m_queue;
255 }
256 
257 void
259 {
260  NS_LOG_FUNCTION (this << q);
261  m_queue = q;
262 }
263 
264 void
266 {
267  NS_LOG_FUNCTION (this << em);
268  m_receiveErrorModel = em;
269 }
270 
271 void
272 SimpleNetDevice::SetIfIndex (const uint32_t index)
273 {
274  NS_LOG_FUNCTION (this << index);
275  m_ifIndex = index;
276 }
277 uint32_t
279 {
280  NS_LOG_FUNCTION (this);
281  return m_ifIndex;
282 }
285 {
286  NS_LOG_FUNCTION (this);
287  return m_channel;
288 }
289 void
291 {
292  NS_LOG_FUNCTION (this << address);
294 }
295 Address
297 {
298  //
299  // Implicit conversion from Mac48Address to Address
300  //
301  NS_LOG_FUNCTION (this);
302  return m_address;
303 }
304 bool
305 SimpleNetDevice::SetMtu (const uint16_t mtu)
306 {
307  NS_LOG_FUNCTION (this << mtu);
308  m_mtu = mtu;
309  return true;
310 }
311 uint16_t
313 {
314  NS_LOG_FUNCTION (this);
315  return m_mtu;
316 }
317 bool
319 {
320  NS_LOG_FUNCTION (this);
321  return m_linkUp;
322 }
323 void
325 {
326  NS_LOG_FUNCTION (this << &callback);
328 }
329 bool
331 {
332  NS_LOG_FUNCTION (this);
333  if (m_pointToPointMode)
334  {
335  return false;
336  }
337  return true;
338 }
339 Address
341 {
342  NS_LOG_FUNCTION (this);
343  return Mac48Address ("ff:ff:ff:ff:ff:ff");
344 }
345 bool
347 {
348  NS_LOG_FUNCTION (this);
349  if (m_pointToPointMode)
350  {
351  return false;
352  }
353  return true;
354 }
355 Address
357 {
358  NS_LOG_FUNCTION (this << multicastGroup);
359  return Mac48Address::GetMulticast (multicastGroup);
360 }
361 
363 {
364  NS_LOG_FUNCTION (this << addr);
365  return Mac48Address::GetMulticast (addr);
366 }
367 
368 bool
370 {
371  NS_LOG_FUNCTION (this);
372  if (m_pointToPointMode)
373  {
374  return true;
375  }
376  return false;
377 }
378 
379 bool
381 {
382  NS_LOG_FUNCTION (this);
383  return false;
384 }
385 
386 bool
387 SimpleNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
388 {
389  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
390 
391  return SendFrom (packet, m_address, dest, protocolNumber);
392 }
393 
394 bool
395 SimpleNetDevice::SendFrom (Ptr<Packet> p, const Address& source, const Address& dest, uint16_t protocolNumber)
396 {
397  NS_LOG_FUNCTION (this << p << source << dest << protocolNumber);
398  if (p->GetSize () > GetMtu ())
399  {
400  return false;
401  }
402  Ptr<Packet> packet = p->Copy ();
403 
405  Mac48Address from = Mac48Address::ConvertFrom (source);
406 
407  SimpleTag tag;
408  tag.SetSrc (from);
409  tag.SetDst (to);
410  tag.SetProto (protocolNumber);
411 
412  p->AddPacketTag (tag);
413 
414  if (m_queue->Enqueue (p))
415  {
416  if (m_queue->GetNPackets () == 1)
417  {
418  p = m_queue->Dequeue ();
419  p->RemovePacketTag (tag);
420  Time txTime = Time (0);
421  if (m_bps > DataRate (0))
422  {
423  txTime = Seconds (m_bps.CalculateTxTime (packet->GetSize ()));
424  }
425  m_channel->Send (p, protocolNumber, to, from, this);
427  }
428  return true;
429  }
430 
431 
432  m_channel->Send (packet, protocolNumber, to, from, this);
433  return true;
434 }
435 
436 
437 void
439 {
440  NS_LOG_FUNCTION (this);
441 
442  if (m_queue->GetNPackets () == 0)
443  {
444  return;
445  }
446 
447  Ptr<Packet> packet = m_queue->Dequeue ();
448 
449  SimpleTag tag;
450  packet->RemovePacketTag (tag);
451 
452  Mac48Address src = tag.GetSrc ();
453  Mac48Address dst = tag.GetDst ();
454  uint16_t proto = tag.GetProto ();
455 
456  m_channel->Send (packet, proto, dst, src, this);
457 
458  if (m_queue->GetNPackets ())
459  {
460  Time txTime = Time (0);
461  if (m_bps > DataRate (0))
462  {
463  txTime = Seconds (m_bps.CalculateTxTime (packet->GetSize ()));
464  }
466  }
467 
468  return;
469 }
470 
471 Ptr<Node>
473 {
474  NS_LOG_FUNCTION (this);
475  return m_node;
476 }
477 void
479 {
480  NS_LOG_FUNCTION (this << node);
481  m_node = node;
482 }
483 bool
485 {
486  NS_LOG_FUNCTION (this);
487  if (m_pointToPointMode)
488  {
489  return false;
490  }
491  return true;
492 }
493 void
495 {
496  NS_LOG_FUNCTION (this << &cb);
497  m_rxCallback = cb;
498 }
499 
500 void
502 {
503  NS_LOG_FUNCTION (this);
504  m_channel = 0;
505  m_node = 0;
507  m_queue->DequeueAll ();
509  {
511  }
513 }
514 
515 
516 void
518 {
519  NS_LOG_FUNCTION (this << &cb);
520  m_promiscCallback = cb;
521 }
522 
523 bool
525 {
526  NS_LOG_FUNCTION (this);
527  return true;
528 }
529 
530 } // namespace ns3
virtual Address GetAddress(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Mac48Address m_src
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
EventId TransmitCompleteEvent
the Tx Complete event
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Hold a bool native type.
Definition: boolean.h:38
Packet addressed to someone else.
Definition: net-device.h:282
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Ptr< Node > m_node
Node this netDevice is associated to.
virtual TypeId GetInstanceTypeId(void) const
virtual Ptr< Channel > GetChannel(void) const
uint16_t m_protocolNumber
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1066
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
bool IsBroadcast(void) const
virtual void SetIfIndex(const uint32_t index)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
bool IsRunning(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:59
virtual bool IsBroadcast(void) const
virtual bool NeedsArp(void) const
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
bool m_pointToPointMode
Flag indicating whether or not the NetDevice is a Point to Point model.
void SetProto(uint16_t proto)
virtual bool IsMulticast(void) const
a polymophic address class
Definition: address.h:86
bool m_linkUp
Flag indicating whether or not the link is up.
Class for representing data rates.
Definition: data-rate.h:71
virtual void SetNode(Ptr< Node > node)
void CopyTo(uint8_t buffer[6]) const
double CalculateTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:229
void SetDst(Mac48Address dst)
static Mac48Address GetMulticast(Ipv4Address address)
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual bool SetMtu(const uint16_t mtu)
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Mac48Address m_address
MAC address.
Ptr< Queue > m_queue
The Queue for outgoing packets.
Mac48Address GetSrc(void) const
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
Internal tag to store source, destination and protocol of each packet.
virtual void Serialize(TagBuffer i) const
static Mac48Address ConvertFrom(const Address &address)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
tag a set of bytes in a packet
Definition: tag.h:36
hold objects of type Ptr
Definition: pointer.h:33
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
bool IsGroup(void) const
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
an EUI-48 address
Definition: mac48-address.h:41
virtual uint16_t GetMtu(void) const
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the SimpleNetDevice.
Mac48Address m_dst
void CopyFrom(const uint8_t buffer[6])
virtual Ptr< Node > GetNode(void) const
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
uint16_t GetProto(void) const
Mac48Address GetDst(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
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...
void SetSrc(Mac48Address src)
read and write tag data
Definition: tag-buffer.h:51
void ConnectWithoutContext(const CallbackBase &callback)
Packet addressed oo us.
Definition: net-device.h:276
Network layer to device interface.
Definition: net-device.h:75
virtual void AddLinkChangeCallback(Callback< void > callback)
hold objects of type ns3::DataRate
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:848
virtual void SetAddress(Address address)
Set the address of this interface.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
Ptr< ErrorModel > m_receiveErrorModel
Receive error model.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:205
Ptr< SimpleChannel > m_channel
the channel the device is connected to
virtual void Deserialize(TagBuffer i)
tuple address
Definition: first.py:37
void TransmitComplete(void)
The TransmitComplete method is used internally to finish the process of sending a packet out on the c...
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:274
void SetQueue(Ptr< Queue > queue)
Attach a queue to the SimpleNetDevice.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Packet addressed to multicast group.
Definition: net-device.h:280
Ptr< Queue > GetQueue(void) const
Get a copy of the attached Queue.
DataRate m_bps
The device nominal Data rate.
a unique identifier for an interface.
Definition: type-id.h:49
virtual Address GetBroadcast(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
Packet addressed to all.
Definition: net-device.h:278
void Print(std::ostream &os) const
virtual uint32_t GetIfIndex(void) const
static TypeId GetTypeId(void)
static TypeId GetTypeId(void)
Get the type ID.
virtual bool SupportsSendFrom(void) const
virtual bool IsLinkUp(void) const
virtual uint32_t GetSerializedSize(void) const
uint32_t m_ifIndex
Interface index.