ip_vs_proto.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * ip_vs_proto.c: transport protocol load balancing support for IPVS
  3. *
  4. * Version: $Id: ip_vs_proto.c,v 1.2 2003/04/18 09:03:16 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  7. * Julian Anastasov <ja@ssi.bg>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Changes:
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/in.h>
  21. #include <linux/ip.h>
  22. #include <net/protocol.h>
  23. #include <net/tcp.h>
  24. #include <net/udp.h>
  25. #include <asm/system.h>
  26. #include <linux/stat.h>
  27. #include <linux/proc_fs.h>
  28. #include <net/ip_vs.h>
  29. /*
  30. * IPVS protocols can only be registered/unregistered when the ipvs
  31. * module is loaded/unloaded, so no lock is needed in accessing the
  32. * ipvs protocol table.
  33. */
  34. #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
  35. #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
  36. static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
  37. /*
  38. * register an ipvs protocol
  39. */
  40. static int register_ip_vs_protocol(struct ip_vs_protocol *pp)
  41. {
  42. unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
  43. pp->next = ip_vs_proto_table[hash];
  44. ip_vs_proto_table[hash] = pp;
  45. if (pp->init != NULL)
  46. pp->init(pp);
  47. return 0;
  48. }
  49. /*
  50. * unregister an ipvs protocol
  51. */
  52. static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
  53. {
  54. struct ip_vs_protocol **pp_p;
  55. unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
  56. pp_p = &ip_vs_proto_table[hash];
  57. for (; *pp_p; pp_p = &(*pp_p)->next) {
  58. if (*pp_p == pp) {
  59. *pp_p = pp->next;
  60. if (pp->exit != NULL)
  61. pp->exit(pp);
  62. return 0;
  63. }
  64. }
  65. return -ESRCH;
  66. }
  67. /*
  68. * get ip_vs_protocol object by its proto.
  69. */
  70. struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
  71. {
  72. struct ip_vs_protocol *pp;
  73. unsigned hash = IP_VS_PROTO_HASH(proto);
  74. for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
  75. if (pp->protocol == proto)
  76. return pp;
  77. }
  78. return NULL;
  79. }
  80. /*
  81. * Propagate event for state change to all protocols
  82. */
  83. void ip_vs_protocol_timeout_change(int flags)
  84. {
  85. struct ip_vs_protocol *pp;
  86. int i;
  87. for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
  88. for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
  89. if (pp->timeout_change)
  90. pp->timeout_change(pp, flags);
  91. }
  92. }
  93. }
  94. int *
  95. ip_vs_create_timeout_table(int *table, int size)
  96. {
  97. int *t;
  98. t = kmalloc(size, GFP_ATOMIC);
  99. if (t == NULL)
  100. return NULL;
  101. memcpy(t, table, size);
  102. return t;
  103. }
  104. /*
  105. * Set timeout value for state specified by name
  106. */
  107. int
  108. ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to)
  109. {
  110. int i;
  111. if (!table || !name || !to)
  112. return -EINVAL;
  113. for (i = 0; i < num; i++) {
  114. if (strcmp(names[i], name))
  115. continue;
  116. table[i] = to * HZ;
  117. return 0;
  118. }
  119. return -ENOENT;
  120. }
  121. const char * ip_vs_state_name(__u16 proto, int state)
  122. {
  123. struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
  124. if (pp == NULL || pp->state_name == NULL)
  125. return "ERR!";
  126. return pp->state_name(state);
  127. }
  128. void
  129. ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
  130. const struct sk_buff *skb,
  131. int offset,
  132. const char *msg)
  133. {
  134. char buf[128];
  135. struct iphdr _iph, *ih;
  136. ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
  137. if (ih == NULL)
  138. sprintf(buf, "%s TRUNCATED", pp->name);
  139. else if (ih->frag_off & __constant_htons(IP_OFFSET))
  140. sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
  141. pp->name, NIPQUAD(ih->saddr),
  142. NIPQUAD(ih->daddr));
  143. else {
  144. __u16 _ports[2], *pptr
  145. ;
  146. pptr = skb_header_pointer(skb, offset + ih->ihl*4,
  147. sizeof(_ports), _ports);
  148. if (pptr == NULL)
  149. sprintf(buf, "%s TRUNCATED %u.%u.%u.%u->%u.%u.%u.%u",
  150. pp->name,
  151. NIPQUAD(ih->saddr),
  152. NIPQUAD(ih->daddr));
  153. else
  154. sprintf(buf, "%s %u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
  155. pp->name,
  156. NIPQUAD(ih->saddr),
  157. ntohs(pptr[0]),
  158. NIPQUAD(ih->daddr),
  159. ntohs(pptr[1]));
  160. }
  161. printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
  162. }
  163. int ip_vs_protocol_init(void)
  164. {
  165. char protocols[64];
  166. #define REGISTER_PROTOCOL(p) \
  167. do { \
  168. register_ip_vs_protocol(p); \
  169. strcat(protocols, ", "); \
  170. strcat(protocols, (p)->name); \
  171. } while (0)
  172. protocols[0] = '\0';
  173. protocols[2] = '\0';
  174. #ifdef CONFIG_IP_VS_PROTO_TCP
  175. REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
  176. #endif
  177. #ifdef CONFIG_IP_VS_PROTO_UDP
  178. REGISTER_PROTOCOL(&ip_vs_protocol_udp);
  179. #endif
  180. #ifdef CONFIG_IP_VS_PROTO_AH
  181. REGISTER_PROTOCOL(&ip_vs_protocol_ah);
  182. #endif
  183. #ifdef CONFIG_IP_VS_PROTO_ESP
  184. REGISTER_PROTOCOL(&ip_vs_protocol_esp);
  185. #endif
  186. IP_VS_INFO("Registered protocols (%s)\n", &protocols[2]);
  187. return 0;
  188. }
  189. void ip_vs_protocol_cleanup(void)
  190. {
  191. struct ip_vs_protocol *pp;
  192. int i;
  193. /* unregister all the ipvs protocols */
  194. for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
  195. while ((pp = ip_vs_proto_table[i]) != NULL)
  196. unregister_ip_vs_protocol(pp);
  197. }
  198. }