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 
29 NS_LOG_COMPONENT_DEFINE ("SimpleNetDevice");
30 
31 namespace ns3 {
32 
33 NS_OBJECT_ENSURE_REGISTERED (SimpleNetDevice)
34  ;
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::SimpleNetDevice")
40  .SetParent<NetDevice> ()
41  .AddConstructor<SimpleNetDevice> ()
42  .AddAttribute ("ReceiveErrorModel",
43  "The receiver error model used to simulate packet loss",
44  PointerValue (),
45  MakePointerAccessor (&SimpleNetDevice::m_receiveErrorModel),
46  MakePointerChecker<ErrorModel> ())
47  .AddTraceSource ("PhyRxDrop",
48  "Trace source indicating a packet has been dropped by the device during reception",
50  ;
51  return tid;
52 }
53 
55  : m_channel (0),
56  m_node (0),
57  m_mtu (0xffff),
58  m_ifIndex (0)
59 {
60  NS_LOG_FUNCTION (this);
61 }
62 
63 void
64 SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
65  Mac48Address to, Mac48Address from)
66 {
67  NS_LOG_FUNCTION (this << packet << protocol << to << from);
68  NetDevice::PacketType packetType;
69 
70  if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt (packet) )
71  {
72  m_phyRxDropTrace (packet);
73  return;
74  }
75 
76  if (to == m_address)
77  {
78  packetType = NetDevice::PACKET_HOST;
79  }
80  else if (to.IsBroadcast ())
81  {
82  packetType = NetDevice::PACKET_HOST;
83  }
84  else if (to.IsGroup ())
85  {
86  packetType = NetDevice::PACKET_MULTICAST;
87  }
88  else
89  {
90  packetType = NetDevice::PACKET_OTHERHOST;
91  }
92  m_rxCallback (this, packet, protocol, from);
93  if (!m_promiscCallback.IsNull ())
94  {
95  m_promiscCallback (this, packet, protocol, from, to, packetType);
96  }
97 }
98 
99 void
101 {
102  NS_LOG_FUNCTION (this << channel);
103  m_channel = channel;
104  m_channel->Add (this);
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION (this << em);
111  m_receiveErrorModel = em;
112 }
113 
114 void
115 SimpleNetDevice::SetIfIndex (const uint32_t index)
116 {
117  NS_LOG_FUNCTION (this << index);
118  m_ifIndex = index;
119 }
120 uint32_t
122 {
123  NS_LOG_FUNCTION (this);
124  return m_ifIndex;
125 }
128 {
129  NS_LOG_FUNCTION (this);
130  return m_channel;
131 }
132 void
134 {
135  NS_LOG_FUNCTION (this << address);
137 }
138 Address
140 {
141  //
142  // Implicit conversion from Mac48Address to Address
143  //
144  NS_LOG_FUNCTION (this);
145  return m_address;
146 }
147 bool
148 SimpleNetDevice::SetMtu (const uint16_t mtu)
149 {
150  NS_LOG_FUNCTION (this << mtu);
151  m_mtu = mtu;
152  return true;
153 }
154 uint16_t
156 {
157  NS_LOG_FUNCTION (this);
158  return m_mtu;
159 }
160 bool
162 {
163  NS_LOG_FUNCTION (this);
164  return true;
165 }
166 void
168 {
169  NS_LOG_FUNCTION (this << &callback);
170 }
171 bool
173 {
174  NS_LOG_FUNCTION (this);
175  return true;
176 }
177 Address
179 {
180  NS_LOG_FUNCTION (this);
181  return Mac48Address ("ff:ff:ff:ff:ff:ff");
182 }
183 bool
185 {
186  NS_LOG_FUNCTION (this);
187  return false;
188 }
189 Address
191 {
192  NS_LOG_FUNCTION (this << multicastGroup);
193  return Mac48Address::GetMulticast (multicastGroup);
194 }
195 
197 {
198  NS_LOG_FUNCTION (this << addr);
199  return Mac48Address::GetMulticast (addr);
200 }
201 
202 bool
204 {
205  NS_LOG_FUNCTION (this);
206  return false;
207 }
208 
209 bool
211 {
212  NS_LOG_FUNCTION (this);
213  return false;
214 }
215 
216 bool
217 SimpleNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
218 {
219  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
221  m_channel->Send (packet, protocolNumber, to, m_address, this);
222  return true;
223 }
224 bool
225 SimpleNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
226 {
227  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
229  Mac48Address from = Mac48Address::ConvertFrom (source);
230  m_channel->Send (packet, protocolNumber, to, from, this);
231  return true;
232 }
233 
234 Ptr<Node>
236 {
237  NS_LOG_FUNCTION (this);
238  return m_node;
239 }
240 void
242 {
243  NS_LOG_FUNCTION (this << node);
244  m_node = node;
245 }
246 bool
248 {
249  NS_LOG_FUNCTION (this);
250  return false;
251 }
252 void
254 {
255  NS_LOG_FUNCTION (this << &cb);
256  m_rxCallback = cb;
257 }
258 
259 void
261 {
262  NS_LOG_FUNCTION (this);
263  m_channel = 0;
264  m_node = 0;
267 }
268 
269 
270 void
272 {
273  NS_LOG_FUNCTION (this << &cb);
274  m_promiscCallback = cb;
275 }
276 
277 bool
279 {
280  NS_LOG_FUNCTION (this);
281  return true;
282 }
283 
284 } // namespace ns3
virtual Address GetAddress(void) const
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
NS_LOG_COMPONENT_DEFINE("SimpleNetDevice")
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Packet addressed to someone else.
Definition: net-device.h:278
virtual Ptr< Channel > GetChannel(void) const
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:1014
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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:336
virtual bool IsBroadcast(void) const
virtual bool NeedsArp(void) const
virtual bool IsMulticast(void) const
a polymophic address class
Definition: address.h:86
virtual void SetNode(Ptr< Node > node)
static Mac48Address GetMulticast(Ipv4Address address)
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual bool SetMtu(const uint16_t mtu)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
static Mac48Address ConvertFrom(const Address &address)
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.
virtual Ptr< Node > GetNode(void) const
NetDevice::PromiscReceiveCallback m_promiscCallback
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...
Packet addressed oo us.
Definition: net-device.h:272
Network layer to device interface.
Definition: net-device.h:75
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual void SetAddress(Address address)
Set the address of this interface.
Ptr< ErrorModel > m_receiveErrorModel
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
Ptr< SimpleChannel > m_channel
tuple address
Definition: first.py:37
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
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:276
a unique identifier for an interface.
Definition: type-id.h:49
virtual Address GetBroadcast(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual uint32_t GetIfIndex(void) const
static TypeId GetTypeId(void)
virtual bool SupportsSendFrom(void) const
virtual bool IsLinkUp(void) const