Debian Codegraph @

src/lldp_thread.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/lldp_thread.c`/
2 /*
3 * Copyright (C) 2020-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
21 #include "config.h"cxx . . $ `<file>/src/config.h`/
22 #include "lldp_thread.h"cxx . . $ `<file>/src/lldp_thread.h`/
23 #include "log.h"cxx . . $ `<file>/src/log.h`/
24 #include "net.h"cxx . . $ `<file>/src/net.h`/
25 #include "net_def.h"cxx . . $ `<file>/src/net_def.h`/
26 #include "packet.h"cxx . . $ `<file>/src/packet.h`/
27 #include "security.h"cxx . . $ `<file>/src/security.h`/
28 #include "stat.h"cxx . . $ `<file>/src/stat.h`/
29 #include "thread.h"cxx . . $ `<file>/src/thread.h`/
30 #include "utils.h"cxx . . $ `<file>/src/utils.h`/
31
32 static void lldp_build_frame_from_rxcxx . . $ lldp_build_frame_from_rx(225f8250db12d4ac).(unsigned char *frame_data, const unsigned char *source)
33 {
34 struct ethhdrcxx . . $ ethhdr# *eth = (struct ethhdrcxx . . $ ethhdr# *)frame_data;
35 struct reference_meta_datacxx . . $ reference_meta_data# *meta;
36
37 /* One task: Swap source. */
38 memcpycxx . . $ memcpy(cc817cb175f282d).(eth->h_sourcecxx . . $ ethhdr#h_source., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
39
40 /* One task: Set the tx timestamp. */
41 meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_data + sizeof(*eth));
42 set_mirror_tx_timestampcxx . . $ set_mirror_tx_timestamp(457bf14cd1570296).(meta);
43 }
44
45 static void lldp_initialize_framecxx . . $ lldp_initialize_frame(14e45335b2c8bf07).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data,
46 const unsigned char *source, const unsigned char *destination)
47 {
48 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
49 struct reference_meta_datacxx . . $ reference_meta_data# *meta;
50 size_t payload_offset;
51 struct ethhdrcxx . . $ ethhdr# *eth;
52
53 /*
54 * LldpFrame:
55 * Destination (multicast)
56 * Source
57 * Ether type: 88cc
58 * Cycle counter
59 * Payload
60 * Padding to maxFrame
61 */
62
63 eth = (struct ethhdrcxx . . $ ethhdr# *)frame_data;
64
65 /* Ethernet header */
66 memcpycxx . . $ memcpy(cc817cb175f282d).(eth->h_destcxx . . $ ethhdr#h_dest., destination, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
67 memcpycxx . . $ memcpy(cc817cb175f282d).(eth->h_sourcecxx . . $ ethhdr#h_source., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
68 eth->h_protocxx . . $ ethhdr#h_proto. = htonscxx . . $ htons(a18c05e4017afb3d).(ETH_P_LLDPcxx . . $ `<external>/15209105680615352250/if_ether.h:100:9`!);
69
70 /* Payload: SequenceCounter + Data */
71 meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_data + sizeof(*eth));
72 memsetcxx . . $ memset(d1de1e4fb215f1c6).(meta, '\0', sizeof(*meta));
73 payload_offset = sizeof(*eth) + sizeof(*meta);
74 memcpycxx . . $ memcpy(cc817cb175f282d).(frame_data + payload_offset, lldp_config->payload_patterncxx . . $ traffic_class_config#payload_pattern.,
75 lldp_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.);
76
77 /* Padding: '\0' */
78 }
79
80 static void lldp_initialize_framescxx . . $ lldp_initialize_frames(e1a9c3903a216417).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data,
81 size_t num_frames, const unsigned char *source,
82 const unsigned char *destination)
83 {
84 size_t i;
85
86 for (i = 0; i < num_frames; i++)
87 lldp_initialize_framecxx . . $ lldp_initialize_frame(14e45335b2c8bf07).(thread_context, frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(frame_data, i), source,
88 destination);
89 }
90
91 static int lldp_send_messagescxx . . $ lldp_send_messages(1230c857beb98e36).(struct thread_contextcxx . . $ thread_context# *thread_context, int socket_fd,
92 struct sockaddr_llcxx . . $ sockaddr_ll# *destination, unsigned char *frame_data,
93 size_t num_frames)
94 {
95 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
96 struct packet_send_requestcxx . . $ packet_send_request# send_req = {
97 .traffic_class = thread_context->traffic_classcxx . . $ thread_context#traffic_class.,
98 .socket_fd = socket_fd,
99 .destination = destination,
100 .frame_data = frame_data,
101 .num_frames = num_frames,
102 .frame_length = lldp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.,
103 .duration = 0,
104 .tx_time_offset = 0,
105 .meta_data_offset = thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset.,
106 .mirror_enabled = lldp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.,
107 .tx_time_enabled = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!,
108 };
109
110 return packet_send_messagescxx . . $ packet_send_messages(db4d6f7226235983).(thread_context->packet_contextcxx . . $ thread_context#packet_context., &send_req);
111 }
112
113 static int lldp_send_framescxx . . $ lldp_send_frames(bbb668e7d71a4c9).(struct thread_contextcxx . . $ thread_context# *thread_context, unsigned char *frame_data,
114 size_t num_frames, int socket_fd, struct sockaddr_llcxx . . $ sockaddr_ll# *destination)
115 {
116 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
117 int len, i;
118
119 /* Send them */
120 len = lldp_send_messagescxx . . $ lldp_send_messages(1230c857beb98e36).(thread_context, socket_fd, destination, frame_data, num_frames);
121
122 for (i = 0; i < len; i++) {
123 uint64_tcxx . . $ uint64_t# sequence_counter;
124
125 sequence_counter = get_sequence_countercxx . . $ get_sequence_counter(9512b04a879eebb0).(frame_data + i * lldp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.,
126 thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset.,
127 lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.);
128
129 stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE., sequence_counter);
130 }
131
132 return len;
133 }
134
135 static int lldp_gen_and_send_framescxx . . $ lldp_gen_and_send_frames(2a5ba8d4c1205443).(struct thread_contextcxx . . $ thread_context# *thread_context, int socket_fd,
136 struct sockaddr_llcxx . . $ sockaddr_ll# *destination,
137 uint64_tcxx . . $ uint64_t# sequence_counter_begin)
138 {
139 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
140 struct reference_meta_datacxx . . $ reference_meta_data# *meta;
141 struct timespeccxx . . $ timespec# tx_time = {};
142 struct ethhdrcxx . . $ ethhdr# *eth;
143 int len, i;
144
145 clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., &tx_time);
146
147 /* Adjust meta data */
148 for (i = 0; i < lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.; i++) {
149 meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_idxcxx . . $ frame_idx(9d0786fb5198b4b4).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data., i) +
150 sizeof(*eth));
151 sequence_counter_to_meta_datacxx . . $ sequence_counter_to_meta_data(9318332f0006d3bc).(meta, sequence_counter_begin + i,
152 lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.);
153
154 tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(meta, ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&tx_time));
155 }
156
157 /* Send them */
158 len = lldp_send_messagescxx . . $ lldp_send_messages(1230c857beb98e36).(thread_context, socket_fd, destination,
159 thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data., lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.);
160
161 for (i = 0; i < len; i++)
162 stat_frame_sentcxx . . $ stat_frame_sent(ca48c151307d73b6).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE., sequence_counter_begin + i);
163
164 return len;
165 }
166
167 static void *lldp_tx_thread_routinecxx . . $ lldp_tx_thread_routine(9f162e79bfc6c74).(void *data)
168 {
169 struct thread_contextcxx . . $ thread_context# *thread_context = data;
170 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
171 size_t received_frames_length = MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.;
172 unsigned char *received_frames = thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.;
173 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = lldp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.;
174 pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex = &thread_context->data_mutexcxx . . $ thread_context#data_mutex.;
175 pthread_cond_tcxx . . $ pthread_cond_t# *cond = &thread_context->data_cond_varcxx . . $ thread_context#data_cond_var.;
176 struct sockaddr_llcxx . . $ sockaddr_ll# destination;
177 unsigned char source[ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!];
178 uint64_tcxx . . $ uint64_t# sequence_counter = 0;
179 unsigned int if_index;
180 int ret, socket_fd;
181
182 socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.;
183
184 ret = get_interface_mac_addresscxx . . $ get_interface_mac_address(6007bd5416c53f30).(lldp_config->interfacecxx . . $ traffic_class_config#interface., source, ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
185 if (ret < 0) {
186 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpTx: Failed to get Source MAC address!\n");
187 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
188 }
189
190 if_index = if_nametoindexcxx . . $ if_nametoindex(c61c63047ce273de).(lldp_config->interfacecxx . . $ traffic_class_config#interface.);
191 if (!if_index) {
192 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpTx: if_nametoindex() failed!\n");
193 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
194 }
195
196 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&destination, '\0', sizeof(destination));
197 destination.sll_familycxx . . $ sockaddr_ll#sll_family. = PF_PACKETcxx . . $ `<external>/8962547315054503424/socket.h:61:9`!;
198 destination.sll_ifindexcxx . . $ sockaddr_ll#sll_ifindex. = if_index;
199 destination.sll_halencxx . . $ sockaddr_ll#sll_halen. = ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!;
200 memcpycxx . . $ memcpy(cc817cb175f282d).(destination.sll_addrcxx . . $ sockaddr_ll#sll_addr., lldp_config->l2_destinationcxx . . $ traffic_class_config#l2_destination., ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
201
202 lldp_initialize_framescxx . . $ lldp_initialize_frames(e1a9c3903a216417).(thread_context, thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.,
203 lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., source,
204 lldp_config->l2_destinationcxx . . $ traffic_class_config#l2_destination.);
205
206 while (!thread_context->stopcxx . . $ thread_context#stop.) {
207 struct timespeccxx . . $ timespec# timeout;
208 size_t num_frames;
209
210 /*
211 * Wait until signalled. These LLDP frames have to be sent after the DCP
212 * frames. Therefore, the DCP TxThread signals this one here.
213 */
214 clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(CLOCK_MONOTONICcxx . . $ `<external>/89459239771508533/time.h:48:10`!, &timeout);
215 timeout.tv_seccxx . . $ timespec#tv_sec.++;
216
217 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(mutex);
218 ret = pthread_cond_timedwaitcxx . . $ pthread_cond_timedwait(d8fcb9edac03ad51).(cond, mutex, &timeout);
219 num_frames = thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available.;
220 thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = 0;
221 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(mutex);
222
223 /* In case of shutdown a signal may be missing. */
224 if (ret == ETIMEDOUTcxx . . $ `<external>/17922322188510340118/errno.h:94:9`!)
225 continue;
226
227 /*
228 * Send LldpFrames, two possibilites:
229 * a) Generate it, or
230 * b) Use received ones if mirror enabled
231 */
232 if (!mirror_enabled) {
233 if (num_frames) {
234 lldp_gen_and_send_framescxx . . $ lldp_gen_and_send_frames(2a5ba8d4c1205443).(thread_context, socket_fd, &destination,
235 sequence_counter);
236 sequence_counter += num_frames;
237 }
238 } else {
239 size_t len;
240
241 ring_buffer_fetchcxx . . $ ring_buffer_fetch(e8bf408057bf6314).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer., received_frames,
242 received_frames_length, &len);
243
244 /* Len should be a multiple of frame size */
245 num_frames = len / lldp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.;
246 lldp_send_framescxx . . $ lldp_send_frames(bbb668e7d71a4c9).(thread_context, received_frames, num_frames, socket_fd,
247 &destination);
248
249 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.);
250 thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = 0;
251 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.);
252 }
253
254 /* Signal next Tx thread */
255 if (thread_context->nextcxx . . $ thread_context#next.) {
256 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->nextcxx . . $ thread_context#next.->data_mutexcxx . . $ thread_context#data_mutex.);
257 pthread_cond_signalcxx . . $ pthread_cond_signal(98095c34a2b0149d).(&thread_context->nextcxx . . $ thread_context#next.->data_cond_varcxx . . $ thread_context#data_cond_var.);
258 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->nextcxx . . $ thread_context#next.->data_mutexcxx . . $ thread_context#data_mutex.);
259 }
260
261 if (thread_context->is_lastcxx . . $ thread_context#is_last.)
262 stat_updatecxx . . $ stat_update(49f6e7a06ebc5aa8).();
263 }
264
265 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
266 }
267
268 static int lldp_rx_framecxx . . $ lldp_rx_frame(ddfd8079f1f43689).(void *data, unsigned char *frame_data, size_t len)
269 {
270 struct thread_contextcxx . . $ thread_context# *thread_context = data;
271 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
272 const unsigned char *expected_pattern = (const unsigned char *)lldp_config->payload_patterncxx . . $ traffic_class_config#payload_pattern.;
273 const size_t expected_pattern_length = lldp_config->payload_pattern_lengthcxx . . $ traffic_class_config#payload_pattern_length.;
274 const size_t num_frames_per_cycle = lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.;
275 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mirror_enabled = lldp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.;
276 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! ignore_rx_errors = lldp_config->ignore_rx_errorscxx . . $ traffic_class_config#ignore_rx_errors.;
277 const size_t frame_length = lldp_config->frame_lengthcxx . . $ traffic_class_config#frame_length.;
278 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! out_of_order, payload_mismatch, frame_id_mismatch;
279 struct reference_meta_datacxx . . $ reference_meta_data# *meta;
280 uint64_tcxx . . $ uint64_t# sequence_counter;
281 uint64_tcxx . . $ uint64_t# tx_timestamp;
282
283 /* Process received frame. */
284 if (len != frame_length) {
285 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "LldpRx: Frame with wrong length received!\n");
286 return -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
287 }
288
289 /*
290 * Check cycle counter and payload. The ether type is checked by the
291 * attached BPF filter.
292 */
293 meta = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_data + sizeof(struct ethhdrcxx . . $ ethhdr#));
294 sequence_counter = meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(meta, num_frames_per_cycle);
295
296 tx_timestamp = meta_data_to_tx_timestampcxx . . $ meta_data_to_tx_timestamp(e17106d766bd017c).(meta);
297
298 out_of_order = sequence_counter != thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.;
299 payload_mismatch = memcmpcxx . . $ memcmp(36f949851efa3055).(frame_data + sizeof(struct ethhdrcxx . . $ ethhdr#) + sizeof(*meta),
300 expected_pattern, expected_pattern_length);
301 frame_id_mismatch = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
302
303 stat_frame_receivedcxx . . $ stat_frame_received(5534a29e0120fff).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE., sequence_counter, out_of_order, payload_mismatch,
304 frame_id_mismatch, tx_timestamp, 0, 0);
305
306 if (out_of_order) {
307 if (!ignore_rx_errors)
308 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING.,
309 "LldpRx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "] SequenceCounter mismatch: %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`!
310 "!\n",
311 sequence_counter, thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.);
312 thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.++;
313 }
314
315 if (payload_mismatch)
316 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING.,
317 "LldpRx: frame[%" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`! "] Payload Pattern mismatch!\n",
318 sequence_counter);
319
320 thread_context->rx_sequence_countercxx . . $ thread_context#rx_sequence_counter.++;
321
322 /* If mirror enabled, assemble and store the frame for Tx later. */
323 if (!mirror_enabled)
324 return 0;
325
326 /* Build new frame for Tx without VLAN info. */
327 lldp_build_frame_from_rxcxx . . $ lldp_build_frame_from_rx(225f8250db12d4ac).(frame_data, thread_context->sourcecxx . . $ thread_context#source.);
328
329 /* Store the new frame. */
330 ring_buffer_addcxx . . $ ring_buffer_add(5f1d237704bb7e40).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer., frame_data, len);
331
332 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.);
333 thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available.++;
334 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.);
335
336 return 0;
337 }
338
339 static void *lldp_rx_thread_routinecxx . . $ lldp_rx_thread_routine(9f162e79bfc6c74).(void *data)
340 {
341 struct thread_contextcxx . . $ thread_context# *thread_context = data;
342 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
343 const uint64_tcxx . . $ uint64_t# cycle_time_ns = app_configcxx . . $ app_config..application_base_cycle_time_nscxx . . $ application_config#application_base_cycle_time_ns.;
344 struct timespeccxx . . $ timespec# wakeup_time;
345 int socket_fd, ret;
346
347 socket_fd = thread_context->socket_fdcxx . . $ thread_context#socket_fd.;
348
349 ret = get_interface_mac_addresscxx . . $ get_interface_mac_address(6007bd5416c53f30).(lldp_config->interfacecxx . . $ traffic_class_config#interface., thread_context->sourcecxx . . $ thread_context#source., ETH_ALENcxx . . $ `<external>/15209105680615352250/if_ether.h:32:9`!);
350 if (ret < 0) {
351 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpRx: Failed to get Source MAC address!\n");
352 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
353 }
354
355 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);
356 if (ret) {
357 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpRx: Failed to calculate thread start time: %s!\n",
358 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
359 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
360 }
361
362 while (!thread_context->stopcxx . . $ thread_context#stop.) {
363 struct packet_receive_requestcxx . . $ packet_receive_request# recv_req = {
364 .traffic_class = thread_context->traffic_classcxx . . $ thread_context#traffic_class.,
365 .socket_fd = socket_fd,
366 .receive_function = lldp_rx_framecxx . . $ lldp_rx_frame(ddfd8079f1f43689).,
367 .data = thread_context,
368 };
369
370 /* Wait until next period. */
371 increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns);
372
373 do {
374 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`!,
375 &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
376 } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!);
377
378 if (ret) {
379 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpRx: clock_nanosleep() failed: %s\n",
380 strerrorcxx . . $ strerror(ff224f080444ee45).(ret));
381 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
382 }
383
384 /* Receive Lldp frames. */
385 packet_receive_messagescxx . . $ packet_receive_messages(dec9d97ca532b11c).(thread_context->packet_contextcxx . . $ thread_context#packet_context., &recv_req);
386 }
387
388 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
389 }
390
391 static void *lldp_tx_generation_thread_routinecxx . . $ lldp_tx_generation_thread_routine(9f162e79bfc6c74).(void *data)
392 {
393 struct thread_contextcxx . . $ thread_context# *thread_context = data;
394 const struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config = thread_context->confcxx . . $ thread_context#conf.;
395 uint64_tcxx . . $ uint64_t# num_frames = lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.;
396 pthread_mutex_tcxx . . $ pthread_mutex_t# *mutex = &thread_context->data_mutexcxx . . $ thread_context#data_mutex.;
397 uint64_tcxx . . $ uint64_t# cycle_time_ns = lldp_config->burst_period_nscxx . . $ traffic_class_config#burst_period_ns.;
398 struct timespeccxx . . $ timespec# wakeup_time;
399 int ret;
400
401 /*
402 * The LLDP frames are generated by bursts with a certain period. This thread is responsible
403 * for generating it.
404 */
405
406 ret = get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(0, &wakeup_time);
407 if (ret) {
408 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.,
409 "LldpTxGen: Failed to calculate thread start time: %s!\n",
410 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
411 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
412 }
413
414 while (!thread_context->stopcxx . . $ thread_context#stop.) {
415 /* Wait until next period */
416 increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&wakeup_time, cycle_time_ns);
417
418 do {
419 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`!,
420 &wakeup_time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
421 } while (ret == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!);
422
423 if (ret) {
424 log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "LldpTxGen: clock_nanosleep() failed: %s\n",
425 strerrorcxx . . $ strerror(ff224f080444ee45).(ret));
426 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
427 }
428
429 /* Generate frames */
430 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(mutex);
431 thread_context->num_frames_availablecxx . . $ thread_context#num_frames_available. = num_frames;
432 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(mutex);
433 }
434
435 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
436 }
437
438 int lldp_threads_createcxx . . $ lldp_threads_create(8542e1c024f3ff56).(struct thread_contextcxx . . $ thread_context# *thread_context)
439 {
440 struct traffic_class_configcxx . . $ traffic_class_config# *lldp_config;
441 int ret;
442
443 if (!config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).("Lldp"))
444 goto out;
445
446 thread_context->confcxx . . $ thread_context#conf. = lldp_config = &app_configcxx . . $ app_config..classescxx . . $ application_config#classes.[LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE.];
447 thread_context->frame_typecxx . . $ thread_context#frame_type. = LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE.;
448 thread_context->traffic_classcxx . . $ thread_context#traffic_class. = stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE.);
449
450 thread_context->socket_fdcxx . . $ thread_context#socket_fd. = create_lldp_socketcxx . . $ create_lldp_socket(b126dc7c1de90089).();
451 if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. < 0) {
452 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create LldpSocket!\n");
453 ret = -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
454 goto err;
455 }
456
457 init_mutexcxx . . $ init_mutex(4f83bfebf4bb9c8b).(&thread_context->data_mutexcxx . . $ thread_context#data_mutex.);
458 init_condition_variablecxx . . $ init_condition_variable(23b56462c1e1452f).(&thread_context->data_cond_varcxx . . $ thread_context#data_cond_var.);
459
460 thread_context->packet_contextcxx . . $ thread_context#packet_context. = packet_initcxx . . $ packet_init(6f6262789a319935).(lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.);
461 if (!thread_context->packet_contextcxx . . $ thread_context#packet_context.) {
462 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate Lldp packet context!\n");
463 ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
464 goto err_packet;
465 }
466
467 thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!);
468 if (!thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.) {
469 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate LldpTxFrameData!\n");
470 ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
471 goto err_tx;
472 }
473
474 thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle., MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`!);
475 if (!thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.) {
476 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate LldpRxFrameData!\n");
477 ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
478 goto err_rx;
479 }
480
481 if (lldp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.) {
482 /* Per period the expectation is: LldpNumFramesPerCycle * MAX_FRAME */
483 thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer. =
484 ring_buffer_allocatecxx . . $ ring_buffer_allocate(a00a27d6b402ebff).(MAX_FRAME_SIZEcxx . . $ `src/thread.h:23:9`! * lldp_config->num_frames_per_cyclecxx . . $ traffic_class_config#num_frames_per_cycle.);
485 if (!thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.) {
486 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to allocate Lldp Mirror RingBuffer!\n");
487 ret = -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
488 goto err_buffer;
489 }
490 }
491
492 ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->tx_task_idcxx . . $ thread_context#tx_task_id., "LldpTxThread",
493 lldp_config->tx_thread_prioritycxx . . $ traffic_class_config#tx_thread_priority., lldp_config->tx_thread_cpucxx . . $ traffic_class_config#tx_thread_cpu.,
494 lldp_tx_thread_routinecxx . . $ lldp_tx_thread_routine(9f162e79bfc6c74)., thread_context);
495 if (ret) {
496 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Lldp Tx Thread!\n");
497 goto err_thread;
498 }
499
500 if (!lldp_config->rx_mirror_enabledcxx . . $ traffic_class_config#rx_mirror_enabled.) {
501 ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., "LldpTxGenThread",
502 lldp_config->tx_thread_prioritycxx . . $ traffic_class_config#tx_thread_priority., lldp_config->tx_thread_cpucxx . . $ traffic_class_config#tx_thread_cpu.,
503 lldp_tx_generation_thread_routinecxx . . $ lldp_tx_generation_thread_routine(9f162e79bfc6c74)., thread_context);
504 if (ret) {
505 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Lldp TxGen Thread!\n");
506 goto err_thread_txgen;
507 }
508 }
509
510 ret = create_rt_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&thread_context->rx_task_idcxx . . $ thread_context#rx_task_id., "LldpRxThread",
511 lldp_config->rx_thread_prioritycxx . . $ traffic_class_config#rx_thread_priority., lldp_config->rx_thread_cpucxx . . $ traffic_class_config#rx_thread_cpu.,
512 lldp_rx_thread_routinecxx . . $ lldp_rx_thread_routine(9f162e79bfc6c74)., thread_context);
513 if (ret) {
514 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Failed to create Lldp Rx Thread!\n");
515 goto err_thread_rx;
516 }
517
518 thread_context->meta_data_offsetcxx . . $ thread_context#meta_data_offset. =
519 get_meta_data_offsetcxx . . $ get_meta_data_offset(16cb9e606c8e3615).(LLDP_FRAME_TYPEcxx . . $ LLDP_FRAME_TYPE., SECURITY_MODE_NONEcxx . . $ SECURITY_MODE_NONE.);
520
521 out:
522 ret = 0;
523
524 return ret;
525
526 err_thread_rx:
527 thread_context->stopcxx . . $ thread_context#stop. = 1;
528 if (thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id.)
529 pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
530 err_thread_txgen:
531 thread_context->stopcxx . . $ thread_context#stop. = 1;
532 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`!);
533 err_thread:
534 ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.);
535 err_buffer:
536 freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.);
537 err_rx:
538 freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.);
539 err_tx:
540 packet_freecxx . . $ packet_free(b20c0f701b2a2d37).(thread_context->packet_contextcxx . . $ thread_context#packet_context.);
541 err_packet:
542 closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.);
543 err:
544 return ret;
545 }
546
547 void lldp_threads_freecxx . . $ lldp_threads_free(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context)
548 {
549 if (!thread_context)
550 return;
551
552 ring_buffer_freecxx . . $ ring_buffer_free(8f2cd700b00968de).(thread_context->mirror_buffercxx . . $ thread_context#mirror_buffer.);
553
554 packet_freecxx . . $ packet_free(b20c0f701b2a2d37).(thread_context->packet_contextcxx . . $ thread_context#packet_context.);
555 freecxx . . $ free(5d22bdacc48458e8).(thread_context->tx_frame_datacxx . . $ thread_context#tx_frame_data.);
556 freecxx . . $ free(5d22bdacc48458e8).(thread_context->rx_frame_datacxx . . $ thread_context#rx_frame_data.);
557
558 if (thread_context->socket_fdcxx . . $ thread_context#socket_fd. > 0)
559 closecxx . . $ close(7864480464b09eea).(thread_context->socket_fdcxx . . $ thread_context#socket_fd.);
560 }
561
562 void lldp_threads_wait_for_finishcxx . . $ lldp_threads_wait_for_finish(f7c2ab51214b7176).(struct thread_contextcxx . . $ thread_context# *thread_context)
563 {
564 if (!thread_context)
565 return;
566
567 if (thread_context->rx_task_idcxx . . $ thread_context#rx_task_id.)
568 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`!);
569 if (thread_context->tx_task_idcxx . . $ thread_context#tx_task_id.)
570 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`!);
571 if (thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id.)
572 pthread_joincxx . . $ pthread_join(269902cf949f12c5).(thread_context->tx_gen_task_idcxx . . $ thread_context#tx_gen_task_id., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
573 }