Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ build_lib(
model/p4-switch-net-device.cc
model/custom-p2p-net-device.cc
model/p4-controller.cc
model/dummy-switch-port.cc
model/dummy-switch-net-device.cc
helper/p4-helper.cc
helper/p4-topology-reader-helper.cc
helper/p4-p2p-helper.cc
helper/build-flowtable-helper.cc
helper/dummy-switch-helper.cc
HEADER_FILES # equivalent to headers.source
utils/p4-queue.h
utils/format-utils.h
Expand All @@ -86,9 +89,12 @@ build_lib(
model/p4-switch-net-device.h
model/custom-p2p-net-device.h
model/p4-controller.h
model/dummy-switch-port.h
model/dummy-switch-net-device.h
helper/p4-helper.h
helper/p4-topology-reader-helper.h
helper/p4-p2p-helper.h
helper/dummy-switch-helper.h
helper/build-flowtable-helper.h
LIBRARIES_TO_LINK
${libcore}
Expand Down
11 changes: 11 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ build_lib_example(
SOURCE_FILES p4-ipv4-animation.cc
LIBRARIES_TO_LINK ${P4SIM_CSMA_LIBS} ${libnetanim} ${libmobility}
)
build_lib_example(
NAME dummy-switch-example
SOURCE_FILES dummy-switch-example.cc
LIBRARIES_TO_LINK
${libp4sim}
${libinternet}
${libapplications}
${libnetwork}
${libcsma}
${libtraffic-control}
)

# ========================= PSA Architecture ===========================

Expand Down
157 changes: 157 additions & 0 deletions examples/dummy-switch-example.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2025 TU Dresden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Mingyu Ma <mingyu.ma@tu-dresden.de>
*/

/**
* Dummy Switch Example
*
* Demonstrates the skeleton DummySwitchNetDevice with 3 hosts and 1 switch.
* Each host is connected via a CSMA link. An optional FifoQueueDisc traffic
* manager is attached to port 2 (egress toward host 2).
*
* Host0 ──┐
* Host1 ──┼── DummySwitch
* Host2 ──┘
*
* A UDP packet is sent from Host0 to Host2. The switch floods to all ports
* except the ingress, so Host1 and Host2 both receive the packet. The TM
* on port 2 is logged.
*/

#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/csma-helper.h"
#include "ns3/dummy-switch-helper.h"
#include "ns3/fifo-queue-disc.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/traffic-control-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("DummySwitchExample");

int
main(int argc, char* argv[])
{
LogComponentEnable("DummySwitchExample", LOG_LEVEL_INFO);
LogComponentEnable("DummySwitchNetDevice", LOG_LEVEL_INFO);
LogComponentEnable("DummySwitchPort", LOG_LEVEL_INFO);

bool enableTm = true;
uint16_t pktSize = 512;

CommandLine cmd;
cmd.AddValue("enableTm", "Enable traffic manager on port 2", enableTm);
cmd.AddValue("pktSize", "UDP packet size in bytes", pktSize);
cmd.Parse(argc, argv);

// ======================== Create nodes ========================
NS_LOG_INFO("Creating 3 hosts and 1 switch node...");
NodeContainer hosts;
hosts.Create(3);

NodeContainer switchNode;
switchNode.Create(1);

// ======================== Create CSMA links ========================
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));

// One link from each host to the switch
NetDeviceContainer switchPorts;
std::vector<NetDeviceContainer> hostDevices(3);

for (uint32_t i = 0; i < 3; i++)
{
NetDeviceContainer link = csma.Install(
NodeContainer(hosts.Get(i), switchNode.Get(0)));
hostDevices[i].Add(link.Get(0)); // host side
switchPorts.Add(link.Get(1)); // switch side
}

// ======================== Install Internet stack ========================
InternetStackHelper internet;
internet.Install(hosts);

// Assign IP addresses to host devices
Ipv4AddressHelper ipv4;
ipv4.SetBase("10.1.1.0", "255.255.255.0");
std::vector<Ipv4InterfaceContainer> hostInterfaces(3);
for (uint32_t i = 0; i < 3; i++)
{
hostInterfaces[i] = ipv4.Assign(hostDevices[i]);
}

// ======================== Install DummySwitch ========================
NS_LOG_INFO("Installing DummySwitchNetDevice on switch node...");
DummySwitchHelper dummySwitchHelper;
Ptr<DummySwitchNetDevice> sw = dummySwitchHelper.Install(switchNode.Get(0), switchPorts);

// ======================== Attach Traffic Manager ========================
if (enableTm)
{
NS_LOG_INFO("Attaching FifoQueueDisc to port 2...");
Ptr<FifoQueueDisc> fifo = CreateObject<FifoQueueDisc>();
fifo->SetMaxSize(QueueSize("100p"));
sw->SetPortTrafficManager(2, fifo);
}

// ======================== Print topology info ========================
NS_LOG_INFO("Switch has " << sw->GetNPorts() << " ports:");
for (uint32_t i = 0; i < sw->GetNPorts(); i++)
{
Ptr<DummySwitchPort> port = sw->GetPort(i);
NS_LOG_INFO(" Port " << i
<< " -> " << port->GetNetDevice()->GetInstanceTypeId().GetName()
<< " (TM: " << (port->HasTrafficManager() ? "yes" : "no")
<< ", Up: " << (port->IsPortUp() ? "yes" : "no") << ")");
}

// ======================== Setup applications ========================
// UDP echo server on host 2
uint16_t port = 9;
UdpEchoServerHelper echoServer(port);
ApplicationContainer serverApp = echoServer.Install(hosts.Get(2));
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));

