| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2021-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <endian.h> |
| 8 | #include <errno.h> |
| 9 | #include <pthread.h> |
| 10 | #include <signal.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | |
| 17 | #include <arpa/inet.h> |
| 18 | |
| 19 | #include <linux/if_ether.h> |
| 20 | #include <linux/if_link.h> |
| 21 | #include <linux/if_packet.h> |
| 22 | #include <linux/if_vlan.h> |
| 23 | |
| 24 | #include "app_config.h" |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "log.h" |
| 28 | #include "net.h" |
| 29 | #include "net_def.h" |
| 30 | #include "packet.h" |
| 31 | #include "security.h" |
| 32 | #include "stat.h" |
| 33 | #include "thread.h" |
| 34 | #include "tsn_thread.h" |
| 35 | #include "tx_time.h" |
| 36 | #include "utils.h" |
| 37 | #include "workload.h" |
| 38 | #include "xdp.h" |
| 39 | |
| 40 | static void tsn_initialize_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 41 | size_t num_frames, const unsigned char *source, |
| 42 | const unsigned char *destination) |
| 43 | { |
| 44 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 45 | size_t i; |
| 46 | |
| 47 | for (i = 0; i < num_frames; ++i) { |
| 48 | unsigned char *frame = frame_idx(frame_data, i); |
| 49 | size_t frame_length = MAX_FRAME_SIZE; |
| 50 | |
| 51 | /* |
| 52 | * In case both AF_XDP and Tx Launch Time or Tx HW Timestamp are enabled the payload |
| 53 | * starts at: frame_data + sizeof(struct xsk_tx_metadata) |
| 54 | */ |
| 55 | #if defined(HAVE_XDP_TX_TIME) || defined(TX_TIMESTAMP) |
| 56 | if (tsn_config->xdp_enabled && |
| 57 | (tsn_config->tx_time_enabled || tsn_config->tx_hwtstamp_enabled)) { |
| 58 | frame += sizeof(struct xsk_tx_metadata); |
| 59 | frame_length -= sizeof(struct xsk_tx_metadata); |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | initialize_profinet_frame(tsn_config->security_mode, frame, frame_length, source, |
| 64 | destination, tsn_config->payload_pattern, |
| 65 | tsn_config->payload_pattern_length, |
| 66 | tsn_config->vid | tsn_config->pcp << VLAN_PCP_SHIFT, |
| 67 | thread_context->frame_id); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static int tsn_send_messages(struct thread_context *thread_context, int socket_fd, |
| 72 | struct sockaddr_ll *destination, unsigned char *frame_data, |
| 73 | size_t num_frames, uint64_t duration) |
| 74 | { |
| 75 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 76 | struct packet_send_request send_req = { |
| 77 | .traffic_class = thread_context->traffic_class, |
| 78 | .socket_fd = socket_fd, |
| 79 | .destination = destination, |
| 80 | .frame_data = frame_data, |
| 81 | .num_frames = num_frames, |
| 82 | .frame_length = tsn_config->frame_length, |
| 83 | .duration = duration, |
| 84 | .tx_time_offset = tsn_config->tx_time_offset_ns, |
| 85 | .meta_data_offset = thread_context->meta_data_offset, |
| 86 | .mirror_enabled = tsn_config->rx_mirror_enabled, |
| 87 | .tx_time_enabled = tsn_config->tx_time_enabled, |
| 88 | }; |
| 89 | |
| 90 | return packet_send_messages(thread_context->packet_context, &send_req); |
| 91 | } |
| 92 | |
| 93 | static int tsn_send_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 94 | size_t num_frames, int socket_fd, struct sockaddr_ll *destination, |
| 95 | uint64_t duration) |
| 96 | { |
| 97 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 98 | int len, i; |
| 99 | |
| 100 | /* Send it */ |
| 101 | len = tsn_send_messages(thread_context, socket_fd, destination, frame_data, num_frames, |
| 102 | duration); |
| 103 | |
| 104 | for (i = 0; i < len; i++) { |
| 105 | uint64_t sequence_counter; |
| 106 | |
| 107 | sequence_counter = get_sequence_counter(frame_data + i * tsn_config->frame_length, |
| 108 | thread_context->meta_data_offset, |
| 109 | tsn_config->num_frames_per_cycle); |
| 110 | |
| 111 | stat_frame_sent(thread_context->frame_type, sequence_counter); |
| 112 | } |
| 113 | |
| 114 | return len; |
| 115 | } |
| 116 | |
| 117 | static int tsn_gen_and_send_frames(struct thread_context *thread_context, int socket_fd, |
| 118 | struct sockaddr_ll *destination, uint64_t wakeup_time, |
| 119 | uint64_t sequence_counter_begin, uint64_t duration) |
| 120 | { |
| 121 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 122 | struct timespec tx_time = {}; |
| 123 | int len, i; |
| 124 | |
| 125 | clock_gettime(app_config.application_clock_id, &tx_time); |
| 126 | |
| 127 | for (i = 0; i < tsn_config->num_frames_per_cycle; i++) { |
| 128 | struct prepare_frame_config frame_config; |
| 129 | int err; |
| 130 | |
| 131 | frame_config.mode = tsn_config->security_mode; |
| 132 | frame_config.security_context = thread_context->tx_security_context; |
| 133 | frame_config.iv_prefix = (const unsigned char *)tsn_config->security_iv_prefix; |
| 134 | frame_config.payload_pattern = thread_context->payload_pattern; |
| 135 | frame_config.payload_pattern_length = thread_context->payload_pattern_length; |
| 136 | frame_config.frame_data = frame_idx(thread_context->tx_frame_data, i); |
| 137 | frame_config.frame_length = tsn_config->frame_length; |
| 138 | frame_config.num_frames_per_cycle = tsn_config->num_frames_per_cycle; |
| 139 | frame_config.sequence_counter = sequence_counter_begin + i; |
| 140 | frame_config.tx_timestamp = ts_to_ns(&tx_time); |
| 141 | frame_config.meta_data_offset = thread_context->meta_data_offset; |
| 142 | |
| 143 | err = prepare_frame_for_tx(&frame_config); |
| 144 | if (err) |
| 145 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to prepare frame for Tx!\n", |
| 146 | thread_context->traffic_class); |
| 147 | } |
| 148 | |
| 149 | /* Send them */ |
| 150 | len = tsn_send_messages(thread_context, socket_fd, destination, |
| 151 | thread_context->tx_frame_data, tsn_config->num_frames_per_cycle, |
| 152 | duration); |
| 153 | |
| 154 | for (i = 0; i < len; i++) |
| 155 | stat_frame_sent(thread_context->frame_type, sequence_counter_begin + i); |
| 156 | |
| 157 | return len; |
| 158 | } |
| 159 | |
| 160 | static void tsn_gen_and_send_xdp_frames(struct thread_context *thread_context, |
| 161 | struct xdp_socket *xsk, uint64_t sequence_counter, |
| 162 | uint64_t wakeup_time, uint64_t duration, |
| 163 | uint32_t *frame_number) |
| 164 | { |
| 165 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 166 | struct xdp_tx_time tx_time = { |
| 167 | .traffic_class = thread_context->traffic_class, |
| 168 | .tx_time_offset = tsn_config->tx_time_offset_ns, |
| 169 | .num_frames_per_cycle = tsn_config->num_frames_per_cycle, |
| 170 | .sequence_counter_begin = sequence_counter, |
| 171 | .duration = duration, |
| 172 | }; |
| 173 | struct xdp_gen_config xdp = { |
| 174 | .mode = tsn_config->security_mode, |
| 175 | .security_context = thread_context->tx_security_context, |
| 176 | .iv_prefix = (const unsigned char *)tsn_config->security_iv_prefix, |
| 177 | .payload_pattern = thread_context->payload_pattern, |
| 178 | .payload_pattern_length = thread_context->payload_pattern_length, |
| 179 | .frame_length = tsn_config->frame_length, |
| 180 | .num_frames_per_cycle = tsn_config->num_frames_per_cycle, |
| 181 | .frame_number = frame_number, |
| 182 | .sequence_counter_begin = sequence_counter, |
| 183 | .meta_data_offset = thread_context->meta_data_offset, |
| 184 | .frame_type = thread_context->frame_type, |
| 185 | .tx_time = tsn_config->tx_time_enabled ? &tx_time : NULL, |
| 186 | }; |
| 187 | |
| 188 | xdp_gen_and_send_frames(xsk, &xdp); |
| 189 | } |
| 190 | |
| 191 | static void *tsn_tx_thread_routine(void *data) |
| 192 | { |
| 193 | struct thread_context *thread_context = data; |
| 194 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 195 | size_t received_frames_length = MAX_FRAME_SIZE * tsn_config->num_frames_per_cycle; |
| 196 | struct security_context *security_context = thread_context->tx_security_context; |
| 197 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 198 | unsigned char *received_frames = thread_context->rx_frame_data; |
| 199 | const bool mirror_enabled = tsn_config->rx_mirror_enabled; |
| 200 | struct sockaddr_ll destination; |
| 201 | unsigned char source[ETH_ALEN]; |
| 202 | uint64_t sequence_counter = 0; |
| 203 | struct timespec wakeup_time; |
| 204 | unsigned int if_index; |
| 205 | uint32_t link_speed; |
| 206 | int ret, socket_fd; |
| 207 | uint64_t duration; |
| 208 | |
| 209 | socket_fd = thread_context->socket_fd; |
| 210 | |
| 211 | ret = get_interface_mac_address(tsn_config->interface, source, ETH_ALEN); |
| 212 | if (ret < 0) { |
| 213 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to get Source MAC address!\n", |
| 214 | thread_context->traffic_class); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | ret = get_interface_link_speed(tsn_config->interface, &link_speed); |
| 219 | if (ret) { |
| 220 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to get link speed!\n", |
| 221 | thread_context->traffic_class); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | if_index = if_nametoindex(tsn_config->interface); |
| 226 | if (!if_index) { |
| 227 | log_message(LOG_LEVEL_ERROR, "%sTx: if_nametoindex() failed!\n", |
| 228 | thread_context->traffic_class); |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | memset(&destination, '\0', sizeof(destination)); |
| 233 | destination.sll_family = PF_PACKET; |
| 234 | destination.sll_ifindex = if_index; |
| 235 | destination.sll_halen = ETH_ALEN; |
| 236 | memcpy(destination.sll_addr, tsn_config->l2_destination, ETH_ALEN); |
| 237 | |
| 238 | duration = tx_time_get_frame_duration(link_speed, tsn_config->frame_length); |
| 239 | |
| 240 | tsn_initialize_frames(thread_context, thread_context->tx_frame_data, |
| 241 | tsn_config->num_frames_per_cycle, source, tsn_config->l2_destination); |
| 242 | |
| 243 | prepare_openssl(security_context); |
| 244 | tsn_initialize_frames(thread_context, thread_context->payload_pattern, 1, source, |
| 245 | tsn_config->l2_destination); |
| 246 | thread_context->payload_pattern += |
| 247 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 248 | thread_context->payload_pattern_length = |
| 249 | tsn_config->frame_length - sizeof(struct vlan_ethernet_header) - |
| 250 | sizeof(struct profinet_secure_header) - sizeof(struct security_checksum); |
| 251 | |
| 252 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 253 | if (ret) { |
| 254 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to calculate thread start time: %s!\n", |
| 255 | thread_context->traffic_class, strerror(errno)); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | while (!thread_context->stop) { |
| 260 | if (!thread_context->is_first) { |
| 261 | struct timespec timeout; |
| 262 | |
| 263 | clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 264 | timeout.tv_sec++; |
| 265 | |
| 266 | pthread_mutex_lock(&thread_context->data_mutex); |
| 267 | ret = pthread_cond_timedwait(&thread_context->data_cond_var, |
| 268 | &thread_context->data_mutex, &timeout); |
| 269 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 270 | |
| 271 | /* In case of shutdown a signal may be missing. */ |
| 272 | if (ret == ETIMEDOUT) |
| 273 | continue; |
| 274 | } else { |
| 275 | /* Wait until next period */ |
| 276 | increment_period(&wakeup_time, cycle_time_ns); |
| 277 | |
| 278 | do { |
| 279 | ret = clock_nanosleep(app_config.application_clock_id, |
| 280 | TIMER_ABSTIME, &wakeup_time, NULL); |
| 281 | } while (ret == EINTR); |
| 282 | |
| 283 | if (ret) { |
| 284 | log_message(LOG_LEVEL_ERROR, "%sTx: clock_nanosleep() failed: %s\n", |
| 285 | thread_context->traffic_class, strerror(ret)); |
| 286 | return NULL; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Send TsnFrames, two possibilites: |
| 292 | * a) Generate it, or |
| 293 | * b) Use received ones if mirror enabled |
| 294 | */ |
| 295 | if (!mirror_enabled) { |
| 296 | tsn_gen_and_send_frames(thread_context, socket_fd, &destination, |
| 297 | ts_to_ns(&wakeup_time), sequence_counter, duration); |
| 298 | |
| 299 | sequence_counter += tsn_config->num_frames_per_cycle; |
| 300 | } else { |
| 301 | size_t len, num_frames; |
| 302 | |
| 303 | ring_buffer_fetch(thread_context->mirror_buffer, received_frames, |
| 304 | received_frames_length, &len); |
| 305 | |
| 306 | /* Len should be a multiple of frame size */ |
| 307 | num_frames = len / tsn_config->frame_length; |
| 308 | tsn_send_frames(thread_context, received_frames, num_frames, socket_fd, |
| 309 | &destination, duration); |
| 310 | } |
| 311 | |
| 312 | /* Signal next Tx thread */ |
| 313 | if (thread_context->next) { |
| 314 | pthread_mutex_lock(&thread_context->next->data_mutex); |
| 315 | pthread_cond_signal(&thread_context->next->data_cond_var); |
| 316 | pthread_mutex_unlock(&thread_context->next->data_mutex); |
| 317 | } |
| 318 | |
| 319 | if (thread_context->is_last) |
| 320 | stat_update(); |
| 321 | } |
| 322 | |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * This Tx thread routine differs to the standard one in terms of the sending |
| 328 | * interface. This one uses the AF_XDP user space interface. |
| 329 | */ |
| 330 | static void *tsn_xdp_tx_thread_routine(void *data) |
| 331 | { |
| 332 | struct thread_context *thread_context = data; |
| 333 | struct security_context *security_context = thread_context->tx_security_context; |
| 334 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 335 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 336 | const bool mirror_enabled = tsn_config->rx_mirror_enabled; |
| 337 | uint32_t frame_number = XSK_RING_PROD__DEFAULT_NUM_DESCS; |
| 338 | size_t num_frames = tsn_config->num_frames_per_cycle; |
| 339 | unsigned char source[ETH_ALEN]; |
| 340 | uint64_t sequence_counter = 0; |
| 341 | struct timespec wakeup_time; |
| 342 | unsigned char *frame_data; |
| 343 | struct xdp_socket *xsk; |
| 344 | uint32_t link_speed; |
| 345 | uint64_t duration; |
| 346 | int ret; |
| 347 | |
| 348 | xsk = thread_context->xsk; |
| 349 | xsk->tx_hwts.rtt = &round_trip_contexts[thread_context->frame_type]; |
| 350 | xsk->tx_hwts.frames_per_cycle = tsn_config->num_frames_per_cycle; |
| 351 | |
| 352 | ret = get_interface_mac_address(tsn_config->interface, source, ETH_ALEN); |
| 353 | if (ret < 0) { |
| 354 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to get Source MAC address!\n", |
| 355 | thread_context->traffic_class); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | ret = get_interface_link_speed(tsn_config->interface, &link_speed); |
| 360 | if (ret) { |
| 361 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to get link speed!\n", |
| 362 | thread_context->traffic_class); |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | duration = tx_time_get_frame_duration(link_speed, tsn_config->frame_length); |
| 367 | |
| 368 | /* First half of umem area is for Rx, the second half is for Tx. */ |
| 369 | frame_data = xsk_umem__get_data(xsk->umem.buffer, |
| 370 | XDP_FRAME_SIZE * XSK_RING_PROD__DEFAULT_NUM_DESCS); |
| 371 | |
| 372 | /* Initialize all Tx frames */ |
| 373 | tsn_initialize_frames(thread_context, frame_data, XSK_RING_CONS__DEFAULT_NUM_DESCS, source, |
| 374 | tsn_config->l2_destination); |
| 375 | |
| 376 | prepare_openssl(security_context); |
| 377 | tsn_initialize_frames(thread_context, thread_context->payload_pattern, 1, source, |
| 378 | tsn_config->l2_destination); |
| 379 | thread_context->payload_pattern += |
| 380 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 381 | thread_context->payload_pattern_length = |
| 382 | tsn_config->frame_length - sizeof(struct vlan_ethernet_header) - |
| 383 | sizeof(struct profinet_secure_header) - sizeof(struct security_checksum); |
| 384 | |
| 385 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 386 | if (ret) { |
| 387 | log_message(LOG_LEVEL_ERROR, "%sTx: Failed to calculate thread start time: %s!\n", |
| 388 | thread_context->traffic_class, strerror(errno)); |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | while (!thread_context->stop) { |
| 393 | if (!thread_context->is_first) { |
| 394 | struct timespec timeout; |
| 395 | |
| 396 | clock_gettime(CLOCK_MONOTONIC, &timeout); |
| 397 | timeout.tv_sec++; |
| 398 | |
| 399 | pthread_mutex_lock(&thread_context->data_mutex); |
| 400 | ret = pthread_cond_timedwait(&thread_context->data_cond_var, |
| 401 | &thread_context->data_mutex, &timeout); |
| 402 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 403 | |
| 404 | /* In case of shutdown a signal may be missing. */ |
| 405 | if (ret == ETIMEDOUT) |
| 406 | continue; |
| 407 | } else { |
| 408 | /* Wait until next period */ |
| 409 | increment_period(&wakeup_time, cycle_time_ns); |
| 410 | |
| 411 | do { |
| 412 | ret = clock_nanosleep(app_config.application_clock_id, |
| 413 | TIMER_ABSTIME, &wakeup_time, NULL); |
| 414 | } while (ret == EINTR); |
| 415 | |
| 416 | if (ret) { |
| 417 | log_message(LOG_LEVEL_ERROR, "%sTx: clock_nanosleep() failed: %s\n", |
| 418 | thread_context->traffic_class, strerror(ret)); |
| 419 | return NULL; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Send TsnFrames, two possibilites: |
| 425 | * a) Generate it, or |
| 426 | * b) Use received ones if mirror enabled |
| 427 | */ |
| 428 | if (!mirror_enabled) { |
| 429 | tsn_gen_and_send_xdp_frames(thread_context, xsk, sequence_counter, |
| 430 | ts_to_ns(&wakeup_time), duration, |
| 431 | &frame_number); |
| 432 | sequence_counter += num_frames; |
| 433 | } else { |
| 434 | unsigned int received; |
| 435 | uint64_t i; |
| 436 | |
| 437 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 438 | |
| 439 | received = thread_context->received_frames; |
| 440 | |
| 441 | sequence_counter = thread_context->rx_sequence_counter - received; |
| 442 | |
| 443 | /* Increment workload outlier count if workload did not finish. */ |
| 444 | if (tsn_config->rx_workload_enabled) { |
| 445 | if (thread_context->workload->workload_done == 0 && |
| 446 | sequence_counter) { |
| 447 | stat_inc_workload_outlier(thread_context->frame_type); |
| 448 | log_message(LOG_LEVEL_DEBUG, "Workload did not finish!\n"); |
| 449 | } else |
| 450 | thread_context->workload->workload_done = 0; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * The XDP receiver stored the frames within the umem area and populated the |
| 455 | * Tx ring. Now, the Tx ring can be committed to the kernel. Furthermore, |
| 456 | * already transmitted frames from last cycle can be recycled for Rx. |
| 457 | */ |
| 458 | |
| 459 | xsk_ring_prod__submit(&xsk->tx, received); |
| 460 | |
| 461 | if (received > 0) { |
| 462 | if (tsn_config->tx_hwtstamp_enabled) { |
| 463 | /* |
| 464 | * Once-per-cycle: record TX SW timestamp for the first |
| 465 | * packet in this cycle and count all frames |
| 466 | */ |
| 467 | stat_frames_sent_batch(thread_context->frame_type, |
| 468 | sequence_counter, received); |
| 469 | } else { |
| 470 | for (i = sequence_counter; i < sequence_counter + received; |
| 471 | ++i) |
| 472 | stat_frame_sent(thread_context->frame_type, i); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | xsk->outstanding_tx += received; |
| 477 | thread_context->received_frames = 0; |
| 478 | |
| 479 | xdp_complete_tx(xsk); |
| 480 | |
| 481 | #ifdef TX_TIMESTAMP |
| 482 | if (received > 0 && tsn_config->tx_hwtstamp_enabled) { |
| 483 | /* |
| 484 | * TX HW timestamp becomes available in the next cycle |
| 485 | * after the packet is transmitted. |
| 486 | * This is one cycle later than SW timestamp tracked |
| 487 | * using sequence_counter |
| 488 | */ |
| 489 | xsk->tx_hwts.seq_lagged = sequence_counter; |
| 490 | } |
| 491 | #endif |
| 492 | |
| 493 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 494 | } |
| 495 | |
| 496 | /* Signal next Tx thread */ |
| 497 | if (thread_context->next) { |
| 498 | pthread_mutex_lock(&thread_context->next->data_mutex); |
| 499 | pthread_cond_signal(&thread_context->next->data_cond_var); |
| 500 | pthread_mutex_unlock(&thread_context->next->data_mutex); |
| 501 | } |
| 502 | |
| 503 | if (thread_context->is_last) |
| 504 | stat_update(); |
| 505 | } |
| 506 | |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | static void *tsn_rx_thread_routine(void *data) |
| 511 | { |
| 512 | struct thread_context *thread_context = data; |
| 513 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 514 | struct timespec wakeup_time; |
| 515 | int socket_fd, ret; |
| 516 | |
| 517 | socket_fd = thread_context->socket_fd; |
| 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, "%sRx: Failed to calculate thread start time: %s!\n", |
| 524 | thread_context->traffic_class, strerror(errno)); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | while (!thread_context->stop) { |
| 529 | struct packet_receive_request recv_req = { |
| 530 | .traffic_class = thread_context->traffic_class, |
| 531 | .socket_fd = socket_fd, |
| 532 | .receive_function = receive_profinet_frame, |
| 533 | .data = thread_context, |
| 534 | }; |
| 535 | |
| 536 | /* Wait until next period. */ |
| 537 | increment_period(&wakeup_time, cycle_time_ns); |
| 538 | |
| 539 | do { |
| 540 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 541 | &wakeup_time, NULL); |
| 542 | } while (ret == EINTR); |
| 543 | |
| 544 | if (ret) { |
| 545 | log_message(LOG_LEVEL_ERROR, "%sRx: clock_nanosleep() failed: %s\n", |
| 546 | thread_context->traffic_class, strerror(ret)); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | /* Receive Tsn frames. */ |
| 551 | packet_receive_messages(thread_context->packet_context, &recv_req); |
| 552 | } |
| 553 | |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | static void *tsn_xdp_rx_thread_routine(void *data) |
| 558 | { |
| 559 | struct thread_context *thread_context = data; |
| 560 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 561 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 562 | const bool mirror_enabled = tsn_config->rx_mirror_enabled; |
| 563 | const size_t frame_length = tsn_config->frame_length; |
| 564 | struct xdp_socket *xsk = thread_context->xsk; |
| 565 | struct timespec wakeup_time; |
| 566 | uint32_t link_speed; |
| 567 | uint64_t duration; |
| 568 | int ret; |
| 569 | |
| 570 | prepare_openssl(thread_context->rx_security_context); |
| 571 | |
| 572 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 573 | if (ret) { |
| 574 | log_message(LOG_LEVEL_ERROR, "%sRx: Failed to calculate thread start time: %s!\n", |
| 575 | thread_context->traffic_class, strerror(errno)); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | ret = get_interface_link_speed(tsn_config->interface, &link_speed); |
| 580 | if (ret) { |
| 581 | log_message(LOG_LEVEL_ERROR, "%sRx: Failed to get link speed!\n", |
| 582 | thread_context->traffic_class); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | duration = tx_time_get_frame_duration(link_speed, tsn_config->frame_length); |
| 587 | |
| 588 | while (!thread_context->stop) { |
| 589 | struct xdp_tx_time tx_time = { |
| 590 | .tx_time_offset = tsn_config->tx_time_offset_ns, |
| 591 | .duration = duration, |
| 592 | .num_frames_per_cycle = tsn_config->num_frames_per_cycle, |
| 593 | .sequence_counter_begin = 0, |
| 594 | .traffic_class = thread_context->traffic_class, |
| 595 | }; |
| 596 | unsigned int received; |
| 597 | |
| 598 | /* Wait until next period */ |
| 599 | increment_period(&wakeup_time, cycle_time_ns); |
| 600 | |
| 601 | do { |
| 602 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 603 | &wakeup_time, NULL); |
| 604 | } while (ret == EINTR); |
| 605 | |
| 606 | if (ret) { |
| 607 | log_message(LOG_LEVEL_ERROR, "%sRx: clock_nanosleep() failed: %s\n", |
| 608 | thread_context->traffic_class, strerror(ret)); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 613 | received = xdp_receive_frames(xsk, frame_length, mirror_enabled, |
| 614 | receive_profinet_frame, thread_context, &tx_time); |
| 615 | thread_context->received_frames = received; |
| 616 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 617 | |
| 618 | if (thread_context->conf->rx_workload_enabled && mirror_enabled) { |
| 619 | /* Run workload if we received frames or prewarm is enabled */ |
| 620 | if (received || thread_context->conf->rx_workload_prewarm) { |
| 621 | thread_context->workload->workload_run = 1; |
| 622 | pthread_cond_signal(&(thread_context->workload->workload_cond)); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | int tsn_threads_create(struct thread_context *thread_context) |
| 631 | { |
| 632 | const struct traffic_class_config *tsn_config = thread_context->conf; |
| 633 | char thread_name[128]; |
| 634 | int ret; |
| 635 | |
| 636 | if (thread_context->frame_type == TSN_HIGH_FRAME_TYPE && |
| 637 | !config_is_traffic_class_active("TsnHigh")) { |
| 638 | ret = 0; |
| 639 | goto out; |
| 640 | } |
| 641 | if (thread_context->frame_type == TSN_LOW_FRAME_TYPE && |
| 642 | !config_is_traffic_class_active("TsnLow")) { |
| 643 | ret = 0; |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | /* For XDP the frames are stored in a umem area. That memory is part of the socket. */ |
| 648 | if (!tsn_config->xdp_enabled) { |
| 649 | thread_context->packet_context = packet_init(tsn_config->num_frames_per_cycle); |
| 650 | if (!thread_context->packet_context) { |
| 651 | fprintf(stderr, "Failed to allocate Tsn packet context!\n"); |
| 652 | ret = -ENOMEM; |
| 653 | goto err_packet; |
| 654 | } |
| 655 | |
| 656 | thread_context->tx_frame_data = |
| 657 | calloc(tsn_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 658 | if (!thread_context->tx_frame_data) { |
| 659 | fprintf(stderr, "Failed to allocate TsnTxFrameData!\n"); |
| 660 | ret = -ENOMEM; |
| 661 | goto err_tx; |
| 662 | } |
| 663 | |
| 664 | thread_context->rx_frame_data = |
| 665 | calloc(tsn_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 666 | if (!thread_context->rx_frame_data) { |
| 667 | fprintf(stderr, "Failed to allocate TsnRxFrameData!\n"); |
| 668 | ret = -ENOMEM; |
| 669 | goto err_rx; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | thread_context->payload_pattern = calloc(1, MAX_FRAME_SIZE); |
| 674 | if (!thread_context->payload_pattern) { |
| 675 | fprintf(stderr, "Failed to allocate TsnPayloadPattern!\n"); |
| 676 | ret = -ENOMEM; |
| 677 | goto err_payload; |
| 678 | } |
| 679 | thread_context->payload_pattern_length = MAX_FRAME_SIZE; |
| 680 | |
| 681 | /* For XDP a AF_XDP socket is allocated. Otherwise a Linux raw socket is used. */ |
| 682 | if (tsn_config->xdp_enabled) { |
| 683 | thread_context->socket_fd = 0; |
| 684 | thread_context->xsk = xdp_open_socket( |
| 685 | tsn_config->interface, app_config.application_xdp_program, |
| 686 | tsn_config->rx_queue, tsn_config->xdp_skb_mode, tsn_config->xdp_zc_mode, |
| 687 | tsn_config->xdp_wakeup_mode, tsn_config->xdp_busy_poll_mode, |
| 688 | tsn_config->tx_time_enabled, tsn_config->tx_hwtstamp_enabled); |
| 689 | if (!thread_context->xsk) { |
| 690 | fprintf(stderr, "Failed to create Tsn Xdp socket!\n"); |
| 691 | ret = -ENOMEM; |
| 692 | goto err_socket; |
| 693 | } |
| 694 | } else { |
| 695 | thread_context->xsk = NULL; |
| 696 | thread_context->socket_fd = thread_context->create_socket(); |
| 697 | if (thread_context->socket_fd < 0) { |
| 698 | fprintf(stderr, "Failed to create TSN Socket!\n"); |
| 699 | ret = -errno; |
| 700 | goto err_socket; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | init_mutex(&thread_context->data_mutex); |
| 705 | init_mutex(&thread_context->xdp_data_mutex); |
| 706 | init_condition_variable(&thread_context->data_cond_var); |
| 707 | |
| 708 | /* Same as above. For XDP the umem area is used. */ |
| 709 | if (tsn_config->rx_mirror_enabled && !tsn_config->xdp_enabled) { |
| 710 | /* Per period the expectation is: TsnNumFramesPerCycle * MAX_FRAME */ |
| 711 | thread_context->mirror_buffer = |
| 712 | ring_buffer_allocate(MAX_FRAME_SIZE * tsn_config->num_frames_per_cycle); |
| 713 | if (!thread_context->mirror_buffer) { |
| 714 | fprintf(stderr, "Failed to allocate Tsn Mirror RingBuffer!\n"); |
| 715 | ret = -ENOMEM; |
| 716 | goto err_buffer; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (tsn_config->security_mode != SECURITY_MODE_NONE) { |
| 721 | thread_context->tx_security_context = security_init( |
| 722 | tsn_config->security_algorithm, (unsigned char *)tsn_config->security_key); |
| 723 | if (!thread_context->tx_security_context) { |
| 724 | fprintf(stderr, "Failed to initialize Tx security context!\n"); |
| 725 | ret = -ENOMEM; |
| 726 | goto err_tx_sec; |
| 727 | } |
| 728 | |
| 729 | thread_context->rx_security_context = security_init( |
| 730 | tsn_config->security_algorithm, (unsigned char *)tsn_config->security_key); |
| 731 | if (!thread_context->rx_security_context) { |
| 732 | fprintf(stderr, "Failed to initialize Rx security context!\n"); |
| 733 | ret = -ENOMEM; |
| 734 | goto err_rx_sec; |
| 735 | } |
| 736 | } else { |
| 737 | thread_context->tx_security_context = NULL; |
| 738 | thread_context->rx_security_context = NULL; |
| 739 | } |
| 740 | |
| 741 | snprintf(thread_name, sizeof(thread_name), "%sTxThread", thread_context->traffic_class); |
| 742 | |
| 743 | ret = create_rt_thread(&thread_context->tx_task_id, thread_name, |
| 744 | tsn_config->tx_thread_priority, tsn_config->tx_thread_cpu, |
| 745 | tsn_config->xdp_enabled ? tsn_xdp_tx_thread_routine |
| 746 | : tsn_tx_thread_routine, |
| 747 | thread_context); |
| 748 | if (ret) { |
| 749 | fprintf(stderr, "Failed to create Tsn Tx Thread!\n"); |
| 750 | goto err_thread; |
| 751 | } |
| 752 | |
| 753 | snprintf(thread_name, sizeof(thread_name), "%sRxThread", thread_context->traffic_class); |
| 754 | |
| 755 | ret = create_rt_thread(&thread_context->rx_task_id, thread_name, |
| 756 | tsn_config->rx_thread_priority, tsn_config->rx_thread_cpu, |
| 757 | tsn_config->xdp_enabled ? tsn_xdp_rx_thread_routine |
| 758 | : tsn_rx_thread_routine, |
| 759 | thread_context); |
| 760 | if (ret) { |
| 761 | fprintf(stderr, "Failed to create Tsn Rx Thread!\n"); |
| 762 | goto err_thread_rx; |
| 763 | } |
| 764 | |
| 765 | /* Create workload thread for execution after network RX */ |
| 766 | if (tsn_config->rx_workload_enabled) { |
| 767 | thread_context->workload = calloc(1, sizeof(struct workload_config)); |
| 768 | |
| 769 | if (!thread_context->workload) { |
| 770 | fprintf(stderr, "Failed to allocate workload!\n"); |
| 771 | ret = -ENOMEM; |
| 772 | goto err_thread_wl; |
| 773 | } |
| 774 | |
| 775 | workload_context_init( |
| 776 | thread_context, tsn_config->workload_file, tsn_config->workload_function, |
| 777 | tsn_config->workload_arguments, tsn_config->workload_setup_function, |
| 778 | tsn_config->workload_setup_arguments, thread_context->frame_type); |
| 779 | ret = create_rt_thread( |
| 780 | &thread_context->workload->workload_task_id, tsn_config->workload_function, |
| 781 | tsn_config->workload_thread_priority, tsn_config->workload_thread_cpu, |
| 782 | &workload_thread_routine, thread_context); |
| 783 | if (ret) { |
| 784 | fprintf(stderr, "Failed to create Tsn Workload Thread!\n"); |
| 785 | goto err_thread_wl; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | thread_context->meta_data_offset = |
| 790 | get_meta_data_offset(thread_context->frame_type, tsn_config->security_mode); |
| 791 | |
| 792 | return 0; |
| 793 | |
| 794 | err_thread_wl: |
| 795 | thread_context->stop = 1; |
| 796 | free(thread_context->workload); |
| 797 | pthread_join(thread_context->rx_task_id, NULL); |
| 798 | err_thread_rx: |
| 799 | thread_context->stop = 1; |
| 800 | pthread_join(thread_context->tx_task_id, NULL); |
| 801 | err_thread: |
| 802 | security_exit(thread_context->rx_security_context); |
| 803 | err_rx_sec: |
| 804 | security_exit(thread_context->tx_security_context); |
| 805 | err_tx_sec: |
| 806 | ring_buffer_free(thread_context->mirror_buffer); |
| 807 | err_buffer: |
| 808 | if (thread_context->socket_fd) |
| 809 | close(thread_context->socket_fd); |
| 810 | if (thread_context->xsk) |
| 811 | xdp_close_socket(thread_context->xsk, tsn_config->interface, |
| 812 | tsn_config->xdp_skb_mode); |
| 813 | err_socket: |
| 814 | free(thread_context->payload_pattern); |
| 815 | err_payload: |
| 816 | free(thread_context->rx_frame_data); |
| 817 | err_rx: |
| 818 | free(thread_context->tx_frame_data); |
| 819 | err_tx: |
| 820 | packet_free(thread_context->packet_context); |
| 821 | err_packet: |
| 822 | out: |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | static void tsn_threads_free(struct thread_context *thread_context) |
| 827 | { |
| 828 | const struct traffic_class_config *tsn_config; |
| 829 | |
| 830 | if (!thread_context) |
| 831 | return; |
| 832 | |
| 833 | tsn_config = thread_context->conf; |
| 834 | |
| 835 | if (thread_context->payload_pattern) { |
| 836 | thread_context->payload_pattern -= |
| 837 | sizeof(struct vlan_ethernet_header) + sizeof(struct profinet_secure_header); |
| 838 | free(thread_context->payload_pattern); |
| 839 | } |
| 840 | |
| 841 | security_exit(thread_context->tx_security_context); |
| 842 | security_exit(thread_context->rx_security_context); |
| 843 | |
| 844 | ring_buffer_free(thread_context->mirror_buffer); |
| 845 | |
| 846 | packet_free(thread_context->packet_context); |
| 847 | free(thread_context->tx_frame_data); |
| 848 | free(thread_context->rx_frame_data); |
| 849 | |
| 850 | if (thread_context->socket_fd > 0) |
| 851 | close(thread_context->socket_fd); |
| 852 | |
| 853 | if (thread_context->xsk) |
| 854 | xdp_close_socket(thread_context->xsk, tsn_config->interface, |
| 855 | tsn_config->xdp_skb_mode); |
| 856 | |
| 857 | if (thread_context->workload != NULL && thread_context->workload->workload_task_id) |
| 858 | workload_thread_free(thread_context); |
| 859 | } |
| 860 | |
| 861 | static void tsn_threads_wait_for_finish(struct thread_context *thread_context) |
| 862 | { |
| 863 | if (!thread_context) |
| 864 | return; |
| 865 | |
| 866 | if (thread_context->workload != NULL && thread_context->workload->workload_task_id) |
| 867 | pthread_join(thread_context->workload->workload_task_id, NULL); |
| 868 | if (thread_context->rx_task_id) |
| 869 | pthread_join(thread_context->rx_task_id, NULL); |
| 870 | if (thread_context->tx_task_id) |
| 871 | pthread_join(thread_context->tx_task_id, NULL); |
| 872 | } |
| 873 | |
| 874 | int tsn_low_threads_create(struct thread_context *tsn_thread_context) |
| 875 | { |
| 876 | tsn_thread_context->conf = &app_config.classes[TSN_LOW_FRAME_TYPE]; |
| 877 | tsn_thread_context->frame_type = TSN_LOW_FRAME_TYPE; |
| 878 | tsn_thread_context->traffic_class = stat_frame_type_to_string(TSN_LOW_FRAME_TYPE); |
| 879 | tsn_thread_context->create_socket = create_tsn_low_socket; |
| 880 | tsn_thread_context->frame_id = |
| 881 | app_config.classes[TSN_LOW_FRAME_TYPE].security_mode == SECURITY_MODE_NONE |
| 882 | ? TSN_LOW_FRAMEID |
| 883 | : TSN_LOW_SEC_FRAMEID; |
| 884 | |
| 885 | return tsn_threads_create(tsn_thread_context); |
| 886 | } |
| 887 | |
| 888 | void tsn_low_threads_free(struct thread_context *thread_context) |
| 889 | { |
| 890 | tsn_threads_free(thread_context); |
| 891 | } |
| 892 | |
| 893 | void tsn_low_threads_wait_for_finish(struct thread_context *thread_context) |
| 894 | { |
| 895 | tsn_threads_wait_for_finish(thread_context); |
| 896 | } |
| 897 | |
| 898 | int tsn_high_threads_create(struct thread_context *tsn_thread_context) |
| 899 | { |
| 900 | tsn_thread_context->conf = &app_config.classes[TSN_HIGH_FRAME_TYPE]; |
| 901 | tsn_thread_context->frame_type = TSN_HIGH_FRAME_TYPE; |
| 902 | tsn_thread_context->traffic_class = stat_frame_type_to_string(TSN_HIGH_FRAME_TYPE); |
| 903 | tsn_thread_context->create_socket = create_tsn_high_socket; |
| 904 | tsn_thread_context->frame_id = |
| 905 | app_config.classes[TSN_HIGH_FRAME_TYPE].security_mode == SECURITY_MODE_NONE |
| 906 | ? TSN_HIGH_FRAMEID |
| 907 | : TSN_HIGH_SEC_FRAMEID; |
| 908 | |
| 909 | return tsn_threads_create(tsn_thread_context); |
| 910 | } |
| 911 | |
| 912 | void tsn_high_threads_free(struct thread_context *thread_context) |
| 913 | { |
| 914 | tsn_threads_free(thread_context); |
| 915 | } |
| 916 | |
| 917 | void tsn_high_threads_wait_for_finish(struct thread_context *thread_context) |
| 918 | { |
| 919 | tsn_threads_wait_for_finish(thread_context); |
| 920 | } |