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
34namespace ns3 {
35
36NS_LOG_COMPONENT_DEFINE ("SimpleNetDevice");
37
41class SimpleTag : public Tag {
42public:
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
89private:
93};
94
95
97
100{
101 static TypeId tid = TypeId ("ns3::SimpleTag")
102 .SetParent<Tag> ()
103 .SetGroupName("Network")
104 .AddConstructor<SimpleTag> ()
105 ;
106 return tid;
107}
108TypeId
110{
111 return GetTypeId ();
112}
113
116{
117 return 8+8+2;
118}
119void
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}
129void
131{
132 uint8_t mac[6];
133 i.Read (mac, 6);
135 i.Read (mac, 6);
138}
139
140void
142{
143 m_src = src;
144}
145
148{
149 return m_src;
150}
151
152void
154{
155 m_dst = dst;
156}
157
160{
161 return m_dst;
162}
163
164void
165SimpleTag::SetProto (uint16_t proto)
166{
167 m_protocolNumber = proto;
168}
169
170uint16_t
172{
173 return m_protocolNumber;
174}
175
176void
177SimpleTag::Print (std::ostream &os) const
178{
179 os << "src=" << m_src << " dst=" << m_dst << " proto=" << m_protocolNumber;
180}
181
182
183
185
186TypeId
188{
189 static TypeId tid = TypeId ("ns3::SimpleNetDevice")
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>"),
207 MakePointerChecker<Queue<Packet> > ())
208 .AddAttribute ("DataRate",
209 "The default data rate for point to point links. Zero means infinite",
210 DataRateValue (DataRate ("0b/s")),
211 MakeDataRateAccessor (&SimpleNetDevice::m_bps),
212 MakeDataRateChecker ())
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
232void
233SimpleNetDevice::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
268 {
269 m_promiscCallback (this, packet, protocol, from, to, packetType);
270 }
271}
272
273void
275{
276 NS_LOG_FUNCTION (this << channel);
278 m_channel->Add (this);
279 m_linkUp = true;
281}
282
285{
286 NS_LOG_FUNCTION (this);
287 return m_queue;
288}
289
290void
292{
293 NS_LOG_FUNCTION (this << q);
294 m_queue = q;
295}
296
297void
299{
300 NS_LOG_FUNCTION (this << em);
302}
303
304void
306{
307 NS_LOG_FUNCTION (this << index);
308 m_ifIndex = index;
309}
312{
313 NS_LOG_FUNCTION (this);
314 return m_ifIndex;
315}
318{
319 NS_LOG_FUNCTION (this);
320 return m_channel;
321}
322void
324{
325 NS_LOG_FUNCTION (this << address);
327}
328Address
330{
331 //
332 // Implicit conversion from Mac48Address to Address
333 //
334 NS_LOG_FUNCTION (this);
335 return m_address;
336}
337bool
338SimpleNetDevice::SetMtu (const uint16_t mtu)
339{
340 NS_LOG_FUNCTION (this << mtu);
341 m_mtu = mtu;
342 return true;
343}
344uint16_t
346{
347 NS_LOG_FUNCTION (this);
348 return m_mtu;
349}
350bool
352{
353 NS_LOG_FUNCTION (this);
354 return m_linkUp;
355}
356void
358{
359 NS_LOG_FUNCTION (this << &callback);
361}
362bool
364{
365 NS_LOG_FUNCTION (this);
367 {
368 return false;
369 }
370 return true;
371}
374{
375 NS_LOG_FUNCTION (this);
376 return Mac48Address ("ff:ff:ff:ff:ff:ff");
377}
378bool
380{
381 NS_LOG_FUNCTION (this);
383 {
384 return false;
385 }
386 return true;
387}
388Address
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
401bool
403{
404 NS_LOG_FUNCTION (this);
406 {
407 return true;
408 }
409 return false;
410}
411
412bool
414{
415 NS_LOG_FUNCTION (this);
416 return false;
417}
418
419bool
420SimpleNetDevice::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
427bool
428SimpleNetDevice::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
438
439 SimpleTag tag;
440 tag.SetSrc (from);
441 tag.SetDst (to);
442 tag.SetProto (protocolNumber);
443
444 p->AddPacketTag (tag);
445
446 if (m_queue->Enqueue (p))
447 {
448 if (m_queue->GetNPackets () == 1 && !FinishTransmissionEvent.IsRunning ())
449 {
451 }
452 return true;
453 }
454
455 return false;
456}
457
458void
460{
461 if (m_queue->GetNPackets () == 0)
462 {
463 return;
464 }
466 "Tried to transmit a packet while another transmission was in progress");
467 Ptr<Packet> packet = m_queue->Dequeue ();
468
479 Time txTime = Time (0);
480 if (m_bps > DataRate (0))
481 {
482 txTime = m_bps.CalculateBytesTxTime (packet->GetSize ());
483 }
485}
486
487void
489{
490 NS_LOG_FUNCTION (this);
491
492 SimpleTag tag;
493 packet->RemovePacketTag (tag);
494
495 Mac48Address src = tag.GetSrc ();
496 Mac48Address dst = tag.GetDst ();
497 uint16_t proto = tag.GetProto ();
498
499 m_channel->Send (packet, proto, dst, src, this);
500
502
503 return;
504}
505
508{
509 NS_LOG_FUNCTION (this);
510 return m_node;
511}
512void
514{
515 NS_LOG_FUNCTION (this << node);
516 m_node = node;
517}
518bool
520{
521 NS_LOG_FUNCTION (this);
523 {
524 return false;
525 }
526 return true;
527}
528void
530{
531 NS_LOG_FUNCTION (this << &cb);
532 m_rxCallback = cb;
533}
534
535void
537{
538 NS_LOG_FUNCTION (this);
539 m_channel = 0;
540 m_node = 0;
542 m_queue->Dispose ();
544 {
546 }
548}
549
550
551void
553{
554 NS_LOG_FUNCTION (this << &cb);
556}
557
558bool
560{
561 NS_LOG_FUNCTION (this);
562 return true;
563}
564
565} // namespace ns3
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Boolean.
Definition: boolean.h:37
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Class for representing data rates.
Definition: data-rate.h:89
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:275
AttributeValue implementation for DataRate.
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:71
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:50
an EUI-48 address
Definition: mac48-address.h:44
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup(void) const
void CopyFrom(const uint8_t buffer[6])
bool IsBroadcast(void) const
static Mac48Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[6]) const
Network layer to device interface.
Definition: net-device.h:96
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:300
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:302
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:963
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:956
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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 ...
virtual bool IsMulticast(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual bool IsBroadcast(void) const
virtual bool IsLinkUp(void) const
virtual Ptr< Node > GetNode(void) const
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).
virtual Address GetAddress(void) const
virtual bool NeedsArp(void) const
virtual Ptr< Channel > GetChannel(void) const
void SetQueue(Ptr< Queue< Packet > > queue)
Attach a queue to the SimpleNetDevice.
NetDevice::ReceiveCallback m_rxCallback
Receive callback.
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
DataRate m_bps
The device nominal Data rate.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Ptr< Queue< Packet > > m_queue
The Queue for outgoing packets.
static TypeId GetTypeId(void)
Get the type ID.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual void SetNode(Ptr< Node > node)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetBroadcast(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the SimpleNetDevice.
void StartTransmission(void)
The StartTransmission method is used internally to start the process of sending a packet out on the c...
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
EventId FinishTransmissionEvent
the Tx Complete event
bool m_linkUp
Flag indicating whether or not the link is up.
virtual void SetAddress(Address address)
Set the address of this interface.
bool m_pointToPointMode
Flag indicating whether or not the NetDevice is a Point to Point model.
void FinishTransmission(Ptr< Packet > packet)
The FinishTransmission method is used internally to finish the process of sending a packet out on the...
Ptr< ErrorModel > m_receiveErrorModel
Receive error model.
Ptr< SimpleChannel > m_channel
the channel the device is connected to
virtual bool SetMtu(const uint16_t mtu)
Ptr< Node > m_node
Node this netDevice is associated to.
virtual void SetIfIndex(const uint32_t index)
uint32_t m_ifIndex
Interface index.
Mac48Address m_address
MAC address.
NetDevice::PromiscReceiveCallback m_promiscCallback
Promiscuous receive callback.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void DoDispose(void)
Destructor implementation.
virtual bool SupportsSendFrom(void) const
virtual uint16_t GetMtu(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual uint32_t GetIfIndex(void) const
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.
SimpleNetDevice tag to store source, destination and protocol of each packet.
Mac48Address m_dst
destination address
virtual void Serialize(TagBuffer i) const
uint16_t m_protocolNumber
protocol number
void SetSrc(Mac48Address src)
Set the source address.
void Print(std::ostream &os) const
virtual void Deserialize(TagBuffer i)
Mac48Address GetDst(void) const
Get the destination address.
void SetProto(uint16_t proto)
Set the protocol number.
Mac48Address GetSrc(void) const
Get the source address.
Mac48Address m_src
source address
virtual uint32_t GetSerializedSize(void) const
static TypeId GetTypeId(void)
Get the type ID.
uint16_t GetProto(void) const
Get the protocol number.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
void SetDst(Mac48Address dst)
Set the destination address.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
Hold variables of type string.
Definition: string.h:41
read and write tag data
Definition: tag-buffer.h:52
void Read(uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:176
TAG_BUFFER_INLINE uint16_t ReadU16(void)
Definition: tag-buffer.h:205
void Write(const uint8_t *buffer, uint32_t size)
Definition: tag-buffer.cc:125
TAG_BUFFER_INLINE void WriteU16(uint16_t v)
Definition: tag-buffer.h:180
tag a set of bytes in a packet
Definition: tag.h:37
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
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:922
#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:88
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:85
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:45
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
address
Definition: first.py:44
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:793
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:92
mac
Definition: third.py:96