forked from unias/docklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnettools.py
executable file
·302 lines (264 loc) · 13 KB
/
nettools.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python3
import subprocess
class ipcontrol(object):
@staticmethod
def parse(cmdout):
links = {}
thislink = None
for line in cmdout.splitlines():
# empty line
if len(line)==0:
continue
# Level 1 : first line of one link
if line[0] != ' ':
blocks = line.split()
thislink = blocks[1].strip(':')
links[thislink] = {}
links[thislink]['state'] = blocks[blocks.index('state')+1] if 'state' in blocks else 'UNKNOWN'
# Level 2 : line with 4 spaces
elif line[4] != ' ':
blocks = line.split()
if blocks[0] == 'inet':
if 'inet' not in links[thislink]:
links[thislink]['inet'] = []
links[thislink]['inet'].append(blocks[1])
# we just need inet (IPv4)
else:
pass
# Level 3 or more : no need for us
else:
pass
return links
@staticmethod
def list_links():
try:
ret = subprocess.run(['ip', 'link', 'show'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
links = ipcontrol.parse(ret.stdout.decode('utf-8'))
return [True, list(links.keys())]
except subprocess.CalledProcessError as suberror:
return [False, "list links failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def link_exist(linkname):
try:
subprocess.run(['ip', 'link', 'show', 'dev', str(linkname)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return True
except subprocess.CalledProcessError:
return False
@staticmethod
def link_info(linkname):
try:
ret = subprocess.run(['ip', 'address', 'show', 'dev', str(linkname)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, ipcontrol.parse(ret.stdout.decode('utf-8'))[str(linkname)]]
except subprocess.CalledProcessError as suberror:
return [False, "get link info failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def link_state(linkname):
try:
ret = subprocess.run(['ip', 'link', 'show', 'dev', str(linkname)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, ipcontrol.parse(ret.stdout.decode('utf-8'))[str(linkname)]['state']]
except subprocess.CalledProcessError as suberror:
return [False, "get link state failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def link_ips(linkname):
[status, info] = ipcontrol.link_info(str(linkname))
if status:
if 'inet' not in info:
return [True, []]
else:
return [True, info['inet']]
else:
return [False, info]
@staticmethod
def up_link(linkname):
try:
subprocess.run(['ip', 'link', 'set', 'dev', str(linkname), 'up'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(linkname)]
except subprocess.CalledProcessError as suberror:
return [False, "set link up failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def down_link(linkname):
try:
subprocess.run(['ip', 'link', 'set', 'dev', str(linkname), 'down'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(linkname)]
except subprocess.CalledProcessError as suberror:
return [False, "set link down failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_addr(linkname, address):
try:
subprocess.run(['ip', 'address', 'add', address, 'dev', str(linkname)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(linkname)]
except subprocess.CalledProcessError as suberror:
return [False, "add address failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def del_addr(linkname, address):
try:
subprocess.run(['ip', 'address', 'del', address, 'dev', str(linkname)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(linkname)]
except subprocess.CalledProcessError as suberror:
return [False, "delete address failed : %s" % suberror.stdout.decode('utf-8')]
# ovs-vsctl list-br
# ovs-vsctl br-exists <Bridge>
# ovs-vsctl add-br <Bridge>
# ovs-vsctl del-br <Bridge>
# ovs-vsctl list-ports <Bridge>
# ovs-vsctl del-port <Bridge> <Port>
# ovs-vsctl add-port <Bridge> <Port> -- set interface <Port> type=gre options:remote_ip=<RemoteIP>
# ovs-vsctl add-port <Bridge> <Port> tag=<ID> -- set interface <Port> type=internal
# ovs-vsctl port-to-br <Port>
# ovs-vsctl set Port <Port> tag=<ID>
# ovs-vsctl clear Port <Port> tag
class ovscontrol(object):
@staticmethod
def list_bridges():
try:
ret = subprocess.run(['ovs-vsctl', 'list-br'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, ret.stdout.decode('utf-8').split()]
except subprocess.CalledProcessError as suberror:
return [False, "list bridges failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def bridge_exist(bridge):
try:
subprocess.run(['ovs-vsctl', 'br-exists', str(bridge)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return True
except subprocess.CalledProcessError:
return False
@staticmethod
def port_tobridge(port):
try:
ret = subprocess.run(['ovs-vsctl', 'port-to-br', str(port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, ret.stdout.decode('utf-8').strip()]
except subprocess.CalledProcessError as suberror:
return [False, suberror.stdout.decode('utf-8')]
@staticmethod
def port_exists(port):
return ovscontrol.port_tobridge(port)[0]
@staticmethod
def add_bridge(bridge):
try:
subprocess.run(['ovs-vsctl', '--may-exist', 'add-br', str(bridge)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(bridge)]
except subprocess.CalledProcessError as suberror:
return [False, "add bridge failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def del_bridge(bridge):
try:
subprocess.run(['ovs-vsctl', 'del-br', str(bridge)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(bridge)]
except subprocess.CalledProcessError as suberror:
return [False, "del bridge failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def list_ports(bridge):
try:
ret = subprocess.run(['ovs-vsctl', 'list-ports', str(bridge)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, ret.stdout.decode('utf-8').split()]
except subprocess.CalledProcessError as suberror:
return [False, "list ports failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def del_port(bridge, port):
try:
subprocess.run(['ovs-vsctl', 'del-port', str(bridge), str(port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "delete port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_port(bridge, port):
try:
subprocess.run(['ovs-vsctl', '--may-exist', 'add-port', str(bridge), str(port)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "add port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_port_internal(bridge, port):
try:
subprocess.run(['ovs-vsctl', 'add-port', str(bridge), str(port), '--', 'set', 'interface', str(port), 'type=internal'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "add port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_port_internal_withtag(bridge, port, tag):
try:
subprocess.run(['ovs-vsctl', 'add-port', str(bridge), str(port), 'tag='+str(tag), '--', 'set', 'interface', str(port), 'type=internal'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "add port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_port_gre(bridge, port, remote):
try:
subprocess.run(['ovs-vsctl', 'add-port', str(bridge), str(port), '--', 'set', 'interface', str(port), 'type=gre', 'options:remote_ip='+str(remote)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "add port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def add_port_gre_withkey(bridge, port, remote, key):
try:
subprocess.run(['ovs-vsctl', '--may-exist', 'add-port', str(bridge), str(port), '--', 'set', 'interface', str(port), 'type=gre', 'options:remote_ip='+str(remote), 'options:key='+str(key)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "add port failed : %s" % suberror.stdout.decode('utf-8')]
@staticmethod
def set_port_tag(port, tag):
try:
subprocess.run(['ovs-vsctl', 'set', 'Port', str(port), 'tag='+str(tag)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True)
return [True, str(port)]
except subprocess.CalledProcessError as suberror:
return [False, "set port tag failed : %s" % suberror.stdout.decode('utf-8')]
class netcontrol(object):
@staticmethod
def bridge_exists(bridge):
return ovscontrol.bridge_exist(bridge)
@staticmethod
def del_bridge(bridge):
return ovscontrol.del_bridge(bridge)
@staticmethod
def new_bridge(bridge):
return ovscontrol.add_bridge(bridge)
@staticmethod
def gre_exists(bridge, remote):
# port is unique, bridge is not necessary
return ovscontrol.port_exists('gre-'+str(remote))
@staticmethod
def setup_gre(bridge, remote):
return ovscontrol.add_port_gre(bridge, 'gre-'+str(remote), remote)
@staticmethod
def gw_exists(bridge, gwport):
return ovscontrol.port_exists(gwport)
@staticmethod
def setup_gw(bridge, gwport, addr):
[status, result] = ovscontrol.add_port_internal(bridge, gwport)
if not status:
return [status, result]
[status, result] = ipcontrol.add_addr(gwport, addr)
if not status:
return [status, result]
return ipcontrol.up_link(gwport)
@staticmethod
def del_gw(bridge, gwport):
return ovscontrol.del_port(bridge, gwport)
@staticmethod
def check_gw(bridge, gwport, uid, addr):
ovscontrol.add_bridge(bridge)
if not netcontrol.gw_exists(bridge, gwport):
return netcontrol.setup_gw(bridge, gwport, addr)
[status, info] = ipcontrol.link_info(gwport)
if not status:
return [False, "get gateway info failed"]
if ('inet' not in info) or (addr not in info['inet']):
ipcontrol.add_addr(gwport, addr)
else:
info['inet'].remove(addr)
for otheraddr in info['inet']:
ipcontrol.del_addr(gwport, otheraddr)
if info['state'] == 'DOWN':
ipcontrol.up_link(gwport)
return [True, "check gateway port %s" % gwport]
@staticmethod
def recover_usernet(portname, uid, GatewayHost, isGatewayHost):
ovscontrol.add_bridge("docklet-br-"+str(uid))
if not isGatewayHost:
[success, ports] = ovscontrol.list_ports("docklet-br-"+str(uid))
if success:
for port in ports:
if port.startswith("gre") and (not port == ("gre-"+str(uid)+"-"+GatewayHost) ) :
ovscontrol.del_port("docklet-br-"+str(uid),port)
ovscontrol.add_port_gre_withkey("docklet-br-"+str(uid), "gre-"+str(uid)+"-"+GatewayHost, GatewayHost, str(uid))
ovscontrol.add_port("docklet-br-"+str(uid), portname)