00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef lint
00036 static const char rcsid[] =
00037 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/classifier/classifier.cc,v 1.42 2005/09/26 09:12:46 lacage Exp $";
00038 #endif
00039
00040 #include <stdlib.h>
00041 #include "config.h"
00042 #include "classifier.h"
00043 #include "packet.h"
00044
00045 static class ClassifierClass : public TclClass {
00046 public:
00047 ClassifierClass() : TclClass("Classifier") {}
00048 TclObject* create(int, const char*const*) {
00049 return (new Classifier());
00050 }
00051 } class_classifier;
00052
00053
00054 Classifier::Classifier() :
00055 slot_(0), nslot_(0), maxslot_(-1), shift_(0), mask_(0xffffffff), nsize_(0)
00056 {
00057 default_target_ = 0;
00058
00059 bind("offset_", &offset_);
00060 bind("shift_", &shift_);
00061 bind("mask_", &mask_);
00062 }
00063
00064 int Classifier::classify(Packet *p)
00065 {
00066 return (mshift(*((int*) p->access(offset_))));
00067 }
00068
00069 Classifier::~Classifier()
00070 {
00071 delete [] slot_;
00072 }
00073
00074 void Classifier::set_table_size(int nn)
00075 {
00076 nsize_ = nn;
00077 }
00078
00079 void Classifier::alloc(int slot)
00080 {
00081 NsObject** old = slot_;
00082 int n = nslot_;
00083 if (old == 0)
00084 if (nsize_ != 0) {
00085
00086 nslot_ = nsize_;
00087 }
00088 else {
00089
00090 nslot_ = 32;
00091 }
00092 while (nslot_ <= slot)
00093 nslot_ <<= 1;
00094 slot_ = new NsObject*[nslot_];
00095 memset(slot_, 0, nslot_ * sizeof(NsObject*));
00096 for (int i = 0; i < n; ++i)
00097 slot_[i] = old[i];
00098 delete [] old;
00099 }
00100
00101
00102 void Classifier::install(int slot, NsObject* p)
00103 {
00104 if (slot >= nslot_)
00105 alloc(slot);
00106 slot_[slot] = p;
00107 if (slot >= maxslot_)
00108 maxslot_ = slot;
00109 }
00110
00111 void Classifier::clear(int slot)
00112 {
00113 slot_[slot] = 0;
00114 if (slot == maxslot_) {
00115 while (--maxslot_ >= 0 && slot_[maxslot_] == 0)
00116 ;
00117 }
00118 }
00119
00120 int Classifier::allocPort (NsObject *nullagent)
00121 {
00122 return getnxt (nullagent);
00123 }
00124
00125 int Classifier::getnxt(NsObject *nullagent)
00126 {
00127 int i;
00128 for (i=0; i < nslot_; i++)
00129 if (slot_[i]==0 || slot_[i]==nullagent)
00130 return i;
00131 i=nslot_;
00132 alloc(nslot_);
00133 return i;
00134 }
00135
00136
00137
00138
00139
00140 void Classifier::recv(Packet* p, Handler*h)
00141 {
00142 NsObject* node = find(p);
00143 if (node == NULL) {
00144
00145
00146
00147
00148 Packet::free(p);
00149 return;
00150 }
00151 node->recv(p,h);
00152 }
00153
00154
00155
00156
00157
00158
00159 NsObject* Classifier::find(Packet* p)
00160 {
00161 NsObject* node = NULL;
00162 int cl = classify(p);
00163 if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0) {
00164 if (default_target_)
00165 return default_target_;
00166
00167
00168
00169
00170 Tcl::instance().evalf("%s no-slot %ld", name(), cl);
00171 if (cl == TWICE) {
00172
00173
00174
00175 cl = classify(p);
00176 if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0)
00177 return (NULL);
00178 }
00179 }
00180 return (node);
00181 }
00182
00183 int Classifier::install_next(NsObject *node) {
00184 int slot = maxslot_ + 1;
00185 install(slot, node);
00186 return (slot);
00187 }
00188
00189 int Classifier::command(int argc, const char*const* argv)
00190 {
00191 Tcl& tcl = Tcl::instance();
00192 if(argc == 2) {
00193 if (strcmp(argv[1], "defaulttarget") == 0) {
00194 if (default_target_ != 0)
00195 tcl.result(default_target_->name());
00196 return (TCL_OK);
00197 }
00198 } else if (argc == 3) {
00199
00200
00201
00202 if (strcmp(argv[1],"alloc-port") == 0) {
00203 int slot;
00204 NsObject* nullagent =
00205 (NsObject*)TclObject::lookup(argv[2]);
00206 slot = getnxt(nullagent);
00207 tcl.resultf("%u",slot);
00208 return(TCL_OK);
00209 }
00210
00211
00212
00213 if (strcmp(argv[1], "clear") == 0) {
00214 int slot = atoi(argv[2]);
00215 clear(slot);
00216 return (TCL_OK);
00217 }
00218
00219
00220
00221 if (strcmp(argv[1], "installNext") == 0) {
00222
00223 NsObject* node = (NsObject*)TclObject::lookup(argv[2]);
00224 if (node == NULL) {
00225 tcl.resultf("Classifier::installNext attempt "
00226 "to install non-object %s into classifier", argv[2]);
00227 return TCL_ERROR;
00228 };
00229 int slot = install_next(node);
00230 tcl.resultf("%u", slot);
00231 return TCL_OK;
00232 }
00233
00234
00235
00236
00237 if (strcmp(argv[1], "slot") == 0) {
00238 int slot = atoi(argv[2]);
00239 if (slot >= 0 && slot < nslot_ && slot_[slot] != NULL) {
00240 tcl.resultf("%s", slot_[slot]->name());
00241 return TCL_OK;
00242 }
00243 tcl.resultf("Classifier: no object at slot %d", slot);
00244 return (TCL_ERROR);
00245 }
00246
00247
00248
00249
00250 if (strcmp(argv[1], "findslot") == 0) {
00251 int slot = 0;
00252 NsObject* node = (NsObject*)TclObject::lookup(argv[2]);
00253 if (node == NULL) {
00254 return (TCL_ERROR);
00255 }
00256 while (slot < nslot_) {
00257
00258
00259
00260 if (slot_[slot] &&
00261 strcmp(slot_[slot]->name(), argv[2]) == 0){
00262 tcl.resultf("%u", slot);
00263 return (TCL_OK);
00264 }
00265 slot++;
00266 }
00267 tcl.result("-1");
00268 return (TCL_OK);
00269 }
00270 if (strcmp(argv[1], "defaulttarget") == 0) {
00271 default_target_=(NsObject*)TclObject::lookup(argv[2]);
00272 if (default_target_ == 0)
00273 return TCL_ERROR;
00274 return TCL_OK;
00275 }
00276 } else if (argc == 4) {
00277
00278
00279
00280 if (strcmp(argv[1], "install") == 0) {
00281 int slot = atoi(argv[2]);
00282 NsObject* node = (NsObject*)TclObject::lookup(argv[3]);
00283 install(slot, node);
00284 return (TCL_OK);
00285 }
00286 }
00287 return (NsObject::command(argc, argv));
00288 }