ip_vs_sync.c 25 KB

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