• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/diffserv/ew.h

00001 
00002 /*
00003  * ew.h
00004  * Copyright (C) 1999 by the University of Southern California
00005  * $Id: ew.h,v 1.6 2005/08/25 18:58:03 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 //
00048 // ew.h (Network early warning system)
00049 //   by Xuan Chen (xuanc@isi.edu), USC/ISI
00050 
00051 #ifndef EW_H
00052 #define EW_H
00053 
00054 #include "packet.h"
00055 #include "dsPolicy.h"
00056 
00057 #define EW_MAX_WIN 8
00058 #define EW_SWIN_SIZE 4
00059 // tolerance of changes for detection and release
00060 #define EW_DETECT_RANGE 0.1
00061 #define EW_RELEASE_RANGE 0.1
00062 // the max and min dropping probability
00063 #define EW_MIN_DROP_P 0.1
00064 #define EW_MAX_DROP_P 0.9
00065 #define EW_FLOW_TIME_OUT 10.0
00066 
00067 #define EW_SAMPLE_INTERVAL 240
00068 #define EW_DETECT_INTERVAL 60
00069 #define EW_MIN_SAMPLE_INTERVAL 60
00070 #define EW_MIN_DETECT_INTERVAL 15
00071 
00072 // HLF's alpha
00073 #define ALPHA 0.875
00074 
00075 #define PKT_ARRIVAL 1001
00076 #define PKT_DEPT 1002
00077 #define PKT_DROP 1003
00078 
00079 #define EW_DT_INV 240
00080 #define EW_DB_INV EW_DT_INV
00081 
00082 #define EW_UNCHANGE -1
00083 
00084 // The default tocken-bucket size and tocken rate (in packet/byte)
00085 #define DEFAULT_TB_SIZE 50
00086 #define DEFAULT_TB_RATE_P 5
00087 #define DEFAULT_TB_RATE_B 1000
00088 
00089 // EW
00090 
00091 // Record the number of packet arrived, departured, and dropped
00092 struct PktRec {
00093   int arrival, dept, drop;
00094 };
00095 
00096 // Record the detection result
00097 struct AListEntry {
00098   int src_id;
00099   int dst_id;
00100   int f_id;
00101   double last_update;
00102   double avg_rate;
00103   double t_front;
00104   double win_length;
00105   struct AListEntry *next;
00106 };
00107 
00108 // List of detection result for aggregates
00109 struct AList {
00110   struct AListEntry *head, *tail;
00111   int count;
00112 };
00113 
00114 // Record the request ratio
00115 struct SWinEntry {
00116   float weight;
00117   int rate;
00118   struct SWinEntry *next;
00119 };
00120 
00121 // Data structure for sliding window
00122 struct SWin {
00123   // counter of entries in the sliding window
00124   int count;
00125   // current running average
00126   int ravg;
00127 
00128   // List of SWin Entries
00129   struct SWinEntry *head;
00130   struct SWinEntry *tail;
00131 };
00132 
00133 // Data structure for High-low filter
00134 // high(t) = alpha * high(t-1) + (1 - alpha) * o(t)
00135 // low(t) = (1 - alpha) * low(t-1) + alpha * o(t)
00136 class HLF {
00137  public:
00138   HLF();
00139 
00140   // configure the HLF
00141   void setAlpha(double);
00142   void reset(double value);
00143   void reset();
00144 
00145   // Get the output of HLF
00146   double getHigh();
00147   double getLow();
00148 
00149   // update high-low filter
00150   void update(double);
00151 
00152  private:
00153   double alpha;
00154   
00155   // the estimated value for the high-pass/low-pass filters
00156   double high, low;
00157 };
00158 
00159 // Definition for a token-bucket rate limitor
00160 class TBrateLimitor {
00161  public:
00162   TBrateLimitor();
00163   TBrateLimitor(double);
00164 
00165   // set the rate
00166   void setRate(double);
00167   // adjust the rate
00168   void adjustRate();
00169 
00170   // parameters for a token bucket
00171   // the size of the token bucket (in bytes or packets)
00172   double bucket_size;             
00173   // number of tokens in the bucket (in bytes or packets)
00174   double token_num;
00175   // the rate to generate tokens
00176   double token_rate, ini_token_rate, last_token_rate;
00177   // last time when updating states
00178   double last_time;
00179 
00180   // in packet or byte mode (packet mode, by default)
00181   int pkt_mode;
00182 
00183   int run(double);
00184   int run(double, double); 
00185 
00186   // states to determine the direction of increasing/decreasing rate
00187   int n_score, p_score;
00188 
00189   // High-low filter
00190   HLF hlf;
00191 
00192   // adjust the score for increasing or decreasing scores
00193   void resetScore();
00194   void adjustScore(int);
00195 };
00196 
00197 // Definition for the EW detector
00198 class EWdetector {
00199  public:
00200   EWdetector();
00201   virtual ~EWdetector() {}
00202 
00203   // Enable detecting and debugging rate (eg: pkt or bit rate)
00204   void setDb(int);
00205   void setDt(int);
00206   void setLink(int, int);
00207 
00208   // set and clean alarm
00209   void setAlarm();
00210   void resetAlarm();
00211   int testAlarm();
00212 
00213   void setChange();
00214   void resetChange();
00215   //private:
00216   // The nodes connected by EW
00217   int ew_src, ew_dst;
00218 
00219   // The current time
00220   double now;
00221 
00222   // The timer for detection and debugging
00223   int db_timer, dt_timer;
00224   // Detecting interval
00225   int dt_inv, db_inv;
00226 
00227   // Current rate and long term average
00228   double cur_rate, avg_rate;
00229 
00230   // The alarm flag and proposed dropping probability
00231   int alarm;
00232 
00233   int change;
00234 
00235   // High-low filter
00236   HLF hlf;
00237 
00238   // Detecting engine
00239   void run(Packet *);
00240   // Conduct measurement
00241   virtual void measure(Packet *) = 0;
00242   // Detect changes
00243   virtual void detect() = 0;
00244   // Update current measurement 
00245   virtual void updateCur() = 0;
00246   // Update long term average
00247   void updateAvg();
00248 
00249   // Trace function
00250   virtual void trace() = 0;
00251 
00252 };
00253 
00254 class EWdetectorP : public EWdetector {
00255  public:
00256   EWdetectorP();
00257   virtual ~EWdetectorP();
00258 
00259   // update packet stats
00260   void updateStats(int);
00261   // get the current request rate
00262   double getRate();
00263 
00264  private:
00265   // keep the packet rate stats
00266   PktRec cur_p, last_p, last_p_db;
00267 
00268   // Conduct measurement
00269   void measure(Packet *);
00270   // Detect changes
00271   void detect();
00272   // Update current measurement 
00273   void updateCur();
00274   // Update long term average
00275   void updateAvg();
00276 
00277   // Trace function
00278   void trace();
00279 };
00280 
00281 // EW
00282 class EWdetectorB : public EWdetector {
00283  public:
00284   EWdetectorB();
00285   virtual ~EWdetectorB();
00286 
00287   // Initialize EW
00288   void init(int);
00289 
00290   // Check if the packet belongs to existing flow
00291   int exFlow(Packet *);
00292 
00293  private:
00294   // Adjustor
00295   float adjustor;
00296   float drop_p;
00297   // keep the count of flows for ARR
00298   int arr_count;
00299 
00300   // List to keep the detection results
00301   struct AList alist;
00302   // The sliding window for running average on the aggregated response rate
00303   struct SWin swin;
00304   
00305   // Conduct measurement
00306   void measure(Packet *);
00307   // Update current measurement 
00308   void updateCur();
00309   // Update long term average
00310   void updateAvg();
00311   // Change detection:
00312   // detect the traffic change in aggregated response rate
00313   void detect();
00314   
00315   // Measurement:
00316   // Conduct the measurement and update AList
00317   void updateAList(Packet *);
00318   // Find the max value in AList
00319   struct AListEntry *getMaxAList();
00320   // Sort AList based on the avg_rate
00321   void sortAList();
00322   // Timeout AList entries
00323   void timeoutAList();
00324   // Find the matched AList entry
00325   struct AListEntry * searchAList(int, int, int);
00326   // Add new entry to AList
00327   struct AListEntry * newAListEntry(int, int, int);
00328   // Get the median of AList
00329   int getMedianAList(int, int);
00330   // Get the rate given the index in the list
00331   int getRateAList(int);
00332   // Reset AList
00333   void resetAList();
00334   // Print one entry in AList
00335   void printAListEntry(struct AListEntry *, int);
00336   // output contents in AList
00337   void printAList();
00338 
00339   // Calculate the aggragated response rate for high-bandwidth flows
00340   int computeARR();
00341 
00342   // Determine the dropping probability based on current measurement
00343   void computeDropP();
00344 
00345   // Increase/decrease the sample interval to adjust the detection latency.
00346   void decSInv();
00347   void incSInv();
00348 
00349   // Trace bit rate (resp rate)
00350   void trace();
00351 
00352   // update swin with the latest measurement for one HTab entry.
00353   void updateSWin(int);
00354   // compute the running average on the sliding window
00355   void ravgSWin();
00356   // Reset SWin
00357   void resetSWin();
00358   // output contents in SWin
00359   void printSWin();
00360   // Print one entry in SWin
00361   void printSWinEntry(struct SWinEntry *, int);
00362 };
00363 
00364 // Eearly warning policy: 
00365 //  basically queueing stuffs
00366 class EWPolicy : public Policy {
00367  public:
00368   EWPolicy();
00369   ~EWPolicy();
00370 
00371   void init(int, int, int);
00372   
00373   // Metering and policing methods:
00374   void applyMeter(policyTableEntry *policy, Packet *pkt);
00375   int applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt);
00376 
00377   //  make packet drop decisions
00378   int dropPacket(Packet *);
00379   // detect if there is an alarm triggered
00380   void detect(Packet *pkt);
00381   
00382   // Enable detecting and debugging packet rate (eg: req rate)
00383   void detectPr();
00384   void detectPr(int);
00385   void detectPr(int, int);
00386 
00387   // Enable detecting and debugging bit rate (eg: resp rate)
00388   void detectBr();
00389   void detectBr(int);
00390   void detectBr(int, int);
00391  
00392   // Rate limitor: packet rate
00393   void limitPr();
00394   void limitPr(double);
00395   // Rate limitor: bit rate
00396   void limitBr();
00397   void limitBr(double);
00398 
00399   // couple EW detector
00400   void coupleEW(EWPolicy *);
00401   void coupleEW(EWPolicy *, double);
00402 
00403   //protected:
00404   EWdetectorB *ewB, *cewB;
00405   EWdetectorP *ewP, *cewP;
00406 
00407   TBrateLimitor *rlP, *rlB;
00408   
00409  private:
00410   // The current time
00411   double now;
00412   
00413   // indicator for detecting and debugging
00414   int ew_adj, qsrc, qdst;
00415 
00416   // rate limits
00417   int max_p, max_b;
00418 
00419   // ew alarm
00420   int alarm, pre_alarm;
00421   int change;
00422 };
00423 #endif

Generated on Tue Aug 10 2010 16:16:06 for ns-2.33 by  doxygen 1.7.1