Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv4.c |
| 3 | * Shortcut forwarding engine - IPv4 edition. |
| 4 | * |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 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 | * |
Xiaoping Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 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 |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 19 | */ |
Matthew McClintock | a322194 | 2014-01-16 11:44:26 -0600 | [diff] [blame] | 20 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 23 | #include <linux/skbuff.h> |
| 24 | #include <linux/icmp.h> |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 25 | #include <net/tcp.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 26 | #include <linux/etherdevice.h> |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 27 | #include <linux/version.h> |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 28 | #include <linux/lockdep.h> |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 29 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 30 | #include "sfe_debug.h" |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 31 | #include "sfe_api.h" |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 32 | #include "sfe.h" |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 33 | #include "sfe_flow_cookie.h" |
| 34 | #include "sfe_ipv4.h" |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 35 | #include "sfe_ipv4_udp.h" |
| 36 | #include "sfe_ipv4_tcp.h" |
| 37 | #include "sfe_ipv4_icmp.h" |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 38 | |
| 39 | static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = { |
| 40 | "UDP_HEADER_INCOMPLETE", |
| 41 | "UDP_NO_CONNECTION", |
| 42 | "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 43 | "UDP_SMALL_TTL", |
| 44 | "UDP_NEEDS_FRAGMENTATION", |
| 45 | "TCP_HEADER_INCOMPLETE", |
| 46 | "TCP_NO_CONNECTION_SLOW_FLAGS", |
| 47 | "TCP_NO_CONNECTION_FAST_FLAGS", |
| 48 | "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 49 | "TCP_SMALL_TTL", |
| 50 | "TCP_NEEDS_FRAGMENTATION", |
| 51 | "TCP_FLAGS", |
| 52 | "TCP_SEQ_EXCEEDS_RIGHT_EDGE", |
| 53 | "TCP_SMALL_DATA_OFFS", |
| 54 | "TCP_BAD_SACK", |
| 55 | "TCP_BIG_DATA_OFFS", |
| 56 | "TCP_SEQ_BEFORE_LEFT_EDGE", |
| 57 | "TCP_ACK_EXCEEDS_RIGHT_EDGE", |
| 58 | "TCP_ACK_BEFORE_LEFT_EDGE", |
| 59 | "ICMP_HEADER_INCOMPLETE", |
| 60 | "ICMP_UNHANDLED_TYPE", |
| 61 | "ICMP_IPV4_HEADER_INCOMPLETE", |
| 62 | "ICMP_IPV4_NON_V4", |
| 63 | "ICMP_IPV4_IP_OPTIONS_INCOMPLETE", |
| 64 | "ICMP_IPV4_UDP_HEADER_INCOMPLETE", |
| 65 | "ICMP_IPV4_TCP_HEADER_INCOMPLETE", |
| 66 | "ICMP_IPV4_UNHANDLED_PROTOCOL", |
| 67 | "ICMP_NO_CONNECTION", |
| 68 | "ICMP_FLUSHED_CONNECTION", |
| 69 | "HEADER_INCOMPLETE", |
Ratheesh Kannoth | 43d64f8 | 2021-10-20 08:23:29 +0530 | [diff] [blame] | 70 | "HEADER_CSUM_BAD", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 71 | "BAD_TOTAL_LENGTH", |
| 72 | "NON_V4", |
| 73 | "NON_INITIAL_FRAGMENT", |
| 74 | "DATAGRAM_INCOMPLETE", |
| 75 | "IP_OPTIONS_INCOMPLETE", |
| 76 | "UNHANDLED_PROTOCOL" |
| 77 | }; |
| 78 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 79 | static struct sfe_ipv4 __si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 80 | |
| 81 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 82 | * sfe_ipv4_gen_ip_csum() |
| 83 | * Generate the IP checksum for an IPv4 header. |
| 84 | * |
| 85 | * Note that this function assumes that we have only 20 bytes of IP header. |
| 86 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 87 | u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 88 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 89 | u32 sum; |
| 90 | u16 *i = (u16 *)iph; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 91 | |
| 92 | iph->check = 0; |
| 93 | |
| 94 | /* |
| 95 | * Generate the sum. |
| 96 | */ |
| 97 | sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9]; |
| 98 | |
| 99 | /* |
| 100 | * Fold it to ones-complement form. |
| 101 | */ |
| 102 | sum = (sum & 0xffff) + (sum >> 16); |
| 103 | sum = (sum & 0xffff) + (sum >> 16); |
| 104 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 105 | return (u16)sum ^ 0xffff; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * sfe_ipv4_get_connection_match_hash() |
| 110 | * Generate the hash used in connection match lookups. |
| 111 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 112 | static inline unsigned int sfe_ipv4_get_connection_match_hash(struct net_device *dev, u8 protocol, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 113 | __be32 src_ip, __be16 src_port, |
| 114 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 115 | { |
| 116 | size_t dev_addr = (size_t)dev; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 117 | u32 hash = ((u32)dev_addr) ^ ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 118 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 119 | } |
| 120 | |
| 121 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 122 | * sfe_ipv4_find_connection_match_rcu() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 123 | * Get the IPv4 flow match info that corresponds to a particular 5-tuple. |
| 124 | * |
| 125 | * On entry we must be holding the lock that protects the hash table. |
| 126 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 127 | struct sfe_ipv4_connection_match * |
| 128 | sfe_ipv4_find_connection_match_rcu(struct sfe_ipv4 *si, struct net_device *dev, u8 protocol, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 129 | __be32 src_ip, __be16 src_port, |
| 130 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 131 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 132 | struct sfe_ipv4_connection_match *cm = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 133 | unsigned int conn_match_idx; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 134 | struct hlist_head *lhead; |
| 135 | |
| 136 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 137 | |
| 138 | conn_match_idx = sfe_ipv4_get_connection_match_hash(dev, protocol, src_ip, src_port, dest_ip, dest_port); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 139 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 140 | lhead = &si->hlist_conn_match_hash_head[conn_match_idx]; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 141 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 142 | hlist_for_each_entry_rcu(cm, lhead, hnode) { |
| 143 | if (cm->match_src_port != src_port |
| 144 | || cm->match_dest_port != dest_port |
| 145 | || cm->match_src_ip != src_ip |
| 146 | || cm->match_dest_ip != dest_ip |
| 147 | || cm->match_protocol != protocol |
| 148 | || cm->match_dev != dev) { |
| 149 | continue; |
| 150 | } |
| 151 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 152 | this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 153 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 154 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 157 | return cm; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * sfe_ipv4_connection_match_update_summary_stats() |
| 162 | * Update the summary stats for a connection match entry. |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 163 | * |
| 164 | * Stats are incremented atomically. So use atomic substraction to update summary |
| 165 | * stats. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 166 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 167 | static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm, |
| 168 | u32 *packets, u32 *bytes) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 169 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 170 | u32 packet_count, byte_count; |
| 171 | |
| 172 | packet_count = atomic_read(&cm->rx_packet_count); |
| 173 | cm->rx_packet_count64 += packet_count; |
| 174 | atomic_sub(packet_count, &cm->rx_packet_count); |
| 175 | |
| 176 | byte_count = atomic_read(&cm->rx_byte_count); |
| 177 | cm->rx_byte_count64 += byte_count; |
| 178 | atomic_sub(byte_count, &cm->rx_byte_count); |
| 179 | |
| 180 | *packets = packet_count; |
| 181 | *bytes = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * sfe_ipv4_connection_match_compute_translations() |
| 186 | * Compute port and address translations for a connection match entry. |
| 187 | */ |
| 188 | static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm) |
| 189 | { |
| 190 | /* |
| 191 | * Before we insert the entry look to see if this is tagged as doing address |
| 192 | * translations. If it is then work out the adjustment that we need to apply |
| 193 | * to the transport checksum. |
| 194 | */ |
| 195 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
| 196 | /* |
| 197 | * Precompute an incremental checksum adjustment so we can |
| 198 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 199 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 200 | u16 src_ip_hi = cm->match_src_ip >> 16; |
| 201 | u16 src_ip_lo = cm->match_src_ip & 0xffff; |
| 202 | u32 xlate_src_ip = ~cm->xlate_src_ip; |
| 203 | u16 xlate_src_ip_hi = xlate_src_ip >> 16; |
| 204 | u16 xlate_src_ip_lo = xlate_src_ip & 0xffff; |
| 205 | u16 xlate_src_port = ~cm->xlate_src_port; |
| 206 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * When we compute this fold it down to a 16-bit offset |
| 210 | * as that way we can avoid having to do a double |
| 211 | * folding of the twos-complement result because the |
| 212 | * addition of 2 16-bit values cannot cause a double |
| 213 | * wrap-around! |
| 214 | */ |
| 215 | adj = src_ip_hi + src_ip_lo + cm->match_src_port |
| 216 | + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port; |
| 217 | adj = (adj & 0xffff) + (adj >> 16); |
| 218 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 219 | cm->xlate_src_csum_adjustment = (u16)adj; |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 220 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
| 224 | /* |
| 225 | * Precompute an incremental checksum adjustment so we can |
| 226 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 227 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 228 | u16 dest_ip_hi = cm->match_dest_ip >> 16; |
| 229 | u16 dest_ip_lo = cm->match_dest_ip & 0xffff; |
| 230 | u32 xlate_dest_ip = ~cm->xlate_dest_ip; |
| 231 | u16 xlate_dest_ip_hi = xlate_dest_ip >> 16; |
| 232 | u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff; |
| 233 | u16 xlate_dest_port = ~cm->xlate_dest_port; |
| 234 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * When we compute this fold it down to a 16-bit offset |
| 238 | * as that way we can avoid having to do a double |
| 239 | * folding of the twos-complement result because the |
| 240 | * addition of 2 16-bit values cannot cause a double |
| 241 | * wrap-around! |
| 242 | */ |
| 243 | adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port |
| 244 | + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port; |
| 245 | adj = (adj & 0xffff) + (adj >> 16); |
| 246 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 247 | cm->xlate_dest_csum_adjustment = (u16)adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 248 | } |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 249 | |
| 250 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 251 | u32 adj = ~cm->match_src_ip + cm->xlate_src_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 252 | if (adj < cm->xlate_src_ip) { |
| 253 | adj++; |
| 254 | } |
| 255 | |
| 256 | adj = (adj & 0xffff) + (adj >> 16); |
| 257 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 258 | cm->xlate_src_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 262 | u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 263 | if (adj < cm->xlate_dest_ip) { |
| 264 | adj++; |
| 265 | } |
| 266 | |
| 267 | adj = (adj & 0xffff) + (adj >> 16); |
| 268 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 269 | cm->xlate_dest_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | * sfe_ipv4_update_summary_stats() |
| 276 | * Update the summary stats. |
| 277 | */ |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 278 | static void sfe_ipv4_update_summary_stats(struct sfe_ipv4 *si, struct sfe_ipv4_stats *stats) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 279 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 280 | int i = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 281 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 282 | memset(stats, 0, sizeof(*stats)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 283 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 284 | for_each_possible_cpu(i) { |
| 285 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 286 | |
| 287 | stats->connection_create_requests64 += s->connection_create_requests64; |
| 288 | stats->connection_create_collisions64 += s->connection_create_collisions64; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 289 | stats->connection_create_failures64 += s->connection_create_failures64; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 290 | stats->connection_destroy_requests64 += s->connection_destroy_requests64; |
| 291 | stats->connection_destroy_misses64 += s->connection_destroy_misses64; |
| 292 | stats->connection_match_hash_hits64 += s->connection_match_hash_hits64; |
| 293 | stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64; |
| 294 | stats->connection_flushes64 += s->connection_flushes64; |
| 295 | stats->packets_forwarded64 += s->packets_forwarded64; |
| 296 | stats->packets_not_forwarded64 += s->packets_not_forwarded64; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 297 | } |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 298 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 302 | * sfe_ipv4_insert_connection_match() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 303 | * Insert a connection match into the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 304 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 305 | static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 306 | struct sfe_ipv4_connection_match *cm) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 307 | { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 308 | unsigned int conn_match_idx |
| 309 | = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol, |
| 310 | cm->match_src_ip, cm->match_src_port, |
| 311 | cm->match_dest_ip, cm->match_dest_port); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 312 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 313 | lockdep_assert_held(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 314 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 315 | hlist_add_head_rcu(&cm->hnode, &si->hlist_conn_match_hash_head[conn_match_idx]); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 316 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 317 | if (!si->flow_cookie_enable) |
| 318 | return; |
| 319 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 320 | /* |
| 321 | * Configure hardware to put a flow cookie in packet of this flow, |
| 322 | * then we can accelerate the lookup process when we received this packet. |
| 323 | */ |
| 324 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 325 | struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
| 326 | |
| 327 | if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) { |
| 328 | flow_cookie_set_func_t func; |
| 329 | |
| 330 | rcu_read_lock(); |
| 331 | func = rcu_dereference(si->flow_cookie_set_func); |
| 332 | if (func) { |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 333 | if (!func(cm->match_protocol, cm->match_src_ip, cm->match_src_port, |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 334 | cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) { |
| 335 | entry->match = cm; |
| 336 | cm->flow_cookie = conn_match_idx; |
| 337 | } |
| 338 | } |
| 339 | rcu_read_unlock(); |
| 340 | |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 348 | * sfe_ipv4_remove_connection_match() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 349 | * Remove a connection match object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 350 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 351 | static inline void sfe_ipv4_remove_connection_match(struct sfe_ipv4 *si, struct sfe_ipv4_connection_match *cm) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 352 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 353 | |
| 354 | lockdep_assert_held(&si->lock); |
| 355 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 356 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 357 | if (si->flow_cookie_enable) { |
| 358 | /* |
| 359 | * Tell hardware that we no longer need a flow cookie in packet of this flow |
| 360 | */ |
| 361 | unsigned int conn_match_idx; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 362 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 363 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 364 | struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 365 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 366 | if (cm == entry->match) { |
| 367 | flow_cookie_set_func_t func; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 368 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 369 | rcu_read_lock(); |
| 370 | func = rcu_dereference(si->flow_cookie_set_func); |
| 371 | if (func) { |
| 372 | func(cm->match_protocol, cm->match_src_ip, cm->match_src_port, |
| 373 | cm->match_dest_ip, cm->match_dest_port, 0); |
| 374 | } |
| 375 | rcu_read_unlock(); |
| 376 | |
| 377 | cm->flow_cookie = 0; |
| 378 | entry->match = NULL; |
| 379 | entry->last_clean_time = jiffies; |
| 380 | break; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 381 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | #endif |
| 385 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 386 | hlist_del_init_rcu(&cm->hnode); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 387 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * sfe_ipv4_get_connection_hash() |
| 392 | * Generate the hash used in connection lookups. |
| 393 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 394 | static inline unsigned int sfe_ipv4_get_connection_hash(u8 protocol, __be32 src_ip, __be16 src_port, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 395 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 396 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 397 | u32 hash = ntohl(src_ip ^ dest_ip) ^ protocol ^ ntohs(src_port ^ dest_port); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 398 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 399 | } |
| 400 | |
| 401 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 402 | * sfe_ipv4_find_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 403 | * Get the IPv4 connection info that corresponds to a particular 5-tuple. |
| 404 | * |
| 405 | * On entry we must be holding the lock that protects the hash table. |
| 406 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 407 | static inline struct sfe_ipv4_connection *sfe_ipv4_find_connection(struct sfe_ipv4 *si, u32 protocol, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 408 | __be32 src_ip, __be16 src_port, |
| 409 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 410 | { |
| 411 | struct sfe_ipv4_connection *c; |
| 412 | unsigned int conn_idx = sfe_ipv4_get_connection_hash(protocol, src_ip, src_port, dest_ip, dest_port); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 413 | |
| 414 | lockdep_assert_held(&si->lock); |
| 415 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 416 | c = si->conn_hash[conn_idx]; |
| 417 | |
| 418 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 419 | * Will need connection entry for next create/destroy metadata, |
| 420 | * So no need to re-order entry for these requests |
| 421 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 422 | while (c) { |
| 423 | if ((c->src_port == src_port) |
| 424 | && (c->dest_port == dest_port) |
| 425 | && (c->src_ip == src_ip) |
| 426 | && (c->dest_ip == dest_ip) |
| 427 | && (c->protocol == protocol)) { |
| 428 | return c; |
| 429 | } |
| 430 | |
| 431 | c = c->next; |
| 432 | } |
| 433 | |
| 434 | return NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 438 | * sfe_ipv4_mark_rule() |
| 439 | * Updates the mark for a current offloaded connection |
| 440 | * |
| 441 | * Will take hash lock upon entry |
| 442 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 443 | void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark) |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 444 | { |
| 445 | struct sfe_ipv4 *si = &__si; |
| 446 | struct sfe_ipv4_connection *c; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 447 | |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 448 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 449 | c = sfe_ipv4_find_connection(si, mark->protocol, |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 450 | mark->src_ip.ip, mark->src_port, |
| 451 | mark->dest_ip.ip, mark->dest_port); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 452 | if (c) { |
Nicolas Costa | f53d6fe | 2014-01-13 16:03:46 -0600 | [diff] [blame] | 453 | WARN_ON((0 != c->mark) && (0 == mark->mark)); |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 454 | c->mark = mark->mark; |
| 455 | } |
Xiaoping Fan | 3c423e3 | 2015-07-03 03:09:29 -0700 | [diff] [blame] | 456 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 457 | |
| 458 | if (c) { |
| 459 | DEBUG_TRACE("Matching connection found for mark, " |
| 460 | "setting from %08x to %08x\n", |
| 461 | c->mark, mark->mark); |
| 462 | } |
Matthew McClintock | be7b47d | 2013-11-27 13:26:23 -0600 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 466 | * sfe_ipv4_insert_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 467 | * Insert a connection into the hash. |
| 468 | * |
| 469 | * On entry we must be holding the lock that protects the hash table. |
| 470 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 471 | static void sfe_ipv4_insert_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 472 | { |
| 473 | struct sfe_ipv4_connection **hash_head; |
| 474 | struct sfe_ipv4_connection *prev_head; |
| 475 | unsigned int conn_idx; |
| 476 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 477 | lockdep_assert_held(&si->lock); |
| 478 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 479 | /* |
| 480 | * Insert entry into the connection hash. |
| 481 | */ |
| 482 | conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 483 | c->dest_ip, c->dest_port); |
| 484 | hash_head = &si->conn_hash[conn_idx]; |
| 485 | prev_head = *hash_head; |
| 486 | c->prev = NULL; |
| 487 | if (prev_head) { |
| 488 | prev_head->prev = c; |
| 489 | } |
| 490 | |
| 491 | c->next = prev_head; |
| 492 | *hash_head = c; |
| 493 | |
| 494 | /* |
| 495 | * Insert entry into the "all connections" list. |
| 496 | */ |
| 497 | if (si->all_connections_tail) { |
| 498 | c->all_connections_prev = si->all_connections_tail; |
| 499 | si->all_connections_tail->all_connections_next = c; |
| 500 | } else { |
| 501 | c->all_connections_prev = NULL; |
| 502 | si->all_connections_head = c; |
| 503 | } |
| 504 | |
| 505 | si->all_connections_tail = c; |
| 506 | c->all_connections_next = NULL; |
| 507 | si->num_connections++; |
| 508 | |
| 509 | /* |
| 510 | * Insert the connection match objects too. |
| 511 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 512 | sfe_ipv4_insert_connection_match(si, c->original_match); |
| 513 | sfe_ipv4_insert_connection_match(si, c->reply_match); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 517 | * sfe_ipv4_remove_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 518 | * Remove a sfe_ipv4_connection object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 519 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 520 | bool sfe_ipv4_remove_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 521 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 522 | lockdep_assert_held(&si->lock); |
| 523 | |
| 524 | if (c->removed) { |
| 525 | DEBUG_ERROR("%px: Connection has been removed already\n", c); |
| 526 | return false; |
| 527 | } |
| 528 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 529 | /* |
| 530 | * Remove the connection match objects. |
| 531 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 532 | sfe_ipv4_remove_connection_match(si, c->reply_match); |
| 533 | sfe_ipv4_remove_connection_match(si, c->original_match); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 534 | |
| 535 | /* |
| 536 | * Unlink the connection. |
| 537 | */ |
| 538 | if (c->prev) { |
| 539 | c->prev->next = c->next; |
| 540 | } else { |
| 541 | unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 542 | c->dest_ip, c->dest_port); |
| 543 | si->conn_hash[conn_idx] = c->next; |
| 544 | } |
| 545 | |
| 546 | if (c->next) { |
| 547 | c->next->prev = c->prev; |
| 548 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 549 | |
| 550 | /* |
| 551 | * Unlink connection from all_connections list |
| 552 | */ |
| 553 | if (c->all_connections_prev) { |
| 554 | c->all_connections_prev->all_connections_next = c->all_connections_next; |
| 555 | } else { |
| 556 | si->all_connections_head = c->all_connections_next; |
| 557 | } |
| 558 | |
| 559 | if (c->all_connections_next) { |
| 560 | c->all_connections_next->all_connections_prev = c->all_connections_prev; |
| 561 | } else { |
| 562 | si->all_connections_tail = c->all_connections_prev; |
| 563 | } |
| 564 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 565 | /* |
| 566 | * If I am the next sync connection, move the sync to my next or head. |
| 567 | */ |
| 568 | if (unlikely(si->wc_next == c)) { |
| 569 | si->wc_next = c->all_connections_next; |
| 570 | } |
| 571 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 572 | c->removed = true; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 573 | si->num_connections--; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 574 | return true; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 578 | * sfe_ipv4_gen_sync_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 579 | * Sync a connection. |
| 580 | * |
| 581 | * On entry to this function we expect that the lock for the connection is either |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 582 | * already held (while called from sfe_ipv4_periodic_sync() or isn't required |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 583 | * (while called from sfe_ipv4_flush_connection()) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 584 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 585 | static void sfe_ipv4_gen_sync_connection(struct sfe_ipv4 *si, struct sfe_ipv4_connection *c, |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 586 | struct sfe_connection_sync *sis, sfe_sync_reason_t reason, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 587 | u64 now_jiffies) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 588 | { |
| 589 | struct sfe_ipv4_connection_match *original_cm; |
| 590 | struct sfe_ipv4_connection_match *reply_cm; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 591 | u32 packet_count, byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Fill in the update message. |
| 595 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 596 | sis->is_v6 = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 597 | sis->protocol = c->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 598 | sis->src_ip.ip = c->src_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 599 | sis->src_ip_xlate.ip = c->src_ip_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 600 | sis->dest_ip.ip = c->dest_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 601 | sis->dest_ip_xlate.ip = c->dest_ip_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 602 | sis->src_port = c->src_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 603 | sis->src_port_xlate = c->src_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 604 | sis->dest_port = c->dest_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 605 | sis->dest_port_xlate = c->dest_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 606 | |
| 607 | original_cm = c->original_match; |
| 608 | reply_cm = c->reply_match; |
| 609 | sis->src_td_max_window = original_cm->protocol_state.tcp.max_win; |
| 610 | sis->src_td_end = original_cm->protocol_state.tcp.end; |
| 611 | sis->src_td_max_end = original_cm->protocol_state.tcp.max_end; |
| 612 | sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win; |
| 613 | sis->dest_td_end = reply_cm->protocol_state.tcp.end; |
| 614 | sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end; |
| 615 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 616 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count); |
| 617 | sis->src_new_packet_count = packet_count; |
| 618 | sis->src_new_byte_count = byte_count; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 619 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 620 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count); |
| 621 | sis->dest_new_packet_count = packet_count; |
| 622 | sis->dest_new_byte_count = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 623 | |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 624 | sis->src_dev = original_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 625 | sis->src_packet_count = original_cm->rx_packet_count64; |
| 626 | sis->src_byte_count = original_cm->rx_byte_count64; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 627 | |
| 628 | sis->dest_dev = reply_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 629 | sis->dest_packet_count = reply_cm->rx_packet_count64; |
| 630 | sis->dest_byte_count = reply_cm->rx_byte_count64; |
| 631 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 632 | sis->reason = reason; |
| 633 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 634 | /* |
| 635 | * Get the time increment since our last sync. |
| 636 | */ |
| 637 | sis->delta_jiffies = now_jiffies - c->last_sync_jiffies; |
| 638 | c->last_sync_jiffies = now_jiffies; |
| 639 | } |
| 640 | |
| 641 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 642 | * sfe_ipv4_free_connection_rcu() |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 643 | * Called at RCU qs state to free the connection object. |
| 644 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 645 | static void sfe_ipv4_free_connection_rcu(struct rcu_head *head) |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 646 | { |
| 647 | struct sfe_ipv4_connection *c; |
| 648 | |
| 649 | /* |
| 650 | * We dont need spin lock as the connection is already removed from link list |
| 651 | */ |
| 652 | c = container_of(head, struct sfe_ipv4_connection, rcu); |
| 653 | |
| 654 | BUG_ON(!c->removed); |
| 655 | |
| 656 | DEBUG_TRACE("%px: connecton has been deleted\n", c); |
| 657 | |
| 658 | /* |
| 659 | * Release our hold of the source and dest devices and free the memory |
| 660 | * for our connection objects. |
| 661 | */ |
| 662 | dev_put(c->original_dev); |
| 663 | dev_put(c->reply_dev); |
| 664 | kfree(c->original_match); |
| 665 | kfree(c->reply_match); |
| 666 | kfree(c); |
| 667 | } |
| 668 | |
| 669 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 670 | * sfe_ipv4_flush_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 671 | * Flush a connection and free all associated resources. |
| 672 | * |
| 673 | * We need to be called with bottom halves disabled locally as we need to acquire |
| 674 | * the connection hash lock and release it again. In general we're actually called |
| 675 | * from within a BH and so we're fine, but we're also called when connections are |
| 676 | * torn down. |
| 677 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 678 | void sfe_ipv4_flush_connection(struct sfe_ipv4 *si, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 679 | struct sfe_ipv4_connection *c, |
| 680 | sfe_sync_reason_t reason) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 681 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 682 | u64 now_jiffies; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 683 | sfe_sync_rule_callback_t sync_rule_callback; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 684 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 685 | BUG_ON(!c->removed); |
| 686 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 687 | this_cpu_inc(si->stats_pcpu->connection_flushes64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 688 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 689 | rcu_read_lock(); |
| 690 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 691 | |
| 692 | /* |
| 693 | * Generate a sync message and then sync. |
| 694 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 695 | if (sync_rule_callback) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 696 | struct sfe_connection_sync sis; |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 697 | now_jiffies = get_jiffies_64(); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 698 | sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 699 | sync_rule_callback(&sis); |
| 700 | } |
| 701 | |
| 702 | rcu_read_unlock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 703 | |
| 704 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 705 | * Release our hold of the source and dest devices and free the memory |
| 706 | * for our connection objects. |
| 707 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 708 | call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 712 | * sfe_ipv4_exception_stats_inc() |
| 713 | * Increment exception stats. |
| 714 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 715 | void sfe_ipv4_exception_stats_inc(struct sfe_ipv4 *si, enum sfe_ipv4_exception_events reason) |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 716 | { |
| 717 | struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu); |
| 718 | stats->exception_events64[reason]++; |
| 719 | stats->packets_not_forwarded64++; |
| 720 | } |
| 721 | |
| 722 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 723 | * sfe_ipv4_recv() |
Matthew McClintock | a8ad796 | 2014-01-16 16:49:30 -0600 | [diff] [blame] | 724 | * Handle packet receives and forwaring. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 725 | * |
| 726 | * Returns 1 if the packet is forwarded or 0 if it isn't. |
| 727 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 728 | int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 729 | { |
| 730 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 731 | unsigned int len; |
| 732 | unsigned int tot_len; |
| 733 | unsigned int frag_off; |
| 734 | unsigned int ihl; |
| 735 | bool flush_on_find; |
| 736 | bool ip_options; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 737 | struct iphdr *iph; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 738 | u32 protocol; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 739 | |
| 740 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 741 | * Check that we have space for an IP header here. |
| 742 | */ |
| 743 | len = skb->len; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 744 | if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 745 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 746 | DEBUG_TRACE("len: %u is too short\n", len); |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame^] | 751 | * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed |
| 752 | * that the L3 checksum is validated by the Rx interface or the tunnel interface that has |
| 753 | * generated the packet. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 754 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 755 | iph = (struct iphdr *)skb->data; |
Ratheesh Kannoth | 43d64f8 | 2021-10-20 08:23:29 +0530 | [diff] [blame] | 756 | if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) { |
| 757 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD); |
| 758 | |
| 759 | DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check); |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Check that our "total length" is large enough for an IP header. |
| 765 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 766 | tot_len = ntohs(iph->tot_len); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 767 | if (unlikely(tot_len < sizeof(struct iphdr))) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 768 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 769 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_BAD_TOTAL_LENGTH); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 770 | DEBUG_TRACE("tot_len: %u is too short\n", tot_len); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Is our IP version wrong? |
| 776 | */ |
| 777 | if (unlikely(iph->version != 4)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 778 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 779 | DEBUG_TRACE("IP version: %u\n", iph->version); |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Does our datagram fit inside the skb? |
| 785 | */ |
| 786 | if (unlikely(tot_len > len)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 787 | |
| 788 | DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 789 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Do we have a non-initial fragment? |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 795 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 796 | frag_off = ntohs(iph->frag_off); |
| 797 | if (unlikely(frag_off & IP_OFFSET)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 798 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 799 | DEBUG_TRACE("non-initial fragment\n"); |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * If we have a (first) fragment then mark it to cause any connection to flush. |
| 805 | */ |
| 806 | flush_on_find = unlikely(frag_off & IP_MF) ? true : false; |
| 807 | |
| 808 | /* |
| 809 | * Do we have any IP options? That's definite a slow path! If we do have IP |
| 810 | * options we need to recheck our header size. |
| 811 | */ |
| 812 | ihl = iph->ihl << 2; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 813 | ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 814 | if (unlikely(ip_options)) { |
| 815 | if (unlikely(len < ihl)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 816 | |
| 817 | DEBUG_TRACE("len: %u is too short for header of size: %u\n", len, ihl); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 818 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | flush_on_find = true; |
| 823 | } |
| 824 | |
| 825 | protocol = iph->protocol; |
| 826 | if (IPPROTO_UDP == protocol) { |
| 827 | return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, flush_on_find); |
| 828 | } |
| 829 | |
| 830 | if (IPPROTO_TCP == protocol) { |
| 831 | return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, flush_on_find); |
| 832 | } |
| 833 | |
| 834 | if (IPPROTO_ICMP == protocol) { |
| 835 | return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl); |
| 836 | } |
| 837 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 838 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 839 | |
| 840 | DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol); |
| 841 | return 0; |
| 842 | } |
| 843 | |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 844 | static void |
| 845 | sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 846 | struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 847 | { |
| 848 | struct sfe_ipv4_connection_match *orig_cm; |
| 849 | struct sfe_ipv4_connection_match *repl_cm; |
| 850 | struct sfe_ipv4_tcp_connection_match *orig_tcp; |
| 851 | struct sfe_ipv4_tcp_connection_match *repl_tcp; |
| 852 | |
| 853 | orig_cm = c->original_match; |
| 854 | repl_cm = c->reply_match; |
| 855 | orig_tcp = &orig_cm->protocol_state.tcp; |
| 856 | repl_tcp = &repl_cm->protocol_state.tcp; |
| 857 | |
| 858 | /* update orig */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 859 | if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) { |
| 860 | orig_tcp->max_win = msg->tcp_rule.flow_max_window; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 861 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 862 | if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) { |
| 863 | orig_tcp->end = msg->tcp_rule.flow_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 864 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 865 | if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) { |
| 866 | orig_tcp->max_end = msg->tcp_rule.flow_max_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* update reply */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 870 | if (repl_tcp->max_win < msg->tcp_rule.return_max_window) { |
| 871 | repl_tcp->max_win = msg->tcp_rule.return_max_window; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 872 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 873 | if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) { |
| 874 | repl_tcp->end = msg->tcp_rule.return_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 875 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 876 | if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) { |
| 877 | repl_tcp->max_end = msg->tcp_rule.return_max_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* update match flags */ |
| 881 | orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 882 | repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 883 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
| 884 | |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 885 | orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 886 | repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | static void |
| 891 | sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 892 | struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 893 | { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 894 | switch (msg->tuple.protocol) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 895 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 896 | sfe_ipv4_update_tcp_state(c, msg); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 897 | break; |
| 898 | } |
| 899 | } |
| 900 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 901 | void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 902 | { |
| 903 | struct sfe_ipv4_connection *c; |
| 904 | struct sfe_ipv4 *si = &__si; |
| 905 | |
| 906 | spin_lock_bh(&si->lock); |
| 907 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 908 | c = sfe_ipv4_find_connection(si, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 909 | msg->tuple.protocol, |
| 910 | msg->tuple.flow_ip, |
| 911 | msg->tuple.flow_ident, |
| 912 | msg->tuple.return_ip, |
| 913 | msg->tuple.return_ident); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 914 | if (c != NULL) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 915 | sfe_ipv4_update_protocol_state(c, msg); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | spin_unlock_bh(&si->lock); |
| 919 | } |
| 920 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 921 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 922 | * sfe_ipv4_create_rule() |
| 923 | * Create a forwarding rule. |
| 924 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 925 | int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 926 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 927 | struct sfe_ipv4 *si = &__si; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 928 | struct sfe_ipv4_connection *c, *c_old; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 929 | struct sfe_ipv4_connection_match *original_cm; |
| 930 | struct sfe_ipv4_connection_match *reply_cm; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 931 | struct net_device *dest_dev; |
| 932 | struct net_device *src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 933 | struct sfe_ipv4_5tuple *tuple = &msg->tuple; |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 934 | s32 flow_interface_num = msg->conn_rule.flow_top_interface_num; |
| 935 | s32 return_interface_num = msg->conn_rule.return_top_interface_num; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 936 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 937 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) { |
| 938 | flow_interface_num = msg->conn_rule.flow_interface_num; |
| 939 | } |
| 940 | |
| 941 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) { |
| 942 | return_interface_num = msg->conn_rule.return_interface_num; |
| 943 | } |
| 944 | |
| 945 | src_dev = dev_get_by_index(&init_net, flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 946 | if (!src_dev) { |
| 947 | DEBUG_WARN("%px: Unable to find src_dev corresponding to %d\n", msg, |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 948 | flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 949 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 953 | dest_dev = dev_get_by_index(&init_net, return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 954 | if (!dest_dev) { |
| 955 | DEBUG_WARN("%px: Unable to find dest_dev corresponding to %d\n", msg, |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 956 | return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 957 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 958 | dev_put(src_dev); |
| 959 | return -EINVAL; |
| 960 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 961 | |
Matthew McClintock | 389b42a | 2014-09-24 14:05:51 -0500 | [diff] [blame] | 962 | if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) || |
| 963 | (src_dev->reg_state != NETREG_REGISTERED))) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 964 | dev_put(src_dev); |
| 965 | dev_put(dest_dev); |
| 966 | DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg, |
| 967 | src_dev->name, dest_dev->name); |
| 968 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Matthew McClintock | 389b42a | 2014-09-24 14:05:51 -0500 | [diff] [blame] | 969 | return -EINVAL; |
| 970 | } |
| 971 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 972 | /* |
| 973 | * Allocate the various connection tracking objects. |
| 974 | */ |
| 975 | c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC); |
| 976 | if (unlikely(!c)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 977 | DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg); |
| 978 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 979 | dev_put(src_dev); |
| 980 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 981 | return -ENOMEM; |
| 982 | } |
| 983 | |
| 984 | original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 985 | if (unlikely(!original_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 986 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
| 987 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 988 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 989 | dev_put(src_dev); |
| 990 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 991 | return -ENOMEM; |
| 992 | } |
| 993 | |
| 994 | reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 995 | if (unlikely(!reply_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 996 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
| 997 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 998 | kfree(original_cm); |
| 999 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1000 | dev_put(src_dev); |
| 1001 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1002 | return -ENOMEM; |
| 1003 | } |
| 1004 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1005 | this_cpu_inc(si->stats_pcpu->connection_create_requests64); |
| 1006 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1007 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1008 | |
| 1009 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1010 | * Check to see if there is already a flow that matches the rule we're |
| 1011 | * trying to create. If there is then we can't create a new one. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1012 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1013 | c_old = sfe_ipv4_find_connection(si, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1014 | msg->tuple.protocol, |
| 1015 | msg->tuple.flow_ip, |
| 1016 | msg->tuple.flow_ident, |
| 1017 | msg->tuple.return_ip, |
| 1018 | msg->tuple.return_ident); |
| 1019 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1020 | if (c_old != NULL) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1021 | this_cpu_inc(si->stats_pcpu->connection_create_collisions64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1022 | |
| 1023 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1024 | * If we already have the flow then it's likely that this |
| 1025 | * request to create the connection rule contains more |
| 1026 | * up-to-date information. Check and update accordingly. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1027 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1028 | sfe_ipv4_update_protocol_state(c, msg); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1029 | spin_unlock_bh(&si->lock); |
| 1030 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1031 | kfree(reply_cm); |
| 1032 | kfree(original_cm); |
| 1033 | kfree(c); |
| 1034 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1035 | dev_put(src_dev); |
| 1036 | dev_put(dest_dev); |
| 1037 | |
| 1038 | DEBUG_TRACE("connection already exists - p:%d\n" |
| 1039 | " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n", |
| 1040 | tuple->protocol, |
| 1041 | src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1042 | dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident)); |
| 1043 | |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 1044 | return -EADDRINUSE; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1048 | * Fill in the "original" direction connection matching object. |
| 1049 | * Note that the transmit MAC address is "dest_mac_xlate" because |
| 1050 | * we always know both ends of a connection by their translated |
| 1051 | * addresses and not their public addresses. |
| 1052 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1053 | original_cm->match_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1054 | original_cm->match_protocol = tuple->protocol; |
| 1055 | original_cm->match_src_ip = tuple->flow_ip; |
| 1056 | original_cm->match_src_port = tuple->flow_ident; |
| 1057 | original_cm->match_dest_ip = tuple->return_ip; |
| 1058 | original_cm->match_dest_port = tuple->return_ident; |
| 1059 | |
| 1060 | original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate; |
| 1061 | original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate; |
| 1062 | original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate; |
| 1063 | original_cm->xlate_dest_port =msg->conn_rule.return_ident_xlate; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1064 | atomic_set(&original_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1065 | original_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1066 | atomic_set(&original_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1067 | original_cm->rx_byte_count64 = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1068 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1069 | original_cm->xmit_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1070 | original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu; |
| 1071 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1072 | original_cm->connection = c; |
| 1073 | original_cm->counter_match = reply_cm; |
| 1074 | original_cm->flags = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1075 | |
| 1076 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1077 | original_cm->priority = msg->qos_rule.flow_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1078 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1079 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1080 | |
| 1081 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1082 | original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1083 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1084 | } |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame^] | 1085 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1086 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1087 | original_cm->flow_cookie = 0; |
| 1088 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1089 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1090 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1091 | original_cm->flow_accel = msg->direction_rule.flow_accel; |
| 1092 | } else { |
| 1093 | original_cm->flow_accel = 1; |
| 1094 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1095 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame^] | 1096 | /* |
| 1097 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1098 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1099 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1100 | * are sending directly to the destination interface that supports it. |
| 1101 | */ |
| 1102 | if (likely(dest_dev->features & NETIF_F_HW_CSUM)) { |
| 1103 | if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) || |
| 1104 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) { |
| 1105 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
| 1106 | } |
| 1107 | } |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1108 | |
| 1109 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1110 | * For the non-arp interface, we don't write L2 HDR. |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1111 | */ |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1112 | if (!(dest_dev->flags & IFF_NOARP)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1113 | |
| 1114 | /* |
| 1115 | * Check whether the rule has configured a specific source MAC address to use. |
| 1116 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1117 | */ |
| 1118 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1119 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) { |
| 1120 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac); |
| 1121 | } else { |
| 1122 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr); |
| 1123 | } |
| 1124 | |
| 1125 | ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac); |
| 1126 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1127 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 1128 | |
| 1129 | /* |
| 1130 | * If our dev writes Ethernet headers then we can write a really fast |
| 1131 | * version. |
| 1132 | */ |
| 1133 | if (dest_dev->header_ops) { |
| 1134 | if (dest_dev->header_ops->create == eth_header) { |
| 1135 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1136 | } |
| 1137 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Fill in the "reply" direction connection matching object. |
| 1142 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1143 | reply_cm->match_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1144 | reply_cm->match_protocol = tuple->protocol; |
| 1145 | reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate; |
| 1146 | reply_cm->match_src_port = msg->conn_rule.return_ident_xlate; |
| 1147 | reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate; |
| 1148 | reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate; |
| 1149 | |
| 1150 | reply_cm->xlate_src_ip = tuple->return_ip; |
| 1151 | reply_cm->xlate_src_port = tuple->return_ident; |
| 1152 | reply_cm->xlate_dest_ip = tuple->flow_ip; |
| 1153 | reply_cm->xlate_dest_port = tuple->flow_ident;; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1154 | |
| 1155 | atomic_set(&reply_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1156 | reply_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1157 | atomic_set(&reply_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1158 | reply_cm->rx_byte_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1159 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1160 | reply_cm->xmit_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1161 | reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1162 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1163 | reply_cm->connection = c; |
| 1164 | reply_cm->counter_match = original_cm; |
| 1165 | reply_cm->flags = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1166 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1167 | reply_cm->priority = msg->qos_rule.return_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1168 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1169 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1170 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1171 | reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1172 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1173 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1174 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1175 | reply_cm->flow_cookie = 0; |
| 1176 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1177 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1178 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1179 | reply_cm->flow_accel = msg->direction_rule.return_accel; |
| 1180 | } else { |
| 1181 | reply_cm->flow_accel = 1; |
| 1182 | } |
| 1183 | |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1184 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame^] | 1185 | /* |
| 1186 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1187 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1188 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1189 | * are sending directly to the destination interface that supports it. |
| 1190 | */ |
| 1191 | if (likely(src_dev->features & NETIF_F_HW_CSUM)) { |
| 1192 | if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) || |
| 1193 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) { |
| 1194 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
| 1195 | } |
| 1196 | } |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1197 | |
| 1198 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1199 | * For the non-arp interface, we don't write L2 HDR. |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1200 | */ |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1201 | if (!(src_dev->flags & IFF_NOARP)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1202 | |
| 1203 | /* |
| 1204 | * Check whether the rule has configured a specific source MAC address to use. |
| 1205 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1206 | */ |
| 1207 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1208 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) { |
| 1209 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac); |
| 1210 | } else { |
| 1211 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr); |
| 1212 | } |
| 1213 | ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac); |
| 1214 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1215 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
| 1216 | |
| 1217 | /* |
| 1218 | * If our dev writes Ethernet headers then we can write a really fast |
| 1219 | * version. |
| 1220 | */ |
| 1221 | if (src_dev->header_ops) { |
| 1222 | if (src_dev->header_ops->create == eth_header) { |
| 1223 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1224 | } |
| 1225 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1228 | if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) || |
| 1229 | (tuple->return_ident != msg->conn_rule.return_ident_xlate)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1230 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 1231 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 1232 | } |
| 1233 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1234 | if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) || |
| 1235 | (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1236 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 1237 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 1238 | } |
| 1239 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1240 | c->protocol = tuple->protocol; |
| 1241 | c->src_ip = tuple->flow_ip; |
| 1242 | c->src_ip_xlate = msg->conn_rule.flow_ip_xlate; |
| 1243 | c->src_port = tuple->flow_ident; |
| 1244 | c->src_port_xlate = msg->conn_rule.flow_ident_xlate; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1245 | c->original_dev = src_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1246 | c->original_match = original_cm; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1247 | c->dest_ip = tuple->return_ip; |
| 1248 | c->dest_ip_xlate = msg->conn_rule.return_ip_xlate; |
| 1249 | c->dest_port = tuple->return_ident; |
| 1250 | c->dest_port_xlate = msg->conn_rule.return_ident_xlate; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1251 | c->reply_dev = dest_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1252 | c->reply_match = reply_cm; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1253 | c->mark = 0; /* TODO : no mark setting for create rule */ |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1254 | c->debug_read_seq = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1255 | c->last_sync_jiffies = get_jiffies_64(); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1256 | c->removed = false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1259 | * Initialize the protocol-specific information that we track. |
| 1260 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1261 | switch (tuple->protocol) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1262 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1263 | original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale; |
| 1264 | original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1; |
| 1265 | original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end; |
| 1266 | original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end; |
| 1267 | |
| 1268 | reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale; |
| 1269 | reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1; |
| 1270 | reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end; |
| 1271 | reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end; |
| 1272 | |
| 1273 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1274 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1275 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1276 | } |
| 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | sfe_ipv4_connection_match_compute_translations(original_cm); |
| 1281 | sfe_ipv4_connection_match_compute_translations(reply_cm); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1282 | sfe_ipv4_insert_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1283 | |
| 1284 | spin_unlock_bh(&si->lock); |
| 1285 | |
| 1286 | /* |
| 1287 | * We have everything we need! |
| 1288 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1289 | DEBUG_INFO("new connection - p: %d\n" |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 1290 | " s: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n" |
| 1291 | " d: %s:%pxM(%pxM):%pI4(%pI4):%u(%u)\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1292 | tuple->protocol, |
| 1293 | src_dev->name, msg->conn_rule.flow_mac, NULL, |
| 1294 | &tuple->flow_ip, &msg->conn_rule.flow_ip_xlate, ntohs(tuple->flow_ident), ntohs(msg->conn_rule.flow_ident_xlate), |
| 1295 | dest_dev->name, NULL, msg->conn_rule.return_mac, |
| 1296 | &tuple->return_ip, &msg->conn_rule.return_ip_xlate, ntohs(tuple->return_ident), ntohs(msg->conn_rule.return_ident_xlate)); |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 1297 | |
| 1298 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1302 | * sfe_ipv4_destroy_rule() |
| 1303 | * Destroy a forwarding rule. |
| 1304 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1305 | void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1306 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1307 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1308 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1309 | bool ret; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1310 | struct sfe_ipv4_5tuple *tuple = &msg->tuple; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1311 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1312 | this_cpu_inc(si->stats_pcpu->connection_destroy_requests64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1313 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1314 | |
| 1315 | /* |
| 1316 | * Check to see if we have a flow that matches the rule we're trying |
| 1317 | * to destroy. If there isn't then we can't destroy it. |
| 1318 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1319 | c = sfe_ipv4_find_connection(si, tuple->protocol, tuple->flow_ip, tuple->flow_ident, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1320 | tuple->return_ip, tuple->return_ident); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1321 | if (!c) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1322 | spin_unlock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1323 | this_cpu_inc(si->stats_pcpu->connection_destroy_misses64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1324 | |
| 1325 | DEBUG_TRACE("connection does not exist - p: %d, s: %pI4:%u, d: %pI4:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1326 | tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1327 | &tuple->return_ip, ntohs(tuple->return_ident)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Remove our connection details from the hash tables. |
| 1333 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1334 | ret = sfe_ipv4_remove_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1335 | spin_unlock_bh(&si->lock); |
| 1336 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1337 | if (ret) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1338 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1339 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1340 | |
| 1341 | DEBUG_INFO("connection destroyed - p: %d, s: %pI4:%u, d: %pI4:%u\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1342 | tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1343 | &tuple->return_ip, ntohs(tuple->return_ident)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1347 | * sfe_ipv4_register_sync_rule_callback() |
| 1348 | * Register a callback for rule synchronization. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1349 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1350 | void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t sync_rule_callback) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1351 | { |
| 1352 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1353 | |
| 1354 | spin_lock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1355 | rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1356 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * sfe_ipv4_get_debug_dev() |
| 1361 | */ |
| 1362 | static ssize_t sfe_ipv4_get_debug_dev(struct device *dev, |
| 1363 | struct device_attribute *attr, |
| 1364 | char *buf) |
| 1365 | { |
| 1366 | struct sfe_ipv4 *si = &__si; |
| 1367 | ssize_t count; |
| 1368 | int num; |
| 1369 | |
| 1370 | spin_lock_bh(&si->lock); |
| 1371 | num = si->debug_dev; |
| 1372 | spin_unlock_bh(&si->lock); |
| 1373 | |
| 1374 | count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num); |
| 1375 | return count; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1379 | * sysfs attributes. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1380 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1381 | static const struct device_attribute sfe_ipv4_debug_dev_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 1382 | __ATTR(debug_dev, S_IWUSR | S_IRUGO, sfe_ipv4_get_debug_dev, NULL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1383 | |
| 1384 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1385 | * sfe_ipv4_destroy_all_rules_for_dev() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1386 | * Destroy all connections that match a particular device. |
| 1387 | * |
| 1388 | * If we pass dev as NULL then this destroys all connections. |
| 1389 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1390 | void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1391 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1392 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1393 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1394 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1395 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1396 | another_round: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1397 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1398 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1399 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1400 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1401 | * Does this connection relate to the device we are destroying? |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1402 | */ |
| 1403 | if (!dev |
| 1404 | || (dev == c->original_dev) |
| 1405 | || (dev == c->reply_dev)) { |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1406 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1407 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1408 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1409 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1410 | if (c) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1411 | ret = sfe_ipv4_remove_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1415 | |
| 1416 | if (c) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1417 | if (ret) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1418 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1419 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1420 | goto another_round; |
| 1421 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1425 | * sfe_ipv4_periodic_sync() |
| 1426 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1427 | static void sfe_ipv4_periodic_sync(struct work_struct *work) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1428 | { |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1429 | struct sfe_ipv4 *si = container_of((struct delayed_work *)work, struct sfe_ipv4, sync_dwork); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1430 | u64 now_jiffies; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1431 | int quota; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1432 | sfe_sync_rule_callback_t sync_rule_callback; |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1433 | struct sfe_ipv4_connection *c; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1434 | |
| 1435 | now_jiffies = get_jiffies_64(); |
| 1436 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1437 | rcu_read_lock(); |
| 1438 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 1439 | if (!sync_rule_callback) { |
| 1440 | rcu_read_unlock(); |
| 1441 | goto done; |
| 1442 | } |
| 1443 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1444 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1445 | |
| 1446 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1447 | * If we have reached the end of the connection list, walk from |
| 1448 | * the connection head. |
| 1449 | */ |
| 1450 | c = si->wc_next; |
| 1451 | if (unlikely(!c)) { |
| 1452 | c = si->all_connections_head; |
| 1453 | } |
| 1454 | |
| 1455 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1456 | * Get an estimate of the number of connections to parse in this sync. |
| 1457 | */ |
| 1458 | quota = (si->num_connections + 63) / 64; |
| 1459 | |
| 1460 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1461 | * Walk the "all connection" list and sync the connection state. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1462 | */ |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1463 | while (likely(c && quota)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1464 | struct sfe_ipv4_connection_match *cm; |
| 1465 | struct sfe_ipv4_connection_match *counter_cm; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1466 | struct sfe_connection_sync sis; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1467 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1468 | cm = c->original_match; |
| 1469 | counter_cm = c->reply_match; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1470 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1471 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1472 | * Didn't receive packets in the original direction or reply |
| 1473 | * direction, move to the next connection. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1474 | */ |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1475 | if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) { |
| 1476 | c = c->all_connections_next; |
| 1477 | continue; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1480 | quota--; |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 1481 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1482 | sfe_ipv4_gen_sync_connection(si, c, &sis, SFE_SYNC_REASON_STATS, now_jiffies); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1483 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1484 | si->wc_next = c->all_connections_next; |
| 1485 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1486 | /* |
| 1487 | * We don't want to be holding the lock when we sync! |
| 1488 | */ |
| 1489 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1490 | sync_rule_callback(&sis); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1491 | spin_lock_bh(&si->lock); |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 1494 | * c must be set and used in the same lock/unlock window; |
| 1495 | * because c could be removed when we don't hold the lock, |
| 1496 | * so delay grabbing until after the callback and relock. |
| 1497 | */ |
| 1498 | c = si->wc_next; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1501 | /* |
| 1502 | * At the end of the sync, put the wc_next to the connection we left. |
| 1503 | */ |
| 1504 | si->wc_next = c; |
| 1505 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1506 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1507 | rcu_read_unlock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1508 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1509 | done: |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1510 | schedule_delayed_work_on(si->work_cpu, (struct delayed_work *)work, ((HZ + 99) / 100)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | #define CHAR_DEV_MSG_SIZE 768 |
| 1514 | |
| 1515 | /* |
| 1516 | * sfe_ipv4_debug_dev_read_start() |
| 1517 | * Generate part of the XML output. |
| 1518 | */ |
| 1519 | static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1520 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1521 | { |
| 1522 | int bytes_read; |
| 1523 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1524 | si->debug_read_seq++; |
| 1525 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1526 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n"); |
| 1527 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1528 | return false; |
| 1529 | } |
| 1530 | |
| 1531 | *length -= bytes_read; |
| 1532 | *total_read += bytes_read; |
| 1533 | |
| 1534 | ws->state++; |
| 1535 | return true; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * sfe_ipv4_debug_dev_read_connections_start() |
| 1540 | * Generate part of the XML output. |
| 1541 | */ |
| 1542 | static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1543 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1544 | { |
| 1545 | int bytes_read; |
| 1546 | |
| 1547 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n"); |
| 1548 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
| 1552 | *length -= bytes_read; |
| 1553 | *total_read += bytes_read; |
| 1554 | |
| 1555 | ws->state++; |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * sfe_ipv4_debug_dev_read_connections_connection() |
| 1561 | * Generate part of the XML output. |
| 1562 | */ |
| 1563 | static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1564 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1565 | { |
| 1566 | struct sfe_ipv4_connection *c; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1567 | struct sfe_ipv4_connection_match *original_cm; |
| 1568 | struct sfe_ipv4_connection_match *reply_cm; |
| 1569 | int bytes_read; |
| 1570 | int protocol; |
| 1571 | struct net_device *src_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1572 | __be32 src_ip; |
| 1573 | __be32 src_ip_xlate; |
| 1574 | __be16 src_port; |
| 1575 | __be16 src_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1576 | u64 src_rx_packets; |
| 1577 | u64 src_rx_bytes; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1578 | struct net_device *dest_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1579 | __be32 dest_ip; |
| 1580 | __be32 dest_ip_xlate; |
| 1581 | __be16 dest_port; |
| 1582 | __be16 dest_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1583 | u64 dest_rx_packets; |
| 1584 | u64 dest_rx_bytes; |
| 1585 | u64 last_sync_jiffies; |
| 1586 | u32 mark, src_priority, dest_priority, src_dscp, dest_dscp; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1587 | u32 packet, byte; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1588 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1589 | int src_flow_cookie, dst_flow_cookie; |
| 1590 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1591 | |
| 1592 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1593 | |
| 1594 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
| 1595 | if (c->debug_read_seq < si->debug_read_seq) { |
| 1596 | c->debug_read_seq = si->debug_read_seq; |
| 1597 | break; |
| 1598 | } |
| 1599 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1600 | |
| 1601 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1602 | * If there were no connections then move to the next state. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1603 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1604 | if (!c || c->removed) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1605 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1606 | ws->state++; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1607 | return true; |
| 1608 | } |
| 1609 | |
| 1610 | original_cm = c->original_match; |
| 1611 | reply_cm = c->reply_match; |
| 1612 | |
| 1613 | protocol = c->protocol; |
| 1614 | src_dev = c->original_dev; |
| 1615 | src_ip = c->src_ip; |
| 1616 | src_ip_xlate = c->src_ip_xlate; |
| 1617 | src_port = c->src_port; |
| 1618 | src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1619 | src_priority = original_cm->priority; |
| 1620 | src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1621 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1622 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte); |
| 1623 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1624 | |
| 1625 | src_rx_packets = original_cm->rx_packet_count64; |
| 1626 | src_rx_bytes = original_cm->rx_byte_count64; |
| 1627 | dest_dev = c->reply_dev; |
| 1628 | dest_ip = c->dest_ip; |
| 1629 | dest_ip_xlate = c->dest_ip_xlate; |
| 1630 | dest_port = c->dest_port; |
| 1631 | dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1632 | dest_priority = reply_cm->priority; |
| 1633 | dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1634 | dest_rx_packets = reply_cm->rx_packet_count64; |
| 1635 | dest_rx_bytes = reply_cm->rx_byte_count64; |
| 1636 | last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies; |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 1637 | mark = c->mark; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1638 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1639 | src_flow_cookie = original_cm->flow_cookie; |
| 1640 | dst_flow_cookie = reply_cm->flow_cookie; |
| 1641 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1642 | spin_unlock_bh(&si->lock); |
| 1643 | |
| 1644 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection " |
| 1645 | "protocol=\"%u\" " |
| 1646 | "src_dev=\"%s\" " |
| 1647 | "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" " |
| 1648 | "src_port=\"%u\" src_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1649 | "src_priority=\"%u\" src_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1650 | "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" " |
| 1651 | "dest_dev=\"%s\" " |
| 1652 | "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" " |
| 1653 | "dest_port=\"%u\" dest_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1654 | "dest_priority=\"%u\" dest_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1655 | "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" " |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1656 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1657 | "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" " |
| 1658 | #endif |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 1659 | "last_sync=\"%llu\" " |
Nicolas Costa | bb85a2e | 2014-01-13 16:26:33 -0600 | [diff] [blame] | 1660 | "mark=\"%08x\" />\n", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1661 | protocol, |
| 1662 | src_dev->name, |
| 1663 | &src_ip, &src_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1664 | ntohs(src_port), ntohs(src_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1665 | src_priority, src_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1666 | src_rx_packets, src_rx_bytes, |
| 1667 | dest_dev->name, |
| 1668 | &dest_ip, &dest_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 1669 | ntohs(dest_port), ntohs(dest_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1670 | dest_priority, dest_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1671 | dest_rx_packets, dest_rx_bytes, |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1672 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1673 | src_flow_cookie, dst_flow_cookie, |
| 1674 | #endif |
Cristian Prundeanu | 592265e | 2013-12-26 11:01:22 -0600 | [diff] [blame] | 1675 | last_sync_jiffies, mark); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1676 | |
| 1677 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1678 | return false; |
| 1679 | } |
| 1680 | |
| 1681 | *length -= bytes_read; |
| 1682 | *total_read += bytes_read; |
| 1683 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1684 | return true; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * sfe_ipv4_debug_dev_read_connections_end() |
| 1689 | * Generate part of the XML output. |
| 1690 | */ |
| 1691 | static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1692 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1693 | { |
| 1694 | int bytes_read; |
| 1695 | |
| 1696 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n"); |
| 1697 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1698 | return false; |
| 1699 | } |
| 1700 | |
| 1701 | *length -= bytes_read; |
| 1702 | *total_read += bytes_read; |
| 1703 | |
| 1704 | ws->state++; |
| 1705 | return true; |
| 1706 | } |
| 1707 | |
| 1708 | /* |
| 1709 | * sfe_ipv4_debug_dev_read_exceptions_start() |
| 1710 | * Generate part of the XML output. |
| 1711 | */ |
| 1712 | static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1713 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1714 | { |
| 1715 | int bytes_read; |
| 1716 | |
| 1717 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n"); |
| 1718 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1719 | return false; |
| 1720 | } |
| 1721 | |
| 1722 | *length -= bytes_read; |
| 1723 | *total_read += bytes_read; |
| 1724 | |
| 1725 | ws->state++; |
| 1726 | return true; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * sfe_ipv4_debug_dev_read_exceptions_exception() |
| 1731 | * Generate part of the XML output. |
| 1732 | */ |
| 1733 | static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1734 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1735 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1736 | int i; |
| 1737 | u64 val = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1738 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1739 | for_each_possible_cpu(i) { |
| 1740 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 1741 | val += s->exception_events64[ws->iter_exception]; |
| 1742 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1743 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1744 | if (val) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1745 | int bytes_read; |
| 1746 | |
| 1747 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, |
| 1748 | "\t\t<exception name=\"%s\" count=\"%llu\" />\n", |
| 1749 | sfe_ipv4_exception_events_string[ws->iter_exception], |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1750 | val); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1751 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
| 1755 | *length -= bytes_read; |
| 1756 | *total_read += bytes_read; |
| 1757 | } |
| 1758 | |
| 1759 | ws->iter_exception++; |
| 1760 | if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) { |
| 1761 | ws->iter_exception = 0; |
| 1762 | ws->state++; |
| 1763 | } |
| 1764 | |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
| 1768 | /* |
| 1769 | * sfe_ipv4_debug_dev_read_exceptions_end() |
| 1770 | * Generate part of the XML output. |
| 1771 | */ |
| 1772 | static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1773 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1774 | { |
| 1775 | int bytes_read; |
| 1776 | |
| 1777 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n"); |
| 1778 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1779 | return false; |
| 1780 | } |
| 1781 | |
| 1782 | *length -= bytes_read; |
| 1783 | *total_read += bytes_read; |
| 1784 | |
| 1785 | ws->state++; |
| 1786 | return true; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * sfe_ipv4_debug_dev_read_stats() |
| 1791 | * Generate part of the XML output. |
| 1792 | */ |
| 1793 | static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1794 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1795 | { |
| 1796 | int bytes_read; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1797 | struct sfe_ipv4_stats stats; |
| 1798 | unsigned int num_conn; |
| 1799 | |
| 1800 | sfe_ipv4_update_summary_stats(si, &stats); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1801 | |
| 1802 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1803 | num_conn = si->num_connections; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1804 | spin_unlock_bh(&si->lock); |
| 1805 | |
| 1806 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats " |
| 1807 | "num_connections=\"%u\" " |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 1808 | "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" " |
| 1809 | "create_requests=\"%llu\" create_collisions=\"%llu\" " |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1810 | "create_failures=\"%llu\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1811 | "destroy_requests=\"%llu\" destroy_misses=\"%llu\" " |
| 1812 | "flushes=\"%llu\" " |
| 1813 | "hash_hits=\"%llu\" hash_reorders=\"%llu\" />\n", |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1814 | num_conn, |
| 1815 | stats.packets_forwarded64, |
| 1816 | stats.packets_not_forwarded64, |
| 1817 | stats.connection_create_requests64, |
| 1818 | stats.connection_create_collisions64, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1819 | stats.connection_create_failures64, |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1820 | stats.connection_destroy_requests64, |
| 1821 | stats.connection_destroy_misses64, |
| 1822 | stats.connection_flushes64, |
| 1823 | stats.connection_match_hash_hits64, |
| 1824 | stats.connection_match_hash_reorders64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1825 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1826 | return false; |
| 1827 | } |
| 1828 | |
| 1829 | *length -= bytes_read; |
| 1830 | *total_read += bytes_read; |
| 1831 | |
| 1832 | ws->state++; |
| 1833 | return true; |
| 1834 | } |
| 1835 | |
| 1836 | /* |
| 1837 | * sfe_ipv4_debug_dev_read_end() |
| 1838 | * Generate part of the XML output. |
| 1839 | */ |
| 1840 | static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1841 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1842 | { |
| 1843 | int bytes_read; |
| 1844 | |
| 1845 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n"); |
| 1846 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1847 | return false; |
| 1848 | } |
| 1849 | |
| 1850 | *length -= bytes_read; |
| 1851 | *total_read += bytes_read; |
| 1852 | |
| 1853 | ws->state++; |
| 1854 | return true; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * Array of write functions that write various XML elements that correspond to |
| 1859 | * our XML output state machine. |
| 1860 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 1861 | static sfe_ipv4_debug_xml_write_method_t sfe_ipv4_debug_xml_write_methods[SFE_IPV4_DEBUG_XML_STATE_DONE] = { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1862 | sfe_ipv4_debug_dev_read_start, |
| 1863 | sfe_ipv4_debug_dev_read_connections_start, |
| 1864 | sfe_ipv4_debug_dev_read_connections_connection, |
| 1865 | sfe_ipv4_debug_dev_read_connections_end, |
| 1866 | sfe_ipv4_debug_dev_read_exceptions_start, |
| 1867 | sfe_ipv4_debug_dev_read_exceptions_exception, |
| 1868 | sfe_ipv4_debug_dev_read_exceptions_end, |
| 1869 | sfe_ipv4_debug_dev_read_stats, |
| 1870 | sfe_ipv4_debug_dev_read_end, |
| 1871 | }; |
| 1872 | |
| 1873 | /* |
| 1874 | * sfe_ipv4_debug_dev_read() |
| 1875 | * Send info to userspace upon read request from user |
| 1876 | */ |
| 1877 | static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset) |
| 1878 | { |
| 1879 | char msg[CHAR_DEV_MSG_SIZE]; |
| 1880 | int total_read = 0; |
| 1881 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 1882 | struct sfe_ipv4 *si = &__si; |
| 1883 | |
| 1884 | ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data; |
| 1885 | while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) { |
| 1886 | if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) { |
| 1887 | continue; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | return total_read; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1895 | * sfe_ipv4_debug_dev_open() |
| 1896 | */ |
| 1897 | static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file) |
| 1898 | { |
| 1899 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 1900 | |
| 1901 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 1902 | if (!ws) { |
| 1903 | ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL); |
| 1904 | if (!ws) { |
| 1905 | return -ENOMEM; |
| 1906 | } |
| 1907 | |
| 1908 | ws->state = SFE_IPV4_DEBUG_XML_STATE_START; |
| 1909 | file->private_data = ws; |
| 1910 | } |
| 1911 | |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * sfe_ipv4_debug_dev_release() |
| 1917 | */ |
| 1918 | static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file) |
| 1919 | { |
| 1920 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 1921 | |
| 1922 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 1923 | if (ws) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1924 | /* |
| 1925 | * We've finished with our output so free the write state. |
| 1926 | */ |
| 1927 | kfree(ws); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1928 | file->private_data = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * File operations used in the debug char device |
| 1936 | */ |
| 1937 | static struct file_operations sfe_ipv4_debug_dev_fops = { |
| 1938 | .read = sfe_ipv4_debug_dev_read, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1939 | .open = sfe_ipv4_debug_dev_open, |
| 1940 | .release = sfe_ipv4_debug_dev_release |
| 1941 | }; |
| 1942 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1943 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1944 | /* |
| 1945 | * sfe_register_flow_cookie_cb |
| 1946 | * register a function in SFE to let SFE use this function to configure flow cookie for a flow |
| 1947 | * |
| 1948 | * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE |
| 1949 | * can use this function to configure flow cookie for a flow. |
| 1950 | * return: 0, success; !=0, fail |
| 1951 | */ |
| 1952 | int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 1953 | { |
| 1954 | struct sfe_ipv4 *si = &__si; |
| 1955 | |
| 1956 | BUG_ON(!cb); |
| 1957 | |
| 1958 | if (si->flow_cookie_set_func) { |
| 1959 | return -1; |
| 1960 | } |
| 1961 | |
| 1962 | rcu_assign_pointer(si->flow_cookie_set_func, cb); |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * sfe_unregister_flow_cookie_cb |
| 1968 | * unregister function which is used to configure flow cookie for a flow |
| 1969 | * |
| 1970 | * return: 0, success; !=0, fail |
| 1971 | */ |
| 1972 | int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 1973 | { |
| 1974 | struct sfe_ipv4 *si = &__si; |
| 1975 | |
| 1976 | RCU_INIT_POINTER(si->flow_cookie_set_func, NULL); |
| 1977 | return 0; |
| 1978 | } |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 1979 | |
| 1980 | /* |
| 1981 | * sfe_ipv4_get_flow_cookie() |
| 1982 | */ |
| 1983 | static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev, |
| 1984 | struct device_attribute *attr, |
| 1985 | char *buf) |
| 1986 | { |
| 1987 | struct sfe_ipv4 *si = &__si; |
Xiaoping Fan | 01c67cc | 2015-11-09 11:31:57 -0800 | [diff] [blame] | 1988 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->flow_cookie_enable); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * sfe_ipv4_set_flow_cookie() |
| 1993 | */ |
| 1994 | static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev, |
| 1995 | struct device_attribute *attr, |
| 1996 | const char *buf, size_t size) |
| 1997 | { |
| 1998 | struct sfe_ipv4 *si = &__si; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1999 | si->flow_cookie_enable = simple_strtol(buf, NULL, 0); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2000 | |
| 2001 | return size; |
| 2002 | } |
| 2003 | |
| 2004 | /* |
| 2005 | * sysfs attributes. |
| 2006 | */ |
| 2007 | static const struct device_attribute sfe_ipv4_flow_cookie_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 2008 | __ATTR(flow_cookie_enable, S_IWUSR | S_IRUGO, sfe_ipv4_get_flow_cookie, sfe_ipv4_set_flow_cookie); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2009 | #endif /*CONFIG_NF_FLOW_COOKIE*/ |
| 2010 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2011 | /* |
| 2012 | * sfe_ipv4_get_cpu() |
| 2013 | */ |
| 2014 | static ssize_t sfe_ipv4_get_cpu(struct device *dev, |
| 2015 | struct device_attribute *attr, |
| 2016 | char *buf) |
| 2017 | { |
| 2018 | struct sfe_ipv4 *si = &__si; |
| 2019 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu); |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * sfe_ipv4_set_cpu() |
| 2024 | */ |
| 2025 | static ssize_t sfe_ipv4_set_cpu(struct device *dev, |
| 2026 | struct device_attribute *attr, |
| 2027 | const char *buf, size_t size) |
| 2028 | { |
| 2029 | struct sfe_ipv4 *si = &__si; |
| 2030 | int work_cpu; |
| 2031 | work_cpu = simple_strtol(buf, NULL, 0); |
| 2032 | if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) { |
| 2033 | si->work_cpu = work_cpu; |
| 2034 | } else { |
| 2035 | dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS); |
| 2036 | } |
| 2037 | return size; |
| 2038 | } |
| 2039 | /* |
| 2040 | * sysfs attributes. |
| 2041 | */ |
| 2042 | static const struct device_attribute sfe_ipv4_cpu_attr = |
| 2043 | __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu); |
| 2044 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2045 | /* |
| 2046 | * sfe_ipv4_conn_match_hash_init() |
| 2047 | * Initialize conn match hash lists |
| 2048 | */ |
| 2049 | static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len) |
| 2050 | { |
| 2051 | struct hlist_head *hash_list = si->hlist_conn_match_hash_head; |
| 2052 | int i; |
| 2053 | |
| 2054 | for (i = 0; i < len; i++) { |
| 2055 | INIT_HLIST_HEAD(&hash_list[i]); |
| 2056 | } |
| 2057 | } |
| 2058 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2059 | /* |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2060 | * sfe_ipv4_init() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2061 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2062 | int sfe_ipv4_init(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2063 | { |
| 2064 | struct sfe_ipv4 *si = &__si; |
| 2065 | int result = -1; |
| 2066 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2067 | DEBUG_INFO("SFE IPv4 init\n"); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2068 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2069 | sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head)); |
| 2070 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2071 | si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO); |
| 2072 | if (!si->stats_pcpu) { |
| 2073 | DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n"); |
| 2074 | goto exit0; |
| 2075 | } |
| 2076 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2077 | /* |
| 2078 | * Create sys/sfe_ipv4 |
| 2079 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2080 | si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL); |
| 2081 | if (!si->sys_ipv4) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2082 | DEBUG_ERROR("failed to register sfe_ipv4\n"); |
| 2083 | goto exit1; |
| 2084 | } |
| 2085 | |
| 2086 | /* |
| 2087 | * Create files, one for each parameter supported by this module. |
| 2088 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2089 | result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2090 | if (result) { |
| 2091 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2092 | goto exit2; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2095 | result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2096 | if (result) { |
| 2097 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
| 2098 | goto exit3; |
| 2099 | } |
| 2100 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2101 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2102 | result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2103 | if (result) { |
| 2104 | DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2105 | goto exit4; |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2106 | } |
| 2107 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
| 2108 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2109 | /* |
| 2110 | * Register our debug char device. |
| 2111 | */ |
| 2112 | result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops); |
| 2113 | if (result < 0) { |
| 2114 | DEBUG_ERROR("Failed to register chrdev: %d\n", result); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2115 | goto exit5; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | si->debug_dev = result; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2119 | si->work_cpu = WORK_CPU_UNBOUND; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2120 | |
| 2121 | /* |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2122 | * Create a work to handle periodic statistics. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2123 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2124 | INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync); |
| 2125 | schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100)); |
| 2126 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2127 | spin_lock_init(&si->lock); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2128 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2129 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2130 | exit5: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2131 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2132 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2133 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2134 | exit4: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2135 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2136 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2137 | exit3: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2138 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2139 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2140 | exit2: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2141 | kobject_put(si->sys_ipv4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2142 | |
| 2143 | exit1: |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2144 | free_percpu(si->stats_pcpu); |
| 2145 | |
| 2146 | exit0: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2147 | return result; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2151 | * sfe_ipv4_exit() |
| 2152 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2153 | void sfe_ipv4_exit(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2154 | { |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2155 | struct sfe_ipv4 *si = &__si; |
| 2156 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2157 | DEBUG_INFO("SFE IPv4 exit\n"); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2158 | /* |
| 2159 | * Destroy all connections. |
| 2160 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2161 | sfe_ipv4_destroy_all_rules_for_dev(NULL); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2162 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2163 | cancel_delayed_work_sync(&si->sync_dwork); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2164 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2165 | unregister_chrdev(si->debug_dev, "sfe_ipv4"); |
| 2166 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2167 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2168 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2169 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2170 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
| 2171 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2172 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2173 | kobject_put(si->sys_ipv4); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2174 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2175 | free_percpu(si->stats_pcpu); |
| 2176 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2179 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2180 | EXPORT_SYMBOL(sfe_register_flow_cookie_cb); |
| 2181 | EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb); |
| 2182 | #endif |