| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/packet.c`/ |
| 2 | /* |
| 3 | * Copyright (C) 2024-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include <linux/if_packet.h> |
| 12 | |
| 13 | #include "log.h"cxx . . $ `<file>/src/log.h`/ |
| 14 | #include "packet.h"cxx . . $ `<file>/src/packet.h`/ |
| 15 | #include "thread.h"cxx . . $ `<file>/src/thread.h`/ |
| 16 | #include "tx_time.h"cxx . . $ `<file>/src/tx_time.h`/ |
| 17 | #include "utils.h"cxx . . $ `<file>/src/utils.h`/ |
| 18 | |
| 19 | struct packet_contextcxx . . $ packet_context# *packet_initcxx . . $ packet_init(6f6262789a319935).(size_t num_frames_per_cycle) |
| 20 | { |
| 21 | struct packet_contextcxx . . $ packet_context# *context; |
| 22 | |
| 23 | context = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*context)); |
| 24 | if (!context) { |
| 25 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate packet context!\n"); |
| 26 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 27 | } |
| 28 | |
| 29 | context->rx_framescxx . . $ packet_context#rx_frames. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * num_frames_per_cycle, sizeof(unsigned char)); |
| 30 | if (!context->rx_framescxx . . $ packet_context#rx_frames.) { |
| 31 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate receive frame buffers!\n"); |
| 32 | goto err_rx_frames; |
| 33 | } |
| 34 | |
| 35 | context->rx_iovecscxx . . $ packet_context#rx_iovecs. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_frames_per_cycle, sizeof(*context->rx_iovecscxx . . $ packet_context#rx_iovecs.)); |
| 36 | if (!context->rx_iovecscxx . . $ packet_context#rx_iovecs.) { |
| 37 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate receive io vectors!\n"); |
| 38 | goto err_rx_iovecs; |
| 39 | } |
| 40 | |
| 41 | context->rx_msgscxx . . $ packet_context#rx_msgs. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_frames_per_cycle, sizeof(*context->rx_msgscxx . . $ packet_context#rx_msgs.)); |
| 42 | if (!context->rx_msgscxx . . $ packet_context#rx_msgs.) { |
| 43 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate receive messages!\n"); |
| 44 | goto err_rx_msgs; |
| 45 | } |
| 46 | |
| 47 | context->tx_iovecscxx . . $ packet_context#tx_iovecs. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_frames_per_cycle, sizeof(*context->tx_iovecscxx . . $ packet_context#tx_iovecs.)); |
| 48 | if (!context->tx_iovecscxx . . $ packet_context#tx_iovecs.) { |
| 49 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate transmit io vectors!\n"); |
| 50 | goto err_tx_iovecs; |
| 51 | } |
| 52 | |
| 53 | context->tx_msgscxx . . $ packet_context#tx_msgs. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_frames_per_cycle, sizeof(*context->tx_msgscxx . . $ packet_context#tx_msgs.)); |
| 54 | if (!context->tx_msgscxx . . $ packet_context#tx_msgs.) { |
| 55 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate transmit messages!\n"); |
| 56 | goto err_tx_msgs; |
| 57 | } |
| 58 | |
| 59 | context->tx_control_msgscxx . . $ packet_context#tx_control_msgs. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(num_frames_per_cycle, sizeof(*context->tx_control_msgscxx . . $ packet_context#tx_control_msgs.)); |
| 60 | if (!context->tx_control_msgscxx . . $ packet_context#tx_control_msgs.) { |
| 61 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocated transmit control messages!\n"); |
| 62 | goto err_tx_ctrl_msgs; |
| 63 | } |
| 64 | |
| 65 | context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle. = num_frames_per_cycle; |
| 66 | |
| 67 | return context; |
| 68 | |
| 69 | err_tx_ctrl_msgs: |
| 70 | freecxx . . $ free(5d22bdacc48458e8).(context->tx_msgscxx . . $ packet_context#tx_msgs.); |
| 71 | err_tx_msgs: |
| 72 | freecxx . . $ free(5d22bdacc48458e8).(context->tx_iovecscxx . . $ packet_context#tx_iovecs.); |
| 73 | err_tx_iovecs: |
| 74 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_msgscxx . . $ packet_context#rx_msgs.); |
| 75 | err_rx_msgs: |
| 76 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_iovecscxx . . $ packet_context#rx_iovecs.); |
| 77 | err_rx_iovecs: |
| 78 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_framescxx . . $ packet_context#rx_frames.); |
| 79 | err_rx_frames: |
| 80 | freecxx . . $ free(5d22bdacc48458e8).(context); |
| 81 | |
| 82 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 83 | } |
| 84 | |
| 85 | void packet_freecxx . . $ packet_free(b20c0f701b2a2d37).(struct packet_contextcxx . . $ packet_context# *context) |
| 86 | { |
| 87 | if (!context) |
| 88 | return; |
| 89 | |
| 90 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_framescxx . . $ packet_context#rx_frames.); |
| 91 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_iovecscxx . . $ packet_context#rx_iovecs.); |
| 92 | freecxx . . $ free(5d22bdacc48458e8).(context->tx_iovecscxx . . $ packet_context#tx_iovecs.); |
| 93 | freecxx . . $ free(5d22bdacc48458e8).(context->rx_msgscxx . . $ packet_context#rx_msgs.); |
| 94 | freecxx . . $ free(5d22bdacc48458e8).(context->tx_msgscxx . . $ packet_context#tx_msgs.); |
| 95 | freecxx . . $ free(5d22bdacc48458e8).(context->tx_control_msgscxx . . $ packet_context#tx_control_msgs.); |
| 96 | freecxx . . $ free(5d22bdacc48458e8).(context); |
| 97 | } |
| 98 | |
| 99 | int packet_send_messagescxx . . $ packet_send_messages(db4d6f7226235983).(struct packet_contextcxx . . $ packet_context# *context, struct packet_send_requestcxx . . $ packet_send_request# *send_req) |
| 100 | { |
| 101 | int sent = 0; |
| 102 | |
| 103 | /* |
| 104 | * It is expected to sent exactly num_frames_per_cycle. However, in case frames arrived late |
| 105 | * or were dropped the number might be different. |
| 106 | */ |
| 107 | while (sent < send_req->num_framescxx . . $ packet_send_request#num_frames.) { |
| 108 | struct ioveccxx . . $ iovec# *iovecs = context->tx_iovecscxx . . $ packet_context#tx_iovecs.; |
| 109 | struct mmsghdrcxx . . $ mmsghdr# *msgs = context->tx_msgscxx . . $ packet_context#tx_msgs.; |
| 110 | int i, inner_sent = 0, to_be_sent; |
| 111 | |
| 112 | to_be_sent = send_req->num_framescxx . . $ packet_send_request#num_frames. - sent; |
| 113 | if (to_be_sent > context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle.) |
| 114 | to_be_sent = context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle.; |
| 115 | |
| 116 | for (i = 0; i < to_be_sent; i++) { |
| 117 | unsigned char *frame; |
| 118 | int idx; |
| 119 | |
| 120 | idx = sent + i; |
| 121 | frame = send_req->mirror_enabledcxx . . $ packet_send_request#mirror_enabled. |
| 122 | ? send_req->frame_datacxx . . $ packet_send_request#frame_data. + idx * send_req->frame_lengthcxx . . $ packet_send_request#frame_length. |
| 123 | : frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(send_req->frame_datacxx . . $ packet_send_request#frame_data., idx); |
| 124 | iovecs[i].iov_basecxx . . $ iovec#iov_base. = frame; |
| 125 | iovecs[i].iov_lencxx . . $ iovec#iov_len. = send_req->frame_lengthcxx . . $ packet_send_request#frame_length.; |
| 126 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_iovcxx . . $ msghdr#msg_iov. = &iovecs[i]; |
| 127 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_iovlencxx . . $ msghdr#msg_iovlen. = 1; |
| 128 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_namecxx . . $ msghdr#msg_name. = send_req->destinationcxx . . $ packet_send_request#destination.; |
| 129 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_namelencxx . . $ msghdr#msg_namelen. = sizeof(*send_req->destinationcxx . . $ packet_send_request#destination.); |
| 130 | |
| 131 | /* In case the user configured Tx Time also add it. */ |
| 132 | if (send_req->tx_time_enabledcxx . . $ packet_send_request#tx_time_enabled.) { |
| 133 | unsigned char *control = context->tx_control_msgscxx . . $ packet_context#tx_control_msgs.[i].controlcxx . . $ tx_control_msg#control.; |
| 134 | uint64_tcxx . . $ uint64_t# tx_time, sequence_counter; |
| 135 | struct cmsghdrcxx . . $ cmsghdr# *cmsg; |
| 136 | |
| 137 | sequence_counter = |
| 138 | get_sequence_countercxx . . $ get_sequence_counter(9512b04a879eebb0).(frame, send_req->meta_data_offsetcxx . . $ packet_send_request#meta_data_offset., |
| 139 | context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle.); |
| 140 | |
| 141 | tx_time = tx_time_get_frame_tx_timecxx . . $ tx_time_get_frame_tx_time(5922d6685f565d0d).( |
| 142 | sequence_counter, send_req->durationcxx . . $ packet_send_request#duration., |
| 143 | context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle., send_req->tx_time_offsetcxx . . $ packet_send_request#tx_time_offset., |
| 144 | send_req->traffic_classcxx . . $ packet_send_request#traffic_class.); |
| 145 | |
| 146 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_controlcxx . . $ msghdr#msg_control. = control; |
| 147 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_controllencxx . . $ msghdr#msg_controllen. = sizeof(*context->tx_control_msgscxx . . $ packet_context#tx_control_msgs.); |
| 148 | |
| 149 | cmsg = CMSG_FIRSTHDRcxx . . $ `<external>/8962547315054503424/socket.h:304:9`!(&msgs[i].msg_hdr); |
| 150 | cmsg->cmsg_levelcxx . . $ cmsghdr#cmsg_level. = SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!; |
| 151 | cmsg->cmsg_typecxx . . $ cmsghdr#cmsg_type. = SO_TXTIMEcxx . . $ `<external>/9030916627833342711/socket.h:104:9`!; |
| 152 | cmsg->cmsg_lencxx . . $ cmsghdr#cmsg_len. = CMSG_LENcxx . . $ `<external>/8962547315054503424/socket.h:311:9`!(sizeof(int64_t)); |
| 153 | *((uint64_tcxx . . $ uint64_t# *)CMSG_DATAcxx . . $ `<external>/8962547315054503424/socket.h:299:10`!(cmsg)) = tx_time; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Send them. */ |
| 158 | while (inner_sent < to_be_sent) { |
| 159 | int len; |
| 160 | |
| 161 | len = sendmmsgcxx . . $ sendmmsg(db0e43d248b96fdd).(send_req->socket_fdcxx . . $ packet_send_request#socket_fd., &msgs[inner_sent], |
| 162 | to_be_sent - inner_sent, 0); |
| 163 | if (len == -1) { |
| 164 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sTx: sendmmsg() failed: %s\n", |
| 165 | send_req->traffic_classcxx . . $ packet_send_request#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 166 | return -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 167 | } |
| 168 | |
| 169 | inner_sent += len; |
| 170 | } |
| 171 | |
| 172 | sent += inner_sent; |
| 173 | } |
| 174 | |
| 175 | return sent; |
| 176 | } |
| 177 | |
| 178 | int packet_receive_messagescxx . . $ packet_receive_messages(dec9d97ca532b11c).(struct packet_contextcxx . . $ packet_context# *context, struct packet_receive_requestcxx . . $ packet_receive_request# *recv_req) |
| 179 | { |
| 180 | int received = 0; |
| 181 | |
| 182 | while (truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!) { |
| 183 | struct ioveccxx . . $ iovec# *iovecs = context->rx_iovecscxx . . $ packet_context#rx_iovecs.; |
| 184 | struct mmsghdrcxx . . $ mmsghdr# *msgs = context->rx_msgscxx . . $ packet_context#rx_msgs.; |
| 185 | int i, len; |
| 186 | |
| 187 | for (i = 0; i < context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle.; i++) { |
| 188 | iovecs[i].iov_basecxx . . $ iovec#iov_base. = frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(context->rx_framescxx . . $ packet_context#rx_frames., i); |
| 189 | iovecs[i].iov_lencxx . . $ iovec#iov_len. = MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!; |
| 190 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_iovcxx . . $ msghdr#msg_iov. = &iovecs[i]; |
| 191 | msgs[i].msg_hdrcxx . . $ mmsghdr#msg_hdr..msg_iovlencxx . . $ msghdr#msg_iovlen. = 1; |
| 192 | } |
| 193 | |
| 194 | len = recvmmsgcxx . . $ recvmmsg(b9756078101de375).(recv_req->socket_fdcxx . . $ packet_receive_request#socket_fd., msgs, context->num_frames_per_cyclecxx . . $ packet_context#num_frames_per_cycle., 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 195 | if (len == -1) { |
| 196 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`!) { |
| 197 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "%sRx: recvmmsg() failed: %s\n", |
| 198 | recv_req->traffic_classcxx . . $ packet_receive_request#traffic_class., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 199 | continue; |
| 200 | } else { |
| 201 | /* No more frames. Comeback within next period. */ |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* Process received frames. */ |
| 207 | for (i = 0; i < len; i++) |
| 208 | recv_req->receive_functioncxx . . $ packet_receive_request#receive_function.(recv_req->datacxx . . $ packet_receive_request#data., frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(context->rx_framescxx . . $ packet_context#rx_frames., i), |
| 209 | msgs[i].msg_lencxx . . $ mmsghdr#msg_len.); |
| 210 | |
| 211 | received += len; |
| 212 | } |
| 213 | |
| 214 | return received; |
| 215 | } |