| 1 | /* SPDX-License-Identifier: BSD-2-Clause */cxx . . $ `<file>/src/utils.h`/ |
| 2 | /* |
| 3 | * Copyright (C) 2020-2024 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _UTILS_H_ |
| 8 | #define _UTILS_H_cxx . . $ `src/utils.h:8:9`! |
| 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"cxx . . $ `<file>/src/config.h`/ |
| 19 | #include "net_def.h"cxx . . $ `<file>/src/net_def.h`/ |
| 20 | #include "security.h"cxx . . $ `<file>/src/security.h`/ |
| 21 | #include "stat.h"cxx . . $ `<file>/src/stat.h`/ |
| 22 | |
| 23 | /* timing */ |
| 24 | #define MSEC_PER_SECcxx . . $ `src/utils.h:24:9`! 1000 |
| 25 | #define USEC_PER_SECcxx . . $ `src/utils.h:25:9`! 1000000 |
| 26 | #define NSEC_PER_SECcxx . . $ `src/utils.h:26:9`! 1000000000LL |
| 27 | |
| 28 | static inline void ns_to_tscxx . . $ ns_to_ts(18e44028df9850c9).(int64_tcxx . . $ int64_t# ns, struct timespeccxx . . $ timespec# *ts) |
| 29 | { |
| 30 | ts->tv_seccxx . . $ timespec#tv_sec. = ns / NSEC_PER_SECcxx . . $ `src/utils.h:26:9`!; |
| 31 | ts->tv_nseccxx . . $ timespec#tv_nsec. = ns % NSEC_PER_SECcxx . . $ `src/utils.h:26:9`!; |
| 32 | } |
| 33 | |
| 34 | static inline int64_tcxx . . $ int64_t# ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(const struct timespeccxx . . $ timespec# *ts) |
| 35 | { |
| 36 | return ts->tv_seccxx . . $ timespec#tv_sec. * NSEC_PER_SECcxx . . $ `src/utils.h:26:9`! + ts->tv_nseccxx . . $ timespec#tv_nsec.; |
| 37 | } |
| 38 | |
| 39 | void increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(struct timespeccxx . . $ timespec# *time, int64_tcxx . . $ int64_t# period_ns); |
| 40 | |
| 41 | void swap_mac_addressescxx . . $ swap_mac_addresses(5500c4293b1bbef0).(void *buffer, size_t len); |
| 42 | void insert_vlan_tagcxx . . $ insert_vlan_tag(136eef189baebb4d).(void *buffer, size_t len, uint16_tcxx . . $ 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_rxcxx . . $ build_vlan_frame_from_rx(ba210f13e41aa3ab).(const unsigned char *old_frame, size_t old_frame_len, |
| 53 | unsigned char *new_frame, size_t new_frame_len, uint16_tcxx . . $ uint16_t# ether_type, |
| 54 | uint16_tcxx . . $ 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_framecxx . . $ initialize_profinet_frame(305659cd59e50ce5).(enum security_modecxx . . $ 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_tcxx . . $ uint16_t# vlan_tci, uint16_tcxx . . $ 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_framecxx . . $ receive_profinet_frame(ddfd8079f1f43689).(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_configcxx . . $ prepare_frame_config# { |
| 82 | enum security_modecxx . . $ security_mode# modecxx . . $ prepare_frame_config#mode.; |
| 83 | struct security_contextcxx . . $ security_context# *security_contextcxx . . $ prepare_frame_config#security_context.; |
| 84 | const unsigned char *iv_prefixcxx . . $ prepare_frame_config#iv_prefix.; |
| 85 | const unsigned char *payload_patterncxx . . $ prepare_frame_config#payload_pattern.; |
| 86 | size_t payload_pattern_lengthcxx . . $ prepare_frame_config#payload_pattern_length.; |
| 87 | unsigned char *frame_datacxx . . $ prepare_frame_config#frame_data.; |
| 88 | size_t frame_lengthcxx . . $ prepare_frame_config#frame_length.; |
| 89 | size_t num_frames_per_cyclecxx . . $ prepare_frame_config#num_frames_per_cycle.; |
| 90 | uint64_tcxx . . $ uint64_t# sequence_countercxx . . $ prepare_frame_config#sequence_counter.; |
| 91 | uint64_tcxx . . $ uint64_t# tx_timestampcxx . . $ prepare_frame_config#tx_timestamp.; |
| 92 | uint32_tcxx . . $ uint32_t# meta_data_offsetcxx . . $ prepare_frame_config#meta_data_offset.; |
| 93 | }; |
| 94 | |
| 95 | int prepare_frame_for_txcxx . . $ prepare_frame_for_tx(f7c7fe1588440d5a).(const struct prepare_frame_configcxx . . $ prepare_frame_config# *frame_config); |
| 96 | |
| 97 | void prepare_ivcxx . . $ prepare_iv(717f525d724fe2d8).(const unsigned char *iv_prefix, uint64_tcxx . . $ uint64_t# sequence_counter, struct security_ivcxx . . $ security_iv# *iv); |
| 98 | |
| 99 | void prepare_opensslcxx . . $ prepare_openssl(180fdc1b6a791d79).(struct security_contextcxx . . $ security_context# *context); |
| 100 | |
| 101 | int get_thread_start_timecxx . . $ get_thread_start_time(72e9d11157e5bc5a).(uint64_tcxx . . $ uint64_t# base_offset, struct timespeccxx . . $ timespec# *wakeup_time); |
| 102 | |
| 103 | void configure_cpu_latencycxx . . $ configure_cpu_latency(49f6e7a06ebc5aa8).(void); |
| 104 | void restore_cpu_latencycxx . . $ restore_cpu_latency(49f6e7a06ebc5aa8).(void); |
| 105 | |
| 106 | /* error handling */ |
| 107 | void pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(int ret, const char *message); |
| 108 | |
| 109 | /* Printing */ |
| 110 | void print_mac_addresscxx . . $ print_mac_address(28caea9380bfac8a).(const unsigned char *mac_address); |
| 111 | void print_payload_patterncxx . . $ print_payload_pattern(7cafb6e0818eff3b).(const char *payload_pattern, size_t payload_pattern_length); |
| 112 | |
| 113 | #define ARRAY_SIZEcxx . . $ `src/utils.h:113:9`!(x) (sizeof(x) / sizeof((x)[0])) |
| 114 | |
| 115 | #define BITcxx . . $ `src/utils.h:115:9`!(x) (1ULL << (x)) |
| 116 | |
| 117 | /* Meta data handling */ |
| 118 | static inline uint64_tcxx . . $ uint64_t# meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(const struct reference_meta_datacxx . . $ reference_meta_data# *meta, |
| 119 | size_t num_frames_per_cycle) |
| 120 | { |
| 121 | uint32_tcxx . . $ uint32_t# frame_counter, cycle_counter; |
| 122 | |
| 123 | frame_counter = be32tohcxx . . $ reference_meta_data#frame_counter.(meta->frame_counter); |
| 124 | cycle_counter = be32tohcxx . . $ `<external>/15855202785537844739/endian.h:46:11`!(meta->cycle_counter); |
| 125 | |
| 126 | return (uint64_tcxx . . $ uint64_t#)cycle_counter * num_frames_per_cycle + frame_counter; |
| 127 | } |
| 128 | |
| 129 | static inline void sequence_counter_to_meta_datacxx . . $ sequence_counter_to_meta_data(9318332f0006d3bc).(struct reference_meta_datacxx . . $ reference_meta_data# *meta, |
| 130 | uint64_tcxx . . $ uint64_t# sequence_counter, |
| 131 | size_t num_frames_per_cycle) |
| 132 | { |
| 133 | meta->frame_countercxx . . $ reference_meta_data#frame_counter. = htobe32cxx . . $ `<external>/15855202785537844739/endian.h:44:11`!(sequence_counter % num_frames_per_cycle); |
| 134 | meta->cycle_countercxx . . $ reference_meta_data#cycle_counter. = htobe32cxx . . $ __bswap_32(2141b656ce2cc063).(sequence_counter / num_frames_per_cycle); |
| 135 | } |
| 136 | |
| 137 | static inline void tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(struct reference_meta_datacxx . . $ reference_meta_data# *meta, uint64_tcxx . . $ uint64_t# timestamp) |
| 138 | { |
| 139 | meta->tx_timestampcxx . . $ reference_meta_data#tx_timestamp. = htobe64cxx . . $ __bswap_64(ecdaa1632f49f591).(timestamp); |
| 140 | } |
| 141 | |
| 142 | static inline uint64_tcxx . . $ uint64_t# meta_data_to_tx_timestampcxx . . $ meta_data_to_tx_timestamp(e17106d766bd017c).(const struct reference_meta_datacxx . . $ reference_meta_data# *meta) |
| 143 | { |
| 144 | uint64_tcxx . . $ uint64_t# tx_timestamp; |
| 145 | |
| 146 | tx_timestamp = be64tohcxx . . $ __bswap_64(ecdaa1632f49f591).(meta->tx_timestamp); |
| 147 | |
| 148 | return tx_timestamp; |
| 149 | } |
| 150 | |
| 151 | static inline void set_mirror_tx_timestampcxx . . $ set_mirror_tx_timestamp(457bf14cd1570296).(struct reference_meta_datacxx . . $ reference_meta_data# *meta) |
| 152 | { |
| 153 | struct timespeccxx . . $ timespec# now; |
| 154 | |
| 155 | clock_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., &now); |
| 156 | |
| 157 | tx_timestamp_to_meta_datacxx . . $ tx_timestamp_to_meta_data(d237292223698be2).(meta, |
| 158 | ts_to_nscxx . . $ ts_to_ns(3d5e857b70563169).(&now) + (app_configcxx . . $ app_config..application_tx_base_offset_nscxx . . $ application_config#application_tx_base_offset_ns. - |
| 159 | app_configcxx . . $ app_config..application_rx_base_offset_nscxx . . $ application_config#application_rx_base_offset_ns.)); |
| 160 | } |
| 161 | |
| 162 | static inline uint64_tcxx . . $ uint64_t# get_sequence_countercxx . . $ get_sequence_counter(9512b04a879eebb0).(const unsigned char *frame_data, |
| 163 | uint32_tcxx . . $ uint32_t# meta_data_offset, size_t num_frames_per_cycle) |
| 164 | { |
| 165 | struct reference_meta_datacxx . . $ reference_meta_data# *meta_data; |
| 166 | |
| 167 | meta_data = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_data + meta_data_offset); |
| 168 | |
| 169 | return meta_data_to_sequence_countercxx . . $ meta_data_to_sequence_counter(85d454852c71d7b9).(meta_data, num_frames_per_cycle); |
| 170 | } |
| 171 | |
| 172 | static inline void set_sequence_countercxx . . $ set_sequence_counter(6a6ec7771d14a153).(unsigned char *frame_data, uint32_tcxx . . $ uint32_t# meta_data_offset, |
| 173 | uint64_tcxx . . $ uint64_t# sequence_counter, size_t num_frames_per_cycle) |
| 174 | { |
| 175 | struct reference_meta_datacxx . . $ reference_meta_data# *meta_data; |
| 176 | |
| 177 | meta_data = (struct reference_meta_datacxx . . $ reference_meta_data# *)(frame_data + meta_data_offset); |
| 178 | |
| 179 | sequence_counter_to_meta_datacxx . . $ sequence_counter_to_meta_data(9318332f0006d3bc).(meta_data, sequence_counter, num_frames_per_cycle); |
| 180 | } |
| 181 | |
| 182 | uint32_tcxx . . $ uint32_t# get_meta_data_offsetcxx . . $ get_meta_data_offset(16cb9e606c8e3615).(enum stat_frame_typecxx . . $ stat_frame_type# frame_type, enum security_modecxx . . $ security_mode# security_mode); |
| 183 | |
| 184 | #endif /* _UTILS_H_ */ |