ip_vs_sync.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the NetFilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Version: $Id: ip_vs_sync.c,v 1.13 2003/06/08 09:31:19 wensong Exp $
  9. *
  10. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  11. *
  12. * ip_vs_sync: sync connection info from master load balancer to backups
  13. * through multicast
  14. *
  15. * Changes:
  16. * Alexandre Cassen : Added master & backup support at a time.
  17. * Alexandre Cassen : Added SyncID support for incoming sync
  18. * messages filtering.
  19. * Justin Ossevoort : Fix endian problem on sync message size.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/net.h>
  25. #include <linux/completion.h>
  26. #include <linux/delay.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/in.h>
  29. #include <linux/igmp.h> /* for ip_mc_join_group */
  30. #include <linux/udp.h>
  31. #include <net/ip.h>
  32. #include <net/sock.h>
  33. #include <asm/uaccess.h> /* for get_fs and set_fs */
  34. #include <net/ip_vs.h>
  35. #define IP_VS_SYNC_GROUP 0xe0000051 /* multicast addr - 224.0.0.81 */
  36. #define IP_VS_SYNC_PORT 8848 /* multicast port */
  37. /*
  38. * IPVS sync connection entry
  39. */
  40. struct ip_vs_sync_conn {
  41. __u8 reserved;
  42. /* Protocol, addresses and port numbers */
  43. __u8 protocol; /* Which protocol (TCP/UDP) */
  44. __be16 cport;
  45. __be16 vport;
  46. __be16 dport;
  47. __be32 caddr; /* client address */
  48. __be32 vaddr; /* virtual address */
  49. __be32 daddr; /* destination address */
  50. /* Flags and state transition */
  51. __be16 flags; /* status flags */
  52. __be16 state; /* state info */
  53. /* The sequence options start here */
  54. };
  55. struct ip_vs_sync_conn_options {
  56. struct ip_vs_seq in_seq; /* incoming seq. struct */
  57. struct ip_vs_seq out_seq; /* outgoing seq. struct */
  58. };
  59. struct ip_vs_sync_thread_data {
  60. struct completion *startup;
  61. int state;
  62. };
  63. #define IP_VS_SYNC_CONN_TIMEOUT (3*60*HZ)
  64. #define SIMPLE_CONN_SIZE (sizeof(struct ip_vs_sync_conn))
  65. #define FULL_CONN_SIZE \
  66. (sizeof(struct ip_vs_sync_conn) + sizeof(struct ip_vs_sync_conn_options))
  67. /*
  68. The master mulitcasts messages to the backup load balancers in the
  69. following format.
  70. 0 1 2 3
  71. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  72. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  73. | Count Conns | SyncID | Size |
  74. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  75. | |
  76. | IPVS Sync Connection (1) |
  77. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  78. | . |
  79. | . |
  80. | . |
  81. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  82. | |
  83. | IPVS Sync Connection (n) |
  84. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  85. */
  86. #define SYNC_MESG_HEADER_LEN 4
  87. struct ip_vs_sync_mesg {
  88. __u8 nr_conns;
  89. __u8 syncid;
  90. __u16 size;
  91. /* ip_vs_sync_conn entries start here */
  92. };
  93. /* the maximum length of sync (sending/receiving) message */
  94. static int sync_send_mesg_maxlen;
  95. static int sync_recv_mesg_maxlen;
  96. struct ip_vs_sync_buff {
  97. struct list_head list;
  98. unsigned long firstuse;
  99. /* pointers for the message data */
  100. struct ip_vs_sync_mesg *mesg;
  101. unsigned char *head;
  102. unsigned char *end;
  103. };
  104. /* the sync_buff list head and the lock */
  105. static LIST_HEAD(ip_vs_sync_queue);
  106. static DEFINE_SPINLOCK(ip_vs_sync_lock);
  107. /* current sync_buff for accepting new conn entries */
  108. static struct ip_vs_sync_buff *curr_sb = NULL;
  109. static DEFINE_SPINLOCK(curr_sb_lock);
  110. /* ipvs sync daemon state */
  111. volatile int ip_vs_sync_state = IP_VS_STATE_NONE;
  112. volatile int ip_vs_master_syncid = 0;
  113. volatile int ip_vs_backup_syncid = 0;
  114. /* multicast interface name */
  115. char ip_vs_master_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  116. char ip_vs_backup_mcast_ifn[IP_VS_IFNAME_MAXLEN];
  117. /* multicast addr */
  118. static struct sockaddr_in mcast_addr;
  119. static inline void sb_queue_tail(struct ip_vs_sync_buff *sb)
  120. {
  121. spin_lock(&ip_vs_sync_lock);
  122. list_add_tail(&sb->list, &ip_vs_sync_queue);
  123. spin_unlock(&ip_vs_sync_lock);
  124. }
  125. static inline struct ip_vs_sync_buff * sb_dequeue(void)
  126. {
  127. struct ip_vs_sync_buff *sb;
  128. spin_lock_bh(&ip_vs_sync_lock);
  129. if (list_empty(&ip_vs_sync_queue)) {
  130. sb = NULL;
  131. } else {
  132. sb = list_entry(ip_vs_sync_queue.next,
  133. struct ip_vs_sync_buff,
  134. list);
  135. list_del(&sb->list);
  136. }
  137. spin_unlock_bh(&ip_vs_sync_lock);
  138. return sb;
  139. }
  140. static inline struct ip_vs_sync_buff * ip_vs_sync_buff_create(void)
  141. {
  142. struct ip_vs_sync_buff *sb;
  143. if (!(sb=kmalloc(sizeof(struct ip_vs_sync_buff), GFP_ATOMIC)))
  144. return NULL;
  145. if (!(sb->mesg=kmalloc(sync_send_mesg_maxlen, GFP_ATOMIC))) {
  146. kfree(sb);
  147. return NULL;
  148. }
  149. sb->mesg->nr_conns = 0;
  150. sb->mesg->syncid = ip_vs_master_syncid;
  151. sb->mesg->size = 4;
  152. sb->head = (unsigned char *)sb->mesg + 4;
  153. sb->end = (unsigned char *)sb->mesg + sync_send_mesg_maxlen;
  154. sb->firstuse = jiffies;
  155. return sb;
  156. }
  157. static inline void ip_vs_sync_buff_release(struct ip_vs_sync_buff *sb)
  158. {
  159. kfree(sb->mesg);
  160. kfree(sb);
  161. }
  162. /*
  163. * Get the current sync buffer if it has been created for more
  164. * than the specified time or the specified time is zero.
  165. */
  166. static inline struct ip_vs_sync_buff *
  167. get_curr_sync_buff(unsigned long time)
  168. {
  169. struct ip_vs_sync_buff *sb;
  170. spin_lock_bh(&curr_sb_lock);
  171. if (curr_sb && (time == 0 ||
  172. time_before(jiffies - curr_sb->firstuse, time))) {
  173. sb = curr_sb;
  174. curr_sb = NULL;
  175. } else
  176. sb = NULL;
  177. spin_unlock_bh(&curr_sb_lock);
  178. return sb;
  179. }
  180. /*
  181. * Add an ip_vs_conn information into the current sync_buff.
  182. * Called by ip_vs_in.
  183. */
  184. void ip_vs_sync_conn(struct ip_vs_conn *cp)
  185. {
  186. struct ip_vs_sync_mesg *m;
  187. struct ip_vs_sync_conn *s;
  188. int len;
  189. spin_lock(&curr_sb_lock);
  190. if (!curr_sb) {
  191. if (!(curr_sb=ip_vs_sync_buff_create())) {
  192. spin_unlock(&curr_sb_lock);
  193. IP_VS_ERR("ip_vs_sync_buff_create failed.\n");
  194. return;
  195. }
  196. }
  197. len = (cp->flags & IP_VS_CONN_F_SEQ_MASK) ? FULL_CONN_SIZE :
  198. SIMPLE_CONN_SIZE;
  199. m = curr_sb->mesg;
  200. s = (struct ip_vs_sync_conn *)curr_sb->head;
  201. /* copy members */
  202. s->protocol = cp->protocol;
  203. s->cport = cp->cport;
  204. s->vport = cp->vport;
  205. s->dport = cp->dport;
  206. s->caddr = cp->caddr;
  207. s->vaddr = cp->vaddr;
  208. s->daddr = cp->daddr;
  209. s->flags = htons(cp->flags & ~IP_VS_CONN_F_HASHED);
  210. s->state = htons(cp->state);
  211. if (cp->flags & IP_VS_CONN_F_SEQ_MASK) {
  212. struct ip_vs_sync_conn_options *opt =
  213. (struct ip_vs_sync_conn_options *)&s[1];
  214. memcpy(opt, &cp->in_seq, sizeof(*opt));
  215. }
  216. m->nr_conns++;
  217. m->size += len;
  218. curr_sb->head += len;
  219. /* check if there is a space for next one */
  220. if (curr_sb->head+FULL_CONN_SIZE > curr_sb->end) {
  221. sb_queue_tail(curr_sb);
  222. curr_sb = NULL;
  223. }
  224. spin_unlock(&curr_sb_lock);
  225. /* synchronize its controller if it has */
  226. if (cp->control)
  227. ip_vs_sync_conn(cp->control);
  228. }
  229. /*
  230. * Process received multicast message and create the corresponding
  231. * ip_vs_conn entries.
  232. */
  233. static void ip_vs_process_message(const char *buffer, const size_t buflen)
  234. {
  235. struct ip_vs_sync_mesg *m = (struct ip_vs_sync_mesg *)buffer;
  236. struct ip_vs_sync_conn *s;
  237. struct ip_vs_sync_conn_options *opt;
  238. struct ip_vs_conn *cp;
  239. char *p;
  240. int i;
  241. /* Convert size back to host byte order */
  242. m->size = ntohs(m->size);
  243. if (buflen != m->size) {
  244. IP_VS_ERR("bogus message\n");
  245. return;
  246. }
  247. /* SyncID sanity check */
  248. if (ip_vs_backup_syncid != 0 && m->syncid != ip_vs_backup_syncid) {
  249. IP_VS_DBG(7, "Ignoring incoming msg with syncid = %d\n",
  250. m->syncid);
  251. return;
  252. }
  253. p = (char *)buffer + sizeof(struct ip_vs_sync_mesg);
  254. for (i=0; i<m->nr_conns; i++) {
  255. unsigned flags;
  256. s = (struct ip_vs_sync_conn *)p;
  257. flags = ntohs(s->flags);
  258. if (!(flags & IP_VS_CONN_F_TEMPLATE))
  259. cp = ip_vs_conn_in_get(s->protocol,
  260. s->caddr, s->cport,
  261. s->vaddr, s->vport);
  262. else
  263. cp = ip_vs_ct_in_get(s->protocol,
  264. s->caddr, s->cport,
  265. s->vaddr, s->vport);
  266. if (!cp) {
  267. cp = ip_vs_conn_new(s->protocol,
  268. s->caddr, s->cport,
  269. s->vaddr, s->vport,
  270. s->daddr, s->dport,
  271. flags, NULL);
  272. if (!cp) {
  273. IP_VS_ERR("ip_vs_conn_new failed\n");
  274. return;
  275. }
  276. cp->state = ntohs(s->state);
  277. } else if (!cp->dest) {
  278. /* it is an entry created by the synchronization */
  279. cp->state = ntohs(s->state);
  280. cp->flags = flags | IP_VS_CONN_F_HASHED;
  281. } /* Note that we don't touch its state and flags
  282. if it is a normal entry. */
  283. if (flags & IP_VS_CONN_F_SEQ_MASK) {
  284. opt = (struct ip_vs_sync_conn_options *)&s[1];
  285. memcpy(&cp->in_seq, opt, sizeof(*opt));
  286. p += FULL_CONN_SIZE;
  287. } else
  288. p += SIMPLE_CONN_SIZE;
  289. atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
  290. cp->timeout = IP_VS_SYNC_CONN_TIMEOUT;
  291. ip_vs_conn_put(cp);
  292. if (p > buffer+buflen) {
  293. IP_VS_ERR("bogus message\n");
  294. return;
  295. }
  296. }
  297. }
  298. /*
  299. * Setup loopback of outgoing multicasts on a sending socket
  300. */
  301. static void set_mcast_loop(struct sock *sk, u_char loop)
  302. {
  303. struct inet_sock *inet = inet_sk(sk);
  304. /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
  305. lock_sock(sk);
  306. inet->mc_loop = loop ? 1 : 0;
  307. release_sock(sk);
  308. }
  309. /*
  310. * Specify TTL for outgoing multicasts on a sending socket
  311. */
  312. static void set_mcast_ttl(struct sock *sk, u_char ttl)
  313. {
  314. struct inet_sock *inet = inet_sk(sk);
  315. /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
  316. lock_sock(sk);
  317. inet->mc_ttl = ttl;
  318. release_sock(sk);
  319. }
  320. /*
  321. * Specifiy default interface for outgoing multicasts
  322. */
  323. static int set_mcast_if(struct sock *sk, char *ifname)
  324. {
  325. struct net_device *dev;
  326. struct inet_sock *inet = inet_sk(sk);
  327. if ((dev = __dev_get_by_name(ifname)) == NULL)
  328. return -ENODEV;
  329. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  330. return -EINVAL;
  331. lock_sock(sk);
  332. inet->mc_index = dev->ifindex;
  333. /* inet->mc_addr = 0; */
  334. release_sock(sk);
  335. return 0;
  336. }
  337. /*
  338. * Set the maximum length of sync message according to the
  339. * specified interface's MTU.
  340. */
  341. static int set_sync_mesg_maxlen(int sync_state)
  342. {
  343. struct net_device *dev;
  344. int num;
  345. if (sync_state == IP_VS_STATE_MASTER) {
  346. if ((dev = __dev_get_by_name(ip_vs_master_mcast_ifn)) == NULL)
  347. return -ENODEV;
  348. num = (dev->mtu - sizeof(struct iphdr) -
  349. sizeof(struct udphdr) -
  350. SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
  351. sync_send_mesg_maxlen =
  352. SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
  353. IP_VS_DBG(7, "setting the maximum length of sync sending "
  354. "message %d.\n", sync_send_mesg_maxlen);
  355. } else if (sync_state == IP_VS_STATE_BACKUP) {
  356. if ((dev = __dev_get_by_name(ip_vs_backup_mcast_ifn)) == NULL)
  357. return -ENODEV;
  358. sync_recv_mesg_maxlen = dev->mtu -
  359. sizeof(struct iphdr) - sizeof(struct udphdr);
  360. IP_VS_DBG(7, "setting the maximum length of sync receiving "
  361. "message %d.\n", sync_recv_mesg_maxlen);
  362. }
  363. return 0;
  364. }
  365. /*
  366. * Join a multicast group.
  367. * the group is specified by a class D multicast address 224.0.0.0/8
  368. * in the in_addr structure passed in as a parameter.
  369. */
  370. static int
  371. join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
  372. {
  373. struct ip_mreqn mreq;
  374. struct net_device *dev;
  375. int ret;
  376. memset(&mreq, 0, sizeof(mreq));
  377. memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
  378. if ((dev = __dev_get_by_name(ifname)) == NULL)
  379. return -ENODEV;
  380. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  381. return -EINVAL;
  382. mreq.imr_ifindex = dev->ifindex;
  383. lock_sock(sk);
  384. ret = ip_mc_join_group(sk, &mreq);
  385. release_sock(sk);
  386. return ret;
  387. }
  388. static int bind_mcastif_addr(struct socket *sock, char *ifname)
  389. {
  390. struct net_device *dev;
  391. __be32 addr;
  392. struct sockaddr_in sin;
  393. if ((dev = __dev_get_by_name(ifname)) == NULL)
  394. return -ENODEV;
  395. addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  396. if (!addr)
  397. IP_VS_ERR("You probably need to specify IP address on "
  398. "multicast interface.\n");
  399. IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
  400. ifname, NIPQUAD(addr));
  401. /* Now bind the socket with the address of multicast interface */
  402. sin.sin_family = AF_INET;
  403. sin.sin_addr.s_addr = addr;
  404. sin.sin_port = 0;
  405. return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
  406. }
  407. /*
  408. * Set up sending multicast socket over UDP
  409. */
  410. static struct socket * make_send_sock(void)
  411. {
  412. struct socket *sock;
  413. /* First create a socket */
  414. if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
  415. IP_VS_ERR("Error during creation of socket; terminating\n");
  416. return NULL;
  417. }
  418. if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
  419. IP_VS_ERR("Error setting outbound mcast interface\n");
  420. goto error;
  421. }
  422. set_mcast_loop(sock->sk, 0);
  423. set_mcast_ttl(sock->sk, 1);
  424. if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
  425. IP_VS_ERR("Error binding address of the mcast interface\n");
  426. goto error;
  427. }
  428. if (sock->ops->connect(sock,
  429. (struct sockaddr*)&mcast_addr,
  430. sizeof(struct sockaddr), 0) < 0) {
  431. IP_VS_ERR("Error connecting to the multicast addr\n");
  432. goto error;
  433. }
  434. return sock;
  435. error:
  436. sock_release(sock);
  437. return NULL;
  438. }
  439. /*
  440. * Set up receiving multicast socket over UDP
  441. */
  442. static struct socket * make_receive_sock(void)
  443. {
  444. struct socket *sock;
  445. /* First create a socket */
  446. if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
  447. IP_VS_ERR("Error during creation of socket; terminating\n");
  448. return NULL;
  449. }
  450. /* it is equivalent to the REUSEADDR option in user-space */
  451. sock->sk->sk_reuse = 1;
  452. if (sock->ops->bind(sock,
  453. (struct sockaddr*)&mcast_addr,
  454. sizeof(struct sockaddr)) < 0) {
  455. IP_VS_ERR("Error binding to the multicast addr\n");
  456. goto error;
  457. }
  458. /* join the multicast group */
  459. if (join_mcast_group(sock->sk,
  460. (struct in_addr*)&mcast_addr.sin_addr,
  461. ip_vs_backup_mcast_ifn) < 0) {
  462. IP_VS_ERR("Error joining to the multicast group\n");
  463. goto error;
  464. }
  465. return sock;
  466. error:
  467. sock_release(sock);
  468. return NULL;
  469. }
  470. static int
  471. ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
  472. {
  473. struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
  474. struct kvec iov;
  475. int len;
  476. EnterFunction(7);
  477. iov.iov_base = (void *)buffer;
  478. iov.iov_len = length;
  479. len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
  480. LeaveFunction(7);
  481. return len;
  482. }
  483. static void
  484. ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
  485. {
  486. int msize;
  487. msize = msg->size;
  488. /* Put size in network byte order */
  489. msg->size = htons(msg->size);
  490. if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
  491. IP_VS_ERR("ip_vs_send_async error\n");
  492. }
  493. static int
  494. ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
  495. {
  496. struct msghdr msg = {NULL,};
  497. struct kvec iov;
  498. int len;
  499. EnterFunction(7);
  500. /* Receive a packet */
  501. iov.iov_base = buffer;
  502. iov.iov_len = (size_t)buflen;
  503. len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
  504. if (len < 0)
  505. return -1;
  506. LeaveFunction(7);
  507. return len;
  508. }
  509. static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
  510. static pid_t sync_master_pid = 0;
  511. static pid_t sync_backup_pid = 0;
  512. static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
  513. static int stop_master_sync = 0;
  514. static int stop_backup_sync = 0;
  515. static void sync_master_loop(void)
  516. {
  517. struct socket *sock;
  518. struct ip_vs_sync_buff *sb;
  519. /* create the sending multicast socket */
  520. sock = make_send_sock();
  521. if (!sock)
  522. return;
  523. IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
  524. "syncid = %d\n",
  525. ip_vs_master_mcast_ifn, ip_vs_master_syncid);
  526. for (;;) {
  527. while ((sb=sb_dequeue())) {
  528. ip_vs_send_sync_msg(sock, sb->mesg);
  529. ip_vs_sync_buff_release(sb);
  530. }
  531. /* check if entries stay in curr_sb for 2 seconds */
  532. if ((sb = get_curr_sync_buff(2*HZ))) {
  533. ip_vs_send_sync_msg(sock, sb->mesg);
  534. ip_vs_sync_buff_release(sb);
  535. }
  536. if (stop_master_sync)
  537. break;
  538. msleep_interruptible(1000);
  539. }
  540. /* clean up the sync_buff queue */
  541. while ((sb=sb_dequeue())) {
  542. ip_vs_sync_buff_release(sb);
  543. }
  544. /* clean up the current sync_buff */
  545. if ((sb = get_curr_sync_buff(0))) {
  546. ip_vs_sync_buff_release(sb);
  547. }
  548. /* release the sending multicast socket */
  549. sock_release(sock);
  550. }
  551. static void sync_backup_loop(void)
  552. {
  553. struct socket *sock;
  554. char *buf;
  555. int len;
  556. if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
  557. IP_VS_ERR("sync_backup_loop: kmalloc error\n");
  558. return;
  559. }
  560. /* create the receiving multicast socket */
  561. sock = make_receive_sock();
  562. if (!sock)
  563. goto out;
  564. IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
  565. "syncid = %d\n",
  566. ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
  567. for (;;) {
  568. /* do you have data now? */
  569. while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
  570. if ((len =
  571. ip_vs_receive(sock, buf,
  572. sync_recv_mesg_maxlen)) <= 0) {
  573. IP_VS_ERR("receiving message error\n");
  574. break;
  575. }
  576. /* disable bottom half, because it accessed the data
  577. shared by softirq while getting/creating conns */
  578. local_bh_disable();
  579. ip_vs_process_message(buf, len);
  580. local_bh_enable();
  581. }
  582. if (stop_backup_sync)
  583. break;
  584. msleep_interruptible(1000);
  585. }
  586. /* release the sending multicast socket */
  587. sock_release(sock);
  588. out:
  589. kfree(buf);
  590. }
  591. static void set_sync_pid(int sync_state, pid_t sync_pid)
  592. {
  593. if (sync_state == IP_VS_STATE_MASTER)
  594. sync_master_pid = sync_pid;
  595. else if (sync_state == IP_VS_STATE_BACKUP)
  596. sync_backup_pid = sync_pid;
  597. }
  598. static void set_stop_sync(int sync_state, int set)
  599. {
  600. if (sync_state == IP_VS_STATE_MASTER)
  601. stop_master_sync = set;
  602. else if (sync_state == IP_VS_STATE_BACKUP)
  603. stop_backup_sync = set;
  604. else {
  605. stop_master_sync = set;
  606. stop_backup_sync = set;
  607. }
  608. }
  609. static int sync_thread(void *startup)
  610. {
  611. DECLARE_WAITQUEUE(wait, current);
  612. mm_segment_t oldmm;
  613. int state;
  614. const char *name;
  615. struct ip_vs_sync_thread_data *tinfo = startup;
  616. /* increase the module use count */
  617. ip_vs_use_count_inc();
  618. if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
  619. state = IP_VS_STATE_MASTER;
  620. name = "ipvs_syncmaster";
  621. } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
  622. state = IP_VS_STATE_BACKUP;
  623. name = "ipvs_syncbackup";
  624. } else {
  625. IP_VS_BUG();
  626. ip_vs_use_count_dec();
  627. return -EINVAL;
  628. }
  629. daemonize(name);
  630. oldmm = get_fs();
  631. set_fs(KERNEL_DS);
  632. /* Block all signals */
  633. spin_lock_irq(&current->sighand->siglock);
  634. siginitsetinv(&current->blocked, 0);
  635. recalc_sigpending();
  636. spin_unlock_irq(&current->sighand->siglock);
  637. /* set the maximum length of sync message */
  638. set_sync_mesg_maxlen(state);
  639. /* set up multicast address */
  640. mcast_addr.sin_family = AF_INET;
  641. mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
  642. mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
  643. add_wait_queue(&sync_wait, &wait);
  644. set_sync_pid(state, current->pid);
  645. complete(tinfo->startup);
  646. /*
  647. * once we call the completion queue above, we should
  648. * null out that reference, since its allocated on the
  649. * stack of the creating kernel thread
  650. */
  651. tinfo->startup = NULL;
  652. /* processing master/backup loop here */
  653. if (state == IP_VS_STATE_MASTER)
  654. sync_master_loop();
  655. else if (state == IP_VS_STATE_BACKUP)
  656. sync_backup_loop();
  657. else IP_VS_BUG();
  658. remove_wait_queue(&sync_wait, &wait);
  659. /* thread exits */
  660. /*
  661. * If we weren't explicitly stopped, then we
  662. * exited in error, and should undo our state
  663. */
  664. if ((!stop_master_sync) && (!stop_backup_sync))
  665. ip_vs_sync_state -= tinfo->state;
  666. set_sync_pid(state, 0);
  667. IP_VS_INFO("sync thread stopped!\n");
  668. set_fs(oldmm);
  669. /* decrease the module use count */
  670. ip_vs_use_count_dec();
  671. set_stop_sync(state, 0);
  672. wake_up(&stop_sync_wait);
  673. /*
  674. * we need to free the structure that was allocated
  675. * for us in start_sync_thread
  676. */
  677. kfree(tinfo);
  678. return 0;
  679. }
  680. static int fork_sync_thread(void *startup)
  681. {
  682. pid_t pid;
  683. /* fork the sync thread here, then the parent process of the
  684. sync thread is the init process after this thread exits. */
  685. repeat:
  686. if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
  687. IP_VS_ERR("could not create sync_thread due to %d... "
  688. "retrying.\n", pid);
  689. msleep_interruptible(1000);
  690. goto repeat;
  691. }
  692. return 0;
  693. }
  694. int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
  695. {
  696. DECLARE_COMPLETION_ONSTACK(startup);
  697. pid_t pid;
  698. struct ip_vs_sync_thread_data *tinfo;
  699. if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
  700. (state == IP_VS_STATE_BACKUP && sync_backup_pid))
  701. return -EEXIST;
  702. /*
  703. * Note that tinfo will be freed in sync_thread on exit
  704. */
  705. tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
  706. if (!tinfo)
  707. return -ENOMEM;
  708. IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
  709. IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
  710. sizeof(struct ip_vs_sync_conn));
  711. ip_vs_sync_state |= state;
  712. if (state == IP_VS_STATE_MASTER) {
  713. strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
  714. sizeof(ip_vs_master_mcast_ifn));
  715. ip_vs_master_syncid = syncid;
  716. } else {
  717. strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
  718. sizeof(ip_vs_backup_mcast_ifn));
  719. ip_vs_backup_syncid = syncid;
  720. }
  721. tinfo->state = state;
  722. tinfo->startup = &startup;
  723. repeat:
  724. if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
  725. IP_VS_ERR("could not create fork_sync_thread due to %d... "
  726. "retrying.\n", pid);
  727. msleep_interruptible(1000);
  728. goto repeat;
  729. }
  730. wait_for_completion(&startup);
  731. return 0;
  732. }
  733. int stop_sync_thread(int state)
  734. {
  735. DECLARE_WAITQUEUE(wait, current);
  736. if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
  737. (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
  738. return -ESRCH;
  739. IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, current->pid);
  740. IP_VS_INFO("stopping sync thread %d ...\n",
  741. (state == IP_VS_STATE_MASTER) ?
  742. sync_master_pid : sync_backup_pid);
  743. __set_current_state(TASK_UNINTERRUPTIBLE);
  744. add_wait_queue(&stop_sync_wait, &wait);
  745. set_stop_sync(state, 1);
  746. ip_vs_sync_state -= state;
  747. wake_up(&sync_wait);
  748. schedule();
  749. __set_current_state(TASK_RUNNING);
  750. remove_wait_queue(&stop_sync_wait, &wait);
  751. /* Note: no need to reap the sync thread, because its parent
  752. process is the init process */
  753. if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
  754. (state == IP_VS_STATE_BACKUP && stop_backup_sync))
  755. IP_VS_BUG();
  756. return 0;
  757. }