blob: 5ada58893739796dd3694f0e8158cf3305676b6b [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Ashlesh Gawandeda475f02017-03-01 17:20:58 -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"]
Vince Lehman3b8bc652015-06-18 15:01:47 -050034 self.convergenceTime = args["ctime"]
35 self.nPings = args["nPings"]
36 self.strategy = args["strategy"]
Ashlesh Gawande044611d2016-12-21 14:24:49 -060037 self.pctTraffic = args["pctTraffic"]
Ashlesh Gawande6a075c22017-08-03 15:15:49 -050038 self.nlsrSecurity = args["nlsrSecurity"]
Alexander Lane1bc9b472018-05-16 15:07:16 -050039 self.arguments = args["arguments"]
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -050040
41 # Used to restart pings on the recovered node if any
42 self.pingedDict = {}
43
ashu34c3ee02015-03-25 14:41:14 -050044 def start(self):
45 self.setup()
46 self.run()
47
48 def setup(self):
49 for host in self.net.hosts:
50 # Set strategy
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060051 host.nfd.setStrategy("/ndn/", self.strategy)
ashu34c3ee02015-03-25 14:41:14 -050052
53 # Start ping server
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060054 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
ashu34c3ee02015-03-25 14:41:14 -050055
56 # Create folder to store ping data
57 host.cmd("mkdir ping-data")
58
Ashlesh Gawande5f470202017-02-25 12:02:53 -060059 self.checkConvergence()
60
61 def checkConvergence(self, convergenceTime = None):
62 if convergenceTime is None:
63 convergenceTime = self.convergenceTime
64
ashu34c3ee02015-03-25 14:41:14 -050065 # Wait for convergence time period
Ashlesh Gawande5f470202017-02-25 12:02:53 -060066 print "Waiting " + str(convergenceTime) + " seconds for convergence..."
67 time.sleep(convergenceTime)
ashu34c3ee02015-03-25 14:41:14 -050068 print "...done"
69
70 # To check whether all the nodes of NLSR have converged
71 didNlsrConverge = True
72
73 # Checking for convergence
74 for host in self.net.hosts:
Ashlesh Gawandef932a182016-12-19 23:45:26 -060075 statusRouter = host.cmd("nfdc fib list | grep site/%C1.Router/cs/")
76 statusPrefix = host.cmd("nfdc fib list | grep ndn | grep site | grep -v Router")
ashu34c3ee02015-03-25 14:41:14 -050077 didNodeConverge = True
Ashlesh Gawandef6a610b2017-02-21 14:48:08 -060078 for node in self.net.hosts:
79 # Node has its own router name in the fib list, but not name prefix
80 if ( ("/ndn/" + node.name + "-site/%C1.Router/cs/" + node.name) not in statusRouter or
81 host.name != node.name and ("/ndn/" + node.name + "-site/" + node.name) not in statusPrefix ):
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060082 didNodeConverge = False
83 didNlsrConverge = False
ashu34c3ee02015-03-25 14:41:14 -050084
85 host.cmd("echo " + str(didNodeConverge) + " > convergence-result &")
86
87 if didNlsrConverge:
88 print("NLSR has successfully converged.")
89 else:
90 print("NLSR has not converged. Exiting...")
Ashlesh Gawande3807c1b2016-08-05 16:27:02 -050091 self.net.stop()
ashu34c3ee02015-03-25 14:41:14 -050092 sys.exit(1)
93
Vince Lehmancb20c542015-05-12 14:04:47 -050094 def ping(self, source, dest, nPings):
95 # Use "&" to run in background and perform parallel pings
96 print "Scheduling ping(s) from %s to %s" % (source.name, dest.name)
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -060097 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 -050098 time.sleep(0.2)
99
ashu34c3ee02015-03-25 14:41:14 -0500100 def startPings(self):
101 for host in self.net.hosts:
102 for other in self.net.hosts:
103 # Do not ping self
104 if host.name != other.name:
Vince Lehmancb20c542015-05-12 14:04:47 -0500105 self.ping(host, other, self.nPings)
ashu34c3ee02015-03-25 14:41:14 -0500106
Vince Lehmand96eed32015-10-22 13:57:27 -0500107 def failNode(self, host):
108 print("Bringing %s down" % host.name)
109 host.nfd.stop()
110
111 def recoverNode(self, host):
112 print("Bringing %s up" % host.name)
113 host.nfd.start()
Ashlesh Gawande708fcca2017-06-23 14:04:12 -0500114 host.nlsr.createFaces()
Vince Lehmand96eed32015-10-22 13:57:27 -0500115 host.nlsr.start()
Ashlesh Gawandee144ceb2016-11-14 13:56:24 -0600116 host.nfd.setStrategy("/ndn/", self.strategy)
117 host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &")
Vince Lehmand96eed32015-10-22 13:57:27 -0500118
Ashlesh Gawanded9c9e522015-10-15 16:40:12 -0500119 def startPctPings(self):
120 nNodesToPing = int(round(len(self.net.hosts)*self.pctTraffic))
121 print "Each node will ping %d node(s)" % nNodesToPing
122 # Temporarily store all the nodes being pinged by a particular node
123 nodesPingedList = []
124
125 for host in self.net.hosts:
126 # Create a circular list
127 pool = cycle(self.net.hosts)
128
129 # Move iterator to current node
130 next(x for x in pool if host.name == x.name)
131
132 # Track number of nodes to ping scheduled for this node
133 nNodesScheduled = 0
134
135 while nNodesScheduled < nNodesToPing:
136 other = pool.next()
137
138 # Do not ping self
139 if host.name != other.name:
140 self.ping(host, other, self.nPings)
141 nodesPingedList.append(other)
142
143 # Always increment because in 100% case a node should not ping itself
144 nNodesScheduled = nNodesScheduled + 1
145
146 self.pingedDict[host] = nodesPingedList
147 nodesPingedList = []
148
Vince Lehman3b8bc652015-06-18 15:01:47 -0500149 @staticmethod
150 def register(name, experimentClass):
Alexander Lane1bc9b472018-05-16 15:07:16 -0500151 ExperimentManager.register(name, experimentClass)