classifier-hash.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) 1997 The 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 Network Research
00017  *  Group at Lawrence Berkeley National Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    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-hash.cc,v 1.30 2005/09/18 23:33:31 tomh Exp $ (LBL)";
00038 #endif
00039 
00040 //
00041 // a generalized classifier for mapping (src/dest/flowid) fields
00042 // to a bucket.  "buckets_" worth of hash table entries are created
00043 // at init time, and other entries in the same bucket are created when
00044 // needed
00045 //
00046 //
00047 
00048 extern "C" {
00049 #include <tcl.h>
00050 }
00051 
00052 #include <stdlib.h>
00053 #include "config.h"
00054 #include "packet.h"
00055 #include "ip.h"
00056 #include "classifier.h"
00057 #include "classifier-hash.h"
00058 
00059 /****************** HashClassifier Methods ************/
00060 
00061 int HashClassifier::classify(Packet * p) {
00062     int slot= lookup(p);
00063     if (slot >= 0 && slot <=maxslot_)
00064         return (slot);
00065     else if (default_ >= 0)
00066         return (default_);
00067     return (unknown(p));
00068 } // HashClassifier::classify
00069 
00070 int HashClassifier::command(int argc, const char*const* argv)
00071 {
00072     Tcl& tcl = Tcl::instance();
00073     /*
00074      * $classifier set-hash $hashbucket src dst fid $slot
00075      */
00076 
00077     if (argc == 7) {
00078         if (strcmp(argv[1], "set-hash") == 0) {
00079             //xxx: argv[2] is ignored for now
00080             nsaddr_t src = atoi(argv[3]);
00081             nsaddr_t dst = atoi(argv[4]);
00082             int fid = atoi(argv[5]);
00083             int slot = atoi(argv[6]);
00084             if (0 > set_hash(src, dst, fid, slot))
00085                 return TCL_ERROR;
00086             return TCL_OK;
00087         }
00088     } else if (argc == 6) {
00089         /* $classifier lookup $hashbuck $src $dst $fid */
00090         if (strcmp(argv[1], "lookup") == 0) {
00091             nsaddr_t src = atoi(argv[3]);
00092             nsaddr_t dst = atoi(argv[4]);
00093             int fid = atoi(argv[5]);
00094             int slot= get_hash(src, dst, fid);
00095             if (slot>=0 && slot <=maxslot_) {
00096                 tcl.resultf("%s", slot_[slot]->name());
00097                 return (TCL_OK);
00098             }
00099             tcl.resultf("");
00100             return (TCL_OK);
00101         }
00102                 // Added by Yun Wang to set rate for TBFlow or TSWFlow
00103                 if (strcmp(argv[1], "set-flowrate") == 0) {
00104                         int fid = atoi(argv[2]);
00105                         nsaddr_t src = 0;  // only use fid
00106                         nsaddr_t dst = 0;  // to classify flows
00107                         int slot = get_hash( src, dst, fid );
00108                         if ( slot >= 0 && slot <= maxslot_ ) {
00109                                 Flow* f = (Flow*)slot_[slot];
00110                                 tcl.evalf("%u set target_rate_ %s",
00111                                         f, argv[3]);
00112                                 tcl.evalf("%u set bucket_depth_ %s",
00113                                         f, argv[4]);
00114                                 tcl.evalf("%u set tbucket_ %s",
00115                                         f, argv[5]);
00116                                 return (TCL_OK);
00117                         }
00118                         else {
00119                           tcl.evalf("%s set-rate %u %u %u %u %s %s %s",
00120                           name(), src, dst, fid, slot, argv[3], argv[4],argv[5])
00121 ;
00122                           return (TCL_OK);
00123                         }
00124                 }  
00125     } else if (argc == 5) {
00126         /* $classifier del-hash src dst fid */
00127         if (strcmp(argv[1], "del-hash") == 0) {
00128             nsaddr_t src = atoi(argv[2]);
00129             nsaddr_t dst = atoi(argv[3]);
00130             int fid = atoi(argv[4]);
00131             
00132             Tcl_HashEntry *ep= Tcl_FindHashEntry(&ht_, 
00133                                  hashkey(src, dst,
00134                                      fid)); 
00135             if (ep) {
00136                 long slot = (long)Tcl_GetHashValue(ep);
00137                 Tcl_DeleteHashEntry(ep);
00138                 tcl.resultf("%lu", slot);
00139                 return (TCL_OK);
00140             }
00141             return (TCL_ERROR);
00142         }
00143     }
00144     return (Classifier::command(argc, argv));
00145 }
00146 
00147 /**************  TCL linkage ****************/
00148 static class SrcDestHashClassifierClass : public TclClass {
00149 public:
00150     SrcDestHashClassifierClass() : TclClass("Classifier/Hash/SrcDest") {}
00151     TclObject* create(int, const char*const*) {
00152         return new SrcDestHashClassifier;
00153     }
00154 } class_hash_srcdest_classifier;
00155 
00156 static class FidHashClassifierClass : public TclClass {
00157 public:
00158     FidHashClassifierClass() : TclClass("Classifier/Hash/Fid") {}
00159     TclObject* create(int, const char*const*) {
00160         return new FidHashClassifier;
00161     }
00162 } class_hash_fid_classifier;
00163 
00164 static class DestHashClassifierClass : public TclClass {
00165 public:
00166     DestHashClassifierClass() : TclClass("Classifier/Hash/Dest") {}
00167     TclObject* create(int, const char*const*) {
00168         return new DestHashClassifier;
00169     }
00170 } class_hash_dest_classifier;
00171 
00172 static class SrcDestFidHashClassifierClass : public TclClass {
00173 public:
00174     SrcDestFidHashClassifierClass() : TclClass("Classifier/Hash/SrcDestFid") {}
00175     TclObject* create(int, const char*const*) {
00176         return new SrcDestFidHashClassifier;
00177     }
00178 } class_hash_srcdestfid_classifier;
00179 
00180 
00181 // DestHashClassifier methods
00182 int DestHashClassifier::classify(Packet *p)
00183 {
00184     int slot= lookup(p);
00185     if (slot >= 0 && slot <=maxslot_)
00186         return (slot);
00187     else if (default_ >= 0)
00188         return (default_);
00189     return -1;
00190 } // HashClassifier::classify
00191 
00192 void DestHashClassifier::do_install(char* dst, NsObject *target) {
00193     nsaddr_t d = atoi(dst);
00194     int slot = getnxt(target);
00195     install(slot, target); 
00196     if (set_hash(0, d, 0, slot) < 0)
00197         fprintf(stderr, "DestHashClassifier::set_hash from within DestHashClassifier::do_install returned value < 0");
00198 }
00199 
00200 int DestHashClassifier::command(int argc, const char*const* argv)
00201 {
00202     if (argc == 4) {
00203         // $classifier install $dst $node
00204         if (strcmp(argv[1], "install") == 0) {
00205             char dst[SMALL_LEN];
00206             strcpy(dst, argv[2]);
00207             NsObject *node = (NsObject*)TclObject::lookup(argv[3]);
00208             //nsaddr_t dst = atoi(argv[2]);
00209             do_install(dst, node); 
00210             return TCL_OK;
00211             //int slot = getnxt(node);
00212             //install(slot, node);
00213             //if (set_hash(0, dst, 0, slot) >= 0)
00214             //return TCL_OK;
00215             //else
00216             //return TCL_ERROR;
00217         } // if
00218     }
00219     return(HashClassifier::command(argc, argv));
00220 } // command
00221 
00222 
00223 
00224 
00225 

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