examples/flowmon.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu May 07 18:17:17 2009 +0100 (2009-05-07)
changeset 3972 4c6cb7db6d87
parent 3964 efdf773a79d0
child 3975 9e833fd91f0d
permissions -rw-r--r--
Add a readme file explaining the purpose of this branch
gjc@3927
     1
# -*-  Mode: Python; -*-
gjc@3927
     2
#  Copyright (c) 2009 INESC Porto
gjc@3927
     3
# 
gjc@3927
     4
#  This program is free software; you can redistribute it and/or modify
gjc@3927
     5
#  it under the terms of the GNU General Public License version 2 as
gjc@3927
     6
#  published by the Free Software Foundation;
gjc@3927
     7
# 
gjc@3927
     8
#  This program is distributed in the hope that it will be useful,
gjc@3927
     9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
gjc@3927
    10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
gjc@3927
    11
#  GNU General Public License for more details.
gjc@3927
    12
# 
gjc@3927
    13
#  You should have received a copy of the GNU General Public License
gjc@3927
    14
#  along with this program; if not, write to the Free Software
gjc@3927
    15
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
gjc@3927
    16
# 
gjc@3927
    17
#  Authors: Gustavo Carneiro <gjc@inescporto.pt>
gjc@3927
    18
gjc@3927
    19
import sys
gjc@3927
    20
import ns3
gjc@3927
    21
gjc@3927
    22
DISTANCE = 150 # (m)
gjc@3927
    23
NUM_NODES_SIDE = 3
gjc@3927
    24
gjc@3927
    25
def main(argv):
gjc@3927
    26
gjc@3938
    27
    cmd = ns3.CommandLine()
gjc@3938
    28
gjc@3938
    29
    cmd.Viz = None
gjc@3938
    30
    cmd.AddValue("Viz", "Enable visualizer")
gjc@3938
    31
gjc@3940
    32
    cmd.NumNodesSide = None
gjc@3940
    33
    cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
gjc@3940
    34
gjc@3938
    35
    cmd.Parse(argv)
gjc@3938
    36
gjc@3927
    37
    ns3.RandomVariable.UseGlobalSeed(1, 1, 2, 3, 5, 8)
gjc@3927
    38
gjc@3927
    39
    #ns3.Config.SetDefault("ns3::Ipv4L3Protocol::CalcChecksum", ns3.BooleanValue(True))
gjc@3927
    40
gjc@3927
    41
    channel = ns3.WifiChannel()
gjc@3927
    42
    channel.SetPropagationDelayModel(ns3.ConstantSpeedPropagationDelayModel())
gjc@3927
    43
    log = ns3.LogDistancePropagationLossModel()
gjc@3927
    44
    log.SetReferenceModel(ns3.FriisPropagationLossModel())
gjc@3927
    45
    channel.SetPropagationLossModel(log)
gjc@3927
    46
gjc@3927
    47
    wifi = ns3.WifiHelper()
gjc@3927
    48
    wifi.SetMac("ns3::AdhocWifiMac")
gjc@3927
    49
    wifi.SetPhy("ns3::WifiPhy");
gjc@3927
    50
    wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
gjc@3927
    51
gjc@3927
    52
    internet = ns3.InternetStackHelper()
gjc@3927
    53
    ipv4Addresses = ns3.Ipv4AddressHelper()
gjc@3927
    54
    ipv4Addresses.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.255"))
gjc@3927
    55
    
gjc@3927
    56
    olsrHelper = ns3.OlsrHelper()
gjc@3927
    57
gjc@3927
    58
    port = 9   # Discard port(RFC 863)
gjc@3927
    59
    onOffHelper = ns3.OnOffHelper("ns3::UdpSocketFactory",
gjc@3927
    60
                                  ns3.Address(ns3.InetSocketAddress(ns3.Ipv4Address("10.0.0.1"), port)))
gjc@3935
    61
    onOffHelper.SetAttribute("DataRate", ns3.DataRateValue(ns3.DataRate("100kbps")))
gjc@3927
    62
    onOffHelper.SetAttribute("OnTime", ns3.RandomVariableValue(ns3.ConstantVariable(1)))
gjc@3927
    63
    onOffHelper.SetAttribute("OffTime", ns3.RandomVariableValue(ns3.ConstantVariable(0)))
gjc@3927
    64
gjc@3928
    65
    addresses = []
gjc@3928
    66
    nodes = []
gjc@3940
    67
gjc@3940
    68
    if cmd.NumNodesSide is None:
gjc@3940
    69
        num_nodes_side = NUM_NODES_SIDE
gjc@3940
    70
    else:
gjc@3940
    71
        num_nodes_side = int(cmd.NumNodesSide)
gjc@3940
    72
gjc@3940
    73
    for xi in range(num_nodes_side):
gjc@3940
    74
        for yi in range(num_nodes_side):
gjc@3927
    75
gjc@3927
    76
            node = ns3.Node()
gjc@3928
    77
            nodes.append(node)
gjc@3928
    78
gjc@3927
    79
            internet.Install(ns3.NodeContainer(node))
gjc@3927
    80
gjc@3927
    81
            mobility = ns3.StaticMobilityModel()
gjc@3927
    82
            mobility.SetPosition(ns3.Vector(xi*DISTANCE, yi*DISTANCE, 0))
gjc@3927
    83
            node.AggregateObject(mobility)
gjc@3927
    84
            
gjc@3927
    85
            devices = wifi.Install(ns3.NodeContainer(node), channel)
