mininet.link.Link
表示基本的一条链路,最基本的链路在 mininet 中其实就是一对 veth 接口对。
1
def __init__( self, node1, node2, port1=None, port2=None,
2
intfName1=None, intfName2=None,
3
intf=Intf, cls1=None, cls2=None, params1=None,
4
params2=None ):
5
"""Create veth link to another node, making two new interfaces.
6
node1: first node
7
node2: second node
8
port1: node1 port number (optional)
9
port2: node2 port number (optional)
10
intf: default interface class/constructor
11
cls1, cls2: optional interface-specific constructors
12
intfName1: node1 interface name (optional)
13
intfName2: node2 interface name (optional)
14
params1: parameters for interface 1
15
params2: parameters for interface 2"""
16
# This is a bit awkward; it seems that having everything in
17
# params would be more orthogonal, but being able to specify
18
# in-line arguments is more convenient!
19
if port1 is None:
20
port1 = node1.newPort()
21
if port2 is None:
22
port2 = node2.newPort()
23
if not intfName1:
24
intfName1 = self.intfName( node1, port1 )
25
if not intfName2:
26
intfName2 = self.intfName( node2, port2 )
27
28
self.makeIntfPair( intfName1, intfName2 )
29
30
if not cls1:
31
cls1 = intf
32
if not cls2:
33
cls2 = intf
34
if not params1:
35
params1 = {}
36
if not params2:
37
params2 = {}
38
39
intf1 = cls1( name=intfName1, node=node1, port=port1,
40
link=self, **params1 )
41
intf2 = cls2( name=intfName2, node=node2, port=port2,
42
link=self, **params2 )
43
44
# All we are is dust in the wind, and our two interfaces
45
self.intf1, self.intf2 = intf1, intf2
Copied!
创建链路时,需要在两个节点上分别生成两个端口,利用节点和端口,获取对应的两个网络接口的名称,例如 s1-eth0 和 h1-eth0,然后调用 makeIntfPair() 方法,最终调用 util.py 中的 makeIntfPair() 方法,调用系统中的 ip link 命令来创造一对 veth pair。
1
def makeIntfPair( intf1, intf2 ):
2
"""Make a veth pair connecting intf1 and intf2.
3
intf1: string, interface
4
intf2: string, interface
5
returns: success boolean"""
6
# Delete any old interfaces with the same names
7
quietRun( 'ip link del ' + intf1 )
8
quietRun( 'ip link del ' + intf2 )
9
# Create new pair
10
cmd = 'ip link add name ' + intf1 + ' type veth peer name ' + intf2
11
return quietRun( cmd )
Copied!
Copy link