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