PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/s2016/ns3-routing/ns-3/src/test/ns3tcp/ns3tcp-interop-test-suite.cc

https://gitlab.com/pmaddi/cs433
C++ | 316 lines | 145 code | 38 blank | 133 comment | 6 complexity | 034203f9d8ed6a9fefc9bd6bbfcae240 MD5 | raw file
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2009 University of Washington
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation;
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "ns3/log.h"
  19. #include "ns3/abort.h"
  20. #include "ns3/test.h"
  21. #include "ns3/pcap-file.h"
  22. #include "ns3/config.h"
  23. #include "ns3/string.h"
  24. #include "ns3/uinteger.h"
  25. #include "ns3/inet-socket-address.h"
  26. #include "ns3/point-to-point-helper.h"
  27. #include "ns3/internet-stack-helper.h"
  28. #include "ns3/ipv4-address-helper.h"
  29. #include "ns3/ipv4-header.h"
  30. #include "ns3/packet-sink-helper.h"
  31. #include "ns3/on-off-helper.h"
  32. #include "ns3/simulator.h"
  33. using namespace ns3;
  34. NS_LOG_COMPONENT_DEFINE ("Ns3TcpInteropTest");
  35. const bool WRITE_VECTORS = false; // set to true to write response vectors
  36. const uint32_t PCAP_LINK_TYPE = 1187373553; // Some large random number -- we use to verify data was written by this program
  37. const uint32_t PCAP_SNAPLEN = 64; // Don't bother to save much data
  38. // ===========================================================================
  39. // This is a simple test to demonstrate how a known good model (a reference
  40. // implementation) may be used to test another model in a relatively simple
  41. // way.
  42. //
  43. // Node zero contains the model under test, in this case the ns-3 TCP
  44. // implementation. Node one contains the reference implementation that we
  45. // assume will generate good test vectors for us. In this case, a Linux
  46. // TCP implementation is used to stimulate the ns-3 TCP model with what we
  47. // assume are perfectly good packets. We watch the ns-3 implementation to
  48. // see what it does in the presence of these assumed good stimuli.
  49. //
  50. // The test is arranged as a typical ns-3 script, but we use the trace system
  51. // to peek into the running system and monitor the ns-3 TCP.
  52. //
  53. // The topology is just two nodes communicating over a point-to-point network.
  54. // The point-to-point network is chosen because it is simple and allows us to
  55. // easily generate pcap traces we can use to separately verify that the ns-3
  56. // implementation is responding correctly. Once the opration is verified, we
  57. // capture a set of response vectors that are then checked in the test to
  58. // ensure that the ns-3 TCP continues to respond correctly over time.
  59. //
  60. // node 0 node 1
  61. // +----------------+ +----------------+
  62. // | ns-3 TCP | | Linux TCP |
  63. // +----------------+ +----------------+
  64. // | 10.1.1.1 | | 10.1.1.2 |
  65. // +----------------+ +----------------+
  66. // | point-to-point | | point-to-point |
  67. // +----------------+ +----------------+
  68. // | |
  69. // +---------------------+
  70. // 5 Mbps, 2 ms
  71. //
  72. // ===========================================================================
  73. class Ns3TcpInteroperabilityTestCase : public TestCase
  74. {
  75. public:
  76. Ns3TcpInteroperabilityTestCase ();
  77. virtual ~Ns3TcpInteroperabilityTestCase ();
  78. private:
  79. virtual void DoSetup (void);
  80. virtual bool DoRun (void);
  81. virtual void DoTeardown (void);
  82. void Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
  83. std::string m_pcapFilename;
  84. PcapFile m_pcapFile;
  85. bool m_writeVectors;
  86. };
  87. Ns3TcpInteroperabilityTestCase::Ns3TcpInteroperabilityTestCase ()
  88. : TestCase ("Check to see that the ns-3 TCP can work with liblinux2.6.26.so"), m_writeVectors(WRITE_VECTORS)
  89. {
  90. }
  91. Ns3TcpInteroperabilityTestCase::~Ns3TcpInteroperabilityTestCase ()
  92. {
  93. }
  94. void
  95. Ns3TcpInteroperabilityTestCase::DoSetup (void)
  96. {
  97. //
  98. // We expect there to be a file called tcp-interop-response-vectors.pcap" in
  99. // the source directory of this file.
  100. //
  101. m_pcapFilename = NS_TEST_SOURCEDIR + "ns3tcp-interop-response-vectors.pcap";
  102. if (m_writeVectors)
  103. {
  104. m_pcapFile.Open (m_pcapFilename, std::ios::out|std::ios::binary);
  105. m_pcapFile.Init(PCAP_LINK_TYPE, PCAP_SNAPLEN);
  106. }
  107. else
  108. {
  109. m_pcapFile.Open (m_pcapFilename, std::ios::in|std::ios::binary);
  110. NS_ABORT_MSG_UNLESS (m_pcapFile.GetDataLinkType () == PCAP_LINK_TYPE, "Wrong response vectors in directory");
  111. }
  112. }
  113. void
  114. Ns3TcpInteroperabilityTestCase::DoTeardown (void)
  115. {
  116. m_pcapFile.Close ();
  117. }
  118. void
  119. Ns3TcpInteroperabilityTestCase::Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface)
  120. {
  121. //
  122. // We're not testing IP so remove and toss the header. In order to do this,
  123. // though, we need to copy the packet since we have a const version.
  124. //
  125. Ptr<Packet> p = packet->Copy ();
  126. Ipv4Header ipHeader;
  127. p->RemoveHeader (ipHeader);
  128. //
  129. // What is left is the TCP header and any data that may be sent. We aren't
  130. // sending any TCP data, so we expect what remains is only TCP header, which
  131. // is a small thing to save.
  132. //
  133. if (m_writeVectors)
  134. {
  135. //
  136. // Save the TCP under test response for later testing.
  137. //
  138. Time tNow = Simulator::Now ();
  139. int64_t tMicroSeconds = tNow.GetMicroSeconds ();
  140. uint32_t size = p->GetSize ();
  141. uint8_t *buf = new uint8_t[size];
  142. p->CopyData (buf, size);
  143. m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000),
  144. uint32_t (tMicroSeconds % 1000000),
  145. buf,
  146. size);
  147. delete [] buf;
  148. }
  149. else
  150. {
  151. //
  152. // Read the TCP under test expected response from the expected vector
  153. // file and see if it still does the right thing.
  154. //
  155. uint8_t expected[PCAP_SNAPLEN];
  156. uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
  157. m_pcapFile.Read (expected, sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen);
  158. uint8_t *actual = new uint8_t[readLen];
  159. p->CopyData (actual, readLen);
  160. uint32_t result = memcmp(actual, expected, readLen);
  161. delete [] actual;
  162. //
  163. // Avoid streams of errors -- only report the first.
  164. //
  165. if (GetErrorStatus () == false)
  166. {
  167. NS_TEST_EXPECT_MSG_EQ (result, 0, "Expected data comparison error");
  168. }
  169. }
  170. }
  171. bool
  172. Ns3TcpInteroperabilityTestCase::DoRun (void)
  173. {
  174. //
  175. // Just create two nodes. One (node zero) will be the node with the TCP
  176. // under test which is the ns-3 TCP implementation. The other node (node
  177. // one) will be the node with the reference implementation we use to drive
  178. // the tests.
  179. //
  180. NodeContainer nodes;
  181. nodes.Create (2);
  182. //
  183. // For this test we'll use a point-to-point net device. It's not as simple
  184. // as a simple-net-device, but it provides nice places to hook trace events
  185. // so we can see what's moving between our nodes.
  186. //
  187. PointToPointHelper pointToPoint;
  188. pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  189. pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
  190. //
  191. // Install the point-to-point devices on both nodes and connec them up.
  192. //
  193. NetDeviceContainer devices;
  194. devices = pointToPoint.Install (nodes);
  195. //
  196. // Install two variants of the internet stack. The first, on node zero
  197. // uses the TCP under test, which is the default ns-3 TCP implementation.
  198. //
  199. InternetStackHelper stack;
  200. stack.Install (nodes.Get (0));
  201. //
  202. // The other node, node one, is going to be set up to use a Linux TCP
  203. // implementation that we consider a known good TCP.
  204. //
  205. std::string nscStack = "liblinux2.6.26.so";
  206. stack.SetTcp ("ns3::NscTcpL4Protocol", "Library", StringValue("liblinux2.6.26.so"));
  207. stack.Install (nodes.Get (1));
  208. //
  209. // Assign the address 10.1.1.1 to the TCP implementation under test (index
  210. // zero) and 10.1.1.2 to the reference implementation (index one).
  211. //
  212. Ipv4AddressHelper address;
  213. address.SetBase ("10.1.1.0", "255.255.255.252");
  214. Ipv4InterfaceContainer interfaces = address.Assign (devices);
  215. //
  216. // We need a place for the TCP data to go on the node with the TCP under
  217. // test, so just create a sink on node zero.
  218. //
  219. uint16_t sinkPort = 8080;
  220. Address sinkAddress (InetSocketAddress(interfaces.GetAddress (0), sinkPort));
  221. PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  222. ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (0));
  223. sinkApps.Start (Seconds (0.));
  224. //
  225. // We need something to shove data down the pipe, so we create an on-off
  226. // application on the soure node with the reference TCP implementation.
  227. // The default behavior is to send for one second, then go quiet for one
  228. // second, and repeat.
  229. //
  230. OnOffHelper onOffHelper ("ns3::TcpSocketFactory", sinkAddress);
  231. onOffHelper.SetAttribute ("MaxBytes", UintegerValue (100000));
  232. ApplicationContainer sourceApps = onOffHelper.Install(nodes.Get(1));
  233. sourceApps.Start (Seconds (1.));
  234. sourceApps.Stop (Seconds (10.));
  235. //
  236. // There are currently a limited number of trace hooks in the ns-3 TCP code.
  237. // Rather than editing TCP to insert a bunch of trace hooks, we can just
  238. // intercept the packets at the IPv4 layer. See internet-stack-helper.cc
  239. // for complete description of the trace hooks. We're interested in the
  240. // responses of the TCP under test, which implies we need to hook the node
  241. // zero Ipv4 layer three transmit trace source. We'll then get all of the
  242. // responses we need
  243. //
  244. Config::Connect ("/NodeList/0/$ns3::Ipv4L3Protocol/Tx",
  245. MakeCallback (&Ns3TcpInteroperabilityTestCase::Ipv4L3Tx, this));
  246. //
  247. // The idea here is that someone will look very closely at the all of the
  248. // communications between the reference TCP and the TCP under test in this
  249. // simulation and determine that all of the responses are correct. We expect
  250. // that this means generating a pcap trace file from the point-to-point link
  251. // and examining the packets closely using tcpdump, wireshark or some such
  252. // program. So we provide the ability to generate a pcap trace of the
  253. // test execution for your perusal.
  254. //
  255. // Once the validation test is determined to be running exactly as exptected,
  256. // we allow you to generate a file that contains the response vectors that
  257. // will be checked during the actual execution of the test.
  258. //
  259. if (m_writeVectors)
  260. {
  261. pointToPoint.EnablePcapAll ("tcp-interop");
  262. }
  263. Simulator::Stop (Seconds(20));
  264. Simulator::Run ();
  265. Simulator::Destroy ();
  266. return GetErrorStatus ();
  267. }
  268. class Ns3TcpInteroperabilityTestSuite : public TestSuite
  269. {
  270. public:
  271. Ns3TcpInteroperabilityTestSuite ();
  272. };
  273. Ns3TcpInteroperabilityTestSuite::Ns3TcpInteroperabilityTestSuite ()
  274. : TestSuite ("ns3-tcp-interoperability", SYSTEM)
  275. {
  276. AddTestCase (new Ns3TcpInteroperabilityTestCase);
  277. }
  278. Ns3TcpInteroperabilityTestSuite ns3TcpInteroperabilityTestSuite;