2 * ip_vs_proto.c: transport protocol load balancing support for IPVS
4 * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
5 * Julian Anastasov <ja@ssi.bg>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/skbuff.h>
21 #include <net/protocol.h>
24 #include <asm/system.h>
25 #include <linux/stat.h>
26 #include <linux/proc_fs.h>
28 #include <net/ip_vs.h>
32 * IPVS protocols can only be registered/unregistered when the ipvs
33 * module is loaded/unloaded, so no lock is needed in accessing the
34 * ipvs protocol table.
37 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
38 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
40 static struct ip_vs_protocol
*ip_vs_proto_table
[IP_VS_PROTO_TAB_SIZE
];
44 * register an ipvs protocol
46 static int __used __init
register_ip_vs_protocol(struct ip_vs_protocol
*pp
)
48 unsigned hash
= IP_VS_PROTO_HASH(pp
->protocol
);
50 pp
->next
= ip_vs_proto_table
[hash
];
51 ip_vs_proto_table
[hash
] = pp
;
61 * unregister an ipvs protocol
63 static int unregister_ip_vs_protocol(struct ip_vs_protocol
*pp
)
65 struct ip_vs_protocol
**pp_p
;
66 unsigned hash
= IP_VS_PROTO_HASH(pp
->protocol
);
68 pp_p
= &ip_vs_proto_table
[hash
];
69 for (; *pp_p
; pp_p
= &(*pp_p
)->next
) {
83 * get ip_vs_protocol object by its proto.
85 struct ip_vs_protocol
* ip_vs_proto_get(unsigned short proto
)
87 struct ip_vs_protocol
*pp
;
88 unsigned hash
= IP_VS_PROTO_HASH(proto
);
90 for (pp
= ip_vs_proto_table
[hash
]; pp
; pp
= pp
->next
) {
91 if (pp
->protocol
== proto
)
100 * Propagate event for state change to all protocols
102 void ip_vs_protocol_timeout_change(int flags
)
104 struct ip_vs_protocol
*pp
;
107 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
108 for (pp
= ip_vs_proto_table
[i
]; pp
; pp
= pp
->next
) {
109 if (pp
->timeout_change
)
110 pp
->timeout_change(pp
, flags
);
117 ip_vs_create_timeout_table(int *table
, int size
)
119 return kmemdup(table
, size
, GFP_ATOMIC
);
124 * Set timeout value for state specified by name
127 ip_vs_set_state_timeout(int *table
, int num
, char **names
, char *name
, int to
)
131 if (!table
|| !name
|| !to
)
134 for (i
= 0; i
< num
; i
++) {
135 if (strcmp(names
[i
], name
))
144 const char * ip_vs_state_name(__u16 proto
, int state
)
146 struct ip_vs_protocol
*pp
= ip_vs_proto_get(proto
);
148 if (pp
== NULL
|| pp
->state_name
== NULL
)
149 return (IPPROTO_IP
== proto
) ? "NONE" : "ERR!";
150 return pp
->state_name(state
);
155 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol
*pp
,
156 const struct sk_buff
*skb
,
161 struct iphdr _iph
, *ih
;
163 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
165 sprintf(buf
, "%s TRUNCATED", pp
->name
);
166 else if (ih
->frag_off
& htons(IP_OFFSET
))
167 sprintf(buf
, "%s %pI4->%pI4 frag",
168 pp
->name
, &ih
->saddr
, &ih
->daddr
);
170 __be16 _ports
[2], *pptr
172 pptr
= skb_header_pointer(skb
, offset
+ ih
->ihl
*4,
173 sizeof(_ports
), _ports
);
175 sprintf(buf
, "%s TRUNCATED %pI4->%pI4",
176 pp
->name
, &ih
->saddr
, &ih
->daddr
);
178 sprintf(buf
, "%s %pI4:%u->%pI4:%u",
180 &ih
->saddr
, ntohs(pptr
[0]),
181 &ih
->daddr
, ntohs(pptr
[1]));
184 printk(KERN_DEBUG
"IPVS: %s: %s\n", msg
, buf
);
187 #ifdef CONFIG_IP_VS_IPV6
189 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol
*pp
,
190 const struct sk_buff
*skb
,
195 struct ipv6hdr _iph
, *ih
;
197 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
199 sprintf(buf
, "%s TRUNCATED", pp
->name
);
200 else if (ih
->nexthdr
== IPPROTO_FRAGMENT
)
201 sprintf(buf
, "%s %pI6->%pI6 frag",
202 pp
->name
, &ih
->saddr
, &ih
->daddr
);
204 __be16 _ports
[2], *pptr
;
206 pptr
= skb_header_pointer(skb
, offset
+ sizeof(struct ipv6hdr
),
207 sizeof(_ports
), _ports
);
209 sprintf(buf
, "%s TRUNCATED %pI6->%pI6",
210 pp
->name
, &ih
->saddr
, &ih
->daddr
);
212 sprintf(buf
, "%s %pI6:%u->%pI6:%u",
214 &ih
->saddr
, ntohs(pptr
[0]),
215 &ih
->daddr
, ntohs(pptr
[1]));
218 printk(KERN_DEBUG
"IPVS: %s: %s\n", msg
, buf
);
224 ip_vs_tcpudp_debug_packet(struct ip_vs_protocol
*pp
,
225 const struct sk_buff
*skb
,
229 #ifdef CONFIG_IP_VS_IPV6
230 if (skb
->protocol
== htons(ETH_P_IPV6
))
231 ip_vs_tcpudp_debug_packet_v6(pp
, skb
, offset
, msg
);
234 ip_vs_tcpudp_debug_packet_v4(pp
, skb
, offset
, msg
);
238 int __init
ip_vs_protocol_init(void)
241 #define REGISTER_PROTOCOL(p) \
243 register_ip_vs_protocol(p); \
244 strcat(protocols, ", "); \
245 strcat(protocols, (p)->name); \
250 #ifdef CONFIG_IP_VS_PROTO_TCP
251 REGISTER_PROTOCOL(&ip_vs_protocol_tcp
);
253 #ifdef CONFIG_IP_VS_PROTO_UDP
254 REGISTER_PROTOCOL(&ip_vs_protocol_udp
);
256 #ifdef CONFIG_IP_VS_PROTO_AH
257 REGISTER_PROTOCOL(&ip_vs_protocol_ah
);
259 #ifdef CONFIG_IP_VS_PROTO_ESP
260 REGISTER_PROTOCOL(&ip_vs_protocol_esp
);
262 IP_VS_INFO("Registered protocols (%s)\n", &protocols
[2]);
268 void ip_vs_protocol_cleanup(void)
270 struct ip_vs_protocol
*pp
;
273 /* unregister all the ipvs protocols */
274 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
275 while ((pp
= ip_vs_proto_table
[i
]) != NULL
)
276 unregister_ip_vs_protocol(pp
);