| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/udp_thread.c`/ |
| 2 | /* |
| 3 | * Copyright (C) 2020-2024 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <pthread.h> |
| 8 | #include <signal.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <arpa/inet.h> |
| 15 | |
| 16 | #include <linux/if_ether.h> |
| 17 | #include <linux/if_link.h> |
| 18 | #include <linux/if_vlan.h> |
| 19 | |
| 20 | #include "config.h"cxx . . $ `<file>/src/config.h`/ |
| 21 | #include "log.h"cxx . . $ `<file>/src/log.h`/ |
| 22 | #include "net.h"cxx . . $ `<file>/src/net.h`/ |
| 23 | #include "net_def.h"cxx . . $ `<file>/src/net_def.h`/ |
| 24 | #include "stat.h"cxx . . $ `<file>/src/stat.h`/ |
| 25 | #include "thread.h"cxx . . $ `<file>/src/thread.h`/ |
| 26 | #include "udp_thread.h"cxx . . $ `<file>/src/udp_thread.h`/ |
| 27 | #include "utils.h"cxx . . $ `<file>/src/utils.h`/ |
| 28 | |
| 29 | static void udp_initialize_framecxx . . $ udp_initialize_frame(3bba8a16eb80028b).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data) |
| 30 | { |
| 31 | const struct traffic_class_configcxx . . $ traffic_class_config# *udp_config = thread_context->confcxx . . $ thread_context#conf.; |
| 32 | struct reference_meta_datacxx . . $ reference_meta_data# *meta; |
| 33 | |
| 34 | /* |
| 35 | * UdpFrame: |
| 36 | * Cycle counter |
| 37 | * Payload |
| 38 | * Padding to maxFrame |
| 39 | */ |
| 40 | |
| 41 | /* Payload: SequenceCounter + Data */ |
| 42 | meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)frame_data; |
| 43 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(meta, '\0', sizeof(*meta)); |
| 44 | memcpycxx . . $ memcpy(cc817cb175f282d).(frame_data + sizeof(*meta), udp_config->payload_patterncxx . . $ traffic_class_config#payload_pattern., |
| 45 | udp_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.); |
| 46 | |
| 47 | /* Padding: '\0' */ |
| 48 | } |
| 49 | |
| 50 | static void udp_send_framecxx . . $ udp_send_frame(8f769fc55b2c1287).(struct thread_contextcxx . . $ thread_context# *thread_context, const unsigned char *frame_data, |
| 51 | size_t frame_length, size_t num_frames_per_cycle, int socket_fd, |
| 52 | const struct sockaddr_storagecxx . . $ sockaddr_storage# *destination) |
| 53 | { |
| 54 | struct reference_meta_datacxx . . $ reference_meta_data# *meta; |
| 55 | struct timespeccxx . . $ timespec# tx_time = {}; |
| 56 | uint64_tcxx . . $ uint64_t# sequence_counter; |
| 57 | ssize_tcxx . . $ ssize_t# ret = -1; |
| 58 | |
| 59 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., &tx_time); |
| 60 | |
| 61 | /* Fetch meta data */ |
| 62 | meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)frame_data; |
| 63 | sequence_counter = meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(meta, num_frames_per_cycle); |
| 64 | |
| 65 | tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(meta, ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&tx_time)); |
| 66 | |
| 67 | /* Send it */ |
| 68 | switch (destination->ss_familycxx . . $ sockaddr_storage#ss_family.) { |
| 69 | case AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!: |
| 70 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(socket_fd, frame_data, frame_length, 0, |
| 71 | (struct sockaddr_incxx . . $ sockaddr_in# *)destination, sizeof(struct sockaddr_incxx . . $ sockaddr_in#)); |
| 72 | break; |
| 73 | case AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!: |
| 74 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(socket_fd, frame_data, frame_length, 0, |
| 75 | (struct sockaddr_in6cxx . . $ sockaddr_in6# *)destination, sizeof(struct sockaddr_in6cxx . . $ sockaddr_in6#)); |
| 76 | break; |
| 77 | } |
| 78 | if (ret < 0) { |
| 79 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sTx: send() for %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! " failed: %s\n", |
| 80 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., sequence_counter, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(thread_context->frame_typecxx . . $ thread_context#frame_type., sequence_counter); |
| 85 | } |
| 86 | |
| 87 | static void udp_gen_and_send_framecxx . . $ udp_gen_and_send_frame(1e6fdab918f8b8f3).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data, |
| 88 | size_t frame_length, size_t num_frames_per_cycle, int socket_fd, |
| 89 | uint64_tcxx . . $ uint64_t# sequence_counter, |
| 90 | const struct sockaddr_storagecxx . . $ sockaddr_storage# *destination) |
| 91 | { |
| 92 | struct reference_meta_datacxx . . $ reference_meta_data# *meta; |
| 93 | struct timespeccxx . . $ timespec# tx_time = {}; |
| 94 | ssize_tcxx . . $ ssize_t# ret = -1; |
| 95 | |
| 96 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., &tx_time); |
| 97 | |
| 98 | /* Adjust meta data */ |
| 99 | meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)frame_data; |
| 100 | sequence_counter_to_meta_datacxx . . $ sequence_counter_to_meta_data(9318332f0006d3bc).(meta, sequence_counter, num_frames_per_cycle); |
| 101 | |
| 102 | tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(meta, ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&tx_time)); |
| 103 | |
| 104 | /* Send it */ |
| 105 | switch (destination->ss_familycxx . . $ sockaddr_storage#ss_family.) { |
| 106 | case AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!: |
| 107 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(socket_fd, frame_data, frame_length, 0, |
| 108 | (struct sockaddr_incxx . . $ sockaddr_in# *)destination, sizeof(struct sockaddr_incxx . . $ sockaddr_in#)); |
| 109 | break; |
| 110 | case AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!: |
| 111 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(socket_fd, frame_data, frame_length, 0, |
| 112 | (struct sockaddr_in6cxx . . $ sockaddr_in6# *)destination, sizeof(struct sockaddr_in6cxx . . $ sockaddr_in6#)); |
| 113 | break; |
| 114 | default: |
| 115 | ret = -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 116 | } |
| 117 | if (ret < 0) { |
| 118 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sTx: send() for %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! " failed: %s\n", |
| 119 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., sequence_counter, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(thread_context->frame_typecxx . . $ thread_context#frame_type., sequence_counter); |
| 124 | } |
| 125 | |
| 126 | static void *udp_tx_thread_routinecxx . . $ udp_tx_thread_routine(9f162e79bfc6c74).(void *data) |
| 127 | { |
| 128 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 129 | const struct traffic_class_configcxx . . $ traffic_class_config# *udp_config = thread_context->confcxx . . $ thread_context#conf.; |
| 130 | size_t received_frames_length = MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 131 | unsigned char *received_frames = thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.; |
| 132 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = udp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 133 | pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex = &thread_context->data_mutexcxx . . $ thread_context#data_mutex.; |
| 134 | pthread_cond_tcxx . . $ pthread_cond_t# *cond = &thread_context->data_cond_varcxx . . $ thread_context#data_cond_var.; |
| 135 | uint64_tcxx . . $ uint64_t# sequence_counter = 0; |
| 136 | unsigned char *frame; |
| 137 | int socket_fd; |
| 138 | |
| 139 | socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.; |
| 140 | frame = thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.; |
| 141 | |
| 142 | udp_initialize_framecxx . . $ udp_initialize_frame(3bba8a16eb80028b).(thread_context, frame); |
| 143 | |
| 144 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 145 | struct timespeccxx . . $ timespec# timeout; |
| 146 | size_t num_frames, i; |
| 147 | int ret; |
| 148 | |
| 149 | /* |
| 150 | * Wait until signalled. These UDP frames have to be sent after the LLDP |
| 151 | * frames. Therefore, the LLDP or UDP High TxThread signals this one here. |
| 152 | */ |
| 153 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!, &timeout); |
| 154 | timeout.tv_seccxx . . $ timespec#tv_sec.++; |
| 155 | |
| 156 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(mutex); |
| 157 | ret = pthread_cond_timedwaitcxx . . $ pthread_cond_timedwait(d8fcb9edac03ad51).(cond, mutex, &timeout); |
| 158 | num_frames = thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available.; |
| 159 | thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = 0; |
| 160 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(mutex); |
| 161 | |
| 162 | /* In case of shutdown a signal may be missing. */ |
| 163 | if (ret == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!) |
| 164 | continue; |
| 165 | |
| 166 | /* |
| 167 | * Send UdpFrames, two possibilites: |
| 168 | * a) Generate it, or |
| 169 | * b) Use received ones if mirror enabled |
| 170 | */ |
| 171 | if (!mirror_enabled) { |
| 172 | /* Send UdpFrames */ |
| 173 | for (i = 0; i < num_frames; ++i) |
| 174 | udp_gen_and_send_framecxx . . $ udp_gen_and_send_frame(1e6fdab918f8b8f3).( |
| 175 | thread_context, frame, udp_config->frame_lengthcxx . . $ traffic_class_config#frame_length., |
| 176 | udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., socket_fd, |
| 177 | sequence_counter++, &thread_context->destinationcxx . . $ thread_context#destination.); |
| 178 | } else { |
| 179 | size_t len; |
| 180 | |
| 181 | ring_buffer_fetchcxx . . $ ring_buffer_fetch(e8bf408057bf6314).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer., received_frames, |
| 182 | received_frames_length, &len); |
| 183 | |
| 184 | /* Len should be a multiple of frame size */ |
| 185 | for (i = 0; i < len / udp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.; ++i) |
| 186 | udp_send_framecxx . . $ udp_send_frame(8f769fc55b2c1287).(thread_context, |
| 187 | received_frames + i * udp_config->frame_lengthcxx . . $ traffic_class_config#frame_length., |
| 188 | udp_config->frame_lengthcxx . . $ traffic_class_config#frame_length., |
| 189 | udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., socket_fd, |
| 190 | &thread_context->destinationcxx . . $ thread_context#destination.); |
| 191 | |
| 192 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.); |
| 193 | thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = 0; |
| 194 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.); |
| 195 | } |
| 196 | |
| 197 | /* Signal next Tx thread */ |
| 198 | if (thread_context->nextcxx . . $ thread_context#next.) { |
| 199 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->nextcxx . . $ thread_context#next.->data_mutexcxx . . $ thread_context#data_mutex.); |
| 200 | pthread_cond_signalcxx . . $ pthread_cond_signal(98095c34a2b0149d).(&thread_context->nextcxx . . $ thread_context#next.->data_cond_varcxx . . $ thread_context#data_cond_var.); |
| 201 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->nextcxx . . $ thread_context#next.->data_mutexcxx . . $ thread_context#data_mutex.); |
| 202 | } |
| 203 | |
| 204 | if (thread_context->is_lastcxx . . $ thread_context#is_last.) |
| 205 | stat_updatecxx . . $ stat_update(49f6e7a06ebc5aa8).(); |
| 206 | } |
| 207 | |
| 208 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 209 | } |
| 210 | |
| 211 | static void *udp_rx_thread_routinecxx . . $ udp_rx_thread_routine(9f162e79bfc6c74).(void *data) |
| 212 | { |
| 213 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 214 | const struct traffic_class_configcxx . . $ traffic_class_config# *udp_config = thread_context->confcxx . . $ thread_context#conf.; |
| 215 | const unsigned char *expected_pattern = (const unsigned char *)udp_config->payload_patterncxx . . $ traffic_class_config#payload_pattern.; |
| 216 | const size_t expected_pattern_length = udp_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.; |
| 217 | const uint64_tcxx . . $ uint64_t# cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.; |
| 218 | const size_t num_frames_per_cycle = udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 219 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = udp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 220 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ignore_rx_errors = udp_config->ignore_rx_errorscxx . . $ traffic_class_config#ignore_rx_errors.; |
| 221 | const ssize_tcxx . . $ ssize_t# frame_length = udp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.; |
| 222 | unsigned char frame[MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!]; |
| 223 | uint64_tcxx . . $ uint64_t# sequence_counter = 0; |
| 224 | struct timespeccxx . . $ timespec# wakeup_time; |
| 225 | uint64_tcxx . . $ uint64_t# tx_timestamp; |
| 226 | int socket_fd, ret; |
| 227 | |
| 228 | socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.; |
| 229 | |
| 230 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(app_configcxx . . $ app_config..application_rx_base_offset_nscxx . . $ application_config#application_rx_base_offset_ns., &wakeup_time); |
| 231 | if (ret) { |
| 232 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sRx: Failed to calculate thread start time: %s!\n", |
| 233 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 234 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 235 | } |
| 236 | |
| 237 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 238 | /* Wait until next period. */ |
| 239 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 240 | |
| 241 | do { |
| 242 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 243 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 244 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 245 | |
| 246 | if (ret) { |
| 247 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sRx: clock_nanosleep() failed: %s\n", |
| 248 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 249 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 250 | } |
| 251 | |
| 252 | /* Receive Udp packets. */ |
| 253 | while (truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!) { |
| 254 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! out_of_order, payload_mismatch, frame_id_mismatch; |
| 255 | struct reference_meta_datacxx . . $ reference_meta_data# *meta; |
| 256 | uint64_tcxx . . $ uint64_t# rx_sequence_counter; |
| 257 | ssize_tcxx . . $ ssize_t# len; |
| 258 | |
| 259 | len = recvcxx . . $ recv(4136b1fcb15a75da).(socket_fd, frame, sizeof(frame), 0); |
| 260 | if (len == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`!) { |
| 261 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sRx: recv() failed: %s\n", |
| 262 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 263 | continue; |
| 264 | } |
| 265 | if (len == 0) |
| 266 | continue; |
| 267 | |
| 268 | /* No more frames. Comeback within next period. */ |
| 269 | if (len == -1 && (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`!)) |
| 270 | break; |
| 271 | |
| 272 | /* Process received packet. */ |
| 273 | if (len != frame_length) { |
| 274 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 275 | "%sRx: Frame with wrong length received!\n", |
| 276 | thread_context->traffic_classcxx . . $ thread_context#traffic_class.); |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Check cycle counter and payload. The ether type is checked by the |
| 282 | * attached BPF filter. |
| 283 | */ |
| 284 | meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)frame; |
| 285 | rx_sequence_counter = |
| 286 | meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(meta, num_frames_per_cycle); |
| 287 | |
| 288 | tx_timestamp = meta_data_to_tx_timestampcxx . . $ meta_data_to_tx_timestamp(e17106d766bd017c).(meta); |
| 289 | set_mirror_tx_timestampcxx . . $ set_mirror_tx_timestamp(457bf14cd1570296).(meta); |
| 290 | |
| 291 | out_of_order = sequence_counter != rx_sequence_counter; |
| 292 | payload_mismatch = memcmpcxx . . $ memcmp(36f949851efa3055).(frame + sizeof(struct reference_meta_datacxx . . $ reference_meta_data#), |
| 293 | expected_pattern, expected_pattern_length); |
| 294 | frame_id_mismatch = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 295 | |
| 296 | stat_frame_receivedcxx . . $ stat_frame_received(5534a29e0120fff).(thread_context->frame_typecxx . . $ thread_context#frame_type., sequence_counter, |
| 297 | out_of_order, payload_mismatch, frame_id_mismatch, |
| 298 | tx_timestamp, 0, 0); |
| 299 | |
| 300 | if (out_of_order) { |
| 301 | if (!ignore_rx_errors) |
| 302 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 303 | "%sRx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! |
| 304 | "] SequenceCounter mismatch: %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "!\n", |
| 305 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., |
| 306 | rx_sequence_counter, sequence_counter); |
| 307 | sequence_counter++; |
| 308 | } |
| 309 | |
| 310 | sequence_counter++; |
| 311 | |
| 312 | if (payload_mismatch) |
| 313 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 314 | "%sRx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "] Payload Pattern mismatch!\n", |
| 315 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., rx_sequence_counter); |
| 316 | |
| 317 | /* If mirror enabled, assemble and store the frame for Tx later. */ |
| 318 | if (!mirror_enabled) |
| 319 | continue; |
| 320 | |
| 321 | /* Store the new frame. */ |
| 322 | ring_buffer_addcxx . . $ ring_buffer_add(5f1d237704bb7e40).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer., frame, len); |
| 323 | |
| 324 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.); |
| 325 | thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available.++; |
| 326 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 331 | } |
| 332 | |
| 333 | static void *udp_tx_generation_thread_routinecxx . . $ udp_tx_generation_thread_routine(9f162e79bfc6c74).(void *data) |
| 334 | { |
| 335 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 336 | const struct traffic_class_configcxx . . $ traffic_class_config# *udp_config = thread_context->confcxx . . $ thread_context#conf.; |
| 337 | pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex = &thread_context->data_mutexcxx . . $ thread_context#data_mutex.; |
| 338 | uint64_tcxx . . $ uint64_t# cycle_time_ns = udp_config->burst_period_nscxx . . $ traffic_class_config#burst_period_ns.; |
| 339 | uint64_tcxx . . $ uint64_t# num_frames = udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 340 | struct timespeccxx . . $ timespec# wakeup_time; |
| 341 | int ret; |
| 342 | |
| 343 | /* |
| 344 | * The UDP frames are generated by bursts with a certain period. This thread is responsible |
| 345 | * for generating it. |
| 346 | */ |
| 347 | |
| 348 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(0, &wakeup_time); |
| 349 | if (ret) { |
| 350 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., |
| 351 | "%sTxGen: Failed to calculate thread start time: %s!\n", |
| 352 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 353 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 354 | } |
| 355 | |
| 356 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 357 | /* Wait until next period */ |
| 358 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 359 | |
| 360 | do { |
| 361 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 362 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 363 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 364 | |
| 365 | if (ret) { |
| 366 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sTxGen: clock_nanosleep() failed: %s\n", |
| 367 | thread_context->traffic_classcxx . . $ thread_context#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 368 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 369 | } |
| 370 | |
| 371 | /* Generate frames */ |
| 372 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(mutex); |
| 373 | thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = num_frames; |
| 374 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(mutex); |
| 375 | } |
| 376 | |
| 377 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 378 | } |
| 379 | |
| 380 | static int udp_threads_createcxx . . $ udp_threads_create(8542e1c024f3ff56).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 381 | { |
| 382 | const struct traffic_class_configcxx . . $ traffic_class_config# *udp_config = thread_context->confcxx . . $ thread_context#conf.; |
| 383 | char thread_name[128]; |
| 384 | int ret; |
| 385 | |
| 386 | if (thread_context->frame_typecxx . . $ thread_context#frame_type. == UDP_HIGH_FRAME_TYPEcxx . . $ UDP_HIGH_FRAME_TYPE. && |
| 387 | !config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("UdpHigh")) { |
| 388 | ret = 0; |
| 389 | goto out; |
| 390 | } |
| 391 | if (thread_context->frame_typecxx . . $ thread_context#frame_type. == UDP_LOW_FRAME_TYPEcxx . . $ UDP_LOW_FRAME_TYPE. && |
| 392 | !config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("UdpLow")) { |
| 393 | ret = 0; |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | thread_context->socket_fdcxx . . $ thread_context#socket_fd. = create_udp_socketcxx . . $ create_udp_socket(2624c86183593345).( |
| 398 | udp_config->l3_destinationcxx . . $ traffic_class_config#l3_destination., udp_config->l3_sourcecxx . . $ traffic_class_config#l3_source., udp_config->l3_portcxx . . $ traffic_class_config#l3_port., |
| 399 | udp_config->socket_prioritycxx . . $ traffic_class_config#socket_priority., &thread_context->destinationcxx . . $ thread_context#destination.); |
| 400 | if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. < 0) { |
| 401 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create UdpSocket!\n"); |
| 402 | ret = -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 403 | goto err; |
| 404 | } |
| 405 | |
| 406 | init_mutexcxx . . $ init_mutex(4f83bfebf4bb9c8b).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.); |
| 407 | init_condition_variablecxx . . $ init_condition_variable(23b56462c1e1452f).(&thread_context->data_cond_varcxx . . $ thread_context#data_cond_var.); |
| 408 | |
| 409 | thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!); |
| 410 | if (!thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.) { |
| 411 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate UdpTxFrameData!\n"); |
| 412 | ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 413 | goto err_tx; |
| 414 | } |
| 415 | |
| 416 | thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!); |
| 417 | if (!thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.) { |
| 418 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate UdpRxFrameData!\n"); |
| 419 | ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 420 | goto err_rx; |
| 421 | } |
| 422 | |
| 423 | if (udp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.) { |
| 424 | /* Per period the expectation is: UdpNumFramesPerCycle * MAX_FRAME */ |
| 425 | thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer. = |
| 426 | ring_buffer_allocatecxx . . $ ring_buffer_allocate(a00a27d6b402ebff).(MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * udp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.); |
| 427 | if (!thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.) { |
| 428 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate Udp Mirror RingBuffer!\n"); |
| 429 | ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!; |
| 430 | goto err_buffer; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(thread_name, sizeof(thread_name), "%sTxThread", thread_context->traffic_classcxx . . $ thread_context#traffic_class.); |
| 435 | |
| 436 | ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., thread_name, |
| 437 | udp_config->tx_thread_prioritycxx . . $ traffic_class_config#tx_thread_priority., udp_config->tx_thread_cpucxx . . $ traffic_class_config#tx_thread_cpu., |
| 438 | udp_tx_thread_routinecxx . . $ udp_tx_thread_routine(9f162e79bfc6c74)., thread_context); |
| 439 | if (ret) { |
| 440 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Udp Tx Thread!\n"); |
| 441 | goto err_thread; |
| 442 | } |
| 443 | |
| 444 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(thread_name, sizeof(thread_name), "%sTxGenThread", thread_context->traffic_classcxx . . $ thread_context#traffic_class.); |
| 445 | |
| 446 | if (!udp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.) { |
| 447 | ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., "UdpLowTxGenThread", |
| 448 | udp_config->tx_thread_prioritycxx . . $ traffic_class_config#tx_thread_priority., udp_config->tx_thread_cpucxx . . $ traffic_class_config#tx_thread_cpu., |
| 449 | udp_tx_generation_thread_routinecxx . . $ udp_tx_generation_thread_routine(9f162e79bfc6c74)., thread_context); |
| 450 | if (ret) { |
| 451 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Udp TxGen Thread!\n"); |
| 452 | goto err_thread_txgen; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(thread_name, sizeof(thread_name), "%sRxThread", thread_context->traffic_classcxx . . $ thread_context#traffic_class.); |
| 457 | |
| 458 | ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->rx_task_idcxx . . $ thread_context#rx_task_id., thread_name, |
| 459 | udp_config->rx_thread_prioritycxx . . $ traffic_class_config#rx_thread_priority., udp_config->rx_thread_cpucxx . . $ traffic_class_config#rx_thread_cpu., |
| 460 | udp_rx_thread_routinecxx . . $ udp_rx_thread_routine(9f162e79bfc6c74)., thread_context); |
| 461 | if (ret) { |
| 462 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Udp Rx Thread!\n"); |
| 463 | goto err_thread_rx; |
| 464 | } |
| 465 | |
| 466 | return 0; |
| 467 | |
| 468 | err_thread_rx: |
| 469 | thread_context->stopcxx . . $ thread_context#stop. = 1; |
| 470 | if (thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id.) |
| 471 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 472 | err_thread_txgen: |
| 473 | thread_context->stopcxx . . $ thread_context#stop. = 1; |
| 474 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 475 | err_thread: |
| 476 | ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.); |
| 477 | err_buffer: |
| 478 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.); |
| 479 | err_rx: |
| 480 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.); |
| 481 | err_tx: |
| 482 | closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.); |
| 483 | err: |
| 484 | out: |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | static void udp_threads_freecxx . . $ udp_threads_free(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 489 | { |
| 490 | if (!thread_context) |
| 491 | return; |
| 492 | |
| 493 | ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.); |
| 494 | |
| 495 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.); |
| 496 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.); |
| 497 | |
| 498 | if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. > 0) |
| 499 | closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.); |
| 500 | } |
| 501 | |
| 502 | static void udp_threads_wait_for_finishcxx . . $ udp_threads_wait_for_finish(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 503 | { |
| 504 | if (!thread_context) |
| 505 | return; |
| 506 | |
| 507 | if (thread_context->rx_task_idcxx . . $ thread_context#rx_task_id.) |
| 508 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->rx_task_idcxx . . $ thread_context#rx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 509 | if (thread_context->tx_task_idcxx . . $ thread_context#tx_task_id.) |
| 510 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 511 | if (thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id.) |
| 512 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 513 | } |
| 514 | |
| 515 | int udp_low_threads_createcxx . . $ udp_low_threads_create(8542e1c024f3ff56).(struct thread_contextcxx . . $ thread_context# *udp_thread_context) |
| 516 | { |
| 517 | udp_thread_context->confcxx . . $ thread_context#conf. = &app_configcxx . . $ app_config..classescxx . . $ application_config#classes.[UDP_LOW_FRAME_TYPEcxx . . $ UDP_LOW_FRAME_TYPE.]; |
| 518 | udp_thread_context->frame_typecxx . . $ thread_context#frame_type. = UDP_LOW_FRAME_TYPEcxx . . $ UDP_LOW_FRAME_TYPE.; |
| 519 | udp_thread_context->traffic_classcxx . . $ thread_context#traffic_class. = stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(UDP_LOW_FRAME_TYPEcxx . . $ UDP_LOW_FRAME_TYPE.); |
| 520 | |
| 521 | return udp_threads_createcxx . . $ udp_threads_create(8542e1c024f3ff56).(udp_thread_context); |
| 522 | } |
| 523 | |
| 524 | void udp_low_threads_freecxx . . $ udp_low_threads_free(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 525 | { |
| 526 | udp_threads_freecxx . . $ udp_threads_free(f7c2ab51214b7176).(thread_context); |
| 527 | } |
| 528 | |
| 529 | void udp_low_threads_wait_for_finishcxx . . $ udp_low_threads_wait_for_finish(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 530 | { |
| 531 | udp_threads_wait_for_finishcxx . . $ udp_threads_wait_for_finish(f7c2ab51214b7176).(thread_context); |
| 532 | } |
| 533 | |
| 534 | int udp_high_threads_createcxx . . $ udp_high_threads_create(8542e1c024f3ff56).(struct thread_contextcxx . . $ thread_context# *udp_thread_context) |
| 535 | { |
| 536 | udp_thread_context->confcxx . . $ thread_context#conf. = &app_configcxx . . $ app_config..classescxx . . $ application_config#classes.[UDP_HIGH_FRAME_TYPEcxx . . $ UDP_HIGH_FRAME_TYPE.]; |
| 537 | udp_thread_context->frame_typecxx . . $ thread_context#frame_type. = UDP_HIGH_FRAME_TYPEcxx . . $ UDP_HIGH_FRAME_TYPE.; |
| 538 | udp_thread_context->traffic_classcxx . . $ thread_context#traffic_class. = stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(UDP_HIGH_FRAME_TYPEcxx . . $ UDP_HIGH_FRAME_TYPE.); |
| 539 | |
| 540 | return udp_threads_createcxx . . $ udp_threads_create(8542e1c024f3ff56).(udp_thread_context); |
| 541 | } |
| 542 | |
| 543 | void udp_high_threads_freecxx . . $ udp_high_threads_free(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 544 | { |
| 545 | udp_threads_freecxx . . $ udp_threads_free(f7c2ab51214b7176).(thread_context); |
| 546 | } |
| 547 | |
| 548 | void udp_high_threads_wait_for_finishcxx . . $ udp_high_threads_wait_for_finish(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 549 | { |
| 550 | udp_threads_wait_for_finishcxx . . $ udp_threads_wait_for_finish(f7c2ab51214b7176).(thread_context); |
| 551 | } |