Debian Codegraph @

src/config.h

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (C) 2020-2025 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6
7 #ifndef _CONFIG_H_
8 #define _CONFIG_H_
9
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <time.h>
16
17 #include <linux/if_ether.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20
21 #include "app_config.h"
22
23 #include "security.h"
24 #include "stat.h"
25
26 struct traffic_class_config {
27 /* General */
28 bool enabled;
29 bool rx_mirror_enabled;
30 char *name;
31 size_t name_length;
32
33 /* Network settings */
34 bool xdp_enabled;
35 bool xdp_skb_mode;
36 bool xdp_zc_mode;
37 bool xdp_wakeup_mode;
38 bool xdp_busy_poll_mode;
39 bool tx_time_enabled;
40 bool tx_hwtstamp_enabled;
41 bool ignore_rx_errors;
42 uint64_t tx_time_offset_ns;
43 uint64_t burst_period_ns;
44
45 /* Traffic class settings */
46 unsigned int ether_type;
47 int vid;
48 int pcp;
49 size_t num_frames_per_cycle;
50 char *payload_pattern;
51 size_t payload_pattern_length;
52 size_t frame_length;
53 int rx_queue;
54 int tx_queue;
55
56 /* Layer 2/3 settings */
57 char interface[IF_NAMESIZE];
58 unsigned char l2_destination[ETH_ALEN];
59 char *l3_port;
60 size_t l3_port_length;
61 char *l3_destination;
62 size_t l3_destination_length;
63 char *l3_source;
64 size_t l3_source_length;
65
66 /* Security settings */
67 enum security_mode security_mode;
68 enum security_algorithm security_algorithm;
69 char *security_key;
70 size_t security_key_length;
71 char *security_iv_prefix;
72 size_t security_iv_prefix_length;
73
74 /* Operating system settings */
75 int socket_priority;
76 int tx_thread_priority;
77 int rx_thread_priority;
78 int tx_thread_cpu;
79 int rx_thread_cpu;
80 int workload_thread_cpu;
81 int workload_thread_priority;
82
83 /* Workload settings */
84 bool rx_workload_enabled;
85 bool rx_workload_prewarm;
86 int rx_workload_skip_count;
87 char *workload_file;
88 size_t workload_file_length;
89 char *workload_function;
90 size_t workload_function_length;
91 char *workload_arguments;
92 size_t workload_arguments_length;
93 char *workload_setup_function;
94 size_t workload_setup_function_length;
95 char *workload_setup_arguments;
96 size_t workload_setup_arguments_length;
97 };
98
99 struct application_config {
100 /* Application scheduling configuration */
101 clockid_t application_clock_id;
102 uint64_t application_base_cycle_time_ns;
103 uint64_t application_base_start_time_ns;
104 uint64_t application_base_start_offset_ns;
105 uint64_t application_tx_base_offset_ns;
106 uint64_t application_rx_base_offset_ns;
107 char *application_xdp_program;
108 size_t application_xdp_program_length;
109 /* Traffic class configurations */
110 struct traffic_class_config classes[NUM_FRAME_TYPES];
111 /* Logging */
112 int log_thread_priority;
113 int log_thread_cpu;
114 char *log_file;
115 size_t log_file_length;
116 char *log_level;
117 size_t log_level_length;
118 /* Debug */
119 bool debug_stop_trace_on_outlier;
120 bool debug_stop_trace_on_error;
121 bool debug_monitor_mode;
122 unsigned char debug_monitor_destination[ETH_ALEN];
123 /* Statistics */
124 bool stats_histogram_enabled;
125 uint64_t stats_histogram_minimum_ns;
126 uint64_t stats_histogram_maximum_ns;
127 char *stats_histogram_file;
128 size_t stats_histogram_file_length;
129 uint64_t stats_collection_interval_ns;
130 /* Log through MQTT */
131 bool log_mqtt;
132 int log_mqtt_thread_priority;
133 int log_mqtt_thread_cpu;
134 size_t log_mqtt_broker_ip_length;
135 char *log_mqtt_broker_ip;
136 int log_mqtt_broker_port;
137 int log_mqtt_keep_alive_secs;
138 size_t log_mqtt_measurement_name_length;
139 char *log_mqtt_measurement_name;
140 /* Log through JSON/UDP */
141 bool log_json;
142 int log_json_thread_priority;
143 int log_json_thread_cpu;
144 size_t log_json_host_length;
145 char *log_json_host;
146 size_t log_json_port_length;
147 char *log_json_port;
148 size_t log_json_measurement_name_length;
149 char *log_json_measurement_name;
150 };
151
152 extern struct application_config app_config;
153
154 int config_read_from_file(const char *config_file);
155 int config_set_defaults(bool mirror_enabled);
156 void config_print_values(void);
157 bool config_sanity_check(void);
158 void config_free(void);
159
160 #define CONFIG_STORE_BOOL_PARAM_CLASS(name, var) \
161 do { \
162 if (!strcmp(key, #name)) { \
163 enum stat_frame_type type = config_opt_to_type(#name); \
164 bool result; \
165 \
166 if (config_parse_bool(value, &result)) { \
167 ret = -EINVAL; \
168 fprintf(stderr, "The value for " #name " is invalid!\n"); \
169 goto err_parse; \
170 } else { \
171 app_config.classes[type].var = result; \
172 } \
173 } \
174 } while (0)
175
176 #define CONFIG_STORE_BOOL_PARAM(name, var) \
177 do { \
178 if (!strcmp(key, #name)) { \
179 bool result; \
180 \
181 if (config_parse_bool(value, &result)) { \
182 ret = -EINVAL; \
183 fprintf(stderr, "The value for " #name " is invalid!\n"); \
184 goto err_parse; \
185 } else { \
186 app_config.var = result; \
187 } \
188 } \
189 } while (0)
190
191 #define CONFIG_STORE_INT_PARAM_CLASS(name, var) \
192 do { \
193 if (!strcmp(key, #name)) { \
194 enum stat_frame_type type = config_opt_to_type(#name); \
195 long result; \
196 \
197 if (config_parse_int(value, &result)) { \
198 ret = -ERANGE; \
199 fprintf(stderr, "The value for " #name " is invalid!\n"); \
200 goto err_parse; \
201 } else { \
202 app_config.classes[type].var = result; \
203 } \
204 } \
205 } while (0)
206
207 #define CONFIG_STORE_INT_PARAM(name, var) \
208 do { \
209 if (!strcmp(key, #name)) { \
210 long result; \
211 \
212 if (config_parse_int(value, &result)) { \
213 ret = -ERANGE; \
214 fprintf(stderr, "The value for " #name " is invalid!\n"); \
215 goto err_parse; \
216 } else { \
217 app_config.var = result; \
218 } \
219 } \
220 } while (0)
221
222 #define CONFIG_STORE_ULONG_PARAM_CLASS(name, var) \
223 do { \
224 if (!strcmp(key, #name)) { \
225 enum stat_frame_type type = config_opt_to_type(#name); \
226 unsigned long long result; \
227 \
228 if (config_parse_ulong(value, &result)) { \
229 ret = -ERANGE; \
230 fprintf(stderr, "The value for " #name " is invalid!\n"); \
231 goto err_parse; \
232 } else { \
233 app_config.classes[type].var = result; \
234 } \
235 } \
236 } while (0)
237
238 #define CONFIG_STORE_TIME_PARAM_CLASS(name, var) \
239 do { \
240 if (!strcmp(key, #name)) { \
241 enum stat_frame_type type = config_opt_to_type(#name); \
242 \
243 if (config_parse_time(value, &app_config.classes[type].var)) { \
244 ret = -ERANGE; \
245 fprintf(stderr, "The value for " #name " is invalid!\n"); \
246 goto err_parse; \
247 } \
248 } \
249 } while (0)
250
251 #define CONFIG_STORE_TIME_PARAM(name, var) \
252 do { \
253 if (!strcmp(key, #name)) { \
254 if (config_parse_time(value, &app_config.var)) { \
255 ret = -ERANGE; \
256 fprintf(stderr, "The value for " #name " is invalid!\n"); \
257 goto err_parse; \
258 } \
259 } \
260 } while (0)
261
262 #define CONFIG_STORE_STRING_PARAM_CLASS(name, var) \
263 do { \
264 if (!strcmp(key, #name)) { \
265 enum stat_frame_type type = config_opt_to_type(#name); \
266 \
267 /* config_set_defaults() may have set a default value. */ \
268 free(app_config.classes[type].var); \
269 app_config.classes[type].var = strdup(value); \
270 if (!app_config.classes[type].var) { \
271 ret = -ENOMEM; \
272 fprintf(stderr, "strdup() for " #name " failed!\n"); \
273 goto err_parse; \
274 } \
275 app_config.classes[type].var##_length = strlen(value); \
276 } \
277 } while (0)
278
279 #define CONFIG_STORE_STRING_PARAM(name, var) \
280 do { \
281 if (!strcmp(key, #name)) { \
282 /* config_set_defaults() may have set a default value. */ \
283 free(app_config.var); \
284 app_config.var = strdup(value); \
285 if (!app_config.var) { \
286 ret = -ENOMEM; \
287 fprintf(stderr, "strdup() for " #name " failed!\n"); \
288 goto err_parse; \
289 } \
290 app_config.var##_length = strlen(value); \
291 } \
292 } while (0)
293
294 #define CONFIG_STORE_INTERFACE_PARAM_CLASS(name, var) \
295 do { \
296 if (!strcmp(key, #name)) { \
297 enum stat_frame_type type = config_opt_to_type(#name); \
298 \
299 strncpy(app_config.classes[type].var, value, \
300 sizeof(app_config.classes[type].var) - 1); \
301 } \
302 } while (0)
303
304 #define CONFIG_STORE_MAC_PARAM_CLASS(name, var) \
305 do { \
306 if (!strcmp(key, #name)) { \
307 enum stat_frame_type type = config_opt_to_type(#name); \
308 unsigned int tmp[ETH_ALEN]; \
309 int i; \
310 \
311 ret = sscanf(value, "%x:%x:%x:%x:%x:%x", &tmp[0], &tmp[1], &tmp[2], \
312 &tmp[3], &tmp[4], &tmp[5]); \
313 \
314 if (ret != ETH_ALEN) { \
315 fprintf(stderr, "Failed to parse MAC Address!\n"); \
316 ret = -EINVAL; \
317 goto err_parse; \
318 } \
319 \
320 for (i = 0; i < ETH_ALEN; ++i) \
321 app_config.classes[type].var[i] = (unsigned char)tmp[i]; \
322 } \
323 } while (0)
324
325 #define CONFIG_STORE_MAC_PARAM(name, var) \
326 do { \
327 if (!strcmp(key, #name)) { \
328 unsigned int tmp[ETH_ALEN]; \
329 int i; \
330 \
331 ret = sscanf(value, "%x:%x:%x:%x:%x:%x", &tmp[0], &tmp[1], &tmp[2], \
332 &tmp[3], &tmp[4], &tmp[5]); \
333 \
334 if (ret != ETH_ALEN) { \
335 fprintf(stderr, "Failed to parse MAC Address!\n"); \
336 ret = -EINVAL; \
337 goto err_parse; \
338 } \
339 \
340 for (i = 0; i < ETH_ALEN; ++i) \
341 app_config.var[i] = (unsigned char)tmp[i]; \
342 } \
343 } while (0)
344
345 #define CONFIG_STORE_CLOCKID_PARAM(name, var) \
346 do { \
347 if (!strcmp(key, #name)) { \
348 if (strcmp(value, "CLOCK_TAI") && strcmp(value, "CLOCK_MONOTONIC")) { \
349 fprintf(stderr, "Invalid clockid specified!\n"); \
350 goto err_parse; \
351 } \
352 \
353 if (!strcmp(value, "CLOCK_TAI")) \
354 app_config.var = CLOCK_TAI; \
355 if (!strcmp(value, "CLOCK_MONOTONIC")) \
356 app_config.var = CLOCK_MONOTONIC; \
357 } \
358 } while (0)
359
360 #define CONFIG_STORE_ETHER_TYPE_CLASS(name, var) \
361 do { \
362 if (!strcmp(key, #name)) { \
363 enum stat_frame_type type = config_opt_to_type(#name); \
364 \
365 app_config.classes[type].var = strtoul(value, &endptr, 16); \
366 if (errno != 0 || endptr == value || *endptr != '\0') { \
367 ret = -ERANGE; \
368 fprintf(stderr, "The value for " #name " is invalid!\n"); \
369 goto err_parse; \
370 } \
371 } \
372 } while (0)
373
374 #define CONFIG_STORE_SECURITY_MODE_PARAM_CLASS(name, var) \
375 do { \
376 if (!strcmp(key, #name)) { \
377 enum stat_frame_type type = config_opt_to_type(#name); \
378 \
379 if (strcasecmp(value, "none") && strcasecmp(value, "ao") && \
380 strcasecmp(value, "ae")) { \
381 fprintf(stderr, "Invalid security mode specified!\n"); \
382 goto err_parse; \
383 } \
384 \
385 if (!strcasecmp(value, "none")) \
386 app_config.classes[type].var = SECURITY_MODE_NONE; \
387 if (!strcasecmp(value, "ao")) \
388 app_config.classes[type].var = SECURITY_MODE_AO; \
389 if (!strcasecmp(value, "ae")) \
390 app_config.classes[type].var = SECURITY_MODE_AE; \
391 } \
392 } while (0)
393
394 #define CONFIG_STORE_SECURITY_ALGORITHM_PARAM_CLASS(name, var) \
395 do { \
396 if (!strcmp(key, #name)) { \
397 enum stat_frame_type type = config_opt_to_type(#name); \
398 \
399 if (strcasecmp(value, "aes256-gcm") && strcasecmp(value, "aes128-gcm") && \
400 strcasecmp(value, "chacha20-poly1305")) { \
401 fprintf(stderr, "Invalid security algorithm specified!\n"); \
402 goto err_parse; \
403 } \
404 if (!strcasecmp(value, "aes256-gcm")) \
405 app_config.classes[type].var = SECURITY_ALGORITHM_AES256_GCM; \
406 if (!strcasecmp(value, "aes128-gcm")) \
407 app_config.classes[type].var = SECURITY_ALGORITHM_AES128_GCM; \
408 if (!strcasecmp(value, "chacha20-poly1305")) \
409 app_config.classes[type].var = \
410 SECURITY_ALGORITHM_CHACHA20_POLY1305; \
411 } \
412 } while (0)
413
414 bool config_is_traffic_class_active(const char *traffic_class);
415
416 static inline bool config_have_busy_poll(void)
417 {
418 #if defined(HAVE_SO_BUSY_POLL) && defined(HAVE_SO_PREFER_BUSY_POLL) && \
419 defined(HAVE_SO_BUSY_POLL_BUDGET)
420 return true;
421 #else
422 return false;
423 #endif
424 }
425
426 static inline bool config_have_mosquitto(void)
427 {
428 #if defined(WITH_MQTT)
429 return true;
430 #else
431 return false;
432 #endif
433 }
434
435 static inline bool config_have_xdp_tx_time(void)
436 {
437 #ifdef HAVE_XDP_TX_TIME
438 return true;
439 #else
440 return false;
441 #endif
442 }
443
444 static inline bool config_have_rx_timestamp(void)
445 {
446 #ifdef RX_TIMESTAMP
447 return true;
448 #else
449 return false;
450 #endif
451 }
452
453 static inline bool config_have_tx_timestamp(void)
454 {
455 #ifdef TX_TIMESTAMP
456 return true;
457 #else
458 return false;
459 #endif
460 }
461
462 #endif /* _CONFIG_H_ */