forked from osrg/bgperf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtester.py
115 lines (99 loc) · 3.96 KB
/
tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from base import Tester
from exabgp import ExaBGP
from bird import BIRD
from settings import dckr
from subprocess import check_output, Popen, PIPE
class ExaBGPTester(Tester, ExaBGP):
CONTAINER_NAME_PREFIX = 'bgperf_exabgp_tester_'
def __init__(self, name, host_dir, conf, image='bgperf/exabgp'):
super(ExaBGPTester, self).__init__(name, host_dir, conf, image)
def configure_neighbors(self, target_conf):
peers = list(self.conf.get('neighbors', {}).values())
for p in peers:
with open('{0}/{1}.conf'.format(self.host_dir, p['router-id']), 'w') as f:
local_address = p['local-address']
config = '''neighbor {0} {{
peer-as {1};
router-id {2};
local-address {3};
local-as {4};
static {{
'''.format(target_conf['local-address'], target_conf['as'],
p['router-id'], local_address, p['as'])
f.write(config)
for path in p['paths']:
f.write(' route {0} next-hop {1};\n'.format(path, local_address))
f.write(''' }
}''')
def get_startup_cmd(self):
startup = ['''#!/bin/bash
ulimit -n 65536''']
peers = list(self.conf.get('neighbors', {}).values())
for p in peers:
startup.append('''env exabgp.log.destination={0}/{1}.log \
exabgp.daemon.daemonize=true \
exabgp.daemon.user=root \
exabgp {0}/{1}.conf'''.format(self.guest_dir, p['router-id']))
return '\n'.join(startup)
class BIRDTester(Tester, BIRD):
CONTAINER_NAME_PREFIX = 'bgperf_bird_tester_'
def __init__(self, name, host_dir, conf, image='bgperf/bird'):
super(BIRDTester, self).__init__('bgperf_bird_' + name, host_dir, conf, image)
def configure_neighbors(self, target_conf):
peers = list(self.conf.get('neighbors', {}).values())
for p in peers:
with open('{0}/{1}.conf'.format(self.host_dir, p['router-id']), 'w') as f:
local_address = p['local-address']
config = '''log "{5}/{2}.log" all;
#debug protocols all;
debug protocols {{states}};
router id {2};
protocol device {{}}
protocol bgp {{
#hold time 5;
source address {3};
connect delay time 1;
interface "eth0";
strict bind;
ipv4 {{ import none; export all; }};
local {3} as {4};
neighbor {0} as {1};
}}
protocol static {{ ipv4;
'''.format(target_conf['local-address'], target_conf['as'],
p['router-id'], local_address, p['as'], self.guest_dir)
f.write(config)
for path in p['paths']:
f.write(' route {0} via {1};\n'.format(path, local_address))
f.write('}')
def get_startup_cmd(self):
startup = [f'''#!/bin/bash
ulimit -n 65536
#sleep 2
#(ip link; ip addr) > {self.guest_dir}/ip-a.log
''']
peers = list(self.conf.get('neighbors', {}).values())
for p in peers:
startup.append('''bird -c {0}/{1}.conf -s {0}/{1}.ctl >>{0}/{1}.log 2>&1\n'''.format(self.guest_dir, p['router-id']))
return '\n'.join(startup)
def find_errors():
grep1 = Popen(('grep RMT /tmp/bgperf/tester/*.log'), shell=True, stdout=PIPE)
grep2 = Popen(('grep', '-v', 'NEXT_HOP'), stdin=grep1.stdout, stdout=PIPE)
errors = check_output(('wc', '-l'), stdin=grep2.stdout)
grep1.wait()
grep2.wait()
return errors.decode('utf-8').strip()