gjc@3928
    86
            ipv4_interfaces = ipv4Addresses.Assign(devices)
gjc@3928
    87
            addresses.append(ipv4_interfaces.GetAddress(0))
gjc@3927
    88
            
gjc@3927
    89
            olsrHelper.Install(ns3.NodeContainer(node))
gjc@3927
    90
gjc@3928
    91
    for i, node in enumerate(nodes):
gjc@3928
    92
        destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
gjc@3934
    93
        #print i, destaddr
gjc@3928
    94
        onOffHelper.SetAttribute("Remote", ns3.AddressValue(ns3.InetSocketAddress(destaddr, port)))
gjc@3928
    95
        app = onOffHelper.Install(ns3.NodeContainer(node))
gjc@3964
    96
        app.Start(ns3.Seconds(ns3.UniformVariable(20, 30).GetValue()))
gjc@3927
    97
            
gjc@3928
    98
    #internet.EnablePcapAll("wifi-olsr")
gjc@3934
    99
    flowmon_helper = ns3.FlowMonitorHelper()
gjc@3942
   100
    #flowmon_helper.SetMonitorAttribute("StartTime", ns3.TimeValue(ns3.Seconds(31)))
gjc@3934
   101
    monitor = flowmon_helper.InstallAll()
pedro@3956
   102
    monitor.SetAttribute("DelayBinWidth", ns3.DoubleValue(0.001))
pedro@3956
   103
    monitor.SetAttribute("JitterBinWidth", ns3.DoubleValue(0.001))
pedro@3956
   104
    monitor.SetAttribute("PacketSizeBinWidth", ns3.DoubleValue(20))
gjc@3927
   105
gjc@3927
   106
    ns3.Simulator.Stop(ns3.Seconds(44.0))
gjc@3938
   107
    if cmd.Viz is not None:
gjc@3938
   108
        import visualizer
gjc@3935
   109
        visualizer.start()
gjc@3935
   110
    else:
gjc@3935
   111
        ns3.Simulator.Run()
gjc@3934
   112
gjc@3934
   113
    def print_stats(os, st):
gjc@3934
   114
        print >> os, "  Tx Bytes: ", st.txBytes
gjc@3934
   115
        print >> os, "  Rx Bytes: ", st.rxBytes
gjc@3934
   116
        print >> os, "  Tx Packets: ", st.txPackets
gjc@3934
   117
        print >> os, "  Rx Packets: ", st.rxPackets
gjc@3934
   118
        print >> os, "  Lost Packets: ", st.lostPackets
gjc@3934
   119
        if st.rxPackets > 0:
gjc@3935
   120
            print >> os, "  Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets)
pedro@3961
   121
	    print >> os, "  Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1))
gjc@3935
   122
            print >> os, "  Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1
gjc@3963
   123
gjc@3969
   124
        if 1:
gjc@3960
   125
            print >> os, "Delay Histogram"
gjc@3969
   126
            for i in range(st.delayHistogram.GetNBins () ):
gjc@3963
   127
              print >> os, " ",i,"(", st.delayHistogram.GetBinStart (i), "-", \
gjc@3960
   128
                  st.delayHistogram.GetBinEnd (i), "): ", st.delayHistogram.GetBinCount (i)
gjc@3963
   129
            print >> os, "Jitter Histogram"
gjc@3969
   130
            for i in range(st.jitterHistogram.GetNBins () ):
gjc@3963
   131
              print >> os, " ",i,"(", st.jitterHistogram.GetBinStart (i), "-", \
gjc@3963
   132
                  st.jitterHistogram.GetBinEnd (i), "): ", st.jitterHistogram.GetBinCount (i)
gjc@3960
   133
            print >> os, "PacketSize Histogram"
gjc@3969
   134
            for i in range(st.packetSizeHistogram.GetNBins () ):
gjc@3963
   135
              print >> os, " ",i,"(", st.packetSizeHistogram.GetBinStart (i), "-", \
gjc@3960
   136
                  st.packetSizeHistogram.GetBinEnd (i), "): ", st.packetSizeHistogram.GetBinCount (i)
gjc@3960
   137
gjc@3960
   138
        for reason, drops in enumerate(st.packetsDropped):
gjc@3964
   139
            print "  Packets dropped by reason %i: %i" % (reason, drops)
gjc@3960
   140
        #for reason, drops in enumerate(st.bytesDropped):
gjc@3960
   141
        #    print "Bytes dropped by reason %i: %i" % (reason, drops)
gjc@3934
   142
gjc@3934
   143
    monitor.CheckForLostPackets()
gjc@3938
   144
    classifier = flowmon_helper.GetClassifier()
gjc@3960
   145
gjc@3934
   146
    for flow_id, flow_stats in monitor.GetFlowStats():
gjc@3936
   147
        t = classifier.FindFlow(flow_id)
gjc@3936
   148
        proto = {6: 'TCP', 17: 'UDP'} [t.protocol]
gjc@3936
   149
        print "FlowID: %i (%s %s/%s --> %s/%i)" % \
gjc@3936
   150
            (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort)
gjc@3960
   151
        print_stats(sys.stdout, flow_stats)
gjc@3960
   152
gjc@3927
   153
gjc@3927
   154
    return 0
gjc@3927
   155
gjc@3927
   156
gjc@3927
   157
if __name__ == '__main__':
gjc@3927
   158
    sys.exit(main(sys.argv))
gjc@3927
   159