Think it as just wire, though CSMA helper help you do more.

sample code

The sample code shows 4 nodes in the same subnet. It is from src/csma/example/csma-one-subnet.cc

I don't explain every line of code, I just explain the part I think critical.

At very beginning, you need to know the two very common objects in the NS-3.

  1. Helper

    Helper is to help you install some protocols or application on the node. Most of time they called XXXHelper, e.g, CSMAHelper. The usage of the helper is to 1.declare a object, 2.using the member function of object to setup the parameter, 3.install it on the nodes (not always nodes)

  2. Container

    There are a lot of data structure to convey the nodes or other useful stuffs. e.g., NodeContainer, NetDeviceContainer, Ipv4InterfaceContainer, etc. You can manipulate them through XXX.Get(i) method, i is the index, XXX is the object instance.

For now, let's start to review the componenets in the code.

  1. CSMAHelper

    it is used to create the CSMA topology. I always think this as the normal wire. SetChannelAttribute is used to setup some parameter. the first string parameter is used to specifciy what the meaning of the arguments is, the second is used to pass the value. The "DataRateValue" and the "TimeValue" is some internal function or constructor (I'm not sure.) used to do the transformation for the convenience.

    Then used the csma, this settle down object to install on the node. Then the nodes are equipped with the CSMA network device and it will return the netdevice container. You can access the network device thorugh the devices ,this container, to manipulate the netdevice. the order will the same as nodes. e.g., devices.Get(0) is the device for nodes.Get(0).

    CsmaHelper csma;
    csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
    csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds  (2)));       
    NetDeviceContainer devices = csma.Install (nodes);
  2. InternetStackHelper

    The nodes with network device is not enough. If you would like to do the global routing. (What's that, in fact I don't know) you need to install the InternetStackHelper to help it do the global routing or your packet is impossible to arrive at the destination.
    What you need to pay attention is that you need to pass the nodes but not devices.

    InternetStackHelper internet;
    internet.Install (nodes);
  3. Ipv4AddressHelper
    This is used to setup ip addresses. It is necessary if you want to send your packet through Internet Protocol. There are some trap for ip assignment.

       Ipv4AddressHelper ipv4;
       ipv4.SetBase ("", "");
       Ipv4InterfaceContainer interfaces = ipv4.Assign (devices);

OnOffHelper and PacketSinkHelper is on duty of application layer

  1. OnOffHelper

    It's the things related to application layer. OnOffHelper is used to only "send" application.
    OnOffHelper is just help you to free from the troublesome stuff like socket. But if you would like to send the packet in fine-grained way, socket is necessary. we talk about socket later.

    OnOffHelper you only need to tell it UDP or TCP type, ip address, port, and the sending rate you want to send to. Then you can send the packet to the destination. In reality, the OnOffHelper can help you to setup more parameters(e.g., delay, On and Off interval) but for here, most of them use default parameters. Finally, you can install it on the nodes you want to make them become the senders. Then setup the start and stop time. The sender application is complete, but you still need a receiver to receive the packets.

       OnOffHelper onoff ("ns3::UdpSocketFactory", 
                          Address (InetSocketAddress (interfaces.GetAddress (1), port)));
       onoff.SetConstantRate (DataRate ("500kb/s"));
       ApplicationContainer app = onoff.Install (nodes.Get (0));
       // Start the application
       app.Start (Seconds (1.0));
       app.Stop (Seconds (10.0));
  2. PacketSinkHelper

    It's the destination of the packet. just like OnOffHelper you need to sepcify the parameter, and install it. That's all.

         PacketSinkHelper sink ("ns3::UdpSocketFactory",
                                Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
         app = sink.Install (nodes.Get (1));
         app.Start (Seconds (0.0));

If the OnOffHelper and PacketSinkHelper are to coarse-grained for you to deliver the algorithm, you can try to use the socket to send every packet in the way you want.


For the simulation, the most important part is to inspect the network. So you have to know what the packet delay is, throughput is, packet loss rate is and so on. NS-3 prepares the Trace Tool to help you log the condition of every packet in different media. (But I didn't use it, I wrote the log function by myself(Peek every packet.))

There are two Trace Helper. AsciiTraceHelper is for human readable log. EnablePcapAll is for not human readable.(Pcap is a kind of standard binary file.) They log the packet information in the specified medium. (If you assign csma to EnableAsciiAll, it will log all the packet go through csma this line.)

    AsciiTraceHelper ascii;
    csma.EnableAsciiAll (ascii.CreateFileStream ("csma-one-subnet.tr"));
    csma.EnablePcapAll ("csma-one-subnet", false);

results matching ""

    No results matching ""