From 1fdbf2906f6baf4678703ee699fdcf3f32b9196e Mon Sep 17 00:00:00 2001 From: Romain Beurdouche Date: Fri, 14 Mar 2025 09:59:01 +0000 Subject: [PATCH] feat(NR PHY simulators): add a SIGINT handler to some PHY simulators When using T2 virtual functions, it is important to properly stop DPDK and free the device. Otherwise the virtual functions may be blocked and a restart of the admin application is necessary. Up to now, SIGINT was shutting down the PHY simulators without freeing the device. This commit adds a signal handler to handle SIGINT in a way that allow to properly free the device. This feature is first added to the PHY simulators that use the T2 which are `nr_ulsim`, `nr_dlsim`, `nr_ulschsim` and `nr_dlschsim`. --- openair1/SIMULATION/NR_PHY/dlschsim.c | 8 ++++++-- openair1/SIMULATION/NR_PHY/dlsim.c | 10 +++++++--- openair1/SIMULATION/NR_PHY/nr_dummy_functions.c | 10 ++++++++++ openair1/SIMULATION/NR_PHY/nr_unitary_defs.h | 11 +++++++++++ openair1/SIMULATION/NR_PHY/ulschsim.c | 8 ++++++-- openair1/SIMULATION/NR_PHY/ulsim.c | 10 +++++++--- 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/openair1/SIMULATION/NR_PHY/dlschsim.c b/openair1/SIMULATION/NR_PHY/dlschsim.c index 977970c9f7..14adffad9b 100644 --- a/openair1/SIMULATION/NR_PHY/dlschsim.c +++ b/openair1/SIMULATION/NR_PHY/dlschsim.c @@ -100,6 +100,10 @@ configmodule_interface_t *uniqCfg = NULL; int main(int argc, char **argv) { + stop = false; + __attribute__((unused)) struct sigaction oldaction; + sigaction(SIGINT, &sigint_action, &oldaction); + int i; double SNR, SNR_lin, snr0 = -2.0, snr1 = 2.0; double snr_step = 0.1; @@ -519,11 +523,11 @@ int main(int argc, char **argv) nr_dlsch_encoding(gNB, &msgDataTx, frame, slot, frame_parms, output, NULL, NULL, NULL, NULL, NULL, NULL, NULL); } - for (SNR = snr0; SNR < snr1; SNR += snr_step) { + for (SNR = snr0; SNR < snr1 && !stop; SNR += snr_step) { n_errors = 0; n_false_positive = 0; - for (trial = 0; trial < n_trials; trial++) { + for (trial = 0; trial < n_trials && !stop; trial++) { for (i = 0; i < available_bits; i++) { #ifdef DEBUG_CODER if ((i&0xf)==0) diff --git a/openair1/SIMULATION/NR_PHY/dlsim.c b/openair1/SIMULATION/NR_PHY/dlsim.c index 76c8881945..95c32bdb94 100644 --- a/openair1/SIMULATION/NR_PHY/dlsim.c +++ b/openair1/SIMULATION/NR_PHY/dlsim.c @@ -266,6 +266,10 @@ int NB_UE_INST = 1; configmodule_interface_t *uniqCfg = NULL; int main(int argc, char **argv) { + stop = false; + __attribute__((unused)) struct sigaction oldaction; + sigaction(SIGINT, &sigint_action, &oldaction); + FILE *csv_file = NULL; char *filename_csv = NULL; setbuf(stdout, NULL); @@ -955,7 +959,7 @@ printf("%d\n", slot); fprintf(csv_file,"avg_round,eff_rate,eff_throughput,TBS\n"); } //--------------- - for (SNR = snr0; SNR < snr1; SNR += .2) { + for (SNR = snr0; SNR < snr1 && !stop; SNR += .2) { varArray_t *table_tx=initVarArray(1000,sizeof(double)); reset_meas(&gNB->phy_proc_tx); @@ -985,7 +989,7 @@ printf("%d\n", slot); n_false_positive = 0; if (n_trials== 1) num_rounds = 1; - for (trial = 0; trial < n_trials; trial++) { + for (trial = 0; trial < n_trials && !stop; trial++) { errors_bit = 0; //multipath channel @@ -1016,7 +1020,7 @@ printf("%d\n", slot); memset(Sched_INFO, 0, sizeof(*Sched_INFO)); Sched_INFO->sched_response_id = -1; - while (round < num_rounds && !UE_harq_process->decodeResult) { + while (round < num_rounds && !UE_harq_process->decodeResult && !stop) { round_trials[round]++; clear_nr_nfapi_information(RC.nrmac[0], 0, frame, slot, &Sched_INFO->DL_req, &Sched_INFO->TX_req, &Sched_INFO->UL_dci_req); diff --git a/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c b/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c index 973aa60930..355c6ad280 100644 --- a/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c +++ b/openair1/SIMULATION/NR_PHY/nr_dummy_functions.c @@ -5,6 +5,16 @@ #include "openair1/PHY/defs_nr_UE.h" #include "position_interface.h" +// Define signal handler to attempt graceful termination +bool stop = false; + +void sigint_handler(int arg) { + static const char msg[] = "\nCaught SIGINT, shutting down\nPress again CTRL+C to force shutdown\n"; + __attribute__((unused)) int unused = write(STDOUT_FILENO, msg, sizeof(msg) - 1); + + stop = true; +} + int oai_nfapi_dl_tti_req(nfapi_nr_dl_tti_request_t *dl_config_req) { return (0); } int oai_nfapi_tx_data_req(nfapi_nr_tx_data_request_t *tx_data_req) { return (0); } int oai_nfapi_ul_dci_req(nfapi_nr_ul_dci_request_t *ul_dci_req) { return (0); } diff --git a/openair1/SIMULATION/NR_PHY/nr_unitary_defs.h b/openair1/SIMULATION/NR_PHY/nr_unitary_defs.h index 504b6a8ed5..38e8df8425 100644 --- a/openair1/SIMULATION/NR_PHY/nr_unitary_defs.h +++ b/openair1/SIMULATION/NR_PHY/nr_unitary_defs.h @@ -36,6 +36,17 @@ #include "NR_ServingCellConfigCommon.h" #include "NR_ServingCellConfig.h" +// Define signal handler to attempt graceful termination +extern bool stop; + +void sigint_handler(int arg); + +const struct sigaction sigint_action = { + .sa_handler = sigint_handler, + // restore handler to default upon entry to the signal handler + .sa_flags = SA_RESETHAND +}; + int oai_exit=0; void exit_function(const char* file, const char* function, const int line, const char *s, const int assert) { diff --git a/openair1/SIMULATION/NR_PHY/ulschsim.c b/openair1/SIMULATION/NR_PHY/ulschsim.c index fcc986e92c..3027ceff58 100644 --- a/openair1/SIMULATION/NR_PHY/ulschsim.c +++ b/openair1/SIMULATION/NR_PHY/ulschsim.c @@ -99,6 +99,10 @@ nrUE_params_t *get_nrUE_params(void) { configmodule_interface_t *uniqCfg = NULL; int main(int argc, char **argv) { + stop = false; + __attribute__((unused)) struct sigaction oldaction; + sigaction(SIGINT, &sigint_action, &oldaction); + int i; double SNR, snr0 = -2.0, snr1 = 2.0, SNR_lin; double snr_step = 0.1; @@ -521,11 +525,11 @@ int main(int argc, char **argv) /////////// //////////////////////////////////////////////////////////////////// - for (SNR = snr0; SNR < snr1; SNR += snr_step) { + for (SNR = snr0; SNR < snr1 && !stop; SNR += snr_step) { n_errors = 0; n_false_positive = 0; - for (trial = 0; trial < n_trials; trial++) { + for (trial = 0; trial < n_trials && !stop; trial++) { errors_bit_uncoded = 0; diff --git a/openair1/SIMULATION/NR_PHY/ulsim.c b/openair1/SIMULATION/NR_PHY/ulsim.c index 4013951cab..051f452ef1 100644 --- a/openair1/SIMULATION/NR_PHY/ulsim.c +++ b/openair1/SIMULATION/NR_PHY/ulsim.c @@ -166,6 +166,10 @@ int NB_UE_INST = 1; configmodule_interface_t *uniqCfg = NULL; int main(int argc, char *argv[]) { + stop = false; + __attribute__((unused)) struct sigaction oldaction; + sigaction(SIGINT, &sigint_action, &oldaction); + FILE *csv_file = NULL; char *filename_csv = NULL; int i; @@ -973,7 +977,7 @@ int main(int argc, char *argv[]) //--------------- int ret = 1; int srs_ret = do_SRS; - for (SNR = snr0; SNR <= snr1; SNR += snr_step) { + for (SNR = snr0; SNR <= snr1 && !stop; SNR += snr_step) { varArray_t *table_rx=initVarArray(1000,sizeof(double)); int error_flag = 0; @@ -1012,13 +1016,13 @@ int main(int argc, char *argv[]) int64_t sum_srs_snr = 0; int srs_snr_count = 0; - for (trial = 0; trial < n_trials; trial++) { + for (trial = 0; trial < n_trials && !stop; trial++) { uint8_t round = 0; crc_status = 1; errors_decoding = 0; - while (round < max_rounds && crc_status) { + while (round < max_rounds && crc_status && !stop) { round_trials[round]++; rv_index = nr_get_rv(round % 4);