p4utils.utils.thrift_API module

This module is a modified version of runtime_CLI that performs control plane operations by connecting to the Thrift server running on a capable switch. The main difference between this code and the original one is that this module is an API, whereas the other is a client.

class p4utils.utils.thrift_API.Action(name, id_)[source]

Bases: object

action_str()[source]
num_params()[source]
runtime_data_str()[source]
class p4utils.utils.thrift_API.ActionProf(name, id_)[source]

Bases: object

action_prof_str()[source]
get_action(action_name, suffix_lookup_map)[source]
p4utils.utils.thrift_API.BmMatchParamExact_to_str(self)[source]
p4utils.utils.thrift_API.BmMatchParamLPM_to_str(self)[source]
p4utils.utils.thrift_API.BmMatchParamRange_to_str(self)[source]
p4utils.utils.thrift_API.BmMatchParamTernary_to_str(self)[source]
p4utils.utils.thrift_API.BmMatchParamValid_to_str(self)[source]
p4utils.utils.thrift_API.BmMatchParam_to_str(self)[source]
exception p4utils.utils.thrift_API.CLI_FormatExploreError[source]

Bases: Exception

class p4utils.utils.thrift_API.CounterArray(name, id_)[source]

Bases: object

counter_str()[source]
class p4utils.utils.thrift_API.MatchType[source]

Bases: object

EXACT = 0[source]
LPM = 1[source]
RANGE = 4[source]
TERNARY = 2[source]
VALID = 3[source]
static from_str(x)[source]
static to_str(x)[source]
class p4utils.utils.thrift_API.MeterArray(name, id_)[source]

Bases: object

meter_str()[source]
class p4utils.utils.thrift_API.MeterType[source]

Bases: object

bytes = 1[source]
static from_str(x)[source]
packets = 0[source]
static to_str(x)[source]
class p4utils.utils.thrift_API.ParseVSet(name, id_)[source]

Bases: object

parse_vset_str()[source]
class p4utils.utils.thrift_API.PreType[source]

Bases: object

SimplePre = 1[source]
SimplePreLAG = 2[source]
static from_str(x)[source]
none = 0[source]
static to_str(x)[source]
class p4utils.utils.thrift_API.RegisterArray(name, id_)[source]

Bases: object

register_str()[source]
class p4utils.utils.thrift_API.ResType[source]

Bases: object

action = 2[source]
action_prof = 1[source]
counter_array = 4[source]
static from_str(x)[source]
meter_array = 3[source]
parse_vset = 6[source]
register_array = 5[source]
table = 0[source]
static to_str(x)[source]
class p4utils.utils.thrift_API.SwitchInfo[source]

Bases: object

load_json_config(standard_client=None, json_path=None)[source]
load_json_str(json_str, architecture_spec=None)[source]
reset_config()[source]
class p4utils.utils.thrift_API.Table(name, id_)[source]

Bases: object

get_action(action_name, suffix_lookup_map)[source]
key_str()[source]
num_key_fields()[source]
table_str()[source]
class p4utils.utils.thrift_API.TableType[source]

Bases: object

static from_str(x)[source]
indirect = 1[source]
indirect_ws = 2[source]
simple = 0[source]
static to_str(x)[source]
class p4utils.utils.thrift_API.ThriftAPI(thrift_port, thrift_ip, pre_type, json_path=None)[source]

Bases: object

This class implements a client that connects to a P4 switch control plane and allows to configure it. It establishes a Thrift connection with the switch server that is then used to forward configuration information.

Parameters:
  • thrift_port (int) – port number the Thrift server listens on
  • thrift_ip (str) – IP the Thrift server listens on
  • pre_type (str) – PreType option
  • json_path (str) – path to the P4 compiled JSON file (optional)

Possible values for pre_type are the following:

  • simple_pre
  • simple_pre_lag
  • none
switch_info[source]

object containing information about all the P4 structures present in the switch.

Type:SwitchInfo
client[source]

Thrift client instance.

mc_client[source]

Thrift mc client instance.

pre_type[source]

given PreType option.

Type:str
table_entries_match_to_handle[source]

