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