| 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 _UTILS_H_ |
| 8 | #define _UTILS_H_ |
| 9 | |
| 10 | #include <endian.h> |
| 11 | #include <errno.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <time.h> |
| 17 | |
| 18 | #include "config.h" |
| 19 | #include "net_def.h" |
| 20 | #include "security.h" |
| 21 | #include "stat.h" |
| 22 | |
| 23 | /* timing */ |
| 24 | #define MSEC_PER_SEC 1000 |
| 25 | #define USEC_PER_SEC 1000000 |
| 26 | #define NSEC_PER_SEC 1000000000LL |
| 27 | |
| 28 | static inline void ns_to_ts(int64_t ns, struct timespec *ts) |
| 29 | { |
| 30 | ts->tv_sec = ns / NSEC_PER_SEC; |
| 31 | ts->tv_nsec = ns % NSEC_PER_SEC; |
| 32 | } |
| 33 | |
| 34 | static inline int64_t ts_to_ns(const struct timespec *ts) |
| 35 | { |
| 36 | return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec; |
| 37 | } |
| 38 | |
| 39 | void increment_period(struct timespec *time, int64_t period_ns); |
| 40 | |
| 41 | void swap_mac_addresses(void *buffer, size_t len); |
| 42 | void insert_vlan_tag(void *buffer, size_t len, uint16_t vlan_tci); |
| 43 | |
| 44 | /* |
| 45 | * This function takes an received Ethernet frame by AF_PACKET sockets and performs two tasks: |
| 46 | * |
| 47 | * 1.) Inject VLAN header |
| 48 | * 2.) Swap source and destination |
| 49 | * |
| 50 | * This function does nothing when the @newFrame isn't sufficent in length. |
| 51 | */ |
| 52 | void build_vlan_frame_from_rx(const unsigned char *old_frame, size_t old_frame_len, |
| 53 | unsigned char *new_frame, size_t new_frame_len, uint16_t ether_type, |
| 54 | uint16_t vlan_tci); |
| 55 | |
| 56 | /* |
| 57 | * This function initializes an PROFINET Ethernet frame. The Ethernet header, PROFINET header and |
| 58 | * payload is initialized. The sequenceCounter is set to zero. |
| 59 | * |
| 60 | * In case the SecurityMode is AE or AO, the PROFINET Ethernet frames will contain the |
| 61 | * SecurityHeader after the FrameID. |
| 62 | */ |
| 63 | void initialize_profinet_frame(enum security_mode mode, unsigned char *frame_data, |
| 64 | size_t frame_length, const unsigned char *source, |
| 65 | const unsigned char *destination, const char *payload_pattern, |
| 66 | size_t payload_pattern_length, uint16_t vlan_tci, uint16_t frame_id); |
| 67 | |
| 68 | /* |
| 69 | * This function receives a Profinet frame. It performs all required tests such as checking sequence |
| 70 | * counters, payload, checksums, etc. This is used for TSN, RTC and RTA as well as by packet and xdp |
| 71 | * code. |
| 72 | */ |
| 73 | int receive_profinet_frame(void *data, unsigned char *frame_data, size_t len); |
| 74 | |
| 75 | /* |
| 76 | * The following function prepares an already initialized PROFINET Ethernet frame for final |
| 77 | * transmission. Depending on traffic class and security modes, different actions have to be taken |
| 78 | * e.g., adjusting the cycle counter and perform authentifcation and/or encryption. |
| 79 | */ |
| 80 | |
| 81 | struct prepare_frame_config { |
| 82 | enum security_mode mode; |
| 83 | struct security_context *security_context; |
| 84 | const unsigned char *iv_prefix; |
| 85 | const unsigned char *payload_pattern; |
| 86 | size_t payload_pattern_length; |
| 87 | unsigned char *frame_data; |
| 88 | size_t frame_length; |
| 89 | size_t num_frames_per_cycle; |
| 90 | uint64_t sequence_counter; |
| 91 | uint64_t tx_timestamp; |
| 92 | uint32_t meta_data_offset; |
| 93 | }; |
| 94 | |
| 95 | int prepare_frame_for_tx(const struct prepare_frame_config *frame_config); |
| 96 | |
| 97 | void prepare_iv(const unsigned char *iv_prefix, uint64_t sequence_counter, struct security_iv *iv); |
| 98 | |
| 99 | void prepare_openssl(struct security_context *context); |
| 100 | |
| 101 | int get_thread_start_time(uint64_t base_offset, struct timespec *wakeup_time); |
| 102 | |
| 103 | void configure_cpu_latency(void); |
| 104 | void restore_cpu_latency(void); |
| 105 | |
| 106 | /* error handling */ |
| 107 | void pthread_error(int ret, const char *message); |
| 108 | |
| 109 | /* Printing */ |
| 110 | void print_mac_address(const unsigned char *mac_address); |
| 111 | void print_payload_pattern(const char *payload_pattern, size_t payload_pattern_length); |
| 112 | |
| 113 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 114 | |
| 115 | #define BIT(x) (1ULL << (x)) |
| 116 | |
| 117 | /* Meta data handling */ |
| 118 | static inline uint64_t meta_data_to_sequence_counter(const struct reference_meta_data *meta, |
| 119 | size_t num_frames_per_cycle) |
| 120 | { |
| 121 | uint32_t frame_counter, cycle_counter; |
| 122 | |
| 123 | frame_counter = be32toh(meta->frame_counter); |
| 124 | cycle_counter = be32toh(meta->cycle_counter); |
| 125 | |
| 126 | return (uint64_t)cycle_counter * num_frames_per_cycle + frame_counter; |
| 127 | } |
| 128 | |
| 129 | static inline void sequence_counter_to_meta_data(struct reference_meta_data *meta, |
| 130 | uint64_t sequence_counter, |
| 131 | size_t num_frames_per_cycle) |
| 132 | { |
| 133 | meta->frame_counter = htobe32(sequence_counter % num_frames_per_cycle); |
| 134 | meta->cycle_counter = htobe32(sequence_counter / num_frames_per_cycle); |
| 135 | } |
| 136 | |
| 137 | static inline void tx_timestamp_to_meta_data(struct reference_meta_data *meta, uint64_t timestamp) |
| 138 | { |
| 139 | meta->tx_timestamp = htobe64(timestamp); |
| 140 | } |
| 141 | |
| 142 | static inline uint64_t meta_data_to_tx_timestamp(const struct reference_meta_data *meta) |
| 143 | { |
| 144 | uint64_t tx_timestamp; |
| 145 | |
| 146 | tx_timestamp = be64toh(meta->tx_timestamp); |
| 147 | |
| 148 | return tx_timestamp; |
| 149 | } |
| 150 | |
| 151 | static inline void set_mirror_tx_timestamp(struct reference_meta_data *meta) |
| 152 | { |
| 153 | struct timespec now; |
| 154 | |
| 155 | clock_gettime(app_config.application_clock_id, &now); |
| 156 | |
| 157 | tx_timestamp_to_meta_data(meta, |
| 158 | ts_to_ns(&now) + (app_config.application_tx_base_offset_ns - |
| 159 | app_config.application_rx_base_offset_ns)); |
| 160 | } |
| 161 | |
| 162 | static inline uint64_t get_sequence_counter(const unsigned char *frame_data, |
| 163 | uint32_t meta_data_offset, size_t num_frames_per_cycle) |
| 164 | { |
| 165 | struct reference_meta_data *meta_data; |
| 166 | |
| 167 | meta_data = (struct reference_meta_data *)(frame_data + meta_data_offset); |
| 168 | |
| 169 | return meta_data_to_sequence_counter(meta_data, num_frames_per_cycle); |
| 170 | } |
| 171 | |
| 172 | static inline void set_sequence_counter(unsigned char *frame_data, uint32_t meta_data_offset, |
| 173 | uint64_t sequence_counter, size_t num_frames_per_cycle) |
| 174 | { |
| 175 | struct reference_meta_data *meta_data; |
| 176 | |
| 177 | meta_data = (struct reference_meta_data *)(frame_data + meta_data_offset); |
| 178 | |
| 179 | sequence_counter_to_meta_data(meta_data, sequence_counter, num_frames_per_cycle); |
| 180 | } |
| 181 | |
| 182 | uint32_t get_meta_data_offset(enum stat_frame_type frame_type, enum security_mode security_mode); |
| 183 | |
| 184 | #endif /* _UTILS_H_ */ |