## mininet / mininet / topolib.py @ master

History | View | Annotate | Download (2.82 KB)

1 | 11a6d400 | Bob Lantz | ```
"Library of potentially useful topologies for Mininet"
``` |
---|---|---|---|

2 | |||

3 | 5a8bb489 | Bob Lantz | from mininet.topo import Topo |

4 | cd745748 | Bob Lantz | from mininet.net import Mininet |

5 | 11a6d400 | Bob Lantz | |

6 | 061598f0 | Bob Lantz | ```
# The build() method is expected to do this:
``` |

7 | ```
# pylint: disable=arguments-differ
``` |
||

8 | b1ec912d | Bob Lantz | |

9 | 11a6d400 | Bob Lantz | class TreeTopo( Topo ): |

10 | ```
"Topology for a tree network with a given depth and fanout."
``` |
||

11 | |||

12 | 1b2c7a31 | Bob Lantz | def build( self, depth=1, fanout=2 ): |

13 | 5a8bb489 | Bob Lantz | ```
# Numbering: h1..N, s1..M
``` |

14 | 06b8210e | Bob Lantz | self.hostNum = 1 |

15 | 5a8bb489 | Bob Lantz | self.switchNum = 1 |

16 | 11a6d400 | Bob Lantz | ```
# Build topology
``` |

17 | 06b8210e | Bob Lantz | ```
self.addTree( depth, fanout )
``` |

18 | 11a6d400 | Bob Lantz | |

19 | 06b8210e | Bob Lantz | def addTree( self, depth, fanout ): |

20 | 11a6d400 | Bob Lantz | ```
"""Add a subtree starting with node n.
``` |

21 | ```
returns: last node added"""
``` |
||

22 | ```
isSwitch = depth > 0
``` |
||

23 | 06b8210e | Bob Lantz | ```
if isSwitch:
``` |

24 | ce15c4f6 | Bob Lantz | node = self.addSwitch( 's%s' % self.switchNum ) |

25 | 06b8210e | Bob Lantz | self.switchNum += 1 |

26 | 5a8bb489 | Bob Lantz | for _ in range( fanout ): |

27 | child = self.addTree( depth - 1, fanout ) |
||

28 | ce15c4f6 | Bob Lantz | ```
self.addLink( node, child )
``` |

29 | 06b8210e | Bob Lantz | ```
else:
``` |

30 | ce15c4f6 | Bob Lantz | node = self.addHost( 'h%s' % self.hostNum ) |

31 | 06b8210e | Bob Lantz | self.hostNum += 1 |

32 | 5a8bb489 | Bob Lantz | ```
return node
``` |

33 | 11a6d400 | Bob Lantz | |

34 | cd745748 | Bob Lantz | |

35 | def TreeNet( depth=1, fanout=2, **kwargs ): |
||

36 | ```
"Convenience function for creating tree networks."
``` |
||

37 | topo = TreeTopo( depth, fanout ) |
||

38 | ```
return Mininet( topo, **kwargs )
``` |
||

39 | b5962e8e | Bob Lantz | |

40 | |||

41 | class TorusTopo( Topo ): |
||

42 | ```
"""2-D Torus topology
``` |
||

43 | ```
WARNING: this topology has LOOPS and WILL NOT WORK
``` |
||

44 | ```
with the default controller or any Ethernet bridge
``` |
||

45 | ```
without STP turned on! It can be used with STP, e.g.:
``` |
||

46 | ```
# mn --topo torus,3,3 --switch lxbr,stp=1 --test pingall"""
``` |
||

47 | 5a530af1 | Bob Lantz | |

48 | b27cce08 | Brian O'Connor | def build( self, x, y, n=1 ): |

49 | ```
"""x: dimension of torus in x-direction
``` |
||

50 | ```
y: dimension of torus in y-direction
``` |
||

51 | ```
n: number of hosts per switch"""
``` |
||

52 | b5962e8e | Bob Lantz | if x < 3 or y < 3: |

53 | raise Exception( 'Please use 3x3 or greater for compatibility ' |
||

54 | 7a3159c9 | Bob Lantz | ```
'with 2.1' )
``` |

55 | b27cce08 | Brian O'Connor | if n == 1: |

56 | genHostName = lambda loc, k: 'h%s' % ( loc ) |
||

57 | ```
else:
``` |
||

58 | genHostName = lambda loc, k: 'h%sx%d' % ( loc, k ) |
||

59 | |||

60 | b5962e8e | Bob Lantz | ```
hosts, switches, dpid = {}, {}, 0
``` |

61 | ```
# Create and wire interior
``` |
||

62 | for i in range( 0, x ): |
||

63 | for j in range( 0, y ): |
||

64 | loc = '%dx%d' % ( i + 1, j + 1 ) |
||

65 | ```
# dpid cannot be zero for OVS
``` |
||

66 | dpid = ( i + 1 ) * 256 + ( j + 1 ) |
||

67 | b1ec912d | Bob Lantz | ```
switch = switches[ i, j ] = self.addSwitch(
``` |

68 | bb35d041 | Bob Lantz | 's' + loc, dpid='%x' % dpid ) |

69 | b27cce08 | Brian O'Connor | for k in range( 0, n ): |

70 | host = hosts[ i, j, k ] = self.addHost( genHostName( loc, k + 1 ) ) |
||

71 | ```
self.addLink( host, switch )
``` |
||

72 | b5962e8e | Bob Lantz | ```
# Connect switches
``` |

73 | for i in range( 0, x ): |
||

74 | for j in range( 0, y ): |
||

75 | sw1 = switches[ i, j ] |
||

76 | ```
sw2 = switches[ i, ( j + 1 ) % y ]
``` |
||

77 | ```
sw3 = switches[ ( i + 1 ) % x, j ]
``` |
||

78 | ```
self.addLink( sw1, sw2 )
``` |
||

79 | ```
self.addLink( sw1, sw3 )
``` |
||

80 | |||

81 | 061598f0 | Bob Lantz | `# pylint: enable=arguments-differ` |