classifier.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1996 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
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             //printf("classifier %x set to %d....%dth visit\n", this, nsize_, i++);
00086             nslot_ = nsize_;
00087         }
00088         else {
00089             //printf("classifier %x set to 32....%dth visit\n", this, j++);
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  * objects only ever see "packet" events, which come either
00138  * from an incoming link or a local agent (i.e., packet source).
00139  */
00140 void Classifier::recv(Packet* p, Handler*h)
00141 {
00142     NsObject* node = find(p);
00143     if (node == NULL) {
00144         /*
00145          * XXX this should be "dropped" somehow.  Right now,
00146          * these events aren't traced.
00147          */
00148         Packet::free(p);
00149         return;
00150     }
00151     node->recv(p,h);
00152 }
00153 
00154 /*
00155  * perform the mapping from packet to object
00156  * perform upcall if no mapping
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          * Sigh.  Can't pass the pkt out to tcl because it's
00168          * not an object.
00169          */
00170         Tcl::instance().evalf("%s no-slot %ld", name(), cl);
00171         if (cl == TWICE) {
00172             /*
00173              * Try again.  Maybe callback patched up the table.
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          * $classifier alloc-port nullagent
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          * $classifier clear $slot
00212          */
00213         if (strcmp(argv[1], "clear") == 0) {
00214             int slot = atoi(argv[2]);
00215             clear(slot);
00216             return (TCL_OK);
00217         }
00218         /*
00219          * $classifier installNext $node
00220          */
00221         if (strcmp(argv[1], "installNext") == 0) {
00222             //int slot = maxslot_ + 1;
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          * $classifier slot snum
00235          * returns the name of the object in slot # snum
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          * $classifier findslot $node
00248          * finds the slot containing $node
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                 // check if the slot is empty (xuanc, 1/14/02) 
00258                 // fix contributed by Frank A. Zdarsky 
00259                 // <frank.zdarsky@kom.tu-darmstadt.de>
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          * $classifier install $slot $node
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 }

Generated on Tue Mar 6 16:47:43 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6