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 #define KMSG_COMPONENT "IPVS"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/skbuff.h>
22 #include <linux/gfp.h>
25 #include <net/protocol.h>
28 #include <linux/stat.h>
29 #include <linux/proc_fs.h>
31 #include <net/ip_vs.h>
35 * IPVS protocols can only be registered/unregistered when the ipvs
36 * module is loaded/unloaded, so no lock is needed in accessing the
37 * ipvs protocol table.
40 #define IP_VS_PROTO_TAB_SIZE 32 /* must be power of 2 */
41 #define IP_VS_PROTO_HASH(proto) ((proto) & (IP_VS_PROTO_TAB_SIZE-1))
43 static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];
47 * register an ipvs protocol
49 static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
51 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
53 pp->next = ip_vs_proto_table[hash];
54 ip_vs_proto_table[hash] = pp;
63 * register an ipvs protocols netns related data
66 register_ip_vs_proto_netns(struct netns_ipvs *ipvs, struct ip_vs_protocol *pp)
68 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
69 struct ip_vs_proto_data *pd =
70 kzalloc(sizeof(struct ip_vs_proto_data), GFP_KERNEL);
75 pd->pp = pp; /* For speed issues */
76 pd->next = ipvs->proto_data_table[hash];
77 ipvs->proto_data_table[hash] = pd;
78 atomic_set(&pd->appcnt, 0); /* Init app counter */
80 if (pp->init_netns != NULL) {
81 int ret = pp->init_netns(ipvs, pd);
83 /* unlink an free proto data */
84 ipvs->proto_data_table[hash] = pd->next;
94 * unregister an ipvs protocol
96 static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
98 struct ip_vs_protocol **pp_p;
99 unsigned int hash = IP_VS_PROTO_HASH(pp->protocol);
101 pp_p = &ip_vs_proto_table[hash];
102 for (; *pp_p; pp_p = &(*pp_p)->next) {
105 if (pp->exit != NULL)
115 * unregister an ipvs protocols netns data
118 unregister_ip_vs_proto_netns(struct netns_ipvs *ipvs, struct ip_vs_proto_data *pd)
120 struct ip_vs_proto_data **pd_p;
121 unsigned int hash = IP_VS_PROTO_HASH(pd->pp->protocol);
123 pd_p = &ipvs->proto_data_table[hash];
124 for (; *pd_p; pd_p = &(*pd_p)->next) {
127 if (pd->pp->exit_netns != NULL)
128 pd->pp->exit_netns(ipvs, pd);
138 * get ip_vs_protocol object by its proto.
140 struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
142 struct ip_vs_protocol *pp;
143 unsigned int hash = IP_VS_PROTO_HASH(proto);
145 for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
146 if (pp->protocol == proto)
152 EXPORT_SYMBOL(ip_vs_proto_get);
155 * get ip_vs_protocol object data by netns and proto
157 struct ip_vs_proto_data *
158 ip_vs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
160 struct ip_vs_proto_data *pd;
161 unsigned int hash = IP_VS_PROTO_HASH(proto);
163 for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) {
164 if (pd->pp->protocol == proto)
170 EXPORT_SYMBOL(ip_vs_proto_data_get);
173 * Propagate event for state change to all protocols
175 void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
177 struct ip_vs_proto_data *pd;
180 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
181 for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
182 if (pd->pp->timeout_change)
183 pd->pp->timeout_change(pd, flags);
190 ip_vs_create_timeout_table(int *table, int size)
192 return kmemdup(table, size, GFP_KERNEL);
197 * Set timeout value for state specified by name
200 ip_vs_set_state_timeout(int *table, int num, const char *const *names,
201 const char *name, int to)
205 if (!table || !name || !to)
208 for (i = 0; i < num; i++) {
209 if (strcmp(names[i], name))
218 const char * ip_vs_state_name(__u16 proto, int state)
220 struct ip_vs_protocol *pp = ip_vs_proto_get(proto);
222 if (pp == NULL || pp->state_name == NULL)
223 return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
224 return pp->state_name(state);
229 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
230 const struct sk_buff *skb,
235 struct iphdr _iph, *ih;
237 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
239 sprintf(buf, "TRUNCATED");
240 else if (ih->frag_off & htons(IP_OFFSET))
241 sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
243 __be16 _ports[2], *pptr;
245 pptr = skb_header_pointer(skb, offset + ih->ihl*4,
246 sizeof(_ports), _ports);
248 sprintf(buf, "TRUNCATED %pI4->%pI4",
249 &ih->saddr, &ih->daddr);
251 sprintf(buf, "%pI4:%u->%pI4:%u",
252 &ih->saddr, ntohs(pptr[0]),
253 &ih->daddr, ntohs(pptr[1]));
256 pr_debug("%s: %s %s\n", msg, pp->name, buf);
259 #ifdef CONFIG_IP_VS_IPV6
261 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
262 const struct sk_buff *skb,
267 struct ipv6hdr _iph, *ih;
269 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
271 sprintf(buf, "TRUNCATED");
272 else if (ih->nexthdr == IPPROTO_FRAGMENT)
273 sprintf(buf, "%pI6c->%pI6c frag", &ih->saddr, &ih->daddr);
275 __be16 _ports[2], *pptr;
277 pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
278 sizeof(_ports), _ports);
280 sprintf(buf, "TRUNCATED %pI6c->%pI6c",
281 &ih->saddr, &ih->daddr);
283 sprintf(buf, "%pI6c:%u->%pI6c:%u",
284 &ih->saddr, ntohs(pptr[0]),
285 &ih->daddr, ntohs(pptr[1]));
288 pr_debug("%s: %s %s\n", msg, pp->name, buf);
294 ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
295 const struct sk_buff *skb,
299 #ifdef CONFIG_IP_VS_IPV6
301 ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
304 ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
308 * per network name-space init
310 int __net_init ip_vs_protocol_net_init(struct netns_ipvs *ipvs)
313 static struct ip_vs_protocol *protos[] = {
314 #ifdef CONFIG_IP_VS_PROTO_TCP
317 #ifdef CONFIG_IP_VS_PROTO_UDP
320 #ifdef CONFIG_IP_VS_PROTO_SCTP
321 &ip_vs_protocol_sctp,
323 #ifdef CONFIG_IP_VS_PROTO_AH
326 #ifdef CONFIG_IP_VS_PROTO_ESP
331 for (i = 0; i < ARRAY_SIZE(protos); i++) {
332 ret = register_ip_vs_proto_netns(ipvs, protos[i]);
339 ip_vs_protocol_net_cleanup(ipvs);
343 void __net_exit ip_vs_protocol_net_cleanup(struct netns_ipvs *ipvs)
345 struct ip_vs_proto_data *pd;
348 /* unregister all the ipvs proto data for this netns */
349 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
350 while ((pd = ipvs->proto_data_table[i]) != NULL)
351 unregister_ip_vs_proto_netns(ipvs, pd);
355 int __init ip_vs_protocol_init(void)
358 #define REGISTER_PROTOCOL(p) \
360 register_ip_vs_protocol(p); \
361 strcat(protocols, ", "); \
362 strcat(protocols, (p)->name); \
367 #ifdef CONFIG_IP_VS_PROTO_TCP
368 REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
370 #ifdef CONFIG_IP_VS_PROTO_UDP
371 REGISTER_PROTOCOL(&ip_vs_protocol_udp);
373 #ifdef CONFIG_IP_VS_PROTO_SCTP
374 REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
376 #ifdef CONFIG_IP_VS_PROTO_AH
377 REGISTER_PROTOCOL(&ip_vs_protocol_ah);
379 #ifdef CONFIG_IP_VS_PROTO_ESP
380 REGISTER_PROTOCOL(&ip_vs_protocol_esp);
382 pr_info("Registered protocols (%s)\n", &protocols[2]);
388 void ip_vs_protocol_cleanup(void)
390 struct ip_vs_protocol *pp;
393 /* unregister all the ipvs protocols */
394 for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
395 while ((pp = ip_vs_proto_table[i]) != NULL)
396 unregister_ip_vs_protocol(pp);