blob: 0bed2478f0dcd8b7f5e950cb65385c6792a02865 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- 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/>.
23#
24# This file incorporates work covered by the following copyright and
25# permission notice:
26#
27# Mininet 2.2.1 License
28#
29# Copyright (c) 2013-2015 Open Networking Laboratory
30# 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
62from ndn.nfd import Nfd
63
64class NdnHostCommon():
65 "Common methods of NdnHost and CpuLimitedNdnHost"
66
67 def configNdn(self):
68 self.buildPeerIp()
69
70 def buildPeerIp(self):
71 for iface in self.intfList():
72 link = iface.link
73 if link:
74 node1, node2 = link.intf1.node, link.intf2.node
75 if node1 == self:
76 self.peerList[node2.name] = link.intf2.node.IP(link.intf2)
77 else:
78 self.peerList[node1.name] = link.intf1.node.IP(link.intf1)
79
80 inited = False
81
82 @classmethod
83 def init(cls):
84 "Initialization for NDNHost class"
85 cls.inited = True
86
87class NdnHost(Host, NdnHostCommon):
88 "NDNHost is a Host that always runs NFD"
89
90 def __init__(self, name, **kwargs):
91
92 Host.__init__(self, name, **kwargs)
93 if not NdnHost.inited:
94 NdnHostCommon.init()
95
Ashlesh Gawande1b663692015-10-14 16:38:10 -050096 # Create home directory for a node
97 self.homeFolder = "%s/%s" % (self.params['workdir'], self.name)
98 self.cmd("mkdir -p %s" % self.homeFolder)
99 self.cmd("cd %s" % self.homeFolder)
100
ashuef3490b2015-02-17 11:01:04 -0600101 self.nfd = Nfd(self)
102 self.nfd.start()
103
104 self.peerList = {}
105
106 def config(self, fib=None, app=None, cache=None, **params):
107
108 r = Node.config(self, **params)
109
110 self.setParam(r, 'app', fib=fib) # why is this not app=app, to be investigated
111 self.setParam(r, 'fib', app=app) # and this fib=fib
112 self.setParam(r, 'cache', cache=cache)
113
114 return r
115
116 def terminate(self):
117 "Stop node."
118 self.nfd.stop()
119 Host.terminate(self)
120
121class CpuLimitedNdnHost(CPULimitedHost, NdnHostCommon):
122 '''CPULimitedNDNHost is a Host that always runs NFD and extends CPULimitedHost.
123 It should be used when one wants to limit the resources of NDN routers and hosts '''
124
125 def __init__(self, name, sched='cfs', **kwargs):
126
127 CPULimitedHost.__init__(self, name, sched, **kwargs)
128 if not NdnHost.inited:
129 NdnHostCommon.init()
130
Ashlesh Gawande1b663692015-10-14 16:38:10 -0500131 # Create home directory for a node
132 self.homeFolder = "%s/%s" % (self.params['workdir'], self.name)
133 self.cmd("mkdir -p %s" % self.homeFolder)
134 self.cmd("cd %s" % self.homeFolder)
135
ashuef3490b2015-02-17 11:01:04 -0600136 self.nfd = Nfd(self)
137 self.nfd.start()
138
139 self.peerList = {}
140
141 def config(self, fib=None, app=None, cpu=None, cores=None, cache=None, **params):
142
143 r = CPULimitedHost.config(self,cpu,cores, **params)
144
145 self.setParam(r, 'app', fib=fib) #????? shoud it be app=app
146 self.setParam(r, 'fib', app=app)
147 self.setParam(r, 'cache', cache=cache)
148
149 return r
150
151 def terminate(self):
152 "Stop node."
153 self.nfd.stop()
154 Host.terminate(self)