This repository has been archived by the owner on Sep 28, 2024. It is now read-only.
forked from Netronome/bpf-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l4lb_map.py
executable file
·75 lines (61 loc) · 2.64 KB
/
l4lb_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
# Copyright (c) 2018 Netronome Systems, Inc.
import argparse
import csv
import json
import os
import subprocess
from bpftool_utils import *
parser = argparse.ArgumentParser(description="Initialize load balancer map")
parser.add_argument('-i', '--interface', action='store', required=True,
help='xdp network interface')
parser.add_argument('-f', '--file', action='store', required=True,
help='Input file containing destination data')
parser.add_argument('-s', '--saddr', action='store', required=False, type=str,
default='10.0.0.1', help='source ip for outgoing packets')
args = parser.parse_args()
tmpfile = 'tmp_bpftool.txt' # batch map updates into a file for bpftool
stats_zero = '00 00 00 00 00 00 00 00' # initialize stats to (u64) zero
interface = args.interface
try:
map_id = str(get_map_ids(interface)[0])
max_dest = get_map_entries(map_id) # map size as defined by xdp program
except:
print("Error finding map for dev: %s" % interface)
sys.exit(1)
try:
dest_file = open(args.file, 'r')
dest_hosts = list(csv.reader(dest_file))
dest_count = len(dest_hosts)
except:
print("Error reading file")
sys.exit(1)
if dest_count > max_dest:
print("Warning: only the first %d destinations will be used" % max_dest)
else:
print("Loading file with %d destinations" % dest_count)
batchfile = open(tmpfile, 'w')
# iterate through data in input file, if it contains less values than max_dest
# map will be filled as round robin
for key in range (0, max_dest):
target_id = key % dest_count
# bpftool requires data as individual bytes
keyval1 = str(key & 0xFF)
keyval2 = str(key >> 8)
saddr = args.saddr.split('.') # source IP for egress packets
daddr = dest_hosts[target_id][0].split('.') # IP of the target server
dmac_hex = dest_hosts[target_id][1].split(':') # MAC of the target server
dmac = [str(int(byte, 16)) for byte in dmac_hex] # convert hex to integers
# Fill in map using struct iptnl_info arrangement as specified in l4lb_xdp.c
COMMAND = (['map update id', map_id,
'key', keyval1, keyval2, '00 00',
'value', saddr[0], saddr[1], saddr[2], saddr[3],
daddr[0], daddr[1], daddr[2], daddr[3],
stats_zero, stats_zero,
dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5],
'00 00 \n'])
COMMAND = ' '.join(COMMAND)
batchfile.write(COMMAND)
batchfile.close()
subprocess.check_output('bpftool batch file %s' % tmpfile, shell=True)