packets can't be sent out of CSMA network

82 views
Skip to first unread message

Red zhaoyj

unread,
Mar 14, 2015, 11:12:00 PM3/14/15
to ns-3-...@googlegroups.com
Hi,

I'm a beginner of ns3 and get troubles when using CSMA module.
I try to model a simple network with 5 nodes. 3 of them are in a CSMA network, and  the rest 2 nodes are connected to the 2 nodes in CSMA network respectively, using P2P. The topology looks like this:
 +------------------a1------P2P------b1
 |                    |
a0    CSMA     |
 |                    |
 +---------------- a2------P2P------c1

But I find it impossible to send packets from a0 to b1, from a0 to c1, and from c1 to b1.
What succeeded is that packets can be sent from c1 to a0, and from b1 to a0.
I don't think there is problem in routing tables of the nodes. But I don't know what is wrong.
Anyone could give me a hand? Thx very much in advance!!

My code is below. >_<

#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;

void showRoutingTable(int time)
{
char name[50];
sprintf(name, "scratch/RoutingTable-%d.txt",time);
Ipv4GlobalRoutingHelper().PrintRoutingTableAllAt(Seconds(time), Create<OutputStreamWrapper>(name, std::ios::out));
}

void directUdp(Ptr<Node> sender, Ptr<Node> recver, Ipv4Address recverAddr, double lastTime = -1)
{
const int port = 1314;
UdpEchoServerHelper s(port);
ApplicationContainer sa = s.Install(recver);
sa.Start(Seconds(0.01));

UdpEchoClientHelper c(recverAddr, port);
c.SetAttribute("Interval", TimeValue(Seconds(0.5)));
c.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer ca = c.Install(sender);
ca.Start(Seconds(0.02));

if (lastTime > 0.01)
{
sa.Stop(Seconds(lastTime));
ca.Stop(Seconds(lastTime));
}
}

Ptr<Ipv4StaticRouting> GetRT(Ptr<Node> node) {
static Ipv4StaticRoutingHelper routingHelper;
return routingHelper.GetStaticRouting(node->GetObject<Ipv4>());
}

int GetIface(Ptr<Node> node, int deviceIdx)
{
return node->GetObject<Ipv4>()->GetInterfaceForDevice(node->GetDevice(deviceIdx));
}

