-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
driver: add RawNetworkInterfaceDriver
This driver allows "raw" control of a network interface (such as Ethernet or WiFi). Signed-off-by: Bastian Krause <[email protected]> Signed-off-by: Rouven Czerwinski <[email protected]>
- Loading branch information
1 parent
9b6cc95
commit 9d7a8e0
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# pylint: disable=no-member | ||
import json | ||
import signal | ||
import subprocess | ||
import shutil | ||
import tempfile | ||
|
||
import attr | ||
|
||
from .common import Driver | ||
from ..factory import target_factory | ||
from ..step import step | ||
from ..util.helper import processwrapper | ||
from ..util.managedfile import ManagedFile | ||
from ..resource.common import NetworkResource | ||
|
||
|
||
@target_factory.reg_driver | ||
@attr.s(eq=False) | ||
class RawNetworkInterfaceDriver(Driver): | ||
bindings = { | ||
"iface": {"NetworkInterface", "RemoteNetworkInterface", "USBNetworkInterface"}, | ||
} | ||
|
||
def __attrs_post_init__(self): | ||
super().__attrs_post_init__() | ||
self.proc_pcap_map = {} | ||
|
||
def _get_wrapper_prefix(self): | ||
return self.iface.command_prefix + ["sudo", "labgrid-raw-interface"] | ||
|
||
@Driver.check_active | ||
@step(args=["count"]) | ||
def record(self, *, count=None): | ||
capture = tempfile.NamedTemporaryFile(prefix="lg-raw-int-cap-") | ||
cmd = self._get_wrapper_prefix() + ["tcpdump", self.iface.ifname] | ||
if count is not None: | ||
cmd.append(str(count)) | ||
|
||
proc = subprocess.Popen(cmd, stdout=capture, stderr=subprocess.PIPE) | ||
self.proc_pcap_map[proc] = capture | ||
return proc | ||
|
||
@Driver.check_active | ||
@step(args=["proc", "filename"]) | ||
def get_record(self, proc, filename, *, timeout=None): | ||
assert proc in self.proc_pcap_map | ||
|
||
try: | ||
_, err = proc.communicate(timeout=timeout) | ||
except subprocess.TimeoutExpired: | ||
proc.send_signal(signal.SIGINT) | ||
_, err = proc.communicate() | ||
|
||
assert proc.returncode == 0, f"tcpdump err={err}" | ||
|
||
capfile = self.proc_pcap_map[proc] | ||
shutil.copy(capfile.name, filename) | ||
capfile.close() | ||
|
||
@Driver.check_active | ||
@step(args=["filename"]) | ||
def start_replay(self, filename): | ||
mf = ManagedFile(filename, self.iface) | ||
mf.sync_to_resource() | ||
|
||
if isinstance(self.iface, NetworkResource): | ||
cmd = self.iface.command_prefix | ||
cmd.append(f"sudo labgrid-raw-interface tcpreplay {self.iface.ifname} < {mf.get_remote_path()}") | ||
indata = None | ||
else: | ||
cmd = self._get_wrapper_prefix() + ["tcpreplay", self.iface.ifname] | ||
indata = open(mf.get_remote_path(), "rb").read() | ||
return subprocess.run(cmd, input=indata) | ||
|
||
@Driver.check_active | ||
@step() | ||
def get_statistics(self): | ||
cmd = self.iface.command_prefix + [ | ||
"ip", | ||
"--json", | ||
"-stats", "-stats", | ||
"link", "show", | ||
self.iface.ifname] | ||
output = processwrapper.check_output(cmd) | ||
return json.loads(output)[0] | ||
|
||
@Driver.check_active | ||
def get_address(self): | ||
return self.get_statistics()["address"] |