dictionary used to associate table matches with the related handle.

Type:dict
act_prof_add_member_to_group(act_prof_name, mbr_handle, grp_handle)[source]

Adds member to group in an action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • mbr_handle (int) – member handle
  • grp_handle (int) – group handle
act_prof_create_group(act_prof_name)[source]

Adds a group to an action pofile.

Parameters:act_prof_name (str) – name of the action profile
Returns:group handle.
Return type:int
act_prof_create_member(act_prof_name, action_name, action_params=[])[source]

Adds a member to an action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • action_name (str) – name of the action
  • action_params (list) – list of action parameters in the order they appear in the P4 code
Returns:

member handle.

Return type:

int

Note

In action_params, action parameters must be str.

act_prof_delete_group(act_prof_name, grp_handle)[source]

Delete a group from an action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • grp_handle (int) – group handle
act_prof_delete_member(act_prof_name, mbr_handle)[source]

Deletes a member in an action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • mbr_handle (int) – member handle
act_prof_dump(act_prof_name)[source]

Displays entries in an action profile.

Parameters:act_prof_name (str) – name of the action profile
act_prof_dump_group(act_prof_name, grp_handle)[source]

Displays some information about a group.

Parameters:
  • act_prof_name (str) – name of the action profile
  • grp_handle (int) – group handle
act_prof_dump_member(act_prof_name, mbr_handle)[source]

Displays some information about a member.

Parameters:
  • act_prof_name (str) – name of the action profile
  • mbr_handle (int) – member handle
act_prof_modify_member(act_prof_name, action_name, mbr_handle, action_params=[])[source]

Modifies member in an action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • action_name (str) – name of the action
  • mbr_handle (int) – member handle
  • action_params (list) – list of action parameters in the order they appear in the P4 code
Returns:

member handle.

Return type:

int

Note

In action_params, action parameters must be str.

act_prof_remove_member_from_group(act_prof_name, mbr_handle, grp_handle)[source]

Removes member from group in action profile.

Parameters:
  • act_prof_name (str) – name of the action profile
  • mbr_handle (int) – member handle
  • grp_handle (int) – group handle
check_act_prof_ws(act_prof)[source]
check_has_pre()[source]
check_indirect(table)[source]
check_indirect_ws(table)[source]
counter_read(counter_name, index)[source]

Reads counter value.

Parameters:
  • counter_name (str) – name of the counter
  • index (int) – index in the array of counters
Returns:

(byte_count, packet_count) where:

  • byte_count is the number of bytes counted;
  • packet_count is the number of bytes counted.

Return type:

tuple

counter_reset(counter_name)[source]

Resets counter array.

Parameters:counter_name (str) – name of the counter
counter_write(counter_name, index, pkts=0, byts=0)[source]

Write counter value.

Parameters:
  • counter_name (str) – name of the counter
  • index (int) – index in the array of counters (first element is at 0)
  • pkts (int) – number of packets to write (default: 0)
  • byts (int) – number of bytes to write (default: 0)
create_match_to_handle_dict()[source]
dump_action_and_data(action_name, action_data)[source]
dump_action_entry(a_entry)[source]
dump_groups(groups)[source]
dump_members(members)[source]
dump_one_entry(table, entry)[source]
dump_one_group(group)[source]
dump_one_member(member)[source]
get_action_profs()[source]
get_actions()[source]
get_counter_arrays()[source]
get_custom_crc_calcs()[source]
get_handle_from_match(table_name, match_keys, pop=False)[source]
get_meter_arrays()[source]
get_mgrp(s)[source]
get_node_handle(s)[source]
get_parse_vsets()[source]
get_register_arrays()[source]
get_res(type_name, name, res_type)[source]
get_suffix_lookup_map()[source]
get_tables()[source]
static get_thrift_services(pre_type)[source]
load_new_config_file(filename)[source]

Loads new JSON config.

Parameters:filename (str) – path to the P4 compiled JSON file
load_table_entries_match_to_handle()[source]
load_table_to_all_names()[source]
mc_dump()[source]

Dumps entries in multicast engine.

mc_mgrp_create(mgrp)[source]

Creates multicast group.

