Debian Codegraph @

src/udp_thread.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2020-2024 Linutronix GmbH
4 * Author Kurt Kanzenbach <[email protected]>
5 */
6
7 #include <pthread.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <arpa/inet.h>
15
16 #include <linux/if_ether.h>
17 #include <linux/if_link.h>
18 #include <linux/if_vlan.h>
19
20 #include "config.h"
21 #include "log.h"
22 #include "net.h"
23 #include "net_def.h"
24 #include "stat.h"
25 #include "thread.h"
26 #include "udp_thread.h"
27 #include "utils.h"
28
29 static void udp_initialize_frame(struct thread_context *thread_context, unsigned char *frame_data)
30 {
31 const struct traffic_class_config *udp_config = thread_context->conf;
32 struct reference_meta_data *meta;
33
34 /*
35 * UdpFrame:
36 * Cycle counter
37 * Payload
38 * Padding to maxFrame
39 */
40
41 /* Payload: SequenceCounter + Data */
42 meta = (struct reference_meta_data *)frame_data;
43 memset(meta, '\0', sizeof(*meta));
44 memcpy(frame_data + sizeof(*meta), udp_config->payload_pattern,
45 udp_config->payload_pattern_length);
46
47 /* Padding: '\0' */
48 }
49
50 static void udp_send_frame(struct thread_context *thread_context, const unsigned char *frame_data,
51 size_t frame_length, size_t num_frames_per_cycle, int socket_fd,
52 const struct sockaddr_storage *destination)
53 {
54 struct reference_meta_data *meta;
55 struct timespec tx_time = {};
56 uint64_t sequence_counter;
57 ssize_t ret = -1;
58
59 clock_gettime(app_config.application_clock_id, &tx_time);
60
61 /* Fetch meta data */
62 meta = (struct reference_meta_data *)frame_data;
63 sequence_counter = meta_data_to_sequence_counter(meta, num_frames_per_cycle);
64
65 tx_timestamp_to_meta_data(meta, ts_to_ns(&tx_time));
66
67 /* Send it */
68 switch (destination->ss_family) {
69 case AF_INET:
70 ret = sendto(socket_fd, frame_data, frame_length, 0,
71 (struct sockaddr_in *)destination, sizeof(struct sockaddr_in));
72 break;
73 case AF_INET6:
74 ret = sendto(socket_fd, frame_data, frame_length, 0,
75 (struct sockaddr_in6 *)destination, sizeof(struct sockaddr_in6));
76 break;
77 }
78 if (ret < 0) {
79 log_message(LOG_LEVEL_ERROR, "%sTx: send() for %" PRIu64 " failed: %s\n",
80 thread_context->traffic_class, sequence_counter, strerror(errno));
81 return;
82 }
83
84 stat_frame_sent(thread_context->frame_type, sequence_counter);
85 }
86
87 static void udp_gen_and_send_frame(struct thread_context *thread_context, unsigned char *frame_data,
88 size_t frame_length, size_t num_frames_per_cycle, int socket_fd,
89 uint64_t sequence_counter,
90 const struct sockaddr_storage *destination)
91 {
92 struct reference_meta_data *meta;
93 struct timespec tx_time = {};
94 ssize_t ret = -1;
95
96 clock_gettime(app_config.application_clock_id, &tx_time);
97
98 /* Adjust meta data */
99 meta = (struct reference_meta_data *)frame_data;
100 sequence_counter_to_meta_data(meta, sequence_counter, num_frames_per_cycle);
101
102 tx_timestamp_to_meta_data(meta, ts_to_ns(&tx_time));
103
104 /* Send it */
105 switch (destination->ss_family) {
106 case AF_INET:
107 ret = sendto(socket_fd, frame_data, frame_length, 0,
108 (struct sockaddr_in *)destination, sizeof(struct sockaddr_in));
109 break;
110 case AF_INET6:
111 ret = sendto(socket_fd, frame_data, frame_length, 0,
112 (struct sockaddr_in6 *)destination, sizeof(struct sockaddr_in6));
113 break;
114 default:
115 ret = -EINVAL;
116 }
117 if (ret < 0) {
118 log_message(LOG_LEVEL_ERROR, "%sTx: send() for %" PRIu64 " failed: %s\n",
119 thread_context->traffic_class, sequence_counter, strerror(errno));
120 return;
121 }
122
123 stat_frame_sent(thread_context->frame_type, sequence_counter);
124 }
125
126 static void *udp_tx_thread_routine(void *data)
127 {
128 struct thread_context *thread_context = data;
129 const struct traffic_class_config *udp_config = thread_context->conf;
130 size_t received_frames_length = MAX_FRAME_SIZE * udp_config->num_frames_per_cycle;
131 unsigned char *received_frames = thread_context->rx_frame_data;
132 const bool mirror_enabled = udp_config->rx_mirror_enabled;
133 pthread_mutex_t *mutex = &thread_context->data_mutex;
134 pthread_cond_t *cond = &thread_context->data_cond_var;
135 uint64_t sequence_counter = 0;
136 unsigned char *frame;
137 int socket_fd;
138
139 socket_fd = thread_context->socket_fd;
140 frame = thread_context->tx_frame_data;
141
142 udp_initialize_frame(thread_context, frame);
143
144 while (!thread_context->stop) {
145 struct timespec timeout;
146 size_t num_frames, i;
147 int ret;
148
149 /*
150 * Wait until signalled. These UDP frames have to be sent after the LLDP
151 * frames. Therefore, the LLDP or UDP High TxThread signals this one here.
152 */
153 clock_gettime(CLOCK_MONOTONIC, &timeout);
154 timeout.tv_sec++;
155
156 pthread_mutex_lock(mutex);
157 ret = pthread_cond_timedwait(cond, mutex, &timeout);
158 num_frames = thread_context->num_frames_available;
159 thread_context->num_frames_available = 0;
160 pthread_mutex_unlock(mutex);
161
162 /* In case of shutdown a signal may be missing. */
163 if (ret == ETIMEDOUT)
164 continue;
165
166 /*
167 * Send UdpFrames, two possibilites:
168 * a) Generate it, or
169 * b) Use received ones if mirror enabled
170 */
171 if (!mirror_enabled) {
172 /* Send UdpFrames */
173 for (i = 0; i < num_frames; ++i)
174 udp_gen_and_send_frame(
175 thread_context, frame, udp_config->frame_length,
176 udp_config->num_frames_per_cycle, socket_fd,
177 sequence_counter++, &thread_context->destination);
178 } else {
179 size_t len;
180
181 ring_buffer_fetch(thread_context->mirror_buffer, received_frames,
182 received_frames_length, &len);
183
184 /* Len should be a multiple of frame size */
185 for (i = 0; i < len / udp_config->frame_length; ++i)
186 udp_send_frame(thread_context,
187 received_frames + i * udp_config->frame_length,
188 udp_config->frame_length,
189 udp_config->num_frames_per_cycle, socket_fd,
190 &thread_context->destination);
191
192 pthread_mutex_lock(&thread_context->data_mutex);
193 thread_context->num_frames_available = 0;
194 pthread_mutex_unlock(&thread_context->data_mutex);
195 }
196
197 /* Signal next Tx thread */
198 if (thread_context->next) {
199 pthread_mutex_lock(&thread_context->next->data_mutex);
200 pthread_cond_signal(&thread_context->next->data_cond_var);
201 pthread_mutex_unlock(&thread_context->next->data_mutex);
202 }
203
204 if (thread_context->is_last)
205 stat_update();
206 }
207
208 return NULL;
209 }
210
211 static void *udp_rx_thread_routine(void *data)
212 {
213 struct thread_context *thread_context = data;
214 const struct traffic_class_config *udp_config = thread_context->conf;
215 const unsigned char *expected_pattern = (const unsigned char *)udp_config->payload_pattern;
216 const size_t expected_pattern_length = udp_config->payload_pattern_length;
217 const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns;
218 const size_t num_frames_per_cycle = udp_config->num_frames_per_cycle;
219 const bool mirror_enabled = udp_config->rx_mirror_enabled;
220 const bool ignore_rx_errors = udp_config->ignore_rx_errors;
221 const ssize_t frame_length = udp_config->frame_length;
222 unsigned char frame[MAX_FRAME_SIZE];
223 uint64_t sequence_counter = 0;
224 struct timespec wakeup_time;
225 uint64_t tx_timestamp;
226 int socket_fd, ret;
227
228 socket_fd = thread_context->socket_fd;
229
230 ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time);
231 if (ret) {
232 log_message(LOG_LEVEL_ERROR, "%sRx: Failed to calculate thread start time: %s!\n",
233 thread_context->traffic_class, strerror(errno));
234 return NULL;
235 }
236
237 while (!thread_context->stop) {
238 /* Wait until next period. */
239 increment_period(&wakeup_time, cycle_time_ns);
240
241 do {
242 ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME,
243 &wakeup_time, NULL);
244 } while (ret == EINTR);
245
246 if (ret) {
247 log_message(LOG_LEVEL_ERROR, "%sRx: clock_nanosleep() failed: %s\n",
248 thread_context->traffic_class, strerror(ret));
249 return NULL;
250 }
251
252 /* Receive Udp packets. */
253 while (true) {
254 bool out_of_order, payload_mismatch, frame_id_mismatch;
255 struct reference_meta_data *meta;
256 uint64_t rx_sequence_counter;
257 ssize_t len;
258
259 len = recv(socket_fd, frame, sizeof(frame), 0);
260 if (len == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
261 log_message(LOG_LEVEL_ERROR, "%sRx: recv() failed: %s\n",
262 thread_context->traffic_class, strerror(errno));
263 continue;
264 }
265 if (len == 0)
266 continue;
267
268 /* No more frames. Comeback within next period. */
269 if (len == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
270 break;
271
272 /* Process received packet. */
273 if (len != frame_length) {
274 log_message(LOG_LEVEL_WARNING,
275 "%sRx: Frame with wrong length received!\n",
276 thread_context->traffic_class);
277 continue;
278 }
279
280 /*
281 * Check cycle counter and payload. The ether type is checked by the
282 * attached BPF filter.
283 */
284 meta = (struct reference_meta_data *)frame;
285 rx_sequence_counter =
286 meta_data_to_sequence_counter(meta, num_frames_per_cycle);
287
288 tx_timestamp = meta_data_to_tx_timestamp(meta);
289 set_mirror_tx_timestamp(meta);
290
291 out_of_order = sequence_counter != rx_sequence_counter;
292 payload_mismatch = memcmp(frame + sizeof(struct reference_meta_data),
293 expected_pattern, expected_pattern_length);
294 frame_id_mismatch = false;
295
296 stat_frame_received(thread_context->frame_type, sequence_counter,
297 out_of_order, payload_mismatch, frame_id_mismatch,
298 tx_timestamp, 0, 0);
299
300 if (out_of_order) {
301 if (!ignore_rx_errors)
302 log_message(LOG_LEVEL_WARNING,
303 "%sRx: frame[%" PRIu64
304 "] SequenceCounter mismatch: %" PRIu64 "!\n",
305 thread_context->traffic_class,
306 rx_sequence_counter, sequence_counter);
307 sequence_counter++;
308 }
309
310 sequence_counter++;
311
312 if (payload_mismatch)
313 log_message(LOG_LEVEL_WARNING,
314 "%sRx: frame[%" PRIu64 "] Payload Pattern mismatch!\n",
315 thread_context->traffic_class, rx_sequence_counter);
316
317 /* If mirror enabled, assemble and store the frame for Tx later. */
318 if (!mirror_enabled)
319 continue;
320
321 /* Store the new frame. */
322 ring_buffer_add(thread_context->mirror_buffer, frame, len);
323
324 pthread_mutex_lock(&thread_context->data_mutex);
325 thread_context->num_frames_available++;
326 pthread_mutex_unlock(&thread_context->data_mutex);
327 }
328 }
329
330 return NULL;
331 }
332
333 static void *udp_tx_generation_thread_routine(void *data)
334 {
335 struct thread_context *thread_context = data;
336 const struct traffic_class_config *udp_config = thread_context->conf;
337 pthread_mutex_t *mutex = &thread_context->data_mutex;
338 uint64_t cycle_time_ns = udp_config->burst_period_ns;
339 uint64_t num_frames = udp_config->num_frames_per_cycle;
340 struct timespec wakeup_time;
341 int ret;
342
343 /*
344 * The UDP frames are generated by bursts with a certain period. This thread is responsible
345 * for generating it.
346 */
347
348 ret = get_thread_start_time(0, &wakeup_time);
349 if (ret) {
350 log_message(LOG_LEVEL_ERROR,
351 "%sTxGen: Failed to calculate thread start time: %s!\n",
352 thread_context->traffic_class, strerror(errno));
353 return NULL;
354 }
355
356 while (!thread_context->stop) {
357 /* Wait until next period */
358 increment_period(&wakeup_time, cycle_time_ns);
359
360 do {
361 ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME,
362 &wakeup_time, NULL);
363 } while (ret == EINTR);
364
365 if (ret) {
366 log_message(LOG_LEVEL_ERROR, "%sTxGen: clock_nanosleep() failed: %s\n",
367 thread_context->traffic_class, strerror(ret));
368 return NULL;
369 }
370
371 /* Generate frames */
372 pthread_mutex_lock(mutex);
373 thread_context->num_frames_available = num_frames;
374 pthread_mutex_unlock(mutex);
375 }
376
377 return NULL;
378 }
379
380 static int udp_threads_create(struct thread_context *thread_context)
381 {
382 const struct traffic_class_config *udp_config = thread_context->conf;
383 char thread_name[128];
384 int ret;
385
386 if (thread_context->frame_type == UDP_HIGH_FRAME_TYPE &&
387 !config_is_traffic_class_active("UdpHigh")) {
388 ret = 0;
389 goto out;
390 }
391 if (thread_context->frame_type == UDP_LOW_FRAME_TYPE &&
392 !config_is_traffic_class_active("UdpLow")) {
393 ret = 0;
394 goto out;
395 }
396
397 thread_context->socket_fd = create_udp_socket(
398 udp_config->l3_destination, udp_config->l3_source, udp_config->l3_port,
399 udp_config->socket_priority, &thread_context->destination);
400 if (thread_context->socket_fd < 0) {
401 fprintf(stderr, "Failed to create UdpSocket!\n");
402 ret = -errno;
403 goto err;
404 }
405
406 init_mutex(&thread_context->data_mutex);
407 init_condition_variable(&thread_context->data_cond_var);
408
409 thread_context->tx_frame_data = calloc(1, MAX_FRAME_SIZE);
410 if (!thread_context->tx_frame_data) {
411 fprintf(stderr, "Failed to allocate UdpTxFrameData!\n");
412 ret = -ENOMEM;
413 goto err_tx;
414 }
415
416 thread_context->rx_frame_data = calloc(udp_config->num_frames_per_cycle, MAX_FRAME_SIZE);
417 if (!thread_context->rx_frame_data) {
418 fprintf(stderr, "Failed to allocate UdpRxFrameData!\n");
419 ret = -ENOMEM;
420 goto err_rx;
421 }
422
423 if (udp_config->rx_mirror_enabled) {
424 /* Per period the expectation is: UdpNumFramesPerCycle * MAX_FRAME */
425 thread_context->mirror_buffer =
426 ring_buffer_allocate(MAX_FRAME_SIZE * udp_config->num_frames_per_cycle);
427 if (!thread_context->mirror_buffer) {
428 fprintf(stderr, "Failed to allocate Udp Mirror RingBuffer!\n");
429 ret = -ENOMEM;
430 goto err_buffer;
431 }
432 }
433
434 snprintf(thread_name, sizeof(thread_name), "%sTxThread", thread_context->traffic_class);
435
436 ret = create_rt_thread(&thread_context->tx_task_id, thread_name,
437 udp_config->tx_thread_priority, udp_config->tx_thread_cpu,
438 udp_tx_thread_routine, thread_context);
439 if (ret) {
440 fprintf(stderr, "Failed to create Udp Tx Thread!\n");
441 goto err_thread;
442 }
443
444 snprintf(thread_name, sizeof(thread_name), "%sTxGenThread", thread_context->traffic_class);
445
446 if (!udp_config->rx_mirror_enabled) {
447 ret = create_rt_thread(&thread_context->tx_gen_task_id, "UdpLowTxGenThread",
448 udp_config->tx_thread_priority, udp_config->tx_thread_cpu,
449 udp_tx_generation_thread_routine, thread_context);
450 if (ret) {
451 fprintf(stderr, "Failed to create Udp TxGen Thread!\n");
452 goto err_thread_txgen;
453 }
454 }
455
456 snprintf(thread_name, sizeof(thread_name), "%sRxThread", thread_context->traffic_class);
457
458 ret = create_rt_thread(&thread_context->rx_task_id, thread_name,
459 udp_config->rx_thread_priority, udp_config->rx_thread_cpu,
460 udp_rx_thread_routine, thread_context);
461 if (ret) {
462 fprintf(stderr, "Failed to create Udp Rx Thread!\n");
463 goto err_thread_rx;
464 }
465
466 return 0;
467
468 err_thread_rx:
469 thread_context->stop = 1;
470 if (thread_context->tx_gen_task_id)
471 pthread_join(thread_context->tx_gen_task_id, NULL);
472 err_thread_txgen:
473 thread_context->stop = 1;
474 pthread_join(thread_context->tx_task_id, NULL);
475 err_thread:
476 ring_buffer_free(thread_context->mirror_buffer);
477 err_buffer:
478 free(thread_context->rx_frame_data);
479 err_rx:
480 free(thread_context->tx_frame_data);
481 err_tx:
482 close(thread_context->socket_fd);
483 err:
484 out:
485 return ret;
486 }
487
488 static void udp_threads_free(struct thread_context *thread_context)
489 {
490 if (!thread_context)
491 return;
492
493 ring_buffer_free(thread_context->mirror_buffer);
494
495 free(thread_context->tx_frame_data);
496 free(thread_context->rx_frame_data);
497
498 if (thread_context->socket_fd > 0)
499 close(thread_context->socket_fd);
500 }
501
502 static void udp_threads_wait_for_finish(struct thread_context *thread_context)
503 {
504 if (!thread_context)
505 return;
506
507 if (thread_context->rx_task_id)
508 pthread_join(thread_context->rx_task_id, NULL);
509 if (thread_context->tx_task_id)
510 pthread_join(thread_context->tx_task_id, NULL);
511 if (thread_context->tx_gen_task_id)
512 pthread_join(thread_context->tx_gen_task_id, NULL);
513 }
514
515 int udp_low_threads_create(struct thread_context *udp_thread_context)
516 {
517 udp_thread_context->conf = &app_config.classes[UDP_LOW_FRAME_TYPE];
518 udp_thread_context->frame_type = UDP_LOW_FRAME_TYPE;
519 udp_thread_context->traffic_class = stat_frame_type_to_string(UDP_LOW_FRAME_TYPE);
520
521 return udp_threads_create(udp_thread_context);
522 }
523
524 void udp_low_threads_free(struct thread_context *thread_context)
525 {
526 udp_threads_free(thread_context);
527 }
528
529 void udp_low_threads_wait_for_finish(struct thread_context *thread_context)
530 {
531 udp_threads_wait_for_finish(thread_context);
532 }
533
534 int udp_high_threads_create(struct thread_context *udp_thread_context)
535 {
536 udp_thread_context->conf = &app_config.classes[UDP_HIGH_FRAME_TYPE];
537 udp_thread_context->frame_type = UDP_HIGH_FRAME_TYPE;
538 udp_thread_context->traffic_class = stat_frame_type_to_string(UDP_HIGH_FRAME_TYPE);
539
540 return udp_threads_create(udp_thread_context);
541 }
542
543 void udp_high_threads_free(struct thread_context *thread_context)
544 {
545 udp_threads_free(thread_context);
546 }
547
548 void udp_high_threads_wait_for_finish(struct thread_context *thread_context)
549 {
550 udp_threads_wait_for_finish(thread_context);
551 }