ip_vs_sync.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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.ip;
  216. s->vaddr = cp->vaddr.ip;
  217. s->daddr = cp->daddr.ip;
  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(AF_INET, s->protocol,
  313. (union nf_inet_addr *)&s->caddr,
  314. s->cport,
  315. (union nf_inet_addr *)&s->vaddr,
  316. s->vport);
  317. else
  318. cp = ip_vs_ct_in_get(AF_INET, s->protocol,
  319. (union nf_inet_addr *)&s->caddr,
  320. s->cport,
  321. (union nf_inet_addr *)&s->vaddr,
  322. s->vport);
  323. if (!cp) {
  324. /*
  325. * Find the appropriate destination for the connection.
  326. * If it is not found the connection will remain unbound
  327. * but still handled.
  328. */
  329. dest = ip_vs_find_dest(AF_INET,
  330. (union nf_inet_addr *)&s->daddr,
  331. s->dport,
  332. (union nf_inet_addr *)&s->vaddr,
  333. s->vport,
  334. s->protocol);
  335. /* Set the approprite ativity flag */
  336. if (s->protocol == IPPROTO_TCP) {
  337. if (state != IP_VS_TCP_S_ESTABLISHED)
  338. flags |= IP_VS_CONN_F_INACTIVE;
  339. else
  340. flags &= ~IP_VS_CONN_F_INACTIVE;
  341. }
  342. cp = ip_vs_conn_new(AF_INET, s->protocol,
  343. (union nf_inet_addr *)&s->caddr,
  344. s->cport,
  345. (union nf_inet_addr *)&s->vaddr,
  346. s->vport,
  347. (union nf_inet_addr *)&s->daddr,
  348. s->dport,
  349. flags, dest);
  350. if (dest)
  351. atomic_dec(&dest->refcnt);
  352. if (!cp) {
  353. IP_VS_ERR("ip_vs_conn_new failed\n");
  354. return;
  355. }
  356. } else if (!cp->dest) {
  357. dest = ip_vs_try_bind_dest(cp);
  358. if (dest)
  359. atomic_dec(&dest->refcnt);
  360. } else if ((cp->dest) && (cp->protocol == IPPROTO_TCP) &&
  361. (cp->state != state)) {
  362. /* update active/inactive flag for the connection */
  363. dest = cp->dest;
  364. if (!(cp->flags & IP_VS_CONN_F_INACTIVE) &&
  365. (state != IP_VS_TCP_S_ESTABLISHED)) {
  366. atomic_dec(&dest->activeconns);
  367. atomic_inc(&dest->inactconns);
  368. cp->flags |= IP_VS_CONN_F_INACTIVE;
  369. } else if ((cp->flags & IP_VS_CONN_F_INACTIVE) &&
  370. (state == IP_VS_TCP_S_ESTABLISHED)) {
  371. atomic_inc(&dest->activeconns);
  372. atomic_dec(&dest->inactconns);
  373. cp->flags &= ~IP_VS_CONN_F_INACTIVE;
  374. }
  375. }
  376. if (opt)
  377. memcpy(&cp->in_seq, opt, sizeof(*opt));
  378. atomic_set(&cp->in_pkts, sysctl_ip_vs_sync_threshold[0]);
  379. cp->state = state;
  380. cp->old_state = cp->state;
  381. /*
  382. * We can not recover the right timeout for templates
  383. * in all cases, we can not find the right fwmark
  384. * virtual service. If needed, we can do it for
  385. * non-fwmark persistent services.
  386. */
  387. if (!(flags & IP_VS_CONN_F_TEMPLATE) && pp->timeout_table)
  388. cp->timeout = pp->timeout_table[state];
  389. else
  390. cp->timeout = (3*60*HZ);
  391. ip_vs_conn_put(cp);
  392. }
  393. }
  394. /*
  395. * Setup loopback of outgoing multicasts on a sending socket
  396. */
  397. static void set_mcast_loop(struct sock *sk, u_char loop)
  398. {
  399. struct inet_sock *inet = inet_sk(sk);
  400. /* setsockopt(sock, SOL_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); */
  401. lock_sock(sk);
  402. inet->mc_loop = loop ? 1 : 0;
  403. release_sock(sk);
  404. }
  405. /*
  406. * Specify TTL for outgoing multicasts on a sending socket
  407. */
  408. static void set_mcast_ttl(struct sock *sk, u_char ttl)
  409. {
  410. struct inet_sock *inet = inet_sk(sk);
  411. /* setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); */
  412. lock_sock(sk);
  413. inet->mc_ttl = ttl;
  414. release_sock(sk);
  415. }
  416. /*
  417. * Specifiy default interface for outgoing multicasts
  418. */
  419. static int set_mcast_if(struct sock *sk, char *ifname)
  420. {
  421. struct net_device *dev;
  422. struct inet_sock *inet = inet_sk(sk);
  423. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  424. return -ENODEV;
  425. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  426. return -EINVAL;
  427. lock_sock(sk);
  428. inet->mc_index = dev->ifindex;
  429. /* inet->mc_addr = 0; */
  430. release_sock(sk);
  431. return 0;
  432. }
  433. /*
  434. * Set the maximum length of sync message according to the
  435. * specified interface's MTU.
  436. */
  437. static int set_sync_mesg_maxlen(int sync_state)
  438. {
  439. struct net_device *dev;
  440. int num;
  441. if (sync_state == IP_VS_STATE_MASTER) {
  442. if ((dev = __dev_get_by_name(&init_net, ip_vs_master_mcast_ifn)) == NULL)
  443. return -ENODEV;
  444. num = (dev->mtu - sizeof(struct iphdr) -
  445. sizeof(struct udphdr) -
  446. SYNC_MESG_HEADER_LEN - 20) / SIMPLE_CONN_SIZE;
  447. sync_send_mesg_maxlen =
  448. SYNC_MESG_HEADER_LEN + SIMPLE_CONN_SIZE * num;
  449. IP_VS_DBG(7, "setting the maximum length of sync sending "
  450. "message %d.\n", sync_send_mesg_maxlen);
  451. } else if (sync_state == IP_VS_STATE_BACKUP) {
  452. if ((dev = __dev_get_by_name(&init_net, ip_vs_backup_mcast_ifn)) == NULL)
  453. return -ENODEV;
  454. sync_recv_mesg_maxlen = dev->mtu -
  455. sizeof(struct iphdr) - sizeof(struct udphdr);
  456. IP_VS_DBG(7, "setting the maximum length of sync receiving "
  457. "message %d.\n", sync_recv_mesg_maxlen);
  458. }
  459. return 0;
  460. }
  461. /*
  462. * Join a multicast group.
  463. * the group is specified by a class D multicast address 224.0.0.0/8
  464. * in the in_addr structure passed in as a parameter.
  465. */
  466. static int
  467. join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
  468. {
  469. struct ip_mreqn mreq;
  470. struct net_device *dev;
  471. int ret;
  472. memset(&mreq, 0, sizeof(mreq));
  473. memcpy(&mreq.imr_multiaddr, addr, sizeof(struct in_addr));
  474. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  475. return -ENODEV;
  476. if (sk->sk_bound_dev_if && dev->ifindex != sk->sk_bound_dev_if)
  477. return -EINVAL;
  478. mreq.imr_ifindex = dev->ifindex;
  479. lock_sock(sk);
  480. ret = ip_mc_join_group(sk, &mreq);
  481. release_sock(sk);
  482. return ret;
  483. }
  484. static int bind_mcastif_addr(struct socket *sock, char *ifname)
  485. {
  486. struct net_device *dev;
  487. __be32 addr;
  488. struct sockaddr_in sin;
  489. if ((dev = __dev_get_by_name(&init_net, ifname)) == NULL)
  490. return -ENODEV;
  491. addr = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  492. if (!addr)
  493. IP_VS_ERR("You probably need to specify IP address on "
  494. "multicast interface.\n");
  495. IP_VS_DBG(7, "binding socket with (%s) %u.%u.%u.%u\n",
  496. ifname, NIPQUAD(addr));
  497. /* Now bind the socket with the address of multicast interface */
  498. sin.sin_family = AF_INET;
  499. sin.sin_addr.s_addr = addr;
  500. sin.sin_port = 0;
  501. return sock->ops->bind(sock, (struct sockaddr*)&sin, sizeof(sin));
  502. }
  503. /*
  504. * Set up sending multicast socket over UDP
  505. */
  506. static struct socket * make_send_sock(void)
  507. {
  508. struct socket *sock;
  509. int result;
  510. /* First create a socket */
  511. result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
  512. if (result < 0) {
  513. IP_VS_ERR("Error during creation of socket; terminating\n");
  514. return ERR_PTR(result);
  515. }
  516. result = set_mcast_if(sock->sk, ip_vs_master_mcast_ifn);
  517. if (result < 0) {
  518. IP_VS_ERR("Error setting outbound mcast interface\n");
  519. goto error;
  520. }
  521. set_mcast_loop(sock->sk, 0);
  522. set_mcast_ttl(sock->sk, 1);
  523. result = bind_mcastif_addr(sock, ip_vs_master_mcast_ifn);
  524. if (result < 0) {
  525. IP_VS_ERR("Error binding address of the mcast interface\n");
  526. goto error;
  527. }
  528. result = sock->ops->connect(sock, (struct sockaddr *) &mcast_addr,
  529. sizeof(struct sockaddr), 0);
  530. if (result < 0) {
  531. IP_VS_ERR("Error connecting to the multicast addr\n");
  532. goto error;
  533. }
  534. return sock;
  535. error:
  536. sock_release(sock);
  537. return ERR_PTR(result);
  538. }
  539. /*
  540. * Set up receiving multicast socket over UDP
  541. */
  542. static struct socket * make_receive_sock(void)
  543. {
  544. struct socket *sock;
  545. int result;
  546. /* First create a socket */
  547. result = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
  548. if (result < 0) {
  549. IP_VS_ERR("Error during creation of socket; terminating\n");
  550. return ERR_PTR(result);
  551. }
  552. /* it is equivalent to the REUSEADDR option in user-space */
  553. sock->sk->sk_reuse = 1;
  554. result = sock->ops->bind(sock, (struct sockaddr *) &mcast_addr,
  555. sizeof(struct sockaddr));
  556. if (result < 0) {
  557. IP_VS_ERR("Error binding to the multicast addr\n");
  558. goto error;
  559. }
  560. /* join the multicast group */
  561. result = join_mcast_group(sock->sk,
  562. (struct in_addr *) &mcast_addr.sin_addr,
  563. ip_vs_backup_mcast_ifn);
  564. if (result < 0) {
  565. IP_VS_ERR("Error joining to the multicast group\n");
  566. goto error;
  567. }
  568. return sock;
  569. error:
  570. sock_release(sock);
  571. return ERR_PTR(result);
  572. }
  573. static int
  574. ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)
  575. {
  576. struct msghdr msg = {.msg_flags = MSG_DONTWAIT|MSG_NOSIGNAL};
  577. struct kvec iov;
  578. int len;
  579. EnterFunction(7);
  580. iov.iov_base = (void *)buffer;
  581. iov.iov_len = length;
  582. len = kernel_sendmsg(sock, &msg, &iov, 1, (size_t)(length));
  583. LeaveFunction(7);
  584. return len;
  585. }
  586. static void
  587. ip_vs_send_sync_msg(struct socket *sock, struct ip_vs_sync_mesg *msg)
  588. {
  589. int msize;
  590. msize = msg->size;
  591. /* Put size in network byte order */
  592. msg->size = htons(msg->size);
  593. if (ip_vs_send_async(sock, (char *)msg, msize) != msize)
  594. IP_VS_ERR("ip_vs_send_async error\n");
  595. }
  596. static int
  597. ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)
  598. {
  599. struct msghdr msg = {NULL,};
  600. struct kvec iov;
  601. int len;
  602. EnterFunction(7);
  603. /* Receive a packet */
  604. iov.iov_base = buffer;
  605. iov.iov_len = (size_t)buflen;
  606. len = kernel_recvmsg(sock, &msg, &iov, 1, buflen, 0);
  607. if (len < 0)
  608. return -1;
  609. LeaveFunction(7);
  610. return len;
  611. }
  612. static int sync_thread_master(void *data)
  613. {
  614. struct ip_vs_sync_thread_data *tinfo = data;
  615. struct ip_vs_sync_buff *sb;
  616. IP_VS_INFO("sync thread started: state = MASTER, mcast_ifn = %s, "
  617. "syncid = %d\n",
  618. ip_vs_master_mcast_ifn, ip_vs_master_syncid);
  619. while (!kthread_should_stop()) {
  620. while ((sb = sb_dequeue())) {
  621. ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
  622. ip_vs_sync_buff_release(sb);
  623. }
  624. /* check if entries stay in curr_sb for 2 seconds */
  625. sb = get_curr_sync_buff(2 * HZ);
  626. if (sb) {
  627. ip_vs_send_sync_msg(tinfo->sock, sb->mesg);
  628. ip_vs_sync_buff_release(sb);
  629. }
  630. schedule_timeout_interruptible(HZ);
  631. }
  632. /* clean up the sync_buff queue */
  633. while ((sb=sb_dequeue())) {
  634. ip_vs_sync_buff_release(sb);
  635. }
  636. /* clean up the current sync_buff */
  637. if ((sb = get_curr_sync_buff(0))) {
  638. ip_vs_sync_buff_release(sb);
  639. }
  640. /* release the sending multicast socket */
  641. sock_release(tinfo->sock);
  642. kfree(tinfo);
  643. return 0;
  644. }
  645. static int sync_thread_backup(void *data)
  646. {
  647. struct ip_vs_sync_thread_data *tinfo = data;
  648. int len;
  649. IP_VS_INFO("sync thread started: state = BACKUP, mcast_ifn = %s, "
  650. "syncid = %d\n",
  651. ip_vs_backup_mcast_ifn, ip_vs_backup_syncid);
  652. while (!kthread_should_stop()) {
  653. wait_event_interruptible(*tinfo->sock->sk->sk_sleep,
  654. !skb_queue_empty(&tinfo->sock->sk->sk_receive_queue)
  655. || kthread_should_stop());
  656. /* do we have data now? */
  657. while (!skb_queue_empty(&(tinfo->sock->sk->sk_receive_queue))) {
  658. len = ip_vs_receive(tinfo->sock, tinfo->buf,
  659. sync_recv_mesg_maxlen);
  660. if (len <= 0) {
  661. IP_VS_ERR("receiving message error\n");
  662. break;
  663. }
  664. /* disable bottom half, because it accesses the data
  665. shared by softirq while getting/creating conns */
  666. local_bh_disable();
  667. ip_vs_process_message(tinfo->buf, len);
  668. local_bh_enable();
  669. }
  670. }
  671. /* release the sending multicast socket */
  672. sock_release(tinfo->sock);
  673. kfree(tinfo->buf);
  674. kfree(tinfo);
  675. return 0;
  676. }
  677. int start_sync_thread(int state, char *mcast_ifn, __u8 syncid)
  678. {
  679. struct ip_vs_sync_thread_data *tinfo;
  680. struct task_struct **realtask, *task;
  681. struct socket *sock;
  682. char *name, *buf = NULL;
  683. int (*threadfn)(void *data);
  684. int result = -ENOMEM;
  685. IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
  686. IP_VS_DBG(7, "Each ip_vs_sync_conn entry needs %Zd bytes\n",
  687. sizeof(struct ip_vs_sync_conn));
  688. if (state == IP_VS_STATE_MASTER) {
  689. if (sync_master_thread)
  690. return -EEXIST;
  691. strlcpy(ip_vs_master_mcast_ifn, mcast_ifn,
  692. sizeof(ip_vs_master_mcast_ifn));
  693. ip_vs_master_syncid = syncid;
  694. realtask = &sync_master_thread;
  695. name = "ipvs_syncmaster";
  696. threadfn = sync_thread_master;
  697. sock = make_send_sock();
  698. } else if (state == IP_VS_STATE_BACKUP) {
  699. if (sync_backup_thread)
  700. return -EEXIST;
  701. strlcpy(ip_vs_backup_mcast_ifn, mcast_ifn,
  702. sizeof(ip_vs_backup_mcast_ifn));
  703. ip_vs_backup_syncid = syncid;
  704. realtask = &sync_backup_thread;
  705. name = "ipvs_syncbackup";
  706. threadfn = sync_thread_backup;
  707. sock = make_receive_sock();
  708. } else {
  709. return -EINVAL;
  710. }
  711. if (IS_ERR(sock)) {
  712. result = PTR_ERR(sock);
  713. goto out;
  714. }
  715. set_sync_mesg_maxlen(state);
  716. if (state == IP_VS_STATE_BACKUP) {
  717. buf = kmalloc(sync_recv_mesg_maxlen, GFP_KERNEL);
  718. if (!buf)
  719. goto outsocket;
  720. }
  721. tinfo = kmalloc(sizeof(*tinfo), GFP_KERNEL);
  722. if (!tinfo)
  723. goto outbuf;
  724. tinfo->sock = sock;
  725. tinfo->buf = buf;
  726. task = kthread_run(threadfn, tinfo, name);
  727. if (IS_ERR(task)) {
  728. result = PTR_ERR(task);
  729. goto outtinfo;
  730. }
  731. /* mark as active */
  732. *realtask = task;
  733. ip_vs_sync_state |= state;
  734. /* increase the module use count */
  735. ip_vs_use_count_inc();
  736. return 0;
  737. outtinfo:
  738. kfree(tinfo);
  739. outbuf:
  740. kfree(buf);
  741. outsocket:
  742. sock_release(sock);
  743. out:
  744. return result;
  745. }
  746. int stop_sync_thread(int state)
  747. {
  748. IP_VS_DBG(7, "%s: pid %d\n", __func__, task_pid_nr(current));
  749. if (state == IP_VS_STATE_MASTER) {
  750. if (!sync_master_thread)
  751. return -ESRCH;
  752. IP_VS_INFO("stopping master sync thread %d ...\n",
  753. task_pid_nr(sync_master_thread));
  754. /*
  755. * The lock synchronizes with sb_queue_tail(), so that we don't
  756. * add sync buffers to the queue, when we are already in
  757. * progress of stopping the master sync daemon.
  758. */
  759. spin_lock_bh(&ip_vs_sync_lock);
  760. ip_vs_sync_state &= ~IP_VS_STATE_MASTER;
  761. spin_unlock_bh(&ip_vs_sync_lock);
  762. kthread_stop(sync_master_thread);
  763. sync_master_thread = NULL;
  764. } else if (state == IP_VS_STATE_BACKUP) {
  765. if (!sync_backup_thread)
  766. return -ESRCH;
  767. IP_VS_INFO("stopping backup sync thread %d ...\n",
  768. task_pid_nr(sync_backup_thread));
  769. ip_vs_sync_state &= ~IP_VS_STATE_BACKUP;
  770. kthread_stop(sync_backup_thread);
  771. sync_backup_thread = NULL;
  772. } else {
  773. return -EINVAL;
  774. }
  775. /* decrease the module use count */
  776. ip_vs_use_count_dec();
  777. return 0;
  778. }