This repository has been archived by the owner on Jul 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.py
93 lines (61 loc) · 2.11 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Linux based switches"""
from subprocess import call
class Bridge(object):
"""Linux based bridge L2 switch
Enable forwarding in linux kernel and iptables.
"""
devnum = 0
def __init__(self, id):
self.id = id
self.links = {}
self.devname = "br%d" % Bridge.devnum
Bridge.devnum += 1
cmd = "brctl addbr %s"
call(cmd % self.devname, shell=True)
cmd = "ifconfig %s up"
call(cmd % self.devname, shell=True)
def add_link(self, devid, devname):
"""Add add a link to the switch
devid - Unique device id
devname - Device name
"""
self.links[devid] = devname
cmd = "brctl addif %s %s"
call(cmd % (self.devname, devname), shell=True)
cmd = "ifconfig %s up"
call(cmd % devname, shell=True)
def close(self):
"""Remove the switch"""
print "Removing switch %s ..." % self.id
cmd = "ifconfig %s down"
call(cmd % self.devname, shell=True)
cmd = "brctl delbr %s"
call(cmd % self.devname, shell=True)
class OpenVSwitch(object):
"""Open vSwitch based switch; Depends on ovs-vswitchd
To use this configure ovs-vswitchd as described in the Open vSwitch
installation manual INSTALL.Linux.
"""
devnum = 0
def __init__(self, id):
self.id = id
self.links = {}
self.devname = "br%d" % OpenVSwitch.devnum
OpenVSwitch.devnum += 1
cmd = "ovs-vsctl add-br %s"
call(cmd % self.devname, shell=True)
def add_link(self, devid, devname):
"""Add add a link to the switch
devid - Unique device id
devname - Device name
"""
self.links[devid] = devname
cmd = "ovs-vsctl add-port %s %s"
call(cmd % (self.devname, devname), shell=True)
cmd = "ifconfig %s up"
call(cmd % devname, shell=True)
def close(self):
"""Remove the switch"""
print "Removing switch %s ..." % self.id
cmd = "ovs-vsctl del-br %s"
call(cmd % self.devname, shell=True)