可參考程序examples/p4-topo-test.cc
1 定義相關全局變量
主要包括ns4文件路徑信息,運行時參數設置信息,初始化下發流表API接口等
P4GlobalVar::g_homePath="/home/kphf1995cm/";
P4GlobalVar::g_ns3RootName = "ns-allinone-3.26/";
P4GlobalVar::g_ns3SrcName = "ns-3.26/";
P4GlobalVar::g_nfDir = P4GlobalVar::g_homePath + P4GlobalVar::g_ns3RootName + P4GlobalVar::g_ns3SrcName + "src/ns4/test/";
P4GlobalVar::g_topoDir = P4GlobalVar::g_homePath + P4GlobalVar::g_ns3RootName + P4GlobalVar::g_ns3SrcName + "src/ns4/topo/";
P4GlobalVar::g_nsType = NS4;
P4GlobalVar::g_runtimeCliTime=10;
SwitchApi::InitApiMap();
P4GlobalVar::InitNfStrUintMap();
2 定義網絡拓撲格式以及路徑
這裏可以繼承P4TopologyReader,實現其中的Read函數,自定義其它的topo格式
我們這裏使用的是已編寫的CsmaTopologyReader
std::string topoFormat("CsmaTopo");
std::string topoPath = P4GlobalVar::g_topoDir + "csmaTopo.txt";
3 定義命令行解析參數
便於在運行時調節參數
CommandLine cmd;
cmd.AddValue("format", "Format to use for data input [Orbis|Inet|Rocketfuel|CsmaTopo].",
topoFormat);
cmd.AddValue("model", "Select p4 model[0] or traditional bridge model[1]", P4GlobalVar::g_nsType);
cmd.AddValue("podnum", "Numbers of built tree topo levels", podNum);
cmd.AddValue("build","Build flow table entries by program[1] or not[0]",toBuild);
cmd.AddValue("application","Application type OnoffSink[0] UdpClientServer[1]",application);
cmd.Parse(argc, argv);
4 構建網絡拓撲
這裏可編寫程序構建需要的網絡拓撲或者直接省略手動定義網絡拓撲
我們這邊採用已編寫的fattree拓撲構建程序
構建好的網絡拓撲會保存在topo目錄下
FattreeTopoHelper treeTopo(podNum,topoPath);
treeTopo.Write();
5 讀取網絡拓撲
P4TopologyReaderHelper p4TopoHelp;
p4TopoHelp.SetFileName(topoInput);
p4TopoHelp.SetFileType(topoFormat);
Ptr<P4TopologyReader> topoReader = p4TopoHelp.GetTopologyReader();
if (topoReader != 0)
{
topoReader->Read();
}
if (topoReader->LinksSize() == 0)
{
NS_LOG_ERROR("Problems reading the topology file. Failing.");
return -1;
}
6 初始化網絡鏈路信息
這裏是根據所讀取的網絡拓撲來初始化網絡鏈路信息
7 構建流表項
這裏是通過程序自動構建每個交換機所需要的流表項,主要是使得數據包能夠從源主機到達目的主機
所構建的流表項保存在flowtable目錄中
也可以手動編寫每個交換機所需要的流表項
if(toBuild==1&&P4GlobalVar::g_nsType==NS4)
{
NS_LOG_LOGIC("BuildFlowtableHelper");
//BuildFlowtableHelper flowtableHelper("fattree",podNum);
BuildFlowtableHelper flowtableHelper;
flowtableHelper.Build(linkSwitchIndex,linkSwitchPort,hostIpv4,switchPortInfo);
flowtableHelper.Write(P4GlobalVar::g_flowTableDir);
flowtableHelper.Show();
}
8 聯通交換機與網絡設備
這裏會構建P4交換機,並將P4交換機的每個端口與一個網絡設備相連
在構建P4交換機同時,會提前下發該交換機的流表項
新創建的P4交換機會被添加到P4控制器中,以便於管理
if (P4GlobalVar::g_nsType == NS4)
{
P4GlobalVar::g_populateFlowTableWay = LOCAL_CALL;
std::string flowTableName;
P4Helper bridge;
for (unsigned int i = 0; i < switchNum; i++)
{
flowTableName = UintToString(i);
//P4GlobalVar::g_networkFunc = SIMPLE_ROUTER;
P4GlobalVar::g_networkFunc = P4GlobalVar::g_nfStrUintMap[switchNetFunc[i]];
P4GlobalVar::SetP4MatchTypeJsonPath();
P4GlobalVar::g_flowTablePath = P4GlobalVar::g_flowTableDir + flowTableName;
bridge.Install(csmaSwitch.Get(i),switchNodes[i].switchDevices);// do what?
}
}
9 安裝應用程序
這裏可給主機或交換機安裝一些應用程序,例如OnoffSink或UdpEchoClient等,也可實現自定義的應用
10 進行模擬
設置啓動、關閉應用程序的時間
這裏的模擬過程是基於ns3的離散事件處理的