| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 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" |
| 14 | #include "packet.h" |
| 15 | #include "thread.h" |
| 16 | #include "tx_time.h" |
| 17 | #include "utils.h" |
| 18 | |
| 19 | struct packet_context *packet_init(size_t num_frames_per_cycle) |
| 20 | { |
| 21 | struct packet_context *context; |
| 22 | |
| 23 | context = calloc(1, sizeof(*context)); |
| 24 | if (!context) { |
| 25 | fprintf(stderr, "Failed to allocate packet context!\n"); |
| 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | context->rx_frames = calloc(MAX_FRAME_SIZE * num_frames_per_cycle, sizeof(unsigned char)); |
| 30 | if (!context->rx_frames) { |
| 31 | fprintf(stderr, "Failed to allocate receive frame buffers!\n"); |
| 32 | goto err_rx_frames; |
| 33 | } |
| 34 | |
| 35 | context->rx_iovecs = calloc(num_frames_per_cycle, sizeof(*context->rx_iovecs)); |
| 36 | if (!context->rx_iovecs) { |
| 37 | fprintf(stderr, "Failed to allocate receive io vectors!\n"); |
| 38 | goto err_rx_iovecs; |
| 39 | } |
| 40 | |
| 41 | context->rx_msgs = calloc(num_frames_per_cycle, sizeof(*context->rx_msgs)); |
| 42 | if (!context->rx_msgs) { |
| 43 | fprintf(stderr, "Failed to allocate receive messages!\n"); |
| 44 | goto err_rx_msgs; |
| 45 | } |
| 46 | |
| 47 | context->tx_iovecs = calloc(num_frames_per_cycle, sizeof(*context->tx_iovecs)); |
| 48 | if (!context->tx_iovecs) { |
| 49 | fprintf(stderr, "Failed to allocate transmit io vectors!\n"); |
| 50 | goto err_tx_iovecs; |
| 51 | } |
| 52 | |
| 53 | context->tx_msgs = calloc(num_frames_per_cycle, sizeof(*context->tx_msgs)); |
| 54 | if (!context->tx_msgs) { |
| 55 | fprintf(stderr, "Failed to allocate transmit messages!\n"); |
| 56 | goto err_tx_msgs; |
| 57 | } |
| 58 | |
| 59 | context->tx_control_msgs = calloc(num_frames_per_cycle, sizeof(*context->tx_control_msgs)); |
| 60 | if (!context->tx_control_msgs) { |
| 61 | fprintf(stderr, "Failed to allocated transmit control messages!\n"); |
| 62 | goto err_tx_ctrl_msgs; |
| 63 | } |
| 64 | |
| 65 | context->num_frames_per_cycle = num_frames_per_cycle; |
| 66 | |
| 67 | return context; |
| 68 | |
| 69 | err_tx_ctrl_msgs: |
| 70 | free(context->tx_msgs); |
| 71 | err_tx_msgs: |
| 72 | free(context->tx_iovecs); |
| 73 | err_tx_iovecs: |
| 74 | free(context->rx_msgs); |
| 75 | err_rx_msgs: |
| 76 | free(context->rx_iovecs); |
| 77 | err_rx_iovecs: |
| 78 | free(context->rx_frames); |
| 79 | err_rx_frames: |
| 80 | free(context); |
| 81 | |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | void packet_free(struct packet_context *context) |
| 86 | { |
| 87 | if (!context) |
| 88 | return; |
| 89 | |
| 90 | free(context->rx_frames); |
| 91 | free(context->rx_iovecs); |
| 92 | free(context->tx_iovecs); |
| 93 | free(context->rx_msgs); |
| 94 | free(context->tx_msgs); |
| 95 | free(context->tx_control_msgs); |
| 96 | free(context); |
| 97 | } |
| 98 | |
| 99 | int packet_send_messages(struct packet_context *context, struct 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_frames) { |
| 108 | struct iovec *iovecs = context->tx_iovecs; |
| 109 | struct mmsghdr *msgs = context->tx_msgs; |
| 110 | int i, inner_sent = 0, to_be_sent; |
| 111 | |
| 112 | to_be_sent = send_req->num_frames - sent; |
| 113 | if (to_be_sent > context->num_frames_per_cycle) |
| 114 | to_be_sent = 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_enabled |
| 122 | ? send_req->frame_data + idx * send_req->frame_length |
| 123 | : frame_idx(send_req->frame_data, idx); |
| 124 | iovecs[i].iov_base = frame; |
| 125 | iovecs[i].iov_len = send_req->frame_length; |
| 126 | msgs[i].msg_hdr.msg_iov = &iovecs[i]; |
| 127 | msgs[i].msg_hdr.msg_iovlen = 1; |
| 128 | msgs[i].msg_hdr.msg_name = send_req->destination; |
| 129 | msgs[i].msg_hdr.msg_namelen = sizeof(*send_req->destination); |
| 130 | |
| 131 | /* In case the user configured Tx Time also add it. */ |
| 132 | if (send_req->tx_time_enabled) { |
| 133 | unsigned char *control = context->tx_control_msgs[i].control; |
| 134 | uint64_t tx_time, sequence_counter; |
| 135 | struct cmsghdr *cmsg; |
| 136 | |
| 137 | sequence_counter = |
| 138 | get_sequence_counter(frame, send_req->meta_data_offset, |
| 139 | context->num_frames_per_cycle); |
| 140 | |
| 141 | tx_time = tx_time_get_frame_tx_time( |
| 142 | sequence_counter, send_req->duration, |
| 143 | context->num_frames_per_cycle, send_req->tx_time_offset, |
| 144 | send_req->traffic_class); |
| 145 | |
| 146 | msgs[i].msg_hdr.msg_control = control; |
| 147 | msgs[i].msg_hdr.msg_controllen = sizeof(*context->tx_control_msgs); |
| 148 | |
| 149 | cmsg = CMSG_FIRSTHDR(&msgs[i].msg_hdr); |
| 150 | cmsg->cmsg_level = SOL_SOCKET; |
| 151 | cmsg->cmsg_type = SO_TXTIME; |
| 152 | cmsg->cmsg_len = CMSG_LEN(sizeof(int64_t)); |
| 153 | *((uint64_t *)CMSG_DATA(cmsg)) = tx_time; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Send them. */ |
| 158 | while (inner_sent < to_be_sent) { |
| 159 | int len; |
| 160 | |
| 161 | len = sendmmsg(send_req->socket_fd, &msgs[inner_sent], |
| 162 | to_be_sent - inner_sent, 0); |
| 163 | if (len == -1) { |
| 164 | log_message(LOG_LEVEL_ERROR, "%sTx: sendmmsg() failed: %s\n", |
| 165 | send_req->traffic_class, strerror(errno)); |
| 166 | return -errno; |
| 167 | } |
| 168 | |
| 169 | inner_sent += len; |
| 170 | } |
| 171 | |
| 172 | sent += inner_sent; |
| 173 | } |
| 174 | |
| 175 | return sent; |
| 176 | } |
| 177 | |
| 178 | int packet_receive_messages(struct packet_context *context, struct packet_receive_request *recv_req) |
| 179 | { |
| 180 | int received = 0; |
| 181 | |
| 182 | while (true) { |
| 183 | struct iovec *iovecs = context->rx_iovecs; |
| 184 | struct mmsghdr *msgs = context->rx_msgs; |
| 185 | int i, len; |
| 186 | |
| 187 | for (i = 0; i < context->num_frames_per_cycle; i++) { |
| 188 | iovecs[i].iov_base = frame_idx(context->rx_frames, i); |
| 189 | iovecs[i].iov_len = MAX_FRAME_SIZE; |
| 190 | msgs[i].msg_hdr.msg_iov = &iovecs[i]; |
| 191 | msgs[i].msg_hdr.msg_iovlen = 1; |
| 192 | } |
| 193 | |
| 194 | len = recvmmsg(recv_req->socket_fd, msgs, context->num_frames_per_cycle, 0, NULL); |
| 195 | if (len == -1) { |
| 196 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 197 | log_message(LOG_LEVEL_ERROR, "%sRx: recvmmsg() failed: %s\n", |
| 198 | recv_req->traffic_class, strerror(errno)); |
| 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_function(recv_req->data, frame_idx(context->rx_frames, i), |
| 209 | msgs[i].msg_len); |
| 210 | |
| 211 | received += len; |
| 212 | } |
| 213 | |
| 214 | return received; |
| 215 | } |