CRE-NS3: Cognitive Radio Extension for NS-3
Cognitive radio extension for ns-3


In this page, we walk through an ns-3 topology to showcase how you can run the simulator with the cognitive radio extension. The source code of the entire cpp run file can be obtained here. You can place this file in the scratch/* directory and simply run
    ./waf --run example
to compile and run it.

We will cover the most relevant part of this script. For an explanation on the rest and what they mean, please refer to any of the ns-3 getting started tutorials.
After setting the mobility model and position our nodes (line 106), we start declaring the CR attributes.

Loading the PU model
  // Read PU file
  Ptr< PUModel > puModel = CreateObject< PUModel >();
  std::string map_file = "map_PUs_multiple.txt";
In this segment, we instantiate the PUModel and load the primary user file. For details about what this primary user file constitues of, please refer to the overview.. You can view the file we use in this example here.

Creating the global repository
We then declare and instantiate the global CR Repository.
  //Create repository
  Ptr< Repository > repo = CreateObject< Repository >();

Install CR devices in nodes
  // Install the CR features into the nodes and return the list of devices
  NetDeviceContainer devices = wifi.InstallCR (repo, puModel, mobility, wifiPhy, wifiMac, c);
Here, we use the newly defined WifiHelper's InstallCR API to install the interfaces in the nodes. More details about this API can be found in our documentation.

Extract CTRL interfaces
We extract the CTRL interfaces from each node's three CR interfaces to assign it an IP address later on.
  // For each CR, we have 3 interfaces. Save the first interface in each
  // node which is the CTRL_IFACE in the device_control array.
  NetDeviceContainer devices_control;
  for (uint32_t i=0; i < devices.GetN(); i=i+3) {

Install the internet stack into each node
  InternetStackHelper internet;
  AodvHelper aodv;
  internet.InstallCR (repo, c);

Assign each CR node an IP address
  Ipv4AddressHelper ipv4;
  NS_LOG_INFO ("Assign IP Addresses.");
  ipv4.SetBase ("", "");
  // IP addresses are only assigned for control devices
  Ipv4InterfaceContainer i = ipv4.Assign (devices_control);
We use the Ipv4AddressHelper to assign each cognitive radio's CTRL interface a unique IP address.

The rest of the script sets up the application to send TCP data from node 0 to node n.
Now we can execute the script and obtain the results:
./waf --run "example"
Waf: Entering directory `/media/main/extra/ns/ns3/ns-3-allinone/ns-3.17/build'
Waf: Leaving directory `/media/main/extra/ns/ns3/ns-3-allinone/ns-3.17/build'
'build' finished successfully (1.149s)
Starting CR test
Total Bytes Received: 7200256

If we run the same simulation with 3 nodes instead of the default 2, we obtain:
./waf --run "example --numNodes=3"
Waf: Entering directory `/media/main/extra/ns/ns3/ns-3-allinone/ns-3.17/build'
Waf: Leaving directory `/media/main/extra/ns/ns3/ns-3-allinone/ns-3.17/build'
'build' finished successfully (1.156s)
Starting CR test
Total Bytes Received: 3692032
We notice the number of received bytes took a substantial hit when we went from 2 to 3 nodes.