ip_vs_proto.c 9.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * ip_vs_proto.c: transport protocol load balancing support for IPVS
 *
 * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
 *              Julian Anastasov <ja@ssi.bg>
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 *
 * Changes:
 *
 */

H
Hannes Eder 已提交
16 17 18
#define KMSG_COMPONENT "IPVS"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
19 20 21
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
22
#include <linux/gfp.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <linux/in.h>
#include <linux/ip.h>
#include <net/protocol.h>
#include <net/tcp.h>
#include <net/udp.h>
#include <asm/system.h>
#include <linux/stat.h>
#include <linux/proc_fs.h>

#include <net/ip_vs.h>


/*
 * IPVS protocols can only be registered/unregistered when the ipvs
 * module is loaded/unloaded, so no lock is needed in accessing the
 * ipvs protocol table.
 */

#define IP_VS_PROTO_TAB_SIZE		32	/* must be power of 2 */
#define IP_VS_PROTO_HASH(proto)		((proto) & (IP_VS_PROTO_TAB_SIZE-1))

static struct ip_vs_protocol *ip_vs_proto_table[IP_VS_PROTO_TAB_SIZE];


/*
 *	register an ipvs protocol
 */
50
static int __used __init register_ip_vs_protocol(struct ip_vs_protocol *pp)
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62
{
	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);

	pp->next = ip_vs_proto_table[hash];
	ip_vs_proto_table[hash] = pp;

	if (pp->init != NULL)
		pp->init(pp);

	return 0;
}

63 64 65
#if defined(CONFIG_IP_VS_PROTO_TCP) || defined(CONFIG_IP_VS_PROTO_UDP) || \
    defined(CONFIG_IP_VS_PROTO_SCTP) || defined(CONFIG_IP_VS_PROTO_AH) || \
    defined(CONFIG_IP_VS_PROTO_ESP)
H
Hans Schillstrom 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/*
 *	register an ipvs protocols netns related data
 */
static int
register_ip_vs_proto_netns(struct net *net, struct ip_vs_protocol *pp)
{
	struct netns_ipvs *ipvs = net_ipvs(net);
	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);
	struct ip_vs_proto_data *pd =
			kzalloc(sizeof(struct ip_vs_proto_data), GFP_ATOMIC);

	if (!pd) {
		pr_err("%s(): no memory.\n", __func__);
		return -ENOMEM;
	}
	pd->pp = pp;	/* For speed issues */
	pd->next = ipvs->proto_data_table[hash];
	ipvs->proto_data_table[hash] = pd;
	atomic_set(&pd->appcnt, 0);	/* Init app counter */

	if (pp->init_netns != NULL)
		pp->init_netns(net, pd);

	return 0;
}
91
#endif
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

/*
 *	unregister an ipvs protocol
 */
static int unregister_ip_vs_protocol(struct ip_vs_protocol *pp)
{
	struct ip_vs_protocol **pp_p;
	unsigned hash = IP_VS_PROTO_HASH(pp->protocol);

	pp_p = &ip_vs_proto_table[hash];
	for (; *pp_p; pp_p = &(*pp_p)->next) {
		if (*pp_p == pp) {
			*pp_p = pp->next;
			if (pp->exit != NULL)
				pp->exit(pp);
			return 0;
		}
	}

	return -ESRCH;
}

H
Hans Schillstrom 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
 *	unregister an ipvs protocols netns data
 */
static int
unregister_ip_vs_proto_netns(struct net *net, struct ip_vs_proto_data *pd)
{
	struct netns_ipvs *ipvs = net_ipvs(net);
	struct ip_vs_proto_data **pd_p;
	unsigned hash = IP_VS_PROTO_HASH(pd->pp->protocol);

	pd_p = &ipvs->proto_data_table[hash];
	for (; *pd_p; pd_p = &(*pd_p)->next) {
		if (*pd_p == pd) {
			*pd_p = pd->next;
			if (pd->pp->exit_netns != NULL)
				pd->pp->exit_netns(net, pd);
			kfree(pd);
			return 0;
		}
	}

	return -ESRCH;
}
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

/*
 *	get ip_vs_protocol object by its proto.
 */
struct ip_vs_protocol * ip_vs_proto_get(unsigned short proto)
{
	struct ip_vs_protocol *pp;
	unsigned hash = IP_VS_PROTO_HASH(proto);

	for (pp = ip_vs_proto_table[hash]; pp; pp = pp->next) {
		if (pp->protocol == proto)
			return pp;
	}

	return NULL;
}
153
EXPORT_SYMBOL(ip_vs_proto_get);
L
Linus Torvalds 已提交
154

H
Hans Schillstrom 已提交
155 156 157 158
/*
 *	get ip_vs_protocol object data by netns and proto
 */
