Allow for multiple IPs per entry in ShapedDevices.csv, seperated by commas

This commit is contained in:
Robert Chacón 2022-09-06 16:33:15 -06:00 committed by GitHub
parent 11bdc0e5cb
commit ba5ce67938
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,17 +48,40 @@ def refreshShapers():
csv_reader = csv.reader(csv_file, delimiter=',') csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader) next(csv_reader)
for row in csv_reader: for row in csv_reader:
circuitID, circuitName, deviceID, deviceName, ParentNode, mac, ipv4, ipv6, downloadMin, uploadMin, downloadMax, uploadMax = row circuitID, circuitName, deviceID, deviceName, ParentNode, mac, ipv4_input, ipv6_input, downloadMin, uploadMin, downloadMax, uploadMax = row
if ipv4 != "": ipv4_hosts = []
if ipv4_input != "":
try: try:
a = ipaddress.ip_address(ipv4) ipv4_input = ipv4_input.replace(' ','')
if "," in ipv4_input:
ipv4_list = ipv4_input.split(',')
else:
ipv4_list = [ipv4_input]
for ipEntry in ipv4_list:
if '/32' in ipEntry:
ipv4_hosts.append(ipaddress.ip_address(ipEntry))
elif '/' in ipEntry:
ipv4_hosts.extend(list(ipaddress.ip_network(ipEntry).hosts()))
else:
ipv4_hosts.append(ipaddress.ip_address(ipEntry))
except ValueError as e: except ValueError as e:
raise Exception("Provided IPv4 '" + ipv4 + "' in ShapedDevices.csv at row " + str(rowNum) + " is not valid.") from e raise Exception("Provided IPv4 '" + ipv4_input + "' in ShapedDevices.csv at row " + str(rowNum) + " is not valid.") from e
if ipv6 != "": if ipv6_input != "":
try: try:
a = ipaddress.ip_address(ipv6) ipv6_input = ipv6_input.replace(' ','')
if "," in ipv6_input:
ipv6_list = ipv6_input.split(',')
else:
ipv6_list = [ipv6_input]
for ipEntry in ipv6_list:
if '/128' in ipEntry:
ipv6_hosts.append(ipaddress.ip_address(ipEntry))
elif '/' in ipEntry:
ipv6_hosts.extend(list(ipaddress.ip_network(ipEntry).hosts()))
else:
ipv6_hosts.append(ipaddress.ip_address(ipEntry))
except ValueError as e: except ValueError as e:
raise Exception("Provided IPv6 '" + ipv6 + "' in ShapedDevices.csv at row " + str(rowNum) + " is not valid.") from e raise Exception("Provided IPv6 '" + ipv6_input + "' in ShapedDevices.csv at row " + str(rowNum) + " is not valid.") from e
try: try:
a = int(downloadMin) a = int(downloadMin)
except ValueError as e: except ValueError as e:
@ -84,7 +107,35 @@ def refreshShapers():
csv_reader = csv.reader(csv_file, delimiter=',') csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader) next(csv_reader)
for row in csv_reader: for row in csv_reader:
circuitID, circuitName, deviceID, deviceName, ParentNode, mac, ipv4, ipv6, downloadMin, uploadMin, downloadMax, uploadMax = row circuitID, circuitName, deviceID, deviceName, ParentNode, mac, ipv4_input, ipv6_input, downloadMin, uploadMin, downloadMax, uploadMax = row
ipv4_hosts = []
if ipv4_input != "":
ipv4_input = ipv4_input.replace(' ','')
if "," in ipv4_input:
ipv4_list = ipv4_input.split(',')
else:
ipv4_list = [ipv4_input]
for ipEntry in ipv4_list:
if '/32' in ipEntry:
ipv4_hosts.append(ipaddress.ip_address(ipEntry))
elif '/' in ipEntry:
ipv4_hosts.extend(list(ipaddress.ip_network(ipEntry).hosts()))
else:
ipv4_hosts.append(ipaddress.ip_address(ipEntry))
ipv6_hosts = []
if ipv6_input != "":
ipv6_input = ipv6_input.replace(' ','')
if "," in ipv6_input:
ipv6_list = ipv6_input.split(',')
else:
ipv6_list = [ipv6_input]
for ipEntry in ipv6_list:
if '/128' in ipEntry:
ipv6_hosts.append(ipaddress.ip_address(ipEntry))
elif '/' in ipEntry:
ipv6_hosts.extend(list(ipaddress.ip_network(ipEntry).hosts()))
else:
ipv6_hosts.append(ipaddress.ip_address(ipEntry))
if circuitID != "": if circuitID != "":
if circuitID in knownCircuitIDs: if circuitID in knownCircuitIDs:
for circuit in subscriberCircuits: for circuit in subscriberCircuits:
@ -102,15 +153,13 @@ def refreshShapers():
"deviceID": deviceID, "deviceID": deviceID,
"deviceName": deviceName, "deviceName": deviceName,
"mac": mac, "mac": mac,
"ipv4": ipv4, "ipv4s": ipv4_hosts,
"ipv6": ipv6, "ipv6s": ipv6_hosts,
} }
devicesListForCircuit.append(thisDevice) devicesListForCircuit.append(thisDevice)
circuit['devices'] = devicesListForCircuit circuit['devices'] = devicesListForCircuit
else: else:
knownCircuitIDs.append(circuitID) knownCircuitIDs.append(circuitID)
ipv4 = ipv4.strip()
ipv6 = ipv6.strip()
if ParentNode == "": if ParentNode == "":
ParentNode = "none" ParentNode = "none"
ParentNode = ParentNode.strip() ParentNode = ParentNode.strip()
@ -119,8 +168,8 @@ def refreshShapers():
"deviceID": deviceID, "deviceID": deviceID,
"deviceName": deviceName, "deviceName": deviceName,
"mac": mac, "mac": mac,
"ipv4": ipv4, "ipv4s": ipv4_hosts,
"ipv6": ipv6, "ipv6s": ipv6_hosts,
} }
deviceListForCircuit.append(thisDevice) deviceListForCircuit.append(thisDevice)
thisCircuit = { thisCircuit = {
@ -136,8 +185,8 @@ def refreshShapers():
} }
subscriberCircuits.append(thisCircuit) subscriberCircuits.append(thisCircuit)
else: else:
ipv4 = ipv4.strip() if circuitName == "":
ipv6 = ipv6.strip() circuitName = deviceName
if ParentNode == "": if ParentNode == "":
ParentNode = "none" ParentNode = "none"
ParentNode = ParentNode.strip() ParentNode = ParentNode.strip()
@ -146,8 +195,8 @@ def refreshShapers():
"deviceID": deviceID, "deviceID": deviceID,
"deviceName": deviceName, "deviceName": deviceName,
"mac": mac, "mac": mac,
"ipv4": ipv4, "ipv4s": ipv4_hosts,
"ipv6": ipv6, "ipv6s": ipv6_hosts,
} }
deviceListForCircuit.append(thisDevice) deviceListForCircuit.append(thisDevice)
thisCircuit = { thisCircuit = {
@ -295,21 +344,18 @@ def refreshShapers():
shell('tc class add dev ' + interfaceB + ' parent ' + elemClassID + ' classid ' + hex(minor) + ' htb rate '+ str(minUpload) + 'mbit ceil '+ str(maxUpload) + 'mbit prio 3') shell('tc class add dev ' + interfaceB + ' parent ' + elemClassID + ' classid ' + hex(minor) + ' htb rate '+ str(minUpload) + 'mbit ceil '+ str(maxUpload) + 'mbit prio 3')
print(tabs + ' ', end='') print(tabs + ' ', end='')
shell('tc qdisc add dev ' + interfaceB + ' parent ' + hex(major) + ':' + hex(minor) + ' ' + fqOrCAKE) shell('tc qdisc add dev ' + interfaceB + ' parent ' + hex(major) + ':' + hex(minor) + ' ' + fqOrCAKE)
parentString = hex(major) + ':'
flowIDstring = hex(major) + ':' + hex(minor)
circuit['qdisc'] = flowIDstring
for device in circuit['devices']: for device in circuit['devices']:
if device['ipv4']: if device['ipv4s']:
parentString = hex(major) + ':' for ipv4 in device['ipv4s']:
flowIDstring = hex(major) + ':' + hex(minor) if '/32' in ipv4:
if '/' in device['ipv4']: ipv4 = ipv4.replace('/32','')
hosts = list(ipaddress.ip_network(device['ipv4']).hosts())
for host in hosts:
print(tabs + ' ', end='')
shell('./xdp-cpumap-tc/src/xdp_iphash_to_cpu_cmdline --add --ip ' + str(host) + ' --cpu ' + hex(queue-1) + ' --classid ' + flowIDstring)
else:
print(tabs + ' ', end='') print(tabs + ' ', end='')
shell('./xdp-cpumap-tc/src/xdp_iphash_to_cpu_cmdline --add --ip ' + device['ipv4'] + ' --cpu ' + hex(queue-1) + ' --classid ' + flowIDstring) shell('./xdp-cpumap-tc/src/xdp_iphash_to_cpu_cmdline --add --ip ' + str(ipv4) + ' --cpu ' + hex(queue-1) + ' --classid ' + flowIDstring)
circuit['qdisc'] = flowIDstring if device['deviceName'] not in devicesShaped:
if device['deviceName'] not in devicesShaped: devicesShaped.append(device['deviceName'])
devicesShaped.append(device['deviceName'])
print() print()
minor += 1 minor += 1
#Recursive call this function for children nodes attached to this node #Recursive call this function for children nodes attached to this node