| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2022-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 <sys/socket.h> |
| 23 | |
| 24 | #include "app_config.h" |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "layer2_thread.h" |
| 28 | #include "log.h" |
| 29 | #include "net.h" |
| 30 | #include "packet.h" |
| 31 | #include "security.h" |
| 32 | #include "stat.h" |
| 33 | #include "thread.h" |
| 34 | #include "tx_time.h" |
| 35 | #include "utils.h" |
| 36 | |
| 37 | static void generic_l2_initialize_frame(struct thread_context *thread_context, |
| 38 | unsigned char *frame_data, const unsigned char *source, |
| 39 | const unsigned char *destination) |
| 40 | { |
| 41 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 42 | struct vlan_ethernet_header *eth; |
| 43 | struct generic_l2_header *l2; |
| 44 | size_t payload_offset; |
| 45 | |
| 46 | /* |
| 47 | * GenericL2Frame: |
| 48 | * Destination |
| 49 | * Source |
| 50 | * VLAN tag |
| 51 | * Ether type |
| 52 | * Cycle counter |
| 53 | * Payload |
| 54 | * Padding to maxFrame |
| 55 | * |
| 56 | * In case both AF_XDP and Tx Launch Time or Tx HW Timestamp are enabled the payload starts |
| 57 | * at: frame_data + sizeof(struct xsk_tx_metadata) |
| 58 | */ |
| 59 | |
| 60 | #if defined(HAVE_XDP_TX_TIME) || defined(TX_TIMESTAMP) |
| 61 | if (l2_config->xdp_enabled && |
| 62 | (l2_config->tx_time_enabled || l2_config->tx_hwtstamp_enabled)) |
| 63 | frame_data += sizeof(struct xsk_tx_metadata); |
| 64 | #endif |
| 65 | |
| 66 | eth = (struct vlan_ethernet_header *)frame_data; |
| 67 | l2 = (struct generic_l2_header *)(frame_data + sizeof(*eth)); |
| 68 | |
| 69 | /* Ethernet header */ |
| 70 | memcpy(eth->destination, destination, ETH_ALEN); |
| 71 | memcpy(eth->source, source, ETH_ALEN); |
| 72 | |
| 73 | /* VLAN Header */ |
| 74 | eth->vlan_proto = htons(ETH_P_8021Q); |
| 75 | eth->vlantci = htons(l2_config->vid | l2_config->pcp << VLAN_PCP_SHIFT); |
| 76 | eth->vlan_encapsulated_proto = htons(l2_config->ether_type); |
| 77 | |
| 78 | /* Generic L2 header */ |
| 79 | l2->meta_data.frame_counter = 0; |
| 80 | l2->meta_data.cycle_counter = 0; |
| 81 | |
| 82 | /* Payload */ |
| 83 | payload_offset = sizeof(*eth) + sizeof(*l2); |
| 84 | memcpy(frame_data + payload_offset, l2_config->payload_pattern, |
| 85 | l2_config->payload_pattern_length); |
| 86 | |
| 87 | /* Padding: '\0' */ |
| 88 | } |
| 89 | |
| 90 | static void generic_l2_initialize_frames(struct thread_context *thread_context, |
| 91 | unsigned char *frame_data, size_t num_frames, |
| 92 | const unsigned char *source, |
| 93 | const unsigned char *destination) |
| 94 | { |
| 95 | size_t i; |
| 96 | |
| 97 | for (i = 0; i < num_frames; ++i) |
| 98 | generic_l2_initialize_frame(thread_context, frame_idx(frame_data, i), source, |
| 99 | destination); |
| 100 | } |
| 101 | |
| 102 | static int generic_l2_send_messages(struct thread_context *thread_context, int socket_fd, |
| 103 | struct sockaddr_ll *destination, unsigned char *frame_data, |
| 104 | size_t num_frames, uint64_t duration) |
| 105 | { |
| 106 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 107 | struct packet_send_request send_req = { |
| 108 | .traffic_class = thread_context->traffic_class, |
| 109 | .socket_fd = socket_fd, |
| 110 | .destination = destination, |
| 111 | .frame_data = frame_data, |
| 112 | .num_frames = num_frames, |
| 113 | .frame_length = l2_config->frame_length, |
| 114 | .duration = duration, |
| 115 | .tx_time_offset = l2_config->tx_time_offset_ns, |
| 116 | .meta_data_offset = thread_context->meta_data_offset, |
| 117 | .mirror_enabled = l2_config->rx_mirror_enabled, |
| 118 | .tx_time_enabled = l2_config->tx_time_enabled, |
| 119 | }; |
| 120 | |
| 121 | return packet_send_messages(thread_context->packet_context, &send_req); |
| 122 | } |
| 123 | |
| 124 | static int generic_l2_send_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 125 | size_t num_frames, int socket_fd, struct sockaddr_ll *destination, |
| 126 | uint64_t duration) |
| 127 | { |
| 128 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 129 | size_t frame_length; |
| 130 | int len, i; |
| 131 | |
| 132 | /* Send it */ |
| 133 | frame_length = l2_config->frame_length; |
| 134 | len = generic_l2_send_messages(thread_context, socket_fd, destination, frame_data, |
| 135 | num_frames, duration); |
| 136 | |
| 137 | for (i = 0; i < len; i++) { |
| 138 | uint64_t sequence_counter; |
| 139 | |
| 140 | sequence_counter = get_sequence_counter(frame_data + i * frame_length, |
| 141 | thread_context->meta_data_offset, |
| 142 | l2_config->num_frames_per_cycle); |
| 143 | stat_frame_sent(GENERICL2_FRAME_TYPE, sequence_counter); |
| 144 | } |
| 145 | |
| 146 | return len; |
| 147 | } |
| 148 | |
| 149 | static int generic_l2_gen_and_send_frames(struct thread_context *thread_context, |
| 150 | size_t num_frames_per_cycle, int socket_fd, |
| 151 | struct sockaddr_ll *destination, |
| 152 | uint64_t sequence_counter_begin, uint64_t duration) |
| 153 | { |
| 154 | struct vlan_ethernet_header *eth; |
| 155 | struct generic_l2_header *l2; |
| 156 | struct timespec tx_time = {}; |
| 157 | int len, i; |
| 158 | |
| 159 | clock_gettime(app_config.application_clock_id, &tx_time); |
| 160 | |
| 161 | /* Adjust meta data */ |
| 162 | for (i = 0; i < num_frames_per_cycle; i++) { |
| 163 | l2 = (struct generic_l2_header *)(frame_idx(thread_context->tx_frame_data, i) + |
| 164 | sizeof(*eth)); |
| 165 | sequence_counter_to_meta_data(&l2->meta_data, sequence_counter_begin + i, |
| 166 | num_frames_per_cycle); |
| 167 | |
| 168 | tx_timestamp_to_meta_data(&l2->meta_data, ts_to_ns(&tx_time)); |
| 169 | } |
| 170 | |
| 171 | /* Send them */ |
| 172 | len = generic_l2_send_messages(thread_context, socket_fd, destination, |
| 173 | thread_context->tx_frame_data, num_frames_per_cycle, |
| 174 | duration); |
| 175 | |
| 176 | for (i = 0; i < len; i++) |
| 177 | stat_frame_sent(GENERICL2_FRAME_TYPE, sequence_counter_begin + i); |
| 178 | |
| 179 | return len; |
| 180 | } |
| 181 | |
| 182 | static void generic_l2_gen_and_send_xdp_frames(struct thread_context *thread_context, |
| 183 | size_t num_frames_per_cycle, |
| 184 | uint64_t sequence_counter, uint64_t wakeup_time, |
| 185 | uint64_t duration, uint32_t *frame_number) |
| 186 | { |
| 187 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 188 | struct xdp_tx_time tx_time = { |
| 189 | .traffic_class = thread_context->traffic_class, |
| 190 | .tx_time_offset = l2_config->tx_time_offset_ns, |
| 191 | .num_frames_per_cycle = num_frames_per_cycle, |
| 192 | .sequence_counter_begin = sequence_counter, |
| 193 | .duration = duration, |
| 194 | }; |
| 195 | struct xdp_gen_config xdp = { |
| 196 | .mode = SECURITY_MODE_NONE, |
| 197 | .security_context = NULL, |
| 198 | .iv_prefix = NULL, |
| 199 | .payload_pattern = NULL, |
| 200 | .payload_pattern_length = 0, |
| 201 | .frame_length = l2_config->frame_length, |
| 202 | .num_frames_per_cycle = num_frames_per_cycle, |
| 203 | .frame_number = frame_number, |
| 204 | .sequence_counter_begin = sequence_counter, |
| 205 | .meta_data_offset = thread_context->meta_data_offset, |
| 206 | .frame_type = GENERICL2_FRAME_TYPE, |
| 207 | .tx_time = l2_config->tx_time_enabled ? &tx_time : NULL, |
| 208 | }; |
| 209 | |
| 210 | xdp_gen_and_send_frames(thread_context->xsk, &xdp); |
| 211 | } |
| 212 | |
| 213 | static void *generic_l2_tx_thread_routine(void *data) |
| 214 | { |
| 215 | struct thread_context *thread_context = data; |
| 216 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 217 | size_t received_frames_length = MAX_FRAME_SIZE * l2_config->num_frames_per_cycle; |
| 218 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 219 | const bool mirror_enabled = l2_config->rx_mirror_enabled; |
| 220 | unsigned char *received_frames = thread_context->rx_frame_data; |
| 221 | struct sockaddr_ll destination; |
| 222 | unsigned char source[ETH_ALEN]; |
| 223 | uint64_t sequence_counter = 0; |
| 224 | struct timespec wakeup_time; |
| 225 | unsigned int if_index; |
| 226 | uint32_t link_speed; |
| 227 | uint64_t duration; |
| 228 | int ret, socket_fd; |
| 229 | |
| 230 | socket_fd = thread_context->socket_fd; |
| 231 | |
| 232 | ret = get_interface_mac_address(l2_config->interface, source, ETH_ALEN); |
| 233 | if (ret < 0) { |
| 234 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: Failed to get Source MAC address!\n"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | ret = get_interface_link_speed(l2_config->interface, &link_speed); |
| 239 | if (ret) { |
| 240 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: Failed to get link speed!\n"); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | if_index = if_nametoindex(l2_config->interface); |
| 245 | if (!if_index) { |
| 246 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: if_nametoindex() failed!\n"); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | memset(&destination, '\0', sizeof(destination)); |
| 251 | destination.sll_family = PF_PACKET; |
| 252 | destination.sll_ifindex = if_index; |
| 253 | destination.sll_halen = ETH_ALEN; |
| 254 | memcpy(destination.sll_addr, l2_config->l2_destination, ETH_ALEN); |
| 255 | |
| 256 | duration = tx_time_get_frame_duration(link_speed, l2_config->frame_length); |
| 257 | |
| 258 | generic_l2_initialize_frames(thread_context, thread_context->tx_frame_data, |
| 259 | l2_config->num_frames_per_cycle, source, |
| 260 | l2_config->l2_destination); |
| 261 | |
| 262 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 263 | if (ret) { |
| 264 | log_message(LOG_LEVEL_ERROR, |
| 265 | "GenericL2Tx: Failed to calculate thread start time: %s!\n", |
| 266 | strerror(errno)); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | while (!thread_context->stop) { |
| 271 | increment_period(&wakeup_time, cycle_time_ns); |
| 272 | |
| 273 | do { |
| 274 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 275 | &wakeup_time, NULL); |
| 276 | } while (ret == EINTR); |
| 277 | |
| 278 | if (ret) { |
| 279 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: clock_nanosleep() failed: %s\n", |
| 280 | strerror(ret)); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | if (!mirror_enabled) { |
| 285 | generic_l2_gen_and_send_frames(thread_context, |
| 286 | l2_config->num_frames_per_cycle, socket_fd, |
| 287 | &destination, sequence_counter, duration); |
| 288 | |
| 289 | sequence_counter += l2_config->num_frames_per_cycle; |
| 290 | } else { |
| 291 | size_t len, num_frames; |
| 292 | |
| 293 | ring_buffer_fetch(thread_context->mirror_buffer, received_frames, |
| 294 | received_frames_length, &len); |
| 295 | |
| 296 | /* Len should be a multiple of frame size */ |
| 297 | num_frames = len / l2_config->frame_length; |
| 298 | generic_l2_send_frames(thread_context, received_frames, num_frames, |
| 299 | socket_fd, &destination, duration); |
| 300 | } |
| 301 | |
| 302 | stat_update(); |
| 303 | } |
| 304 | |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | static void *generic_l2_xdp_tx_thread_routine(void *data) |
| 309 | { |
| 310 | struct thread_context *thread_context = data; |
| 311 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 312 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 313 | const bool mirror_enabled = l2_config->rx_mirror_enabled; |
| 314 | uint32_t frame_number = XSK_RING_PROD__DEFAULT_NUM_DESCS; |
| 315 | size_t num_frames = l2_config->num_frames_per_cycle; |
| 316 | unsigned char source[ETH_ALEN]; |
| 317 | uint64_t sequence_counter = 0; |
| 318 | struct timespec wakeup_time; |
| 319 | unsigned char *frame_data; |
| 320 | struct xdp_socket *xsk; |
| 321 | uint32_t link_speed; |
| 322 | uint64_t duration; |
| 323 | int ret; |
| 324 | |
| 325 | xsk = thread_context->xsk; |
| 326 | xsk->tx_hwts.rtt = &round_trip_contexts[GENERICL2_FRAME_TYPE]; |
| 327 | xsk->tx_hwts.frames_per_cycle = l2_config->num_frames_per_cycle; |
| 328 | |
| 329 | ret = get_interface_mac_address(l2_config->interface, source, ETH_ALEN); |
| 330 | if (ret < 0) { |
| 331 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: Failed to get Source MAC address!\n"); |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | ret = get_interface_link_speed(l2_config->interface, &link_speed); |
| 336 | if (ret) { |
| 337 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: Failed to get link speed!\n"); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | duration = tx_time_get_frame_duration(link_speed, l2_config->frame_length); |
| 342 | |
| 343 | /* First half of umem area is for Rx, the second half is for Tx. */ |
| 344 | frame_data = xsk_umem__get_data(xsk->umem.buffer, |
| 345 | XDP_FRAME_SIZE * XSK_RING_PROD__DEFAULT_NUM_DESCS); |
| 346 | |
| 347 | /* Initialize all Tx frames */ |
| 348 | generic_l2_initialize_frames(thread_context, frame_data, XSK_RING_CONS__DEFAULT_NUM_DESCS, |
| 349 | source, l2_config->l2_destination); |
| 350 | |
| 351 | ret = get_thread_start_time(app_config.application_tx_base_offset_ns, &wakeup_time); |
| 352 | if (ret) { |
| 353 | log_message(LOG_LEVEL_ERROR, |
| 354 | "GenericL2Tx: Failed to calculate thread start time: %s!\n", |
| 355 | strerror(errno)); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | while (!thread_context->stop) { |
| 360 | increment_period(&wakeup_time, cycle_time_ns); |
| 361 | |
| 362 | do { |
| 363 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 364 | &wakeup_time, NULL); |
| 365 | } while (ret == EINTR); |
| 366 | |
| 367 | if (ret) { |
| 368 | log_message(LOG_LEVEL_ERROR, "GenericL2Tx: clock_nanosleep() failed: %s\n", |
| 369 | strerror(ret)); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | if (!mirror_enabled) { |
| 374 | generic_l2_gen_and_send_xdp_frames(thread_context, num_frames, |
| 375 | sequence_counter, ts_to_ns(&wakeup_time), |
| 376 | duration, &frame_number); |
| 377 | sequence_counter += num_frames; |
| 378 | } else { |
| 379 | unsigned int received; |
| 380 | uint64_t i; |
| 381 | |
| 382 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 383 | |
| 384 | received = thread_context->received_frames; |
| 385 | |
| 386 | sequence_counter = thread_context->rx_sequence_counter - received; |
| 387 | |
| 388 | /* |
| 389 | * The XDP receiver stored the frames within the umem area and populated the |
| 390 | * Tx ring. Now, the Tx ring can be committed to the kernel. Furthermore, |
| 391 | * already transmitted frames from last cycle can be recycled for Rx. |
| 392 | */ |
| 393 | |
| 394 | xsk_ring_prod__submit(&xsk->tx, received); |
| 395 | |
| 396 | if (received > 0) { |
| 397 | if (l2_config->tx_hwtstamp_enabled) { |
| 398 | /* |
| 399 | * Once-per-cycle: record TX SW timestamp for the first |
| 400 | * packet in this cycle and count all frames |
| 401 | */ |
| 402 | stat_frames_sent_batch(GENERICL2_FRAME_TYPE, |
| 403 | sequence_counter, received); |
| 404 | } else { |
| 405 | for (i = sequence_counter; i < sequence_counter + received; |
| 406 | ++i) |
| 407 | stat_frame_sent(GENERICL2_FRAME_TYPE, i); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | xsk->outstanding_tx += received; |
| 412 | thread_context->received_frames = 0; |
| 413 | xdp_complete_tx(xsk); |
| 414 | |
| 415 | #ifdef TX_TIMESTAMP |
| 416 | if (received > 0 && l2_config->tx_hwtstamp_enabled) { |
| 417 | /* |
| 418 | * TX HW timestamp becomes available in the next cycle |
| 419 | * after the packet is transmitted. |
| 420 | * This is one cycle later than SW timestamp tracked |
| 421 | * using sequence_counter |
| 422 | */ |
| 423 | xsk->tx_hwts.seq_lagged = sequence_counter; |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 428 | } |
| 429 | |
| 430 | stat_update(); |
| 431 | } |
| 432 | |
| 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | static int generic_l2_rx_frame(void *data, unsigned char *frame_data, size_t len) |
| 437 | { |
| 438 | struct thread_context *thread_context = data; |
| 439 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 440 | const unsigned char *expected_pattern = (const unsigned char *)l2_config->payload_pattern; |
| 441 | const size_t expected_pattern_length = l2_config->payload_pattern_length; |
| 442 | const size_t num_frames_per_cycle = l2_config->num_frames_per_cycle; |
| 443 | const bool mirror_enabled = l2_config->rx_mirror_enabled; |
| 444 | const bool ignore_rx_errors = l2_config->ignore_rx_errors; |
| 445 | uint64_t tx_timestamp, rx_hw_timestamp, rx_sw_timestamp; |
| 446 | size_t expected_frame_length = l2_config->frame_length; |
| 447 | bool out_of_order, payload_mismatch, frame_id_mismatch; |
| 448 | unsigned char new_frame[MAX_FRAME_SIZE]; |
| 449 | struct generic_l2_header *l2; |
| 450 | uint64_t sequence_counter; |
| 451 | bool vlan_tag_missing; |
| 452 | void *p = frame_data; |
| 453 | struct ethhdr *eth; |
| 454 | uint16_t proto; |
| 455 | |
| 456 | if (len < sizeof(struct vlan_ethernet_header)) { |
| 457 | log_message(LOG_LEVEL_WARNING, "GenericL2Rx: Too small frame received!\n"); |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | eth = p; |
| 462 | if (eth->h_proto == htons(ETH_P_8021Q)) { |
| 463 | struct vlan_ethernet_header *veth = p; |
| 464 | |
| 465 | proto = veth->vlan_encapsulated_proto; |
| 466 | p += sizeof(*veth); |
| 467 | vlan_tag_missing = false; |
| 468 | } else { |
| 469 | proto = eth->h_proto; |
| 470 | p += sizeof(*eth); |
| 471 | expected_frame_length -= sizeof(struct vlan_header); |
| 472 | vlan_tag_missing = true; |
| 473 | } |
| 474 | |
| 475 | if (proto != htons(l2_config->ether_type)) { |
| 476 | log_message(LOG_LEVEL_WARNING, |
| 477 | "GenericL2Rx: Frame with wrong Ether Type received!\n"); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | /* Check frame length: VLAN tag might be stripped or not. Check it. */ |
| 482 | if (len != expected_frame_length) { |
| 483 | log_message(LOG_LEVEL_WARNING, "GenericL2Rx: Frame with wrong length received!\n"); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | /* Check cycle counter and payload. */ |
| 488 | l2 = p; |
| 489 | p += sizeof(*l2); |
| 490 | |
| 491 | sequence_counter = meta_data_to_sequence_counter(&l2->meta_data, num_frames_per_cycle); |
| 492 | |
| 493 | tx_timestamp = meta_data_to_tx_timestamp(&l2->meta_data); |
| 494 | set_mirror_tx_timestamp(&l2->meta_data); |
| 495 | |
| 496 | xdp_get_timestamp_metadata(frame_data, &rx_hw_timestamp, &rx_sw_timestamp); |
| 497 | out_of_order = sequence_counter != thread_context->rx_sequence_counter; |
| 498 | payload_mismatch = memcmp(p, expected_pattern, expected_pattern_length); |
| 499 | frame_id_mismatch = false; |
| 500 | |
| 501 | stat_frame_received(GENERICL2_FRAME_TYPE, sequence_counter, out_of_order, payload_mismatch, |
| 502 | frame_id_mismatch, tx_timestamp, rx_hw_timestamp, rx_sw_timestamp); |
| 503 | |
| 504 | if (out_of_order) { |
| 505 | if (!ignore_rx_errors) |
| 506 | log_message(LOG_LEVEL_WARNING, |
| 507 | "GenericL2Rx: frame[%" PRIu64 |
| 508 | "] SequenceCounter mismatch: %" PRIu64 "!\n", |
| 509 | sequence_counter, thread_context->rx_sequence_counter); |
| 510 | thread_context->rx_sequence_counter++; |
| 511 | } |
| 512 | |
| 513 | if (payload_mismatch) |
| 514 | log_message(LOG_LEVEL_WARNING, |
| 515 | "GenericL2Rx: frame[%" PRIu64 "] Payload Pattern mismatch!\n", |
| 516 | sequence_counter); |
| 517 | |
| 518 | thread_context->rx_sequence_counter++; |
| 519 | |
| 520 | /* |
| 521 | * If mirror enabled, assemble and store the frame for Tx later. |
| 522 | * |
| 523 | * In case of XDP the Rx umem area will be reused for Tx. |
| 524 | */ |
| 525 | if (!mirror_enabled) |
| 526 | return 0; |
| 527 | |
| 528 | if (l2_config->xdp_enabled) { |
| 529 | /* Re-add vlan tag */ |
| 530 | if (vlan_tag_missing) |
| 531 | insert_vlan_tag(frame_data, len, |
| 532 | l2_config->vid | l2_config->pcp << VLAN_PCP_SHIFT); |
| 533 | |
| 534 | /* Swap mac addresses inline */ |
| 535 | swap_mac_addresses(frame_data, len); |
| 536 | } else { |
| 537 | /* Build new frame for Tx with VLAN info. */ |
| 538 | build_vlan_frame_from_rx(frame_data, len, new_frame, sizeof(new_frame), |
| 539 | l2_config->ether_type, |
| 540 | l2_config->vid | l2_config->pcp << VLAN_PCP_SHIFT); |
| 541 | |
| 542 | /* Store the new frame. */ |
| 543 | ring_buffer_add(thread_context->mirror_buffer, new_frame, len + 4); |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static void *generic_l2_rx_thread_routine(void *data) |
| 550 | { |
| 551 | struct thread_context *thread_context = data; |
| 552 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 553 | struct timespec wakeup_time; |
| 554 | int socket_fd, ret; |
| 555 | |
| 556 | socket_fd = thread_context->socket_fd; |
| 557 | |
| 558 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 559 | if (ret) { |
| 560 | log_message(LOG_LEVEL_ERROR, |
| 561 | "GenericL2Rx: Failed to calculate thread start time: %s!\n", |
| 562 | strerror(errno)); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | while (!thread_context->stop) { |
| 567 | struct packet_receive_request recv_req = { |
| 568 | .traffic_class = thread_context->traffic_class, |
| 569 | .socket_fd = socket_fd, |
| 570 | .receive_function = generic_l2_rx_frame, |
| 571 | .data = thread_context, |
| 572 | }; |
| 573 | |
| 574 | /* Wait until next period. */ |
| 575 | increment_period(&wakeup_time, cycle_time_ns); |
| 576 | |
| 577 | do { |
| 578 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 579 | &wakeup_time, NULL); |
| 580 | } while (ret == EINTR); |
| 581 | |
| 582 | if (ret) { |
| 583 | log_message(LOG_LEVEL_ERROR, "GenericL2Rx: clock_nanosleep() failed: %s\n", |
| 584 | strerror(ret)); |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | /* Receive Layer 2 frames. */ |
| 589 | packet_receive_messages(thread_context->packet_context, &recv_req); |
| 590 | } |
| 591 | |
| 592 | return NULL; |
| 593 | } |
| 594 | |
| 595 | static void *generic_l2_xdp_rx_thread_routine(void *data) |
| 596 | { |
| 597 | struct thread_context *thread_context = data; |
| 598 | const long long cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 599 | const struct traffic_class_config *l2_config = thread_context->conf; |
| 600 | const bool mirror_enabled = l2_config->rx_mirror_enabled; |
| 601 | const size_t frame_length = l2_config->frame_length; |
| 602 | struct xdp_socket *xsk = thread_context->xsk; |
| 603 | struct timespec wakeup_time; |
| 604 | uint32_t link_speed; |
| 605 | uint64_t duration; |
| 606 | int ret; |
| 607 | |
| 608 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 609 | if (ret) { |
| 610 | log_message(LOG_LEVEL_ERROR, |
| 611 | "GenericL2Rx: Failed to calculate thread start time: %s!\n", |
| 612 | strerror(errno)); |
| 613 | return NULL; |
| 614 | } |
| 615 | |
| 616 | ret = get_interface_link_speed(l2_config->interface, &link_speed); |
| 617 | if (ret) { |
| 618 | log_message(LOG_LEVEL_ERROR, "GenericL2Rx: Failed to get link speed!\n"); |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | duration = tx_time_get_frame_duration(link_speed, l2_config->frame_length); |
| 623 | |
| 624 | while (!thread_context->stop) { |
| 625 | struct xdp_tx_time tx_time = { |
| 626 | .tx_time_offset = l2_config->tx_time_offset_ns, |
| 627 | .duration = duration, |
| 628 | .num_frames_per_cycle = l2_config->num_frames_per_cycle, |
| 629 | .sequence_counter_begin = 0, |
| 630 | .traffic_class = thread_context->traffic_class, |
| 631 | }; |
| 632 | unsigned int received; |
| 633 | |
| 634 | /* Wait until next period */ |
| 635 | increment_period(&wakeup_time, cycle_time_ns); |
| 636 | |
| 637 | do { |
| 638 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 639 | &wakeup_time, NULL); |
| 640 | } while (ret == EINTR); |
| 641 | |
| 642 | if (ret) { |
| 643 | log_message(LOG_LEVEL_ERROR, "GenericL2Rx: clock_nanosleep() failed: %s\n", |
| 644 | strerror(ret)); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | pthread_mutex_lock(&thread_context->xdp_data_mutex); |
| 649 | received = xdp_receive_frames(xsk, frame_length, mirror_enabled, |
| 650 | generic_l2_rx_frame, thread_context, &tx_time); |
| 651 | thread_context->received_frames = received; |
| 652 | pthread_mutex_unlock(&thread_context->xdp_data_mutex); |
| 653 | } |
| 654 | |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | struct thread_context *generic_l2_threads_create(void) |
| 659 | { |
| 660 | struct traffic_class_config *l2_config; |
| 661 | struct thread_context *thread_context; |
| 662 | char thread_name[128]; |
| 663 | int ret; |
| 664 | |
| 665 | thread_context = calloc(1, sizeof(*thread_context)); |
| 666 | if (!thread_context) |
| 667 | return NULL; |
| 668 | |
| 669 | if (!config_is_traffic_class_active("GenericL2")) |
| 670 | goto out; |
| 671 | |
| 672 | thread_context->conf = l2_config = &app_config.classes[GENERICL2_FRAME_TYPE]; |
| 673 | thread_context->frame_type = GENERICL2_FRAME_TYPE; |
| 674 | thread_context->traffic_class = stat_frame_type_to_string(GENERICL2_FRAME_TYPE); |
| 675 | |
| 676 | /* For XDP the frames are stored in a umem area. That memory is part of the socket. */ |
| 677 | if (!l2_config->xdp_enabled) { |
| 678 | thread_context->packet_context = packet_init(l2_config->num_frames_per_cycle); |
| 679 | if (!thread_context->packet_context) { |
| 680 | fprintf(stderr, "Failed to allocate GenericL2 packet context!\n"); |
| 681 | goto err_packet; |
| 682 | } |
| 683 | |
| 684 | thread_context->tx_frame_data = |
| 685 | calloc(l2_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 686 | if (!thread_context->tx_frame_data) { |
| 687 | fprintf(stderr, "Failed to allocate GenericL2TxFrameData\n"); |
| 688 | goto err_tx; |
| 689 | } |
| 690 | |
| 691 | thread_context->rx_frame_data = |
| 692 | calloc(l2_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 693 | if (!thread_context->rx_frame_data) { |
| 694 | fprintf(stderr, "Failed to allocate GenericL2RxFrameData\n"); |
| 695 | goto err_rx; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /* For XDP a AF_XDP socket is allocated. Otherwise a Linux raw socket is used. */ |
| 700 | if (l2_config->xdp_enabled) { |
| 701 | thread_context->socket_fd = 0; |
| 702 | thread_context->xsk = xdp_open_socket( |
| 703 | l2_config->interface, app_config.application_xdp_program, |
| 704 | l2_config->rx_queue, l2_config->xdp_skb_mode, l2_config->xdp_zc_mode, |
| 705 | l2_config->xdp_wakeup_mode, l2_config->xdp_busy_poll_mode, |
| 706 | l2_config->tx_time_enabled, l2_config->tx_hwtstamp_enabled); |
| 707 | if (!thread_context->xsk) { |
| 708 | fprintf(stderr, "Failed to create GenericL2 Xdp socket!\n"); |
| 709 | goto err_socket; |
| 710 | } |
| 711 | } else { |
| 712 | thread_context->xsk = NULL; |
| 713 | thread_context->socket_fd = create_generic_l2_socket(); |
| 714 | if (thread_context->socket_fd < 0) { |
| 715 | fprintf(stderr, "Failed to create GenericL2 Socket!\n"); |
| 716 | goto err_socket; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | init_mutex(&thread_context->xdp_data_mutex); |
| 721 | |
| 722 | /* Same as above. For XDP the umem area is used. */ |
| 723 | if (l2_config->rx_mirror_enabled && !l2_config->xdp_enabled) { |
| 724 | /* Per period the expectation is: GenericL2NumFramesPerCycle * MAX_FRAME */ |
| 725 | thread_context->mirror_buffer = |
| 726 | ring_buffer_allocate(MAX_FRAME_SIZE * l2_config->num_frames_per_cycle); |
| 727 | if (!thread_context->mirror_buffer) { |
| 728 | fprintf(stderr, "Failed to allocate GenericL2 Mirror RingBuffer!\n"); |
| 729 | goto err_buffer; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | snprintf(thread_name, sizeof(thread_name), "%sTxThread", l2_config->name); |
| 734 | |
| 735 | ret = create_rt_thread(&thread_context->tx_task_id, thread_name, |
| 736 | l2_config->tx_thread_priority, l2_config->tx_thread_cpu, |
| 737 | l2_config->xdp_enabled ? generic_l2_xdp_tx_thread_routine |
| 738 | : generic_l2_tx_thread_routine, |
| 739 | thread_context); |
| 740 | if (ret) { |
| 741 | fprintf(stderr, "Failed to create GenericL2 Tx Thread!\n"); |
| 742 | goto err_thread; |
| 743 | } |
| 744 | |
| 745 | snprintf(thread_name, sizeof(thread_name), "%sRxThread", l2_config->name); |
| 746 | |
| 747 | ret = create_rt_thread(&thread_context->rx_task_id, thread_name, |
| 748 | l2_config->rx_thread_priority, l2_config->rx_thread_cpu, |
| 749 | l2_config->xdp_enabled ? generic_l2_xdp_rx_thread_routine |
| 750 | : generic_l2_rx_thread_routine, |
| 751 | thread_context); |
| 752 | if (ret) { |
| 753 | fprintf(stderr, "Failed to create GenericL2 Rx Thread!\n"); |
| 754 | goto err_thread_rx; |
| 755 | } |
| 756 | |
| 757 | thread_context->meta_data_offset = |
| 758 | get_meta_data_offset(GENERICL2_FRAME_TYPE, SECURITY_MODE_NONE); |
| 759 | |
| 760 | out: |
| 761 | return thread_context; |
| 762 | |
| 763 | err_thread_rx: |
| 764 | thread_context->stop = 1; |
| 765 | pthread_join(thread_context->tx_task_id, NULL); |
| 766 | err_thread: |
| 767 | ring_buffer_free(thread_context->mirror_buffer); |
| 768 | err_buffer: |
| 769 | if (thread_context->socket_fd) |
| 770 | close(thread_context->socket_fd); |
| 771 | if (thread_context->xsk) |
| 772 | xdp_close_socket(thread_context->xsk, l2_config->interface, |
| 773 | l2_config->xdp_skb_mode); |
| 774 | err_socket: |
| 775 | free(thread_context->rx_frame_data); |
| 776 | err_rx: |
| 777 | free(thread_context->tx_frame_data); |
| 778 | err_tx: |
| 779 | packet_free(thread_context->packet_context); |
| 780 | err_packet: |
| 781 | free(thread_context); |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | void generic_l2_threads_free(struct thread_context *thread_context) |
| 786 | { |
| 787 | struct traffic_class_config *l2_config; |
| 788 | |
| 789 | if (!thread_context) |
| 790 | return; |
| 791 | |
| 792 | l2_config = thread_context->conf; |
| 793 | |
| 794 | ring_buffer_free(thread_context->mirror_buffer); |
| 795 | |
| 796 | packet_free(thread_context->packet_context); |
| 797 | free(thread_context->tx_frame_data); |
| 798 | free(thread_context->rx_frame_data); |
| 799 | |
| 800 | if (thread_context->socket_fd > 0) |
| 801 | close(thread_context->socket_fd); |
| 802 | |
| 803 | if (thread_context->xsk) |
| 804 | xdp_close_socket(thread_context->xsk, l2_config->interface, |
| 805 | l2_config->xdp_skb_mode); |
| 806 | |
| 807 | free(thread_context); |
| 808 | } |
| 809 | |
| 810 | void generic_l2_threads_wait_for_finish(struct thread_context *thread_context) |
| 811 | { |
| 812 | if (!thread_context) |
| 813 | return; |
| 814 | |
| 815 | if (thread_context->rx_task_id) |
| 816 | pthread_join(thread_context->rx_task_id, NULL); |
| 817 | if (thread_context->tx_task_id) |
| 818 | pthread_join(thread_context->tx_task_id, NULL); |
| 819 | } |