Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv4.h |
| 3 | * Shortcut forwarding engine header file for IPv4. |
| 4 | * |
| 5 | * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved. |
| 6 | * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved. |
| 7 | * |
| 8 | * Permission to use, copy, modify, and/or distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | */ |
| 20 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 21 | #define SFE_IPV4_DSCP_MASK 0x3 |
| 22 | #define SFE_IPV4_DSCP_SHIFT 2 |
| 23 | |
| 24 | /* |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 25 | * Specifies the lower bound on ACK numbers carried in the TCP header |
| 26 | */ |
| 27 | #define SFE_IPV4_TCP_MAX_ACK_WINDOW 65520 |
| 28 | |
| 29 | /* |
| 30 | * IPv4 TCP connection match additional data. |
| 31 | */ |
| 32 | struct sfe_ipv4_tcp_connection_match { |
| 33 | u8 win_scale; /* Window scale */ |
| 34 | u32 max_win; /* Maximum window size seen */ |
| 35 | u32 end; /* Sequence number of the next byte to send (seq + segment length) */ |
| 36 | u32 max_end; /* Sequence number of the last byte to ack */ |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Bit flags for IPv4 connection matching entry. |
| 41 | */ |
| 42 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC (1<<0) |
| 43 | /* Perform source translation */ |
| 44 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST (1<<1) |
| 45 | /* Perform destination translation */ |
| 46 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK (1<<2) |
| 47 | /* Ignore TCP sequence numbers */ |
| 48 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR (1<<3) |
| 49 | /* Fast Ethernet header write */ |
| 50 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR (1<<4) |
| 51 | /* Fast Ethernet header write */ |
| 52 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK (1<<5) |
| 53 | /* remark priority of SKB */ |
| 54 | #define SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK (1<<6) |
| 55 | /* remark DSCP of packet */ |
| 56 | |
| 57 | /* |
| 58 | * IPv4 connection matching structure. |
| 59 | */ |
| 60 | struct sfe_ipv4_connection_match { |
| 61 | /* |
| 62 | * References to other objects. |
| 63 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 64 | struct hlist_node hnode; |
| 65 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 66 | struct sfe_ipv4_connection *connection; |
| 67 | struct sfe_ipv4_connection_match *counter_match; |
| 68 | /* Matches the flow in the opposite direction as the one in *connection */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 69 | /* |
| 70 | * Characteristics that identify flows that match this rule. |
| 71 | */ |
| 72 | struct net_device *match_dev; /* Network device */ |
| 73 | u8 match_protocol; /* Protocol */ |
| 74 | __be32 match_src_ip; /* Source IP address */ |
| 75 | __be32 match_dest_ip; /* Destination IP address */ |
| 76 | __be16 match_src_port; /* Source port/connection ident */ |
| 77 | __be16 match_dest_port; /* Destination port/connection ident */ |
| 78 | |
| 79 | /* |
| 80 | * Control the operations of the match. |
| 81 | */ |
| 82 | u32 flags; /* Bit flags */ |
| 83 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 84 | u32 flow_cookie; /* used flow cookie, for debug */ |
| 85 | #endif |
| 86 | #ifdef CONFIG_XFRM |
| 87 | u32 flow_accel; /* The flow accelerated or not */ |
| 88 | #endif |
| 89 | |
| 90 | /* |
| 91 | * Connection state that we track once we match. |
| 92 | */ |
| 93 | union { /* Protocol-specific state */ |
| 94 | struct sfe_ipv4_tcp_connection_match tcp; |
| 95 | } protocol_state; |
| 96 | /* |
| 97 | * Stats recorded in a sync period. These stats will be added to |
| 98 | * rx_packet_count64/rx_byte_count64 after a sync period. |
| 99 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 100 | atomic_t rx_packet_count; |
| 101 | atomic_t rx_byte_count; |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Packet translation information. |
| 105 | */ |
| 106 | __be32 xlate_src_ip; /* Address after source translation */ |
| 107 | __be16 xlate_src_port; /* Port/connection ident after source translation */ |
| 108 | u16 xlate_src_csum_adjustment; |
| 109 | /* Transport layer checksum adjustment after source translation */ |
| 110 | u16 xlate_src_partial_csum_adjustment; |
| 111 | /* Transport layer pseudo header checksum adjustment after source translation */ |
| 112 | |
| 113 | __be32 xlate_dest_ip; /* Address after destination translation */ |
| 114 | __be16 xlate_dest_port; /* Port/connection ident after destination translation */ |
| 115 | u16 xlate_dest_csum_adjustment; |
| 116 | /* Transport layer checksum adjustment after destination translation */ |
| 117 | u16 xlate_dest_partial_csum_adjustment; |
| 118 | /* Transport layer pseudo header checksum adjustment after destination translation */ |
| 119 | |
| 120 | /* |
| 121 | * QoS information |
| 122 | */ |
| 123 | u32 priority; |
| 124 | u32 dscp; |
| 125 | |
| 126 | /* |
| 127 | * Packet transmit information. |
| 128 | */ |
| 129 | struct net_device *xmit_dev; /* Network device on which to transmit */ |
| 130 | unsigned short int xmit_dev_mtu; |
| 131 | /* Interface MTU */ |
| 132 | u16 xmit_dest_mac[ETH_ALEN / 2]; |
| 133 | /* Destination MAC address to use when forwarding */ |
| 134 | u16 xmit_src_mac[ETH_ALEN / 2]; |
| 135 | /* Source MAC address to use when forwarding */ |
| 136 | |
| 137 | /* |
| 138 | * Summary stats. |
| 139 | */ |
| 140 | u64 rx_packet_count64; |
| 141 | u64 rx_byte_count64; |
| 142 | }; |
| 143 | |
| 144 | /* |
| 145 | * Per-connection data structure. |
| 146 | */ |
| 147 | struct sfe_ipv4_connection { |
| 148 | struct sfe_ipv4_connection *next; |
| 149 | /* Pointer to the next entry in a hash chain */ |
| 150 | struct sfe_ipv4_connection *prev; |
| 151 | /* Pointer to the previous entry in a hash chain */ |
| 152 | int protocol; /* IP protocol number */ |
| 153 | __be32 src_ip; /* Src IP addr pre-translation */ |
| 154 | __be32 src_ip_xlate; /* Src IP addr post-translation */ |
| 155 | __be32 dest_ip; /* Dest IP addr pre-translation */ |
| 156 | __be32 dest_ip_xlate; /* Dest IP addr post-translation */ |
| 157 | __be16 src_port; /* Src port pre-translation */ |
| 158 | __be16 src_port_xlate; /* Src port post-translation */ |
| 159 | __be16 dest_port; /* Dest port pre-translation */ |
| 160 | __be16 dest_port_xlate; /* Dest port post-translation */ |
| 161 | struct sfe_ipv4_connection_match *original_match; |
| 162 | /* Original direction matching structure */ |
| 163 | struct net_device *original_dev; |
| 164 | /* Original direction source device */ |
| 165 | struct sfe_ipv4_connection_match *reply_match; |
| 166 | /* Reply direction matching structure */ |
| 167 | struct net_device *reply_dev; /* Reply direction source device */ |
| 168 | u64 last_sync_jiffies; /* Jiffies count for the last sync */ |
| 169 | struct sfe_ipv4_connection *all_connections_next; |
| 170 | /* Pointer to the next entry in the list of all connections */ |
| 171 | struct sfe_ipv4_connection *all_connections_prev; |
| 172 | /* Pointer to the previous entry in the list of all connections */ |
| 173 | u32 mark; /* mark for outgoing packet */ |
| 174 | u32 debug_read_seq; /* sequence number for debug dump */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 175 | bool removed; /* Indicates the connection is removed */ |
| 176 | struct rcu_head rcu; /* delay rcu free */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /* |
| 180 | * IPv4 connections and hash table size information. |
| 181 | */ |
| 182 | #define SFE_IPV4_CONNECTION_HASH_SHIFT 12 |
| 183 | #define SFE_IPV4_CONNECTION_HASH_SIZE (1 << SFE_IPV4_CONNECTION_HASH_SHIFT) |
| 184 | #define SFE_IPV4_CONNECTION_HASH_MASK (SFE_IPV4_CONNECTION_HASH_SIZE - 1) |
| 185 | |
| 186 | enum sfe_ipv4_exception_events { |
| 187 | SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE, |
| 188 | SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION, |
| 189 | SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT, |
| 190 | SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL, |
| 191 | SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION, |
| 192 | SFE_IPV4_EXCEPTION_EVENT_TCP_HEADER_INCOMPLETE, |
| 193 | SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_SLOW_FLAGS, |
| 194 | SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_FAST_FLAGS, |
| 195 | SFE_IPV4_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT, |
| 196 | SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_TTL, |
| 197 | SFE_IPV4_EXCEPTION_EVENT_TCP_NEEDS_FRAGMENTATION, |
| 198 | SFE_IPV4_EXCEPTION_EVENT_TCP_FLAGS, |
| 199 | SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_EXCEEDS_RIGHT_EDGE, |
| 200 | SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_DATA_OFFS, |
| 201 | SFE_IPV4_EXCEPTION_EVENT_TCP_BAD_SACK, |
| 202 | SFE_IPV4_EXCEPTION_EVENT_TCP_BIG_DATA_OFFS, |
| 203 | SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_BEFORE_LEFT_EDGE, |
| 204 | SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_EXCEEDS_RIGHT_EDGE, |
| 205 | SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_BEFORE_LEFT_EDGE, |
| 206 | SFE_IPV4_EXCEPTION_EVENT_ICMP_HEADER_INCOMPLETE, |
| 207 | SFE_IPV4_EXCEPTION_EVENT_ICMP_UNHANDLED_TYPE, |
| 208 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_HEADER_INCOMPLETE, |
| 209 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_NON_V4, |
| 210 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_IP_OPTIONS_INCOMPLETE, |
| 211 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UDP_HEADER_INCOMPLETE, |
| 212 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_TCP_HEADER_INCOMPLETE, |
| 213 | SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UNHANDLED_PROTOCOL, |
| 214 | SFE_IPV4_EXCEPTION_EVENT_ICMP_NO_CONNECTION, |
| 215 | SFE_IPV4_EXCEPTION_EVENT_ICMP_FLUSHED_CONNECTION, |
| 216 | SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE, |
| 217 | SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH, |
| 218 | SFE_IPV4_EXCEPTION_EVENT_NON_V4, |
| 219 | SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT, |
| 220 | SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE, |
| 221 | SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE, |
| 222 | SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL, |
| 223 | SFE_IPV4_EXCEPTION_EVENT_LAST |
| 224 | }; |
| 225 | |
| 226 | /* |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 227 | * per CPU stats |
| 228 | */ |
| 229 | struct sfe_ipv4_stats { |
| 230 | /* |
| 231 | * Stats recorded in a sync period. These stats will be added to |
| 232 | * connection_xxx64 after a sync period. |
| 233 | */ |
| 234 | u64 connection_create_requests64; |
| 235 | /* Number of IPv4 connection create requests */ |
| 236 | u64 connection_create_collisions64; |
| 237 | /* Number of IPv4 connection create requests that collided with existing hash table entries */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 238 | u64 connection_create_failures64; |
| 239 | /* Number of IPv4 connection create requests that failed */ |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 240 | u64 connection_destroy_requests64; |
| 241 | /* Number of IPv4 connection destroy requests */ |
| 242 | u64 connection_destroy_misses64; |
| 243 | /* Number of IPv4 connection destroy requests that missed our hash table */ |
| 244 | u64 connection_match_hash_hits64; |
| 245 | /* Number of IPv4 connection match hash hits */ |
| 246 | u64 connection_match_hash_reorders64; |
| 247 | /* Number of IPv4 connection match hash reorders */ |
| 248 | u64 connection_flushes64; /* Number of IPv4 connection flushes */ |
| 249 | u64 packets_forwarded64; /* Number of IPv4 packets forwarded */ |
| 250 | u64 packets_not_forwarded64; /* Number of IPv4 packets not forwarded */ |
| 251 | u64 exception_events64[SFE_IPV4_EXCEPTION_EVENT_LAST]; |
| 252 | }; |
| 253 | |
| 254 | /* |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 255 | * Per-module structure. |
| 256 | */ |
| 257 | struct sfe_ipv4 { |
| 258 | spinlock_t lock; /* Lock for SMP correctness */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 259 | struct sfe_ipv4_connection *all_connections_head; |
| 260 | /* Head of the list of all connections */ |
| 261 | struct sfe_ipv4_connection *all_connections_tail; |
| 262 | /* Tail of the list of all connections */ |
| 263 | unsigned int num_connections; /* Number of connections */ |
| 264 | struct timer_list timer; /* Timer used for periodic sync ops */ |
| 265 | sfe_sync_rule_callback_t __rcu sync_rule_callback; |
| 266 | /* Callback function registered by a connection manager for stats syncing */ |
| 267 | struct sfe_ipv4_connection *conn_hash[SFE_IPV4_CONNECTION_HASH_SIZE]; |
| 268 | /* Connection hash table */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 269 | |
| 270 | struct hlist_head hlist_conn_match_hash_head[SFE_IPV4_CONNECTION_HASH_SIZE]; |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 271 | /* Connection match hash table */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 272 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 273 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 274 | struct sfe_flow_cookie_entry sfe_flow_cookie_table[SFE_FLOW_COOKIE_SIZE]; |
| 275 | /* flow cookie table*/ |
| 276 | flow_cookie_set_func_t flow_cookie_set_func; |
| 277 | /* function used to configure flow cookie in hardware*/ |
| 278 | int flow_cookie_enable; |
| 279 | /* Enable/disable flow cookie at runtime */ |
| 280 | #endif |
| 281 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 282 | struct sfe_ipv4_stats __percpu *stats_pcpu; |
| 283 | /* Per CPU statistics. */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 284 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame^] | 285 | struct sfe_ipv4_connection *wc_next; /* Connection list walk pointer for stats sync */ |
| 286 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 287 | /* |
| 288 | * Control state. |
| 289 | */ |
| 290 | struct kobject *sys_sfe_ipv4; /* sysfs linkage */ |
| 291 | int debug_dev; /* Major number of the debug char device */ |
| 292 | u32 debug_read_seq; /* sequence number for debug dump */ |
| 293 | }; |
| 294 | |
| 295 | /* |
| 296 | * Enumeration of the XML output. |
| 297 | */ |
| 298 | enum sfe_ipv4_debug_xml_states { |
| 299 | SFE_IPV4_DEBUG_XML_STATE_START, |
| 300 | SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_START, |
| 301 | SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_CONNECTION, |
| 302 | SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_END, |
| 303 | SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_START, |
| 304 | SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_EXCEPTION, |
| 305 | SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_END, |
| 306 | SFE_IPV4_DEBUG_XML_STATE_STATS, |
| 307 | SFE_IPV4_DEBUG_XML_STATE_END, |
| 308 | SFE_IPV4_DEBUG_XML_STATE_DONE |
| 309 | }; |
| 310 | |
| 311 | /* |
| 312 | * XML write state. |
| 313 | */ |
| 314 | struct sfe_ipv4_debug_xml_write_state { |
| 315 | enum sfe_ipv4_debug_xml_states state; |
| 316 | /* XML output file state machine state */ |
| 317 | int iter_exception; /* Next exception iterator */ |
| 318 | }; |
| 319 | |
| 320 | typedef bool (*sfe_ipv4_debug_xml_write_method_t)(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 321 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws); |
| 322 | |
| 323 | void sfe_ipv4_exit(void); |
| 324 | int sfe_ipv4_init(void); |