dummynet.DummyNet¶
- class dummynet.DummyNet(shell, namespace=<factory>, cgroups=<factory>, namespaces=<factory>, cleaners=<factory>)¶
A DummyNet object is used to create a network of virtual ethernet devices and bind them to namespaces.
- Parameters:
shell (NamespaceShell | HostShell)
namespace (NamespaceScoped)
cgroups (OrderedDict[CGroupScoped, CGroup])
namespaces (OrderedDict[NamespaceScoped, Self])
cleaners (List[CleanupItem])
- __init__(shell, namespace=<factory>, cgroups=<factory>, namespaces=<factory>, cleaners=<factory>)¶
- Parameters:
shell (NamespaceShell | HostShell)
namespace (NamespaceScoped)
cgroups (OrderedDict[CGroupScoped, CGroup])
namespaces (OrderedDict[NamespaceScoped, Self])
cleaners (List[CleanupItem])
- Return type:
None
- add_cgroup(name, shell, log, cpu_limit=None, memory_limit=None)¶
Creates a new cgroup object.
- Parameters:
name (str) – The name of the cgroup.
shell (NamespaceShell | HostShell) – The shell object used for executing shell commands.
log (Logger) – The log object used for logging messages.
cpu_limit (float | None) – The ratio of CPU usage limit for the cgroup. Between 0 and 1. Defaults to None.
memory_limit (int | None) – The memory usage hard-limit for the cgroup. In bytes. Defaults to None. if memory usage exceeds the limit, the processes will get killed by the kernel. OOM.
- Returns:
A CGroup object.
- Return type:
- addr_add(ip, interface)¶
Adds an IP-address to a network interface.
- Parameters:
ip (str)
interface (InterfaceScoped | str)
- Return type:
None
- addr_del(ip, interface)¶
Deletes an IP-address to a network interface.
- Parameters:
ip (str)
interface (InterfaceScoped | str)
- Return type:
None
- bridge_add(name)¶
Adds a bridge
- Parameters:
name (str)
- Return type:
- bridge_list()¶
List the different bridges
- Return type:
list[InterfaceScoped]
- bridge_set(bridge, interface)¶
Adds an interface to a bridge
- Parameters:
bridge (InterfaceScoped | str)
interface (InterfaceScoped | str)
- Return type:
None
- cgroup_list()¶
Returns a list of all cgroups. Runs ‘find /sys/fs/cgroup -maxdepth 1 -mindepth 1 -type d’
- Return type:
list[CGroupScoped]
- cleanup()¶
Cleans up all the created network namespaces and bridges
- Return type:
None
- down(interface)¶
Sets the given network device to ‘down’
- Parameters:
interface (InterfaceScoped | str)
- Return type:
None
- forward(from_interface, to_interface)¶
Forwards all traffic from one network interface to another.
- Parameters:
from_interface (InterfaceScoped | str)
to_interface (InterfaceScoped | str)
- Return type:
None
- link_delete(interface)¶
Deletes a specific network interface.
- Parameters:
interface (InterfaceScoped | str)
- Return type:
None
- link_list(link_type=None)¶
Returns the output of the ‘ip link list’ command parsed to a list of strings
- Parameters:
link_type – The type of link to list (e.g. veth or bridge)
- Returns:
A list of interfaces of the links
- Return type:
list[InterfaceScoped]
- link_set(namespace, interface)¶
Binds a network interface (usually the veths) to a namespace.
The namespace parameter is the name of the namespace as a string
- Parameters:
namespace (NamespaceScoped | Self | str) – The namespace to bind the interface to
interface (InterfaceScoped | str) – The interface to bind to the namespace
- Return type:
None
- link_veth_add(p1_name, p2_name)¶
Adds a virtual ethernet between two endpoints.
Name of the link will be ‘p1_name@p2_name’ when you look at ‘ip addr’ in the terminal
- Parameters:
p1_name (str) – Name of the first endpoint
p2_name (str) – Name of the second endpoint
- Returns:
A 2-tuple of
p1_nameandp2_nameas interfaces- Return type:
tuple[InterfaceScoped, InterfaceScoped]
- link_vlan_add(parent_interface, vlan_id)¶
Add a VLAN subinterface to a parent interface.
- Parameters:
parent_interface (InterfaceScoped | str) – The parent interface to create a vlan from
vlan_id (int) – The numeric identifier of the vlan to add the interface to
- Return type:
- netns_add(name)¶
Adds a new network namespace.
Returns a new DummyNet object with a NamespaceShell, a wrapper to the command-line but with every command prefixed by ‘ip netns exec name’ This returned object is the main component for creating a dummy-network. Configuring these namespaces with the other utility commands allows you to configure the networks.
- Parameters:
name (str)
- Return type:
Self
- netns_delete(namespace)¶
Deletes a specific network namespace. Note that before deleting a network namespace all processes in that namespace should be killed. Using e.g.:
process_list = net.netns_get_process_list(ns_name).splitlines() for process in process_list: self.netns_kill_process(name, process)
- Parameters:
name – The namespace to delete
namespace (NamespaceScoped | Self | str)
- netns_kill_all(namespace)¶
Kills all processes running in a network namespace
- Parameters:
namespace (NamespaceScoped | Self | str)
- netns_kill_process(namespace, pid)¶
Kills a process in a network namespace
- Parameters:
namespace (NamespaceScoped | Self | str)
pid (int)
- netns_list()¶
Returns a list of all network namespaces. Runs ‘ip netns list’
- Return type:
list[NamespaceScoped]
- netns_process_list(namespace)¶
Returns a list of all processes in a network namespace
- Parameters:
namespace (NamespaceScoped | Self | str)
- Return type:
list[str]
- route(ip)¶
Sets a new default IP-route.
- Parameters:
ip (str)
- Return type:
None
- run(cmd, cwd=None)¶
Wrapper for the command-line access
- Parameters:
cmd (str) – The command to run
cwd – The working directory to run the command in
- Return type:
- run_async(cmd, daemon=False, cwd=None)¶
Wrapper for the concurrent command-line access
Asynchronous commands run in the background. The process is launched via the shell.
- Parameters:
cmd (str) – The command to run
daemon – Whether to run the command as a daemon
cwd – The working directory to run the command in
- Return type:
- tc(interface, delay=None, loss=None, rate=None, limit=None, cwd=None)¶
Modifies the given interface by adding any artificial combination of delay, packet loss, bandwidth constraints or queue limits
- Parameters:
interface (InterfaceScoped | str)
- Return type:
None
- tc_show(interface, cwd=None)¶
Shows the current traffic-control configurations in the given interface
- Parameters:
interface (InterfaceScoped | str)
- up(interface)¶
Sets the given network device to ‘up’
- Parameters:
interface (InterfaceScoped | str)
- Return type:
None