Deleted Added
full compact
alias_db.c (176884) alias_db.c (179480)
1/*-
2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_db.c 176884 2008-03-06 21:50:41Z piso $");
28__FBSDID("$FreeBSD: head/sys/netinet/libalias/alias_db.c 179480 2008-06-01 18:34:58Z mav $");
29
30/*
31 Alias_db.c encapsulates all data structures used for storing
32 packet aliasing data. Other parts of the aliasing software
33 access data through functions provided in this file.
34
35 Data storage is based on the notion of a "link", which is
36 established for ICMP echo/reply packets, UDP datagrams and

--- 138 unchanged lines hidden (view full) ---

175
176
177/*
178 Constants (note: constants are also defined
179 near relevant functions or structs)
180*/
181
182/* Parameters used for cleanup of expired links */
29
30/*
31 Alias_db.c encapsulates all data structures used for storing
32 packet aliasing data. Other parts of the aliasing software
33 access data through functions provided in this file.
34
35 Data storage is based on the notion of a "link", which is
36 established for ICMP echo/reply packets, UDP datagrams and

--- 138 unchanged lines hidden (view full) ---

175
176
177/*
178 Constants (note: constants are also defined
179 near relevant functions or structs)
180*/
181
182/* Parameters used for cleanup of expired links */
183#define ALIAS_CLEANUP_INTERVAL_SECS 60
184#define ALIAS_CLEANUP_MAX_SPOKES 30
183/* NOTE: ALIAS_CLEANUP_INTERVAL_SECS must be less then LINK_TABLE_OUT_SIZE */
184#define ALIAS_CLEANUP_INTERVAL_SECS 64
185#define ALIAS_CLEANUP_MAX_SPOKES (LINK_TABLE_OUT_SIZE/5)
185
186/* Timeouts (in seconds) for different link types */
187#define ICMP_EXPIRE_TIME 60
188#define UDP_EXPIRE_TIME 60
189#define PROTO_EXPIRE_TIME 60
190#define FRAGMENT_ID_EXPIRE_TIME 10
191#define FRAGMENT_PTR_EXPIRE_TIME 30
192

--- 616 unchanged lines hidden (view full) ---

809
810 return (0);
811}
812
813static void
814CleanupAliasData(struct libalias *la)
815{
816 struct alias_link *lnk;
186
187/* Timeouts (in seconds) for different link types */
188#define ICMP_EXPIRE_TIME 60
189#define UDP_EXPIRE_TIME 60
190#define PROTO_EXPIRE_TIME 60
191#define FRAGMENT_ID_EXPIRE_TIME 10
192#define FRAGMENT_PTR_EXPIRE_TIME 30
193

--- 616 unchanged lines hidden (view full) ---

810
811 return (0);
812}
813
814static void
815CleanupAliasData(struct libalias *la)
816{
817 struct alias_link *lnk;
817 int i, icount;
818 int i;
818
819 LIBALIAS_LOCK_ASSERT(la);
819
820 LIBALIAS_LOCK_ASSERT(la);
820 icount = 0;
821 for (i = 0; i < LINK_TABLE_OUT_SIZE; i++) {
821 for (i = 0; i < LINK_TABLE_OUT_SIZE; i++) {
822 lnk = LIST_FIRST(&la->linkTableOut[i]);
823 while (lnk != NULL) {
824 struct alias_link *link_next;
825
826 link_next = LIST_NEXT(lnk, list_out);
827 icount++;
822 while ((lnk = LIST_FIRST(&la->linkTableOut[i])) != NULL)
828 DeleteLink(lnk);
823 DeleteLink(lnk);
829 lnk = link_next;
830 }
831 }
832
833 la->cleanupIndex = 0;
834}
835
836
837static void
838IncrementalCleanup(struct libalias *la)
839{
824 }
825
826 la->cleanupIndex = 0;
827}
828
829
830static void
831IncrementalCleanup(struct libalias *la)
832{
840 int icount;
841 struct alias_link *lnk;
833 struct alias_link *lnk, *lnk_tmp;
842
843 LIBALIAS_LOCK_ASSERT(la);
834
835 LIBALIAS_LOCK_ASSERT(la);
844 icount = 0;
845 lnk = LIST_FIRST(&la->linkTableOut[la->cleanupIndex++]);
846 while (lnk != NULL) {
847 int idelta;
848 struct alias_link *link_next;
849
850 link_next = LIST_NEXT(lnk, list_out);
851 idelta = la->timeStamp - lnk->timestamp;
852 switch (lnk->link_type) {
853 case LINK_TCP:
854 if (idelta > lnk->expire_time) {
855 struct tcp_dat *tcp_aux;
856
857 tcp_aux = lnk->data.tcp;
858 if (tcp_aux->state.in != ALIAS_TCP_STATE_CONNECTED
859 || tcp_aux->state.out != ALIAS_TCP_STATE_CONNECTED) {
860 DeleteLink(lnk);
861 icount++;
862 }
863 }
864 break;
865 default:
866 if (idelta > lnk->expire_time) {
867 DeleteLink(lnk);
868 icount++;
869 }
870 break;
871 }
872 lnk = link_next;
836 LIST_FOREACH_SAFE(lnk, &la->linkTableOut[la->cleanupIndex++],
837 list_out, lnk_tmp) {
838 if (la->timeStamp - lnk->timestamp > lnk->expire_time)
839 DeleteLink(lnk);
873 }
874
875 if (la->cleanupIndex == LINK_TABLE_OUT_SIZE)
876 la->cleanupIndex = 0;
877}
878
879static void
880DeleteLink(struct alias_link *lnk)

--- 251 unchanged lines hidden (view full) ---

1132 int replace_partial_links)
1133{
1134 u_int i;
1135 struct alias_link *lnk;
1136
1137 LIBALIAS_LOCK_ASSERT(la);
1138 i = StartPointOut(src_addr, dst_addr, src_port, dst_port, link_type);
1139 LIST_FOREACH(lnk, &la->linkTableOut[i], list_out) {
840 }
841
842 if (la->cleanupIndex == LINK_TABLE_OUT_SIZE)
843 la->cleanupIndex = 0;
844}
845
846static void
847DeleteLink(struct alias_link *lnk)

