ip_vs_sync.c 24 KB

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