p4utils.mininetlib.network_API module¶
This module provides an API to easily and programmatically build a virtual network using Mininet as framework. It is an extension of mininet.topo. Here, you will find the documentation for all the methods that will help you instantiate and start the network.
-
class
p4utils.mininetlib.network_API.
NetworkAPI
(*args, **params)[source]¶ Bases:
mininet.topo.Topo
Network definition and initialization API.
-
compilers
[source]¶ list of compiler instances (one per P4 source provided) to compile P4 code.
Type: list
-
net
[source]¶ network instance implemented using an extension to Mininet network class.
Type: mininet.net.Mininet
-
ipv4_net
[source]¶ IPv4 network address generator (by default within the network
10.0.0.0/8
). a different network can be specified usingsetIpBase()
.Type: ipaddress.IPv4Network
-
cpu_bridge
[source]¶ name of the bridge used to connect all the CPU ports of the P4 switches.
Type: str
-
auto_arp_tables
[source]¶ automatically populate the ARP tables of each hosts with MACs from the other hosts present in the same subnetwork.
Type: bool
-
addHost
(name, **opts)[source]¶ Adds P4 host node to the network.
Parameters: - name (str) – host name
- **opts – host options (optional)
Returns: host name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addLink
(node1, node2, port1=None, port2=None, key=None, **opts)[source]¶ Adds link between two nodes.
Parameters: - node1 (str) – name of the first node
- node2 (str) – name of the second node
- port1 (int) – port number on the first node (optional)
- port2 (int) – port number on the second node (optional)
- key (int) – id used to identify multiple edges which link two same nodes (optional)
- **opts – link options as listed below (optional)
In particular, **opts can include the following:
- intfName1 (
str
): name of the interface of the first node - intfName2 (
str
): name of the interface of the second node - addr1 (
str
) : MAC address of the interface of the first node - addr2 (
str
) : MAC address of the interface of the second node - weight (
int
) : weight used to compute shortest paths
Returns: key of the link between node1 and node2. Return type: int Note
If
key
is None, then the next available number is used. If not specified, all the optional fields are assigned automatically by the methodauto_assignment()
before the network is started.Warning
The interface names must not be in the canonical format (i.e.
node-ethN
whereN
is the port number of the interface) because the automatic assignment uses it.
-
addNode
(name, **opts)[source]¶ Adds a node to the network.
Parameters: - name (str) – node name
- **opts (kwargs) – node options
Returns: node name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addP4RuntimeSwitch
(name, **opts)[source]¶ Adds P4 runtime switch node to Mininet topology.
Parameters: - name (str) – P4Runtime switch name
- **opts – switch options
Returns: P4Runtime switch name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addP4Switch
(name, **opts)[source]¶ Adds P4 switch node to the network.
Parameters: - name (str) – switch name
- opts (kwargs) – switch options
Returns: P4 switch name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addRouter
(name, **opts)[source]¶ Adds a router node to the network.
Parameters: - name (str) – router name
- **opts – router options
Returns: router name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addSwitch
(name, **opts)[source]¶ Adds switch node to the network.
Parameters: - name (str) – switch name
- **opts – switch options
Returns: switch name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
addTask
(name, exe, start=0, duration=0, enableScheduler=True, args=(), kwargs={})[source]¶ Adds a task to the node.
Parameters: - name (str) – node name
- exe (str or types.FunctionType) – executable to run (either a shell string command or a Python function)
- start (float) – task delay in seconds with respect to the network starting time.
- duration (float) – task duration time in seconds (if duration is
lower than or equal to
0
, then the task has no time limitation) - enableScheduler (bool) – whether to automatically enable the TaskServer or not
- args (tuple or list) – positional arguments for the passed function
- kwargs (dict) – key-word arguments for the passed function
Note
This method can automatically enable the task scheduler (provided by the
TaskServer
) on the node with the socket lacated in the default path, if it has not been previously enabled.
-
addTaskFile
(filepath, def_mod='p4utils.utils.traffic_utils')[source]¶ Adds the tasks to the node.
Parameters: The file has to be a set of lines, where each one has the following syntax:
<node> <start> <duration> <exe> [<arg1>] ... [<argN>] [--mod <module>] [--<key1> <kwarg1>] ... [--<keyM> <kwargM>]
Note
A non-default module can be specified in the command with
--mod <module>
.
-
addTofino
(name, **opts)[source]¶ Adds Tofino switch node to Mininet topology.
Parameters: - name (str) – P4Runtime switch name
- **opts – switch options
Returns: Tofino switch name.
Return type: Warning
If a node with the same name is already present, this method will overwrite it.
-
areNeighbors
(node1, node2)[source]¶ Checks if two node are neighbors.
Parameters: Returns: True if node1 and node2 are neighbors, False otherwise.
Return type:
-
deleteLink
(node1, node2, key=None)[source]¶ Delete link.
Parameters: Note
If
key
is None, then the link with the lowest key value is considered.
-
disableDebugger
(name)[source]¶ Disables debugger for the P4 switch.
Parameters: name (str) – P4 switch name Note
For the default setting check out
P4Switch
.
-
disableDebuggerAll
()[source]¶ Disable debugger for all the P4 switches.
Note
For the default setting check out
P4Switch
.
-
disableDhcp
(name)[source]¶ Disables DHCP server in hosts.
Parameters: name (str) – host name Raises: Exception
– if the specified node is not a host.
-
disableLog
(name)[source]¶ Disables log for node (also for its task scheduler).
Parameters: name (str) – node name
-
disablePcapDump
(name)[source]¶ Disables generation of
.pcap
files for the P4 switch.Parameters: name (str) – name of the P4 switch Note
For the default setting check out
P4Switch
.
-
disablePcapDumpAll
()[source]¶ Disables generation of
.pcap
files for all the P4 switches.Note
For the default setting check out
P4Switch
.
-
disableScheduler
(name)[source]¶ Disables the task scheduler server for the node.
Parameters: name (str) – node name
-
enableArpTables
()[source]¶ Enables the static ARP entries for hosts in the same network.
Note
This option is enabled by default.
-
enableCpuPort
(name)[source]¶ Enables CPU port on switch.
Parameters: name (str) – P4 switch name Warning
This operation will create a new switch called
sw-cpu
. Should a node with the same name exist, then it would be overwritten.
-
enableCpuPortAll
()[source]¶ Enables CPU port on all the P4 switches.
Warning
This applies only to already defined switches. If other switches are added after this command, they won’t have any CPU port enabled.
-
enableDebugger
(name)[source]¶ Enables debugger for the P4 switch.
Parameters: name (str) – P4 switch name Note
For the default setting check out
P4Switch
.
-
enableDebuggerAll
()[source]¶ Enable debugger for all the P4 switches.
Note
For the default setting check out
P4Switch
.
-
enableDhcp
(name)[source]¶ Enables DHCP server in hosts.
Parameters: name (str) – host name Raises: Exception
– if the specified node is not a host.
-
enableGwArp
()[source]¶ Enables the static ARP entry in hosts for the gateway only.
Note
This option is enabled by default.
-
enableLog
(name, log_dir='./log')[source]¶ Enables log for node (also for its task scheduler).
Parameters:
-
enableLogAll
(log_dir='./log')[source]¶ Enables log for all the nodes (also for their task schedulers).
Parameters: log_dir (str) – path to the log directory
-
enablePcapDump
(name, pcap_dir='./pcap')[source]¶ Enables generation of
.pcap
files for the P4 switch.Parameters: Note
For the default setting check out
P4Switch
.
-
enablePcapDumpAll
(pcap_dir='./pcap')[source]¶ Enables generation of
.pcap
files for all the P4 switches.Parameters: pcap_dir (str) – where to save .pcap
filesNote
For the default setting check out
P4Switch
.
-
enableScheduler
(name, path='/tmp')[source]¶ Enables the task scheduler server for the node.
Parameters:
-
enableSchedulerAll
(path='/tmp')[source]¶ Enables a task scheduler server for each node.
Parameters: path (str) – name of the directory where the socket files will be placed
-
execScript
(cmd, out_file=None, reboot=True)[source]¶ Executes the given command in the main namespace after the network boot.
Parameters:
-
getLink
(node1, node2, key=None)[source]¶ Returns link metadata dictionary.
Parameters: Returns: (link, key)
wherelink
is adict
containing allthe information about the link and
key
is the id of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
getNode
(name)[source]¶ Gets node information.
Parameters: node (str) – node name Returns: node metadata. Return type: dict
-
hasCpuPort
(name)[source]¶ Checks if the P4 Switch has a CPU port.
Parameters: name (str) – P4 switch name Returns: True if node has a CPU port, False otherwise. Return type: bool
-
hasScheduler
(name)[source]¶ Checks if a host has an active scheduler.
Parameters: name (str) – node name Returns: True if the node has an active scheduler, False otherwise. Return type: bool
-
hosts
(sort=True, withInfo=False)[source] Returns hosts.
Parameters: Returns: list of
(host [, info])
.Return type:
-
isHost
(name)[source]¶ Checks if node is a host.
Parameters: name (str) – node name Returns: True if node is a host, False otherwise. Return type: bool
-
isNode
(name)[source]¶ Checks if node exists.
Parameters: name (str) – node name Returns: True if node exists, False otherwise. Return type: bool
-
isP4RuntimeSwitch
(name)[source]¶ Checks if node is a P4Runtime switch.
Parameters: name (str) – node name Returns: True if node is a P4Runtime switch, else False. Return type: bool
-
isP4Switch
(name)[source]¶ Checks if the node is a P4 switch.
Parameters: name (str) – node name Returns: True if node is a P4 switch, False otherwise. Return type: bool
-
isRouter
(name)[source]¶ Checks if a node is a router.
Parameters: name (str) – node name Returns: True if node is a router, False otherwise. Return type: bool
-
isSwitch
(name)[source]¶ Checks if node is a switch.
Parameters: name (str) – node name Returns: True if node is a switch, False otherwise. Return type: bool
-
isTofino
(name)[source]¶ Checks if node is a Tofino switch.
Parameters: name (str) – node name Returns: True if node is a Tofino switch, else False. Return type: bool
-
l2
()[source]¶ Automated IP/MAC assignment strategy for already initialized links and nodes. All the devices are placed inside the same IPv4 network (
10.0.0.0/16
).Assumes
- Each host is connected to exactly one switch.
- Only switches and hosts are allowed.
- Parallel links are not allowed.
Warning
Routers are not supported.
-
l3
()[source]¶ Automated IP/MAC assignment strategy for already initialized links and nodes. All the hosts have a different subnetwork that is shared with the fake IP address of the switch port they are connected to.
Assumes
- Each host is connected to exactly one switch.
- Only switches and hosts are allowed.
- Parallel links are not allowed.
Warning
Routers are not supported.
-
links
(sort=False, withKeys=False, withInfo=False)[source]¶ Returns links with no duplicated edges. Every link is listed exactly once.
Parameters: Returns: list of
(src, dst [, key, info ])
.Return type:
-
mixed
()[source]¶ Automated IP/MAC assignment strategy for already initialized links and nodes. All the hosts connected to the same switch are placed in the same subnetwork. Different switches (even those linked together) are placed in different subnetworks.
Assumes
- Each host is connected to exactly one switch.
- Only switches and hosts are allowed.
- Parallel links are not allowed.
Warning
Routers are not supported.
-
nodes
(sort=True, withInfo=False)[source]¶ Returns all the nodes.
Parameters: Returns: list of
(node [, info])
.Return type:
-
p4rtswitches
(sort=True, withInfo=False)[source]¶ Returns P4Runtime switches.
Parameters: Returns: list of
(p4runtimeswitch [, info])
.Return type:
-
p4switches
(sort=True, withInfo=False)[source]¶ Returns P4 switches.
- Args:
- sort (bool) : sort switches alphabetically withInfo (bool): retrieve node information
Returns: list of (p4switch [, info])
.Return type: list
-
popLink
(node1, node2, key=None)[source]¶ Pops link.
Parameters: Returns: (link, key)
wherelink
is adict
containing allthe information about the link and
key
is the id of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
popNode
(name, remove_links=True)[source]¶ Pops node.
Parameters: Returns: node metadata.
Return type:
-
routers
(sort=True, withInfo=False)[source]¶ Return routers.
Parameters: Returns: list of
(router [, info])
.Return type:
-
setBw
(node1, node2, bw, key=None)[source]¶ Sets link bandwidth.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setBwAll
(bw)[source]¶ Sets bandwidth for all the links currently in the network.
Parameters: bw (float) – bandwidth (in Mbps) Warning
This only sets the bandwidth of the links that have been already added to the topology: those added after this method is called will not meet this setting.
-
setCompiler
(compilerClass=None, **kwargs)[source]¶ Sets the default P4 compiler class and options.
Parameters: - compilerClass (type) – compiler class to use for the compilation of P4 code
- **kwargs – key-word arguments to pass to the compiler object when it is first instantiated
Note
One can provide both
compilerClass
and**kwargs
or only one of them (e.g. one may want to use the default compilerP4C
and pass some parameters to it).
-
setDelay
(node1, node2, delay, key=None)[source]¶ Sets link delay.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setDelayAll
(delay)[source]¶ Sets delay for all the links currently in the network.
Parameters: delay (int) – transmission delay (in ms) Warning
This only sets the delay of the links that have been already added to the topology: those added after this method is called will not meet this setting.
-
setIntfIp
(node1, node2, ip, key=None)[source]¶ Sets IP of node1’s interface facing node2 with the specified key.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setIntfMac
(node1, node2, mac, key=None)[source]¶ Sets MAC of node1’s interface facing node2 with the specified key.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setIntfName
(node1, node2, intfName, key=None)[source]¶ Sets name of node1’s interface facing node2 with the specified key.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setIntfPort
(node1, node2, port, key=None)[source]¶ Sets port number of node1’s interface facing node2 with the specified key.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setIpBase
(ipBase)[source]¶ Sets the network in which all the L3 devices will be placed if no explicit assignment is performed (e.g. assignment strategies or manual assignment).
Parameters: ipBase (str) – IP address / mask (e.g. 10.0.0.0/8
)Warning
Remember that setting the IP base won’t automatically change the already assigned IP. If you want to specify a different network, please use this method before any node is added to the network.
-
setLogLevel
(logLevel)[source]¶ Sets the log level for the execution.
Parameters: logLevel (str) – level of logging detail. Possible logLevel values are the follwing (in decreasing order of detail):
debug
info
output
warning
error
critical
-
setLoss
(node1, node2, loss, key=None)[source]¶ Sets link loss.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setLossAll
(loss)[source]¶ Sets loss for all the links currently in the network.
Parameters: loss (float) – packet loss rate (e.g. 0.5
means that 50% of packets will exeperience a loss)Warning
This only sets the loss rate of the links that have been already added to the topology: those added after this method is called will not meet this setting.
-
setMaxQueueSize
(node1, node2, max_queue_size, key=None)[source]¶ Sets link max queue size.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-
setMaxQueueSizeAll
(max_queue_size)[source]¶ Sets max queue size for all the links currently in the network.
Parameters: max_queue_size (int) – maximum number of packets the qdisc
may hold queued at a time.Warning
This only sets the max queue size of the links that have been already added to the topology: those added after this method is called will not meet this setting.
-
setNet
(netClass=None, **kwargs)[source]¶ Sets the default network class and options.
Parameters: - netClass (type) – network class to use for the orchestration
- **kwargs – key-word arguments to pass to the network object when it is first instantiated
Note
One can provide both
netClass
and**kwargs
or only one of them (e.g. one may want to use the default networkP4Mininet
and pass some parameters to it).
-
setP4CliInput
(name, cli_input)[source]¶ Sets the path to the command line configuration file for a Thrift capable P4 switch.
Parameters:
-
setP4SourceAll
(p4_src)[source]¶ Sets the same P4 source for all the P4 switches.
Parameters: p4_src (str) – path to the P4 file
-
setPriorityQueueNum
(name, queue_num)[source]¶ Sets the queue_num for the ports of the P4 switch. :param name: P4 switch name :type name: str :param queue_num: Number of priority queues for each port :type queue_num: int
-
setSwitchClient
(swclientClass=None, **kwargs)[source]¶ Sets the default switch client class and options.
Parameters: - swclientClass (type) – Thrift client class to use for the the control plane configuration
- **kwargs – key-word arguments to pass to the client object when it is first instantiated
Note
One can provide both
swclientClass
and**kwargs
or only one of them (e.g. one may want to use the default clientThriftClient
and pass some parameters to it).
-
setSwitchDpid
(name, dpid)[source]¶ Sets Switch DPID.
Parameters: Note
This method only applies to non P4 switches since P4 switches’ DPIDs are determined by their IDs.
-
setTopologyFile
(topoFile)[source]¶ Sets the file where the topology will be saved for subsequent queries in the exercises.
Parameters: topoFile (str) – path to the topology database file Note
The topology database is stored in
./topology.json
by default.
-
switches
(sort=True, withInfo=False)[source]¶ Returns switches.
Parameters: Returns: list of
(switch [, info])
.Return type:
-
updateLink
(node1, node2, key=None, **opts)[source]¶ Updates link metadata dictionary. In fact, delete the node and create a new one with the updated information.
Parameters: Returns: key of the link between node1 and node2.
Return type: Note
If
key
is None, then the link with the lowest key value is considered.
-