Profile Modules

The TRex EMUProfile includes a number of namespaces. Each namespace is identified by: port, vlan tci(up to 2) and tpids (would be extended in the future). Each namespace has a list of clients (empty on initialize). Each client is identified with a unique mac address (key) in his own namespace. Clients also may have: ipv4, ipv4 default gateway, ipv6 addresses and more.

There are Default Plugins we can insert for every namespace / client. The default plugin of the profile will create each namespace with that plugin. Same for default namespace plugin, each client in the specific namespace will be created with the default plugin. In case we want to make a client with a particular plugin that’s fine, the new client plugin will override the default one (same for namespace creation and profile default plugins).

Notice There is a difference between a namespace plugin (located at ctx, each new ns will have that plugs by default) and a client namespace (located at ns, each new client in the ns will have that plug by default).

Example: (emu/simple_emu.py)

from trex.emu.api import *
import argparse


class Prof1():
    def __init__(self):
        self.mac = Mac('00:00:00:70:00:01')
        self.def_ns_plugs  = {'igmp' : {'dmac':self.mac.V()}} 
        self.def_c_plugs  = {'arp': {'timer': 50},
                             'igmp': {},
                            }

    def create_profile(self, ns_size, clients_size, mac, ipv4, dg, ipv6):
        ns_list = []

        # create different namespace each time
        vport, tci, tpid = 0, 1, 0x8100

        # iterate namespaces, increment vport each time 
        for i in range(vport, ns_size + vport):
            ns_key = EMUNamespaceKey(vport  = i,
                                    tci     = tci,
                                    tpid    = tpid
                                    )
            ns = EMUNamespaceObj(ns_key = ns_key, def_c_plugs = self.def_c_plugs
                                )

            mac  = Mac(mac)
            ipv4 = Ipv4(ipv4)
            dg   = Ipv4(dg)
            ipv6 = Ipv6(ipv6)
            
            # create a different client each time
            for j in range(clients_size):
                client = EMUClientObj(mac     = mac[j].V(),
                                      ipv4    = ipv4[j].V(),
                                      ipv4_dg = dg.V(),
                                      ipv6    = ipv6[j].V(),
                                      )
                ns.add_clients(client)

            ns_list.append(ns)

        return EMUProfile(ns = ns_list, def_ns_plugs = self.def_ns_plugs)

    def get_profile(self, tuneables):
        # Argparse for tunables
        parser = argparse.ArgumentParser(description='Argparser for simple emu profile.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        parser.add_argument('--ns', type = int, default = 1,
                    help='Number of namespaces to create')
        parser.add_argument('--clients', type = int, default = 15,
                    help='Number of clients to create in each namespace')

        # client args
        parser.add_argument('--mac', type = str, default = '00:00:00:70:00:01',
            help='Mac address of the first client')
        parser.add_argument('--4', type = str, default = '1.1.2.3', dest = 'ipv4',
            help='IPv4 address of the first client')
        parser.add_argument('--dg', type = str, default = '1.1.2.1',
            help='Default Gateway address of the first client')
        parser.add_argument('--6', type = str, default = '2001:DB8:1::2', dest = 'ipv6',
            help='IPv6 address of the first client')

        args = parser.parse_args(tuneables)
        
        assert 0 < args.ns < 65535, 'Namespcaes size must be positive! in range of ports: 0 < ns < 65535'
        assert 0 < args.clients, 'Clients size must be positive!'
        
        return self.create_profile(args.ns, args.clients, args.mac, args.ipv4, args.dg, args.ipv6)

def register():
    return Prof1()

Every emu profile has argparse so tunable can be passed from outside and use the profile as they like.

EMUProfile class

class trex.emu.trex_emu_profile.EMUProfile(ns=None, def_ns_plugs=None)[source]

Create an Emu profile.

# 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
parameters
ns: EmuNamespaceObj or list of EmuNamespaceObj

The namespace/s to use in profile, defaults to None.

def_ns_plugs: dict

Dictionary with all the default namespace plugins, keys as plugs names and values as plugs info. If a namespace hasn’t plugins it will take the default, defaults to None.

raises
  • exe:’TRexError’

    In any case of wrong parameters.

returns

EMUProfile: Emu profile object, ready to send.

to_json()[source]

Convert profile to a json.

returns
dict: Profile data in dictionary format.
i.e: {
‘def_ns_plugs’: {‘igmp’: {}},
‘namespaces’: [
{
‘def_c_plugs’: {‘icmp’: {}},
‘clients’: [{‘mac’: ‘00:00:00:70:00:01’,
‘ipv4’: ‘1.1.1.3’,
‘ipv4_dg: ‘1.1.1.1’
}],
‘plugs’: {‘arp’: {}},
}]
}
classmethod load(filename, tunables)[source]

Load emu profile by its type. Supported type for now is .py

Parameters

filename : string as filename tunables : list of strings, list of tunables data. i.e: [’–clients’, ‘10’]

EMUNamespaceObj class

class trex.emu.trex_emu_profile.EMUNamespaceObj(ns_key, clients=None, plugs=None, def_c_plugs=None)[source]

Create Emu Namespace object.

# 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)

# Summary: ns obj will have igmp, 1 client with icmp plugin and arp plugin he got by default from ns
parameters
ns_key: EMUNamespaceKey

Namespace key identification.

clients: EMUClientObj or list of EMUClientObj

Client/s for namespace, defaults to None.

plugs: dictionary

Plugins for namespace, keys as plug names values as plug indfo. Not to be confused with def_c_plugs plugs are namespace plugins, defaults to None.

def_c_plugs: dictionary

