monitor_util.c

Go to the documentation of this file.
00001 /* $Id: monitor_util.c,v 1.13 2001/09/29 16:06:39 jm Exp $
00002  * Monitor utilities module
00003  *
00004  * Dynamic hierarchial IP tunnel
00005  * Copyright (C) 1998-2000, Dynamics group
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License version 2 as
00009  * published by the Free Software Foundation. See README and COPYING for
00010  * more details.
00011  */
00012 
00013 #include <sys/socket.h>
00014 #include <netinet/in.h>
00015 #include <arpa/inet.h>
00016 #include <assert.h>
00017 #include <string.h>
00018 #include <stddef.h>
00019 #include "monitor.h"
00020 #include "debug.h"
00021 #include "mn.h" /* MAX_INTERFACES */
00022 
00023 #define DEBUG_FLAG 'm' /* same as in monitor.c */
00024 
00025 extern struct monitor_global_data mon_conf;
00026 extern struct monitor_interface mon_devs[MAX_INTERFACES];
00027 
00028 
00029 int next(int p)
00030 {
00031         assert(p >= 0);
00032         if (p == 9)
00033                 return 0;
00034         return p + 1;
00035 
00036 }
00037 
00038 
00039 int prev(int p)
00040 {
00041         assert(p >= 0);
00042         if (p == 0)
00043                 return 9;
00044         return p - 1;
00045 }
00046 
00047 
00048 /* return:
00049  *   <0  worst happens to be the current FA :(
00050  *   >0  the released slot
00051  */
00052 int drop_worst(struct timeval *tstamp, struct monitor_interface *dev)
00053 {
00054         int worst_avg = 1000; /* theoretical max quality is below */
00055         int worst_index = -2, i;
00056 
00057         /* (POLICY) */
00058         /* Find old entries */
00059         for (i = 0; i < MAX_MONITOR; i++) {
00060                 /* old ghost. If MAX_TIME has passed
00061                  * for this entry and it still doesn't
00062                  * have decent values, just drop it */
00063                 if (dev->mon[i].added.tv_sec + 
00064                     mon_conf.worst_max_time < tstamp->tv_sec &&
00065                     dev->mon[i].avg == 0)
00066                         return i;
00067         }
00068 
00069         /* No old entries found. Find the worst entry */
00070         for (i = 0; i < MAX_MONITOR; i++) {
00071                 if ((dev->mon[i].added.tv_sec + 
00072                      mon_conf.worst_min_time) > tstamp->tv_sec)
00073                         return -1; /* new entry. Don't update faster than
00074                                     * in MIN_TIME interval */
00075                 if (dev->mon[i].avg < worst_avg) {
00076                         worst_avg = dev->mon[i].avg;
00077                         worst_index = i;
00078                 }
00079         }
00080 
00081         if (worst_index >= 0 && worst_index != mon_conf.current_fa)
00082                 return worst_index;
00083 
00084         /* worst one is current FA :( */
00085         return -1;
00086 }
00087 
00088 
00089 int get_free_slot(struct timeval *tstamp, struct monitor_interface *dev)
00090 {
00091         int i, ph;
00092 
00093         for (i = 0; i < MAX_MONITOR; i++) {
00094                 if (dev->mon[i].active != 1)
00095                         return i;
00096         }
00097 
00098         /* no non-active entries found */
00099         ph = drop_worst(tstamp, dev);
00100         if (ph < 0) {
00101                 DEBUG(DEBUG_FLAG, "get_free_slot: Working set is full, "
00102                       "worst entry just changed. Not dropped.\n");
00103                 return -1;
00104         }
00105 
00106         DEBUG(DEBUG_FLAG, "get_free_slot: Working set is full, "
00107               "worst entry dropped (%s).\n",
00108               inet_ntoa(dev->mon[ph].adv->addr));
00109         return ph;
00110 }
00111 
00112 
00113 struct monitor_interface *monitor_get_dev(char *ifname)
00114 {
00115         int i;
00116 
00117         DEBUG(DEBUG_FLAG, "monitor_get_dev: %s (len %d), interfaces %d\n",
00118               ifname, strlen(ifname), mon_conf.interfaces);
00119         if (ifname == NULL) {
00120                 DEBUG(DEBUG_FLAG, "monitor_get_dev: ifname == NULL!\n");
00121                 return NULL;
00122         }
00123 
00124         for(i = 0; i< MAX_INTERFACES; i++) {
00125                 if (mon_devs[i].in_use && 
00126                     !strncmp(ifname, mon_devs[i].ifname, IFNAMSIZ)) {
00127                         return &mon_devs[i];
00128                 }
00129         }
00130 
00131         DEBUG(DEBUG_FLAG, "monitor_get_dev: interface %s not found\n",
00132               ifname);
00133         return NULL;
00134 }
00135 
00136 
00137 void print_entries(void)
00138 {
00139         int i, j;
00140 
00141         for (j = 0; j < MAX_INTERFACES; j++) {
00142                 if (!mon_devs[j].in_use)
00143                         continue;
00144                 for (i = 0; i < MAX_MONITOR; i++) {
00145                         if (mon_devs[j].mon[i].active == 1) {
00146                                 DEBUG(DEBUG_FLAG, "%-15s: prio %d\n",
00147                                       inet_ntoa(mon_devs[j].mon[i].adv->addr),
00148                                       mon_devs[j].mon[i].adv->priority);
00149                         }
00150                 }
00151         }
00152 }

Generated on Tue Jan 15 08:50:43 2008 for Virtual foreign agent generator version 0.1 by  doxygen 1.5.1