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
|
/*
* Copyright (c) 1997, 1998 The 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/emulate/tap.cc,v 1.15 2005/08/22 05:08:33 tomh Exp $ (UCB)";
#endif
#include "tap.h"
static class TapAgentClass : public TclClass {
public:
TapAgentClass() : TclClass("Agent/Tap") {}
TclObject* create(int, const char*const*) {
return (new TapAgent());
}
} class_tap_agent;
TapAgent::TapAgent() : Agent(PT_LIVE), net_(NULL)
{
bind("maxpkt_", &maxpkt_);
}
//
// link in a network to the agent. Assumes net_ is non-zero
//
int
TapAgent::linknet()
{
int mode = net_->mode();
int rchan = net_->rchannel();
int wchan = net_->schannel();
unlink();
if (mode == O_RDONLY || mode == O_RDWR) {
// reading enabled?
if (rchan < 0) {
fprintf(stderr,
"TapAgent(%s): network %s not open for reading (mode:%d)\n",
name(), net_->name(), mode);
return (TCL_ERROR);
}
link(rchan, TCL_READABLE);
TDEBUG3("TapAgent(%s): linked sock %d as READABLE\n",
name(), rchan);
} else if (mode != O_WRONLY) {
if (mode == -1) {
fprintf(stderr,
"TapAgent(%s): Network(%s) not opened properly.\n",
name(), net_->name());
fprintf(stderr,
"(choose: readonly, readwrite, or writeonly)\n");
} else {
fprintf(stderr,
"TapAgent(%s): unknown mode %d in Network(%s)\n",
name(), mode, net_->name());
}
return (TCL_ERROR);
}
if (mode == O_WRONLY || mode == O_RDWR) {
// writing enabled?
if (wchan < 0) {
fprintf(stderr,
"TapAgent(%s): network %s not open for writing\n",
name(), net_->name());
return (TCL_ERROR);
}
}
return (TCL_OK);
}
int
TapAgent::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 2) {
if (strcmp(argv[1], "network") == 0) {
tcl.result(name());
return(TCL_OK);
}
}
if (argc == 3) {
if (strcmp(argv[1], "network") == 0) {
net_ = (Network *)TclObject::lookup(argv[2]);
if (net_ != 0) {
return(linknet());
} else {
fprintf(stderr,
"TapAgent(%s): unknown network %s\n",
name(), argv[2]);
return (TCL_ERROR);
}
return(TCL_OK);
}
}
return (Agent::command(argc, argv));
}
/*
* Receive a packet off the network and inject into the simulation.
*/
void
TapAgent::recvpkt()
{
if (net_->mode() != O_RDWR && net_->mode() != O_RDONLY) {
fprintf(stderr,
"TapAgent(%s): recvpkt called while in write-only mode!\n",
name());
return;
}
if (maxpkt_ <= 0) {
fprintf(stderr,
"TapAgent(%s): recvpkt: maxpkt_ value too low (%d)\n",
name(), maxpkt_);
return;
}
// allocate packet and a data payload
Packet* p = allocpkt(maxpkt_);
// fill up payload
sockaddr addr; // not really used (yet)
double tstamp;
int cc = net_->recv(p->accessdata(), maxpkt_, addr, tstamp);
if (cc <= 0) {
if (cc < 0) {
perror("recv");
}
Packet::free(p);
return;
}
TDEBUG4("%f: Tap(%s): recvpkt, cc:%d\n", now(), name(), cc);
// inject into simulator
hdr_cmn* ch = HDR_CMN(p);
ch->size() = cc;
/*
* if the time-stamp on the pkt is sufficiently far in the future,
* put it in the scheduler instead of forwarding it immediately.
* This can happen if we are pulling packet from a trace file
* and we don't want them to be dispatched until later
*
* this agent assumes that the time stamps are in absolute
* time, so adjust it to relative time here
*/
double when = tstamp - now();
if (when > 0.0) {
TDEBUG5("%f: Tap(%s): DEFERRED PACKET %f secs, uid: "UID_PRINTF_FORMAT"\n",
now(), name(), when, p->uid_);
ch->timestamp() = when;
Scheduler::instance().schedule(target_, p, when);
} else {
TDEBUG4("%f: Tap(%s): recvpkt, writing to target: %s\n",
now(), name(), target_->name());
ch->timestamp() = now();
target_->recv(p);
}
return;
}
void
TapAgent::dispatch(int)
{
/*
* Just process one packet. We could put a loop here
* but instead we allow the dispatcher to call us back
* if there is a queue in the socket buffer; this allows
* other events to get a chance to slip in...
*/
#ifdef notdef
Scheduler::instance().sync(); // sim clock gets set to now
#endif
recvpkt();
}
/*
* SIM -> Live
*
* Receive a packet from the simulation and inject into the network.
* if there is no network attached, call Connector::drop() to send
* to drop target
*/
void
TapAgent::recv(Packet* p, Handler*)
{
(void) sendpkt(p);
Packet::free(p);
return;
}
int
TapAgent::sendpkt(Packet* p)
{
if (net_->mode() != O_RDWR && net_->mode() != O_WRONLY) {
fprintf(stderr,
"TapAgent(%s): sendpkt called while in read-only mode!\n",
name());
return (-1);
}
// send packet into the live network
hdr_cmn* hc = HDR_CMN(p);
if (net_ == NULL) {
fprintf(stderr,
"TapAgent(%s): sendpkt attempted with NULL net\n",
name());
drop(p);
return (-1);
}
if (net_->send(p->accessdata(), hc->size()) < 0) {
fprintf(stderr,
"TapAgent(%s): sendpkt (%p, %d): %s\n",
name(), p->accessdata(), hc->size(), strerror(errno));
return (-1);
}
TDEBUG3("TapAgent(%s): sent packet (sz: %d)\n",
name(), hc->size());
return 0;
}
|