xref: /linux/include/net/ip_vs.h (revision 5367e14e92bbb0779612d36cce7c6e95d3bc5e68)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* IP Virtual Server
3  * data structure and functionality definitions
4  */
5 
6 #ifndef _NET_IP_VS_H
7 #define _NET_IP_VS_H
8 
9 #include <linux/ip_vs.h>                /* definitions shared with userland */
10 
11 #include <asm/types.h>                  /* for __uXX types */
12 
13 #include <linux/list.h>                 /* for struct list_head */
14 #include <linux/rculist_bl.h>           /* for struct hlist_bl_head */
15 #include <linux/spinlock.h>             /* for struct rwlock_t */
16 #include <linux/atomic.h>               /* for struct atomic_t */
17 #include <linux/refcount.h>             /* for struct refcount_t */
18 #include <linux/workqueue.h>
19 
20 #include <linux/compiler.h>
21 #include <linux/timer.h>
22 #include <linux/bug.h>
23 
24 #include <net/checksum.h>
25 #include <linux/netfilter.h>		/* for union nf_inet_addr */
26 #include <linux/ip.h>
27 #include <linux/ipv6.h>			/* for struct ipv6hdr */
28 #include <net/ipv6.h>
29 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
30 #include <net/netfilter/nf_conntrack.h>
31 #endif
32 #include <net/net_namespace.h>		/* Netw namespace */
33 #include <linux/sched/isolation.h>
34 #include <linux/siphash.h>
35 
36 #define IP_VS_HDR_INVERSE	1
37 #define IP_VS_HDR_ICMP		2
38 
39 /* Destination Server Config Flags */
40 #define IP_VS_DEST_CF_AVAILABLE	0x0001		/* server is available */
41 
42 /* conn_tab limits (as per Kconfig) */
43 #define IP_VS_CONN_TAB_MIN_BITS	8
44 #if BITS_PER_LONG > 32
45 #define IP_VS_CONN_TAB_MAX_BITS	27
46 #else
47 #define IP_VS_CONN_TAB_MAX_BITS	20
48 #endif
49 
50 /* conn_max limits */
51 #if BITS_PER_LONG > 32
52 /* Limit of atomic_t but restricted by roundup_pow_of_two() in ip_vs_core.c */
53 #define IP_VS_CONN_MAX	(1 << 30)
54 #else
55 #define IP_VS_CONN_MAX	(1 << 24)
56 #endif
57 
58 /* svc_table limits */
59 #define IP_VS_SVC_TAB_MIN_BITS	4
60 #define IP_VS_SVC_TAB_MAX_BITS	20
61 
62 /* Generic access of ipvs struct */
63 static inline struct netns_ipvs *net_ipvs(struct net* net)
64 {
65 	return net->ipvs;
66 }
67 
68 /* Connections' size value needed by ip_vs_ctl.c */
69 extern int ip_vs_conn_tab_size;
70 
71 struct ip_vs_iphdr {
72 	int hdr_flags;	/* ipvs flags */
73 	__u32 off;	/* Where IP or IPv4 header starts */
74 	__u32 len;	/* IPv4 simply where L4 starts
75 			 * IPv6 where L4 Transport Header starts */
76 	__u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
77 	__s16 protocol;
78 	__s32 flags;
79 	union nf_inet_addr saddr;
80 	union nf_inet_addr daddr;
81 };
82 
83 static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
84 				      int len, void *buffer)
85 {
86 	return skb_header_pointer(skb, offset, len, buffer);
87 }
88 
89 /* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6.
90  * IPv6 requires some extra work, as finding proper header position,
91  * depend on the IPv6 extension headers.
92  */
93 static inline int
94 ip_vs_fill_iph_skb_off(int af, const struct sk_buff *skb, int offset,
95 		       int hdr_flags, struct ip_vs_iphdr *iphdr)
96 {
97 	iphdr->hdr_flags = hdr_flags;
98 	iphdr->off = offset;
99 
100 #ifdef CONFIG_IP_VS_IPV6
101 	if (af == AF_INET6) {
102 		struct ipv6hdr _iph;
103 		const struct ipv6hdr *iph = skb_header_pointer(
104 			skb, offset, sizeof(_iph), &_iph);
105 		if (!iph)
106 			return 0;
107 
108 		iphdr->saddr.in6 = iph->saddr;
109 		iphdr->daddr.in6 = iph->daddr;
110 		/* ipv6_find_hdr() updates len, flags */
111 		iphdr->len	 = offset;
112 		iphdr->flags	 = 0;
113 		iphdr->protocol  = ipv6_find_hdr(skb, &iphdr->len, -1,
114 						 &iphdr->fragoffs,
115 						 &iphdr->flags);
116 		if (iphdr->protocol < 0)
117 			return 0;
118 	} else
119 #endif
120 	{
121 		struct iphdr _iph;
122 		const struct iphdr *iph = skb_header_pointer(
123 			skb, offset, sizeof(_iph), &_iph);
124 		if (!iph)
125 			return 0;
126 
127 		iphdr->len	= offset + iph->ihl * 4;
128 		iphdr->fragoffs	= 0;
129 		iphdr->protocol	= iph->protocol;
130 		iphdr->saddr.ip	= iph->saddr;
131 		iphdr->daddr.ip	= iph->daddr;
132 	}
133 
134 	return 1;
135 }
136 
137 static inline int
138 ip_vs_fill_iph_skb_icmp(int af, const struct sk_buff *skb, int offset,
139 			bool inverse, struct ip_vs_iphdr *iphdr)
140 {
141 	int hdr_flags = IP_VS_HDR_ICMP;
142 
143 	if (inverse)
144 		hdr_flags |= IP_VS_HDR_INVERSE;
145 
146 	return ip_vs_fill_iph_skb_off(af, skb, offset, hdr_flags, iphdr);
147 }
148 
149 static inline int
150 ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, bool inverse,
151 		   struct ip_vs_iphdr *iphdr)
152 {
153 	int hdr_flags = 0;
154 
155 	if (inverse)
156 		hdr_flags |= IP_VS_HDR_INVERSE;
157 
158 	return ip_vs_fill_iph_skb_off(af, skb, skb_network_offset(skb),
159 				      hdr_flags, iphdr);
160 }
161 
162 static inline bool
163 ip_vs_iph_inverse(const struct ip_vs_iphdr *iph)
164 {
165 	return !!(iph->hdr_flags & IP_VS_HDR_INVERSE);
166 }
167 
168 static inline bool
169 ip_vs_iph_icmp(const struct ip_vs_iphdr *iph)
170 {
171 	return !!(iph->hdr_flags & IP_VS_HDR_ICMP);
172 }
173 
174 static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
175 				   const union nf_inet_addr *src)
176 {
177 #ifdef CONFIG_IP_VS_IPV6
178 	if (af == AF_INET6)
179 		dst->in6 = src->in6;
180 	else
181 #endif
182 	dst->ip = src->ip;
183 }
184 
185 static inline void ip_vs_addr_set(int af, union nf_inet_addr *dst,
186 				  const union nf_inet_addr *src)
187 {
188 #ifdef CONFIG_IP_VS_IPV6
189 	if (af == AF_INET6) {
190 		dst->in6 = src->in6;
191 		return;
192 	}
193 #endif
194 	dst->ip = src->ip;
195 	dst->all[1] = 0;
196 	dst->all[2] = 0;
197 	dst->all[3] = 0;
198 }
199 
200 static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
201 				   const union nf_inet_addr *b)
202 {
203 #ifdef CONFIG_IP_VS_IPV6
204 	if (af == AF_INET6)
205 		return ipv6_addr_equal(&a->in6, &b->in6);
206 #endif
207 	return a->ip == b->ip;
208 }
209 
210 #ifdef CONFIG_IP_VS_DEBUG
211 #include <linux/net.h>
212 
213 int ip_vs_get_debug_level(void);
214 
215 static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
216 					 const union nf_inet_addr *addr,
217 					 int *idx)
218 {
219 	int len;
220 #ifdef CONFIG_IP_VS_IPV6
221 	if (af == AF_INET6)
222 		len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6c]",
223 			       &addr->in6) + 1;
224 	else
225 #endif
226 		len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
227 			       &addr->ip) + 1;
228 
229 	*idx += len;
230 	BUG_ON(*idx > buf_len + 1);
231 	return &buf[*idx - len];
232 }
233 
234 #define IP_VS_DBG_BUF(level, msg, ...)					\
235 	do {								\
236 		char ip_vs_dbg_buf[160];				\
237 		int ip_vs_dbg_idx = 0;					\
238 		if (level <= ip_vs_get_debug_level())			\
239 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
240 	} while (0)
241 #define IP_VS_ERR_BUF(msg...)						\
242 	do {								\
243 		char ip_vs_dbg_buf[160];				\
244 		int ip_vs_dbg_idx = 0;					\
245 		pr_err(msg);						\
246 	} while (0)
247 
248 /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
249 #define IP_VS_DBG_ADDR(af, addr)					\
250 	ip_vs_dbg_addr(af, ip_vs_dbg_buf,				\
251 		       sizeof(ip_vs_dbg_buf), addr,			\
252 		       &ip_vs_dbg_idx)
253 
254 #define IP_VS_DBG(level, msg, ...)					\
255 	do {								\
256 		if (level <= ip_vs_get_debug_level())			\
257 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
258 	} while (0)
259 #define IP_VS_DBG_RL(msg, ...)						\
260 	do {								\
261 		if (net_ratelimit())					\
262 			printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__);	\
263 	} while (0)
264 #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg)			\
265 	do {								\
266 		if (level <= ip_vs_get_debug_level())			\
267 			pp->debug_packet(af, pp, skb, ofs, msg);	\
268 	} while (0)
269 #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg)			\
270 	do {								\
271 		if (level <= ip_vs_get_debug_level() &&			\
272 		    net_ratelimit())					\
273 			pp->debug_packet(af, pp, skb, ofs, msg);	\
274 	} while (0)
275 #else	/* NO DEBUGGING at ALL */
276 #define IP_VS_DBG_BUF(level, msg...)  do {} while (0)
277 #define IP_VS_ERR_BUF(msg...)  do {} while (0)
278 #define IP_VS_DBG(level, msg...)  do {} while (0)
279 #define IP_VS_DBG_RL(msg...)  do {} while (0)
280 #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg)	do {} while (0)
281 #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg)	do {} while (0)
282 #endif
283 
284 #define IP_VS_BUG() BUG()
285 #define IP_VS_ERR_RL(msg, ...)						\
286 	do {								\
287 		if (net_ratelimit())					\
288 			pr_err(msg, ##__VA_ARGS__);			\
289 	} while (0)
290 
291 struct ip_vs_aligned_lock {
292 	spinlock_t	l;	/* Protect buckets */
293 } ____cacheline_aligned_in_smp;
294 
295 /* For arrays per family */
296 enum {
297 	IP_VS_AF_INET,
298 	IP_VS_AF_INET6,
299 	IP_VS_AF_MAX
300 };
301 
302 static inline int ip_vs_af_index(int af)
303 {
304 	return af == AF_INET6 ? IP_VS_AF_INET6 : IP_VS_AF_INET;
305 }
306 
307 /* work_flags */
308 enum {
309 	IP_VS_WORK_SVC_RESIZE,		/* Schedule svc_resize_work */
310 	IP_VS_WORK_SVC_NORESIZE,	/* Stopping svc_resize_work */
311 	IP_VS_WORK_CONN_RESIZE,		/* Schedule conn_resize_work */
312 };
313 
314 /* The port number of FTP service (in network order). */
315 #define FTPPORT  cpu_to_be16(21)
316 #define FTPDATA  cpu_to_be16(20)
317 
318 /* TCP State Values */
319 enum {
320 	IP_VS_TCP_S_NONE = 0,
321 	IP_VS_TCP_S_ESTABLISHED,
322 	IP_VS_TCP_S_SYN_SENT,
323 	IP_VS_TCP_S_SYN_RECV,
324 	IP_VS_TCP_S_FIN_WAIT,
325 	IP_VS_TCP_S_TIME_WAIT,
326 	IP_VS_TCP_S_CLOSE,
327 	IP_VS_TCP_S_CLOSE_WAIT,
328 	IP_VS_TCP_S_LAST_ACK,
329 	IP_VS_TCP_S_LISTEN,
330 	IP_VS_TCP_S_SYNACK,
331 	IP_VS_TCP_S_LAST
332 };
333 
334 /* UDP State Values */
335 enum {
336 	IP_VS_UDP_S_NORMAL,
337 	IP_VS_UDP_S_LAST,
338 };
339 
340 /* ICMP State Values */
341 enum {
342 	IP_VS_ICMP_S_NORMAL,
343 	IP_VS_ICMP_S_LAST,
344 };
345 
346 /* SCTP State Values */
347 enum ip_vs_sctp_states {
348 	IP_VS_SCTP_S_NONE,
349 	IP_VS_SCTP_S_INIT1,
350 	IP_VS_SCTP_S_INIT,
351 	IP_VS_SCTP_S_COOKIE_SENT,
352 	IP_VS_SCTP_S_COOKIE_REPLIED,
353 	IP_VS_SCTP_S_COOKIE_WAIT,
354 	IP_VS_SCTP_S_COOKIE,
355 	IP_VS_SCTP_S_COOKIE_ECHOED,
356 	IP_VS_SCTP_S_ESTABLISHED,
357 	IP_VS_SCTP_S_SHUTDOWN_SENT,
358 	IP_VS_SCTP_S_SHUTDOWN_RECEIVED,
359 	IP_VS_SCTP_S_SHUTDOWN_ACK_SENT,
360 	IP_VS_SCTP_S_REJECTED,
361 	IP_VS_SCTP_S_CLOSED,
362 	IP_VS_SCTP_S_LAST
363 };
364 
365 /* Connection templates use bits from state */
366 #define IP_VS_CTPL_S_NONE		0x0000
367 #define IP_VS_CTPL_S_ASSURED		0x0001
368 #define IP_VS_CTPL_S_LAST		0x0002
369 
370 /* Delta sequence info structure
371  * Each ip_vs_conn has 2 (output AND input seq. changes).
372  * Only used in the VS/NAT.
373  */
374 struct ip_vs_seq {
375 	__u32			init_seq;	/* Add delta from this seq */
376 	__u32			delta;		/* Delta in sequence numbers */
377 	__u32			previous_delta;	/* Delta in sequence numbers
378 						 * before last resized pkt */
379 };
380 
381 /* counters per cpu */
382 struct ip_vs_counters {
383 	u64_stats_t	conns;		/* connections scheduled */
384 	u64_stats_t	inpkts;		/* incoming packets */
385 	u64_stats_t	outpkts;	/* outgoing packets */
386 	u64_stats_t	inbytes;	/* incoming bytes */
387 	u64_stats_t	outbytes;	/* outgoing bytes */
388 };
389 /* Stats per cpu */
390 struct ip_vs_cpu_stats {
391 	struct ip_vs_counters   cnt;
392 	struct u64_stats_sync   syncp;
393 };
394 
395 /* Default nice for estimator kthreads */
396 #define IPVS_EST_NICE		0
397 
398 /* IPVS statistics objects */
399 struct ip_vs_estimator {
400 	struct hlist_node	list;
401 
402 	u64			last_inbytes;
403 	u64			last_outbytes;
404 	u64			last_conns;
405 	u64			last_inpkts;
406 	u64			last_outpkts;
407 
408 	u64			cps;
409 	u64			inpps;
410 	u64			outpps;
411 	u64			inbps;
412 	u64			outbps;
413 
414 	s32			ktid:16,	/* kthread ID, -1=temp list */
415 				ktrow:8,	/* row/tick ID for kthread */
416 				ktcid:8;	/* chain ID for kthread tick */
417 };
418 
419 /*
420  * IPVS statistics object, 64-bit kernel version of struct ip_vs_stats_user
421  */
422 struct ip_vs_kstats {
423 	u64			conns;		/* connections scheduled */
424 	u64			inpkts;		/* incoming packets */
425 	u64			outpkts;	/* outgoing packets */
426 	u64			inbytes;	/* incoming bytes */
427 	u64			outbytes;	/* outgoing bytes */
428 
429 	u64			cps;		/* current connection rate */
430 	u64			inpps;		/* current in packet rate */
431 	u64			outpps;		/* current out packet rate */
432 	u64			inbps;		/* current in byte rate */
433 	u64			outbps;		/* current out byte rate */
434 };
435 
436 struct ip_vs_stats {
437 	struct ip_vs_kstats	kstats;		/* kernel statistics */
438 	struct ip_vs_estimator	est;		/* estimator */
439 	struct ip_vs_cpu_stats __percpu	*cpustats;	/* per cpu counters */
440 	spinlock_t		lock;		/* spin lock */
441 	struct ip_vs_kstats	kstats0;	/* reset values */
442 };
443 
444 struct ip_vs_stats_rcu {
445 	struct ip_vs_stats	s;
446 	struct rcu_head		rcu_head;
447 };
448 
449 int ip_vs_stats_init_alloc(struct ip_vs_stats *s);
450 struct ip_vs_stats *ip_vs_stats_alloc(void);
451 void ip_vs_stats_release(struct ip_vs_stats *stats);
452 void ip_vs_stats_free(struct ip_vs_stats *stats);
453 
454 /* Process estimators in multiple timer ticks (20/50/100, see ktrow) */
455 #define IPVS_EST_NTICKS		50
456 /* Estimation uses a 2-second period containing ticks (in jiffies) */
457 #define IPVS_EST_TICK		((2 * HZ) / IPVS_EST_NTICKS)
458 
459 /* Limit of CPU load per kthread (8 for 12.5%), ratio of CPU capacity (1/C).
460  * Value of 4 and above ensures kthreads will take work without exceeding
461  * the CPU capacity under different circumstances.
462  */
463 #define IPVS_EST_LOAD_DIVISOR	8
464 
465 /* Kthreads should not have work that exceeds the CPU load above 50% */
466 #define IPVS_EST_CPU_KTHREADS	(IPVS_EST_LOAD_DIVISOR / 2)
467 
468 /* Desired number of chains per timer tick (chain load factor in 100us units),
469  * 48=4.8ms of 40ms tick (12% CPU usage):
470  * 2 sec * 1000 ms in sec * 10 (100us in ms) / 8 (12.5%) / 50
471  */
472 #define IPVS_EST_CHAIN_FACTOR	\
473 	ALIGN_DOWN(2 * 1000 * 10 / IPVS_EST_LOAD_DIVISOR / IPVS_EST_NTICKS, 8)
474 
475 /* Compiled number of chains per tick
476  * The defines should match cond_resched_rcu
477  */
478 #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
479 #define IPVS_EST_TICK_CHAINS	IPVS_EST_CHAIN_FACTOR
480 #else
481 #define IPVS_EST_TICK_CHAINS	1
482 #endif
483 
484 #if IPVS_EST_NTICKS > 127
485 #error Too many timer ticks for ktrow
486 #endif
487 
488 /* Multiple chains processed in same tick */
489 struct ip_vs_est_tick_data {
490 	struct rcu_head		rcu_head;
491 	struct hlist_head	chains[IPVS_EST_TICK_CHAINS];
492 	DECLARE_BITMAP(present, IPVS_EST_TICK_CHAINS);
493 	DECLARE_BITMAP(full, IPVS_EST_TICK_CHAINS);
494 	int			chain_len[IPVS_EST_TICK_CHAINS];
495 };
496 
497 /* Context for estimation kthread */
498 struct ip_vs_est_kt_data {
499 	struct netns_ipvs	*ipvs;
500 	struct task_struct	*task;		/* task if running */
501 	struct ip_vs_est_tick_data __rcu *ticks[IPVS_EST_NTICKS];
502 	DECLARE_BITMAP(avail, IPVS_EST_NTICKS);	/* tick has space for ests */
503 	unsigned long		est_timer;	/* estimation timer (jiffies) */
504 	struct ip_vs_stats	*calc_stats;	/* Used for calculation */
505 	int			needed;		/* task is needed */
506 	int			tick_len[IPVS_EST_NTICKS];	/* est count */
507 	int			id;		/* ktid per netns */
508 	int			chain_max;	/* max ests per tick chain */
509 	int			tick_max;	/* max ests per tick */
510 	int			est_count;	/* attached ests to kthread */
511 	int			est_max_count;	/* max ests per kthread */
512 	int			add_row;	/* row for new ests */
513 	int			est_row;	/* estimated row */
514 };
515 
516 /* IPVS resizable hash tables */
517 struct ip_vs_rht {
518 	struct hlist_bl_head		*buckets;
519 	struct ip_vs_rht __rcu		*new_tbl; /* New/Same table	*/
520 	seqcount_t			*seqc;	/* Protects moves	*/
521 	struct ip_vs_aligned_lock	*lock;	/* Protect seqc		*/
522 	int				mask;	/* Buckets mask		*/
523 	int				size;	/* Buckets		*/
524 	int				seqc_mask; /* seqc mask		*/
525 	int				lock_mask; /* lock mask		*/
526 	u32				table_id;
527 	int				u_thresh; /* upper threshold	*/
528 	int				l_thresh; /* lower threshold	*/
529 	int				lfactor;  /* Load Factor (shift)*/
530 	int				bits;	/* size = 1 << bits	*/
531 	siphash_key_t			hash_key;
532 	struct rcu_head			rcu_head;
533 };
534 
535 /**
536  * ip_vs_rht_for_each_table() - Walk the hash tables
537  * @table:	struct ip_vs_rht __rcu *table
538  * @t:		current table, used as cursor, struct ip_vs_rht *var
539  * @p:		previous table, temp struct ip_vs_rht *var
540  *
541  * Walk tables assuming others can not change the installed tables
542  */
543 #define ip_vs_rht_for_each_table(table, t, p)				\
544 	for (p = NULL, t = rcu_dereference_protected(table, 1);		\
545 	     t != p;							\
546 	     p = t, t = rcu_dereference_protected(t->new_tbl, 1))
547 
548 /**
549  * ip_vs_rht_for_each_table_rcu() - Walk the hash tables under RCU reader lock
550  * @table:	struct ip_vs_rht __rcu *table
551  * @t:		current table, used as cursor, struct ip_vs_rht *var
552  * @p:		previous table, temp struct ip_vs_rht *var
553  *
554  * We usually search in one table and also in second table on resizing
555  */
556 #define ip_vs_rht_for_each_table_rcu(table, t, p)			\
557 	for (p = NULL, t = rcu_dereference(table);			\
558 	     t != p;							\
559 	     p = t, t = rcu_dereference(t->new_tbl))
560 
561 /**
562  * ip_vs_rht_for_each_bucket() - Walk all table buckets
563  * @t:		current table, used as cursor, struct ip_vs_rht *var
564  * @bucket:	bucket index, used as cursor, u32 var
565  * @head:	bucket address, used as cursor, struct hlist_bl_head *var
566  */
567 #define ip_vs_rht_for_each_bucket(t, bucket, head)			\
568 	for (bucket = 0, head = (t)->buckets;				\
569 	     bucket < t->size; bucket++, head++)
570 
571 /**
572  * ip_vs_rht_for_bucket_retry() - Retry bucket if entries are moved
573  * @t:		current table, used as cursor, struct ip_vs_rht *var
574  * @bucket:	index of current bucket or hash key
575  * @sc:		temp seqcount_t *var
576  * @seq:	temp unsigned int var for sequence count
577  * @retry:	temp int var
578  */
579 #define ip_vs_rht_for_bucket_retry(t, bucket, sc, seq, retry)		\
580 	for (retry = 1, sc = &(t)->seqc[(bucket) & (t)->seqc_mask];	\
581 	     retry && ({ seq = read_seqcount_begin(sc); 1; });		\
582 	     retry = read_seqcount_retry(sc, seq))
583 
584 /**
585  * DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU() - Declare variables
586  *
587  * Variables for ip_vs_rht_walk_buckets_rcu
588  */
589 #define DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU()				\
590 	struct ip_vs_rht *_t, *_p;					\
591 	unsigned int _seq;						\
592 	seqcount_t *_sc;						\
593 	u32 _bucket;							\
594 	int _retry
595 /**
596  * ip_vs_rht_walk_buckets_rcu() - Walk all buckets under RCU read lock
597  * @table:	struct ip_vs_rht __rcu *table
598  * @head:	bucket address, used as cursor, struct hlist_bl_head *var
599  *
600  * Can be used while others add/delete/move entries
601  * Not suitable if duplicates are not desired
602  * Possible cases for reader that uses cond_resched_rcu() in the loop:
603  * - new table can not be installed, no need to repeat
604  * - new table can be installed => check and repeat if new table is
605  * installed, needed for !PREEMPT_RCU
606  */
607 #define ip_vs_rht_walk_buckets_rcu(table, head)				\
608 	ip_vs_rht_for_each_table_rcu(table, _t, _p)			\
609 		ip_vs_rht_for_each_bucket(_t, _bucket, head)		\
610 			ip_vs_rht_for_bucket_retry(_t, _bucket, _sc,	\
611 						   _seq, _retry)
612 
613 /**
614  * DECLARE_IP_VS_RHT_WALK_BUCKET_RCU() - Declare variables
615  *
616  * Variables for ip_vs_rht_walk_bucket_rcu
617  */
618 #define DECLARE_IP_VS_RHT_WALK_BUCKET_RCU()				\
619 	unsigned int _seq;						\
620 	seqcount_t *_sc;						\
621 	int _retry
622 /**
623  * ip_vs_rht_walk_bucket_rcu() - Walk bucket under RCU read lock
624  * @t:		current table, struct ip_vs_rht *var
625  * @bucket:	index of current bucket or hash key
626  * @head:	bucket address, used as cursor, struct hlist_bl_head *var
627  *
628  * Can be used while others add/delete/move entries
629  * Not suitable if duplicates are not desired
630  * Possible cases for reader that uses cond_resched_rcu() in the loop:
631  * - new table can not be installed, no need to repeat
632  * - new table can be installed => check and repeat if new table is
633  * installed, needed for !PREEMPT_RCU
634  */
635 #define ip_vs_rht_walk_bucket_rcu(t, bucket, head)			\
636 	if (({ head = (t)->buckets + ((bucket) & (t)->mask); 0; }))	\
637 		{}							\
638 	else								\
639 		ip_vs_rht_for_bucket_retry(t, (bucket), _sc, _seq, _retry)
640 
641 /**
642  * DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU() - Declare variables
643  *
644  * Variables for ip_vs_rht_walk_buckets_safe_rcu
645  */
646 #define DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU()			\
647 	struct ip_vs_rht *_t, *_p;					\
648 	u32 _bucket
649 /**
650  * ip_vs_rht_walk_buckets_safe_rcu() - Walk all buckets under RCU read lock
651  * @table:	struct ip_vs_rht __rcu *table
652  * @head:	bucket address, used as cursor, struct hlist_bl_head *var
653  *
654  * Can be used while others add/delete entries but moving is disabled
655  * Using cond_resched_rcu() should be safe if tables do not change
656  */
657 #define ip_vs_rht_walk_buckets_safe_rcu(table, head)			\
658 	ip_vs_rht_for_each_table_rcu(table, _t, _p)			\
659 		ip_vs_rht_for_each_bucket(_t, _bucket, head)
660 
661 /**
662  * DECLARE_IP_VS_RHT_WALK_BUCKETS() - Declare variables
663  *
664  * Variables for ip_vs_rht_walk_buckets
665  */
666 #define DECLARE_IP_VS_RHT_WALK_BUCKETS()				\
667 	struct ip_vs_rht *_t, *_p;					\
668 	u32 _bucket
669 
670 /**
671  * ip_vs_rht_walk_buckets() - Walk all buckets
672  * @table:	struct ip_vs_rht __rcu *table
673  * @head:	bucket address, used as cursor, struct hlist_bl_head *var
674  *
675  * Use if others can not add/delete/move entries
676  */
677 #define ip_vs_rht_walk_buckets(table, head)				\
678 	ip_vs_rht_for_each_table(table, _t, _p)				\
679 		ip_vs_rht_for_each_bucket(_t, _bucket, head)
680 
681 /* Entries can be in one of two tables, so we flip bit when new table is
682  * created and store it as highest bit in hash keys
683  */
684 #define IP_VS_RHT_TABLE_ID_MASK	BIT(31)
685 
686 /* Check if hash key is from this table */
687 static inline bool ip_vs_rht_same_table(struct ip_vs_rht *t, u32 hash_key)
688 {
689 	return !((t->table_id ^ hash_key) & IP_VS_RHT_TABLE_ID_MASK);
690 }
691 
692 /* Build per-table hash key from hash value */
693 static inline u32 ip_vs_rht_build_hash_key(struct ip_vs_rht *t, u32 hash)
694 {
695 	return t->table_id | (hash & ~IP_VS_RHT_TABLE_ID_MASK);
696 }
697 
698 void ip_vs_rht_free(struct ip_vs_rht *t);
699 void ip_vs_rht_rcu_free(struct rcu_head *head);
700 struct ip_vs_rht *ip_vs_rht_alloc(int buckets, int scounts, int locks);
701 int ip_vs_rht_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t, int n,
702 			   int lfactor, int min_bits, int max_bits);
703 void ip_vs_rht_set_thresholds(struct ip_vs_rht *t, int size, int lfactor,
704 			      int min_bits, int max_bits);
705 u32 ip_vs_rht_hash_linfo(struct ip_vs_rht *t, int af,
706 			 const union nf_inet_addr *addr, u32 v1, u32 v2);
707 
708 struct dst_entry;
709 struct iphdr;
710 struct ip_vs_conn;
711 struct ip_vs_app;
712 struct sk_buff;
713 struct ip_vs_proto_data;
714 
715 struct ip_vs_protocol {
716 	struct ip_vs_protocol	*next;
717 	char			*name;
718 	u16			protocol;
719 	u16			num_states;
720 	int			dont_defrag;
721 
722 	void (*init)(struct ip_vs_protocol *pp);
723 
724 	void (*exit)(struct ip_vs_protocol *pp);
725 
726 	int (*init_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd);
727 
728 	void (*exit_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd);
729 
730 	int (*conn_schedule)(struct netns_ipvs *ipvs,
731 			     int af, struct sk_buff *skb,
732 			     struct ip_vs_proto_data *pd,
733 			     int *verdict, struct ip_vs_conn **cpp,
734 			     struct ip_vs_iphdr *iph);
735 
736 	struct ip_vs_conn *
737 	(*conn_in_get)(struct netns_ipvs *ipvs,
738 		       int af,
739 		       const struct sk_buff *skb,
740 		       const struct ip_vs_iphdr *iph);
741 
742 	struct ip_vs_conn *
743 	(*conn_out_get)(struct netns_ipvs *ipvs,
744 			int af,
745 			const struct sk_buff *skb,
746 			const struct ip_vs_iphdr *iph);
747 
748 	int (*snat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
749 			    struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
750 
751 	int (*dnat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
752 			    struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
753 
754 	const char *(*state_name)(int state);
755 
756 	void (*state_transition)(struct ip_vs_conn *cp, int direction,
757 				 const struct sk_buff *skb,
758 				 struct ip_vs_proto_data *pd,
759 				 unsigned int iph_len);
760 
761 	int (*register_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc);
762 
763 	void (*unregister_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc);
764 
765 	int (*app_conn_bind)(struct ip_vs_conn *cp);
766 
767 	void (*debug_packet)(int af, struct ip_vs_protocol *pp,
768 			     const struct sk_buff *skb,
769 			     int offset,
770 			     const char *msg);
771 
772 	void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
773 };
774 
775 /* protocol data per netns */
776 struct ip_vs_proto_data {
777 	struct ip_vs_proto_data	*next;
778 	struct ip_vs_protocol	*pp;
779 	int			*timeout_table;	/* protocol timeout table */
780 	atomic_t		appcnt;		/* counter of proto app incs. */
781 	struct tcp_states_t	*tcp_state_table;
782 };
783 
784 struct ip_vs_protocol   *ip_vs_proto_get(unsigned short proto);
785 struct ip_vs_proto_data *ip_vs_proto_data_get(struct netns_ipvs *ipvs,
786 					      unsigned short proto);
787 
788 struct ip_vs_conn_param {
789 	struct netns_ipvs		*ipvs;
790 	const union nf_inet_addr	*caddr;
791 	const union nf_inet_addr	*vaddr;
792 	__be16				cport;
793 	__be16				vport;
794 	__u16				protocol;
795 	u16				af;
796 
797 	const struct ip_vs_pe		*pe;
798 	char				*pe_data;
799 	__u8				pe_data_len;
800 };
801 
802 /* Hash node in conn_tab */
803 struct ip_vs_conn_hnode {
804 	struct hlist_bl_node	node;		/* node in conn_tab */
805 	u32			hash_key;	/* Key for the hash table */
806 	u8			dir;		/* 0=out->in, 1=in->out */
807 } __packed;
808 
809 /* IP_VS structure allocated for each dynamically scheduled connection */
810 struct ip_vs_conn {
811 	/* Cacheline for hash table nodes - rarely modified */
812 
813 	struct ip_vs_conn_hnode	hn0;		/* Original direction */
814 	u8			af;		/* address family */
815 	__be16                  cport;
816 	struct ip_vs_conn_hnode	hn1;		/* Reply direction */
817 	u8			daf;		/* Address family of the dest */
818 	__be16                  dport;
819 	struct ip_vs_dest       *dest;          /* real server */
820 	atomic_t                n_control;      /* Number of controlled ones */
821 	volatile __u32          flags;          /* status flags */
822 	/* 44/64 */
823 
824 	struct ip_vs_conn       *control;       /* Master control connection */
825 	const struct ip_vs_pe	*pe;
826 	char			*pe_data;
827 	__u8			pe_data_len;
828 	volatile __u16          state;          /* state info */
829 	volatile __u16          old_state;      /* old state, to be used for
830 						 * state transition triggered
831 						 * synchronization
832 						 */
833 	/* 2-byte hole */
834 	/* 64/96 */
835 
836 	union nf_inet_addr      caddr;          /* client address */
837 	union nf_inet_addr      vaddr;          /* virtual address */
838 	/* 96/128 */
839 
840 	union nf_inet_addr      daddr;          /* destination address */
841 	__u32			fwmark;		/* Fire wall mark from skb */
842 	__be16                  vport;
843 	__u16                   protocol;       /* Which protocol (TCP/UDP) */
844 
845 	/* Note: we can group the following members into a structure,
846 	 * in order to save more space, and the following members are
847 	 * only used in VS/NAT anyway
848 	 */
849 	struct ip_vs_app        *app;           /* bound ip_vs_app object */
850 	void                    *app_data;      /* Application private data */
851 	/* 128/168 */
852 	struct_group(sync_conn_opt,
853 		struct ip_vs_seq  in_seq;       /* incoming seq. struct */
854 		struct ip_vs_seq  out_seq;      /* outgoing seq. struct */
855 	);
856 	/* 152/192 */
857 
858 	struct timer_list	timer;		/* Expiration timer */
859 	volatile unsigned long	timeout;	/* timeout */
860 	spinlock_t              lock;           /* lock for state transition */
861 	refcount_t		refcnt;		/* reference count */
862 	atomic_t                in_pkts;        /* incoming packet counter */
863 	/* 64-bit: 4-byte gap */
864 
865 	/* 188/256 */
866 	unsigned long		sync_endtime;	/* jiffies + sent_retries */
867 	struct netns_ipvs	*ipvs;
868 
869 	/* Packet transmitter for different forwarding methods.  If it
870 	 * mangles the packet, it must return NF_DROP or better NF_STOLEN,
871 	 * otherwise this must be changed to a sk_buff **.
872 	 * NF_ACCEPT can be returned when destination is local.
873 	 */
874 	int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
875 			   struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
876 
877 	struct rcu_head		rcu_head;
878 };
879 
880 /* Extended internal versions of struct ip_vs_service_user and ip_vs_dest_user
881  * for IPv6 support.
882  *
883  * We need these to conveniently pass around service and destination
884  * options, but unfortunately, we also need to keep the old definitions to
885  * maintain userspace backwards compatibility for the setsockopt interface.
886  */
887 struct ip_vs_service_user_kern {
888 	/* virtual service addresses */
889 	u16			af;
890 	u16			protocol;
891 	union nf_inet_addr	addr;		/* virtual ip address */
892 	__be16			port;
893 	u32			fwmark;		/* firewall mark of service */
894 
895 	/* virtual service options */
896 	char			*sched_name;
897 	char			*pe_name;
898 	unsigned int		flags;		/* virtual service flags */
899 	unsigned int		timeout;	/* persistent timeout in sec */
900 	__be32			netmask;	/* persistent netmask or plen */
901 };
902 
903 
904 struct ip_vs_dest_user_kern {
905 	/* destination server address */
906 	union nf_inet_addr	addr;
907 	__be16			port;
908 
909 	/* real server options */
910 	unsigned int		conn_flags;	/* connection flags */
911 	int			weight;		/* destination weight */
912 
913 	/* thresholds for active connections */
914 	u32			u_threshold;	/* upper threshold */
915 	u32			l_threshold;	/* lower threshold */
916 
917 	/* Address family of addr */
918 	u16			af;
919 
920 	u16			tun_type;	/* tunnel type */
921 	__be16			tun_port;	/* tunnel port */
922 	u16			tun_flags;	/* tunnel flags */
923 };
924 
925 
926 /*
927  * The information about the virtual service offered to the net and the
928  * forwarding entries.
929  */
930 struct ip_vs_service {
931 	struct hlist_bl_node	s_list;   /* node in service table */
932 	u32			hash_key; /* Key for the hash table */
933 	u16			af;       /* address family */
934 	__u16			protocol; /* which protocol (TCP/UDP) */
935 
936 	union nf_inet_addr	addr;	  /* IP address for virtual service */
937 	__u32                   fwmark;   /* firewall mark of the service */
938 	atomic_t		refcnt;   /* reference counter */
939 	__be16			port;	  /* port number for the service */
940 	unsigned int		flags;	  /* service status flags */
941 	unsigned int		timeout;  /* persistent timeout in ticks */
942 	__be32			netmask;  /* grouping granularity, mask/plen */
943 	struct netns_ipvs	*ipvs;
944 
945 	struct list_head	destinations;  /* real server d-linked list */
946 	__u32			num_dests;     /* number of servers */
947 	struct ip_vs_stats      stats;         /* statistics for the service */
948 
949 	/* for scheduling */
950 	struct ip_vs_scheduler __rcu *scheduler; /* bound scheduler object */
951 	spinlock_t		sched_lock;    /* lock sched_data */
952 	void			*sched_data;   /* scheduler application data */
953 
954 	/* alternate persistence engine */
955 	struct ip_vs_pe __rcu	*pe;
956 	int			conntrack_afmask;
957 
958 	struct rcu_head		rcu_head;
959 };
960 
961 /* Information for cached dst */
962 struct ip_vs_dest_dst {
963 	struct dst_entry	*dst_cache;	/* destination cache entry */
964 	u32			dst_cookie;
965 	union nf_inet_addr	dst_saddr;
966 	struct rcu_head		rcu_head;
967 };
968 
969 /* The real server destination forwarding entry with ip address, port number,
970  * and so on.
971  */
972 struct ip_vs_dest {
973 	/* Cacheline for hash table nodes - read-mostly */
974 
975 	struct list_head	n_list;   /* for the dests in the service */
976 	struct hlist_node	d_list;   /* for table with all the dests */
977 
978 	atomic_t		weight;		/* server weight */
979 	atomic_t		last_weight;	/* server latest weight */
980 
981 	/* connection thresholds */
982 	u32			l_threshold_val;/* used lower threshold */
983 	u32			u_threshold_val;/* used upper threshold */
984 	/* 32/48 */
985 	u32			l_threshold;	/* lower threshold */
986 	u32			u_threshold;	/* upper threshold */
987 
988 	unsigned long		cflags;		/* config flags */
989 
990 	/* 44/64 */
991 	atomic_t		conn_flags;	/* flags to copy to conn */
992 
993 	__u16			tun_type;	/* tunnel type */
994 	__be16			tun_port;	/* tunnel port */
995 	__u16			tun_flags;	/* tunnel flags */
996 
997 	u16			af;		/* address family */
998 	__be16			port;		/* port number of the server */
999 	/* 60/80 */
1000 	union nf_inet_addr	addr;		/* IP address of the server */
1001 
1002 	/* connection counters */
1003 	atomic_t		totalconns;	/* total connections */
1004 	atomic_t		activeconns;	/* active connections */
1005 	atomic_t		persistconns;	/* persistent connections */
1006 
1007 	refcount_t		refcnt;		/* reference counter */
1008 	struct ip_vs_stats      stats;          /* statistics */
1009 	unsigned long		idle_start;	/* start time, jiffies */
1010 
1011 	/* for destination cache */
1012 	spinlock_t		dst_lock;	/* lock of dst_cache */
1013 	struct ip_vs_dest_dst __rcu *dest_dst;	/* cached dst info */
1014 
1015 	/* for virtual service */
1016 	struct ip_vs_service __rcu *svc;	/* service it belongs to */
1017 	__u16			protocol;	/* which protocol (TCP/UDP) */
1018 	__be16			vport;		/* virtual port number */
1019 	union nf_inet_addr	vaddr;		/* virtual IP address */
1020 	__u32			vfwmark;	/* firewall mark of service */
1021 
1022 	struct rcu_head		rcu_head;
1023 	struct list_head	t_list;		/* in dest_trash */
1024 	unsigned long		rcu_state;	/* GP cookie on unlink (trash or rehash) */
1025 	unsigned int		in_rs_table:1,	/* we are in rs_table */
1026 				need_gp:1;	/* Need GP on rehash */
1027 };
1028 
1029 /* The scheduler object */
1030 struct ip_vs_scheduler {
1031 	struct list_head	n_list;		/* d-linked list head */
1032 	char			*name;		/* scheduler name */
1033 	atomic_t		refcnt;		/* reference counter */
1034 	struct module		*module;	/* THIS_MODULE/NULL */
1035 
1036 	/* scheduler initializing service */
1037 	int (*init_service)(struct ip_vs_service *svc);
1038 	/* scheduling service finish */
1039 	void (*done_service)(struct ip_vs_service *svc);
1040 	/* dest is linked */
1041 	int (*add_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
1042 	/* dest is unlinked */
1043 	int (*del_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
1044 	/* dest is updated */
1045 	int (*upd_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
1046 
1047 	/* selecting a server from the given service */
1048 	struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
1049 				       const struct sk_buff *skb,
1050 				       struct ip_vs_iphdr *iph);
1051 };
1052 
1053 /* The persistence engine object */
1054 struct ip_vs_pe {
1055 	struct list_head	n_list;		/* d-linked list head */
1056 	char			*name;		/* scheduler name */
1057 	atomic_t		refcnt;		/* reference counter */
1058 	struct module		*module;	/* THIS_MODULE/NULL */
1059 
1060 	/* get the connection template, if any */
1061 	int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
1062 	bool (*ct_match)(const struct ip_vs_conn_param *p,
1063 			 struct ip_vs_conn *ct);
1064 	u32 (*hashkey_raw)(const struct ip_vs_conn_param *p,
1065 			   struct ip_vs_rht *t, bool inverse);
1066 	int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
1067 	/* create connections for real-server outgoing packets */
1068 	struct ip_vs_conn* (*conn_out)(struct ip_vs_service *svc,
1069 				       struct ip_vs_dest *dest,
1070 				       struct sk_buff *skb,
1071 				       const struct ip_vs_iphdr *iph,
1072 				       __be16 dport, __be16 cport);
1073 };
1074 
1075 /* The application module object (a.k.a. app incarnation) */
1076 struct ip_vs_app {
1077 	struct list_head	a_list;		/* member in app list */
1078 	int			type;		/* IP_VS_APP_TYPE_xxx */
1079 	char			*name;		/* application module name */
1080 	__u16			protocol;
1081 	struct module		*module;	/* THIS_MODULE/NULL */
1082 	struct list_head	incs_list;	/* list of incarnations */
1083 
1084 	/* members for application incarnations */
1085 	struct list_head	p_list;		/* member in proto app list */
1086 	struct ip_vs_app	*app;		/* its real application */
1087 	__be16			port;		/* port number in net order */
1088 	atomic_t		usecnt;		/* usage counter */
1089 	struct rcu_head		rcu_head;
1090 
1091 	/* output hook: Process packet in inout direction, diff set for TCP.
1092 	 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
1093 	 *	   2=Mangled but checksum was not updated
1094 	 */
1095 	int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
1096 		       struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh);
1097 
1098 	/* input hook: Process packet in outin direction, diff set for TCP.
1099 	 * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
1100 	 *	   2=Mangled but checksum was not updated
1101 	 */
1102 	int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
1103 		      struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh);
1104 
1105 	/* ip_vs_app initializer */
1106 	int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
1107 
1108 	/* ip_vs_app finish */
1109 	int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
1110 
1111 
1112 	/* not used now */
1113 	int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
1114 			 struct ip_vs_protocol *);
1115 
1116 	void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
1117 
1118 	int *			timeout_table;
1119 	int *			timeouts;
1120 	int			timeouts_size;
1121 
1122 	int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
1123 			     int *verdict, struct ip_vs_conn **cpp);
1124 
1125 	struct ip_vs_conn *
1126 	(*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
1127 		       const struct iphdr *iph, int inverse);
1128 
1129 	struct ip_vs_conn *
1130 	(*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
1131 			const struct iphdr *iph, int inverse);
1132 
1133 	int (*state_transition)(struct ip_vs_conn *cp, int direction,
1134 				const struct sk_buff *skb,
1135 				struct ip_vs_app *app);
1136 
1137 	void (*timeout_change)(struct ip_vs_app *app, int flags);
1138 };
1139 
1140 struct ipvs_master_sync_state {
1141 	struct list_head	sync_queue;
1142 	struct ip_vs_sync_buff	*sync_buff;
1143 	unsigned long		sync_queue_len;
1144 	unsigned int		sync_queue_delay;
1145 	struct delayed_work	master_wakeup_work;
1146 	struct netns_ipvs	*ipvs;
1147 };
1148 
1149 struct ip_vs_sync_thread_data;
1150 
1151 /* How much time to keep dests in trash */
1152 #define IP_VS_DEST_TRASH_PERIOD		(120 * HZ)
1153 
1154 struct ipvs_sync_daemon_cfg {
1155 	union nf_inet_addr	mcast_group;
1156 	int			syncid;
1157 	u16			sync_maxlen;
1158 	u16			mcast_port;
1159 	u8			mcast_af;
1160 	u8			mcast_ttl;
1161 	/* multicast interface name */
1162 	char			mcast_ifn[IP_VS_IFNAME_MAXLEN];
1163 };
1164 
1165 /* IPVS in network namespace */
1166 struct netns_ipvs {
1167 	int			gen;		/* Generation */
1168 	int			enable;		/* enable like nf_hooks do */
1169 	/* Hash table: for real service lookups */
1170 	#define IP_VS_RTAB_BITS 4
1171 	#define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
1172 	#define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
1173 
1174 	struct hlist_head	rs_table[IP_VS_RTAB_SIZE];
1175 	/* ip_vs_app */
1176 	struct list_head	app_list;
1177 	/* ip_vs_proto */
1178 	#define IP_VS_PROTO_TAB_SIZE	32	/* must be power of 2 */
1179 	struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
1180 	/* ip_vs_proto_tcp */
1181 #ifdef CONFIG_IP_VS_PROTO_TCP
1182 	#define	TCP_APP_TAB_BITS	4
1183 	#define	TCP_APP_TAB_SIZE	(1 << TCP_APP_TAB_BITS)
1184 	#define	TCP_APP_TAB_MASK	(TCP_APP_TAB_SIZE - 1)
1185 	struct list_head	tcp_apps[TCP_APP_TAB_SIZE];
1186 #endif
1187 	/* ip_vs_proto_udp */
1188 #ifdef CONFIG_IP_VS_PROTO_UDP
1189 	#define	UDP_APP_TAB_BITS	4
1190 	#define	UDP_APP_TAB_SIZE	(1 << UDP_APP_TAB_BITS)
1191 	#define	UDP_APP_TAB_MASK	(UDP_APP_TAB_SIZE - 1)
1192 	struct list_head	udp_apps[UDP_APP_TAB_SIZE];
1193 #endif
1194 	/* ip_vs_proto_sctp */
1195 #ifdef CONFIG_IP_VS_PROTO_SCTP
1196 	#define SCTP_APP_TAB_BITS	4
1197 	#define SCTP_APP_TAB_SIZE	(1 << SCTP_APP_TAB_BITS)
1198 	#define SCTP_APP_TAB_MASK	(SCTP_APP_TAB_SIZE - 1)
1199 	/* Hash table for SCTP application incarnations	 */
1200 	struct list_head	sctp_apps[SCTP_APP_TAB_SIZE];
1201 #endif
1202 	/* ip_vs_conn */
1203 	atomic_t		conn_count;      /* connection counter */
1204 	atomic_t		no_cport_conns[IP_VS_AF_MAX];
1205 	struct delayed_work	conn_resize_work;/* resize conn_tab */
1206 
1207 	/* ip_vs_ctl */
1208 	struct ip_vs_stats_rcu	*tot_stats;      /* Statistics & est. */
1209 
1210 	/* Trash for destinations */
1211 	struct list_head	dest_trash;
1212 	spinlock_t		dest_trash_lock;
1213 	struct timer_list	dest_trash_timer; /* expiration timer */
1214 	struct mutex		service_mutex;    /* service reconfig */
1215 	struct rw_semaphore	svc_resize_sem;   /* svc_table resizing */
1216 	struct rw_semaphore	svc_replace_sem;  /* svc_table replace */
1217 	struct delayed_work	svc_resize_work;  /* resize svc_table */
1218 	atomic_t		svc_table_changes;/* ++ on table changes */
1219 	/* Service counters */
1220 	atomic_t		num_services[IP_VS_AF_MAX];   /* Services */
1221 	atomic_t		fwm_services[IP_VS_AF_MAX];   /* Services */
1222 	atomic_t		nonfwm_services[IP_VS_AF_MAX];/* Services */
1223 	atomic_t		ftpsvc_counter[IP_VS_AF_MAX]; /* FTPPORT */
1224 	atomic_t		nullsvc_counter[IP_VS_AF_MAX];/* Zero port */
1225 	atomic_t		conn_out_counter[IP_VS_AF_MAX];/* out conn */
1226 
1227 #ifdef CONFIG_SYSCTL
1228 	/* delayed work for expiring no dest connections */
1229 	struct delayed_work	expire_nodest_conn_work;
1230 	/* 1/rate drop and drop-entry variables */
1231 	struct delayed_work	defense_work;   /* Work handler */
1232 	int			drop_rate;
1233 	int			drop_counter;
1234 	int			old_secure_tcp;
1235 	atomic_t		dropentry;
1236 	s8			dropentry_counters[8];
1237 	/* locks in ctl.c */
1238 	spinlock_t		dropentry_lock;  /* drop entry handling */
1239 	spinlock_t		droppacket_lock; /* drop packet handling */
1240 	spinlock_t		securetcp_lock;  /* state and timeout tables */
1241 
1242 	/* sys-ctl struct */
1243 	struct ctl_table_header	*sysctl_hdr;
1244 	struct ctl_table	*sysctl_tbl;
1245 #endif
1246 
1247 	/* sysctl variables */
1248 	int			sysctl_amemthresh;
1249 	int			sysctl_am_droprate;
1250 #ifdef CONFIG_SYSCTL
1251 	int			sysctl_conn_max;/* soft limit for conns */
1252 	int			conn_max_limit;	/* hard limit for conn_max */
1253 #endif
1254 	int			sysctl_drop_entry;
1255 	int			sysctl_drop_packet;
1256 	int			sysctl_secure_tcp;
1257 #ifdef CONFIG_IP_VS_NFCT
1258 	int			sysctl_conntrack;
1259 #endif
1260 	int			sysctl_snat_reroute;
1261 	int			sysctl_sync_ver;
1262 	int			sysctl_sync_ports;
1263 	int			sysctl_sync_persist_mode;
1264 	unsigned long		sysctl_sync_qlen_max;
1265 	int			sysctl_sync_sock_size;
1266 	int			sysctl_cache_bypass;
1267 	int			sysctl_expire_nodest_conn;
1268 	int			sysctl_sloppy_tcp;
1269 	int			sysctl_sloppy_sctp;
1270 	int			sysctl_expire_quiescent_template;
1271 	int			sysctl_sync_threshold[2];
1272 	unsigned int		sysctl_sync_refresh_period;
1273 	int			sysctl_sync_retries;
1274 	int			sysctl_nat_icmp_send;
1275 	int			sysctl_pmtu_disc;
1276 	int			sysctl_backup_only;
1277 	int			sysctl_conn_reuse_mode;
1278 	int			sysctl_schedule_icmp;
1279 	int			sysctl_ignore_tunneled;
1280 	int			sysctl_run_estimation;
1281 #ifdef CONFIG_SYSCTL
1282 	cpumask_var_t		sysctl_est_cpulist;	/* kthread cpumask */
1283 	int			est_cpulist_valid;	/* cpulist set */
1284 	int			sysctl_est_nice;	/* kthread nice */
1285 	int			est_stopped;		/* stop tasks */
1286 #endif
1287 	int			sysctl_conn_lfactor;
1288 	int			sysctl_svc_lfactor;
1289 
1290 	/* ip_vs_lblc */
1291 	int			sysctl_lblc_expiration;
1292 	struct ctl_table_header	*lblc_ctl_header;
1293 	struct ctl_table	*lblc_ctl_table;
1294 	/* ip_vs_lblcr */
1295 	int			sysctl_lblcr_expiration;
1296 	struct ctl_table_header	*lblcr_ctl_header;
1297 	struct ctl_table	*lblcr_ctl_table;
1298 	unsigned long		work_flags;	/* IP_VS_WORK_* flags */
1299 	/* ip_vs_est */
1300 	struct delayed_work	est_reload_work;/* Reload kthread tasks */
1301 	struct mutex		est_mutex;	/* protect kthread tasks */
1302 	struct hlist_head	est_temp_list;	/* Ests during calc phase */
1303 	struct ip_vs_est_kt_data **est_kt_arr;	/* Array of kthread data ptrs */
1304 	unsigned long		est_max_threads;/* Hard limit of kthreads */
1305 	int			est_calc_phase;	/* Calculation phase */
1306 	int			est_chain_max;	/* Calculated chain_max */
1307 	int			est_kt_count;	/* Allocated ptrs */
1308 	int			est_add_ktid;	/* ktid where to add ests */
1309 	atomic_t		est_genid;	/* kthreads reload genid */
1310 	atomic_t		est_genid_done;	/* applied genid */
1311 	/* ip_vs_sync */
1312 	spinlock_t		sync_lock;
1313 	struct ipvs_master_sync_state *ms;
1314 	spinlock_t		sync_buff_lock;
1315 	struct ip_vs_sync_thread_data *master_tinfo;
1316 	struct ip_vs_sync_thread_data *backup_tinfo;
1317 	int			threads_mask;
1318 	volatile int		sync_state;
1319 	struct mutex		sync_mutex;
1320 	struct ipvs_sync_daemon_cfg	mcfg;	/* Master Configuration */
1321 	struct ipvs_sync_daemon_cfg	bcfg;	/* Backup Configuration */
1322 	/* net name space ptr */
1323 	struct net		*net;            /* Needed by timer routines */
1324 	/* Number of heterogeneous destinations, needed because heterogeneous
1325 	 * are not supported when synchronization is enabled.
1326 	 */
1327 	unsigned int		mixed_address_family_dests;
1328 	unsigned int		hooks_afmask;	/* &1=AF_INET, &2=AF_INET6 */
1329 
1330 	struct ip_vs_rht __rcu	*svc_table;	/* Services */
1331 	struct ip_vs_rht __rcu	*conn_tab;	/* Connections */
1332 	atomic_t		conn_tab_changes;/* ++ on new table */
1333 };
1334 
1335 #define DEFAULT_SYNC_THRESHOLD	3
1336 #define DEFAULT_SYNC_PERIOD	50
1337 #define DEFAULT_SYNC_VER	1
1338 #define DEFAULT_SLOPPY_TCP	0
1339 #define DEFAULT_SLOPPY_SCTP	0
1340 #define DEFAULT_SYNC_REFRESH_PERIOD	(0U * HZ)
1341 #define DEFAULT_SYNC_RETRIES		0
1342 #define IPVS_SYNC_WAKEUP_RATE	8
1343 #define IPVS_SYNC_QLEN_MAX	(IPVS_SYNC_WAKEUP_RATE * 4)
1344 #define IPVS_SYNC_SEND_DELAY	(HZ / 50)
1345 #define IPVS_SYNC_CHECK_PERIOD	HZ
1346 #define IPVS_SYNC_FLUSH_TIME	(HZ * 2)
1347 #define IPVS_SYNC_PORTS_MAX	(1 << 6)
1348 
1349 #ifdef CONFIG_SYSCTL
1350 
1351 static inline int sysctl_conn_max(struct netns_ipvs *ipvs)
1352 {
1353 	return READ_ONCE(ipvs->sysctl_conn_max);
1354 }
1355 
1356 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
1357 {
1358 	return ipvs->sysctl_sync_threshold[0];
1359 }
1360 
1361 static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
1362 {
1363 	return READ_ONCE(ipvs->sysctl_sync_threshold[1]);
1364 }
1365 
1366 static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1367 {
1368 	return READ_ONCE(ipvs->sysctl_sync_refresh_period);
1369 }
1370 
1371 static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1372 {
1373 	return ipvs->sysctl_sync_retries;
1374 }
1375 
1376 static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
1377 {
1378 	return ipvs->sysctl_sync_ver;
1379 }
1380 
1381 static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
1382 {
1383 	return ipvs->sysctl_sloppy_tcp;
1384 }
1385 
1386 static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
1387 {
1388 	return ipvs->sysctl_sloppy_sctp;
1389 }
1390 
1391 static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1392 {
1393 	return READ_ONCE(ipvs->sysctl_sync_ports);
1394 }
1395 
1396 static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
1397 {
1398 	return ipvs->sysctl_sync_persist_mode;
1399 }
1400 
1401 static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
1402 {
1403 	return ipvs->sysctl_sync_qlen_max;
1404 }
1405 
1406 static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
1407 {
1408 	return ipvs->sysctl_sync_sock_size;
1409 }
1410 
1411 static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
1412 {
1413 	return ipvs->sysctl_pmtu_disc;
1414 }
1415 
1416 static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
1417 {
1418 	return ipvs->sync_state & IP_VS_STATE_BACKUP &&
1419 	       ipvs->sysctl_backup_only;
1420 }
1421 
1422 static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
1423 {
1424 	return ipvs->sysctl_conn_reuse_mode;
1425 }
1426 
1427 static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
1428 {
1429 	return ipvs->sysctl_expire_nodest_conn;
1430 }
1431 
1432 static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
1433 {
1434 	return ipvs->sysctl_schedule_icmp;
1435 }
1436 
1437 static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
1438 {
1439 	return ipvs->sysctl_ignore_tunneled;
1440 }
1441 
1442 static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
1443 {
1444 	return ipvs->sysctl_cache_bypass;
1445 }
1446 
1447 static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
1448 {
1449 	return ipvs->sysctl_run_estimation;
1450 }
1451 
1452 static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs)
1453 {
1454 	if (ipvs->est_cpulist_valid)
1455 		return ipvs->sysctl_est_cpulist;
1456 	else
1457 		return housekeeping_cpumask(HK_TYPE_KTHREAD);
1458 }
1459 
1460 static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs)
1461 {
1462 	if (ipvs->est_cpulist_valid)
1463 		return ipvs->sysctl_est_cpulist;
1464 	else
1465 		return NULL;
1466 }
1467 
1468 static inline int sysctl_est_nice(struct netns_ipvs *ipvs)
1469 {
1470 	return ipvs->sysctl_est_nice;
1471 }
1472 
1473 #else
1474 
1475 static inline int sysctl_conn_max(struct netns_ipvs *ipvs)
1476 {
1477 	return IP_VS_CONN_MAX;
1478 }
1479 
1480 static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
1481 {
1482 	return DEFAULT_SYNC_THRESHOLD;
1483 }
1484 
1485 static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
1486 {
1487 	return DEFAULT_SYNC_PERIOD;
1488 }
1489 
1490 static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
1491 {
1492 	return DEFAULT_SYNC_REFRESH_PERIOD;
1493 }
1494 
1495 static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
1496 {
1497 	return DEFAULT_SYNC_RETRIES & 3;
1498 }
1499 
1500 static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
1501 {
1502 	return DEFAULT_SYNC_VER;
1503 }
1504 
1505 static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
1506 {
1507 	return DEFAULT_SLOPPY_TCP;
1508 }
1509 
1510 static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
1511 {
1512 	return DEFAULT_SLOPPY_SCTP;
1513 }
1514 
1515 static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
1516 {
1517 	return 1;
1518 }
1519 
1520 static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
1521 {
1522 	return 0;
1523 }
1524 
1525 static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
1526 {
1527 	return IPVS_SYNC_QLEN_MAX;
1528 }
1529 
1530 static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
1531 {
1532 	return 0;
1533 }
1534 
1535 static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
1536 {
1537 	return 1;
1538 }
1539 
1540 static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
1541 {
1542 	return 0;
1543 }
1544 
1545 static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
1546 {
1547 	return 1;
1548 }
1549 
1550 static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
1551 {
1552 	return 0;
1553 }
1554 
1555 static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
1556 {
1557 	return 0;
1558 }
1559 
1560 static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
1561 {
1562 	return 0;
1563 }
1564 
1565 static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
1566 {
1567 	return 0;
1568 }
1569 
1570 static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
1571 {
1572 	return 1;
1573 }
1574 
1575 static inline const struct cpumask *__sysctl_est_cpulist(struct netns_ipvs *ipvs)
1576 {
1577 	return housekeeping_cpumask(HK_TYPE_KTHREAD);
1578 }
1579 
1580 static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs)
1581 {
1582 	return NULL;
1583 }
1584 
1585 static inline int sysctl_est_nice(struct netns_ipvs *ipvs)
1586 {
1587 	return IPVS_EST_NICE;
1588 }
1589 
1590 #endif
1591 
1592 /* Get load factor to map conn_count/u_thresh to t->size */
1593 static inline int sysctl_conn_lfactor(struct netns_ipvs *ipvs)
1594 {
1595 	return READ_ONCE(ipvs->sysctl_conn_lfactor);
1596 }
1597 
1598 /* Get load factor to map num_services/u_thresh to t->size
1599  * Smaller value decreases u_thresh to reduce collisions but increases
1600  * the table size
1601  * Returns factor where:
1602  * - <0: u_thresh = size >> -factor, eg. lfactor -2 = 25% load
1603  * - >=0: u_thresh = size << factor, eg. lfactor 1 = 200% load
1604  */
1605 static inline int sysctl_svc_lfactor(struct netns_ipvs *ipvs)
1606 {
1607 	return READ_ONCE(ipvs->sysctl_svc_lfactor);
1608 }
1609 
1610 static inline bool sysctl_est_cpulist_empty(struct netns_ipvs *ipvs)
1611 {
1612 	guard(rcu)();
1613 	return cpumask_empty(__sysctl_est_cpulist(ipvs));
1614 }
1615 
1616 static inline unsigned int sysctl_est_cpulist_weight(struct netns_ipvs *ipvs)
1617 {
1618 	guard(rcu)();
1619 	return cpumask_weight(__sysctl_est_cpulist(ipvs));
1620 }
1621 
1622 /* IPVS core functions
1623  * (from ip_vs_core.c)
1624  */
1625 const char *ip_vs_proto_name(unsigned int proto);
1626 void ip_vs_init_hash_table(struct list_head *table, int rows);
1627 struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
1628 				      struct ip_vs_dest *dest,
1629 				      struct sk_buff *skb,
1630 				      const struct ip_vs_iphdr *iph,
1631 				      __be16 dport,
1632 				      __be16 cport);
1633 #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
1634 
1635 #define IP_VS_APP_TYPE_FTP	1
1636 
1637 /* ip_vs_conn handling functions
1638  * (from ip_vs_conn.c)
1639  */
1640 enum {
1641 	IP_VS_DIR_INPUT = 0,
1642 	IP_VS_DIR_OUTPUT,
1643 	IP_VS_DIR_INPUT_ONLY,
1644 	IP_VS_DIR_LAST,
1645 };
1646 
1647 static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol,
1648 					 const union nf_inet_addr *caddr,
1649 					 __be16 cport,
1650 					 const union nf_inet_addr *vaddr,
1651 					 __be16 vport,
1652 					 struct ip_vs_conn_param *p)
1653 {
1654 	p->ipvs = ipvs;
1655 	p->af = af;
1656 	p->protocol = protocol;
1657 	p->caddr = caddr;
1658 	p->cport = cport;
1659 	p->vaddr = vaddr;
1660 	p->vport = vport;
1661 	p->pe = NULL;
1662 	p->pe_data = NULL;
1663 }
1664 
1665 struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
1666 struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
1667 
1668 struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
1669 					    const struct sk_buff *skb,
1670 					    const struct ip_vs_iphdr *iph);
1671 
1672 struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
1673 
1674 struct ip_vs_conn * ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
1675 					     const struct sk_buff *skb,
1676 					     const struct ip_vs_iphdr *iph);
1677 
1678 /* Get reference to gain full access to conn.
1679  * By default, RCU read-side critical sections have access only to
1680  * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
1681  */
1682 static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
1683 {
1684 	return refcount_inc_not_zero(&cp->refcnt);
1685 }
1686 
1687 /* put back the conn without restarting its timer */
1688 static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
1689 {
1690 	refcount_dec(&cp->refcnt);
1691 }
1692 void ip_vs_conn_put(struct ip_vs_conn *cp);
1693 void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
1694 int ip_vs_conn_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t,
1695 			    int lfactor);
1696 struct ip_vs_rht *ip_vs_conn_tab_alloc(struct netns_ipvs *ipvs, int buckets,
1697 				       int lfactor);
1698 
1699 static inline struct ip_vs_conn *
1700 ip_vs_hn0_to_conn(struct ip_vs_conn_hnode *hn)
1701 {
1702 	return container_of(hn, struct ip_vs_conn, hn0);
1703 }
1704 
1705 static inline struct ip_vs_conn *
1706 ip_vs_hn_to_conn(struct ip_vs_conn_hnode *hn)
1707 {
1708 	return hn->dir ? container_of(hn, struct ip_vs_conn, hn1) :
1709 			 container_of(hn, struct ip_vs_conn, hn0);
1710 }
1711 
1712 struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
1713 				  const union nf_inet_addr *daddr,
1714 				  __be16 dport, unsigned int flags,
1715 				  struct ip_vs_dest *dest, __u32 fwmark);
1716 void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
1717 
1718 const char *ip_vs_state_name(const struct ip_vs_conn *cp);
1719 
1720 void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
1721 int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest);
1722 void ip_vs_random_dropentry(struct netns_ipvs *ipvs);
1723 int ip_vs_conn_init(void);
1724 void ip_vs_conn_cleanup(void);
1725 
1726 static inline void ip_vs_control_del(struct ip_vs_conn *cp)
1727 {
1728 	struct ip_vs_conn *ctl_cp = cp->control;
1729 	if (!ctl_cp) {
1730 		IP_VS_ERR_BUF("request control DEL for uncontrolled: "
1731 			      "%s:%d to %s:%d\n",
1732 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1733 			      ntohs(cp->cport),
1734 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1735 			      ntohs(cp->vport));
1736 
1737 		return;
1738 	}
1739 
1740 	IP_VS_DBG_BUF(7, "DELeting control for: "
1741 		      "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1742 		      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1743 		      ntohs(cp->cport),
1744 		      IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1745 		      ntohs(ctl_cp->cport));
1746 
1747 	cp->control = NULL;
1748 	if (atomic_read(&ctl_cp->n_control) == 0) {
1749 		IP_VS_ERR_BUF("BUG control DEL with n=0 : "
1750 			      "%s:%d to %s:%d\n",
1751 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1752 			      ntohs(cp->cport),
1753 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1754 			      ntohs(cp->vport));
1755 
1756 		return;
1757 	}
1758 	atomic_dec(&ctl_cp->n_control);
1759 }
1760 
1761 static inline void
1762 ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
1763 {
1764 	if (cp->control) {
1765 		IP_VS_ERR_BUF("request control ADD for already controlled: "
1766 			      "%s:%d to %s:%d\n",
1767 			      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1768 			      ntohs(cp->cport),
1769 			      IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
1770 			      ntohs(cp->vport));
1771 
1772 		ip_vs_control_del(cp);
1773 	}
1774 
1775 	IP_VS_DBG_BUF(7, "ADDing control for: "
1776 		      "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
1777 		      IP_VS_DBG_ADDR(cp->af, &cp->caddr),
1778 		      ntohs(cp->cport),
1779 		      IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
1780 		      ntohs(ctl_cp->cport));
1781 
1782 	cp->control = ctl_cp;
1783 	atomic_inc(&ctl_cp->n_control);
1784 }
1785 
1786 /* Mark our template as assured */
1787 static inline void
1788 ip_vs_control_assure_ct(struct ip_vs_conn *cp)
1789 {
1790 	struct ip_vs_conn *ct = cp->control;
1791 
1792 	if (ct && !(ct->state & IP_VS_CTPL_S_ASSURED) &&
1793 	    (ct->flags & IP_VS_CONN_F_TEMPLATE))
1794 		ct->state |= IP_VS_CTPL_S_ASSURED;
1795 }
1796 
1797 /* IPVS netns init & cleanup functions */
1798 int ip_vs_estimator_net_init(struct netns_ipvs *ipvs);
1799 int ip_vs_control_net_init(struct netns_ipvs *ipvs);
1800 int ip_vs_protocol_net_init(struct netns_ipvs *ipvs);
1801 int ip_vs_app_net_init(struct netns_ipvs *ipvs);
1802 int ip_vs_conn_net_init(struct netns_ipvs *ipvs);
1803 int ip_vs_sync_net_init(struct netns_ipvs *ipvs);
1804 void ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs);
1805 void ip_vs_app_net_cleanup(struct netns_ipvs *ipvs);
1806 void ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs);
1807 void ip_vs_control_net_cleanup(struct netns_ipvs *ipvs);
1808 void ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs);
1809 void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs);
1810 void ip_vs_service_nets_cleanup(struct list_head *net_list);
1811 
1812 /* IPVS application functions
1813  * (from ip_vs_app.c)
1814  */
1815 #define IP_VS_APP_MAX_PORTS  8
1816 struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
1817 void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
1818 int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
1819 void ip_vs_unbind_app(struct ip_vs_conn *cp);
1820 int register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
1821 			   __u16 port);
1822 int ip_vs_app_inc_get(struct ip_vs_app *inc);
1823 void ip_vs_app_inc_put(struct ip_vs_app *inc);
1824 
1825 int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb,
1826 		      struct ip_vs_iphdr *ipvsh);
1827 int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb,
1828 		     struct ip_vs_iphdr *ipvsh);
1829 
1830 int register_ip_vs_pe(struct ip_vs_pe *pe);
1831 int unregister_ip_vs_pe(struct ip_vs_pe *pe);
1832 struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
1833 struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
1834 
1835 /* Use a #define to avoid all of module.h just for these trivial ops */
1836 #define ip_vs_pe_get(pe)			\
1837 	if (pe && pe->module)			\
1838 		__module_get(pe->module);
1839 
1840 #define ip_vs_pe_put(pe)			\
1841 	if (pe && pe->module)			\
1842 		module_put(pe->module);
1843 
1844 /* IPVS protocol functions (from ip_vs_proto.c) */
1845 int ip_vs_protocol_init(void);
1846 void ip_vs_protocol_cleanup(void);
1847 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
1848 int *ip_vs_create_timeout_table(int *table, int size);
1849 void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
1850 			       const struct sk_buff *skb, int offset,
1851 			       const char *msg);
1852 
1853 extern struct ip_vs_protocol ip_vs_protocol_tcp;
1854 extern struct ip_vs_protocol ip_vs_protocol_udp;
1855 extern struct ip_vs_protocol ip_vs_protocol_icmp;
1856 extern struct ip_vs_protocol ip_vs_protocol_esp;
1857 extern struct ip_vs_protocol ip_vs_protocol_ah;
1858 extern struct ip_vs_protocol ip_vs_protocol_sctp;
1859 
1860 /* Registering/unregistering scheduler functions
1861  * (from ip_vs_sched.c)
1862  */
1863 int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
1864 int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
1865 int ip_vs_bind_scheduler(struct ip_vs_service *svc,
1866 			 struct ip_vs_scheduler *scheduler);
1867 void ip_vs_unbind_scheduler(struct ip_vs_service *svc);
1868 struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
1869 void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
1870 struct ip_vs_conn *
1871 ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
1872 	       struct ip_vs_proto_data *pd, int *ignored,
1873 	       struct ip_vs_iphdr *iph);
1874 int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
1875 		struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
1876 
1877 void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
1878 
1879 /* IPVS control data and functions (from ip_vs_ctl.c) */
1880 extern struct ip_vs_stats ip_vs_stats;
1881 extern int sysctl_ip_vs_sync_ver;
1882 
1883 struct ip_vs_service *
1884 ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
1885 		  const union nf_inet_addr *vaddr, __be16 vport);
1886 
1887 bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
1888 			    const union nf_inet_addr *daddr, __be16 dport);
1889 
1890 struct ip_vs_dest *
1891 ip_vs_find_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
1892 			const union nf_inet_addr *daddr, __be16 dport);
1893 struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
1894 				     const union nf_inet_addr *daddr,
1895 				     __be16 tun_port);
1896 
1897 int ip_vs_use_count_inc(void);
1898 void ip_vs_use_count_dec(void);
1899 int ip_vs_register_nl_ioctl(void);
1900 void ip_vs_unregister_nl_ioctl(void);
1901 int ip_vs_control_init(void);
1902 void ip_vs_control_cleanup(void);
1903 struct ip_vs_dest *
1904 ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
1905 		const union nf_inet_addr *daddr, __be16 dport,
1906 		const union nf_inet_addr *vaddr, __be16 vport,
1907 		__u16 protocol, __u32 fwmark, __u32 flags);
1908 void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
1909 
1910 static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
1911 {
1912 	refcount_inc(&dest->refcnt);
1913 }
1914 
1915 static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
1916 {
1917 	smp_mb__before_atomic();
1918 	refcount_dec(&dest->refcnt);
1919 }
1920 
1921 static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
1922 {
1923 	if (refcount_dec_and_test(&dest->refcnt))
1924 		kfree(dest);
1925 }
1926 
1927 void ip_vs_dest_update_overload(struct ip_vs_dest *dest);
1928 
1929 /* Check if dest is in overloaded state */
1930 static inline bool ip_vs_dest_is_overloaded(const struct ip_vs_dest *dest)
1931 {
1932 	return READ_ONCE(dest->l_threshold_val);
1933 }
1934 
1935 /* IPVS sync daemon data and function prototypes
1936  * (from ip_vs_sync.c)
1937  */
1938 int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *cfg,
1939 		      int state);
1940 int stop_sync_thread(struct netns_ipvs *ipvs, int state);
1941 void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts);
1942 
1943 /* IPVS rate estimator prototypes (from ip_vs_est.c) */
1944 int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
1945 void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
1946 void ip_vs_zero_estimator(struct ip_vs_stats *stats);
1947 void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
1948 void ip_vs_est_reload_start(struct netns_ipvs *ipvs, bool restart);
1949 int ip_vs_est_kthread_start(struct netns_ipvs *ipvs,
1950 			    struct ip_vs_est_kt_data *kd);
1951 void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);
1952 
1953 static inline void ip_vs_stop_estimator_tot_stats(struct netns_ipvs *ipvs)
1954 {
1955 #ifdef CONFIG_SYSCTL
1956 	ip_vs_stop_estimator(ipvs, &ipvs->tot_stats->s);
1957 	ipvs->tot_stats->s.est.ktid = -2;
1958 #endif
1959 }
1960 
1961 static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs)
1962 {
1963 #ifdef CONFIG_SYSCTL
1964 	/* Stop tasks while cpulist is empty or if disabled with flag */
1965 	ipvs->est_stopped = !sysctl_run_estimation(ipvs) ||
1966 			    (ipvs->est_cpulist_valid &&
1967 			     sysctl_est_cpulist_empty(ipvs));
1968 #endif
1969 }
1970 
1971 static inline bool ip_vs_est_stopped(struct netns_ipvs *ipvs)
1972 {
1973 #ifdef CONFIG_SYSCTL
1974 	return ipvs->est_stopped;
1975 #else
1976 	return false;
1977 #endif
1978 }
1979 
1980 static inline int ip_vs_est_max_threads(struct netns_ipvs *ipvs)
1981 {
1982 	unsigned int limit = IPVS_EST_CPU_KTHREADS *
1983 			     sysctl_est_cpulist_weight(ipvs);
1984 
1985 	return max(1U, limit);
1986 }
1987 
1988 /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
1989 int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1990 		    struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
1991 int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1992 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
1993 int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1994 		   struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
1995 int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1996 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
1997 int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
1998 		  struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
1999 int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
2000 		    struct ip_vs_protocol *pp, unsigned int toff,
2001 		    unsigned int hooknum, struct ip_vs_iphdr *ciph);
2002 void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
2003 
2004 #ifdef CONFIG_IP_VS_IPV6
2005 int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
2006 			 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
2007 int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
2008 		      struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
2009 int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
2010 			 struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
2011 int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
2012 		     struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
2013 int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
2014 		       struct ip_vs_protocol *pp, unsigned int toff,
2015 		       unsigned int hooknum, struct ip_vs_iphdr *ciph);
2016 #endif
2017 
2018 #ifdef CONFIG_SYSCTL
2019 /* This is a simple mechanism to ignore packets when
2020  * we are loaded. Just set ip_vs_drop_rate to 'n' and
2021  * we start to drop 1/rate of the packets
2022  */
2023 static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
2024 {
2025 	if (!ipvs->drop_rate)
2026 		return 0;
2027 	if (--ipvs->drop_counter > 0)
2028 		return 0;
2029 	ipvs->drop_counter = ipvs->drop_rate;
2030 	return 1;
2031 }
2032 #else
2033 static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
2034 #endif
2035 
2036 #ifdef CONFIG_SYSCTL
2037 /* Enqueue delayed work for expiring no dest connections
2038  * Only run when sysctl_expire_nodest=1
2039  */
2040 static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs)
2041 {
2042 	if (sysctl_expire_nodest_conn(ipvs))
2043 		queue_delayed_work(system_long_wq,
2044 				   &ipvs->expire_nodest_conn_work, 1);
2045 }
2046 
2047 void ip_vs_expire_nodest_conn_flush(struct netns_ipvs *ipvs);
2048 #else
2049 static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs) {}
2050 #endif
2051 
2052 #define IP_VS_DFWD_METHOD(dest) (atomic_read(&(dest)->conn_flags) & \
2053 				 IP_VS_CONN_F_FWD_MASK)
2054 
2055 /* ip_vs_fwd_tag returns the forwarding tag of the connection */
2056 #define IP_VS_FWD_METHOD(cp)  (cp->flags & IP_VS_CONN_F_FWD_MASK)
2057 
2058 static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
2059 {
2060 	char fwd;
2061 
2062 	switch (IP_VS_FWD_METHOD(cp)) {
2063 	case IP_VS_CONN_F_MASQ:
2064 		fwd = 'M'; break;
2065 	case IP_VS_CONN_F_LOCALNODE:
2066 		fwd = 'L'; break;
2067 	case IP_VS_CONN_F_TUNNEL:
2068 		fwd = 'T'; break;
2069 	case IP_VS_CONN_F_DROUTE:
2070 		fwd = 'R'; break;
2071 	case IP_VS_CONN_F_BYPASS:
2072 		fwd = 'B'; break;
2073 	default:
2074 		fwd = '?'; break;
2075 	}
2076 	return fwd;
2077 }
2078 
2079 /* Check if connection uses double hashing */
2080 static inline bool ip_vs_conn_use_hash2(struct ip_vs_conn *cp)
2081 {
2082 	return IP_VS_FWD_METHOD(cp) == IP_VS_CONN_F_MASQ &&
2083 	       !(cp->flags & IP_VS_CONN_F_TEMPLATE);
2084 }
2085 
2086 bool ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
2087 		    struct ip_vs_conn *cp, int dir, unsigned int toff,
2088 		    bool has_ports, struct ip_vs_iphdr *ciph);
2089 
2090 #ifdef CONFIG_IP_VS_IPV6
2091 void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
2092 		       struct ip_vs_conn *cp, int dir, unsigned int toff,
2093 		       bool has_ports, struct ip_vs_iphdr *ciph);
2094 #endif
2095 
2096 static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
2097 {
2098 	__be32 diff[2] = { ~old, new };
2099 
2100 	return csum_partial(diff, sizeof(diff), oldsum);
2101 }
2102 
2103 #ifdef CONFIG_IP_VS_IPV6
2104 static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
2105 					__wsum oldsum)
2106 {
2107 	__be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
2108 			    new[3],  new[2],  new[1],  new[0] };
2109 
2110 	return csum_partial(diff, sizeof(diff), oldsum);
2111 }
2112 #endif
2113 
2114 static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
2115 {
2116 	__be16 diff[2] = { ~old, new };
2117 
2118 	return csum_partial(diff, sizeof(diff), oldsum);
2119 }
2120 
2121 static inline bool ip_vs_checksum_needed(struct sk_buff *skb)
2122 {
2123 	/* Checksum unnecessary or already validated? */
2124 	if (skb_csum_unnecessary(skb))
2125 		return false;
2126 	/* Locally generated ? */
2127 	if (!skb->dev)
2128 		return false;
2129 	return true;
2130 }
2131 
2132 static inline bool ip_vs_checksum_common_check(struct sk_buff *skb,
2133 					       int offset, int proto, int af)
2134 {
2135 	if (!ip_vs_checksum_needed(skb))
2136 		return true;
2137 	/* Validate csum even for FORWARD */
2138 	return !nf_checksum(skb, NF_INET_LOCAL_IN, offset, proto, af);
2139 }
2140 
2141 /* Forget current conntrack (unconfirmed) and attach notrack entry */
2142 static inline void ip_vs_notrack(struct sk_buff *skb)
2143 {
2144 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
2145 	enum ip_conntrack_info ctinfo;
2146 	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
2147 
2148 	if (ct) {
2149 		nf_reset_ct(skb);
2150 		nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
2151 	}
2152 #endif
2153 }
2154 
2155 #ifdef CONFIG_IP_VS_NFCT
2156 /* Netfilter connection tracking
2157  * (from ip_vs_nfct.c)
2158  */
2159 static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
2160 {
2161 #ifdef CONFIG_SYSCTL
2162 	return ipvs->sysctl_conntrack;
2163 #else
2164 	return 0;
2165 #endif
2166 }
2167 
2168 void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
2169 			    int outin);
2170 int ip_vs_confirm_conntrack(struct sk_buff *skb);
2171 void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
2172 			       struct ip_vs_conn *cp, u8 proto,
2173 			       const __be16 port, int from_rs);
2174 void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
2175 
2176 #else
2177 
2178 static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
2179 {
2180 	return 0;
2181 }
2182 
2183 static inline void ip_vs_update_conntrack(struct sk_buff *skb,
2184 					  struct ip_vs_conn *cp, int outin)
2185 {
2186 }
2187 
2188 static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
2189 {
2190 	return NF_ACCEPT;
2191 }
2192 
2193 static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
2194 {
2195 }
2196 #endif /* CONFIG_IP_VS_NFCT */
2197 
2198 /* Using old conntrack that can not be redirected to another real server? */
2199 static inline bool ip_vs_conn_uses_old_conntrack(struct ip_vs_conn *cp,
2200 						 struct sk_buff *skb)
2201 {
2202 #ifdef CONFIG_IP_VS_NFCT
2203 	enum ip_conntrack_info ctinfo;
2204 	struct nf_conn *ct;
2205 
2206 	ct = nf_ct_get(skb, &ctinfo);
2207 	if (ct && nf_ct_is_confirmed(ct))
2208 		return true;
2209 #endif
2210 	return false;
2211 }
2212 
2213 static inline int ip_vs_register_conntrack(struct ip_vs_service *svc)
2214 {
2215 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
2216 	int afmask = (svc->af == AF_INET6) ? 2 : 1;
2217 	int ret = 0;
2218 
2219 	if (!(svc->conntrack_afmask & afmask)) {
2220 		ret = nf_ct_netns_get(svc->ipvs->net, svc->af);
2221 		if (ret >= 0)
2222 			svc->conntrack_afmask |= afmask;
2223 	}
2224 	return ret;
2225 #else
2226 	return 0;
2227 #endif
2228 }
2229 
2230 static inline void ip_vs_unregister_conntrack(struct ip_vs_service *svc)
2231 {
2232 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
2233 	int afmask = (svc->af == AF_INET6) ? 2 : 1;
2234 
2235 	if (svc->conntrack_afmask & afmask) {
2236 		nf_ct_netns_put(svc->ipvs->net, svc->af);
2237 		svc->conntrack_afmask &= ~afmask;
2238 	}
2239 #endif
2240 }
2241 
2242 int ip_vs_register_hooks(struct netns_ipvs *ipvs, unsigned int af);
2243 void ip_vs_unregister_hooks(struct netns_ipvs *ipvs, unsigned int af);
2244 
2245 static inline int
2246 ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
2247 {
2248 	/* We think the overhead of processing active connections is 257
2249 	 * times higher than that of inactive connections in average. (This
2250 	 * 257 times might not be accurate, we will change it later) We
2251 	 * use the following formula to estimate the overhead now:
2252 	 *		  dest->activeconns*256 + dest->totalconns
2253 	 */
2254 	return (atomic_read(&dest->activeconns) << 8) +
2255 		atomic_read(&dest->totalconns);
2256 }
2257 
2258 static inline int
2259 ip_vs_dest_inactconns(const struct ip_vs_dest *dest)
2260 {
2261 	return max(atomic_read(&dest->totalconns) -
2262 		   atomic_read(&dest->activeconns), 0);
2263 }
2264 
2265 #ifdef CONFIG_IP_VS_PROTO_TCP
2266 INDIRECT_CALLABLE_DECLARE(int
2267 	tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
2268 			 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
2269 #endif
2270 
2271 #ifdef CONFIG_IP_VS_PROTO_UDP
2272 INDIRECT_CALLABLE_DECLARE(int
2273 	udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
2274 			 struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
2275 #endif
2276 #endif	/* _NET_IP_VS_H */
2277