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 | 5f27e23 | 2022-01-06 14:39:04 +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> |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 26 | #include <net/udp.h> |
| 27 | #include <net/vxlan.h> |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 28 | #include <linux/etherdevice.h> |
Tian Yang | 45f39c8 | 2020-10-06 14:07:47 -0700 | [diff] [blame] | 29 | #include <linux/version.h> |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 30 | #include <linux/lockdep.h> |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 31 | #include <linux/refcount.h> |
| 32 | #include <linux/netfilter.h> |
| 33 | #include <linux/inetdevice.h> |
| 34 | #include <linux/netfilter_ipv4.h> |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 35 | #include <linux/seqlock.h> |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 36 | #include <net/protocol.h> |
| 37 | #include <net/gre.h> |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 38 | |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 39 | #include "sfe_debug.h" |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 40 | #include "sfe_api.h" |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 41 | #include "sfe.h" |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 42 | #include "sfe_flow_cookie.h" |
| 43 | #include "sfe_ipv4.h" |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 44 | #include "sfe_ipv4_udp.h" |
| 45 | #include "sfe_ipv4_tcp.h" |
| 46 | #include "sfe_ipv4_icmp.h" |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 47 | #include "sfe_pppoe.h" |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 48 | #include "sfe_ipv4_gre.h" |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 49 | |
| 50 | static char *sfe_ipv4_exception_events_string[SFE_IPV4_EXCEPTION_EVENT_LAST] = { |
| 51 | "UDP_HEADER_INCOMPLETE", |
| 52 | "UDP_NO_CONNECTION", |
| 53 | "UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 54 | "UDP_SMALL_TTL", |
| 55 | "UDP_NEEDS_FRAGMENTATION", |
| 56 | "TCP_HEADER_INCOMPLETE", |
| 57 | "TCP_NO_CONNECTION_SLOW_FLAGS", |
| 58 | "TCP_NO_CONNECTION_FAST_FLAGS", |
| 59 | "TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 60 | "TCP_SMALL_TTL", |
| 61 | "TCP_NEEDS_FRAGMENTATION", |
| 62 | "TCP_FLAGS", |
| 63 | "TCP_SEQ_EXCEEDS_RIGHT_EDGE", |
| 64 | "TCP_SMALL_DATA_OFFS", |
| 65 | "TCP_BAD_SACK", |
| 66 | "TCP_BIG_DATA_OFFS", |
| 67 | "TCP_SEQ_BEFORE_LEFT_EDGE", |
| 68 | "TCP_ACK_EXCEEDS_RIGHT_EDGE", |
| 69 | "TCP_ACK_BEFORE_LEFT_EDGE", |
| 70 | "ICMP_HEADER_INCOMPLETE", |
| 71 | "ICMP_UNHANDLED_TYPE", |
| 72 | "ICMP_IPV4_HEADER_INCOMPLETE", |
| 73 | "ICMP_IPV4_NON_V4", |
| 74 | "ICMP_IPV4_IP_OPTIONS_INCOMPLETE", |
| 75 | "ICMP_IPV4_UDP_HEADER_INCOMPLETE", |
| 76 | "ICMP_IPV4_TCP_HEADER_INCOMPLETE", |
| 77 | "ICMP_IPV4_UNHANDLED_PROTOCOL", |
| 78 | "ICMP_NO_CONNECTION", |
| 79 | "ICMP_FLUSHED_CONNECTION", |
| 80 | "HEADER_INCOMPLETE", |
Ratheesh Kannoth | 43d64f8 | 2021-10-20 08:23:29 +0530 | [diff] [blame] | 81 | "HEADER_CSUM_BAD", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 82 | "BAD_TOTAL_LENGTH", |
| 83 | "NON_V4", |
| 84 | "NON_INITIAL_FRAGMENT", |
| 85 | "DATAGRAM_INCOMPLETE", |
| 86 | "IP_OPTIONS_INCOMPLETE", |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 87 | "UNHANDLED_PROTOCOL", |
Nitin Shetty | 16ab38d | 2022-02-09 01:26:19 +0530 | [diff] [blame] | 88 | "NO_HEADROOM", |
| 89 | "INVALID_PPPOE_SESSION", |
| 90 | "INCORRECT_PPPOE_PARSING", |
| 91 | "PPPOE_NOT_SET_IN_CME", |
| 92 | "INGRESS_VLAN_TAG_MISMATCH", |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 93 | "INVALID_SOURCE_INTERFACE", |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 94 | "GRE_HEADER_INCOMPLETE", |
| 95 | "GRE_NO_CONNECTION", |
| 96 | "GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT", |
| 97 | "GRE_SMALL_TTL", |
| 98 | "GRE_NEEDS_FRAGMENTATION" |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 101 | static struct sfe_ipv4 __si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 102 | |
| 103 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 104 | * sfe_ipv4_gen_ip_csum() |
| 105 | * Generate the IP checksum for an IPv4 header. |
| 106 | * |
| 107 | * Note that this function assumes that we have only 20 bytes of IP header. |
| 108 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 109 | u16 sfe_ipv4_gen_ip_csum(struct iphdr *iph) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 110 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 111 | u32 sum; |
| 112 | u16 *i = (u16 *)iph; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 113 | |
| 114 | iph->check = 0; |
| 115 | |
| 116 | /* |
| 117 | * Generate the sum. |
| 118 | */ |
| 119 | sum = i[0] + i[1] + i[2] + i[3] + i[4] + i[5] + i[6] + i[7] + i[8] + i[9]; |
| 120 | |
| 121 | /* |
| 122 | * Fold it to ones-complement form. |
| 123 | */ |
| 124 | sum = (sum & 0xffff) + (sum >> 16); |
| 125 | sum = (sum & 0xffff) + (sum >> 16); |
| 126 | |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 127 | return (u16)sum ^ 0xffff; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * sfe_ipv4_get_connection_match_hash() |
| 132 | * Generate the hash used in connection match lookups. |
| 133 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 134 | 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] | 135 | __be32 src_ip, __be16 src_port, |
| 136 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 137 | { |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 138 | 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] | 139 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 140 | } |
| 141 | |
| 142 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 143 | * sfe_ipv4_find_connection_match_rcu() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 144 | * Get the IPv4 flow match info that corresponds to a particular 5-tuple. |
| 145 | * |
| 146 | * On entry we must be holding the lock that protects the hash table. |
| 147 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 148 | struct sfe_ipv4_connection_match * |
| 149 | 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] | 150 | __be32 src_ip, __be16 src_port, |
| 151 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 152 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 153 | struct sfe_ipv4_connection_match *cm = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 154 | unsigned int conn_match_idx; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 155 | struct hlist_head *lhead; |
| 156 | |
| 157 | WARN_ON_ONCE(!rcu_read_lock_held()); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 158 | |
| 159 | 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] | 160 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 161 | lhead = &si->hlist_conn_match_hash_head[conn_match_idx]; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 162 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 163 | hlist_for_each_entry_rcu(cm, lhead, hnode) { |
| 164 | if (cm->match_src_port != src_port |
| 165 | || cm->match_dest_port != dest_port |
| 166 | || cm->match_src_ip != src_ip |
| 167 | || cm->match_dest_ip != dest_ip |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 168 | || cm->match_protocol != protocol) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 169 | continue; |
| 170 | } |
| 171 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 172 | this_cpu_inc(si->stats_pcpu->connection_match_hash_hits64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 173 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 174 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 177 | return cm; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * sfe_ipv4_connection_match_update_summary_stats() |
| 182 | * Update the summary stats for a connection match entry. |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 183 | * |
| 184 | * Stats are incremented atomically. So use atomic substraction to update summary |
| 185 | * stats. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 186 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 187 | static inline void sfe_ipv4_connection_match_update_summary_stats(struct sfe_ipv4_connection_match *cm, |
| 188 | u32 *packets, u32 *bytes) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 189 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 190 | u32 packet_count, byte_count; |
| 191 | |
| 192 | packet_count = atomic_read(&cm->rx_packet_count); |
| 193 | cm->rx_packet_count64 += packet_count; |
| 194 | atomic_sub(packet_count, &cm->rx_packet_count); |
| 195 | |
| 196 | byte_count = atomic_read(&cm->rx_byte_count); |
| 197 | cm->rx_byte_count64 += byte_count; |
| 198 | atomic_sub(byte_count, &cm->rx_byte_count); |
| 199 | |
| 200 | *packets = packet_count; |
| 201 | *bytes = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * sfe_ipv4_connection_match_compute_translations() |
| 206 | * Compute port and address translations for a connection match entry. |
| 207 | */ |
| 208 | static void sfe_ipv4_connection_match_compute_translations(struct sfe_ipv4_connection_match *cm) |
| 209 | { |
| 210 | /* |
| 211 | * Before we insert the entry look to see if this is tagged as doing address |
| 212 | * translations. If it is then work out the adjustment that we need to apply |
| 213 | * to the transport checksum. |
| 214 | */ |
| 215 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
| 216 | /* |
| 217 | * Precompute an incremental checksum adjustment so we can |
| 218 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 219 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 220 | u16 src_ip_hi = cm->match_src_ip >> 16; |
| 221 | u16 src_ip_lo = cm->match_src_ip & 0xffff; |
| 222 | u32 xlate_src_ip = ~cm->xlate_src_ip; |
| 223 | u16 xlate_src_ip_hi = xlate_src_ip >> 16; |
| 224 | u16 xlate_src_ip_lo = xlate_src_ip & 0xffff; |
| 225 | u16 xlate_src_port = ~cm->xlate_src_port; |
| 226 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * When we compute this fold it down to a 16-bit offset |
| 230 | * as that way we can avoid having to do a double |
| 231 | * folding of the twos-complement result because the |
| 232 | * addition of 2 16-bit values cannot cause a double |
| 233 | * wrap-around! |
| 234 | */ |
| 235 | adj = src_ip_hi + src_ip_lo + cm->match_src_port |
| 236 | + xlate_src_ip_hi + xlate_src_ip_lo + xlate_src_port; |
| 237 | adj = (adj & 0xffff) + (adj >> 16); |
| 238 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 239 | cm->xlate_src_csum_adjustment = (u16)adj; |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 240 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
| 244 | /* |
| 245 | * Precompute an incremental checksum adjustment so we can |
| 246 | * edit packets in this stream very quickly. The algorithm is from RFC1624. |
| 247 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 248 | u16 dest_ip_hi = cm->match_dest_ip >> 16; |
| 249 | u16 dest_ip_lo = cm->match_dest_ip & 0xffff; |
| 250 | u32 xlate_dest_ip = ~cm->xlate_dest_ip; |
| 251 | u16 xlate_dest_ip_hi = xlate_dest_ip >> 16; |
| 252 | u16 xlate_dest_ip_lo = xlate_dest_ip & 0xffff; |
| 253 | u16 xlate_dest_port = ~cm->xlate_dest_port; |
| 254 | u32 adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * When we compute this fold it down to a 16-bit offset |
| 258 | * as that way we can avoid having to do a double |
| 259 | * folding of the twos-complement result because the |
| 260 | * addition of 2 16-bit values cannot cause a double |
| 261 | * wrap-around! |
| 262 | */ |
| 263 | adj = dest_ip_hi + dest_ip_lo + cm->match_dest_port |
| 264 | + xlate_dest_ip_hi + xlate_dest_ip_lo + xlate_dest_port; |
| 265 | adj = (adj & 0xffff) + (adj >> 16); |
| 266 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 267 | cm->xlate_dest_csum_adjustment = (u16)adj; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 268 | } |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 269 | |
| 270 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 271 | u32 adj = ~cm->match_src_ip + cm->xlate_src_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 272 | if (adj < cm->xlate_src_ip) { |
| 273 | adj++; |
| 274 | } |
| 275 | |
| 276 | adj = (adj & 0xffff) + (adj >> 16); |
| 277 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 278 | cm->xlate_src_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST) { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 282 | u32 adj = ~cm->match_dest_ip + cm->xlate_dest_ip; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 283 | if (adj < cm->xlate_dest_ip) { |
| 284 | adj++; |
| 285 | } |
| 286 | |
| 287 | adj = (adj & 0xffff) + (adj >> 16); |
| 288 | adj = (adj & 0xffff) + (adj >> 16); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 289 | cm->xlate_dest_partial_csum_adjustment = (u16)adj; |
Xiaoping Fan | ad755af | 2015-04-01 16:58:46 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | * sfe_ipv4_update_summary_stats() |
| 296 | * Update the summary stats. |
| 297 | */ |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 298 | 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] | 299 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 300 | int i = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 301 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 302 | memset(stats, 0, sizeof(*stats)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 303 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 304 | for_each_possible_cpu(i) { |
| 305 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 306 | |
| 307 | stats->connection_create_requests64 += s->connection_create_requests64; |
| 308 | stats->connection_create_collisions64 += s->connection_create_collisions64; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 309 | stats->connection_create_failures64 += s->connection_create_failures64; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 310 | stats->connection_destroy_requests64 += s->connection_destroy_requests64; |
| 311 | stats->connection_destroy_misses64 += s->connection_destroy_misses64; |
| 312 | stats->connection_match_hash_hits64 += s->connection_match_hash_hits64; |
| 313 | stats->connection_match_hash_reorders64 += s->connection_match_hash_reorders64; |
| 314 | stats->connection_flushes64 += s->connection_flushes64; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 315 | stats->packets_dropped64 += s->packets_dropped64; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 316 | stats->packets_forwarded64 += s->packets_forwarded64; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 317 | stats->packets_fast_xmited64 += s->packets_fast_xmited64; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 318 | stats->packets_not_forwarded64 += s->packets_not_forwarded64; |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 319 | stats->pppoe_encap_packets_forwarded64 += s->pppoe_encap_packets_forwarded64; |
| 320 | stats->pppoe_decap_packets_forwarded64 += s->pppoe_decap_packets_forwarded64; |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 321 | stats->pppoe_bridge_packets_forwarded64 += s->pppoe_bridge_packets_forwarded64; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 322 | } |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 323 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 327 | * sfe_ipv4_insert_connection_match() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 328 | * Insert a connection match into the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 329 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 330 | static inline void sfe_ipv4_insert_connection_match(struct sfe_ipv4 *si, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 331 | struct sfe_ipv4_connection_match *cm) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 332 | { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 333 | unsigned int conn_match_idx |
| 334 | = sfe_ipv4_get_connection_match_hash(cm->match_dev, cm->match_protocol, |
| 335 | cm->match_src_ip, cm->match_src_port, |
| 336 | cm->match_dest_ip, cm->match_dest_port); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 337 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 338 | lockdep_assert_held(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 339 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 340 | 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] | 341 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 342 | if (!si->flow_cookie_enable) |
| 343 | return; |
| 344 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 345 | /* |
| 346 | * Configure hardware to put a flow cookie in packet of this flow, |
| 347 | * then we can accelerate the lookup process when we received this packet. |
| 348 | */ |
| 349 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 350 | struct sfe_flow_cookie_entry *entry = &si->sfe_flow_cookie_table[conn_match_idx]; |
| 351 | |
| 352 | if ((NULL == entry->match) && time_is_before_jiffies(entry->last_clean_time + HZ)) { |
| 353 | flow_cookie_set_func_t func; |
| 354 | |
| 355 | rcu_read_lock(); |
| 356 | func = rcu_dereference(si->flow_cookie_set_func); |
| 357 | if (func) { |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 358 | 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] | 359 | cm->match_dest_ip, cm->match_dest_port, conn_match_idx)) { |
| 360 | entry->match = cm; |
| 361 | cm->flow_cookie = conn_match_idx; |
| 362 | } |
| 363 | } |
| 364 | rcu_read_unlock(); |
| 365 | |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 373 | * sfe_ipv4_remove_connection_match() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 374 | * Remove a connection match object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 375 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 376 | 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] | 377 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 378 | |
| 379 | lockdep_assert_held(&si->lock); |
| 380 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 381 | #ifdef CONFIG_NF_FLOW_COOKIE |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 382 | if (si->flow_cookie_enable) { |
| 383 | /* |
| 384 | * Tell hardware that we no longer need a flow cookie in packet of this flow |
| 385 | */ |
| 386 | unsigned int conn_match_idx; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 387 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 388 | for (conn_match_idx = 1; conn_match_idx < SFE_FLOW_COOKIE_SIZE; conn_match_idx++) { |
| 389 | 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] | 390 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 391 | if (cm == entry->match) { |
| 392 | flow_cookie_set_func_t func; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 393 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 394 | rcu_read_lock(); |
| 395 | func = rcu_dereference(si->flow_cookie_set_func); |
| 396 | if (func) { |
| 397 | func(cm->match_protocol, cm->match_src_ip, cm->match_src_port, |
| 398 | cm->match_dest_ip, cm->match_dest_port, 0); |
| 399 | } |
| 400 | rcu_read_unlock(); |
| 401 | |
| 402 | cm->flow_cookie = 0; |
| 403 | entry->match = NULL; |
| 404 | entry->last_clean_time = jiffies; |
| 405 | break; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 406 | } |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | #endif |
| 410 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 411 | hlist_del_init_rcu(&cm->hnode); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 412 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
| 416 | * sfe_ipv4_get_connection_hash() |
| 417 | * Generate the hash used in connection lookups. |
| 418 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 419 | 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] | 420 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 421 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 422 | 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] | 423 | return ((hash >> SFE_IPV4_CONNECTION_HASH_SHIFT) ^ hash) & SFE_IPV4_CONNECTION_HASH_MASK; |
| 424 | } |
| 425 | |
| 426 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 427 | * sfe_ipv4_find_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 428 | * Get the IPv4 connection info that corresponds to a particular 5-tuple. |
| 429 | * |
| 430 | * On entry we must be holding the lock that protects the hash table. |
| 431 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 432 | 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] | 433 | __be32 src_ip, __be16 src_port, |
| 434 | __be32 dest_ip, __be16 dest_port) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 435 | { |
| 436 | struct sfe_ipv4_connection *c; |
| 437 | 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] | 438 | |
| 439 | lockdep_assert_held(&si->lock); |
| 440 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 441 | c = si->conn_hash[conn_idx]; |
| 442 | |
| 443 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 444 | * Will need connection entry for next create/destroy metadata, |
| 445 | * So no need to re-order entry for these requests |
| 446 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 447 | while (c) { |
| 448 | if ((c->src_port == src_port) |
| 449 | && (c->dest_port == dest_port) |
| 450 | && (c->src_ip == src_ip) |
| 451 | && (c->dest_ip == dest_ip) |
| 452 | && (c->protocol == protocol)) { |
| 453 | return c; |
| 454 | } |
| 455 | |
| 456 | c = c->next; |
| 457 | } |
| 458 | |
| 459 | return NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 463 | * sfe_ipv4_insert_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 464 | * Insert a connection into the hash. |
| 465 | * |
| 466 | * On entry we must be holding the lock that protects the hash table. |
| 467 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 468 | 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] | 469 | { |
| 470 | struct sfe_ipv4_connection **hash_head; |
| 471 | struct sfe_ipv4_connection *prev_head; |
| 472 | unsigned int conn_idx; |
| 473 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 474 | lockdep_assert_held(&si->lock); |
| 475 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 476 | /* |
| 477 | * Insert entry into the connection hash. |
| 478 | */ |
| 479 | conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 480 | c->dest_ip, c->dest_port); |
| 481 | hash_head = &si->conn_hash[conn_idx]; |
| 482 | prev_head = *hash_head; |
| 483 | c->prev = NULL; |
| 484 | if (prev_head) { |
| 485 | prev_head->prev = c; |
| 486 | } |
| 487 | |
| 488 | c->next = prev_head; |
| 489 | *hash_head = c; |
| 490 | |
| 491 | /* |
| 492 | * Insert entry into the "all connections" list. |
| 493 | */ |
| 494 | if (si->all_connections_tail) { |
| 495 | c->all_connections_prev = si->all_connections_tail; |
| 496 | si->all_connections_tail->all_connections_next = c; |
| 497 | } else { |
| 498 | c->all_connections_prev = NULL; |
| 499 | si->all_connections_head = c; |
| 500 | } |
| 501 | |
| 502 | si->all_connections_tail = c; |
| 503 | c->all_connections_next = NULL; |
| 504 | si->num_connections++; |
| 505 | |
| 506 | /* |
| 507 | * Insert the connection match objects too. |
| 508 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 509 | sfe_ipv4_insert_connection_match(si, c->original_match); |
| 510 | sfe_ipv4_insert_connection_match(si, c->reply_match); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 514 | * sfe_ipv4_remove_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 515 | * Remove a sfe_ipv4_connection object from the hash. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 516 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 517 | 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] | 518 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 519 | lockdep_assert_held(&si->lock); |
| 520 | |
| 521 | if (c->removed) { |
| 522 | DEBUG_ERROR("%px: Connection has been removed already\n", c); |
| 523 | return false; |
| 524 | } |
| 525 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 526 | /* |
| 527 | * Remove the connection match objects. |
| 528 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 529 | sfe_ipv4_remove_connection_match(si, c->reply_match); |
| 530 | sfe_ipv4_remove_connection_match(si, c->original_match); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Unlink the connection. |
| 534 | */ |
| 535 | if (c->prev) { |
| 536 | c->prev->next = c->next; |
| 537 | } else { |
| 538 | unsigned int conn_idx = sfe_ipv4_get_connection_hash(c->protocol, c->src_ip, c->src_port, |
| 539 | c->dest_ip, c->dest_port); |
| 540 | si->conn_hash[conn_idx] = c->next; |
| 541 | } |
| 542 | |
| 543 | if (c->next) { |
| 544 | c->next->prev = c->prev; |
| 545 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Unlink connection from all_connections list |
| 549 | */ |
| 550 | if (c->all_connections_prev) { |
| 551 | c->all_connections_prev->all_connections_next = c->all_connections_next; |
| 552 | } else { |
| 553 | si->all_connections_head = c->all_connections_next; |
| 554 | } |
| 555 | |
| 556 | if (c->all_connections_next) { |
| 557 | c->all_connections_next->all_connections_prev = c->all_connections_prev; |
| 558 | } else { |
| 559 | si->all_connections_tail = c->all_connections_prev; |
| 560 | } |
| 561 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 562 | /* |
| 563 | * If I am the next sync connection, move the sync to my next or head. |
| 564 | */ |
| 565 | if (unlikely(si->wc_next == c)) { |
| 566 | si->wc_next = c->all_connections_next; |
| 567 | } |
| 568 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 569 | c->removed = true; |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 570 | si->num_connections--; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 571 | return true; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 575 | * sfe_ipv4_gen_sync_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 576 | * Sync a connection. |
| 577 | * |
| 578 | * 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] | 579 | * 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] | 580 | * (while called from sfe_ipv4_flush_connection()) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 581 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 582 | 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] | 583 | struct sfe_connection_sync *sis, sfe_sync_reason_t reason, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 584 | u64 now_jiffies) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 585 | { |
| 586 | struct sfe_ipv4_connection_match *original_cm; |
| 587 | struct sfe_ipv4_connection_match *reply_cm; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 588 | u32 packet_count, byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * Fill in the update message. |
| 592 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 593 | sis->is_v6 = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 594 | sis->protocol = c->protocol; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 595 | sis->src_ip.ip = c->src_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 596 | sis->src_ip_xlate.ip = c->src_ip_xlate; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 597 | sis->dest_ip.ip = c->dest_ip; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 598 | sis->dest_ip_xlate.ip = c->dest_ip_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 599 | sis->src_port = c->src_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 600 | sis->src_port_xlate = c->src_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 601 | sis->dest_port = c->dest_port; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 602 | sis->dest_port_xlate = c->dest_port_xlate; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 603 | |
| 604 | original_cm = c->original_match; |
| 605 | reply_cm = c->reply_match; |
| 606 | sis->src_td_max_window = original_cm->protocol_state.tcp.max_win; |
| 607 | sis->src_td_end = original_cm->protocol_state.tcp.end; |
| 608 | sis->src_td_max_end = original_cm->protocol_state.tcp.max_end; |
| 609 | sis->dest_td_max_window = reply_cm->protocol_state.tcp.max_win; |
| 610 | sis->dest_td_end = reply_cm->protocol_state.tcp.end; |
| 611 | sis->dest_td_max_end = reply_cm->protocol_state.tcp.max_end; |
| 612 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 613 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet_count, &byte_count); |
| 614 | sis->src_new_packet_count = packet_count; |
| 615 | sis->src_new_byte_count = byte_count; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 616 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 617 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet_count, &byte_count); |
| 618 | sis->dest_new_packet_count = packet_count; |
| 619 | sis->dest_new_byte_count = byte_count; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 620 | |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 621 | sis->src_dev = original_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 622 | sis->src_packet_count = original_cm->rx_packet_count64; |
| 623 | sis->src_byte_count = original_cm->rx_byte_count64; |
Matthew McClintock | d0cdb80 | 2014-02-24 16:30:35 -0600 | [diff] [blame] | 624 | |
| 625 | sis->dest_dev = reply_cm->match_dev; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 626 | sis->dest_packet_count = reply_cm->rx_packet_count64; |
| 627 | sis->dest_byte_count = reply_cm->rx_byte_count64; |
| 628 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 629 | sis->reason = reason; |
| 630 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 631 | /* |
| 632 | * Get the time increment since our last sync. |
| 633 | */ |
| 634 | sis->delta_jiffies = now_jiffies - c->last_sync_jiffies; |
| 635 | c->last_sync_jiffies = now_jiffies; |
| 636 | } |
| 637 | |
| 638 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 639 | * sfe_ipv4_free_connection_rcu() |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 640 | * Called at RCU qs state to free the connection object. |
| 641 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 642 | static void sfe_ipv4_free_connection_rcu(struct rcu_head *head) |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 643 | { |
| 644 | struct sfe_ipv4_connection *c; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 645 | struct udp_sock *up; |
| 646 | struct sock *sk; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * We dont need spin lock as the connection is already removed from link list |
| 650 | */ |
| 651 | c = container_of(head, struct sfe_ipv4_connection, rcu); |
| 652 | |
| 653 | BUG_ON(!c->removed); |
| 654 | |
| 655 | DEBUG_TRACE("%px: connecton has been deleted\n", c); |
| 656 | |
| 657 | /* |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 658 | * Decrease the refcount taken in function sfe_ipv4_create_rule(), |
| 659 | * during call of __udp4_lib_lookup() |
| 660 | */ |
| 661 | up = c->reply_match->up; |
| 662 | if (up) { |
| 663 | sk = (struct sock *)up; |
| 664 | sock_put(sk); |
| 665 | } |
| 666 | |
| 667 | /* |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 668 | * Release our hold of the source and dest devices and free the memory |
| 669 | * for our connection objects. |
| 670 | */ |
| 671 | dev_put(c->original_dev); |
| 672 | dev_put(c->reply_dev); |
| 673 | kfree(c->original_match); |
| 674 | kfree(c->reply_match); |
| 675 | kfree(c); |
| 676 | } |
| 677 | |
| 678 | /* |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 679 | * sfe_ipv4_sync_status() |
| 680 | * update a connection status to its connection manager. |
| 681 | * |
| 682 | * si: the ipv4 context |
| 683 | * c: which connection to be notified |
| 684 | * reason: what kind of notification: flush, stats or destroy |
| 685 | */ |
| 686 | void sfe_ipv4_sync_status(struct sfe_ipv4 *si, |
| 687 | struct sfe_ipv4_connection *c, |
| 688 | sfe_sync_reason_t reason) |
| 689 | { |
| 690 | struct sfe_connection_sync sis; |
| 691 | u64 now_jiffies; |
| 692 | sfe_sync_rule_callback_t sync_rule_callback; |
| 693 | |
| 694 | rcu_read_lock(); |
| 695 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 696 | if (!sync_rule_callback) { |
| 697 | rcu_read_unlock(); |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Generate a sync message and then sync. |
| 703 | */ |
| 704 | now_jiffies = get_jiffies_64(); |
| 705 | sfe_ipv4_gen_sync_connection(si, c, &sis, reason, now_jiffies); |
| 706 | sync_rule_callback(&sis); |
| 707 | |
| 708 | rcu_read_unlock(); |
| 709 | } |
| 710 | |
| 711 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 712 | * sfe_ipv4_flush_connection() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 713 | * Flush a connection and free all associated resources. |
| 714 | * |
| 715 | * We need to be called with bottom halves disabled locally as we need to acquire |
| 716 | * the connection hash lock and release it again. In general we're actually called |
| 717 | * from within a BH and so we're fine, but we're also called when connections are |
| 718 | * torn down. |
| 719 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 720 | void sfe_ipv4_flush_connection(struct sfe_ipv4 *si, |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 721 | struct sfe_ipv4_connection *c, |
| 722 | sfe_sync_reason_t reason) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 723 | { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 724 | BUG_ON(!c->removed); |
| 725 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 726 | this_cpu_inc(si->stats_pcpu->connection_flushes64); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 727 | sfe_ipv4_sync_status(si, c, reason); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 728 | |
| 729 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 730 | * Release our hold of the source and dest devices and free the memory |
| 731 | * for our connection objects. |
| 732 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 733 | call_rcu(&c->rcu, sfe_ipv4_free_connection_rcu); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 737 | * sfe_ipv4_service_class_stats_inc() |
| 738 | * Increment per cpu per service class stats. |
| 739 | */ |
| 740 | void sfe_ipv4_service_class_stats_inc(struct sfe_ipv4 *si, uint8_t sid, uint64_t bytes) |
| 741 | { |
| 742 | struct sfe_ipv4_service_class_stats_db *sc_stats_db = this_cpu_ptr(si->stats_pcpu_psc); |
| 743 | struct sfe_ipv4_per_service_class_stats *sc_stats = &sc_stats_db->psc_stats[sid]; |
| 744 | |
| 745 | write_seqcount_begin(&sc_stats->seq); |
| 746 | sc_stats->tx_bytes += bytes; |
| 747 | sc_stats->tx_packets++; |
| 748 | write_seqcount_end(&sc_stats->seq); |
| 749 | } |
| 750 | |
| 751 | /* |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 752 | * sfe_ipv4_exception_stats_inc() |
| 753 | * Increment exception stats. |
| 754 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 755 | 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] | 756 | { |
| 757 | struct sfe_ipv4_stats *stats = this_cpu_ptr(si->stats_pcpu); |
| 758 | stats->exception_events64[reason]++; |
| 759 | stats->packets_not_forwarded64++; |
| 760 | } |
| 761 | |
| 762 | /* |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 763 | * sfe_ipv4_is_loal_ip() |
| 764 | * Returns true if IP is local; returns false otherwise. |
| 765 | */ |
| 766 | static bool sfe_ipv4_is_local_ip(struct sfe_ipv4 *si, __be32 ip_addr) |
| 767 | { |
| 768 | struct net_device *dev; |
| 769 | |
| 770 | dev = ip_dev_find(&init_net, ip_addr); |
| 771 | if (dev) { |
| 772 | dev_put(dev); |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 780 | * sfe_ipv4_recv() |
Matthew McClintock | a8ad796 | 2014-01-16 16:49:30 -0600 | [diff] [blame] | 781 | * Handle packet receives and forwaring. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 782 | * |
| 783 | * Returns 1 if the packet is forwarded or 0 if it isn't. |
| 784 | */ |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 785 | int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb, struct sfe_l2_info *l2_info, bool tun_outer) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 786 | { |
| 787 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 788 | unsigned int len; |
| 789 | unsigned int tot_len; |
| 790 | unsigned int frag_off; |
| 791 | unsigned int ihl; |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 792 | bool sync_on_find; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 793 | bool ip_options; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 794 | struct iphdr *iph; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 795 | u32 protocol; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 796 | |
| 797 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 798 | * Check that we have space for an IP header here. |
| 799 | */ |
| 800 | len = skb->len; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 801 | if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 802 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 803 | DEBUG_TRACE("len: %u is too short\n", len); |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 808 | * Validate ip csum if necessary. If ip_summed is set to CHECKSUM_UNNECESSARY, it is assumed |
| 809 | * that the L3 checksum is validated by the Rx interface or the tunnel interface that has |
| 810 | * generated the packet. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 811 | */ |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 812 | iph = (struct iphdr *)skb->data; |
Ratheesh Kannoth | 43d64f8 | 2021-10-20 08:23:29 +0530 | [diff] [blame] | 813 | if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && (ip_fast_csum((u8 *)iph, iph->ihl))) { |
| 814 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_HEADER_CSUM_BAD); |
| 815 | |
| 816 | DEBUG_TRACE("Bad IPv4 header csum: 0x%x\n", iph->check); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Check that our "total length" is large enough for an IP header. |
| 822 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 823 | tot_len = ntohs(iph->tot_len); |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 824 | if (unlikely(tot_len < sizeof(struct iphdr))) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 825 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 826 | 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] | 827 | DEBUG_TRACE("tot_len: %u is too short\n", tot_len); |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Is our IP version wrong? |
| 833 | */ |
| 834 | if (unlikely(iph->version != 4)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 835 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NON_V4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 836 | DEBUG_TRACE("IP version: %u\n", iph->version); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Does our datagram fit inside the skb? |
| 842 | */ |
| 843 | if (unlikely(tot_len > len)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 844 | DEBUG_TRACE("tot_len: %u, exceeds len: %u\n", tot_len, len); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 845 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Do we have a non-initial fragment? |
Nicolas Costa | ac2979c | 2014-01-14 10:35:24 -0600 | [diff] [blame] | 851 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 852 | frag_off = ntohs(iph->frag_off); |
| 853 | if (unlikely(frag_off & IP_OFFSET)) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 854 | 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] | 855 | DEBUG_TRACE("non-initial fragment\n"); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * If we have a (first) fragment then mark it to cause any connection to flush. |
| 861 | */ |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 862 | sync_on_find = unlikely(frag_off & IP_MF) ? true : false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * Do we have any IP options? That's definite a slow path! If we do have IP |
| 866 | * options we need to recheck our header size. |
| 867 | */ |
| 868 | ihl = iph->ihl << 2; |
Ratheesh Kannoth | 741f799 | 2021-10-20 07:39:52 +0530 | [diff] [blame] | 869 | ip_options = unlikely(ihl != sizeof(struct iphdr)) ? true : false; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 870 | if (unlikely(ip_options)) { |
| 871 | if (unlikely(len < ihl)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 872 | |
| 873 | 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] | 874 | 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] | 875 | return 0; |
| 876 | } |
| 877 | |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 878 | sync_on_find = true; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | protocol = iph->protocol; |
| 882 | if (IPPROTO_UDP == protocol) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 883 | return sfe_ipv4_recv_udp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | if (IPPROTO_TCP == protocol) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 887 | return sfe_ipv4_recv_tcp(si, skb, dev, len, iph, ihl, sync_on_find, l2_info); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (IPPROTO_ICMP == protocol) { |
| 891 | return sfe_ipv4_recv_icmp(si, skb, dev, len, iph, ihl); |
| 892 | } |
| 893 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 894 | #ifdef SFE_GRE_TUN_ENABLE |
| 895 | if (IPPROTO_GRE == protocol) { |
Nitin Shetty | 2114a89 | 2022-01-28 20:03:56 +0530 | [diff] [blame] | 896 | return sfe_ipv4_recv_gre(si, skb, dev, len, iph, ihl, sync_on_find, l2_info, tun_outer); |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 897 | } |
| 898 | #endif |
| 899 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 900 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UNHANDLED_PROTOCOL); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 901 | |
| 902 | DEBUG_TRACE("not UDP, TCP or ICMP: %u\n", protocol); |
| 903 | return 0; |
| 904 | } |
| 905 | |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 906 | static void |
| 907 | sfe_ipv4_update_tcp_state(struct sfe_ipv4_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 908 | struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 909 | { |
| 910 | struct sfe_ipv4_connection_match *orig_cm; |
| 911 | struct sfe_ipv4_connection_match *repl_cm; |
| 912 | struct sfe_ipv4_tcp_connection_match *orig_tcp; |
| 913 | struct sfe_ipv4_tcp_connection_match *repl_tcp; |
| 914 | |
| 915 | orig_cm = c->original_match; |
| 916 | repl_cm = c->reply_match; |
| 917 | orig_tcp = &orig_cm->protocol_state.tcp; |
| 918 | repl_tcp = &repl_cm->protocol_state.tcp; |
| 919 | |
| 920 | /* update orig */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 921 | if (orig_tcp->max_win < msg->tcp_rule.flow_max_window) { |
| 922 | orig_tcp->max_win = msg->tcp_rule.flow_max_window; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 923 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 924 | if ((s32)(orig_tcp->end - msg->tcp_rule.flow_end) < 0) { |
| 925 | orig_tcp->end = msg->tcp_rule.flow_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 926 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 927 | if ((s32)(orig_tcp->max_end - msg->tcp_rule.flow_max_end) < 0) { |
| 928 | orig_tcp->max_end = msg->tcp_rule.flow_max_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* update reply */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 932 | if (repl_tcp->max_win < msg->tcp_rule.return_max_window) { |
| 933 | repl_tcp->max_win = msg->tcp_rule.return_max_window; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 934 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 935 | if ((s32)(repl_tcp->end - msg->tcp_rule.return_end) < 0) { |
| 936 | repl_tcp->end = msg->tcp_rule.return_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 937 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 938 | if ((s32)(repl_tcp->max_end - msg->tcp_rule.return_max_end) < 0) { |
| 939 | repl_tcp->max_end = msg->tcp_rule.return_max_end; |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* update match flags */ |
| 943 | orig_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 944 | repl_cm->flags &= ~SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 945 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
| 946 | |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 947 | orig_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 948 | repl_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | static void |
| 953 | sfe_ipv4_update_protocol_state(struct sfe_ipv4_connection *c, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 954 | struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 955 | { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 956 | switch (msg->tuple.protocol) { |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 957 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 958 | sfe_ipv4_update_tcp_state(c, msg); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | } |
| 962 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 963 | /* |
| 964 | * sfe_ipv4_match_entry_set_vlan() |
| 965 | */ |
| 966 | static void sfe_ipv4_match_entry_set_vlan( |
| 967 | struct sfe_ipv4_connection_match *cm, |
| 968 | u32 primary_ingress_vlan_tag, |
| 969 | u32 primary_egress_vlan_tag, |
| 970 | u32 secondary_ingress_vlan_tag, |
| 971 | u32 secondary_egress_vlan_tag) |
| 972 | { |
| 973 | u16 tpid; |
| 974 | /* |
| 975 | * Prevent stacking header counts when updating. |
| 976 | */ |
| 977 | cm->ingress_vlan_hdr_cnt = 0; |
| 978 | cm->egress_vlan_hdr_cnt = 0; |
| 979 | memset(cm->ingress_vlan_hdr, 0, sizeof(cm->ingress_vlan_hdr)); |
| 980 | memset(cm->egress_vlan_hdr, 0, sizeof(cm->egress_vlan_hdr)); |
| 981 | |
| 982 | /* |
| 983 | * vlan_hdr[0] corresponds to outer tag |
| 984 | * vlan_hdr[1] corresponds to inner tag |
| 985 | * Extract the vlan information (tpid and tci) from rule message |
| 986 | */ |
| 987 | if ((primary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 988 | tpid = (u16)(primary_ingress_vlan_tag >> 16); |
| 989 | cm->ingress_vlan_hdr[0].tpid = ntohs(tpid); |
| 990 | cm->ingress_vlan_hdr[0].tci = (u16)primary_ingress_vlan_tag; |
| 991 | cm->ingress_vlan_hdr_cnt++; |
| 992 | } |
| 993 | |
| 994 | if ((secondary_ingress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 995 | tpid = (u16)(secondary_ingress_vlan_tag >> 16); |
| 996 | cm->ingress_vlan_hdr[1].tpid = ntohs(tpid); |
| 997 | cm->ingress_vlan_hdr[1].tci = (u16)secondary_ingress_vlan_tag; |
| 998 | cm->ingress_vlan_hdr_cnt++; |
| 999 | } |
| 1000 | |
| 1001 | if ((primary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1002 | tpid = (u16)(primary_egress_vlan_tag >> 16); |
| 1003 | cm->egress_vlan_hdr[0].tpid = ntohs(tpid); |
| 1004 | cm->egress_vlan_hdr[0].tci = (u16)primary_egress_vlan_tag; |
| 1005 | cm->egress_vlan_hdr_cnt++; |
| 1006 | } |
| 1007 | |
| 1008 | if ((secondary_egress_vlan_tag & VLAN_VID_MASK) != SFE_VLAN_ID_NOT_CONFIGURED) { |
| 1009 | tpid = (u16)(secondary_egress_vlan_tag >> 16); |
| 1010 | cm->egress_vlan_hdr[1].tpid = ntohs(tpid); |
| 1011 | cm->egress_vlan_hdr[1].tci = (u16)secondary_egress_vlan_tag; |
| 1012 | cm->egress_vlan_hdr_cnt++; |
| 1013 | } |
| 1014 | } |
| 1015 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1016 | void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg) |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1017 | { |
| 1018 | struct sfe_ipv4_connection *c; |
| 1019 | struct sfe_ipv4 *si = &__si; |
| 1020 | |
| 1021 | spin_lock_bh(&si->lock); |
| 1022 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1023 | c = sfe_ipv4_find_connection(si, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1024 | msg->tuple.protocol, |
| 1025 | msg->tuple.flow_ip, |
| 1026 | msg->tuple.flow_ident, |
| 1027 | msg->tuple.return_ip, |
| 1028 | msg->tuple.return_ident); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1029 | if (c != NULL) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1030 | sfe_ipv4_update_protocol_state(c, msg); |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | spin_unlock_bh(&si->lock); |
| 1034 | } |
| 1035 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1036 | /* |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1037 | * sfe_ipv4_xmit_eth_type_check() |
| 1038 | * Checking if MAC header has to be written. |
| 1039 | */ |
| 1040 | static inline bool sfe_ipv4_xmit_eth_type_check(struct net_device *dev, u32 cm_flags) |
| 1041 | { |
| 1042 | if (!(dev->flags & IFF_NOARP)) { |
| 1043 | return true; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * For PPPoE, since we are now supporting PPPoE encapsulation, we are writing L2 header. |
| 1048 | */ |
| 1049 | if (unlikely(cm_flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) { |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1057 | * sfe_ipv4_create_rule() |
| 1058 | * Create a forwarding rule. |
| 1059 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1060 | int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1061 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1062 | struct sfe_ipv4 *si = &__si; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1063 | struct sfe_ipv4_connection *c, *c_old; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1064 | struct sfe_ipv4_connection_match *original_cm; |
| 1065 | struct sfe_ipv4_connection_match *reply_cm; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1066 | struct net_device *dest_dev; |
| 1067 | struct net_device *src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1068 | struct sfe_ipv4_5tuple *tuple = &msg->tuple; |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1069 | s32 flow_interface_num = msg->conn_rule.flow_top_interface_num; |
| 1070 | s32 return_interface_num = msg->conn_rule.return_top_interface_num; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1071 | struct net *net; |
| 1072 | struct sock *sk; |
| 1073 | unsigned int src_if_idx; |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 1074 | u32 flow_sawf_tag; |
| 1075 | u32 return_sawf_tag; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1076 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1077 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) { |
| 1078 | flow_interface_num = msg->conn_rule.flow_interface_num; |
| 1079 | } |
| 1080 | |
| 1081 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) { |
| 1082 | return_interface_num = msg->conn_rule.return_interface_num; |
| 1083 | } |
| 1084 | |
| 1085 | src_dev = dev_get_by_index(&init_net, flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1086 | if (!src_dev) { |
| 1087 | 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] | 1088 | flow_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1089 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | |
Suruchi Suman | c1a4a61 | 2021-10-21 14:50:23 +0530 | [diff] [blame] | 1093 | dest_dev = dev_get_by_index(&init_net, return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1094 | if (!dest_dev) { |
| 1095 | 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] | 1096 | return_interface_num); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1097 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1098 | dev_put(src_dev); |
| 1099 | return -EINVAL; |
| 1100 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1101 | |
Matthew McClintock | 389b42a | 2014-09-24 14:05:51 -0500 | [diff] [blame] | 1102 | if (unlikely((dest_dev->reg_state != NETREG_REGISTERED) || |
| 1103 | (src_dev->reg_state != NETREG_REGISTERED))) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1104 | dev_put(src_dev); |
| 1105 | dev_put(dest_dev); |
| 1106 | DEBUG_WARN("%px: src_dev=%s and dest_dev=%s are unregistered\n", msg, |
| 1107 | src_dev->name, dest_dev->name); |
| 1108 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Matthew McClintock | 389b42a | 2014-09-24 14:05:51 -0500 | [diff] [blame] | 1109 | return -EINVAL; |
| 1110 | } |
| 1111 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1112 | /* |
| 1113 | * Allocate the various connection tracking objects. |
| 1114 | */ |
| 1115 | c = (struct sfe_ipv4_connection *)kmalloc(sizeof(struct sfe_ipv4_connection), GFP_ATOMIC); |
| 1116 | if (unlikely(!c)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1117 | DEBUG_WARN("%px: memory allocation of connection entry failed\n", msg); |
| 1118 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
| 1119 | dev_put(src_dev); |
| 1120 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1121 | return -ENOMEM; |
| 1122 | } |
| 1123 | |
| 1124 | original_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 1125 | if (unlikely(!original_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1126 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
| 1127 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1128 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1129 | dev_put(src_dev); |
| 1130 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1131 | return -ENOMEM; |
| 1132 | } |
| 1133 | |
| 1134 | reply_cm = (struct sfe_ipv4_connection_match *)kmalloc(sizeof(struct sfe_ipv4_connection_match), GFP_ATOMIC); |
| 1135 | if (unlikely(!reply_cm)) { |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1136 | DEBUG_WARN("%px: memory allocation of connection match entry failed\n", msg); |
| 1137 | this_cpu_inc(si->stats_pcpu->connection_create_failures64); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1138 | kfree(original_cm); |
| 1139 | kfree(c); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1140 | dev_put(src_dev); |
| 1141 | dev_put(dest_dev); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1142 | return -ENOMEM; |
| 1143 | } |
| 1144 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1145 | this_cpu_inc(si->stats_pcpu->connection_create_requests64); |
| 1146 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1147 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1148 | |
| 1149 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1150 | * Check to see if there is already a flow that matches the rule we're |
| 1151 | * 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] | 1152 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1153 | c_old = sfe_ipv4_find_connection(si, |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1154 | msg->tuple.protocol, |
| 1155 | msg->tuple.flow_ip, |
| 1156 | msg->tuple.flow_ident, |
| 1157 | msg->tuple.return_ip, |
| 1158 | msg->tuple.return_ident); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1159 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1160 | if (c_old != NULL) { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1161 | this_cpu_inc(si->stats_pcpu->connection_create_collisions64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1162 | |
| 1163 | /* |
Nicolas Costa | 436926b | 2014-01-14 10:36:22 -0600 | [diff] [blame] | 1164 | * If we already have the flow then it's likely that this |
| 1165 | * request to create the connection rule contains more |
| 1166 | * up-to-date information. Check and update accordingly. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1167 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1168 | sfe_ipv4_update_protocol_state(c, msg); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1169 | spin_unlock_bh(&si->lock); |
| 1170 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1171 | kfree(reply_cm); |
| 1172 | kfree(original_cm); |
| 1173 | kfree(c); |
| 1174 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1175 | dev_put(src_dev); |
| 1176 | dev_put(dest_dev); |
| 1177 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1178 | DEBUG_TRACE("%px: connection already exists - p:%d\n" |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1179 | " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n", |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1180 | msg, tuple->protocol, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1181 | src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1182 | dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident)); |
| 1183 | |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 1184 | return -EADDRINUSE; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1188 | * Fill in the "original" direction connection matching object. |
| 1189 | * Note that the transmit MAC address is "dest_mac_xlate" because |
| 1190 | * we always know both ends of a connection by their translated |
| 1191 | * addresses and not their public addresses. |
| 1192 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1193 | original_cm->match_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1194 | original_cm->match_protocol = tuple->protocol; |
| 1195 | original_cm->match_src_ip = tuple->flow_ip; |
Suruchi Suman | 66609a7 | 2022-01-20 02:34:25 +0530 | [diff] [blame] | 1196 | original_cm->match_src_port = netif_is_vxlan(src_dev) ? 0 : tuple->flow_ident; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1197 | original_cm->match_dest_ip = tuple->return_ip; |
| 1198 | original_cm->match_dest_port = tuple->return_ident; |
| 1199 | |
| 1200 | original_cm->xlate_src_ip = msg->conn_rule.flow_ip_xlate; |
| 1201 | original_cm->xlate_src_port = msg->conn_rule.flow_ident_xlate; |
| 1202 | original_cm->xlate_dest_ip = msg->conn_rule.return_ip_xlate; |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1203 | original_cm->xlate_dest_port = msg->conn_rule.return_ident_xlate; |
| 1204 | |
| 1205 | if (tuple->protocol == IPPROTO_GRE) { |
| 1206 | /* |
| 1207 | * the PPTP is 4 tuple lookup. |
| 1208 | * During th rule lookup destination call id from packet |
| 1209 | * is matched against destination port in cm. |
| 1210 | */ |
| 1211 | original_cm->match_src_port = 0; |
| 1212 | original_cm->xlate_src_port = 0; |
| 1213 | } |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1214 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1215 | atomic_set(&original_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1216 | original_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1217 | atomic_set(&original_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1218 | original_cm->rx_byte_count64 = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1219 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1220 | original_cm->xmit_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1221 | original_cm->xmit_dev_mtu = msg->conn_rule.return_mtu; |
| 1222 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1223 | original_cm->connection = c; |
| 1224 | original_cm->counter_match = reply_cm; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1225 | original_cm->l2_hdr_size = 0; |
| 1226 | original_cm->flags = 0; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1227 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1228 | /* |
| 1229 | * UDP Socket is valid only in decap direction. |
| 1230 | */ |
| 1231 | RCU_INIT_POINTER(original_cm->up, NULL); |
| 1232 | |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 1233 | if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) { |
| 1234 | original_cm->mark = msg->mark_rule.flow_mark; |
| 1235 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK; |
| 1236 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1237 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1238 | original_cm->priority = msg->qos_rule.flow_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1239 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1240 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1241 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1242 | original_cm->dscp = msg->dscp_rule.flow_dscp << SFE_IPV4_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1243 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1244 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1245 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1246 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW; |
| 1247 | } |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 1248 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_FLOW_TRANSMIT_FAST) { |
| 1249 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION; |
| 1250 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1251 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1252 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 1253 | * Mark SAWF metadata if the sawf tag is valid and set. |
| 1254 | */ |
| 1255 | original_cm->sawf_valid = false; |
| 1256 | flow_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.flow_mark); |
| 1257 | if (likely(SFE_SAWF_TAG_IS_VALID(flow_sawf_tag))) { |
| 1258 | original_cm->mark = msg->sawf_rule.flow_mark; |
| 1259 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK; |
| 1260 | original_cm->sawf_valid = true; |
| 1261 | } |
| 1262 | |
| 1263 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1264 | * Add VLAN rule to original_cm |
| 1265 | */ |
| 1266 | if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) { |
| 1267 | struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule; |
| 1268 | struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule; |
| 1269 | sfe_ipv4_match_entry_set_vlan(original_cm, |
| 1270 | vlan_primary_rule->ingress_vlan_tag, |
| 1271 | vlan_primary_rule->egress_vlan_tag, |
| 1272 | vlan_secondary_rule->ingress_vlan_tag, |
| 1273 | vlan_secondary_rule->egress_vlan_tag); |
| 1274 | |
| 1275 | if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE) && |
| 1276 | original_cm->egress_vlan_hdr_cnt > 0) { |
| 1277 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG; |
| 1278 | original_cm->l2_hdr_size += original_cm->egress_vlan_hdr_cnt * VLAN_HLEN; |
| 1279 | } |
| 1280 | } |
| 1281 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1282 | if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, original_cm->match_dest_ip)) { |
| 1283 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 1284 | } |
| 1285 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1286 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1287 | original_cm->flow_cookie = 0; |
| 1288 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1289 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1290 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1291 | original_cm->flow_accel = msg->direction_rule.flow_accel; |
| 1292 | } else { |
| 1293 | original_cm->flow_accel = 1; |
| 1294 | } |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1295 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1296 | /* |
| 1297 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1298 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1299 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1300 | * are sending directly to the destination interface that supports it. |
| 1301 | */ |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1302 | if (likely(dest_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(dest_dev)) { |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1303 | if ((msg->conn_rule.return_top_interface_num == msg->conn_rule.return_interface_num) || |
| 1304 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_RETURN_BOTTOM_INTERFACE)) { |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1305 | |
| 1306 | /* |
| 1307 | * Dont enable CSUM offload |
| 1308 | */ |
| 1309 | #if 0 |
Suruchi Suman | f207718 | 2022-01-13 21:35:23 +0530 | [diff] [blame] | 1310 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1311 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1312 | } |
| 1313 | } |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1314 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1315 | reply_cm->l2_hdr_size = 0; |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 1316 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) { |
| 1317 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK; |
| 1318 | } |
| 1319 | |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 1320 | reply_cm->flags = 0; |
| 1321 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1322 | /* |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 1323 | * Adding PPPoE parameters to original and reply entries based on the direction where |
| 1324 | * PPPoE header is valid in ECM rule. |
| 1325 | * |
| 1326 | * If PPPoE is valid in flow direction (from interface is PPPoE), then |
| 1327 | * original cm will have PPPoE at ingress (strip PPPoE header) |
| 1328 | * reply cm will have PPPoE at egress (add PPPoE header) |
| 1329 | * |
| 1330 | * If PPPoE is valid in return direction (to interface is PPPoE), then |
| 1331 | * original cm will have PPPoE at egress (add PPPoE header) |
| 1332 | * reply cm will have PPPoE at ingress (strip PPPoE header) |
| 1333 | */ |
| 1334 | if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_DECAP_VALID) { |
| 1335 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP; |
| 1336 | original_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id; |
| 1337 | ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac); |
| 1338 | |
| 1339 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1340 | reply_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 1341 | reply_cm->pppoe_session_id = msg->pppoe_rule.flow_pppoe_session_id; |
| 1342 | ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.flow_pppoe_remote_mac); |
| 1343 | } |
| 1344 | |
| 1345 | if (msg->valid_flags & SFE_RULE_CREATE_PPPOE_ENCAP_VALID) { |
| 1346 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1347 | original_cm->l2_hdr_size += SFE_PPPOE_SESSION_HEADER_SIZE; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 1348 | original_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id; |
| 1349 | ether_addr_copy(original_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac); |
| 1350 | |
| 1351 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP; |
| 1352 | reply_cm->pppoe_session_id = msg->pppoe_rule.return_pppoe_session_id; |
| 1353 | ether_addr_copy(reply_cm->pppoe_remote_mac, msg->pppoe_rule.return_pppoe_remote_mac); |
| 1354 | } |
| 1355 | |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 1356 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_SRC_INTERFACE_CHECK) { |
| 1357 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK; |
| 1358 | } |
| 1359 | |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 1360 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1361 | * For the non-arp interface, we don't write L2 HDR. |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1362 | */ |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1363 | if (sfe_ipv4_xmit_eth_type_check(dest_dev, original_cm->flags)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1364 | |
| 1365 | /* |
| 1366 | * Check whether the rule has configured a specific source MAC address to use. |
| 1367 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1368 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1369 | |
| 1370 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1371 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->conn_rule.flow_mac); |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1372 | } else { |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1373 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1374 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_RETURN_VALID)) { |
| 1375 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.return_src_mac); |
| 1376 | } else { |
| 1377 | ether_addr_copy((u8 *)original_cm->xmit_src_mac, (u8 *)dest_dev->dev_addr); |
| 1378 | } |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | ether_addr_copy((u8 *)original_cm->xmit_dest_mac, (u8 *)msg->conn_rule.return_mac); |
| 1382 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1383 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1384 | original_cm->l2_hdr_size += ETH_HLEN; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1385 | |
| 1386 | /* |
| 1387 | * If our dev writes Ethernet headers then we can write a really fast |
| 1388 | * version. |
| 1389 | */ |
| 1390 | if (dest_dev->header_ops) { |
| 1391 | if (dest_dev->header_ops->create == eth_header) { |
| 1392 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1393 | } |
| 1394 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Fill in the "reply" direction connection matching object. |
| 1399 | */ |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1400 | reply_cm->match_dev = dest_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1401 | reply_cm->match_protocol = tuple->protocol; |
| 1402 | reply_cm->match_src_ip = msg->conn_rule.return_ip_xlate; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1403 | |
| 1404 | /* |
| 1405 | * Keep source port as 0 for VxLAN tunnels. |
| 1406 | */ |
| 1407 | if (netif_is_vxlan(src_dev) || netif_is_vxlan(dest_dev)) { |
| 1408 | reply_cm->match_src_port = 0; |
| 1409 | } else { |
| 1410 | reply_cm->match_src_port = msg->conn_rule.return_ident_xlate; |
| 1411 | } |
| 1412 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1413 | reply_cm->match_dest_ip = msg->conn_rule.flow_ip_xlate; |
| 1414 | reply_cm->match_dest_port = msg->conn_rule.flow_ident_xlate; |
| 1415 | |
| 1416 | reply_cm->xlate_src_ip = tuple->return_ip; |
| 1417 | reply_cm->xlate_src_port = tuple->return_ident; |
| 1418 | reply_cm->xlate_dest_ip = tuple->flow_ip; |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1419 | reply_cm->xlate_dest_port = tuple->flow_ident; |
| 1420 | |
| 1421 | if (tuple->protocol == IPPROTO_GRE) { |
| 1422 | /* |
| 1423 | * the PPTP is 4 tuple lookup. |
| 1424 | * During th rule lookup destination call id from packet |
| 1425 | * is matched against destination port in cm. |
| 1426 | */ |
| 1427 | reply_cm->match_src_port = 0; |
| 1428 | reply_cm->xlate_src_port = 0; |
| 1429 | } |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1430 | |
| 1431 | atomic_set(&reply_cm->rx_packet_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1432 | reply_cm->rx_packet_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1433 | atomic_set(&reply_cm->rx_byte_count, 0); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1434 | reply_cm->rx_byte_count64 = 0; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1435 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1436 | reply_cm->xmit_dev = src_dev; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1437 | reply_cm->xmit_dev_mtu = msg->conn_rule.flow_mtu; |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1438 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1439 | reply_cm->connection = c; |
| 1440 | reply_cm->counter_match = original_cm; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 1441 | |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 1442 | if (msg->valid_flags & SFE_RULE_CREATE_MARK_VALID) { |
| 1443 | reply_cm->mark = msg->mark_rule.return_mark; |
| 1444 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK; |
| 1445 | } |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1446 | if (msg->valid_flags & SFE_RULE_CREATE_QOS_VALID) { |
| 1447 | reply_cm->priority = msg->qos_rule.return_qos_tag; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1448 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK; |
| 1449 | } |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1450 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1451 | if (msg->valid_flags & SFE_RULE_CREATE_DSCP_MARKING_VALID) { |
| 1452 | reply_cm->dscp = msg->dscp_rule.return_dscp << SFE_IPV4_DSCP_SHIFT; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 1453 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK; |
| 1454 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1455 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1456 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW; |
| 1457 | } |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 1458 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_RETURN_TRANSMIT_FAST) { |
| 1459 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION; |
| 1460 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1461 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1462 | if ((IPPROTO_GRE == tuple->protocol) && !sfe_ipv4_is_local_ip(si, reply_cm->match_dest_ip)) { |
| 1463 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 1464 | } |
| 1465 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1466 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 1467 | * Mark SAWF metadata in reply match if the sawf tag is valid. |
| 1468 | */ |
| 1469 | reply_cm->sawf_valid = false; |
| 1470 | return_sawf_tag = SFE_GET_SAWF_TAG(msg->sawf_rule.return_mark); |
| 1471 | if (likely(SFE_SAWF_TAG_IS_VALID(return_sawf_tag))) { |
| 1472 | reply_cm->mark = msg->sawf_rule.return_mark; |
| 1473 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_MARK; |
| 1474 | reply_cm->sawf_valid = true; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1478 | * Setup UDP Socket if found to be valid for decap. |
| 1479 | */ |
| 1480 | RCU_INIT_POINTER(reply_cm->up, NULL); |
| 1481 | net = dev_net(reply_cm->match_dev); |
| 1482 | src_if_idx = src_dev->ifindex; |
| 1483 | |
| 1484 | rcu_read_lock(); |
| 1485 | |
| 1486 | /* |
| 1487 | * Look for the associated sock object. |
| 1488 | * __udp4_lib_lookup() holds a reference for this sock object, |
| 1489 | * which will be released in sfe_ipv4_free_connection_rcu() |
| 1490 | */ |
| 1491 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 1492 | sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port, |
| 1493 | reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, &udp_table); |
| 1494 | #else |
| 1495 | sk = __udp4_lib_lookup(net, reply_cm->match_dest_ip, reply_cm->match_dest_port, |
| 1496 | reply_cm->xlate_src_ip, reply_cm->xlate_src_port, src_if_idx, 0, &udp_table, NULL); |
| 1497 | #endif |
| 1498 | |
| 1499 | rcu_read_unlock(); |
| 1500 | |
| 1501 | /* |
| 1502 | * We set the UDP sock pointer as valid only for decap direction. |
| 1503 | */ |
| 1504 | if (sk && udp_sk(sk)->encap_type) { |
| 1505 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 1506 | if (!atomic_add_unless(&sk->sk_refcnt, 1, 0)) { |
| 1507 | #else |
| 1508 | if (!refcount_inc_not_zero(&sk->sk_refcnt)) { |
| 1509 | #endif |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1510 | spin_unlock_bh(&si->lock); |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 1511 | kfree(reply_cm); |
| 1512 | kfree(original_cm); |
| 1513 | kfree(c); |
| 1514 | |
| 1515 | DEBUG_TRACE("%px: sfe: unable to take reference for socket(%px) p:%d\n" |
| 1516 | " s: %s:%pM:%pI4:%u, d: %s:%pM:%pI4:%u\n", |
| 1517 | msg, sk, tuple->protocol, |
| 1518 | src_dev->name, msg->conn_rule.flow_mac, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1519 | dest_dev->name, msg->conn_rule.return_mac, &tuple->return_ip, ntohs(tuple->return_ident)); |
| 1520 | |
| 1521 | dev_put(src_dev); |
| 1522 | dev_put(dest_dev); |
| 1523 | |
| 1524 | return -ESHUTDOWN; |
| 1525 | } |
| 1526 | |
| 1527 | rcu_assign_pointer(reply_cm->up, udp_sk(sk)); |
| 1528 | |
| 1529 | DEBUG_INFO("%px: Sock(%px) lookup success with reply_cm direction\n", msg, sk); |
| 1530 | DEBUG_INFO("%px: SFE connection -\n" |
| 1531 | " s: %s:%pI4(%pI4):%u(%u)\n" |
| 1532 | " d: %s:%pI4(%pI4):%u(%u)\n", |
| 1533 | msg, reply_cm->match_dev->name, &reply_cm->match_src_ip, &reply_cm->xlate_src_ip, |
| 1534 | ntohs(reply_cm->match_src_port), ntohs(reply_cm->xlate_src_port), |
| 1535 | reply_cm->xmit_dev->name, &reply_cm->match_dest_ip, &reply_cm->xlate_dest_ip, |
| 1536 | ntohs(reply_cm->match_dest_port), ntohs(reply_cm->xlate_dest_port)); |
| 1537 | } |
| 1538 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1539 | /* |
| 1540 | * Add VLAN rule to reply_cm |
| 1541 | */ |
| 1542 | if (msg->valid_flags & SFE_RULE_CREATE_VLAN_VALID) { |
| 1543 | struct sfe_vlan_rule *vlan_primary_rule = &msg->vlan_primary_rule; |
| 1544 | struct sfe_vlan_rule *vlan_secondary_rule = &msg->vlan_secondary_rule; |
| 1545 | sfe_ipv4_match_entry_set_vlan(reply_cm, |
| 1546 | vlan_primary_rule->egress_vlan_tag, |
| 1547 | vlan_primary_rule->ingress_vlan_tag, |
| 1548 | vlan_secondary_rule->egress_vlan_tag, |
| 1549 | vlan_secondary_rule->ingress_vlan_tag); |
| 1550 | |
| 1551 | if ((msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE) && |
| 1552 | reply_cm->egress_vlan_hdr_cnt > 0) { |
| 1553 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG; |
| 1554 | reply_cm->l2_hdr_size += reply_cm->egress_vlan_hdr_cnt * VLAN_HLEN; |
| 1555 | } |
| 1556 | } |
| 1557 | |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1558 | /* |
| 1559 | * the net_protocol handler will be used only in decap path |
| 1560 | * for non passthrough case. |
| 1561 | */ |
| 1562 | original_cm->proto = NULL; |
| 1563 | reply_cm->proto = NULL; |
| 1564 | |
| 1565 | #ifdef SFE_GRE_TUN_ENABLE |
| 1566 | if ((IPPROTO_GRE == tuple->protocol) && !(reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH)) { |
| 1567 | rcu_read_lock(); |
| 1568 | reply_cm->proto = rcu_dereference(inet_protos[IPPROTO_GRE]); |
| 1569 | rcu_read_unlock(); |
| 1570 | |
| 1571 | if (unlikely(!reply_cm->proto)) { |
| 1572 | kfree(reply_cm); |
| 1573 | kfree(original_cm); |
| 1574 | kfree(c); |
| 1575 | dev_put(src_dev); |
| 1576 | dev_put(dest_dev); |
| 1577 | DEBUG_WARN("sfe: GRE proto handler is not registered\n"); |
| 1578 | return -EPERM; |
| 1579 | } |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 1583 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 1584 | reply_cm->flow_cookie = 0; |
| 1585 | #endif |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1586 | #ifdef CONFIG_XFRM |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1587 | if (msg->valid_flags & SFE_RULE_CREATE_DIRECTION_VALID) { |
| 1588 | reply_cm->flow_accel = msg->direction_rule.return_accel; |
| 1589 | } else { |
| 1590 | reply_cm->flow_accel = 1; |
| 1591 | } |
| 1592 | |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 1593 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1594 | /* |
| 1595 | * If l2_features are disabled and flow uses l2 features such as macvlan/bridge/pppoe/vlan, |
| 1596 | * bottom interfaces are expected to be disabled in the flow rule and always top interfaces |
| 1597 | * are used. In such cases, do not use HW csum offload. csum offload is used only when we |
| 1598 | * are sending directly to the destination interface that supports it. |
| 1599 | */ |
Nitin Shetty | e6ed5b5 | 2021-12-27 14:50:11 +0530 | [diff] [blame] | 1600 | if (likely(src_dev->features & NETIF_F_HW_CSUM) && sfe_dev_has_hw_csum(src_dev)) { |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1601 | if ((msg->conn_rule.flow_top_interface_num == msg->conn_rule.flow_interface_num) || |
| 1602 | (msg->rule_flags & SFE_RULE_CREATE_FLAG_USE_FLOW_BOTTOM_INTERFACE)) { |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1603 | /* |
| 1604 | * Dont enable CSUM offload |
| 1605 | */ |
| 1606 | #if 0 |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1607 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD; |
Ratheesh Kannoth | 4844553 | 2022-02-07 16:19:00 +0530 | [diff] [blame] | 1608 | #endif |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 1609 | } |
| 1610 | } |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1611 | |
| 1612 | /* |
Ken Zhu | bbf4965 | 2021-09-12 15:33:09 -0700 | [diff] [blame] | 1613 | * For the non-arp interface, we don't write L2 HDR. |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1614 | */ |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 1615 | if (sfe_ipv4_xmit_eth_type_check(src_dev, reply_cm->flags)) { |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1616 | |
| 1617 | /* |
| 1618 | * Check whether the rule has configured a specific source MAC address to use. |
| 1619 | * This is needed when virtual L3 interfaces such as br-lan, macvlan, vlan are used during egress |
| 1620 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1621 | |
| 1622 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_BRIDGE_FLOW) { |
| 1623 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->conn_rule.return_mac); |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1624 | } else { |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1625 | if ((msg->valid_flags & SFE_RULE_CREATE_SRC_MAC_VALID) && |
| 1626 | (msg->src_mac_rule.mac_valid_flags & SFE_SRC_MAC_FLOW_VALID)) { |
| 1627 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)msg->src_mac_rule.flow_src_mac); |
| 1628 | } else { |
| 1629 | ether_addr_copy((u8 *)reply_cm->xmit_src_mac, (u8 *)src_dev->dev_addr); |
| 1630 | } |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1631 | } |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 1632 | |
Ratheesh Kannoth | 29140aa | 2021-10-20 08:25:02 +0530 | [diff] [blame] | 1633 | ether_addr_copy((u8 *)reply_cm->xmit_dest_mac, (u8 *)msg->conn_rule.flow_mac); |
| 1634 | |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1635 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR; |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1636 | reply_cm->l2_hdr_size += ETH_HLEN; |
Matthew McClintock | db5ac51 | 2014-01-16 17:01:40 -0600 | [diff] [blame] | 1637 | |
| 1638 | /* |
| 1639 | * If our dev writes Ethernet headers then we can write a really fast |
| 1640 | * version. |
| 1641 | */ |
| 1642 | if (src_dev->header_ops) { |
| 1643 | if (src_dev->header_ops->create == eth_header) { |
| 1644 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR; |
| 1645 | } |
| 1646 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1649 | if ((tuple->return_ip != msg->conn_rule.return_ip_xlate) || |
| 1650 | (tuple->return_ident != msg->conn_rule.return_ident_xlate)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1651 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 1652 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 1653 | } |
| 1654 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1655 | if ((tuple->flow_ip != msg->conn_rule.flow_ip_xlate) || |
| 1656 | (tuple->flow_ident != msg->conn_rule.flow_ident_xlate)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1657 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC; |
| 1658 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST; |
| 1659 | } |
| 1660 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1661 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1662 | * Initialize the protocol-specific information that we track. |
| 1663 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1664 | switch (tuple->protocol) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1665 | case IPPROTO_TCP: |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1666 | original_cm->protocol_state.tcp.win_scale = msg->tcp_rule.flow_window_scale; |
| 1667 | original_cm->protocol_state.tcp.max_win = msg->tcp_rule.flow_max_window ? msg->tcp_rule.flow_max_window : 1; |
| 1668 | original_cm->protocol_state.tcp.end = msg->tcp_rule.flow_end; |
| 1669 | original_cm->protocol_state.tcp.max_end = msg->tcp_rule.flow_max_end; |
| 1670 | |
| 1671 | reply_cm->protocol_state.tcp.win_scale = msg->tcp_rule.return_window_scale; |
| 1672 | reply_cm->protocol_state.tcp.max_win = msg->tcp_rule.return_max_window ? msg->tcp_rule.return_max_window : 1; |
| 1673 | reply_cm->protocol_state.tcp.end = msg->tcp_rule.return_end; |
| 1674 | reply_cm->protocol_state.tcp.max_end = msg->tcp_rule.return_max_end; |
| 1675 | |
| 1676 | if (msg->rule_flags & SFE_RULE_CREATE_FLAG_NO_SEQ_CHECK) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1677 | original_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1678 | reply_cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK; |
| 1679 | } |
| 1680 | break; |
| 1681 | } |
| 1682 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1683 | /* |
| 1684 | * Fill in the ipv4_connection object. |
| 1685 | */ |
| 1686 | c->protocol = tuple->protocol; |
| 1687 | c->src_ip = tuple->flow_ip; |
| 1688 | c->src_ip_xlate = msg->conn_rule.flow_ip_xlate; |
| 1689 | c->src_port = tuple->flow_ident; |
| 1690 | c->src_port_xlate = msg->conn_rule.flow_ident_xlate; |
| 1691 | c->original_dev = src_dev; |
| 1692 | c->original_match = original_cm; |
| 1693 | c->dest_ip = tuple->return_ip; |
| 1694 | c->dest_ip_xlate = msg->conn_rule.return_ip_xlate; |
| 1695 | c->dest_port = tuple->return_ident; |
| 1696 | c->dest_port_xlate = msg->conn_rule.return_ident_xlate; |
| 1697 | c->reply_dev = dest_dev; |
| 1698 | c->reply_match = reply_cm; |
| 1699 | c->debug_read_seq = 0; |
| 1700 | c->last_sync_jiffies = get_jiffies_64(); |
| 1701 | c->removed = false; |
| 1702 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1703 | sfe_ipv4_connection_match_compute_translations(original_cm); |
| 1704 | sfe_ipv4_connection_match_compute_translations(reply_cm); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1705 | sfe_ipv4_insert_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1706 | |
| 1707 | spin_unlock_bh(&si->lock); |
| 1708 | |
| 1709 | /* |
| 1710 | * We have everything we need! |
| 1711 | */ |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1712 | DEBUG_INFO("NEW connection - p: %d\n" |
| 1713 | "original_cm: match_dev=src_dev: %s %d %pM\n" |
| 1714 | " xmit_dev=dest_dev: %s %d %pM\n" |
| 1715 | " xmit_src_mac: %pM\n" |
| 1716 | " xmit_dest_mac: %pM\n" |
| 1717 | " flags: %x l2_hdr: %u\n" |
| 1718 | "flow_ip: %pI4:%u\n" |
| 1719 | "flow_ip_xlate: %pI4:%u\n" |
| 1720 | "flow_mac: %pM\n" |
| 1721 | "reply_cm: match_dev=dest_dev: %s %d %pM\n" |
| 1722 | " xmit_dev=src_dev: %s %d %pM\n" |
| 1723 | " xmit_src_mac: %pM\n" |
| 1724 | " xmit_dest_mac: %pM\n" |
| 1725 | " flags: %x l2_hdr: %u\n" |
| 1726 | "return_ip: %pI4:%u\n" |
| 1727 | "return_ip_xlate: %pI4:%u\n" |
| 1728 | "return_mac: %pM\n" |
| 1729 | "flags: valid=%x src_mac_valid=%x\n", |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1730 | tuple->protocol, |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 1731 | original_cm->match_dev->name, original_cm->match_dev->ifindex, original_cm->match_dev->dev_addr, |
| 1732 | original_cm->xmit_dev->name, original_cm->xmit_dev->ifindex, original_cm->xmit_dev->dev_addr, |
| 1733 | original_cm->xmit_src_mac, original_cm->xmit_dest_mac, original_cm->flags, original_cm->l2_hdr_size, |
| 1734 | &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1735 | &msg->conn_rule.flow_ip_xlate, ntohs(msg->conn_rule.flow_ident_xlate), |
| 1736 | msg->conn_rule.flow_mac, |
| 1737 | reply_cm->match_dev->name, reply_cm->match_dev->ifindex, reply_cm->match_dev->dev_addr, |
| 1738 | reply_cm->xmit_dev->name, reply_cm->xmit_dev->ifindex, reply_cm->xmit_dev->dev_addr, |
| 1739 | reply_cm->xmit_src_mac, reply_cm->xmit_dest_mac, reply_cm->flags, reply_cm->l2_hdr_size, |
| 1740 | &tuple->return_ip, ntohs(tuple->return_ident), |
| 1741 | &msg->conn_rule.return_ip_xlate, ntohs(msg->conn_rule.return_ident_xlate), |
| 1742 | msg->conn_rule.return_mac, |
| 1743 | msg->valid_flags, msg->src_mac_rule.mac_valid_flags); |
Nicolas Costa | 514fde0 | 2014-01-13 15:50:29 -0600 | [diff] [blame] | 1744 | |
| 1745 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1749 | * sfe_ipv4_destroy_rule() |
| 1750 | * Destroy a forwarding rule. |
| 1751 | */ |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1752 | void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1753 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1754 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1755 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1756 | bool ret; |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 1757 | struct sfe_ipv4_5tuple *tuple = &msg->tuple; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1758 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1759 | this_cpu_inc(si->stats_pcpu->connection_destroy_requests64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1760 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1761 | |
| 1762 | /* |
| 1763 | * Check to see if we have a flow that matches the rule we're trying |
| 1764 | * to destroy. If there isn't then we can't destroy it. |
| 1765 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1766 | 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] | 1767 | tuple->return_ip, tuple->return_ident); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1768 | if (!c) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1769 | spin_unlock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 1770 | this_cpu_inc(si->stats_pcpu->connection_destroy_misses64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1771 | |
| 1772 | 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] | 1773 | tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1774 | &tuple->return_ip, ntohs(tuple->return_ident)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1775 | return; |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * Remove our connection details from the hash tables. |
| 1780 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1781 | ret = sfe_ipv4_remove_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1782 | spin_unlock_bh(&si->lock); |
| 1783 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1784 | if (ret) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1785 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1786 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1787 | |
| 1788 | 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] | 1789 | tuple->protocol, &tuple->flow_ip, ntohs(tuple->flow_ident), |
| 1790 | &tuple->return_ip, ntohs(tuple->return_ident)); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1794 | * sfe_ipv4_register_sync_rule_callback() |
| 1795 | * Register a callback for rule synchronization. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1796 | */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1797 | 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] | 1798 | { |
| 1799 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1800 | |
| 1801 | spin_lock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1802 | rcu_assign_pointer(si->sync_rule_callback, sync_rule_callback); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1803 | spin_unlock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * sfe_ipv4_get_debug_dev() |
| 1808 | */ |
| 1809 | static ssize_t sfe_ipv4_get_debug_dev(struct device *dev, |
| 1810 | struct device_attribute *attr, |
| 1811 | char *buf) |
| 1812 | { |
| 1813 | struct sfe_ipv4 *si = &__si; |
| 1814 | ssize_t count; |
| 1815 | int num; |
| 1816 | |
| 1817 | spin_lock_bh(&si->lock); |
| 1818 | num = si->debug_dev; |
| 1819 | spin_unlock_bh(&si->lock); |
| 1820 | |
| 1821 | count = snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", num); |
| 1822 | return count; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1826 | * sysfs attributes. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1827 | */ |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1828 | static const struct device_attribute sfe_ipv4_debug_dev_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 1829 | __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] | 1830 | |
| 1831 | /* |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1832 | * sfe_ipv4_destroy_all_rules_for_dev() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1833 | * Destroy all connections that match a particular device. |
| 1834 | * |
| 1835 | * If we pass dev as NULL then this destroys all connections. |
| 1836 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1837 | void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1838 | { |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1839 | struct sfe_ipv4 *si = &__si; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1840 | struct sfe_ipv4_connection *c; |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1841 | bool ret; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1842 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1843 | another_round: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1844 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1845 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1846 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1847 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1848 | * Does this connection relate to the device we are destroying? |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1849 | */ |
| 1850 | if (!dev |
| 1851 | || (dev == c->original_dev) |
| 1852 | || (dev == c->reply_dev)) { |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1853 | break; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1854 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1855 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1856 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1857 | if (c) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1858 | ret = sfe_ipv4_remove_connection(si, c); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1862 | |
| 1863 | if (c) { |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1864 | if (ret) { |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1865 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_DESTROY); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 1866 | } |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1867 | goto another_round; |
| 1868 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1872 | * sfe_ipv4_periodic_sync() |
| 1873 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1874 | static void sfe_ipv4_periodic_sync(struct work_struct *work) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1875 | { |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1876 | 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] | 1877 | u64 now_jiffies; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1878 | int quota; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1879 | sfe_sync_rule_callback_t sync_rule_callback; |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1880 | struct sfe_ipv4_connection *c; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1881 | |
| 1882 | now_jiffies = get_jiffies_64(); |
| 1883 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1884 | rcu_read_lock(); |
| 1885 | sync_rule_callback = rcu_dereference(si->sync_rule_callback); |
| 1886 | if (!sync_rule_callback) { |
| 1887 | rcu_read_unlock(); |
| 1888 | goto done; |
| 1889 | } |
| 1890 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1891 | spin_lock_bh(&si->lock); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1892 | |
| 1893 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1894 | * If we have reached the end of the connection list, walk from |
| 1895 | * the connection head. |
| 1896 | */ |
| 1897 | c = si->wc_next; |
| 1898 | if (unlikely(!c)) { |
| 1899 | c = si->all_connections_head; |
| 1900 | } |
| 1901 | |
| 1902 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1903 | * Get an estimate of the number of connections to parse in this sync. |
| 1904 | */ |
| 1905 | quota = (si->num_connections + 63) / 64; |
| 1906 | |
| 1907 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1908 | * Walk the "all connection" list and sync the connection state. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1909 | */ |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1910 | while (likely(c && quota)) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1911 | struct sfe_ipv4_connection_match *cm; |
| 1912 | struct sfe_ipv4_connection_match *counter_cm; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1913 | struct sfe_connection_sync sis; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1914 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1915 | cm = c->original_match; |
| 1916 | counter_cm = c->reply_match; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1917 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1918 | /* |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1919 | * Didn't receive packets in the original direction or reply |
| 1920 | * direction, move to the next connection. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1921 | */ |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1922 | if ((!atomic_read(&cm->rx_packet_count)) && !(atomic_read(&counter_cm->rx_packet_count))) { |
| 1923 | c = c->all_connections_next; |
| 1924 | continue; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1925 | } |
| 1926 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1927 | quota--; |
Matthew McClintock | af48f1e | 2014-01-23 15:29:19 -0600 | [diff] [blame] | 1928 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1929 | 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] | 1930 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1931 | si->wc_next = c->all_connections_next; |
| 1932 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1933 | /* |
| 1934 | * We don't want to be holding the lock when we sync! |
| 1935 | */ |
| 1936 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1937 | sync_rule_callback(&sis); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1938 | spin_lock_bh(&si->lock); |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1939 | |
| 1940 | /* |
| 1941 | * c must be set and used in the same lock/unlock window; |
| 1942 | * because c could be removed when we don't hold the lock, |
| 1943 | * so delay grabbing until after the callback and relock. |
| 1944 | */ |
| 1945 | c = si->wc_next; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
Ken Zhu | dc42367 | 2021-09-02 18:27:01 -0700 | [diff] [blame] | 1948 | /* |
| 1949 | * At the end of the sync, put the wc_next to the connection we left. |
| 1950 | */ |
| 1951 | si->wc_next = c; |
| 1952 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1953 | spin_unlock_bh(&si->lock); |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1954 | rcu_read_unlock(); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1955 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 1956 | done: |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 1957 | 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] | 1958 | } |
| 1959 | |
| 1960 | #define CHAR_DEV_MSG_SIZE 768 |
| 1961 | |
| 1962 | /* |
| 1963 | * sfe_ipv4_debug_dev_read_start() |
| 1964 | * Generate part of the XML output. |
| 1965 | */ |
| 1966 | static bool sfe_ipv4_debug_dev_read_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1967 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1968 | { |
| 1969 | int bytes_read; |
| 1970 | |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 1971 | si->debug_read_seq++; |
| 1972 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 1973 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "<sfe_ipv4>\n"); |
| 1974 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1975 | return false; |
| 1976 | } |
| 1977 | |
| 1978 | *length -= bytes_read; |
| 1979 | *total_read += bytes_read; |
| 1980 | |
| 1981 | ws->state++; |
| 1982 | return true; |
| 1983 | } |
| 1984 | |
| 1985 | /* |
| 1986 | * sfe_ipv4_debug_dev_read_connections_start() |
| 1987 | * Generate part of the XML output. |
| 1988 | */ |
| 1989 | static bool sfe_ipv4_debug_dev_read_connections_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 1990 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 1991 | { |
| 1992 | int bytes_read; |
| 1993 | |
| 1994 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<connections>\n"); |
| 1995 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
| 1999 | *length -= bytes_read; |
| 2000 | *total_read += bytes_read; |
| 2001 | |
| 2002 | ws->state++; |
| 2003 | return true; |
| 2004 | } |
| 2005 | |
| 2006 | /* |
| 2007 | * sfe_ipv4_debug_dev_read_connections_connection() |
| 2008 | * Generate part of the XML output. |
| 2009 | */ |
| 2010 | static bool sfe_ipv4_debug_dev_read_connections_connection(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2011 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2012 | { |
| 2013 | struct sfe_ipv4_connection *c; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2014 | struct sfe_ipv4_connection_match *original_cm; |
| 2015 | struct sfe_ipv4_connection_match *reply_cm; |
| 2016 | int bytes_read; |
| 2017 | int protocol; |
| 2018 | struct net_device *src_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2019 | __be32 src_ip; |
| 2020 | __be32 src_ip_xlate; |
| 2021 | __be16 src_port; |
| 2022 | __be16 src_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2023 | u64 src_rx_packets; |
| 2024 | u64 src_rx_bytes; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2025 | struct net_device *dest_dev; |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2026 | __be32 dest_ip; |
| 2027 | __be32 dest_ip_xlate; |
| 2028 | __be16 dest_port; |
| 2029 | __be16 dest_port_xlate; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2030 | u64 dest_rx_packets; |
| 2031 | u64 dest_rx_bytes; |
| 2032 | u64 last_sync_jiffies; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2033 | u32 src_mark, dest_mark, src_priority, dest_priority, src_dscp, dest_dscp; |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2034 | bool original_cm_sawf_valid, reply_cm_sawf_valid; |
| 2035 | u32 flow_service_class, return_service_class; |
| 2036 | u32 flow_msduq, return_msduq; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2037 | u32 packet, byte, original_cm_flags; |
| 2038 | u16 pppoe_session_id; |
| 2039 | u8 pppoe_remote_mac[ETH_ALEN]; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2040 | u32 original_fast_xmit, reply_fast_xmit; |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2041 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2042 | int src_flow_cookie, dst_flow_cookie; |
| 2043 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2044 | |
| 2045 | spin_lock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2046 | |
| 2047 | for (c = si->all_connections_head; c; c = c->all_connections_next) { |
| 2048 | if (c->debug_read_seq < si->debug_read_seq) { |
| 2049 | c->debug_read_seq = si->debug_read_seq; |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2053 | |
| 2054 | /* |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2055 | * If there were no connections then move to the next state. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2056 | */ |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2057 | if (!c || c->removed) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2058 | spin_unlock_bh(&si->lock); |
Xiaoping Fan | 3458647 | 2015-07-03 02:20:35 -0700 | [diff] [blame] | 2059 | ws->state++; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2060 | return true; |
| 2061 | } |
| 2062 | |
| 2063 | original_cm = c->original_match; |
| 2064 | reply_cm = c->reply_match; |
| 2065 | |
| 2066 | protocol = c->protocol; |
| 2067 | src_dev = c->original_dev; |
| 2068 | src_ip = c->src_ip; |
| 2069 | src_ip_xlate = c->src_ip_xlate; |
| 2070 | src_port = c->src_port; |
| 2071 | src_port_xlate = c->src_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2072 | src_priority = original_cm->priority; |
| 2073 | src_dscp = original_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2074 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2075 | sfe_ipv4_connection_match_update_summary_stats(original_cm, &packet, &byte); |
| 2076 | sfe_ipv4_connection_match_update_summary_stats(reply_cm, &packet, &byte); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2077 | |
| 2078 | src_rx_packets = original_cm->rx_packet_count64; |
| 2079 | src_rx_bytes = original_cm->rx_byte_count64; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2080 | src_mark = original_cm->mark; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2081 | original_fast_xmit = (original_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2082 | dest_dev = c->reply_dev; |
| 2083 | dest_ip = c->dest_ip; |
| 2084 | dest_ip_xlate = c->dest_ip_xlate; |
| 2085 | dest_port = c->dest_port; |
| 2086 | dest_port_xlate = c->dest_port_xlate; |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2087 | dest_priority = reply_cm->priority; |
| 2088 | dest_dscp = reply_cm->dscp >> SFE_IPV4_DSCP_SHIFT; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2089 | dest_rx_packets = reply_cm->rx_packet_count64; |
| 2090 | dest_rx_bytes = reply_cm->rx_byte_count64; |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2091 | dest_mark = reply_cm->mark; |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2092 | reply_fast_xmit = (reply_cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2093 | last_sync_jiffies = get_jiffies_64() - c->last_sync_jiffies; |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2094 | original_cm_flags = original_cm->flags; |
| 2095 | pppoe_session_id = original_cm->pppoe_session_id; |
| 2096 | ether_addr_copy(pppoe_remote_mac, original_cm->pppoe_remote_mac); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2097 | original_cm_sawf_valid = original_cm->sawf_valid; |
| 2098 | reply_cm_sawf_valid = reply_cm->sawf_valid; |
| 2099 | flow_service_class = SFE_GET_SAWF_SERVICE_CLASS(original_cm->mark); |
| 2100 | flow_msduq = SFE_GET_SAWF_MSDUQ(original_cm->mark); |
| 2101 | return_service_class = SFE_GET_SAWF_SERVICE_CLASS(reply_cm->mark); |
| 2102 | return_msduq = SFE_GET_SAWF_MSDUQ(reply_cm->mark); |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2103 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2104 | src_flow_cookie = original_cm->flow_cookie; |
| 2105 | dst_flow_cookie = reply_cm->flow_cookie; |
| 2106 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2107 | spin_unlock_bh(&si->lock); |
| 2108 | |
| 2109 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t\t<connection " |
| 2110 | "protocol=\"%u\" " |
| 2111 | "src_dev=\"%s\" " |
| 2112 | "src_ip=\"%pI4\" src_ip_xlate=\"%pI4\" " |
| 2113 | "src_port=\"%u\" src_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2114 | "src_priority=\"%u\" src_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2115 | "src_rx_pkts=\"%llu\" src_rx_bytes=\"%llu\" " |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2116 | "src_mark=\"%08x\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2117 | "src_fast_xmit=\"%s\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2118 | "dest_dev=\"%s\" " |
| 2119 | "dest_ip=\"%pI4\" dest_ip_xlate=\"%pI4\" " |
| 2120 | "dest_port=\"%u\" dest_port_xlate=\"%u\" " |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2121 | "dest_priority=\"%u\" dest_dscp=\"%u\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2122 | "dest_rx_pkts=\"%llu\" dest_rx_bytes=\"%llu\" " |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2123 | "dest_mark=\"%08x\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2124 | "reply_fast_xmit=\"%s\" " |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2125 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2126 | "src_flow_cookie=\"%d\" dst_flow_cookie=\"%d\" " |
| 2127 | #endif |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2128 | "last_sync=\"%llu\" ", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2129 | protocol, |
| 2130 | src_dev->name, |
| 2131 | &src_ip, &src_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2132 | ntohs(src_port), ntohs(src_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2133 | src_priority, src_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2134 | src_rx_packets, src_rx_bytes, |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2135 | src_mark, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2136 | original_fast_xmit ? "Yes" : "No", |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2137 | dest_dev->name, |
| 2138 | &dest_ip, &dest_ip_xlate, |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2139 | ntohs(dest_port), ntohs(dest_port_xlate), |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 2140 | dest_priority, dest_dscp, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2141 | dest_rx_packets, dest_rx_bytes, |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2142 | dest_mark, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2143 | reply_fast_xmit ? "Yes" : "No", |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2144 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2145 | src_flow_cookie, dst_flow_cookie, |
| 2146 | #endif |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 2147 | last_sync_jiffies); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2148 | |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2149 | 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] | 2150 | 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] | 2151 | pppoe_session_id, pppoe_remote_mac); |
| 2152 | } |
| 2153 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2154 | if (original_cm_sawf_valid) { |
| 2155 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "flow_service_class=\"%d\" flow_msduq=\"%d\" ", |
| 2156 | flow_service_class, flow_msduq); |
| 2157 | } |
| 2158 | |
| 2159 | if (reply_cm_sawf_valid) { |
| 2160 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "return_service_class=\"%d\" return_msduq=\"%d\" ", |
| 2161 | return_service_class, return_msduq); |
| 2162 | } |
| 2163 | |
Guduri Prathyusha | eb31c90 | 2021-11-10 20:18:50 +0530 | [diff] [blame] | 2164 | bytes_read += snprintf(msg + bytes_read, CHAR_DEV_MSG_SIZE, "/>\n"); |
| 2165 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2166 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2167 | return false; |
| 2168 | } |
| 2169 | |
| 2170 | *length -= bytes_read; |
| 2171 | *total_read += bytes_read; |
| 2172 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2173 | return true; |
| 2174 | } |
| 2175 | |
| 2176 | /* |
| 2177 | * sfe_ipv4_debug_dev_read_connections_end() |
| 2178 | * Generate part of the XML output. |
| 2179 | */ |
| 2180 | static bool sfe_ipv4_debug_dev_read_connections_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2181 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2182 | { |
| 2183 | int bytes_read; |
| 2184 | |
| 2185 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</connections>\n"); |
| 2186 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2187 | return false; |
| 2188 | } |
| 2189 | |
| 2190 | *length -= bytes_read; |
| 2191 | *total_read += bytes_read; |
| 2192 | |
| 2193 | ws->state++; |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
| 2197 | /* |
| 2198 | * sfe_ipv4_debug_dev_read_exceptions_start() |
| 2199 | * Generate part of the XML output. |
| 2200 | */ |
| 2201 | static bool sfe_ipv4_debug_dev_read_exceptions_start(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2202 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2203 | { |
| 2204 | int bytes_read; |
| 2205 | |
| 2206 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<exceptions>\n"); |
| 2207 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2208 | return false; |
| 2209 | } |
| 2210 | |
| 2211 | *length -= bytes_read; |
| 2212 | *total_read += bytes_read; |
| 2213 | |
| 2214 | ws->state++; |
| 2215 | return true; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * sfe_ipv4_debug_dev_read_exceptions_exception() |
| 2220 | * Generate part of the XML output. |
| 2221 | */ |
| 2222 | static bool sfe_ipv4_debug_dev_read_exceptions_exception(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2223 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2224 | { |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2225 | int i; |
| 2226 | u64 val = 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2227 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2228 | for_each_possible_cpu(i) { |
| 2229 | const struct sfe_ipv4_stats *s = per_cpu_ptr(si->stats_pcpu, i); |
| 2230 | val += s->exception_events64[ws->iter_exception]; |
| 2231 | } |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2232 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2233 | if (val) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2234 | int bytes_read; |
| 2235 | |
| 2236 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, |
| 2237 | "\t\t<exception name=\"%s\" count=\"%llu\" />\n", |
| 2238 | sfe_ipv4_exception_events_string[ws->iter_exception], |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2239 | val); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2240 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2241 | return false; |
| 2242 | } |
| 2243 | |
| 2244 | *length -= bytes_read; |
| 2245 | *total_read += bytes_read; |
| 2246 | } |
| 2247 | |
| 2248 | ws->iter_exception++; |
| 2249 | if (ws->iter_exception >= SFE_IPV4_EXCEPTION_EVENT_LAST) { |
| 2250 | ws->iter_exception = 0; |
| 2251 | ws->state++; |
| 2252 | } |
| 2253 | |
| 2254 | return true; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * sfe_ipv4_debug_dev_read_exceptions_end() |
| 2259 | * Generate part of the XML output. |
| 2260 | */ |
| 2261 | static bool sfe_ipv4_debug_dev_read_exceptions_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2262 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2263 | { |
| 2264 | int bytes_read; |
| 2265 | |
| 2266 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t</exceptions>\n"); |
| 2267 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
| 2271 | *length -= bytes_read; |
| 2272 | *total_read += bytes_read; |
| 2273 | |
| 2274 | ws->state++; |
| 2275 | return true; |
| 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * sfe_ipv4_debug_dev_read_stats() |
| 2280 | * Generate part of the XML output. |
| 2281 | */ |
| 2282 | static bool sfe_ipv4_debug_dev_read_stats(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2283 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2284 | { |
| 2285 | int bytes_read; |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2286 | struct sfe_ipv4_stats stats; |
| 2287 | unsigned int num_conn; |
| 2288 | |
| 2289 | sfe_ipv4_update_summary_stats(si, &stats); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2290 | |
| 2291 | spin_lock_bh(&si->lock); |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2292 | num_conn = si->num_connections; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2293 | spin_unlock_bh(&si->lock); |
| 2294 | |
| 2295 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "\t<stats " |
| 2296 | "num_connections=\"%u\" " |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2297 | "pkts_dropped=\"%llu\" " |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2298 | "pkts_fast_xmited=\"%llu\" " |
Xiaoping Fan | 5917642 | 2015-05-22 15:58:10 -0700 | [diff] [blame] | 2299 | "pkts_forwarded=\"%llu\" pkts_not_forwarded=\"%llu\" " |
| 2300 | "create_requests=\"%llu\" create_collisions=\"%llu\" " |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 2301 | "create_failures=\"%llu\" " |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2302 | "destroy_requests=\"%llu\" destroy_misses=\"%llu\" " |
| 2303 | "flushes=\"%llu\" " |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 2304 | "hash_hits=\"%llu\" hash_reorders=\"%llu\" " |
| 2305 | "pppoe_encap_pkts_fwded=\"%llu\" " |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 2306 | "pppoe_decap_pkts_fwded=\"%llu\" " |
| 2307 | "pppoe_bridge_pkts_fwded=\"%llu\" />\n", |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2308 | num_conn, |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2309 | stats.packets_dropped64, |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2310 | stats.packets_fast_xmited64, |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2311 | stats.packets_forwarded64, |
| 2312 | stats.packets_not_forwarded64, |
| 2313 | stats.connection_create_requests64, |
| 2314 | stats.connection_create_collisions64, |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame] | 2315 | stats.connection_create_failures64, |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2316 | stats.connection_destroy_requests64, |
| 2317 | stats.connection_destroy_misses64, |
| 2318 | stats.connection_flushes64, |
| 2319 | stats.connection_match_hash_hits64, |
Guduri Prathyusha | 647fe3e | 2021-11-22 19:17:51 +0530 | [diff] [blame] | 2320 | stats.connection_match_hash_reorders64, |
| 2321 | stats.pppoe_encap_packets_forwarded64, |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 2322 | stats.pppoe_decap_packets_forwarded64, |
| 2323 | stats.pppoe_bridge_packets_forwarded64); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2324 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2325 | return false; |
| 2326 | } |
| 2327 | |
| 2328 | *length -= bytes_read; |
| 2329 | *total_read += bytes_read; |
| 2330 | |
| 2331 | ws->state++; |
| 2332 | return true; |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * sfe_ipv4_debug_dev_read_end() |
| 2337 | * Generate part of the XML output. |
| 2338 | */ |
| 2339 | static bool sfe_ipv4_debug_dev_read_end(struct sfe_ipv4 *si, char *buffer, char *msg, size_t *length, |
| 2340 | int *total_read, struct sfe_ipv4_debug_xml_write_state *ws) |
| 2341 | { |
| 2342 | int bytes_read; |
| 2343 | |
| 2344 | bytes_read = snprintf(msg, CHAR_DEV_MSG_SIZE, "</sfe_ipv4>\n"); |
| 2345 | if (copy_to_user(buffer + *total_read, msg, CHAR_DEV_MSG_SIZE)) { |
| 2346 | return false; |
| 2347 | } |
| 2348 | |
| 2349 | *length -= bytes_read; |
| 2350 | *total_read += bytes_read; |
| 2351 | |
| 2352 | ws->state++; |
| 2353 | return true; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
| 2357 | * Array of write functions that write various XML elements that correspond to |
| 2358 | * our XML output state machine. |
| 2359 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 2360 | 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] | 2361 | sfe_ipv4_debug_dev_read_start, |
| 2362 | sfe_ipv4_debug_dev_read_connections_start, |
| 2363 | sfe_ipv4_debug_dev_read_connections_connection, |
| 2364 | sfe_ipv4_debug_dev_read_connections_end, |
| 2365 | sfe_ipv4_debug_dev_read_exceptions_start, |
| 2366 | sfe_ipv4_debug_dev_read_exceptions_exception, |
| 2367 | sfe_ipv4_debug_dev_read_exceptions_end, |
| 2368 | sfe_ipv4_debug_dev_read_stats, |
| 2369 | sfe_ipv4_debug_dev_read_end, |
| 2370 | }; |
| 2371 | |
| 2372 | /* |
| 2373 | * sfe_ipv4_debug_dev_read() |
| 2374 | * Send info to userspace upon read request from user |
| 2375 | */ |
| 2376 | static ssize_t sfe_ipv4_debug_dev_read(struct file *filp, char *buffer, size_t length, loff_t *offset) |
| 2377 | { |
| 2378 | char msg[CHAR_DEV_MSG_SIZE]; |
| 2379 | int total_read = 0; |
| 2380 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2381 | struct sfe_ipv4 *si = &__si; |
| 2382 | |
| 2383 | ws = (struct sfe_ipv4_debug_xml_write_state *)filp->private_data; |
| 2384 | while ((ws->state != SFE_IPV4_DEBUG_XML_STATE_DONE) && (length > CHAR_DEV_MSG_SIZE)) { |
| 2385 | if ((sfe_ipv4_debug_xml_write_methods[ws->state])(si, buffer, msg, &length, &total_read, ws)) { |
| 2386 | continue; |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | return total_read; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2394 | * sfe_ipv4_debug_dev_open() |
| 2395 | */ |
| 2396 | static int sfe_ipv4_debug_dev_open(struct inode *inode, struct file *file) |
| 2397 | { |
| 2398 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2399 | |
| 2400 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 2401 | if (!ws) { |
| 2402 | ws = kzalloc(sizeof(struct sfe_ipv4_debug_xml_write_state), GFP_KERNEL); |
| 2403 | if (!ws) { |
| 2404 | return -ENOMEM; |
| 2405 | } |
| 2406 | |
| 2407 | ws->state = SFE_IPV4_DEBUG_XML_STATE_START; |
| 2408 | file->private_data = ws; |
| 2409 | } |
| 2410 | |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * sfe_ipv4_debug_dev_release() |
| 2416 | */ |
| 2417 | static int sfe_ipv4_debug_dev_release(struct inode *inode, struct file *file) |
| 2418 | { |
| 2419 | struct sfe_ipv4_debug_xml_write_state *ws; |
| 2420 | |
| 2421 | ws = (struct sfe_ipv4_debug_xml_write_state *)file->private_data; |
| 2422 | if (ws) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2423 | /* |
| 2424 | * We've finished with our output so free the write state. |
| 2425 | */ |
| 2426 | kfree(ws); |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2427 | file->private_data = NULL; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * File operations used in the debug char device |
| 2435 | */ |
| 2436 | static struct file_operations sfe_ipv4_debug_dev_fops = { |
| 2437 | .read = sfe_ipv4_debug_dev_read, |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2438 | .open = sfe_ipv4_debug_dev_open, |
| 2439 | .release = sfe_ipv4_debug_dev_release |
| 2440 | }; |
| 2441 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2442 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2443 | /* |
| 2444 | * sfe_register_flow_cookie_cb |
| 2445 | * register a function in SFE to let SFE use this function to configure flow cookie for a flow |
| 2446 | * |
| 2447 | * Hardware driver which support flow cookie should register a callback function in SFE. Then SFE |
| 2448 | * can use this function to configure flow cookie for a flow. |
| 2449 | * return: 0, success; !=0, fail |
| 2450 | */ |
| 2451 | int sfe_register_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 2452 | { |
| 2453 | struct sfe_ipv4 *si = &__si; |
| 2454 | |
| 2455 | BUG_ON(!cb); |
| 2456 | |
| 2457 | if (si->flow_cookie_set_func) { |
| 2458 | return -1; |
| 2459 | } |
| 2460 | |
| 2461 | rcu_assign_pointer(si->flow_cookie_set_func, cb); |
| 2462 | return 0; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * sfe_unregister_flow_cookie_cb |
| 2467 | * unregister function which is used to configure flow cookie for a flow |
| 2468 | * |
| 2469 | * return: 0, success; !=0, fail |
| 2470 | */ |
| 2471 | int sfe_unregister_flow_cookie_cb(flow_cookie_set_func_t cb) |
| 2472 | { |
| 2473 | struct sfe_ipv4 *si = &__si; |
| 2474 | |
| 2475 | RCU_INIT_POINTER(si->flow_cookie_set_func, NULL); |
| 2476 | return 0; |
| 2477 | } |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2478 | |
| 2479 | /* |
| 2480 | * sfe_ipv4_get_flow_cookie() |
| 2481 | */ |
| 2482 | static ssize_t sfe_ipv4_get_flow_cookie(struct device *dev, |
| 2483 | struct device_attribute *attr, |
| 2484 | char *buf) |
| 2485 | { |
| 2486 | struct sfe_ipv4 *si = &__si; |
Xiaoping Fan | 01c67cc | 2015-11-09 11:31:57 -0800 | [diff] [blame] | 2487 | 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] | 2488 | } |
| 2489 | |
| 2490 | /* |
| 2491 | * sfe_ipv4_set_flow_cookie() |
| 2492 | */ |
| 2493 | static ssize_t sfe_ipv4_set_flow_cookie(struct device *dev, |
| 2494 | struct device_attribute *attr, |
| 2495 | const char *buf, size_t size) |
| 2496 | { |
| 2497 | struct sfe_ipv4 *si = &__si; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2498 | si->flow_cookie_enable = simple_strtol(buf, NULL, 0); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2499 | |
| 2500 | return size; |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * sysfs attributes. |
| 2505 | */ |
| 2506 | static const struct device_attribute sfe_ipv4_flow_cookie_attr = |
Xiaoping Fan | e70da41 | 2016-02-26 16:47:57 -0800 | [diff] [blame] | 2507 | __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] | 2508 | #endif /*CONFIG_NF_FLOW_COOKIE*/ |
| 2509 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2510 | /* |
| 2511 | * sfe_ipv4_get_cpu() |
| 2512 | */ |
| 2513 | static ssize_t sfe_ipv4_get_cpu(struct device *dev, |
| 2514 | struct device_attribute *attr, |
| 2515 | char *buf) |
| 2516 | { |
| 2517 | struct sfe_ipv4 *si = &__si; |
| 2518 | return snprintf(buf, (ssize_t)PAGE_SIZE, "%d\n", si->work_cpu); |
| 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | * sfe_ipv4_set_cpu() |
| 2523 | */ |
| 2524 | static ssize_t sfe_ipv4_set_cpu(struct device *dev, |
| 2525 | struct device_attribute *attr, |
| 2526 | const char *buf, size_t size) |
| 2527 | { |
| 2528 | struct sfe_ipv4 *si = &__si; |
| 2529 | int work_cpu; |
| 2530 | work_cpu = simple_strtol(buf, NULL, 0); |
| 2531 | if ((work_cpu >= 0) && (work_cpu <= NR_CPUS)) { |
| 2532 | si->work_cpu = work_cpu; |
| 2533 | } else { |
| 2534 | dev_err(dev, "%s is not in valid range[0,%d]", buf, NR_CPUS); |
| 2535 | } |
| 2536 | return size; |
| 2537 | } |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2538 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2539 | /* |
| 2540 | * sysfs attributes. |
| 2541 | */ |
| 2542 | static const struct device_attribute sfe_ipv4_cpu_attr = |
| 2543 | __ATTR(stats_work_cpu, S_IWUSR | S_IRUGO, sfe_ipv4_get_cpu, sfe_ipv4_set_cpu); |
| 2544 | |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2545 | /* |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2546 | * sfe_ipv4_conn_match_hash_init() |
| 2547 | * Initialize conn match hash lists |
| 2548 | */ |
| 2549 | static void sfe_ipv4_conn_match_hash_init(struct sfe_ipv4 *si, int len) |
| 2550 | { |
| 2551 | struct hlist_head *hash_list = si->hlist_conn_match_hash_head; |
| 2552 | int i; |
| 2553 | |
| 2554 | for (i = 0; i < len; i++) { |
| 2555 | INIT_HLIST_HEAD(&hash_list[i]); |
| 2556 | } |
| 2557 | } |
| 2558 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2559 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2560 | /* |
| 2561 | * sfe_ipv4_local_out() |
| 2562 | * Called for packets from ip_local_out() - post encapsulation & other packets |
| 2563 | */ |
| 2564 | static unsigned int sfe_ipv4_local_out(void *priv, struct sk_buff *skb, const struct nf_hook_state *nhs) |
| 2565 | { |
Nitin Shetty | c28f817 | 2022-02-04 16:23:46 +0530 | [diff] [blame] | 2566 | struct sfe_l2_info l2_info = {0}; |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 2567 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2568 | DEBUG_TRACE("%px: sfe: sfe_ipv4_local_out hook called.\n", skb); |
| 2569 | |
| 2570 | if (likely(skb->skb_iif)) { |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 2571 | return sfe_ipv4_recv(skb->dev, skb, &l2_info, true) ? NF_STOLEN : NF_ACCEPT; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | return NF_ACCEPT; |
| 2575 | } |
| 2576 | |
| 2577 | /* |
| 2578 | * struct nf_hook_ops sfe_ipv4_ops_local_out[] |
| 2579 | * Hooks into netfilter local out packet monitoring points. |
| 2580 | */ |
| 2581 | static struct nf_hook_ops sfe_ipv4_ops_local_out[] __read_mostly = { |
| 2582 | |
| 2583 | /* |
| 2584 | * Local out routing hook is used to monitor packets. |
| 2585 | */ |
| 2586 | { |
| 2587 | .hook = sfe_ipv4_local_out, |
| 2588 | .pf = PF_INET, |
| 2589 | .hooknum = NF_INET_LOCAL_OUT, |
| 2590 | .priority = NF_IP_PRI_FIRST, |
| 2591 | }, |
| 2592 | }; |
| 2593 | #endif |
| 2594 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2595 | /* |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2596 | * sfe_ipv4_init() |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2597 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2598 | int sfe_ipv4_init(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2599 | { |
| 2600 | struct sfe_ipv4 *si = &__si; |
| 2601 | int result = -1; |
| 2602 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2603 | DEBUG_INFO("SFE IPv4 init\n"); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2604 | |
Ratheesh Kannoth | 94fc5b8 | 2021-10-20 07:45:06 +0530 | [diff] [blame] | 2605 | sfe_ipv4_conn_match_hash_init(si, ARRAY_SIZE(si->hlist_conn_match_hash_head)); |
| 2606 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2607 | si->stats_pcpu = alloc_percpu_gfp(struct sfe_ipv4_stats, GFP_KERNEL | __GFP_ZERO); |
| 2608 | if (!si->stats_pcpu) { |
| 2609 | DEBUG_ERROR("failed to allocate stats memory for sfe_ipv4\n"); |
| 2610 | goto exit0; |
| 2611 | } |
| 2612 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2613 | /* |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2614 | * Allocate per cpu per service class memory. |
| 2615 | */ |
| 2616 | si->stats_pcpu_psc = alloc_percpu_gfp(struct sfe_ipv4_service_class_stats_db, |
| 2617 | GFP_KERNEL | __GFP_ZERO); |
| 2618 | if (!si->stats_pcpu_psc) { |
| 2619 | DEBUG_ERROR("failed to allocate per cpu per service clas stats memory\n"); |
| 2620 | goto exit1; |
| 2621 | } |
| 2622 | |
| 2623 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2624 | * Create sys/sfe_ipv4 |
| 2625 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2626 | si->sys_ipv4 = kobject_create_and_add("sfe_ipv4", NULL); |
| 2627 | if (!si->sys_ipv4) { |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2628 | DEBUG_ERROR("failed to register sfe_ipv4\n"); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2629 | goto exit2; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* |
| 2633 | * Create files, one for each parameter supported by this module. |
| 2634 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2635 | 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] | 2636 | if (result) { |
| 2637 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2638 | goto exit3; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2641 | result = sysfs_create_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2642 | if (result) { |
| 2643 | DEBUG_ERROR("failed to register debug dev file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2644 | goto exit4; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2647 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2648 | 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] | 2649 | if (result) { |
| 2650 | DEBUG_ERROR("failed to register flow cookie enable file: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2651 | goto exit5; |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2652 | } |
| 2653 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
| 2654 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2655 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2656 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2657 | result = nf_register_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2658 | #else |
| 2659 | result = nf_register_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2660 | #endif |
| 2661 | if (result < 0) { |
| 2662 | DEBUG_ERROR("can't register nf local out hook: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2663 | goto exit6; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2664 | } |
| 2665 | DEBUG_INFO("Register nf local out hook success: %d\n", result); |
| 2666 | #endif |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2667 | /* |
| 2668 | * Register our debug char device. |
| 2669 | */ |
| 2670 | result = register_chrdev(0, "sfe_ipv4", &sfe_ipv4_debug_dev_fops); |
| 2671 | if (result < 0) { |
| 2672 | DEBUG_ERROR("Failed to register chrdev: %d\n", result); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2673 | goto exit7; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | si->debug_dev = result; |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2677 | si->work_cpu = WORK_CPU_UNBOUND; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2678 | |
| 2679 | /* |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2680 | * Create a work to handle periodic statistics. |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2681 | */ |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2682 | INIT_DELAYED_WORK(&(si->sync_dwork), sfe_ipv4_periodic_sync); |
| 2683 | schedule_delayed_work_on(si->work_cpu, &(si->sync_dwork), ((HZ + 99) / 100)); |
| 2684 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2685 | spin_lock_init(&si->lock); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2686 | return 0; |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2687 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2688 | exit7: |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2689 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2690 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2691 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2692 | nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2693 | #else |
| 2694 | nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2695 | #endif |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2696 | exit6: |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2697 | #endif |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2698 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2699 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2700 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2701 | exit5: |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2702 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2703 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2704 | exit4: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2705 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2706 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2707 | exit3: |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2708 | kobject_put(si->sys_ipv4); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2709 | |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2710 | exit2: |
| 2711 | free_percpu(si->stats_pcpu_psc); |
| 2712 | |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2713 | exit1: |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2714 | free_percpu(si->stats_pcpu); |
| 2715 | |
| 2716 | exit0: |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2717 | return result; |
| 2718 | } |
| 2719 | |
| 2720 | /* |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2721 | * sfe_ipv4_exit() |
| 2722 | */ |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 2723 | void sfe_ipv4_exit(void) |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2724 | { |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2725 | struct sfe_ipv4 *si = &__si; |
| 2726 | |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2727 | DEBUG_INFO("SFE IPv4 exit\n"); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2728 | /* |
| 2729 | * Destroy all connections. |
| 2730 | */ |
Dave Hudson | dcd08fb | 2013-11-22 09:25:16 -0600 | [diff] [blame] | 2731 | sfe_ipv4_destroy_all_rules_for_dev(NULL); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2732 | |
Ken Zhu | 137722d | 2021-09-23 17:57:36 -0700 | [diff] [blame] | 2733 | cancel_delayed_work_sync(&si->sync_dwork); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2734 | |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2735 | unregister_chrdev(si->debug_dev, "sfe_ipv4"); |
| 2736 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 2737 | #ifdef SFE_PROCESS_LOCAL_OUT |
| 2738 | DEBUG_TRACE("sfe: Unregister local out hook\n"); |
| 2739 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 2740 | nf_unregister_hooks(sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2741 | #else |
| 2742 | nf_unregister_net_hooks(&init_net, sfe_ipv4_ops_local_out, ARRAY_SIZE(sfe_ipv4_ops_local_out)); |
| 2743 | #endif |
| 2744 | #endif |
| 2745 | |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2746 | #ifdef CONFIG_NF_FLOW_COOKIE |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2747 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_flow_cookie_attr.attr); |
Xiaoping Fan | 640faf4 | 2015-08-28 15:50:55 -0700 | [diff] [blame] | 2748 | #endif /* CONFIG_NF_FLOW_COOKIE */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2749 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_debug_dev_attr.attr); |
Ken Zhu | 7e38d1a | 2021-11-30 17:31:46 -0800 | [diff] [blame] | 2750 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2751 | sysfs_remove_file(si->sys_ipv4, &sfe_ipv4_cpu_attr.attr); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2752 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 2753 | kobject_put(si->sys_ipv4); |
Dave Hudson | 87973cd | 2013-10-22 16:00:04 +0100 | [diff] [blame] | 2754 | |
Ratheesh Kannoth | 3aeb289 | 2021-10-20 07:57:15 +0530 | [diff] [blame] | 2755 | free_percpu(si->stats_pcpu); |
Parikshit Gune | d31a820 | 2022-01-05 22:15:04 +0530 | [diff] [blame^] | 2756 | free_percpu(si->stats_pcpu_psc); |
Dave Hudson | aaf97ca | 2013-06-13 17:52:29 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
Xiaoping Fan | d1dc7b2 | 2015-01-23 00:43:56 -0800 | [diff] [blame] | 2759 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 2760 | EXPORT_SYMBOL(sfe_register_flow_cookie_cb); |
| 2761 | EXPORT_SYMBOL(sfe_unregister_flow_cookie_cb); |
| 2762 | #endif |