Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / ipvs / ip_vs_proto.c
blob253c46252bd5b1dfeae6382c94a2eaf7340278ae
1 /*
2 * ip_vs_proto.c: transport protocol load balancing support for IPVS
4 * Version: $Id: ip_vs_proto.c,v 1.2 2003/04/18 09:03:16 wensong Exp $
6 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
7 * Julian Anastasov <ja@ssi.bg>
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.
14 * Changes:
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/skbuff.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <net/protocol.h>
24 #include <net/tcp.h>
25 #include <net/udp.h>
26 #include <asm/system.h>
27 #include <linux/stat.h>
28 #include <linux/proc_fs.h>
30 #include <net/ip_vs.h>
34 * IPVS protocols can only be registered/unregistered when the ipvs
35 * module is loaded/unloaded, so no lock is needed in accessing the
36 * ipvs protocol table.
39 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
40 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
42 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
46 * register an ipvs protocol
48 static int register_ip_vs_protocol(struct ip_vs_protocol *pp)
50 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
52 pp->next = ip_vs_proto_table[hash];
53 ip_vs_proto_table[hash] = pp;
55 if (pp->init != NULL)
56 pp->init(pp);
58 return 0;
63 * unregister an ipvs protocol
65 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
67 struct ip_vs_protocol **pp_p;
68 unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
70 pp_p = &ip_vs_proto_table[hash];
71 for (; *pp_p; pp_p = &(*pp_p)->next) {
72 if (*pp_p == pp) {
73 *pp_p = pp->next;
74 if (pp->exit != NULL)
75 pp->exit(pp);
76 return 0;
80 return -ESRCH;
85 * get ip_vs_protocol object by its proto.
87 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
89 struct ip_vs_protocol *pp;
90 unsigned hash = IP_VS_PROTO_HASH(proto);
92 for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
93 if (pp->protocol == proto)
94 return pp;
97 return NULL;
102 * Propagate event for state change to all protocols
104 void ip_vs_protocol_timeout_change(int flags)
106 struct ip_vs_protocol *pp;
107 int i;
109 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
110 for (pp = ip_vs_proto_table[i]; pp; pp = pp->next) {
111 if (pp->timeout_change)
112 pp->timeout_change(pp, flags);
118 int *
119 ip_vs_create_timeout_table(int *table, int size)
121 int *t;
123 t = kmalloc(size, GFP_ATOMIC);
124 if (t == NULL)
125 return NULL;
126 memcpy(t, table, size);
127 return t;
132 * Set timeout value for state specified by name
135 ip_vs_set_state_timeout(int *table, int num, char **names, char *name, int to)
137 int i;
139 if (!table || !name || !to)
140 return -EINVAL;
142 for (i = 0; i < num; i++) {
143 if (strcmp(names[i], name))
144 continue;
145 table[i] = to * HZ;
146 return 0;
148 return -ENOENT;
152 const char * ip_vs_state_name(__u16 proto, int state)
154 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
156 if (pp == NULL || pp->state_name == NULL)
157 return "ERR!";
158 return pp->state_name(state);
162 void
163 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol *pp,
164 const struct sk_buff *skb,
165 int offset,
166 const char *msg)
168 char buf[128];
169 struct iphdr _iph, *ih;
171 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
172 if (ih == NULL)
173 sprintf(buf, "%s TRUNCATED", pp->name);
174 else if (ih->frag_off & __constant_htons(IP_OFFSET))
175 sprintf(buf, "%s %u.%u.%u.%u->%u.%u.%u.%u frag",
176 pp->name, NIPQUAD(ih->saddr),
177 NIPQUAD(ih->daddr));
178 else {
179 __u16 _ports[2], *pptr
181 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
182 sizeof(_ports), _ports);
183 if (pptr == NULL)
184 sprintf(buf, "%s TRUNCATED %u.%u.%u.%u->%u.%u.%u.%u",
185 pp->name,
186 NIPQUAD(ih->saddr),
187 NIPQUAD(ih->daddr));
188 else
189 sprintf(buf, "%s %u.%u.%u.%u:%u->%u.%u.%u.%u:%u",
190 pp->name,
191 NIPQUAD(ih->saddr),
192 ntohs(pptr[0]),
193 NIPQUAD(ih->daddr),
194 ntohs(pptr[1]));
197 printk(KERN_DEBUG "IPVS: %s: %s\n", msg, buf);
201 int ip_vs_protocol_init(void)
203 char protocols[64];
204 #define REGISTER_PROTOCOL(p) \
205 do { \
206 register_ip_vs_protocol(p); \
207 strcat(protocols, ", "); \
208 strcat(protocols, (p)->name); \
209 } while (0)
211 protocols[0] = '\0';
212 protocols[2] = '\0';
213 #ifdef CONFIG_IP_VS_PROTO_TCP
214 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
215 #endif
216 #ifdef CONFIG_IP_VS_PROTO_UDP
217 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
218 #endif
219 #ifdef CONFIG_IP_VS_PROTO_ICMP
220 REGISTER_PROTOCOL(&ip_vs_protocol_icmp);
221 #endif
222 #ifdef CONFIG_IP_VS_PROTO_AH
223 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
224 #endif
225 #ifdef CONFIG_IP_VS_PROTO_ESP
226 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
227 #endif
228 IP_VS_INFO("Registered protocols (%s)\n", &protocols[2]);
230 return 0;
234 void ip_vs_protocol_cleanup(void)
236 struct ip_vs_protocol *pp;
237 int i;
239 /* unregister all the ipvs protocols */
240 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
241 while ((pp = ip_vs_proto_table[i]) != NULL)
242 unregister_ip_vs_protocol(pp);