blob: 12f2ffab6149809d2b4c8d30790cdea8bb8ba8bd [file] [log] [blame]
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001/*
2 * sfe_cm.h
3 * Shortcut forwarding engine.
4 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -07005 * 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 Fand44a5b42015-05-26 17:37:37 -070016 */
17
18/*
19 * connection flags.
20 */
Xiaoping Fane1963d42015-08-25 17:06:19 -070021#define SFE_CREATE_FLAG_NO_SEQ_CHECK (1<<0)
Xiaoping Fand44a5b42015-05-26 17:37:37 -070022 /* Indicates that we should not check sequence numbers */
Xiaoping Fane1963d42015-08-25 17:06:19 -070023#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 Fand44a5b42015-05-26 17:37:37 -070027
28/*
29 * IPv6 address structure
30 */
31struct sfe_ipv6_addr {
32 __be32 addr[4];
33};
34
35typedef union {
36 __be32 ip;
37 struct sfe_ipv6_addr ip6[1];
38} sfe_ip_addr_t;
39
40/*
41 * connection creation structure.
42 */
43struct 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 Chen8748eb32015-06-18 12:58:48 -070071#ifdef CONFIG_XFRM
72 uint32_t original_accel;
73 uint32_t reply_accel;
74#endif
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070075 uint32_t src_priority;
76 uint32_t dest_priority;
77 uint32_t src_dscp;
78 uint32_t dest_dscp;
Xiaoping Fand44a5b42015-05-26 17:37:37 -070079};
80
81/*
82 * connection destruction structure.
83 */
84struct 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 Fan99cb4c12015-08-21 19:07:32 -070092typedef 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 Fand44a5b42015-05-26 17:37:37 -070098/*
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 */
104struct 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 Fan99cb4c12015-08-21 19:07:32 -0700110 sfe_ip_addr_t src_ip_xlate; /* NATed source address */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700111 __be16 src_port; /* Non-NAT source port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700112 __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 Fand44a5b42015-05-26 17:37:37 -0700115 __be16 dest_port; /* Non-NAT destination port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700116 __be16 dest_port_xlate; /* NATed destination port */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700117 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 Fan99cb4c12015-08-21 19:07:32 -0700131 uint32_t reason; /* reason for stats sync message, i.e. destroy, flush, period sync */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700132 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 */
138struct 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 */
150typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *);
151
152/*
153 * IPv4 APIs used by connection manager
154 */
155extern int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb);
156extern int sfe_ipv4_create_rule(struct sfe_connection_create *sic);
157extern void sfe_ipv4_destroy_rule(struct sfe_connection_destroy *sid);
158extern void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev);
159extern void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
160extern void sfe_ipv4_update_rule(struct sfe_connection_create *sic);
161extern void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700162
163#ifdef SFE_SUPPORT_IPV6
164/*
165 * IPv6 APIs used by connection manager
166 */
167extern int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb);
168extern int sfe_ipv6_create_rule(struct sfe_connection_create *sic);
169extern void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid);
170extern void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev);
171extern void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
172extern void sfe_ipv6_update_rule(struct sfe_connection_create *sic);
173extern void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark);
174#else
175static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb)
176{
177 return 0;
178}
179
180static inline int sfe_ipv6_create_rule(struct sfe_connection_create *sic)
181{
182 return -1;
183}
184
185static inline void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid)
186{
187 return;
188}
189
190static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
191{
192 return;
193}
194
195static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback)
196{
197 return;
198}
199
200static inline void sfe_ipv6_update_rule(struct sfe_connection_create *sic)
201{
202 return;
203}
204
205static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark)
206{
207 return;
208}
209#endif
Xiaoping Fan9b6bb332016-04-05 19:21:26 -0700210
211/*
212 * sfe_ipv6_addr_equal()
213 * compare ipv6 address
214 *
215 * return: 1, equal; 0, no equal
216 */
217static 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 */
240static 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}