| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <getopt.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #include <sys/mman.h> |
| 13 | |
| 14 | #include "app_config.h" |
| 15 | #include "config.h" |
| 16 | #include "dcp_thread.h" |
| 17 | #include "hist.h" |
| 18 | #include "layer2_thread.h" |
| 19 | #include "lldp_thread.h" |
| 20 | #include "log.h" |
| 21 | #include "log_json.h" |
| 22 | #include "log_mqtt.h" |
| 23 | #include "print.h" |
| 24 | #include "rta_thread.h" |
| 25 | #include "rtc_thread.h" |
| 26 | #include "stat.h" |
| 27 | #include "thread.h" |
| 28 | #include "tsn_thread.h" |
| 29 | #include "udp_thread.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | static struct option long_options[] = { |
| 33 | {"config", optional_argument, NULL, 'c'}, |
| 34 | {"help", no_argument, NULL, 'h'}, |
| 35 | {"version", no_argument, NULL, 'V'}, |
| 36 | {NULL}, |
| 37 | }; |
| 38 | |
| 39 | static struct log_mqtt_thread_context *log_mqtt_thread; |
| 40 | static struct log_json_thread_context *log_json_thread; |
| 41 | static struct log_thread_context *log_thread; |
| 42 | static struct thread_context *g2_threads; |
| 43 | static struct thread_context *threads; |
| 44 | |
| 45 | static void term_handler(int sig) |
| 46 | { |
| 47 | int i; |
| 48 | |
| 49 | printf("Stopping all application threads...\n"); |
| 50 | |
| 51 | print_stop = 1; |
| 52 | |
| 53 | if (log_mqtt_thread) |
| 54 | log_mqtt_thread->stop = 1; |
| 55 | |
| 56 | if (log_json_thread) |
| 57 | log_json_thread->stop = 1; |
| 58 | |
| 59 | if (log_thread) |
| 60 | log_thread->stop = 1; |
| 61 | |
| 62 | if (g2_threads) |
| 63 | g2_threads->stop = 1; |
| 64 | |
| 65 | if (threads) |
| 66 | for (i = 0; i < NUM_PN_THREAD_TYPES; i++) |
| 67 | threads[i].stop = 1; |
| 68 | } |
| 69 | |
| 70 | static void reset_stats_handler(int sig) |
| 71 | { |
| 72 | reset_stats = 1; |
| 73 | } |
| 74 | |
| 75 | static void setup_signals(void) |
| 76 | { |
| 77 | struct sigaction sa1, sa2; |
| 78 | |
| 79 | sigemptyset(&sa1.sa_mask); |
| 80 | sa1.sa_handler = term_handler; |
| 81 | sa1.sa_flags = 0; |
| 82 | |
| 83 | sigemptyset(&sa2.sa_mask); |
| 84 | sa2.sa_handler = reset_stats_handler; |
| 85 | sa2.sa_flags = 0; |
| 86 | |
| 87 | if (sigaction(SIGTERM, &sa1, NULL)) |
| 88 | perror("sigaction() failed"); |
| 89 | if (sigaction(SIGINT, &sa1, NULL)) |
| 90 | perror("sigaction() failed"); |
| 91 | if (sigaction(SIGUSR1, &sa2, NULL)) |
| 92 | perror("sigaction() failed"); |
| 93 | } |
| 94 | |
| 95 | static void print_usage_and_die(void) |
| 96 | { |
| 97 | fprintf(stderr, "usage: reference [options]\n"); |
| 98 | fprintf(stderr, " options:\n"); |
| 99 | fprintf(stderr, " -h, --help: Print this help text\n"); |
| 100 | fprintf(stderr, " -V, --version: Print version\n"); |
| 101 | fprintf(stderr, " -c, --config: Path to config file\n"); |
| 102 | |
| 103 | exit(EXIT_SUCCESS); |
| 104 | } |
| 105 | |
| 106 | static void print_version_and_die(void) |
| 107 | { |
| 108 | printf("reference: version \"%s\"\n", VERSION); |
| 109 | exit(EXIT_SUCCESS); |
| 110 | } |
| 111 | |
| 112 | int main(int argc, char *argv[]) |
| 113 | { |
| 114 | const char *config_file = NULL; |
| 115 | int c, ret; |
| 116 | |
| 117 | while ((c = getopt_long(argc, argv, "c:hV", long_options, NULL)) != -1) { |
| 118 | switch (c) { |
| 119 | case 'V': |
| 120 | print_version_and_die(); |
| 121 | break; |
| 122 | case 'c': |
| 123 | config_file = optarg; |
| 124 | break; |
| 125 | case 'h': |
| 126 | default: |
| 127 | print_usage_and_die(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | ret = config_set_defaults(false); |
| 132 | if (ret) { |
| 133 | fprintf(stderr, "Failed to set default config values!\n"); |
| 134 | exit(EXIT_FAILURE); |
| 135 | } |
| 136 | |
| 137 | if (!config_file) { |
| 138 | fprintf(stderr, "Specifying an configuration file is mandatory. See tests/ " |
| 139 | "directory for examples!\n"); |
| 140 | exit(EXIT_FAILURE); |
| 141 | } |
| 142 | |
| 143 | ret = config_read_from_file(config_file); |
| 144 | if (ret) { |
| 145 | fprintf(stderr, "Failed to parse configuration file!\n"); |
| 146 | exit(EXIT_FAILURE); |
| 147 | } |
| 148 | |
| 149 | config_print_values(); |
| 150 | |
| 151 | if (!config_sanity_check()) { |
| 152 | fprintf(stderr, "Configuration failed sanity checks!\n"); |
| 153 | exit(EXIT_FAILURE); |
| 154 | } |
| 155 | |
| 156 | if (mlockall(MCL_CURRENT | MCL_FUTURE)) { |
| 157 | perror("mlockall() failed"); |
| 158 | exit(EXIT_FAILURE); |
| 159 | } |
| 160 | |
| 161 | configure_cpu_latency(); |
| 162 | |
| 163 | setup_signals(); |
| 164 | |
| 165 | ret = log_init(); |
| 166 | if (ret) { |
| 167 | fprintf(stderr, "Failed to initialize logging!\n"); |
| 168 | exit(EXIT_FAILURE); |
| 169 | } |
| 170 | |
| 171 | ret = histogram_init(); |
| 172 | if (ret) { |
| 173 | fprintf(stderr, "Failed to initialize histogram code!\n"); |
| 174 | exit(EXIT_FAILURE); |
| 175 | } |
| 176 | |
| 177 | ret = stat_init(LOG_REFERENCE); |
| 178 | if (ret) { |
| 179 | fprintf(stderr, "Failed to initialize statistics!\n"); |
| 180 | exit(EXIT_FAILURE); |
| 181 | } |
| 182 | |
| 183 | log_thread = log_thread_create(); |
| 184 | if (!log_thread) { |
| 185 | fprintf(stderr, "Failed to create and start Log Thread!\n"); |
| 186 | exit(EXIT_FAILURE); |
| 187 | } |
| 188 | |
| 189 | log_mqtt_thread = log_mqtt_thread_create(); |
| 190 | if (!log_mqtt_thread && app_config.log_mqtt) { |
| 191 | fprintf(stderr, "Failed to create and start Log via MQTT Thread!\n"); |
| 192 | exit(EXIT_FAILURE); |
| 193 | } |
| 194 | |
| 195 | log_json_thread = log_json_thread_create(); |
| 196 | if (!log_json_thread && app_config.log_json) { |
| 197 | fprintf(stderr, "Failed to create and start Log via JSON Thread!\n"); |
| 198 | exit(EXIT_FAILURE); |
| 199 | } |
| 200 | |
| 201 | g2_threads = generic_l2_threads_create(); |
| 202 | if (!g2_threads) { |
| 203 | fprintf(stderr, "Failed to create and start Generic L2 Threads!\n"); |
| 204 | exit(EXIT_FAILURE); |
| 205 | } |
| 206 | |
| 207 | threads = calloc(NUM_PN_THREAD_TYPES, sizeof(struct thread_context)); |
| 208 | if (!threads) { |
| 209 | fprintf(stderr, "Failed to allocate PN threads!\n"); |
| 210 | exit(EXIT_FAILURE); |
| 211 | } |
| 212 | |
| 213 | if (link_pn_threads(threads)) { |
| 214 | fprintf(stderr, "Failed to determine PN traffic classes order!\n"); |
| 215 | exit(EXIT_FAILURE); |
| 216 | } |
| 217 | |
| 218 | ret = udp_low_threads_create(&threads[UDP_LOW_THREAD]); |
| 219 | if (ret) { |
| 220 | fprintf(stderr, "Failed to create and start UDP Low Threads!\n"); |
| 221 | exit(EXIT_FAILURE); |
| 222 | } |
| 223 | |
| 224 | ret = udp_high_threads_create(&threads[UDP_HIGH_THREAD]); |
| 225 | if (ret) { |
| 226 | fprintf(stderr, "Failed to create and start UDP High Threads!\n"); |
| 227 | exit(EXIT_FAILURE); |
| 228 | } |
| 229 | |
| 230 | ret = lldp_threads_create(&threads[LLDP_THREAD]); |
| 231 | if (ret) { |
| 232 | fprintf(stderr, "Failed to create and start LLDP Threads!\n"); |
| 233 | exit(EXIT_FAILURE); |
| 234 | } |
| 235 | |
| 236 | ret = dcp_threads_create(&threads[DCP_THREAD]); |
| 237 | if (ret) { |
| 238 | fprintf(stderr, "Failed to create and start DCP Threads!\n"); |
| 239 | exit(EXIT_FAILURE); |
| 240 | } |
| 241 | |
| 242 | ret = rta_threads_create(&threads[RTA_THREAD]); |
| 243 | if (ret) { |
| 244 | fprintf(stderr, "Failed to create and start RTA Threads!\n"); |
| 245 | exit(EXIT_FAILURE); |
| 246 | } |
| 247 | |
| 248 | ret = rtc_threads_create(&threads[RTC_THREAD]); |
| 249 | if (ret) { |
| 250 | fprintf(stderr, "Failed to create and start RTC Threads!\n"); |
| 251 | exit(EXIT_FAILURE); |
| 252 | } |
| 253 | |
| 254 | ret = tsn_low_threads_create(&threads[TSN_LOW_THREAD]); |
| 255 | if (ret) { |
| 256 | fprintf(stderr, "Failed to create and start TSN Low Threads!\n"); |
| 257 | exit(EXIT_FAILURE); |
| 258 | } |
| 259 | |
| 260 | ret = tsn_high_threads_create(&threads[TSN_HIGH_THREAD]); |
| 261 | if (ret) { |
| 262 | fprintf(stderr, "Failed to create and start TSN High Threads!\n"); |
| 263 | exit(EXIT_FAILURE); |
| 264 | } |
| 265 | |
| 266 | print_stats(); |
| 267 | |
| 268 | tsn_high_threads_wait_for_finish(&threads[TSN_HIGH_THREAD]); |
| 269 | tsn_low_threads_wait_for_finish(&threads[TSN_LOW_THREAD]); |
| 270 | rtc_threads_wait_for_finish(&threads[RTC_THREAD]); |
| 271 | rta_threads_wait_for_finish(&threads[RTA_THREAD]); |
| 272 | dcp_threads_wait_for_finish(&threads[DCP_THREAD]); |
| 273 | lldp_threads_wait_for_finish(&threads[LLDP_THREAD]); |
| 274 | udp_high_threads_wait_for_finish(&threads[UDP_HIGH_THREAD]); |
| 275 | udp_low_threads_wait_for_finish(&threads[UDP_LOW_THREAD]); |
| 276 | generic_l2_threads_wait_for_finish(g2_threads); |
| 277 | log_mqtt_thread_wait_for_finish(log_mqtt_thread); |
| 278 | log_json_thread_wait_for_finish(log_json_thread); |
| 279 | log_thread_wait_for_finish(log_thread); |
| 280 | |
| 281 | histogram_write(); |
| 282 | |
| 283 | tsn_high_threads_free(&threads[TSN_HIGH_THREAD]); |
| 284 | tsn_low_threads_free(&threads[TSN_LOW_THREAD]); |
| 285 | rtc_threads_free(&threads[RTC_THREAD]); |
| 286 | rta_threads_free(&threads[RTA_THREAD]); |
| 287 | dcp_threads_free(&threads[DCP_THREAD]); |
| 288 | lldp_threads_free(&threads[LLDP_THREAD]); |
| 289 | udp_high_threads_free(&threads[UDP_HIGH_THREAD]); |
| 290 | udp_low_threads_free(&threads[UDP_LOW_THREAD]); |
| 291 | generic_l2_threads_free(g2_threads); |
| 292 | log_mqtt_thread_free(log_mqtt_thread); |
| 293 | log_json_thread_free(log_json_thread); |
| 294 | log_thread_free(log_thread); |
| 295 | |
| 296 | histogram_free(); |
| 297 | stat_free(); |
| 298 | log_free(); |
| 299 | config_free(); |
| 300 | free(threads); |
| 301 | |
| 302 | restore_cpu_latency(); |
| 303 | |
| 304 | return EXIT_SUCCESS; |
| 305 | } |