blob: d13f088395498b6626e4d138e7f7ec968368b486 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Ashlesh Gawande5f470202017-02-25 12:02:53 -06003# Copyright (C) 2015-2017 The University of Memphis,
4# Arizona Board of Regents,
5# Regents of the University of California.
Vince Lehmanb8b18062015-07-14 13:07:22 -05006#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
ashu34c3ee02015-03-25 14:41:14 -050023
24import time
25import sys
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -050026from itertools import cycle
ashu34c3ee02015-03-25 14:41:14 -050027
Vince Lehman3b8bc652015-06-18 15:01:47 -050028from ndn import ExperimentManager
29
ashu34c3ee02015-03-25 14:41:14 -050030class Experiment:
31
Vince Lehman3b8bc652015-06-18 15:01:47 -050032 def __init__(self, args):
33 self.net = args["net"]
34 self.nodes = args["nodes"]
35 self.convergenceTime = args["ctime"]
36 self.nPings = args["nPings"]
37 self.strategy = args["strategy"]
Ashlesh Gawande044611d2016-12-21 14:24:49 -060038 self.pctTraffic = args["pctTraffic"]
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -050039
40 # Used to restart pings on the recovered node if any
41 self.pingedDict = {}
42
ashu34c3ee02015-03-25 14:41:14 -050043 def start(self):
44 self.setup()
45 self.run()
46
47 def setup(self):
48 for host in self.net.hosts:
49 # Set strategy
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060050 host.nfd.setStrategy("/ndn/", self.strategy)
ashu34c3ee02015-03-25 14:41:14 -050051
52 # Start ping server
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060053 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
ashu34c3ee02015-03-25 14:41:14 -050054
55 # Create folder to store ping data
56 host.cmd("mkdir ping-data")
57
Ashlesh Gawande5f470202017-02-25 12:02:53 -060058 self.checkConvergence()
59
60 def checkConvergence(self, convergenceTime = None):
61 if convergenceTime is None:
62 convergenceTime = self.convergenceTime
63
ashu34c3ee02015-03-25 14:41:14 -050064 # Wait for convergence time period
Ashlesh Gawande5f470202017-02-25 12:02:53 -060065 print "Waiting " + str(convergenceTime) + " seconds for convergence..."
66 time.sleep(convergenceTime)
ashu34c3ee02015-03-25 14:41:14 -050067 print "...done"
68
69 # To check whether all the nodes of NLSR have converged
70 didNlsrConverge = True
71
72 # Checking for convergence
73 for host in self.net.hosts:
Ashlesh Gawandef932a182016-12-19 23:45:26 -060074 statusRouter = host.cmd("nfdc fib list | grep site/%C1.Router/cs/")
75 statusPrefix = host.cmd("nfdc fib list | grep ndn | grep site | grep -v Router")
ashu34c3ee02015-03-25 14:41:14 -050076 didNodeConverge = True
77 for node in self.nodes.split(","):
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060078 if ( ("/ndn/" + node + "-site/%C1.Router/cs/" + node) not in statusRouter or
79 str(host) != node and ("/ndn/" + node + "-site/" + node) not in statusPrefix ):
80 didNodeConverge = False
81 didNlsrConverge = False
ashu34c3ee02015-03-25 14:41:14 -050082
83 host.cmd("echo " + str(didNodeConverge) + " > convergence-result &")
84
85 if didNlsrConverge:
86 print("NLSR has successfully converged.")
87 else:
88 print("NLSR has not converged. Exiting...")
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -050089 self.net.stop()
ashu34c3ee02015-03-25 14:41:14 -050090 sys.exit(1)
91
Vince Lehmancb20c542015-05-12 14:04:47 -050092 def ping(self, source, dest, nPings):
93 # Use "&" to run in background and perform parallel pings
94 print "Scheduling ping(s) from %s to %s" % (source.name, dest.name)
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060095 source.cmd("ndnping -t -c "+ str(nPings) + " /ndn/" + dest.name + "-site/" + dest.name + " >> ping-data/" + dest.name + ".txt &")
Vince Lehmancb20c542015-05-12 14:04:47 -050096 time.sleep(0.2)
97
ashu34c3ee02015-03-25 14:41:14 -050098 def startPings(self):
99 for host in self.net.hosts:
100 for other in self.net.hosts:
101 # Do not ping self
102 if host.name != other.name:
Vince Lehmancb20c542015-05-12 14:04:47 -0500103 self.ping(host, other, self.nPings)
ashu34c3ee02015-03-25 14:41:14 -0500104
Vince Lehmand96eed32015-10-22 13:57:27 -0500105 def failNode(self, host):
106 print("Bringing %s down" % host.name)
107 host.nfd.stop()
108
109 def recoverNode(self, host):
110 print("Bringing %s up" % host.name)
111 host.nfd.start()
112 host.nlsr.start()
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -0600113 host.nfd.setStrategy("/ndn/", self.strategy)
114 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
Vince Lehmand96eed32015-10-22 13:57:27 -0500115
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500116 def startPctPings(self):
117 nNodesToPing = int(round(len(self.net.hosts)*self.pctTraffic))
118 print "Each node will ping %d node(s)" % nNodesToPing
119 # Temporarily store all the nodes being pinged by a particular node
120 nodesPingedList = []
121
122 for host in self.net.hosts:
123 # Create a circular list
124 pool = cycle(self.net.hosts)
125
126 # Move iterator to current node
127 next(x for x in pool if host.name == x.name)
128
129 # Track number of nodes to ping scheduled for this node
130 nNodesScheduled = 0
131
132 while nNodesScheduled < nNodesToPing:
133 other = pool.next()
134
135 # Do not ping self
136 if host.name != other.name:
137 self.ping(host, other, self.nPings)
138 nodesPingedList.append(other)
139
140 # Always increment because in 100% case a node should not ping itself
141 nNodesScheduled = nNodesScheduled + 1
142
143 self.pingedDict[host] = nodesPingedList
144 nodesPingedList = []
145
Vince Lehman3b8bc652015-06-18 15:01:47 -0500146 @staticmethod
147 def register(name, experimentClass):
148 ExperimentManager.register(name, experimentClass)