// UDP echo client on host 0
Ipv4Address serverAddr = hostInterfaces[2].GetAddress(0);
UdpEchoClientHelper echoClient(serverAddr, port);
echoClient.SetAttribute("MaxPackets", UintegerValue(3));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(pktSize));

ApplicationContainer clientApp = echoClient.Install(hosts.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));

// ======================== Enable PCAP tracing ========================
csma.EnablePcapAll("dummy-switch-example");

// ======================== Run simulation ========================
NS_LOG_INFO("Running simulation...");
Simulator::Stop(Seconds(11.0));
Simulator::Run();
Simulator::Destroy();

NS_LOG_INFO("Simulation complete.");
return 0;
}
69 changes: 69 additions & 0 deletions model/dummy-switch-helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2025 TU Dresden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Mingyu Ma <mingyu.ma@tu-dresden.de>
*/

#include "ns3/dummy-switch-helper.h"

#include "ns3/log.h"
#include "ns3/names.h"

namespace ns3
{

NS_LOG_COMPONENT_DEFINE("DummySwitchHelper");

DummySwitchHelper::DummySwitchHelper()
{
NS_LOG_FUNCTION(this);
m_deviceFactory.SetTypeId("ns3::DummySwitchNetDevice");
}

void
DummySwitchHelper::SetDeviceAttribute(std::string n1, const AttributeValue& v1)
{
NS_LOG_FUNCTION(this << n1);
m_deviceFactory.Set(n1, v1);
}

Ptr<DummySwitchNetDevice>
DummySwitchHelper::Install(Ptr<Node> node, NetDeviceContainer portDevices)
{
NS_LOG_FUNCTION(this << node);

Ptr<DummySwitchNetDevice> dev = m_deviceFactory.Create<DummySwitchNetDevice>();
node->AddDevice(dev);

for (uint32_t i = 0; i < portDevices.GetN(); i++)
{
dev->AddPort(portDevices.Get(i));
}

NS_LOG_INFO("Installed DummySwitchNetDevice on node " << node->GetId()
<< " with " << portDevices.GetN() << " ports");
return dev;
}

Ptr<DummySwitchNetDevice>
DummySwitchHelper::Install(std::string nodeName, NetDeviceContainer portDevices)
{
NS_LOG_FUNCTION(this << nodeName);
Ptr<Node> node = Names::Find<Node>(nodeName);
return Install(node, portDevices);
}

} // namespace ns3
78 changes: 78 additions & 0 deletions model/dummy-switch-helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025 TU Dresden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Authors: Mingyu Ma <mingyu.ma@tu-dresden.de>
*/

#ifndef DUMMY_SWITCH_HELPER_H
#define DUMMY_SWITCH_HELPER_H

#include "ns3/dummy-switch-net-device.h"
#include "ns3/net-device-container.h"
#include "ns3/node.h"
#include "ns3/object-factory.h"

namespace ns3
{

/**
* \ingroup p4sim
* \brief Helper to install DummySwitchNetDevice on nodes.
*
* DummySwitchHelper simplifies creating and configuring a DummySwitchNetDevice.
* It follows the same pattern as P4Helper and BridgeHelper.
*/
class DummySwitchHelper
{
public:
DummySwitchHelper();

/**
* \brief Set an attribute on the DummySwitchNetDevice.
* \param n1 attribute name
* \param v1 attribute value
*/
void SetDeviceAttribute(std::string n1, const AttributeValue& v1);

/**
* \brief Install a DummySwitchNetDevice on a node.
*
* Creates a DummySwitchNetDevice, adds each NetDevice in the container
* as a port, and installs the switch on the node.
*
* \param node the node to install on
* \param portDevices the NetDevices to add as switch ports
* \return the created DummySwitchNetDevice
*/
Ptr<DummySwitchNetDevice> Install(Ptr<Node> node,
NetDeviceContainer portDevices);

/**
* \brief Install a DummySwitchNetDevice on a node (by name).
* \param nodeName the name of the node
* \param portDevices the NetDevices to add as switch ports
* \return the created DummySwitchNetDevice
*/
Ptr<DummySwitchNetDevice> Install(std::string nodeName,
NetDeviceContainer portDevices);

private:
ObjectFactory m_deviceFactory; //!< Factory for creating DummySwitchNetDevice
};

} // namespace ns3

#endif /* DUMMY_SWITCH_HELPER_H */
Loading