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 #include "template.h"
00050 #include "ack-recons.h"
00051
00052 static class AckReconsControllerClass : public TclClass {
00053 public:
00054 AckReconsControllerClass() : TclClass("AckReconsControllerClass") { }
00055 TclObject* create(int, const char*const*) {
00056 return (new AckReconsController);
00057 }
00058 } class_ackrecons_controller;
00059
00060 static class AckReconsClass : public TclClass {
00061 public:
00062 AckReconsClass() : TclClass("Agent/AckReconsClass") { }
00063 TclObject* create(int, const char*const* argv) {
00064 return new AckRecons(atoi(argv[4]), atoi(argv[5]));
00065 }
00066 } class_ackrecons;
00067
00068
00069
00070
00071 void
00072 AckReconsController::recv(Packet *p, Handler *)
00073 {
00074 Tcl& tcl = Tcl::instance();
00075 hdr_ip *ip = hdr_ip::access(p);
00076 tcl.evalf("%s demux %d %d", name(),
00077 ip->saddr(), ip->daddr());
00078 AckRecons *ackRecons =
00079 (AckRecons *) TclObject::lookup(tcl.result());
00080 if (ackRecons == NULL) {
00081 printf("Error: malformed ack reconstructor\n");
00082 abort();
00083 }
00084 ackRecons->spq_ = spq_;
00085 ackRecons->recv(p);
00086 }
00087
00088 void
00089 AckRecons::recv(Packet *pkt)
00090 {
00091 double now = Scheduler::instance().clock();
00092 hdr_tcp *tcph = hdr_tcp::access(pkt);
00093 int &ack = tcph->seqno(), a, i;
00094 Tcl& tcl = Tcl::instance();
00095 #ifdef DEBUG
00096 printf("%f\tRecd ack %d\n", now, ack);
00097 #endif
00098 if (ackTemplate_ == 0)
00099 ackTemplate_ = pkt->copy();
00100 if (adaptive_)
00101 tcl.evalf("%s ackbw %d %f\n", name(), ack, now);
00102
00103 tcl.evalf("%s spacing %d\n", name(), ack);
00104
00105
00106
00107
00108
00109 if ((!ackPending_ && ack-lastAck_ <= deltaAckThresh_) || dupacks_) {
00110 if (ack == lastRealAck_)
00111 dupacks_++;
00112 else if (ack > lastAck_) {
00113 dupacks_ = 0;
00114 lastAck_ = ack;
00115 lastTime_ = now;
00116 }
00117 spq_->reconsAcks_ = 0;
00118 spq_->enque(pkt);
00119 spq_->reconsAcks_ = 1;
00120 #ifdef DEBUG
00121 printf("\t%f\tEnqueuing ack %d in order\n", now, ack);
00122 #endif
00123 } else {
00124 if (ack == lastRealAck_)
00125 dupacks_++;
00126
00127 double starttime = max(now, lastTime_);
00128 for (a = lastAck_+delack_, i=0; a <= ack; a += delack_, i++)
00129 sendack(a, starttime + i*ackSpacing_ - now);
00130 if ((a-ack)%delack_)
00131 sendack(ack, starttime + i*ackSpacing_ - now);
00132 Packet::free(pkt);
00133 }
00134 if (ack >= lastRealAck_) {
00135 lastRealAck_ = ack;
00136 lastRealTime_ = now;
00137 }
00138 }
00139
00140 int
00141 AckRecons::command(int argc, const char*const* argv)
00142 {
00143 return Agent::command(argc, argv);
00144 }
00145
00146
00147
00148
00149 void
00150 AckRecons::sendack(int ack, double t)
00151 {
00152 Packet *ackp = ackTemplate_->copy();
00153 Scheduler &s = Scheduler::instance();
00154 hdr_tcp *th = hdr_tcp::access(ackp);
00155 th->seqno() = ack;
00156
00157 if (th->ts() == hdr_tcp::access(ackp)->ts()) {
00158 hdr_flags *fh = hdr_flags::access(ackp);
00159 fh->no_ts_ = 1;
00160 th->ts_ = s.clock();
00161 }
00162 s.schedule((Handler *)this, (Event *)ackp, t);
00163 ackPending_++;
00164 #ifdef DEBUG
00165 printf("\t%f\tScheduling ack %d to be sent at %f\n",
00166 s.clock(), ack, s.clock() + t);
00167 #endif
00168 }
00169
00170
00171
00172
00173 void
00174 AckRecons::handle(Event *e)
00175 {
00176 Packet *p = (Packet *) e;
00177 hdr_tcp *th = hdr_tcp::access(p);
00178 ackPending_--;
00179 if (lastAck_ < th->seqno()) {
00180 spq_->reconsAcks_ = 0;
00181
00182
00183
00184
00185
00186 target_->recv(p);
00187 spq_->reconsAcks_ = 1;
00188 lastTime_ = Scheduler::instance().clock();
00189 lastAck_ = th->seqno();
00190 #ifdef DEBUG
00191 printf("%f\tSending scheduled ack %d\n",lastTime_,th->seqno());
00192 #endif
00193 } else {
00194 Packet::free(p);
00195 #ifdef DEBUG
00196 printf("%f\tack %d superceded by ack %d at %f\n",
00197 Scheduler::instance().clock(), th->seqno(), lastAck_,
00198 lastTime_);
00199 #endif
00200 }
00201 }