classifier-hash.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (c) 1997 The Regents of the University of California.
  4.  * All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *  This product includes software developed by the Network Research
  17.  *  Group at Lawrence Berkeley National Laboratory.
  18.  * 4. Neither the name of the University nor of the Laboratory may be used
  19.  *    to endorse or promote products derived from this software without
  20.  *    specific prior written permission.
  21.  * 
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34. #ifndef lint
  35. static const char rcsid[] =
  36.     "@(#) $Header: /cvsroot/nsnam/ns-2/classifier/classifier-hash.cc,v 1.30 2005/09/18 23:33:31 tomh Exp $ (LBL)";
  37. #endif
  38. //
  39. // a generalized classifier for mapping (src/dest/flowid) fields
  40. // to a bucket.  "buckets_" worth of hash table entries are created
  41. // at init time, and other entries in the same bucket are created when
  42. // needed
  43. //
  44. //
  45. extern "C" {
  46. #include <tcl.h>
  47. }
  48. #include <stdlib.h>
  49. #include "config.h"
  50. #include "packet.h"
  51. #include "ip.h"
  52. #include "classifier.h"
  53. #include "classifier-hash.h"
  54. /****************** HashClassifier Methods ************/
  55. int HashClassifier::classify(Packet * p) {
  56. int slot= lookup(p);
  57. if (slot >= 0 && slot <=maxslot_)
  58. return (slot);
  59. else if (default_ >= 0)
  60. return (default_);
  61. return (unknown(p));
  62. } // HashClassifier::classify
  63. int HashClassifier::command(int argc, const char*const* argv)
  64. {
  65. Tcl& tcl = Tcl::instance();
  66. /*
  67.  * $classifier set-hash $hashbucket src dst fid $slot
  68.  */
  69. if (argc == 7) {
  70. if (strcmp(argv[1], "set-hash") == 0) {
  71. //xxx: argv[2] is ignored for now
  72. nsaddr_t src = atoi(argv[3]);
  73. nsaddr_t dst = atoi(argv[4]);
  74. int fid = atoi(argv[5]);
  75. int slot = atoi(argv[6]);
  76. if (0 > set_hash(src, dst, fid, slot))
  77. return TCL_ERROR;
  78. return TCL_OK;
  79. }
  80. } else if (argc == 6) {
  81. /* $classifier lookup $hashbuck $src $dst $fid */
  82. if (strcmp(argv[1], "lookup") == 0) {
  83. nsaddr_t src = atoi(argv[3]);
  84. nsaddr_t dst = atoi(argv[4]);
  85. int fid = atoi(argv[5]);
  86. int slot= get_hash(src, dst, fid);
  87. if (slot>=0 && slot <=maxslot_) {
  88. tcl.resultf("%s", slot_[slot]->name());
  89. return (TCL_OK);
  90. }
  91. tcl.resultf("");
  92. return (TCL_OK);
  93. }
  94.                 // Added by Yun Wang to set rate for TBFlow or TSWFlow
  95.                 if (strcmp(argv[1], "set-flowrate") == 0) {
  96.                         int fid = atoi(argv[2]);
  97.                         nsaddr_t src = 0;  // only use fid
  98.                         nsaddr_t dst = 0;  // to classify flows
  99.                         int slot = get_hash( src, dst, fid );
  100.                         if ( slot >= 0 && slot <= maxslot_ ) {
  101.                                 Flow* f = (Flow*)slot_[slot];
  102.                                 tcl.evalf("%u set target_rate_ %s",
  103.                                         f, argv[3]);
  104.                                 tcl.evalf("%u set bucket_depth_ %s",
  105.                                         f, argv[4]);
  106.                                 tcl.evalf("%u set tbucket_ %s",
  107.                                         f, argv[5]);
  108.                                 return (TCL_OK);
  109.                         }
  110.                         else {
  111.                           tcl.evalf("%s set-rate %u %u %u %u %s %s %s",
  112.                           name(), src, dst, fid, slot, argv[3], argv[4],argv[5])
  113. ;
  114.                           return (TCL_OK);
  115.                         }
  116.                 }  
  117. } else if (argc == 5) {
  118. /* $classifier del-hash src dst fid */
  119. if (strcmp(argv[1], "del-hash") == 0) {
  120. nsaddr_t src = atoi(argv[2]);
  121. nsaddr_t dst = atoi(argv[3]);
  122. int fid = atoi(argv[4]);
  123. Tcl_HashEntry *ep= Tcl_FindHashEntry(&ht_, 
  124.      hashkey(src, dst,
  125.      fid)); 
  126. if (ep) {
  127. long slot = (long)Tcl_GetHashValue(ep);
  128. Tcl_DeleteHashEntry(ep);
  129. tcl.resultf("%lu", slot);
  130. return (TCL_OK);
  131. }
  132. return (TCL_ERROR);
  133. }
  134. }
  135. return (Classifier::command(argc, argv));
  136. }
  137. /**************  TCL linkage ****************/
  138. static class SrcDestHashClassifierClass : public TclClass {
  139. public:
  140. SrcDestHashClassifierClass() : TclClass("Classifier/Hash/SrcDest") {}
  141. TclObject* create(int, const char*const*) {
  142. return new SrcDestHashClassifier;
  143. }
  144. } class_hash_srcdest_classifier;
  145. static class FidHashClassifierClass : public TclClass {
  146. public:
  147. FidHashClassifierClass() : TclClass("Classifier/Hash/Fid") {}
  148. TclObject* create(int, const char*const*) {
  149. return new FidHashClassifier;
  150. }
  151. } class_hash_fid_classifier;
  152. static class DestHashClassifierClass : public TclClass {
  153. public:
  154. DestHashClassifierClass() : TclClass("Classifier/Hash/Dest") {}
  155. TclObject* create(int, const char*const*) {
  156. return new DestHashClassifier;
  157. }
  158. } class_hash_dest_classifier;
  159. static class SrcDestFidHashClassifierClass : public TclClass {
  160. public:
  161. SrcDestFidHashClassifierClass() : TclClass("Classifier/Hash/SrcDestFid") {}
  162. TclObject* create(int, const char*const*) {
  163. return new SrcDestFidHashClassifier;
  164. }
  165. } class_hash_srcdestfid_classifier;
  166. // DestHashClassifier methods
  167. int DestHashClassifier::classify(Packet *p)
  168. {
  169. int slot= lookup(p);
  170. if (slot >= 0 && slot <=maxslot_)
  171. return (slot);
  172. else if (default_ >= 0)
  173. return (default_);
  174. return -1;
  175. } // HashClassifier::classify
  176. void DestHashClassifier::do_install(char* dst, NsObject *target) {
  177. nsaddr_t d = atoi(dst);
  178. int slot = getnxt(target);
  179. install(slot, target); 
  180. if (set_hash(0, d, 0, slot) < 0)
  181. fprintf(stderr, "DestHashClassifier::set_hash from within DestHashClassifier::do_install returned value < 0");
  182. }
  183. int DestHashClassifier::command(int argc, const char*const* argv)
  184. {
  185. if (argc == 4) {
  186. // $classifier install $dst $node
  187. if (strcmp(argv[1], "install") == 0) {
  188. char dst[SMALL_LEN];
  189. strcpy(dst, argv[2]);
  190. NsObject *node = (NsObject*)TclObject::lookup(argv[3]);
  191. //nsaddr_t dst = atoi(argv[2]);
  192. do_install(dst, node); 
  193. return TCL_OK;
  194. //int slot = getnxt(node);
  195. //install(slot, node);
  196. //if (set_hash(0, dst, 0, slot) >= 0)
  197. //return TCL_OK;
  198. //else
  199. //return TCL_ERROR;
  200. } // if
  201. }
  202. return(HashClassifier::command(argc, argv));
  203. } // command