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
];
45 /* States for conn templates: NONE or words separated with ",", max 15 chars */
46 static const char *ip_vs_ctpl_state_name_table
[IP_VS_CTPL_S_LAST
] = {
47 [IP_VS_CTPL_S_NONE
] = "NONE",
48 [IP_VS_CTPL_S_ASSURED
] = "ASSURED",
52 * register an ipvs protocol
54 static int __used __init
register_ip_vs_protocol(struct ip_vs_protocol
*pp
)
56 unsigned int hash
= IP_VS_PROTO_HASH(pp
->protocol
);
58 pp
->next
= ip_vs_proto_table
[hash
];
59 ip_vs_proto_table
[hash
] = pp
;
68 * register an ipvs protocols netns related data
71 register_ip_vs_proto_netns(struct netns_ipvs
*ipvs
, struct ip_vs_protocol
*pp
)
73 unsigned int hash
= IP_VS_PROTO_HASH(pp
->protocol
);
74 struct ip_vs_proto_data
*pd
=
75 kzalloc(sizeof(struct ip_vs_proto_data
), GFP_KERNEL
);
80 pd
->pp
= pp
; /* For speed issues */
81 pd
->next
= ipvs
->proto_data_table
[hash
];
82 ipvs
->proto_data_table
[hash
] = pd
;
83 atomic_set(&pd
->appcnt
, 0); /* Init app counter */
85 if (pp
->init_netns
!= NULL
) {
86 int ret
= pp
->init_netns(ipvs
, pd
);
88 /* unlink an free proto data */
89 ipvs
->proto_data_table
[hash
] = pd
->next
;
99 * unregister an ipvs protocol
101 static int unregister_ip_vs_protocol(struct ip_vs_protocol
*pp
)
103 struct ip_vs_protocol
**pp_p
;
104 unsigned int hash
= IP_VS_PROTO_HASH(pp
->protocol
);
106 pp_p
= &ip_vs_proto_table
[hash
];
107 for (; *pp_p
; pp_p
= &(*pp_p
)->next
) {
110 if (pp
->exit
!= NULL
)
120 * unregister an ipvs protocols netns data
123 unregister_ip_vs_proto_netns(struct netns_ipvs
*ipvs
, struct ip_vs_proto_data
*pd
)
125 struct ip_vs_proto_data
**pd_p
;
126 unsigned int hash
= IP_VS_PROTO_HASH(pd
->pp
->protocol
);
128 pd_p
= &ipvs
->proto_data_table
[hash
];
129 for (; *pd_p
; pd_p
= &(*pd_p
)->next
) {
132 if (pd
->pp
->exit_netns
!= NULL
)
133 pd
->pp
->exit_netns(ipvs
, pd
);
143 * get ip_vs_protocol object by its proto.
145 struct ip_vs_protocol
* ip_vs_proto_get(unsigned short proto
)
147 struct ip_vs_protocol
*pp
;
148 unsigned int hash
= IP_VS_PROTO_HASH(proto
);
150 for (pp
= ip_vs_proto_table
[hash
]; pp
; pp
= pp
->next
) {
151 if (pp
->protocol
== proto
)
157 EXPORT_SYMBOL(ip_vs_proto_get
);
160 * get ip_vs_protocol object data by netns and proto
162 struct ip_vs_proto_data
*
163 ip_vs_proto_data_get(struct netns_ipvs
*ipvs
, unsigned short proto
)
165 struct ip_vs_proto_data
*pd
;
166 unsigned int hash
= IP_VS_PROTO_HASH(proto
);
168 for (pd
= ipvs
->proto_data_table
[hash
]; pd
; pd
= pd
->next
) {
169 if (pd
->pp
->protocol
== proto
)
175 EXPORT_SYMBOL(ip_vs_proto_data_get
);
178 * Propagate event for state change to all protocols
180 void ip_vs_protocol_timeout_change(struct netns_ipvs
*ipvs
, int flags
)
182 struct ip_vs_proto_data
*pd
;
185 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
186 for (pd
= ipvs
->proto_data_table
[i
]; pd
; pd
= pd
->next
) {
187 if (pd
->pp
->timeout_change
)
188 pd
->pp
->timeout_change(pd
, flags
);
195 ip_vs_create_timeout_table(int *table
, int size
)
197 return kmemdup(table
, size
, GFP_KERNEL
);
201 const char *ip_vs_state_name(const struct ip_vs_conn
*cp
)
203 unsigned int state
= cp
->state
;
204 struct ip_vs_protocol
*pp
;
206 if (cp
->flags
& IP_VS_CONN_F_TEMPLATE
) {
208 if (state
>= IP_VS_CTPL_S_LAST
)
210 return ip_vs_ctpl_state_name_table
[state
] ? : "?";
212 pp
= ip_vs_proto_get(cp
->protocol
);
213 if (pp
== NULL
|| pp
->state_name
== NULL
)
214 return (cp
->protocol
== IPPROTO_IP
) ? "NONE" : "ERR!";
215 return pp
->state_name(state
);
220 ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol
*pp
,
221 const struct sk_buff
*skb
,
226 struct iphdr _iph
, *ih
;
228 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
230 sprintf(buf
, "TRUNCATED");
231 else if (ih
->frag_off
& htons(IP_OFFSET
))
232 sprintf(buf
, "%pI4->%pI4 frag", &ih
->saddr
, &ih
->daddr
);
234 __be16 _ports
[2], *pptr
;
236 pptr
= skb_header_pointer(skb
, offset
+ ih
->ihl
*4,
237 sizeof(_ports
), _ports
);
239 sprintf(buf
, "TRUNCATED %pI4->%pI4",
240 &ih
->saddr
, &ih
->daddr
);
242 sprintf(buf
, "%pI4:%u->%pI4:%u",
243 &ih
->saddr
, ntohs(pptr
[0]),
244 &ih
->daddr
, ntohs(pptr
[1]));
247 pr_debug("%s: %s %s\n", msg
, pp
->name
, buf
);
250 #ifdef CONFIG_IP_VS_IPV6
252 ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol
*pp
,
253 const struct sk_buff
*skb
,
258 struct ipv6hdr _iph
, *ih
;
260 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
262 sprintf(buf
, "TRUNCATED");
263 else if (ih
->nexthdr
== IPPROTO_FRAGMENT
)
264 sprintf(buf
, "%pI6c->%pI6c frag", &ih
->saddr
, &ih
->daddr
);
266 __be16 _ports
[2], *pptr
;
268 pptr
= skb_header_pointer(skb
, offset
+ sizeof(struct ipv6hdr
),
269 sizeof(_ports
), _ports
);
271 sprintf(buf
, "TRUNCATED %pI6c->%pI6c",
272 &ih
->saddr
, &ih
->daddr
);
274 sprintf(buf
, "%pI6c:%u->%pI6c:%u",
275 &ih
->saddr
, ntohs(pptr
[0]),
276 &ih
->daddr
, ntohs(pptr
[1]));
279 pr_debug("%s: %s %s\n", msg
, pp
->name
, buf
);
285 ip_vs_tcpudp_debug_packet(int af
, struct ip_vs_protocol
*pp
,
286 const struct sk_buff
*skb
,
290 #ifdef CONFIG_IP_VS_IPV6
292 ip_vs_tcpudp_debug_packet_v6(pp
, skb
, offset
, msg
);
295 ip_vs_tcpudp_debug_packet_v4(pp
, skb
, offset
, msg
);
299 * per network name-space init
301 int __net_init
ip_vs_protocol_net_init(struct netns_ipvs
*ipvs
)
304 static struct ip_vs_protocol
*protos
[] = {
305 #ifdef CONFIG_IP_VS_PROTO_TCP
308 #ifdef CONFIG_IP_VS_PROTO_UDP
311 #ifdef CONFIG_IP_VS_PROTO_SCTP
312 &ip_vs_protocol_sctp
,
314 #ifdef CONFIG_IP_VS_PROTO_AH
317 #ifdef CONFIG_IP_VS_PROTO_ESP
322 for (i
= 0; i
< ARRAY_SIZE(protos
); i
++) {
323 ret
= register_ip_vs_proto_netns(ipvs
, protos
[i
]);
330 ip_vs_protocol_net_cleanup(ipvs
);
334 void __net_exit
ip_vs_protocol_net_cleanup(struct netns_ipvs
*ipvs
)
336 struct ip_vs_proto_data
*pd
;
339 /* unregister all the ipvs proto data for this netns */
340 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
341 while ((pd
= ipvs
->proto_data_table
[i
]) != NULL
)
342 unregister_ip_vs_proto_netns(ipvs
, pd
);
346 int __init
ip_vs_protocol_init(void)
349 #define REGISTER_PROTOCOL(p) \
351 register_ip_vs_protocol(p); \
352 strcat(protocols, ", "); \
353 strcat(protocols, (p)->name); \
358 #ifdef CONFIG_IP_VS_PROTO_TCP
359 REGISTER_PROTOCOL(&ip_vs_protocol_tcp
);
361 #ifdef CONFIG_IP_VS_PROTO_UDP
362 REGISTER_PROTOCOL(&ip_vs_protocol_udp
);
364 #ifdef CONFIG_IP_VS_PROTO_SCTP
365 REGISTER_PROTOCOL(&ip_vs_protocol_sctp
);
367 #ifdef CONFIG_IP_VS_PROTO_AH
368 REGISTER_PROTOCOL(&ip_vs_protocol_ah
);
370 #ifdef CONFIG_IP_VS_PROTO_ESP
371 REGISTER_PROTOCOL(&ip_vs_protocol_esp
);
373 pr_info("Registered protocols (%s)\n", &protocols
[2]);
379 void ip_vs_protocol_cleanup(void)
381 struct ip_vs_protocol
*pp
;
384 /* unregister all the ipvs protocols */
385 for (i
= 0; i
< IP_VS_PROTO_TAB_SIZE
; i
++) {
386 while ((pp
= ip_vs_proto_table
[i
]) != NULL
)
387 unregister_ip_vs_protocol(pp
);