blob: 75b89d53cf360303b1d8830eef0c50a600b4dfb0 [file] [log] [blame]
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05301/*
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 Kannoth24fb1db2021-10-20 07:28:06 +053021#define SFE_IPV4_DSCP_MASK 0x3
22#define SFE_IPV4_DSCP_SHIFT 2
23
24/*
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053025 * 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 */
32struct 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 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +053056#define SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD (1<<7)
57 /* checksum offload.*/
Guduri Prathyushaeb31c902021-11-10 20:18:50 +053058#define SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP (1<<8)
59 /* Indicates that PPPoE should be decapsulated */
60#define SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP (1<<9)
61 /* Indicates that PPPoE should be encapsulated */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053062
63/*
64 * IPv4 connection matching structure.
65 */
66struct sfe_ipv4_connection_match {
67 /*
68 * References to other objects.
69 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +053070 struct hlist_node hnode;
71
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053072 struct sfe_ipv4_connection *connection;
73 struct sfe_ipv4_connection_match *counter_match;
74 /* Matches the flow in the opposite direction as the one in *connection */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053075 /*
76 * Characteristics that identify flows that match this rule.
77 */
78 struct net_device *match_dev; /* Network device */
79 u8 match_protocol; /* Protocol */
80 __be32 match_src_ip; /* Source IP address */
81 __be32 match_dest_ip; /* Destination IP address */
82 __be16 match_src_port; /* Source port/connection ident */
83 __be16 match_dest_port; /* Destination port/connection ident */
84
85 /*
86 * Control the operations of the match.
87 */
88 u32 flags; /* Bit flags */
89#ifdef CONFIG_NF_FLOW_COOKIE
90 u32 flow_cookie; /* used flow cookie, for debug */
91#endif
92#ifdef CONFIG_XFRM
93 u32 flow_accel; /* The flow accelerated or not */
94#endif
95
96 /*
97 * Connection state that we track once we match.
98 */
99 union { /* Protocol-specific state */
100 struct sfe_ipv4_tcp_connection_match tcp;
101 } protocol_state;
102 /*
103 * Stats recorded in a sync period. These stats will be added to
104 * rx_packet_count64/rx_byte_count64 after a sync period.
105 */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530106 atomic_t rx_packet_count;
107 atomic_t rx_byte_count;
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530108
109 /*
110 * Packet translation information.
111 */
112 __be32 xlate_src_ip; /* Address after source translation */
113 __be16 xlate_src_port; /* Port/connection ident after source translation */
114 u16 xlate_src_csum_adjustment;
115 /* Transport layer checksum adjustment after source translation */
116 u16 xlate_src_partial_csum_adjustment;
117 /* Transport layer pseudo header checksum adjustment after source translation */
118
119 __be32 xlate_dest_ip; /* Address after destination translation */
120 __be16 xlate_dest_port; /* Port/connection ident after destination translation */
121 u16 xlate_dest_csum_adjustment;
122 /* Transport layer checksum adjustment after destination translation */
123 u16 xlate_dest_partial_csum_adjustment;
124 /* Transport layer pseudo header checksum adjustment after destination translation */
125
126 /*
127 * QoS information
128 */
129 u32 priority;
130 u32 dscp;
131
132 /*
133 * Packet transmit information.
134 */
135 struct net_device *xmit_dev; /* Network device on which to transmit */
136 unsigned short int xmit_dev_mtu;
137 /* Interface MTU */
138 u16 xmit_dest_mac[ETH_ALEN / 2];
139 /* Destination MAC address to use when forwarding */
140 u16 xmit_src_mac[ETH_ALEN / 2];
141 /* Source MAC address to use when forwarding */
142
143 /*
144 * Summary stats.
145 */
146 u64 rx_packet_count64;
147 u64 rx_byte_count64;
Guduri Prathyushaeb31c902021-11-10 20:18:50 +0530148
149 /*
150 * PPPoE information
151 */
152 u16 pppoe_session_id;
153 u8 pppoe_remote_mac[ETH_ALEN];
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530154};
155
156/*
157 * Per-connection data structure.
158 */
159struct sfe_ipv4_connection {
160 struct sfe_ipv4_connection *next;
161 /* Pointer to the next entry in a hash chain */
162 struct sfe_ipv4_connection *prev;
163 /* Pointer to the previous entry in a hash chain */
164 int protocol; /* IP protocol number */
165 __be32 src_ip; /* Src IP addr pre-translation */
166 __be32 src_ip_xlate; /* Src IP addr post-translation */
167 __be32 dest_ip; /* Dest IP addr pre-translation */
168 __be32 dest_ip_xlate; /* Dest IP addr post-translation */
169 __be16 src_port; /* Src port pre-translation */
170 __be16 src_port_xlate; /* Src port post-translation */
171 __be16 dest_port; /* Dest port pre-translation */
172 __be16 dest_port_xlate; /* Dest port post-translation */
173 struct sfe_ipv4_connection_match *original_match;
174 /* Original direction matching structure */
175 struct net_device *original_dev;
176 /* Original direction source device */
177 struct sfe_ipv4_connection_match *reply_match;
178 /* Reply direction matching structure */
179 struct net_device *reply_dev; /* Reply direction source device */
180 u64 last_sync_jiffies; /* Jiffies count for the last sync */
181 struct sfe_ipv4_connection *all_connections_next;
182 /* Pointer to the next entry in the list of all connections */
183 struct sfe_ipv4_connection *all_connections_prev;
184 /* Pointer to the previous entry in the list of all connections */
185 u32 mark; /* mark for outgoing packet */
186 u32 debug_read_seq; /* sequence number for debug dump */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530187 bool removed; /* Indicates the connection is removed */
188 struct rcu_head rcu; /* delay rcu free */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530189};
190
191/*
192 * IPv4 connections and hash table size information.
193 */
194#define SFE_IPV4_CONNECTION_HASH_SHIFT 12
195#define SFE_IPV4_CONNECTION_HASH_SIZE (1 << SFE_IPV4_CONNECTION_HASH_SHIFT)
196#define SFE_IPV4_CONNECTION_HASH_MASK (SFE_IPV4_CONNECTION_HASH_SIZE - 1)
197
198enum sfe_ipv4_exception_events {
199 SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE,
200 SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION,
201 SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT,
202 SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL,
203 SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION,
204 SFE_IPV4_EXCEPTION_EVENT_TCP_HEADER_INCOMPLETE,
205 SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_SLOW_FLAGS,
206 SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_FAST_FLAGS,
207 SFE_IPV4_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT,
208 SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_TTL,
209 SFE_IPV4_EXCEPTION_EVENT_TCP_NEEDS_FRAGMENTATION,
210 SFE_IPV4_EXCEPTION_EVENT_TCP_FLAGS,
211 SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_EXCEEDS_RIGHT_EDGE,
212 SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_DATA_OFFS,
213 SFE_IPV4_EXCEPTION_EVENT_TCP_BAD_SACK,
214 SFE_IPV4_EXCEPTION_EVENT_TCP_BIG_DATA_OFFS,
215 SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_BEFORE_LEFT_EDGE,
216 SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_EXCEEDS_RIGHT_EDGE,
217 SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_BEFORE_LEFT_EDGE,
218 SFE_IPV4_EXCEPTION_EVENT_ICMP_HEADER_INCOMPLETE,
219 SFE_IPV4_EXCEPTION_EVENT_ICMP_UNHANDLED_TYPE,
220 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_HEADER_INCOMPLETE,
221 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_NON_V4,
222 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_IP_OPTIONS_INCOMPLETE,
223 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UDP_HEADER_INCOMPLETE,
224 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_TCP_HEADER_INCOMPLETE,
225 SFE_IPV4_EXCEPTION_EVENT_ICMP_IPV4_UNHANDLED_PROTOCOL,
226 SFE_IPV4_EXCEPTION_EVENT_ICMP_NO_CONNECTION,
227 SFE_IPV4_EXCEPTION_EVENT_ICMP_FLUSHED_CONNECTION,
228 SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE,
Ratheesh Kannoth43d64f82021-10-20 08:23:29 +0530229 SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD,
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530230 SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH,
231 SFE_IPV4_EXCEPTION_EVENT_NON_V4,
232 SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT,
233 SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE,
234 SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE,
235 SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL,
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530236 SFE_IPV4_EXCEPTION_EVENT_PPPOE_HEADER_ENCAP_FAILED,
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530237 SFE_IPV4_EXCEPTION_EVENT_LAST
238};
239
240/*
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530241 * per CPU stats
242 */
243struct sfe_ipv4_stats {
244 /*
245 * Stats recorded in a sync period. These stats will be added to
246 * connection_xxx64 after a sync period.
247 */
248 u64 connection_create_requests64;
249 /* Number of IPv4 connection create requests */
250 u64 connection_create_collisions64;
251 /* Number of IPv4 connection create requests that collided with existing hash table entries */
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530252 u64 connection_create_failures64;
253 /* Number of IPv4 connection create requests that failed */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530254 u64 connection_destroy_requests64;
255 /* Number of IPv4 connection destroy requests */
256 u64 connection_destroy_misses64;
257 /* Number of IPv4 connection destroy requests that missed our hash table */
258 u64 connection_match_hash_hits64;
259 /* Number of IPv4 connection match hash hits */
260 u64 connection_match_hash_reorders64;
261 /* Number of IPv4 connection match hash reorders */
262 u64 connection_flushes64; /* Number of IPv4 connection flushes */
263 u64 packets_forwarded64; /* Number of IPv4 packets forwarded */
264 u64 packets_not_forwarded64; /* Number of IPv4 packets not forwarded */
265 u64 exception_events64[SFE_IPV4_EXCEPTION_EVENT_LAST];
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530266 u64 pppoe_encap_packets_forwarded64; /* Number of IPv4 PPPOE encap packets forwarded */
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530267};
268
269/*
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530270 * Per-module structure.
271 */
272struct sfe_ipv4 {
273 spinlock_t lock; /* Lock for SMP correctness */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530274 struct sfe_ipv4_connection *all_connections_head;
275 /* Head of the list of all connections */
276 struct sfe_ipv4_connection *all_connections_tail;
277 /* Tail of the list of all connections */
278 unsigned int num_connections; /* Number of connections */
Ken Zhu137722d2021-09-23 17:57:36 -0700279 struct delayed_work sync_dwork; /* Work to sync the statistics */
280 unsigned int work_cpu; /* The core to run stats sync on */
281
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530282 sfe_sync_rule_callback_t __rcu sync_rule_callback;
283 /* Callback function registered by a connection manager for stats syncing */
284 struct sfe_ipv4_connection *conn_hash[SFE_IPV4_CONNECTION_HASH_SIZE];
285 /* Connection hash table */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530286
287 struct hlist_head hlist_conn_match_hash_head[SFE_IPV4_CONNECTION_HASH_SIZE];
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530288 /* Connection match hash table */
Ratheesh Kannoth94fc5b82021-10-20 07:45:06 +0530289
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530290#ifdef CONFIG_NF_FLOW_COOKIE
291 struct sfe_flow_cookie_entry sfe_flow_cookie_table[SFE_FLOW_COOKIE_SIZE];
292 /* flow cookie table*/
293 flow_cookie_set_func_t flow_cookie_set_func;
294 /* function used to configure flow cookie in hardware*/
295 int flow_cookie_enable;
296 /* Enable/disable flow cookie at runtime */
297#endif
298
Ratheesh Kannoth3aeb2892021-10-20 07:57:15 +0530299 struct sfe_ipv4_stats __percpu *stats_pcpu;
300 /* Per CPU statistics. */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530301
Ken Zhudc423672021-09-02 18:27:01 -0700302 struct sfe_ipv4_connection *wc_next; /* Connection list walk pointer for stats sync */
303
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530304 /*
305 * Control state.
306 */
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530307 struct kobject *sys_ipv4; /* sysfs linkage */
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530308 int debug_dev; /* Major number of the debug char device */
309 u32 debug_read_seq; /* sequence number for debug dump */
310};
311
312/*
313 * Enumeration of the XML output.
314 */
315enum sfe_ipv4_debug_xml_states {
316 SFE_IPV4_DEBUG_XML_STATE_START,
317 SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_START,
318 SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_CONNECTION,
319 SFE_IPV4_DEBUG_XML_STATE_CONNECTIONS_END,
320 SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_START,
321 SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_EXCEPTION,
322 SFE_IPV4_DEBUG_XML_STATE_EXCEPTIONS_END,
323 SFE_IPV4_DEBUG_XML_STATE_STATS,
324 SFE_IPV4_DEBUG_XML_STATE_END,
325 SFE_IPV4_DEBUG_XML_STATE_DONE
326};
327
328/*
329 * XML write state.
330 */
331struct sfe_ipv4_debug_xml_write_state {
332 enum sfe_ipv4_debug_xml_states state;
333 /* XML output file state machine state */
334 int iter_exception; /* Next exception iterator */
335};
336
337typedef bool (*sfe_ipv4_debug_xml_write_method_t)(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length,
338 int *total_read, struct sfe_ipv4_debug_xml_write_state *ws);
339
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530340u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph);
341void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason);
342bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c);
343void sfe_ipv4_flush_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c, sfe_sync_reason_t reason);
344
345struct sfe_ipv4_connection_match *
346sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol,
347 __be32 src_ip, __be16 src_port,
348 __be32 dest_ip, __be16 dest_port);
349
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530350void sfe_ipv4_exit(void);
351int sfe_ipv4_init(void);