Namespace modules¶
The API works when stack: linux_based in trex_cfg Using this API the user can add a separate Linux network namespace that represents a client. This client is virtualy attached to a bridge that connected to TRex port. ARP/IPV6 ND packets move from clients to DUT and DUT to clients but not from clients to clients due to n^2 flood startup issue. Using this method it is possible to leverage linux ipv4/ipv6 network stack for emulating l2 protocols.
notes: adding/removing namespace is rather slow (~100msec)
The following snippet create one namespace with ipv4 and ipv6
c = STLClient(verbose_level = 'error')
c.connect()
my_ports=[0,1]
c.reset(ports = my_ports)
# move to service mode
c.set_service_mode (ports = my_ports, enabled = True)
cmds=NSCmds()
MAC="00:01:02:03:04:05"
cmds.add_node(MAC) # add namespace
cmds.set_vlan(MAC,[123,123]) # add valn + QinQ tags
cmds.set_ipv4(MAC,"1.1.1.3","1.1.1.2") # configure ipv4 and default gateway
cmds.set_ipv6(MAC,True) # enable ipv6 (auto mode, get src addrees from the router
# start the batch
c.set_namespace_start( 0, cmds)
# wait for the results
res = c.wait_for_async_results(0);
# print the results
print(res);
Another method to do that:
c = STLClient(verbose_level = 'error')
c.connect()
my_ports=[0,1]
c.reset(ports = my_ports)
# move to service mode
c.set_service_mode (ports = my_ports, enabled = True)
# remove all old name spaces from all the ports
c.namespace_remove_all()
# utility function
MAC="00:01:02:03:04:05"
# each function will block
c.set_namespace(0,method='add_node',mac=MAC)
c.set_namespace(0,method='set_vlan',vlans=[123,123])
c.set_namespace(0,method='set_ipv4', mac=MAC, ipv4="1.1.1.3", dg="1.1.1.2")
c.set_namespace(0,method='set_ipv6', mac=MAC, enable= True)
Get Stats:
c = STLClient(verbose_level = 'error')
c.connect()
my_ports=[0,1]
c.reset(ports = my_ports)
# get all active nodes on port 0
r=c.set_namespace(0, method='get_nodes')
c.set_namespace (0, method='get_nodes_info', macs_list=r)
r=c.set_namespace(0, method='counters_get_meta')
r=c.set_namespace(0, method='counters_get_values', zeros=True)
build a networks of x clients
def get_mac (prefix,index):
mac="{}:{:02x}:{:02x}".format(prefix,(index>>8)&0xff,(index&0xff))
return (mac)
def get_ipv4 (prefix,index):
ipv4="{}.{:d}.{:d}".format(prefix,(index>>8)&0xff,(index&0xff))
return(ipv4)
# build a networks of 00:01:02:03:xx:xx and 1.1.x.x and IPV6 =auto with default gateway of 1.1.1.2 for all
def build_network (size):
cmds=NSCmds()
MAC_PREFIX="00:01:02:03"
IPV4_PREFIX="1.1"
IPV4_DG ='1.1.1.2'
for i in range(size):
mac = get_mac (MAC_PREFIX,i+257+1)
ipv4 =get_ipv4 (IPV4_PREFIX,259+i)
cmds.add_node(mac)
cmds.set_ipv4(mac,ipv4,IPV4_DG)
cmds.set_ipv6(mac,True)
return (cmds)
NSCmds class¶
- class trex.common.trex_ns.NSCmds[source]¶
namespace commands builder
- add_node(mac, is_bird=False, shared_ns=None)[source]¶
add new virtual interface and it’s namespace, in shared_ns and bird cases only virtual interface will be created
- Parameters
- mac: string
MAC address in the format of xx:xx:xx:xx:xx:xx
- is_bird: bool
True if the new node will be a bird node. Notice it’s mutually exclusive with shared_ns
- shared_ns: string
The name of the shared namespace to paired with. Notice it’s mutually exclusive with is_bird
simply adding new namespace to TRex with no veth. return the name of the new namespace.
- remove_node(mac)[source]¶
remove namespace
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
remove shared namespace.
parameters:
- shared_ns: string
The name of the namespace to delete
- set_dg(shared_ns, dg)[source]¶
set or change default gateway for the whole namespace. works only on shared_ns nodes.
parameters:
- shared_ns: string
The name of the pre-created namespace
- dg: string
The new default gateway to set
- set_vlan(mac, vlans, tpids=None)[source]¶
add/remove QinQ and Dot1Q. could be up to 2 tags
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- vlans: list
Array of up to 2 uint16 tags. In case of empty remove the vlans
- tpids: list
- Array of tpids that correspond to vlans.Default is [0x8100] in case of single VLAN and [0x88a8, 0x8100] in case of QinQ
- set_ipv4(mac, ipv4, dg=None, subnet=None, shared_ns=False)[source]¶
set or change ipv4 configuration
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- ipv4: string
IPv4 self address
- dg: string
Default gateway. Using only when shared_ns is set to False.
- subnet: int
Subnet mask of the ipv4 address. Using only when shared_ns is set to True.
- shared_ns: bool
True or False if the node it’s bird node or shared_ns node. Notice shared ns nodes need to supply subnet instead of default gateway.
- set_vlan_filter(mac, is_no_tcp_udp=True, is_tcp_udp=False)[source]¶
Set vlan filter mask according to “trex_vlan_filter.h” file.
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- is_no_tcp_udp: bool
True if the new filter should pass no_tcp_udp traffic i.e ICMP.
- is_tcp_udp: bool
True if the new filter should pass tcp_udp traffic i.e BGP.
- set_filter(mac, bpf_filter)[source]¶
set or change bpf filter. Warning - bad filter might crash TRex!
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- bpf_filter: string
Valid bfp filter
- set_ipv6(mac, enable, src_ipv6=None, subnet=None, shared_ns=False)[source]¶
set ns ipv6
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- enablebool
Enable ipv6.
src_ipv6: None for auto, or ipv6 addr
- subnet: int
Subnet mask of the ipv6 address. Using only when shared_ns is set to True.
- shared_ns: bool
True or False if the node it’s bird node or shared_ns node. Notice shared ns nodes need to supply subnet.
- set_mtu(mac, mtu)[source]¶
Set mtu for a given mac
- Parameters
- mac: string
Key to the already created namespace in format xx:xx:xx:xx:xx:xx
- mtu: int
The new mtu to set for the node with that mac
- get_nodes_info(macs_list)[source]¶
provide list of macs return alist of objects with each namepace information
- clear_counters()[source]¶
clear debug counters. these counters are global to all users in the system.