154 lines
5.1 KiB
C++
154 lines
5.1 KiB
C++
|
|
#include "ethernet-generator.h"
|
||
|
|
|
||
|
|
#include "ns3/log.h"
|
||
|
|
#include "ns3/simulator.h"
|
||
|
|
#include "ns3/socket-factory.h"
|
||
|
|
#include "ns3/socket.h"
|
||
|
|
#include "ns3/uinteger.h"
|
||
|
|
#include "ns3/names.h"
|
||
|
|
#include "ns3/ethernet-net-device.h"
|
||
|
|
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstdlib>
|
||
|
|
|
||
|
|
namespace ns3
|
||
|
|
{
|
||
|
|
|
||
|
|
NS_LOG_COMPONENT_DEFINE("EthernetGenerator");
|
||
|
|
|
||
|
|
NS_OBJECT_ENSURE_REGISTERED(EthernetGenerator);
|
||
|
|
|
||
|
|
TypeId
|
||
|
|
EthernetGenerator::GetTypeId()
|
||
|
|
{
|
||
|
|
static TypeId tid =
|
||
|
|
TypeId("ns3::EthernetGenerator")
|
||
|
|
.SetParent<Application>()
|
||
|
|
.SetGroupName("Applications")
|
||
|
|
.AddConstructor<EthernetGenerator>()
|
||
|
|
.AddAttribute("Address",
|
||
|
|
"Destination Mac Address",
|
||
|
|
AddressValue(Mac48Address("ff:ff:ff:ff:ff:ff")),
|
||
|
|
MakeAddressAccessor(&EthernetGenerator::m_destAddress),
|
||
|
|
MakeAddressChecker())
|
||
|
|
.AddAttribute("PayloadSize",
|
||
|
|
"Payload Size in bytes",
|
||
|
|
UintegerValue(100),
|
||
|
|
MakeUintegerAccessor(&EthernetGenerator::m_payload_size),
|
||
|
|
MakeUintegerChecker<uint16_t>())
|
||
|
|
.AddAttribute("BurstSize",
|
||
|
|
"Burst size",
|
||
|
|
UintegerValue(1),
|
||
|
|
MakeUintegerAccessor(&EthernetGenerator::m_burst_size),
|
||
|
|
MakeUintegerChecker<uint16_t>())
|
||
|
|
.AddAttribute("Period",
|
||
|
|
"Period between burst",
|
||
|
|
TimeValue(Seconds(1)),
|
||
|
|
MakeTimeAccessor(&EthernetGenerator::m_period),
|
||
|
|
MakeTimeChecker())
|
||
|
|
.AddAttribute("InterFrame",
|
||
|
|
"Period between two packet of a burst",
|
||
|
|
TimeValue(Seconds(0)),
|
||
|
|
MakeTimeAccessor(&EthernetGenerator::m_interframe),
|
||
|
|
MakeTimeChecker())
|
||
|
|
.AddAttribute("Offset",
|
||
|
|
"Time offset between application start and first packet emission",
|
||
|
|
TimeValue(Seconds(0)),
|
||
|
|
MakeTimeAccessor(&EthernetGenerator::m_offset),
|
||
|
|
MakeTimeChecker())
|
||
|
|
.AddAttribute("VlanID",
|
||
|
|
"Vlan ID",
|
||
|
|
UintegerValue(65535),
|
||
|
|
MakeUintegerAccessor(&EthernetGenerator::m_vid),
|
||
|
|
MakeUintegerChecker<uint16_t>())
|
||
|
|
.AddAttribute("PCP",
|
||
|
|
"PCP field",
|
||
|
|
UintegerValue(0),
|
||
|
|
MakeUintegerAccessor(&EthernetGenerator::m_pcp),
|
||
|
|
MakeUintegerChecker<uint8_t>())
|
||
|
|
.AddAttribute("DEI",
|
||
|
|
"DEI bit",
|
||
|
|
UintegerValue(0),
|
||
|
|
MakeUintegerAccessor(&EthernetGenerator::m_dei),
|
||
|
|
MakeUintegerChecker<uint8_t>())
|
||
|
|
.AddTraceSource("PktSent",
|
||
|
|
"Trace source indicating a packet was given to the netDevice"
|
||
|
|
"by the application",
|
||
|
|
MakeTraceSourceAccessor(&EthernetGenerator::m_pktSentTrace),
|
||
|
|
"ns3::EthernetGenerator::PacketVlanTraceCallback");
|
||
|
|
return tid;
|
||
|
|
}
|
||
|
|
|
||
|
|
EthernetGenerator::EthernetGenerator()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
m_net = nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
EthernetGenerator::~EthernetGenerator()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::Setup(Ptr<EthernetNetDevice> net)
|
||
|
|
{
|
||
|
|
m_net = net;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::DoDispose()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
Application::DoDispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::StartApplication()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
if (m_vid==65535 && m_payload_size < MIN_PAYLOAD_SIZE + 4){
|
||
|
|
m_payload_size = MIN_PAYLOAD_SIZE + 4;
|
||
|
|
}
|
||
|
|
else if(m_payload_size < MIN_PAYLOAD_SIZE){
|
||
|
|
m_payload_size = MIN_PAYLOAD_SIZE;
|
||
|
|
}
|
||
|
|
m_sendEvent = Simulator::Schedule(m_offset, &EthernetGenerator::SendBurst, this);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::StopApplication()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
Simulator::Cancel(m_sendEvent);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::SendBurst()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
for (int i = 0; i < m_burst_size; i++) {
|
||
|
|
Simulator::Schedule(m_interframe*i, &EthernetGenerator::Send, this);
|
||
|
|
}
|
||
|
|
m_sendEvent = Simulator::Schedule(m_period, &EthernetGenerator::SendBurst, this);
|
||
|
|
}
|
||
|
|
|
||
|
|
void
|
||
|
|
EthernetGenerator::Send()
|
||
|
|
{
|
||
|
|
NS_LOG_FUNCTION(this);
|
||
|
|
|
||
|
|
Ptr<Packet> p = Create<Packet>(m_payload_size);
|
||
|
|
//NS_LOG_INFO((Simulator::Now()).As(Time::S) << " \t" << Names::FindName(m_net->GetNode()) << "/" << Names::FindName(m_net) <<" : Pkt #" << p->GetUid() <<"(vid="<< m_vid <<") given to the netDevice ! " << p->ToString());
|
||
|
|
if (m_vid==65535){
|
||
|
|
m_net->Send(p, m_destAddress, 0xEDE1);
|
||
|
|
}
|
||
|
|
else{
|
||
|
|
m_net->Send(p, m_destAddress, 0xEDE1, m_vid, m_pcp, m_dei);
|
||
|
|
}
|
||
|
|
m_pktSentTrace(p, m_vid);
|
||
|
|
}
|
||
|
|
|
||
|
|
} // Namespace ns3
|