This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
f5dag.py
executable file
·423 lines (344 loc) · 14.3 KB
/
f5dag.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python
# Copyright (C) F5 Networks, Inc. 2015-2016
# Author: Christian Koenning, christian@f5.com
# No part of the software may be reproduced or transmitted in any
# form or by any means, electronic or mechanical, for any purpose,
# without express written permission of F5 Networks, Inc.
import eossdk
import sys
import json
import urllib2
import time
import socket
import hashlib
class Disaggregator():
''' encapsulates Disaggration functionality
connects to url and retrieves JSON doc
from the TMOS Controller
'''
def __init__(self, directFlowMgr, url, logger):
self.flowsAdded = 0
self.flowsDeleted = 0
self._directFlowMgr = directFlowMgr
self._url = url
self._rules = ''
self._logger = logger
self._monitorVlan = ''
self._clusterMembers = ''
self._clusterMacAddr = ''
self.updateDag()
self.installedFlows = set()
def setUrl(self, url):
''' setter for external update of _url '''
self._url = url
def getMonitorVlan(self):
''' access method for _monitorVlan '''
return int(self._monitorVlan)
def getClusterMembers(self):
''' access method for _clusterMembers'''
return self._clusterMembers
def updateDag(self):
''' retrieves the Disaggregation JSON doc from TMOS'''
if (self._url == ''):
data = self.loadDagFromFile()
else:
response = urllib2.urlopen(self._url)
res = response.read()
data = json.loads(res)
self._clusterMembers = data['clusterMembers']
self._monitorVlan = data['monitorVlan']
self._rules = data['rules']
self._clusterMacAddr = self.parseClusterMacAddr()
return True
def loadDagFromFile(self):
'''load the Dissagregation data for testing purposes
from the testsuite.json file
'''
json_data = open('/mnt/flash/testsuite.json')
data = json.load(json_data)
return data
def getClusterMacAddr(self):
''' external access'''
return dict.fromkeys(self._clusterMacAddr)
def parseClusterMacAddr(self):
''' parse the dissagregation JSON doc and return all
virtual mac addresses observed in a set
'''
result = set()
for data in self._rules:
result.add(data['action']['dl_src'])
return result
def calcDottedNetmask(self, mask):
''' helper function to cope with CDIR data in the JSON doc
and directflow expected a dotted netmask
'''
bits = 0
for i in xrange(32-mask, 32):
bits |= (1 << i)
return '%d.%d.%d.%d' % ((bits & 0xff000000) >> 24,
(bits & 0xff0000) >> 16,
(bits & 0xff00) >> 8,
(bits & 0xff))
def createDirectflow(self,
forwardingTable,
name,
ipSrc,
ipSrcMask,
ipDst,
ipDstMask,
vlan,
ethDst):
''' creates directflow entry in the switch. is idempodent'''
match = eossdk.FlowMatch()
matchFieldSet = eossdk.FlowMatchFieldSet()
if vlan is not None:
matchFieldSet.vlan_id_is(True)
match.vlan_id_is(int(vlan), 4095)
if ipSrc is not None:
matchFieldSet.ip_src_is(True)
ipSrc = eossdk.IpAddr(ipSrc)
ipSrcMask = eossdk.IpAddr(ipSrcMask)
match.ip_src_is(ipSrc, ipSrcMask)
if ipDst is not None:
matchFieldSet.ip_dst_is(True)
ipDst = eossdk.IpAddr(ipDst)
ipDstMask = eossdk.IpAddr(ipDstMask)
match.ip_dst_is(ipDst, ipDstMask)
match.match_field_set_is(matchFieldSet)
outputInt = eossdk.IntfId(forwardingTable[ethDst])
outputIntfs = [outputInt]
action = eossdk.FlowAction()
actionSet = eossdk.FlowActionSet()
if outputIntfs is not None:
actionSet.set_output_intfs_is(True)
action.output_intfs_is(tuple(outputIntfs))
if ethDst is not None:
actionSet.set_eth_dst_is(True)
newEthDst = eossdk.EthAddr(ethDst)
action.eth_dst_is(newEthDst)
action.action_set_is(actionSet)
self._directFlowMgr.flow_entry_set(eossdk.FlowEntry(name,
match,
action,
0))
def deleteDirectflow(self, name):
''' wrapper function to call Directflow delete from sdk'''
self._directFlowMgr.flow_entry_del(name)
def getInstalledFlows(self):
''' retrieve all installed flow name and return them in a set'''
result = set()
for flow in self._directFlowMgr.flow_entry_iter():
result.add(flow.name())
return result
def updateInstalledFlows(self):
''' convinience function to update the installed flows on object'''
self.installedFlows = self.getInstalledFlows()
def syncDirectflows(self, forwardingTable, periodic):
''' will sync flows from TMOS to Arista
Note that the uuid is deterministic, and contains
configTimestamp as prefix,
vlan, network and netmask as suffix
'''
if (periodic):
self.updateDag()
self.updateInstalledFlows()
for rule in self._rules:
ruleif = forwardingTable[rule['action']['dl_src']]
name = hashlib.sha1(rule['id'] + ruleif).hexdigest()
if (name in self.installedFlows):
self.installedFlows.remove(name)
else:
ipSrc = None
ipSrcMask = None
ipDst = None
ipDstMask = None
if ('nw_src' in rule['match']):
srcNet = rule['match']['nw_src']
ipSrc, sCdir = srcNet.split('/')
ipSrcMask = self.calcDottedNetmask(int(sCdir))
if ('nw_dst' in rule['match']):
dstNet = rule['match']['nw_dst']
ipDst, dCdir = dstNet.split('/')
ipDstMask = self.calcDottedNetmask(int(dCdir))
vlan = rule['match']['vlan_vid']
dstMac = rule['action']['dl_src']
self.flowsAdded += 1
self.createDirectflow(forwardingTable,
name,
ipSrc,
ipSrcMask,
ipDst,
ipDstMask,
vlan,
dstMac)
# whatever is still in the self.installedFlows needs to be deleted
for flow in self.installedFlows:
self.flowsDeleted += 1
self.deleteDirectflow(flow)
if (self.flowsAdded != 0 or self.flowsDeleted != 0):
if periodic:
msg = 'PERIODIC add %s Flows, del %s Flows' % \
(self.flowsAdded, self.flowsDeleted)
else:
msg = 'FAILOVER updated %s Flows' % self.flowsAdded
self._logger.log(msg)
self.flowsAdded = 0
self.flowsDeleted = 0
self.installedFlows = None
return True
class MacHandler(eossdk.AgentHandler,
eossdk.MacTableHandler,
eossdk.FlowHandler):
''' handles the mac address to port mapping
and registers a callback to the sdk to be notified
for changes of the virtual mac address of the Honeybee
Cluster.
'''
def __init__(self, sdk):
self._sdk = sdk
self.agentMgr = sdk.get_agent_mgr()
self.macTableMgr = sdk.get_mac_table_mgr()
self.directFlowMgr = sdk.get_directflow_mgr()
self.tmosUrl = 'http://10.20.1.10'
self.syncInterval = 5
self.logger = Logmsg('f5dag')
eossdk.FlowHandler.__init__(self, self.directFlowMgr)
self.dag = Disaggregator(self.directFlowMgr, self.tmosUrl, self.logger)
self.discoverCluster()
eossdk.AgentHandler.__init__(self, self.agentMgr)
eossdk.MacTableHandler.__init__(self, self.macTableMgr)
self.forwardingTable = dict.fromkeys(self.dag.getClusterMacAddr())
self.logger.log('initializing F5 TMOS Disaggregator...')
def getSyncInterval(self):
''' convinience function '''
return self.syncInterval
def sendDiscoverMessage(self, destIp):
''' helper function to send udp messages to
the cluster members, so that the fwd table will be
popluated
'''
msg = 'sending Discovery Message for %s' % destIp
self.logger.log(msg)
self._destIp = destIp
self._destPort = 666
self.message = 'F5 discovery'
self.sock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
self.sock.sendto(self.message, (self._destIp, self._destPort))
def discoverCluster(self):
''' will send each clusterIpAddresses a discovery
message
'''
for address in self.dag.getClusterMembers():
self.sendDiscoverMessage(address)
def queryMacTable(self):
''' this function will query the cluster state from the
forwarding table at startup of the deamon. This is
important if for any reason the sdk daemon is getting
restarted unexecptectly
'''
foundClusterMacAddresses = 0
for macKey in self.macTableMgr.mac_table_status_iter():
macEntry = self.macTableMgr.mac_entry_status(macKey)
if (macEntry.eth_addr().to_string()
in self.forwardingTable and
macEntry.vlan_id() == self.dag.getMonitorVlan()):
foundClusterMacAddresses += 1
self.forwardingTable[macEntry.eth_addr().to_string()]=\
macEntry.intf().to_string()
msg = 'updating entry for %s ' % (macEntry.to_string())
self.logger.log(msg)
if (foundClusterMacAddresses < len(self.forwardingTable)):
raise f5ClusterMacAddressesNotFound()
return True
def updateControllerStatus(self, status):
''' convinience function '''
if (status == 'connected'):
status = 'connected on ' + str(time.time())
self.agentMgr.status_set('Controller', status)
return True
def on_initialized(self):
''' callback which will be executed after the SDK initalized
registers the Honeybee cluster virtual mac addresses to the
on_mac_entry_set callback
'''
for fwdTableEntry in self.forwardingTable:
msg = 'registering mac %s for on_mac_entry_set' % fwdTableEntry
self.logger.log(msg)
self.watch_mac_entry(eossdk.MacKey(self.dag.getMonitorVlan(),
eossdk.EthAddr(fwdTableEntry)),
True)
self.queryMacTable()
self.agentMgr.status_set('Version', 'Honeybee V1')
configTmosURL = self.agentMgr.agent_option('tmosUrl')
if configTmosURL:
self.tmosUrl = configTmosURL
configSyncInterval = self.agentMgr.agent_option('syncInterval')
if configSyncInterval:
self.syncInterval = configSyncInterval
self.updateControllerStatus('initializing...')
if (self.dag.syncDirectflows(self.forwardingTable, True)):
self.updateControllerStatus('connected')
def on_agent_option(self, optionName, value):
''' callback for Agent option changes'''
if (value):
if (optionName == 'tmosUrl'):
self.tmosUrl = value
self.dag.setUrl(self.tmosUrl)
elif(optionName == 'syncInterval'):
self.syncInterval = value
def on_mac_entry_set(self, entry):
''' callback when the cluster mac address change
calls the dag to update the flow tables
'''
msg = '%s moved to port %s ' % \
(entry.eth_addr().to_string(),
entry.intf().to_string())
self.logger.log(msg)
self.forwardingTable[entry.eth_addr().to_string()] = \
entry.intf().to_string()
self.dag.syncDirectflows(self.forwardingTable, False)
def on_sync(self):
''' sync invoked by the sheduler'''
# this is the periodic call
if (self.dag.syncDirectflows(self.forwardingTable, True)):
self.updateControllerStatus('connected')
else:
self.updateControllerStatus('ERROR')
class f5ClusterMacAddressesNotFound(Exception):
''' custom defined Exeption which gets
raised at startup when the cluster node
port connection cannot be learned from the
MAC forwarding table. This usually is caused
by a wiring problem
'''
def __str__(self):
return 'MAC forwarding table does not contain cluster addresses'
class Logmsg(object):
''' Wrapper to sent to stdout '''
def __init__(self, name):
self.tracer = eossdk.Tracer(name)
def log(self, msg):
self.tracer.trace0(msg)
print msg
class UpdateSheduler(eossdk.TimeoutHandler):
''' Sheduler Object from the eossdk '''
def __init__(self, sdk, macHandler, poll_interval):
self._sdk = sdk
self._poll_interval = poll_interval
self._macHandler = macHandler
eossdk.TimeoutHandler.__init__(self, self._sdk.get_timeout_mgr())
self.timeout_time_is(eossdk.now() + self._poll_interval)
def poll(self):
self._macHandler.on_sync()
def on_timeout(self):
self.poll()
self.timeout_time_is(eossdk.now() + self._poll_interval)
def main(args):
''' Instanciating the Mac Handler and Disaggregator Object'''
sdk = eossdk.Sdk()
macHandler = MacHandler(sdk)
sheduler = UpdateSheduler(sdk, macHandler, macHandler.getSyncInterval())
sdk.main_loop(sys.argv)
if __name__ == '__main__':
sys.exit(main(sys.argv))