| 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 | /* |
| 3 | * Copyright (C) 2020-2024 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _THREAD_H_ |
| 8 | #define _THREAD_H_ |
| 9 | |
| 10 | #include <pthread.h> |
| 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #include <sys/socket.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | #include <linux/if_ether.h> |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "xdp.h" |
| 22 | |
| 23 | #define MAX_FRAME_SIZE XDP_FRAME_SIZE |
| 24 | |
| 25 | static inline unsigned char *frame_idx(unsigned char *frame_data, int idx) |
| 26 | { |
| 27 | return frame_data + idx * MAX_FRAME_SIZE; |
| 28 | } |
| 29 | |
| 30 | struct ring_buffer; |
| 31 | struct xdp_socket; |
| 32 | struct security_context; |
| 33 | struct packet_context; |
| 34 | |
| 35 | struct thread_context { |
| 36 | /* Task related */ |
| 37 | pthread_t rx_task_id; /* Receiver Thread */ |
| 38 | uint64_t rx_sequence_counter; /* Rx cycle counter */ |
| 39 | pthread_t tx_task_id; /* Sender Thread */ |
| 40 | pthread_t tx_gen_task_id; /* Sender generation thread */ |
| 41 | volatile int stop; /* Done? */ |
| 42 | |
| 43 | /* RAW socket related */ |
| 44 | int socket_fd; /* Shared RAW socket */ |
| 45 | unsigned char *tx_frame_data; /* Tx frame data */ |
| 46 | unsigned char *rx_frame_data; /* Rx frame data */ |
| 47 | unsigned char source[ETH_ALEN]; /* Source MAC Address */ |
| 48 | struct sockaddr_storage destination; /* Where to send L3 frames to */ |
| 49 | struct ring_buffer *mirror_buffer; /* Rx frames to be mirrored */ |
| 50 | struct packet_context *packet_context; /* Packet Tx/Rx context */ |
| 51 | |
| 52 | /* XDP socket related */ |
| 53 | struct xdp_socket *xsk; /* XDP socket reference */ |
| 54 | unsigned int received_frames; /* Amount of frames received within cycle */ |
| 55 | pthread_mutex_t xdp_data_mutex; /* Protect concurrent access to Xsk */ |
| 56 | |
| 57 | /* Data flow related */ |
| 58 | struct thread_context *next; /* Pointer to next traffic class */ |
| 59 | pthread_mutex_t data_mutex; /* Mutex to protect frame data */ |
| 60 | pthread_cond_t data_cond_var; /* Cond var to signal Tx thread */ |
| 61 | size_t num_frames_available; /* How many frames are ready to be sent? */ |
| 62 | bool is_first; /* Is this the first active traffic class? */ |
| 63 | bool is_last; /* Is this the last active traffic class? */ |
| 64 | |
| 65 | /* Security related */ |
| 66 | struct security_context *tx_security_context; /* Tx context for Auth and Crypt */ |
| 67 | struct security_context *rx_security_context; /* Rx context for Auth and Crypt */ |
| 68 | unsigned char *payload_pattern; /* Frame payload pattern used for AE */ |
| 69 | size_t payload_pattern_length; /* Length of payload pattern */ |
| 70 | |
| 71 | /* Traffic class related */ |
| 72 | struct traffic_class_config *conf; /* Pointer to traffic class configuration */ |
| 73 | enum stat_frame_type frame_type; /* Frame type */ |
| 74 | const char *traffic_class; /* Traffic class string */ |
| 75 | uint32_t meta_data_offset; /* Where is the MetaData in the frame? */ |
| 76 | uint16_t frame_id; /* What's the Profinet frame id */ |
| 77 | int (*create_socket)(void); /* Function to create socket */ |
| 78 | |
| 79 | /* Workload related */ |
| 80 | struct workload_config *workload; |
| 81 | }; |
| 82 | |
| 83 | enum pn_thread_type { |
| 84 | TSN_HIGH_THREAD = 0, |
| 85 | TSN_LOW_THREAD, |
| 86 | RTC_THREAD, |
| 87 | RTA_THREAD, |
| 88 | DCP_THREAD, |
| 89 | LLDP_THREAD, |
| 90 | UDP_HIGH_THREAD, |
| 91 | UDP_LOW_THREAD, |
| 92 | NUM_PN_THREAD_TYPES, |
| 93 | }; |
| 94 | |
| 95 | int create_rt_thread(pthread_t *task_id, const char *thread_name, int priority, int cpu_core, |
| 96 | void *(*thread_routine)(void *), void *data); |
| 97 | void init_mutex(pthread_mutex_t *mutex); |
| 98 | void init_condition_variable(pthread_cond_t *cond_var); |
| 99 | int link_pn_threads(struct thread_context *pn_threads); |
| 100 | |
| 101 | #endif /* _THREAD_H_ */ |