00001 00002 /* 00003 * dewp.cc 00004 * Copyright (C) 1999 by the University of Southern California 00005 * $Id: dewp.cc,v 1.2 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 // dewp.h (Early worm propagation detection) 00049 // by Xuan Chen (xuanc@isi.edu), USC/ISI 00050 00051 #include "ip.h" 00052 #include "tcp.h" 00053 #include "tcp-full.h" 00054 #include "random.h" 00055 00056 #include "dewp.h" 00057 BPEntry *DEWPPolicy::bport_list = NULL; 00058 // Initialize parameters 00059 double DEWPPolicy::dt_inv_ = DT_INV; 00060 double DEWPPolicy::beta = 0.5; 00061 double DEWPPolicy::alpha = 0.125; 00062 // num per second 00063 int DEWPPolicy::anum_th = 50; 00064 00065 // EW Policy: deal with queueing stuffs. 00066 //Constructor. 00067 DEWPPolicy::DEWPPolicy() : Policy() { 00068 // Initialize detectors 00069 cdewp = NULL; 00070 00071 bport_list = new BPEntry[P_LEN]; 00072 for (int i = 0; i < P_LEN; i++) { 00073 dport_list[i] = 0; 00074 bport_list[i].s = bport_list[i].b = 0; 00075 bport_list[i].anum = bport_list[i].last_anum = bport_list[i].avg_anum = 0; 00076 bport_list[i].last_time = 0; 00077 bport_list[i].aset = NULL; 00078 } 00079 } 00080 00081 //Deconstructor. 00082 DEWPPolicy::~DEWPPolicy(){ 00083 if (cdewp) 00084 free(cdewp); 00085 00086 for (int i = 0; i < P_LEN; i++) { 00087 if (bport_list[i].aset) { 00088 AddrEntry * p = bport_list[i].aset; 00089 AddrEntry * q; 00090 while (p) { 00091 q = p->next; 00092 free(p); 00093 p = q; 00094 } 00095 } 00096 } 00097 } 00098 00099 // Initialize the DEWP parameters 00100 void DEWPPolicy::init(double dt_inv) { 00101 dt_inv_ = dt_inv; 00102 //printf("%f\n", dt_inv_); 00103 } 00104 00105 // DEWP meter: do nothing. 00106 // measurement is done in policer: we need to know whether the packet is 00107 // dropped or not. 00108 void DEWPPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) { 00109 hdr_ip* iph = hdr_ip::access(pkt); 00110 00111 dport_list[iph->dport()] = Scheduler::instance().clock(); 00112 00113 return; 00114 } 00115 00116 // DEWP Policer 00117 // 1. do measurement: P: both arrival and departure; B: only departure 00118 // 2. make packet drop decisions 00119 int DEWPPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt) { 00120 //printf("enter applyPolicer "); 00121 00122 // can't count/penalize ACKs: 00123 // with resp: may cause inaccurate calculation with TSW(??) 00124 // with req: may cause resp retransmission. 00125 // just pass them through 00126 hdr_ip* iph = hdr_ip::access(pkt); 00127 detect(pkt); 00128 if (bport_list[iph->dport()].b) { 00129 //printf("downgrade!\n"); 00130 return(policer->downgrade1); 00131 } else { 00132 //printf("initial!\n"); 00133 return(policer->initialCodePt); 00134 } 00135 } 00136 00137 // detect if there is alarm triggered 00138 void DEWPPolicy::detect(Packet *pkt) { 00139 // it is not for outbound traffic 00140 if (!cdewp) 00141 return; 00142 00143 // get the current time 00144 now = Scheduler::instance().clock(); 00145 00146 // get IP header 00147 hdr_ip* iph = hdr_ip::access(pkt); 00148 int dport = iph->dport(); 00149 unsigned int daddr = iph->daddr(); 00150 00151 // use dport matching to find suspects 00152 if (dport_list[dport] > 0 && (cdewp->dport_list[dport]) > 0 && 00153 now - dport_list[dport]< dt_inv_ && 00154 now - cdewp->dport_list[dport] < dt_inv_) { 00155 if (bport_list[dport].s == 0) { 00156 printf("S %.2f %d\n", now, dport); 00157 bport_list[dport].s = 1; 00158 } 00159 } 00160 00161 // count outbound traffic only 00162 if (bport_list[dport].s == 1 && daddr > 0) { 00163 AddrEntry * p = bport_list[dport].aset; 00164 00165 while (p) { 00166 if (p->addr == daddr) { 00167 break; 00168 } 00169 p = p->next; 00170 } 00171 00172 if (!p) { 00173 AddrEntry * new_addr = new AddrEntry; 00174 00175 new_addr->addr = daddr; 00176 new_addr->next = NULL; 00177 00178 if (bport_list[dport].aset) { 00179 new_addr->next = bport_list[dport].aset; 00180 }; 00181 bport_list[dport].aset = new_addr; 00182 } 00183 bport_list[dport].anum++; 00184 00185 /* debug purpose only 00186 p = bport_list[dport].aset; 00187 printf("[%d] ", dport); 00188 while (p) { 00189 printf("%u ", p->addr); 00190 p = p->next; 00191 } 00192 printf("\n"); 00193 */ 00194 00195 if (now - bport_list[dport].last_time > dt_inv_) { 00196 //printf("DT %f %d %d %d\n", now, dport, bport_list[dport].anum, bport_list[dport].avg_anum); 00197 if (bport_list[dport].anum > anum_th * dt_inv_ && 00198 bport_list[dport].avg_anum > 0 && 00199 bport_list[dport].anum > (1 + beta) * bport_list[dport].avg_anum) { 00200 if (bport_list[dport].b == 0) { 00201 bport_list[dport].b = 1; 00202 printf("B %.2f %d %d %d\n", now, dport, bport_list[dport].anum, bport_list[dport].avg_anum); 00203 } 00204 } 00205 00206 bport_list[dport].avg_anum = (int) (alpha * bport_list[dport].avg_anum + 00207 (1 - alpha) * bport_list[dport].anum); 00208 if (bport_list[dport].avg_anum == 0 && bport_list[dport].anum > 0) 00209 bport_list[dport].avg_anum = bport_list[dport].anum; 00210 bport_list[dport].last_anum = bport_list[dport].anum; 00211 bport_list[dport].anum = 0; 00212 bport_list[dport].last_time = now; 00213 } 00214 00215 } 00216 00217 } 00218 00219 // make packet drop decisions 00220 int DEWPPolicy::dropPacket(Packet *pkt) { 00221 00222 return(0); 00223 } 00224 00225 // couple DEWP detector 00226 void DEWPPolicy::couple(DEWPPolicy *ewpc) { 00227 cdewp = ewpc; 00228 } 00229 00230 // End of DEWP
1.4.6