ip_vs.h 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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 int sysctl_est_nice(struct netns_ipvs *ipvs)
  996. {
  997. return ipvs->sysctl_est_nice;
  998. }
  999. #else
  1000. static inline int sysctl_sync_threshold(struct netns_ipvs *ipvs)
  1001. {
  1002. return DEFAULT_SYNC_THRESHOLD;
  1003. }
  1004. static inline int sysctl_sync_period(struct netns_ipvs *ipvs)
  1005. {
  1006. return DEFAULT_SYNC_PERIOD;
  1007. }
  1008. static inline unsigned int sysctl_sync_refresh_period(struct netns_ipvs *ipvs)
  1009. {
  1010. return DEFAULT_SYNC_REFRESH_PERIOD;
  1011. }
  1012. static inline int sysctl_sync_retries(struct netns_ipvs *ipvs)
  1013. {
  1014. return DEFAULT_SYNC_RETRIES & 3;
  1015. }
  1016. static inline int sysctl_sync_ver(struct netns_ipvs *ipvs)
  1017. {
  1018. return DEFAULT_SYNC_VER;
  1019. }
  1020. static inline int sysctl_sloppy_tcp(struct netns_ipvs *ipvs)
  1021. {
  1022. return DEFAULT_SLOPPY_TCP;
  1023. }
  1024. static inline int sysctl_sloppy_sctp(struct netns_ipvs *ipvs)
  1025. {
  1026. return DEFAULT_SLOPPY_SCTP;
  1027. }
  1028. static inline int sysctl_sync_ports(struct netns_ipvs *ipvs)
  1029. {
  1030. return 1;
  1031. }
  1032. static inline int sysctl_sync_persist_mode(struct netns_ipvs *ipvs)
  1033. {
  1034. return 0;
  1035. }
  1036. static inline unsigned long sysctl_sync_qlen_max(struct netns_ipvs *ipvs)
  1037. {
  1038. return IPVS_SYNC_QLEN_MAX;
  1039. }
  1040. static inline int sysctl_sync_sock_size(struct netns_ipvs *ipvs)
  1041. {
  1042. return 0;
  1043. }
  1044. static inline int sysctl_pmtu_disc(struct netns_ipvs *ipvs)
  1045. {
  1046. return 1;
  1047. }
  1048. static inline int sysctl_backup_only(struct netns_ipvs *ipvs)
  1049. {
  1050. return 0;
  1051. }
  1052. static inline int sysctl_conn_reuse_mode(struct netns_ipvs *ipvs)
  1053. {
  1054. return 1;
  1055. }
  1056. static inline int sysctl_expire_nodest_conn(struct netns_ipvs *ipvs)
  1057. {
  1058. return 0;
  1059. }
  1060. static inline int sysctl_schedule_icmp(struct netns_ipvs *ipvs)
  1061. {
  1062. return 0;
  1063. }
  1064. static inline int sysctl_ignore_tunneled(struct netns_ipvs *ipvs)
  1065. {
  1066. return 0;
  1067. }
  1068. static inline int sysctl_cache_bypass(struct netns_ipvs *ipvs)
  1069. {
  1070. return 0;
  1071. }
  1072. static inline int sysctl_run_estimation(struct netns_ipvs *ipvs)
  1073. {
  1074. return 1;
  1075. }
  1076. static inline const struct cpumask *sysctl_est_cpulist(struct netns_ipvs *ipvs)
  1077. {
  1078. return housekeeping_cpumask(HK_TYPE_KTHREAD);
  1079. }
  1080. static inline int sysctl_est_nice(struct netns_ipvs *ipvs)
  1081. {
  1082. return IPVS_EST_NICE;
  1083. }
  1084. #endif
  1085. /* IPVS core functions
  1086. * (from ip_vs_core.c)
  1087. */
  1088. const char *ip_vs_proto_name(unsigned int proto);
  1089. void ip_vs_init_hash_table(struct list_head *table, int rows);
  1090. struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,
  1091. struct ip_vs_dest *dest,
  1092. struct sk_buff *skb,
  1093. const struct ip_vs_iphdr *iph,
  1094. __be16 dport,
  1095. __be16 cport);
  1096. #define IP_VS_INIT_HASH_TABLE(t) ip_vs_init_hash_table((t), ARRAY_SIZE((t)))
  1097. #define IP_VS_APP_TYPE_FTP 1
  1098. /* ip_vs_conn handling functions
  1099. * (from ip_vs_conn.c)
  1100. */
  1101. enum {
  1102. IP_VS_DIR_INPUT = 0,
  1103. IP_VS_DIR_OUTPUT,
  1104. IP_VS_DIR_INPUT_ONLY,
  1105. IP_VS_DIR_LAST,
  1106. };
  1107. static inline void ip_vs_conn_fill_param(struct netns_ipvs *ipvs, int af, int protocol,
  1108. const union nf_inet_addr *caddr,
  1109. __be16 cport,
  1110. const union nf_inet_addr *vaddr,
  1111. __be16 vport,
  1112. struct ip_vs_conn_param *p)
  1113. {
  1114. p->ipvs = ipvs;
  1115. p->af = af;
  1116. p->protocol = protocol;
  1117. p->caddr = caddr;
  1118. p->cport = cport;
  1119. p->vaddr = vaddr;
  1120. p->vport = vport;
  1121. p->pe = NULL;
  1122. p->pe_data = NULL;
  1123. }
  1124. struct ip_vs_conn *ip_vs_conn_in_get(const struct ip_vs_conn_param *p);
  1125. struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p);
  1126. struct ip_vs_conn * ip_vs_conn_in_get_proto(struct netns_ipvs *ipvs, int af,
  1127. const struct sk_buff *skb,
  1128. const struct ip_vs_iphdr *iph);
  1129. struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p);
  1130. struct ip_vs_conn * ip_vs_conn_out_get_proto(struct netns_ipvs *ipvs, int af,
  1131. const struct sk_buff *skb,
  1132. const struct ip_vs_iphdr *iph);
  1133. /* Get reference to gain full access to conn.
  1134. * By default, RCU read-side critical sections have access only to
  1135. * conn fields and its PE data, see ip_vs_conn_rcu_free() for reference.
  1136. */
  1137. static inline bool __ip_vs_conn_get(struct ip_vs_conn *cp)
  1138. {
  1139. return refcount_inc_not_zero(&cp->refcnt);
  1140. }
  1141. /* put back the conn without restarting its timer */
  1142. static inline void __ip_vs_conn_put(struct ip_vs_conn *cp)
  1143. {
  1144. smp_mb__before_atomic();
  1145. refcount_dec(&cp->refcnt);
  1146. }
  1147. void ip_vs_conn_put(struct ip_vs_conn *cp);
  1148. void ip_vs_conn_fill_cport(struct ip_vs_conn *cp, __be16 cport);
  1149. struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,
  1150. const union nf_inet_addr *daddr,
  1151. __be16 dport, unsigned int flags,
  1152. struct ip_vs_dest *dest, __u32 fwmark);
  1153. void ip_vs_conn_expire_now(struct ip_vs_conn *cp);
  1154. const char *ip_vs_state_name(const struct ip_vs_conn *cp);
  1155. void ip_vs_tcp_conn_listen(struct ip_vs_conn *cp);
  1156. int ip_vs_check_template(struct ip_vs_conn *ct, struct ip_vs_dest *cdest);
  1157. void ip_vs_random_dropentry(struct netns_ipvs *ipvs);
  1158. int ip_vs_conn_init(void);
  1159. void ip_vs_conn_cleanup(void);
  1160. static inline void ip_vs_control_del(struct ip_vs_conn *cp)
  1161. {
  1162. struct ip_vs_conn *ctl_cp = cp->control;
  1163. if (!ctl_cp) {
  1164. IP_VS_ERR_BUF("request control DEL for uncontrolled: "
  1165. "%s:%d to %s:%d\n",
  1166. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1167. ntohs(cp->cport),
  1168. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1169. ntohs(cp->vport));
  1170. return;
  1171. }
  1172. IP_VS_DBG_BUF(7, "DELeting control for: "
  1173. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1174. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1175. ntohs(cp->cport),
  1176. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1177. ntohs(ctl_cp->cport));
  1178. cp->control = NULL;
  1179. if (atomic_read(&ctl_cp->n_control) == 0) {
  1180. IP_VS_ERR_BUF("BUG control DEL with n=0 : "
  1181. "%s:%d to %s:%d\n",
  1182. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1183. ntohs(cp->cport),
  1184. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1185. ntohs(cp->vport));
  1186. return;
  1187. }
  1188. atomic_dec(&ctl_cp->n_control);
  1189. }
  1190. static inline void
  1191. ip_vs_control_add(struct ip_vs_conn *cp, struct ip_vs_conn *ctl_cp)
  1192. {
  1193. if (cp->control) {
  1194. IP_VS_ERR_BUF("request control ADD for already controlled: "
  1195. "%s:%d to %s:%d\n",
  1196. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1197. ntohs(cp->cport),
  1198. IP_VS_DBG_ADDR(cp->af, &cp->vaddr),
  1199. ntohs(cp->vport));
  1200. ip_vs_control_del(cp);
  1201. }
  1202. IP_VS_DBG_BUF(7, "ADDing control for: "
  1203. "cp.dst=%s:%d ctl_cp.dst=%s:%d\n",
  1204. IP_VS_DBG_ADDR(cp->af, &cp->caddr),
  1205. ntohs(cp->cport),
  1206. IP_VS_DBG_ADDR(cp->af, &ctl_cp->caddr),
  1207. ntohs(ctl_cp->cport));
  1208. cp->control = ctl_cp;
  1209. atomic_inc(&ctl_cp->n_control);
  1210. }
  1211. /* Mark our template as assured */
  1212. static inline void
  1213. ip_vs_control_assure_ct(struct ip_vs_conn *cp)
  1214. {
  1215. struct ip_vs_conn *ct = cp->control;
  1216. if (ct && !(ct->state & IP_VS_CTPL_S_ASSURED) &&
  1217. (ct->flags & IP_VS_CONN_F_TEMPLATE))
  1218. ct->state |= IP_VS_CTPL_S_ASSURED;
  1219. }
  1220. /* IPVS netns init & cleanup functions */
  1221. int ip_vs_estimator_net_init(struct netns_ipvs *ipvs);
  1222. int ip_vs_control_net_init(struct netns_ipvs *ipvs);
  1223. int ip_vs_protocol_net_init(struct netns_ipvs *ipvs);
  1224. int ip_vs_app_net_init(struct netns_ipvs *ipvs);
  1225. int ip_vs_conn_net_init(struct netns_ipvs *ipvs);
  1226. int ip_vs_sync_net_init(struct netns_ipvs *ipvs);
  1227. void ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs);
  1228. void ip_vs_app_net_cleanup(struct netns_ipvs *ipvs);
  1229. void ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs);
  1230. void ip_vs_control_net_cleanup(struct netns_ipvs *ipvs);
  1231. void ip_vs_estimator_net_cleanup(struct netns_ipvs *ipvs);
  1232. void ip_vs_sync_net_cleanup(struct netns_ipvs *ipvs);
  1233. void ip_vs_service_nets_cleanup(struct list_head *net_list);
  1234. /* IPVS application functions
  1235. * (from ip_vs_app.c)
  1236. */
  1237. #define IP_VS_APP_MAX_PORTS 8
  1238. struct ip_vs_app *register_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1239. void unregister_ip_vs_app(struct netns_ipvs *ipvs, struct ip_vs_app *app);
  1240. int ip_vs_bind_app(struct ip_vs_conn *cp, struct ip_vs_protocol *pp);
  1241. void ip_vs_unbind_app(struct ip_vs_conn *cp);
  1242. int register_ip_vs_app_inc(struct netns_ipvs *ipvs, struct ip_vs_app *app, __u16 proto,
  1243. __u16 port);
  1244. int ip_vs_app_inc_get(struct ip_vs_app *inc);
  1245. void ip_vs_app_inc_put(struct ip_vs_app *inc);
  1246. int ip_vs_app_pkt_out(struct ip_vs_conn *, struct sk_buff *skb,
  1247. struct ip_vs_iphdr *ipvsh);
  1248. int ip_vs_app_pkt_in(struct ip_vs_conn *, struct sk_buff *skb,
  1249. struct ip_vs_iphdr *ipvsh);
  1250. int register_ip_vs_pe(struct ip_vs_pe *pe);
  1251. int unregister_ip_vs_pe(struct ip_vs_pe *pe);
  1252. struct ip_vs_pe *ip_vs_pe_getbyname(const char *name);
  1253. struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name);
  1254. /* Use a #define to avoid all of module.h just for these trivial ops */
  1255. #define ip_vs_pe_get(pe) \
  1256. if (pe && pe->module) \
  1257. __module_get(pe->module);
  1258. #define ip_vs_pe_put(pe) \
  1259. if (pe && pe->module) \
  1260. module_put(pe->module);
  1261. /* IPVS protocol functions (from ip_vs_proto.c) */
  1262. int ip_vs_protocol_init(void);
  1263. void ip_vs_protocol_cleanup(void);
  1264. void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags);
  1265. int *ip_vs_create_timeout_table(int *table, int size);
  1266. void ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
  1267. const struct sk_buff *skb, int offset,
  1268. const char *msg);
  1269. extern struct ip_vs_protocol ip_vs_protocol_tcp;
  1270. extern struct ip_vs_protocol ip_vs_protocol_udp;
  1271. extern struct ip_vs_protocol ip_vs_protocol_icmp;
  1272. extern struct ip_vs_protocol ip_vs_protocol_esp;
  1273. extern struct ip_vs_protocol ip_vs_protocol_ah;
  1274. extern struct ip_vs_protocol ip_vs_protocol_sctp;
  1275. /* Registering/unregistering scheduler functions
  1276. * (from ip_vs_sched.c)
  1277. */
  1278. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1279. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
  1280. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  1281. struct ip_vs_scheduler *scheduler);
  1282. void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
  1283. struct ip_vs_scheduler *sched);
  1284. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name);
  1285. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler);
  1286. struct ip_vs_conn *
  1287. ip_vs_schedule(struct ip_vs_service *svc, struct sk_buff *skb,
  1288. struct ip_vs_proto_data *pd, int *ignored,
  1289. struct ip_vs_iphdr *iph);
  1290. int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb,
  1291. struct ip_vs_proto_data *pd, struct ip_vs_iphdr *iph);
  1292. void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg);
  1293. /* IPVS control data and functions (from ip_vs_ctl.c) */
  1294. extern struct ip_vs_stats ip_vs_stats;
  1295. extern int sysctl_ip_vs_sync_ver;
  1296. struct ip_vs_service *
  1297. ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u32 fwmark, __u16 protocol,
  1298. const union nf_inet_addr *vaddr, __be16 vport);
  1299. bool ip_vs_has_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
  1300. const union nf_inet_addr *daddr, __be16 dport);
  1301. struct ip_vs_dest *
  1302. ip_vs_find_real_service(struct netns_ipvs *ipvs, int af, __u16 protocol,
  1303. const union nf_inet_addr *daddr, __be16 dport);
  1304. struct ip_vs_dest *ip_vs_find_tunnel(struct netns_ipvs *ipvs, int af,
  1305. const union nf_inet_addr *daddr,
  1306. __be16 tun_port);
  1307. int ip_vs_use_count_inc(void);
  1308. void ip_vs_use_count_dec(void);
  1309. int ip_vs_register_nl_ioctl(void);
  1310. void ip_vs_unregister_nl_ioctl(void);
  1311. int ip_vs_control_init(void);
  1312. void ip_vs_control_cleanup(void);
  1313. struct ip_vs_dest *
  1314. ip_vs_find_dest(struct netns_ipvs *ipvs, int svc_af, int dest_af,
  1315. const union nf_inet_addr *daddr, __be16 dport,
  1316. const union nf_inet_addr *vaddr, __be16 vport,
  1317. __u16 protocol, __u32 fwmark, __u32 flags);
  1318. void ip_vs_try_bind_dest(struct ip_vs_conn *cp);
  1319. static inline void ip_vs_dest_hold(struct ip_vs_dest *dest)
  1320. {
  1321. refcount_inc(&dest->refcnt);
  1322. }
  1323. static inline void ip_vs_dest_put(struct ip_vs_dest *dest)
  1324. {
  1325. smp_mb__before_atomic();
  1326. refcount_dec(&dest->refcnt);
  1327. }
  1328. static inline void ip_vs_dest_put_and_free(struct ip_vs_dest *dest)
  1329. {
  1330. if (refcount_dec_and_test(&dest->refcnt))
  1331. kfree(dest);
  1332. }
  1333. /* IPVS sync daemon data and function prototypes
  1334. * (from ip_vs_sync.c)
  1335. */
  1336. int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *cfg,
  1337. int state);
  1338. int stop_sync_thread(struct netns_ipvs *ipvs, int state);
  1339. void ip_vs_sync_conn(struct netns_ipvs *ipvs, struct ip_vs_conn *cp, int pkts);
  1340. /* IPVS rate estimator prototypes (from ip_vs_est.c) */
  1341. int ip_vs_start_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1342. void ip_vs_stop_estimator(struct netns_ipvs *ipvs, struct ip_vs_stats *stats);
  1343. void ip_vs_zero_estimator(struct ip_vs_stats *stats);
  1344. void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats);
  1345. void ip_vs_est_reload_start(struct netns_ipvs *ipvs);
  1346. int ip_vs_est_kthread_start(struct netns_ipvs *ipvs,
  1347. struct ip_vs_est_kt_data *kd);
  1348. void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);
  1349. static inline void ip_vs_est_stopped_recalc(struct netns_ipvs *ipvs)
  1350. {
  1351. #ifdef CONFIG_SYSCTL
  1352. /* Stop tasks while cpulist is empty or if disabled with flag */
  1353. ipvs->est_stopped = !sysctl_run_estimation(ipvs) ||
  1354. (ipvs->est_cpulist_valid &&
  1355. cpumask_empty(sysctl_est_cpulist(ipvs)));
  1356. #endif
  1357. }
  1358. static inline bool ip_vs_est_stopped(struct netns_ipvs *ipvs)
  1359. {
  1360. #ifdef CONFIG_SYSCTL
  1361. return ipvs->est_stopped;
  1362. #else
  1363. return false;
  1364. #endif
  1365. }
  1366. static inline int ip_vs_est_max_threads(struct netns_ipvs *ipvs)
  1367. {
  1368. unsigned int limit = IPVS_EST_CPU_KTHREADS *
  1369. cpumask_weight(sysctl_est_cpulist(ipvs));
  1370. return max(1U, limit);
  1371. }
  1372. /* Various IPVS packet transmitters (from ip_vs_xmit.c) */
  1373. int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1374. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1375. int ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1376. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1377. int ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1378. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1379. int ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1380. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1381. int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1382. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1383. int ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
  1384. struct ip_vs_protocol *pp, int offset,
  1385. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1386. void ip_vs_dest_dst_rcu_free(struct rcu_head *head);
  1387. #ifdef CONFIG_IP_VS_IPV6
  1388. int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1389. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1390. int ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1391. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1392. int ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1393. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1394. int ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1395. struct ip_vs_protocol *pp, struct ip_vs_iphdr *iph);
  1396. int ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
  1397. struct ip_vs_protocol *pp, int offset,
  1398. unsigned int hooknum, struct ip_vs_iphdr *iph);
  1399. #endif
  1400. #ifdef CONFIG_SYSCTL
  1401. /* This is a simple mechanism to ignore packets when
  1402. * we are loaded. Just set ip_vs_drop_rate to 'n' and
  1403. * we start to drop 1/rate of the packets
  1404. */
  1405. static inline int ip_vs_todrop(struct netns_ipvs *ipvs)
  1406. {
  1407. if (!ipvs->drop_rate)
  1408. return 0;
  1409. if (--ipvs->drop_counter > 0)
  1410. return 0;
  1411. ipvs->drop_counter = ipvs->drop_rate;
  1412. return 1;
  1413. }
  1414. #else
  1415. static inline int ip_vs_todrop(struct netns_ipvs *ipvs) { return 0; }
  1416. #endif
  1417. #ifdef CONFIG_SYSCTL
  1418. /* Enqueue delayed work for expiring no dest connections
  1419. * Only run when sysctl_expire_nodest=1
  1420. */
  1421. static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs)
  1422. {
  1423. if (sysctl_expire_nodest_conn(ipvs))
  1424. queue_delayed_work(system_long_wq,
  1425. &ipvs->expire_nodest_conn_work, 1);
  1426. }
  1427. void ip_vs_expire_nodest_conn_flush(struct netns_ipvs *ipvs);
  1428. #else
  1429. static inline void ip_vs_enqueue_expire_nodest_conns(struct netns_ipvs *ipvs) {}
  1430. #endif
  1431. #define IP_VS_DFWD_METHOD(dest) (atomic_read(&(dest)->conn_flags) & \
  1432. IP_VS_CONN_F_FWD_MASK)
  1433. /* ip_vs_fwd_tag returns the forwarding tag of the connection */
  1434. #define IP_VS_FWD_METHOD(cp) (cp->flags & IP_VS_CONN_F_FWD_MASK)
  1435. static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)
  1436. {
  1437. char fwd;
  1438. switch (IP_VS_FWD_METHOD(cp)) {
  1439. case IP_VS_CONN_F_MASQ:
  1440. fwd = 'M'; break;
  1441. case IP_VS_CONN_F_LOCALNODE:
  1442. fwd = 'L'; break;
  1443. case IP_VS_CONN_F_TUNNEL:
  1444. fwd = 'T'; break;
  1445. case IP_VS_CONN_F_DROUTE:
  1446. fwd = 'R'; break;
  1447. case IP_VS_CONN_F_BYPASS:
  1448. fwd = 'B'; break;
  1449. default:
  1450. fwd = '?'; break;
  1451. }
  1452. return fwd;
  1453. }
  1454. void ip_vs_nat_icmp(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1455. struct ip_vs_conn *cp, int dir);
  1456. #ifdef CONFIG_IP_VS_IPV6
  1457. void ip_vs_nat_icmp_v6(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1458. struct ip_vs_conn *cp, int dir);
  1459. #endif
  1460. __sum16 ip_vs_checksum_complete(struct sk_buff *skb, int offset);
  1461. static inline __wsum ip_vs_check_diff4(__be32 old, __be32 new, __wsum oldsum)
  1462. {
  1463. __be32 diff[2] = { ~old, new };
  1464. return csum_partial(diff, sizeof(diff), oldsum);
  1465. }
  1466. #ifdef CONFIG_IP_VS_IPV6
  1467. static inline __wsum ip_vs_check_diff16(const __be32 *old, const __be32 *new,
  1468. __wsum oldsum)
  1469. {
  1470. __be32 diff[8] = { ~old[3], ~old[2], ~old[1], ~old[0],
  1471. new[3], new[2], new[1], new[0] };
  1472. return csum_partial(diff, sizeof(diff), oldsum);
  1473. }
  1474. #endif
  1475. static inline __wsum ip_vs_check_diff2(__be16 old, __be16 new, __wsum oldsum)
  1476. {
  1477. __be16 diff[2] = { ~old, new };
  1478. return csum_partial(diff, sizeof(diff), oldsum);
  1479. }
  1480. /* Forget current conntrack (unconfirmed) and attach notrack entry */
  1481. static inline void ip_vs_notrack(struct sk_buff *skb)
  1482. {
  1483. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1484. enum ip_conntrack_info ctinfo;
  1485. struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
  1486. if (ct) {
  1487. nf_conntrack_put(&ct->ct_general);
  1488. nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
  1489. }
  1490. #endif
  1491. }
  1492. #ifdef CONFIG_IP_VS_NFCT
  1493. /* Netfilter connection tracking
  1494. * (from ip_vs_nfct.c)
  1495. */
  1496. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1497. {
  1498. #ifdef CONFIG_SYSCTL
  1499. return ipvs->sysctl_conntrack;
  1500. #else
  1501. return 0;
  1502. #endif
  1503. }
  1504. void ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp,
  1505. int outin);
  1506. int ip_vs_confirm_conntrack(struct sk_buff *skb);
  1507. void ip_vs_nfct_expect_related(struct sk_buff *skb, struct nf_conn *ct,
  1508. struct ip_vs_conn *cp, u_int8_t proto,
  1509. const __be16 port, int from_rs);
  1510. void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp);
  1511. #else
  1512. static inline int ip_vs_conntrack_enabled(struct netns_ipvs *ipvs)
  1513. {
  1514. return 0;
  1515. }
  1516. static inline void ip_vs_update_conntrack(struct sk_buff *skb,
  1517. struct ip_vs_conn *cp, int outin)
  1518. {
  1519. }
  1520. static inline int ip_vs_confirm_conntrack(struct sk_buff *skb)
  1521. {
  1522. return NF_ACCEPT;
  1523. }
  1524. static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
  1525. {
  1526. }
  1527. #endif /* CONFIG_IP_VS_NFCT */
  1528. /* Using old conntrack that can not be redirected to another real server? */
  1529. static inline bool ip_vs_conn_uses_old_conntrack(struct ip_vs_conn *cp,
  1530. struct sk_buff *skb)
  1531. {
  1532. #ifdef CONFIG_IP_VS_NFCT
  1533. enum ip_conntrack_info ctinfo;
  1534. struct nf_conn *ct;
  1535. ct = nf_ct_get(skb, &ctinfo);
  1536. if (ct && nf_ct_is_confirmed(ct))
  1537. return true;
  1538. #endif
  1539. return false;
  1540. }
  1541. static inline int ip_vs_register_conntrack(struct ip_vs_service *svc)
  1542. {
  1543. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  1544. int afmask = (svc->af == AF_INET6) ? 2 : 1;
  1545. int ret = 0;
  1546. if (!(svc->conntrack_afmask & afmask)) {
  1547. ret = nf_ct_netns_get(svc->ipvs->net, svc->af);
  1548. if (ret >= 0)
  1549. svc->conntrack_afmask |= afmask;
  1550. }
  1551. return ret;
  1552. #else
  1553. return 0;
  1554. #endif
  1555. }
  1556. static inline void ip_vs_unregister_conntrack(struct ip_vs_service *svc)
  1557. {
  1558. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  1559. int afmask = (svc->af == AF_INET6) ? 2 : 1;
  1560. if (svc->conntrack_afmask & afmask) {
  1561. nf_ct_netns_put(svc->ipvs->net, svc->af);
  1562. svc->conntrack_afmask &= ~afmask;
  1563. }
  1564. #endif
  1565. }
  1566. int ip_vs_register_hooks(struct netns_ipvs *ipvs, unsigned int af);
  1567. void ip_vs_unregister_hooks(struct netns_ipvs *ipvs, unsigned int af);
  1568. static inline int
  1569. ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
  1570. {
  1571. /* We think the overhead of processing active connections is 256
  1572. * times higher than that of inactive connections in average. (This
  1573. * 256 times might not be accurate, we will change it later) We
  1574. * use the following formula to estimate the overhead now:
  1575. * dest->activeconns*256 + dest->inactconns
  1576. */
  1577. return (atomic_read(&dest->activeconns) << 8) +
  1578. atomic_read(&dest->inactconns);
  1579. }
  1580. #ifdef CONFIG_IP_VS_PROTO_TCP
  1581. INDIRECT_CALLABLE_DECLARE(int
  1582. tcp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1583. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  1584. #endif
  1585. #ifdef CONFIG_IP_VS_PROTO_UDP
  1586. INDIRECT_CALLABLE_DECLARE(int
  1587. udp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
  1588. struct ip_vs_conn *cp, struct ip_vs_iphdr *iph));
  1589. #endif
  1590. #endif /* _NET_IP_VS_H */