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