Parameters:mgrp (int) – multicast group id
Returns:multicast group handle.
Return type:int

Note

The multicast group handle is equal to the multicast group id.

mc_mgrp_destroy(mgrp)[source]

Destroys multicast group.

Parameters:mgrp (int) – multicast group id
mc_node_associate(mgrp, l1_hdl)[source]

Associates node to multicast group.

Parameters:
  • mgrp (int) – multicast group id
  • l1_hdl (int) – node handle
mc_node_create(rid, ports, lags=[])[source]

Creates multicast node.

Parameters:
  • rid (int) – replication id
  • ports (list) – list of port numbers
  • lags (list) – list of lags
Returns:

node handle.

Return type:

int

mc_node_destroy(l1_hdl)[source]

Destroys multicast node.

Parameters:l1_hdl (int) – node handle
mc_node_dissociate(mgrp, l1_hdl)[source]

Dissociates node from multicast group.

Parameters:
  • mgrp (int) – multicast group handle
  • l1_hdl (int) – node handle
mc_node_update(l1_hdl, ports, lags=[])[source]

Updates multicast node.

Parameters:
  • l1_hdl (int) – node handle
  • ports (list) – list of port numbers
  • lags (list) – list of lags
Returns:

node handle.

Return type:

int

mc_set_lag_membership(lag_index, ports)[source]

Sets lag membership of port list.

Parameters:
  • lag_index (int) – lag index
  • ports (list) – list of port numbers
meter_array_set_rates(meter_name, rates)[source]

Configures rates for an entire meter array.

Parameters:
  • meter_name (str) – name of the meter
  • rates (list) – [(cir, cburst), (pir, pburst)]

Note

cir and pir use units/second, cbursts and pburst use units where units is bytes or packets, depending on the meter type.

meter_get_rates(meter_name, index)[source]

Retrieves rates for a meter.

Parameters:
  • meter_name (str) – name of the meter
  • index (int) – index in the array of meters (first element is at 0)
Returns:

[(cir, cburst), (pir, pburst)].

Return type:

list

Note

cir and pir use units/second, cbursts and pburst use units where units is bytes or packets, depending on the meter type.

meter_set_rates(meter_name, index, rates)[source]

Configures rates for a meter.

Parameters:
  • meter_name (str) – name of the meter
  • index (int) – index in the array of meters (first element is at 0)
  • rates (list) – [(cir, cburst), (pir, pburst)]

Note

cir and pir use units/second, cbursts and pburst use units where units is bytes or packets, depending on the meter type.

parse_runtime_data(action, action_params)[source]
port_add(iface_name, port_num, pcap_path='')[source]

Adds a port to the switch (behavior depends on device manager used).

Parameters:
  • iface_name (str) – interface name
  • port_num (int) – port number
  • pcap_path (str) – path where the .pcap files are saved (optional)
port_remove(port_num)[source]

Removes a port from the switch (behavior depends on device manager used).

Parameters:port_num (int) – port number
ports_to_port_map_str(ports, description='port')[source]
print_set_default(table_name, action_name, runtime_data)[source]
print_table_add(match_key, action_name, runtime_data)[source]
pvs_add(pvs_name, value)[source]

Adds a value to a parser value set.

Parameters:
  • pvs_name (str) – name of the parser value set
  • value (int) – value to add

Warning

BMv2 will not report an error if the value already exists.

pvs_clear(pvs_name)[source]

Removes all values from a parser value set.

Parameters:pvs_name (str) – name of the parser value set
pvs_get(pvs_name)[source]

Retrieves all values from a parser value set.

Parameters:pvs_name (str) – name of the parser value set
Returns:values of the parser value set
Return type:list

Note

Values are displayed in no particular order, one per line.

pvs_remove(pvs_name, value)[source]

Removes a value from a parser value set.

Parameters:
  • pvs_name (str) – name of the parser value set
  • value (int) – value to remove

Warning

BMv2 will not report an error if the value does not exist.

register_read(register_name, index=None, show=False)[source]

Reads register value.

Parameters:
  • register_name (str) – name of the register
  • index (int) – index in the array of registers (if None, the whole array will be read)
  • show (bool) – enable verbose output
