22 #include "ns3/uinteger.h"
24 #include <sys/ioctl.h>
38 .SetGroupName(
"Network")
101 : m_bufferSize(65536),
131 struct netmap_ring* rxring;
133 uint32_t rxRingIndex = 0;
137 while (rxRingIndex < m_nifp->ni_rx_rings)
139 rxring = NETMAP_RXRING(
m_nifp, rxRingIndex);
141 if (!nm_ring_empty(rxring))
143 uint32_t i = rxring->cur;
144 uint8_t* buffer = (uint8_t*)NETMAP_BUF(rxring, rxring->slot[i].buf_idx);
145 len = rxring->slot[i].len;
146 NS_LOG_DEBUG(
"Received a packet of " << len <<
" bytes");
149 memcpy(buf, buffer, len);
152 rxring->head = rxring->cur = nm_ring_next(rxring, i);
154 ioctl(
m_fd, NIOCRXSYNC,
nullptr);
178 TypeId(
"ns3::NetmapNetDevice")
180 .SetGroupName(
"FdNetDevice")
182 .AddAttribute(
"SyncAndNotifyQueuePeriod",
183 "The period of time (in number of us) after which the device syncs the "
184 "netmap ring and notifies queue status.",
187 MakeUintegerChecker<uint8_t>());
218 fdReader->SetBufferSize(
GetMtu() + 22);
219 fdReader->SetNetmapIfp(
m_nifp);
252 struct netmap_ring* txring;
253 txring = NETMAP_TXRING(
m_nifp, 0);
255 int tail = txring->tail;
260 uint32_t bytesInQueue = 0;
262 for (
int i = 1; i < inQueue; i++)
264 bytesInQueue += txring->slot[tail].len;
311 struct netmap_ring* txring;
312 txring = NETMAP_TXRING(
m_nifp, 0);
314 return nm_ring_space(txring);
323 struct netmap_ring* txring = NETMAP_TXRING(
m_nifp, 0);
325 uint32_t prevTotalTransmittedBytes = 0;
335 uint32_t deltaBytes = totalTransmittedBytes - prevTotalTransmittedBytes;
337 prevTotalTransmittedBytes = totalTransmittedBytes;
340 m_queue->NotifyTransmittedBytes(deltaBytes);
353 NS_LOG_DEBUG(
"Space in the netmap ring of " << nm_ring_space(txring) <<
" packets");
364 struct netmap_ring* txring;
368 txring = NETMAP_TXRING(
m_nifp, 0);
378 if (!nm_ring_empty(txring))
380 uint32_t i = txring->cur;
381 uint8_t* buf = (uint8_t*)NETMAP_BUF(txring, txring->slot[i].buf_idx);
383 memcpy(buf, buffer, length);
384 txring->slot[i].len = length;
386 txring->head = txring->cur = nm_ring_next(txring, i);
392 m_queue->NotifyQueuedBytes(length);
395 if (nm_ring_space(txring) == 0)
a NetDevice to read/write network traffic from/into a file descriptor.
int GetFileDescriptor() const
Get the associated file descriptor.
uint16_t GetMtu() const override
int m_fd
The file descriptor to read from.
Network device transmission queue.
virtual void Stop()
Called by the device to stop this device transmission queue.
virtual void Wake()
Called by the device to wake the queue disc associated with this device transmission queue.
virtual bool IsStopped() const
Get the status of the device transmission queue.
virtual void Start()
Called by the device to start this device transmission queue.
virtual void NotifyTransmittedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes it is going to transmit.
virtual void NotifyQueuedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes queued to the device queue.
Network device transmission queue with lock.
virtual void NotifyTransmittedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes it is going to transmit.
virtual void NotifyQueuedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes queued to the device queue.
virtual bool IsStopped() const
Get the status of the device transmission queue.
virtual void Wake()
Called by the device to wake the queue disc associated with this device transmission queue.
std::mutex m_mutex
Mutex to serialize the operations performed on the queue.
static TypeId GetTypeId()
Get the type ID.
virtual void Stop()
Called by the device to stop this device transmission queue.
virtual void Start()
Called by the device to start this device transmission queue.
virtual ~NetDeviceQueueLock()
void SetBufferSize(uint32_t bufferSize)
Set size of the read buffer.
FdReader::Data DoRead()
The read implementation.
void SetNetmapIfp(struct netmap_if *nifp)
Set netmap interface representation.
NetmapNetDeviceFdReader()
struct netmap_if * m_nifp
Netmap interface representation.
uint32_t m_bufferSize
size of the read buffer
a NetDevice to read/write network traffic from/into a netmap file descriptor.
int GetSpaceInNetmapTxRing() const
Get the number of slots currently available in the netmap transmission ring.
uint32_t m_nRxRings
Number of receiver rings.
void SetRxRingsInfo(uint32_t nRxRings, uint32_t nRxRingsSlots)
Set the netmap receiver rings info.
virtual ~NetmapNetDevice()
void SetNetmapInterfaceRepresentation(struct netmap_if *nifp)
Set the netmap interface representation.
void DoFinishStoppingDevice()
Complete additional actions, if any, to tear down the device.
Ptr< NetDeviceQueue > m_queue
NetDevice queue.
uint8_t m_syncAndNotifyQueuePeriod
The period of time in us after which the device syncs the netmap ring and notifies queue status.
void DoFinishStartingDevice()
Complete additional actions, if any, to spin up down the device.
virtual ssize_t Write(uint8_t *buffer, size_t length)
The function Writes a packet into the netmap transmission ring.
uint32_t m_nTxRingsSlots
Number of slots in the transmission rings.
uint32_t GetBytesInNetmapTxRing()
Get the number of bytes currently in the netmap transmission ring.
std::thread m_syncAndNotifyQueueThread
Thread used to perform the flow control.
uint32_t m_nRxRingsSlots
Number of slots in the receiver rings.
struct netmap_if * m_nifp
Netmap interface representation.
uint32_t m_nTxRings
Number of transmission rings.
static TypeId GetTypeId()
Get the type ID.
void SetNetDeviceQueue(Ptr< NetDeviceQueue > queue)
Set the NetDeviceQueue.
uint32_t m_totalQueuedBytes
Total queued bytes.
std::atomic< bool > m_syncAndNotifyQueueThreadRun
Running flag of the flow control thread.
Ptr< FdReader > DoCreateFdReader()
Create the FdReader object.
virtual void SyncAndNotifyQueue()
This function syncs netmap ring and notifies netdevice queue.
void SetTxRingsInfo(uint32_t nTxRings, uint32_t nTxRingsSlots)
Set the netmap transmission rings info.
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
A structure representing data read.