2018-08-16 09:56:15 +00:00
|
|
|
"""
|
|
|
|
Task to run tests with network delay between two remotes using tc and netem.
|
|
|
|
Reference:https://wiki.linuxfoundation.org/networking/netem.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import contextlib
|
|
|
|
from teuthology import misc as teuthology
|
|
|
|
from cStringIO import StringIO
|
|
|
|
from teuthology.orchestra import run
|
|
|
|
from teuthology import contextutil
|
|
|
|
from paramiko import SSHException
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import gevent
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def set_priority(interface):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# create a priority queueing discipline
|
2018-08-20 13:57:37 +00:00
|
|
|
return ['sudo', 'tc', 'qdisc', 'add', 'dev', interface, 'root', 'handle', '1:', 'prio']
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def show_tc(interface):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# shows tc device present
|
2018-08-20 13:57:37 +00:00
|
|
|
return ['sudo', 'tc', 'qdisc', 'show', 'dev', interface]
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def del_tc(interface):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
return ['sudo', 'tc', 'qdisc', 'del', 'dev', interface, 'root']
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def cmd_prefix(interface):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# prepare command to set delay
|
2018-08-20 13:57:37 +00:00
|
|
|
cmd1 = ['sudo', 'tc', 'qdisc', 'add', 'dev', interface, 'parent',
|
2018-08-16 09:56:15 +00:00
|
|
|
'1:1', 'handle', '2:', 'netem', 'delay']
|
|
|
|
|
|
|
|
# prepare command to change delay
|
2018-08-20 13:57:37 +00:00
|
|
|
cmd2 = ['sudo', 'tc', 'qdisc', 'replace', 'dev', interface, 'root', 'netem', 'delay']
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# prepare command to apply filter to the matched ip/host
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
cmd3 = ['sudo', 'tc', 'filter', 'add', 'dev', interface,
|
2018-08-16 09:56:15 +00:00
|
|
|
'parent', '1:0', 'protocol', 'ip', 'pref', '55',
|
|
|
|
'handle', '::55', 'u32', 'match', 'ip', 'dst']
|
|
|
|
|
|
|
|
return cmd1, cmd2, cmd3
|
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def static_delay(remote, host, interface, delay):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
""" Sets a constant delay between two hosts to emulate network delays using tc qdisc and netem"""
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
set_delay, change_delay, set_ip = cmd_prefix(interface)
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
ip = socket.gethostbyname(host.hostname)
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
r = remote.run(args=show_tc(interface), stdout=StringIO())
|
2018-08-16 09:56:15 +00:00
|
|
|
if r.stdout.getvalue().strip().find('refcnt') == -1:
|
|
|
|
# call set_priority() func to create priority queue
|
|
|
|
# if not already created(indicated by -1)
|
|
|
|
log.info('Create priority queue')
|
2018-08-20 13:57:37 +00:00
|
|
|
remote.run(args=set_priority(interface))
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# set static delay, with +/- 5ms jitter with normal distribution as default
|
|
|
|
log.info('Setting delay to %s' % delay)
|
|
|
|
set_delay.extend(['%s' % delay, '5ms', 'distribution', 'normal'])
|
|
|
|
remote.run(args=set_delay)
|
|
|
|
|
|
|
|
# set delay to a particular remote node via ip
|
|
|
|
log.info('Delay set on %s' % remote)
|
|
|
|
set_ip.extend(['%s' % ip, 'flowid', '2:1'])
|
|
|
|
remote.run(args=set_ip)
|
|
|
|
else:
|
|
|
|
# if the device is already created, only change the delay
|
|
|
|
log.info('Setting delay to %s' % delay)
|
|
|
|
change_delay.extend(['%s' % delay, '5ms', 'distribution', 'normal'])
|
|
|
|
remote.run(args=change_delay)
|
2018-09-03 07:39:02 +00:00
|
|
|
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def variable_delay(remote, host, interface, delay_range=[]):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
""" Vary delay between two values"""
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
set_delay, change_delay, set_ip = cmd_prefix(interface)
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
ip = socket.gethostbyname(host.hostname)
|
|
|
|
|
|
|
|
# delay1 has to be lower than delay2
|
|
|
|
delay1 = delay_range[0]
|
|
|
|
delay2 = delay_range[1]
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
r = remote.run(args=show_tc(interface), stdout=StringIO())
|
2018-08-16 09:56:15 +00:00
|
|
|
if r.stdout.getvalue().strip().find('refcnt') == -1:
|
|
|
|
# call set_priority() func to create priority queue
|
|
|
|
# if not already created(indicated by -1)
|
2018-08-20 13:57:37 +00:00
|
|
|
remote.run(args=set_priority(interface))
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
# set variable delay
|
|
|
|
log.info('Setting varying delay')
|
|
|
|
set_delay.extend(['%s' % delay1, '%s' % delay2])
|
|
|
|
remote.run(args=set_delay)
|
|
|
|
|
|
|
|
# set delay to a particular remote node via ip
|
|
|
|
log.info('Delay set on %s' % remote)
|
|
|
|
set_ip.extend(['%s' % ip, 'flowid', '2:1'])
|
|
|
|
remote.run(args=set_ip)
|
|
|
|
else:
|
|
|
|
# if the device is already created, only change the delay
|
|
|
|
log.info('Setting varying delay')
|
|
|
|
change_delay.extend(['%s' % delay1, '%s' % delay2])
|
|
|
|
remote.run(args=change_delay)
|
|
|
|
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
def delete_dev(remote, interface):
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
""" Delete the qdisc if present"""
|
|
|
|
|
|
|
|
log.info('Delete tc')
|
2018-08-20 13:57:37 +00:00
|
|
|
r = remote.run(args=show_tc(interface), stdout=StringIO())
|
2018-08-16 09:56:15 +00:00
|
|
|
if r.stdout.getvalue().strip().find('refcnt') != -1:
|
2018-08-20 13:57:37 +00:00
|
|
|
remote.run(args=del_tc(interface))
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Toggle:
|
|
|
|
|
|
|
|
stop_event = gevent.event.Event()
|
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
def __init__(self, ctx, remote, host, interface, interval):
|
|
|
|
self.ctx = ctx
|
2018-08-16 09:56:15 +00:00
|
|
|
self.remote = remote
|
|
|
|
self.host = host
|
|
|
|
self.interval = interval
|
2018-08-20 13:57:37 +00:00
|
|
|
self.interface = interface
|
2018-08-16 09:56:15 +00:00
|
|
|
self.ip = socket.gethostbyname(self.host.hostname)
|
|
|
|
|
|
|
|
def packet_drop(self):
|
|
|
|
|
|
|
|
""" Drop packets to the remote ip specified"""
|
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
_, _, set_ip = cmd_prefix(self.interface)
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-08-20 13:57:37 +00:00
|
|
|
r = self.remote.run(args=show_tc(self.interface), stdout=StringIO())
|
2018-08-16 09:56:15 +00:00
|
|
|
if r.stdout.getvalue().strip().find('refcnt') == -1:
|
2018-08-20 13:57:37 +00:00
|
|
|
self.remote.run(args=set_priority(self.interface))
|
2018-08-16 09:56:15 +00:00
|
|
|
# packet drop to specific ip
|
|
|
|
log.info('Drop all packets to %s' % self.host)
|
|
|
|
set_ip.extend(['%s' % self.ip, 'action', 'drop'])
|
|
|
|
self.remote.run(args=set_ip)
|
|
|
|
|
|
|
|
def link_toggle(self):
|
2018-09-03 07:39:02 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
For toggling packet drop and recovery in regular interval.
|
|
|
|
If interval is 5s, link is up for 5s and link is down for 5s
|
|
|
|
"""
|
|
|
|
|
|
|
|
while not self.stop_event.is_set():
|
|
|
|
self.stop_event.wait(timeout=self.interval)
|
|
|
|
# simulate link down
|
|
|
|
try:
|
|
|
|
self.packet_drop()
|
|
|
|
log.info('link down')
|
|
|
|
except SSHException as e:
|
|
|
|
log.debug('Failed to run command')
|
|
|
|
|
|
|
|
self.stop_event.wait(timeout=self.interval)
|
|
|
|
# if qdisc exist,delete it.
|
|
|
|
try:
|
|
|
|
delete_dev(self.remote, self.interface)
|
|
|
|
log.info('link up')
|
|
|
|
except SSHException as e:
|
|
|
|
log.debug('Failed to run command')
|
|
|
|
|
|
|
|
def begin(self, gname):
|
2018-08-16 09:56:15 +00:00
|
|
|
self.thread = gevent.spawn(self.link_toggle)
|
2018-09-03 07:39:02 +00:00
|
|
|
self.ctx.netem.names[gname] = self.thread
|
2018-08-16 09:56:15 +00:00
|
|
|
|
2018-09-03 07:39:02 +00:00
|
|
|
def end(self, gname):
|
|
|
|
self.stop_event.set()
|
|
|
|
log.info('gname is {}'.format(self.ctx.netem.names[gname]))
|
|
|
|
self.ctx.netem.names[gname].get()
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
"""
|
|
|
|
Invoked during unwinding if the test fails or exits before executing task 'link_recover'
|
|
|
|
"""
|
|
|
|
log.info('Clean up')
|
2018-08-16 09:56:15 +00:00
|
|
|
self.stop_event.set()
|
|
|
|
self.thread.get()
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def task(ctx, config):
|
|
|
|
|
|
|
|
"""
|
|
|
|
- netem:
|
|
|
|
clients: [c1.rgw.0]
|
2018-08-20 13:57:37 +00:00
|
|
|
iface: eno1
|
2018-08-16 09:56:15 +00:00
|
|
|
dst_client: [c2.rgw.1]
|
|
|
|
delay: 10ms
|
|
|
|
|
|
|
|
- netem:
|
|
|
|
clients: [c1.rgw.0]
|
2018-08-20 13:57:37 +00:00
|
|
|
iface: eno1
|
2018-08-16 09:56:15 +00:00
|
|
|
dst_client: [c2.rgw.1]
|
|
|
|
delay_range: [10ms, 20ms] # (min, max)
|
|
|
|
|
|
|
|
- netem:
|
|
|
|
clients: [rgw.1, mon.0]
|
2018-08-20 13:57:37 +00:00
|
|
|
iface: eno1
|
2018-09-03 07:39:02 +00:00
|
|
|
gname: t1
|
2018-08-16 09:56:15 +00:00
|
|
|
dst_client: [c2.rgw.1]
|
|
|
|
link_toggle_interval: 10 # no unit mentioned. By default takes seconds.
|
|
|
|
|
|
|
|
- netem:
|
|
|
|
clients: [rgw.1, mon.0]
|
2018-08-20 13:57:37 +00:00
|
|
|
iface: eno1
|
2018-09-03 07:39:02 +00:00
|
|
|
link_recover: [t1, t2]
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
log.info('config %s' % config)
|
|
|
|
|
|
|
|
assert isinstance(config, dict), \
|
|
|
|
"please list clients to run on"
|
|
|
|
if not hasattr(ctx, 'netem'):
|
|
|
|
ctx.netem = argparse.Namespace()
|
2018-09-03 07:39:02 +00:00
|
|
|
ctx.netem.names = {}
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
if config.get('dst_client') is not None:
|
|
|
|
dst = config.get('dst_client')
|
|
|
|
(host,) = ctx.cluster.only(dst).remotes.iterkeys()
|
|
|
|
|
|
|
|
for role in config.get('clients', None):
|
2018-09-03 07:39:02 +00:00
|
|
|
(remote,) = ctx.cluster.only(role).remotes.iterkeys()
|
2018-08-16 09:56:15 +00:00
|
|
|
ctx.netem.remote = remote
|
|
|
|
if config.get('delay', False):
|
2018-08-20 13:57:37 +00:00
|
|
|
static_delay(remote, host, config.get('iface'), config.get('delay'))
|
2018-08-16 09:56:15 +00:00
|
|
|
if config.get('delay_range', False):
|
2018-08-20 13:57:37 +00:00
|
|
|
variable_delay(remote, host, config.get('iface'), config.get('delay_range'))
|
2018-08-16 09:56:15 +00:00
|
|
|
if config.get('link_toggle_interval', False):
|
|
|
|
log.info('Toggling link for %s' % config.get('link_toggle_interval'))
|
|
|
|
global toggle
|
2018-09-03 07:39:02 +00:00
|
|
|
toggle = Toggle(ctx, remote, host, config.get('iface'), config.get('link_toggle_interval'))
|
|
|
|
toggle.begin(config.get('gname'))
|
2018-08-16 09:56:15 +00:00
|
|
|
if config.get('link_recover', False):
|
|
|
|
log.info('Recovering link')
|
2018-09-03 07:39:02 +00:00
|
|
|
for gname in config.get('link_recover'):
|
|
|
|
toggle.end(gname)
|
|
|
|
log.info('sleeping')
|
|
|
|
time.sleep(config.get('link_toggle_interval'))
|
|
|
|
delete_dev(ctx.netem.remote, config.get('iface'))
|
|
|
|
del ctx.netem.names[gname]
|
2018-08-16 09:56:15 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
2018-09-03 07:39:02 +00:00
|
|
|
if ctx.netem.names:
|
|
|
|
toggle.cleanup()
|
2018-08-16 09:56:15 +00:00
|
|
|
for role in config.get('clients'):
|
|
|
|
(remote,) = ctx.cluster.only(role).remotes.iterkeys()
|
2018-08-20 13:57:37 +00:00
|
|
|
delete_dev(remote, config.get('iface'))
|
2018-08-16 09:56:15 +00:00
|
|
|
|