Returns:

register value or list of values.

Return type:

int or list

register_reset(register_name)[source]

Resets all the cells in the register array to 0.

Parameters:register_name (str) – name of the register
register_write(register_name, index, value)[source]

Writes register value.

Parameters:
  • register_name (str) – name of the register
  • index (int or list) – index in the array of registers or [start_index, end_index]
  • value (int) – value to write
reset_state()[source]

Resets all state in the switch (table entries, registers, …), but P4 config is preserved.

serialize_state(filename)[source]

Serializes the switch state and dumps it to user-specified file.

set_crc16_parameters(name, polynomial, initial_remainder, final_xor_value, reflect_data, reflect_remainder)[source]

Changes the parameters for a custom crc16 hash.

Parameters:
  • name (str) – hash name
  • polynomial (int) – polynomial
  • intial_remainder (int) – initial reminder
  • final_xor_value (int) – final xor value
  • reflect_data (bool) – reflect data or do not
  • reflect_remainder (bool) – reflect remainder or do not
set_crc32_parameters(name, polynomial, initial_remainder, final_xor_value, reflect_data, reflect_remainder)[source]

Changes the parameters for a custom crc32 hash.

Parameters:
  • name (str) – name
  • polynomial (int) – polynomial
  • intial_remainder (int) – initial reminder
  • final_xor_value (int) – final xor value
  • reflect_data (bool) – reflect data or do not
  • reflect_remainder (bool) – reflect remainder or do not
set_crc_parameters_common(name, polynomial, initial_remainder, final_xor_value, reflect_data, reflect_remainder, crc_width=16)[source]
shell(line)[source]

Runs a shell command.

show_actions()[source]

Lists actions defined in the P4 program.

show_ports()[source]

Shows the ports connected to the switch.

show_pvs(line)[source]

Lists parser value sets defined in the P4 program.

show_tables()[source]

Lists tables defined in the P4 program.

swap_configs()[source]

Swap the 2 existing configs, need to have called load_new_config_file() before.

switch_info()[source]

Shows some basic info about the switch.

table_add(table_name, action_name, match_keys, action_params=[], prio=0)[source]

Adds entry to a match table.

Parameters:
  • table_name (str) – name of the table
  • action_name (str) – name of the action to execute
  • match_keys (list) – list of matches in the order they appear in the P4 code
  • action_params (list) – list of action parameters in the order they appear in the P4 code
  • prio (int) – priority in ternary matches
Returns:

entry handle.

Return type:

int

Note

  • In action_params and match_keys, action parameters and match keys must be str.
  • A higher prio number indicates that the entry must be given higher priority when performing a table lookup.
table_clear(table_name)[source]

Clears all entries in a match table (direct or indirect), but not the default entry.

Parameters:table_name (str) – name of the table
table_delete(table_name, entry_handle, quiet=False)[source]

Deletes entry from a match table.

Parameters:
  • table_name (str) – name of the table
  • entry_handle (int) – entry handle
  • quiet (bool) – disable verbose output
table_delete_match(table_name, match_keys)[source]

Deletes entry from a table using match keys.

Parameters:
  • table_name (str) – name of the table
  • match_keys (list) – list of matches in the order they appear in the P4 code

Note

In match_keys, match keys must be str.

Warning

This may not work with ternary matches and priority entries.

table_dump(table_name)[source]

Displays entries in a match-table.

Parameters:table_name (str) – name of the table
table_dump_entry(table_name, entry_handle)[source]

Displays some information about a table entry.

Parameters:
  • table_name (str) – name of the table
  • entry_handle (int) – entry handle
table_dump_entry_from_key(table_name, match_keys, prio=0)[source]

Displays some information about a table entry using match keys.

Parameters:
  • table_name (str) – name of the table
  • match_keys (list) – list of matches in the order they appear in the P4 code
  • prio (int) – priority in ternary match

Note

In match_keys, match keys must be str.

table_info(table_name)[source]

Shows info about a table.

Parameters:table_name (str) – name of the table
table_modify(table_name, action_name, entry_handle, action_params=[])[source]

