26 #include "ns3/net-device-queue-interface.h"
27 #include "ns3/simulator.h"
28 #include "ns3/system-thread.h"
29 #include "ns3/system-condition.h"
30 #include "ns3/system-mutex.h"
31 #include "ns3/uinteger.h"
33 #include <sys/ioctl.h>
35 #include <sys/signal.h>
41 #include <rte_ethdev.h>
42 #include <rte_common.h>
43 #include <rte_mempool.h>
45 #include <rte_malloc.h>
46 #include <rte_cycles.h>
62 .SetGroupName (
"FdNetDevice")
64 .AddAttribute (
"TxTimeout",
65 "The time to wait before transmitting burst from Tx buffer.",
69 .AddAttribute (
"MaxRxBurst",
73 MakeUintegerChecker<uint32_t> ())
74 .AddAttribute (
"MaxTxBurst",
78 MakeUintegerChecker<uint32_t> ())
79 .AddAttribute (
"MempoolCacheSize",
80 "Size of mempool cache.",
83 MakeUintegerChecker<uint32_t> ())
84 .AddAttribute (
"NbRxDesc",
85 "Number of Rx descriptors.",
88 MakeUintegerChecker<uint16_t> ())
89 .AddAttribute (
"NbTxDesc",
90 "Number of Tx descriptors.",
93 MakeUintegerChecker<uint16_t> ())
110 rte_eal_wait_lcore (1);
128 #define CHECK_INTERVAL 100
129 #define MAX_CHECK_TIME 90
130 uint8_t count, allPortsUp, printFlag = 0;
131 struct rte_eth_link link;
146 memset (&link, 0,
sizeof(link));
151 if (link.link_status)
157 printf (
"Port %d Link Down\n",
m_portId);
162 if (link.link_status == ETH_LINK_DOWN)
191 if (signum == SIGINT || signum == SIGTERM)
193 printf (
"\n\nSignal %d received, preparing to exit...\n",
215 for (uint16_t i = 0; i <
m_rxBuffer->length; i++)
217 struct rte_mbuf *pkt = NULL;
225 uint8_t * buf = rte_pktmbuf_mtod (pkt, uint8_t *);
226 size_t length = pkt->data_len;
238 lcoreId = rte_lcore_id ();
266 command.append (
"dpdk-devbind.py --force ");
267 command.append (
"--bind=");
268 command.append (dpdkDriver.c_str ());
269 command.append (
" ");
271 printf (
"Executing: %s\n", command.c_str ());
272 if (system (command.c_str ()))
274 rte_exit (EXIT_FAILURE,
"Execution failed - bye\n");
281 int ret = rte_eal_init (argc, argv);
284 rte_exit (EXIT_FAILURE,
"Invalid EAL arguments\n");
291 unsigned nbPorts = rte_eth_dev_count_avail ();
294 rte_exit (EXIT_FAILURE,
"No Ethernet ports - bye\n");
300 rte_exit (EXIT_FAILURE,
"Cannot get port id - bye\n");
304 unsigned int nbLcores = 2;
312 m_mempool = rte_pktmbuf_pool_create (
"mbuf_pool", nbMbufs,
314 RTE_MBUF_DEFAULT_BUF_SIZE,
319 rte_exit (EXIT_FAILURE,
"Cannot init mbuf pool\n");
323 static struct rte_eth_conf portConf = {};
324 portConf.rxmode = {};
325 portConf.rxmode.split_hdr_size = 0;
326 portConf.txmode = {};
327 portConf.txmode.mq_mode = ETH_MQ_TX_NONE;
329 struct rte_eth_rxconf reqConf;
330 struct rte_eth_txconf txqConf;
331 struct rte_eth_conf localPortConf = portConf;
332 struct rte_eth_dev_info devInfo;
335 rte_eth_dev_info_get (
m_portId, &devInfo);
336 if (devInfo.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
338 localPortConf.txmode.offloads |=
339 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
341 ret = rte_eth_dev_configure (
m_portId, 1, 1, &localPortConf);
344 rte_exit (EXIT_FAILURE,
"Cannot configure device: err=%d, port=%u\n",
351 rte_exit (EXIT_FAILURE,
352 "Cannot adjust number of descriptors: err=%d, port=%u\n",
358 reqConf = devInfo.default_rxconf;
359 reqConf.offloads = localPortConf.rxmode.offloads;
366 rte_exit (EXIT_FAILURE,
"rte_eth_rx_queue_setup:err=%d, port=%u\n",
372 txqConf = devInfo.default_txconf;
373 txqConf.offloads = localPortConf.txmode.offloads;
379 rte_exit (EXIT_FAILURE,
"rte_eth_tx_queue_setup:err=%d, port=%u\n",
385 rte_zmalloc_socket (
"tx_buffer",
390 rte_zmalloc_socket (
"rx_buffer",
395 rte_exit (EXIT_FAILURE,
"Cannot allocate buffer for rx/tx on port %u\n",
406 rte_exit (EXIT_FAILURE,
"rte_eth_dev_start:err=%d, port=%u\n",
410 rte_eth_promiscuous_enable (
m_portId);
415 rte_eal_mp_remote_launch (
LaunchCore,
this, CALL_MASTER);
421 struct rte_mbuf *pkt = rte_pktmbuf_alloc (
m_mempool);
426 uint8_t *buf = rte_pktmbuf_mtod (pkt, uint8_t *);
433 struct rte_mbuf *pkt;
439 pkt = (
struct rte_mbuf *)
441 sizeof(
struct rte_mbuf) + RTE_PKTMBUF_HEADROOM);
443 rte_pktmbuf_free (pkt);
449 struct rte_mbuf ** pkt =
new struct rte_mbuf*[1];
458 pkt[0] = (
struct rte_mbuf *)
459 RTE_PTR_SUB ( buffer,
460 sizeof(
struct rte_mbuf) + RTE_PKTMBUF_HEADROOM);
462 pkt[0]->pkt_len = length;
463 pkt[0]->data_len = length;
A class which provides a simple way to implement a Critical Section.
a NetDevice to read/write network traffic from/into a Dpdk enabled port.
static int LaunchCore(void *arg)
A function to handle rx & tx operations.
virtual void FreeBuffer(uint8_t *buf)
Free the given packet buffer.
uint32_t m_maxRxPktBurst
Size of Rx burst.
void InitDpdk(int argc, char **argv, std::string dpdkDriver)
Initialize Dpdk.
void SetDeviceName(std::string deviceName)
Set device name.
void HandleTx()
Transmit packets in burst from the tx_buffer to the nic.
static void SignalHandler(int signum)
A signal handler for SIGINT and SIGTERM signals.
~DpdkNetDevice()
Destructor for the DpdkNetDevice.
struct rte_eth_dev_tx_buffer * m_txBuffer
Buffer to handle burst transmission.
struct rte_eth_dev_tx_buffer * m_rxBuffer
Buffer to handle burst reception.
uint32_t m_maxTxPktBurst
Size of Tx burst.
EventId m_txEvent
Event for stale packet transmission.
std::string m_deviceName
The device name;.
static volatile bool m_forceQuit
Condition variable for Dpdk to stop.
void DoFinishStoppingDevice(void)
Complete additional actions, if any, to tear down the device.
uint16_t m_nbTxDesc
Number of Tx descriptors.
uint16_t m_nbRxDesc
Number of Rx descriptors.
bool IsLinkUp(void) const
Check the status of the link.
struct rte_mempool * m_mempool
Packet memory pool.
uint16_t m_portId
The port number of the device to be used.
virtual uint8_t * AllocateBuffer(size_t len)
Allocate packet buffer.
DpdkNetDevice()
Constructor for the DpdkNetDevice.
void HandleRx()
Receive packets in burst from the nic to the rx_buffer.
void CheckAllPortsLinkStatus(void)
Check the link status of all ports in up to 9s and print them finally.
Time m_txTimeout
The time to wait before transmitting burst from Tx buffer.
ssize_t Write(uint8_t *buffer, size_t length)
Write packet data to device.
static TypeId GetTypeId(void)
Get the type ID.
uint32_t m_mempoolCacheSize
Mempool cache size.
a NetDevice to read/write network traffic from/into a file descriptor.
std::queue< std::pair< uint8_t *, ssize_t > > m_pendingQueue
Number of packets that were received and scheduled for read but not yet read.
SystemMutex m_pendingReadMutex
Mutex to increase pending read counter.
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
AttributeValue implementation for Time.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
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.