Files

286 lines
9.1 KiB
C++
Raw Permalink Normal View History

2025-12-01 15:56:02 +01:00
#include "tsn-node.h"
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/names.h"
#include "ns3/clock.h"
#include "ns3/tsn-net-device.h"
#include "ns3/tsn-aggregated-net-device.h"
#include "ns3/tsn-multidrop-net-device.h"
#include "ns3/switch-net-device.h"
namespace ns3
{
NS_LOG_COMPONENT_DEFINE("TsnNode");
NS_OBJECT_ENSURE_REGISTERED(TsnNode);
TypeId
TsnNode::GetTypeId()
{
static TypeId tid =
TypeId("ns3::TsnNode")
.SetParent<Node>()
.SetGroupName("tsn")
.AddConstructor<TsnNode>()
.AddAttribute("MaxSidEntryNumber",
"The maximum number of entry in the stream identification function table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxSidEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxPsfpFilterEntryNumber",
"The maximum number of entry in the stream filter table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxPsfpFilterEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxPsfpStreamGateEntryNumber",
"The maximum number of entry in the stream gate table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxPsfpStreamGateEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxPsfpFlowMeterEntryNumber",
"The maximum number of entry in the flow meter table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxPsfpFlowMeterEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxFrerSeqGenEntryNumber",
"The maximum number of entry in the sequence generation table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxFrerSeqGenEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxFrerSeqRcvyEntryNumber",
"The maximum number of entry in the sequence recovery table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxFrerSeqRcvyEntryNumber),
MakeUintegerChecker<uint16_t>())
.AddAttribute("MaxFrerSeqEncEntryNumber",
"The maximum number of entry in the sequence encode table",
UintegerValue(65535),
MakeUintegerAccessor(&TsnNode::m_maxFrerSeqEncEntryNumber),
MakeUintegerChecker<uint16_t>());
return tid;
}
TsnNode::TsnNode() : Node()
{
NS_LOG_FUNCTION(this);
m_mainClock = CreateObject<Clock>();
}
TsnNode::TsnNode(uint32_t sid) : Node(sid)
{
NS_LOG_FUNCTION(this << sid);
}
TsnNode::~TsnNode()
{
NS_LOG_FUNCTION(this);
}
uint32_t
TsnNode::AddDevice(Ptr<TsnNetDevice> device)
{
NS_LOG_FUNCTION(this << device);
device->SetNode(this);
return Node::AddDevice(device);
}
uint32_t
TsnNode::AddDevice(Ptr<TsnAggregatedNetDevice> device)
{
NS_LOG_FUNCTION(this << device);
device->SetNode(this);
return Node::AddDevice(device);
}
uint32_t
TsnNode::AddDevice(Ptr<TsnMultidropNetDevice> device)
{
NS_LOG_FUNCTION(this << device);
device->SetNode(this);
return Node::AddDevice(device);
}
uint32_t
TsnNode::AddDevice(Ptr<SwitchNetDevice> device)
{
NS_LOG_FUNCTION(this << device);
device->SetNode(this);
return Node::AddDevice(device);
}
void
TsnNode::SetMainClock(Ptr<Clock> clock)
{
NS_LOG_FUNCTION(this);
clock->AddClockCorrectionCallback(MakeCallback(&TsnNode::CallBackClockUpdate, this));
m_mainClock = clock;
}
void
TsnNode::AddClock(Ptr<Clock> clock)
{
NS_LOG_FUNCTION(this);
clock->AddClockCorrectionCallback(MakeCallback(&TsnNode::CallBackClockUpdate, this));
m_clocks.insert(m_clocks.end(), clock);
}
void
TsnNode::CallBackClockUpdate()
{
NS_LOG_FUNCTION(this);
//Update all TAS on each node net device
for(int i = 0; i<(int)GetNDevices(); i++)
{
if( GetDevice(i)->GetObject<TsnNetDevice>() != nullptr)
{
GetDevice(i)->GetObject<TsnNetDevice>()->GetTas()->ClockUpdate();
}
}
}
void
TsnNode::setActiveClock(uint32_t activeClockId)
{
NS_LOG_FUNCTION(this);
m_activeClockId = activeClockId;
}
Ptr<Clock>
TsnNode::GetActiveClock()
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_clocks.size() > m_activeClockId, "No active clock on Node " << this);
return m_clocks[m_activeClockId];
}
Time
TsnNode::GetTime() const
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_mainClock != nullptr, "No main clock was declared with SetMainClock(Ptr<Clock> clock) on Node " << this);
return m_mainClock->GetLocalTime();
}
Time
TsnNode::GetTime(uint32_t clockId) const
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_clocks.size() > clockId, "No clock with clockId "<< clockId << " on Node " << this);
return m_clocks[clockId]->GetLocalTime();
}
//Stream identification
void
TsnNode::AddStreamIdentificationFunction(
uint32_t streamHandle,
Ptr<StreamIdentificationFunction> streamIdentificationFunction,
std::vector<Ptr<TsnNetDevice>> outFacInputPortList,
std::vector<Ptr<TsnNetDevice>> inFacInputPortList,
std::vector<Ptr<TsnNetDevice>> inFacOutputPortList,
std::vector<Ptr<TsnNetDevice>> outFacOutputPortList)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_streamIdentityTable.size() < m_maxSidEntryNumber, "Trying to add more stream identification function than the " << m_maxSidEntryNumber << " available.");
Ptr<StreamIdEntry> entry = CreateObject<StreamIdEntry>();
entry->SetAttribute("StreamHandle", UintegerValue(streamHandle));
entry->SetStreamIdentificationFunction(streamIdentificationFunction);
entry->SetOutFacInputPortList(outFacInputPortList);
entry->SetInFacInputPortList(inFacInputPortList);
entry->SetInFacOutputPortList(inFacOutputPortList);
entry->SetOutFacOutputPortList(outFacOutputPortList);
m_streamIdentityTable.insert(m_streamIdentityTable.end(),entry);
}
std::vector<Ptr<StreamIdEntry>>
TsnNode::GetStreamIdentityTable(){
NS_LOG_FUNCTION(this);
return m_streamIdentityTable;
}
//PSFP
void
TsnNode::AddStreamFilter(Ptr<StreamFilterInstance> streamFilterInstance)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_streamFilterInstanceTable.size() < m_maxPsfpFilterEntryNumber, "Trying to add more stream filter than the " << m_maxPsfpFilterEntryNumber << " available.");
m_streamFilterInstanceTable.insert(m_streamFilterInstanceTable.end(),streamFilterInstance);
}
uint16_t
TsnNode::AddFlowMeter(Ptr<FlowMeterInstance> flowMeterInstance)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_flowMeterInstanceTable.size() < m_maxPsfpFlowMeterEntryNumber, "Trying to add more flow meter than the " << m_maxPsfpFlowMeterEntryNumber << " available.");
m_flowMeterInstanceTable.insert(m_flowMeterInstanceTable.end(),flowMeterInstance);
return m_flowMeterInstanceTable.size() - 1;
}
std::vector<Ptr<StreamFilterInstance>>
TsnNode::GetStreamFilterInstanceTable()
{
NS_LOG_FUNCTION(this);
return m_streamFilterInstanceTable;
}
std::vector<Ptr<FlowMeterInstance>>
TsnNode::GetFlowMeterInstanceTable()
{
NS_LOG_FUNCTION(this);
return m_flowMeterInstanceTable;
}
//FRER
void
TsnNode::AddSequenceGenerationFunction(Ptr<SequenceGenerationFunction> entry)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_frerSeqGenTable.size() < m_maxFrerSeqGenEntryNumber, "Trying to add more sequence generation fonction than the " << m_maxFrerSeqGenEntryNumber << " available.");
m_frerSeqGenTable.insert(m_frerSeqGenTable.end(),entry);
}
std::vector<Ptr<SequenceGenerationFunction>>
TsnNode::GetSequenceGenerationTable()
{
NS_LOG_FUNCTION(this);
return m_frerSeqGenTable;
}
void
TsnNode::AddSequenceRecoveryFunction(Ptr<SequenceRecoveryFunction> entry)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_frerSeqRcvyTable.size() < m_maxFrerSeqRcvyEntryNumber, "Trying to add more sequence recovery fonction than the " << m_maxFrerSeqRcvyEntryNumber << " available.");
m_frerSeqRcvyTable.insert(m_frerSeqRcvyTable.end(),entry);
}
std::vector<Ptr<SequenceRecoveryFunction>>
TsnNode::GetSequenceRecoveryTable()
{
NS_LOG_FUNCTION(this);
return m_frerSeqRcvyTable;
}
void
TsnNode::AddSequenceEncodeDecodeFunction(Ptr<SequenceEncodeDecodeFunction> entry)
{
NS_LOG_FUNCTION(this);
NS_ASSERT_MSG(m_frerSeqEncTable.size() < m_maxFrerSeqEncEntryNumber, "Trying to add more sequence encode/decode fonction than the " << m_maxFrerSeqEncEntryNumber << " available.");
m_frerSeqEncTable.insert(m_frerSeqEncTable.end(),entry);
}
std::vector<Ptr<SequenceEncodeDecodeFunction>>
TsnNode::GetSequenceEncodeDecodeTable()
{
NS_LOG_FUNCTION(this);
return m_frerSeqEncTable;
}
}