--- 251 unchanged lines hidden (view full) ---

1099 int replace_partial_links)
1100{
1101 u_int i;
1102 struct alias_link *lnk;
1103
1104 LIBALIAS_LOCK_ASSERT(la);
1105 i = StartPointOut(src_addr, dst_addr, src_port, dst_port, link_type);
1106 LIST_FOREACH(lnk, &la->linkTableOut[i], list_out) {
1140 if (lnk->src_addr.s_addr == src_addr.s_addr
1141 && lnk->server == NULL
1142 && lnk->dst_addr.s_addr == dst_addr.s_addr
1143 && lnk->dst_port == dst_port
1144 && lnk->src_port == src_port
1145 && lnk->link_type == link_type) {
1107 if (lnk->dst_addr.s_addr == dst_addr.s_addr &&
1108 lnk->src_addr.s_addr == src_addr.s_addr &&
1109 lnk->src_port == src_port &&
1110 lnk->dst_port == dst_port &&
1111 lnk->link_type == link_type &&
1112 lnk->server == NULL) {
1146 lnk->timestamp = la->timeStamp;
1147 break;
1148 }
1149 }
1150
1151/* Search for partially specified links. */
1152 if (lnk == NULL && replace_partial_links) {
1153 if (dst_port != 0 && dst_addr.s_addr != INADDR_ANY) {

--- 1030 unchanged lines hidden (view full) ---

2184 every 60 seconds.
2185
2186 (prototype in alias_local.h)
2187*/
2188
2189void
2190HouseKeeping(struct libalias *la)
2191{
1113 lnk->timestamp = la->timeStamp;
1114 break;
1115 }
1116 }
1117
1118/* Search for partially specified links. */
1119 if (lnk == NULL && replace_partial_links) {
1120 if (dst_port != 0 && dst_addr.s_addr != INADDR_ANY) {

--- 1030 unchanged lines hidden (view full) ---

2151 every 60 seconds.
2152
2153 (prototype in alias_local.h)
2154*/
2155
2156void
2157HouseKeeping(struct libalias *la)
2158{
2192 int i, n, n100;
2159 int i, n;
2193#ifndef _KERNEL
2194 struct timeval tv;
2195 struct timezone tz;
2196#endif
2197
2198 LIBALIAS_LOCK_ASSERT(la);
2199 /*
2200 * Save system time (seconds) in global variable timeStamp for use
2201 * by other functions. This is done so as not to unnecessarily
2202 * waste timeline by making system calls.
2203 */
2204#ifdef _KERNEL
2205 la->timeStamp = time_uptime;
2206#else
2207 gettimeofday(&tv, &tz);
2208 la->timeStamp = tv.tv_sec;
2209#endif
2210
2211 /* Compute number of spokes (output table link chains) to cover */
2160#ifndef _KERNEL
2161 struct timeval tv;
2162 struct timezone tz;
2163#endif
2164
2165 LIBALIAS_LOCK_ASSERT(la);
2166 /*
2167 * Save system time (seconds) in global variable timeStamp for use
2168 * by other functions. This is done so as not to unnecessarily
2169 * waste timeline by making system calls.
2170 */
2171#ifdef _KERNEL
2172 la->timeStamp = time_uptime;
2173#else
2174 gettimeofday(&tv, &tz);
2175 la->timeStamp = tv.tv_sec;
2176#endif
2177
2178 /* Compute number of spokes (output table link chains) to cover */
2212 n100 = LINK_TABLE_OUT_SIZE * 100 + la->houseKeepingResidual;
2213 n100 *= la->timeStamp - la->lastCleanupTime;
2214 n100 /= ALIAS_CLEANUP_INTERVAL_SECS;
2179 n = LINK_TABLE_OUT_SIZE * (la->timeStamp - la->lastCleanupTime);
2180 n /= ALIAS_CLEANUP_INTERVAL_SECS;
2215
2181
2216 n = n100 / 100;
2217
2218 /* Handle different cases */
2182 /* Handle different cases */
2219 if (n > ALIAS_CLEANUP_MAX_SPOKES) {
2220 n = ALIAS_CLEANUP_MAX_SPOKES;
2183 if (n > 0) {
2184 if (n > ALIAS_CLEANUP_MAX_SPOKES)
2185 n = ALIAS_CLEANUP_MAX_SPOKES;
2221 la->lastCleanupTime = la->timeStamp;
2186 la->lastCleanupTime = la->timeStamp;
2222 la->houseKeepingResidual = 0;
2223
2224 for (i = 0; i < n; i++)
2225 IncrementalCleanup(la);
2187 for (i = 0; i < n; i++)
2188 IncrementalCleanup(la);
2226 } else if (n > 0) {
2227 la->lastCleanupTime = la->timeStamp;
2228 la->houseKeepingResidual = n100 - 100 * n;
2229
2230 for (i = 0; i < n; i++)
2231 IncrementalCleanup(la);
2232 } else if (n < 0) {
2233#ifdef LIBALIAS_DEBUG
2234 fprintf(stderr, "PacketAlias/HouseKeeping(): ");
2235 fprintf(stderr, "something unexpected in time values\n");
2236#endif
2237 la->lastCleanupTime = la->timeStamp;
2189 } else if (n < 0) {
2190#ifdef LIBALIAS_DEBUG
2191 fprintf(stderr, "PacketAlias/HouseKeeping(): ");
2192 fprintf(stderr, "something unexpected in time values\n");
2193#endif
2194 la->lastCleanupTime = la->timeStamp;
2238 la->houseKeepingResidual = 0;
2239 }
2240}
2241
2242/* Init the log file and enable logging */
2243static int
2244InitPacketAliasLog(struct libalias *la)
2245{
2246

--- 277 unchanged lines hidden (view full) ---

2524#ifdef _KERNEL
2525 la->timeStamp = time_uptime;
2526 la->lastCleanupTime = time_uptime;
2527#else
2528 gettimeofday(&tv, &tz);
2529 la->timeStamp = tv.tv_sec;
2530 la->lastCleanupTime = tv.tv_sec;
2531#endif
2195 }
2196}
2197
2198/* Init the log file and enable logging */
2199static int
2200InitPacketAliasLog(struct libalias *la)
2201{
2202

--- 277 unchanged lines hidden (view full) ---

2480#ifdef _KERNEL
2481 la->timeStamp = time_uptime;
2482 la->lastCleanupTime = time_uptime;
2483#else
2484 gettimeofday(&tv, &tz);
2485 la->timeStamp = tv.tv_sec;
2486 la->lastCleanupTime = tv.tv_sec;
2487#endif
2532 la->houseKeepingResidual = 0;
2533
2534 for (i = 0; i < LINK_TABLE_OUT_SIZE; i++)
2535 LIST_INIT(&la->linkTableOut[i]);
2536 for (i = 0; i < LINK_TABLE_IN_SIZE; i++)
2537 LIST_INIT(&la->linkTableIn[i]);
2538 LIBALIAS_LOCK_INIT(la);
2539 LIBALIAS_LOCK(la);
2540 } else {

--- 380 unchanged lines hidden ---
2488
2489 for (i = 0; i < LINK_TABLE_OUT_SIZE; i++)
2490 LIST_INIT(&la->linkTableOut[i]);
2491 for (i = 0; i < LINK_TABLE_IN_SIZE; i++)
2492 LIST_INIT(&la->linkTableIn[i]);
2493 LIBALIAS_LOCK_INIT(la);
2494 LIBALIAS_LOCK(la);
2495 } else {

--- 380 unchanged lines hidden ---