Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sfe_cm.h |
| 3 | * Shortcut forwarding engine. |
| 4 | * |
Xiaoping Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 5 | * Copyright (c) 2013-2015 The Linux Foundation. All rights reserved. |
| 6 | * Permission to use, copy, modify, and/or distribute this software for |
| 7 | * any purpose with or without fee is hereby granted, provided that the |
| 8 | * above copyright notice and this permission notice appear in all copies. |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 15 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * connection flags. |
| 20 | */ |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 21 | #define SFE_CREATE_FLAG_NO_SEQ_CHECK (1<<0) |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 22 | /* Indicates that we should not check sequence numbers */ |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 23 | #define SFE_CREATE_FLAG_REMARK_PRIORITY (1<<1) |
| 24 | /* Indicates that we should remark priority of skb */ |
| 25 | #define SFE_CREATE_FLAG_REMARK_DSCP (1<<2) |
| 26 | /* Indicates that we should remark DSCP of packet */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * IPv6 address structure |
| 30 | */ |
| 31 | struct sfe_ipv6_addr { |
| 32 | __be32 addr[4]; |
| 33 | }; |
| 34 | |
| 35 | typedef union { |
| 36 | __be32 ip; |
| 37 | struct sfe_ipv6_addr ip6[1]; |
| 38 | } sfe_ip_addr_t; |
| 39 | |
| 40 | /* |
| 41 | * connection creation structure. |
| 42 | */ |
| 43 | struct sfe_connection_create { |
| 44 | int protocol; |
| 45 | struct net_device *src_dev; |
| 46 | struct net_device *dest_dev; |
| 47 | uint32_t flags; |
| 48 | uint32_t src_mtu; |
| 49 | uint32_t dest_mtu; |
| 50 | sfe_ip_addr_t src_ip; |
| 51 | sfe_ip_addr_t src_ip_xlate; |
| 52 | sfe_ip_addr_t dest_ip; |
| 53 | sfe_ip_addr_t dest_ip_xlate; |
| 54 | __be16 src_port; |
| 55 | __be16 src_port_xlate; |
| 56 | __be16 dest_port; |
| 57 | __be16 dest_port_xlate; |
| 58 | uint8_t src_mac[ETH_ALEN]; |
| 59 | uint8_t src_mac_xlate[ETH_ALEN]; |
| 60 | uint8_t dest_mac[ETH_ALEN]; |
| 61 | uint8_t dest_mac_xlate[ETH_ALEN]; |
| 62 | uint8_t src_td_window_scale; |
| 63 | uint32_t src_td_max_window; |
| 64 | uint32_t src_td_end; |
| 65 | uint32_t src_td_max_end; |
| 66 | uint8_t dest_td_window_scale; |
| 67 | uint32_t dest_td_max_window; |
| 68 | uint32_t dest_td_end; |
| 69 | uint32_t dest_td_max_end; |
| 70 | uint32_t mark; |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 71 | #ifdef CONFIG_XFRM |
| 72 | uint32_t original_accel; |
| 73 | uint32_t reply_accel; |
| 74 | #endif |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 75 | uint32_t src_priority; |
| 76 | uint32_t dest_priority; |
| 77 | uint32_t src_dscp; |
| 78 | uint32_t dest_dscp; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * connection destruction structure. |
| 83 | */ |
| 84 | struct sfe_connection_destroy { |
| 85 | int protocol; |
| 86 | sfe_ip_addr_t src_ip; |
| 87 | sfe_ip_addr_t dest_ip; |
| 88 | __be16 src_port; |
| 89 | __be16 dest_port; |
| 90 | }; |
| 91 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 92 | typedef enum sfe_sync_reason { |
| 93 | SFE_SYNC_REASON_STATS, /* Sync is to synchronize stats */ |
| 94 | SFE_SYNC_REASON_FLUSH, /* Sync is to flush a entry */ |
| 95 | SFE_SYNC_REASON_DESTROY /* Sync is to destroy a entry(requested by connection manager) */ |
| 96 | } sfe_sync_reason_t; |
| 97 | |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Structure used to sync connection stats/state back within the system. |
| 100 | * |
| 101 | * NOTE: The addresses here are NON-NAT addresses, i.e. the true endpoint addressing. |
| 102 | * 'src' is the creator of the connection. |
| 103 | */ |
| 104 | struct sfe_connection_sync { |
| 105 | struct net_device *src_dev; |
| 106 | struct net_device *dest_dev; |
| 107 | int is_v6; /* Is it for ipv6? */ |
| 108 | int protocol; /* IP protocol number (IPPROTO_...) */ |
| 109 | sfe_ip_addr_t src_ip; /* Non-NAT source address, i.e. the creator of the connection */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 110 | sfe_ip_addr_t src_ip_xlate; /* NATed source address */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 111 | __be16 src_port; /* Non-NAT source port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 112 | __be16 src_port_xlate; /* NATed source port */ |
| 113 | sfe_ip_addr_t dest_ip; /* Non-NAT destination address, i.e. to whom the connection was created */ |
| 114 | sfe_ip_addr_t dest_ip_xlate; /* NATed destination address */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 115 | __be16 dest_port; /* Non-NAT destination port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 116 | __be16 dest_port_xlate; /* NATed destination port */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 117 | uint32_t src_td_max_window; |
| 118 | uint32_t src_td_end; |
| 119 | uint32_t src_td_max_end; |
| 120 | uint64_t src_packet_count; |
| 121 | uint64_t src_byte_count; |
| 122 | uint32_t src_new_packet_count; |
| 123 | uint32_t src_new_byte_count; |
| 124 | uint32_t dest_td_max_window; |
| 125 | uint32_t dest_td_end; |
| 126 | uint32_t dest_td_max_end; |
| 127 | uint64_t dest_packet_count; |
| 128 | uint64_t dest_byte_count; |
| 129 | uint32_t dest_new_packet_count; |
| 130 | uint32_t dest_new_byte_count; |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 131 | uint32_t reason; /* reason for stats sync message, i.e. destroy, flush, period sync */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 132 | uint64_t delta_jiffies; /* Time to be added to the current timeout to keep the connection alive */ |
| 133 | }; |
| 134 | |
| 135 | /* |
| 136 | * connection mark structure |
| 137 | */ |
| 138 | struct sfe_connection_mark { |
| 139 | int protocol; |
| 140 | sfe_ip_addr_t src_ip; |
| 141 | sfe_ip_addr_t dest_ip; |
| 142 | __be16 src_port; |
| 143 | __be16 dest_port; |
| 144 | uint32_t mark; |
| 145 | }; |
| 146 | |
| 147 | /* |
| 148 | * Type used for a sync rule callback. |
| 149 | */ |
| 150 | typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *); |
| 151 | |
| 152 | /* |
| 153 | * IPv4 APIs used by connection manager |
| 154 | */ |
| 155 | extern int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb); |
| 156 | extern int sfe_ipv4_create_rule(struct sfe_connection_create *sic); |
| 157 | extern void sfe_ipv4_destroy_rule(struct sfe_connection_destroy *sid); |
| 158 | extern void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev); |
| 159 | extern void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
| 160 | extern void sfe_ipv4_update_rule(struct sfe_connection_create *sic); |
| 161 | extern void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 162 | |
| 163 | #ifdef SFE_SUPPORT_IPV6 |
| 164 | /* |
| 165 | * IPv6 APIs used by connection manager |
| 166 | */ |
| 167 | extern int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb); |
| 168 | extern int sfe_ipv6_create_rule(struct sfe_connection_create *sic); |
| 169 | extern void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid); |
| 170 | extern void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev); |
| 171 | extern void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
| 172 | extern void sfe_ipv6_update_rule(struct sfe_connection_create *sic); |
| 173 | extern void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark); |
| 174 | #else |
| 175 | static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb) |
| 176 | { |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static inline int sfe_ipv6_create_rule(struct sfe_connection_create *sic) |
| 181 | { |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | static inline void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid) |
| 186 | { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev) |
| 191 | { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback) |
| 196 | { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | static inline void sfe_ipv6_update_rule(struct sfe_connection_create *sic) |
| 201 | { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark) |
| 206 | { |
| 207 | return; |
| 208 | } |
| 209 | #endif |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * sfe_ipv6_addr_equal() |
| 213 | * compare ipv6 address |
| 214 | * |
| 215 | * return: 1, equal; 0, no equal |
| 216 | */ |
| 217 | static inline int sfe_ipv6_addr_equal(struct sfe_ipv6_addr *a, |
| 218 | struct sfe_ipv6_addr *b) |
| 219 | { |
| 220 | return a->addr[0] == b->addr[0] && |
| 221 | a->addr[1] == b->addr[1] && |
| 222 | a->addr[2] == b->addr[2] && |
| 223 | a->addr[3] == b->addr[3]; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * sfe_ipv4_addr_equal() |
| 228 | * compare ipv4 address |
| 229 | * |
| 230 | * return: 1, equal; 0, no equal |
| 231 | */ |
| 232 | #define sfe_ipv4_addr_equal(a, b) ((uint32_t)(a) == (uint32_t)(b)) |
| 233 | |
| 234 | /* |
| 235 | * sfe_addr_equal() |
| 236 | * compare ipv4 or ipv6 address |
| 237 | * |
| 238 | * return: 1, equal; 0, no equal |
| 239 | */ |
| 240 | static inline int sfe_addr_equal(sfe_ip_addr_t *a, |
| 241 | sfe_ip_addr_t *b, int is_v4) |
| 242 | { |
| 243 | return is_v4 ? sfe_ipv4_addr_equal(a->ip, b->ip) : sfe_ipv6_addr_equal(a->ip6, b->ip6); |
| 244 | } |