| 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 <errno.h> |
| 8 | #include <pthread.h> |
| 9 | #include <signal.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include <arpa/inet.h> |
| 17 | |
| 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/if_link.h> |
| 20 | #include <linux/if_packet.h> |
| 21 | #include <linux/if_vlan.h> |
| 22 | |
| 23 | #include "config.h" |
| 24 | #include "log.h" |
| 25 | #include "net.h" |
| 26 | #include "packet.h" |
| 27 | #include "rtc_thread.h" |
| 28 | #include "security.h" |
| 29 | #include "stat.h" |
| 30 | #include "thread.h" |
| 31 | #include "utils.h" |
| 32 | #include "xdp.h" |
| 33 | |
| 34 | static void rtc_initialize_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 35 | size_t num_frames, const unsigned char *source, |
| 36 | const unsigned char *destination) |
| 37 | { |
| 38 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 39 | size_t i; |
| 40 | |
| 41 | for (i = 0; i < num_frames; ++i) { |
| 42 | unsigned char *frame = frame_idx(frame_data, i); |
| 43 | size_t frame_length = MAX_FRAME_SIZE; |
| 44 | |
| 45 | /* |
| 46 | * In case both AF_XDP and Tx HW Timestamp are enabled the payload starts at: |
| 47 | * frame_data + sizeof(struct xsk_tx_metadata) |
| 48 | */ |
| 49 | #ifdef TX_TIMESTAMP |
| 50 | if (rtc_config->xdp_enabled && rtc_config->tx_hwtstamp_enabled) { |
| 51 | frame += sizeof(struct xsk_tx_metadata); |
| 52 | frame_length -= sizeof(struct xsk_tx_metadata); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | initialize_profinet_frame(rtc_config->security_mode, frame, frame_length, source, |
| 57 | destination, rtc_config->payload_pattern, |
| 58 | rtc_config->payload_pattern_length, |
| 59 | rtc_config->vid | rtc_config->pcp << VLAN_PCP_SHIFT, |
| 60 | thread_context->frame_id); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static int rtc_send_messages(struct thread_context *thread_context, int socket_fd, |
| 65 | struct sockaddr_ll *destination, unsigned char *frame_data, |
| 66 | size_t num_frames) |
| 67 | { |
| 68 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 69 | struct packet_send_request send_req = { |
| 70 | .traffic_class = stat_frame_type_to_string(RTC_FRAME_TYPE), |
| 71 | .socket_fd = socket_fd, |
| 72 | .destination = destination, |
| 73 | .frame_data = frame_data, |
| 74 | .num_frames = num_frames, |
| 75 | .frame_length = rtc_config->frame_length, |
| 76 | .duration = 0, |
| 77 | .tx_time_offset = 0, |
| 78 | .meta_data_offset = thread_context->meta_data_offset, |
| 79 | .mirror_enabled = rtc_config->rx_mirror_enabled, |
| 80 | .tx_time_enabled = false, |
| 81 | }; |
| 82 | |
| 83 | return packet_send_messages(thread_context->packet_context, &send_req); |
| 84 | } |
| 85 | |
| 86 | static int rtc_send_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 87 | size_t num_frames, int socket_fd, struct sockaddr_ll *destination) |
| 88 | { |
| 89 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 90 | int len, i; |
| 91 | |
| 92 | /* Send it */ |
| 93 | len = rtc_send_messages(thread_context, socket_fd, destination, frame_data, num_frames); |
| 94 | |
| 95 | for (i = 0; i < len; i++) { |
| 96 | uint64_t sequence_counter; |
| 97 | |
| 98 | sequence_counter = get_sequence_counter(frame_data + i * rtc_config->frame_length, |
| 99 | thread_context->meta_data_offset, |
| 100 | rtc_config->num_frames_per_cycle); |
| 101 | |
| 102 | stat_frame_sent(RTC_FRAME_TYPE, sequence_counter); |
| 103 | } |
| 104 | |
| 105 | return len; |
| 106 | } |
| 107 | |
| 108 | static int rtc_gen_and_send_frames(struct thread_context *thread_context, int socket_fd, |
| 109 | struct sockaddr_ll *destination, uint64_t sequence_counter_begin) |
| 110 | { |
| 111 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 112 | struct timespec tx_time = {}; |
| 113 | int len, i; |
| 114 | |
| 115 | clock_gettime(app_config.application_clock_id, &tx_time); |
| 116 | |
| 117 | for (i = 0; i < rtc_config->num_frames_per_cycle; i++) { |
| 118 | struct prepare_frame_config frame_config; |
| 119 | int err; |
| 120 | |
| 121 | frame_config.mode = rtc_config->security_mode; |
| 122 | frame_config.security_context = thread_context->tx_security_context; |
| 123 | frame_config.iv_prefix = (const unsigned char *)rtc_config->security_iv_prefix; |
| 124 | frame_config.payload_pattern = thread_context->payload_pattern; |
| 125 | frame_config.payload_pattern_length = thread_context->payload_pattern_length; |
| 126 | frame_config.frame_data = frame_idx(thread_context->tx_frame_data, i); |
| 127 | frame_config.frame_length = rtc_config->frame_length; |
| 128 | frame_config.num_frames_per_cycle = rtc_config->num_frames_per_cycle; |
| 129 | frame_config.sequence_counter = sequence_counter_begin + i; |
| 130 | frame_config.tx_timestamp = ts_to_ns(&tx_time); |
| 131 | frame_config.meta_data_offset = thread_context->meta_data_offset; |
| 132 | |
| 133 | err = prepare_frame_for_tx(&frame_config); |
| 134 | if (err) |
| 135 | log_message(LOG_LEVEL_ERROR, "RtcTx: Failed to prepare frame for Tx!\n"); |
| 136 | } |
| 137 | |
| 138 | /* Send it */ |
| 139 | len = rtc_send_messages(thread_context, socket_fd, destination, |
| 140 | thread_context->tx_frame_data, rtc_config->num_frames_per_cycle); |
| 141 | |
| 142 | for (i = 0; i < len; i++) |
| 143 | stat_frame_sent(RTC_FRAME_TYPE, sequence_counter_begin + i); |
| 144 | |
| 145 | return len; |
| 146 | } |
| 147 | |
| 148 | static void rtc_gen_and_send_xdp_frames(struct thread_context *thread_context, |
| 149 | struct xdp_socket *xsk, uint64_t sequence_counter, |
| 150 | uint32_t *frame_number) |
| 151 | { |
| 152 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 153 | struct xdp_gen_config xdp; |
| 154 | |
| 155 | xdp.mode = rtc_config->security_mode; |
| 156 | xdp.security_context = thread_context->tx_security_context; |
| 157 | xdp.iv_prefix = (const unsigned char *)rtc_config->security_iv_prefix; |
| 158 | xdp.payload_pattern = thread_context->payload_pattern; |
| 159 | xdp.payload_pattern_length = thread_context->payload_pattern_length; |
| 160 | xdp.frame_length = rtc_config->frame_length; |
| 161 | xdp.num_frames_per_cycle = rtc_config->num_frames_per_cycle; |
| 162 | xdp.frame_number = frame_number; |
| 163 | xdp.sequence_counter_begin = sequence_counter; |
| 164 | xdp.meta_data_offset = thread_context->meta_data_offset; |
| 165 | xdp.frame_type = RTC_FRAME_TYPE; |
| 166 | xdp.tx_time = NULL; |
| 167 | |
| 168 | xdp_gen_and_send_frames(xsk, &xdp); |
| 169 | } |
| 170 | |
| 171 | static void *rtc_tx_thread_routine(void *data) |
| 172 | { |
| 173 | struct thread_context *thread_context = data; |
| 174 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 175 | size_t received_frames_length = MAX_FRAME_SIZE * rtc_config->num_frames_per_cycle; |
| 176 | struct security_context *security_context = thread_context->tx_security_context; |
| 177 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 178 | unsigned char *received_frames = thread_context->rx_frame_data; |
| 179 | const bool mirror_enabled = rtc_config->rx_mirror_enabled; |
| 180 | struct sockaddr_ll destination; |
| 181 | unsigned char source[ETH_ALEN]; |
| 182 | uint64_t sequence_counter = 0; |
| 183 | struct timespec wakeup_time; |
| 184 | unsigned int if_index; |
| 185 | int ret, socket_fd; |
| 186 | |
| 187 | socket_fd = thread_context->socket_fd; |
| 188 | |
| 189 | ret = get_interface_mac_address(rtc_config->interface, source, ETH_ALEN); |
| 190 | if (ret < 0) { |
| 191 | log_message(LOG_LEVEL_ERROR, "RtcTx: Failed to get Source MAC address!\n"); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | if_index = if_nametoindex(rtc_config->interface); |
| 196 | if (!if_index) { |
| 197 | log_message(LOG_LEVEL_ERROR, "RtcTx: if_nametoindex() failed!\n"); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | memset(&destination, '\0', sizeof(destination)); |
| 202 | destination.sll_family = PF_PACKET; |
| 203 | destination.sll_ifindex = if_index; |
| 204 | destination.sll_halen = ETH_ALEN; |
| 205 | memcpy(destination.sll_addr, rtc_config->l2_destination, ETH_ALEN); |
| 206 | |
| 207 | rtc_initialize_frames(thread_context, thread_context->tx_frame_data, |
| 208 | rtc_config->num_frames_per_cycle, source, rtc_config->l2_destination); |
| 209 | |
| 210 | prepare_openssl(security_context); |
| 211 | rtc_initialize_frames(thread_context, thread_context->payload_pattern, 1, source, |
| 212 | rtc_config->l2_destination); |
| 213 | thread_context->payload_pattern += |
| 214 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 215 | thread_context->payload_pattern_length = |
| 216 | rtc_config->frame_length - sizeof(struct vlan_ethernet_header) - |
| 217 | sizeof(struct profinet_secure_header) - sizeof(struct security_checksum); |
| 218 | |
| 219 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 220 | if (ret) { |
| 221 | log_message(LOG_LEVEL_ERROR, "RtcTx: Failed to calculate thread start time: %s!\n", |
| 222 | strerror(errno)); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | while (!thread_context->stop) { |
| 227 | if (!thread_context->is_first) { |
| 228 | struct timespec timeout; |
| 229 | |
| 230 | /* |
| 231 | * Wait until signalled. These RTC frames have to be sent after the TSN Low |
| 232 | * frames. |
| 233 | */ |
| 234 | clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 235 | timeout.tv_sec++; |
| 236 | |
| 237 | pthread_mutex_lock(&thread_context->data_mutex); |
| 238 | ret = pthread_cond_timedwait(&thread_context->data_cond_var, |
| 239 | &thread_context->data_mutex, &timeout); |
| 240 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 241 | |
| 242 | /* In case of shutdown a signal may be missing. */ |
| 243 | if (ret == ETIMEDOUT) |
| 244 | continue; |
| 245 | } else { |
| 246 | /* Wait until next period */ |
| 247 | increment_period(&wakeup_time, cycle_time_ns); |
| 248 | |
| 249 | do { |
| 250 | ret = clock_nanosleep(app_config.application_clock_id, |
| 251 | TIMER_ABSTIME, &wakeup_time, NULL); |
| 252 | } while (ret == EINTR); |
| 253 | |
| 254 | if (ret) { |
| 255 | log_message(LOG_LEVEL_ERROR, |
| 256 | "RtcTx: clock_nanosleep() failed: %s\n", strerror(ret)); |
| 257 | return NULL; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Send RtcFrames, two possibilites: |
| 263 | * a) Generate it, or |
| 264 | * b) Use received ones if mirror enabled |
| 265 | */ |
| 266 | if (!mirror_enabled) { |
| 267 | rtc_gen_and_send_frames(thread_context, socket_fd, &destination, |
| 268 | sequence_counter); |
| 269 | |
| 270 | sequence_counter += rtc_config->num_frames_per_cycle; |
| 271 | } else { |
| 272 | size_t len, num_frames; |
| 273 | |
| 274 | ring_buffer_fetch(thread_context->mirror_buffer, received_frames, |
| 275 | received_frames_length, &len); |
| 276 | |
| 277 | /* Len should be a multiple of frame size */ |
| 278 | num_frames = len / rtc_config->frame_length; |
| 279 | rtc_send_frames(thread_context, received_frames, num_frames, socket_fd, |
| 280 | &destination); |
| 281 | } |
| 282 | |
| 283 | /* Signal next Tx thread */ |
| 284 | if (thread_context->next) { |
| 285 | pthread_mutex_lock(&thread_context->next->data_mutex); |
| 286 | pthread_cond_signal(&thread_context->next->data_cond_var); |
| 287 | pthread_mutex_unlock(&thread_context->next->data_mutex); |
| 288 | } |
| 289 | |
| 290 | if (thread_context->is_last) |
| 291 | stat_update(); |
| 292 | } |
| 293 | |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * This Tx thread routine differs to the standard one in terms of the sending interface. This one |
| 299 | * uses the AF_XDP user space interface. |
| 300 | */ |
| 301 | static void *rtc_xdp_tx_thread_routine(void *data) |
| 302 | { |
| 303 | struct thread_context *thread_context = data; |
| 304 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 305 | struct security_context *security_context = thread_context->tx_security_context; |
| 306 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 307 | const bool mirror_enabled = rtc_config->rx_mirror_enabled; |
| 308 | uint32_t frame_number = XSK_RING_PROD__DEFAULT_NUM_DESCS; |
| 309 | const size_t num_frames = rtc_config->num_frames_per_cycle; |
| 310 | unsigned char source[ETH_ALEN]; |
| 311 | uint64_t sequence_counter = 0; |
| 312 | struct timespec wakeup_time; |
| 313 | unsigned char *frame_data; |
| 314 | struct xdp_socket *xsk; |
| 315 | int ret; |
| 316 | |
| 317 | xsk = thread_context->xsk; |
| 318 | xsk->tx_hwts.rtt = &round_trip_contexts[RTC_FRAME_TYPE]; |
| 319 | xsk->tx_hwts.frames_per_cycle = rtc_config->num_frames_per_cycle; |
| 320 | |
| 321 | ret = get_interface_mac_address(rtc_config->interface, source, ETH_ALEN); |
| 322 | if (ret < 0) { |
| 323 | log_message(LOG_LEVEL_ERROR, "RtcTx: Failed to get Source MAC address!\n"); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | /* First half of umem area is for Rx, the second half is for Tx. */ |
| 328 | frame_data = xsk_umem__get_data(xsk->umem.buffer, |
| 329 | XDP_FRAME_SIZE * XSK_RING_PROD__DEFAULT_NUM_DESCS); |
| 330 | |
| 331 | /* Initialize all Tx frames */ |
| 332 | rtc_initialize_frames(thread_context, frame_data, XSK_RING_CONS__DEFAULT_NUM_DESCS, source, |
| 333 | rtc_config->l2_destination); |
| 334 | |
| 335 | prepare_openssl(security_context); |
| 336 | rtc_initialize_frames(thread_context, thread_context->payload_pattern, 1, source, |
| 337 | rtc_config->l2_destination); |
| 338 | thread_context->payload_pattern += |
| 339 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 340 | thread_context->payload_pattern_length = |
| 341 | rtc_config->frame_length - sizeof(struct vlan_ethernet_header) - |
| 342 | sizeof(struct profinet_secure_header) - sizeof(struct security_checksum); |
| 343 | |
| 344 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 345 | if (ret) { |
| 346 | log_message(LOG_LEVEL_ERROR, "RtcTx: Failed to calculate thread start time: %s!\n", |
| 347 | strerror(errno)); |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | while (!thread_context->stop) { |
| 352 | if (!thread_context->is_first) { |
| 353 | struct timespec timeout; |
| 354 | |
| 355 | /* |
| 356 | * Wait until signalled. These RTC frames have to be sent after the TSN Low |
| 357 | * frames. |
| 358 | */ |
| 359 | clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 360 | timeout.tv_sec++; |
| 361 | |
| 362 | pthread_mutex_lock(&thread_context->data_mutex); |
| 363 | ret = pthread_cond_timedwait(&thread_context->data_cond_var, |
| 364 | &thread_context->data_mutex, &timeout); |
| 365 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 366 | |
| 367 | /* In case of shutdown a signal may be missing. */ |
| 368 | if (ret == ETIMEDOUT) |
| 369 | continue; |
| 370 | } else { |
| 371 | /* Wait until next period */ |
| 372 | increment_period(&wakeup_time, cycle_time_ns); |
| 373 | |
| 374 | do { |
| 375 | ret = clock_nanosleep(app_config.application_clock_id, |
| 376 | TIMER_ABSTIME, &wakeup_time, NULL); |
| 377 | } while (ret == EINTR); |
| 378 | |
| 379 | if (ret) { |
| 380 | log_message(LOG_LEVEL_ERROR, |
| 381 | "RtcTx: clock_nanosleep() failed: %s\n", strerror(ret)); |
| 382 | return NULL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Send RtcFrames, two possibilites: |
| 388 | * a) Generate it, or |
| 389 | * b) Use received ones if mirror enabled |
| 390 | */ |
| 391 | if (!mirror_enabled) { |
| 392 | rtc_gen_and_send_xdp_frames(thread_context, xsk, sequence_counter, |
| 393 | &frame_number); |
| 394 | sequence_counter += num_frames; |
| 395 | } else { |
| 396 | unsigned int received; |
| 397 | uint64_t i; |
| 398 | |
| 399 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 400 | |
| 401 | received = thread_context->received_frames; |
| 402 | |
| 403 | sequence_counter = thread_context->rx_sequence_counter - received; |
| 404 | |
| 405 | /* |
| 406 | * The XDP receiver stored the frames within the umem area and populated the |
| 407 | * Tx ring. Now, the Tx ring can be committed to the kernel. Furthermore, |
| 408 | * already transmitted frames from last cycle can be recycled for Rx. |
| 409 | */ |
| 410 | |
| 411 | xsk_ring_prod__submit(&xsk->tx, received); |
| 412 | |
| 413 | if (received > 0) { |
| 414 | if (rtc_config->tx_hwtstamp_enabled) { |
| 415 | /* |
| 416 | * Once-per-cycle: record TX SW timestamp for the first |
| 417 | * packet in this cycle and count all frames |
| 418 | */ |
| 419 | stat_frames_sent_batch(RTC_FRAME_TYPE, sequence_counter, |
| 420 | received); |
| 421 | } else { |
| 422 | for (i = sequence_counter; i < sequence_counter + received; |
| 423 | ++i) |
| 424 | stat_frame_sent(RTC_FRAME_TYPE, i); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | xsk->outstanding_tx += received; |
| 429 | thread_context->received_frames = 0; |
| 430 | xdp_complete_tx(xsk); |
| 431 | |
| 432 | #ifdef TX_TIMESTAMP |
| 433 | if (received > 0 && rtc_config->tx_hwtstamp_enabled) { |
| 434 | /* |
| 435 | * TX HW timestamp becomes available in the next cycle |
| 436 | * after the packet is transmitted. |
| 437 | * This is one cycle later than SW timestamp tracked |
| 438 | * using sequence_counter |
| 439 | */ |
| 440 | xsk->tx_hwts.seq_lagged = sequence_counter; |
| 441 | } |
| 442 | #endif |
| 443 | |
| 444 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 445 | } |
| 446 | |
| 447 | /* Signal next Tx thread */ |
| 448 | if (thread_context->next) { |
| 449 | pthread_mutex_lock(&thread_context->next->data_mutex); |
| 450 | pthread_cond_signal(&thread_context->next->data_cond_var); |
| 451 | pthread_mutex_unlock(&thread_context->next->data_mutex); |
| 452 | } |
| 453 | |
| 454 | if (thread_context->is_last) |
| 455 | stat_update(); |
| 456 | } |
| 457 | |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | static void *rtc_rx_thread_routine(void *data) |
| 462 | { |
| 463 | struct thread_context *thread_context = data; |
| 464 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 465 | struct timespec wakeup_time; |
| 466 | int socket_fd, ret; |
| 467 | |
| 468 | socket_fd = thread_context->socket_fd; |
| 469 | |
| 470 | prepare_openssl(thread_context->rx_security_context); |
| 471 | |
| 472 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 473 | if (ret) { |
| 474 | log_message(LOG_LEVEL_ERROR, "RtcRx: Failed to calculate thread start time: %s!\n", |
| 475 | strerror(errno)); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | while (!thread_context->stop) { |
| 480 | struct packet_receive_request recv_req = { |
| 481 | .traffic_class = thread_context->traffic_class, |
| 482 | .socket_fd = socket_fd, |
| 483 | .receive_function = receive_profinet_frame, |
| 484 | .data = thread_context, |
| 485 | }; |
| 486 | |
| 487 | /* Wait until next period. */ |
| 488 | increment_period(&wakeup_time, cycle_time_ns); |
| 489 | |
| 490 | do { |
| 491 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 492 | &wakeup_time, NULL); |
| 493 | } while (ret == EINTR); |
| 494 | |
| 495 | if (ret) { |
| 496 | log_message(LOG_LEVEL_ERROR, "RtcRx: clock_nanosleep() failed: %s\n", |
| 497 | strerror(ret)); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | /* Receive Rtc frames. */ |
| 502 | packet_receive_messages(thread_context->packet_context, &recv_req); |
| 503 | } |
| 504 | |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | static void *rtc_xdp_rx_thread_routine(void *data) |
| 509 | { |
| 510 | struct thread_context *thread_context = data; |
| 511 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 512 | const struct traffic_class_config *rtc_config = thread_context->conf; |
| 513 | const bool mirror_enabled = rtc_config->rx_mirror_enabled; |
| 514 | const size_t frame_length = rtc_config->frame_length; |
| 515 | struct xdp_socket *xsk = thread_context->xsk; |
| 516 | struct timespec wakeup_time; |
| 517 | int ret; |
| 518 | |
| 519 | prepare_openssl(thread_context->rx_security_context); |
| 520 | |
| 521 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 522 | if (ret) { |
| 523 | log_message(LOG_LEVEL_ERROR, "RtcRx: Failed to calculate thread start time: %s!\n", |
| 524 | strerror(errno)); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | while (!thread_context->stop) { |
| 529 | unsigned int received; |
| 530 | |
| 531 | /* Wait until next period */ |
| 532 | increment_period(&wakeup_time, cycle_time_ns); |
| 533 | |
| 534 | do { |
| 535 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 536 | &wakeup_time, NULL); |
| 537 | } while (ret == EINTR); |
| 538 | |
| 539 | if (ret) { |
| 540 | log_message(LOG_LEVEL_ERROR, "RtcRx: clock_nanosleep() failed: %s\n", |
| 541 | strerror(ret)); |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 546 | received = xdp_receive_frames(xsk, frame_length, mirror_enabled, |
| 547 | receive_profinet_frame, thread_context, NULL); |
| 548 | thread_context->received_frames = received; |
| 549 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 550 | } |
| 551 | |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | int rtc_threads_create(struct thread_context *thread_context) |
| 556 | { |
| 557 | struct traffic_class_config *rtc_config; |
| 558 | int ret; |
| 559 | |
| 560 | if (!config_is_traffic_class_active("Rtc")) |
| 561 | goto out; |
| 562 | |
| 563 | init_mutex(&thread_context->data_mutex); |
| 564 | init_mutex(&thread_context->xdp_data_mutex); |
| 565 | init_condition_variable(&thread_context->data_cond_var); |
| 566 | |
| 567 | thread_context->conf = rtc_config = &app_config.classes[RTC_FRAME_TYPE]; |
| 568 | thread_context->frame_type = RTC_FRAME_TYPE; |
| 569 | thread_context->traffic_class = stat_frame_type_to_string(RTC_FRAME_TYPE); |
| 570 | thread_context->frame_id = |
| 571 | rtc_config->security_mode == SECURITY_MODE_NONE ? RTC_FRAMEID : RTC_SEC_FRAMEID; |
| 572 | |
| 573 | /* For XDP the frames are stored in a umem area. That memory is part of the socket. */ |
| 574 | if (!rtc_config->xdp_enabled) { |
| 575 | thread_context->packet_context = packet_init(rtc_config->num_frames_per_cycle); |
| 576 | if (!thread_context->packet_context) { |
| 577 | fprintf(stderr, "Failed to allocate Rtc packet context!\n"); |
| 578 | ret = -ENOMEM; |
| 579 | goto err_packet; |
| 580 | } |
| 581 | |
| 582 | thread_context->tx_frame_data = |
| 583 | calloc(rtc_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 584 | if (!thread_context->tx_frame_data) { |
| 585 | fprintf(stderr, "Failed to allocate RtcTxFrameData!\n"); |
| 586 | ret = -ENOMEM; |
| 587 | goto err_tx; |
| 588 | } |
| 589 | |
| 590 | thread_context->rx_frame_data = |
| 591 | calloc(rtc_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 592 | if (!thread_context->rx_frame_data) { |
| 593 | fprintf(stderr, "Failed to allocate RtcRxFrameData!\n"); |
| 594 | ret = -ENOMEM; |
| 595 | goto err_rx; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | thread_context->payload_pattern = calloc(1, MAX_FRAME_SIZE); |
| 600 | if (!thread_context->payload_pattern) { |
| 601 | fprintf(stderr, "Failed to allocate RtcPayloadPattern!\n"); |
| 602 | ret = -ENOMEM; |
| 603 | goto err_payload; |
| 604 | } |
| 605 | thread_context->payload_pattern_length = MAX_FRAME_SIZE; |
| 606 | |
| 607 | /* For XDP a AF_XDP socket is allocated. Otherwise a Linux raw socket is used. */ |
| 608 | if (rtc_config->xdp_enabled) { |
| 609 | thread_context->socket_fd = 0; |
| 610 | thread_context->xsk = xdp_open_socket( |
| 611 | rtc_config->interface, app_config.application_xdp_program, |
| 612 | rtc_config->rx_queue, rtc_config->xdp_skb_mode, rtc_config->xdp_zc_mode, |
| 613 | rtc_config->xdp_wakeup_mode, rtc_config->xdp_busy_poll_mode, false, |
| 614 | rtc_config->tx_hwtstamp_enabled); |
| 615 | if (!thread_context->xsk) { |
| 616 | fprintf(stderr, "Failed to create Rtc Xdp socket!\n"); |
| 617 | ret = -ENOMEM; |
| 618 | goto err_socket; |
| 619 | } |
| 620 | } else { |
| 621 | thread_context->xsk = NULL; |
| 622 | thread_context->socket_fd = create_rtc_socket(); |
| 623 | if (thread_context->socket_fd < 0) { |
| 624 | fprintf(stderr, "Failed to create RtcSocket!\n"); |
| 625 | ret = -errno; |
| 626 | goto err_socket; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* Same as above. For XDP the umem area is used. */ |
| 631 | if (rtc_config->rx_mirror_enabled && !rtc_config->xdp_enabled) { |
| 632 | /* Per period the expectation is: RtcNumFramesPerCycle * MAX_FRAME */ |
| 633 | thread_context->mirror_buffer = |
| 634 | ring_buffer_allocate(MAX_FRAME_SIZE * rtc_config->num_frames_per_cycle); |
| 635 | if (!thread_context->mirror_buffer) { |
| 636 | fprintf(stderr, "Failed to allocate Rtc Mirror RingBuffer!\n"); |
| 637 | ret = -ENOMEM; |
| 638 | goto err_thread; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (rtc_config->security_mode != SECURITY_MODE_NONE) { |
| 643 | thread_context->tx_security_context = security_init( |
| 644 | rtc_config->security_algorithm, (unsigned char *)rtc_config->security_key); |
| 645 | if (!thread_context->tx_security_context) { |
| 646 | fprintf(stderr, "Failed to initialize Tx security context!\n"); |
| 647 | ret = -ENOMEM; |
| 648 | goto err_tx_sec; |
| 649 | } |
| 650 | |
| 651 | thread_context->rx_security_context = security_init( |
| 652 | rtc_config->security_algorithm, (unsigned char *)rtc_config->security_key); |
| 653 | if (!thread_context->rx_security_context) { |
| 654 | fprintf(stderr, "Failed to initialize Rx security context!\n"); |
| 655 | ret = -ENOMEM; |
| 656 | goto err_rx_sec; |
| 657 | } |
| 658 | } else { |
| 659 | thread_context->tx_security_context = NULL; |
| 660 | thread_context->rx_security_context = NULL; |
| 661 | } |
| 662 | |
| 663 | ret = create_rt_thread(&thread_context->tx_task_id, "RtcTxThread", |
| 664 | rtc_config->tx_thread_priority, rtc_config->tx_thread_cpu, |
| 665 | rtc_config->xdp_enabled ? rtc_xdp_tx_thread_routine |
| 666 | : rtc_tx_thread_routine, |
| 667 | thread_context); |
| 668 | if (ret) { |
| 669 | fprintf(stderr, "Failed to create Rtc Tx thread!\n"); |
| 670 | goto err_thread_create1; |
| 671 | } |
| 672 | |
| 673 | ret = create_rt_thread(&thread_context->rx_task_id, "RtcRxThread", |
| 674 | rtc_config->rx_thread_priority, rtc_config->rx_thread_cpu, |
| 675 | rtc_config->xdp_enabled ? rtc_xdp_rx_thread_routine |
| 676 | : rtc_rx_thread_routine, |
| 677 | thread_context); |
| 678 | if (ret) { |
| 679 | fprintf(stderr, "Failed to create Rtc Rx thread!\n"); |
| 680 | goto err_thread_create2; |
| 681 | } |
| 682 | |
| 683 | thread_context->meta_data_offset = |
| 684 | get_meta_data_offset(RTC_FRAME_TYPE, rtc_config->security_mode); |
| 685 | |
| 686 | out: |
| 687 | return 0; |
| 688 | |
| 689 | err_thread_create2: |
| 690 | thread_context->stop = 1; |
| 691 | pthread_join(thread_context->tx_task_id, NULL); |
| 692 | err_thread_create1: |
| 693 | security_exit(thread_context->rx_security_context); |
| 694 | err_rx_sec: |
| 695 | security_exit(thread_context->tx_security_context); |
| 696 | err_tx_sec: |
| 697 | ring_buffer_free(thread_context->mirror_buffer); |
| 698 | err_thread: |
| 699 | if (thread_context->socket_fd) |
| 700 | close(thread_context->socket_fd); |
| 701 | if (thread_context->xsk) |
| 702 | xdp_close_socket(thread_context->xsk, rtc_config->interface, |
| 703 | rtc_config->xdp_skb_mode); |
| 704 | err_socket: |
| 705 | free(thread_context->payload_pattern); |
| 706 | err_payload: |
| 707 | free(thread_context->rx_frame_data); |
| 708 | err_rx: |
| 709 | free(thread_context->tx_frame_data); |
| 710 | err_tx: |
| 711 | packet_free(thread_context->packet_context); |
| 712 | err_packet: |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | void rtc_threads_free(struct thread_context *thread_context) |
| 717 | { |
| 718 | struct traffic_class_config *rtc_config; |
| 719 | |
| 720 | if (!thread_context) |
| 721 | return; |
| 722 | |
| 723 | rtc_config = thread_context->conf; |
| 724 | |
| 725 | if (thread_context->payload_pattern) { |
| 726 | thread_context->payload_pattern -= |
| 727 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 728 | free(thread_context->payload_pattern); |
| 729 | } |
| 730 | |
| 731 | security_exit(thread_context->tx_security_context); |
| 732 | security_exit(thread_context->rx_security_context); |
| 733 | |
| 734 | ring_buffer_free(thread_context->mirror_buffer); |
| 735 | |
| 736 | packet_free(thread_context->packet_context); |
| 737 | free(thread_context->tx_frame_data); |
| 738 | free(thread_context->rx_frame_data); |
| 739 | |
| 740 | if (thread_context->socket_fd > 0) |
| 741 | close(thread_context->socket_fd); |
| 742 | |
| 743 | if (thread_context->xsk) |
| 744 | xdp_close_socket(thread_context->xsk, rtc_config->interface, |
| 745 | rtc_config->xdp_skb_mode); |
| 746 | } |
| 747 | |
| 748 | void rtc_threads_wait_for_finish(struct thread_context *thread_context) |
| 749 | { |
| 750 | if (!thread_context) |
| 751 | return; |
| 752 | |
| 753 | if (thread_context->rx_task_id) |
| 754 | pthread_join(thread_context->rx_task_id, NULL); |
| 755 | if (thread_context->tx_task_id) |
| 756 | pthread_join(thread_context->tx_task_id, NULL); |
| 757 | } |