Skip to content

Commit

Permalink
[dash-p4] Add macro for defining and update regular counters. (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
r12f authored Mar 4, 2024
1 parent c5f713f commit 04e8617
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 65 deletions.
97 changes: 65 additions & 32 deletions dash-pipeline/bmv2/dash_arch_specific.p4
Original file line number Diff line number Diff line change
@@ -1,47 +1,80 @@
#ifndef __DASH_TARGET_SPECIFIC__
#define __DASH_TARGET_SPECIFIC__

#ifdef TARGET_BMV2_V1MODEL

#include <v1model.p4>
//
// P4 arch/target includes
//
#if defined(TARGET_BMV2_V1MODEL)
#include <v1model.p4>
#elif defined(TARGET_DPDK_PNA) // TARGET_BMV2_V1MODEL
#include <pna.p4>
#endif // TARGET_DPDK_PNA

//
// Counters
#define DEFINE_TABLE_COUNTER(counter_name) direct_counter(CounterType.packets_and_bytes) counter_name;
#define ATTACH_TABLE_COUNTER(counter_name) counters = counter_name;
#define DIRECT_COUNTER_TABLE_PROPERTY counters
// - The counters are defined differently for different arch.
//
#if defined(TARGET_BMV2_V1MODEL)

#define DEFINE_COUNTER(name, count, ...) \
@SaiCounter[__VA_ARGS__] \
counter(count, CounterType.packets_and_bytes) name;

// DBC (Design By Contract) macros
#define REQUIRES(cond) assert(cond)
#define DEFINE_PACKET_COUNTER(name, count, ...) \
@SaiCounter[__VA_ARGS__] \
counter(count, CounterType.packets) name;

#endif // TARGET_BMV2_V1MODEL
#define DEFINE_BYTE_COUNTER(name, count, ...) \
@SaiCounter[__VA_ARGS__] \
counter(count, CounterType.bytes) name;

#ifdef TARGET_DPDK_PNA
#define UPDATE_COUNTER(name, index) \
name.count((bit<32>)index)

#define DEFINE_TABLE_COUNTER(counter_name) direct_counter(CounterType.packets_and_bytes) counter_name;
#define ATTACH_TABLE_COUNTER(counter_name) counters = counter_name;

#elif defined(TARGET_DPDK_PNA) // TARGET_BMV2_V1MODEL

// Counters are not supported yet for PNA arch in DASH
#define DEFINE_COUNTER(name, count, ...)
#define DEFINE_PACKET_COUNTER(name, count, ...)
#define DEFINE_BYTE_COUNTER(name, count, ...)
#define UPDATE_COUNTER(name, index)

#include <pna.p4>
#ifdef DPDK_SUPPORTS_DIRECT_COUNTER_ON_WILDCARD_KEY_TABLE
// Omit all direct counters for tables with ternary match keys,
// because the latest version of p4c-dpdk as of 2023-Jan-26 does
// not support this combination of features. If you try to
// compile it with this code enabled, the error message looks like
// this:
//
// [--Werror=target-error] error: Direct counters and direct meters are unsupported for wildcard match table outbound_acl_stage1:dash_acl_rule|dash_acl
//
// This p4c issue is tracking this feature gap in p4c-dpdk:
// https://github.com/p4lang/p4c/issues/3868
#define DEFINE_TABLE_COUNTER(counter_name) DirectCounter<bit<64>>(PNA_CounterType_t.PACKETS_AND_BYTES) counter_name;
#define ATTACH_TABLE_COUNTER(counter_name) pna_direct_counter = counter_name;
#else
#define DEFINE_TABLE_COUNTER(counter_name)
#define ATTACH_TABLE_COUNTER(counter_name)
#endif

// Counters
#ifdef DPDK_SUPPORTS_DIRECT_COUNTER_ON_WILDCARD_KEY_TABLE
// Omit all direct counters for tables with ternary match keys,
// because the latest version of p4c-dpdk as of 2023-Jan-26 does
// not support this combination of features. If you try to
// compile it with this code enabled, the error message looks like
// this:
//
// [--Werror=target-error] error: Direct counters and direct meters are unsupported for wildcard match table outbound_acl_stage1:dash_acl_rule|dash_acl
//
// This p4c issue is tracking this feature gap in p4c-dpdk:
// https://github.com/p4lang/p4c/issues/3868
#define DEFINE_TABLE_COUNTER(counter_name) DirectCounter<bit<64>>(PNA_CounterType_t.PACKETS_AND_BYTES) counter_name;
#define ATTACH_TABLE_COUNTER(counter_name) pna_direct_counter = counter_name;
#else
#define DEFINE_TABLE_COUNTER(counter_name)
#define ATTACH_TABLE_COUNTER(counter_name)
#endif
#endif // TARGET_DPDK_PNA

//
// DBC (Design By Contract) macros
// NOTE: PNA doesn't support assert, hence all macros are defined as empty
#define REQUIRES(cond)

// - These macros will be used as a replacement for asserts, which makes the precondition and postcondition checks more explicit.
//
#if defined(TARGET_BMV2_V1MODEL)

#define REQUIRES(cond) assert(cond)

#elif defined(TARGET_DPDK_PNA) // TARGET_BMV2_V1MODEL

// NOTE: PNA doesn't support assert, hence all macros are defined as empty
#define REQUIRES(cond)

#endif // TARGET_DPDK_PNA

#endif // __DASH_TARGET_SPECIFIC__
18 changes: 4 additions & 14 deletions dash-pipeline/bmv2/dash_pipeline.p4
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ control dash_ingress(
action accept() {
}

#ifdef TARGET_BMV2_V1MODEL
@SaiCounter[name="lb_fast_path_icmp_in", attr_type="stats"]
counter(1, CounterType.packets_and_bytes) port_lb_fast_path_icmp_in_counter;
#endif
DEFINE_COUNTER(port_lb_fast_path_icmp_in_counter, 1, name="lb_fast_path_icmp_in", attr_type="stats")

@SaiTable[name = "vip", api = "dash_vip"]
table vip {
Expand Down Expand Up @@ -95,10 +92,7 @@ control dash_ingress(
meta.stage4_dash_acl_group_id = ## prefix ##_stage4_dash_acl_group_id; \
meta.stage5_dash_acl_group_id = ## prefix ##_stage5_dash_acl_group_id;

#ifdef TARGET_BMV2_V1MODEL
@SaiCounter[name="lb_fast_path_icmp_in", attr_type="stats", action_names="set_eni_attrs"]
counter(MAX_ENI, CounterType.packets_and_bytes) eni_lb_fast_path_icmp_in_counter;
#endif
DEFINE_COUNTER(eni_lb_fast_path_icmp_in_counter, MAX_ENI, name="lb_fast_path_icmp_in", attr_type="stats", action_names="set_eni_attrs")

action set_eni_attrs(bit<32> cps,
bit<32> pps,
Expand Down Expand Up @@ -244,9 +238,7 @@ control dash_ingress(
#endif // TARGET_DPDK_PNA

if (meta.is_fast_path_icmp_flow_redirection_packet) {
#ifdef TARGET_BMV2_V1MODEL
port_lb_fast_path_icmp_in_counter.count(0);
#endif
UPDATE_COUNTER(port_lb_fast_path_icmp_in_counter, 0);
}

if (vip.apply().hit) {
Expand Down Expand Up @@ -311,9 +303,7 @@ control dash_ingress(
}

if (meta.is_fast_path_icmp_flow_redirection_packet) {
#ifdef TARGET_BMV2_V1MODEL
eni_lb_fast_path_icmp_in_counter.count((bit<32>)meta.eni_id);
#endif
UPDATE_COUNTER(eni_lb_fast_path_icmp_in_counter, meta.eni_id);
}

acl_group.apply();
Expand Down
9 changes: 2 additions & 7 deletions dash-pipeline/bmv2/stages/eni_lookup.p4
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ control eni_lookup_stage(
inout headers_t hdr,
inout metadata_t meta)
{
#ifdef TARGET_BMV2_V1MODEL
@SaiCounter[name="lb_fast_path_eni_miss", attr_type="stats"]
counter(1, CounterType.packets_and_bytes) port_lb_fast_path_eni_miss_counter;
#endif
DEFINE_COUNTER(port_lb_fast_path_eni_miss_counter, 1, name="lb_fast_path_eni_miss", attr_type="stats")

action set_eni(@SaiVal[type="sai_object_id_t"] bit<16> eni_id) {
meta.eni_id = eni_id;
Expand Down Expand Up @@ -39,9 +36,7 @@ control eni_lookup_stage(

if (!eni_ether_address_map.apply().hit) {
if (meta.is_fast_path_icmp_flow_redirection_packet) {
#ifdef TARGET_BMV2_V1MODEL
port_lb_fast_path_eni_miss_counter.count(0);
#endif
UPDATE_COUNTER(port_lb_fast_path_eni_miss_counter, 0);
}
}
}
Expand Down
16 changes: 4 additions & 12 deletions dash-pipeline/bmv2/stages/metering_update.p4
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ control metering_update_stage(

// MAX_METER_BUCKET = MAX_ENI(64) * NUM_BUCKETS_PER_ENI(4096)
#define MAX_METER_BUCKETS 262144
#ifdef TARGET_BMV2_V1MODEL
@SaiCounter[name="outbound", action_names="meter_bucket_action", attr_type="counter_attr"]
counter(MAX_METER_BUCKETS, CounterType.bytes) meter_bucket_outbound;
@SaiCounter[name="inbound", action_names="meter_bucket_action", attr_type="counter_attr"]
counter(MAX_METER_BUCKETS, CounterType.bytes) meter_bucket_inbound;
#endif // TARGET_BMV2_V1MODEL
DEFINE_BYTE_COUNTER(meter_bucket_outbound, MAX_METER_BUCKETS, name="outbound", action_names="meter_bucket_action", attr_type="counter_attr")
DEFINE_BYTE_COUNTER(meter_bucket_inbound, MAX_METER_BUCKETS, name="inbound", action_names="meter_bucket_action", attr_type="counter_attr")
action meter_bucket_action(@SaiVal[type="sai_uint32_t", skipattr="true"] bit<32> meter_bucket_index) {
meta.meter_bucket_index = meter_bucket_index;
}
Expand Down Expand Up @@ -104,13 +100,9 @@ control metering_update_stage(

meter_bucket.apply();
if (meta.direction == dash_direction_t.OUTBOUND) {
#ifdef TARGET_BMV2_V1MODEL
meter_bucket_outbound.count(meta.meter_bucket_index);
#endif
UPDATE_COUNTER(meter_bucket_outbound, meta.meter_bucket_index);
} else if (meta.direction == dash_direction_t.INBOUND) {
#ifdef TARGET_BMV2_V1MODEL
meter_bucket_inbound.count(meta.meter_bucket_index);
#endif
UPDATE_COUNTER(meter_bucket_inbound, meta.meter_bucket_index);
}

eni_meter.apply();
Expand Down

0 comments on commit 04e8617

Please sign in to comment.