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