00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #ifdef NS_DIFFUSION
00050
00051 #include "diffrtg.h"
00052 #include "address.h"
00053 #include "scheduler.h"
00054 #include "diffagent.h"
00055
00056
00057 static class DiffRoutingAgentClass : public TclClass {
00058 public:
00059 DiffRoutingAgentClass() : TclClass("Agent/DiffusionRouting") {}
00060 TclObject* create(int argc, const char*const* argv) {
00061 if (argc == 5)
00062 return(new DiffRoutingAgent(atoi(argv[4])));
00063
00064 fprintf(stderr, "Insufficient number of args for creating DiffRtgAgent");
00065 return (NULL);
00066 }
00067 } class_diffusion_routing_agent;
00068
00069
00070 void LocalApp::sendPacket(DiffPacket msg, int len, int dst) {
00071 agent_->sendPacket(msg, len, dst);
00072 }
00073
00074
00075 DiffPacket LocalApp::recvPacket(int fd) {
00076 DiffPacket p;
00077
00078 fprintf(stderr, "This function should not get called; call DiffRoutingAgent::recv(Packet *, Handler *) instead\n\n");
00079 exit(1);
00080 return (p);
00081 }
00082
00083
00084 void LinkLayerAbs::sendPacket(DiffPacket dp, int len, int dst) {
00085 Packet *p;
00086 hdr_cmn *ch;
00087 hdr_ip *iph;
00088 Message *msg;
00089
00090 msg = (Message *)dp;
00091 p = agent_->createNsPkt(msg, len, dst);
00092 iph = HDR_IP(p);
00093 ch = HDR_CMN(p);
00094 iph->saddr() = agent_->addr();
00095 iph->sport() = agent_->port();
00096 iph->daddr() = msg->next_hop_;
00097 iph->dport() = agent_->port();
00098 ch->next_hop_ = msg->next_hop_;
00099 agent_->send(p, 0);
00100
00101 }
00102
00103
00104 DiffPacket LinkLayerAbs::recvPacket(int fd) {
00105 DiffPacket p;
00106
00107 fprintf(stderr, "This function should not get called; call DiffRoutingAgent::recv(Packet *, Handler *) instead\n\n");
00108 exit(1);
00109 return (p);
00110 }
00111
00112
00113
00114 DiffRoutingAgent::DiffRoutingAgent(int nodeid) : Agent(PT_DIFF) {
00115 agent_ = new DiffusionCoreAgent(this, nodeid);
00116
00117 }
00118
00119
00120 void DiffRoutingAgent::sendPacket(DiffPacket dp, int len, int dst) {
00121 Packet *p;
00122 hdr_ip *iph;
00123 Message *msg;
00124
00125 msg = (Message *)dp;
00126 p = createNsPkt(msg, len, dst);
00127 iph = HDR_IP(p);
00128 iph->dport() = dst;
00129
00130
00131 (void)Scheduler::instance().schedule(port_dmux(), p, 0.000001);
00132
00133 }
00134
00135 void
00136 DiffRoutingAgent::initpkt(Packet* p, Message* msg, int len)
00137 {
00138 hdr_cmn* ch = HDR_CMN(p);
00139 hdr_ip* iph = HDR_IP(p);
00140 AppData *diffdata;
00141
00142 diffdata = new DiffusionData(msg, len);
00143 p->setdata(diffdata);
00144
00145
00146 ch->uid() = msg->pkt_num_;
00147 ch->ptype() = type_;
00148 ch->size() = size_;
00149 ch->timestamp() = Scheduler::instance().clock();
00150 ch->iface() = UNKN_IFACE.value();
00151 ch->direction() = hdr_cmn::NONE;
00152 ch->error() = 0;
00153
00154 iph->saddr() = addr();
00155 iph->sport() = port();
00156 iph->daddr() = addr();
00157 iph->flowid() = fid_;
00158 iph->prio() = prio_;
00159 iph->ttl() = defttl_;
00160
00161 hdr_flags* hf = hdr_flags::access(p);
00162 hf->ecn_capable_ = 0;
00163 hf->ecn_ = 0;
00164 hf->eln_ = 0;
00165 hf->ecn_to_echo_ = 0;
00166 hf->fs_ = 0;
00167 hf->no_ts_ = 0;
00168 hf->pri_ = 0;
00169 hf->cong_action_ = 0;
00170
00171 }
00172
00173
00174 Packet*
00175 DiffRoutingAgent::createNsPkt(Message *msg, int len, int dst) {
00176 Packet *p;
00177
00178 p = Packet::alloc();
00179 initpkt(p, msg, len);
00180 return p;
00181 }
00182
00183 void DiffRoutingAgent::recv(Packet *p, Handler *h) {
00184 Message *msg;
00185 DiffusionData *diffdata;
00186
00187 diffdata = (DiffusionData *)(p->userdata());
00188 msg = diffdata->data();
00189
00190 agent_->recvMessage(msg);
00191
00192
00193 Packet::free(p);
00194 }
00195
00196 int DiffRoutingAgent::command(int argc, const char*const* argv) {
00197 if (argc == 2) {
00198 if (strcasecmp(argv[1], "start")==0) {
00199
00200
00201
00202
00203
00204
00205
00206 return TCL_OK;
00207 }
00208
00209 }
00210 else if (argc == 3) {
00211 if (strcasecmp(argv[1], "addr") == 0) {
00212 addr_ = (Address::instance().str2addr(argv[2]));
00213 return TCL_OK;
00214 }
00215 if (strcasecmp(argv[1], "stop-time")==0) {
00216
00217
00218 TimerCallback *callback;
00219 callback = new DiffusionStopTimer(agent_);
00220 agent_->timers_manager_->addTimer(atoi(argv[2])*1000, callback);
00221 return TCL_OK;
00222 }
00223 TclObject *obj;
00224 if ((obj = TclObject::lookup (argv[2])) == 0) {
00225 fprintf(stderr, "_Diffusion Node_ %s lookup of %s failed\n", argv[1], argv[2]);
00226 return TCL_ERROR;
00227 }
00228 if (strcasecmp(argv[1], "port-dmux") == 0) {
00229 port_dmux_ = (PortClassifier *)obj;
00230 return TCL_OK;
00231 }
00232 if (strcasecmp(argv[1], "add-ll") == 0) {
00233 target_ = (LL*)obj;
00234 return TCL_OK;
00235 }
00236 if (strcasecmp(argv[1], "tracetarget") == 0) {
00237 tracetarget_ = (Trace *)obj;
00238 return TCL_OK;
00239 }
00240 }
00241 return Agent::command(argc, argv);
00242 }
00243
00244
00245 void DiffRoutingAgent::trace(char *fmt, ...) {
00246 va_list ap;
00247 if (!tracetarget_)
00248 return;
00249
00250 va_start (ap, fmt);
00251 vsprintf (tracetarget_->pt_->buffer (), fmt, ap);
00252 tracetarget_->pt_->dump ();
00253 va_end (ap);
00254 }
00255
00256
00257 #endif // NS