classifier-hash.h

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  * $Header: /nfs/jade/vint/CVSROOT/ns-2/classifier/classifier-hash.h,v 1.9 2005/09/18 23:33:31 tomh Exp $
00035  */
00036 
00037 #include "classifier.h"
00038 #include "ip.h"
00039 
00040 class Flow;
00041 
00042 /* class defs for HashClassifier (base), SrcDest, SrcDestFid HashClassifiers */
00043 class HashClassifier : public Classifier {
00044 public:
00045     HashClassifier(int keylen) : default_(-1), keylen_(keylen) {
00046         // shift + mask picked up from underlying Classifier object
00047         bind("default_", &default_);
00048         Tcl_InitHashTable(&ht_, keylen);
00049     }       
00050     ~HashClassifier() {
00051         Tcl_DeleteHashTable(&ht_);
00052     };
00053     virtual int classify(Packet *p);
00054     virtual long lookup(Packet* p) {
00055         hdr_ip* h = hdr_ip::access(p);
00056         return get_hash(mshift(h->saddr()), mshift(h->daddr()), 
00057                 h->flowid());
00058     }
00059     virtual long unknown(Packet* p) {
00060         hdr_ip* h = hdr_ip::access(p);
00061         Tcl::instance().evalf("%s unknown-flow %u %u %u",
00062                       name(), h->saddr(), h->daddr(),
00063                       h->flowid()); 
00064         return lookup(p);
00065     };
00066     void set_default(int slot) { default_ = slot; } 
00067     int do_set_hash(nsaddr_t src, nsaddr_t dst, int fid, int slot) {
00068         return (set_hash(src,dst,fid,slot));
00069     }
00070     void set_table_size(int nn) {}
00071 protected:
00072     union hkey {
00073         struct {
00074             int fid;
00075         } Fid;
00076         struct {
00077             nsaddr_t dst;
00078         } Dst;
00079         struct {
00080             nsaddr_t src, dst;
00081         } SrcDst;
00082         struct {
00083             nsaddr_t src, dst;
00084             int fid;
00085         } SrcDstFid;
00086     };
00087         
00088     long lookup(nsaddr_t src, nsaddr_t dst, int fid) {
00089         return get_hash(src, dst, fid);
00090     }
00091     int newflow(Packet* pkt) {
00092         hdr_ip* h = hdr_ip::access(pkt);
00093         Tcl::instance().evalf("%s unknown-flow %u %u %u",
00094                       name(), h->saddr(), h->daddr(),
00095                       h->flowid()); 
00096         return lookup(pkt);
00097     };
00098     void reset() {
00099         Tcl_DeleteHashTable(&ht_);
00100         Tcl_InitHashTable(&ht_, keylen_);
00101     }
00102 
00103     virtual const char* hashkey(nsaddr_t, nsaddr_t, int)=0; 
00104 
00105     int set_hash(nsaddr_t src, nsaddr_t dst, int fid, long slot) {
00106         int newEntry;
00107         Tcl_HashEntry *ep= Tcl_CreateHashEntry(&ht_,
00108                                hashkey(src, dst, fid),
00109                                &newEntry); 
00110         if (ep) {
00111             Tcl_SetHashValue(ep, slot);
00112             return slot;
00113         }
00114         return -1;
00115     }
00116     long get_hash(nsaddr_t src, nsaddr_t dst, int fid) {
00117         Tcl_HashEntry *ep= Tcl_FindHashEntry(&ht_, 
00118                              hashkey(src, dst, fid)); 
00119         if (ep)
00120             return (long)Tcl_GetHashValue(ep);
00121         return -1;
00122     }
00123     
00124     virtual int command(int argc, const char*const* argv);
00125 
00126 
00127     int default_;
00128     Tcl_HashTable ht_;
00129     hkey buf_;
00130     int keylen_;
00131 };
00132 
00133 class SrcDestFidHashClassifier : public HashClassifier {
00134 public:
00135     SrcDestFidHashClassifier() : HashClassifier(3) {
00136     }
00137 protected:
00138     const char* hashkey(nsaddr_t src, nsaddr_t dst, int fid) {
00139         buf_.SrcDstFid.src= mshift(src);
00140         buf_.SrcDstFid.dst= mshift(dst);
00141         buf_.SrcDstFid.fid= fid;
00142         return (const char*) &buf_;
00143     }
00144 };
00145 
00146 class SrcDestHashClassifier : public HashClassifier {
00147 public:
00148     SrcDestHashClassifier() : HashClassifier(2) {
00149     int command(int argc, const char*const* argv);
00150     int classify(Packet *p);
00151     }
00152 protected:
00153     const char*  hashkey(nsaddr_t src, nsaddr_t dst, int) {
00154         buf_.SrcDst.src= mshift(src);
00155         buf_.SrcDst.dst= mshift(dst);
00156         return (const char*) &buf_;
00157     }
00158 };
00159 
00160 class FidHashClassifier : public HashClassifier {
00161 public:
00162     FidHashClassifier() : HashClassifier(TCL_ONE_WORD_KEYS) {
00163     }
00164 protected:
00165     const char* hashkey(nsaddr_t, nsaddr_t, int fid) {
00166         long key = fid;
00167         return (const char*) key;
00168     }
00169 };
00170 
00171 class DestHashClassifier : public HashClassifier {
00172 public:
00173     DestHashClassifier() : HashClassifier(TCL_ONE_WORD_KEYS) {}
00174     virtual int command(int argc, const char*const* argv);
00175     int classify(Packet *p);
00176     virtual void do_install(char *dst, NsObject *target);
00177 protected:
00178     const char* hashkey(nsaddr_t, nsaddr_t dst, int) {
00179         long key = mshift(dst);
00180         return (const char*) key;
00181     }
00182 };
00183 

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