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
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #ifndef lint
00068 static const char rcsid[] =
00069 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/queue/rio.cc,v 1.12 2004/05/25 03:23:05 sfloyd Exp $ (LBL)";
00070 #endif
00071
00072 #include "rio.h"
00073 #include "tclcl.h"
00074 #include "packet.h"
00075 #include "random.h"
00076 #include "flags.h"
00077 #include "delay.h"
00078 #include "template.h"
00079 #include "red.h"
00080
00081 static class RIOClass : public TclClass {
00082 public:
00083 RIOClass() : TclClass("Queue/RED/RIO") {}
00084 TclObject* create(int, const char*const*) {
00085 return (new RIOQueue);
00086 }
00087 } class_rio;
00088
00089 RIOQueue::RIOQueue() : in_len_(0), in_bcount_(0), in_idle_(1)
00090 {
00091 bind("in_thresh_", &edp_in_.th_min);
00092 bind("in_maxthresh_", &edp_in_.th_max);
00093 bind("out_thresh_", &edp_out_.th_min);
00094 bind("out_maxthresh_", &edp_out_.th_max);
00095 bind("in_linterm_", &edp_in_.max_p_inv);
00096
00097
00098
00099 bind_bool("in_gentle_",&edp_in_.gentle);
00100 bind_bool("out_gentle_",&edp_out_.gentle);
00101
00102 bind("in_ave_", &edv_in_.v_ave);
00103 bind("out_ave_", &edv_out_.v_ave);
00104 bind("in_prob1_", &edv_in_.v_prob1);
00105 bind("out_prob1_", &edv_out_.v_prob1);
00106 bind("priority_method_", &priority_method_);
00107
00108
00109
00110
00111
00112 }
00113
00114 void RIOQueue::reset()
00115 {
00116
00117
00118
00119
00120
00121 if (qib_) {
00122 edp_in_.th_min *= edp_.mean_pktsize;
00123 edp_in_.th_max *= edp_.mean_pktsize;
00124
00125 edp_out_.th_min *= edp_.mean_pktsize;
00126 edp_out_.th_max *= edp_.mean_pktsize;
00127 }
00128
00129
00130 if (edp_.gentle) {
00131 edp_in_.gentle = true;
00132 edp_out_.gentle = true;
00133 }
00134 if (edp_in_.gentle) {
00135 edv_in_.v_c = ( 1.0 - 1 / edp_in_.max_p_inv ) / edp_in_.th_max;
00136 edv_in_.v_d = 2 / edp_in_.max_p_inv - 1.0;
00137 }
00138 if (edp_out_.gentle) {
00139 edv_out_.v_c = ( 1.0 - 1 / edp_.max_p_inv ) / edp_out_.th_max;
00140 edv_out_.v_d = 2 / edp_.max_p_inv - 1.0;
00141 }
00142
00143
00144 edv_in_.v_ave = 0.0;
00145 edv_in_.v_slope = 0.0;
00146 edv_in_.drops = 0;
00147 edv_in_.count = 0;
00148 edv_in_.count_bytes = 0;
00149 edv_in_.old = 0;
00150 edv_in_.v_a = 1 / (edp_in_.th_max - edp_in_.th_min);
00151 edv_in_.v_b = - edp_in_.th_min / (edp_in_.th_max - edp_in_.th_min);
00152
00153
00154 edv_out_.v_ave = 0.0;
00155 edv_out_.v_slope = 0.0;
00156 edv_out_.drops = 0;
00157 edv_out_.count = 0;
00158 edv_out_.count_bytes = 0;
00159 edv_out_.old = 0;
00160 edv_out_.v_a = 1 / (edp_out_.th_max - edp_out_.th_min);
00161 edv_out_.v_b = - edp_out_.th_min / (edp_out_.th_max - edp_out_.th_min);
00162
00163 in_idle_ = 1;
00164 if (&Scheduler::instance() == NULL) {
00165 in_idletime_ = 0.0;
00166 }
00167 REDQueue::reset();
00168 }
00169
00170
00171
00172
00173 Packet* RIOQueue::deque()
00174 {
00175 Packet *p;
00176 p = REDQueue::deque();
00177
00178 if (p != 0) {
00179 hdr_flags* hf = hdr_flags::access(p);
00180 if (hf->pri_) {
00181
00182 in_idle_ = 0;
00183 in_bcount_ -= hdr_cmn::access(p)->size();
00184 --in_len_;
00185 }
00186 } else {
00187 in_idle_ = 1;
00188 }
00189 return (p);
00190 }
00191
00192
00193
00194
00195 int
00196 RIOQueue::drop_in_early(Packet* pkt)
00197 {
00198 hdr_cmn* ch = hdr_cmn::access(pkt);
00199
00200 edv_in_.v_prob1 = REDQueue::calculate_p(edv_in_.v_ave, edp_in_.th_max,
00201 edp_in_.gentle, edv_in_.v_a, edv_in_.v_b, edv_in_.v_c,
00202 edv_in_.v_d, edp_in_.max_p_inv);
00203 edv_in_.v_prob = REDQueue::modify_p(edv_in_.v_prob1, edv_in_.count,
00204 edv_in_.count_bytes, edp_.bytes, edp_.mean_pktsize, edp_.wait,
00205 ch->size());
00206
00207
00208 double u = Random::uniform();
00209 if (u <= edv_in_.v_prob) {
00210
00211 edv_in_.count = 0;
00212 edv_in_.count_bytes = 0;
00213 hdr_flags* hf = hdr_flags::access(pickPacketForECN(pkt));
00214 if (edp_.setbit && hf->ect() &&
00215 edv_in_.v_ave < edp_in_.th_max) {
00216 hf->ce() = 1;
00217 return (0);
00218 } else {
00219 return (1);
00220 }
00221 }
00222 return (0);
00223 }
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 int RIOQueue::drop_out_early(Packet* pkt)
00234 {
00235 hdr_cmn* ch = hdr_cmn::access(pkt);
00236
00237 edv_out_.v_prob1 = REDQueue::calculate_p(edv_.v_ave, edp_out_.th_max,
00238 edp_out_.gentle, edv_out_.v_a, edv_out_.v_b, edv_out_.v_c,
00239 edv_out_.v_d, edp_.max_p_inv);
00240 edv_out_.v_prob = REDQueue::modify_p(edv_out_.v_prob1, edv_out_.count,
00241 edv_out_.count_bytes, edp_.bytes, edp_.mean_pktsize, edp_.wait,
00242 ch->size());
00243
00244
00245 double u = Random::uniform();
00246 if (u <= edv_out_.v_prob) {
00247
00248 edv_out_.count = 0;
00249 edv_out_.count_bytes = 0;
00250 hdr_flags* hf = hdr_flags::access(pickPacketForECN(pkt));
00251 if (edp_.setbit && hf->ecn_capable_ &&
00252 edv_.v_ave < edp_out_.th_max) {
00253 hf->ce() = 1;
00254 return (0);
00255 } else {
00256 return (1);
00257 }
00258 }
00259 return (0);
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 #define DTYPE_NONE 0
00280 #define DTYPE_FORCED 1
00281 #define DTYPE_UNFORCED 2
00282
00283 void RIOQueue::enque(Packet* pkt)
00284 {
00285
00286
00287 hdr_flags* hf = hdr_flags::access(pkt);
00288 hdr_ip* iph = hdr_ip::access(pkt);
00289 if (priority_method_ == 1) {
00290 hf->pri_ = iph->flowid();
00291 }
00292
00293
00294
00295 if (hf->pri_) {
00296
00297
00298
00299
00300
00301
00302
00303 int m = 0;
00304 int m_in = 0;
00305 double now = Scheduler::instance().clock();
00306
00307 if (in_idle_) {
00308 in_idle_ = 0;
00309 m_in = int(edp_.ptc * (now - idletime_));
00310 }
00311 if (idle_) {
00312 idle_ = 0;
00313 m = int(edp_.ptc * (now - idletime_));
00314 }
00315
00316
00317
00318
00319
00320
00321
00322 edv_.v_ave = REDQueue::estimator(qib_ ? q_->byteLength() : q_->length(), m + 1,
00323 edv_.v_ave, edp_.q_w);
00324 edv_in_.v_ave = REDQueue::estimator(qib_ ? in_bcount_ : in_len_,
00325 m_in + 1, edv_in_.v_ave, edp_.q_w);
00326
00327
00328
00329
00330
00331
00332
00333 hdr_cmn* ch = hdr_cmn::access(pkt);
00334 ++edv_.count;
00335 edv_.count_bytes += ch->size();
00336
00337
00338 ++edv_in_.count;
00339 edv_in_.count_bytes += ch->size();
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 register double in_qavg = edv_in_.v_ave;
00351 int droptype = DTYPE_NONE;
00352 int qlen = qib_ ? q_->byteLength() : q_->length();
00353 int in_qlen = qib_ ? in_bcount_ : in_len_;
00354 int qlim = qib_ ? (qlim_ * edp_.mean_pktsize) : qlim_;
00355
00356 curq_ = qlen;
00357
00358 if (in_qavg >= edp_in_.th_min && in_qlen > 1) {
00359 if ((!edp_in_.gentle && in_qavg >= edp_in_.th_max) ||
00360 (edp_in_.gentle && in_qavg >= 2 * edp_in_.th_max)) {
00361 droptype = DTYPE_FORCED;
00362 } else if (edv_in_.old == 0) {
00363
00364
00365
00366
00367
00368
00369 edv_in_.count = 1;
00370 edv_in_.count_bytes = ch->size();
00371 edv_in_.old = 1;
00372 } else if (drop_in_early(pkt)) {
00373 droptype = DTYPE_UNFORCED;
00374 }
00375 } else {
00376
00377 edv_in_.v_prob = 0.0;
00378 edv_in_.old = 0;
00379 }
00380 if (qlen >= qlim) {
00381
00382 droptype = DTYPE_FORCED;
00383 }
00384
00385 if (droptype == DTYPE_UNFORCED) {
00386
00387 Packet *pkt_to_drop = pickPacketForECN(pkt);
00388
00389
00390
00391
00392 if (pkt_to_drop != pkt) {
00393 q_->enque(pkt);
00394
00395 ++in_len_;
00396 in_bcount_ += ch->size();
00397 q_->remove(pkt_to_drop);
00398
00399 if (hdr_flags::access(pkt_to_drop)->pri_)
00400 {
00401 in_bcount_ -=
00402 hdr_cmn::access(pkt_to_drop)->size();
00403 --in_len_;
00404 }
00405 pkt = pkt_to_drop;
00406 }
00407
00408 if (de_drop_ != NULL)
00409 de_drop_->recv(pkt);
00410 else
00411 drop(pkt);
00412 } else {
00413
00414 q_->enque(pkt);
00415
00416 ++in_len_;
00417 in_bcount_ += ch->size();
00418
00419
00420 if (droptype == DTYPE_FORCED) {
00421
00422 pkt = pickPacketToDrop();
00423 q_->remove(pkt);
00424
00425 if (hdr_flags::access(pkt)->pri_) {
00426 in_bcount_ -= hdr_cmn::access(pkt)->size();
00427 --in_len_;
00428 }
00429 drop(pkt);
00430 if (!ns1_compat_) {
00431
00432 edv_.count = 0;
00433 edv_.count_bytes = 0;
00434 edv_in_.count = 0;
00435 edv_in_.count_bytes = 0;
00436 }
00437 }
00438 }
00439 }
00440
00441 else {
00442
00443
00444
00445
00446
00447
00448 hdr_cmn* ch = hdr_cmn::access(pkt);
00449 ++edv_.count;
00450 edv_.count_bytes += ch->size();
00451
00452
00453 ++edv_out_.count;
00454 edv_out_.count_bytes += ch->size();
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 register double qavg = edv_.v_ave;
00469
00470 int droptype = DTYPE_NONE;
00471 int qlen = qib_ ? q_->byteLength() : q_->length();
00472
00473
00474
00475 int qlim = qib_ ? (qlim_ * edp_.mean_pktsize) : qlim_;
00476
00477 curq_ = qlen;
00478
00479 if (qavg >= edp_out_.th_min && qlen > 1) {
00480 if (!edp_out_.gentle && qavg >= edp_out_.th_max ||
00481 (edp_out_.gentle && qavg >= 2 * edp_out_.th_max)) {
00482 droptype = DTYPE_FORCED;
00483 } else if (edv_out_.old == 0) {
00484
00485
00486
00487
00488
00489
00490 edv_out_.count = 1;
00491 edv_out_.count_bytes = ch->size();
00492 edv_out_.old = 1;
00493 } else if (drop_out_early(pkt)) {
00494 droptype = DTYPE_UNFORCED;
00495 }
00496 } else {
00497 edv_out_.v_prob = 0.0;
00498 edv_out_.old = 0;
00499 }
00500 if (qlen >= qlim) {
00501
00502 droptype = DTYPE_FORCED;
00503 }
00504
00505 if (droptype == DTYPE_UNFORCED) {
00506
00507 Packet *pkt_to_drop = pickPacketForECN(pkt);
00508
00509
00510
00511
00512 if (pkt_to_drop != pkt) {
00513 q_->enque(pkt);
00514
00515 q_->remove(pkt_to_drop);
00516
00517 if (hdr_flags::access(pkt_to_drop)->pri_)
00518 {
00519 in_bcount_ -=hdr_cmn::access(pkt_to_drop)->size();
00520 --in_len_;
00521 }
00522 pkt = pkt_to_drop;
00523 }
00524
00525 if (de_drop_ != NULL)
00526 de_drop_->recv(pkt);
00527 else
00528 drop(pkt);
00529 } else {
00530
00531 q_->enque(pkt);
00532
00533
00534
00535 if (droptype == DTYPE_FORCED) {
00536
00537 pkt = pickPacketToDrop();
00538 q_->remove(pkt);
00539
00540 if (hdr_flags::access(pkt)->pri_)
00541 {
00542 in_bcount_ -= hdr_cmn::access(pkt)->size();
00543 --in_len_;
00544 }
00545 drop(pkt);
00546 }
00547 }
00548 }
00549
00550 return;
00551 }
00552
00553
00554
00555
00556
00557
00558
00559
00560 void
00561 RIOQueue::trace(TracedVar* v)
00562 {
00563 char wrk[500], *p;
00564
00565 if (((p = strstr(v->name(), "ave")) == NULL) &&
00566 ((p = strstr(v->name(), "in_ave")) == NULL) &&
00567 ((p = strstr(v->name(), "out_ave")) == NULL) &&
00568 ((p = strstr(v->name(), "prob")) == NULL) &&
00569 ((p = strstr(v->name(), "in_prob")) == NULL) &&
00570 ((p = strstr(v->name(), "out_prob")) == NULL) &&
00571 ((p = strstr(v->name(), "curq")) == NULL)) {
00572 fprintf(stderr, "RIO:unknown trace var %s\n",
00573 v->name());
00574 return;
00575 }
00576
00577 if (tchan_) {
00578 int n;
00579 double t = Scheduler::instance().clock();
00580
00581 if (*p == 'c') {
00582 sprintf(wrk, "Q %g %d", t, int(*((TracedInt*) v)));
00583 } else {
00584 sprintf(wrk, "%c %g %g", *p, t,
00585 double(*((TracedDouble*) v)));
00586 }
00587 n = strlen(wrk);
00588 wrk[n] = '\n';
00589 wrk[n+1] = 0;
00590 (void)Tcl_Write(tchan_, wrk, n+1);
00591 }
00592 return;
00593 }
00594
00595
00596 void RIOQueue::print_edp()
00597 {
00598 REDQueue::print_edp();
00599 printf("in_minth: %f, in_maxth: %f\n", edp_in_.th_min, edp_in_.th_max);
00600 printf("out_minth: %f, out_maxth: %f\n",
00601 edp_out_.th_min, edp_out_.th_max);
00602 printf("qlim: %d, in_idletime: %f\n", qlim_, in_idletime_);
00603 printf("=========\n");
00604 }
00605
00606 void RIOQueue::print_edv()
00607 {
00608 REDQueue::print_edv();
00609 printf("in_v_a: %f, in_v_b: %f\n", edv_in_.v_a, edv_in_.v_b);
00610 printf("out_v_a: %f, out_v_b: %f\n", edv_out_.v_a, edv_out_.v_b);
00611 }