dummynet.DummyNet

class dummynet.DummyNet(shell)

A DummyNet object is used to create a network of virtual ethernet devices and bind them to namespaces.

__init__(shell)

Creates a new DummyNet object.

Parameters:

shell – The shell to use for running commands

add_cgroup(name: str, shell, log: Logger, cpu_limit=None, memory_limit=None)

Creates a new cgroup object.

Parameters:
  • name – The name of the cgroup.

  • shell – The shell object used for executing shell commands.

  • log – The log object used for logging messages.

  • cpu_limit – The ratio of CPU usage limit for the cgroup. Between 0 and 1. Defaults to None.

  • memory_limit – 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.

addr_add(ip, interface)

Adds an IP-address to a network interface.

bridge_add(name)

Adds a bridge

bridge_list()

List the different bridges

bridge_set(name, interface)

Adds an interface to a bridge

bridge_up(name)

Brings a bridge up

cgroup_cleanup()

Cleans up all the created cgroups.

cleanup()

Cleans up all the created network namespaces and bridges

forward(from_interface, to_interface)

Forwards all traffic from one network interface to another.

Deletes a specific network interface.

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 strings with the names of the links

Binds a network interface (usually the veths) to a namespace.

The namespace parameter is the name of the namespace as a string

Parameters:
  • namespace – The namespace to bind the interface to

  • interface – The interface to bind to the namespace

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 – Name of the first endpoint

  • p2_name – Name of the second endpoint

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.

netns_delete(name)

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 – Name of the namespace to delete

netns_kill_all(name)

Kills all processes running in a network namespace

netns_kill_process(name, pid)

Kills a process in a network namespace

netns_list()

Returns a list of all network namespaces. Runs ‘ip netns list’

netns_process_list(name)

Returns a list of all processes in a network namespace

route(ip)

Sets a new default IP-route.

run(cmd, cwd=None)

Wrapper for the command-line access

Parameters:
  • cmd – The command to run

  • cwd – The working directory to run the command in

Returns:

A dummynet.RunInfo object

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 – The command to run

  • daemon – Whether to run the command as a daemon

  • cwd – The working directory to run the command in

Returns:

A dummynet.RunInfo object

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

tc_show(interface, cwd=None)

Shows the current traffic-control configurations in the given interface

up(interface)

Sets the given network interface to ‘up’