A Discrete-Event Network Simulator
API
csma-channel.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Emmanuelle Laprise
3  * Copyright (c) 2012 Jeff Young
4  * Copyright (c) 2014 Murphy McCauley
5  * Copyright (c) 2017 Luciano Jerez Chaves
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation;
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Author: Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
21  * Author: Jeff Young <jyoung9@gatech.edu>
22  * Author: Murphy McCauley <murphy.mccauley@gmail.com>
23  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
24  */
25 
26 #include "csma-channel.h"
27 
28 #include "csma-net-device.h"
29 
30 #include "ns3/log.h"
31 #include "ns3/packet.h"
32 #include "ns3/simulator.h"
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE("CsmaChannel");
38 
39 NS_OBJECT_ENSURE_REGISTERED(CsmaChannel);
40 
41 TypeId
43 {
44  static TypeId tid =
45  TypeId("ns3::CsmaChannel")
46  .SetParent<Channel>()
47  .SetGroupName("Csma")
48  .AddConstructor<CsmaChannel>()
49  .AddAttribute(
50  "DataRate",
51  "The transmission data rate to be provided to devices connected to the channel",
52  DataRateValue(DataRate(0xffffffff)),
53  MakeDataRateAccessor(&CsmaChannel::m_bps),
54  MakeDataRateChecker())
55  .AddAttribute("Delay",
56  "Transmission delay through the channel",
57  TimeValue(Seconds(0)),
60  .AddAttribute("FullDuplex",
61  "Whether the channel is full-duplex mode.",
63  BooleanValue(false),
66  return tid;
67 }
68 
70  : Channel()
71 {
73  m_deviceList.clear();
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79  m_deviceList.clear();
80 }
81 
82 int32_t
84 {
85  NS_LOG_FUNCTION(this << device);
86  NS_ASSERT(device);
87 
88  //
89  // For full-duplex links we can only attach two devices to the same channel
90  // since there is no backoff mechanism for concurrent transmissions.
91  //
92  if (m_fullDuplex && m_deviceList.size() >= 2)
93  {
94  NS_LOG_DEBUG("Falling back to half-duplex");
95  m_fullDuplex = false;
96  }
97 
98  CsmaDeviceRec rec(device);
99 
100  m_deviceList.push_back(rec);
101  SetState(m_deviceList.size() - 1, IDLE);
102  SetCurrentSrc(m_deviceList.size() - 1, m_deviceList.size() - 1);
103  return (m_deviceList.size() - 1);
104 }
105 
106 bool
108 {
109  NS_LOG_FUNCTION(this << device);
110  NS_ASSERT(device);
111 
112  std::vector<CsmaDeviceRec>::iterator it;
113  for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
114  {
115  if (it->devicePtr == device)
116  {
117  if (!it->active)
118  {
119  it->active = true;
120  return true;
121  }
122  else
123  {
124  return false;
125  }
126  }
127  }
128  return false;
129 }
130 
131 bool
132 CsmaChannel::Reattach(uint32_t deviceId)
133 {
134  NS_LOG_FUNCTION(this << deviceId);
135 
136  if (deviceId >= m_deviceList.size())
137  {
138  return false;
139  }
140 
141  if (m_deviceList[deviceId].active)
142  {
143  return false;
144  }
145  else
146  {
147  m_deviceList[deviceId].active = true;
148  return true;
149  }
150 }
151 
152 bool
153 CsmaChannel::Detach(uint32_t deviceId)
154 {
155  NS_LOG_FUNCTION(this << deviceId);
156 
157  if (deviceId < m_deviceList.size())
158  {
159  if (!m_deviceList[deviceId].active)
160  {
161  NS_LOG_WARN("CsmaChannel::Detach(): Device is already detached (" << deviceId << ")");
162  return false;
163  }
164 
165  m_deviceList[deviceId].active = false;
166 
167  if ((GetState(deviceId) == TRANSMITTING) && (GetCurrentSrc(deviceId) == deviceId))
168  {
169  NS_LOG_WARN("CsmaChannel::Detach(): Device is currently"
170  << "transmitting (" << deviceId << ")");
171  }
172 
173  return true;
174  }
175  else
176  {
177  return false;
178  }
179 }
180 
181 bool
183 {
184  NS_LOG_FUNCTION(this << device);
185  NS_ASSERT(device);
186 
187  std::vector<CsmaDeviceRec>::iterator it;
188  for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
189  {
190  if ((it->devicePtr == device) && (it->active))
191  {
192  it->active = false;
193  return true;
194  }
195  }
196  return false;
197 }
198 
199 bool
201 {
202  NS_LOG_FUNCTION(this << p << srcId);
203  NS_LOG_INFO("UID is " << p->GetUid() << ")");
204 
205  if (GetState(srcId) != IDLE)
206  {
207  NS_LOG_WARN("CsmaChannel::TransmitStart(): State is not IDLE");
208  return false;
209  }
210 
211  if (!IsActive(srcId))
212  {
213  NS_LOG_ERROR(
214  "CsmaChannel::TransmitStart(): Selected source is not currently attached to network");
215  return false;
216  }
217 
218  NS_LOG_LOGIC("switch to TRANSMITTING");
219  SetCurrentPkt(srcId, p->Copy());
220  SetCurrentSrc(srcId, srcId);
221  SetState(srcId, TRANSMITTING);
222  return true;
223 }
224 
225 bool
226 CsmaChannel::IsActive(uint32_t deviceId)
227 {
228  return (m_deviceList[deviceId].active);
229 }
230 
231 bool
233 {
234  return m_fullDuplex;
235 }
236 
237 bool
239 {
240  NS_LOG_FUNCTION(this << GetCurrentPkt(srcId) << GetCurrentSrc(srcId));
241  NS_LOG_INFO("UID is " << GetCurrentPkt(srcId)->GetUid() << ")");
242 
243  NS_ASSERT(GetState(srcId) == TRANSMITTING);
244  SetState(srcId, PROPAGATING);
245 
246  bool retVal = true;
247 
248  if (!IsActive(GetCurrentSrc(srcId)))
249  {
250  NS_LOG_ERROR("CsmaChannel::TransmitEnd(): Seclected source was detached before the end of "
251  "the transmission");
252  retVal = false;
253  }
254 
255  NS_LOG_LOGIC("Schedule event in " << m_delay.As(Time::S));
256 
257  NS_LOG_LOGIC("Receive");
258 
259  std::vector<CsmaDeviceRec>::iterator it;
260  for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
261  {
262  if (it->IsActive() && it->devicePtr != m_deviceList[GetCurrentSrc(srcId)].devicePtr)
263  {
264  // schedule reception events
265  Simulator::ScheduleWithContext(it->devicePtr->GetNode()->GetId(),
266  m_delay,
268  it->devicePtr,
269  GetCurrentPkt(srcId)->Copy(),
270  m_deviceList[GetCurrentSrc(srcId)].devicePtr);
271  }
272  }
273 
274  // Schedule for the TX side to go back to IDLE.
275  if (IsFullDuplex())
276  {
277  //
278  // In full-duplex mode, the channel should be IDLE during propagation
279  // since it's ok to start transmitting again. In this case, we don't need
280  // to wait for the channel delay.
281  //
283  }
284  else
285  {
286  //
287  // In half-duplex mode, the channel can only go back to IDLE after
288  // propagation delay.
289  //
291  }
292  return retVal;
293 }
294 
295 void
297 {
298  NS_LOG_FUNCTION(this << GetCurrentPkt(deviceId));
299  NS_LOG_INFO("UID is " << GetCurrentPkt(deviceId)->GetUid() << ")");
300 
301  NS_ASSERT(GetState(deviceId) == PROPAGATING);
302  SetState(deviceId, IDLE);
303 }
304 
305 uint32_t
307 {
308  int numActDevices = 0;
309  std::vector<CsmaDeviceRec>::iterator it;
310  for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
311  {
312  if (it->active)
313  {
314  numActDevices++;
315  }
316  }
317  return numActDevices;
318 }
319 
320 std::size_t
322 {
323  return m_deviceList.size();
324 }
325 
327 CsmaChannel::GetCsmaDevice(std::size_t i) const
328 {
329  return m_deviceList[i].devicePtr;
330 }
331 
332 int32_t
334 {
335  std::vector<CsmaDeviceRec>::iterator it;
336  int i = 0;
337  for (it = m_deviceList.begin(); it < m_deviceList.end(); it++)
338  {
339  if (it->devicePtr == device)
340  {
341  if (it->active)
342  {
343  return i;
344  }
345  else
346  {
347  return -2;
348  }
349  }
350  i++;
351  }
352  return -1;
353 }
354 
355 bool
356 CsmaChannel::IsBusy(uint32_t deviceId)
357 {
358  if (GetState(deviceId) == IDLE)
359  {
360  return false;
361  }
362  else
363  {
364  return true;
365  }
366 }
367 
368 DataRate
370 {
371  return m_bps;
372 }
373 
374 Time
376 {
377  return m_delay;
378 }
379 
381 CsmaChannel::GetState(uint32_t deviceId)
382 {
383  return m_state[m_fullDuplex ? deviceId : 0];
384 }
385 
387 CsmaChannel::GetDevice(std::size_t i) const
388 {
389  return GetCsmaDevice(i);
390 }
391 
393 CsmaChannel::GetCurrentPkt(uint32_t deviceId)
394 {
395  return m_currentPkt[m_fullDuplex ? deviceId : 0];
396 }
397 
398 void
400 {
401  m_currentPkt[m_fullDuplex ? deviceId : 0] = pkt;
402 }
403 
404 uint32_t
405 CsmaChannel::GetCurrentSrc(uint32_t deviceId)
406 {
407  return m_currentSrc[m_fullDuplex ? deviceId : 0];
408 }
409 
410 void
411 CsmaChannel::SetCurrentSrc(uint32_t deviceId, uint32_t transmitterId)
412 {
413  m_currentSrc[m_fullDuplex ? deviceId : 0] = transmitterId;
414 }
415 
416 void
418 {
419  m_state[m_fullDuplex ? deviceId : 0] = state;
420 }
421 
423 {
424  active = false;
425 }
426 
428 {
429  devicePtr = device;
430  active = true;
431 }
432 
434 {
435  devicePtr = deviceRec.devicePtr;
436  active = deviceRec.active;
437 }
438 
439 bool
441 {
442  return active;
443 }
444 
445 } // namespace ns3
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Abstract Channel Base Class.
Definition: channel.h:45
Csma Channel.
Definition: csma-channel.h:93
void PropagationCompleteEvent(uint32_t deviceId)
Indicates that the channel has finished propagating the current packet.
void SetState(uint32_t deviceId, WireState state)
Sets the state of the channel.
~CsmaChannel() override
Destroy a CsmaChannel.
Definition: csma-channel.cc:76
Ptr< CsmaNetDevice > GetCsmaDevice(std::size_t i) const
uint32_t GetNumActDevices()
DataRate m_bps
The assigned data rate of the channel.
Definition: csma-channel.h:332
bool m_fullDuplex
Whether the channel is in full-duplex mode.
Definition: csma-channel.h:342
DataRate GetDataRate()
Get the assigned data rate of the channel.
bool Reattach(uint32_t deviceId)
Reattach a previously detached net device to the channel.
bool IsActive(uint32_t deviceId)
Indicates if a net device is currently attached or detached from the channel.
Time GetDelay()
Get the assigned speed-of-light delay of the channel.
bool IsBusy(uint32_t deviceId)
Indicates if the channel is busy.
int32_t GetDeviceNum(Ptr< CsmaNetDevice > device)
Time m_delay
The assigned speed-of-light delay of the channel.
Definition: csma-channel.h:337
bool TransmitStart(Ptr< const Packet > p, uint32_t srcId)
Start transmitting a packet over the channel.
static TypeId GetTypeId()
Get the type ID.
Definition: csma-channel.cc:42
WireState GetState(uint32_t deviceId)
Checks the state of the channel.
CsmaChannel()
Create a CsmaChannel.
Definition: csma-channel.cc:69
bool Detach(Ptr< CsmaNetDevice > device)
Detach a given netdevice from this channel.
bool TransmitEnd(uint32_t deviceId)
Indicates that the net device has finished transmitting the packet over the channel.
void SetCurrentPkt(uint32_t deviceId, Ptr< Packet > pkt)
Sets the current packet.
void SetCurrentSrc(uint32_t deviceId, uint32_t transmitterId)
Sets the current transmitter.
WireState m_state[2]
Current state of each subchannel.
Definition: csma-channel.h:376
std::vector< CsmaDeviceRec > m_deviceList
List of the net devices that have been or are currently connected to the channel.
Definition: csma-channel.h:355
Ptr< Packet > GetCurrentPkt(uint32_t deviceId)
Gets current packet.
int32_t Attach(Ptr< CsmaNetDevice > device)
Attach a given netdevice to this channel.
Definition: csma-channel.cc:83
bool IsFullDuplex() const
Indicates if channel is operating in full-duplex mode.
Ptr< NetDevice > GetDevice(std::size_t i) const override
uint32_t m_currentSrc[2]
Device Id of the source that is currently transmitting on the subchannel, or the last source to have ...
Definition: csma-channel.h:370
uint32_t GetCurrentSrc(uint32_t deviceId)
Gets current transmitter.
WireState
Current state of the channel.
Definition: csma-channel.h:99
@ PROPAGATING
Channel is BUSY, packet is propagating to all attached net devices.
Definition: csma-channel.h:102
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:100
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
Definition: csma-channel.h:101
Ptr< Packet > m_currentPkt[2]
The Packet that is currently being transmitted on the subchannel (or the last packet to have been tra...
Definition: csma-channel.h:362
std::size_t GetNDevices() const override
CsmaNetDevice Record.
Definition: csma-channel.h:50
bool IsActive() const
Ptr< CsmaNetDevice > devicePtr
Pointer to the net device.
Definition: csma-channel.h:52
bool active
Is net device enabled to TX/RX.
Definition: csma-channel.h:53
void Receive(Ptr< Packet > p, Ptr< CsmaNetDevice > sender)
Receive a packet from a connected CsmaChannel.
Class for representing data rates.
Definition: data-rate.h:90
AttributeValue implementation for DataRate.
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
uint64_t GetUid() const
Returns the packet's Uid.
Definition: packet.cc:412
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
@ S
second
Definition: nstime.h:116
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition: type-id.h:67
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
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:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1424
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:649