ip_vs_sync.c 25 KB

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