📄 ip_vs_lblc.c
字号:
/* * IPVS: Locality-Based Least-Connection scheduling module * * Version: $Id: ip_vs_lblc.c,v 1.9 2002/03/25 12:44:35 wensong Exp $ * * Authors: Wensong Zhang <wensong@gnuchina.org> * * 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: * Martin Hamilton : fixed the terrible locking bugs * *lock(tbl->lock) ==> *lock(&tbl->lock) * Wensong Zhang : fixed the uninitilized tbl->lock bug * Wensong Zhang : added doing full expiration check to * collect stale entries of 24+ hours when * no partial expire check in a half hour * Julian Anastasov : replaced del_timer call with del_timer_sync * to avoid the possible race between timer * handler and del_timer thread in SMP * *//* * The lblc algorithm is as follows (pseudo code): * * if cachenode[dest_ip] is null then * n, cachenode[dest_ip] <- {weighted least-conn node}; * else * n <- cachenode[dest_ip]; * if (n is dead) OR * (n.conns>n.weight AND * there is a node m with m.conns<m.weight/2) then * n, cachenode[dest_ip] <- {weighted least-conn node}; * * return n; * * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing * me to write this module. */#include <linux/module.h>#include <linux/kernel.h>/* for sysctl */#include <linux/fs.h>#include <linux/sysctl.h>#include <net/ip_vs.h>/* * It is for garbage collection of stale IPVS lblc entries, * when the table is full. */#define CHECK_EXPIRE_INTERVAL (60*HZ)#define ENTRY_TIMEOUT (6*60*HZ)/* * It is for full expiration check. * When there is no partial expiration check (garbage collection) * in a half hour, do a full expiration check to collect stale * entries that haven't been touched for a day. */#define COUNT_FOR_FULL_EXPIRATION 30static int sysctl_ip_vs_lblc_expiration = 24*60*60*HZ;/* * for IPVS lblc entry hash table */#ifndef CONFIG_IP_VS_LBLC_TAB_BITS#define CONFIG_IP_VS_LBLC_TAB_BITS 10#endif#define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS#define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)#define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)/* * IPVS lblc entry represents an association between destination * IP address and its destination server */struct ip_vs_lblc_entry { struct list_head list; __u32 addr; /* destination IP address */ struct ip_vs_dest *dest; /* real server (cache) */ unsigned long lastuse; /* last used time */};/* * IPVS lblc hash table */struct ip_vs_lblc_table { rwlock_t lock; /* lock for this table */ struct list_head bucket[IP_VS_LBLC_TAB_SIZE]; /* hash bucket */ atomic_t entries; /* number of entries */ int max_size; /* maximum size of entries */ struct timer_list periodic_timer; /* collect stale entries */ int rover; /* rover for expire check */ int counter; /* counter for no expire */};/* * IPVS LBLC sysctl table */struct ip_vs_lblc_sysctl_table { struct ctl_table_header *sysctl_header; ctl_table vs_vars[2]; ctl_table vs_dir[2]; ctl_table ipv4_dir[2]; ctl_table root_dir[2];};static struct ip_vs_lblc_sysctl_table lblc_sysctl_table = { NULL, {{NET_IPV4_VS_LBLC_EXPIRE, "lblc_expiration", &sysctl_ip_vs_lblc_expiration, sizeof(int), 0644, NULL, &proc_dointvec_jiffies}, {0}}, {{NET_IPV4_VS, "vs", NULL, 0, 0555, lblc_sysctl_table.vs_vars}, {0}}, {{NET_IPV4, "ipv4", NULL, 0, 0555, lblc_sysctl_table.vs_dir}, {0}}, {{CTL_NET, "net", NULL, 0, 0555, lblc_sysctl_table.ipv4_dir}, {0}}};/* * new/free a ip_vs_lblc_entry, which is a mapping of a destionation * IP address to a server. */static inline struct ip_vs_lblc_entry *ip_vs_lblc_new(__u32 daddr, struct ip_vs_dest *dest){ struct ip_vs_lblc_entry *en; en = kmalloc(sizeof(struct ip_vs_lblc_entry), GFP_ATOMIC); if (en == NULL) { IP_VS_ERR("ip_vs_lblc_new(): no memory\n"); return NULL; } INIT_LIST_HEAD(&en->list); en->addr = daddr; atomic_inc(&dest->refcnt); en->dest = dest; return en;}static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en){ list_del(&en->list); /* * We don't kfree dest because it is refered either by its service * or the trash dest list. */ atomic_dec(&en->dest->refcnt); kfree(en);}/* * Returns hash value for IPVS LBLC entry */static inline unsigned ip_vs_lblc_hashkey(__u32 addr){ return (ntohl(addr)*2654435761UL) & IP_VS_LBLC_TAB_MASK;}/* * Hash an entry in the ip_vs_lblc_table. * returns bool success. */static intip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en){ unsigned hash; if (!list_empty(&en->list)) { IP_VS_ERR("ip_vs_lblc_hash(): request for already hashed, " "called from %p\n", __builtin_return_address(0)); return 0; } /* * Hash by destination IP address */ hash = ip_vs_lblc_hashkey(en->addr); write_lock(&tbl->lock); list_add(&en->list, &tbl->bucket[hash]); atomic_inc(&tbl->entries); write_unlock(&tbl->lock); return 1;}#if 0000/* * Unhash ip_vs_lblc_entry from ip_vs_lblc_table. * returns bool success. */static int ip_vs_lblc_unhash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en){ if (list_empty(&en->list)) { IP_VS_ERR("ip_vs_lblc_unhash(): request for not hashed entry, " "called from %p\n", __builtin_return_address(0)); return 0; } /* * Remove it from the table */ write_lock(&tbl->lock); list_del(&en->list); INIT_LIST_HEAD(&en->list); write_unlock(&tbl->lock); return 1;}#endif/* * Get ip_vs_lblc_entry associated with supplied parameters. */static inline struct ip_vs_lblc_entry *ip_vs_lblc_get(struct ip_vs_lblc_table *tbl, __u32 addr){ unsigned hash; struct ip_vs_lblc_entry *en; struct list_head *l,*e; hash = ip_vs_lblc_hashkey(addr); l = &tbl->bucket[hash]; read_lock(&tbl->lock); for (e=l->next; e!=l; e=e->next) { en = list_entry(e, struct ip_vs_lblc_entry, list); if (en->addr == addr) { /* HIT */ read_unlock(&tbl->lock); return en; } } read_unlock(&tbl->lock); return NULL;}/* * Flush all the entries of the specified table. */static void ip_vs_lblc_flush(struct ip_vs_lblc_table *tbl){ int i; struct list_head *l; struct ip_vs_lblc_entry *en; for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) { write_lock(&tbl->lock); for (l=&tbl->bucket[i]; l->next!=l; ) { en = list_entry(l->next, struct ip_vs_lblc_entry, list); ip_vs_lblc_free(en); atomic_dec(&tbl->entries); } write_unlock(&tbl->lock); }}static inline void ip_vs_lblc_full_check(struct ip_vs_lblc_table *tbl){ unsigned long now = jiffies; int i, j; struct list_head *l, *e; struct ip_vs_lblc_entry *en; for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) { j = (j + 1) & IP_VS_LBLC_TAB_MASK; e = l = &tbl->bucket[j]; write_lock(&tbl->lock); while (e->next != l) { en = list_entry(e->next, struct ip_vs_lblc_entry, list); if ((now - en->lastuse) < sysctl_ip_vs_lblc_expiration) { e = e->next; continue; } ip_vs_lblc_free(en); atomic_dec(&tbl->entries); } write_unlock(&tbl->lock);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -