c++networkingtcpns-3

TCP packet drop (ns3)


I am new to ns3 network simulator and wanted to know how to get the number of packet drops in a TCP connection. I know of the following command:

devices.Get (1)->TraceConnectWithoutContext ("PhyRxDrop", MakeBoundCallback (&RxDrop, stream));

But this is helpful only for a single TCP connection over a p2p link. In my topology, there is a single p2p connection but 2 applications using TCP over that same p2p link and I would like to know individually for each TCP connection the number of dropped packets. I have researched online quite a lot but was unable to find any resources. Kindly point to some resources or give the class name which I can use to detect TCP connection-specific packet losses.

The above command as of now combines the packet losses for both the connections and outputs them to the stream because they are over the same p2p link.


Solution

  • The usage of RxDrop tells me you're using referring to fourth.cc in the ns-3 tutorial. Connecting to the PhyRxDrop TraceSource will result in the requested CallBack being invoked for each dropped packet. ns-3 doesn't have a packet filter such that the CallBack would only be invoked for some packets.

    However, you can determine which connection a packet corresponds to. Simply strip the packet headers, and inspect the port numbers. Remember every connection is defined by a unique 4-tuple: (host IP, host port, destination IP, destination port).

    static void
    RxDrop(Ptr<const Packet> packet) {
        /* 
         * Need to copy packet since headers need to be removed
         * to be inspected. Alternatively, remove the headers,
         * and add them back.
         */
        Ptr<Packet> copy = packet->Copy();
    
        // Headers must be removed in the order they're present.
        PppHeader pppHeader;
        copy->RemoveHeader(pppHeader);
        Ipv4Header ipHeader;
        copy->RemoveHeader(ipHeader);
        TcpHeader tcpHeader;
        copy->RemoveHeader(tcpHeader);
    
        std::cout << "Source IP: ";
        ipHeader.GetSource().Print(std::cout);
        std::cout << std::endl;
        std::cout << "Source Port: " << tcpHeader.GetSourcePort() << std::endl;
        std::cout << "Destination IP: ";
        ipHeader.GetDestination().Print(std::cout);
        std::cout << std::endl;
        std::cout << "Destination Port: " << tcpHeader.GetDestinationPort() << std::endl;
    }