Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015 The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 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/>. |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 23 | |
| 24 | import time |
| 25 | import sys |
Ashlesh Gawande | d9c9e52 | 2015-10-15 16:40:12 -0500 | [diff] [blame] | 26 | from itertools import cycle |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 27 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 28 | from ndn import ExperimentManager |
| 29 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 30 | class Experiment: |
| 31 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 32 | 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 Gawande | d9c9e52 | 2015-10-15 16:40:12 -0500 | [diff] [blame] | 38 | self.pctTraffic = float(args["pctTraffic"]) |
| 39 | |
| 40 | # Used to restart pings on the recovered node if any |
| 41 | self.pingedDict = {} |
| 42 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 43 | |
| 44 | 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 Gawande | e144ceb | 2016-11-14 13:56:24 -0600 | [diff] [blame] | 51 | host.nfd.setStrategy("/ndn/", self.strategy) |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 52 | |
| 53 | # Start ping server |
Ashlesh Gawande | e144ceb | 2016-11-14 13:56:24 -0600 | [diff] [blame] | 54 | host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &") |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 55 | |
| 56 | # Create folder to store ping data |
| 57 | host.cmd("mkdir ping-data") |
| 58 | |
| 59 | # Wait for convergence time period |
| 60 | print "Waiting " + str(self.convergenceTime) + " seconds for convergence..." |
| 61 | time.sleep(self.convergenceTime) |
| 62 | print "...done" |
| 63 | |
| 64 | # To check whether all the nodes of NLSR have converged |
| 65 | didNlsrConverge = True |
| 66 | |
| 67 | # Checking for convergence |
| 68 | for host in self.net.hosts: |
Ashlesh Gawande | f932a18 | 2016-12-19 23:45:26 -0600 | [diff] [blame^] | 69 | statusRouter = host.cmd("nfdc fib list | grep site/%C1.Router/cs/") |
| 70 | statusPrefix = host.cmd("nfdc fib list | grep ndn | grep site | grep -v Router") |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 71 | didNodeConverge = True |
| 72 | for node in self.nodes.split(","): |
Ashlesh Gawande | e144ceb | 2016-11-14 13:56:24 -0600 | [diff] [blame] | 73 | if ( ("/ndn/" + node + "-site/%C1.Router/cs/" + node) not in statusRouter or |
| 74 | str(host) != node and ("/ndn/" + node + "-site/" + node) not in statusPrefix ): |
| 75 | didNodeConverge = False |
| 76 | didNlsrConverge = False |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 77 | |
| 78 | host.cmd("echo " + str(didNodeConverge) + " > convergence-result &") |
| 79 | |
| 80 | if didNlsrConverge: |
| 81 | print("NLSR has successfully converged.") |
| 82 | else: |
| 83 | print("NLSR has not converged. Exiting...") |
Ashlesh Gawande | 3807c1b | 2016-08-05 16:27:02 -0500 | [diff] [blame] | 84 | self.net.stop() |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 85 | sys.exit(1) |
| 86 | |
Vince Lehman | cb20c54 | 2015-05-12 14:04:47 -0500 | [diff] [blame] | 87 | def ping(self, source, dest, nPings): |
| 88 | # Use "&" to run in background and perform parallel pings |
| 89 | print "Scheduling ping(s) from %s to %s" % (source.name, dest.name) |
Ashlesh Gawande | e144ceb | 2016-11-14 13:56:24 -0600 | [diff] [blame] | 90 | source.cmd("ndnping -t -c "+ str(nPings) + " /ndn/" + dest.name + "-site/" + dest.name + " >> ping-data/" + dest.name + ".txt &") |
Vince Lehman | cb20c54 | 2015-05-12 14:04:47 -0500 | [diff] [blame] | 91 | time.sleep(0.2) |
| 92 | |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 93 | def startPings(self): |
| 94 | for host in self.net.hosts: |
| 95 | for other in self.net.hosts: |
| 96 | # Do not ping self |
| 97 | if host.name != other.name: |
Vince Lehman | cb20c54 | 2015-05-12 14:04:47 -0500 | [diff] [blame] | 98 | self.ping(host, other, self.nPings) |
ashu | 34c3ee0 | 2015-03-25 14:41:14 -0500 | [diff] [blame] | 99 | |
Vince Lehman | d96eed3 | 2015-10-22 13:57:27 -0500 | [diff] [blame] | 100 | def failNode(self, host): |
| 101 | print("Bringing %s down" % host.name) |
| 102 | host.nfd.stop() |
| 103 | |
| 104 | def recoverNode(self, host): |
| 105 | print("Bringing %s up" % host.name) |
| 106 | host.nfd.start() |
| 107 | host.nlsr.start() |
Ashlesh Gawande | e144ceb | 2016-11-14 13:56:24 -0600 | [diff] [blame] | 108 | host.nfd.setStrategy("/ndn/", self.strategy) |
| 109 | host.cmd("ndnpingserver /ndn/" + str(host) + "-site/" + str(host) + " > ping-server &") |
Vince Lehman | d96eed3 | 2015-10-22 13:57:27 -0500 | [diff] [blame] | 110 | |
Ashlesh Gawande | d9c9e52 | 2015-10-15 16:40:12 -0500 | [diff] [blame] | 111 | def startPctPings(self): |
| 112 | nNodesToPing = int(round(len(self.net.hosts)*self.pctTraffic)) |
| 113 | print "Each node will ping %d node(s)" % nNodesToPing |
| 114 | # Temporarily store all the nodes being pinged by a particular node |
| 115 | nodesPingedList = [] |
| 116 | |
| 117 | for host in self.net.hosts: |
| 118 | # Create a circular list |
| 119 | pool = cycle(self.net.hosts) |
| 120 | |
| 121 | # Move iterator to current node |
| 122 | next(x for x in pool if host.name == x.name) |
| 123 | |
| 124 | # Track number of nodes to ping scheduled for this node |
| 125 | nNodesScheduled = 0 |
| 126 | |
| 127 | while nNodesScheduled < nNodesToPing: |
| 128 | other = pool.next() |
| 129 | |
| 130 | # Do not ping self |
| 131 | if host.name != other.name: |
| 132 | self.ping(host, other, self.nPings) |
| 133 | nodesPingedList.append(other) |
| 134 | |
| 135 | # Always increment because in 100% case a node should not ping itself |
| 136 | nNodesScheduled = nNodesScheduled + 1 |
| 137 | |
| 138 | self.pingedDict[host] = nodesPingedList |
| 139 | nodesPingedList = [] |
| 140 | |
Vince Lehman | 3b8bc65 | 2015-06-18 15:01:47 -0500 | [diff] [blame] | 141 | @staticmethod |
| 142 | def register(name, experimentClass): |
| 143 | ExperimentManager.register(name, experimentClass) |