struct ip_vs_proto_data *
159
__ipvs_proto_data_get(struct netns_ipvs *ipvs, unsigned short proto)
H
Hans Schillstrom 已提交
160 161 162 163 164 165 166 167 168 169 170
{
	struct ip_vs_proto_data *pd;
	unsigned hash = IP_VS_PROTO_HASH(proto);

	for (pd = ipvs->proto_data_table[hash]; pd; pd = pd->next) {
		if (pd->pp->protocol == proto)
			return pd;
	}

	return NULL;
}
171 172 173 174 175 176 177 178

struct ip_vs_proto_data *
ip_vs_proto_data_get(struct net *net, unsigned short proto)
{
	struct netns_ipvs *ipvs = net_ipvs(net);

	return __ipvs_proto_data_get(ipvs, proto);
}
H
Hans Schillstrom 已提交
179
EXPORT_SYMBOL(ip_vs_proto_data_get);
L
Linus Torvalds 已提交
180 181 182 183

/*
 *	Propagate event for state change to all protocols
 */
184
void ip_vs_protocol_timeout_change(struct netns_ipvs *ipvs, int flags)
L
Linus Torvalds 已提交
185
{
186
	struct ip_vs_proto_data *pd;
L
Linus Torvalds 已提交
187 188 189
	int i;

	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
190 191 192
		for (pd = ipvs->proto_data_table[i]; pd; pd = pd->next) {
			if (pd->pp->timeout_change)
				pd->pp->timeout_change(pd, flags);
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200
		}
	}
}


int *
ip_vs_create_timeout_table(int *table, int size)
{
201
	return kmemdup(table, size, GFP_ATOMIC);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208
}


/*
 *	Set timeout value for state specified by name
 */
int
209 210
ip_vs_set_state_timeout(int *table, int num, const char *const *names,
			const char *name, int to)
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
	int i;

	if (!table || !name || !to)
		return -EINVAL;

	for (i = 0; i < num; i++) {
		if (strcmp(names[i], name))
			continue;
		table[i] = to * HZ;
		return 0;
	}
	return -ENOENT;
}


const char * ip_vs_state_name(__u16 proto, int state)
{
	struct ip_vs_protocol *pp = ip_vs_proto_get(proto);

	if (pp == NULL || pp->state_name == NULL)
232
		return (IPPROTO_IP == proto) ? "NONE" : "ERR!";
L
Linus Torvalds 已提交
233 234 235 236
	return pp->state_name(state);
}


237
static void
238 239 240 241
ip_vs_tcpudp_debug_packet_v4(struct ip_vs_protocol *pp,
			     const struct sk_buff *skb,
			     int offset,
			     const char *msg)
L
Linus Torvalds 已提交
242 243 244 245 246 247
{
	char buf[128];
	struct iphdr _iph, *ih;

	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
	if (ih == NULL)
248
		sprintf(buf, "TRUNCATED");
249
	else if (ih->frag_off & htons(IP_OFFSET))
250
		sprintf(buf, "%pI4->%pI4 frag", &ih->saddr, &ih->daddr);
L
Linus Torvalds 已提交
251
	else {
252 253
		__be16 _ports[2], *pptr;

L
Linus Torvalds 已提交
254 255 256
		pptr = skb_header_pointer(skb, offset + ih->ihl*4,
					  sizeof(_ports), _ports);
		if (pptr == NULL)
257 258
			sprintf(buf, "TRUNCATED %pI4->%pI4",
				&ih->saddr, &ih->daddr);
L
Linus Torvalds 已提交
259
		else
260
			sprintf(buf, "%pI4:%u->%pI4:%u",
261 262
				&ih->saddr, ntohs(pptr[0]),
				&ih->daddr, ntohs(pptr[1]));
L
Linus Torvalds 已提交
263 264
	}

265
	pr_debug("%s: %s %s\n", msg, pp->name, buf);
L
Linus Torvalds 已提交
266 267
}

268
#ifdef CONFIG_IP_VS_IPV6
269
static void
270 271 272 273 274 275 276 277 278 279
ip_vs_tcpudp_debug_packet_v6(struct ip_vs_protocol *pp,
			     const struct sk_buff *skb,
			     int offset,
			     const char *msg)
{
	char buf[192];
	struct ipv6hdr _iph, *ih;

	ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
	if (ih == NULL)
280
		sprintf(buf, "TRUNCATED");
281
	else if (ih->nexthdr == IPPROTO_FRAGMENT)
282
		sprintf(buf, "%pI6->%pI6 frag",	&ih->saddr, &ih->daddr);
283 284 285 286 287 288
	else {
		__be16 _ports[2], *pptr;

		pptr = skb_header_pointer(skb, offset + sizeof(struct ipv6hdr),
					  sizeof(_ports), _ports);
		if (pptr == NULL)
289 290
			sprintf(buf, "TRUNCATED %pI6->%pI6",
				&ih->saddr, &ih->daddr);
291
		else
292
			sprintf(buf, "%pI6:%u->%pI6:%u",
293 294
				&ih->saddr, ntohs(pptr[0]),
				&ih->daddr, ntohs(pptr[1]));
295 296
	}

297
	pr_debug("%s: %s %s\n", msg, pp->name, buf);
298 299 300 301 302
}
#endif


