From 95ce23acd244a096ca93d4f7987db00aca77253e Mon Sep 17 00:00:00 2001 From: Vineet1101 Date: Tue, 3 Feb 2026 18:20:28 +0530 Subject: [PATCH 1/5] added steps to run example in ns-3 Signed-off-by: Vineet1101 --- doc/P4sim/README.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/doc/P4sim/README.md b/doc/P4sim/README.md index bc085125..4204beb6 100644 --- a/doc/P4sim/README.md +++ b/doc/P4sim/README.md @@ -82,18 +82,7 @@ cd ../.. ``` --- - -## 4. Run an Example - -You can run a built-in example using: - -```bash -./ns3 run "exampleA" # This will run exampleA (name). -``` - ---- - -## 5. Configure P4 Files in Your Simulation +## 4. Configure P4 Files in Your Simulation You may need to **manually update file paths** for P4 artifacts in your simulation code. @@ -108,6 +97,13 @@ std::string topoInput = "/home/p4/workdir/ns3.39/contrib/p4sim/test/test_simple/ Make sure these paths match your actual working directory and files. +--- +## 5. Run an Example +Before running the example you need to **copy and paste** that particular example inside `ns3's scratch` folder then you can run a built-in example using: + +```bash +./ns3 run scratch/"exampleA" # You should be in the ns-3 directory before running this command.This will run exampleA (name). +``` --- ## Done! From 4883788ddf73396a66ac5532cc928cdfbbdfd288 Mon Sep 17 00:00:00 2001 From: Vineet1101 Date: Sun, 15 Feb 2026 15:38:20 +0530 Subject: [PATCH 2/5] added more details to run examples Signed-off-by: Vineet1101 --- doc/P4sim/README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/doc/P4sim/README.md b/doc/P4sim/README.md index 4204beb6..d47e8295 100644 --- a/doc/P4sim/README.md +++ b/doc/P4sim/README.md @@ -82,27 +82,26 @@ cd ../.. ``` --- -## 4. Configure P4 Files in Your Simulation +## 4. Running an example -You may need to **manually update file paths** for P4 artifacts in your simulation code. - -Example path updates: +Choose any example you want to run from `p4sim/examples` directory. Let's say you want to run `p4-basic-controller.cc` then inside the file's code change these paths according to your folder structure: ```cpp // p4 is the username -std::string p4JsonPath = "/home/p4/workdir/ns3.39/contrib/p4sim/test/test_simple/test_simple.json"; -std::string flowTablePath = "/home/p4/workdir/ns3.39/contrib/p4sim/test/test_simple/flowtable_0.txt"; -std::string topoInput = "/home/p4/workdir/ns3.39/contrib/p4sim/test/test_simple/topo.txt"; + std::string p4JsonPath = "/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/p4_basic.json"; + std::string flowTableDirPath ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/";// path to flow table folder where all flow tables are located. + std::string topoInput ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/topo.txt";//path to topo file that the example uses + // This is for p4-basic-controller.cc file ``` Make sure these paths match your actual working directory and files. --- ## 5. Run an Example -Before running the example you need to **copy and paste** that particular example inside `ns3's scratch` folder then you can run a built-in example using: +Before running the example you need to **copy and paste** that particular example inside `ns3's scratch` folder then you can run a example using: ```bash -./ns3 run scratch/"exampleA" # You should be in the ns-3 directory before running this command.This will run exampleA (name). +./ns3 run scratch/p4-basic-controller.cc # You should be in the ns-3 directory before running this command. This will run p4-basic-controller.cc. ``` --- From 089af33c5f25e1f37f427f001b5cd6bc0716c11e Mon Sep 17 00:00:00 2001 From: Vineet1101 Date: Tue, 17 Feb 2026 08:42:11 +0530 Subject: [PATCH 3/5] added more details and bash script to run p4sim examples Signed-off-by: Vineet1101 --- doc/P4sim/README.md | 48 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/doc/P4sim/README.md b/doc/P4sim/README.md index d47e8295..ff20f51f 100644 --- a/doc/P4sim/README.md +++ b/doc/P4sim/README.md @@ -82,26 +82,54 @@ cd ../.. ``` --- -## 4. Running an example +## 4. Prepare an Example -Choose any example you want to run from `p4sim/examples` directory. Let's say you want to run `p4-basic-controller.cc` then inside the file's code change these paths according to your folder structure: +You can run any example from the `p4sim/examples` directory. In this guide, we use `p4-basic-controller.cc` as a demonstration, but the steps apply to any example file (with appropriate path adjustments). +To run `p4-basic-controller.cc`, you must ensure the file paths inside the code match your local directory structure. + +The original file uses placeholder paths: ```cpp -// p4 is the username std::string p4JsonPath = "/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/p4_basic.json"; - std::string flowTableDirPath ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/";// path to flow table folder where all flow tables are located. - std::string topoInput ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/topo.txt";//path to topo file that the example uses - // This is for p4-basic-controller.cc file + std::string flowTableDirPath ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/"; + std::string topoInput ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/topo.txt"; ``` -Make sure these paths match your actual working directory and files. +You can automatically copy the example to the `scratch` directory and update these paths using the commands below: + +```bash +# Ensure you are in the ns-3 root directory +# Define the example you want to run (change this for other examples) +EXAMPLE_NAME="p4-basic-controller.cc" + +# Ensure you are in the ns-3 root directory +cd ~/workdir/ns3.39 + +# 1. Copy the example to the scratch directory +cp contrib/p4sim/examples/$EXAMPLE_NAME scratch/ + +# 2. Update the hardcoded paths to match your current location +# Get the absolute path to the examples directory +P4_EXAMPLES_DIR=$(pwd)/contrib/p4sim/examples/ + +# Use sed to replace the placeholder path with your actual path in the copied file +sed -i "s|/home/p4/workdir/ns3.39/contrib/p4sim/examples/|$P4_EXAMPLES_DIR|g" scratch/$EXAMPLE_NAME + +# Notes: +# - The sed command assumes the example uses the specific placeholder path: +# "/home/p4/workdir/ns3.39/contrib/p4sim/examples/" +# - If your example uses different paths, you may need to adjust the sed command or edit the file manually. +``` --- -## 5. Run an Example -Before running the example you need to **copy and paste** that particular example inside `ns3's scratch` folder then you can run a example using: +## 5. Run the Example + +With the file configured in the `scratch` folder, execute it using: ```bash -./ns3 run scratch/p4-basic-controller.cc # You should be in the ns-3 directory before running this command. This will run p4-basic-controller.cc. +# Run the example using the variable defined above, or type the name directly +./ns3 run scratch/$EXAMPLE_NAME + ``` --- From b40167ee8864b03fa62d00d6c7025091015546f4 Mon Sep 17 00:00:00 2001 From: Vineet1101 Date: Tue, 17 Feb 2026 09:11:21 +0530 Subject: [PATCH 4/5] removed repeated parts in the docs and added bash script Signed-off-by: Vineet1101 --- doc/P4sim/README.md | 54 ++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/doc/P4sim/README.md b/doc/P4sim/README.md index ff20f51f..de88c786 100644 --- a/doc/P4sim/README.md +++ b/doc/P4sim/README.md @@ -222,17 +222,24 @@ git apply ./contrib/p4sim/doc/changes.patch ./ns3 build ``` -### 7. Run a Simulation Example +### 7. Run a Simulation Example + +Instead of modifying the source files directly, it is recommended to copy the example to the `scratch` directory. We can then use a command to automatically update the hardcoded paths to match your environment. + ```bash -./ns3 run "exampleA" # This will run exampleA (name). - -# In the p4sim example, you may need to adjust the path of p4 and other files. -# For example: -# std::string p4JsonPath = -# "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/test_simple.json"; -# std::string flowTablePath = -# "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/flowtable_0.txt"; -# std::string topoInput = "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/topo.txt"; +# Ensure you are in the ns-3 root directory +EXAMPLE_NAME="p4-basic-controller.cc" +P4_EXAMPLES_DIR=$(pwd)/contrib/p4sim/examples/ + +# 1. Copy the example to the scratch directory +cp contrib/p4sim/examples/$EXAMPLE_NAME scratch/ + +# 2. Update the paths automatically +# Note: This command replaces the default placeholder path with your actual path +sed -i "s|/home/p4/workdir/ns3.39/contrib/p4sim/examples/|$P4_EXAMPLES_DIR|g" scratch/$EXAMPLE_NAME + +# 3. Run the example +./ns3 run scratch/$EXAMPLE_NAME ``` --- @@ -299,17 +306,24 @@ sudo ./set_pkg_config_env.sh ./ns3 build ``` -### 7. Run a Simulation Example +### 7. Run a Simulation Example + +Instead of modifying the source files directly, it is recommended to copy the example to the `scratch` directory. We can then use a command to automatically update the hardcoded paths to match your environment. + ```bash -./ns3 run "exampleA" # This will run exampleA (name). - -# In the p4sim example, you may need to adjust the path of p4 and other files. -# For example: -# std::string p4JsonPath = -# "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/test_simple.json"; -# std::string flowTablePath = -# "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/flowtable_0.txt"; -# std::string topoInput = "/home/p4/workdir/ns3.35/contrib/p4sim/test/test_simple/topo.txt"; +# Ensure you are in the ns-3 root directory +EXAMPLE_NAME="p4-basic-controller.cc" +P4_EXAMPLES_DIR=$(pwd)/contrib/p4sim/examples/ + +# 1. Copy the example to the scratch directory +cp contrib/p4sim/examples/$EXAMPLE_NAME scratch/ + +# 2. Update the paths automatically +# Note: This command replaces the default placeholder path with your actual path +sed -i "s|/home/p4/workdir/ns3.39/contrib/p4sim/examples/|$P4_EXAMPLES_DIR|g" scratch/$EXAMPLE_NAME + +# 3. Run the example +./ns3 run scratch/$EXAMPLE_NAME ``` --- From 93006f92b2e65711de1cc8110a1f3d0f94828047 Mon Sep 17 00:00:00 2001 From: Vineet1101 Date: Tue, 17 Feb 2026 15:22:24 +0530 Subject: [PATCH 5/5] merged section 4 and 5 of the documentaion Signed-off-by: Vineet1101 --- doc/P4sim/README.md | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/doc/P4sim/README.md b/doc/P4sim/README.md index de88c786..d7303892 100644 --- a/doc/P4sim/README.md +++ b/doc/P4sim/README.md @@ -82,29 +82,21 @@ cd ../.. ``` --- -## 4. Prepare an Example +## 4. Run a Simulation Example You can run any example from the `p4sim/examples` directory. In this guide, we use `p4-basic-controller.cc` as a demonstration, but the steps apply to any example file (with appropriate path adjustments). -To run `p4-basic-controller.cc`, you must ensure the file paths inside the code match your local directory structure. +To run `p4-basic-controller.cc`, you must first ensure the file paths inside the code match your local directory structure. The original file uses placeholder paths, which we will update automatically. We will also copy the example to the `scratch` directory to avoid modifying the source directly. -The original file uses placeholder paths: -```cpp - std::string p4JsonPath = "/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/p4_basic.json"; - std::string flowTableDirPath ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/"; - std::string topoInput ="/home/p4/workdir/ns3.39/contrib/p4sim/examples/p4src/p4_basic/topo.txt"; -``` - -You can automatically copy the example to the `scratch` directory and update these paths using the commands below: +Run the following commands to prepare and run the example: ```bash # Ensure you are in the ns-3 root directory +cd ~/workdir/ns3.39 + # Define the example you want to run (change this for other examples) EXAMPLE_NAME="p4-basic-controller.cc" -# Ensure you are in the ns-3 root directory -cd ~/workdir/ns3.39 - # 1. Copy the example to the scratch directory cp contrib/p4sim/examples/$EXAMPLE_NAME scratch/ @@ -113,23 +105,12 @@ cp contrib/p4sim/examples/$EXAMPLE_NAME scratch/ P4_EXAMPLES_DIR=$(pwd)/contrib/p4sim/examples/ # Use sed to replace the placeholder path with your actual path in the copied file +# Note: This command targets the specific placeholder path used in the example: +# "/home/p4/workdir/ns3.39/contrib/p4sim/examples/" sed -i "s|/home/p4/workdir/ns3.39/contrib/p4sim/examples/|$P4_EXAMPLES_DIR|g" scratch/$EXAMPLE_NAME -# Notes: -# - The sed command assumes the example uses the specific placeholder path: -# "/home/p4/workdir/ns3.39/contrib/p4sim/examples/" -# - If your example uses different paths, you may need to adjust the sed command or edit the file manually. -``` - ---- -## 5. Run the Example - -With the file configured in the `scratch` folder, execute it using: - -```bash -# Run the example using the variable defined above, or type the name directly +# 3. Run the example ./ns3 run scratch/$EXAMPLE_NAME - ``` ---