ip_vs_sync.c 23 KB

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