void
303
ip_vs_tcpudp_debug_packet(int af, struct ip_vs_protocol *pp,
304 305 306 307 308
			  const struct sk_buff *skb,
			  int offset,
			  const char *msg)
{
#ifdef CONFIG_IP_VS_IPV6
309
	if (af == AF_INET6)
310 311 312 313 314 315
		ip_vs_tcpudp_debug_packet_v6(pp, skb, offset, msg);
	else
#endif
		ip_vs_tcpudp_debug_packet_v4(pp, skb, offset, msg);
}

316 317 318 319 320
/*
 * per network name-space init
 */
static int __net_init __ip_vs_protocol_init(struct net *net)
{
321 322
#ifdef CONFIG_IP_VS_PROTO_TCP
	register_ip_vs_proto_netns(net, &ip_vs_protocol_tcp);
323 324 325
#endif
#ifdef CONFIG_IP_VS_PROTO_UDP
	register_ip_vs_proto_netns(net, &ip_vs_protocol_udp);
326 327 328
#endif
#ifdef CONFIG_IP_VS_PROTO_SCTP
	register_ip_vs_proto_netns(net, &ip_vs_protocol_sctp);
329 330 331 332 333 334
#endif
#ifdef CONFIG_IP_VS_PROTO_AH
	register_ip_vs_proto_netns(net, &ip_vs_protocol_ah);
#endif
#ifdef CONFIG_IP_VS_PROTO_ESP
	register_ip_vs_proto_netns(net, &ip_vs_protocol_esp);
335
#endif
336 337 338 339 340
	return 0;
}

static void __net_exit __ip_vs_protocol_cleanup(struct net *net)
{
341 342 343 344 345 346 347 348 349
	struct netns_ipvs *ipvs = net_ipvs(net);
	struct ip_vs_proto_data *pd;
	int i;

	/* unregister all the ipvs proto data for this netns */
	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
		while ((pd = ipvs->proto_data_table[i]) != NULL)
			unregister_ip_vs_proto_netns(net, pd);
	}
350 351 352 353 354 355
}

static struct pernet_operations ipvs_proto_ops = {
	.init = __ip_vs_protocol_init,
	.exit = __ip_vs_protocol_cleanup,
};
L
Linus Torvalds 已提交
356

357
int __init ip_vs_protocol_init(void)
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
{
	char protocols[64];
#define REGISTER_PROTOCOL(p)			\
	do {					\
		register_ip_vs_protocol(p);	\
		strcat(protocols, ", ");	\
		strcat(protocols, (p)->name);	\
	} while (0)

	protocols[0] = '\0';
	protocols[2] = '\0';
#ifdef CONFIG_IP_VS_PROTO_TCP
	REGISTER_PROTOCOL(&ip_vs_protocol_tcp);
#endif
#ifdef CONFIG_IP_VS_PROTO_UDP
	REGISTER_PROTOCOL(&ip_vs_protocol_udp);
#endif
375 376 377
#ifdef CONFIG_IP_VS_PROTO_SCTP
	REGISTER_PROTOCOL(&ip_vs_protocol_sctp);
#endif
L
Linus Torvalds 已提交
378 379 380 381 382 383
#ifdef CONFIG_IP_VS_PROTO_AH
	REGISTER_PROTOCOL(&ip_vs_protocol_ah);
#endif
#ifdef CONFIG_IP_VS_PROTO_ESP
	REGISTER_PROTOCOL(&ip_vs_protocol_esp);
#endif
384
	pr_info("Registered protocols (%s)\n", &protocols[2]);
385
	return register_pernet_subsys(&ipvs_proto_ops);
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395

	return 0;
}


void ip_vs_protocol_cleanup(void)
{
	struct ip_vs_protocol *pp;
	int i;

396
	unregister_pernet_subsys(&ipvs_proto_ops);
L
Linus Torvalds 已提交
397 398 399 400 401 402
	/* unregister all the ipvs protocols */
	for (i = 0; i < IP_VS_PROTO_TAB_SIZE; i++) {
		while ((pp = ip_vs_proto_table[i]) != NULL)
			unregister_ip_vs_protocol(pp);
	}
}