|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2010 Hajime Tazaki |
| 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 |
* Author: John Abraham <john.abraham@gatech.edu> |
| 19 |
* Adapted from: ipv4-raw-test.cc |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ns3/test.h" |
| 23 |
#include "ns3/socket-factory.h" |
| 24 |
#include "ns3/ipv4-raw-socket-factory.h" |
| 25 |
#include "ns3/simulator.h" |
| 26 |
#include "ns3/simple-channel.h" |
| 27 |
#include "ns3/simple-net-device.h" |
| 28 |
#include "ns3/drop-tail-queue.h" |
| 29 |
#include "ns3/socket.h" |
| 30 |
|
| 31 |
#include "ns3/log.h" |
| 32 |
#include "ns3/node.h" |
| 33 |
#include "ns3/inet-socket-address.h" |
| 34 |
#include "ns3/boolean.h" |
| 35 |
|
| 36 |
#include "ns3/arp-l3-protocol.h" |
| 37 |
#include "ns3/ipv4-l3-protocol.h" |
| 38 |
#include "ns3/icmpv4-l4-protocol.h" |
| 39 |
#include "ns3/ipv4-list-routing.h" |
| 40 |
#include "ns3/ipv4-static-routing.h" |
| 41 |
|
| 42 |
#include <string> |
| 43 |
#include <sstream> |
| 44 |
#include <limits> |
| 45 |
#include <netinet/in.h> |
| 46 |
#include <sys/socket.h> |
| 47 |
#include <sys/types.h> |
| 48 |
namespace ns3 { |
| 49 |
|
| 50 |
static void |
| 51 |
AddInternetStack (Ptr<Node> node) |
| 52 |
{ |
| 53 |
//ARP |
| 54 |
Ptr<ArpL3Protocol> arp = CreateObject<ArpL3Protocol> (); |
| 55 |
node->AggregateObject (arp); |
| 56 |
//IPV4 |
| 57 |
Ptr<Ipv4L3Protocol> ipv4 = CreateObject<Ipv4L3Protocol> (); |
| 58 |
//Routing for Ipv4 |
| 59 |
Ptr<Ipv4ListRouting> ipv4Routing = CreateObject<Ipv4ListRouting> (); |
| 60 |
ipv4->SetRoutingProtocol (ipv4Routing); |
| 61 |
Ptr<Ipv4StaticRouting> ipv4staticRouting = CreateObject<Ipv4StaticRouting> (); |
| 62 |
ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0); |
| 63 |
node->AggregateObject (ipv4); |
| 64 |
//ICMP |
| 65 |
Ptr<Icmpv4L4Protocol> icmp = CreateObject<Icmpv4L4Protocol> (); |
| 66 |
node->AggregateObject (icmp); |
| 67 |
// //Ipv4Raw |
| 68 |
// Ptr<Ipv4UdpL4Protocol> udp = CreateObject<UdpL4Protocol> (); |
| 69 |
// node->AggregateObject(udp); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
class Ipv4HeaderTest : public TestCase |
| 74 |
{ |
| 75 |
Ptr<Packet> m_receivedPacket; |
| 76 |
Ipv4Header m_receivedHeader; |
| 77 |
void DoSendData_IpHdr_DSCP (Ptr<Socket> socket, std::string to, Ipv4Header::DSCPType dscp,Ipv4Header::ECNType); |
| 78 |
void SendData_IpHdr_DSCP (Ptr<Socket> socket, std::string to, Ipv4Header::DSCPType dscp, Ipv4Header::ECNType); |
| 79 |
|
| 80 |
public: |
| 81 |
virtual void DoRun (void); |
| 82 |
Ipv4HeaderTest (); |
| 83 |
|
| 84 |
void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from); |
| 85 |
void ReceivePkt (Ptr<Socket> socket); |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
Ipv4HeaderTest::Ipv4HeaderTest () |
| 90 |
: TestCase ("IPv4 Header Test") |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
void Ipv4HeaderTest::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &from) |
| 95 |
{ |
| 96 |
m_receivedPacket = packet; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
void Ipv4HeaderTest::ReceivePkt (Ptr<Socket> socket) |
| 101 |
{ |
| 102 |
uint32_t availableData; |
| 103 |
availableData = socket->GetRxAvailable (); |
| 104 |
m_receivedPacket = socket->Recv (2, MSG_PEEK); |
| 105 |
NS_ASSERT (m_receivedPacket->GetSize () == 2); |
| 106 |
m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0); |
| 107 |
NS_ASSERT (availableData == m_receivedPacket->GetSize ()); |
| 108 |
//cast availableData to void, to suppress 'availableData' set but not used |
| 109 |
//compiler warning |
| 110 |
(void) availableData; |
| 111 |
m_receivedPacket->PeekHeader (m_receivedHeader); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void |
| 117 |
Ipv4HeaderTest::DoSendData_IpHdr_DSCP (Ptr<Socket> socket, std::string to, Ipv4Header::DSCPType dscp, Ipv4Header::ECNType ecn) |
| 118 |
{ |
| 119 |
Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 0); |
| 120 |
socket->SetAttribute ("IpHeaderInclude", BooleanValue (true)); |
| 121 |
Ptr<Packet> p = Create<Packet> (123); |
| 122 |
Ipv4Header ipHeader; |
| 123 |
ipHeader.SetSource (Ipv4Address ("10.0.0.2")); |
| 124 |
ipHeader.SetDestination (Ipv4Address (to.c_str ())); |
| 125 |
ipHeader.SetProtocol (0); |
| 126 |
ipHeader.SetPayloadSize (p->GetSize ()); |
| 127 |
ipHeader.SetTtl (255); |
| 128 |
ipHeader.SetDSCP (dscp); |
| 129 |
ipHeader.SetECN (ecn); |
| 130 |
p->AddHeader (ipHeader); |
| 131 |
|
| 132 |
NS_TEST_EXPECT_MSG_EQ (socket->SendTo (p, 0, realTo), |
| 133 |
143, to); |
| 134 |
socket->SetAttribute ("IpHeaderInclude", BooleanValue (false)); |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
Ipv4HeaderTest::SendData_IpHdr_DSCP (Ptr<Socket> socket, std::string to, Ipv4Header::DSCPType dscp, Ipv4Header::ECNType ecn) |
| 139 |
{ |
| 140 |
m_receivedPacket = Create<Packet> (); |
| 141 |
Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0), |
| 142 |
&Ipv4HeaderTest::DoSendData_IpHdr_DSCP, this, socket, to, dscp, ecn); |
| 143 |
Simulator::Run (); |
| 144 |
} |
| 145 |
|
| 146 |
void |
| 147 |
Ipv4HeaderTest::DoRun (void) |
| 148 |
{ |
| 149 |
// Create topology |
| 150 |
|
| 151 |
// Receiver Node |
| 152 |
Ptr<Node> rxNode = CreateObject<Node> (); |
| 153 |
AddInternetStack (rxNode); |
| 154 |
Ptr<SimpleNetDevice> rxDev1, rxDev2; |
| 155 |
{ // first interface |
| 156 |
rxDev1 = CreateObject<SimpleNetDevice> (); |
| 157 |
rxDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
| 158 |
rxNode->AddDevice (rxDev1); |
| 159 |
Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> (); |
| 160 |
uint32_t netdev_idx = ipv4->AddInterface (rxDev1); |
| 161 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U)); |
| 162 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
| 163 |
ipv4->SetUp (netdev_idx); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
// Sender Node |
| 168 |
Ptr<Node> txNode = CreateObject<Node> (); |
| 169 |
AddInternetStack (txNode); |
| 170 |
Ptr<SimpleNetDevice> txDev1; |
| 171 |
{ |
| 172 |
txDev1 = CreateObject<SimpleNetDevice> (); |
| 173 |
txDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ())); |
| 174 |
txNode->AddDevice (txDev1); |
| 175 |
Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> (); |
| 176 |
uint32_t netdev_idx = ipv4->AddInterface (txDev1); |
| 177 |
Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U)); |
| 178 |
ipv4->AddAddress (netdev_idx, ipv4Addr); |
| 179 |
ipv4->SetUp (netdev_idx); |
| 180 |
} |
| 181 |
|
| 182 |
// link the two nodes |
| 183 |
Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> (); |
| 184 |
rxDev1->SetChannel (channel1); |
| 185 |
txDev1->SetChannel (channel1); |
| 186 |
|
| 187 |
|
| 188 |
// Create the IPv4 Raw sockets |
| 189 |
Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv4RawSocketFactory> (); |
| 190 |
Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket (); |
| 191 |
NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("0.0.0.0"), 0)), 0, "trivial"); |
| 192 |
rxSocket->SetRecvCallback (MakeCallback (&Ipv4HeaderTest::ReceivePkt, this)); |
| 193 |
|
| 194 |
|
| 195 |
Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv4RawSocketFactory> (); |
| 196 |
Ptr<Socket> txSocket = txSocketFactory->CreateSocket (); |
| 197 |
|
| 198 |
// ------ Now the tests ------------ |
| 199 |
|
| 200 |
// DSCP Tests |
| 201 |
std::cout << "DSCP Test\n"; |
| 202 |
|
| 203 |
std::vector <Ipv4Header::DSCPType> vDSCPTypes; |
| 204 |
vDSCPTypes.push_back (Ipv4Header::DSCPDefault); |
| 205 |
vDSCPTypes.push_back (Ipv4Header::CS1); |
| 206 |
vDSCPTypes.push_back (Ipv4Header::AF11); |
| 207 |
vDSCPTypes.push_back (Ipv4Header::AF12); |
| 208 |
vDSCPTypes.push_back (Ipv4Header::AF13); |
| 209 |
vDSCPTypes.push_back (Ipv4Header::CS2); |
| 210 |
vDSCPTypes.push_back (Ipv4Header::AF21); |
| 211 |
vDSCPTypes.push_back (Ipv4Header::AF22); |
| 212 |
vDSCPTypes.push_back (Ipv4Header::AF23); |
| 213 |
vDSCPTypes.push_back (Ipv4Header::CS3); |
| 214 |
vDSCPTypes.push_back (Ipv4Header::AF31); |
| 215 |
vDSCPTypes.push_back (Ipv4Header::AF32); |
| 216 |
vDSCPTypes.push_back (Ipv4Header::AF33); |
| 217 |
vDSCPTypes.push_back (Ipv4Header::CS4); |
| 218 |
vDSCPTypes.push_back (Ipv4Header::AF41); |
| 219 |
vDSCPTypes.push_back (Ipv4Header::AF42); |
| 220 |
vDSCPTypes.push_back (Ipv4Header::AF43); |
| 221 |
vDSCPTypes.push_back (Ipv4Header::CS5); |
| 222 |
vDSCPTypes.push_back (Ipv4Header::EF); |
| 223 |
vDSCPTypes.push_back (Ipv4Header::CS6); |
| 224 |
vDSCPTypes.push_back (Ipv4Header::CS7); |
| 225 |
|
| 226 |
for (uint32_t i = 0; i < vDSCPTypes.size (); i++) |
| 227 |
{ |
| 228 |
SendData_IpHdr_DSCP (txSocket, "10.0.0.1", vDSCPTypes [i], Ipv4Header::ECT1); |
| 229 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1"); |
| 230 |
NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetDSCP (), vDSCPTypes [i], "recv(hdrincl): 10.0.0.1"); |
| 231 |
m_receivedHeader.Print (std::cout); |
| 232 |
std::cout << std::endl; |
| 233 |
m_receivedPacket->RemoveAllByteTags (); |
| 234 |
m_receivedPacket = 0; |
| 235 |
} |
| 236 |
|
| 237 |
// ECN tests |
| 238 |
std::cout << "ECN Test\n"; |
| 239 |
std::vector <Ipv4Header::ECNType> vECNTypes; |
| 240 |
vECNTypes.push_back (Ipv4Header::NotECT); |
| 241 |
vECNTypes.push_back (Ipv4Header::ECT1); |
| 242 |
vECNTypes.push_back (Ipv4Header::ECT0); |
| 243 |
vECNTypes.push_back (Ipv4Header::CE); |
| 244 |
|
| 245 |
for (uint32_t i = 0; i < vECNTypes.size (); i++) |
| 246 |
{ |
| 247 |
SendData_IpHdr_DSCP (txSocket, "10.0.0.1", Ipv4Header::DSCPDefault, vECNTypes [i]); |
| 248 |
NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 143, "recv(hdrincl): 10.0.0.1"); |
| 249 |
NS_TEST_EXPECT_MSG_EQ (m_receivedHeader.GetECN (), vECNTypes [i], "recv(hdrincl): 10.0.0.1"); |
| 250 |
m_receivedHeader.Print (std::cout); |
| 251 |
std::cout << std::endl; |
| 252 |
m_receivedPacket->RemoveAllByteTags (); |
| 253 |
m_receivedPacket = 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
Simulator::Destroy (); |
| 259 |
} |
| 260 |
//----------------------------------------------------------------------------- |
| 261 |
class Ipv4HeaderTestSuite : public TestSuite |
| 262 |
{ |
| 263 |
public: |
| 264 |
Ipv4HeaderTestSuite () : TestSuite ("ipv4-header", UNIT) |
| 265 |
{ |
| 266 |
AddTestCase (new Ipv4HeaderTest); |
| 267 |
} |
| 268 |
} g_ipv4HeaderTestSuite; |
| 269 |
|
| 270 |
} // namespace ns3 |