Default client plugins, defaults to None. Every client in the namespace gets those default plugins, If a client has his own plugs it will be merged with the default one and overide duplicates giving favor to the client.

add_clients(clients)[source]

Add clients to namespace.

parameters
clients: EMUClientObj / list of EMUClientObj

Clients to add, may be a list or just one.

raises
  • exe:’TRexError’

    If client with the same mac address is already in namespace.

to_json()[source]

Convert namespace to a json.

returns
dict: Namespace data in dictionary format
i.e: {
‘def_c_plugs’: {},
‘clients’: [{‘mac’: ‘00:00:00:70:00:01’,
‘ipv4’: ‘1.1.1.3’,
‘ipv4_dg: ‘1.1.1.1’
}],
‘plugs’: {‘arp’: {}},
}
get_fields()[source]

Get namespace fields.

returns
dict: Dictionary with all namespace fields.
i.e: {
‘vport’: 0, ‘tci’: [0, 0], ‘tpid’: [0, 0], ‘plugs’: {‘igmp’: {}}
}

EMUClientObj class

class trex.emu.trex_emu_profile.EMUClientObj(mac, **kwargs)[source]

Create a new Emu client object.

# creating a simple client
mac = Mac('00:00:00:70:00:01)  # creating Mac obj
EMUClientObj(mac.V())          # converting mac to list of bytes

# creating a client with parameters
ipv4, ipv4_dg = Ipv4('1.1.1.3'), Ipv4('1.1.1.1')
ipv6 = Ipv6('::1234')

# kwargs keys must be identical to the ones in our docs, see kwargs description.
kwargs = {
    'ipv4': ipv4.V(),        # converting every field using .V()
    'ipv4_dg': ipv4_dg.V(),
    'ipv6': ipv6.V(),
    'plugs': {'arp': {}}     # client's plugin
}
EMUClientObj(mac.V(), ipv4 = ipv4.V(), **kwargs)  
parameters
mac: list of bytes

Mac address for a client. see trex.emu.trex_emu_conversions.Mac

kwargs: dict

Additional parameters for client, besides the mac. Those may be found in emu docs under CClientCmd struct

Here are some of the useful ones:

Key

Type

Description

ipv4

List of 4 bytes

IPv4 address of client

ipv4_dg

List of 4 bytes

IPv4 address of the default gateway

ipv6

List of 16 bytes

IPv6 address of client

dg_ipv6

List of 16 bytes

IPv6 address of the default gateway

plugs

Dictionary

Dict with client’s plugins. keys as plug names, value as plug info.

to_json()[source]

Convert client into a JSON format.

returns
dict: Client’s data in a dictionary format.
i.e: {‘mac’: ‘00:00:00:70:00:01’,
‘ipv4’: ‘1.1.1.3’,
‘ipv4_dg: ‘1.1.1.1’
}
get_fields(to_bytes=False)[source]

Get client’s inner fields.

parameters
to_bytes: bool

True will convert fields into list of bytes, False to strings. Defaults to False.

returns
dict: Dictionary represents the client’s fields.
i.e: client.get_fields(to_bytes = False) ->
{ ‘mac’: ‘00:00:00:70:00:01’,
‘ipv4’: ‘1.1.1.3’,
‘ipv4_dg: ‘1.1.1.1’
}

EMUNamespaceKey class

class trex.emu.trex_emu_profile.EMUNamespaceKey(vport, tci=None, tpid=None, **kwargs)[source]

Creates a namespace key, defines a namespace in emulation server.

# creating a namespace key with no vlans
ns_key = EMUNamespaceKey(vport = 0)

# creating a namespace key with vlan using default tpid(0x8100)
ns_key = EMUNamespaceKey(vport = 0, tci = 1)

# creating a namespace key with 2 vlans using tpid
ns_key = EMUNamespaceKey(vport = 0, tci = [1, 2], tpid = [0x8100, 0x8100])
parameters
vport: int (Mandatory)

Port for the namespace.

tci: int or list of 2 ints. (Optional)

Tag Control Information/s for the namespace (up to 2), defaults to None means no Vlan.

tpid: int or 2 ints. (Optional)

Tag Protocol Identifier/s for the tci/s (up to 2), defaults to None means no vlan. If tci supplied without tpid, it will set tpid to 0x8100 by default.

raises
  • exe:’TRexError’

    In any case of wrong parameters.

conv_to_dict(add_tunnel_key=False)[source]

Convert ns key to dictionary.

parameters
add_tunnel_key: bool

True will add the tunnel key (ready for sending over RPC), defaults to False.

returns

dict: dictionary with ns key parameters

EMUClientKey class

class trex.emu.trex_emu_profile.EMUClientKey(ns_key, mac)[source]

Creating client key for identification clients in emu.

mac    = Mac('00:00:00:70:00:01')       # creating a Mac obj
ns_key = EMUNamespaceKey(vport = 0)     # creating ns_key with no vlans
c_key  = EMUClientKey(ns_key, mac.V())  # creating client key, notice mac converted to list of bytes
parameters
ns_key: EMUNamespaceKey

Namespace key, see trex.emu.trex_emu_profile.EMUNamespaceKey

mac: list of bytes

Mac address of the client. Use Mac object to convert from string to list of bytes see trex.emu.trex_emu_conversions.Mac

conv_to_dict(add_ns=False, to_bytes=False)[source]

Convert the key to a dictionary.

parameters
add_ns: bool

True will add the ns key as well to the dict, defaults to False.

to_bytes: bool

True will convert values to bytes, defaults to False.

returns

dict: dictionary representing the client key