From 3a4d33225a226518f4bd4c7adfd4fcc475073529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Wed, 12 Apr 2023 13:29:05 -0600 Subject: [PATCH 01/40] Update LibreQoS.py --- src/LibreQoS.py | 366 ++++-------------------------------------------- 1 file changed, 30 insertions(+), 336 deletions(-) diff --git a/src/LibreQoS.py b/src/LibreQoS.py index 93fb90b9..d3b43a48 100755 --- a/src/LibreQoS.py +++ b/src/LibreQoS.py @@ -18,6 +18,7 @@ import argparse import logging import shutil import binpacking +from deepdiff import DeepDiff from ispConfig import sqm, upstreamBandwidthCapacityDownloadMbps, upstreamBandwidthCapacityUploadMbps, \ interfaceA, interfaceB, enableActualShellCommands, useBinPackingToBalanceCPU, monitorOnlyMode, \ @@ -917,14 +918,7 @@ def refreshShapersUpdateOnly(): else: print("ERROR: lqosd is not running. Aborting") os._exit(-1) - - # Check that we aren't running LibreQoS.py more than once at a time - if is_libre_already_running(): - print("LibreQoS.py is already running in another process. Aborting.") - os._exit(-1) - - # We've got this far, so create a lock file - create_lock_file() + # Starting print("refreshShapers starting at " + datetime.now().strftime("%d/%m/%Y %H:%M:%S")) @@ -943,35 +937,27 @@ def refreshShapersUpdateOnly(): # Check validation safeToRunRefresh = False if (validateNetworkAndDevices() == True): - shutil.copyfile('ShapedDevices.csv', 'lastGoodConfig.csv') - shutil.copyfile('network.json', 'lastGoodConfig.json') - print("Backed up good config as lastGoodConfig.csv and lastGoodConfig.json") safeToRunRefresh = True else: warnings.warn("Validation failed. Will now exit.", stacklevel=2) if safeToRunRefresh == True: - - # Load queuingStructure - with open('queuingStructure.json', 'r') as infile: - queuingStructure = json.loads(infile.read()) - - - network = queuingStructure['Network'] - lastUsedClassIDCounterByCPU = queuingStructure['lastUsedClassIDCounterByCPU'] - generatedPNs = queuingStructure['generatedPNs'] - + networkChanged = False + devicesChanged = False + # Check for changes to network.json + if os.path.isfile('lastGoodConfig.json'): + with open('lastGoodConfig.json', 'r') as j: + originalNetwork = json.loads(j.read()) + with open('network.json', 'r') as j: + newestNetwork = json.loads(j.read()) + ddiff = DeepDiff(originalNetwork, newestNetwork, ignore_order=True) + if ddiff != {}: + networkChanged = True + + # Check for changes to ShapedDevices.csv newlyUpdatedSubscriberCircuits, newlyUpdatedDictForCircuitsWithoutParentNodes = loadSubscriberCircuits('ShapedDevices.csv') lastLoadedSubscriberCircuits, lastLoadedDictForCircuitsWithoutParentNodes = loadSubscriberCircuits('ShapedDevices.lastLoaded.csv') - - # Load stats files - with open('statsByParentNode.json', 'r') as j: - parentNodes = json.loads(j.read()) - - with open('statsByCircuit.json', 'r') as j: - subscriberCircuits = json.loads(j.read()) - newlyUpdatedSubscriberCircuitsByID = {} for circuit in newlyUpdatedSubscriberCircuits: circuitid = circuit['circuitID'] @@ -982,327 +968,35 @@ def refreshShapersUpdateOnly(): circuitid = circuit['circuitID'] lastLoadedSubscriberCircuitsByID[circuitid] = circuit - - def removeDeviceIPsFromFilter(circuit): - for device in circuit['devices']: - for ipv4 in device['ipv4s']: - #shell('./bin/xdp_iphash_to_cpu_cmdline del ip ' + str(ipv4)) - delete_ip_mapping(str(ipv4)) - for ipv6 in device['ipv6s']: - #shell('./bin/xdp_iphash_to_cpu_cmdline del ip ' + str(ipv6)) - delete_ip_mapping(str(ipv6)) - - - def addDeviceIPsToFilter(circuit, cpuNumHex, classId, upload = False): - # `lqosd` expects the CPU and ClassIDs to be in Hex. - # `ClassId` wasn't present in the circuit data, so it's now a required parameter. - for device in circuit['devices']: - for ipv4 in device['ipv4s']: - #shell('./bin/xdp_iphash_to_cpu_cmdline add --ip ' + str(ipv4) + ' --cpu ' + cpuNumHex + ' --classid ' + circuit['classid']) - add_ip_mapping(str(ipv4), classId, str(cpuNumHex), upload) - for ipv6 in device['ipv6s']: - #shell('./bin/xdp_iphash_to_cpu_cmdline add --ip ' + str(ipv6) + ' --cpu ' + cpuNumHex + ' --classid ' + circuit['classid']) - add_ip_mapping(str(ipv6), classId, str(cpuNumHex), upload) - - - def getAllParentNodes(data, allParentNodes): - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - if thisParentNode not in allParentNodes: - allParentNodes.append(thisParentNode) - if 'children' in data[node]: - result = getAllParentNodes(data[node]['children'], allParentNodes) - for entry in result: - if entry not in allParentNodes: - allParentNodes.append(entry) - return allParentNodes - allParentNodes = getAllParentNodes(network, []) - - - def getClassIDofParentNodes(data, classIDOfParentNodes): - revisedClassIDOfParentNodes = classIDOfParentNodes - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - classIDOfParentNodes[thisParentNode] = data[node]['classid'] - if 'children' in data[node]: - result = getClassIDofParentNodes(data[node]['children'], revisedClassIDOfParentNodes) - revisedClassIDOfParentNodes.update(result) - return revisedClassIDOfParentNodes - classIDOfParentNodes = getClassIDofParentNodes(network, {}) - - - def getAllCircuitIDs(data, allCircuitIDs): - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - if 'circuits' in data[node]: - for circuit in data[node]['circuits']: - if circuit['circuitID'] not in allCircuitIDs: - allCircuitIDs.append(circuit['circuitID']) - if 'children' in data[node]: - result = getAllCircuitIDs(data[node]['children'], allCircuitIDs) - for entry in result: - if entry not in allCircuitIDs: - allCircuitIDs.append(result) - return allCircuitIDs - allCircuitIDs = getAllCircuitIDs(network, []) - - - def getClassIDofExistingCircuitID(data, classIDofExistingCircuitID): - revisedClassIDofExistingCircuitID = classIDofExistingCircuitID - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - if 'circuits' in data[node]: - for circuit in data[node]['circuits']: - revisedClassIDofExistingCircuitID[circuit['circuitID']] = circuit['classid'] - if 'children' in data[node]: - result = getClassIDofExistingCircuitID(data[node]['children'], revisedClassIDofExistingCircuitID) - revisedClassIDofExistingCircuitID.update(result) - return revisedClassIDofExistingCircuitID - classIDofExistingCircuitID = getClassIDofExistingCircuitID(network, {}) - - - def getParentNodeOfCircuitID(data, parentNodeOfCircuitID, allCircuitIDs): - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - if 'circuits' in data[node]: - for circuit in data[node]['circuits']: - parentNodeOfCircuitID[circuit['circuitID']] = thisParentNode - if 'children' in data[node]: - result = getParentNodeOfCircuitID(data[node]['children'], parentNodeOfCircuitID, allCircuitIDs) - parentNodeOfCircuitID.update(result) - return parentNodeOfCircuitID - parentNodeOfCircuitID = getParentNodeOfCircuitID(network, {}, allCircuitIDs) - - - def getCPUnumOfParentNodes(data, cpuNumOfParentNode): - revisedCpuNumOfParentNode = cpuNumOfParentNode - for node in data: - if isinstance(node, str): - if (isinstance(data[node], dict)) and (node != 'children'): - thisParentNode = node - revisedCpuNumOfParentNode[thisParentNode] = data[node]['cpuNum'] - if 'children' in data[node]: - result = getCPUnumOfParentNodes(data[node]['children'], revisedCpuNumOfParentNode) - revisedCpuNumOfParentNode.update(result) - return revisedCpuNumOfParentNode - - cpuNumOfParentNodeHex = getCPUnumOfParentNodes(network, {}) - cpuNumOfParentNodeInt = {} - for key, value in cpuNumOfParentNodeHex.items(): - cpuNumOfParentNodeInt[key] = int(value,16) + 1 - - - def addCircuitHTBandQdisc(circuit, parentNodeClassID): - minor = circuit['classid'].split(':')[1] - for interface in [interfaceA, interfaceB]: - if interface == interfaceA: - rate = str(circuit['minDownload']) - ceil = str(circuit['maxDownload']) - else: - rate = str(circuit['minUpload']) - ceil = str(circuit['maxUpload']) - command = 'tc class add dev ' + interface + ' parent ' + parentNodeClassID + ' classid ' + minor + ' htb rate ' + rate + 'Mbit ceil ' + ceil + 'Mbit' - print(command) - shell(command) - # Only add CAKE / fq_codel qdisc if monitorOnlyMode is Off - if monitorOnlyMode == False: - command = 'tc qdisc add dev ' + interface + ' parent ' + classID + ' ' + sqm - print(command) - shell(command) - - - def delHTBclass(classid): - for interface in [interfaceA, interfaceB]: - command = 'tc class del dev ' + interface + ' classid ' + classid - print(command) - shell(command) - - - generatedPNcounter = 1 - circuitsIDsToRemove = [] - circuitsToUpdateByID = {} - circuitsToAddByParentNode = {} for circuitID, circuit in lastLoadedSubscriberCircuitsByID.items(): - # Same circuit, update parameters (bandwidth, devices) - bandwidthChanged = False - devicesChanged = False if (circuitID in newlyUpdatedSubscriberCircuitsByID) and (circuitID in lastLoadedSubscriberCircuitsByID): if newlyUpdatedSubscriberCircuitsByID[circuitID]['maxDownload'] != lastLoadedSubscriberCircuitsByID[circuitID]['maxDownload']: - bandwidthChanged = True + devicesChanged = True if newlyUpdatedSubscriberCircuitsByID[circuitID]['maxUpload'] != lastLoadedSubscriberCircuitsByID[circuitID]['maxUpload']: - bandwidthChanged = True + devicesChanged = True if newlyUpdatedSubscriberCircuitsByID[circuitID]['minDownload'] != lastLoadedSubscriberCircuitsByID[circuitID]['minDownload']: - bandwidthChanged = True + devicesChanged = True if newlyUpdatedSubscriberCircuitsByID[circuitID]['minUpload'] != lastLoadedSubscriberCircuitsByID[circuitID]['minUpload']: - bandwidthChanged = True + devicesChanged = True if newlyUpdatedSubscriberCircuitsByID[circuitID]['devices'] != lastLoadedSubscriberCircuitsByID[circuitID]['devices']: devicesChanged = True - if bandwidthChanged == True: - if newlyUpdatedSubscriberCircuitsByID[circuitID]['ParentNode'] == lastLoadedSubscriberCircuitsByID[circuitID]['ParentNode']: - parentNodeActual = circuit['ParentNode'] - if parentNodeActual == 'none': - # In flat network, there's still a Parent Node (GENERATED_PN_X) so we need to use that here - parentNodeActual = parentNodeOfCircuitID[circuitID] - parentNodeClassID = classIDOfParentNodes[parentNodeActual] - classid = classIDofExistingCircuitID[circuitID] - minor = classid.split(':')[1] - for interface in [interfaceA, interfaceB]: - if interface == interfaceA: - rate = str(newlyUpdatedSubscriberCircuitsByID[circuitID]['minDownload']) - ceil = str(newlyUpdatedSubscriberCircuitsByID[circuitID]['maxDownload']) - else: - rate = str(newlyUpdatedSubscriberCircuitsByID[circuitID]['minUpload']) - ceil = str(newlyUpdatedSubscriberCircuitsByID[circuitID]['maxUpload']) - command = 'tc class change dev ' + interface + ' parent ' + parentNodeClassID + ' classid ' + minor + ' htb rate ' + rate + 'Mbit ceil ' + ceil + 'Mbit' - shell(command) - else: - removeDeviceIPsFromFilter(lastLoadedSubscriberCircuitsByID[circuitID]) - # Delete HTB class, qdisc. Then recreat it. - classID = classIDofExistingCircuitID[circuitID] - circuit['classid'] = classID - delHTBclass(classID) - parentNodeClassID = classIDOfParentNodes[parentNodeOfCircuitID[circuitID]] - addCircuitHTBandQdisc(circuit, parentNodeClassID) - cpuNumHex = cpuNumOfParentNodeHex[parentNodeActual] - addDeviceIPsToFilter(newlyUpdatedSubscriberCircuitsByID[circuitID], cpuNumHex, classID) - if devicesChanged: - parentNodeActual = circuit['ParentNode'] - if parentNodeActual == 'none': - # In flat network, there's still a Parent Node (GENERATED_PN_X) so we need to use that here - parentNodeActual = parentNodeOfCircuitID[circuitID] - cpuNumHex = cpuNumOfParentNodeHex[parentNodeActual] - parentNodeClassID = classIDOfParentNodes[parentNodeActual] - addDeviceIPsToFilter(newlyUpdatedSubscriberCircuitsByID[circuitID], cpuNumHex, parentNodeClassID) - - newlyUpdatedSubscriberCircuitsByID[circuitID]['classid'] = lastLoadedSubscriberCircuitsByID[circuitID]['classid'] - if (bandwidthChanged) or (devicesChanged): - circuitsToUpdateByID[circuitID] = newlyUpdatedSubscriberCircuitsByID[circuitID] - - - # Removed circuit - if (circuitID in lastLoadedSubscriberCircuitsByID) and (circuitID not in newlyUpdatedSubscriberCircuitsByID): - circuitsIDsToRemove.append(circuitID) - removeDeviceIPsFromFilter(lastLoadedSubscriberCircuitsByID[circuitID]) - classid = classIDofExistingCircuitID[circuitID] - delHTBclass(classid) - - - # New circuit + if newlyUpdatedSubscriberCircuitsByID[circuitID]['ParentNode'] != lastLoadedSubscriberCircuitsByID[circuitID]['ParentNode']: + devicesChanged = True + else: + devicesChanged = True for circuitID, circuit in newlyUpdatedSubscriberCircuitsByID.items(): - if (circuitID in newlyUpdatedSubscriberCircuitsByID) and (circuitID not in lastLoadedSubscriberCircuitsByID): - if newlyUpdatedSubscriberCircuitsByID[circuitID]['ParentNode'] == 'none': - newlyUpdatedSubscriberCircuitsByID[circuitID]['ParentNode'] = 'Generated_PN_' + str(generatedPNcounter) - generatedPNcounter += 1 - if generatedPNcounter > len(generatedPNs): - generatedPNcounter = 1 - cpuNumHex = cpuNumOfParentNodeHex[circuit['ParentNode']] - - cpuNumInt = cpuNumOfParentNodeInt[circuit['ParentNode']] - parentNodeClassID = classIDOfParentNodes[circuit['ParentNode']] - classID = parentNodeClassID.split(':')[0] + ':' + hex(lastUsedClassIDCounterByCPU[str(cpuNumInt)]) - lastUsedClassIDCounterByCPU[str(cpuNumInt)] = lastUsedClassIDCounterByCPU[str(cpuNumInt)] + 1 - circuit['classid'] = classID - # Add HTB class, qdisc - addCircuitHTBandQdisc(circuit, parentNodeClassID) - addDeviceIPsToFilter(circuit, cpuNumHex, parentNodeClassID) - if circuit['ParentNode'] in circuitsToAddByParentNode: - temp = circuitsToAddByParentNode[circuit['ParentNode']] - temp.append(circuit) - circuitsToAddByParentNode[circuit['ParentNode']] = temp - else: - temp = [] - temp.append(circuit) - circuitsToAddByParentNode[circuit['ParentNode']] = temp - - - # Update network structure reflecting new circuits, removals, and changes - itemsToChange = (circuitsIDsToRemove, circuitsToUpdateByID, circuitsToAddByParentNode) - def updateNetworkStructure(data, depth, itemsToChange): - circuitsIDsToRemove, circuitsToUpdateByID, circuitsToAddByParentNode = itemsToChange - parentNodesToAddCircuitsTo = [] - for thing in circuitsToAddByParentNode: - parentNodesToAddCircuitsTo.append(thing) - revisedData = data - for node in revisedData: - if isinstance(node, str): - if (isinstance(revisedData[node], dict)) and (node != 'children'): - thisParentNode = node - if 'circuits' in revisedData[node]: - for circuit in revisedData[node]['circuits']: - if circuit['circuitID'] in circuitsToUpdateByID: - circuit = circuitsToUpdateByID[circuit['circuitID']] - print('updated') - if circuit['circuitID'] in circuitsIDsToRemove: - revisedData[node]['circuits'].remove(circuit) - if thisParentNode in parentNodesToAddCircuitsTo: - if 'circuits' in revisedData[node]: - temp = revisedData[node]['circuits'] - for circuit in circuitsToAddByParentNode[thisParentNode]: - temp.append(circuit) - print('added ') - print(circuit) - revisedData[node]['circuits'] = temp - else: - temp = [] - for circuit in circuitsToAddByParentNode[thisParentNode]: - temp.append(circuit) - revisedData[node]['circuits'] = temp - if 'children' in revisedData[node]: - result = updateNetworkStructure(revisedData[node]['children'], depth+1, itemsToChange) - revisedData[node]['children'] = result - return revisedData - network = updateNetworkStructure(network, 0 , itemsToChange) - - - # Record start time of actual filter reload - reloadStartTime = datetime.now() + if (circuitID not in lastLoadedSubscriberCircuitsByID): + devicesChanged = True - # Record end time of all reload commands - reloadEndTime = datetime.now() - - - queuingStructure = {} - queuingStructure['Network'] = network - queuingStructure['lastUsedClassIDCounterByCPU'] = lastUsedClassIDCounterByCPU - queuingStructure['generatedPNs'] = generatedPNs - # Save queuingStructure - with open('queuingStructure.json', 'w') as infile: - json.dump(queuingStructure, infile, indent=4) - - - # copy ShapedDevices.csv and save as ShapedDevices.lastLoaded.csv and lastGoodConfig.csv - shutil.copyfile('ShapedDevices.csv', 'ShapedDevices.lastLoaded.csv') - shutil.copyfile('ShapedDevices.csv', 'lastGoodConfig.csv') - - - # Save for stats - with open('statsByCircuit.json', 'w') as f: - f.write(json.dumps(subscriberCircuits, indent=4)) - with open('statsByParentNode.json', 'w') as f: - f.write(json.dumps(parentNodes, indent=4)) - - - # Report reload time - reloadTimeSeconds = ((reloadEndTime - reloadStartTime).seconds) + (((reloadEndTime - reloadStartTime).microseconds) / 1000000) - print("Queue and IP filter partial reload completed in " + "{:g}".format(round(reloadTimeSeconds,1)) + " seconds") - + if devicesChanged or networkChanged: + print('Observed changes to ShapedDevices.csv or network.json. Applying full reload now') + refreshShapers() + else: + print('Observed no changes to ShapedDevices.csv or network.json. Leaving queues as is.') # Done print("refreshShapersUpdateOnly completed on " + datetime.now().strftime("%d/%m/%Y %H:%M:%S")) - - # Free the lock file - free_lock_file() if __name__ == '__main__': # Check that the host lqosd is running -- knotmirror 0.0.1 From c555801733a0c3845966c4903bcef8afd371433c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Wed, 12 Apr 2023 13:29:28 -0600 Subject: [PATCH 02/40] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 71d8b35f..ba497ca3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ schedule waitress setuptools >= 65.5.1 apscheduler +deepdiff -- knotmirror 0.0.1 From 855b83883196b86ed97859ba040c0fa91d241998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Wed, 12 Apr 2023 20:52:42 -0600 Subject: [PATCH 03/40] Update integrationUISP.py --- src/integrationUISP.py | 113 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 46 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 079c3b4d..2d607d89 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -147,19 +147,17 @@ def findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps): if device['overview']['downlinkCapacity'] is not None and device['overview']['uplinkCapacity'] is not None: download = int(device['overview']['downlinkCapacity']/ 1000000) upload = int(device['overview']['uplinkCapacity']/ 1000000) - else: - download = generatedPNDownloadMbps - upload = generatedPNUploadMbps - # Make sure to use half of reported bandwidth for AF60-LRs - if device['identification']['model'] == "AF60-LR": - download = int(download / 2) - upload = int(download / 2) - if device['identification']['site']['id'] in foundAirFibersBySite: - if (download > foundAirFibersBySite[device['identification']['site']['id']]['download']) or (upload > foundAirFibersBySite[device['identification']['site']['id']]['upload']): - foundAirFibersBySite[device['identification']['site']['id']]['download'] = download - foundAirFibersBySite[device['identification']['site']['id']]['upload'] = upload - else: - foundAirFibersBySite[device['identification']['site']['id']] = {'download': download, 'upload': upload} + # Make sure to use half of reported bandwidth for AF60/AF60-LRs + if (device['identification']['model'] == "AF60-LR") or (device['identification']['model'] == "AF60"): + download = int(download / 2) + upload = int(download / 2) + if device['identification']['site']['id'] in foundAirFibersBySite: + if (download > foundAirFibersBySite[device['identification']['site']['id']]['download']) or (upload > foundAirFibersBySite[device['identification']['site']['id']]['upload']): + foundAirFibersBySite[device['identification']['site']['id']]['download'] = download + foundAirFibersBySite[device['identification']['site']['id']]['upload'] = upload + #print(device['identification']['name'] + ' will override bandwidth for site ' + device['identification']['site']['name']) + else: + foundAirFibersBySite[device['identification']['site']['id']] = {'download': download, 'upload': upload} return foundAirFibersBySite def buildSiteList(sites, dataLinks): @@ -240,6 +238,34 @@ def loadRoutingOverrides(): #print(overrides) return overrides +def findNodesBranchedOffPtMP(siteList, dataLinks, sites): + nodeOffPtMP = {} + #print(siteList) + for site in sites: + id = site['identification']['id'] + name = site['identification']['name'] + type = site['identification']['type'] + if type == 'site': + #parent = findInSiteListById(siteList, id)['parent'] + if ('parent' in site['identification']) and (site['identification']['parent'] is not None): + parent = site['identification']['parent']['id'] + for link in dataLinks: + if link['from']['site']['identification']['id'] == parent: + if link['to']['site']['identification']['id'] == id: + if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': + if 'overview' in link['to']['device']: + if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): + if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): + # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit + download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) + upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) + nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], + 'parentName': link['from']['device']['identification']['name'], + 'download': download, + 'upload': upload + } + return nodeOffPtMP + def buildFullGraph(): # Attempts to build a full network graph, incorporating as much of the UISP # hierarchy as possible. @@ -253,18 +279,28 @@ def buildFullGraph(): dataLinks = uispRequest("data-links?siteLinksOnly=true") # Build Site Capacities + print("Compiling Site Bandwidths") siteBandwidth = buildSiteBandwidths() + print("Finding AP Capacities") findApCapacities(devices, siteBandwidth) - foundAirFibersBySite = findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps) - # Create a list of just network sites + print('Creating list of sites') siteList = buildSiteList(sites, dataLinks) rootSite = findInSiteList(siteList, uispSite) + print("Finding PtP Capacities") + foundAirFibersBySite = findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps) + print("Finding PtMP Capacities") + nodeOffPtMP = findNodesBranchedOffPtMP(siteList, dataLinks, sites) + + print('Creating list of route overrides') routeOverrides = loadRoutingOverrides() if rootSite is None: print("ERROR: Unable to find root site in UISP") return + print('Creating graph') walkGraphOutwards(siteList, rootSite, routeOverrides) + + # Debug code: dump the list of site parents # for s in siteList: # if s['parent'] == "": @@ -273,25 +309,8 @@ def buildFullGraph(): # p = findInSiteListById(siteList, s['parent'])['name'] # print(s['name'] + " (" + str(s['cost']) + ") <-- " + p) - # Find Nodes Connected By PtMP - nodeOffPtMP = {} - for site in sites: - id = site['identification']['id'] - name = site['identification']['name'] - type = site['identification']['type'] - parent = findInSiteListById(siteList, id)['parent'] - if type == 'site': - for link in dataLinks: - if link['from']['site'] is not None and link['from']['site']['identification']['id'] == parent: - if link['to']['site'] is not None and link['to']['site']['identification']['id'] == id: - if link['from']['device'] is not None and link['to']['device'] is not None and link['to']['device']['overview'] is not None and link['to']['device']['overview']['downlinkCapacity'] is not None and link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': - # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit - download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) - upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'download': download, - 'upload': upload - } + + print("Building Topology") net = NetworkGraph() # Add all sites and client sites @@ -312,21 +331,23 @@ def buildFullGraph(): match type: case "site": nodeType = NodeType.site - if id in nodeOffPtMP: - parent = nodeOffPtMP[id]['parent'] if name in siteBandwidth: # Use the CSV bandwidth values download = siteBandwidth[name]["download"] upload = siteBandwidth[name]["upload"] - elif id in nodeOffPtMP: - download = nodeOffPtMP[id]['download'] - upload = nodeOffPtMP[id]['upload'] - elif id in foundAirFibersBySite: - download = foundAirFibersBySite[id]['download'] - upload = foundAirFibersBySite[id]['upload'] - else: - # Add them just in case - siteBandwidth[name] = { + else: + # Use limits from foundAirFibersBySite + if id in foundAirFibersBySite: + download = foundAirFibersBySite[id]['download'] + upload = foundAirFibersBySite[id]['upload'] + #print('Site ' + name + ' will use bandwidth from foundAirFibersBySite.') + # Use limits from nodeOffPtMP only if they're greater than what is already set + if id in nodeOffPtMP: + if (nodeOffPtMP[id]['download'] >= download) or (nodeOffPtMP[id]['upload'] >= upload): + download = nodeOffPtMP[id]['download'] + upload = nodeOffPtMP[id]['upload'] + #print('Site ' + name + ' will use bandwidth from nodeOffPtMP.') + siteBandwidth[name] = { "download": download, "upload": upload} case default: nodeType = NodeType.client @@ -364,7 +385,7 @@ def buildFullGraph(): # TODO: Figure out Mikrotik IPv6? mac = device['identification']['mac'] - + net.addRawNode(NetworkNode(id=device['identification']['id'], displayName=device['identification'] ['name'], parentId=id, type=NodeType.device, ipv4=ipv4, ipv6=ipv6, mac=mac)) -- knotmirror 0.0.1 From 99c716de86e5f4e954d079b8dafe420874de20a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 08:48:00 -0600 Subject: [PATCH 04/40] Update integrationUISP.py --- src/integrationUISP.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 2d607d89..5b173341 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -250,20 +250,21 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites): if ('parent' in site['identification']) and (site['identification']['parent'] is not None): parent = site['identification']['parent']['id'] for link in dataLinks: - if link['from']['site']['identification']['id'] == parent: - if link['to']['site']['identification']['id'] == id: - if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': - if 'overview' in link['to']['device']: - if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): - if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): - # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit - download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) - upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'parentName': link['from']['device']['identification']['name'], - 'download': download, - 'upload': upload - } + if link['to']['site']['identification'] is not None: + if link['from']['site']['identification']['id'] == parent: + if link['to']['site']['identification']['id'] == id: + if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': + if 'overview' in link['to']['device']: + if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): + if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): + # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit + download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) + upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) + nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], + 'parentName': link['from']['device']['identification']['name'], + 'download': download, + 'upload': upload + } return nodeOffPtMP def buildFullGraph(): -- knotmirror 0.0.1 From 8f55f4ac4ebfa8d905c15e6eb57ab3aa87c49947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 08:51:53 -0600 Subject: [PATCH 05/40] Update integrationUISP.py --- src/integrationUISP.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 5b173341..f3fc80a0 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -250,21 +250,22 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites): if ('parent' in site['identification']) and (site['identification']['parent'] is not None): parent = site['identification']['parent']['id'] for link in dataLinks: - if link['to']['site']['identification'] is not None: - if link['from']['site']['identification']['id'] == parent: - if link['to']['site']['identification']['id'] == id: - if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': - if 'overview' in link['to']['device']: - if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): - if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): - # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit - download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) - upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'parentName': link['from']['device']['identification']['name'], - 'download': download, - 'upload': upload - } + if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): + if ('identification' in link['from']['site']) and (link['from']['site']['identification'] is not None): + if link['from']['site']['identification']['id'] == parent: + if link['to']['site']['identification']['id'] == id: + if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': + if 'overview' in link['to']['device']: + if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): + if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): + # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit + download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) + upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) + nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], + 'parentName': link['from']['device']['identification']['name'], + 'download': download, + 'upload': upload + } return nodeOffPtMP def buildFullGraph(): -- knotmirror 0.0.1 From ec2ed8e4ad4c68db25b0f9f422afe387f52b09bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 09:03:30 -0600 Subject: [PATCH 06/40] Update integrationUISP.py --- src/integrationUISP.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index f3fc80a0..065fa937 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -250,22 +250,23 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites): if ('parent' in site['identification']) and (site['identification']['parent'] is not None): parent = site['identification']['parent']['id'] for link in dataLinks: - if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): - if ('identification' in link['from']['site']) and (link['from']['site']['identification'] is not None): - if link['from']['site']['identification']['id'] == parent: - if link['to']['site']['identification']['id'] == id: - if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': - if 'overview' in link['to']['device']: - if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): - if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): - # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit - download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) - upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'parentName': link['from']['device']['identification']['name'], - 'download': download, - 'upload': upload - } + if (link['to']['site'] is not None) and (link['to']['site']['identification'] is not None): + if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): + if ('identification' in link['from']['site']) and (link['from']['site']['identification'] is not None): + if link['from']['site']['identification']['id'] == parent: + if link['to']['site']['identification']['id'] == id: + if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': + if 'overview' in link['to']['device']: + if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): + if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): + # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit + download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) + upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) + nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], + 'parentName': link['from']['device']['identification']['name'], + 'download': download, + 'upload': upload + } return nodeOffPtMP def buildFullGraph(): -- knotmirror 0.0.1 From 942e4db89d25b3832e391ab8606b28aa7f230050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 09:28:59 -0600 Subject: [PATCH 07/40] Update integrationUISP.py --- src/integrationUISP.py | 52 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 065fa937..6828733c 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -238,35 +238,37 @@ def loadRoutingOverrides(): #print(overrides) return overrides -def findNodesBranchedOffPtMP(siteList, dataLinks, sites): +def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite): nodeOffPtMP = {} - #print(siteList) for site in sites: id = site['identification']['id'] name = site['identification']['name'] type = site['identification']['type'] - if type == 'site': - #parent = findInSiteListById(siteList, id)['parent'] - if ('parent' in site['identification']) and (site['identification']['parent'] is not None): - parent = site['identification']['parent']['id'] - for link in dataLinks: - if (link['to']['site'] is not None) and (link['to']['site']['identification'] is not None): - if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): - if ('identification' in link['from']['site']) and (link['from']['site']['identification'] is not None): - if link['from']['site']['identification']['id'] == parent: - if link['to']['site']['identification']['id'] == id: - if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': - if 'overview' in link['to']['device']: - if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): - if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): - # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit - download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) - upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'parentName': link['from']['device']['identification']['name'], - 'download': download, - 'upload': upload - } + if id != rootSite['id']: + trueParent = findInSiteListById(siteList, id)['parent'] + if type == 'site': + #parent = findInSiteListById(siteList, id)['parent'] + if ('parent' in site['identification']) and (site['identification']['parent'] is not None): + parent = site['identification']['parent']['id'] + for link in dataLinks: + if (link['to']['site'] is not None) and (link['to']['site']['identification'] is not None): + if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): + if ('identification' in link['from']['site']) and (link['from']['site']['identification'] is not None): + # Respect parent defined by topology and overrides + if link['from']['site']['identification']['id'] == trueParent: + if link['to']['site']['identification']['id'] == id: + if link['from']['device']['overview']['wirelessMode'] == 'ap-ptmp': + if 'overview' in link['to']['device']: + if ('downlinkCapacity' in link['to']['device']['overview']) and ('uplinkCapacity' in link['to']['device']['overview']): + if (link['to']['device']['overview']['downlinkCapacity'] is not None) and (link['to']['device']['overview']['uplinkCapacity'] is not None): + # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit + download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) + upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) + nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], + 'parentName': link['from']['device']['identification']['name'], + 'download': download, + 'upload': upload + } return nodeOffPtMP def buildFullGraph(): @@ -334,6 +336,8 @@ def buildFullGraph(): match type: case "site": nodeType = NodeType.site + if id in nodeOffPtMP: + parent = nodeOffPtMP[id]['parent'] if name in siteBandwidth: # Use the CSV bandwidth values download = siteBandwidth[name]["download"] -- knotmirror 0.0.1 From 25ffa131b308ed31757a13f23b75a55b98f798da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 09:32:50 -0600 Subject: [PATCH 08/40] Update integrationUISP.py --- src/integrationUISP.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 6828733c..e67761bc 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -294,8 +294,6 @@ def buildFullGraph(): rootSite = findInSiteList(siteList, uispSite) print("Finding PtP Capacities") foundAirFibersBySite = findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps) - print("Finding PtMP Capacities") - nodeOffPtMP = findNodesBranchedOffPtMP(siteList, dataLinks, sites) print('Creating list of route overrides') routeOverrides = loadRoutingOverrides() @@ -305,6 +303,8 @@ def buildFullGraph(): print('Creating graph') walkGraphOutwards(siteList, rootSite, routeOverrides) + print("Finding PtMP Capacities") + nodeOffPtMP = findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite) # Debug code: dump the list of site parents # for s in siteList: -- knotmirror 0.0.1 From e5d70d4e7a0f07c406d3e551511ef46c1c7cb060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 19:32:00 -0600 Subject: [PATCH 09/40] Update integrationUISP.py --- src/integrationUISP.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index e67761bc..5f834d85 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -131,11 +131,23 @@ def findApCapacities(devices, siteBandwidth): if device['identification']['role'] == "ap": name = device['identification']['name'] if not name in siteBandwidth and device['overview']['downlinkCapacity'] and device['overview']['uplinkCapacity']: + safeToUse = True download = int(device['overview'] ['downlinkCapacity'] / 1000000) upload = int(device['overview']['uplinkCapacity'] / 1000000) - siteBandwidth[device['identification']['name']] = { - "download": download, "upload": upload} + if download < 15: + print("WARNING: Device '" + device['identification']['hostname'] + "' has unusually low download capacity (" + str(upload) + " Mbps). Discarding in favor of parent site rates.") + safeToUse = False + if upload < 15: + print("WARNING: Device '" + device['identification']['hostname'] + "' has unusually low upload capacity (" + str(download) + " Mbps). Discarding in favor of parent site rates.") + safeToUse = False + if device['identification']['model'] == 'WaveAP': + if (download < 500) or (upload < 500): + download = 2450 + upload = 2450 + if safeToUse: + siteBandwidth[device['identification']['name']] = { + "download": download, "upload": upload} def findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps): foundAirFibersBySite = {} -- knotmirror 0.0.1 From 5f3079953865426508cd92a0f640f0167fba6152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Thu, 13 Apr 2023 20:16:33 -0600 Subject: [PATCH 10/40] Add files via upload --- src/csvToNetworkJSON.py | 58 ++++++++++++++++++++++++++++++++++ src/manualNetwork.template.csv | 4 +++ 2 files changed, 62 insertions(+) create mode 100644 src/csvToNetworkJSON.py create mode 100644 src/manualNetwork.template.csv diff --git a/src/csvToNetworkJSON.py b/src/csvToNetworkJSON.py new file mode 100644 index 00000000..b5e2de76 --- /dev/null +++ b/src/csvToNetworkJSON.py @@ -0,0 +1,58 @@ +from pythonCheck import checkPythonVersion +checkPythonVersion() +import os +import csv +import json +from ispConfig import uispSite, uispStrategy, overwriteNetworkJSONalways +from ispConfig import generatedPNUploadMbps, generatedPNDownloadMbps, upstreamBandwidthCapacityDownloadMbps, upstreamBandwidthCapacityUploadMbps +from integrationCommon import NetworkGraph, NetworkNode, NodeType + +def csvToNetworkJSONfile(): + sites = [] + with open('manualNetwork.csv') as csv_file: + csv_reader = csv.reader(csv_file, delimiter=',') + for row in csv_reader: + if 'Site Name' in row[0]: + header = row + else: + name, down, up, parent = row + site = {'name': name, + 'download': down, + 'upload': up, + 'parent': parent} + sites.append(site) + + net = NetworkGraph() + idCounter = 1000 + nameToID = {} + for site in sites: + site['id'] = idCounter + idCounter = idCounter + 1 + nameToID[site['name']] = site['id'] + for site in sites: + id = site['id'] + if site['parent'] == '': + parentID = None + else: + parentID = nameToID[site['parent']] + name = site['name'] + parent = site['parent'] + download = site['download'] + upload = site['upload'] + nodeType = NodeType.site + node = NetworkNode(id=id, displayName=name, type=nodeType, + parentId=parentID, download=download, upload=upload, address=None, customerName=None) + net.addRawNode(node) + net.prepareTree() + net.plotNetworkGraph(False) + if net.doesNetworkJsonExist(): + if overwriteNetworkJSONalways: + net.createNetworkJson() + else: + print("network.json already exists and overwriteNetworkJSONalways set to False. Leaving in-place.") + else: + net.createNetworkJson() + net.createShapedDevices() + +if __name__ == '__main__': + csvToNetworkJSONfile() diff --git a/src/manualNetwork.template.csv b/src/manualNetwork.template.csv new file mode 100644 index 00000000..79619eee --- /dev/null +++ b/src/manualNetwork.template.csv @@ -0,0 +1,4 @@ +Site Name,Download Mbps,Upload Mbps,Parent Site Name +SiteA,1000,1000, +SiteB,975,975,SiteA +SiteC,950,950,SiteB -- knotmirror 0.0.1 From 3bb8a3015215f11ea5b91db0c555e4705244b17b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 13:24:12 -0600 Subject: [PATCH 11/40] Update integrationUISP.py --- src/integrationUISP.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 5f834d85..5cbb4e68 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -295,6 +295,26 @@ def buildFullGraph(): devices = uispRequest("devices?withInterfaces=true&authorized=true") dataLinks = uispRequest("data-links?siteLinksOnly=true") + # If multiple Internet-connected sites, create Internet root node: + internetConnectedSites = [] + for link in dataLinks: + if link['canDelete'] == False: + if link['from']['device']['identification']['id'] == link['to']['device']['identification']['id']: + siteID = link['from']['site']['identification']['id'] + # Found internet link + internetConnectedSites.append(siteID) + if len(internetConnectedSites) > 1: + internetSite = {'id': '001', 'identification': {'id': '001', 'status': 'active', 'name': 'Internet', 'parent': None, 'type': 'site'}} + sites.append(internetSite) + uispSite = 'Internet' + for link in dataLinks: + if link['canDelete'] == False: + if link['from']['device']['identification']['id'] == link['to']['device']['identification']['id']: + link['from']['site']['identification']['id'] = '001' + link['from']['site']['identification']['name'] = 'Internet' + # Found internet link + internetConnectedSites.append(siteID) + # Build Site Capacities print("Compiling Site Bandwidths") siteBandwidth = buildSiteBandwidths() -- knotmirror 0.0.1 From 13f0df7e0c3b76cac03f646eaf001c5bc87bff3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 13:26:28 -0600 Subject: [PATCH 12/40] Update integrationUISP.py --- src/integrationUISP.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 5cbb4e68..ad1d30d9 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -287,7 +287,7 @@ def buildFullGraph(): # Attempts to build a full network graph, incorporating as much of the UISP # hierarchy as possible. from integrationCommon import NetworkGraph, NetworkNode, NodeType - from ispConfig import generatedPNUploadMbps, generatedPNDownloadMbps + from ispConfig import uispSite, generatedPNUploadMbps, generatedPNDownloadMbps # Load network sites print("Loading Data from UISP") -- knotmirror 0.0.1 From 2d3ff07f9e1278cc9da50c863b770733c1a50fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 14:45:13 -0600 Subject: [PATCH 13/40] Update integrationUISP.py --- src/integrationUISP.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index ad1d30d9..3100f33f 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -283,19 +283,7 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite): } return nodeOffPtMP -def buildFullGraph(): - # Attempts to build a full network graph, incorporating as much of the UISP - # hierarchy as possible. - from integrationCommon import NetworkGraph, NetworkNode, NodeType - from ispConfig import uispSite, generatedPNUploadMbps, generatedPNDownloadMbps - - # Load network sites - print("Loading Data from UISP") - sites = uispRequest("sites") - devices = uispRequest("devices?withInterfaces=true&authorized=true") - dataLinks = uispRequest("data-links?siteLinksOnly=true") - - # If multiple Internet-connected sites, create Internet root node: +def handleMultipleInternetNodes(sites, dataLinks, uispSite): internetConnectedSites = [] for link in dataLinks: if link['canDelete'] == False: @@ -314,6 +302,22 @@ def buildFullGraph(): link['from']['site']['identification']['name'] = 'Internet' # Found internet link internetConnectedSites.append(siteID) + return(sites, dataLinks, uispSite) + +def buildFullGraph(): + # Attempts to build a full network graph, incorporating as much of the UISP + # hierarchy as possible. + from integrationCommon import NetworkGraph, NetworkNode, NodeType + from ispConfig import uispSite, generatedPNUploadMbps, generatedPNDownloadMbps + + # Load network sites + print("Loading Data from UISP") + sites = uispRequest("sites") + devices = uispRequest("devices?withInterfaces=true&authorized=true") + dataLinks = uispRequest("data-links?siteLinksOnly=true") + + # If multiple Internet-connected sites, create Internet root node: + sites, dataLinks, uispSite = handleMultipleInternetNodes(sites, dataLinks, uispSite) # Build Site Capacities print("Compiling Site Bandwidths") -- knotmirror 0.0.1 From 5c5f9ee1e596fb7c8251aa98a1fa55e1d2c402bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 16:00:22 -0600 Subject: [PATCH 14/40] Update integrationUISP.py --- src/integrationUISP.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 3100f33f..43aaad27 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -250,18 +250,17 @@ def loadRoutingOverrides(): #print(overrides) return overrides -def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite): +def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite, foundAirFibersBySite): nodeOffPtMP = {} - for site in sites: - id = site['identification']['id'] - name = site['identification']['name'] - type = site['identification']['type'] + for site in siteList: + id = site['id'] + name = site['name'] if id != rootSite['id']: - trueParent = findInSiteListById(siteList, id)['parent'] - if type == 'site': + if id not in foundAirFibersBySite: + trueParent = findInSiteListById(siteList, id)['parent'] #parent = findInSiteListById(siteList, id)['parent'] - if ('parent' in site['identification']) and (site['identification']['parent'] is not None): - parent = site['identification']['parent']['id'] + if site['parent'] is not None: + parent = site['parent'] for link in dataLinks: if (link['to']['site'] is not None) and (link['to']['site']['identification'] is not None): if ('identification' in link['to']['site']) and (link['to']['site']['identification'] is not None): @@ -276,12 +275,12 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite): # Capacity of the PtMP client radio feeding the PoP will be used as the site bandwidth limit download = int(round(link['to']['device']['overview']['downlinkCapacity']/1000000)) upload = int(round(link['to']['device']['overview']['uplinkCapacity']/1000000)) - nodeOffPtMP[id] = { 'parent': link['from']['device']['identification']['id'], - 'parentName': link['from']['device']['identification']['name'], - 'download': download, + nodeOffPtMP[id] = {'download': download, 'upload': upload } - return nodeOffPtMP + #print('Site ' + name + ' will use PtMP AP as parent.') + site['parent'] = parent + return siteList, nodeOffPtMP def handleMultipleInternetNodes(sites, dataLinks, uispSite): internetConnectedSites = [] @@ -302,6 +301,7 @@ def handleMultipleInternetNodes(sites, dataLinks, uispSite): link['from']['site']['identification']['name'] = 'Internet' # Found internet link internetConnectedSites.append(siteID) + print("Multiple Internet links detected. Will create 'Internet' root node.") return(sites, dataLinks, uispSite) def buildFullGraph(): @@ -330,7 +330,6 @@ def buildFullGraph(): rootSite = findInSiteList(siteList, uispSite) print("Finding PtP Capacities") foundAirFibersBySite = findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps) - print('Creating list of route overrides') routeOverrides = loadRoutingOverrides() if rootSite is None: @@ -338,9 +337,8 @@ def buildFullGraph(): return print('Creating graph') walkGraphOutwards(siteList, rootSite, routeOverrides) - print("Finding PtMP Capacities") - nodeOffPtMP = findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite) + siteList, nodeOffPtMP = findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite, foundAirFibersBySite) # Debug code: dump the list of site parents # for s in siteList: -- knotmirror 0.0.1 From 96577962705b7081c958ffe4cef3fa8cda3d3e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 16:02:21 -0600 Subject: [PATCH 15/40] Update integrationUISP.py --- src/integrationUISP.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 43aaad27..d837ab6b 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -370,8 +370,6 @@ def buildFullGraph(): match type: case "site": nodeType = NodeType.site - if id in nodeOffPtMP: - parent = nodeOffPtMP[id]['parent'] if name in siteBandwidth: # Use the CSV bandwidth values download = siteBandwidth[name]["download"] @@ -381,13 +379,13 @@ def buildFullGraph(): if id in foundAirFibersBySite: download = foundAirFibersBySite[id]['download'] upload = foundAirFibersBySite[id]['upload'] - #print('Site ' + name + ' will use bandwidth from foundAirFibersBySite.') - # Use limits from nodeOffPtMP only if they're greater than what is already set - if id in nodeOffPtMP: - if (nodeOffPtMP[id]['download'] >= download) or (nodeOffPtMP[id]['upload'] >= upload): - download = nodeOffPtMP[id]['download'] - upload = nodeOffPtMP[id]['upload'] - #print('Site ' + name + ' will use bandwidth from nodeOffPtMP.') + # If no airFibers were found, and node originates off PtMP, treat as child node of that PtMP AP + else: + if id in nodeOffPtMP: + if (nodeOffPtMP[id]['download'] >= download) or (nodeOffPtMP[id]['upload'] >= upload): + download = nodeOffPtMP[id]['download'] + upload = nodeOffPtMP[id]['upload'] + siteBandwidth[name] = { "download": download, "upload": upload} case default: -- knotmirror 0.0.1 From eced3e2ac588c67e33e7bb2f6394da53e75c27dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Fri, 14 Apr 2023 23:56:08 -0600 Subject: [PATCH 16/40] Update integrationUISP.py --- src/integrationUISP.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index d837ab6b..79458db4 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -160,9 +160,9 @@ def findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps): download = int(device['overview']['downlinkCapacity']/ 1000000) upload = int(device['overview']['uplinkCapacity']/ 1000000) # Make sure to use half of reported bandwidth for AF60/AF60-LRs - if (device['identification']['model'] == "AF60-LR") or (device['identification']['model'] == "AF60"): - download = int(download / 2) - upload = int(download / 2) + #if (device['identification']['model'] == "AF60-LR") or (device['identification']['model'] == "AF60"): + # download = int(download / 2) + # upload = int(download / 2) if device['identification']['site']['id'] in foundAirFibersBySite: if (download > foundAirFibersBySite[device['identification']['site']['id']]['download']) or (upload > foundAirFibersBySite[device['identification']['site']['id']]['upload']): foundAirFibersBySite[device['identification']['site']['id']]['download'] = download -- knotmirror 0.0.1 From 3d6d32c300bbee686b55fb0b1cd774dd0fc8d3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 09:42:03 -0600 Subject: [PATCH 17/40] Update integrationUISP.py --- src/integrationUISP.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 79458db4..3850608b 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -159,10 +159,10 @@ def findAirfibers(devices, generatedPNDownloadMbps, generatedPNUploadMbps): if device['overview']['downlinkCapacity'] is not None and device['overview']['uplinkCapacity'] is not None: download = int(device['overview']['downlinkCapacity']/ 1000000) upload = int(device['overview']['uplinkCapacity']/ 1000000) - # Make sure to use half of reported bandwidth for AF60/AF60-LRs - #if (device['identification']['model'] == "AF60-LR") or (device['identification']['model'] == "AF60"): - # download = int(download / 2) - # upload = int(download / 2) + # Make sure to factor in gigabit port for AF60/AF60-LRs + if (device['identification']['model'] == "AF60-LR") or (device['identification']['model'] == "AF60"): + download = min(download,950) + upload = min(upload,950) if device['identification']['site']['id'] in foundAirFibersBySite: if (download > foundAirFibersBySite[device['identification']['site']['id']]['download']) or (upload > foundAirFibersBySite[device['identification']['site']['id']]['upload']): foundAirFibersBySite[device['identification']['site']['id']]['download'] = download -- knotmirror 0.0.1 From c62f6d3e3cd620fe0bcefb97e2f96371c69c3c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 09:44:50 -0600 Subject: [PATCH 18/40] Fix https://github.com/LibreQoE/LibreQoS/issues/309 --- src/integrationCommon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrationCommon.py b/src/integrationCommon.py index 6e89c3c9..665af88a 100644 --- a/src/integrationCommon.py +++ b/src/integrationCommon.py @@ -437,5 +437,5 @@ class NetworkGraph: if (self.nodes[child].type != NodeType.client and self.nodes[child].type != NodeType.device) or showClients: dot.edge("N" + str(i), "N" + str(child)) - dot.render("network.pdf") + dot.render("network") -- knotmirror 0.0.1 From 2b169305861b478e9b6ad7fe90557524fc241093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 09:56:45 -0600 Subject: [PATCH 19/40] https://github.com/LibreQoE/LibreQoS/issues/336 --- src/integrationUISP.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index 3850608b..d13daf32 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -135,11 +135,8 @@ def findApCapacities(devices, siteBandwidth): download = int(device['overview'] ['downlinkCapacity'] / 1000000) upload = int(device['overview']['uplinkCapacity'] / 1000000) - if download < 15: - print("WARNING: Device '" + device['identification']['hostname'] + "' has unusually low download capacity (" + str(upload) + " Mbps). Discarding in favor of parent site rates.") - safeToUse = False - if upload < 15: - print("WARNING: Device '" + device['identification']['hostname'] + "' has unusually low upload capacity (" + str(download) + " Mbps). Discarding in favor of parent site rates.") + if (download < 15) or (upload < 15): + print("WARNING: Device '" + device['identification']['hostname'] + "' has unusually low capacity (" + str(download) + '/' + str(upload) + " Mbps). Discarding in favor of parent site rates.") safeToUse = False if device['identification']['model'] == 'WaveAP': if (download < 500) or (upload < 500): -- knotmirror 0.0.1 From 286c2a4931b317f1e0cd9bf507bd4c0e1cc60a5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 10:03:41 -0600 Subject: [PATCH 20/40] Only print warning if actual site goes off PtMP AP, not client sites --- src/integrationUISP.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/integrationUISP.py b/src/integrationUISP.py index d13daf32..15870cfe 100644 --- a/src/integrationUISP.py +++ b/src/integrationUISP.py @@ -275,8 +275,9 @@ def findNodesBranchedOffPtMP(siteList, dataLinks, sites, rootSite, foundAirFiber nodeOffPtMP[id] = {'download': download, 'upload': upload } - #print('Site ' + name + ' will use PtMP AP as parent.') - site['parent'] = parent + site['parent'] = parent + if site['type'] == 'site': + print('Site ' + name + ' will use PtMP AP as parent.') return siteList, nodeOffPtMP def handleMultipleInternetNodes(sites, dataLinks, uispSite): -- knotmirror 0.0.1 From ffda0a08f11aef06707a9c5d6fd7b35e82d1e1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 10:29:49 -0600 Subject: [PATCH 21/40] Render network map graph horizontally. Makes much more efficient use of space. --- src/integrationCommon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/integrationCommon.py b/src/integrationCommon.py index 665af88a..a70d8adb 100644 --- a/src/integrationCommon.py +++ b/src/integrationCommon.py @@ -418,7 +418,7 @@ class NetworkGraph: import graphviz dot = graphviz.Digraph( - 'network', comment="Network Graph", engine="dot") + 'network', comment="Network Graph", engine="dot", graph_attr={'rankdir':'LR'}) for (i, node) in enumerate(self.nodes): if ((node.type != NodeType.client and node.type != NodeType.device) or showClients): @@ -436,6 +436,6 @@ class NetworkGraph: if child != i: if (self.nodes[child].type != NodeType.client and self.nodes[child].type != NodeType.device) or showClients: dot.edge("N" + str(i), "N" + str(child)) - + dot = dot.unflatten(stagger=3)#, fanout=True) dot.render("network") -- knotmirror 0.0.1 From 4861b5f2fdb592707028d8fb7c1d0085879e00d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 10:37:21 -0600 Subject: [PATCH 22/40] Rename integrationUISProutes.csv to integrationUISProutes.template.csv --- ...tegrationUISProutes.csv => integrationUISProutes.template.csv} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{integrationUISProutes.csv => integrationUISProutes.template.csv} (100%) diff --git a/src/integrationUISProutes.csv b/src/integrationUISProutes.template.csv similarity index 100% rename from src/integrationUISProutes.csv rename to src/integrationUISProutes.template.csv -- knotmirror 0.0.1 From 1ee7484d86094c62e4fd7dc65a40078fac457312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sat, 15 Apr 2023 11:24:07 -0600 Subject: [PATCH 23/40] Add 'type' to network.json. For https://github.com/LibreQoE/LibreQoS/issues/308 --- src/integrationCommon.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/integrationCommon.py b/src/integrationCommon.py index a70d8adb..6a771253 100644 --- a/src/integrationCommon.py +++ b/src/integrationCommon.py @@ -63,6 +63,22 @@ class NodeType(enum.IntEnum): clientWithChildren = 5 device = 6 +def nodeTypeToString(integer): + string = '' + match integer: + case 1: + string = 'root' + case 2: + string = 'site' + case 3: + string = 'ap' + case 4: + string = 'client' + case 5: + string = 'clientWithChildren' + case 6: + string = 'device' + return(string) class NetworkNode: # Defines a node on a LibreQoS network graph. @@ -311,6 +327,7 @@ class NetworkGraph: node = { "downloadBandwidthMbps": self.nodes[idx].downloadMbps, "uploadBandwidthMbps": self.nodes[idx].uploadMbps, + 'type': nodeTypeToString(self.nodes[idx].type), } children = {} hasChildren = False -- knotmirror 0.0.1 From d965f51a646c982a83a1b66f69f9144ba4e0adfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sun, 16 Apr 2023 09:28:53 -0600 Subject: [PATCH 24/40] Update ispConfig.example.py --- src/ispConfig.example.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ispConfig.example.py b/src/ispConfig.example.py index 9da3813c..53a010b8 100644 --- a/src/ispConfig.example.py +++ b/src/ispConfig.example.py @@ -23,6 +23,9 @@ interfaceA = 'eth1' # Interface connected to edge router interfaceB = 'eth2' +# Queue refresh scheduler (lqos_scheduler). Minutes between reloads. +queueRefreshIntervalMins = 30 + # WORK IN PROGRESS. Note that interfaceA determines the "stick" interface # I could only get scanning to work if I issued ethtool -K enp1s0f1 rxvlan off OnAStick = False -- knotmirror 0.0.1 From c7d501a9577783a8fe621715372d5dceebb1e17d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Sun, 16 Apr 2023 09:32:17 -0600 Subject: [PATCH 25/40] Add option to control frequency of queue refreshes in minutes - queueRefreshIntervalMins --- src/scheduler.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/scheduler.py b/src/scheduler.py index c1235812..0c1de7ba 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -3,6 +3,10 @@ import datetime from LibreQoS import refreshShapers, refreshShapersUpdateOnly from graphInfluxDB import refreshBandwidthGraphs, refreshLatencyGraphs from ispConfig import influxDBEnabled, automaticImportUISP, automaticImportSplynx +try: + from ispConfig import queueRefreshIntervalMins +except: + queueRefreshIntervalMins = 30 if automaticImportUISP: from integrationUISP import importFromUISP if automaticImportSplynx: @@ -44,7 +48,7 @@ def importAndShapePartialReload(): if __name__ == '__main__': importAndShapeFullReload() - ads.add_job(importAndShapePartialReload, 'interval', minutes=30) + ads.add_job(importAndShapePartialReload, 'interval', minutes=queueRefreshIntervalMins) if influxDBEnabled: ads.add_job(graphHandler, 'interval', seconds=10) -- knotmirror 0.0.1 From 80a135672026bddcd4d8924b002844f5a4d97d6d Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Mon, 17 Apr 2023 13:28:09 +0000 Subject: [PATCH 26/40] Add a tool to track eBPF map performance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New tool: lqos_map_perf is included in the distribution. It runs the two map iterators that pound the lqosd/eBPF system with map iterations, counts the number of entries returned and reports on overall performance (in µs). This is intended to help with diagnosing map reading performance issues and act as a bench-test to see if using eBPF iterators actually helps. --- src/build_dpkg.sh | 2 +- src/build_rust.sh | 2 +- src/rust/Cargo.lock | 7 +++++++ src/rust/Cargo.toml | 1 + src/rust/lqos_map_perf/Cargo.toml | 7 +++++++ src/rust/lqos_map_perf/src/main.rs | 24 ++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/rust/lqos_map_perf/Cargo.toml create mode 100644 src/rust/lqos_map_perf/src/main.rs diff --git a/src/build_dpkg.sh b/src/build_dpkg.sh index 8f7b7640..cc562d6d 100755 --- a/src/build_dpkg.sh +++ b/src/build_dpkg.sh @@ -22,7 +22,7 @@ ETC_DIR=$DPKG_DIR/etc MOTD_DIR=$DPKG_DIR/etc/update-motd.d LQOS_FILES="graphInfluxDB.py influxDBdashboardTemplate.json integrationCommon.py integrationRestHttp.py integrationSplynx.py integrationUISP.py ispConfig.example.py LibreQoS.py lqos.example lqTools.py mikrotikFindIPv6.py network.example.json pythonCheck.py README.md scheduler.py ShapedDevices.example.csv" LQOS_BIN_FILES="lqos_scheduler.service.example lqosd.service.example lqos_node_manager.service.example" -RUSTPROGS="lqosd lqtop xdp_iphash_to_cpu_cmdline xdp_pping lqos_node_manager lqusers lqos_setup" +RUSTPROGS="lqosd lqtop xdp_iphash_to_cpu_cmdline xdp_pping lqos_node_manager lqusers lqos_setup lqos_map_perf" #################################################### # Clean any previous dist build diff --git a/src/build_rust.sh b/src/build_rust.sh index cc5547eb..bebc1c58 100755 --- a/src/build_rust.sh +++ b/src/build_rust.sh @@ -52,7 +52,7 @@ rustup update # Start building echo "Please wait while the system is compiled. Service will not be interrupted during this stage." -PROGS="lqosd lqtop xdp_iphash_to_cpu_cmdline xdp_pping lqos_node_manager lqusers" +PROGS="lqosd lqtop xdp_iphash_to_cpu_cmdline xdp_pping lqos_node_manager lqusers lqos_map_perf" mkdir -p bin/static pushd rust > /dev/null #cargo clean diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index 2f8f803f..d204dfbb 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -1455,6 +1455,13 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "lqos_map_perf" +version = "0.1.0" +dependencies = [ + "lqos_sys", +] + [[package]] name = "lqos_node_manager" version = "0.1.0" diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index a6a78d94..a86c8057 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -26,4 +26,5 @@ members = [ "lqos_setup", # A quick CLI setup for first-time users "lqos_anonymous_stats_server", # The server for gathering anonymous usage data. "lqos_heimdall", # Library for managing Heimdall flow watching + "lqos_map_perf", # A CLI tool for testing eBPF map performance ] diff --git a/src/rust/lqos_map_perf/Cargo.toml b/src/rust/lqos_map_perf/Cargo.toml new file mode 100644 index 00000000..34ec96e1 --- /dev/null +++ b/src/rust/lqos_map_perf/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lqos_map_perf" +version = "0.1.0" +edition = "2021" + +[dependencies] +lqos_sys = { path = "../lqos_sys" } diff --git a/src/rust/lqos_map_perf/src/main.rs b/src/rust/lqos_map_perf/src/main.rs new file mode 100644 index 00000000..c31c1433 --- /dev/null +++ b/src/rust/lqos_map_perf/src/main.rs @@ -0,0 +1,24 @@ +use std::time::Instant; + +use lqos_sys::{rtt_for_each, throughput_for_each}; + +fn main() { + println!("LibreQoS Map Performance Tool"); + + // Test the RTT map + let mut rtt_count = 0; + let now = Instant::now(); + rtt_for_each(&mut |_rtt, _tracker| { + rtt_count += 1; + }); + let elapsed = now.elapsed(); + println!("RTT map: {} entries in {} µs", rtt_count, elapsed.as_micros()); + + let mut tp_count = 0; + let now = Instant::now(); + throughput_for_each(&mut |_ip, _hosts| { + tp_count += 1; + }); + let elapsed = now.elapsed(); + println!("TP map: {} entries in {} µs", tp_count, elapsed.as_micros()); +} -- knotmirror 0.0.1 From cc88ed1c627868adf98ce4954a175604d8cc19a6 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Tue, 18 Apr 2023 13:05:55 +0000 Subject: [PATCH 27/40] Add the new tool to the git ignore list. --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5d1cdb70..46789d81 100644 --- a/.gitignore +++ b/.gitignore @@ -66,7 +66,7 @@ src/bin/lqos_node_manager src/bin/webusers src/bin/lqusers src/bin/Rocket.toml - +src/bin/lqos_map_perf # Ignore project folders for some IDEs .idea/ -- knotmirror 0.0.1 From f0975ccbcd91b9d3c2916f8d93d78c939e61ed5e Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Tue, 18 Apr 2023 17:24:25 +0000 Subject: [PATCH 28/40] Use eBPF iterators (requires kernel 5.8+) instead of next_entry Replaces the eBPF interface "bpf_map_get_next_key" followed by "bpf_map_lookup_elem" cycle for iterating maps with a call to the kernel-provided eBPF iterator service. Currently only implemented for throughput and RTT tracking. Further commits will tighten the scope and eventually remove the legacy setup completely. For 10k hosts, this reduces the number of system calls from 20,002 to 4. The remaining items are a futex lock (required for safety), and an iterator call. --- src/rust/Cargo.lock | 1 + src/rust/lqos_sys/Cargo.toml | 1 + src/rust/lqos_sys/src/bpf/lqos_kern.c | 55 ++++++ src/rust/lqos_sys/src/bpf/wrapper.c | 74 ++++++++ src/rust/lqos_sys/src/bpf/wrapper.h | 7 +- src/rust/lqos_sys/src/bpf_iterator.rs | 168 ++++++++++++++++++ src/rust/lqos_sys/src/bpf_map.rs | 1 + src/rust/lqos_sys/src/kernel_wrapper.rs | 29 ++- src/rust/lqos_sys/src/lib.rs | 1 + src/rust/lqos_sys/src/lqos_kernel.rs | 7 +- src/rust/lqos_sys/src/tcp_rtt.rs | 14 +- src/rust/lqos_sys/src/throughput.rs | 13 +- .../src/throughput_tracker/tracking_data.rs | 41 +++-- 13 files changed, 367 insertions(+), 45 deletions(-) create mode 100644 src/rust/lqos_sys/src/bpf_iterator.rs diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index d204dfbb..0f0ffb4f 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -1542,6 +1542,7 @@ dependencies = [ "nix", "once_cell", "thiserror", + "zerocopy", ] [[package]] diff --git a/src/rust/lqos_sys/Cargo.toml b/src/rust/lqos_sys/Cargo.toml index a743824f..dcffa3a9 100644 --- a/src/rust/lqos_sys/Cargo.toml +++ b/src/rust/lqos_sys/Cargo.toml @@ -16,6 +16,7 @@ lqos_utils = { path = "../lqos_utils" } once_cell = "1" dashmap = "5" thiserror = "1" +zerocopy = { version = "0.6.1", features = ["simd"] } [build-dependencies] bindgen = "0" diff --git a/src/rust/lqos_sys/src/bpf/lqos_kern.c b/src/rust/lqos_sys/src/bpf/lqos_kern.c index 9e4e5295..20bfc1a3 100644 --- a/src/rust/lqos_sys/src/bpf/lqos_kern.c +++ b/src/rust/lqos_sys/src/bpf/lqos_kern.c @@ -319,4 +319,59 @@ int bifrost(struct __sk_buff *skb) return TC_ACT_UNSPEC; } +/* + * Structs for map iteration programs + * See https://github.com/xdp-project/bpf-examples + */ +struct bpf_iter_meta { + struct seq_file *seq; + __u64 session_id; + __u64 seq_num; +} __attribute__((preserve_access_index)); + +struct bpf_iter__bpf_map_elem { + struct bpf_iter_meta *meta; + struct bpf_map *map; + void *key; + void *value; +}; + +SEC("iter/bpf_map_elem") +int throughput_reader(struct bpf_iter__bpf_map_elem *ctx) +{ + // The sequence file + struct seq_file *seq = ctx->meta->seq; + struct host_counter *counter = ctx->value; + struct in6_addr *ip = ctx->key; + + // Bail on end + if (counter == NULL || ip == NULL) { + return 0; + } + + bpf_seq_write(seq, ip, sizeof(struct in6_addr)); + bpf_seq_write(seq, counter, sizeof(struct host_counter)); + //BPF_SEQ_PRINTF(seq, "%d %d\n", counter->download_bytes, counter->upload_bytes); + return 0; +} + +SEC("iter/bpf_map_elem") +int rtt_reader(struct bpf_iter__bpf_map_elem *ctx) +{ + // The sequence file + struct seq_file *seq = ctx->meta->seq; + struct rotating_performance *counter = ctx->value; + struct in6_addr *ip = ctx->key; + + // Bail on end + if (counter == NULL || ip == NULL) { + return 0; + } + + //BPF_SEQ_PRINTF(seq, "%d %d\n", counter->next_entry, counter->rtt[0]); + bpf_seq_write(seq, ip, sizeof(struct in6_addr)); + bpf_seq_write(seq, counter, sizeof(struct rotating_performance)); + return 0; +} + char _license[] SEC("license") = "GPL"; diff --git a/src/rust/lqos_sys/src/bpf/wrapper.c b/src/rust/lqos_sys/src/bpf/wrapper.c index 8ca2f74d..94ae7914 100644 --- a/src/rust/lqos_sys/src/bpf/wrapper.c +++ b/src/rust/lqos_sys/src/bpf/wrapper.c @@ -246,3 +246,77 @@ int tc_attach_ingress(int ifindex, bool verbose, struct lqos_kern *obj) out: return err; } + +// Iterator code +#include +#include + +struct bpf_link *setup_iterator_link( + struct bpf_program *prog, + struct bpf_map *map +) { + int map_fd; // File descriptor for the map itself + struct bpf_link *link; // Value to return with the link + union bpf_iter_link_info linfo = { 0 }; + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, iter_opts, + .link_info = &linfo, + .link_info_len = sizeof(linfo)); + + map_fd = bpf_map__fd(map); + if (map_fd < 0) { + fprintf(stderr, "bpf_map__fd() fails\n"); + return NULL; + } + linfo.map.map_fd = map_fd; + + link = bpf_program__attach_iter(prog, &iter_opts); + if (!link) { + fprintf(stderr, "bpf_program__attach_iter() fails\n"); + return NULL; + } + return link; +} + +int read_tp_buffer(struct bpf_program *prog, struct bpf_map *map) +{ + struct bpf_link *link; + char buf[16] = {}; + int iter_fd = -1, len; + int ret = 0; + int map_fd; + + union bpf_iter_link_info linfo = { 0 }; + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, iter_opts, + .link_info = &linfo, + .link_info_len = sizeof(linfo)); + + map_fd = bpf_map__fd(map); + if (map_fd < 0) { + fprintf(stderr, "bpf_map__fd() fails\n"); + return map_fd; + } + linfo.map.map_fd = map_fd; + + link = bpf_program__attach_iter(prog, &iter_opts); + if (!link) { + fprintf(stderr, "bpf_program__attach_iter() fails\n"); + return -1; + } + iter_fd = bpf_iter_create(bpf_link__fd(link)); + if (iter_fd < 0) { + fprintf(stderr, "bpf_iter_create() fails\n"); + ret = -1; + goto free_link; + } + /* not check contents, but ensure read() ends without error */ + while ((len = read(iter_fd, buf, sizeof(buf) - 1)) > 0) { + buf[len] = 0; + printf("%s", buf); + } + printf("\n"); +free_link: + if (iter_fd >= 0) + close(iter_fd); + bpf_link__destroy(link); + return 0; +} \ No newline at end of file diff --git a/src/rust/lqos_sys/src/bpf/wrapper.h b/src/rust/lqos_sys/src/bpf/wrapper.h index a6c8e074..e24a3dc0 100644 --- a/src/rust/lqos_sys/src/bpf/wrapper.h +++ b/src/rust/lqos_sys/src/bpf/wrapper.h @@ -1,5 +1,8 @@ #include "lqos_kern_skel.h" #include +#include +#include +#include extern struct lqos_kern * lqos_kern_open(); extern int lqos_kern_load(struct lqos_kern * skel); @@ -8,4 +11,6 @@ extern int tc_detach_egress(int ifindex, bool verbose, bool flush_hook, const ch extern int tc_attach_ingress(int ifindex, bool verbose, struct lqos_kern *obj); extern int tc_detach_ingress(int ifindex, bool verbose, bool flush_hook, const char * ifname); extern __u64 max_tracker_ips(); -extern void do_not_print(); \ No newline at end of file +extern void do_not_print(); +int read_tp_buffer(struct bpf_program *prog, struct bpf_map *map); +struct bpf_link * setup_iterator_link(struct bpf_program *prog, struct bpf_map *map); diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs new file mode 100644 index 00000000..90b90a6a --- /dev/null +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -0,0 +1,168 @@ +use crate::{ + kernel_wrapper::BPF_SKELETON, lqos_kernel::bpf, HostCounter, + RttTrackingEntry, +}; +use lqos_utils::XdpIpAddress; +use once_cell::sync::Lazy; +use std::{ + fs::File, io::Read, marker::PhantomData, os::fd::FromRawFd, sync::Mutex, fmt::Debug, +}; +use thiserror::Error; +use zerocopy::FromBytes; + +struct BpfMapIterator { + link: *mut bpf::bpf_link, + _phantom: PhantomData<(KEY, VALUE)>, +} + +unsafe impl Sync for BpfMapIterator {} +unsafe impl Send for BpfMapIterator {} + +impl BpfMapIterator { + fn new( + program: *mut bpf::bpf_program, + map: *mut bpf::bpf_map, + ) -> Result { + let link = unsafe { bpf::setup_iterator_link(program, map) }; + if !link.is_null() { + Ok(Self { link, _phantom: PhantomData }) + } else { + Err(BpfIteratorError::FailedToLink) + } + } + + fn as_file(&self) -> Result { + let link_fd = unsafe { bpf::bpf_link__fd(self.link) }; + let iter_fd = unsafe { bpf::bpf_iter_create(link_fd) }; + if iter_fd < 0 { + log::error!("Unable to create map file descriptor"); + Err(BpfIteratorError::FailedToCreateFd) + } else { + unsafe { Ok(File::from_raw_fd(iter_fd)) } + } + } + + fn iter(&self) -> Result, BpfIteratorError> { + let mut file = self.as_file()?; + let mut buf = Vec::new(); + let bytes_read = file.read_to_end(&mut buf); + match bytes_read { + Ok(_) => Ok(BpfMapIter { buffer: buf, index: 0, _phantom: PhantomData }), + Err(e) => { + log::error!("Unable to read from kernel map iterator file"); + log::error!("{e:?}"); + Err(BpfIteratorError::UnableToCreateIterator) + } + } + } +} + +impl Drop for BpfMapIterator { + fn drop(&mut self) { + unsafe { + bpf::bpf_link__destroy(self.link); + } + } +} + +pub(crate) struct BpfMapIter { + buffer: Vec, + index: usize, + _phantom: PhantomData<(K, V)>, +} + +impl BpfMapIter { + const KEY_SIZE: usize = std::mem::size_of::(); + const VALUE_SIZE: usize = std::mem::size_of::(); + const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; +} + +impl Iterator for BpfMapIter +where + K: FromBytes + Debug, + V: FromBytes + Debug, +{ + type Item = (K, V); + + fn next(&mut self) -> Option { + if self.index + Self::TOTAL_SIZE <= self.buffer.len() { + let key = K::read_from(&self.buffer[self.index..self.index + Self::KEY_SIZE]); + self.index += Self::KEY_SIZE; + let value = V::read_from( + &self.buffer + [self.index ..self.index + Self::VALUE_SIZE], + ); + self.index += Self::VALUE_SIZE; + Some((key.unwrap(), value.unwrap())) + } else { + None + } + } +} + +#[derive(Debug, Error)] +enum BpfIteratorError { + #[error("Failed to create iterator link")] + FailedToLink, + #[error("Failed to create file descriptor")] + FailedToCreateFd, + #[error("Iterator error")] + UnableToCreateIterator, +} + +static MAP_TRAFFIC: Lazy< + Mutex>>, +> = Lazy::new(|| Mutex::new(None)); + +static RTT_TRACKER: Lazy< + Mutex>>, +> = Lazy::new(|| Mutex::new(None)); + +pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &HostCounter)) { + let mut traffic = MAP_TRAFFIC.lock().unwrap(); + if traffic.is_none() { + let lock = BPF_SKELETON.lock().unwrap(); + if let Some(skeleton) = lock.as_ref() { + let skeleton = skeleton.get_ptr(); + if let Ok(iter) = unsafe { + BpfMapIterator::new( + (*skeleton).progs.throughput_reader, + (*skeleton).maps.map_traffic, + ) + } { + *traffic = Some(iter); + } + } + } + + if let Some(iter) = traffic.as_mut() { + iter.iter().unwrap().for_each(|(k, v)| { + //println!("{:?} {:?}", k, v); + callback(&k, &v); + }); + } +} + +pub fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { + let mut traffic = RTT_TRACKER.lock().unwrap(); + if traffic.is_none() { + let lock = BPF_SKELETON.lock().unwrap(); + if let Some(skeleton) = lock.as_ref() { + let skeleton = skeleton.get_ptr(); + if let Ok(iter) = unsafe { + BpfMapIterator::new( + (*skeleton).progs.rtt_reader, + (*skeleton).maps.rtt_tracker, + ) + } { + *traffic = Some(iter); + } + } + } + + if let Some(iter) = traffic.as_mut() { + iter.iter().unwrap().for_each(|(k, v)| { + callback(&k, &v); + }); + } +} diff --git a/src/rust/lqos_sys/src/bpf_map.rs b/src/rust/lqos_sys/src/bpf_map.rs index 6f6f3043..f52aa7e1 100644 --- a/src/rust/lqos_sys/src/bpf_map.rs +++ b/src/rust/lqos_sys/src/bpf_map.rs @@ -94,6 +94,7 @@ where value_ptr as *mut c_void, ); //result.push((key.clone(), value.clone())); + println!("old callback"); callback(&key, &value); prev_key = key_ptr; } diff --git a/src/rust/lqos_sys/src/kernel_wrapper.rs b/src/rust/lqos_sys/src/kernel_wrapper.rs index 6375a83f..1c6cb133 100644 --- a/src/rust/lqos_sys/src/kernel_wrapper.rs +++ b/src/rust/lqos_sys/src/kernel_wrapper.rs @@ -1,8 +1,28 @@ +use std::sync::Mutex; +use once_cell::sync::Lazy; use crate::lqos_kernel::{ attach_xdp_and_tc_to_interface, unload_xdp_from_interface, - InterfaceDirection, bpf::ring_buffer_sample_fn, + InterfaceDirection, bpf::{ring_buffer_sample_fn, self}, }; +/// Safer wrapper around pointers to `bpf::lqos_kern`. It really isn't +/// a great idea to be passing mutable pointers around like this, but the C +/// world insists on it. +pub(crate) struct LqosKernBpfWrapper { + ptr: *mut bpf::lqos_kern, +} + +impl LqosKernBpfWrapper { + pub(crate) fn get_ptr(&self) -> *mut bpf::lqos_kern { + self.ptr + } +} + +unsafe impl Sync for LqosKernBpfWrapper {} +unsafe impl Send for LqosKernBpfWrapper {} + +pub(crate) static BPF_SKELETON: Lazy>> = Lazy::new(|| Mutex::new(None)); + /// A wrapper-type that stores the interfaces to which the XDP and TC programs should /// be attached. Performs the attachment process, and hooks "drop" to unattach the /// programs when the structure falls out of scope. @@ -31,7 +51,7 @@ impl LibreQoSKernels { to_isp: to_isp.to_string(), on_a_stick: false, }; - attach_xdp_and_tc_to_interface( + let skeleton = attach_xdp_and_tc_to_interface( &kernel.to_internet, InterfaceDirection::Internet, heimdall_event_handler, @@ -41,6 +61,7 @@ impl LibreQoSKernels { InterfaceDirection::IspNetwork, heimdall_event_handler, )?; + BPF_SKELETON.lock().unwrap().replace(LqosKernBpfWrapper { ptr: skeleton }); Ok(kernel) } @@ -66,12 +87,12 @@ impl LibreQoSKernels { to_isp: String::new(), on_a_stick: true, }; - attach_xdp_and_tc_to_interface( + let skeleton = attach_xdp_and_tc_to_interface( &kernel.to_internet, InterfaceDirection::OnAStick(internet_vlan, isp_vlan), heimdall_event_handler, )?; - + BPF_SKELETON.lock().unwrap().replace(LqosKernBpfWrapper { ptr: skeleton }); Ok(kernel) } } diff --git a/src/rust/lqos_sys/src/lib.rs b/src/rust/lqos_sys/src/lib.rs index 40b56dae..c3c64aba 100644 --- a/src/rust/lqos_sys/src/lib.rs +++ b/src/rust/lqos_sys/src/lib.rs @@ -22,6 +22,7 @@ mod lqos_kernel; mod tcp_rtt; mod throughput; mod linux; +mod bpf_iterator; pub use ip_mapping::{ add_ip_to_tc, clear_ips_from_tc, del_ip_from_tc, list_mapped_ips, diff --git a/src/rust/lqos_sys/src/lqos_kernel.rs b/src/rust/lqos_sys/src/lqos_kernel.rs index 89a132d3..ff67d196 100644 --- a/src/rust/lqos_sys/src/lqos_kernel.rs +++ b/src/rust/lqos_sys/src/lqos_kernel.rs @@ -14,6 +14,8 @@ use log::{info, warn}; use nix::libc::{geteuid, if_nametoindex}; use std::{ffi::{CString, c_void}, process::Command}; +use self::bpf::lqos_kern; + pub(crate) mod bpf { #![allow(warnings, unused)] include!(concat!(env!("OUT_DIR"), "/bindings.rs")); @@ -114,7 +116,7 @@ pub fn attach_xdp_and_tc_to_interface( interface_name: &str, direction: InterfaceDirection, heimdall_event_handler: bpf::ring_buffer_sample_fn, -) -> Result<()> { +) -> Result<*mut lqos_kern> { check_root()?; // Check the interface is valid let interface_index = interface_name_to_index(interface_name)?; @@ -229,7 +231,8 @@ pub fn attach_xdp_and_tc_to_interface( } } - Ok(()) + + Ok(skeleton) } unsafe fn attach_xdp_best_available( diff --git a/src/rust/lqos_sys/src/tcp_rtt.rs b/src/rust/lqos_sys/src/tcp_rtt.rs index 5742f17b..2740c7c6 100644 --- a/src/rust/lqos_sys/src/tcp_rtt.rs +++ b/src/rust/lqos_sys/src/tcp_rtt.rs @@ -1,8 +1,10 @@ -use crate::bpf_map::BpfMap; +use lqos_utils::XdpIpAddress; +use zerocopy::FromBytes; +use crate::bpf_iterator::iterate_rtt; /// Entry from the XDP rtt_tracker map. #[repr(C)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, FromBytes)] pub struct RttTrackingEntry { /// Array containing TCP round-trip times. Convert to an `f32` and divide by `100.0` for actual numbers. pub rtt: [u32; 60], @@ -29,10 +31,6 @@ impl Default for RttTrackingEntry { /// Only IP addresses facing the ISP Network side are tracked. /// /// Executes `callback` for each entry. -pub fn rtt_for_each(callback: &mut dyn FnMut(&[u8; 16], &RttTrackingEntry)) { - if let Ok(rtt_tracker) = - BpfMap::<[u8; 16], RttTrackingEntry>::from_path("/sys/fs/bpf/rtt_tracker") - { - rtt_tracker.for_each(callback); - } +pub fn rtt_for_each(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { + iterate_rtt(callback); } diff --git a/src/rust/lqos_sys/src/throughput.rs b/src/rust/lqos_sys/src/throughput.rs index 75c458b7..c7749457 100644 --- a/src/rust/lqos_sys/src/throughput.rs +++ b/src/rust/lqos_sys/src/throughput.rs @@ -1,10 +1,9 @@ use lqos_utils::XdpIpAddress; - -use crate::{bpf_per_cpu_map::BpfPerCpuMap}; +use zerocopy::FromBytes; /// Representation of the XDP map from map_traffic #[repr(C)] -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, FromBytes)] pub struct HostCounter { /// Download bytes counter (keeps incrementing) pub download_bytes: u64, @@ -28,11 +27,7 @@ pub struct HostCounter { /// Iterates through all throughput entries, and sends them in turn to `callback`. /// This elides the need to clone or copy data. pub fn throughput_for_each( - callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter]), + callback: &mut dyn FnMut(&XdpIpAddress, &HostCounter), ) { - if let Ok(throughput) = BpfPerCpuMap::::from_path( - "/sys/fs/bpf/map_traffic", - ) { - throughput.for_each(callback); - } + crate::bpf_iterator::iterate_throughput(callback); } diff --git a/src/rust/lqosd/src/throughput_tracker/tracking_data.rs b/src/rust/lqosd/src/throughput_tracker/tracking_data.rs index 73af07fa..c54e2336 100644 --- a/src/rust/lqosd/src/throughput_tracker/tracking_data.rs +++ b/src/rust/lqosd/src/throughput_tracker/tracking_data.rs @@ -111,18 +111,18 @@ impl ThroughputTracker { if let Some(mut entry) = raw_data.get_mut(xdp_ip) { entry.bytes = (0, 0); entry.packets = (0, 0); - for c in counts { - entry.bytes.0 += c.download_bytes; - entry.bytes.1 += c.upload_bytes; - entry.packets.0 += c.download_packets; - entry.packets.1 += c.upload_packets; - if c.tc_handle != 0 { - entry.tc_handle = TcHandle::from_u32(c.tc_handle); + //for c in counts { + entry.bytes.0 += counts.download_bytes; + entry.bytes.1 += counts.upload_bytes; + entry.packets.0 += counts.download_packets; + entry.packets.1 += counts.upload_packets; + if counts.tc_handle != 0 { + entry.tc_handle = TcHandle::from_u32(counts.tc_handle); } - if c.last_seen != 0 { - entry.last_seen = c.last_seen; + if counts.last_seen != 0 { + entry.last_seen = counts.last_seen; } - } + //} if entry.packets != entry.prev_packets { entry.most_recent_cycle = self_cycle; @@ -155,15 +155,15 @@ impl ThroughputTracker { last_fresh_rtt_data_cycle: 0, last_seen: 0, }; - for c in counts { - entry.bytes.0 += c.download_bytes; - entry.bytes.1 += c.upload_bytes; - entry.packets.0 += c.download_packets; - entry.packets.1 += c.upload_packets; - if c.tc_handle != 0 { - entry.tc_handle = TcHandle::from_u32(c.tc_handle); + //for c in counts { + entry.bytes.0 += counts.download_bytes; + entry.bytes.1 += counts.upload_bytes; + entry.packets.0 += counts.download_packets; + entry.packets.1 += counts.upload_packets; + if counts.tc_handle != 0 { + entry.tc_handle = TcHandle::from_u32(counts.tc_handle); } - } + //} raw_data.insert(*xdp_ip, entry); } }); @@ -171,10 +171,9 @@ impl ThroughputTracker { pub(crate) fn apply_rtt_data(&self) { let self_cycle = self.cycle.load(std::sync::atomic::Ordering::Relaxed); - rtt_for_each(&mut |raw_ip, rtt| { + rtt_for_each(&mut |ip, rtt| { if rtt.has_fresh_data != 0 { - let ip = XdpIpAddress(*raw_ip); - if let Some(mut tracker) = self.raw_data.get_mut(&ip) { + if let Some(mut tracker) = self.raw_data.get_mut(ip) { tracker.recent_rtt_data = rtt.rtt; tracker.last_fresh_rtt_data_cycle = self_cycle; if let Some(parents) = &tracker.network_json_parents { -- knotmirror 0.0.1 From 78e36d2e21e88a20c60e8dfb9af9d6fd2d5e147c Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Tue, 18 Apr 2023 20:37:49 +0000 Subject: [PATCH 29/40] Handle per-CPU maps correctly in eBPF iterators The eBPF iterator interface is now per-CPU aware, and correctly extracts a slice of data for each per-CPU map (or a slice of 1 when using a non-CPU divided map). --- src/rust/lqos_sys/src/bpf/lqos_kern.c | 20 +++++++++- src/rust/lqos_sys/src/bpf_iterator.rs | 39 +++++++++++++----- src/rust/lqos_sys/src/lqos_kernel.rs | 3 +- src/rust/lqos_sys/src/throughput.rs | 2 +- .../src/throughput_tracker/tracking_data.rs | 40 +++++++++---------- 5 files changed, 70 insertions(+), 34 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf/lqos_kern.c b/src/rust/lqos_sys/src/bpf/lqos_kern.c index 20bfc1a3..bd445a11 100644 --- a/src/rust/lqos_sys/src/bpf/lqos_kern.c +++ b/src/rust/lqos_sys/src/bpf/lqos_kern.c @@ -336,21 +336,32 @@ struct bpf_iter__bpf_map_elem { void *value; }; +volatile const int NUM_CPUS = 0; + SEC("iter/bpf_map_elem") int throughput_reader(struct bpf_iter__bpf_map_elem *ctx) { // The sequence file struct seq_file *seq = ctx->meta->seq; - struct host_counter *counter = ctx->value; + void *counter = ctx->value; struct in6_addr *ip = ctx->key; + __u32 num_cpus = NUM_CPUS; // Bail on end if (counter == NULL || ip == NULL) { return 0; } + if (ctx->meta->seq_num == 0) { + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); + } + bpf_seq_write(seq, ip, sizeof(struct in6_addr)); - bpf_seq_write(seq, counter, sizeof(struct host_counter)); + for (__u32 i=0; idownload_bytes, counter->upload_bytes); return 0; } @@ -362,12 +373,17 @@ int rtt_reader(struct bpf_iter__bpf_map_elem *ctx) struct seq_file *seq = ctx->meta->seq; struct rotating_performance *counter = ctx->value; struct in6_addr *ip = ctx->key; + __u32 num_cpus = 1; // Bail on end if (counter == NULL || ip == NULL) { return 0; } + if (ctx->meta->seq_num == 0) { + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); + } + //BPF_SEQ_PRINTF(seq, "%d %d\n", counter->next_entry, counter->rtt[0]); bpf_seq_write(seq, ip, sizeof(struct in6_addr)); bpf_seq_write(seq, counter, sizeof(struct rotating_performance)); diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 90b90a6a..f4ccb5e3 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -47,7 +47,7 @@ impl BpfMapIterator { let mut buf = Vec::new(); let bytes_read = file.read_to_end(&mut buf); match bytes_read { - Ok(_) => Ok(BpfMapIter { buffer: buf, index: 0, _phantom: PhantomData }), + Ok(_) => Ok(BpfMapIter::new(buf)), Err(e) => { log::error!("Unable to read from kernel map iterator file"); log::error!("{e:?}"); @@ -69,12 +69,26 @@ pub(crate) struct BpfMapIter { buffer: Vec, index: usize, _phantom: PhantomData<(K, V)>, + num_cpus: u32, } impl BpfMapIter { const KEY_SIZE: usize = std::mem::size_of::(); const VALUE_SIZE: usize = std::mem::size_of::(); const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; + + fn new(buffer: Vec) -> Self { + let first_four : [u8; 4] = [buffer[0], buffer[1], buffer[2], buffer[3]]; + let num_cpus = u32::from_ne_bytes(first_four); + //println!("CPUs: {num_cpus}"); + + Self { + buffer, + index: std::mem::size_of::(), + _phantom: PhantomData, + num_cpus, + } + } } impl Iterator for BpfMapIter @@ -82,18 +96,23 @@ where K: FromBytes + Debug, V: FromBytes + Debug, { - type Item = (K, V); + type Item = (K, Vec); fn next(&mut self) -> Option { if self.index + Self::TOTAL_SIZE <= self.buffer.len() { let key = K::read_from(&self.buffer[self.index..self.index + Self::KEY_SIZE]); self.index += Self::KEY_SIZE; - let value = V::read_from( - &self.buffer - [self.index ..self.index + Self::VALUE_SIZE], - ); - self.index += Self::VALUE_SIZE; - Some((key.unwrap(), value.unwrap())) + let mut vals = Vec::new(); + for _ in 0..self.num_cpus { + let value = V::read_from( + &self.buffer + [self.index ..self.index + Self::VALUE_SIZE], + ); + vals.push(value.unwrap()); + self.index += Self::VALUE_SIZE; + } + //println!("{key:?} {vals:?}"); + Some((key.unwrap(), vals)) } else { None } @@ -118,7 +137,7 @@ static RTT_TRACKER: Lazy< Mutex>>, > = Lazy::new(|| Mutex::new(None)); -pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &HostCounter)) { +pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter])) { let mut traffic = MAP_TRAFFIC.lock().unwrap(); if traffic.is_none() { let lock = BPF_SKELETON.lock().unwrap(); @@ -162,7 +181,7 @@ pub fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { if let Some(iter) = traffic.as_mut() { iter.iter().unwrap().for_each(|(k, v)| { - callback(&k, &v); + callback(&k, &v[0]); // Not per-CPU }); } } diff --git a/src/rust/lqos_sys/src/lqos_kernel.rs b/src/rust/lqos_sys/src/lqos_kernel.rs index ff67d196..d3db199a 100644 --- a/src/rust/lqos_sys/src/lqos_kernel.rs +++ b/src/rust/lqos_sys/src/lqos_kernel.rs @@ -14,7 +14,7 @@ use log::{info, warn}; use nix::libc::{geteuid, if_nametoindex}; use std::{ffi::{CString, c_void}, process::Command}; -use self::bpf::lqos_kern; +use self::bpf::{lqos_kern, libbpf_num_possible_cpus}; pub(crate) mod bpf { #![allow(warnings, unused)] @@ -123,6 +123,7 @@ pub fn attach_xdp_and_tc_to_interface( set_strict_mode()?; let skeleton = unsafe { let skeleton = open_kernel()?; + (*(*skeleton).rodata).NUM_CPUS = libbpf_num_possible_cpus(); (*(*skeleton).data).direction = match direction { InterfaceDirection::Internet => 1, InterfaceDirection::IspNetwork => 2, diff --git a/src/rust/lqos_sys/src/throughput.rs b/src/rust/lqos_sys/src/throughput.rs index c7749457..df5ef2d3 100644 --- a/src/rust/lqos_sys/src/throughput.rs +++ b/src/rust/lqos_sys/src/throughput.rs @@ -27,7 +27,7 @@ pub struct HostCounter { /// Iterates through all throughput entries, and sends them in turn to `callback`. /// This elides the need to clone or copy data. pub fn throughput_for_each( - callback: &mut dyn FnMut(&XdpIpAddress, &HostCounter), + callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter]), ) { crate::bpf_iterator::iterate_throughput(callback); } diff --git a/src/rust/lqosd/src/throughput_tracker/tracking_data.rs b/src/rust/lqosd/src/throughput_tracker/tracking_data.rs index c54e2336..f204d8df 100644 --- a/src/rust/lqosd/src/throughput_tracker/tracking_data.rs +++ b/src/rust/lqosd/src/throughput_tracker/tracking_data.rs @@ -111,18 +111,18 @@ impl ThroughputTracker { if let Some(mut entry) = raw_data.get_mut(xdp_ip) { entry.bytes = (0, 0); entry.packets = (0, 0); - //for c in counts { - entry.bytes.0 += counts.download_bytes; - entry.bytes.1 += counts.upload_bytes; - entry.packets.0 += counts.download_packets; - entry.packets.1 += counts.upload_packets; - if counts.tc_handle != 0 { - entry.tc_handle = TcHandle::from_u32(counts.tc_handle); + for c in counts { + entry.bytes.0 += c.download_bytes; + entry.bytes.1 += c.upload_bytes; + entry.packets.0 += c.download_packets; + entry.packets.1 += c.upload_packets; + if c.tc_handle != 0 { + entry.tc_handle = TcHandle::from_u32(c.tc_handle); } - if counts.last_seen != 0 { - entry.last_seen = counts.last_seen; + if c.last_seen != 0 { + entry.last_seen = c.last_seen; } - //} + } if entry.packets != entry.prev_packets { entry.most_recent_cycle = self_cycle; @@ -131,8 +131,8 @@ impl ThroughputTracker { net_json.add_throughput_cycle( parents, ( - entry.bytes.0 - entry.prev_bytes.0, - entry.bytes.1 - entry.prev_bytes.1, + entry.bytes.0.saturating_sub(entry.prev_bytes.0), + entry.bytes.1.saturating_sub(entry.prev_bytes.1), ), ); } @@ -155,15 +155,15 @@ impl ThroughputTracker { last_fresh_rtt_data_cycle: 0, last_seen: 0, }; - //for c in counts { - entry.bytes.0 += counts.download_bytes; - entry.bytes.1 += counts.upload_bytes; - entry.packets.0 += counts.download_packets; - entry.packets.1 += counts.upload_packets; - if counts.tc_handle != 0 { - entry.tc_handle = TcHandle::from_u32(counts.tc_handle); + for c in counts { + entry.bytes.0 += c.download_bytes; + entry.bytes.1 += c.upload_bytes; + entry.packets.0 += c.download_packets; + entry.packets.1 += c.upload_packets; + if c.tc_handle != 0 { + entry.tc_handle = TcHandle::from_u32(c.tc_handle); } - //} + } raw_data.insert(*xdp_ip, entry); } }); -- knotmirror 0.0.1 From 04ff52a007bd2cc966f5219c369a3e964b27d176 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Tue, 18 Apr 2023 21:51:50 +0000 Subject: [PATCH 30/40] Remove 2 more syscalls by removing a useless mutex. --- src/rust/lqos_sys/src/bpf_iterator.rs | 30 +++++++++++++-------------- src/rust/lqos_sys/src/tcp_rtt.rs | 4 +++- src/rust/lqos_sys/src/throughput.rs | 4 +++- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index f4ccb5e3..73967749 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -129,17 +129,16 @@ enum BpfIteratorError { UnableToCreateIterator, } -static MAP_TRAFFIC: Lazy< - Mutex>>, -> = Lazy::new(|| Mutex::new(None)); +static mut MAP_TRAFFIC: Lazy< + Option>, +> = Lazy::new(|| None); -static RTT_TRACKER: Lazy< - Mutex>>, -> = Lazy::new(|| Mutex::new(None)); +static mut RTT_TRACKER: Lazy< + Option>, +> = Lazy::new(|| None); -pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter])) { - let mut traffic = MAP_TRAFFIC.lock().unwrap(); - if traffic.is_none() { +pub unsafe fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter])) { + if MAP_TRAFFIC.is_none() { let lock = BPF_SKELETON.lock().unwrap(); if let Some(skeleton) = lock.as_ref() { let skeleton = skeleton.get_ptr(); @@ -149,12 +148,12 @@ pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter] (*skeleton).maps.map_traffic, ) } { - *traffic = Some(iter); + *MAP_TRAFFIC = Some(iter); } } } - if let Some(iter) = traffic.as_mut() { + if let Some(iter) = MAP_TRAFFIC.as_mut() { iter.iter().unwrap().for_each(|(k, v)| { //println!("{:?} {:?}", k, v); callback(&k, &v); @@ -162,9 +161,8 @@ pub fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter] } } -pub fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { - let mut traffic = RTT_TRACKER.lock().unwrap(); - if traffic.is_none() { +pub unsafe fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { + if RTT_TRACKER.is_none() { let lock = BPF_SKELETON.lock().unwrap(); if let Some(skeleton) = lock.as_ref() { let skeleton = skeleton.get_ptr(); @@ -174,12 +172,12 @@ pub fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { (*skeleton).maps.rtt_tracker, ) } { - *traffic = Some(iter); + *RTT_TRACKER = Some(iter); } } } - if let Some(iter) = traffic.as_mut() { + if let Some(iter) = RTT_TRACKER.as_mut() { iter.iter().unwrap().for_each(|(k, v)| { callback(&k, &v[0]); // Not per-CPU }); diff --git a/src/rust/lqos_sys/src/tcp_rtt.rs b/src/rust/lqos_sys/src/tcp_rtt.rs index 2740c7c6..467440f1 100644 --- a/src/rust/lqos_sys/src/tcp_rtt.rs +++ b/src/rust/lqos_sys/src/tcp_rtt.rs @@ -32,5 +32,7 @@ impl Default for RttTrackingEntry { /// /// Executes `callback` for each entry. pub fn rtt_for_each(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { - iterate_rtt(callback); + unsafe { + iterate_rtt(callback); + } } diff --git a/src/rust/lqos_sys/src/throughput.rs b/src/rust/lqos_sys/src/throughput.rs index df5ef2d3..f6115ee5 100644 --- a/src/rust/lqos_sys/src/throughput.rs +++ b/src/rust/lqos_sys/src/throughput.rs @@ -29,5 +29,7 @@ pub struct HostCounter { pub fn throughput_for_each( callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter]), ) { - crate::bpf_iterator::iterate_throughput(callback); + unsafe { + crate::bpf_iterator::iterate_throughput(callback); + } } -- knotmirror 0.0.1 From 122caaf10b55453628ddbf563346f2fe365cec06 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 13:02:11 +0000 Subject: [PATCH 31/40] Remove a no-longer-needed import. --- src/rust/lqos_sys/src/bpf_iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 73967749..209c7dd5 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -5,7 +5,7 @@ use crate::{ use lqos_utils::XdpIpAddress; use once_cell::sync::Lazy; use std::{ - fs::File, io::Read, marker::PhantomData, os::fd::FromRawFd, sync::Mutex, fmt::Debug, + fs::File, io::Read, marker::PhantomData, os::fd::FromRawFd, fmt::Debug, }; use thiserror::Error; use zerocopy::FromBytes; -- knotmirror 0.0.1 From 1c588a5a484b2d76c137e928794f261a08605d53 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 13:12:18 +0000 Subject: [PATCH 32/40] Start documenting the types created in this patch-chain. --- src/rust/lqos_sys/src/bpf_iterator.rs | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 209c7dd5..6e8eaac0 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -10,15 +10,39 @@ use std::{ use thiserror::Error; use zerocopy::FromBytes; +/// Represents a link to an eBPF defined iterator. The iterators +/// must be available in the BPF skeleton, and the skeleton must +/// be loaded. These are designed to be lazy-initialized on a +/// per-map basis. The `MAP_TRAFFIC` and `RTT_ITERATOR` types +/// implement this type. +/// +/// Normal usage is to initialize the iterator and keep it around. +/// When you need to query the iterator, execute the `iter` method +/// and treat it as a normal Rust iterator. struct BpfMapIterator { link: *mut bpf::bpf_link, _phantom: PhantomData<(KEY, VALUE)>, } +// The BPF map is re-entrant and thread safe. There's no clean +// way to represent this in Rust, so we just mark it as such. unsafe impl Sync for BpfMapIterator {} unsafe impl Send for BpfMapIterator {} impl BpfMapIterator { + /// Create a new link to an eBPF map, that *must* have an iterator + /// function defined in the eBPF program - and exposed in the + /// skeleton. + /// + /// # Safety + /// + /// * This is unsafe, it relies on the skeleton having been properly + /// initialized prior to using this type. + /// + /// # Arguments + /// + /// * `program` - The eBPF program that points to the iterator function. + /// * `map` - The eBPF map that the iterator function will iterate over. fn new( program: *mut bpf::bpf_program, map: *mut bpf::bpf_map, @@ -31,6 +55,9 @@ impl BpfMapIterator { } } + /// Create a "link file descriptor", connecting the eBPF iterator + /// to a Linux file descriptor. This instantiates the iterator + /// in the kernel and allows us to read from it. fn as_file(&self) -> Result { let link_fd = unsafe { bpf::bpf_link__fd(self.link) }; let iter_fd = unsafe { bpf::bpf_iter_create(link_fd) }; @@ -42,6 +69,11 @@ impl BpfMapIterator { } } + /// Transform the iterator into a Rust iterator. This can then + /// be used like a regular iterator. The iterator owns the + /// file's buffer and provides references. The iterator MUST + /// outlive the functions that use it (you can clone all you + /// like). fn iter(&self) -> Result, BpfIteratorError> { let mut file = self.as_file()?; let mut buf = Vec::new(); @@ -57,6 +89,8 @@ impl BpfMapIterator { } } +/// When the iterator is dropped, we need to destroy the link. +/// This is handled by the kernel when the program is unloaded. impl Drop for BpfMapIterator { fn drop(&mut self) { unsafe { @@ -65,6 +99,10 @@ impl Drop for BpfMapIterator { } } +/// Rust iterator for reading data from eBPF map iterators. +/// Transforms the data into the appropriate types, and returns +/// a tuple of the key, and a vector of values (1 per CPU for +/// CPU_MAP types, 1 entry for all others). pub(crate) struct BpfMapIter { buffer: Vec, index: usize, @@ -77,6 +115,9 @@ impl BpfMapIter { const VALUE_SIZE: usize = std::mem::size_of::(); const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; + /// Transforms the buffer into a Rust iterator. The buffer + /// is *moved* into the iterator, which retains ownership + /// throughout. fn new(buffer: Vec) -> Self { let first_four : [u8; 4] = [buffer[0], buffer[1], buffer[2], buffer[3]]; let num_cpus = u32::from_ne_bytes(first_four); -- knotmirror 0.0.1 From 9261641f42bf382aad60724dbf095b7fe78cc30b Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 16:38:39 +0000 Subject: [PATCH 33/40] Avoid reallocation of multi-cpu vectors during iteration. --- src/rust/lqos_sys/src/bpf_iterator.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 6e8eaac0..a70dd88c 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -29,7 +29,9 @@ struct BpfMapIterator { unsafe impl Sync for BpfMapIterator {} unsafe impl Send for BpfMapIterator {} -impl BpfMapIterator { +impl BpfMapIterator +where VALUE: FromBytes + Debug + Clone + Default +{ /// Create a new link to an eBPF map, that *must* have an iterator /// function defined in the eBPF program - and exposed in the /// skeleton. @@ -107,10 +109,13 @@ pub(crate) struct BpfMapIter { buffer: Vec, index: usize, _phantom: PhantomData<(K, V)>, - num_cpus: u32, + num_cpus: usize, + row_data: Vec, } -impl BpfMapIter { +impl BpfMapIter +where V: FromBytes + Debug + Clone + Default +{ const KEY_SIZE: usize = std::mem::size_of::(); const VALUE_SIZE: usize = std::mem::size_of::(); const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; @@ -120,7 +125,7 @@ impl BpfMapIter { /// throughout. fn new(buffer: Vec) -> Self { let first_four : [u8; 4] = [buffer[0], buffer[1], buffer[2], buffer[3]]; - let num_cpus = u32::from_ne_bytes(first_four); + let num_cpus = u32::from_ne_bytes(first_four) as usize; //println!("CPUs: {num_cpus}"); Self { @@ -128,14 +133,15 @@ impl BpfMapIter { index: std::mem::size_of::(), _phantom: PhantomData, num_cpus, + row_data: vec![V::default(); num_cpus], } } } impl Iterator for BpfMapIter where - K: FromBytes + Debug, - V: FromBytes + Debug, + K: FromBytes + Debug + Clone, + V: FromBytes + Debug + Clone + Default, { type Item = (K, Vec); @@ -143,17 +149,16 @@ where if self.index + Self::TOTAL_SIZE <= self.buffer.len() { let key = K::read_from(&self.buffer[self.index..self.index + Self::KEY_SIZE]); self.index += Self::KEY_SIZE; - let mut vals = Vec::new(); - for _ in 0..self.num_cpus { + for cpu in 0..self.num_cpus { let value = V::read_from( &self.buffer [self.index ..self.index + Self::VALUE_SIZE], ); - vals.push(value.unwrap()); + self.row_data[cpu] = value.unwrap(); self.index += Self::VALUE_SIZE; } //println!("{key:?} {vals:?}"); - Some((key.unwrap(), vals)) + Some((key.unwrap(), self.row_data.clone())) } else { None } -- knotmirror 0.0.1 From 239b271e0ac9dd4c57b4aefe60c41610376b4e2d Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 18:31:16 +0000 Subject: [PATCH 34/40] Add a little padding to the output stream kernel side, to nicely align on 64-bit boundaries. Then use aligned casting for zero-copy userspace access to the data. Another smallish speed increase. --- src/rust/lqos_sys/src/bpf/lqos_kern.c | 2 + src/rust/lqos_sys/src/bpf_iterator.rs | 176 +++++++++++++------------- 2 files changed, 90 insertions(+), 88 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf/lqos_kern.c b/src/rust/lqos_sys/src/bpf/lqos_kern.c index bd445a11..4eb0908a 100644 --- a/src/rust/lqos_sys/src/bpf/lqos_kern.c +++ b/src/rust/lqos_sys/src/bpf/lqos_kern.c @@ -354,6 +354,7 @@ int throughput_reader(struct bpf_iter__bpf_map_elem *ctx) if (ctx->meta->seq_num == 0) { bpf_seq_write(seq, &num_cpus, sizeof(__u32)); + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); // Repeat for padding } bpf_seq_write(seq, ip, sizeof(struct in6_addr)); @@ -382,6 +383,7 @@ int rtt_reader(struct bpf_iter__bpf_map_elem *ctx) if (ctx->meta->seq_num == 0) { bpf_seq_write(seq, &num_cpus, sizeof(__u32)); + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); // Padding } //BPF_SEQ_PRINTF(seq, "%d %d\n", counter->next_entry, counter->rtt[0]); diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index a70dd88c..2d06600e 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -5,7 +5,7 @@ use crate::{ use lqos_utils::XdpIpAddress; use once_cell::sync::Lazy; use std::{ - fs::File, io::Read, marker::PhantomData, os::fd::FromRawFd, fmt::Debug, + fmt::Debug, fs::File, io::Read, marker::PhantomData, os::fd::FromRawFd, }; use thiserror::Error; use zerocopy::FromBytes; @@ -15,7 +15,7 @@ use zerocopy::FromBytes; /// be loaded. These are designed to be lazy-initialized on a /// per-map basis. The `MAP_TRAFFIC` and `RTT_ITERATOR` types /// implement this type. -/// +/// /// Normal usage is to initialize the iterator and keep it around. /// When you need to query the iterator, execute the `iter` method /// and treat it as a normal Rust iterator. @@ -29,20 +29,22 @@ struct BpfMapIterator { unsafe impl Sync for BpfMapIterator {} unsafe impl Send for BpfMapIterator {} -impl BpfMapIterator -where VALUE: FromBytes + Debug + Clone + Default +impl BpfMapIterator +where + KEY: FromBytes + Debug + Clone, + VALUE: FromBytes + Debug + Clone + Default, { /// Create a new link to an eBPF map, that *must* have an iterator /// function defined in the eBPF program - and exposed in the /// skeleton. - /// + /// /// # Safety - /// + /// /// * This is unsafe, it relies on the skeleton having been properly /// initialized prior to using this type. - /// + /// /// # Arguments - /// + /// /// * `program` - The eBPF program that points to the iterator function. /// * `map` - The eBPF map that the iterator function will iterate over. fn new( @@ -71,22 +73,85 @@ where VALUE: FromBytes + Debug + Clone + Default } } - /// Transform the iterator into a Rust iterator. This can then - /// be used like a regular iterator. The iterator owns the - /// file's buffer and provides references. The iterator MUST - /// outlive the functions that use it (you can clone all you - /// like). - fn iter(&self) -> Result, BpfIteratorError> { + const KEY_SIZE: usize = std::mem::size_of::(); + const VALUE_SIZE: usize = std::mem::size_of::(); + const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; + + fn for_each_per_cpu( + &self, + callback: &mut dyn FnMut(&KEY, &[VALUE]), + ) -> Result<(), BpfIteratorError> { let mut file = self.as_file()?; let mut buf = Vec::new(); let bytes_read = file.read_to_end(&mut buf); match bytes_read { - Ok(_) => Ok(BpfMapIter::new(buf)), Err(e) => { log::error!("Unable to read from kernel map iterator file"); log::error!("{e:?}"); Err(BpfIteratorError::UnableToCreateIterator) } + Ok(_) => { + let first_four_bytes: [u8; 4] = [buf[0], buf[1], buf[2], buf[3]]; + let num_cpus = u32::from_ne_bytes(first_four_bytes) as usize; + let mut index = 8; + while index + Self::TOTAL_SIZE <= buf.len() { + let key_start = index; + let key_end = key_start + Self::KEY_SIZE; + let key_slice = &buf[key_start..key_end]; + let (_head, key, _tail) = unsafe { &key_slice.align_to::() }; + + let value_start = key_end; + let value_end = value_start + (num_cpus * Self::VALUE_SIZE); + let value_slice = &buf[value_start..value_end]; + let (_head, values, _tail) = + unsafe { &value_slice.align_to::() }; + debug_assert_eq!(values.len(), num_cpus); + + callback(&key[0], values); + + index += Self::KEY_SIZE + (num_cpus * Self::VALUE_SIZE); + } + Ok(()) + } + } + } + + fn for_each( + &self, + callback: &mut dyn FnMut(&KEY, &VALUE), + ) -> Result<(), BpfIteratorError> { + let mut file = self.as_file()?; + let mut buf = Vec::new(); + let bytes_read = file.read_to_end(&mut buf); + match bytes_read { + Err(e) => { + log::error!("Unable to read from kernel map iterator file"); + log::error!("{e:?}"); + Err(BpfIteratorError::UnableToCreateIterator) + } + Ok(_) => { + let first_four_bytes: [u8; 4] = [buf[0], buf[1], buf[2], buf[3]]; + let num_cpus = u32::from_ne_bytes(first_four_bytes) as usize; + debug_assert_eq!(num_cpus, 1); + let mut index = 8; + while index + Self::TOTAL_SIZE <= buf.len() { + let key_start = index; + let key_end = key_start + Self::KEY_SIZE; + let key_slice = &buf[key_start..key_end]; + let (_head, key, _tail) = unsafe { &key_slice.align_to::() }; + + let value_start = key_end; + let value_end = value_start + Self::VALUE_SIZE; + let value_slice = &buf[value_start..value_end]; + let (_head, values, _tail) = + unsafe { &value_slice.align_to::() }; + + callback(&key[0], &values[0]); + + index += Self::KEY_SIZE + Self::VALUE_SIZE; + } + Ok(()) + } } } } @@ -101,70 +166,6 @@ impl Drop for BpfMapIterator { } } -/// Rust iterator for reading data from eBPF map iterators. -/// Transforms the data into the appropriate types, and returns -/// a tuple of the key, and a vector of values (1 per CPU for -/// CPU_MAP types, 1 entry for all others). -pub(crate) struct BpfMapIter { - buffer: Vec, - index: usize, - _phantom: PhantomData<(K, V)>, - num_cpus: usize, - row_data: Vec, -} - -impl BpfMapIter -where V: FromBytes + Debug + Clone + Default -{ - const KEY_SIZE: usize = std::mem::size_of::(); - const VALUE_SIZE: usize = std::mem::size_of::(); - const TOTAL_SIZE: usize = Self::KEY_SIZE + Self::VALUE_SIZE; - - /// Transforms the buffer into a Rust iterator. The buffer - /// is *moved* into the iterator, which retains ownership - /// throughout. - fn new(buffer: Vec) -> Self { - let first_four : [u8; 4] = [buffer[0], buffer[1], buffer[2], buffer[3]]; - let num_cpus = u32::from_ne_bytes(first_four) as usize; - //println!("CPUs: {num_cpus}"); - - Self { - buffer, - index: std::mem::size_of::(), - _phantom: PhantomData, - num_cpus, - row_data: vec![V::default(); num_cpus], - } - } -} - -impl Iterator for BpfMapIter -where - K: FromBytes + Debug + Clone, - V: FromBytes + Debug + Clone + Default, -{ - type Item = (K, Vec); - - fn next(&mut self) -> Option { - if self.index + Self::TOTAL_SIZE <= self.buffer.len() { - let key = K::read_from(&self.buffer[self.index..self.index + Self::KEY_SIZE]); - self.index += Self::KEY_SIZE; - for cpu in 0..self.num_cpus { - let value = V::read_from( - &self.buffer - [self.index ..self.index + Self::VALUE_SIZE], - ); - self.row_data[cpu] = value.unwrap(); - self.index += Self::VALUE_SIZE; - } - //println!("{key:?} {vals:?}"); - Some((key.unwrap(), self.row_data.clone())) - } else { - None - } - } -} - #[derive(Debug, Error)] enum BpfIteratorError { #[error("Failed to create iterator link")] @@ -183,7 +184,9 @@ static mut RTT_TRACKER: Lazy< Option>, > = Lazy::new(|| None); -pub unsafe fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter])) { +pub unsafe fn iterate_throughput( + callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter]), +) { if MAP_TRAFFIC.is_none() { let lock = BPF_SKELETON.lock().unwrap(); if let Some(skeleton) = lock.as_ref() { @@ -200,14 +203,13 @@ pub unsafe fn iterate_throughput(callback: &mut dyn FnMut(&XdpIpAddress, &[HostC } if let Some(iter) = MAP_TRAFFIC.as_mut() { - iter.iter().unwrap().for_each(|(k, v)| { - //println!("{:?} {:?}", k, v); - callback(&k, &v); - }); + let _ = iter.for_each_per_cpu(callback); } } -pub unsafe fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry)) { +pub unsafe fn iterate_rtt( + callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEntry), +) { if RTT_TRACKER.is_none() { let lock = BPF_SKELETON.lock().unwrap(); if let Some(skeleton) = lock.as_ref() { @@ -224,8 +226,6 @@ pub unsafe fn iterate_rtt(callback: &mut dyn FnMut(&XdpIpAddress, &RttTrackingEn } if let Some(iter) = RTT_TRACKER.as_mut() { - iter.iter().unwrap().for_each(|(k, v)| { - callback(&k, &v[0]); // Not per-CPU - }); + let _ = iter.for_each(callback); } } -- knotmirror 0.0.1 From d5becf71901027a0d45caf0ea3fc98df0bbc7d15 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 20:17:19 +0000 Subject: [PATCH 35/40] Now that we're differentiating between single-CPU and per-CPU map types, we can eliminate 8 bytes of bus traffic by not reporting CPU counts (and padding) --- src/rust/lqos_sys/src/bpf/lqos_kern.c | 6 ------ src/rust/lqos_sys/src/bpf_iterator.rs | 5 +---- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf/lqos_kern.c b/src/rust/lqos_sys/src/bpf/lqos_kern.c index 4eb0908a..52e4353f 100644 --- a/src/rust/lqos_sys/src/bpf/lqos_kern.c +++ b/src/rust/lqos_sys/src/bpf/lqos_kern.c @@ -374,18 +374,12 @@ int rtt_reader(struct bpf_iter__bpf_map_elem *ctx) struct seq_file *seq = ctx->meta->seq; struct rotating_performance *counter = ctx->value; struct in6_addr *ip = ctx->key; - __u32 num_cpus = 1; // Bail on end if (counter == NULL || ip == NULL) { return 0; } - if (ctx->meta->seq_num == 0) { - bpf_seq_write(seq, &num_cpus, sizeof(__u32)); - bpf_seq_write(seq, &num_cpus, sizeof(__u32)); // Padding - } - //BPF_SEQ_PRINTF(seq, "%d %d\n", counter->next_entry, counter->rtt[0]); bpf_seq_write(seq, ip, sizeof(struct in6_addr)); bpf_seq_write(seq, counter, sizeof(struct rotating_performance)); diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 2d06600e..087709ce 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -130,10 +130,7 @@ where Err(BpfIteratorError::UnableToCreateIterator) } Ok(_) => { - let first_four_bytes: [u8; 4] = [buf[0], buf[1], buf[2], buf[3]]; - let num_cpus = u32::from_ne_bytes(first_four_bytes) as usize; - debug_assert_eq!(num_cpus, 1); - let mut index = 8; + let mut index = 0; while index + Self::TOTAL_SIZE <= buf.len() { let key_start = index; let key_end = key_start + Self::KEY_SIZE; -- knotmirror 0.0.1 From 1e8ac640745f3b890047e1adf4d4f237560ddf6c Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 20:54:10 +0000 Subject: [PATCH 36/40] Update Heimdall to also use eBPF iterators This eliminates another set of syscalls per cycle. The old per_cpu map system is now removed, in favor of direct iteration of maps. --- src/rust/lqos_heimdall/src/flows.rs | 39 ++--------- src/rust/lqos_sys/src/bpf/common/heimdall.h | 2 + src/rust/lqos_sys/src/bpf/lqos_kern.c | 28 ++++++++ src/rust/lqos_sys/src/bpf_iterator.rs | 39 ++++++++++- src/rust/lqos_sys/src/bpf_per_cpu_map.rs | 77 --------------------- src/rust/lqos_sys/src/heimdall_data.rs | 33 +++++++++ src/rust/lqos_sys/src/lib.rs | 8 +-- 7 files changed, 108 insertions(+), 118 deletions(-) delete mode 100644 src/rust/lqos_sys/src/bpf_per_cpu_map.rs create mode 100644 src/rust/lqos_sys/src/heimdall_data.rs diff --git a/src/rust/lqos_heimdall/src/flows.rs b/src/rust/lqos_heimdall/src/flows.rs index c88313e2..215e093d 100644 --- a/src/rust/lqos_heimdall/src/flows.rs +++ b/src/rust/lqos_heimdall/src/flows.rs @@ -1,43 +1,11 @@ use crate::{timeline::expire_timeline, FLOW_EXPIRE_SECS}; use dashmap::DashMap; use lqos_bus::{tos_parser, BusResponse, FlowTransport}; -use lqos_sys::bpf_per_cpu_map::BpfPerCpuMap; +use lqos_sys::heimdall_data::{HeimdallKey, HeimdallData}; use lqos_utils::{unix_time::time_since_boot, XdpIpAddress}; use once_cell::sync::Lazy; use std::{collections::HashSet, time::Duration}; -/// Representation of the eBPF `heimdall_key` type. -#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] -#[repr(C)] -pub struct HeimdallKey { - /// Mapped `XdpIpAddress` source for the flow. - pub src_ip: XdpIpAddress, - /// Mapped `XdpIpAddress` destination for the flow - pub dst_ip: XdpIpAddress, - /// IP protocol (see the Linux kernel!) - pub ip_protocol: u8, - /// Source port number, or ICMP type. - pub src_port: u16, - /// Destination port number. - pub dst_port: u16, -} - -/// Mapped representation of the eBPF `heimdall_data` type. -#[derive(Debug, Clone, Default)] -#[repr(C)] -pub struct HeimdallData { - /// Last seen, in nanoseconds (since boot time). - pub last_seen: u64, - /// Number of bytes since the flow started being tracked - pub bytes: u64, - /// Number of packets since the flow started being tracked - pub packets: u64, - /// IP header TOS value - pub tos: u8, - /// Reserved to pad the structure - pub reserved: [u8; 3], -} - #[derive(Clone, Debug, PartialEq, Eq, Hash)] struct FlowKey { src: XdpIpAddress, @@ -95,11 +63,12 @@ static FLOW_DATA: Lazy> = Lazy::new(DashMap::new); fn heimdall_for_each( callback: &mut dyn FnMut(&HeimdallKey, &[HeimdallData]), ) { - if let Ok(heimdall) = BpfPerCpuMap::::from_path( + /*if let Ok(heimdall) = BpfPerCpuMap::::from_path( "/sys/fs/bpf/heimdall", ) { heimdall.for_each(callback); - } + }*/ + lqos_sys::iterate_heimdall(callback); } diff --git a/src/rust/lqos_sys/src/bpf/common/heimdall.h b/src/rust/lqos_sys/src/bpf/common/heimdall.h index 9657c8fb..9d5b8342 100644 --- a/src/rust/lqos_sys/src/bpf/common/heimdall.h +++ b/src/rust/lqos_sys/src/bpf/common/heimdall.h @@ -67,6 +67,7 @@ struct heimdall_key __u8 ip_protocol; __u16 src_port; __u16 dst_port; + __u8 pad; }; struct heimdall_data { @@ -155,6 +156,7 @@ static __always_inline void update_heimdall(struct dissector_t *dissector, __u32 { bpf_debug("Failed to insert tracking"); } + //bpf_debug("Inserted tracking"); } } else if (mode == 2) { struct heimdall_event event = {0}; diff --git a/src/rust/lqos_sys/src/bpf/lqos_kern.c b/src/rust/lqos_sys/src/bpf/lqos_kern.c index 52e4353f..c4980a69 100644 --- a/src/rust/lqos_sys/src/bpf/lqos_kern.c +++ b/src/rust/lqos_sys/src/bpf/lqos_kern.c @@ -386,4 +386,32 @@ int rtt_reader(struct bpf_iter__bpf_map_elem *ctx) return 0; } +SEC("iter/bpf_map_elem") +int heimdall_reader(struct bpf_iter__bpf_map_elem *ctx) { + // The sequence file + struct seq_file *seq = ctx->meta->seq; + void *counter = ctx->value; + struct heimdall_key *ip = ctx->key; + __u32 num_cpus = NUM_CPUS; + + if (ctx->meta->seq_num == 0) { + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); + bpf_seq_write(seq, &num_cpus, sizeof(__u32)); // Repeat for padding + } + + // Bail on end + if (counter == NULL || ip == NULL) { + return 0; + } + + bpf_seq_write(seq, ip, sizeof(struct heimdall_key)); + for (__u32 i=0; idownload_bytes, counter->upload_bytes); + return 0; +} + char _license[] SEC("license") = "GPL"; diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 087709ce..25c8b044 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -1,6 +1,6 @@ use crate::{ kernel_wrapper::BPF_SKELETON, lqos_kernel::bpf, HostCounter, - RttTrackingEntry, + RttTrackingEntry, heimdall_data::{HeimdallKey, HeimdallData}, }; use lqos_utils::XdpIpAddress; use once_cell::sync::Lazy; @@ -90,7 +90,11 @@ where log::error!("{e:?}"); Err(BpfIteratorError::UnableToCreateIterator) } - Ok(_) => { + Ok(bytes) => { + if bytes == 0 { + // Not having any data is not an error + return Ok(()); + } let first_four_bytes: [u8; 4] = [buf[0], buf[1], buf[2], buf[3]]; let num_cpus = u32::from_ne_bytes(first_four_bytes) as usize; let mut index = 8; @@ -98,11 +102,13 @@ where let key_start = index; let key_end = key_start + Self::KEY_SIZE; let key_slice = &buf[key_start..key_end]; + //println!("{:?}", unsafe { &key_slice.align_to::() }); let (_head, key, _tail) = unsafe { &key_slice.align_to::() }; let value_start = key_end; let value_end = value_start + (num_cpus * Self::VALUE_SIZE); let value_slice = &buf[value_start..value_end]; + //println!("{:?}", unsafe { &value_slice.align_to::() }); let (_head, values, _tail) = unsafe { &value_slice.align_to::() }; debug_assert_eq!(values.len(), num_cpus); @@ -181,6 +187,10 @@ static mut RTT_TRACKER: Lazy< Option>, > = Lazy::new(|| None); +static mut HEIMDALL_TRACKER: Lazy< + Option>, +> = Lazy::new(|| None); + pub unsafe fn iterate_throughput( callback: &mut dyn FnMut(&XdpIpAddress, &[HostCounter]), ) { @@ -226,3 +236,28 @@ pub unsafe fn iterate_rtt( let _ = iter.for_each(callback); } } + +pub fn iterate_heimdall( + callback: &mut dyn FnMut(&HeimdallKey, &[HeimdallData]), +) { + unsafe { + if HEIMDALL_TRACKER.is_none() { + let lock = BPF_SKELETON.lock().unwrap(); + if let Some(skeleton) = lock.as_ref() { + let skeleton = skeleton.get_ptr(); + if let Ok(iter) = { + BpfMapIterator::new( + (*skeleton).progs.heimdall_reader, + (*skeleton).maps.heimdall, + ) + } { + *HEIMDALL_TRACKER = Some(iter); + } + } + } + + if let Some(iter) = HEIMDALL_TRACKER.as_mut() { + let _ = iter.for_each_per_cpu(callback); + } + } +} \ No newline at end of file diff --git a/src/rust/lqos_sys/src/bpf_per_cpu_map.rs b/src/rust/lqos_sys/src/bpf_per_cpu_map.rs deleted file mode 100644 index 5fdc31a2..00000000 --- a/src/rust/lqos_sys/src/bpf_per_cpu_map.rs +++ /dev/null @@ -1,77 +0,0 @@ -use anyhow::{Error, Result}; -use libbpf_sys::{ - bpf_map_get_next_key, bpf_map_lookup_elem, bpf_obj_get, -}; -use std::fmt::Debug; -use std::{ - ffi::{c_void, CString}, - marker::PhantomData, - ptr::null_mut, -}; - -use crate::num_possible_cpus; - -/// Represents an underlying BPF map, accessed via the filesystem. -/// `BpfMap` *only* talks to PER-CPU variants of maps. -/// -/// `K` is the *key* type, indexing the map. -/// `V` is the *value* type, and must exactly match the underlying C data type. -pub struct BpfPerCpuMap { - fd: i32, - _key_phantom: PhantomData, - _val_phantom: PhantomData, -} - -impl BpfPerCpuMap -where - K: Default + Clone, - V: Default + Clone + Debug, -{ - /// Connect to a PER-CPU BPF map via a filename. Connects the internal - /// file descriptor, which is held until the structure is - /// dropped. The index of the CPU is *not* specified. - pub fn from_path(filename: &str) -> Result { - let filename_c = CString::new(filename)?; - let fd = unsafe { bpf_obj_get(filename_c.as_ptr()) }; - if fd < 0 { - Err(Error::msg("Unable to open BPF map")) - } else { - Ok(Self { fd, _key_phantom: PhantomData, _val_phantom: PhantomData }) - } - } - - /// Instead of clonining into a vector - /// and allocating, calls `callback` for each key/value slice - /// with references to the data returned from C. - pub fn for_each(&self, callback: &mut dyn FnMut(&K, &[V])) { - let num_cpus = num_possible_cpus().unwrap(); - let mut prev_key: *mut K = null_mut(); - let mut key: K = K::default(); - let key_ptr: *mut K = &mut key; - let mut value = vec![V::default(); num_cpus as usize]; - let value_ptr = value.as_mut_ptr(); - - unsafe { - while bpf_map_get_next_key( - self.fd, - prev_key as *mut c_void, - key_ptr as *mut c_void, - ) == 0 - { - bpf_map_lookup_elem( - self.fd, - key_ptr as *mut c_void, - value_ptr as *mut c_void, - ); - callback(&key, &value); - prev_key = key_ptr; - } - } - } -} - -impl Drop for BpfPerCpuMap { - fn drop(&mut self) { - let _ = nix::unistd::close(self.fd); - } -} diff --git a/src/rust/lqos_sys/src/heimdall_data.rs b/src/rust/lqos_sys/src/heimdall_data.rs new file mode 100644 index 00000000..31be4bca --- /dev/null +++ b/src/rust/lqos_sys/src/heimdall_data.rs @@ -0,0 +1,33 @@ +use lqos_utils::XdpIpAddress; +use zerocopy::FromBytes; + +/// Representation of the eBPF `heimdall_key` type. +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, FromBytes)] +#[repr(C)] +pub struct HeimdallKey { + /// Mapped `XdpIpAddress` source for the flow. + pub src_ip: XdpIpAddress, + /// Mapped `XdpIpAddress` destination for the flow + pub dst_ip: XdpIpAddress, + /// IP protocol (see the Linux kernel!) + pub ip_protocol: u8, + /// Source port number, or ICMP type. + pub src_port: u16, + /// Destination port number. + pub dst_port: u16, + _padding: u8, +} + +/// Mapped representation of the eBPF `heimdall_data` type. +#[derive(Debug, Clone, Default, FromBytes)] +#[repr(C)] +pub struct HeimdallData { + /// Last seen, in nanoseconds (since boot time). + pub last_seen: u64, + /// Number of bytes since the flow started being tracked + pub bytes: u64, + /// Number of packets since the flow started being tracked + pub packets: u64, + /// IP header TOS value + pub tos: u8, +} \ No newline at end of file diff --git a/src/rust/lqos_sys/src/lib.rs b/src/rust/lqos_sys/src/lib.rs index c3c64aba..d8ac2c3e 100644 --- a/src/rust/lqos_sys/src/lib.rs +++ b/src/rust/lqos_sys/src/lib.rs @@ -11,10 +11,6 @@ mod bifrost_maps; /// built-in, compiled eBPF programs. This is very-low level and should /// be handled with caution. pub mod bpf_map; -/// Provides direct access to LibBPF functionality, as exposed by the -/// built-in, compiled eBPF programs. This is very-low level and should -/// be handled with caution. -pub mod bpf_per_cpu_map; mod cpu_map; mod ip_mapping; mod kernel_wrapper; @@ -23,6 +19,9 @@ mod tcp_rtt; mod throughput; mod linux; mod bpf_iterator; +/// Data shared between eBPF and Heimdall that needs local access +/// for map control. +pub mod heimdall_data; pub use ip_mapping::{ add_ip_to_tc, clear_ips_from_tc, del_ip_from_tc, list_mapped_ips, @@ -32,3 +31,4 @@ pub use linux::num_possible_cpus; pub use lqos_kernel::max_tracked_ips; pub use tcp_rtt::{rtt_for_each, RttTrackingEntry}; pub use throughput::{throughput_for_each, HostCounter}; +pub use bpf_iterator::iterate_heimdall; \ No newline at end of file -- knotmirror 0.0.1 From 775a903f2d8429977b0b1d3765767b99925c0891 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 20:55:16 +0000 Subject: [PATCH 37/40] Missing doctag --- src/rust/lqos_sys/src/bpf_iterator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rust/lqos_sys/src/bpf_iterator.rs b/src/rust/lqos_sys/src/bpf_iterator.rs index 25c8b044..f3c77c0e 100644 --- a/src/rust/lqos_sys/src/bpf_iterator.rs +++ b/src/rust/lqos_sys/src/bpf_iterator.rs @@ -237,6 +237,7 @@ pub unsafe fn iterate_rtt( } } +/// Iterate through the heimdall map and call the callback for each entry. pub fn iterate_heimdall( callback: &mut dyn FnMut(&HeimdallKey, &[HeimdallData]), ) { -- knotmirror 0.0.1 From 1727468e668393334b731169eb669f0c1fd3acfa Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 20:56:56 +0000 Subject: [PATCH 38/40] Remove now unused code. --- src/rust/lqos_sys/src/bpf_map.rs | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf_map.rs b/src/rust/lqos_sys/src/bpf_map.rs index f52aa7e1..29bb8c24 100644 --- a/src/rust/lqos_sys/src/bpf_map.rs +++ b/src/rust/lqos_sys/src/bpf_map.rs @@ -72,35 +72,6 @@ where result } - /// Iterates the undlering BPF map, and sends references to the - /// results directly to a callback - pub fn for_each(&self, callback: &mut dyn FnMut(&K, &V)) { - let mut prev_key: *mut K = null_mut(); - let mut key: K = K::default(); - let key_ptr: *mut K = &mut key; - let mut value = V::default(); - let value_ptr: *mut V = &mut value; - - unsafe { - while bpf_map_get_next_key( - self.fd, - prev_key as *mut c_void, - key_ptr as *mut c_void, - ) == 0 - { - bpf_map_lookup_elem( - self.fd, - key_ptr as *mut c_void, - value_ptr as *mut c_void, - ); - //result.push((key.clone(), value.clone())); - println!("old callback"); - callback(&key, &value); - prev_key = key_ptr; - } - } - } - /// Inserts an entry into a BPF map. /// Use this sparingly, because it briefly pauses XDP access to the /// underlying map (through internal locking we can't reach from -- knotmirror 0.0.1 From 54d59af34705923aaec9e8a0a3e108d41e56c676 Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Wed, 19 Apr 2023 21:29:15 +0000 Subject: [PATCH 39/40] Reduce default maximum tracked hosts to 64k from 128k --- src/rust/lqos_sys/src/bpf/common/maximums.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rust/lqos_sys/src/bpf/common/maximums.h b/src/rust/lqos_sys/src/bpf/common/maximums.h index 4c20f219..7520cbc9 100644 --- a/src/rust/lqos_sys/src/bpf/common/maximums.h +++ b/src/rust/lqos_sys/src/bpf/common/maximums.h @@ -1,10 +1,10 @@ #pragma once // Maximum number of client IPs we are tracking -#define MAX_TRACKED_IPS 128000 +#define MAX_TRACKED_IPS 64000 // Maximum number of TC class mappings to support -#define IP_HASH_ENTRIES_MAX 128000 +#define IP_HASH_ENTRIES_MAX 64000 // Maximum number of supported CPUs #define MAX_CPUS 1024 -- knotmirror 0.0.1 From 71e7610c752344b69a9242ea052960d803838dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Chac=C3=B3n?= Date: Wed, 19 Apr 2023 19:09:56 -0600 Subject: [PATCH 40/40] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 46789d81..21c20fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -44,6 +44,8 @@ src/statsByParentNode.json src/lastGoodConfig.json src/lastGoodConfig.csv src/mikrotikDHCPRouterList.csv +src/integrationUISPbandwidths.csv +src/integrationUISProutes.csv src/longTermStats.json src/queuingStructure.json src/tinsStats.json -- knotmirror 0.0.1