ip_vs.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* IP Virtual Server
  3. * data structure and functionality definitions
  4. */
  5. #ifndef _NET_IP_VS_H
  6. #define _NET_IP_VS_H
  7. #include <linux/ip_vs.h> /* definitions shared with userland */
  8. #include <asm/types.h> /* for __uXX types */
  9. #include <linux/list.h> /* for struct list_head */
  10. #include <linux/spinlock.h> /* for struct rwlock_t */
  11. #include <linux/atomic.h> /* for struct atomic_t */
  12. #include <linux/refcount.h> /* for struct refcount_t */
  13. #include <linux/workqueue.h>
  14. #include <linux/compiler.h>
  15. #include <linux/timer.h>
  16. #include <linux/bug.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter.h> /* for union nf_inet_addr */
  19. #include <linux/ip.h>
  20. #include <linux/ipv6.h> /* for struct ipv6hdr */
  21. #include <net/ipv6.h>
  22. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  23. #include <net/netfilter/nf_conntrack.h>
  24. #endif
  25. #include <net/net_namespace.h> /* Netw namespace */
  26. #include <linux/sched/isolation.h>
  27. #define IP_VS_HDR_INVERSE 1
  28. #define IP_VS_HDR_ICMP 2
  29. /* Generic access of ipvs struct */
  30. static inline struct netns_ipvs *net_ipvs(struct net* net)
  31. {
  32. return net->ipvs;
  33. }
  34. /* Connections' size value needed by ip_vs_ctl.c */
  35. extern int ip_vs_conn_tab_size;
  36. extern struct mutex __ip_vs_mutex;
  37. struct ip_vs_iphdr {
  38. int hdr_flags; /* ipvs flags */
  39. __u32 off; /* Where IP or IPv4 header starts */
  40. __u32 len; /* IPv4 simply where L4 starts
  41. * IPv6 where L4 Transport Header starts */
  42. __u16 fragoffs; /* IPv6 fragment offset, 0 if first frag (or not frag)*/
  43. __s16 protocol;
  44. __s32 flags;
  45. union nf_inet_addr saddr;
  46. union nf_inet_addr daddr;
  47. };
  48. static inline void *frag_safe_skb_hp(const struct sk_buff *skb, int offset,
  49. int len, void *buffer)
  50. {
  51. return skb_header_pointer(skb, offset, len, buffer);
  52. }
  53. /* This function handles filling *ip_vs_iphdr, both for IPv4 and IPv6.
  54. * IPv6 requires some extra work, as finding proper header position,
  55. * depend on the IPv6 extension headers.
  56. */
  57. static inline int
  58. ip_vs_fill_iph_skb_off(int af, const struct sk_buff *skb, int offset,
  59. int hdr_flags, struct ip_vs_iphdr *iphdr)
  60. {
  61. iphdr->hdr_flags = hdr_flags;
  62. iphdr->off = offset;
  63. #ifdef CONFIG_IP_VS_IPV6
  64. if (af == AF_INET6) {
  65. struct ipv6hdr _iph;
  66. const struct ipv6hdr *iph = skb_header_pointer(
  67. skb, offset, sizeof(_iph), &_iph);
  68. if (!iph)
  69. return 0;
  70. iphdr->saddr.in6 = iph->saddr;
  71. iphdr->daddr.in6 = iph->daddr;
  72. /* ipv6_find_hdr() updates len, flags */
  73. iphdr->len = offset;
  74. iphdr->flags = 0;
  75. iphdr->protocol = ipv6_find_hdr(skb, &iphdr->len, -1,
  76. &iphdr->fragoffs,
  77. &iphdr->flags);
  78. if (iphdr->protocol < 0)
  79. return 0;
  80. } else
  81. #endif
  82. {
  83. struct iphdr _iph;
  84. const struct iphdr *iph = skb_header_pointer(
  85. skb, offset, sizeof(_iph), &_iph);
  86. if (!iph)
  87. return 0;
  88. iphdr->len = offset + iph->ihl * 4;
  89. iphdr->fragoffs = 0;
  90. iphdr->protocol = iph->protocol;
  91. iphdr->saddr.ip = iph->saddr;
  92. iphdr->daddr.ip = iph->daddr;
  93. }
  94. return 1;
  95. }
  96. static inline int
  97. ip_vs_fill_iph_skb_icmp(int af, const struct sk_buff *skb, int offset,
  98. bool inverse, struct ip_vs_iphdr *iphdr)
  99. {
  100. int hdr_flags = IP_VS_HDR_ICMP;
  101. if (inverse)
  102. hdr_flags |= IP_VS_HDR_INVERSE;
  103. return ip_vs_fill_iph_skb_off(af, skb, offset, hdr_flags, iphdr);
  104. }
  105. static inline int
  106. ip_vs_fill_iph_skb(int af, const struct sk_buff *skb, bool inverse,
  107. struct ip_vs_iphdr *iphdr)
  108. {
  109. int hdr_flags = 0;
  110. if (inverse)
  111. hdr_flags |= IP_VS_HDR_INVERSE;
  112. return ip_vs_fill_iph_skb_off(af, skb, skb_network_offset(skb),
  113. hdr_flags, iphdr);
  114. }
  115. static inline bool
  116. ip_vs_iph_inverse(const struct ip_vs_iphdr *iph)
  117. {
  118. return !!(iph->hdr_flags & IP_VS_HDR_INVERSE);
  119. }
  120. static inline bool
  121. ip_vs_iph_icmp(const struct ip_vs_iphdr *iph)
  122. {
  123. return !!(iph->hdr_flags & IP_VS_HDR_ICMP);
  124. }
  125. static inline void ip_vs_addr_copy(int af, union nf_inet_addr *dst,
  126. const union nf_inet_addr *src)
  127. {
  128. #ifdef CONFIG_IP_VS_IPV6
  129. if (af == AF_INET6)
  130. dst->in6 = src->in6;
  131. else
  132. #endif
  133. dst->ip = src->ip;
  134. }
  135. static inline void ip_vs_addr_set(int af, union nf_inet_addr *dst,
  136. const union nf_inet_addr *src)
  137. {
  138. #ifdef CONFIG_IP_VS_IPV6
  139. if (af == AF_INET6) {
  140. dst->in6 = src->in6;
  141. return;
  142. }
  143. #endif
  144. dst->ip = src->ip;
  145. dst->all[1] = 0;
  146. dst->all[2] = 0;
  147. dst->all[3] = 0;
  148. }
  149. static inline int ip_vs_addr_equal(int af, const union nf_inet_addr *a,
  150. const union nf_inet_addr *b)
  151. {
  152. #ifdef CONFIG_IP_VS_IPV6
  153. if (af == AF_INET6)
  154. return ipv6_addr_equal(&a->in6, &b->in6);
  155. #endif
  156. return a->ip == b->ip;
  157. }
  158. #ifdef CONFIG_IP_VS_DEBUG
  159. #include <linux/net.h>
  160. int ip_vs_get_debug_level(void);
  161. static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len,
  162. const union nf_inet_addr *addr,
  163. int *idx)
  164. {
  165. int len;
  166. #ifdef CONFIG_IP_VS_IPV6
  167. if (af == AF_INET6)
  168. len = snprintf(&buf[*idx], buf_len - *idx, "[%pI6c]",
  169. &addr->in6) + 1;
  170. else
  171. #endif
  172. len = snprintf(&buf[*idx], buf_len - *idx, "%pI4",
  173. &addr->ip) + 1;
  174. *idx += len;
  175. BUG_ON(*idx > buf_len + 1);
  176. return &buf[*idx - len];
  177. }
  178. #define IP_VS_DBG_BUF(level, msg, ...) \
  179. do { \
  180. char ip_vs_dbg_buf[160]; \
  181. int ip_vs_dbg_idx = 0; \
  182. if (level <= ip_vs_get_debug_level()) \
  183. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  184. } while (0)
  185. #define IP_VS_ERR_BUF(msg...) \
  186. do { \
  187. char ip_vs_dbg_buf[160]; \
  188. int ip_vs_dbg_idx = 0; \
  189. pr_err(msg); \
  190. } while (0)
  191. /* Only use from within IP_VS_DBG_BUF() or IP_VS_ERR_BUF macros */
  192. #define IP_VS_DBG_ADDR(af, addr) \
  193. ip_vs_dbg_addr(af, ip_vs_dbg_buf, \
  194. sizeof(ip_vs_dbg_buf), addr, \
  195. &ip_vs_dbg_idx)
  196. #define IP_VS_DBG(level, msg, ...) \
  197. do { \
  198. if (level <= ip_vs_get_debug_level()) \
  199. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  200. } while (0)
  201. #define IP_VS_DBG_RL(msg, ...) \
  202. do { \
  203. if (net_ratelimit()) \
  204. printk(KERN_DEBUG pr_fmt(msg), ##__VA_ARGS__); \
  205. } while (0)
  206. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) \
  207. do { \
  208. if (level <= ip_vs_get_debug_level()) \
  209. pp->debug_packet(af, pp, skb, ofs, msg); \
  210. } while (0)
  211. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) \
  212. do { \
  213. if (level <= ip_vs_get_debug_level() && \
  214. net_ratelimit()) \
  215. pp->debug_packet(af, pp, skb, ofs, msg); \
  216. } while (0)
  217. #else /* NO DEBUGGING at ALL */
  218. #define IP_VS_DBG_BUF(level, msg...) do {} while (0)
  219. #define IP_VS_ERR_BUF(msg...) do {} while (0)
  220. #define IP_VS_DBG(level, msg...) do {} while (0)
  221. #define IP_VS_DBG_RL(msg...) do {} while (0)
  222. #define IP_VS_DBG_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  223. #define IP_VS_DBG_RL_PKT(level, af, pp, skb, ofs, msg) do {} while (0)
  224. #endif
  225. #define IP_VS_BUG() BUG()
  226. #define IP_VS_ERR_RL(msg, ...) \
  227. do { \
  228. if (net_ratelimit()) \
  229. pr_err(msg, ##__VA_ARGS__); \
  230. } while (0)
  231. /* The port number of FTP service (in network order). */
  232. #define FTPPORT cpu_to_be16(21)
  233. #define FTPDATA cpu_to_be16(20)
  234. /* TCP State Values */
  235. enum {
  236. IP_VS_TCP_S_NONE = 0,
  237. IP_VS_TCP_S_ESTABLISHED,
  238. IP_VS_TCP_S_SYN_SENT,
  239. IP_VS_TCP_S_SYN_RECV,
  240. IP_VS_TCP_S_FIN_WAIT,
  241. IP_VS_TCP_S_TIME_WAIT,
  242. IP_VS_TCP_S_CLOSE,
  243. IP_VS_TCP_S_CLOSE_WAIT,
  244. IP_VS_TCP_S_LAST_ACK,
  245. IP_VS_TCP_S_LISTEN,
  246. IP_VS_TCP_S_SYNACK,
  247. IP_VS_TCP_S_LAST
  248. };
  249. /* UDP State Values */
  250. enum {
  251. IP_VS_UDP_S_NORMAL,
  252. IP_VS_UDP_S_LAST,
  253. };
  254. /* ICMP State Values */
  255. enum {
  256. IP_VS_ICMP_S_NORMAL,
  257. IP_VS_ICMP_S_LAST,
  258. };
  259. /* SCTP State Values */
  260. enum ip_vs_sctp_states {
  261. IP_VS_SCTP_S_NONE,
  262. IP_VS_SCTP_S_INIT1,
  263. IP_VS_SCTP_S_INIT,
  264. IP_VS_SCTP_S_COOKIE_SENT,
  265. IP_VS_SCTP_S_COOKIE_REPLIED,
  266. IP_VS_SCTP_S_COOKIE_WAIT,
  267. IP_VS_SCTP_S_COOKIE,
  268. IP_VS_SCTP_S_COOKIE_ECHOED,
  269. IP_VS_SCTP_S_ESTABLISHED,
  270. IP_VS_SCTP_S_SHUTDOWN_SENT,
  271. IP_VS_SCTP_S_SHUTDOWN_RECEIVED,
  272. IP_VS_SCTP_S_SHUTDOWN_ACK_SENT,
  273. IP_VS_SCTP_S_REJECTED,
  274. IP_VS_SCTP_S_CLOSED,
  275. IP_VS_SCTP_S_LAST
  276. };
  277. /* Connection templates use bits from state */
  278. #define IP_VS_CTPL_S_NONE 0x0000
  279. #define IP_VS_CTPL_S_ASSURED 0x0001
  280. #define IP_VS_CTPL_S_LAST 0x0002
  281. /* Delta sequence info structure
  282. * Each ip_vs_conn has 2 (output AND input seq. changes).
  283. * Only used in the VS/NAT.
  284. */
  285. struct ip_vs_seq {
  286. __u32 init_seq; /* Add delta from this seq */
  287. __u32 delta; /* Delta in sequence numbers */
  288. __u32 previous_delta; /* Delta in sequence numbers
  289. * before last resized pkt */
  290. };
  291. /* counters per cpu */
  292. struct ip_vs_counters {
  293. u64_stats_t conns; /* connections scheduled */
  294. u64_stats_t inpkts; /* incoming packets */
  295. u64_stats_t outpkts; /* outgoing packets */
  296. u64_stats_t inbytes; /* incoming bytes */
  297. u64_stats_t outbytes; /* outgoing bytes */
  298. };
  299. /* Stats per cpu */
  300. struct ip_vs_cpu_stats {
  301. struct ip_vs_counters cnt;
  302. struct u64_stats_sync syncp;
  303. };
  304. /* Default nice for estimator kthreads */
  305. #define IPVS_EST_NICE 0
  306. /* IPVS statistics objects */
  307. struct ip_vs_estimator {
  308. struct hlist_node list;
  309. u64 last_inbytes;
  310. u64 last_outbytes;
  311. u64 last_conns;
  312. u64 last_inpkts;
  313. u64 last_outpkts;
  314. u64 cps;
  315. u64 inpps;
  316. u64 outpps;
  317. u64 inbps;
  318. u64 outbps;
  319. s32 ktid:16, /* kthread ID, -1=temp list */
  320. ktrow:8, /* row/tick ID for kthread */
  321. ktcid:8; /* chain ID for kthread tick */
  322. };
  323. /*
  324. * IPVS statistics object, 64-bit kernel version of struct ip_vs_stats_user
  325. */
  326. struct ip_vs_kstats {
  327. u64 conns; /* connections scheduled */
  328. u64 inpkts; /* incoming packets */
  329. u64 outpkts; /* outgoing packets */
  330. u64 inbytes; /* incoming bytes */
  331. u64 outbytes; /* outgoing bytes */
  332. u64 cps; /* current connection rate */
  333. u64 inpps; /* current in packet rate */
  334. u64 outpps; /* current out packet rate */
  335. u64 inbps; /* current in byte rate */
  336. u64 outbps; /* current out byte rate */
  337. };
  338. struct ip_vs_stats {
  339. struct ip_vs_kstats kstats; /* kernel statistics */
  340. struct ip_vs_estimator est; /* estimator */
  341. struct ip_vs_cpu_stats __percpu *cpustats; /* per cpu counters */
  342. spinlock_t lock; /* spin lock */
  343. struct ip_vs_kstats kstats0; /* reset values */
  344. };
  345. struct ip_vs_stats_rcu {
  346. struct ip_vs_stats s;
  347. struct rcu_head rcu_head;
  348. };
  349. int ip_vs_stats_init_alloc(struct ip_vs_stats *s);
  350. struct ip_vs_stats *ip_vs_stats_alloc(void);
  351. void ip_vs_stats_release(struct ip_vs_stats *stats);
  352. void ip_vs_stats_free(struct ip_vs_stats *stats);
  353. /* Process estimators in multiple timer ticks (20/50/100, see ktrow) */
  354. #define IPVS_EST_NTICKS 50
  355. /* Estimation uses a 2-second period containing ticks (in jiffies) */
  356. #define IPVS_EST_TICK ((2 * HZ) / IPVS_EST_NTICKS)
  357. /* Limit of CPU load per kthread (8 for 12.5%), ratio of CPU capacity (1/C).
  358. * Value of 4 and above ensures kthreads will take work without exceeding
  359. * the CPU capacity under different circumstances.
  360. */
  361. #define IPVS_EST_LOAD_DIVISOR 8
  362. /* Kthreads should not have work that exceeds the CPU load above 50% */
  363. #define IPVS_EST_CPU_KTHREADS (IPVS_EST_LOAD_DIVISOR / 2)
  364. /* Desired number of chains per timer tick (chain load factor in 100us units),
  365. * 48=4.8ms of 40ms tick (12% CPU usage):
  366. * 2 sec * 1000 ms in sec * 10 (100us in ms) / 8 (12.5%) / 50
  367. */
  368. #define IPVS_EST_CHAIN_FACTOR \
  369. ALIGN_DOWN(2 * 1000 * 10 / IPVS_EST_LOAD_DIVISOR / IPVS_EST_NTICKS, 8)
  370. /* Compiled number of chains per tick
  371. * The defines should match cond_resched_rcu
  372. */
  373. #if defined(CONFIG_DEBUG_ATOMIC_SLEEP) || !defined(CONFIG_PREEMPT_RCU)
  374. #define IPVS_EST_TICK_CHAINS IPVS_EST_CHAIN_FACTOR
  375. #else
  376. #define IPVS_EST_TICK_CHAINS 1
  377. #endif
  378. #if IPVS_EST_NTICKS > 127
  379. #error Too many timer ticks for ktrow
  380. #endif
  381. /* Multiple chains processed in same tick */
  382. struct ip_vs_est_tick_data {
  383. struct rcu_head rcu_head;
  384. struct hlist_head chains[IPVS_EST_TICK_CHAINS];
  385. DECLARE_BITMAP(present, IPVS_EST_TICK_CHAINS);
  386. DECLARE_BITMAP(full, IPVS_EST_TICK_CHAINS);
  387. int chain_len[IPVS_EST_TICK_CHAINS];
  388. };
  389. /* Context for estimation kthread */
  390. struct ip_vs_est_kt_data {
  391. struct netns_ipvs *ipvs;
  392. struct task_struct *task; /* task if running */
  393. struct ip_vs_est_tick_data __rcu *ticks[IPVS_EST_NTICKS];
  394. DECLARE_BITMAP(avail, IPVS_EST_NTICKS); /* tick has space for ests */
  395. unsigned long est_timer; /* estimation timer (jiffies) */
  396. struct ip_vs_stats *calc_stats; /* Used for calculation */
  397. int tick_len[IPVS_EST_NTICKS]; /* est count */
  398. int id; /* ktid per netns */
  399. int chain_max; /* max ests per tick chain */
  400. int tick_max; /* max ests per tick */
  401. int est_count; /* attached ests to kthread */
  402. int est_max_count; /* max ests per kthread */
  403. int add_row; /* row for new ests */
  404. int est_row; /* estimated row */
  405. };
  406. struct dst_entry;
  407. struct iphdr;
  408. struct ip_vs_conn;
  409. struct ip_vs_app;
  410. struct sk_buff;
  411. struct ip_vs_proto_data;
  412. struct ip_vs_protocol {
  413. struct ip_vs_protocol *next;
  414. char *name;
  415. u16 protocol;
  416. u16 num_states;
  417. int dont_defrag;
  418. void (*init)(struct ip_vs_protocol *pp);
  419. void (*exit)(struct ip_vs_protocol *pp);
  420. int (*init_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd);
  421. void (*exit_netns)(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd);
  422. int (*conn_schedule)(struct netns_ipvs *ipvs,
  423. int af, struct sk_buff *skb,
  424. struct ip_vs_proto_data *pd,
  425. int *verdict, struct ip_vs_conn **cpp,
  426. struct ip_vs_iphdr *iph);
  427. struct ip_vs_conn *
  428. (*conn_in_get)(struct netns_ipvs *ipvs,
  429. int af,
  430. const struct sk_buff *skb,
  431. const struct ip_vs_iphdr *iph);
  432. struct ip_vs_conn *
  433. (*conn_out_get)(struct netns_ipvs *ipvs,
  434. int af,
  435. const struct sk_buff *skb,
  436. const struct ip_vs_iphdr *iph);
  437. int (*snat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
  438. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
  439. int (*dnat_handler)(struct sk_buff *skb, struct ip_vs_protocol *pp,
  440. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph);
  441. const char *(*state_name)(int state);
  442. void (*state_transition)(struct ip_vs_conn *cp, int direction,
  443. const struct sk_buff *skb,
  444. struct ip_vs_proto_data *pd);
  445. int (*register_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc);
  446. void (*unregister_app)(struct netns_ipvs *ipvs, struct ip_vs_app *inc);
  447. int (*app_conn_bind)(struct ip_vs_conn *cp);
  448. void (*debug_packet)(int af, struct ip_vs_protocol *pp,
  449. const struct sk_buff *skb,
  450. int offset,
  451. const char *msg);
  452. void (*timeout_change)(struct ip_vs_proto_data *pd, int flags);
  453. };
  454. /* protocol data per netns */
  455. struct ip_vs_proto_data {
  456. struct ip_vs_proto_data *next;
  457. struct ip_vs_protocol *pp;
  458. int *timeout_table; /* protocol timeout table */
  459. atomic_t appcnt; /* counter of proto app incs. */
  460. struct tcp_states_t *tcp_state_table;
  461. };
  462. struct ip_vs_protocol *ip_vs_proto_get(unsigned short proto);
  463. struct ip_vs_proto_data *ip_vs_proto_data_get(struct netns_ipvs *ipvs,
  464. unsigned short proto);
  465. struct ip_vs_conn_param {
  466. struct netns_ipvs *ipvs;
  467. const union nf_inet_addr *caddr;
  468. const union nf_inet_addr *vaddr;
  469. __be16 cport;
  470. __be16 vport;
  471. __u16 protocol;
  472. u16 af;
  473. const struct ip_vs_pe *pe;
  474. char *pe_data;
  475. __u8 pe_data_len;
  476. };
  477. /* IP_VS structure allocated for each dynamically scheduled connection */
  478. struct ip_vs_conn {
  479. struct hlist_node c_list; /* hashed list heads */
  480. /* Protocol, addresses and port numbers */
  481. __be16 cport;
  482. __be16 dport;
  483. __be16 vport;
  484. u16 af; /* address family */
  485. union nf_inet_addr caddr; /* client address */
  486. union nf_inet_addr vaddr; /* virtual address */
  487. union nf_inet_addr daddr; /* destination address */
  488. volatile __u32 flags; /* status flags */
  489. __u16 protocol; /* Which protocol (TCP/UDP) */
  490. __u16 daf; /* Address family of the dest */
  491. struct netns_ipvs *ipvs;
  492. /* counter and timer */
  493. refcount_t refcnt; /* reference count */
  494. struct timer_list timer; /* Expiration timer */
  495. volatile unsigned long timeout; /* timeout */
  496. /* Flags and state transition */
  497. spinlock_t lock; /* lock for state transition */
  498. volatile __u16 state; /* state info */
  499. volatile __u16 old_state; /* old state, to be used for
  500. * state transition triggered
  501. * synchronization
  502. */
  503. __u32 fwmark; /* Fire wall mark from skb */
  504. unsigned long sync_endtime; /* jiffies + sent_retries */
  505. /* Control members */
  506. struct ip_vs_conn *control; /* Master control connection */
  507. atomic_t n_control; /* Number of controlled ones */
  508. struct ip_vs_dest *dest; /* real server */
  509. atomic_t in_pkts; /* incoming packet counter */
  510. /* Packet transmitter for different forwarding methods. If it
  511. * mangles the packet, it must return NF_DROP or better NF_STOLEN,
  512. * otherwise this must be changed to a sk_buff **.
  513. * NF_ACCEPT can be returned when destination is local.
  514. */
  515. int (*packet_xmit)(struct sk_buff *skb, struct ip_vs_conn *cp,
  516. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  517. /* Note: we can group the following members into a structure,
  518. * in order to save more space, and the following members are
  519. * only used in VS/NAT anyway
  520. */
  521. struct ip_vs_app *app; /* bound ip_vs_app object */
  522. void *app_data; /* Application private data */
  523. struct_group(sync_conn_opt,
  524. struct ip_vs_seq in_seq; /* incoming seq. struct */
  525. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  526. );
  527. const struct ip_vs_pe *pe;
  528. char *pe_data;
  529. __u8 pe_data_len;
  530. struct rcu_head rcu_head;
  531. };
  532. /* Extended internal versions of struct ip_vs_service_user and ip_vs_dest_user
  533. * for IPv6 support.
  534. *
  535. * We need these to conveniently pass around service and destination
  536. * options, but unfortunately, we also need to keep the old definitions to
  537. * maintain userspace backwards compatibility for the setsockopt interface.
  538. */
  539. struct ip_vs_service_user_kern {
  540. /* virtual service addresses */
  541. u16 af;
  542. u16 protocol;
  543. union nf_inet_addr addr; /* virtual ip address */
  544. __be16 port;
  545. u32 fwmark; /* firewall mark of service */
  546. /* virtual service options */
  547. char *sched_name;
  548. char *pe_name;
  549. unsigned int flags; /* virtual service flags */
  550. unsigned int timeout; /* persistent timeout in sec */
  551. __be32 netmask; /* persistent netmask or plen */
  552. };
  553. struct ip_vs_dest_user_kern {
  554. /* destination server address */
  555. union nf_inet_addr addr;
  556. __be16 port;
  557. /* real server options */
  558. unsigned int conn_flags; /* connection flags */
  559. int weight; /* destination weight */
  560. /* thresholds for active connections */
  561. u32 u_threshold; /* upper threshold */
  562. u32 l_threshold; /* lower threshold */
  563. /* Address family of addr */
  564. u16 af;
  565. u16 tun_type; /* tunnel type */
  566. __be16 tun_port; /* tunnel port */
  567. u16 tun_flags; /* tunnel flags */
  568. };
  569. /*
  570. * The information about the virtual service offered to the net and the
  571. * forwarding entries.
  572. */
  573. struct ip_vs_service {
  574. struct hlist_node s_list; /* for normal service table */
  575. struct hlist_node f_list; /* for fwmark-based service table */
  576. atomic_t refcnt; /* reference counter */
  577. u16 af; /* address family */
  578. __u16 protocol; /* which protocol (TCP/UDP) */
  579. union nf_inet_addr addr; /* IP address for virtual service */
  580. __be16 port; /* port number for the service */
  581. __u32 fwmark; /* firewall mark of the service */
  582. unsigned int flags; /* service status flags */
  583. unsigned int timeout; /* persistent timeout in ticks */
  584. __be32 netmask; /* grouping granularity, mask/plen */
  585. struct netns_ipvs *ipvs;
  586. struct list_head destinations; /* real server d-linked list */
  587. __u32 num_dests; /* number of servers */
  588. struct ip_vs_stats stats; /* statistics for the service */
  589. /* for scheduling */
  590. struct ip_vs_scheduler __rcu *scheduler; /* bound scheduler object */
  591. spinlock_t sched_lock; /* lock sched_data */
  592. void *sched_data; /* scheduler application data */
  593. /* alternate persistence engine */
  594. struct ip_vs_pe __rcu *pe;
  595. int conntrack_afmask;
  596. struct rcu_head rcu_head;
  597. };
  598. /* Information for cached dst */
  599. struct ip_vs_dest_dst {
  600. struct dst_entry *dst_cache; /* destination cache entry */
  601. u32 dst_cookie;
  602. union nf_inet_addr dst_saddr;
  603. struct rcu_head rcu_head;
  604. };
  605. /* The real server destination forwarding entry with ip address, port number,
  606. * and so on.
  607. */
  608. struct ip_vs_dest {
  609. struct list_head n_list; /* for the dests in the service */
  610. struct hlist_node d_list; /* for table with all the dests */
  611. u16 af; /* address family */
  612. __be16 port; /* port number of the server */
  613. union nf_inet_addr addr; /* IP address of the server */
  614. volatile unsigned int flags; /* dest status flags */
  615. atomic_t conn_flags; /* flags to copy to conn */
  616. atomic_t weight; /* server weight */
  617. atomic_t last_weight; /* server latest weight */
  618. __u16 tun_type; /* tunnel type */
  619. __be16 tun_port; /* tunnel port */
  620. __u16 tun_flags; /* tunnel flags */
  621. refcount_t refcnt; /* reference counter */
  622. struct ip_vs_stats stats; /* statistics */
  623. unsigned long idle_start; /* start time, jiffies */
  624. /* connection counters and thresholds */
  625. atomic_t activeconns; /* active connections */
  626. atomic_t inactconns; /* inactive connections */
  627. atomic_t persistconns; /* persistent connections */
  628. __u32 u_threshold; /* upper threshold */
  629. __u32 l_threshold; /* lower threshold */
  630. /* for destination cache */
  631. spinlock_t dst_lock; /* lock of dst_cache */
  632. struct ip_vs_dest_dst __rcu *dest_dst; /* cached dst info */
  633. /* for virtual service */
  634. struct ip_vs_service __rcu *svc; /* service it belongs to */
  635. __u16 protocol; /* which protocol (TCP/UDP) */
  636. __be16 vport; /* virtual port number */
  637. union nf_inet_addr vaddr; /* virtual IP address */
  638. __u32 vfwmark; /* firewall mark of service */
  639. struct rcu_head rcu_head;
  640. struct list_head t_list; /* in dest_trash */
  641. unsigned int in_rs_table:1; /* we are in rs_table */
  642. };
  643. /* The scheduler object */
  644. struct ip_vs_scheduler {
  645. struct list_head n_list; /* d-linked list head */
  646. char *name; /* scheduler name */
  647. atomic_t refcnt; /* reference counter */
  648. struct module *module; /* THIS_MODULE/NULL */
  649. /* scheduler initializing service */
  650. int (*init_service)(struct ip_vs_service *svc);
  651. /* scheduling service finish */
  652. void (*done_service)(struct ip_vs_service *svc);
  653. /* dest is linked */
  654. int (*add_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  655. /* dest is unlinked */
  656. int (*del_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  657. /* dest is updated */
  658. int (*upd_dest)(struct ip_vs_service *svc, struct ip_vs_dest *dest);
  659. /* selecting a server from the given service */
  660. struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc,
  661. const struct sk_buff *skb,
  662. struct ip_vs_iphdr *iph);
  663. };
  664. /* The persistence engine object */
  665. struct ip_vs_pe {
  666. struct list_head n_list; /* d-linked list head */
  667. char *name; /* scheduler name */
  668. atomic_t refcnt; /* reference counter */
  669. struct module *module; /* THIS_MODULE/NULL */
  670. /* get the connection template, if any */
  671. int (*fill_param)(struct ip_vs_conn_param *p, struct sk_buff *skb);
  672. bool (*ct_match)(const struct ip_vs_conn_param *p,
  673. struct ip_vs_conn *ct);
  674. u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, u32 initval,
  675. bool inverse);
  676. int (*show_pe_data)(const struct ip_vs_conn *cp, char *buf);
  677. /* create connections for real-server outgoing packets */
  678. struct ip_vs_conn* (*conn_out)(struct ip_vs_service *svc,
  679. struct ip_vs_dest *dest,
  680. struct sk_buff *skb,
  681. const struct ip_vs_iphdr *iph,
  682. __be16 dport, __be16 cport);
  683. };
  684. /* The application module object (a.k.a. app incarnation) */
  685. struct ip_vs_app {
  686. struct list_head a_list; /* member in app list */
  687. int type; /* IP_VS_APP_TYPE_xxx */
  688. char *name; /* application module name */
  689. __u16 protocol;
  690. struct module *module; /* THIS_MODULE/NULL */
  691. struct list_head incs_list; /* list of incarnations */
  692. /* members for application incarnations */
  693. struct list_head p_list; /* member in proto app list */
  694. struct ip_vs_app *app; /* its real application */
  695. __be16 port; /* port number in net order */
  696. atomic_t usecnt; /* usage counter */
  697. struct rcu_head rcu_head;
  698. /* output hook: Process packet in inout direction, diff set for TCP.
  699. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  700. * 2=Mangled but checksum was not updated
  701. */
  702. int (*pkt_out)(struct ip_vs_app *, struct ip_vs_conn *,
  703. struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh);
  704. /* input hook: Process packet in outin direction, diff set for TCP.
  705. * Return: 0=Error, 1=Payload Not Mangled/Mangled but checksum is ok,
  706. * 2=Mangled but checksum was not updated
  707. */
  708. int (*pkt_in)(struct ip_vs_app *, struct ip_vs_conn *,
  709. struct sk_buff *, int *diff, struct ip_vs_iphdr *ipvsh);
  710. /* ip_vs_app initializer */
  711. int (*init_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  712. /* ip_vs_app finish */
  713. int (*done_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  714. /* not used now */
  715. int (*bind_conn)(struct ip_vs_app *, struct ip_vs_conn *,
  716. struct ip_vs_protocol *);
  717. void (*unbind_conn)(struct ip_vs_app *, struct ip_vs_conn *);
  718. int * timeout_table;
  719. int * timeouts;
  720. int timeouts_size;
  721. int (*conn_schedule)(struct sk_buff *skb, struct ip_vs_app *app,
  722. int *verdict, struct ip_vs_conn **cpp);
  723. struct ip_vs_conn *
  724. (*conn_in_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  725. const struct iphdr *iph, int inverse);
  726. struct ip_vs_conn *
  727. (*conn_out_get)(const struct sk_buff *skb, struct ip_vs_app *app,
  728. const struct iphdr *iph, int inverse);
  729. int (*state_transition)(struct ip_vs_conn *cp, int direction,
  730. const struct sk_buff *skb,
  731. struct ip_vs_app *app);
  732. void (*timeout_change)(struct ip_vs_app *app, int flags);
  733. };
  734. struct ipvs_master_sync_state {
  735. struct list_head sync_queue;
  736. struct ip_vs_sync_buff *sync_buff;
  737. unsigned long sync_queue_len;
  738. unsigned int sync_queue_delay;
  739. struct delayed_work master_wakeup_work;
  740. struct netns_ipvs *ipvs;
  741. };
  742. struct ip_vs_sync_thread_data;
  743. /* How much time to keep dests in trash */
  744. #define IP_VS_DEST_TRASH_PERIOD (120 * HZ)
  745. struct ipvs_sync_daemon_cfg {
  746. union nf_inet_addr mcast_group;
  747. int syncid;
  748. u16 sync_maxlen;
  749. u16 mcast_port;
  750. u8 mcast_af;
  751. u8 mcast_ttl;
  752. /* multicast interface name */
  753. char mcast_ifn[IP_VS_IFNAME_MAXLEN];
  754. };
  755. /* IPVS in network namespace */
  756. struct netns_ipvs {
  757. int gen; /* Generation */
  758. int enable; /* enable like nf_hooks do */
  759. /* Hash table: for real service lookups */
  760. #define IP_VS_RTAB_BITS 4
  761. #define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)
  762. #define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1)
  763. struct hlist_head rs_table[IP_VS_RTAB_SIZE];
  764. /* ip_vs_app */
  765. struct list_head app_list;
  766. /* ip_vs_proto */
  767. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  768. struct ip_vs_proto_data *proto_data_table[IP_VS_PROTO_TAB_SIZE];
  769. /* ip_vs_proto_tcp */
  770. #ifdef CONFIG_IP_VS_PROTO_TCP
  771. #define TCP_APP_TAB_BITS 4
  772. #define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)
  773. #define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)
  774. struct list_head tcp_apps[TCP_APP_TAB_SIZE];
  775. #endif
  776. /* ip_vs_proto_udp */
  777. #ifdef CONFIG_IP_VS_PROTO_UDP
  778. #define UDP_APP_TAB_BITS 4
  779. #define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)
  780. #define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)
  781. struct list_head udp_apps[UDP_APP_TAB_SIZE];
  782. #endif
  783. /* ip_vs_proto_sctp */
  784. #ifdef CONFIG_IP_VS_PROTO_SCTP
  785. #define SCTP_APP_TAB_BITS 4
  786. #define SCTP_APP_TAB_SIZE (1 << SCTP_APP_TAB_BITS)
  787. #define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)
  788. /* Hash table for SCTP application incarnations */
  789. struct list_head sctp_apps[SCTP_APP_TAB_SIZE];
  790. #endif
  791. /* ip_vs_conn */
  792. atomic_t conn_count; /* connection counter */
  793. /* ip_vs_ctl */
  794. struct ip_vs_stats_rcu *tot_stats; /* Statistics & est. */
  795. int num_services; /* no of virtual services */
  796. int num_services6; /* IPv6 virtual services */
  797. /* Trash for destinations */
  798. struct list_head dest_trash;
  799. spinlock_t dest_trash_lock;
  800. struct timer_list dest_trash_timer; /* expiration timer */
  801. /* Service counters */
  802. atomic_t ftpsvc_counter;
  803. atomic_t nullsvc_counter;
  804. atomic_t conn_out_counter;
  805. #ifdef CONFIG_SYSCTL
  806. /* delayed work for expiring no dest connections */
  807. struct delayed_work expire_nodest_conn_work;
  808. /* 1/rate drop and drop-entry variables */
  809. struct delayed_work defense_work; /* Work handler */
  810. int drop_rate;
  811. int drop_counter;
  812. int old_secure_tcp;
  813. atomic_t dropentry;
  814. /* locks in ctl.c */
  815. spinlock_t dropentry_lock; /* drop entry handling */
  816. spinlock_t droppacket_lock; /* drop packet handling */
  817. spinlock_t securetcp_lock; /* state and timeout tables */
  818. /* sys-ctl struct */
  819. struct ctl_table_header *sysctl_hdr;
  820. struct ctl_table *sysctl_tbl;
  821. #endif
  822. /* sysctl variables */
  823. int sysctl_amemthresh;
  824. int sysctl_am_droprate;
  825. int sysctl_drop_entry;
  826. int sysctl_drop_packet;
  827. int sysctl_secure_tcp;
  828. #ifdef CONFIG_IP_VS_NFCT
  829. int sysctl_conntrack;
  830. #endif
  831. int sysctl_snat_reroute;
  832. int sysctl_sync_ver;
  833. int sysctl_sync_ports;
  834. int sysctl_sync_persist_mode;
  835. unsigned long sysctl_sync_qlen_max;
  836. int sysctl_sync_sock_size;
  837. int sysctl_cache_bypass;
  838. int sysctl_expire_nodest_conn;
  839. int sysctl_sloppy_tcp;
  840. int sysctl_sloppy_sctp;
  841. int sysctl_expire_quiescent_template;
  842. int sysctl_sync_threshold[2];
  843. unsigned int sysctl_sync_refresh_period;
  844. int sysctl_sync_retries;
  845. int sysctl_nat_icmp_send;
  846. int sysctl_pmtu_disc;
  847. int sysctl_backup_only;
  848. int sysctl_conn_reuse_mode;
  849. int sysctl_schedule_icmp;
  850. int sysctl_ignore_tunneled;
  851. int sysctl_run_estimation;
  852. #ifdef CONFIG_SYSCTL
  853. cpumask_var_t sysctl_est_cpulist; /* kthread cpumask */
  854. int est_cpulist_valid; /* cpulist set */
  855. int sysctl_est_nice; /* kthread nice */
  856. int est_stopped; /* stop tasks */
  857. #endif
  858. /* ip_vs_lblc */
  859. int sysctl_lblc_expiration;
  860. struct ctl_table_header *lblc_ctl_header;
  861. struct ctl_table *lblc_ctl_table;
  862. /* ip_vs_lblcr */
  863. int sysctl_lblcr_expiration;
  864. struct ctl_table_header *lblcr_ctl_header;
  865. struct ctl_table *lblcr_ctl_table;
  866. /* ip_vs_est */
  867. struct delayed_work est_reload_work;/* Reload kthread tasks */
  868. struct mutex est_mutex; /* protect kthread tasks */
  869. struct hlist_head est_temp_list; /* Ests during calc phase */
  870. struct ip_vs_est_kt_data **est_kt_arr; /* Array of kthread data ptrs */
  871. unsigned long est_max_threads;/* Hard limit of kthreads */
  872. int est_calc_phase; /* Calculation phase */
  873. int est_chain_max; /* Calculated chain_max */
  874. int est_kt_count; /* Allocated ptrs */
  875. int est_add_ktid; /* ktid where to add ests */
  876. atomic_t est_genid; /* kthreads reload genid */
  877. atomic_t est_genid_done; /* applied genid */
  878. /* ip_vs_sync */
  879. spinlock_t sync_lock;
  880. struct ipvs_master_sync_state *ms;
  881. spinlock_t sync_buff_lock;
  882. struct ip_vs_sync_thread_data *master_tinfo;
  883. struct ip_vs_sync_thread_data *backup_tinfo;
  884. int threads_mask;
  885. volatile int sync_state;
  886. struct mutex sync_mutex;
  887. struct ipvs_sync_daemon_cfg mcfg; /* Master Configuration */
  888. struct ipvs_sync_daemon_cfg bcfg; /* Backup Configuration */
  889. /* net name space ptr */
  890. struct net *net; /* Needed by timer routines */
  891. /* Number of heterogeneous destinations, needed because heterogeneous
  892. * are not supported when synchronization is enabled.
  893. */
  894. unsigned int mixed_address_family_dests;
  895. unsigned int hooks_afmask; /* &1=AF_INET, &2=AF_INET6 */
  896. };
  897. #define DEFAULT_SYNC_THRESHOLD 3
  898. #define DEFAULT_SYNC_PERIOD 50
  899. #define DEFAULT_SYNC_VER 1
  900. #define DEFAULT_SLOPPY_TCP 0
  901. #define DEFAULT_SLOPPY_SCTP 0
  902. #define DEFAULT_SYNC_REFRESH_PERIOD (0U * HZ)
  903. #define DEFAULT_SYNC_RETRIES 0
  904. #define IPVS_SYNC_WAKEUP_RATE 8
  905. #define IPVS_SYNC_QLEN_MAX (IPVS_SYNC_WAKEUP_RATE * 4)
  906. #define IPVS_SYNC_SEND_DELAY (HZ / 50)
  907. #define IPVS_SYNC_CHECK_PERIOD HZ
  908. #define IPVS_SYNC_FLUSH_TIME (HZ * 2)
  909. #define IPVS_SYNC_PORTS_MAX (1 << 6)
  910. #ifdef CONFIG_SYSCTL
  911. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  912. {
  913. return ipvs->sysctl_sync_threshold[0];
  914. }
  915. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  916. {
  917. return READ_ONCE(ipvs->sysctl_sync_threshold[1]);
  918. }
  919. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  920. {
  921. return READ_ONCE(ipvs->sysctl_sync_refresh_period);
  922. }
  923. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  924. {
  925. return ipvs->sysctl_sync_retries;
  926. }
  927. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  928. {
  929. return ipvs->sysctl_sync_ver;
  930. }
  931. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  932. {
  933. return ipvs->sysctl_sloppy_tcp;
  934. }
  935. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  936. {
  937. return ipvs->sysctl_sloppy_sctp;
  938. }
  939. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  940. {
  941. return READ_ONCE(ipvs->sysctl_sync_ports);
  942. }
  943. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  944. {
  945. return ipvs->sysctl_sync_persist_mode;
  946. }
  947. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  948. {
  949. return ipvs->sysctl_sync_qlen_max;
  950. }
  951. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  952. {
  953. return ipvs->sysctl_sync_sock_size;
  954. }
  955. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  956. {
  957. return ipvs->sysctl_pmtu_disc;
  958. }
  959. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  960. {
  961. return ipvs->sync_state & IP_VS_STATE_BACKUP &&
  962. ipvs->sysctl_backup_only;
  963. }
  964. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  965. {
  966. return ipvs->sysctl_conn_reuse_mode;
  967. }
  968. static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
  969. {
  970. return ipvs->sysctl_expire_nodest_conn;
  971. }
  972. static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
  973. {
  974. return ipvs->sysctl_schedule_icmp;
  975. }
  976. static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
  977. {
  978. return ipvs->sysctl_ignore_tunneled;
  979. }
  980. static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
  981. {
  982. return ipvs->sysctl_cache_bypass;
  983. }
  984. static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
  985. {
  986. return ipvs->sysctl_run_estimation;
  987. }
  988. static inline const struct cpumask *sysctl_est_cpulist(struct netns_ipvs *ipvs)
  989. {
  990. if (ipvs->est_cpulist_valid)
  991. return ipvs->sysctl_est_cpulist;
  992. else
  993. return housekeeping_cpumask(HK_TYPE_KTHREAD);
  994. }
  995. static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs)
  996. {
  997. if (ipvs->est_cpulist_valid)
  998. return ipvs->sysctl_est_cpulist;
  999. else
  1000. return NULL;
  1001. }
  1002. static inline int sysctl_est_nice(struct netns_ipvs *ipvs)
  1003. {
  1004. return ipvs->sysctl_est_nice;
  1005. }
  1006. #else
  1007. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  1008. {
  1009. return DEFAULT_SYNC_THRESHOLD;
  1010. }
  1011. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  1012. {
  1013. return DEFAULT_SYNC_PERIOD;
  1014. }
  1015. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  1016. {
  1017. return DEFAULT_SYNC_REFRESH_PERIOD;
  1018. }
  1019. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  1020. {
  1021. return DEFAULT_SYNC_RETRIES & 3;
  1022. }
  1023. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  1024. {
  1025. return DEFAULT_SYNC_VER;
  1026. }
  1027. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  1028. {
  1029. return DEFAULT_SLOPPY_TCP;
  1030. }
  1031. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  1032. {
  1033. return DEFAULT_SLOPPY_SCTP;
  1034. }
  1035. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  1036. {
  1037. return 1;
  1038. }
  1039. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  1040. {
  1041. return 0;
  1042. }
  1043. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  1044. {
  1045. return IPVS_SYNC_QLEN_MAX;
  1046. }
  1047. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  1048. {
  1049. return 0;
  1050. }
  1051. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  1052. {
  1053. return 1;
  1054. }
  1055. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  1056. {
  1057. return 0;
  1058. }
  1059. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  1060. {
  1061. return 1;
  1062. }
  1063. static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
  1064. {
  1065. return 0;
  1066. }
  1067. static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
  1068. {
  1069. return 0;
  1070. }
  1071. static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
  1072. {
  1073. return 0;
  1074. }
  1075. static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
  1076. {
  1077. return 0;
  1078. }
  1079. static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
  1080. {
  1081. return 1;
  1082. }
  1083. static inline const struct cpumask *sysctl_est_cpulist(struct netns_ipvs *ipvs)
  1084. {
  1085. return housekeeping_cpumask(HK_TYPE_KTHREAD);
  1086. }
  1087. static inline const struct cpumask *sysctl_est_preferred_cpulist(struct netns_ipvs *ipvs)
  1088. {
  1089. return NULL;
  1090. }
  1091. static inline int sysctl_est_nice(struct netns_ipvs *ipvs)
  1092. {
  1093. return IPVS_EST_NICE;
  1094. }
  1095. #endif
  1096. /* IPVS core functions
  1097. * (from ip_vs_core.c)
  1098. */
  1099. const char *ip_vs_proto_name(unsigned int proto);
  1100. void ip_vs_init_hash_table(struct list_head *table, int rows);
  1101. struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
  1102. struct ip_vs_dest *dest,
  1103. struct sk_buff *skb,
  1104. const struct ip_vs_iphdr *iph,
  1105. __be16 dport,
  1106. __be16 cport);
  1107. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  1108. #define IP_VS_APP_TYPE_FTP 1
  1109. /* ip_vs_conn handling functions
  1110. * (from ip_vs_conn.c)
  1111. */
  1112. enum {
  1113. IP_VS_DIR_INPUT = 0,
  1114. IP_VS_DIR_OUTPUT,
  1115. IP_VS_DIR_INPUT_ONLY,
  1116. IP_VS_DIR_LAST,
  1117. };
  1118. static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol,
  1119. const union nf_inet_addr *caddr,
  1120. __be16 cport,
  1121. const union nf_inet_addr *vaddr,
  1122. __be16 vport,
  1123. struct ip_vs_conn_param *p)
  1124. {
  1125. p->ipvs = ipvs;
  1126. p->af = af;
  1127. p->protocol = protocol;
  1128. p->caddr = caddr;
  1129. p->cport = cport;
  1130. p->vaddr = vaddr;
  1131. p->vport = vport;
  1132. p->pe = NULL;
  1133. p->pe_data = NULL;
  1134. }
  1135. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  1136. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  1137. struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
  1138. const struct sk_buff *skb,
  1139. const struct ip_vs_iphdr *iph);
  1140. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  1141. struct ip_vs_conn * ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
  1142. const struct sk_buff *skb,
  1143. const struct ip_vs_iphdr *iph);
  1144. /* Get reference to gain full access to conn.
  1145. * By default, RCU read-side critical sections have access only to
  1146. * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
  1147. */
  1148. static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
  1149. {
  1150. return refcount_inc_not_zero(&cp->refcnt);
  1151. }
  1152. /* put back the conn without restarting its timer */
  1153. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  1154. {
  1155. smp_mb__before_atomic();
  1156. refcount_dec(&cp->refcnt);
  1157. }
  1158. void ip_vs_conn_put(struct ip_vs_conn *cp);
  1159. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  1160. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
  1161. const union nf_inet_addr *daddr,
  1162. __be16 dport, unsigned int flags,
  1163. struct ip_vs_dest *dest, __u32 fwmark);
  1164. void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  1165. const char *ip_vs_state_name(const struct ip_vs_conn *cp);
  1166. void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  1167. int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest);
  1168. void ip_vs_random_dropentry(struct netns_ipvs *ipvs);
  1169. int ip_vs_conn_init(void);
  1170. void ip_vs_conn_cleanup(void);
  1171. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  1172. {
  1173. struct ip_vs_conn *ctl_cp = cp->control;
  1174. if (!ctl_cp) {
  1175. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  1176. "%s:%d to %s:%d\n",
  1177. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1178. ntohs(cp->cport),
  1179. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1180. ntohs(cp->vport));
  1181. return;
  1182. }
  1183. IP_VS_DBG_BUF(7, "DELeting control for: "
  1184. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1185. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1186. ntohs(cp->cport),
  1187. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1188. ntohs(ctl_cp->cport));
  1189. cp->control = NULL;
  1190. if (atomic_read(&ctl_cp->n_control) == 0) {
  1191. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  1192. "%s:%d to %s:%d\n",
  1193. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1194. ntohs(cp->cport),
  1195. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1196. ntohs(cp->vport));
  1197. return;
  1198. }
  1199. atomic_dec(&ctl_cp->n_control);
  1200. }
  1201. static inline void
  1202. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  1203. {
  1204. if (cp->control) {
  1205. IP_VS_ERR_BUF("request control ADD for already controlled: "
  1206. "%s:%d to %s:%d\n",
  1207. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1208. ntohs(cp->cport),
  1209. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1210. ntohs(cp->vport));
  1211. ip_vs_control_del(cp);
  1212. }
  1213. IP_VS_DBG_BUF(7, "ADDing control for: "
  1214. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1215. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1216. ntohs(cp->cport),
  1217. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1218. ntohs(ctl_cp->cport));
  1219. cp->control = ctl_cp;
  1220. atomic_inc(&ctl_cp->n_control);
  1221. }
  1222. /* Mark our template as assured */
  1223. static inline void
  1224. ip_vs_control_assure_ct(struct ip_vs_conn *cp)
  1225. {
  1226. struct ip_vs_conn *ct = cp->control;
  1227. if (ct && !(ct->state & IP_VS_CTPL_S_ASSURED) &&
  1228. (ct->flags & IP_VS_CONN_F_TEMPLATE))
  1229. ct->state |= IP_VS_CTPL_S_ASSURED;
  1230. }
  1231. /* IPVS netns init & cleanup functions */
  1232. int ip_vs_estimator_net_init(struct netns_ipvs *ipvs);
  1233. int ip_vs_control_net_init(struct netns_ipvs *ipvs);
  1234. int ip_vs_protocol_net_init(struct netns_ipvs *ipvs);
  1235. int ip_vs_app_net_init(struct netns_ipvs *ipvs);
  1236. int ip_vs_conn_net_init(struct netns_ipvs *ipvs);
  1237. int ip_vs_sync_net_init(struct netns_ipvs *ipvs);
  1238. void ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs);
  1239. void ip_vs_app_net_cleanup(struct netns_ipvs *ipvs);
  1240. void ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs);
  1241. void ip_vs_control_net_cleanup(struct netns_ipvs *ipvs);
  1242. void ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs);
  1243. void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs);
  1244. void ip_vs_service_nets_cleanup(struct list_head *net_list);
  1245. /* IPVS application functions
  1246. * (from ip_vs_app.c)
  1247. */
  1248. #define IP_VS_APP_MAX_PORTS 8
  1249. struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1250. void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1251. int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1252. void ip_vs_unbind_app(struct ip_vs_conn *cp);
  1253. int register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
  1254. __u16 port);
  1255. int ip_vs_app_inc_get(struct ip_vs_app *inc);
  1256. void ip_vs_app_inc_put(struct ip_vs_app *inc);
  1257. int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb,
  1258. struct ip_vs_iphdr *ipvsh);
  1259. int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb,
  1260. struct ip_vs_iphdr *ipvsh);
  1261. int register_ip_vs_pe(struct ip_vs_pe *pe);
  1262. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  1263. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  1264. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  1265. /* Use a #define to avoid all of module.h just for these trivial ops */
  1266. #define ip_vs_pe_get(pe) \
  1267. if (pe && pe->module) \
  1268. __module_get(pe->module);
  1269. #define ip_vs_pe_put(pe) \
  1270. if (pe && pe->module) \
  1271. module_put(pe->module);
  1272. /* IPVS protocol functions (from ip_vs_proto.c) */
  1273. int ip_vs_protocol_init(void);
  1274. void ip_vs_protocol_cleanup(void);
  1275. void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  1276. int *ip_vs_create_timeout_table(int *table, int size);
  1277. void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  1278. const struct sk_buff *skb, int offset,
  1279. const char *msg);
  1280. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1281. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1282. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1283. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1284. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1285. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1286. /* Registering/unregistering scheduler functions
  1287. * (from ip_vs_sched.c)
  1288. */
  1289. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1290. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1291. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1292. struct ip_vs_scheduler *scheduler);
  1293. void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
  1294. struct ip_vs_scheduler *sched);
  1295. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1296. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1297. struct ip_vs_conn *
  1298. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1299. struct ip_vs_proto_data *pd, int *ignored,
  1300. struct ip_vs_iphdr *iph);
  1301. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1302. struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
  1303. void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1304. /* IPVS control data and functions (from ip_vs_ctl.c) */
  1305. extern struct ip_vs_stats ip_vs_stats;
  1306. extern int sysctl_ip_vs_sync_ver;
  1307. struct ip_vs_service *
  1308. ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
  1309. const union nf_inet_addr *vaddr, __be16 vport);
  1310. bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
  1311. const union nf_inet_addr *daddr, __be16 dport);
  1312. struct ip_vs_dest *
  1313. ip_vs_find_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
  1314. const union nf_inet_addr *daddr, __be16 dport);
  1315. struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
  1316. const union nf_inet_addr *daddr,
  1317. __be16 tun_port);
  1318. int ip_vs_use_count_inc(void);
  1319. void ip_vs_use_count_dec(void);
  1320. int ip_vs_register_nl_ioctl(void);
  1321. void ip_vs_unregister_nl_ioctl(void);
  1322. int ip_vs_control_init(void);
  1323. void ip_vs_control_cleanup(void);
  1324. struct ip_vs_dest *
  1325. ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
  1326. const union nf_inet_addr *daddr, __be16 dport,
  1327. const union nf_inet_addr *vaddr, __be16 vport,
  1328. __u16 protocol, __u32 fwmark, __u32 flags);
  1329. void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1330. static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
  1331. {
  1332. refcount_inc(&dest->refcnt);
  1333. }
  1334. static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
  1335. {
  1336. smp_mb__before_atomic();
  1337. refcount_dec(&dest->refcnt);
  1338. }
  1339. static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
  1340. {
  1341. if (refcount_dec_and_test(&dest->refcnt))
  1342. kfree(dest);
  1343. }
  1344. /* IPVS sync daemon data and function prototypes
  1345. * (from ip_vs_sync.c)
  1346. */
  1347. int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *cfg,
  1348. int state);
  1349. int stop_sync_thread(struct netns_ipvs *ipvs, int state);
  1350. void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts);
  1351. /* IPVS rate estimator prototypes (from ip_vs_est.c) */
  1352. int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1353. void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1354. void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1355. void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
  1356. void ip_vs_est_reload_start(struct netns_ipvs *ipvs);
  1357. int ip_vs_est_kthread_start(struct netns_ipvs *ipvs,
  1358. struct ip_vs_est_kt_data *kd);
  1359. void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);
  1360. static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs)
  1361. {
  1362. #ifdef CONFIG_SYSCTL
  1363. /* Stop tasks while cpulist is empty or if disabled with flag */
  1364. ipvs->est_stopped = !sysctl_run_estimation(ipvs) ||
  1365. (ipvs->est_cpulist_valid &&
  1366. cpumask_empty(sysctl_est_cpulist(ipvs)));
  1367. #endif
  1368. }
  1369. static inline bool ip_vs_est_stopped(struct netns_ipvs *ipvs)
  1370. {
  1371. #ifdef CONFIG_SYSCTL
  1372. return ipvs->est_stopped;
  1373. #else
  1374. return false;
  1375. #endif
  1376. }
  1377. static inline int ip_vs_est_max_threads(struct netns_ipvs *ipvs)
  1378. {
  1379. unsigned int limit = IPVS_EST_CPU_KTHREADS *
  1380. cpumask_weight(sysctl_est_cpulist(ipvs));
  1381. return max(1U, limit);
  1382. }
  1383. /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
  1384. int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1385. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1386. int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1387. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1388. int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1389. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1390. int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1391. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1392. int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1393. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1394. int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1395. struct ip_vs_protocol *pp, int offset,
  1396. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1397. void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
  1398. #ifdef CONFIG_IP_VS_IPV6
  1399. int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1400. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1401. int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1402. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1403. int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1404. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1405. int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1406. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1407. int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1408. struct ip_vs_protocol *pp, int offset,
  1409. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1410. #endif
  1411. #ifdef CONFIG_SYSCTL
  1412. /* This is a simple mechanism to ignore packets when
  1413. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1414. * we start to drop 1/rate of the packets
  1415. */
  1416. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1417. {
  1418. if (!ipvs->drop_rate)
  1419. return 0;
  1420. if (--ipvs->drop_counter > 0)
  1421. return 0;
  1422. ipvs->drop_counter = ipvs->drop_rate;
  1423. return 1;
  1424. }
  1425. #else
  1426. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1427. #endif
  1428. #ifdef CONFIG_SYSCTL
  1429. /* Enqueue delayed work for expiring no dest connections
  1430. * Only run when sysctl_expire_nodest=1
  1431. */
  1432. static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs)
  1433. {
  1434. if (sysctl_expire_nodest_conn(ipvs))
  1435. queue_delayed_work(system_long_wq,
  1436. &ipvs->expire_nodest_conn_work, 1);
  1437. }
  1438. void ip_vs_expire_nodest_conn_flush(struct netns_ipvs *ipvs);
  1439. #else
  1440. static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs) {}
  1441. #endif
  1442. #define IP_VS_DFWD_METHOD(dest) (atomic_read(&(dest)->conn_flags) & \
  1443. IP_VS_CONN_F_FWD_MASK)
  1444. /* ip_vs_fwd_tag returns the forwarding tag of the connection */
  1445. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1446. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1447. {
  1448. char fwd;
  1449. switch (IP_VS_FWD_METHOD(cp)) {
  1450. case IP_VS_CONN_F_MASQ:
  1451. fwd = 'M'; break;
  1452. case IP_VS_CONN_F_LOCALNODE:
  1453. fwd = 'L'; break;
  1454. case IP_VS_CONN_F_TUNNEL:
  1455. fwd = 'T'; break;
  1456. case IP_VS_CONN_F_DROUTE:
  1457. fwd = 'R'; break;
  1458. case IP_VS_CONN_F_BYPASS:
  1459. fwd = 'B'; break;
  1460. default:
  1461. fwd = '?'; break;
  1462. }
  1463. return fwd;
  1464. }
  1465. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1466. struct ip_vs_conn *cp, int dir);
  1467. #ifdef CONFIG_IP_VS_IPV6
  1468. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1469. struct ip_vs_conn *cp, int dir);
  1470. #endif
  1471. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1472. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1473. {
  1474. __be32 diff[2] = { ~old, new };
  1475. return csum_partial(diff, sizeof(diff), oldsum);
  1476. }
  1477. #ifdef CONFIG_IP_VS_IPV6
  1478. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1479. __wsum oldsum)
  1480. {
  1481. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1482. new[3], new[2], new[1], new[0] };
  1483. return csum_partial(diff, sizeof(diff), oldsum);
  1484. }
  1485. #endif
  1486. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1487. {
  1488. __be16 diff[2] = { ~old, new };
  1489. return csum_partial(diff, sizeof(diff), oldsum);
  1490. }
  1491. /* Forget current conntrack (unconfirmed) and attach notrack entry */
  1492. static inline void ip_vs_notrack(struct sk_buff *skb)
  1493. {
  1494. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1495. enum ip_conntrack_info ctinfo;
  1496. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1497. if (ct) {
  1498. nf_conntrack_put(&ct->ct_general);
  1499. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  1500. }
  1501. #endif
  1502. }
  1503. #ifdef CONFIG_IP_VS_NFCT
  1504. /* Netfilter connection tracking
  1505. * (from ip_vs_nfct.c)
  1506. */
  1507. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1508. {
  1509. #ifdef CONFIG_SYSCTL
  1510. return ipvs->sysctl_conntrack;
  1511. #else
  1512. return 0;
  1513. #endif
  1514. }
  1515. void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1516. int outin);
  1517. int ip_vs_confirm_conntrack(struct sk_buff *skb);
  1518. void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1519. struct ip_vs_conn *cp, u_int8_t proto,
  1520. const __be16 port, int from_rs);
  1521. void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1522. #else
  1523. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1524. {
  1525. return 0;
  1526. }
  1527. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1528. struct ip_vs_conn *cp, int outin)
  1529. {
  1530. }
  1531. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
  1532. {
  1533. return NF_ACCEPT;
  1534. }
  1535. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1536. {
  1537. }
  1538. #endif /* CONFIG_IP_VS_NFCT */
  1539. /* Using old conntrack that can not be redirected to another real server? */
  1540. static inline bool ip_vs_conn_uses_old_conntrack(struct ip_vs_conn *cp,
  1541. struct sk_buff *skb)
  1542. {
  1543. #ifdef CONFIG_IP_VS_NFCT
  1544. enum ip_conntrack_info ctinfo;
  1545. struct nf_conn *ct;
  1546. ct = nf_ct_get(skb, &ctinfo);
  1547. if (ct && nf_ct_is_confirmed(ct))
  1548. return true;
  1549. #endif
  1550. return false;
  1551. }
  1552. static inline int ip_vs_register_conntrack(struct ip_vs_service *svc)
  1553. {
  1554. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  1555. int afmask = (svc->af == AF_INET6) ? 2 : 1;
  1556. int ret = 0;
  1557. if (!(svc->conntrack_afmask & afmask)) {
  1558. ret = nf_ct_netns_get(svc->ipvs->net, svc->af);
  1559. if (ret >= 0)
  1560. svc->conntrack_afmask |= afmask;
  1561. }
  1562. return ret;
  1563. #else
  1564. return 0;
  1565. #endif
  1566. }
  1567. static inline void ip_vs_unregister_conntrack(struct ip_vs_service *svc)
  1568. {
  1569. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  1570. int afmask = (svc->af == AF_INET6) ? 2 : 1;
  1571. if (svc->conntrack_afmask & afmask) {
  1572. nf_ct_netns_put(svc->ipvs->net, svc->af);
  1573. svc->conntrack_afmask &= ~afmask;
  1574. }
  1575. #endif
  1576. }
  1577. int ip_vs_register_hooks(struct netns_ipvs *ipvs, unsigned int af);
  1578. void ip_vs_unregister_hooks(struct netns_ipvs *ipvs, unsigned int af);
  1579. static inline int
  1580. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1581. {
  1582. /* We think the overhead of processing active connections is 256
  1583. * times higher than that of inactive connections in average. (This
  1584. * 256 times might not be accurate, we will change it later) We
  1585. * use the following formula to estimate the overhead now:
  1586. * dest->activeconns*256 + dest->inactconns
  1587. */
  1588. return (atomic_read(&dest->activeconns) << 8) +
  1589. atomic_read(&dest->inactconns);
  1590. }
  1591. #ifdef CONFIG_IP_VS_PROTO_TCP
  1592. INDIRECT_CALLABLE_DECLARE(int
  1593. tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1594. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  1595. #endif
  1596. #ifdef CONFIG_IP_VS_PROTO_UDP
  1597. INDIRECT_CALLABLE_DECLARE(int
  1598. udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1599. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  1600. #endif
  1601. #endif /* _NET_IP_VS_H */