blob: 25677948751e70a24eea9a07e0997e0bde4b388b [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Alexander Lane9944cf52018-05-17 12:16:50 -05003# Copyright (C) 2015-2018, The University of Memphis,
Ashlesh Gawande0cccdb82016-08-15 12:58:06 -05004# 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/>.
23#
24# This file incorporates work covered by the following copyright and
25# permission notice:
26#
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050027# Mininet 2.3.0d1 License
Vince Lehmanb8b18062015-07-14 13:07:22 -050028#
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050029# Copyright (c) 2013-2016 Open Networking Laboratory
Vince Lehmanb8b18062015-07-14 13:07:22 -050030# Copyright (c) 2009-2012 Bob Lantz and The Board of Trustees of
31# The Leland Stanford Junior University
32#
33# Original authors: Bob Lantz and Brandon Heller
34#
35# We are making Mininet available for public use and benefit with the
36# expectation that others will use, modify and enhance the Software and
37# contribute those enhancements back to the community. However, since we
38# would like to make the Software available for broadest use, with as few
39# restrictions as possible permission is hereby granted, free of charge, to
40# any person obtaining a copy of this Software to deal in the Software
41# under the copyrights without restriction, including without limitation
42# the rights to use, copy, modify, merge, publish, distribute, sublicense,
43# and/or sell copies of the Software, and to permit persons to whom the
44# Software is furnished to do so, subject to the following conditions:
45#
46# The above copyright notice and this permission notice shall be included
47# in all copies or substantial portions of the Software.
48#
49# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
50# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56#
57# The name and trademarks of copyright holder(s) may NOT be used in
58# advertising or publicity pertaining to the Software or any derivatives
59# without specific, written prior permission.
60
ashuef3490b2015-02-17 11:01:04 -060061from mininet.node import CPULimitedHost, Host, Node
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -050062from mininet.examples.cluster import RemoteMixin
63
ashuef3490b2015-02-17 11:01:04 -060064from ndn.nfd import Nfd
65
66class NdnHostCommon():
67 "Common methods of NdnHost and CpuLimitedNdnHost"
68
69 def configNdn(self):
70 self.buildPeerIp()
71
72 def buildPeerIp(self):
73 for iface in self.intfList():
74 link = iface.link
75 if link:
76 node1, node2 = link.intf1.node, link.intf2.node
77 if node1 == self:
78 self.peerList[node2.name] = link.intf2.node.IP(link.intf2)
79 else:
80 self.peerList[node1.name] = link.intf1.node.IP(link.intf1)
81
82 inited = False
83
84 @classmethod
85 def init(cls):
86 "Initialization for NDNHost class"
87 cls.inited = True
88
89class NdnHost(Host, NdnHostCommon):
90 "NDNHost is a Host that always runs NFD"
91
92 def __init__(self, name, **kwargs):
93
94 Host.__init__(self, name, **kwargs)
95 if not NdnHost.inited:
96 NdnHostCommon.init()
97
Ashlesh Gawande1b663692015-10-14 16:38:10 -050098 # Create home directory for a node
99 self.homeFolder = "%s/%s" % (self.params['workdir'], self.name)
100 self.cmd("mkdir -p %s" % self.homeFolder)
101 self.cmd("cd %s" % self.homeFolder)
102
Ashlesh Gawande532302b2018-02-15 18:58:20 -0600103 self.nfd = None
ashuef3490b2015-02-17 11:01:04 -0600104
105 self.peerList = {}
106
Vince Lehmanc64dcb82015-10-21 16:04:09 -0500107 def config(self, app=None, cache=None, **params):
ashuef3490b2015-02-17 11:01:04 -0600108
109 r = Node.config(self, **params)
110
Vince Lehmanc64dcb82015-10-21 16:04:09 -0500111 self.setParam(r, 'app', app=app)
ashuef3490b2015-02-17 11:01:04 -0600112 self.setParam(r, 'cache', cache=cache)
113
114 return r
115
116 def terminate(self):
117 "Stop node."
Ashlesh Gawande532302b2018-02-15 18:58:20 -0600118 if self.nfd is not None:
119 self.nfd.stop()
ashuef3490b2015-02-17 11:01:04 -0600120 Host.terminate(self)
121
122class CpuLimitedNdnHost(CPULimitedHost, NdnHostCommon):
123 '''CPULimitedNDNHost is a Host that always runs NFD and extends CPULimitedHost.
124 It should be used when one wants to limit the resources of NDN routers and hosts '''
125
126 def __init__(self, name, sched='cfs', **kwargs):
127
128 CPULimitedHost.__init__(self, name, sched, **kwargs)
129 if not NdnHost.inited:
130 NdnHostCommon.init()
131
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500132 # Create home directory for a node
133 self.homeFolder = "%s/%s" % (self.params['workdir'], self.name)
134 self.cmd("mkdir -p %s" % self.homeFolder)
135 self.cmd("cd %s" % self.homeFolder)
136
Ashlesh Gawande532302b2018-02-15 18:58:20 -0600137 self.nfd = None
ashuef3490b2015-02-17 11:01:04 -0600138
139 self.peerList = {}
140
Vince Lehmanc64dcb82015-10-21 16:04:09 -0500141 def config(self, app=None, cpu=None, cores=None, cache=None, **params):
ashuef3490b2015-02-17 11:01:04 -0600142
143 r = CPULimitedHost.config(self,cpu,cores, **params)
144
Vince Lehmanc64dcb82015-10-21 16:04:09 -0500145 self.setParam(r, 'app', app=app)
ashuef3490b2015-02-17 11:01:04 -0600146 self.setParam(r, 'cache', cache=cache)
147
148 return r
149
150 def terminate(self):
151 "Stop node."
Ashlesh Gawande532302b2018-02-15 18:58:20 -0600152 if self.nfd is not None:
153 self.nfd.stop()
154 CPULimitedHost.terminate(self)
Ashlesh Gawandef5f304b2016-06-16 16:42:41 -0500155
156class RemoteNdnHost(RemoteMixin, NdnHost):
157 "A node on a remote server"
158 pass