ip_vs_sync.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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;
  257. s = (struct ip_vs_sync_conn *)p;
  258. flags = ntohs(s->flags);
  259. if (!(flags & IP_VS_CONN_F_TEMPLATE))
  260. cp = ip_vs_conn_in_get(s->protocol,
  261. s->caddr, s->cport,
  262. s->vaddr, s->vport);
  263. else
  264. cp = ip_vs_ct_in_get(s->protocol,
  265. s->caddr, s->cport,
  266. s->vaddr, s->vport);
  267. if (!cp) {
  268. /*
  269. * Find the appropriate destination for the connection.
  270. * If it is not found the connection will remain unbound
  271. * but still handled.
  272. */
  273. dest = ip_vs_find_dest(s->daddr, s->dport,
  274. s->vaddr, s->vport,
  275. s->protocol);
  276. cp = ip_vs_conn_new(s->protocol,
  277. s->caddr, s->cport,
  278. s->vaddr, s->vport,
  279. s->daddr, s->dport,
  280. flags, dest);
  281. if (dest)
  282. atomic_dec(&dest->refcnt);
  283. if (!cp) {
  284. IP_VS_ERR("ip_vs_conn_new failed\n");
  285. return;
  286. }
  287. cp->state = ntohs(s->state);
  288. } else if (!cp->dest) {
  289. dest = ip_vs_try_bind_dest(cp);
  290. if (!dest) {
  291. /* it is an unbound entry created by
  292. * synchronization */
  293. cp->flags = flags | IP_VS_CONN_F_HASHED;
  294. } else
  295. atomic_dec(&dest->refcnt);
  296. } /* Note that we don't touch its state and flags
  297. if it is a normal entry. */
  298. if (flags & IP_VS_CONN_F_SEQ_MASK) {
  299. opt = (struct ip_vs_sync_conn_options *)&s[1];
  300. memcpy(&cp->in_seq, opt, sizeof(*opt));
  301. p += FULL_CONN_SIZE;
  302. } else
  303. p += SIMPLE_CONN_SIZE;
  304. atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
  305. cp->state = ntohs(s->state);
  306. pp = ip_vs_proto_get(s->protocol);
  307. cp->timeout = pp->timeout_table[cp->state];
  308. ip_vs_conn_put(cp);
  309. if (p > buffer+buflen) {
  310. IP_VS_ERR("bogus message\n");
  311. return;
  312. }
  313. }
  314. }
  315. /*
  316. * Setup loopback of outgoing multicasts on a sending socket
  317. */
  318. static void set_mcast_loop(struct sock *sk, u_char loop)
  319. {
  320. struct inet_sock *inet = inet_sk(sk);
  321. /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
  322. lock_sock(sk);
  323. inet->mc_loop = loop ? 1 : 0;
  324. release_sock(sk);
  325. }
  326. /*
  327. * Specify TTL for outgoing multicasts on a sending socket
  328. */
  329. static void set_mcast_ttl(struct sock *sk, u_char ttl)
  330. {
  331. struct inet_sock *inet = inet_sk(sk);
  332. /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
  333. lock_sock(sk);
  334. inet->mc_ttl = ttl;
  335. release_sock(sk);
  336. }
  337. /*
  338. * Specifiy default interface for outgoing multicasts
  339. */
  340. static int set_mcast_if(struct sock *sk, char *ifname)
  341. {
  342. struct net_device *dev;
  343. struct inet_sock *inet = inet_sk(sk);
  344. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  345. return -ENODEV;
  346. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  347. return -EINVAL;
  348. lock_sock(sk);
  349. inet->mc_index = dev->ifindex;
  350. /* inet->mc_addr = 0; */
  351. release_sock(sk);
  352. return 0;
  353. }
  354. /*
  355. * Set the maximum length of sync message according to the
  356. * specified interface's MTU.
  357. */
  358. static int set_sync_mesg_maxlen(int sync_state)
  359. {
  360. struct net_device *dev;
  361. int num;
  362. if (sync_state == IP_VS_STATE_MASTER) {
  363. if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
  364. return -ENODEV;
  365. num = (dev->mtu - sizeof(struct iphdr) -
  366. sizeof(struct udphdr) -
  367. SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
  368. sync_send_mesg_maxlen =
  369. SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
  370. IP_VS_DBG(7, "setting the maximum length of sync sending "
  371. "message %d.\n", sync_send_mesg_maxlen);
  372. } else if (sync_state == IP_VS_STATE_BACKUP) {
  373. if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
  374. return -ENODEV;
  375. sync_recv_mesg_maxlen = dev->mtu -
  376. sizeof(struct iphdr) - sizeof(struct udphdr);
  377. IP_VS_DBG(7, "setting the maximum length of sync receiving "
  378. "message %d.\n", sync_recv_mesg_maxlen);
  379. }
  380. return 0;
  381. }
  382. /*
  383. * Join a multicast group.
  384. * the group is specified by a class D multicast address 224.0.0.0/8
  385. * in the in_addr structure passed in as a parameter.
  386. */
  387. static int
  388. join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
  389. {
  390. struct ip_mreqn mreq;
  391. struct net_device *dev;
  392. int ret;
  393. memset(&mreq, 0, sizeof(mreq));
  394. memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
  395. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  396. return -ENODEV;
  397. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  398. return -EINVAL;
  399. mreq.imr_ifindex = dev->ifindex;
  400. lock_sock(sk);
  401. ret = ip_mc_join_group(sk, &mreq);
  402. release_sock(sk);
  403. return ret;
  404. }
  405. static int bind_mcastif_addr(struct socket *sock, char *ifname)
  406. {
  407. struct net_device *dev;
  408. __be32 addr;
  409. struct sockaddr_in sin;
  410. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  411. return -ENODEV;
  412. addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  413. if (!addr)
  414. IP_VS_ERR("You probably need to specify IP address on "
  415. "multicast interface.\n");
  416. IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
  417. ifname, NIPQUAD(addr));
  418. /* Now bind the socket with the address of multicast interface */
  419. sin.sin_family = AF_INET;
  420. sin.sin_addr.s_addr = addr;
  421. sin.sin_port = 0;
  422. return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
  423. }
  424. /*
  425. * Set up sending multicast socket over UDP
  426. */
  427. static struct socket * make_send_sock(void)
  428. {
  429. struct socket *sock;
  430. /* First create a socket */
  431. if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
  432. IP_VS_ERR("Error during creation of socket; terminating\n");
  433. return NULL;
  434. }
  435. if (set_mcast_if(sock->sk, ip_vs_master_mcast_ifn) < 0) {
  436. IP_VS_ERR("Error setting outbound mcast interface\n");
  437. goto error;
  438. }
  439. set_mcast_loop(sock->sk, 0);
  440. set_mcast_ttl(sock->sk, 1);
  441. if (bind_mcastif_addr(sock, ip_vs_master_mcast_ifn) < 0) {
  442. IP_VS_ERR("Error binding address of the mcast interface\n");
  443. goto error;
  444. }
  445. if (sock->ops->connect(sock,
  446. (struct sockaddr*)&mcast_addr,
  447. sizeof(struct sockaddr), 0) < 0) {
  448. IP_VS_ERR("Error connecting to the multicast addr\n");
  449. goto error;
  450. }
  451. return sock;
  452. error:
  453. sock_release(sock);
  454. return NULL;
  455. }
  456. /*
  457. * Set up receiving multicast socket over UDP
  458. */
  459. static struct socket * make_receive_sock(void)
  460. {
  461. struct socket *sock;
  462. /* First create a socket */
  463. if (sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock) < 0) {
  464. IP_VS_ERR("Error during creation of socket; terminating\n");
  465. return NULL;
  466. }
  467. /* it is equivalent to the REUSEADDR option in user-space */
  468. sock->sk->sk_reuse = 1;
  469. if (sock->ops->bind(sock,
  470. (struct sockaddr*)&mcast_addr,
  471. sizeof(struct sockaddr)) < 0) {
  472. IP_VS_ERR("Error binding to the multicast addr\n");
  473. goto error;
  474. }
  475. /* join the multicast group */
  476. if (join_mcast_group(sock->sk,
  477. (struct in_addr*)&mcast_addr.sin_addr,
  478. ip_vs_backup_mcast_ifn) < 0) {
  479. IP_VS_ERR("Error joining to the multicast group\n");
  480. goto error;
  481. }
  482. return sock;
  483. error:
  484. sock_release(sock);
  485. return NULL;
  486. }
  487. static int
  488. ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
  489. {
  490. struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
  491. struct kvec iov;
  492. int len;
  493. EnterFunction(7);
  494. iov.iov_base = (void *)buffer;
  495. iov.iov_len = length;
  496. len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
  497. LeaveFunction(7);
  498. return len;
  499. }
  500. static void
  501. ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
  502. {
  503. int msize;
  504. msize = msg->size;
  505. /* Put size in network byte order */
  506. msg->size = htons(msg->size);
  507. if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
  508. IP_VS_ERR("ip_vs_send_async error\n");
  509. }
  510. static int
  511. ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
  512. {
  513. struct msghdr msg = {NULL,};
  514. struct kvec iov;
  515. int len;
  516. EnterFunction(7);
  517. /* Receive a packet */
  518. iov.iov_base = buffer;
  519. iov.iov_len = (size_t)buflen;
  520. len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
  521. if (len < 0)
  522. return -1;
  523. LeaveFunction(7);
  524. return len;
  525. }
  526. static DECLARE_WAIT_QUEUE_HEAD(sync_wait);
  527. static pid_t sync_master_pid = 0;
  528. static pid_t sync_backup_pid = 0;
  529. static DECLARE_WAIT_QUEUE_HEAD(stop_sync_wait);
  530. static int stop_master_sync = 0;
  531. static int stop_backup_sync = 0;
  532. static void sync_master_loop(void)
  533. {
  534. struct socket *sock;
  535. struct ip_vs_sync_buff *sb;
  536. /* create the sending multicast socket */
  537. sock = make_send_sock();
  538. if (!sock)
  539. return;
  540. IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
  541. "syncid = %d\n",
  542. ip_vs_master_mcast_ifn, ip_vs_master_syncid);
  543. for (;;) {
  544. while ((sb=sb_dequeue())) {
  545. ip_vs_send_sync_msg(sock, sb->mesg);
  546. ip_vs_sync_buff_release(sb);
  547. }
  548. /* check if entries stay in curr_sb for 2 seconds */
  549. if ((sb = get_curr_sync_buff(2*HZ))) {
  550. ip_vs_send_sync_msg(sock, sb->mesg);
  551. ip_vs_sync_buff_release(sb);
  552. }
  553. if (stop_master_sync)
  554. break;
  555. msleep_interruptible(1000);
  556. }
  557. /* clean up the sync_buff queue */
  558. while ((sb=sb_dequeue())) {
  559. ip_vs_sync_buff_release(sb);
  560. }
  561. /* clean up the current sync_buff */
  562. if ((sb = get_curr_sync_buff(0))) {
  563. ip_vs_sync_buff_release(sb);
  564. }
  565. /* release the sending multicast socket */
  566. sock_release(sock);
  567. }
  568. static void sync_backup_loop(void)
  569. {
  570. struct socket *sock;
  571. char *buf;
  572. int len;
  573. if (!(buf = kmalloc(sync_recv_mesg_maxlen, GFP_ATOMIC))) {
  574. IP_VS_ERR("sync_backup_loop: kmalloc error\n");
  575. return;
  576. }
  577. /* create the receiving multicast socket */
  578. sock = make_receive_sock();
  579. if (!sock)
  580. goto out;
  581. IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
  582. "syncid = %d\n",
  583. ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
  584. for (;;) {
  585. /* do you have data now? */
  586. while (!skb_queue_empty(&(sock->sk->sk_receive_queue))) {
  587. if ((len =
  588. ip_vs_receive(sock, buf,
  589. sync_recv_mesg_maxlen)) <= 0) {
  590. IP_VS_ERR("receiving message error\n");
  591. break;
  592. }
  593. /* disable bottom half, because it accessed the data
  594. shared by softirq while getting/creating conns */
  595. local_bh_disable();
  596. ip_vs_process_message(buf, len);
  597. local_bh_enable();
  598. }
  599. if (stop_backup_sync)
  600. break;
  601. msleep_interruptible(1000);
  602. }
  603. /* release the sending multicast socket */
  604. sock_release(sock);
  605. out:
  606. kfree(buf);
  607. }
  608. static void set_sync_pid(int sync_state, pid_t sync_pid)
  609. {
  610. if (sync_state == IP_VS_STATE_MASTER)
  611. sync_master_pid = sync_pid;
  612. else if (sync_state == IP_VS_STATE_BACKUP)
  613. sync_backup_pid = sync_pid;
  614. }
  615. static void set_stop_sync(int sync_state, int set)
  616. {
  617. if (sync_state == IP_VS_STATE_MASTER)
  618. stop_master_sync = set;
  619. else if (sync_state == IP_VS_STATE_BACKUP)
  620. stop_backup_sync = set;
  621. else {
  622. stop_master_sync = set;
  623. stop_backup_sync = set;
  624. }
  625. }
  626. static int sync_thread(void *startup)
  627. {
  628. DECLARE_WAITQUEUE(wait, current);
  629. mm_segment_t oldmm;
  630. int state;
  631. const char *name;
  632. struct ip_vs_sync_thread_data *tinfo = startup;
  633. /* increase the module use count */
  634. ip_vs_use_count_inc();
  635. if (ip_vs_sync_state & IP_VS_STATE_MASTER && !sync_master_pid) {
  636. state = IP_VS_STATE_MASTER;
  637. name = "ipvs_syncmaster";
  638. } else if (ip_vs_sync_state & IP_VS_STATE_BACKUP && !sync_backup_pid) {
  639. state = IP_VS_STATE_BACKUP;
  640. name = "ipvs_syncbackup";
  641. } else {
  642. IP_VS_BUG();
  643. ip_vs_use_count_dec();
  644. return -EINVAL;
  645. }
  646. daemonize(name);
  647. oldmm = get_fs();
  648. set_fs(KERNEL_DS);
  649. /* Block all signals */
  650. spin_lock_irq(&current->sighand->siglock);
  651. siginitsetinv(&current->blocked, 0);
  652. recalc_sigpending();
  653. spin_unlock_irq(&current->sighand->siglock);
  654. /* set the maximum length of sync message */
  655. set_sync_mesg_maxlen(state);
  656. /* set up multicast address */
  657. mcast_addr.sin_family = AF_INET;
  658. mcast_addr.sin_port = htons(IP_VS_SYNC_PORT);
  659. mcast_addr.sin_addr.s_addr = htonl(IP_VS_SYNC_GROUP);
  660. add_wait_queue(&sync_wait, &wait);
  661. set_sync_pid(state, task_pid_nr(current));
  662. complete(tinfo->startup);
  663. /*
  664. * once we call the completion queue above, we should
  665. * null out that reference, since its allocated on the
  666. * stack of the creating kernel thread
  667. */
  668. tinfo->startup = NULL;
  669. /* processing master/backup loop here */
  670. if (state == IP_VS_STATE_MASTER)
  671. sync_master_loop();
  672. else if (state == IP_VS_STATE_BACKUP)
  673. sync_backup_loop();
  674. else IP_VS_BUG();
  675. remove_wait_queue(&sync_wait, &wait);
  676. /* thread exits */
  677. /*
  678. * If we weren't explicitly stopped, then we
  679. * exited in error, and should undo our state
  680. */
  681. if ((!stop_master_sync) && (!stop_backup_sync))
  682. ip_vs_sync_state -= tinfo->state;
  683. set_sync_pid(state, 0);
  684. IP_VS_INFO("sync thread stopped!\n");
  685. set_fs(oldmm);
  686. /* decrease the module use count */
  687. ip_vs_use_count_dec();
  688. set_stop_sync(state, 0);
  689. wake_up(&stop_sync_wait);
  690. /*
  691. * we need to free the structure that was allocated
  692. * for us in start_sync_thread
  693. */
  694. kfree(tinfo);
  695. return 0;
  696. }
  697. static int fork_sync_thread(void *startup)
  698. {
  699. pid_t pid;
  700. /* fork the sync thread here, then the parent process of the
  701. sync thread is the init process after this thread exits. */
  702. repeat:
  703. if ((pid = kernel_thread(sync_thread, startup, 0)) < 0) {
  704. IP_VS_ERR("could not create sync_thread due to %d... "
  705. "retrying.\n", pid);
  706. msleep_interruptible(1000);
  707. goto repeat;
  708. }
  709. return 0;
  710. }
  711. int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
  712. {
  713. DECLARE_COMPLETION_ONSTACK(startup);
  714. pid_t pid;
  715. struct ip_vs_sync_thread_data *tinfo;
  716. if ((state == IP_VS_STATE_MASTER && sync_master_pid) ||
  717. (state == IP_VS_STATE_BACKUP && sync_backup_pid))
  718. return -EEXIST;
  719. /*
  720. * Note that tinfo will be freed in sync_thread on exit
  721. */
  722. tinfo = kmalloc(sizeof(struct ip_vs_sync_thread_data), GFP_KERNEL);
  723. if (!tinfo)
  724. return -ENOMEM;
  725. IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
  726. IP_VS_DBG(7, "Each ip_vs_sync_conn entry need %Zd bytes\n",
  727. sizeof(struct ip_vs_sync_conn));
  728. ip_vs_sync_state |= state;
  729. if (state == IP_VS_STATE_MASTER) {
  730. strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
  731. sizeof(ip_vs_master_mcast_ifn));
  732. ip_vs_master_syncid = syncid;
  733. } else {
  734. strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
  735. sizeof(ip_vs_backup_mcast_ifn));
  736. ip_vs_backup_syncid = syncid;
  737. }
  738. tinfo->state = state;
  739. tinfo->startup = &startup;
  740. repeat:
  741. if ((pid = kernel_thread(fork_sync_thread, tinfo, 0)) < 0) {
  742. IP_VS_ERR("could not create fork_sync_thread due to %d... "
  743. "retrying.\n", pid);
  744. msleep_interruptible(1000);
  745. goto repeat;
  746. }
  747. wait_for_completion(&startup);
  748. return 0;
  749. }
  750. int stop_sync_thread(int state)
  751. {
  752. DECLARE_WAITQUEUE(wait, current);
  753. if ((state == IP_VS_STATE_MASTER && !sync_master_pid) ||
  754. (state == IP_VS_STATE_BACKUP && !sync_backup_pid))
  755. return -ESRCH;
  756. IP_VS_DBG(7, "%s: pid %d\n", __FUNCTION__, task_pid_nr(current));
  757. IP_VS_INFO("stopping sync thread %d ...\n",
  758. (state == IP_VS_STATE_MASTER) ?
  759. sync_master_pid : sync_backup_pid);
  760. __set_current_state(TASK_UNINTERRUPTIBLE);
  761. add_wait_queue(&stop_sync_wait, &wait);
  762. set_stop_sync(state, 1);
  763. ip_vs_sync_state -= state;
  764. wake_up(&sync_wait);
  765. schedule();
  766. __set_current_state(TASK_RUNNING);
  767. remove_wait_queue(&stop_sync_wait, &wait);
  768. /* Note: no need to reap the sync thread, because its parent
  769. process is the init process */
  770. if ((state == IP_VS_STATE_MASTER && stop_master_sync) ||
  771. (state == IP_VS_STATE_BACKUP && stop_backup_sync))
  772. IP_VS_BUG();
  773. return 0;
  774. }