| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/layer2_thread.c`/ |
| 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"cxx . . $ `<file>/build/app_config.h`/ |
| 25 | |
| 26 | #include "config.h"cxx . . $ `<file>/src/config.h`/ |
| 27 | #include "layer2_thread.h"cxx . . $ `<file>/src/layer2_thread.h`/ |
| 28 | #include "log.h"cxx . . $ `<file>/src/log.h`/ |
| 29 | #include "net.h"cxx . . $ `<file>/src/net.h`/ |
| 30 | #include "packet.h"cxx . . $ `<file>/src/packet.h`/ |
| 31 | #include "security.h"cxx . . $ `<file>/src/security.h`/ |
| 32 | #include "stat.h"cxx . . $ `<file>/src/stat.h`/ |
| 33 | #include "thread.h"cxx . . $ `<file>/src/thread.h`/ |
| 34 | #include "tx_time.h"cxx . . $ `<file>/src/tx_time.h`/ |
| 35 | #include "utils.h"cxx . . $ `<file>/src/utils.h`/ |
| 36 | |
| 37 | static void generic_l2_initialize_framecxx . . $ generic_l2_initialize_frame(14e45335b2c8bf07).(struct thread_contextcxx . . $ thread_context# *thread_context, |
| 38 | unsigned char *frame_data, const unsigned char *source, |
| 39 | const unsigned char *destination) |
| 40 | { |
| 41 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 42 | struct vlan_ethernet_headercxx . . $ vlan_ethernet_header# *eth; |
| 43 | struct generic_l2_headercxx . . $ 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_TIMEcxx . . $ `build/app_config.h:17:9`!) || defined(TX_TIMESTAMP) |
| 61 | if (l2_config->xdp_enabledcxx . . $ traffic_class_config#xdp_enabled. && |
| 62 | (l2_config->tx_time_enabledcxx . . $ traffic_class_config#tx_time_enabled. || l2_config->tx_hwtstamp_enabledcxx . . $ traffic_class_config#tx_hwtstamp_enabled.)) |
| 63 | frame_data += sizeof(struct xsk_tx_metadatacxx . . $ xsk_tx_metadata#); |
| 64 | #endif |
| 65 | |
| 66 | eth = (struct vlan_ethernet_headercxx . . $ vlan_ethernet_header# *)frame_data; |
| 67 | l2 = (struct generic_l2_headercxx . . $ generic_l2_header# *)(frame_data + sizeof(*eth)); |
| 68 | |
| 69 | /* Ethernet header */ |
| 70 | memcpycxx . . $ memcpy(cc817cb175f282d).(eth->destinationcxx . . $ vlan_ethernet_header#destination., destination, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!); |
| 71 | memcpycxx . . $ memcpy(cc817cb175f282d).(eth->sourcecxx . . $ vlan_ethernet_header#source., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!); |
| 72 | |
| 73 | /* VLAN Header */ |
| 74 | eth->vlan_protocxx . . $ vlan_ethernet_header#vlan_proto. = htonscxx . . $ htons(a18c05e4017afb3d).(ETH_P_8021Qcxx . . $ `<external>/15209105680615352250/if_ether.h:71:9`!); |
| 75 | eth->vlantcicxx . . $ vlan_ethernet_header#vlantci. = htonscxx . . $ htons(a18c05e4017afb3d).(l2_config->vidcxx . . $ traffic_class_config#vid. | l2_config->pcpcxx . . $ traffic_class_config#pcp. << VLAN_PCP_SHIFTcxx . . $ `src/net_def.h:26:9`!); |
| 76 | eth->vlan_encapsulated_protocxx . . $ vlan_ethernet_header#vlan_encapsulated_proto. = htonscxx . . $ htons(a18c05e4017afb3d).(l2_config->ether_typecxx . . $ traffic_class_config#ether_type.); |
| 77 | |
| 78 | /* Generic L2 header */ |
| 79 | l2->meta_datacxx . . $ generic_l2_header#meta_data..frame_countercxx . . $ reference_meta_data#frame_counter. = 0; |
| 80 | l2->meta_datacxx . . $ generic_l2_header#meta_data..cycle_countercxx . . $ reference_meta_data#cycle_counter. = 0; |
| 81 | |
| 82 | /* Payload */ |
| 83 | payload_offset = sizeof(*eth) + sizeof(*l2); |
| 84 | memcpycxx . . $ memcpy(cc817cb175f282d).(frame_data + payload_offset, l2_config->payload_patterncxx . . $ traffic_class_config#payload_pattern., |
| 85 | l2_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.); |
| 86 | |
| 87 | /* Padding: '\0' */ |
| 88 | } |
| 89 | |
| 90 | static void generic_l2_initialize_framescxx . . $ generic_l2_initialize_frames(e1a9c3903a216417).(struct thread_contextcxx . . $ 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_framecxx . . $ generic_l2_initialize_frame(14e45335b2c8bf07).(thread_context, frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(frame_data, i), source, |
| 99 | destination); |
| 100 | } |
| 101 | |
| 102 | static int generic_l2_send_messagescxx . . $ generic_l2_send_messages(80bab491920559ba).(struct thread_contextcxx . . $ thread_context# *thread_context, int socket_fd, |
| 103 | struct sockaddr_llcxx . . $ sockaddr_ll# *destination, unsigned char *frame_data, |
| 104 | size_t num_frames, uint64_tcxx . . $ uint64_t# duration) |
| 105 | { |
| 106 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 107 | struct packet_send_requestcxx . . $ packet_send_request# send_req = { |
| 108 | .traffic_class = thread_context->traffic_classcxx . . $ 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_lengthcxx . . $ traffic_class_config#frame_length., |
| 114 | .duration = duration, |
| 115 | .tx_time_offset = l2_config->tx_time_offset_nscxx . . $ traffic_class_config#tx_time_offset_ns., |
| 116 | .meta_data_offset = thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset., |
| 117 | .mirror_enabled = l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled., |
| 118 | .tx_time_enabled = l2_config->tx_time_enabledcxx . . $ traffic_class_config#tx_time_enabled., |
| 119 | }; |
| 120 | |
| 121 | return packet_send_messagescxx . . $ packet_send_messages(db4d6f7226235983).(thread_context->packet_contextcxx . . $ thread_context#packet_context., &send_req); |
| 122 | } |
| 123 | |
| 124 | static int generic_l2_send_framescxx . . $ generic_l2_send_frames(53779637f63d9f2f).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data, |
| 125 | size_t num_frames, int socket_fd, struct sockaddr_llcxx . . $ sockaddr_ll# *destination, |
| 126 | uint64_tcxx . . $ uint64_t# duration) |
| 127 | { |
| 128 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 129 | size_t frame_length; |
| 130 | int len, i; |
| 131 | |
| 132 | /* Send it */ |
| 133 | frame_length = l2_config->frame_lengthcxx . . $ traffic_class_config#frame_length.; |
| 134 | len = generic_l2_send_messagescxx . . $ generic_l2_send_messages(80bab491920559ba).(thread_context, socket_fd, destination, frame_data, |
| 135 | num_frames, duration); |
| 136 | |
| 137 | for (i = 0; i < len; i++) { |
| 138 | uint64_tcxx . . $ uint64_t# sequence_counter; |
| 139 | |
| 140 | sequence_counter = get_sequence_countercxx . . $ get_sequence_counter(9512b04a879eebb0).(frame_data + i * frame_length, |
| 141 | thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset., |
| 142 | l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.); |
| 143 | stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., sequence_counter); |
| 144 | } |
| 145 | |
| 146 | return len; |
| 147 | } |
| 148 | |
| 149 | static int generic_l2_gen_and_send_framescxx . . $ generic_l2_gen_and_send_frames(b673f002f771ffc0).(struct thread_contextcxx . . $ thread_context# *thread_context, |
| 150 | size_t num_frames_per_cycle, int socket_fd, |
| 151 | struct sockaddr_llcxx . . $ sockaddr_ll# *destination, |
| 152 | uint64_tcxx . . $ uint64_t# sequence_counter_begin, uint64_tcxx . . $ uint64_t# duration) |
| 153 | { |
| 154 | struct vlan_ethernet_headercxx . . $ vlan_ethernet_header# *eth; |
| 155 | struct generic_l2_headercxx . . $ generic_l2_header# *l2; |
| 156 | struct timespeccxx . . $ timespec# tx_time = {}; |
| 157 | int len, i; |
| 158 | |
| 159 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_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_headercxx . . $ generic_l2_header# *)(frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data., i) + |
| 164 | sizeof(*eth)); |
| 165 | sequence_counter_to_meta_datacxx . . $ sequence_counter_to_meta_data(9318332f0006d3bc).(&l2->meta_datacxx . . $ generic_l2_header#meta_data., sequence_counter_begin + i, |
| 166 | num_frames_per_cycle); |
| 167 | |
| 168 | tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(&l2->meta_datacxx . . $ generic_l2_header#meta_data., ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&tx_time)); |
| 169 | } |
| 170 | |
| 171 | /* Send them */ |
| 172 | len = generic_l2_send_messagescxx . . $ generic_l2_send_messages(80bab491920559ba).(thread_context, socket_fd, destination, |
| 173 | thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data., num_frames_per_cycle, |
| 174 | duration); |
| 175 | |
| 176 | for (i = 0; i < len; i++) |
| 177 | stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., sequence_counter_begin + i); |
| 178 | |
| 179 | return len; |
| 180 | } |
| 181 | |
| 182 | static void generic_l2_gen_and_send_xdp_framescxx . . $ generic_l2_gen_and_send_xdp_frames(95adf57d8034a66c).(struct thread_contextcxx . . $ thread_context# *thread_context, |
| 183 | size_t num_frames_per_cycle, |
| 184 | uint64_tcxx . . $ uint64_t# sequence_counter, uint64_tcxx . . $ uint64_t# wakeup_time, |
| 185 | uint64_tcxx . . $ uint64_t# duration, uint32_tcxx . . $ uint32_t# *frame_number) |
| 186 | { |
| 187 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 188 | struct xdp_tx_timecxx . . $ xdp_tx_time# tx_time = { |
| 189 | .traffic_class = thread_context->traffic_classcxx . . $ thread_context#traffic_class., |
| 190 | .tx_time_offset = l2_config->tx_time_offset_nscxx . . $ traffic_class_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_configcxx . . $ xdp_gen_config# xdp = { |
| 196 | .mode = SECURITY_MODE_NONEcxx . . $ SECURITY_MODE_NONE., |
| 197 | .security_context = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 198 | .iv_prefix = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 199 | .payload_pattern = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 200 | .payload_pattern_length = 0, |
| 201 | .frame_length = l2_config->frame_lengthcxx . . $ traffic_class_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_offsetcxx . . $ thread_context#meta_data_offset., |
| 206 | .frame_type = GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., |
| 207 | .tx_time = l2_config->tx_time_enabledcxx . . $ traffic_class_config#tx_time_enabled. ? &tx_time : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, |
| 208 | }; |
| 209 | |
| 210 | xdp_gen_and_send_framescxx . . $ xdp_gen_and_send_frames(312660e902b5d61d).(thread_context->xskcxx . . $ thread_context#xsk., &xdp); |
| 211 | } |
| 212 | |
| 213 | static void *generic_l2_tx_thread_routinecxx . . $ generic_l2_tx_thread_routine(9f162e79bfc6c74).(void *data) |
| 214 | { |
| 215 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 216 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 217 | size_t received_frames_length = MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 218 | const long long cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.; |
| 219 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 220 | unsigned char *received_frames = thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.; |
| 221 | struct sockaddr_llcxx . . $ sockaddr_ll# destination; |
| 222 | unsigned char source[ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!]; |
| 223 | uint64_tcxx . . $ uint64_t# sequence_counter = 0; |
| 224 | struct timespeccxx . . $ timespec# wakeup_time; |
| 225 | unsigned int if_index; |
| 226 | uint32_tcxx . . $ uint32_t# link_speed; |
| 227 | uint64_tcxx . . $ uint64_t# duration; |
| 228 | int ret, socket_fd; |
| 229 | |
| 230 | socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.; |
| 231 | |
| 232 | ret = get_interface_mac_addresscxx . . $ get_interface_mac_address(6007bd5416c53f30).(l2_config->interfacecxx . . $ traffic_class_config#interface., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!); |
| 233 | if (ret < 0) { |
| 234 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: Failed to get Source MAC address!\n"); |
| 235 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 236 | } |
| 237 | |
| 238 | ret = get_interface_link_speedcxx . . $ get_interface_link_speed(6974107818eeb505).(l2_config->interfacecxx . . $ traffic_class_config#interface., &link_speed); |
| 239 | if (ret) { |
| 240 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: Failed to get link speed!\n"); |
| 241 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 242 | } |
| 243 | |
| 244 | if_index = if_nametoindexcxx . . $ if_nametoindex(c61c63047ce273de).(l2_config->interfacecxx . . $ traffic_class_config#interface.); |
| 245 | if (!if_index) { |
| 246 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: if_nametoindex() failed!\n"); |
| 247 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 248 | } |
| 249 | |
| 250 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&destination, '\0', sizeof(destination)); |
| 251 | destination.sll_familycxx . . $ sockaddr_ll#sll_family. = PF_PACKETcxx . . $ `<external>/8962547315054503424/socket.h:61:9`!; |
| 252 | destination.sll_ifindexcxx . . $ sockaddr_ll#sll_ifindex. = if_index; |
| 253 | destination.sll_halencxx . . $ sockaddr_ll#sll_halen. = ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!; |
| 254 | memcpycxx . . $ memcpy(cc817cb175f282d).(destination.sll_addrcxx . . $ sockaddr_ll#sll_addr., l2_config->l2_destinationcxx . . $ traffic_class_config#l2_destination., ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!); |
| 255 | |
| 256 | duration = tx_time_get_frame_durationcxx . . $ tx_time_get_frame_duration(b831a5b42b98fcd).(link_speed, l2_config->frame_lengthcxx . . $ traffic_class_config#frame_length.); |
| 257 | |
| 258 | generic_l2_initialize_framescxx . . $ generic_l2_initialize_frames(e1a9c3903a216417).(thread_context, thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data., |
| 259 | l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., source, |
| 260 | l2_config->l2_destinationcxx . . $ traffic_class_config#l2_destination.); |
| 261 | |
| 262 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(app_configcxx . . $ app_config..application_tx_base_offset_nscxx . . $ application_config#application_tx_base_offset_ns., &wakeup_time); |
| 263 | if (ret) { |
| 264 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., |
| 265 | "GenericL2Tx: Failed to calculate thread start time: %s!\n", |
| 266 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 267 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 268 | } |
| 269 | |
| 270 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 271 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 272 | |
| 273 | do { |
| 274 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 275 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 276 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 277 | |
| 278 | if (ret) { |
| 279 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: clock_nanosleep() failed: %s\n", |
| 280 | strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 281 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 282 | } |
| 283 | |
| 284 | if (!mirror_enabled) { |
| 285 | generic_l2_gen_and_send_framescxx . . $ generic_l2_gen_and_send_frames(b673f002f771ffc0).(thread_context, |
| 286 | l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., socket_fd, |
| 287 | &destination, sequence_counter, duration); |
| 288 | |
| 289 | sequence_counter += l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 290 | } else { |
| 291 | size_t len, num_frames; |
| 292 | |
| 293 | ring_buffer_fetchcxx . . $ ring_buffer_fetch(e8bf408057bf6314).(thread_context->mirror_buffercxx . . $ 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_lengthcxx . . $ traffic_class_config#frame_length.; |
| 298 | generic_l2_send_framescxx . . $ generic_l2_send_frames(53779637f63d9f2f).(thread_context, received_frames, num_frames, |
| 299 | socket_fd, &destination, duration); |
| 300 | } |
| 301 | |
| 302 | stat_updatecxx . . $ stat_update(49f6e7a06ebc5aa8).(); |
| 303 | } |
| 304 | |
| 305 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 306 | } |
| 307 | |
| 308 | static void *generic_l2_xdp_tx_thread_routinecxx . . $ generic_l2_xdp_tx_thread_routine(9f162e79bfc6c74).(void *data) |
| 309 | { |
| 310 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 311 | const long long cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.; |
| 312 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 313 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 314 | uint32_tcxx . . $ uint32_t# frame_number = XSK_RING_PROD__DEFAULT_NUM_DESCScxx . . $ `src/xdp.h:30:9`!; |
| 315 | size_t num_frames = l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 316 | unsigned char source[ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!]; |
| 317 | uint64_tcxx . . $ uint64_t# sequence_counter = 0; |
| 318 | struct timespeccxx . . $ timespec# wakeup_time; |
| 319 | unsigned char *frame_data; |
| 320 | struct xdp_socketcxx . . $ xdp_socket# *xsk; |
| 321 | uint32_tcxx . . $ uint32_t# link_speed; |
| 322 | uint64_tcxx . . $ uint64_t# duration; |
| 323 | int ret; |
| 324 | |
| 325 | xsk = thread_context->xskcxx . . $ thread_context#xsk.; |
| 326 | xsk->tx_hwtscxx . . $ xdp_socket#tx_hwts..rttcxx . . $ xdp_tx_hwts#rtt. = &round_trip_contextscxx . . $ round_trip_contexts.[GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE.]; |
| 327 | xsk->tx_hwtscxx . . $ xdp_socket#tx_hwts..frames_per_cyclecxx . . $ xdp_tx_hwts#frames_per_cycle. = l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 328 | |
| 329 | ret = get_interface_mac_addresscxx . . $ get_interface_mac_address(6007bd5416c53f30).(l2_config->interfacecxx . . $ traffic_class_config#interface., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!); |
| 330 | if (ret < 0) { |
| 331 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: Failed to get Source MAC address!\n"); |
| 332 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 333 | } |
| 334 | |
| 335 | ret = get_interface_link_speedcxx . . $ get_interface_link_speed(6974107818eeb505).(l2_config->interfacecxx . . $ traffic_class_config#interface., &link_speed); |
| 336 | if (ret) { |
| 337 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: Failed to get link speed!\n"); |
| 338 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 339 | } |
| 340 | |
| 341 | duration = tx_time_get_frame_durationcxx . . $ tx_time_get_frame_duration(b831a5b42b98fcd).(link_speed, l2_config->frame_lengthcxx . . $ traffic_class_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_datacxx . . $ xsk_umem__get_data(c096771c57425568).(xsk->umemcxx . . $ xdp_socket#umem..buffercxx . . $ xsk_umem_info#buffer., |
| 345 | XDP_FRAME_SIZEcxx . . $ `src/xdp.h:34:9`! * XSK_RING_PROD__DEFAULT_NUM_DESCScxx . . $ `src/xdp.h:30:9`!); |
| 346 | |
| 347 | /* Initialize all Tx frames */ |
| 348 | generic_l2_initialize_framescxx . . $ generic_l2_initialize_frames(e1a9c3903a216417).(thread_context, frame_data, XSK_RING_CONS__DEFAULT_NUM_DESCScxx . . $ `src/xdp.h:29:9`!, |
| 349 | source, l2_config->l2_destinationcxx . . $ traffic_class_config#l2_destination.); |
| 350 | |
| 351 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(app_configcxx . . $ app_config..application_tx_base_offset_nscxx . . $ application_config#application_tx_base_offset_ns., &wakeup_time); |
| 352 | if (ret) { |
| 353 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., |
| 354 | "GenericL2Tx: Failed to calculate thread start time: %s!\n", |
| 355 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 356 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 357 | } |
| 358 | |
| 359 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 360 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 361 | |
| 362 | do { |
| 363 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 364 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 365 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 366 | |
| 367 | if (ret) { |
| 368 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Tx: clock_nanosleep() failed: %s\n", |
| 369 | strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 370 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 371 | } |
| 372 | |
| 373 | if (!mirror_enabled) { |
| 374 | generic_l2_gen_and_send_xdp_framescxx . . $ generic_l2_gen_and_send_xdp_frames(95adf57d8034a66c).(thread_context, num_frames, |
| 375 | sequence_counter, ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&wakeup_time), |
| 376 | duration, &frame_number); |
| 377 | sequence_counter += num_frames; |
| 378 | } else { |
| 379 | unsigned int received; |
| 380 | uint64_tcxx . . $ uint64_t# i; |
| 381 | |
| 382 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->xdp_data_mutexcxx . . $ thread_context#xdp_data_mutex.); |
| 383 | |
| 384 | received = thread_context->received_framescxx . . $ thread_context#received_frames.; |
| 385 | |
| 386 | sequence_counter = thread_context->rx_sequence_countercxx . . $ 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__submitcxx . . $ xsk_ring_prod__submit(9991f52d60245542).(&xsk->txcxx . . $ xdp_socket#tx., received); |
| 395 | |
| 396 | if (received > 0) { |
| 397 | if (l2_config->tx_hwtstamp_enabledcxx . . $ traffic_class_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_batchcxx . . $ stat_frames_sent_batch(1b9110cae93061b5).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., |
| 403 | sequence_counter, received); |
| 404 | } else { |
| 405 | for (i = sequence_counter; i < sequence_counter + received; |
| 406 | ++i) |
| 407 | stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., i); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | xsk->outstanding_txcxx . . $ xdp_socket#outstanding_tx. += received; |
| 412 | thread_context->received_framescxx . . $ thread_context#received_frames. = 0; |
| 413 | xdp_complete_txcxx . . $ xdp_complete_tx(5f53ef6b518fdd5d).(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_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->xdp_data_mutexcxx . . $ thread_context#xdp_data_mutex.); |
| 428 | } |
| 429 | |
| 430 | stat_updatecxx . . $ stat_update(49f6e7a06ebc5aa8).(); |
| 431 | } |
| 432 | |
| 433 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 434 | } |
| 435 | |
| 436 | static int generic_l2_rx_framecxx . . $ generic_l2_rx_frame(ddfd8079f1f43689).(void *data, unsigned char *frame_data, size_t len) |
| 437 | { |
| 438 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 439 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 440 | const unsigned char *expected_pattern = (const unsigned char *)l2_config->payload_patterncxx . . $ traffic_class_config#payload_pattern.; |
| 441 | const size_t expected_pattern_length = l2_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.; |
| 442 | const size_t num_frames_per_cycle = l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; |
| 443 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 444 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ignore_rx_errors = l2_config->ignore_rx_errorscxx . . $ traffic_class_config#ignore_rx_errors.; |
| 445 | uint64_tcxx . . $ uint64_t# tx_timestamp, rx_hw_timestamp, rx_sw_timestamp; |
| 446 | size_t expected_frame_length = l2_config->frame_lengthcxx . . $ traffic_class_config#frame_length.; |
| 447 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! out_of_order, payload_mismatch, frame_id_mismatch; |
| 448 | unsigned char new_frame[MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!]; |
| 449 | struct generic_l2_headercxx . . $ generic_l2_header# *l2; |
| 450 | uint64_tcxx . . $ uint64_t# sequence_counter; |
| 451 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! vlan_tag_missing; |
| 452 | void *p = frame_data; |
| 453 | struct ethhdrcxx . . $ ethhdr# *eth; |
| 454 | uint16_tcxx . . $ uint16_t# proto; |
| 455 | |
| 456 | if (len < sizeof(struct vlan_ethernet_headercxx . . $ vlan_ethernet_header#)) { |
| 457 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "GenericL2Rx: Too small frame received!\n"); |
| 458 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 459 | } |
| 460 | |
| 461 | eth = p; |
| 462 | if (eth->h_protocxx . . $ ethhdr#h_proto. == htonscxx . . $ htons(a18c05e4017afb3d).(ETH_P_8021Qcxx . . $ `<external>/15209105680615352250/if_ether.h:71:9`!)) { |
| 463 | struct vlan_ethernet_headercxx . . $ vlan_ethernet_header# *veth = p; |
| 464 | |
| 465 | proto = veth->vlan_encapsulated_protocxx . . $ vlan_ethernet_header#vlan_encapsulated_proto.; |
| 466 | p += sizeof(*veth); |
| 467 | vlan_tag_missing = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 468 | } else { |
| 469 | proto = eth->h_protocxx . . $ ethhdr#h_proto.; |
| 470 | p += sizeof(*eth); |
| 471 | expected_frame_length -= sizeof(struct vlan_headercxx . . $ vlan_header#); |
| 472 | vlan_tag_missing = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 473 | } |
| 474 | |
| 475 | if (proto != htonscxx . . $ htons(a18c05e4017afb3d).(l2_config->ether_typecxx . . $ traffic_class_config#ether_type.)) { |
| 476 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 477 | "GenericL2Rx: Frame with wrong Ether Type received!\n"); |
| 478 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 479 | } |
| 480 | |
| 481 | /* Check frame length: VLAN tag might be stripped or not. Check it. */ |
| 482 | if (len != expected_frame_length) { |
| 483 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "GenericL2Rx: Frame with wrong length received!\n"); |
| 484 | return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 485 | } |
| 486 | |
| 487 | /* Check cycle counter and payload. */ |
| 488 | l2 = p; |
| 489 | p += sizeof(*l2); |
| 490 | |
| 491 | sequence_counter = meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(&l2->meta_datacxx . . $ generic_l2_header#meta_data., num_frames_per_cycle); |
| 492 | |
| 493 | tx_timestamp = meta_data_to_tx_timestampcxx . . $ meta_data_to_tx_timestamp(e17106d766bd017c).(&l2->meta_datacxx . . $ generic_l2_header#meta_data.); |
| 494 | set_mirror_tx_timestampcxx . . $ set_mirror_tx_timestamp(457bf14cd1570296).(&l2->meta_datacxx . . $ generic_l2_header#meta_data.); |
| 495 | |
| 496 | xdp_get_timestamp_metadatacxx . . $ xdp_get_timestamp_metadata(cb618da060df1bcb).(frame_data, &rx_hw_timestamp, &rx_sw_timestamp); |
| 497 | out_of_order = sequence_counter != thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.; |
| 498 | payload_mismatch = memcmpcxx . . $ memcmp(36f949851efa3055).(p, expected_pattern, expected_pattern_length); |
| 499 | frame_id_mismatch = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 500 | |
| 501 | stat_frame_receivedcxx . . $ stat_frame_received(5534a29e0120fff).(GENERICL2_FRAME_TYPEcxx . . $ 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_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 507 | "GenericL2Rx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! |
| 508 | "] SequenceCounter mismatch: %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "!\n", |
| 509 | sequence_counter, thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.); |
| 510 | thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.++; |
| 511 | } |
| 512 | |
| 513 | if (payload_mismatch) |
| 514 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., |
| 515 | "GenericL2Rx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "] Payload Pattern mismatch!\n", |
| 516 | sequence_counter); |
| 517 | |
| 518 | thread_context->rx_sequence_countercxx . . $ 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_enabledcxx . . $ traffic_class_config#xdp_enabled.) { |
| 529 | /* Re-add vlan tag */ |
| 530 | if (vlan_tag_missing) |
| 531 | insert_vlan_tagcxx . . $ insert_vlan_tag(136eef189baebb4d).(frame_data, len, |
| 532 | l2_config->vidcxx . . $ traffic_class_config#vid. | l2_config->pcpcxx . . $ traffic_class_config#pcp. << VLAN_PCP_SHIFTcxx . . $ `src/net_def.h:26:9`!); |
| 533 | |
| 534 | /* Swap mac addresses inline */ |
| 535 | swap_mac_addressescxx . . $ swap_mac_addresses(5500c4293b1bbef0).(frame_data, len); |
| 536 | } else { |
| 537 | /* Build new frame for Tx with VLAN info. */ |
| 538 | build_vlan_frame_from_rxcxx . . $ build_vlan_frame_from_rx(ba210f13e41aa3ab).(frame_data, len, new_frame, sizeof(new_frame), |
| 539 | l2_config->ether_typecxx . . $ traffic_class_config#ether_type., |
| 540 | l2_config->vidcxx . . $ traffic_class_config#vid. | l2_config->pcpcxx . . $ traffic_class_config#pcp. << VLAN_PCP_SHIFTcxx . . $ `src/net_def.h:26:9`!); |
| 541 | |
| 542 | /* Store the new frame. */ |
| 543 | ring_buffer_addcxx . . $ ring_buffer_add(5f1d237704bb7e40).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer., new_frame, len + 4); |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | static void *generic_l2_rx_thread_routinecxx . . $ generic_l2_rx_thread_routine(9f162e79bfc6c74).(void *data) |
| 550 | { |
| 551 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 552 | const uint64_tcxx . . $ uint64_t# cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.; |
| 553 | struct timespeccxx . . $ timespec# wakeup_time; |
| 554 | int socket_fd, ret; |
| 555 | |
| 556 | socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.; |
| 557 | |
| 558 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(app_configcxx . . $ app_config..application_rx_base_offset_nscxx . . $ application_config#application_rx_base_offset_ns., &wakeup_time); |
| 559 | if (ret) { |
| 560 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., |
| 561 | "GenericL2Rx: Failed to calculate thread start time: %s!\n", |
| 562 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 563 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 564 | } |
| 565 | |
| 566 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 567 | struct packet_receive_requestcxx . . $ packet_receive_request# recv_req = { |
| 568 | .traffic_class = thread_context->traffic_classcxx . . $ thread_context#traffic_class., |
| 569 | .socket_fd = socket_fd, |
| 570 | .receive_function = generic_l2_rx_framecxx . . $ generic_l2_rx_frame(ddfd8079f1f43689)., |
| 571 | .data = thread_context, |
| 572 | }; |
| 573 | |
| 574 | /* Wait until next period. */ |
| 575 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 576 | |
| 577 | do { |
| 578 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 579 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 580 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 581 | |
| 582 | if (ret) { |
| 583 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Rx: clock_nanosleep() failed: %s\n", |
| 584 | strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 585 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 586 | } |
| 587 | |
| 588 | /* Receive Layer 2 frames. */ |
| 589 | packet_receive_messagescxx . . $ packet_receive_messages(dec9d97ca532b11c).(thread_context->packet_contextcxx . . $ thread_context#packet_context., &recv_req); |
| 590 | } |
| 591 | |
| 592 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 593 | } |
| 594 | |
| 595 | static void *generic_l2_xdp_rx_thread_routinecxx . . $ generic_l2_xdp_rx_thread_routine(9f162e79bfc6c74).(void *data) |
| 596 | { |
| 597 | struct thread_contextcxx . . $ thread_context# *thread_context = data; |
| 598 | const long long cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.; |
| 599 | const struct traffic_class_configcxx . . $ traffic_class_config# *l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 600 | const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.; |
| 601 | const size_t frame_length = l2_config->frame_lengthcxx . . $ traffic_class_config#frame_length.; |
| 602 | struct xdp_socketcxx . . $ xdp_socket# *xsk = thread_context->xskcxx . . $ thread_context#xsk.; |
| 603 | struct timespeccxx . . $ timespec# wakeup_time; |
| 604 | uint32_tcxx . . $ uint32_t# link_speed; |
| 605 | uint64_tcxx . . $ uint64_t# duration; |
| 606 | int ret; |
| 607 | |
| 608 | ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(app_configcxx . . $ app_config..application_rx_base_offset_nscxx . . $ application_config#application_rx_base_offset_ns., &wakeup_time); |
| 609 | if (ret) { |
| 610 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., |
| 611 | "GenericL2Rx: Failed to calculate thread start time: %s!\n", |
| 612 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 613 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 614 | } |
| 615 | |
| 616 | ret = get_interface_link_speedcxx . . $ get_interface_link_speed(6974107818eeb505).(l2_config->interfacecxx . . $ traffic_class_config#interface., &link_speed); |
| 617 | if (ret) { |
| 618 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Rx: Failed to get link speed!\n"); |
| 619 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 620 | } |
| 621 | |
| 622 | duration = tx_time_get_frame_durationcxx . . $ tx_time_get_frame_duration(b831a5b42b98fcd).(link_speed, l2_config->frame_lengthcxx . . $ traffic_class_config#frame_length.); |
| 623 | |
| 624 | while (!thread_context->stopcxx . . $ thread_context#stop.) { |
| 625 | struct xdp_tx_timecxx . . $ xdp_tx_time# tx_time = { |
| 626 | .tx_time_offset = l2_config->tx_time_offset_nscxx . . $ traffic_class_config#tx_time_offset_ns., |
| 627 | .duration = duration, |
| 628 | .num_frames_per_cycle = l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., |
| 629 | .sequence_counter_begin = 0, |
| 630 | .traffic_class = thread_context->traffic_classcxx . . $ thread_context#traffic_class., |
| 631 | }; |
| 632 | unsigned int received; |
| 633 | |
| 634 | /* Wait until next period */ |
| 635 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns); |
| 636 | |
| 637 | do { |
| 638 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, |
| 639 | &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 640 | } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!); |
| 641 | |
| 642 | if (ret) { |
| 643 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "GenericL2Rx: clock_nanosleep() failed: %s\n", |
| 644 | strerrorcxx . . $ strerror(ff224f080444ee45).(ret)); |
| 645 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 646 | } |
| 647 | |
| 648 | pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->xdp_data_mutexcxx . . $ thread_context#xdp_data_mutex.); |
| 649 | received = xdp_receive_framescxx . . $ xdp_receive_frames(a8ae63e30c683a9b).(xsk, frame_length, mirror_enabled, |
| 650 | generic_l2_rx_framecxx . . $ generic_l2_rx_frame(ddfd8079f1f43689)., thread_context, &tx_time); |
| 651 | thread_context->received_framescxx . . $ thread_context#received_frames. = received; |
| 652 | pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->xdp_data_mutexcxx . . $ thread_context#xdp_data_mutex.); |
| 653 | } |
| 654 | |
| 655 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 656 | } |
| 657 | |
| 658 | struct thread_contextcxx . . $ thread_context# *generic_l2_threads_createcxx . . $ generic_l2_threads_create(cd031c27c2e2a5f2).(void) |
| 659 | { |
| 660 | struct traffic_class_configcxx . . $ traffic_class_config# *l2_config; |
| 661 | struct thread_contextcxx . . $ thread_context# *thread_context; |
| 662 | char thread_name[128]; |
| 663 | int ret; |
| 664 | |
| 665 | thread_context = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*thread_context)); |
| 666 | if (!thread_context) |
| 667 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 668 | |
| 669 | if (!config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("GenericL2")) |
| 670 | goto out; |
| 671 | |
| 672 | thread_context->confcxx . . $ thread_context#conf. = l2_config = &app_configcxx . . $ app_config..classescxx . . $ application_config#classes.[GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE.]; |
| 673 | thread_context->frame_typecxx . . $ thread_context#frame_type. = GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE.; |
| 674 | thread_context->traffic_classcxx . . $ thread_context#traffic_class. = stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(GENERICL2_FRAME_TYPEcxx . . $ 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_enabledcxx . . $ traffic_class_config#xdp_enabled.) { |
| 678 | thread_context->packet_contextcxx . . $ thread_context#packet_context. = packet_initcxx . . $ packet_init(6f6262789a319935).(l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.); |
| 679 | if (!thread_context->packet_contextcxx . . $ thread_context#packet_context.) { |
| 680 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate GenericL2 packet context!\n"); |
| 681 | goto err_packet; |
| 682 | } |
| 683 | |
| 684 | thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data. = |
| 685 | calloccxx . . $ calloc(63a7bf2941bfcd7a).(l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!); |
| 686 | if (!thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.) { |
| 687 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate GenericL2TxFrameData\n"); |
| 688 | goto err_tx; |
| 689 | } |
| 690 | |
| 691 | thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data. = |
| 692 | calloccxx . . $ calloc(63a7bf2941bfcd7a).(l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!); |
| 693 | if (!thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.) { |
| 694 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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_enabledcxx . . $ traffic_class_config#xdp_enabled.) { |
| 701 | thread_context->socket_fdcxx . . $ thread_context#socket_fd. = 0; |
| 702 | thread_context->xskcxx . . $ thread_context#xsk. = xdp_open_socketcxx . . $ xdp_open_socket(ec858eaaf043e631).( |
| 703 | l2_config->interfacecxx . . $ traffic_class_config#interface., app_configcxx . . $ app_config..application_xdp_programcxx . . $ application_config#application_xdp_program., |
| 704 | l2_config->rx_queuecxx . . $ traffic_class_config#rx_queue., l2_config->xdp_skb_modecxx . . $ traffic_class_config#xdp_skb_mode., l2_config->xdp_zc_modecxx . . $ traffic_class_config#xdp_zc_mode., |
| 705 | l2_config->xdp_wakeup_modecxx . . $ traffic_class_config#xdp_wakeup_mode., l2_config->xdp_busy_poll_modecxx . . $ traffic_class_config#xdp_busy_poll_mode., |
| 706 | l2_config->tx_time_enabledcxx . . $ traffic_class_config#tx_time_enabled., l2_config->tx_hwtstamp_enabledcxx . . $ traffic_class_config#tx_hwtstamp_enabled.); |
| 707 | if (!thread_context->xskcxx . . $ thread_context#xsk.) { |
| 708 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create GenericL2 Xdp socket!\n"); |
| 709 | goto err_socket; |
| 710 | } |
| 711 | } else { |
| 712 | thread_context->xskcxx . . $ thread_context#xsk. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 713 | thread_context->socket_fdcxx . . $ thread_context#socket_fd. = create_generic_l2_socketcxx . . $ create_generic_l2_socket(b126dc7c1de90089).(); |
| 714 | if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. < 0) { |
| 715 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create GenericL2 Socket!\n"); |
| 716 | goto err_socket; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | init_mutexcxx . . $ init_mutex(4f83bfebf4bb9c8b).(&thread_context->xdp_data_mutexcxx . . $ thread_context#xdp_data_mutex.); |
| 721 | |
| 722 | /* Same as above. For XDP the umem area is used. */ |
| 723 | if (l2_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled. && !l2_config->xdp_enabledcxx . . $ traffic_class_config#xdp_enabled.) { |
| 724 | /* Per period the expectation is: GenericL2NumFramesPerCycle * MAX_FRAME */ |
| 725 | thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer. = |
| 726 | ring_buffer_allocatecxx . . $ ring_buffer_allocate(a00a27d6b402ebff).(MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * l2_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.); |
| 727 | if (!thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.) { |
| 728 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate GenericL2 Mirror RingBuffer!\n"); |
| 729 | goto err_buffer; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(thread_name, sizeof(thread_name), "%sTxThread", l2_config->namecxx . . $ traffic_class_config#name.); |
| 734 | |
| 735 | ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., thread_name, |
| 736 | l2_config->tx_thread_prioritycxx . . $ traffic_class_config#tx_thread_priority., l2_config->tx_thread_cpucxx . . $ traffic_class_config#tx_thread_cpu., |
| 737 | l2_config->xdp_enabledcxx . . $ traffic_class_config#xdp_enabled. ? generic_l2_xdp_tx_thread_routinecxx . . $ generic_l2_xdp_tx_thread_routine(9f162e79bfc6c74). |
| 738 | : generic_l2_tx_thread_routinecxx . . $ generic_l2_tx_thread_routine(9f162e79bfc6c74)., |
| 739 | thread_context); |
| 740 | if (ret) { |
| 741 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create GenericL2 Tx Thread!\n"); |
| 742 | goto err_thread; |
| 743 | } |
| 744 | |
| 745 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(thread_name, sizeof(thread_name), "%sRxThread", l2_config->namecxx . . $ traffic_class_config#name.); |
| 746 | |
| 747 | ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->rx_task_idcxx . . $ thread_context#rx_task_id., thread_name, |
| 748 | l2_config->rx_thread_prioritycxx . . $ traffic_class_config#rx_thread_priority., l2_config->rx_thread_cpucxx . . $ traffic_class_config#rx_thread_cpu., |
| 749 | l2_config->xdp_enabledcxx . . $ traffic_class_config#xdp_enabled. ? generic_l2_xdp_rx_thread_routinecxx . . $ generic_l2_xdp_rx_thread_routine(9f162e79bfc6c74). |
| 750 | : generic_l2_rx_thread_routinecxx . . $ generic_l2_rx_thread_routine(9f162e79bfc6c74)., |
| 751 | thread_context); |
| 752 | if (ret) { |
| 753 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create GenericL2 Rx Thread!\n"); |
| 754 | goto err_thread_rx; |
| 755 | } |
| 756 | |
| 757 | thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset. = |
| 758 | get_meta_data_offsetcxx . . $ get_meta_data_offset(16cb9e606c8e3615).(GENERICL2_FRAME_TYPEcxx . . $ GENERICL2_FRAME_TYPE., SECURITY_MODE_NONEcxx . . $ SECURITY_MODE_NONE.); |
| 759 | |
| 760 | out: |
| 761 | return thread_context; |
| 762 | |
| 763 | err_thread_rx: |
| 764 | thread_context->stopcxx . . $ thread_context#stop. = 1; |
| 765 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 766 | err_thread: |
| 767 | ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.); |
| 768 | err_buffer: |
| 769 | if (thread_context->socket_fdcxx . . $ thread_context#socket_fd.) |
| 770 | closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.); |
| 771 | if (thread_context->xskcxx . . $ thread_context#xsk.) |
| 772 | xdp_close_socketcxx . . $ xdp_close_socket(fc530d27d072e0f5).(thread_context->xskcxx . . $ thread_context#xsk., l2_config->interfacecxx . . $ traffic_class_config#interface., |
| 773 | l2_config->xdp_skb_modecxx . . $ traffic_class_config#xdp_skb_mode.); |
| 774 | err_socket: |
| 775 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.); |
| 776 | err_rx: |
| 777 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.); |
| 778 | err_tx: |
| 779 | packet_freecxx . . $ packet_free(b20c0f701b2a2d37).(thread_context->packet_contextcxx . . $ thread_context#packet_context.); |
| 780 | err_packet: |
| 781 | freecxx . . $ free(5d22bdacc48458e8).(thread_context); |
| 782 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 783 | } |
| 784 | |
| 785 | void generic_l2_threads_freecxx . . $ generic_l2_threads_free(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 786 | { |
| 787 | struct traffic_class_configcxx . . $ traffic_class_config# *l2_config; |
| 788 | |
| 789 | if (!thread_context) |
| 790 | return; |
| 791 | |
| 792 | l2_config = thread_context->confcxx . . $ thread_context#conf.; |
| 793 | |
| 794 | ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.); |
| 795 | |
| 796 | packet_freecxx . . $ packet_free(b20c0f701b2a2d37).(thread_context->packet_contextcxx . . $ thread_context#packet_context.); |
| 797 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.); |
| 798 | freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.); |
| 799 | |
| 800 | if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. > 0) |
| 801 | closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.); |
| 802 | |
| 803 | if (thread_context->xskcxx . . $ thread_context#xsk.) |
| 804 | xdp_close_socketcxx . . $ xdp_close_socket(fc530d27d072e0f5).(thread_context->xskcxx . . $ thread_context#xsk., l2_config->interfacecxx . . $ traffic_class_config#interface., |
| 805 | l2_config->xdp_skb_modecxx . . $ traffic_class_config#xdp_skb_mode.); |
| 806 | |
| 807 | freecxx . . $ free(5d22bdacc48458e8).(thread_context); |
| 808 | } |
| 809 | |
| 810 | void generic_l2_threads_wait_for_finishcxx . . $ generic_l2_threads_wait_for_finish(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context) |
| 811 | { |
| 812 | if (!thread_context) |
| 813 | return; |
| 814 | |
| 815 | if (thread_context->rx_task_idcxx . . $ thread_context#rx_task_id.) |
| 816 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->rx_task_idcxx . . $ thread_context#rx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 817 | if (thread_context->tx_task_idcxx . . $ thread_context#tx_task_id.) |
| 818 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 819 | } |