ip_vs_sync.c 25 KB

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