Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-nitb |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 21 | |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 22 | from ..core import log, process |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 23 | |
| 24 | class PcapRecorder(log.Origin): |
| 25 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 26 | def __init__(self, testenv, run_dir, iface=None, filters='', netns=None): |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 27 | self.iface = iface |
| 28 | if not self.iface: |
| 29 | self.iface = "any" |
Neels Hofmeyr | 95fd673 | 2017-05-15 14:03:07 +0200 | [diff] [blame] | 30 | self.filters = filters |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 31 | super().__init__(log.C_RUN, 'pcap-recorder_%s' % self.iface, filters=self.filters) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 32 | self.testenv = testenv |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 33 | self.run_dir = run_dir |
Pau Espin Pedrol | 772b2b1 | 2018-10-26 15:53:35 +0200 | [diff] [blame] | 34 | self.netns = netns |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 35 | self.start() |
| 36 | |
| 37 | def start(self): |
Pau Espin Pedrol | 2c48c34 | 2018-02-23 17:25:39 +0100 | [diff] [blame] | 38 | self.dbg('Recording pcap', self.run_dir, self.filters) |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 39 | dumpfile = os.path.join(os.path.abspath(self.run_dir), self.name() + ".pcap") |
Pau Espin Pedrol | 772b2b1 | 2018-10-26 15:53:35 +0200 | [diff] [blame] | 40 | popen_args = ('tcpdump', '-n', |
| 41 | '-i', self.iface, |
| 42 | '-w', dumpfile, |
| 43 | self.filters) |
| 44 | if self.netns: |
| 45 | self.process = process.NetNSProcess(self.name(), self.run_dir, self.netns, popen_args) |
| 46 | else: |
| 47 | self.process = process.Process(self.name(), self.run_dir, popen_args) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 48 | self.testenv.remember_to_stop(self.process) |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 49 | self.process.launch() |
| 50 | |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 51 | def running(self): |
| 52 | return not self.process.terminated() |
Neels Hofmeyr | 143ab81 | 2017-05-14 15:43:30 +0200 | [diff] [blame] | 53 | |
| 54 | # vim: expandtab tabstop=4 shiftwidth=4 |