Mininet’s API is built at three primary levels:
* Low-level API: The low-level API consists of the base node and link classes (such as Host,Switch, and Link and their subclasses) which can actually be instantiated individually and used to create a network, but it is a bit unwieldy.
* Mid-level API: The mid-level API adds the Mininet object which serves as a container for nodes and links. It provides a number of methods (such as addHost(), addSwitch(), andaddLink()) for adding nodes and links to a network, as well as network configuration, startup and shutdown (notably start() and stop().)mininet 对象作为一个容器提供了几个功能,如方便的添加节点,链路,配置网络,开启关闭网络等。
* High-level API: The high-level API adds a topology template abstraction, the Topo class, which provides the ability to create reusable, parametrized topology templates. These templates can be passed to the mn command (via the --custom option) and used from the command line.
It is valuable to understand each of the API levels. In general when you want to control nodes and switches directly, you use the low-level API. When you want to start or stop a network, you usually use the mid-level API (notably the Mininet class.)
Things become interesting when you start thinking about creating full networks. Full networks can be created using any of the API levels (as seen in the examples), but usually you will want to pick either the mid-level API (e.g. Mininet.add*()) or the high-level API (Topo.add*()) to create your networks.
Here are examples of creating networks using each API level:
Low-level API: nodes and links
h1 = Host( 'h1' )
h2 = Host( 'h2' )
s1 = OVSSwitch( 's1', inNamespace=False )
c0 = Controller( 'c0', inNamespace=False )
Link( h1, s1 )
Link( h2, s1 )
h1.setIP( '10.1/8' )
h2.setIP( '10.2/8' )
c0.start()
s1.start( [ c0 ] )
print h1.cmd( 'ping -c1', h2.IP() )
s1.stop()
c0.stop()
Mid-level API: Network object
net = Mininet()
h1 = net.addHost( 'h1' )
h2 = net.addHost( 'h2' )
s1 = net.addSwitch( 's1' )
c0 = net.addController( 'c0' )
net.addLink( h1, s1 )
net.addLink( h2, s1 )
net.start()
print h1.cmd( 'ping -c1', h2.IP() )
CLI( net )
net.stop()
High-level API: Topology templates
class SingleSwitchTopo( Topo ):
"Single Switch Topology"
def build( self, count=1 ):
hosts = [ self.addHost( 'h%d' % i )
for i in range( 1, count + 1 ) ]
s1 = self.addSwitch( 's1' )
for h in hosts:
self.addLink( h, s1 )
net = Mininet( topo=SingleSwitchTopo( 3 ) )
net.start()
CLI( net )
net.stop()
As you can see, the mid-level API is a bit simpler because it doesn’t require creation of a topology class. The low-level and mid-level APIs are flexible and powerful, but may be less convenient to reuse compared to the high-level Topo API.
Note also that in Mininet versions before 2.2.0 the high-level Topo doesn’t support multiple links between nodes, but the lower level APIs do. Currently Topo also doesn’t concern itself with which switches are controlled by which controllers (you can use a custom Switch subclass to do this, as described above.) With the mid-level and low-level APIs, you can manually start the switches if desired, passing the appropriate list of controllers to each switch.
每一个控制器控制哪些交换机用顶级api暂时无法完成。
文档生成
sudo apt-get install doxypy
cd ~/mininet
make doc
cd doc
python -m SimpleHTTPServer
参考网址
http://api.mininet.org.
https://github.com/mininet/mininet/wiki/Introduction-to-Mininet#examples