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/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 ("ns3::SimpleTag")
102  .SetParent<Tag> ()
103  .SetGroupName("Network")
104  .AddConstructor<SimpleTag> ()
105  ;
106  return tid;
107 }
108 TypeId
110 {
111  return GetTypeId ();
112 }
113 
114 uint32_t
116 {
117  return 8+8+2;
118 }
119 void
121 {
122  uint8_t mac[6];
123  m_src.CopyTo (mac);
124  i.Write (mac, 6);
125  m_dst.CopyTo (mac);
126  i.Write (mac, 6);
128 }
129 void
131 {
132  uint8_t mac[6];
133  i.Read (mac, 6);
134  m_src.CopyFrom (mac);
135  i.Read (mac, 6);
136  m_dst.CopyFrom (mac);
137  m_protocolNumber = i.ReadU16 ();
138 }
139 
140 void
142 {
143  m_src = src;
144 }
145 
147 SimpleTag::GetSrc (void) const
148 {
149  return m_src;
150 }
151 
152 void
154 {
155  m_dst = dst;
156 }
157 
159 SimpleTag::GetDst (void) const
160 {
161  return m_dst;
162 }
163 
164 void
165 SimpleTag::SetProto (uint16_t proto)
166 {
167  m_protocolNumber = proto;
168 }
169 
170 uint16_t
172 {
173  return m_protocolNumber;
174 }
175 
176 void
177 SimpleTag::Print (std::ostream &os) const
178 {
179  os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
180 }
181 
182 
183 
185 
186 TypeId
188 {
189  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
190  .SetParent<NetDevice> ()
191  .SetGroupName("Network")
192  .AddConstructor<SimpleNetDevice> ()
193  .AddAttribute ("ReceiveErrorModel",
194  "The receiver error model used to simulate packet loss",
195  PointerValue (),
197  MakePointerChecker<ErrorModel> ())
198  .AddAttribute ("PointToPointMode",
199  "The device is configured in Point to Point mode",
200  BooleanValue (false),
203  .AddAttribute ("TxQueue",
204  "A queue to use as the transmit queue in the device.",
205  StringValue ("ns3::DropTailQueue<Packet>"),
208  .AddAttribute ("DataRate",
209  "The default data rate for point to point links. Zero means infinite",
210  DataRateValue (DataRate ("0b/s")),
213  .AddTraceSource ("PhyRxDrop",
214  "Trace source indicating a packet has been dropped "
215  "by the device during reception",
217  "ns3::Packet::TracedCallback")
218  ;
219  return tid;
220 }
221 
223  : m_channel (0),
224  m_node (0),
225  m_mtu (0xffff),
226  m_ifIndex (0),
227  m_linkUp (false)
228 {
229  NS_LOG_FUNCTION (this);
230 }
231 
232 void
233 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
234  Mac48Address to, Mac48Address from)
235 {
236  NS_LOG_FUNCTION (this << packet << protocol << to << from);
237  NetDevice::PacketType packetType;
238 
239  if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
240  {
241  m_phyRxDropTrace (packet);
242  return;
243  }
244 
245  if (to == m_address)
246  {
247  packetType = NetDevice::PACKET_HOST;
248  }
249  else if (to.IsBroadcast ())
250  {
251  packetType = NetDevice::PACKET_BROADCAST;
252  }
253  else if (to.IsGroup ())
254  {
255  packetType = NetDevice::PACKET_MULTICAST;
256  }
257  else
258  {
259  packetType = NetDevice::PACKET_OTHERHOST;
260  }
261 
262  if (packetType != NetDevice::PACKET_OTHERHOST)
263  {
264  m_rxCallback (this, packet, protocol, from);
265  }
266 
267  if (!m_promiscCallback.IsNull ())
268  {
269  m_promiscCallback (this, packet, protocol, from, to, packetType);
270  }
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION (this << channel);
277  m_channel = channel;
278  m_channel->Add (this);
279  m_linkUp = true;
281 }
282 
285 {
286  NS_LOG_FUNCTION (this);
287  return m_queue;
288 }
289 
290 void
292 {
293  NS_LOG_FUNCTION (this << q);
294  m_queue = q;
295 }
296 
297 void
299 {
300  NS_LOG_FUNCTION (this << em);
301  m_receiveErrorModel = em;
302 }
303 
304 void
305 SimpleNetDevice::SetIfIndex (const uint32_t index)
306 {
307  NS_LOG_FUNCTION (this << index);
308  m_ifIndex = index;
309 }
310 uint32_t
312 {
313  NS_LOG_FUNCTION (this);
314  return m_ifIndex;
315 }
318 {
319  NS_LOG_FUNCTION (this);
320  return m_channel;
321 }
322 void
324 {
325  NS_LOG_FUNCTION (this << address);
327 }
328 Address
330 {
331  //
332  // Implicit conversion from Mac48Address to Address
333  //
334  NS_LOG_FUNCTION (this);
335  return m_address;
336 }
337 bool
338 SimpleNetDevice::SetMtu (const uint16_t mtu)
339 {
340  NS_LOG_FUNCTION (this << mtu);
341  m_mtu = mtu;
342  return true;
343 }
344 uint16_t
346 {
347  NS_LOG_FUNCTION (this);
348  return m_mtu;
349 }
350 bool
352 {
353  NS_LOG_FUNCTION (this);
354  return m_linkUp;
355 }
356 void
358 {
359  NS_LOG_FUNCTION (this << &callback);
361 }
362 bool
364 {
365  NS_LOG_FUNCTION (this);
366  if (m_pointToPointMode)
367  {
368  return false;
369  }
370  return true;
371 }
372 Address
374 {
375  NS_LOG_FUNCTION (this);
376  return Mac48Address ("ff:ff:ff:ff:ff:ff");
377 }
378 bool
380 {
381  NS_LOG_FUNCTION (this);
382  if (m_pointToPointMode)
383  {
384  return false;
385  }
386  return true;
387 }
388 Address
390 {
391  NS_LOG_FUNCTION (this << multicastGroup);
392  return Mac48Address::GetMulticast (multicastGroup);
393 }
394 
396 {
397  NS_LOG_FUNCTION (this << addr);
398  return Mac48Address::GetMulticast (addr);
399 }
400 
401 bool
403 {
404  NS_LOG_FUNCTION (this);
405  if (m_pointToPointMode)
406  {
407  return true;
408  }
409  return false;
410 }
411 
412 bool
414 {
415  NS_LOG_FUNCTION (this);
416  return false;
417 }
418 
419 bool
420 SimpleNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
421 {
422  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
423 
424  return SendFrom (packet, m_address, dest, protocolNumber);
425 }
426 
427 bool
428 SimpleNetDevice::SendFrom (Ptr<Packet> p, const Address& source, const Address& dest, uint16_t protocolNumber)
429 {
430  NS_LOG_FUNCTION (this << p << source << dest << protocolNumber);
431  if (p->GetSize () > GetMtu ())
432  {
433  return false;
434  }
435  Ptr<Packet> packet = p->Copy ();
436 
438  Mac48Address from = Mac48Address::ConvertFrom (source);
439 
440  SimpleTag tag;
441  tag.SetSrc (from);
442  tag.SetDst (to);
443  tag.SetProto (protocolNumber);
444 
445  p->AddPacketTag (tag);
446 
447  if (m_queue->Enqueue (p))
448  {
449  if (m_queue->GetNPackets () == 1 && !TransmitCompleteEvent.IsRunning ())
450  {
451  p = m_queue->Dequeue ();
452  p->RemovePacketTag (tag);
453  Time txTime = Time (0);
454  if (m_bps > DataRate (0))
455  {
456  txTime = m_bps.CalculateBytesTxTime (packet->GetSize ());
457  }
458  m_channel->Send (p, protocolNumber, to, from, this);
460  }
461  return true;
462  }
463 
464 
465  m_channel->Send (packet, protocolNumber, to, from, this);
466  return true;
467 }
468 
469 
470 void
472 {
473  NS_LOG_FUNCTION (this);
474 
475  if (m_queue->GetNPackets () == 0)
476  {
477  return;
478  }
479 
480  Ptr<Packet> packet = m_queue->Dequeue ();
481 
482  SimpleTag tag;
483  packet->RemovePacketTag (tag);
484 
485  Mac48Address src = tag.GetSrc ();
486  Mac48Address dst = tag.GetDst ();
487  uint16_t proto = tag.GetProto ();
488 
489  m_channel->Send (packet, proto, dst, src, this);
490 
491  if (m_queue->GetNPackets ())
492  {
493  Time txTime = Time (0);
494  if (m_bps > DataRate (0))
495  {
496  txTime = m_bps.CalculateBytesTxTime (packet->GetSize ());
497  }
499  }
500 
501  return;
502 }
503 
504 Ptr<Node>
506 {
507  NS_LOG_FUNCTION (this);
508  return m_node;
509 }
510 void
512 {
513  NS_LOG_FUNCTION (this << node);
514  m_node = node;
515 }
516 bool
518 {
519  NS_LOG_FUNCTION (this);
520  if (m_pointToPointMode)
521  {
522  return false;
523  }
524  return true;
525 }
526 void
528 {
529  NS_LOG_FUNCTION (this << &cb);
530  m_rxCallback = cb;
531 }
532 
533 void
535 {
536  NS_LOG_FUNCTION (this);
537  m_channel = 0;
538  m_node = 0;
540  m_queue->Flush ();
542  {
544  }
546 }
547 
548 
549 void
551 {
552  NS_LOG_FUNCTION (this << &cb);
553  m_promiscCallback = cb;
554 }
555 
556 bool
558 {
559  NS_LOG_FUNCTION (this);
560  return true;
561 }
562 
563 } // namespace ns3
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
bool IsBroadcast(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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 "...
void Print(std::ostream &os) const
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Packet addressed to someone else.
Definition: net-device.h:304
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
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.
Ptr< AttributeChecker > MakePointerChecker(void)
Create a PointerChecker for a type.
Definition: pointer.h:224
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:84
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
void SetDst(Mac48Address dst)
Set the destination address.
virtual void SetIfIndex(const uint32_t index)
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:743
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual bool IsLinkUp(void) const
bool m_pointToPointMode
Flag indicating whether or not the NetDevice is a Point to Point model.
virtual void Deserialize(TagBuffer i)
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.
channel
Definition: third.py:92
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:88
virtual void SetNode(Ptr< Node > node)
void SetProto(uint16_t proto)
Set the protocol number.
void CopyTo(uint8_t buffer[6]) const
virtual bool IsMulticast(void) const
virtual bool SupportsSendFrom(void) const
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:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1389
static Mac48Address GetMulticast(Ipv4Address address)
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual bool SetMtu(const uint16_t mtu)
virtual bool NeedsArp(void) const
This device assumes 48-bit mac addressing; there is also the possibility to add an ErrorModel if you ...
mac
Definition: third.py:99
Mac48Address m_address
MAC address.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual Address GetAddress(void) const
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:242
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
SimpleNetDevice tag to store source, destination and protocol of each packet.
static Mac48Address ConvertFrom(const Address &address)
Ptr< Queue< Packet > > m_queue
The Queue for outgoing packets.
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<T>.
Definition: pointer.h:36
address
Definition: first.py:37
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
static TypeId GetTypeId(void)
Get the type ID.
an EUI-48 address
Definition: mac48-address.h:43
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the SimpleNetDevice.
Mac48Address m_dst
destination address
bool IsGroup(void) const
void CopyFrom(const uint8_t buffer[6])
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
virtual Ptr< Node > GetNode(void) const
uint16_t GetProto(void) const
Get the protocol number.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Mac48Address GetDst(void) const
Get the destination address.
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...
read and write tag data
Definition: tag-buffer.h:51
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Packet addressed oo us.
Definition: net-device.h:298
Network layer to device interface.
Definition: net-device.h:95
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:863
virtual void AddLinkChangeCallback(Callback< void > callback)
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void SetQueue(Ptr< Queue< Packet > > queue)
Attach a queue to the SimpleNetDevice.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:870
virtual void SetAddress(Address address)
Set the address of this interface.
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
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
virtual uint32_t GetSerializedSize(void) const
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)
virtual uint16_t GetMtu(void) const
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
virtual Address GetBroadcast(void) const
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:205
Ptr< SimpleChannel > m_channel
the channel the device is connected to
virtual uint32_t GetIfIndex(void) const
void TransmitComplete(void)
The TransmitComplete method is used internally to finish the process of sending a packet out on the c...
void SetSrc(Mac48Address src)
Set the source address.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
virtual Ptr< Channel > GetChannel(void) const
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Packet addressed to multicast group.
Definition: net-device.h:302
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
DataRate m_bps
The device nominal Data rate.
virtual bool IsBroadcast(void) const
a unique identifier for an interface.
Definition: type-id.h:58
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:235
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
Packet addressed to all.
Definition: net-device.h:300
static TypeId GetTypeId(void)
Get the type ID.
Mac48Address GetSrc(void) const
Get the source address.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void Serialize(TagBuffer i) const
uint32_t m_ifIndex
Interface index.