int main(int argc, char** argv)
{
CommandLine cmd;
cmd.Parse(argc, argv);

NodeContainer A, B, C;
A.Create(3);
B.Create(1);
C.Create(1);

InternetStackHelper is;
is.Install(A);
is.Install(B);
is.Install(C);

B.Add(A.Get(1));
C.Add(A.Get(2));

Ipv4AddressHelper ipv4;
ipv4.SetBase("1.0.0.0", "255.0.0.0");

CsmaHelper csma;
csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
Ipv4InterfaceContainer a = ipv4.Assign(csma.Install(A)), b, c;

PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", DataRateValue(DataRate("1Gbps")));
p2p.SetDeviceAttribute("Mtu", UintegerValue(1500));
p2p.SetChannelAttribute("Delay", TimeValue(Seconds(0.01)));

ipv4.SetBase("2.0.0.0", "255.0.0.0");
b = ipv4.Assign(p2p.Install(B.Get(1), B.Get(0)));

ipv4.SetBase("3.0.0.0", "255.0.0.0");
c = ipv4.Assign(p2p.Install(C.Get(1), C.Get(0)));

showRoutingTable(0);

GetRT(A.Get(0))->AddNetworkRouteTo("2.0.0.0", "255.0.0.0", 1);
GetRT(A.Get(0))->AddNetworkRouteTo("3.0.0.0", "255.0.0.0", 1);
GetRT(A.Get(2))->AddNetworkRouteTo("2.0.0.0", "255.0.0.0", 1);
GetRT(A.Get(1))->AddNetworkRouteTo("3.0.0.0", "255.0.0.0", 1);
GetRT(C.Get(0))->AddNetworkRouteTo("1.0.0.0", "255.0.0.0", 1);
GetRT(B.Get(0))->AddNetworkRouteTo("3.0.0.0", "255.0.0.0", 1);

std::cout<<A.Get(0)->GetId()<<' '<<B.Get(0)->GetId()<<' '<<b.GetAddress(1)<<std::endl;
std::cout<<A.Get(0)->GetId()<<' '<<A.Get(1)->GetId()<<' '<<b.GetAddress(0)<<std::endl;
// directUdp(A.Get(0), B.Get(0), b.GetAddress(1));
// directUdp(A.Get(0), A.Get(1), a.GetAddress(1));
// directUdp(B.Get(1), B.Get(0), b.GetAddress(1));
// directUdp(A.Get(0), C.Get(0), c.GetAddress(1));
directUdp(C.Get(0), A.Get(0), a.GetAddress(0));
// directUdp(B.Get(0), C.Get(0), c.GetAddress(1));

FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor;
monitor = flowmon.InstallAll ();

Simulator::Run();
Simulator::Destroy();
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();

std::cout << "source destination throughput loss trPacketNumber aveDelay aveJitter" << std::endl;

for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
double throughput =i->second.rxBytes * 8.0 / (10*1024*1024); //Mbps
double loss = i->second.txPackets - i->second.rxPackets; //个
double trPacketNumber =i->second.txPackets; //个     

std::cout <<  t.sourceAddress << " ";
std::cout <<  t.destinationAddress << " ";
std::cout <<  throughput << " ";
std::cout <<  loss << " ";
std::cout <<  trPacketNumber << " ";
std::cout <<  i->second.delaySum.GetSeconds() / i->second.rxPackets << " "; //平均时延
std::cout <<  i->second.jitterSum.GetSeconds() / i->second.rxPackets << "\n"; //平均抖动
}
return 0;
}



Tommaso Pecorella

unread,
Mar 15, 2015, 3:32:50 AM3/15/15
to ns-3-...@googlegroups.com
Hi,

let's see...
I don't think there is problem in routing tables of the nodes
aaaaaand...... it's a routing problem.

First things first tho.
1) You forgot to adda Simulator::Stop(sometime).
2) Use the attachments... it's easier than copy--paste
3) You didn't push into node B and C all the routes (but that wasn't the problem)

However, THE problem is elsewhere.
You used this function to add the routes:
void
Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network,
                                     
Ipv4Mask networkMask,
                                      uint32_t
interface,
                                      uint32_t metric
)
This is ok for the p2p but not for the csma. For csma you need to use this one:
void
Ipv4StaticRouting::AddNetworkRouteTo (Ipv4Address network,
                                     
Ipv4Mask networkMask,
                                     
Ipv4Address nextHop,
                                      uint32_t
interface,
                                      uint32_t metric
)

I.e., the one with he next hop address.

The reason is: a pop link is a "noarp" interface, and it will send packets in the link no matter what (and it will receive them too). A csma link needs to know the address of the next hop. 

Cheers,

T.


Message has been deleted

Red zhaoyj

unread,
Mar 15, 2015, 4:21:49 AM3/15/15
to ns-3-...@googlegroups.com
Hi,

Amazingly it really works! Thx you very much.
Actually I use the 'visualizer' module as well, so I seldom use 'Simulator::Stop(..)'. Sorry for this if it has caused any problems to you. >_<
Next time I will remember to use attachment! I am also new in this groups actually.

Thank you again !

Bests,


Tommaso Pecorella於 2015年3月15日星期日 UTC+8下午3時32分50秒寫道:

Tommaso Pecorella

unread,
Mar 15, 2015, 6:28:06 AM3/15/15
to ns-3-...@googlegroups.com
No problem for the attach / Stop. The stop is a nuisance, an the attach... I think they should double the menu size.

Cheers,

T.
Reply all
Reply to author
Forward
0 new messages