Modifies entry of a match table.

Parameters:
  • table_name (str) – name of the table
  • action_name (str) – name of the action
  • entry_handle (int) – entry handle
  • action_params (list) – list of action parameters in the order they appear in the P4 code
Returns:

entry handle.

Return type:

int

Note

In action_params, action parameters must be str.

table_modify_match(table_name, action_name, match_keys, action_params=[])[source]

Modifies entry of a match table using match keys.

Parameters:
  • table_name (str) – name of the table
  • action_name (str) – name of the action
  • match_keys (list) – list of matches in the order they appear in the P4 code
  • action_params (list) – list of action parameters in the order they appear in the P4 code
Returns:

entry handle.

Return type:

int

Note

In action_params, action parameters must be str.

table_num_entries(table_name)[source]

Returns the number of entries in a match table (direct or indirect).

Parameters:table_name (str) – name of the table
Returns:the number of entries in a table.
Return type:int
table_reset_default(table_name)[source]

Resets default entry for a match table.

Parameters:table_name (str) – name of the table
table_set_default(table_name, action_name, action_params=[])[source]

Sets default action for a match table.

Parameters:
  • table_name (str) – name of the table
  • action_name (str) – name of the default action
  • action_params (list) – list of parameters for the default action

Note

In action_params, action parameters must be str.

table_set_timeout(table_name, entry_handle, timeout_ms)[source]

Sets a timeout in ms for a given entry; the table has to support timeouts.

Parameters:
  • table_name (str) – name of the table
  • entry_handle (int) – entry handle
  • timeout_ms (int) – entry timeout in ms
table_show_actions(table_name)[source]

Lists one table’s actions as per the P4 program.

Parameters:table_name (str) – name of the table
write_config_to_file(filename)[source]

Retrieves the JSON config currently used by the switch and dumps it to user-specified file.

exception p4utils.utils.thrift_API.UIn_BadIPv4Error[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_BadIPv6Error[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_BadMacError[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_BadParamError(info='')[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_Error(info='')[source]

Bases: Exception

exception p4utils.utils.thrift_API.UIn_MatchKeyError(info='')[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_ResourceError(res_type, name)[source]

Bases: p4utils.utils.thrift_API.UIn_Error

exception p4utils.utils.thrift_API.UIn_RuntimeDataError(info='')[source]

Bases: p4utils.utils.thrift_API.UIn_Error

p4utils.utils.thrift_API.bytes_to_string(byte_array)[source]
p4utils.utils.thrift_API.deprecated_act_prof(substitute, with_selection=False, strictly_deprecated=True)[source]
p4utils.utils.thrift_API.enum(type_name, *sequential, **named)[source]
p4utils.utils.thrift_API.handle_bad_input(f)[source]
p4utils.utils.thrift_API.handle_bad_input_mc(f)[source]
p4utils.utils.thrift_API.hex_to_i16(h)[source]
p4utils.utils.thrift_API.hex_to_i32(h)[source]
p4utils.utils.thrift_API.hexstr(v)[source]
p4utils.utils.thrift_API.i16_to_hex(h)[source]
p4utils.utils.thrift_API.i32_to_hex(h)[source]
p4utils.utils.thrift_API.int_to_bytes(i, num)[source]
p4utils.utils.thrift_API.ipv4Addr_to_bytes(addr)[source]
p4utils.utils.thrift_API.ipv6Addr_to_bytes(addr)[source]
p4utils.utils.thrift_API.macAddr_to_bytes(addr)[source]
p4utils.utils.thrift_API.parse_bool(s)[source]
p4utils.utils.thrift_API.parse_match_key(table, key_fields)[source]
p4utils.utils.thrift_API.parse_param(input_str, bitwidth)[source]
p4utils.utils.thrift_API.parse_pvs_value(input_str, bitwidth)[source]
p4utils.utils.thrift_API.parse_runtime_data(action, params)[source]
p4utils.utils.thrift_API.printable_byte_str(s)[source]
p4utils.utils.thrift_API.table_error_name(x)[source]
p4utils.utils.thrift_API.thrift_connect(thrift_ip, thrift_port, services)[source]