A Discrete-Event Network Simulator
API
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/string.h"
30 #include "ns3/tag.h"
31 #include "ns3/simulator.h"
32 #include "ns3/drop-tail-queue.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("SimpleNetDevice");
37 
41 class SimpleTag : public Tag {
42 public:
47  static TypeId GetTypeId (void);
48  virtual TypeId GetInstanceTypeId (void) const;
49 
50  virtual uint32_t GetSerializedSize (void) const;
51  virtual void Serialize (TagBuffer i) const;
52  virtual void Deserialize (TagBuffer i);
53 
58  void SetSrc (Mac48Address src);
63  Mac48Address GetSrc (void) const;
64 
69  void SetDst (Mac48Address dst);
74  Mac48Address GetDst (void) const;
75 
80  void SetProto (uint16_t proto);
85  uint16_t GetProto (void) const;
86 
87  void Print (std::ostream &os) const;
88 
89 private:
92  uint16_t m_protocolNumber;
93 };
94 
95 
97 
98 TypeId
100 {
101  static TypeId tid = TypeId ("SimpleTag")
102  .SetParent<Tag> ()
103  .AddConstructor<SimpleTag> ()
104  ;
105  return tid;
106 }
107 TypeId
109 {
110  return GetTypeId ();
111 }
112 
113 uint32_t
115 {
116  return 8+8+2;
117 }
118 void
120 {
121  uint8_t mac[6];
122  m_src.CopyTo (mac);
123  i.Write (mac, 6);
124  m_dst.CopyTo (mac);
125  i.Write (mac, 6);
127 }
128 void
130 {
131  uint8_t mac[6];
132  i.Read (mac, 6);
133  m_src.CopyFrom (mac);
134  i.Read (mac, 6);
135  m_dst.CopyFrom (mac);
136  m_protocolNumber = i.ReadU16 ();
137 }
138 
139 void
141 {
142  m_src = src;
143 }
144 
146 SimpleTag::GetSrc (void) const
147 {
148  return m_src;
149 }
150 
151 void
153 {
154  m_dst = dst;
155 }
156 
158 SimpleTag::GetDst (void) const
159 {
160  return m_dst;
161 }
162 
163 void
164 SimpleTag::SetProto (uint16_t proto)
165 {
166  m_protocolNumber = proto;
167 }
168 
169 uint16_t
171 {
172  return m_protocolNumber;
173 }
174 
175 void
176 SimpleTag::Print (std::ostream &os) const
177 {
178  os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
179 }
180 
181 
182 
184 
185 TypeId
187 {
188  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
189  .SetParent<NetDevice> ()
190  .AddConstructor<SimpleNetDevice> ()
191  .AddAttribute ("ReceiveErrorModel",
192  "The receiver error model used to simulate packet loss",
193  PointerValue (),
195  MakePointerChecker<ErrorModel> ())
196  .AddAttribute ("PointToPointMode",
197  "The device is configured in Point to Point mode",
198  BooleanValue (false),
201  .AddAttribute ("TxQueue",
202  "A queue to use as the transmit queue in the device.",
203  StringValue ("ns3::DropTailQueue"),
205  MakePointerChecker<Queue> ())
206  .AddAttribute ("DataRate",
207  "The default data rate for point to point links. Zero means infinite",
208  DataRateValue (DataRate ("0b/s")),
211  .AddTraceSource ("PhyRxDrop",
212  "Trace source indicating a packet has been dropped "
213  "by the device during reception",
215  "ns3::Packet::TracedCallback")
216  ;
217  return tid;
218 }
219 
221  : m_channel (0),
222  m_node (0),
223  m_mtu (0xffff),
224  m_ifIndex (0),
225  m_linkUp (false)
226 {
227  NS_LOG_FUNCTION (this);
228 }
229 
230 void
231 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
232  Mac48Address to, Mac48Address from)
233 {
234  NS_LOG_FUNCTION (this << packet << protocol << to << from);
235  NetDevice::PacketType packetType;
236 
237  if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
238  {
239  m_phyRxDropTrace (packet);
240  return;
241  }
242 
243  if (to == m_address)
244  {
245  packetType = NetDevice::PACKET_HOST;
246  }
247  else if (to.IsBroadcast ())
248  {
249  packetType = NetDevice::PACKET_BROADCAST;
250  }
251  else if (to.IsGroup ())
252  {
253  packetType = NetDevice::PACKET_MULTICAST;
254  }
255  else
256  {
257  packetType = NetDevice::PACKET_OTHERHOST;
258  }
259 
260  if (packetType != NetDevice::PACKET_OTHERHOST)
261  {
262  m_rxCallback (this, packet, protocol, from);
263  }
264 
265  if (!m_promiscCallback.IsNull ())
266  {
267  m_promiscCallback (this, packet, protocol, from, to, packetType);
268  }
269 }
270 
271 void
273 {
274  NS_LOG_FUNCTION (this << channel);
275  m_channel = channel;
276  m_channel->Add (this);
277  m_linkUp = true;
279 }
280 
283 {
284  NS_LOG_FUNCTION (this);
285  return m_queue;
286 }
287 
288 void
290 {
291  NS_LOG_FUNCTION (this << q);
292  m_queue = q;
293 }
294 
295 void
297 {
298  NS_LOG_FUNCTION (this << em);
299  m_receiveErrorModel = em;
300 }
301 
302 void
303 SimpleNetDevice::SetIfIndex (const uint32_t index)
304 {
305  NS_LOG_FUNCTION (this << index);
306  m_ifIndex = index;
307 }
308 uint32_t
310 {
311  NS_LOG_FUNCTION (this);
312  return m_ifIndex;
313 }
316 {
317  NS_LOG_FUNCTION (this);
318  return m_channel;
319 }
320 void
322 {
323  NS_LOG_FUNCTION (this << address);
325 }
326 Address
328 {
329  //
330  // Implicit conversion from Mac48Address to Address
331  //
332  NS_LOG_FUNCTION (this);
333  return m_address;
334 }
335 bool
336 SimpleNetDevice::SetMtu (const uint16_t mtu)
337 {
338  NS_LOG_FUNCTION (this << mtu);
339  m_mtu = mtu;
340  return true;
341 }
342 uint16_t
344 {
345  NS_LOG_FUNCTION (this);
346  return m_mtu;
347 }
348 bool
350 {
351  NS_LOG_FUNCTION (this);
352  return m_linkUp;
353 }
354 void
356 {
357  NS_LOG_FUNCTION (this << &callback);
359 }
360 bool
362 {
363  NS_LOG_FUNCTION (this);
364  if (m_pointToPointMode)
365  {
366  return false;
367  }
368  return true;
369 }
370 Address
372 {
373  NS_LOG_FUNCTION (this);
374  return Mac48Address ("ff:ff:ff:ff:ff:ff");
375 }
376 bool
378 {
379  NS_LOG_FUNCTION (this);
380  if (m_pointToPointMode)
381  {
382  return false;
383  }
384  return true;
385 }
386 Address
388 {
389  NS_LOG_FUNCTION (this << multicastGroup);
390  return Mac48Address::GetMulticast (multicastGroup);
391 }
392 
394 {
395  NS_LOG_FUNCTION (this << addr);
396  return Mac48Address::GetMulticast (addr);
397 }
398 
399 bool
401 {
402  NS_LOG_FUNCTION (this);
403  if (m_pointToPointMode)
404  {
405  return true;
406  }
407  return false;
408 }
409 
410 bool
412 {
413  NS_LOG_FUNCTION (this);
414  return false;
415 }
416 
417 bool
418 SimpleNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
419 {
420  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
421 
422  return SendFrom (packet, m_address, dest, protocolNumber);
423 }
424 
425 bool
426 SimpleNetDevice::SendFrom (Ptr<Packet> p, const Address& source, const Address& dest, uint16_t protocolNumber)
427 {
428  NS_LOG_FUNCTION (this << p << source << dest << protocolNumber);
429  if (p->GetSize () > GetMtu ())
430  {
431  return false;
432  }
433  Ptr<Packet> packet = p->Copy ();
434 
436  Mac48Address from = Mac48Address::ConvertFrom (source);
437 
438  SimpleTag tag;
439  tag.SetSrc (from);
440  tag.SetDst (to);
441  tag.SetProto (protocolNumber);
442 
443  p->AddPacketTag (tag);
444 
445  if (m_queue->Enqueue (p))
446  {
447  if (m_queue->GetNPackets () == 1)
448  {
449  p = m_queue->Dequeue ();
450  p->RemovePacketTag (tag);
451  Time txTime = Time (0);
452  if (m_bps > DataRate (0))
453  {
454  txTime = Seconds (m_bps.CalculateTxTime (packet->GetSize ()));
455  }
456  m_channel->Send (p, protocolNumber, to, from, this);
458  }
459  return true;
460  }
461 
462 
463  m_channel->Send (packet, protocolNumber, to, from, this);
464  return true;
465 }
466 
467 
468 void
470 {
471  NS_LOG_FUNCTION (this);
472 
473  if (m_queue->GetNPackets () == 0)
474  {
475  return;
476  }
477 
478  Ptr<Packet> packet = m_queue->Dequeue ();
479 
480  SimpleTag tag;
481  packet->RemovePacketTag (tag);
482 
483  Mac48Address src = tag.GetSrc ();
484  Mac48Address dst = tag.GetDst ();
485  uint16_t proto = tag.GetProto ();
486 
487  m_channel->Send (packet, proto, dst, src, this);
488 
489  if (m_queue->GetNPackets ())
490  {
491  Time txTime = Time (0);
492  if (m_bps > DataRate (0))
493  {
494  txTime = Seconds (m_bps.CalculateTxTime (packet->GetSize ()));
495  }
497  }
498 
499  return;
500 }
501 
502 Ptr<Node>
504 {
505  NS_LOG_FUNCTION (this);
506  return m_node;
507 }
508 void
510 {
511  NS_LOG_FUNCTION (this << node);
512  m_node = node;
513 }
514 bool
516 {
517  NS_LOG_FUNCTION (this);
518  if (m_pointToPointMode)
519  {
520  return false;
521  }
522  return true;
523 }
524 void
526 {
527  NS_LOG_FUNCTION (this << &cb);
528  m_rxCallback = cb;
529 }
530 
531 void
533 {
534  NS_LOG_FUNCTION (this);
535  m_channel = 0;
536  m_node = 0;
538  m_queue->DequeueAll ();
540  {
542  }
544 }
545 
546 
547 void
549 {
550  NS_LOG_FUNCTION (this << &cb);
551  m_promiscCallback = cb;
552 }
553 
554 bool
556 {
557  NS_LOG_FUNCTION (this);
558  return true;
559 }
560 
561 } // namespace ns3
virtual Address GetAddress(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Mac48Address m_src
source address
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 "...
AttributeValue implementation for Boolean.
Definition: boolean.h:34
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< Node > m_node
Node this netDevice is associated to.
Hold variables of type string.
Definition: string.h:41
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual Ptr< Channel > GetChannel(void) const
uint16_t m_protocolNumber
protocol number
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
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:81
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
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:1072
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:272
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
bool IsBroadcast(void) const
virtual void SetIfIndex(const uint32_t index)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:338
Packet addressed to multicast group.
Definition: net-device.h:278
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
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:819
bool m_pointToPointMode
Flag indicating whether or not the NetDevice is a Point to Point model.
Packet addressed oo us.
Definition: net-device.h:274
void SetProto(uint16_t proto)
Set the protocol number.
virtual bool IsMulticast(void) const
a polymophic address class
Definition: address.h:90
Ptr< const AttributeChecker > MakeDataRateChecker(void)
Definition: data-rate.cc:30
bool m_linkUp
Flag indicating whether or not the link is up.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Class for representing data rates.
Definition: data-rate.h:87
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)
Set the destination address.
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:210
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
Get the source address.
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
Ptr< const AttributeAccessor > MakeDataRateAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: data-rate.h:219
SimpleNetDevice 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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
bool IsGroup(void) const
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Packet addressed to someone else.
Definition: net-device.h:280
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
an EUI-48 address
Definition: mac48-address.h:43
virtual uint16_t GetMtu(void) const
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the SimpleNetDevice.
Mac48Address m_dst
destination address
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
Get the protocol number.
Mac48Address GetDst(void) const
Get the destination address.
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
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)
Set the source address.
read and write tag data
Definition: tag-buffer.h:51
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:75
virtual void AddLinkChangeCallback(Callback< void > callback)
AttributeValue implementation for DataRate.
Definition: data-rate.h:219
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
Packet addressed to all.
Definition: net-device.h:276
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:859
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:53
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
virtual void DoDispose(void)
Destructor implementation.
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...
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.
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:51
virtual Address GetBroadcast(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void Print(std::ostream &os) const
virtual uint32_t GetIfIndex(void) const
static TypeId GetTypeId(void)
Get the type ID.
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.