| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 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" |
| 22 | #include "lldp_thread.h" |
| 23 | #include "log.h" |
| 24 | #include "net.h" |
| 25 | #include "net_def.h" |
| 26 | #include "packet.h" |
| 27 | #include "security.h" |
| 28 | #include "stat.h" |
| 29 | #include "thread.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | static void lldp_build_frame_from_rx(unsigned char *frame_data, const unsigned char *source) |
| 33 | { |
| 34 | struct ethhdr *eth = (struct ethhdr *)frame_data; |
| 35 | struct reference_meta_data *meta; |
| 36 | |
| 37 | /* One task: Swap source. */ |
| 38 | memcpy(eth->h_source, source, ETH_ALEN); |
| 39 | |
| 40 | /* One task: Set the tx timestamp. */ |
| 41 | meta = (struct reference_meta_data *)(frame_data + sizeof(*eth)); |
| 42 | set_mirror_tx_timestamp(meta); |
| 43 | } |
| 44 | |
| 45 | static void lldp_initialize_frame(struct thread_context *thread_context, unsigned char *frame_data, |
| 46 | const unsigned char *source, const unsigned char *destination) |
| 47 | { |
| 48 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 49 | struct reference_meta_data *meta; |
| 50 | size_t payload_offset; |
| 51 | struct 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 ethhdr *)frame_data; |
| 64 | |
| 65 | /* Ethernet header */ |
| 66 | memcpy(eth->h_dest, destination, ETH_ALEN); |
| 67 | memcpy(eth->h_source, source, ETH_ALEN); |
| 68 | eth->h_proto = htons(ETH_P_LLDP); |
| 69 | |
| 70 | /* Payload: SequenceCounter + Data */ |
| 71 | meta = (struct reference_meta_data *)(frame_data + sizeof(*eth)); |
| 72 | memset(meta, '\0', sizeof(*meta)); |
| 73 | payload_offset = sizeof(*eth) + sizeof(*meta); |
| 74 | memcpy(frame_data + payload_offset, lldp_config->payload_pattern, |
| 75 | lldp_config->payload_pattern_length); |
| 76 | |
| 77 | /* Padding: '\0' */ |
| 78 | } |
| 79 | |
| 80 | static void lldp_initialize_frames(struct 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_frame(thread_context, frame_idx(frame_data, i), source, |
| 88 | destination); |
| 89 | } |
| 90 | |
| 91 | static int lldp_send_messages(struct thread_context *thread_context, int socket_fd, |
| 92 | struct sockaddr_ll *destination, unsigned char *frame_data, |
| 93 | size_t num_frames) |
| 94 | { |
| 95 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 96 | struct packet_send_request send_req = { |
| 97 | .traffic_class = 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_length, |
| 103 | .duration = 0, |
| 104 | .tx_time_offset = 0, |
| 105 | .meta_data_offset = thread_context->meta_data_offset, |
| 106 | .mirror_enabled = lldp_config->rx_mirror_enabled, |
| 107 | .tx_time_enabled = false, |
| 108 | }; |
| 109 | |
| 110 | return packet_send_messages(thread_context->packet_context, &send_req); |
| 111 | } |
| 112 | |
| 113 | static int lldp_send_frames(struct thread_context *thread_context, unsigned char *frame_data, |
| 114 | size_t num_frames, int socket_fd, struct sockaddr_ll *destination) |
| 115 | { |
| 116 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 117 | int len, i; |
| 118 | |
| 119 | /* Send them */ |
| 120 | len = lldp_send_messages(thread_context, socket_fd, destination, frame_data, num_frames); |
| 121 | |
| 122 | for (i = 0; i < len; i++) { |
| 123 | uint64_t sequence_counter; |
| 124 | |
| 125 | sequence_counter = get_sequence_counter(frame_data + i * lldp_config->frame_length, |
| 126 | thread_context->meta_data_offset, |
| 127 | lldp_config->num_frames_per_cycle); |
| 128 | |
| 129 | stat_frame_sent(LLDP_FRAME_TYPE, sequence_counter); |
| 130 | } |
| 131 | |
| 132 | return len; |
| 133 | } |
| 134 | |
| 135 | static int lldp_gen_and_send_frames(struct thread_context *thread_context, int socket_fd, |
| 136 | struct sockaddr_ll *destination, |
| 137 | uint64_t sequence_counter_begin) |
| 138 | { |
| 139 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 140 | struct reference_meta_data *meta; |
| 141 | struct timespec tx_time = {}; |
| 142 | struct ethhdr *eth; |
| 143 | int len, i; |
| 144 | |
| 145 | clock_gettime(app_config.application_clock_id, &tx_time); |
| 146 | |
| 147 | /* Adjust meta data */ |
| 148 | for (i = 0; i < lldp_config->num_frames_per_cycle; i++) { |
| 149 | meta = (struct reference_meta_data *)(frame_idx(thread_context->tx_frame_data, i) + |
| 150 | sizeof(*eth)); |
| 151 | sequence_counter_to_meta_data(meta, sequence_counter_begin + i, |
| 152 | lldp_config->num_frames_per_cycle); |
| 153 | |
| 154 | tx_timestamp_to_meta_data(meta, ts_to_ns(&tx_time)); |
| 155 | } |
| 156 | |
| 157 | /* Send them */ |
| 158 | len = lldp_send_messages(thread_context, socket_fd, destination, |
| 159 | thread_context->tx_frame_data, lldp_config->num_frames_per_cycle); |
| 160 | |
| 161 | for (i = 0; i < len; i++) |
| 162 | stat_frame_sent(LLDP_FRAME_TYPE, sequence_counter_begin + i); |
| 163 | |
| 164 | return len; |
| 165 | } |
| 166 | |
| 167 | static void *lldp_tx_thread_routine(void *data) |
| 168 | { |
| 169 | struct thread_context *thread_context = data; |
| 170 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 171 | size_t received_frames_length = MAX_FRAME_SIZE * lldp_config->num_frames_per_cycle; |
| 172 | unsigned char *received_frames = thread_context->rx_frame_data; |
| 173 | const bool mirror_enabled = lldp_config->rx_mirror_enabled; |
| 174 | pthread_mutex_t *mutex = &thread_context->data_mutex; |
| 175 | pthread_cond_t *cond = &thread_context->data_cond_var; |
| 176 | struct sockaddr_ll destination; |
| 177 | unsigned char source[ETH_ALEN]; |
| 178 | uint64_t sequence_counter = 0; |
| 179 | unsigned int if_index; |
| 180 | int ret, socket_fd; |
| 181 | |
| 182 | socket_fd = thread_context->socket_fd; |
| 183 | |
| 184 | ret = get_interface_mac_address(lldp_config->interface, source, ETH_ALEN); |
| 185 | if (ret < 0) { |
| 186 | log_message(LOG_LEVEL_ERROR, "LldpTx: Failed to get Source MAC address!\n"); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | if_index = if_nametoindex(lldp_config->interface); |
| 191 | if (!if_index) { |
| 192 | log_message(LOG_LEVEL_ERROR, "LldpTx: if_nametoindex() failed!\n"); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | memset(&destination, '\0', sizeof(destination)); |
| 197 | destination.sll_family = PF_PACKET; |
| 198 | destination.sll_ifindex = if_index; |
| 199 | destination.sll_halen = ETH_ALEN; |
| 200 | memcpy(destination.sll_addr, lldp_config->l2_destination, ETH_ALEN); |
| 201 | |
| 202 | lldp_initialize_frames(thread_context, thread_context->tx_frame_data, |
| 203 | lldp_config->num_frames_per_cycle, source, |
| 204 | lldp_config->l2_destination); |
| 205 | |
| 206 | while (!thread_context->stop) { |
| 207 | struct 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_gettime(CLOCK_MONOTONIC, &timeout); |
| 215 | timeout.tv_sec++; |
| 216 | |
| 217 | pthread_mutex_lock(mutex); |
| 218 | ret = pthread_cond_timedwait(cond, mutex, &timeout); |
| 219 | num_frames = thread_context->num_frames_available; |
| 220 | thread_context->num_frames_available = 0; |
| 221 | pthread_mutex_unlock(mutex); |
| 222 | |
| 223 | /* In case of shutdown a signal may be missing. */ |
| 224 | if (ret == ETIMEDOUT) |
| 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_frames(thread_context, socket_fd, &destination, |
| 235 | sequence_counter); |
| 236 | sequence_counter += num_frames; |
| 237 | } |
| 238 | } else { |
| 239 | size_t len; |
| 240 | |
| 241 | ring_buffer_fetch(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_length; |
| 246 | lldp_send_frames(thread_context, received_frames, num_frames, socket_fd, |
| 247 | &destination); |
| 248 | |
| 249 | pthread_mutex_lock(&thread_context->data_mutex); |
| 250 | thread_context->num_frames_available = 0; |
| 251 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 252 | } |
| 253 | |
| 254 | /* Signal next Tx thread */ |
| 255 | if (thread_context->next) { |
| 256 | pthread_mutex_lock(&thread_context->next->data_mutex); |
| 257 | pthread_cond_signal(&thread_context->next->data_cond_var); |
| 258 | pthread_mutex_unlock(&thread_context->next->data_mutex); |
| 259 | } |
| 260 | |
| 261 | if (thread_context->is_last) |
| 262 | stat_update(); |
| 263 | } |
| 264 | |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | static int lldp_rx_frame(void *data, unsigned char *frame_data, size_t len) |
| 269 | { |
| 270 | struct thread_context *thread_context = data; |
| 271 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 272 | const unsigned char *expected_pattern = (const unsigned char *)lldp_config->payload_pattern; |
| 273 | const size_t expected_pattern_length = lldp_config->payload_pattern_length; |
| 274 | const size_t num_frames_per_cycle = lldp_config->num_frames_per_cycle; |
| 275 | const bool mirror_enabled = lldp_config->rx_mirror_enabled; |
| 276 | const bool ignore_rx_errors = lldp_config->ignore_rx_errors; |
| 277 | const size_t frame_length = lldp_config->frame_length; |
| 278 | bool out_of_order, payload_mismatch, frame_id_mismatch; |
| 279 | struct reference_meta_data *meta; |
| 280 | uint64_t sequence_counter; |
| 281 | uint64_t tx_timestamp; |
| 282 | |
| 283 | /* Process received frame. */ |
| 284 | if (len != frame_length) { |
| 285 | log_message(LOG_LEVEL_WARNING, "LldpRx: Frame with wrong length received!\n"); |
| 286 | return -EINVAL; |
| 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_data *)(frame_data + sizeof(struct ethhdr)); |
| 294 | sequence_counter = meta_data_to_sequence_counter(meta, num_frames_per_cycle); |
| 295 | |
| 296 | tx_timestamp = meta_data_to_tx_timestamp(meta); |
| 297 | |
| 298 | out_of_order = sequence_counter != thread_context->rx_sequence_counter; |
| 299 | payload_mismatch = memcmp(frame_data + sizeof(struct ethhdr) + sizeof(*meta), |
| 300 | expected_pattern, expected_pattern_length); |
| 301 | frame_id_mismatch = false; |
| 302 | |
| 303 | stat_frame_received(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_message(LOG_LEVEL_WARNING, |
| 309 | "LldpRx: frame[%" PRIu64 "] SequenceCounter mismatch: %" PRIu64 |
| 310 | "!\n", |
| 311 | sequence_counter, thread_context->rx_sequence_counter); |
| 312 | thread_context->rx_sequence_counter++; |
| 313 | } |
| 314 | |
| 315 | if (payload_mismatch) |
| 316 | log_message(LOG_LEVEL_WARNING, |
| 317 | "LldpRx: frame[%" PRIu64 "] Payload Pattern mismatch!\n", |
| 318 | sequence_counter); |
| 319 | |
| 320 | 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_rx(frame_data, thread_context->source); |
| 328 | |
| 329 | /* Store the new frame. */ |
| 330 | ring_buffer_add(thread_context->mirror_buffer, frame_data, len); |
| 331 | |
| 332 | pthread_mutex_lock(&thread_context->data_mutex); |
| 333 | thread_context->num_frames_available++; |
| 334 | pthread_mutex_unlock(&thread_context->data_mutex); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static void *lldp_rx_thread_routine(void *data) |
| 340 | { |
| 341 | struct thread_context *thread_context = data; |
| 342 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 343 | const uint64_t cycle_time_ns = app_config.application_base_cycle_time_ns; |
| 344 | struct timespec wakeup_time; |
| 345 | int socket_fd, ret; |
| 346 | |
| 347 | socket_fd = thread_context->socket_fd; |
| 348 | |
| 349 | ret = get_interface_mac_address(lldp_config->interface, thread_context->source, ETH_ALEN); |
| 350 | if (ret < 0) { |
| 351 | log_message(LOG_LEVEL_ERROR, "LldpRx: Failed to get Source MAC address!\n"); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | ret = get_thread_start_time(app_config.application_rx_base_offset_ns, &wakeup_time); |
| 356 | if (ret) { |
| 357 | log_message(LOG_LEVEL_ERROR, "LldpRx: Failed to calculate thread start time: %s!\n", |
| 358 | strerror(errno)); |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | while (!thread_context->stop) { |
| 363 | struct packet_receive_request recv_req = { |
| 364 | .traffic_class = thread_context->traffic_class, |
| 365 | .socket_fd = socket_fd, |
| 366 | .receive_function = lldp_rx_frame, |
| 367 | .data = thread_context, |
| 368 | }; |
| 369 | |
| 370 | /* Wait until next period. */ |
| 371 | increment_period(&wakeup_time, cycle_time_ns); |
| 372 | |
| 373 | do { |
| 374 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 375 | &wakeup_time, NULL); |
| 376 | } while (ret == EINTR); |
| 377 | |
| 378 | if (ret) { |
| 379 | log_message(LOG_LEVEL_ERROR, "LldpRx: clock_nanosleep() failed: %s\n", |
| 380 | strerror(ret)); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | /* Receive Lldp frames. */ |
| 385 | packet_receive_messages(thread_context->packet_context, &recv_req); |
| 386 | } |
| 387 | |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | static void *lldp_tx_generation_thread_routine(void *data) |
| 392 | { |
| 393 | struct thread_context *thread_context = data; |
| 394 | const struct traffic_class_config *lldp_config = thread_context->conf; |
| 395 | uint64_t num_frames = lldp_config->num_frames_per_cycle; |
| 396 | pthread_mutex_t *mutex = &thread_context->data_mutex; |
| 397 | uint64_t cycle_time_ns = lldp_config->burst_period_ns; |
| 398 | struct 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_time(0, &wakeup_time); |
| 407 | if (ret) { |
| 408 | log_message(LOG_LEVEL_ERROR, |
| 409 | "LldpTxGen: Failed to calculate thread start time: %s!\n", |
| 410 | strerror(errno)); |
| 411 | return NULL; |
| 412 | } |
| 413 | |
| 414 | while (!thread_context->stop) { |
| 415 | /* Wait until next period */ |
| 416 | increment_period(&wakeup_time, cycle_time_ns); |
| 417 | |
| 418 | do { |
| 419 | ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, |
| 420 | &wakeup_time, NULL); |
| 421 | } while (ret == EINTR); |
| 422 | |
| 423 | if (ret) { |
| 424 | log_message(LOG_LEVEL_ERROR, "LldpTxGen: clock_nanosleep() failed: %s\n", |
| 425 | strerror(ret)); |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | /* Generate frames */ |
| 430 | pthread_mutex_lock(mutex); |
| 431 | thread_context->num_frames_available = num_frames; |
| 432 | pthread_mutex_unlock(mutex); |
| 433 | } |
| 434 | |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | int lldp_threads_create(struct thread_context *thread_context) |
| 439 | { |
| 440 | struct traffic_class_config *lldp_config; |
| 441 | int ret; |
| 442 | |
| 443 | if (!config_is_traffic_class_active("Lldp")) |
| 444 | goto out; |
| 445 | |
| 446 | thread_context->conf = lldp_config = &app_config.classes[LLDP_FRAME_TYPE]; |
| 447 | thread_context->frame_type = LLDP_FRAME_TYPE; |
| 448 | thread_context->traffic_class = stat_frame_type_to_string(LLDP_FRAME_TYPE); |
| 449 | |
| 450 | thread_context->socket_fd = create_lldp_socket(); |
| 451 | if (thread_context->socket_fd < 0) { |
| 452 | fprintf(stderr, "Failed to create LldpSocket!\n"); |
| 453 | ret = -errno; |
| 454 | goto err; |
| 455 | } |
| 456 | |
| 457 | init_mutex(&thread_context->data_mutex); |
| 458 | init_condition_variable(&thread_context->data_cond_var); |
| 459 | |
| 460 | thread_context->packet_context = packet_init(lldp_config->num_frames_per_cycle); |
| 461 | if (!thread_context->packet_context) { |
| 462 | fprintf(stderr, "Failed to allocate Lldp packet context!\n"); |
| 463 | ret = -ENOMEM; |
| 464 | goto err_packet; |
| 465 | } |
| 466 | |
| 467 | thread_context->tx_frame_data = calloc(lldp_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 468 | if (!thread_context->tx_frame_data) { |
| 469 | fprintf(stderr, "Failed to allocate LldpTxFrameData!\n"); |
| 470 | ret = -ENOMEM; |
| 471 | goto err_tx; |
| 472 | } |
| 473 | |
| 474 | thread_context->rx_frame_data = calloc(lldp_config->num_frames_per_cycle, MAX_FRAME_SIZE); |
| 475 | if (!thread_context->rx_frame_data) { |
| 476 | fprintf(stderr, "Failed to allocate LldpRxFrameData!\n"); |
| 477 | ret = -ENOMEM; |
| 478 | goto err_rx; |
| 479 | } |
| 480 | |
| 481 | if (lldp_config->rx_mirror_enabled) { |
| 482 | /* Per period the expectation is: LldpNumFramesPerCycle * MAX_FRAME */ |
| 483 | thread_context->mirror_buffer = |
| 484 | ring_buffer_allocate(MAX_FRAME_SIZE * lldp_config->num_frames_per_cycle); |
| 485 | if (!thread_context->mirror_buffer) { |
| 486 | fprintf(stderr, "Failed to allocate Lldp Mirror RingBuffer!\n"); |
| 487 | ret = -ENOMEM; |
| 488 | goto err_buffer; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | ret = create_rt_thread(&thread_context->tx_task_id, "LldpTxThread", |
| 493 | lldp_config->tx_thread_priority, lldp_config->tx_thread_cpu, |
| 494 | lldp_tx_thread_routine, thread_context); |
| 495 | if (ret) { |
| 496 | fprintf(stderr, "Failed to create Lldp Tx Thread!\n"); |
| 497 | goto err_thread; |
| 498 | } |
| 499 | |
| 500 | if (!lldp_config->rx_mirror_enabled) { |
| 501 | ret = create_rt_thread(&thread_context->tx_gen_task_id, "LldpTxGenThread", |
| 502 | lldp_config->tx_thread_priority, lldp_config->tx_thread_cpu, |
| 503 | lldp_tx_generation_thread_routine, thread_context); |
| 504 | if (ret) { |
| 505 | fprintf(stderr, "Failed to create Lldp TxGen Thread!\n"); |
| 506 | goto err_thread_txgen; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | ret = create_rt_thread(&thread_context->rx_task_id, "LldpRxThread", |
| 511 | lldp_config->rx_thread_priority, lldp_config->rx_thread_cpu, |
| 512 | lldp_rx_thread_routine, thread_context); |
| 513 | if (ret) { |
| 514 | fprintf(stderr, "Failed to create Lldp Rx Thread!\n"); |
| 515 | goto err_thread_rx; |
| 516 | } |
| 517 | |
| 518 | thread_context->meta_data_offset = |
| 519 | get_meta_data_offset(LLDP_FRAME_TYPE, SECURITY_MODE_NONE); |
| 520 | |
| 521 | out: |
| 522 | ret = 0; |
| 523 | |
| 524 | return ret; |
| 525 | |
| 526 | err_thread_rx: |
| 527 | thread_context->stop = 1; |
| 528 | if (thread_context->tx_gen_task_id) |
| 529 | pthread_join(thread_context->tx_gen_task_id, NULL); |
| 530 | err_thread_txgen: |
| 531 | thread_context->stop = 1; |
| 532 | pthread_join(thread_context->tx_task_id, NULL); |
| 533 | err_thread: |
| 534 | ring_buffer_free(thread_context->mirror_buffer); |
| 535 | err_buffer: |
| 536 | free(thread_context->rx_frame_data); |
| 537 | err_rx: |
| 538 | free(thread_context->tx_frame_data); |
| 539 | err_tx: |
| 540 | packet_free(thread_context->packet_context); |
| 541 | err_packet: |
| 542 | close(thread_context->socket_fd); |
| 543 | err: |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | void lldp_threads_free(struct thread_context *thread_context) |
| 548 | { |
| 549 | if (!thread_context) |
| 550 | return; |
| 551 | |
| 552 | ring_buffer_free(thread_context->mirror_buffer); |
| 553 | |
| 554 | packet_free(thread_context->packet_context); |
| 555 | free(thread_context->tx_frame_data); |
| 556 | free(thread_context->rx_frame_data); |
| 557 | |
| 558 | if (thread_context->socket_fd > 0) |
| 559 | close(thread_context->socket_fd); |
| 560 | } |
| 561 | |
| 562 | void lldp_threads_wait_for_finish(struct thread_context *thread_context) |
| 563 | { |
| 564 | if (!thread_context) |
| 565 | return; |
| 566 | |
| 567 | if (thread_context->rx_task_id) |
| 568 | pthread_join(thread_context->rx_task_id, NULL); |
| 569 | if (thread_context->tx_task_id) |
| 570 | pthread_join(thread_context->tx_task_id, NULL); |
| 571 | if (thread_context->tx_gen_task_id) |
| 572 | pthread_join(thread_context->tx_gen_task_id, NULL); |
| 573 | } |