Set TOS field

Topology

N0(UDP Sender)----P2Plink(5Mbps,2ms)---N1(UDP Sink)

 

When the packet sent from N0, the TOS (type of service field in the IP header) will be set.

 

tos-udp.cc (put this file under scratch)

#include <fstream>

#include <string.h>

 

#include "ns3/core-module.h"

#include "ns3/internet-module.h"

#include "ns3/point-to-point-module.h"

#include "ns3/packet-sink.h"

#include "ns3/packet-sink-helper.h"

#include "ns3/on-off-helper.h"

 

using namespace ns3;

 

NS_LOG_COMPONENT_DEFINE ("TOSUDP");

 

 

int

main (int argc, char *argv[])

{

  NS_LOG_INFO ("Create nodes.");

  NodeContainer n;

  n.Create (2);

 

  InternetStackHelper internet;

  internet.Install (n);

 

  NS_LOG_INFO ("Create channels.");

 

  PointToPointHelper pointToPoint;

  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  pointToPoint.SetDeviceAttribute("Mtu", UintegerValue(1400));  

  NetDeviceContainer p2pDevices;

  p2pDevices = pointToPoint.Install (n);    

 

  Ipv4AddressHelper ipv4;

 

  NS_LOG_INFO ("Assign IP Addresses.");

  ipv4.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer i = ipv4.Assign (p2pDevices);

 

  NS_LOG_INFO ("Create Applications.");

 

  PacketSinkHelper sinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));

  ApplicationContainer sinkApp = sinkHelper.Install (n.Get(1));

 

  InetSocketAddress sinkSocket (i.GetAddress (1), 9);

  //std::vector<uint8_t> tosValues = {0x70, 0x28, 0xb8, 0xc0}; //AC_BE, AC_BK, AC_VI, AC_VO

  sinkSocket.SetTos (0xc0);

  OnOffHelper server ("ns3::UdpSocketFactory", sinkSocket);

  server.SetAttribute ("PacketSize", UintegerValue (172)); // G.711 encoding, 20ms delay, 160bytes +12bytes(rtp)

  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));

  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

  server.SetAttribute ("DataRate", DataRateValue (DataRate ("64Kbps")));

  ApplicationContainer serverApp = server.Install (n.Get(0));

 

  sinkApp.Start (Seconds (0.0));

  serverApp.Start (Seconds (1.0));

 

  Simulator::Stop (Seconds (12.0));

  pointToPoint.EnablePcapAll("tos-udp");

  NS_LOG_INFO ("Run Simulation.");

  Simulator::Run ();

  Simulator::Destroy ();

  NS_LOG_INFO ("Done.");

 

}

 

Execution

After simulation, you will get tos-udp-0-1.pcap and tos-udp-1-0.pcap.

 

 

Back to NS3 Learning Guide

Last Modified: 2022/2/6 done

 

[Author]

Dr. Chih-Heng Ke

Department of Computer Science and Information Engineering, National Quemoy University, Kinmen, Taiwan

Email: smallko@gmail.com