1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* Copyright (c) 1996 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the MASH Research
* Group at the University of California Berkeley.
* 4. Neither the name of the University nor of the Research Group may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /cvsroot/nsnam/ns-2/classifier/classifier.cc,v 1.44 2010/03/08 05:54:49 tom_henderson Exp $";
#endif
#include <stdlib.h>
#include "config.h"
#include "classifier.h"
#include "packet.h"
static class ClassifierClass : public TclClass {
public:
ClassifierClass() : TclClass("Classifier") {}
TclObject* create(int, const char*const*) {
return (new Classifier());
}
} class_classifier;
Classifier::Classifier() :
slot_(0), nslot_(0), maxslot_(-1), shift_(0), mask_(0xffffffff), nsize_(0)
{
default_target_ = 0;
bind("offset_", &offset_);
bind("shift_", &shift_);
bind("mask_", &mask_);
}
int Classifier::classify(Packet *p)
{
return (mshift(*((int*) p->access(offset_))));
}
Classifier::~Classifier()
{
delete [] slot_;
}
void Classifier::set_table_size(int nn)
{
nsize_ = nn;
}
void Classifier::alloc(int slot)
{
NsObject** old = slot_;
int n = nslot_;
if (old == 0)
{
if (nsize_ != 0) {
//printf("classifier %x set to %d....%dth visit\n", this, nsize_, i++);
nslot_ = nsize_;
}
else {
//printf("classifier %x set to 32....%dth visit\n", this, j++);
nslot_ = 32;
}
}
while (nslot_ <= slot)
nslot_ <<= 1;
slot_ = new NsObject*[nslot_];
memset(slot_, 0, nslot_ * sizeof(NsObject*));
for (int i = 0; i < n; ++i)
slot_[i] = old[i];
delete [] old;
}
void Classifier::install(int slot, NsObject* p)
{
if (slot >= nslot_)
alloc(slot);
slot_[slot] = p;
if (slot >= maxslot_)
maxslot_ = slot;
}
void Classifier::clear(int slot)
{
slot_[slot] = 0;
if (slot == maxslot_) {
while (--maxslot_ >= 0 && slot_[maxslot_] == 0)
;
}
}
int Classifier::allocPort (NsObject *nullagent)
{
return getnxt (nullagent);
}
int Classifier::getnxt(NsObject *nullagent)
{
int i;
for (i=0; i < nslot_; i++)
if (slot_[i]==0 || slot_[i]==nullagent)
return i;
i=nslot_;
alloc(nslot_);
return i;
}
/*
* objects only ever see "packet" events, which come either
* from an incoming link or a local agent (i.e., packet source).
*/
void Classifier::recv(Packet* p, Handler*h)
{
NsObject* node = find(p);
if (node == NULL) {
/*
* XXX this should be "dropped" somehow. Right now,
* these events aren't traced.
*/
Packet::free(p);
return;
}
node->recv(p,h);
}
/*
* perform the mapping from packet to object
* perform upcall if no mapping
*/
NsObject* Classifier::find(Packet* p)
{
NsObject* node = NULL;
int cl = classify(p);
if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0) {
if (default_target_)
return default_target_;
/*
* Sigh. Can't pass the pkt out to tcl because it's
* not an object.
*/
Tcl::instance().evalf("%s no-slot %ld", name(), cl);
if (cl == TWICE) {
/*
* Try again. Maybe callback patched up the table.
*/
cl = classify(p);
if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0)
return (NULL);
}
}
return (node);
}
int Classifier::install_next(NsObject *node) {
int slot = maxslot_ + 1;
install(slot, node);
return (slot);
}
int Classifier::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if(argc == 2) {
if (strcmp(argv[1], "defaulttarget") == 0) {
if (default_target_ != 0)
tcl.result(default_target_->name());
return (TCL_OK);
}
} else if (argc == 3) {
/*
* $classifier alloc-port nullagent
*/
if (strcmp(argv[1],"alloc-port") == 0) {
int slot;
NsObject* nullagent =
(NsObject*)TclObject::lookup(argv[2]);
slot = getnxt(nullagent);
tcl.resultf("%u",slot);
return(TCL_OK);
}
/*
* $classifier clear $slot
*/
if (strcmp(argv[1], "clear") == 0) {
int slot = atoi(argv[2]);
clear(slot);
return (TCL_OK);
}
/*
* $classifier installNext $node
*/
if (strcmp(argv[1], "installNext") == 0) {
//int slot = maxslot_ + 1;
NsObject* node = (NsObject*)TclObject::lookup(argv[2]);
if (node == NULL) {
tcl.resultf("Classifier::installNext attempt "
"to install non-object %s into classifier", argv[2]);
return TCL_ERROR;
};
int slot = install_next(node);
tcl.resultf("%u", slot);
return TCL_OK;
}
/*
* $classifier slot snum
* returns the name of the object in slot # snum
*/
if (strcmp(argv[1], "slot") == 0) {
int slot = atoi(argv[2]);
if (slot >= 0 && slot < nslot_ && slot_[slot] != NULL) {
tcl.resultf("%s", slot_[slot]->name());
return TCL_OK;
}
tcl.resultf("Classifier: no object at slot %d", slot);
return (TCL_ERROR);
}
/*
* $classifier findslot $node
* finds the slot containing $node
*/
if (strcmp(argv[1], "findslot") == 0) {
int slot = 0;
NsObject* node = (NsObject*)TclObject::lookup(argv[2]);
if (node == NULL) {
return (TCL_ERROR);
}
while (slot < nslot_) {
// check if the slot is empty (xuanc, 1/14/02)
// fix contributed by Frank A. Zdarsky
// <frank.zdarsky@kom.tu-darmstadt.de>
if (slot_[slot] &&
strcmp(slot_[slot]->name(), argv[2]) == 0){
tcl.resultf("%u", slot);
return (TCL_OK);
}
slot++;
}
tcl.result("-1");
return (TCL_OK);
}
if (strcmp(argv[1], "defaulttarget") == 0) {
default_target_=(NsObject*)TclObject::lookup(argv[2]);
if (default_target_ == 0)
return TCL_ERROR;
return TCL_OK;
}
} else if (argc == 4) {
/*
* $classifier install $slot $node
*/
if (strcmp(argv[1], "install") == 0) {
int slot = atoi(argv[2]);
NsObject* node = (NsObject*)TclObject::lookup(argv[3]);
install(slot, node);
return (TCL_OK);
}
}
return (NsObject::command(argc, argv));
}
void Classifier::set_table_size(int, int)
{}
|