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