NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
pcc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 
31 #include <doca_argp.h>
32 #include <doca_dev.h>
33 #include <doca_log.h>
34 #include <doca_pcc.h>
35 
36 #include "pcc_core.h"
37 
38 static const char *status_str[DOCA_PCC_PS_ERROR + 1] = {"Active", "Standby", "Deactivated", "Error"};
39 static bool host_stop;
41 static bool got_debug_sig;
42 
43 /*
44  * Signal sigusr1 handler
45  *
46  * @signum [in]: signal number
47  */
48 static void siguser1_handler(int signum)
49 {
50  if (signum == SIGUSR1) {
51  got_debug_sig = true;
52  }
53 }
54 
55 /*
56  * Signal sigint handler
57  *
58  * @dummy [in]: Dummy parameter because this handler must accept parameter of type int
59  */
60 static void sigint_handler(int dummy)
61 {
62  (void)dummy;
63  host_stop = true;
64  signal(SIGINT, SIG_DFL);
65 }
66 
67 /*
68  * Application main function
69  *
70  * @argc [in]: command line arguments size
71  * @argv [in]: array of command line arguments
72  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
73  */
74 int main(int argc, char **argv)
75 {
76  struct pcc_config cfg = {0};
77  struct pcc_resources resources = {0};
78  doca_pcc_process_state_t process_status;
79  doca_error_t result, tmp_result;
80  int exit_status = EXIT_FAILURE;
81  bool enable_debug = false;
82  struct doca_log_backend *sdk_log;
83 
84  /* Set the default configuration values (Example values) */
85  cfg.wait_time = -1;
86  cfg.role = PCC_ROLE_RP;
88  memcpy(cfg.threads_list, default_pcc_rp_threads_list, sizeof(default_pcc_rp_threads_list));
90  cfg.probe_packet_format = PCC_DEV_PROBE_PACKET_CCMAD;
91  cfg.remote_sw_handler = false;
94  cfg.gns_ignore_value = IFA2_GNS_IGNORE_DEFAULT_VALUE;
95  cfg.gns_ignore_mask = IFA2_GNS_IGNORE_DEFAULT_MASK;
96  strcpy(cfg.coredump_file, PCC_COREDUMP_FILE_DEFAULT_PATH);
98 
99  /* Add SIGINT signal handler for graceful exit */
100  if (signal(SIGINT, sigint_handler) == SIG_ERR) {
101  PRINT_ERROR("Error: SIGINT error\n");
103  }
104  /* Add SIGUSR1 signal handler for printing debug info */
105  if (signal(SIGUSR1, siguser1_handler) == SIG_ERR) {
106  PRINT_ERROR("Error: SIGUSR1 error\n");
108  }
109 
110  /* Register a logger backend */
112  if (result != DOCA_SUCCESS)
113  return EXIT_FAILURE;
114 
115  /* Register a logger backend for internal SDK errors and warnings */
116  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
117  if (result != DOCA_SUCCESS)
118  return EXIT_FAILURE;
120  if (result != DOCA_SUCCESS)
121  return EXIT_FAILURE;
122 
123  /* Initialize argparser */
125  if (result != DOCA_SUCCESS) {
126  PRINT_ERROR("Error: Failed to init ARGP resources: %s\n", doca_error_get_descr(result));
127  return EXIT_FAILURE;
128  }
129 
130  /* Register DOCA PCC application params */
132  if (result != DOCA_SUCCESS) {
133  PRINT_ERROR("Error: Failed to register parameters: %s\n", doca_error_get_descr(result));
134  goto argp_cleanup;
135  }
136 
137  /* Start argparser */
138  result = doca_argp_start(argc, argv);
139  if (result != DOCA_SUCCESS) {
140  PRINT_ERROR("Error: Failed to parse input: %s\n", doca_error_get_descr(result));
141  goto argp_cleanup;
142  }
143 
144  /* Get the log level */
146  if (result != DOCA_SUCCESS) {
147  PRINT_ERROR("Error: Failed to get log level: %s\n", doca_error_get_descr(result));
148  goto argp_cleanup;
149  }
150 
151  /* Initialize DOCA PCC application resources */
153  if (result != DOCA_SUCCESS) {
154  PRINT_ERROR("Error: Failed to initialize PCC resources: %s\n", doca_error_get_descr(result));
155  goto argp_cleanup;
156  }
157 
158  PRINT_INFO("Info: Welcome to DOCA Programmable Congestion Control (PCC) application\n");
159  PRINT_INFO("Info: Starting DOCA PCC\n");
160 
161  /* Start DOCA PCC */
162  result = doca_pcc_start(resources.doca_pcc);
163  if (result != DOCA_SUCCESS) {
164  PRINT_ERROR("Error: Failed to start PCC\n");
165  goto destroy_pcc;
166  }
167 
168  /* Send request to device */
170  if (result != DOCA_SUCCESS) {
171  PRINT_ERROR("Error: Failed to send mailbox request\n");
172  goto destroy_pcc;
173  }
174 
175  host_stop = false;
176  PRINT_INFO("Info: Press ctrl + C to exit\n");
177  while (!host_stop) {
178  if (got_debug_sig) {
179  if (enable_debug == false) {
180  enable_debug = true;
181  result = doca_pcc_enable_debug(resources.doca_pcc, enable_debug);
182  if (result != DOCA_SUCCESS) {
183  PRINT_ERROR("Error: failed to enable debug\n");
184  }
185  } else {
187  if (result != DOCA_SUCCESS) {
188  PRINT_ERROR("Error: failed to dump debug\n");
189  }
190  }
191  got_debug_sig = 0;
192  }
193  result = doca_pcc_get_process_state(resources.doca_pcc, &process_status);
194  if (result != DOCA_SUCCESS) {
195  PRINT_ERROR("Error: Failed to query PCC\n");
196  goto destroy_pcc;
197  }
198 
199  PRINT_INFO("Info: PCC host status %s\n", status_str[process_status]);
200 
201  if (process_status == DOCA_PCC_PS_DEACTIVATED || process_status == DOCA_PCC_PS_ERROR)
202  break;
203 
204  PRINT_INFO("Info: Waiting on DOCA PCC\n");
205  result = doca_pcc_wait(resources.doca_pcc, cfg.wait_time);
206  if (result != DOCA_SUCCESS) {
207  PRINT_ERROR("Error: Failed to wait PCC\n");
208  goto destroy_pcc;
209  }
210  }
211 
212  PRINT_INFO("Info: Finished waiting on DOCA PCC\n");
213 
214  exit_status = EXIT_SUCCESS;
215 
216 destroy_pcc:
217  tmp_result = pcc_destroy(&resources);
218  if (tmp_result != DOCA_SUCCESS) {
219  PRINT_ERROR("Error: Failed to destroy DOCA PCC application resources: %s\n",
220  doca_error_get_descr(tmp_result));
221  DOCA_ERROR_PROPAGATE(result, tmp_result);
222  }
223 argp_cleanup:
224  tmp_result = doca_argp_destroy();
225  if (tmp_result != DOCA_SUCCESS) {
226  PRINT_ERROR("Error: Failed to destroy ARGP: %s\n", doca_error_get_descr(tmp_result));
227  DOCA_ERROR_PROPAGATE(result, tmp_result);
228  }
229  return exit_status;
230 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
struct rdma_resources resources
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL doca_error_t doca_argp_get_log_level(int *log_level)
Get the log level the user inserted it.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
#define DOCA_ERROR_PROPAGATE(r, t)
Save the first encountered doca_error_t.
Definition: doca_error.h:83
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_OPERATING_SYSTEM
Definition: doca_error.h:58
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_ERROR
Definition: doca_log.h:46
DOCA_EXPERIMENTAL doca_error_t doca_pcc_dump_debug(const struct doca_pcc *pcc)
Dump device side debug info.
DOCA_STABLE doca_error_t doca_pcc_wait(struct doca_pcc *pcc, int wait_time)
Wait on events or timeout from device for given time in seconds.
doca_pcc_process_state_t
Process states.
Definition: doca_pcc.h:766
DOCA_EXPERIMENTAL doca_error_t doca_pcc_enable_debug(const struct doca_pcc *pcc, bool enable)
Enable or disable device side debug.
DOCA_STABLE doca_error_t doca_pcc_start(struct doca_pcc *pcc)
Start a PCC context Register the pcc process in the NIC hw.
DOCA_STABLE doca_error_t doca_pcc_get_process_state(const struct doca_pcc *pcc, doca_pcc_process_state_t *process_state)
Return the state of the process.
@ DOCA_PCC_PS_DEACTIVATED
Definition: doca_pcc.h:769
@ DOCA_PCC_PS_ERROR
Definition: doca_pcc.h:770
const struct ip_frag_config * cfg
Definition: ip_frag_dp.c:0
static bool host_stop
Definition: pcc.c:39
static const char * status_str[DOCA_PCC_PS_ERROR+1]
Definition: pcc.c:38
static bool got_debug_sig
Definition: pcc.c:41
int main(int argc, char **argv)
Definition: pcc.c:74
static void sigint_handler(int dummy)
Definition: pcc.c:60
int log_level
Definition: pcc.c:40
static void siguser1_handler(int signum)
Definition: pcc.c:48
const uint32_t default_pcc_rp_threads_list[PCC_RP_THREADS_NUM_DEFAULT_VALUE]
Definition: pcc_core.c:57
doca_error_t pcc_init(struct pcc_config *cfg, struct pcc_resources *resources)
Definition: pcc_core.c:246
doca_error_t register_pcc_params(void)
Definition: pcc_core.c:898
doca_error_t pcc_mailbox_send(struct pcc_config *cfg, struct pcc_resources *resources)
Definition: pcc_core.c:491
doca_error_t pcc_destroy(struct pcc_resources *resources)
Definition: pcc_core.c:525
@ PCC_ROLE_RP
Definition: pcc_core.h:125
#define PCC_RP_THREADS_NUM_DEFAULT_VALUE
Definition: pcc_core.h:33
#define LOG_LEVEL_INFO
Definition: pcc_core.h:51
#define IFA2_HOP_LIMIT_DEFAULT_VALUE
Definition: pcc_core.h:37
#define PCC_COREDUMP_FILE_DEFAULT_PATH
Definition: pcc_core.h:41
#define IFA2_GNS_DEFAULT_VALUE
Definition: pcc_core.h:38
@ PCC_DEV_PROBE_PACKET_CCMAD
Definition: pcc_core.h:116
#define IFA2_GNS_IGNORE_DEFAULT_VALUE
Definition: pcc_core.h:39
struct doca_pcc_app * pcc_rp_rtt_template_app
#define PRINT_ERROR(...)
Definition: pcc_core.h:68
#define PRINT_INFO(...)
Definition: pcc_core.h:80
#define IFA2_GNS_IGNORE_DEFAULT_MASK
Definition: pcc_core.h:40