From ac53fef21cad5adfb1efc51a24e20b993047547b Mon Sep 17 00:00:00 2001 From: Kirill Faizullin Date: Fri, 24 Apr 2026 15:54:37 +0300 Subject: [PATCH 1/3] feat(nic-stats): added NIC statistics in shared memory with FFI and RPC --- api/counter.h | 6 ++ controlplane/ffi/shm.go | 13 +++ controlplane/ynpb/counters.proto | 4 + dataplane/dataplane.c | 46 +++++----- dataplane/worker.c | 151 ++++++++++++++++++++----------- lib/controlplane/agent/agent.c | 68 ++++++++++++++ lib/controlplane/config/zone.h | 7 ++ lib/dataplane/config/zone.h | 15 +++ 8 files changed, 233 insertions(+), 77 deletions(-) diff --git a/api/counter.h b/api/counter.h index debcaace1..f4a0f3616 100644 --- a/api/counter.h +++ b/api/counter.h @@ -49,6 +49,12 @@ yanet_get_chain_counters( const char *chain_name ); +struct counter_handle_list * +yanet_get_nic_counters( + struct dp_config *dp_config, + const char *device_name +); + // Get module counters, optionally filtered by name. struct counter_handle_list * yanet_get_module_counters( diff --git a/controlplane/ffi/shm.go b/controlplane/ffi/shm.go index 15a3dc799..a5b73d32a 100644 --- a/controlplane/ffi/shm.go +++ b/controlplane/ffi/shm.go @@ -755,6 +755,19 @@ func (m *DPConfig) PerformanceCounters( return result, nil } +func (m *DPConfig) NicCounters(deviceName string) []CounterInfo { + cDeviceName := C.CString(deviceName) + defer C.free(unsafe.Pointer(cDeviceName)) + counters := C.yanet_get_nic_counters(m.ptr, cDeviceName) + defer C.yanet_counter_handle_list_free(counters) + + if counters == nil { + return nil + } + + return m.encodeCounters(counters) +} + type ModuleReference struct { Device string Pipeline string diff --git a/controlplane/ynpb/counters.proto b/controlplane/ynpb/counters.proto index 97c8cd365..153717201 100644 --- a/controlplane/ynpb/counters.proto +++ b/controlplane/ynpb/counters.proto @@ -11,11 +11,15 @@ service CountersService { rpc Function(FunctionCountersRequest) returns (CountersResponse) {} rpc Chain(ChainCountersRequest) returns (CountersResponse) {} rpc Module(ModuleCountersRequest) returns (CountersResponse) {} + rpc Nic(NicCounterRequest) returns (CountersResponse) {} rpc Perf(PerfCountersRequest) returns (PerfCountersResponse) {} } message DeviceCountersRequest { string device = 1; } +message NicCounterRequest { string device = 1; } + + message PipelineCountersRequest { string device = 1; string pipeline = 2; diff --git a/dataplane/dataplane.c b/dataplane/dataplane.c index 14d3b4e80..ef5c07830 100644 --- a/dataplane/dataplane.c +++ b/dataplane/dataplane.c @@ -617,11 +617,11 @@ stat_thread(void *arg) { struct rte_eth_xstat_name names[4096]; struct rte_eth_xstat xstats0[dataplane->device_count][4096]; - struct rte_eth_stats stats0[dataplane->device_count]; + // struct rte_eth_stats stats0[dataplane->device_count]; for (uint16_t idx = 0; idx < dataplane->device_count; ++idx) { - rte_eth_stats_get( - dataplane->devices[idx].port_id, &stats0[idx] - ); + // rte_eth_stats_get( + // dataplane->devices[idx].port_id, &stats0[idx] + // ); rte_eth_xstats_get( dataplane->devices[idx].port_id, xstats0[idx], 4096 ); @@ -631,25 +631,25 @@ stat_thread(void *arg) { sleep(1); for (uint16_t idx = 0; idx < dataplane->device_count; ++idx) { - struct rte_eth_stats stats1; - rte_eth_stats_get( - dataplane->devices[idx].port_id, &stats1 - ); - fprintf(log, - "dev %u ib %li ob %li ip %li op %li ie %li oe " - "%li\n", - idx, - (int64_t)(stats1.ibytes - stats0[idx].ibytes), - (int64_t)(stats1.obytes - stats0[idx].obytes), - (int64_t)(stats1.ipackets - stats0[idx].ipackets - ), - (int64_t)(stats1.opackets - stats0[idx].opackets - ), - (int64_t)(stats1.ierrors - stats0[idx].ierrors), - (int64_t)(stats1.oerrors - stats0[idx].oerrors) - ); - - memcpy(&stats0[idx], &stats1, sizeof(stats1)); + // struct rte_eth_stats stats1; + // rte_eth_stats_get( + // dataplane->devices[idx].port_id, &stats1 + // ); + // fprintf(log, + // "dev %u ib %li ob %li ip %li op %li ie %li oe " + // "%li\n", + // idx, + // (int64_t)(stats1.ibytes - stats0[idx].ibytes), + // (int64_t)(stats1.obytes - stats0[idx].obytes), + // (int64_t)(stats1.ipackets - stats0[idx].ipackets + // ), + // (int64_t)(stats1.opackets - stats0[idx].opackets + // ), + // (int64_t)(stats1.ierrors - stats0[idx].ierrors), + // (int64_t)(stats1.oerrors - stats0[idx].oerrors) + // ); + + // memcpy(&stats0[idx], &stats1, sizeof(stats1)); struct rte_eth_xstat xstats1[4096]; rte_eth_xstats_get_names( diff --git a/dataplane/worker.c b/dataplane/worker.c index 60cc8c7cf..5724cdcfd 100644 --- a/dataplane/worker.c +++ b/dataplane/worker.c @@ -55,6 +55,8 @@ #include +#define NIC_STATS_FREAQUENCY 1000 + static void worker_read(struct dataplane_worker *worker, struct packet_list *packets) { struct worker_read_ctx *ctx = &worker->read_ctx; @@ -271,6 +273,52 @@ worker_write(struct dataplane_worker *worker, struct packet_list *packets) { } } +struct nic_stat { + uint64_t ibytes; + uint64_t obytes; + uint64_t ipackets; + uint64_t opackets; + uint64_t ierrors; + uint64_t oerrors; + uint64_t rx_nombuf; +}; + +void worker_unload_nic_stats(struct dataplane_worker *worker) { + struct rte_eth_stats stats1; + struct dp_worker *dp_worker = worker->dp_worker; + + // const struct nic_stat stats0 = { + // .ibytes = *dp_worker->nic_rx_bytes, + // .obytes = *dp_worker->nic_tx_bytes, + // .ipackets = *dp_worker->nic_rx_packets, + // .opackets = *dp_worker->nic_tx_packets, + // .ierrors = *dp_worker->nic_rx_errors, + // .oerrors = *dp_worker->nic_tx_errors, + // .rx_nombuf = *dp_worker->nic_rx_nombuf + // }; + + + rte_eth_stats_get(worker->port_id, &stats1); + + // struct nic_stat diff = { + // .ibytes = stats1.ibytes - stats0.ibytes, + // .obytes = stats1.obytes - stats0.obytes, + // .ipackets = stats1.ipackets - stats0.ipackets, + // .opackets = stats1.opackets - stats0.opackets, + // .ierrors = stats1.ierrors - stats0.ierrors, + // .oerrors = stats1.oerrors - stats0.oerrors, + // .rx_nombuf = stats1.rx_nombuf - stats0.rx_nombuf + // }; + + *dp_worker->nic_rx_bytes = stats1.ibytes; + *dp_worker->nic_tx_bytes = stats1.obytes; + *dp_worker->nic_rx_packets = stats1.ipackets; + *dp_worker->nic_tx_packets = stats1.opackets; + *dp_worker->nic_rx_errors = stats1.ierrors; + *dp_worker->nic_tx_errors = stats1.oerrors; + *dp_worker->nic_rx_nombuf = stats1.rx_nombuf; +} + static void worker_loop_round(struct dataplane_worker *worker) { // Initialize current worker time @@ -312,7 +360,11 @@ worker_loop_round(struct dataplane_worker *worker) { uint64_t device_count = cp_config_gen->device_registry.registry.capacity; + uint64_t iterations = 0; + while (1) { + iterations = (iterations + 1) % NIC_STATS_FREAQUENCY; + if (iterations == 0) worker_unload_nic_stats(worker); struct packet_front schedule_input[device_count]; for (uint64_t idx = 0; idx < device_count; ++idx) @@ -406,6 +458,32 @@ worker_thread_start(void *arg) { return NULL; } +static const struct { + const char *name; + uint64_t size; + const size_t *offset; +} worker_counter_info[] = { + {"iterations", 1, (const size_t[]){offsetof(struct dp_worker, iterations)}}, + {"rx", 2, (const size_t[]){offsetof(struct dp_worker, rx_count), offsetof(struct dp_worker, rx_size)}}, + {"tx", 2, (const size_t[]){offsetof(struct dp_worker, tx_count), offsetof(struct dp_worker, tx_size)}}, + {"remote_rx", 1, (const size_t[]){offsetof(struct dp_worker, remote_rx_count)}}, + {"remote_tx", 1, (const size_t[]){offsetof(struct dp_worker, remote_tx_count)},}, + {"nic_rx", 2, (const size_t[]){offsetof(struct dp_worker, nic_rx_packets), offsetof(struct dp_worker, nic_rx_bytes)}}, + {"nic_tx", 2, (const size_t[]){offsetof(struct dp_worker, nic_tx_packets), offsetof(struct dp_worker, nic_tx_bytes)}}, + {"nic_rx_tx_errors", 2, (const size_t[]){offsetof(struct dp_worker, nic_rx_errors), offsetof(struct dp_worker, nic_tx_errors)}}, + {"nic_rx_nombuf", 1, (const size_t[]){offsetof(struct dp_worker, nic_rx_nombuf)}}, +}; + +uint64_t** +get_worker_field_ptr(struct dp_worker *worker, int info_index, int offset_index) { + return (uint64_t**)((char*)worker + worker_counter_info[info_index].offset[offset_index]); +} + +#define ARRAY_SIZE(arr) (size_t)(sizeof(arr) / sizeof((arr)[0])) + +static uint64_t +worker_counter_ids[ARRAY_SIZE(worker_counter_info)]; + int dataplane_worker_init( struct dataplane *dataplane, @@ -551,14 +629,14 @@ dataplane_worker_init( counter_registry_init( &dp_config->worker_counters, &dp_config->memory_context, 0 ); - - counter_registry_register(&dp_config->worker_counters, "iterations", 1); - - counter_registry_register(&dp_config->worker_counters, "rx", 2); - counter_registry_register(&dp_config->worker_counters, "tx", 2); - counter_registry_register(&dp_config->worker_counters, "remote_rx", 2); - - counter_registry_register(&dp_config->worker_counters, "remote_tx", 2); + + for (size_t i = 0; i < ARRAY_SIZE(worker_counter_info); ++i) { + worker_counter_ids[i] = counter_registry_register( + &dp_config->worker_counters, + worker_counter_info[i].name, + worker_counter_info[i].size + ); + } return 0; @@ -574,53 +652,18 @@ dataplane_worker_start(struct dataplane_worker *worker) { struct dp_worker *dp_worker = worker->dp_worker; struct dp_config *dp_config = worker->instance->dp_config; // FIXME: do not use hard-coded counter identifiers - dp_worker->iterations = counter_get_address( - 0, dp_worker->idx, ADDR_OF(&dp_config->worker_counter_storage) - ); - dp_worker->rx_count = - counter_get_address( - 1, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 0; - dp_worker->rx_size = counter_get_address( - 1, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 1; - - dp_worker->tx_count = - counter_get_address( - 2, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 0; - dp_worker->tx_size = counter_get_address( - 2, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 1; - - dp_worker->remote_rx_count = - counter_get_address( - 3, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 0; - - dp_worker->remote_tx_count = - counter_get_address( - 4, - dp_worker->idx, - ADDR_OF(&dp_config->worker_counter_storage) - ) + - 0; + for (size_t i = 0; i < ARRAY_SIZE(worker_counter_info); ++i) { + for (size_t j = 0; j < worker_counter_info[i].size; ++j) { + uint64_t **field_ptr = get_worker_field_ptr(dp_worker, i, j); + *field_ptr = counter_get_address( + worker_counter_ids[i], + dp_worker->idx, + ADDR_OF(&dp_config->worker_counter_storage) + ) + j; + } + } + pthread_attr_t wrk_th_attr; pthread_attr_init(&wrk_th_attr); diff --git a/lib/controlplane/agent/agent.c b/lib/controlplane/agent/agent.c index cbe065c04..329d1f2bf 100644 --- a/lib/controlplane/agent/agent.c +++ b/lib/controlplane/agent/agent.c @@ -1648,6 +1648,74 @@ yanet_get_device_counters( return list; } +struct counter_handle_list * +yanet_get_nic_counters( + struct dp_config *dp_config, + const char *device_name +) { + const char* query[] = { + "nic_rx", + "nic_tx", + "nic_rx_tx_errors", + "nic_rx_nombuf", + }; + int query_count = 4; + + struct cp_config *cp_config = ADDR_OF(&dp_config->cp_config); + cp_config_lock(cp_config); + struct cp_config_gen *cp_config_gen = + ADDR_OF(&cp_config->cp_config_gen); + + struct counter_registry *counter_registry; + struct counter_storage *counter_storage; + + struct counter_storage *cs = cp_config_gen_get_nic_counter_storage( + cp_config_gen, device_name + ); + + if (cs == NULL) { + cp_config_unlock(cp_config); + return NULL; + } + counter_storage = cs; + counter_registry = ADDR_OF(&counter_storage->registry); + + uint64_t count = counter_registry->count; + struct counter *names = ADDR_OF(&counter_registry->names); + + // FIXME: unlock is correct + cp_config_unlock(cp_config); + + struct counter_handle_list *list = (struct counter_handle_list *)malloc( + sizeof(struct counter_handle_list) + + sizeof(struct counter_handle) * count + ); + + if (list == NULL) + return NULL; + list->instance_count = + ADDR_OF(&counter_storage->allocator)->instance_count; + list->count = count; + struct counter_handle *handlers = list->counters; + + uint64_t out_idx = 0; + for (uint64_t idx = 0; idx < count; ++idx) { + if (!counter_name_matches_query( + names[idx].name, query, query_count + )) { + continue; + } + strtcpy(handlers[out_idx].name, names[idx].name, 60); + handlers[out_idx].size = names[idx].size; + handlers[out_idx].gen = names[idx].gen; + handlers[out_idx].value_handle = + counter_get_value_handle(idx, counter_storage); + out_idx++; + } + + return list; +} + struct counter_handle * yanet_get_counter(struct counter_handle_list *counters, uint64_t idx) { if (idx >= counters->count) diff --git a/lib/controlplane/config/zone.h b/lib/controlplane/config/zone.h index 379f3d54f..6fd3eb4dd 100644 --- a/lib/controlplane/config/zone.h +++ b/lib/controlplane/config/zone.h @@ -279,6 +279,13 @@ cp_config_gen_get_device_counter_storage( ); } +static inline struct counter_storage * +cp_config_gen_get_nic_counter_storage( + struct cp_config_gen *config_gen, const char *device_name +) { + return cp_config_gen_get_device_counter_storage(config_gen, device_name); +} + struct cp_module * cp_config_gen_lookup_module( struct cp_config_gen *config_gen, const char *type, const char *name diff --git a/lib/dataplane/config/zone.h b/lib/dataplane/config/zone.h index 1c6643145..a245505ae 100644 --- a/lib/dataplane/config/zone.h +++ b/lib/dataplane/config/zone.h @@ -62,6 +62,21 @@ struct dp_worker { struct rte_mempool *rx_mempool; uint8_t pad[24]; + + // trade-off: nic stats unload not often + // bcs put it in second cache line + uint64_t *nic_rx_packets; + uint64_t *nic_rx_bytes; + + uint64_t *nic_tx_packets; + uint64_t *nic_tx_bytes; + + uint64_t *nic_rx_errors; + uint64_t *nic_tx_errors; + + uint64_t *nic_rx_nombuf; + + uint8_t pad2[8]; }; struct dp_config { uint32_t instance_count; From 397110c3d4f3b9b7a0ff05d02e20e54baa174dc7 Mon Sep 17 00:00:00 2001 From: Kirill Faizullin Date: Wed, 29 Apr 2026 08:57:38 +0300 Subject: [PATCH 2/3] fix: correct names for style, logic for getting counters and added impl for rpc --- api/counter.h | 3 +- controlplane/ffi/shm.go | 6 +-- .../internal/gateway/counters_service.go | 14 ++++++ controlplane/ynpb/counters.proto | 4 +- dataplane/worker.c | 46 +++---------------- lib/controlplane/agent/agent.c | 45 ++++++++---------- 6 files changed, 46 insertions(+), 72 deletions(-) diff --git a/api/counter.h b/api/counter.h index f4a0f3616..5116781fe 100644 --- a/api/counter.h +++ b/api/counter.h @@ -51,8 +51,7 @@ yanet_get_chain_counters( struct counter_handle_list * yanet_get_nic_counters( - struct dp_config *dp_config, - const char *device_name + struct dp_config *dp_config ); // Get module counters, optionally filtered by name. diff --git a/controlplane/ffi/shm.go b/controlplane/ffi/shm.go index a5b73d32a..d8338ddaf 100644 --- a/controlplane/ffi/shm.go +++ b/controlplane/ffi/shm.go @@ -755,10 +755,8 @@ func (m *DPConfig) PerformanceCounters( return result, nil } -func (m *DPConfig) NicCounters(deviceName string) []CounterInfo { - cDeviceName := C.CString(deviceName) - defer C.free(unsafe.Pointer(cDeviceName)) - counters := C.yanet_get_nic_counters(m.ptr, cDeviceName) +func (m *DPConfig) NICCounters() []CounterInfo { + counters := C.yanet_get_nic_counters(m.ptr) defer C.yanet_counter_handle_list_free(counters) if counters == nil { diff --git a/controlplane/internal/gateway/counters_service.go b/controlplane/internal/gateway/counters_service.go index 68783d762..9cbe87f5e 100644 --- a/controlplane/internal/gateway/counters_service.go +++ b/controlplane/internal/gateway/counters_service.go @@ -129,6 +129,20 @@ func (m *CountersService) Module( return response, nil } +func (m *CountersService) NIC( + ctx context.Context, + request *ynpb.NICCounterRequest, +) (*ynpb.CountersResponse, error) { + dpConfig := m.shm.DPConfig(m.instanceID) + counterValues := dpConfig.NICCounters() + + response := &ynpb.CountersResponse{ + Counters: m.encodeCounters(counterValues), + } + + return response, nil +} + func (m *CountersService) Perf( ctx context.Context, request *ynpb.PerfCountersRequest, diff --git a/controlplane/ynpb/counters.proto b/controlplane/ynpb/counters.proto index 153717201..3c3cfa307 100644 --- a/controlplane/ynpb/counters.proto +++ b/controlplane/ynpb/counters.proto @@ -11,13 +11,13 @@ service CountersService { rpc Function(FunctionCountersRequest) returns (CountersResponse) {} rpc Chain(ChainCountersRequest) returns (CountersResponse) {} rpc Module(ModuleCountersRequest) returns (CountersResponse) {} - rpc Nic(NicCounterRequest) returns (CountersResponse) {} + rpc NIC(NICCounterRequest) returns (CountersResponse) {} rpc Perf(PerfCountersRequest) returns (PerfCountersResponse) {} } message DeviceCountersRequest { string device = 1; } -message NicCounterRequest { string device = 1; } +message NICCounterRequest { string device = 1; } message PipelineCountersRequest { diff --git a/dataplane/worker.c b/dataplane/worker.c index 5724cdcfd..6036f88b2 100644 --- a/dataplane/worker.c +++ b/dataplane/worker.c @@ -55,7 +55,7 @@ #include -#define NIC_STATS_FREAQUENCY 1000 +#define NIC_STATS_FREQUENCY 1000 static void worker_read(struct dataplane_worker *worker, struct packet_list *packets) { @@ -273,42 +273,11 @@ worker_write(struct dataplane_worker *worker, struct packet_list *packets) { } } -struct nic_stat { - uint64_t ibytes; - uint64_t obytes; - uint64_t ipackets; - uint64_t opackets; - uint64_t ierrors; - uint64_t oerrors; - uint64_t rx_nombuf; -}; - void worker_unload_nic_stats(struct dataplane_worker *worker) { struct rte_eth_stats stats1; struct dp_worker *dp_worker = worker->dp_worker; - - // const struct nic_stat stats0 = { - // .ibytes = *dp_worker->nic_rx_bytes, - // .obytes = *dp_worker->nic_tx_bytes, - // .ipackets = *dp_worker->nic_rx_packets, - // .opackets = *dp_worker->nic_tx_packets, - // .ierrors = *dp_worker->nic_rx_errors, - // .oerrors = *dp_worker->nic_tx_errors, - // .rx_nombuf = *dp_worker->nic_rx_nombuf - // }; - rte_eth_stats_get(worker->port_id, &stats1); - - // struct nic_stat diff = { - // .ibytes = stats1.ibytes - stats0.ibytes, - // .obytes = stats1.obytes - stats0.obytes, - // .ipackets = stats1.ipackets - stats0.ipackets, - // .opackets = stats1.opackets - stats0.opackets, - // .ierrors = stats1.ierrors - stats0.ierrors, - // .oerrors = stats1.oerrors - stats0.oerrors, - // .rx_nombuf = stats1.rx_nombuf - stats0.rx_nombuf - // }; *dp_worker->nic_rx_bytes = stats1.ibytes; *dp_worker->nic_tx_bytes = stats1.obytes; @@ -360,12 +329,11 @@ worker_loop_round(struct dataplane_worker *worker) { uint64_t device_count = cp_config_gen->device_registry.registry.capacity; - uint64_t iterations = 0; - while (1) { - iterations = (iterations + 1) % NIC_STATS_FREAQUENCY; - if (iterations == 0) worker_unload_nic_stats(worker); - + if (*worker->dp_worker->iterations % NIC_STATS_FREQUENCY == 0) { + worker_unload_nic_stats(worker); + } + struct packet_front schedule_input[device_count]; for (uint64_t idx = 0; idx < device_count; ++idx) packet_front_init(schedule_input + idx); @@ -467,7 +435,7 @@ static const struct { {"rx", 2, (const size_t[]){offsetof(struct dp_worker, rx_count), offsetof(struct dp_worker, rx_size)}}, {"tx", 2, (const size_t[]){offsetof(struct dp_worker, tx_count), offsetof(struct dp_worker, tx_size)}}, {"remote_rx", 1, (const size_t[]){offsetof(struct dp_worker, remote_rx_count)}}, - {"remote_tx", 1, (const size_t[]){offsetof(struct dp_worker, remote_tx_count)},}, + {"remote_tx", 1, (const size_t[]){offsetof(struct dp_worker, remote_tx_count)}}, {"nic_rx", 2, (const size_t[]){offsetof(struct dp_worker, nic_rx_packets), offsetof(struct dp_worker, nic_rx_bytes)}}, {"nic_tx", 2, (const size_t[]){offsetof(struct dp_worker, nic_tx_packets), offsetof(struct dp_worker, nic_tx_bytes)}}, {"nic_rx_tx_errors", 2, (const size_t[]){offsetof(struct dp_worker, nic_rx_errors), offsetof(struct dp_worker, nic_tx_errors)}}, @@ -475,7 +443,7 @@ static const struct { }; uint64_t** -get_worker_field_ptr(struct dp_worker *worker, int info_index, int offset_index) { +get_worker_field_ptr(struct dp_worker *worker, size_t info_index, size_t offset_index) { return (uint64_t**)((char*)worker + worker_counter_info[info_index].offset[offset_index]); } diff --git a/lib/controlplane/agent/agent.c b/lib/controlplane/agent/agent.c index 329d1f2bf..63efb92cb 100644 --- a/lib/controlplane/agent/agent.c +++ b/lib/controlplane/agent/agent.c @@ -1650,8 +1650,7 @@ yanet_get_device_counters( struct counter_handle_list * yanet_get_nic_counters( - struct dp_config *dp_config, - const char *device_name + struct dp_config *dp_config ) { const char* query[] = { "nic_rx", @@ -1659,42 +1658,38 @@ yanet_get_nic_counters( "nic_rx_tx_errors", "nic_rx_nombuf", }; - int query_count = 4; + int query_count = sizeof(query) / sizeof(query[0]); - struct cp_config *cp_config = ADDR_OF(&dp_config->cp_config); - cp_config_lock(cp_config); - struct cp_config_gen *cp_config_gen = - ADDR_OF(&cp_config->cp_config_gen); + struct counter_registry *counter_registry = &dp_config->worker_counters; + struct counter_storage *storage = + ADDR_OF(&dp_config->worker_counter_storage); - struct counter_registry *counter_registry; - struct counter_storage *counter_storage; + uint64_t count = counter_registry->count; + struct counter *names = ADDR_OF(&counter_registry->names); - struct counter_storage *cs = cp_config_gen_get_nic_counter_storage( - cp_config_gen, device_name - ); + uint64_t match_count = 0; + + for (uint64_t idx = 0; idx < count; ++idx) { + if (!counter_name_matches_query(names[idx].name, query, query_count)) { + continue; + } - if (cs == NULL) { - cp_config_unlock(cp_config); - return NULL; + match_count++; } - counter_storage = cs; - counter_registry = ADDR_OF(&counter_storage->registry); - uint64_t count = counter_registry->count; - struct counter *names = ADDR_OF(&counter_registry->names); - - // FIXME: unlock is correct - cp_config_unlock(cp_config); + if (match_count == 0) { + return NULL; + } struct counter_handle_list *list = (struct counter_handle_list *)malloc( sizeof(struct counter_handle_list) + - sizeof(struct counter_handle) * count + sizeof(struct counter_handle) * match_count ); if (list == NULL) return NULL; list->instance_count = - ADDR_OF(&counter_storage->allocator)->instance_count; + ADDR_OF(&storage->allocator)->instance_count; list->count = count; struct counter_handle *handlers = list->counters; @@ -1709,7 +1704,7 @@ yanet_get_nic_counters( handlers[out_idx].size = names[idx].size; handlers[out_idx].gen = names[idx].gen; handlers[out_idx].value_handle = - counter_get_value_handle(idx, counter_storage); + counter_get_value_handle(idx, storage); out_idx++; } From fec309159375e91ef3c16628f327d2c456860b76 Mon Sep 17 00:00:00 2001 From: Kirill Faizullin Date: Wed, 29 Apr 2026 09:29:08 +0300 Subject: [PATCH 3/3] fix: format and delete comments --- api/counter.h | 4 +--- controlplane/ynpb/counters.proto | 1 - dataplane/dataplane.c | 24 ------------------------ lib/controlplane/agent/agent.c | 25 ++++++++++++------------- lib/controlplane/config/zone.h | 7 ------- 5 files changed, 13 insertions(+), 48 deletions(-) diff --git a/api/counter.h b/api/counter.h index 5116781fe..b4406dcff 100644 --- a/api/counter.h +++ b/api/counter.h @@ -50,9 +50,7 @@ yanet_get_chain_counters( ); struct counter_handle_list * -yanet_get_nic_counters( - struct dp_config *dp_config -); +yanet_get_nic_counters(struct dp_config *dp_config); // Get module counters, optionally filtered by name. struct counter_handle_list * diff --git a/controlplane/ynpb/counters.proto b/controlplane/ynpb/counters.proto index 3c3cfa307..99bd765eb 100644 --- a/controlplane/ynpb/counters.proto +++ b/controlplane/ynpb/counters.proto @@ -19,7 +19,6 @@ message DeviceCountersRequest { string device = 1; } message NICCounterRequest { string device = 1; } - message PipelineCountersRequest { string device = 1; string pipeline = 2; diff --git a/dataplane/dataplane.c b/dataplane/dataplane.c index ef5c07830..30bda79ee 100644 --- a/dataplane/dataplane.c +++ b/dataplane/dataplane.c @@ -617,11 +617,7 @@ stat_thread(void *arg) { struct rte_eth_xstat_name names[4096]; struct rte_eth_xstat xstats0[dataplane->device_count][4096]; - // struct rte_eth_stats stats0[dataplane->device_count]; for (uint16_t idx = 0; idx < dataplane->device_count; ++idx) { - // rte_eth_stats_get( - // dataplane->devices[idx].port_id, &stats0[idx] - // ); rte_eth_xstats_get( dataplane->devices[idx].port_id, xstats0[idx], 4096 ); @@ -631,26 +627,6 @@ stat_thread(void *arg) { sleep(1); for (uint16_t idx = 0; idx < dataplane->device_count; ++idx) { - // struct rte_eth_stats stats1; - // rte_eth_stats_get( - // dataplane->devices[idx].port_id, &stats1 - // ); - // fprintf(log, - // "dev %u ib %li ob %li ip %li op %li ie %li oe " - // "%li\n", - // idx, - // (int64_t)(stats1.ibytes - stats0[idx].ibytes), - // (int64_t)(stats1.obytes - stats0[idx].obytes), - // (int64_t)(stats1.ipackets - stats0[idx].ipackets - // ), - // (int64_t)(stats1.opackets - stats0[idx].opackets - // ), - // (int64_t)(stats1.ierrors - stats0[idx].ierrors), - // (int64_t)(stats1.oerrors - stats0[idx].oerrors) - // ); - - // memcpy(&stats0[idx], &stats1, sizeof(stats1)); - struct rte_eth_xstat xstats1[4096]; rte_eth_xstats_get_names( dataplane->devices[idx].port_id, names, 4096 diff --git a/lib/controlplane/agent/agent.c b/lib/controlplane/agent/agent.c index 63efb92cb..f91a1a2f0 100644 --- a/lib/controlplane/agent/agent.c +++ b/lib/controlplane/agent/agent.c @@ -1649,15 +1649,13 @@ yanet_get_device_counters( } struct counter_handle_list * -yanet_get_nic_counters( - struct dp_config *dp_config -) { - const char* query[] = { - "nic_rx", - "nic_tx", - "nic_rx_tx_errors", - "nic_rx_nombuf", - }; +yanet_get_nic_counters(struct dp_config *dp_config) { + const char *query[] = { + "nic_rx", + "nic_tx", + "nic_rx_tx_errors", + "nic_rx_nombuf", + }; int query_count = sizeof(query) / sizeof(query[0]); struct counter_registry *counter_registry = &dp_config->worker_counters; @@ -1668,9 +1666,11 @@ yanet_get_nic_counters( struct counter *names = ADDR_OF(&counter_registry->names); uint64_t match_count = 0; - + for (uint64_t idx = 0; idx < count; ++idx) { - if (!counter_name_matches_query(names[idx].name, query, query_count)) { + if (!counter_name_matches_query( + names[idx].name, query, query_count + )) { continue; } @@ -1688,8 +1688,7 @@ yanet_get_nic_counters( if (list == NULL) return NULL; - list->instance_count = - ADDR_OF(&storage->allocator)->instance_count; + list->instance_count = ADDR_OF(&storage->allocator)->instance_count; list->count = count; struct counter_handle *handlers = list->counters; diff --git a/lib/controlplane/config/zone.h b/lib/controlplane/config/zone.h index 6fd3eb4dd..379f3d54f 100644 --- a/lib/controlplane/config/zone.h +++ b/lib/controlplane/config/zone.h @@ -279,13 +279,6 @@ cp_config_gen_get_device_counter_storage( ); } -static inline struct counter_storage * -cp_config_gen_get_nic_counter_storage( - struct cp_config_gen *config_gen, const char *device_name -) { - return cp_config_gen_get_device_counter_storage(config_gen, device_name); -} - struct cp_module * cp_config_gen_lookup_module( struct cp_config_gen *config_gen, const char *type, const char *name