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 #include "dsPolicy.h"
00060 #include "ew.h"
00061 #include "packet.h"
00062 #include "tcp.h"
00063 #include "random.h"
00064
00065
00066
00067 PolicyClassifier::PolicyClassifier() {
00068 int i;
00069
00070 policyTableSize = 0;
00071 policerTableSize = 0;
00072
00073 for (i = 0; i < MAX_POLICIES; i++)
00074 policy_pool[i] = NULL;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 void PolicyClassifier::addPolicyEntry(int argc, const char*const* argv) {
00096 if (policyTableSize == MAX_POLICIES)
00097 printf("ERROR: Policy Table size limit exceeded.\n");
00098 else {
00099 policyTable[policyTableSize].sourceNode = atoi(argv[2]);
00100 policyTable[policyTableSize].destNode = atoi(argv[3]);
00101 policyTable[policyTableSize].codePt = atoi(argv[5]);
00102 policyTable[policyTableSize].arrivalTime = 0;
00103 policyTable[policyTableSize].winLen = 1.0;
00104
00105 if (strcmp(argv[4], "Dumb") == 0) {
00106 if(!policy_pool[DUMB])
00107 policy_pool[DUMB] = new DumbPolicy;
00108 policyTable[policyTableSize].policy_index = DUMB;
00109 policyTable[policyTableSize].policer = dumbPolicer;
00110 policyTable[policyTableSize].meter = dumbMeter;
00111 } else if (strcmp(argv[4], "TSW2CM") == 0) {
00112 if(!policy_pool[TSW2CM])
00113 policy_pool[TSW2CM] = new TSW2CMPolicy;
00114 policyTable[policyTableSize].policy_index = TSW2CM;
00115 policyTable[policyTableSize].policer = TSW2CMPolicer;
00116 policyTable[policyTableSize].meter = tswTagger;
00117
00118 policyTable[policyTableSize].cir =
00119 policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
00120 if (argc == 8) policyTable[policyTableSize].winLen = (double) atof(argv[7]);
00121 } else if (strcmp(argv[4], "TSW3CM") == 0) {
00122 if(!policy_pool[TSW3CM])
00123 policy_pool[TSW3CM] = new TSW3CMPolicy;
00124 policyTable[policyTableSize].policy_index = TSW3CM;
00125 policyTable[policyTableSize].policer = TSW3CMPolicer;
00126 policyTable[policyTableSize].meter = tswTagger;
00127
00128 policyTable[policyTableSize].cir =
00129 policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
00130 policyTable[policyTableSize].pir = (double) atof(argv[7]) / 8.0;
00131 } else if (strcmp(argv[4], "TokenBucket") == 0) {
00132 if(!policy_pool[TB])
00133 policy_pool[TB] = (Policy *) new TBPolicy;
00134 policyTable[policyTableSize].policy_index = TB;
00135 policyTable[policyTableSize].policer = tokenBucketPolicer;
00136 policyTable[policyTableSize].meter = tokenBucketMeter;
00137
00138 policyTable[policyTableSize].cir =
00139 policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
00140 policyTable[policyTableSize].cbs =
00141 policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
00142 } else if (strcmp(argv[4], "srTCM") == 0) {
00143 if(!policy_pool[SRTCM])
00144 policy_pool[SRTCM] = new SRTCMPolicy;
00145 policyTable[policyTableSize].policy_index = SRTCM;
00146 policyTable[policyTableSize].policer = srTCMPolicer;
00147 policyTable[policyTableSize].meter = srTCMMeter;
00148
00149 policyTable[policyTableSize].cir =
00150 policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
00151 policyTable[policyTableSize].cbs =
00152 policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
00153 policyTable[policyTableSize].ebs =
00154 policyTable[policyTableSize].eBucket = (double) atof(argv[8]);
00155 } else if (strcmp(argv[4], "trTCM") == 0) {
00156 if(!policy_pool[TRTCM])
00157 policy_pool[TRTCM] = new TRTCMPolicy;
00158 policyTable[policyTableSize].policy_index = TRTCM;
00159 policyTable[policyTableSize].policer = trTCMPolicer;
00160 policyTable[policyTableSize].meter = trTCMMeter;
00161
00162 policyTable[policyTableSize].cir =
00163 policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
00164 policyTable[policyTableSize].cbs =
00165 policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
00166 policyTable[policyTableSize].pir = (double) atof(argv[8]) / 8.0;
00167 policyTable[policyTableSize].pbs =
00168 policyTable[policyTableSize].pBucket = (double) atof(argv[9]);
00169 } else if (strcmp(argv[4], "SFD") == 0) {
00170 if(!policy_pool[SFD])
00171 policy_pool[SFD] = new SFDPolicy;
00172 policyTable[policyTableSize].policy_index = SFD;
00173 policyTable[policyTableSize].policer = SFDPolicer;
00174 policyTable[policyTableSize].meter = sfdTagger;
00175
00176
00177 policyTable[policyTableSize].cir = atoi(argv[6]);
00178 } else if (strcmp(argv[4], "EW") == 0) {
00179 if(!policy_pool[EWP])
00180 policy_pool[EWP] = new EWPolicy();
00181
00182 ((EWPolicy *)policy_pool[EWP])->
00183 init(atoi(argv[6]), atoi(argv[7]), atoi(argv[8]));
00184
00185 policyTable[policyTableSize].policy_index = EWP;
00186 policyTable[policyTableSize].policer = EWPolicer;
00187 policyTable[policyTableSize].meter = ewTagger;
00188 } else {
00189 printf("No applicable policy specified, exit!!!\n");
00190 exit(-1);
00191 }
00192 policyTableSize++;
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 policyTableEntry* PolicyClassifier::getPolicyTableEntry(nsaddr_t source, nsaddr_t dest) {
00205 for (int i = 0; i <= policyTableSize; i++) {
00206 if ((policyTable[i].sourceNode == source) || (policyTable[i].sourceNode == ANY_HOST)) {
00207 if ((policyTable[i].destNode == dest) || (policyTable[i].destNode == ANY_HOST))
00208 return(&policyTable[i]);
00209 }
00210 }
00211
00212
00213 printf("ERROR: No Policy Table entry found for Source %d-Destination %d.\n", source, dest);
00214 printPolicyTable();
00215 return(NULL);
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 void PolicyClassifier::addPolicerEntry(int argc, const char*const* argv) {
00230
00231
00232
00233 if (policerTableSize == MAX_CP)
00234 printf("ERROR: Policer Table size limit exceeded.\n");
00235 else {
00236 if (strcmp(argv[2], "Dumb") == 0) {
00237 if(!policy_pool[DUMB])
00238 policy_pool[DUMB] = new DumbPolicy;
00239 policerTable[policerTableSize].policer = dumbPolicer;
00240 policerTable[policerTableSize].policy_index = DUMB;
00241 } else if (strcmp(argv[2], "TSW2CM") == 0) {
00242 if(!policy_pool[TSW2CM])
00243 policy_pool[TSW2CM] = new TSW2CMPolicy;
00244 policerTable[policerTableSize].policer = TSW2CMPolicer;
00245 policerTable[policerTableSize].policy_index = TSW2CM;
00246 } else if (strcmp(argv[2], "TSW3CM") == 0) {
00247 if(!policy_pool[TSW3CM])
00248 policy_pool[TSW3CM] = new TSW3CMPolicy;
00249 policerTable[policerTableSize].policer = TSW3CMPolicer;
00250 policerTable[policerTableSize].policy_index = TSW3CM;
00251 } else if (strcmp(argv[2], "TokenBucket") == 0) {
00252 if(!policy_pool[TB])
00253 policy_pool[TB] = new TBPolicy;
00254 policerTable[policerTableSize].policer = tokenBucketPolicer;
00255 policerTable[policerTableSize].policy_index = TB;
00256 } else if (strcmp(argv[2], "srTCM") == 0) {
00257 if(!policy_pool[SRTCM])
00258 policy_pool[SRTCM] = new SRTCMPolicy;
00259 policerTable[policerTableSize].policer = srTCMPolicer;
00260 policerTable[policerTableSize].policy_index = SRTCM;
00261 } else if (strcmp(argv[2], "trTCM") == 0){
00262 if(!policy_pool[TRTCM])
00263 policy_pool[TRTCM] = new TRTCMPolicy;
00264 policerTable[policerTableSize].policer = trTCMPolicer;
00265 policerTable[policerTableSize].policy_index = TRTCM;
00266 } else if (strcmp(argv[2], "SFD") == 0) {
00267 if(!policy_pool[SFD])
00268 policy_pool[SFD] = new SFDPolicy;
00269 policerTable[policerTableSize].policer = SFDPolicer;
00270 policerTable[policerTableSize].policy_index = SFD;
00271 } else if (strcmp(argv[2], "EW") == 0) {
00272 if(!policy_pool[EWP])
00273 policy_pool[EWP] = new EWPolicy;
00274 policerTable[policerTableSize].policer = EWPolicer;
00275 policerTable[policerTableSize].policy_index = EWP;
00276 } else {
00277 printf("No applicable policer specified, exit!!!\n");
00278 exit(-1);
00279 }
00280 };
00281
00282 policerTable[policerTableSize].initialCodePt = atoi(argv[3]);
00283 policerTable[policerTableSize].downgrade1 = atoi(argv[4]);
00284 if (argc == 6)
00285 policerTable[policerTableSize].downgrade2 = atoi(argv[5]);
00286 policerTableSize++;
00287 }
00288
00289
00290 policerTableEntry* PolicyClassifier::getPolicerTableEntry(int policy_index, int oldCodePt) {
00291 for (int i = 0; i < policerTableSize; i++)
00292 if ((policerTable[i].policy_index == policy_index) &&
00293 (policerTable[i].initialCodePt == oldCodePt))
00294 return(&policerTable[i]);
00295
00296 printf("ERROR: No Policer Table entry found for initial code point %d.\n", oldCodePt);
00297
00298 return(NULL);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 int PolicyClassifier::mark(Packet *pkt) {
00308 policyTableEntry *policy;
00309 policerTableEntry *policer;
00310 int policy_index;
00311 int codePt;
00312 hdr_ip* iph;
00313 int fid;
00314
00315 iph = hdr_ip::access(pkt);
00316 fid = iph->flowid();
00317 policy = getPolicyTableEntry(iph->saddr(), iph->daddr());
00318 if (policy) {
00319 codePt = policy->codePt;
00320 policy_index = policy->policy_index;
00321 policer = getPolicerTableEntry(policy_index, codePt);
00322
00323
00324 if (policy_pool[policy_index]) {
00325 policy_pool[policy_index]->applyMeter(policy, pkt);
00326 codePt = policy_pool[policy_index]->applyPolicer(policy, policer, pkt);
00327 }
00328 } else {
00329 printf("The policy object doesn't exist, ERROR!!!\n");
00330 exit(-1);
00331 }
00332
00333 iph->prio_ = codePt;
00334 return(codePt);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344 void PolicyClassifier::updatePolicyRTT(int argc, const char*const* argv) {
00345 policyTableEntry *policy;
00346
00347 policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));
00348 if (policy == NULL)
00349 printf("ERROR: cannot update RTT; no existing policy found for Source %d-Desination %d.\n",
00350 atoi(argv[2]), atoi(argv[3]));
00351 else {
00352 policy->winLen = (double) atof(argv[4]);
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363 double PolicyClassifier::getCBucket(const char*const* argv) {
00364 policyTableEntry *policy;
00365
00366 policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));
00367 if (policy == NULL) {
00368 printf("ERROR: cannot get bucket size; no existing policy found for Source %d-Desination %d.\n",
00369 atoi(argv[2]), atoi(argv[3]));
00370 return(-1);
00371 }
00372 else {
00373 if ((policy->policer == tokenBucketPolicer) || (policy->policer == srTCMPolicer) || (policy->policer == trTCMPolicer))
00374 return(policy->cBucket);
00375 else {
00376 printf("ERROR: cannot get bucket size; the Source %d-Desination %d Policy does not include a Committed Bucket.\n", atoi(argv[2]), atoi(argv[3]));
00377 return(-1);
00378 }
00379 }
00380 }
00381
00382
00383 void PolicyClassifier::printPolicyTable() {
00384 printf("Policy Table(%d):\n",policyTableSize);
00385 for (int i = 0; i < policyTableSize; i++)
00386 {
00387 switch (policyTable[i].policer) {
00388 case dumbPolicer:
00389 printf("Flow (%d to %d): DUMB policer, ",
00390 policyTable[i].sourceNode,policyTable[i].destNode);
00391 printf("initial code point %d\n", policyTable[i].codePt);
00392 break;
00393 case TSW2CMPolicer:
00394 printf("Flow (%d to %d): TSW2CM policer, ",
00395 policyTable[i].sourceNode,policyTable[i].destNode);
00396 printf("initial code point %d, CIR %.1f bps.\n",
00397 policyTable[i].codePt, policyTable[i].cir * 8);
00398 break;
00399 case TSW3CMPolicer:
00400 printf("Flow (%d to %d): TSW3CM policer, initial code ",
00401 policyTable[i].sourceNode, policyTable[i].destNode);
00402 printf("point %d, CIR %.1f bps, PIR %.1f bytes.\n",
00403 policyTable[i].codePt, policyTable[i].cir * 8,
00404 policyTable[i].pir * 8);
00405 break;
00406 case tokenBucketPolicer:
00407 printf("Flow (%d to %d): Token Bucket policer, ",
00408 policyTable[i].sourceNode,policyTable[i].destNode);
00409 printf("initial code point %d, CIR %.1f bps, CBS %.1f bytes.\n",
00410 policyTable[i].codePt, policyTable[i].cir * 8,
00411 policyTable[i].cbs);
00412 break;
00413 case srTCMPolicer:
00414 printf("Flow (%d to %d): srTCM policer, initial code ",
00415 policyTable[i].sourceNode, policyTable[i].destNode);
00416 printf("point %d, CIR %.1f bps, CBS %.1f bytes, EBS %.1f bytes.\n",
00417 policyTable[i].codePt, policyTable[i].cir * 8,
00418 policyTable[i].cbs, policyTable[i].ebs);
00419 break;
00420 case trTCMPolicer:
00421 printf("Flow (%d to %d): trTCM policer, initial code ",
00422 policyTable[i].destNode, policyTable[i].sourceNode);
00423 printf("point %d, CIR %.1f bps, CBS %.1f bytes, PIR %.1f bps, ",
00424 policyTable[i].codePt, policyTable[i].cir * 8,
00425 policyTable[i].cbs, policyTable[i].pir * 8);
00426 printf("PBS %.1f bytes.\n", policyTable[i].pbs);
00427 break;
00428 case SFDPolicer:
00429 printf("Flow (%d to %d): SFD policer, ",
00430 policyTable[i].sourceNode,policyTable[i].destNode);
00431 printf("initial code point %d, TH %d bytes.\n",
00432 policyTable[i].codePt, (int)policyTable[i].cir);
00433 break;
00434 case EWPolicer:
00435 printf("Flow (%d to %d): EW policer, ",
00436 policyTable[i].sourceNode,policyTable[i].destNode);
00437 printf("initial code point %d.\n", policyTable[i].codePt);
00438 break;
00439 default:
00440 printf("ERROR: Unknown policer type in Policy Table.\n");
00441 }
00442 }
00443 printf("\n");
00444 }
00445
00446
00447 void PolicyClassifier::printPolicerTable() {
00448 bool threeColor;
00449
00450 printf("Policer Table:\n");
00451 for (int i = 0; i < policerTableSize; i++) {
00452 threeColor = false;
00453 switch (policerTable[i].policer) {
00454 case dumbPolicer:
00455 printf("Dumb ");
00456 break;
00457 case TSW2CMPolicer:
00458 printf("TSW2CM ");
00459 break;
00460 case TSW3CMPolicer:
00461 printf("TSW3CM ");
00462 threeColor = true;
00463 break;
00464 case tokenBucketPolicer:
00465 printf("Token Bucket ");
00466 break;
00467 case srTCMPolicer:
00468 printf("srTCM ");
00469 threeColor = true;
00470 break;
00471 case trTCMPolicer:
00472 printf("trTCM ");
00473 threeColor = true;
00474 break;
00475 case SFDPolicer:
00476 printf("SFD ");
00477
00478 break;
00479 case EWPolicer:
00480 printf("EW ");
00481 break;
00482 default:
00483 printf("ERROR: Unknown policer type in Policer Table.");
00484 }
00485
00486 if (threeColor) {
00487 printf("policer code point %d is policed to yellow ",
00488 policerTable[i].initialCodePt);
00489 printf("code point %d and red code point %d.\n",
00490 policerTable[i].downgrade1,
00491 policerTable[i].downgrade2);
00492 } else
00493 printf("policer code point %d is policed to code point %d.\n",
00494 policerTable[i].initialCodePt,
00495 policerTable[i].downgrade1);
00496 }
00497 printf("\n");
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508 void DumbPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00509 policy->arrivalTime = Scheduler::instance().clock();
00510 }
00511
00512
00513
00514
00515
00516 int DumbPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt) {
00517 return(policer->initialCodePt);
00518 }
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532 void TSW2CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00533 double now, bytesInTSW, newBytes;
00534 hdr_cmn* hdr = hdr_cmn::access(pkt);
00535
00536 bytesInTSW = policy->avgRate * policy->winLen;
00537 newBytes = bytesInTSW + (double) hdr->size();
00538 now = Scheduler::instance().clock();
00539 policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);
00540 policy->arrivalTime = now;
00541 }
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 int TSW2CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt) {
00554 if ((policy->avgRate > policy->cir)
00555 && (Random::uniform(0.0, 1.0) <= (1-(policy->cir/policy->avgRate)))) {
00556 return(policer->downgrade1);
00557 }
00558 else {
00559 return(policer->initialCodePt);
00560 }
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575 void TSW3CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00576 double now, bytesInTSW, newBytes;
00577 hdr_cmn* hdr = hdr_cmn::access(pkt);
00578
00579 bytesInTSW = policy->avgRate * policy->winLen;
00580 newBytes = bytesInTSW + (double) hdr->size();
00581 now = Scheduler::instance().clock();
00582 policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);
00583 policy->arrivalTime = now;
00584 }
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 int TSW3CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt) {
00607 double rand = policy->avgRate * (1.0 - Random::uniform(0.0, 1.0));
00608
00609 if (rand > policy->pir)
00610 return (policer->downgrade2);
00611 else if (rand > policy->cir)
00612 return(policer->downgrade1);
00613 else
00614 return(policer->initialCodePt);
00615 }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627 void TBPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00628 double now = Scheduler::instance().clock();
00629 double tokenBytes;
00630
00631 tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
00632 if (policy->cBucket + tokenBytes <= policy->cbs)
00633 policy->cBucket += tokenBytes;
00634 else
00635 policy->cBucket = policy->cbs;
00636 policy->arrivalTime = now;
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651 int TBPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet* pkt) {
00652 hdr_cmn* hdr = hdr_cmn::access(pkt);
00653
00654 double size = (double) hdr->size();
00655
00656 if ((policy->cBucket - size) >= 0) {
00657 policy->cBucket -= size;
00658 return(policer->initialCodePt);
00659 } else{
00660 return(policer->downgrade1);
00661 }
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681 void SRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00682 double now = Scheduler::instance().clock();
00683 double tokenBytes;
00684
00685 tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
00686 if (policy->cBucket + tokenBytes <= policy->cbs)
00687 policy->cBucket += tokenBytes;
00688 else {
00689 tokenBytes = tokenBytes - (policy->cbs - policy->cBucket);
00690
00691 policy->cBucket = policy->cbs;
00692 if (policy->eBucket + tokenBytes <= policy->ebs)
00693 policy->eBucket += tokenBytes;
00694 else
00695 policy->eBucket = policy->ebs;
00696 }
00697 policy->arrivalTime = now;
00698 }
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714 int SRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet* pkt) {
00715
00716 hdr_cmn* hdr = hdr_cmn::access(pkt);
00717 double size = (double) hdr->size();
00718
00719 if ((policy->cBucket - size) >= 0) {
00720 policy->cBucket -= size;
00721 return(policer->initialCodePt);
00722 } else {
00723 if ((policy->eBucket - size) >= 0) {
00724 policy->eBucket -= size;
00725 return(policer->downgrade1);
00726 } else
00727 return(policer->downgrade2);
00728 }
00729 }
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 void TRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00747 double now = Scheduler::instance().clock();
00748 double tokenBytes;
00749 tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
00750 if (policy->cBucket + tokenBytes <= policy->cbs)
00751 policy->cBucket += tokenBytes;
00752 else
00753 policy->cBucket = policy->cbs;
00754
00755 tokenBytes = (double) policy->pir * (now - policy->arrivalTime);
00756 if (policy->pBucket + tokenBytes <= policy->pbs)
00757 policy->pBucket += tokenBytes;
00758 else
00759 policy->pBucket = policy->pbs;
00760
00761 policy->arrivalTime = now;
00762 }
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778 int TRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet* pkt) {
00779 hdr_cmn* hdr = hdr_cmn::access(pkt);
00780 double size = (double) hdr->size();
00781
00782 if ((policy->pBucket - size) < 0)
00783 return(policer->downgrade2);
00784 else {
00785 if ((policy->cBucket - size) < 0) {
00786 policy->pBucket -= size;
00787 return(policer->downgrade1);
00788 } else {
00789 policy->cBucket -= size;
00790 policy->pBucket -= size;
00791 return(policer->initialCodePt);
00792 }
00793 }
00794 }
00795
00796
00797
00798
00799 SFDPolicy::SFDPolicy() : Policy() {
00800 flow_table.head = NULL;
00801 flow_table.tail = NULL;
00802 }
00803
00804
00805 SFDPolicy::~SFDPolicy(){
00806 struct flow_entry *p, *q;
00807 p = q = flow_table.head;
00808 while (p) {
00809 printf("free flow: %d\n", p->fid);
00810 q = p;
00811 p = p->next;
00812 free(q);
00813 }
00814
00815 p = q = NULL;
00816 flow_table.head = flow_table.tail = NULL;
00817 }
00818
00819
00820
00821
00822
00823
00824
00825 void SFDPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
00826 int fid, src_id, dst_id;
00827 struct flow_entry *p, *q, *new_entry;
00828
00829 double now = Scheduler::instance().clock();
00830 hdr_cmn* hdr = hdr_cmn::access(pkt);
00831 hdr_ip* iph = hdr_ip::access(pkt);
00832 fid = iph->flowid();
00833 dst_id = iph->daddr();
00834 src_id = iph->saddr();
00835
00836
00837
00838
00839 p = q = flow_table.head;
00840 while (p) {
00841
00842 if (p->fid == fid) {
00843
00844 p->last_update = now;
00845 p->bytes_sent += hdr->size();
00846 return;
00847 } else if (p->last_update + FLOW_TIME_OUT < now){
00848
00849 if (p == flow_table.head){
00850 if (p == flow_table.tail) {
00851 flow_table.head = flow_table.tail = NULL;
00852 free(p);
00853 p = q = NULL;
00854 } else {
00855 flow_table.head = p->next;
00856 free(p);
00857 p = q = flow_table.head;
00858 }
00859 } else {
00860 q->next = p->next;
00861 if (p == flow_table.tail)
00862 flow_table.tail = q;
00863 free(p);
00864 p = q->next;
00865 }
00866 } else {
00867 q = p;
00868 p = q->next;
00869 }
00870 }
00871
00872
00873 if (!p) {
00874 new_entry = new flow_entry;
00875 new_entry->fid = fid;
00876 new_entry->src_id = src_id;
00877 new_entry->dst_id = dst_id;
00878 new_entry->last_update = now;
00879 new_entry->bytes_sent = hdr->size();
00880 new_entry->count = 0;
00881 new_entry->next = NULL;
00882
00883
00884 if (flow_table.tail)
00885 flow_table.tail->next = new_entry;
00886 else
00887 flow_table.head = new_entry;
00888 flow_table.tail = new_entry;
00889 }
00890
00891
00892 return;
00893 }
00894
00895
00896
00897
00898
00899 int SFDPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Packet *pkt) {
00900 int fid, src_id, dst_id;
00901 struct flow_entry *p;
00902
00903 hdr_ip* iph = hdr_ip::access(pkt);
00904 fid = iph->flowid();
00905 dst_id = iph->daddr();
00906 src_id = iph->saddr();
00907
00908
00909
00910
00911 p = flow_table.head;
00912 while (p) {
00913
00914 if (p->fid == iph->flowid()) {
00915
00916 if (p->bytes_sent > policy->cir) {
00917
00918 if (policer->downgrade2 == 0) {
00919
00920
00921 return(policer->downgrade1);
00922 } else if (policer->downgrade2 == 1) {
00923
00924 if (Random::uniform(0.0, 1.0) > (1 - (policy->cir/p->bytes_sent))) {
00925
00926 return(policer->initialCodePt);
00927 } else {
00928
00929 return(policer->downgrade1);
00930 }
00931 } else {
00932
00933 if (p->count == 5) {
00934
00935 p->count = 0;
00936
00937 return(policer->initialCodePt);
00938 } else {
00939 p->count++;
00940
00941 return(policer->downgrade1);
00942 }
00943 }
00944 } else {
00945
00946 return(policer->initialCodePt);
00947 }
00948 }
00949 p = p->next;
00950 }
00951
00952
00953 if (!p) {
00954 printf ("MISS: no flow %d (%d, %d) in the table\n", fid, src_id, dst_id);
00955 printFlowTable();
00956 };
00957
00958
00959 return(policer->initialCodePt);
00960 }
00961
00962
00963 void SFDPolicy::printFlowTable() {
00964 struct flow_entry *p;
00965 printf("Flow table:\n");
00966
00967 p = flow_table.head;
00968 while (p) {
00969 printf("flow id: %d [%d %d], bytesSent: %d, last_update: %f\n",
00970 p->fid, p->src_id, p->dst_id, p->bytes_sent, p->last_update);
00971 p = p-> next;
00972 }
00973 p = NULL;
00974 printf("\n");
00975 }
00976