Client Module¶
The EMUClient provides access to the TRex emulation server.
User can interact with TRex emulation server using this client. The protocol is JSON-RPC2 over ZMQ transport. for EMU to work, you must move the port to promiscuous and to get multicast packet. You should be enable –software mode too
Adding and removing clients example:
arp example:
#!/usr/bin/python
import emu_path
from trex.emu.api import *
import pprint
c = EMUClient(server='localhost',
sync_port=4510,
verbose_level= "error",
logger=None,
sync_timeout=None)
port = 0
mac = Mac('00:00:00:70:00:01')
ipv4 = Ipv4('1.1.1.3')
ipv4_dg = Ipv4('1.1.1.1')
emu_client = EMUClientObj(mac = mac.V(),
ipv4 = ipv4.V(),
ipv4_dg = ipv4_dg.V(),
plugs = {'arp': {'enable': True}})
ns_key = EMUNamespaceKey(vport = port)
c_key = EMUClientKey(ns_key, mac.V())
emu_ns = EMUNamespaceObj(ns_key = ns_key,
clients = emu_client)
profile = EMUProfile(ns = emu_ns, def_ns_plugs = {'ipv6':{}})
# MAKE sure the TRex server is in --software mode promiscuous and to get multicast enabled
# trex_c.connect()
# trex_c.acquire(ports = my_ports)
# trex_c.set_port_attr(promiscuous=True) << get all the packets
# # move the mode to service mode in case DHCP is enabled
# trex_c.set_service_mode(ports = my_ports, enabled = False, filtered = True, mask = (NO_TCP_UDP_MASK | DHCP_MASK ))
#
# connect and load profile
c.connect()
c.load_profile(profile = profile)
# print tables of namespaces and clients
print("Show all emu ns and clients:")
c.print_all_ns_clients(max_ns_show = 1, max_c_show = 10)
# send gratuitous arp
print("Let's enable gratuitous arp")
c.arp.cmd_query(c_key, garp = True)
# show arp cache
res = c.arp.show_cache(ns_key)
print("Arp cache:")
for r in res:
pprint.pprint(r)
# arp counters
print('Arp counters:')
res = c.arp.get_counters(ns_key, zero = True, verbose = False)
pprint.pprint(res)
#!/usr/bin/python
import emu_path
from trex.emu.api import *
import pprint
EMU_SERVER = "localhost"
c = EMUClient(server=EMU_SERVER,
sync_port=4510,
verbose_level= "error",
logger=None,
sync_timeout=None)
# connect
c.connect()
mac_1, mac_2 = Mac('00:00:00:70:00:01'), Mac('00:00:00:70:00:02')
ipv4_1, ipv4_2 = Ipv4('1.1.1.3'), Ipv4('1.1.1.4')
ipv4_dg = Ipv4('1.1.1.1')
emu_client1 = EMUClientObj(mac = mac_1.V(), ipv4 = ipv4_1.V(), ipv4_dg = ipv4_dg.V())
# plugins only for c2, will override default
c2_plugs = {'arp': {'timer': 30},
'igmp': {}
}
emu_client2 = EMUClientObj(mac = mac_2.V(),
ipv4 = ipv4_2.V(),
ipv4_dg = ipv4_dg.V(),
plugs = c2_plugs)
# Default plugin for all clients in the ns
def_c_plugs = {'arp': {'timer': 50},
'icmp': {},
}
emu_clients = [emu_client1, emu_client2]
ns_key = EMUNamespaceKey(vport = 0)
emu_ns = EMUNamespaceObj(ns_key = ns_key,
clients = emu_clients,
def_c_plugs = def_c_plugs)
profile = EMUProfile(ns = emu_ns)
# start the emu profile
c.load_profile(profile = profile, max_rate = 2048, verbose = True)
# print tables of namespaces and clients
print("Let's see the clients we added")
c.print_all_ns_clients(max_ns_show = 1, max_c_show = 10)
# remove all of them
print("Removing Profile..")
c.remove_all_clients_and_ns()
# print again (should be empty)
print("Let's see the profile after removal")
c.print_all_ns_clients(max_ns_show = 1, max_c_show = 10)
Wrapping up example:
#!/usr/bin/python
import emu_path
from trex.emu.api import *
from trex.emu.trex_emu_conversions import Mac, Ipv4
import pprint
c = EMUClient(server = 'localhost',
sync_port = 4510,
verbose_level = "error",
logger = None,
sync_timeout = None)
c.connect()
mac_1, mac_2 = Mac('00:00:00:70:00:01'), Mac('00:00:00:70:00:02')
ipv4_1, ipv4_2 = Ipv4('1.1.1.3'), Ipv4('1.1.1.4')
ipv4_dg = Ipv4('1.1.1.1')
emu_client1 = EMUClientObj(mac = mac_1.V(), ipv4 = ipv4_1.V(), ipv4_dg = ipv4_dg.V())
# plugins only for c2, will override default
c2_plugs = {'arp': {'timer': 30},
'igmp': {}
}
emu_client2 = EMUClientObj(mac = mac_2.V(),
ipv4 = ipv4_2.V(),
ipv4_dg = ipv4_dg.V(),
plugs = c2_plugs)
# Default plugin for all clients in the ns
def_c_plugs = {'arp': {'timer': 50},
'icmp': {},
}
emu_clients = [emu_client1, emu_client2]
ns_key = EMUNamespaceKey(vport = 0)
emu_ns = EMUNamespaceObj(ns_key = ns_key,
clients = emu_clients,
def_c_plugs = def_c_plugs)
profile = EMUProfile(ns = emu_ns)
# start the emu profile
c.load_profile(profile = profile, max_rate = 2048, verbose = True)
# print tables of namespaces and clients
list_of_ns = c.get_all_ns_and_clients()
# Notice, you can use: "c.print_all_ns_clients(max_ns_show = 1, max_c_show = 10)" to print info in a table.
print("Let's see the profile we have created:")
for ns in list_of_ns:
pprint.pprint(ns)
'''
Should see:
client1 with: arp(timer of 30), icmp.
client2 with: arp(timer of 50), icmp, igmp.
'''
NOTICE: More emu_examples can be found at: scripts/automation/trex_control_plane/interactive/trex/examples/emu
In addition to the Python API, a console-based API interface is also available.
Console-like example
$ > ./trex-console --emu
(trex)> emu_load_profile -f emu/simple_emu.py -t --ns 2 --clients 5
...
(trex)> emu_show_all
This example will load the profile at: “emu/simple_emu.py” with tunnables of 2 namespace with 5 clients in each one. Emu profiles have their own argparse so if you aren’t sure whats the right tunnables, you may check them running: “emu_load_profile -f emu/simple_emu.py -t –help”
IMPORTANT -t must located at the end of the command.
You can explore more emu commands by entering “?” to the console and look under “Emulation Commands”. Some of the commands have additional flags, you can check them by entering “–help” at the end of the command.
EMUClient class¶
- class trex.emu.trex_emu_client.EMUClient(username='hhaim', server='localhost', sync_port=4510, verbose_level='error', logger=None, sync_timeout=None)[source]¶
Request Response abstract client. Does not support Async events like TRex Client
contains common code , only Emulation client will use it EMUClient
- get_all_ns_and_clients()[source]¶
Gets all the namespaces and clients from emu server. No yield, eager approach.
- returns
- list: List of dictionaries with all namespaces and clients.i.e: [{‘vport’: 0, ‘tci’: [1, 0], ‘tpid’: [33024, 0],‘clients’: [{‘mac’: ‘00:00:00:70:00:01’, ‘ipv4’: ‘1.1.2.3’, ‘ipv4_dg’: ‘1.1.2.1’, ‘ipv4_mtu’: 1500, ‘ipv6_local’: ‘fe80::200:ff:fe70:1’, ‘ipv6_slaac’: ‘::’, ‘ipv6’: ‘2001:db8:1::2’, ‘dg_ipv6’: ‘::’, ‘dhcp_ipv6’: ‘::’, ‘ipv4_force_dg’: False, ‘ipv4_force_mac’: ‘00:00:00:00:00:00’, ‘ipv6_force_dg’: False, ‘ipv6_force_mac’: ‘00:00:00:00:00:00’, ‘dgw’: {‘resolve’: False, ‘rmac’: ‘00:00:00:00:00:00’}, ‘ipv6_router’: None, ‘ipv6_dgw’: None, ‘plug_names’: ‘arp, igmp’}]}]
- get_all_clients_for_ns(ns_key)[source]¶
Gets all clients for a given namespace. No yield, eager approach.
- parameters
- ns_key: EMUNamespaceKey
- returns
list: List of EMUClientKey represeting all clients in namespace.
- get_counters(meta=False, zero=False, mask=None, clear=False)[source]¶
Get global counters from emu server.
- parameters
- meta: bool
True will get the meta data, defaults to False.
- zero: bool
True will send also zero values in request, defaults to False.
- mask: list of strings
List of wanted tables, defaults to None means all.
- clear: bool
Clear the counters and exit, defaults to False.
- returns
- dict: Dictionary of all wanted counters. Keys as tables names and values as dictionaries with data.example when meta = True, all counters will be held in res[‘table_name’][‘meta’] as a list.{‘parser’: {‘meta’: [{“info”: “INFO”,“name”: “eventsChangeSrc”,“value”: 1,“zero”: false,“unit”: “ops”,“help”: “change src ipv4 events”}],‘name’: ‘parser’}}example when meta = False, all counters will be held in res[‘table_name’] as a dict, counter name as a keys and numeric value as values.{‘parser’: {‘eventsChangeSrc’: 1, …}}
- print_all_ns_clients(max_ns_show, max_c_show, to_json=False, to_yaml=False, ipv6=False, dg4=False, dg6=False, ipv6_router=False)[source]¶
Prints all the namespaces and clients in tables.
- Parameters
- max_ns_show: int
Max namespace to show each time.
- max_c_show: int
Max clients to show each time.
- to_json: bool
Print the data in a json format.
- to_yaml: bool
Print the data in a yaml format.
- ipv6: bool
Show client’s ipv6 values.
- dg4: bool
Show client’s default ipv4 gateway table.
- dg6: bool
Show client’s default ipv6 gateway table.
- ipv6_router: bool
Show client’s ipv6 router table.
- get_def_c_plugs(ns_key)[source]¶
- Gets the client default plugin in a specific namespace
- parameters
- ns_key: EMUNamespaceKey
- shutdown(time)[source]¶
Shut down the EMU server gracefully after time seconds. If the server is marked for shutdown and it wasn’t shut yet, you can recall this to set a new time for shutdown.
- Args:
time (uint32): Time in seconds to shutdown the server from now.
- load_profile(profile, max_rate=None, tunables=None, dry=False, verbose=False)[source]¶
### Creating a profile and loading it ### # creating a namespace key with no vlans ns_key = EMUNamespaceKey(vport = 0) # plugs for namespace plugs = {'igmp': {}} # default plugins for each client in the namespace def_c_plugs = {'arp': {}} # creating a simple client mac = Mac('00:00:00:70:00:01) # creating Mac obj kwargs = { 'ipv4': [1, 1, 1, 3], 'ipv4_dg': [1, 1, 1, 1], 'plugs': {'icmp': {}} } client = EMUClientObj(mac.V(), **kwargs) # converting mac to list of bytes # creating the namespace with 1 client ns = EMUNamespaceObj(ns_key = ns_key, clients = [client], plugs = plugs, def_c_plugs = def_c_plugs) # every ns in the profile will have by default ipv6 plugin def_ns_plugs = {'ipv6': {}} # creating the profile and send it profile = EMUProfile(ns = ns, def_ns_plugs = def_ns_plugs) emu_client.load_profile(profile = profile) # using EMUClient ### loading from a file ### # loading profile from a file, creating 10K clients, sending 1K clients per second. emu_client.load_profile(profile = 'emu/simple_emu.py', max_rate = 1000, tunables = ['--clients', '10000'])
Load emu profile, might be EMUProfile object or a path to a valid profile. Supported type for now is .py Pay attention sending many clients with no max_rate may cause the router to crash, if you are going to send more than 10K clients perhaps you should limit max_rate using the plicer to 1000/sec ~.
- Parameters
- profilestring or EMUProfile
Filename (with path) of the profile or a valid EMUProfile object.
- max_rateint
Max clients rate to send (clients/sec), “None” means with no policer interference. see
trex.emu.trex_emu_profile.EMUProfile- tunableslist of strings
Tunables line as list of strings. i.e: [’–ns’, ‘1’, ‘–clients’, ‘10’].
- dry: bool
True will not send the profile, only print as JSON.
- verbose: bool
True will print timings for converting and sending profile.
- Raises
TRexErrorIn any case of invalid profile.
- remove_profile(max_rate=None)[source]¶
Remove current profile from emu server, all namespaces and clients will be removed
- Parameters
- max_rate: int
Max rate of client / sec for removing clients, defaults to None.
- add_ns(ns_list)[source]¶
Add namespaces to EMU server.
- Parameters
- ns_list: list of EMUNamespaceObj
- Raises
TRexError
- remove_ns(ns_keys)[source]¶
Remove namespaces from EMU server.
- Parameters
- ns_keys: list of EMUNamespaceKey
- Raises
TRexError
- add_clients(ns_key, clients, max_rate=None, verbose=False)[source]¶
Add client to EMU server.
- Parameters
- ns_key: EMUNamespaceKey
- clients: list of EMUClientObj
- max_rate: int
Max clients rate to send (clients/sec), “None” means with no policer interference.
- verbose: bool
True will print messages to screen as well as a progress bar.
- Raises
TRexError
- remove_clients(c_keys, max_rate)[source]¶
Remove clients from a specific namespace.
- Parameters
- c_keys: list of EMUClientKey
- max_rate: int
Max clients rate to send (clients/sec), “None” means with no policer interference.
- Raises
TRexError
- remove_all_clients_and_ns(max_rate=None)[source]¶
Remove all current namespaces and their clients from emu server
- Parameters
- max_rate: int
Max clients rate to send (clients/sec), “None” means with no policer interference.
- stop_autotriggers_ns(ns_key)[source]¶
Stops autotriggers for given namespace (useful when removing profile).
- Parameters
- ns_key: EMUNamespaceKey
- Raises
TRexError
- set_def_ns_plugs(def_plugs)[source]¶
Set the namespace default plugins. Every new namespace in that profile will have that plugin.
- Parameters
- def_plugs: dictionary
Map plugin_name -> plugin_data, each plugin here will be added to every new namespace. If new namespace will provide a plugin, it will override the default one.
- set_def_c_plugs(ns_key, def_plugs)[source]¶
Set the client default plugins. Every new client in that namespace will have that plugin.
- Parameters
- ns_key: EMUNamespaceKey
- def_plugs: dictionary
Map plugin_name -> plugin_data, each plugin here will be added to every new client. If new client will provide a plugin, it will override the default one.
- set_verbose(level)[source]¶
Sets verbose level
- Parameters
- levelstr
“none” - be silent no matter what “critical” “error” - show only errors (default client mode) “warning” “info” “debug” - print everything
- Raises
None
- resource_monitor_get_counters(meta=False, zero=False, mask=None, clear=False)[source]¶
Get resource monitor counters from emu server.
- parameters
- meta: bool
True will get the meta data, defaults to False.
- zero: bool
True will send also zero values in request, defaults to False.
- mask: list of strings
List of wanted tables, defaults to None means all.
- clear: bool
Clear the counters and exit, defaults to False.
- returns
- dict: Dictionary of all wanted counters. Keys as tables names and values as dictionaries with data.example when meta = True, all counters will be held in res[‘table_name’][‘meta’] as a list.{‘parser’: {‘meta’: [{“info”: “INFO”,“name”: “eventsChangeSrc”,“value”: 1,“zero”: false,“unit”: “ops”,“help”: “change src ipv4 events”}],‘name’: ‘parser’}}example when meta = False, all counters will be held in res[‘table_name’] as a dict, counter name as a keys and numeric value as values.{‘parser’: {‘eventsChangeSrc’: 1, …}}
- show_counters_ctx_line(line)[source]¶
Show counters data from ctx according to given tables.
Counter postfix: INFO - no postfix, WARNING - ‘+’, ERROR - ‘*’
- probe_server()[source]¶
Probe the server for the version / mode
Can be used to determine stateless or advanced statefull mode
- Parameters
none
- Returns
dictionary describing server version and configuration
- Raises
None
- property logger¶
Get the associated logger
- Parameters
none
- Returns
Logger object
- Raises
None
- get_verbose()[source]¶
Get the verbose mode
- Parameters
none
- Returns
Get the verbose mode as Bool
- Raises
None
- get_server_supported_cmds()[source]¶
- Parameters
None
- Returns
List of commands supported by server RPC
- Raises
None
- set_timeout(timeout_sec)[source]¶
Set timeout for connectivity to TRex server. Must be not connected.
- Parameters
- timeout_secint or float
- Timeout in seconds for sync operations.If async data does not arrive for this period, disconnect.
- Raises
TRexError- in case client is already connected.
- is_connected()[source]¶
Indicate if the connection is established to the server.
- Parameters
None
- Returns
True if the connection is established to the server o.w False
- Raises
None