/*											/*
 * Copyright (c) 2000 Nortel Networks							 * Copyright (c) 2000 Nortel Networks
 * All rights reserved.									 * All rights reserved.
 * 											 * 
 * Redistribution and use in source and binary forms, with or without			 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions			 * modification, are permitted provided that the following conditions
 * are met:										 * are met:
 * 1. Redistributions of source code must retain the above copyright			 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.			 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright			 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the		 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.		 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software		 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:					 *    must display the following acknowledgement:
 *      This product includes software developed by Nortel Networks.			 *      This product includes software developed by Nortel Networks.
 * 4. The name of the Nortel Networks may not be used					 * 4. The name of the Nortel Networks may not be used
 *    to endorse or promote products derived from this software without			 *    to endorse or promote products derived from this software without
 *    specific prior written permission.						 *    specific prior written permission.
 * 											 * 
 * THIS SOFTWARE IS PROVIDED BY NORTEL AND CONTRIBUTORS ``AS IS'' AND			 * THIS SOFTWARE IS PROVIDED BY NORTEL AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE		 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE		 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL NORTEL OR CONTRIBUTORS BE LIABLE			 * ARE DISCLAIMED.  IN NO EVENT SHALL NORTEL OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL		 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS		 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)		 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT		 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY		 * 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		 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.										 * SUCH DAMAGE.
 *											 *
 * Developed by: Farhan Shallwani, Jeremy Ethridge					 * Developed by: Farhan Shallwani, Jeremy Ethridge
 *               Peter Pieda, and Mandeep Baines					 *               Peter Pieda, and Mandeep Baines
 * Maintainer: Peter Pieda <ppieda@nortelnetworks.com>					 * Maintainer: Peter Pieda <ppieda@nortelnetworks.com>
 */											 */

/* 											/* 
 *  Integrated into ns main distribution and reorganized by 				 *  Integrated into ns main distribution and reorganized by 
 *  Xuan Chen (xuanc@isi.edu). The main changes are:					 *  Xuan Chen (xuanc@isi.edu). The main changes are:
 *											 *
 *  1. Defined two seperated classes, PolicyClassifier and Policy, to handle 		 *  1. Defined two seperated classes, PolicyClassifier and Policy, to handle 
 *     the work done by class Policy before.						 *     the work done by class Policy before.
 *     Class PolicyClassifier now only keeps states for each flow and pointers		 *     Class PolicyClassifier now only keeps states for each flow and pointers
 *     to certain policies. 								 *     to certain policies. 
 *     The policies perform the diffserv related jobs as described			 *     The policies perform the diffserv related jobs as described
 *     below. (eg, traffic metering and packet marking.)				 *     below. (eg, traffic metering and packet marking.)
 *     class Policy functions like the class Classifer.					 *     class Policy functions like the class Classifer.
 *											 *
 *  2. Created a general supper class Policy so that new policy can be added		 *  2. Created a general supper class Policy so that new policy can be added
 *     by just creating a subclass of Policy. Examples are given (eg, 			 *     by just creating a subclass of Policy. Examples are given (eg, 
 *     DumbPolicy) to help people trying to add their own new policies.			 *     DumbPolicy) to help people trying to add their own new policies.
 *											 *
 *  TODO:										 *  TODO:
 *  1. implement the multiple policy support by applying the idea of 			 *  1. implement the multiple policy support by applying the idea of 
 *     multi-policy.									 *     multi-policy.
 *											 *
 */											 */

#include "dsPolicy.h"									#include "dsPolicy.h"
#include "ew.h"										#include "ew.h"
#include "packet.h"									#include "packet.h"
#include "tcp.h"									#include "tcp.h"
#include "random.h"									#include "random.h"

// The definition of class PolicyClassifier.						// The definition of class PolicyClassifier.
//Constructor.										//Constructor.
PolicyClassifier::PolicyClassifier() {							PolicyClassifier::PolicyClassifier() {
  int i;										  int i;

  policyTableSize = 0;									  policyTableSize = 0;
  policerTableSize = 0;									  policerTableSize = 0;

  for (i = 0; i < MAX_POLICIES; i++) 							  for (i = 0; i < MAX_POLICIES; i++) 
    policy_pool[i] = NULL;								    policy_pool[i] = NULL;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void addPolicyEntry()									void addPolicyEntry()
    Adds an entry to policyTable according to the arguments in argv.  A source		    Adds an entry to policyTable according to the arguments in argv.  A source
and destination node ID must be specified in argv, followed by a policy type		and destination node ID must be specified in argv, followed by a policy type
and policy-specific parameters.  Supported policies and their parameters		and policy-specific parameters.  Supported policies and their parameters
are:											are:

TSW2CM        InitialCodePoint  CIR							TSW2CM        InitialCodePoint  CIR
TSW3CM        InitialCodePoint  CIR  PIR						TSW3CM        InitialCodePoint  CIR  PIR
TokenBucket   InitialCodePoint  CIR  CBS						TokenBucket   InitialCodePoint  CIR  CBS
srTCM         InitialCodePoint  CIR  CBS  EBS						srTCM         InitialCodePoint  CIR  CBS  EBS
trTCM         InitialCodePoint  CIR  CBS  PIR  PBS					trTCM         InitialCodePoint  CIR  CBS  PIR  PBS

    No error-checking is performed on the parameters.  CIR and PIR should be		    No error-checking is performed on the parameters.  CIR and PIR should be
specified in bits per second; CBS, EBS, and PBS should be specified in bytes.		specified in bits per second; CBS, EBS, and PBS should be specified in bytes.

    If the Policy Table is full, this method prints an error message.			    If the Policy Table is full, this method prints an error message.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void PolicyClassifier::addPolicyEntry(int argc, const char*const* argv) {		void PolicyClassifier::addPolicyEntry(int argc, const char*const* argv) {
  if (policyTableSize == MAX_POLICIES)							  if (policyTableSize == MAX_POLICIES)
    printf("ERROR: Policy Table size limit exceeded.\n");				    printf("ERROR: Policy Table size limit exceeded.\n");
  else {										  else {
    policyTable[policyTableSize].sourceNode = atoi(argv[2]);				    policyTable[policyTableSize].sourceNode = atoi(argv[2]);
    policyTable[policyTableSize].destNode = atoi(argv[3]);				    policyTable[policyTableSize].destNode = atoi(argv[3]);
    policyTable[policyTableSize].codePt = atoi(argv[5]);				    policyTable[policyTableSize].codePt = atoi(argv[5]);
    policyTable[policyTableSize].arrivalTime = 0;					    policyTable[policyTableSize].arrivalTime = 0;
    policyTable[policyTableSize].winLen = 1.0;						    policyTable[policyTableSize].winLen = 1.0;
    											    
    if (strcmp(argv[4], "Dumb") == 0) {							    if (strcmp(argv[4], "Dumb") == 0) {
      if(!policy_pool[DUMB])								      if(!policy_pool[DUMB])
	policy_pool[DUMB] = new DumbPolicy;							policy_pool[DUMB] = new DumbPolicy;
      policyTable[policyTableSize].policy_index = DUMB;   				      policyTable[policyTableSize].policy_index = DUMB;   
      policyTable[policyTableSize].policer = dumbPolicer;				      policyTable[policyTableSize].policer = dumbPolicer;
      policyTable[policyTableSize].meter = dumbMeter;					      policyTable[policyTableSize].meter = dumbMeter;
    } else if (strcmp(argv[4], "TSW2CM") == 0) {					    } else if (strcmp(argv[4], "TSW2CM") == 0) {
      if(!policy_pool[TSW2CM])								      if(!policy_pool[TSW2CM])
	policy_pool[TSW2CM] = new TSW2CMPolicy;							policy_pool[TSW2CM] = new TSW2CMPolicy;
      policyTable[policyTableSize].policy_index = TSW2CM;   				      policyTable[policyTableSize].policy_index = TSW2CM;   
      policyTable[policyTableSize].policer = TSW2CMPolicer;				      policyTable[policyTableSize].policer = TSW2CMPolicer;
      policyTable[policyTableSize].meter = tswTagger;					      policyTable[policyTableSize].meter = tswTagger;

      policyTable[policyTableSize].cir =						      policyTable[policyTableSize].cir =
	policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;			policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
      if (argc == 8) policyTable[policyTableSize].winLen = (double) atof(argv[7]);/* 	      if (argc == 8) policyTable[policyTableSize].winLen = (double) atof(argv[7]);/* 
    } else if (strcmp(argv[4], "TSW3CM") == 0) {					    } else if (strcmp(argv[4], "TSW3CM") == 0) {
      if(!policy_pool[TSW3CM])								      if(!policy_pool[TSW3CM])
	policy_pool[TSW3CM] = new TSW3CMPolicy;							policy_pool[TSW3CM] = new TSW3CMPolicy;
      policyTable[policyTableSize].policy_index = TSW3CM;   				      policyTable[policyTableSize].policy_index = TSW3CM;   
      policyTable[policyTableSize].policer = TSW3CMPolicer;				      policyTable[policyTableSize].policer = TSW3CMPolicer;
      policyTable[policyTableSize].meter = tswTagger;					      policyTable[policyTableSize].meter = tswTagger;

      policyTable[policyTableSize].cir =						      policyTable[policyTableSize].cir =
	policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;			policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
      policyTable[policyTableSize].pir = (double) atof(argv[7]) / 8.0;			      policyTable[policyTableSize].pir = (double) atof(argv[7]) / 8.0;
    } else if (strcmp(argv[4], "TokenBucket") == 0) {					    } else if (strcmp(argv[4], "TokenBucket") == 0) {
      if(!policy_pool[TB])								      if(!policy_pool[TB])
	policy_pool[TB] = (Policy *) new TBPolicy;						policy_pool[TB] = (Policy *) new TBPolicy;
      policyTable[policyTableSize].policy_index = TB;   				      policyTable[policyTableSize].policy_index = TB;   
      policyTable[policyTableSize].policer = tokenBucketPolicer;			      policyTable[policyTableSize].policer = tokenBucketPolicer;
      policyTable[policyTableSize].meter = tokenBucketMeter;				      policyTable[policyTableSize].meter = tokenBucketMeter;
      											      
      policyTable[policyTableSize].cir =						      policyTable[policyTableSize].cir =
	policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;			policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
      policyTable[policyTableSize].cbs =						      policyTable[policyTableSize].cbs =
	policyTable[policyTableSize].cBucket = (double) atof(argv[7]);				policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
        									      <
//      printf("addPolicyEntry(cir: %f, cbs: %f)\n", policyTable[policyTableSize].cir <
//      printf("   %s %s %s %s %s %s %s %s\n\n", argv[0], argv[1], argv[2], argv[3],  <
      fflush(stdout);								      <
    } else if (strcmp(argv[4], "srTCM") == 0) {						    } else if (strcmp(argv[4], "srTCM") == 0) {
      if(!policy_pool[SRTCM])								      if(!policy_pool[SRTCM])
	policy_pool[SRTCM] = new SRTCMPolicy;							policy_pool[SRTCM] = new SRTCMPolicy;
      policyTable[policyTableSize].policy_index = SRTCM;   				      policyTable[policyTableSize].policy_index = SRTCM;   
      policyTable[policyTableSize].policer = srTCMPolicer;				      policyTable[policyTableSize].policer = srTCMPolicer;
      policyTable[policyTableSize].meter = srTCMMeter;      				      policyTable[policyTableSize].meter = srTCMMeter;      

      policyTable[policyTableSize].cir =						      policyTable[policyTableSize].cir =
	policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;			policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
      policyTable[policyTableSize].cbs =						      policyTable[policyTableSize].cbs =
	policyTable[policyTableSize].cBucket = (double) atof(argv[7]);				policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
      policyTable[policyTableSize].ebs =						      policyTable[policyTableSize].ebs =
	policyTable[policyTableSize].eBucket = (double) atof(argv[8]);				policyTable[policyTableSize].eBucket = (double) atof(argv[8]);
    } else if (strcmp(argv[4], "trTCM") == 0) {						    } else if (strcmp(argv[4], "trTCM") == 0) {
      if(!policy_pool[TRTCM])								      if(!policy_pool[TRTCM])
	policy_pool[TRTCM] = new TRTCMPolicy;							policy_pool[TRTCM] = new TRTCMPolicy;
      policyTable[policyTableSize].policy_index = TRTCM;  				      policyTable[policyTableSize].policy_index = TRTCM;  
      policyTable[policyTableSize].policer = trTCMPolicer;				      policyTable[policyTableSize].policer = trTCMPolicer;
      policyTable[policyTableSize].meter = trTCMMeter;					      policyTable[policyTableSize].meter = trTCMMeter;
										      |	      
      policyTable[policyTableSize].cir =						      policyTable[policyTableSize].cir =
	policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;			policyTable[policyTableSize].avgRate = (double) atof(argv[6]) / 8.0;
      policyTable[policyTableSize].cbs =						      policyTable[policyTableSize].cbs =
	policyTable[policyTableSize].cBucket = (double) atof(argv[7]);				policyTable[policyTableSize].cBucket = (double) atof(argv[7]);
      policyTable[policyTableSize].pir = (double) atof(argv[8]) / 8.0;			      policyTable[policyTableSize].pir = (double) atof(argv[8]) / 8.0;
      policyTable[policyTableSize].pbs =						      policyTable[policyTableSize].pbs =
	policyTable[policyTableSize].pBucket = (double) atof(argv[9]);				policyTable[policyTableSize].pBucket = (double) atof(argv[9]);
    } else if (strcmp(argv[4], "SFD") == 0) {						    } else if (strcmp(argv[4], "SFD") == 0) {
      if(!policy_pool[SFD])								      if(!policy_pool[SFD])
	policy_pool[SFD] = new SFDPolicy;							policy_pool[SFD] = new SFDPolicy;
      policyTable[policyTableSize].policy_index = SFD;					      policyTable[policyTableSize].policy_index = SFD;
      policyTable[policyTableSize].policer = SFDPolicer;				      policyTable[policyTableSize].policer = SFDPolicer;
      policyTable[policyTableSize].meter = sfdTagger;					      policyTable[policyTableSize].meter = sfdTagger;

      // Use cir as the transmission size threshold for the moment.			      // Use cir as the transmission size threshold for the moment.
      policyTable[policyTableSize].cir = atoi(argv[6]);					      policyTable[policyTableSize].cir = atoi(argv[6]);
    } else if (strcmp(argv[4], "EW") == 0) {						    } else if (strcmp(argv[4], "EW") == 0) {
      if(!policy_pool[EWP])								      if(!policy_pool[EWP])
	policy_pool[EWP] = new EWPolicy();							policy_pool[EWP] = new EWPolicy();
      											      
      ((EWPolicy *)policy_pool[EWP])->							      ((EWPolicy *)policy_pool[EWP])->
	init(atoi(argv[6]), atoi(argv[7]), atoi(argv[8]));					init(atoi(argv[6]), atoi(argv[7]), atoi(argv[8]));

      policyTable[policyTableSize].policy_index = EWP;					      policyTable[policyTableSize].policy_index = EWP;
      policyTable[policyTableSize].policer = EWPolicer;					      policyTable[policyTableSize].policer = EWPolicer;
      policyTable[policyTableSize].meter = ewTagger;					      policyTable[policyTableSize].meter = ewTagger;
  } else {										  } else {
      printf("No applicable policy specified, exit!!!\n");				      printf("No applicable policy specified, exit!!!\n");
      exit(-1);										      exit(-1);
    }											    }
    policyTableSize++;									    policyTableSize++;
  }											  }
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
policyTableEntry* PolicyClassifier::getPolicyTableEntry(long source, long dest)		policyTableEntry* PolicyClassifier::getPolicyTableEntry(long source, long dest)
Pre: policyTable holds exactly one entry for the specified source-dest pair.		Pre: policyTable holds exactly one entry for the specified source-dest pair.
Post: Finds the policyTable array that matches the specified source-dest pair.		Post: Finds the policyTable array that matches the specified source-dest pair.
Returns: On success, returns a pointer to the corresponding policyTableEntry;		Returns: On success, returns a pointer to the corresponding policyTableEntry;
  on failure, returns NULL.								  on failure, returns NULL.
Note: the source-destination pair could be one-any or any-any (xuanc)			Note: the source-destination pair could be one-any or any-any (xuanc)
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
policyTableEntry* PolicyClassifier::getPolicyTableEntry(nsaddr_t source, nsaddr_t des	policyTableEntry* PolicyClassifier::getPolicyTableEntry(nsaddr_t source, nsaddr_t des
  for (int i = 0; i <= policyTableSize; i++) {						  for (int i = 0; i <= policyTableSize; i++) {
    if ((policyTable[i].sourceNode == source) || (policyTable[i].sourceNode == ANY_HO	    if ((policyTable[i].sourceNode == source) || (policyTable[i].sourceNode == ANY_HO
      if ((policyTable[i].destNode == dest) || (policyTable[i].destNode == ANY_HOST))	      if ((policyTable[i].destNode == dest) || (policyTable[i].destNode == ANY_HOST))
	return(&policyTable[i]);								return(&policyTable[i]);
    }											    }
  }											  }
  											  
  // !!! Could make a default code point for undefined flows:				  // !!! Could make a default code point for undefined flows:
  printf("ERROR: %d No Policy Table entry found for Source %d-Destination %d.\n", thi |	  printf("ERROR: No Policy Table entry found for Source %d-Destination %d.\n", source
  printPolicyTable();									  printPolicyTable();
  return(NULL);										  return(NULL);
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void addPolicerEntry(int argc, const char*const* argv)					void addPolicerEntry(int argc, const char*const* argv)
Pre: argv contains a valid command line for adding a policer entry.			Pre: argv contains a valid command line for adding a policer entry.
Post: Adds an entry to policerTable according to the arguments in argv.  No		Post: Adds an entry to policerTable according to the arguments in argv.  No
  error-checking is done on the arguments.  A policer type should be specified,		  error-checking is done on the arguments.  A policer type should be specified,
  consisting of one of the names {TSW2CM, TSW3CM, TokenBucket,				  consisting of one of the names {TSW2CM, TSW3CM, TokenBucket,
  srTCM, trTCM}, followed by an initial code point.  Next should be an			  srTCM, trTCM}, followed by an initial code point.  Next should be an
  out-of-profile code point for policers with two-rate markers; or a yellow and		  out-of-profile code point for policers with two-rate markers; or a yellow and
  a red code point for policers with three drop precedences.				  a red code point for policers with three drop precedences.
      If policerTable is full, an error message is printed.				      If policerTable is full, an error message is printed.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void PolicyClassifier::addPolicerEntry(int argc, const char*const* argv) {		void PolicyClassifier::addPolicerEntry(int argc, const char*const* argv) {
  //int cur_policy;									  //int cur_policy;
  										      |
										      >
  if (policerTableSize == MAX_CP)							  if (policerTableSize == MAX_CP)
    printf("ERROR: Policer Table size limit exceeded.\n");				    printf("ERROR: Policer Table size limit exceeded.\n");
  else {										  else {
    if (strcmp(argv[2], "Dumb") == 0) {							    if (strcmp(argv[2], "Dumb") == 0) {
      if(!policy_pool[DUMB])								      if(!policy_pool[DUMB])
	policy_pool[DUMB] = new DumbPolicy;							policy_pool[DUMB] = new DumbPolicy;
      policerTable[policerTableSize].policer = dumbPolicer;      			      policerTable[policerTableSize].policer = dumbPolicer;      
      policerTable[policerTableSize].policy_index = DUMB;      				      policerTable[policerTableSize].policy_index = DUMB;      
    } else if (strcmp(argv[2], "TSW2CM") == 0) {					    } else if (strcmp(argv[2], "TSW2CM") == 0) {
      if(!policy_pool[TSW2CM])								      if(!policy_pool[TSW2CM])
	policy_pool[TSW2CM] = new TSW2CMPolicy;							policy_pool[TSW2CM] = new TSW2CMPolicy;
      policerTable[policerTableSize].policer = TSW2CMPolicer;				      policerTable[policerTableSize].policer = TSW2CMPolicer;
      policerTable[policerTableSize].policy_index = TSW2CM;      			      policerTable[policerTableSize].policy_index = TSW2CM;      
    } else if (strcmp(argv[2], "TSW3CM") == 0) {					    } else if (strcmp(argv[2], "TSW3CM") == 0) {
      if(!policy_pool[TSW3CM])								      if(!policy_pool[TSW3CM])
	policy_pool[TSW3CM] = new TSW3CMPolicy;							policy_pool[TSW3CM] = new TSW3CMPolicy;
      policerTable[policerTableSize].policer = TSW3CMPolicer;				      policerTable[policerTableSize].policer = TSW3CMPolicer;
      policerTable[policerTableSize].policy_index = TSW3CM;      			      policerTable[policerTableSize].policy_index = TSW3CM;      
    } else if (strcmp(argv[2], "TokenBucket") == 0) {					    } else if (strcmp(argv[2], "TokenBucket") == 0) {
      if(!policy_pool[TB])								      if(!policy_pool[TB])
	policy_pool[TB] = new TBPolicy;								policy_pool[TB] = new TBPolicy;
      policerTable[policerTableSize].policer = tokenBucketPolicer;			      policerTable[policerTableSize].policer = tokenBucketPolicer;
      policerTable[policerTableSize].policy_index = TB;      				      policerTable[policerTableSize].policy_index = TB;      
    } else if (strcmp(argv[2], "srTCM") == 0) {						    } else if (strcmp(argv[2], "srTCM") == 0) {
      if(!policy_pool[SRTCM])								      if(!policy_pool[SRTCM])
	policy_pool[SRTCM] = new SRTCMPolicy;							policy_pool[SRTCM] = new SRTCMPolicy;
      policerTable[policerTableSize].policer = srTCMPolicer;				      policerTable[policerTableSize].policer = srTCMPolicer;
      policerTable[policerTableSize].policy_index = SRTCM;      			      policerTable[policerTableSize].policy_index = SRTCM;      
    } else if (strcmp(argv[2], "trTCM") == 0){						    } else if (strcmp(argv[2], "trTCM") == 0){
      if(!policy_pool[TRTCM])								      if(!policy_pool[TRTCM])
	policy_pool[TRTCM] = new TRTCMPolicy;							policy_pool[TRTCM] = new TRTCMPolicy;
      policerTable[policerTableSize].policer = trTCMPolicer;				      policerTable[policerTableSize].policer = trTCMPolicer;
      policerTable[policerTableSize].policy_index = TRTCM;      			      policerTable[policerTableSize].policy_index = TRTCM;      
    } else if (strcmp(argv[2], "SFD") == 0) {						    } else if (strcmp(argv[2], "SFD") == 0) {
      if(!policy_pool[SFD])								      if(!policy_pool[SFD])
	policy_pool[SFD] = new SFDPolicy;							policy_pool[SFD] = new SFDPolicy;
      policerTable[policerTableSize].policer = SFDPolicer;				      policerTable[policerTableSize].policer = SFDPolicer;
      policerTable[policerTableSize].policy_index = SFD;      				      policerTable[policerTableSize].policy_index = SFD;      
    } else if (strcmp(argv[2], "EW") == 0) {						    } else if (strcmp(argv[2], "EW") == 0) {
      if(!policy_pool[EWP])								      if(!policy_pool[EWP])
	policy_pool[EWP] = new EWPolicy;							policy_pool[EWP] = new EWPolicy;
      policerTable[policerTableSize].policer = EWPolicer;				      policerTable[policerTableSize].policer = EWPolicer;
      policerTable[policerTableSize].policy_index = EWP;      				      policerTable[policerTableSize].policy_index = EWP;      
    } else {										    } else {
      printf("No applicable policer specified, exit!!!\n");				      printf("No applicable policer specified, exit!!!\n");
      exit(-1);										      exit(-1);
    }											    }
  };											  };
  											  
  policerTable[policerTableSize].initialCodePt = atoi(argv[3]);				  policerTable[policerTableSize].initialCodePt = atoi(argv[3]);
  policerTable[policerTableSize].downgrade1 = atoi(argv[4]);				  policerTable[policerTableSize].downgrade1 = atoi(argv[4]);
  if (argc == 6)									  if (argc == 6)
    policerTable[policerTableSize].downgrade2 = atoi(argv[5]);				    policerTable[policerTableSize].downgrade2 = atoi(argv[5]);
  policerTableSize++;									  policerTableSize++;
}											}

// Return the entry of Policer table with policerType and initCodePoint matched		// Return the entry of Policer table with policerType and initCodePoint matched
policerTableEntry* PolicyClassifier::getPolicerTableEntry(int policy_index, int oldCo	policerTableEntry* PolicyClassifier::getPolicerTableEntry(int policy_index, int oldCo
  for (int i = 0; i < policerTableSize; i++)						  for (int i = 0; i < policerTableSize; i++)
    if ((policerTable[i].policy_index == policy_index) &&				    if ((policerTable[i].policy_index == policy_index) &&
	(policerTable[i].initialCodePt == oldCodePt))						(policerTable[i].initialCodePt == oldCodePt))
      return(&policerTable[i]);								      return(&policerTable[i]);

  printf("ERROR: No Policer Table entry found for initial code point %d.\n", oldCodeP	  printf("ERROR: No Policer Table entry found for initial code point %d.\n", oldCodeP
  //printPolicerTable();								  //printPolicerTable();
  return(NULL);										  return(NULL);
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
int mark(Packet *pkt, double minRTT)							int mark(Packet *pkt, double minRTT)
Pre: The source-destination pair taken from pkt matches a valid entry in		Pre: The source-destination pair taken from pkt matches a valid entry in
  policyTable.										  policyTable.
Post: pkt is marked with an appropriate code point.					Post: pkt is marked with an appropriate code point.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int PolicyClassifier::mark(Packet *pkt) {						int PolicyClassifier::mark(Packet *pkt) {
  policyTableEntry *policy;								  policyTableEntry *policy;
  policerTableEntry *policer;								  policerTableEntry *policer;
  int policy_index;									  int policy_index;
  int codePt;										  int codePt;
  hdr_ip* iph;										  hdr_ip* iph;
  int fid;										  int fid;
										      |	  
  iph = hdr_ip::access(pkt);								  iph = hdr_ip::access(pkt);
  fid = iph->flowid();									  fid = iph->flowid();
  policy = getPolicyTableEntry(iph->saddr(), iph->daddr());				  policy = getPolicyTableEntry(iph->saddr(), iph->daddr());
  if (policy) {										  if (policy) {
    codePt = policy->codePt;								    codePt = policy->codePt;
    policy_index = policy->policy_index;						    policy_index = policy->policy_index;
    policer = getPolicerTableEntry(policy_index, codePt);				    policer = getPolicerTableEntry(policy_index, codePt);
  } else {									      <
    printf("getPolicy() failed\n");						      <
  }										      <

  if (policy_pool[policy_index]) {						      |	    // bug pointed by Jason Kenney <jason@linear.engmath.dal.ca>
    policy_pool[policy_index]->applyMeter(policy, pkt);				      |	    if (policy_pool[policy_index]) {
    codePt = policy_pool[policy_index]->applyPolicer(policy, policer, pkt);	      |	      policy_pool[policy_index]->applyMeter(policy, pkt);
										      >	      codePt = policy_pool[policy_index]->applyPolicer(policy, policer, pkt);
										      >	    }
  } else {										  } else {
    printf("The policy object doesn't exist, ERROR!!!\n");				    printf("The policy object doesn't exist, ERROR!!!\n");
    exit(-1);									      |	    exit(-1);    
  }											  }
										      |	  
  iph->prio_ = codePt;									  iph->prio_ = codePt;
  return(codePt);									  return(codePt);
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
Pre: The command line specifies a source and destination node for which an		Pre: The command line specifies a source and destination node for which an
  RTT-Aware policy exists and a current RTT value for that policy.			  RTT-Aware policy exists and a current RTT value for that policy.
Post: The aggRTT field of the appropriate policy is updated to a weighted		Post: The aggRTT field of the appropriate policy is updated to a weighted
  average of the previous value and the new RTT value specified in the command		  average of the previous value and the new RTT value specified in the command
  line.  If no matching policy is found, an error message is printed.			  line.  If no matching policy is found, an error message is printed.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void PolicyClassifier::updatePolicyRTT(int argc, const char*const* argv) {		void PolicyClassifier::updatePolicyRTT(int argc, const char*const* argv) {
  policyTableEntry *policy;								  policyTableEntry *policy;
  											  
  policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));				  policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));
  if (policy == NULL)									  if (policy == NULL)
    printf("ERROR: cannot update RTT; no existing policy found for Source %d-Desinati	    printf("ERROR: cannot update RTT; no existing policy found for Source %d-Desinati
	   atoi(argv[2]), atoi(argv[3]));							   atoi(argv[2]), atoi(argv[3]));
  else {										  else {
    policy->winLen = (double) atof(argv[4]);						    policy->winLen = (double) atof(argv[4]);
  }											  }
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
Pre: The command line specifies a source and destination node for which a		Pre: The command line specifies a source and destination node for which a
  policy exists that uses a cBucket value.  That policy's cBucket parameter is		  policy exists that uses a cBucket value.  That policy's cBucket parameter is
  currently valid.									  currently valid.
Post: The policy's cBucket value is found and returned.					Post: The policy's cBucket value is found and returned.
Returns: The value cBucket on success; or -1 on an error.				Returns: The value cBucket on success; or -1 on an error.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
double PolicyClassifier::getCBucket(const char*const* argv) {				double PolicyClassifier::getCBucket(const char*const* argv) {
  policyTableEntry *policy;								  policyTableEntry *policy;
  											  
  policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));				  policy = getPolicyTableEntry(atoi(argv[2]), atoi(argv[3]));
  if (policy == NULL) {									  if (policy == NULL) {
    printf("ERROR: cannot get bucket size; no existing policy found for Source %d-Des	    printf("ERROR: cannot get bucket size; no existing policy found for Source %d-Des
	   atoi(argv[2]), atoi(argv[3]));							   atoi(argv[2]), atoi(argv[3]));
    return(-1);										    return(-1);
  }											  }
  else {										  else {
    if ((policy->policer == tokenBucketPolicer) || (policy->policer == srTCMPolicer) 	    if ((policy->policer == tokenBucketPolicer) || (policy->policer == srTCMPolicer) 
      return(policy->cBucket);								      return(policy->cBucket);
    else {										    else {
      printf("ERROR: cannot get bucket size; the Source %d-Desination %d Policy does 	      printf("ERROR: cannot get bucket size; the Source %d-Desination %d Policy does 
      return(-1);									      return(-1);
    }											    }
  }											  }
}											}

//    Prints the policyTable, one entry per line.					//    Prints the policyTable, one entry per line.
void PolicyClassifier::printPolicyTable() {						void PolicyClassifier::printPolicyTable() {
  printf("Policy Table(%d):\n",policyTableSize);					  printf("Policy Table(%d):\n",policyTableSize);
  for (int i = 0; i < policyTableSize; i++)						  for (int i = 0; i < policyTableSize; i++)
    {											    {
      switch (policyTable[i].policer) {							      switch (policyTable[i].policer) {
      case dumbPolicer:									      case dumbPolicer:
	printf("Flow (%d to %d): DUMB policer, ",						printf("Flow (%d to %d): DUMB policer, ",
               policyTable[i].sourceNode,policyTable[i].destNode);			               policyTable[i].sourceNode,policyTable[i].destNode);
	printf("initial code point %d\n", policyTable[i].codePt);				printf("initial code point %d\n", policyTable[i].codePt);
	break;											break;
      case TSW2CMPolicer:								      case TSW2CMPolicer:
	printf("Flow (%d to %d): TSW2CM policer, ",						printf("Flow (%d to %d): TSW2CM policer, ",
               policyTable[i].sourceNode,policyTable[i].destNode);			               policyTable[i].sourceNode,policyTable[i].destNode);
	printf("initial code point %d, CIR %.1f bps.\n",					printf("initial code point %d, CIR %.1f bps.\n",
               policyTable[i].codePt, policyTable[i].cir * 8);				               policyTable[i].codePt, policyTable[i].cir * 8);
	break;											break;
      case TSW3CMPolicer:								      case TSW3CMPolicer:
	printf("Flow (%d to %d): TSW3CM policer, initial code ",				printf("Flow (%d to %d): TSW3CM policer, initial code ",
               policyTable[i].sourceNode, policyTable[i].destNode);			               policyTable[i].sourceNode, policyTable[i].destNode);
	printf("point %d, CIR %.1f bps, PIR %.1f bytes.\n",					printf("point %d, CIR %.1f bps, PIR %.1f bytes.\n",
               policyTable[i].codePt, policyTable[i].cir * 8,				               policyTable[i].codePt, policyTable[i].cir * 8,
               policyTable[i].pir * 8);							               policyTable[i].pir * 8);
	break;											break;
      case tokenBucketPolicer:								      case tokenBucketPolicer:
	printf("Flow (%d to %d): Token Bucket policer, ",					printf("Flow (%d to %d): Token Bucket policer, ",
               policyTable[i].sourceNode,policyTable[i].destNode);			               policyTable[i].sourceNode,policyTable[i].destNode);
	printf("initial code  point %d, CIR %.1f bps, CBS %.1f bytes.\n",			printf("initial code  point %d, CIR %.1f bps, CBS %.1f bytes.\n",
               policyTable[i].codePt, policyTable[i].cir * 8,				               policyTable[i].codePt, policyTable[i].cir * 8,
               policyTable[i].cbs);							               policyTable[i].cbs);
	break;											break;
      case srTCMPolicer:								      case srTCMPolicer:
	printf("Flow (%d to %d): srTCM policer, initial code ",					printf("Flow (%d to %d): srTCM policer, initial code ",
               policyTable[i].sourceNode, policyTable[i].destNode);			               policyTable[i].sourceNode, policyTable[i].destNode);
	printf("point %d, CIR %.1f bps, CBS %.1f bytes, EBS %.1f bytes.\n",			printf("point %d, CIR %.1f bps, CBS %.1f bytes, EBS %.1f bytes.\n",
               policyTable[i].codePt, policyTable[i].cir * 8,				               policyTable[i].codePt, policyTable[i].cir * 8,
               policyTable[i].cbs, policyTable[i].ebs);					               policyTable[i].cbs, policyTable[i].ebs);
	break;											break;
      case trTCMPolicer:								      case trTCMPolicer:
	printf("Flow (%d to %d): trTCM policer, initial code ",					printf("Flow (%d to %d): trTCM policer, initial code ",
               policyTable[i].destNode, policyTable[i].sourceNode);			               policyTable[i].destNode, policyTable[i].sourceNode);
	printf("point %d, CIR %.1f bps, CBS %.1f bytes, PIR %.1f bps, ",			printf("point %d, CIR %.1f bps, CBS %.1f bytes, PIR %.1f bps, ",
	       policyTable[i].codePt, policyTable[i].cir * 8,					       policyTable[i].codePt, policyTable[i].cir * 8,
               policyTable[i].cbs, policyTable[i].pir * 8);				               policyTable[i].cbs, policyTable[i].pir * 8);
	printf("PBS %.1f bytes.\n", policyTable[i].pbs);					printf("PBS %.1f bytes.\n", policyTable[i].pbs);
	break;											break;
      case SFDPolicer:									      case SFDPolicer:
	printf("Flow (%d to %d): SFD policer, ",						printf("Flow (%d to %d): SFD policer, ",
	       policyTable[i].sourceNode,policyTable[i].destNode);				       policyTable[i].sourceNode,policyTable[i].destNode);
	printf("initial code point %d, TH %d bytes.\n",						printf("initial code point %d, TH %d bytes.\n",
	       policyTable[i].codePt, (int)policyTable[i].cir);					       policyTable[i].codePt, (int)policyTable[i].cir);
	break;											break;
      case EWPolicer:									      case EWPolicer:
	printf("Flow (%d to %d): EW policer, ",							printf("Flow (%d to %d): EW policer, ",
	       policyTable[i].sourceNode,policyTable[i].destNode);				       policyTable[i].sourceNode,policyTable[i].destNode);
	printf("initial code point %d.\n", policyTable[i].codePt);				printf("initial code point %d.\n", policyTable[i].codePt);
	break;											break;
      default:										      default:
	printf("ERROR: Unknown policer type in Policy Table.\n");				printf("ERROR: Unknown policer type in Policy Table.\n");
      }											      }
    }											    }
  printf("\n");										  printf("\n");
}											}

// Prints the policerTable, one entry per line.						// Prints the policerTable, one entry per line.
void PolicyClassifier::printPolicerTable() {						void PolicyClassifier::printPolicerTable() {
  bool threeColor;									  bool threeColor;
  											  
  printf("Policer Table:\n");								  printf("Policer Table:\n");
  for (int i = 0; i < policerTableSize; i++) {						  for (int i = 0; i < policerTableSize; i++) {
    threeColor = false;									    threeColor = false;
    switch (policerTable[i].policer) {							    switch (policerTable[i].policer) {
    case dumbPolicer:									    case dumbPolicer:
      printf("Dumb ");									      printf("Dumb ");
      break;										      break;
    case TSW2CMPolicer:									    case TSW2CMPolicer:
      printf("TSW2CM ");								      printf("TSW2CM ");
      break;										      break;
    case TSW3CMPolicer:									    case TSW3CMPolicer:
      printf("TSW3CM ");								      printf("TSW3CM ");
      threeColor = true;								      threeColor = true;
      break;										      break;
    case tokenBucketPolicer:								    case tokenBucketPolicer:
      printf("Token Bucket ");								      printf("Token Bucket ");
      break;										      break;
    case srTCMPolicer:									    case srTCMPolicer:
      printf("srTCM ");									      printf("srTCM ");
      threeColor = true;								      threeColor = true;
      break;										      break;
    case trTCMPolicer:									    case trTCMPolicer:
      printf("trTCM ");									      printf("trTCM ");
      threeColor = true;								      threeColor = true;
      break;										      break;
    case SFDPolicer:									    case SFDPolicer:
      printf("SFD ");									      printf("SFD ");
      //printFlowTable();								      //printFlowTable();
      break;										      break;
    case EWPolicer:									    case EWPolicer:
      printf("EW ");									      printf("EW ");
      break;										      break;
    default:										    default:
      printf("ERROR: Unknown policer type in Policer Table.");				      printf("ERROR: Unknown policer type in Policer Table.");
    }											    }
    											    
    if (threeColor) {									    if (threeColor) {
      printf("policer code point %d is policed to yellow ",				      printf("policer code point %d is policed to yellow ",
	     policerTable[i].initialCodePt);							     policerTable[i].initialCodePt);
      printf("code point %d and red code point %d.\n",					      printf("code point %d and red code point %d.\n",
	     policerTable[i].downgrade1,							     policerTable[i].downgrade1,
	     policerTable[i].downgrade2);							     policerTable[i].downgrade2);
    } else										    } else
      printf("policer code point %d is policed to code point %d.\n",			      printf("policer code point %d is policed to code point %d.\n",
	     policerTable[i].initialCodePt,							     policerTable[i].initialCodePt,
	     policerTable[i].downgrade1);							     policerTable[i].downgrade1);
  }											  }
  printf("\n");										  printf("\n");
}											}

// The beginning of the definition of DumbPolicy					// The beginning of the definition of DumbPolicy
// DumbPolicy will do nothing, but is a good example to show how to add 		// DumbPolicy will do nothing, but is a good example to show how to add 
// new policy.										// new policy.

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void DumbPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			void DumbPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
Do nothing										Do nothing
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void DumbPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void DumbPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  policy->arrivalTime = Scheduler::instance().clock();  				  policy->arrivalTime = Scheduler::instance().clock();  
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
int DumbPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *pkt	int DumbPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *pkt
Always return the initial codepoint.							Always return the initial codepoint.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int DumbPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pa	int DumbPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pa
  return(policer->initialCodePt);							  return(policer->initialCodePt);
}											}

// The end of DumbPolicy								// The end of DumbPolicy

// The beginning of the definition of TSW2CM						// The beginning of the definition of TSW2CM
/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void TSW2CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			void TSW2CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
Pre: policy's variables avgRate, arrivalTime, and winLen hold valid values; and		Pre: policy's variables avgRate, arrivalTime, and winLen hold valid values; and
  pkt points to a newly-arrived packet.							  pkt points to a newly-arrived packet.
Post: Adjusts policy's TSW state variables avgRate and arrivalTime (also called		Post: Adjusts policy's TSW state variables avgRate and arrivalTime (also called
  tFront) according to the specified packet.						  tFront) according to the specified packet.
Note: See the paper "Explicit Allocation of Best effor Delivery Service" (David		Note: See the paper "Explicit Allocation of Best effor Delivery Service" (David
  Clark and Wenjia Fang), Section 3.3, for a description of the TSW Tagger.		  Clark and Wenjia Fang), Section 3.3, for a description of the TSW Tagger.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void TSW2CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void TSW2CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  double now, bytesInTSW, newBytes;							  double now, bytesInTSW, newBytes;
  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);
  											  
  bytesInTSW = policy->avgRate * policy->winLen;					  bytesInTSW = policy->avgRate * policy->winLen;
  newBytes = bytesInTSW + (double) hdr->size();						  newBytes = bytesInTSW + (double) hdr->size();
  now = Scheduler::instance().clock();							  now = Scheduler::instance().clock();
  policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);		  policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);
  policy->arrivalTime = now;								  policy->arrivalTime = now;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
int TSW2CMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *p	int TSW2CMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *p
Pre: policy points to a policytableEntry that is using the TSW2CM policer and		Pre: policy points to a policytableEntry that is using the TSW2CM policer and
  whose state variables (avgRate and cir) are up to date.				  whose state variables (avgRate and cir) are up to date.
Post: If policy's avgRate exceeds its CIR, this method returns an out-of-profile	Post: If policy's avgRate exceeds its CIR, this method returns an out-of-profile
  code point with a probability of ((rate - CIR) / rate).  If it does not		  code point with a probability of ((rate - CIR) / rate).  If it does not
  downgrade the code point, this method simply returns the initial code point.		  downgrade the code point, this method simply returns the initial code point.
Returns: A code point to apply to the current packet.					Returns: A code point to apply to the current packet.
Uses: Method downgradeOne().								Uses: Method downgradeOne().
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int TSW2CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, 	int TSW2CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, 
  if ((policy->avgRate > policy->cir)							  if ((policy->avgRate > policy->cir)
      && (Random::uniform(0.0, 1.0) <= (1-(policy->cir/policy->avgRate)))) {		      && (Random::uniform(0.0, 1.0) <= (1-(policy->cir/policy->avgRate)))) {
    return(policer->downgrade1);							    return(policer->downgrade1);
  }											  }
  else {										  else {
    return(policer->initialCodePt);							    return(policer->initialCodePt);
  }											  }
}											}

// The end of TSW2CM									// The end of TSW2CM

// The Beginning of TSW3CM								// The Beginning of TSW3CM
/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void TSW3CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			void TSW3CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
Pre: policy's variables avgRate, arrivalTime, and winLen hold valid values; and		Pre: policy's variables avgRate, arrivalTime, and winLen hold valid values; and
  pkt points to a newly-arrived packet.							  pkt points to a newly-arrived packet.
Post: Adjusts policy's TSW state variables avgRate and arrivalTime (also called		Post: Adjusts policy's TSW state variables avgRate and arrivalTime (also called
  tFront) according to the specified packet.						  tFront) according to the specified packet.
Note: See the paper "Explicit Allocation of Best effor Delivery Service" (David		Note: See the paper "Explicit Allocation of Best effor Delivery Service" (David
  Clark and Wenjia Fang), Section 3.3, for a description of the TSW Tagger.		  Clark and Wenjia Fang), Section 3.3, for a description of the TSW Tagger.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void TSW3CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void TSW3CMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  double now, bytesInTSW, newBytes;							  double now, bytesInTSW, newBytes;
  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);
  											  
  bytesInTSW = policy->avgRate * policy->winLen;					  bytesInTSW = policy->avgRate * policy->winLen;
  newBytes = bytesInTSW + (double) hdr->size();						  newBytes = bytesInTSW + (double) hdr->size();
  now = Scheduler::instance().clock();							  now = Scheduler::instance().clock();
  policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);		  policy->avgRate = newBytes / (now - policy->arrivalTime + policy->winLen);
  policy->arrivalTime = now;								  policy->arrivalTime = now;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
int TSW3CMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *p	int TSW3CMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *p
Pre: policy points to a policytableEntry that is using the TSW3CM policer and		Pre: policy points to a policytableEntry that is using the TSW3CM policer and
  whose state variables (avgRate, cir, and pir) are up to date.				  whose state variables (avgRate, cir, and pir) are up to date.
Post: Sets code points with the following probabilities when rate > PIR:		Post: Sets code points with the following probabilities when rate > PIR:

          red:    (rate - PIR) / rate							          red:    (rate - PIR) / rate
          yellow: (PIR - CIR) / rate							          yellow: (PIR - CIR) / rate
          green:  CIR / rate								          green:  CIR / rate

and with the following code points when CIR < rate <= PIR:				and with the following code points when CIR < rate <= PIR:

          red:    0									          red:    0
          yellow: (rate - CIR) / rate							          yellow: (rate - CIR) / rate
          green:  CIR / rate								          green:  CIR / rate

    When rate is under CIR, a packet is always marked green.				    When rate is under CIR, a packet is always marked green.
Returns: A code point to apply to the current packet.					Returns: A code point to apply to the current packet.
Uses: Methods downgradeOne() and downgradeTwo().					Uses: Methods downgradeOne() and downgradeTwo().
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int TSW3CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, 	int TSW3CMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, 
  double rand = policy->avgRate * (1.0 - Random::uniform(0.0, 1.0));			  double rand = policy->avgRate * (1.0 - Random::uniform(0.0, 1.0));
  											  
  if (rand > policy->pir)								  if (rand > policy->pir)
    return (policer->downgrade2);							    return (policer->downgrade2);
  else if (rand > policy->cir)								  else if (rand > policy->cir)
    return(policer->downgrade1);							    return(policer->downgrade1);
  else											  else
    return(policer->initialCodePt);							    return(policer->initialCodePt);
}											}
 											 
// End of TSW3CM									// End of TSW3CM

// Begin of Token Bucket.								// Begin of Token Bucket.
/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void TBPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			void TBPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
Pre: policy's variables cBucket, cir, cbs, and arrivalTime hold valid values.		Pre: policy's variables cBucket, cir, cbs, and arrivalTime hold valid values.
Post: Increments policy's Token Bucket state variable cBucket according to the		Post: Increments policy's Token Bucket state variable cBucket according to the
  elapsed time since the last packet arrival.  cBucket is filled at a rate equal  to 	  elapsed time since the last packet arrival.  cBucket is filled at a rate equal  to 
  This method also sets arrivalTime equal to the current simulator time.		  This method also sets arrivalTime equal to the current simulator time.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void TBPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void TBPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  double now = Scheduler::instance().clock();						  double now = Scheduler::instance().clock();
  double tokenBytes;									  double tokenBytes;

  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);			  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
  										      <
//  printf("TBPolicy::applyMeter(cir:%f, cbs:%f)\n", policy->cir, policy->cbs);	      <
  										      <
  if (policy->cBucket + tokenBytes <= policy->cbs)					  if (policy->cBucket + tokenBytes <= policy->cbs)
   policy->cBucket += tokenBytes;							   policy->cBucket += tokenBytes;
  else											  else
   policy->cBucket = policy->cbs;							   policy->cBucket = policy->cbs;
  policy->arrivalTime = now;								  policy->arrivalTime = now;
}											}

/*----------------------------------------------------------------------------		/*----------------------------------------------------------------------------
int TBPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt,			int TBPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt,
        Packet* pkt)									        Packet* pkt)
Pre: policy points to a policytableEntry that is using the Token Bucket policer		Pre: policy points to a policytableEntry that is using the Token Bucket policer
and whose state variable (cBucket) is up to date.  pkt points to a			and whose state variable (cBucket) is up to date.  pkt points to a
newly-arrived packet.									newly-arrived packet.
Post: If policy's cBucket is at least as large as pkt's size, cBucket is		Post: If policy's cBucket is at least as large as pkt's size, cBucket is
decremented by that size and the initial code point is retained.  Otherwise,		decremented by that size and the initial code point is retained.  Otherwise,
the code point is downgraded.								the code point is downgraded.
Returns: A code point to apply to the current packet.					Returns: A code point to apply to the current packet.
Uses: Method downgradeOne().								Uses: Method downgradeOne().
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int TBPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pack	int TBPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pack
  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);

  double size = (double) hdr->size();							  double size = (double) hdr->size();

  if ((policy->cBucket - size) >= 0) {							  if ((policy->cBucket - size) >= 0) {
    policy->cBucket -= size;								    policy->cBucket -= size;
//    printf("TBPolicy::applyPolicer(initial:%d)\n", policer->initialCodePt);	      <
    return(policer->initialCodePt);							    return(policer->initialCodePt);
  } else{										  } else{
//    printf("TBPolicy::applyPolicer(downgrade:%d)\n", policer->downgrade1);	      <
    return(policer->downgrade1);							    return(policer->downgrade1);
  }											  }
}											}

// End of Tocken Bucket.								// End of Tocken Bucket.

// Begining of SRTCM									// Begining of SRTCM
/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void SRTCMPolicy::applyMeter(policyTableEntry *policy)					void SRTCMPolicy::applyMeter(policyTableEntry *policy)
Pre: policy's variables cBucket, eBucket, cir, cbs, ebs, and arrivalTime hold		Pre: policy's variables cBucket, eBucket, cir, cbs, ebs, and arrivalTime hold
  valid values.										  valid values.
Post: Increments policy's srTCM state variables cBucket and eBucket according		Post: Increments policy's srTCM state variables cBucket and eBucket according
  to the elapsed time since the last packet arrival.  cBucket is filled at a		  to the elapsed time since the last packet arrival.  cBucket is filled at a
  rate equal to CIR, capped at an upper bound of CBS.  When cBucket is full		  rate equal to CIR, capped at an upper bound of CBS.  When cBucket is full
  (equal to CBS), eBucket is filled at a rate equal to CIR, capped at an upper		  (equal to CBS), eBucket is filled at a rate equal to CIR, capped at an upper
  bound of EBS.										  bound of EBS.
      This method also sets arrivalTime equal to the current				      This method also sets arrivalTime equal to the current
  simulator time.									  simulator time.
Note: See the Internet Draft, "A Single Rate Three Color Marker" (Heinanen et		Note: See the Internet Draft, "A Single Rate Three Color Marker" (Heinanen et
  al; May, 1999) for a description of the srTCM.					  al; May, 1999) for a description of the srTCM.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void SRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void SRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  double now = Scheduler::instance().clock();						  double now = Scheduler::instance().clock();
  double tokenBytes;									  double tokenBytes;
  											  
  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);			  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
  if (policy->cBucket + tokenBytes <= policy->cbs)					  if (policy->cBucket + tokenBytes <= policy->cbs)
    policy->cBucket += tokenBytes;							    policy->cBucket += tokenBytes;
  else {										  else {
    tokenBytes = tokenBytes - (policy->cbs - policy->cBucket);				    tokenBytes = tokenBytes - (policy->cbs - policy->cBucket);
    											    
    policy->cBucket = policy->cbs;							    policy->cBucket = policy->cbs;
    if (policy->eBucket + tokenBytes <= policy->ebs)					    if (policy->eBucket + tokenBytes <= policy->ebs)
      policy->eBucket += tokenBytes;							      policy->eBucket += tokenBytes;
    else										    else
      policy->eBucket = policy->ebs;							      policy->eBucket = policy->ebs;
  }											  }
  policy->arrivalTime = now;								  policy->arrivalTime = now;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
int SRTCMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet* pk	int SRTCMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet* pk
Pre: policy points to a policyTableEntry that is using the srTCM policer and		Pre: policy points to a policyTableEntry that is using the srTCM policer and
  whose state variables (cBucket and eBucket) are up to date.  pkt points to a		  whose state variables (cBucket and eBucket) are up to date.  pkt points to a
  newly-arrived packet.									  newly-arrived packet.
Post: If policy's cBucket is at least as large as pkt's size, cBucket is		Post: If policy's cBucket is at least as large as pkt's size, cBucket is
  decremented by that size and the initial code point is retained.  Otherwise,		  decremented by that size and the initial code point is retained.  Otherwise,
  if eBucket is at least as large as the packet, eBucket is decremented and the		  if eBucket is at least as large as the packet, eBucket is decremented and the
  yellow code point is returned.  Otherwise, the red code point is returned.		  yellow code point is returned.  Otherwise, the red code point is returned.
Returns: A code point to apply to the current packet.					Returns: A code point to apply to the current packet.
Uses: Method downgradeOne() and downgradeTwo().						Uses: Method downgradeOne() and downgradeTwo().
Note: See the Internet Draft, "A Single Rate Three Color Marker" (Heinanen et		Note: See the Internet Draft, "A Single Rate Three Color Marker" (Heinanen et
  al; May, 1999) for a description of the srTCM.					  al; May, 1999) for a description of the srTCM.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int SRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, P	int SRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, P

  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);
  double size = (double) hdr->size();							  double size = (double) hdr->size();
  											  
  if ((policy->cBucket - size) >= 0) {							  if ((policy->cBucket - size) >= 0) {
    policy->cBucket -= size;								    policy->cBucket -= size;
    return(policer->initialCodePt);							    return(policer->initialCodePt);
  } else {										  } else {
    if ((policy->eBucket - size) >= 0) {						    if ((policy->eBucket - size) >= 0) {
      policy->eBucket -= size;								      policy->eBucket -= size;
      return(policer->downgrade1);							      return(policer->downgrade1);
    } else										    } else
      return(policer->downgrade2);							      return(policer->downgrade2);
  }											  }
}											}

// End of SRTCM										// End of SRTCM

// Beginning of TRTCM									// Beginning of TRTCM
/*----------------------------------------------------------------------------		/*----------------------------------------------------------------------------
void TRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			void TRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
Pre: policy's variables cBucket, pBucket, cir, pir, cbs, pbs, and arrivalTime		Pre: policy's variables cBucket, pBucket, cir, pir, cbs, pbs, and arrivalTime
  hold valid values.									  hold valid values.
Post: Increments policy's trTCM state variables cBucket and pBucket according		Post: Increments policy's trTCM state variables cBucket and pBucket according
  to the elapsed time since the last packet arrival.  cBucket is filled at a		  to the elapsed time since the last packet arrival.  cBucket is filled at a
  rate equal to CIR, capped at an upper bound of CBS.  pBucket is filled at a		  rate equal to CIR, capped at an upper bound of CBS.  pBucket is filled at a
  rate equal to PIR, capped at an upper bound of PBS.					  rate equal to PIR, capped at an upper bound of PBS.
      This method also sets arrivalTime equal to the current simulator time.		      This method also sets arrivalTime equal to the current simulator time.
Note: See the Internet Draft, "A Two Rate Three Color Marker" (Heinanen et al;		Note: See the Internet Draft, "A Two Rate Three Color Marker" (Heinanen et al;
  May, 1999) for a description of the srTCM.						  May, 1999) for a description of the srTCM.
---------------------------------------------------------------------------*/		---------------------------------------------------------------------------*/
void TRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void TRTCMPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  double now = Scheduler::instance().clock();						  double now = Scheduler::instance().clock();
  double tokenBytes;									  double tokenBytes;
  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);			  tokenBytes = (double) policy->cir * (now - policy->arrivalTime);
  if (policy->cBucket + tokenBytes <= policy->cbs)					  if (policy->cBucket + tokenBytes <= policy->cbs)
    policy->cBucket += tokenBytes;							    policy->cBucket += tokenBytes;
  else											  else
    policy->cBucket = policy->cbs;							    policy->cBucket = policy->cbs;
  											  
  tokenBytes = (double) policy->pir * (now - policy->arrivalTime);			  tokenBytes = (double) policy->pir * (now - policy->arrivalTime);
  if (policy->pBucket + tokenBytes <= policy->pbs)					  if (policy->pBucket + tokenBytes <= policy->pbs)
    policy->pBucket += tokenBytes;							    policy->pBucket += tokenBytes;
  else											  else
    policy->pBucket = policy->pbs;							    policy->pBucket = policy->pbs;
  											  
  policy->arrivalTime = now;								  policy->arrivalTime = now;
}											}

/*----------------------------------------------------------------------------		/*----------------------------------------------------------------------------
int TRTCMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet* pk	int TRTCMPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet* pk
Pre: policy points to a policyTableEntry that is using the trTCM policer and		Pre: policy points to a policyTableEntry that is using the trTCM policer and
  whose state variables (cBucket and pBucket) are up to date.  pkt points to a		  whose state variables (cBucket and pBucket) are up to date.  pkt points to a
  newly-arrived packet.									  newly-arrived packet.
Post: If policy's pBucket is smaller than pkt's size, the red code point is		Post: If policy's pBucket is smaller than pkt's size, the red code point is
  retained.  Otherwise, if cBucket is smaller than the packet size, the yellow		  retained.  Otherwise, if cBucket is smaller than the packet size, the yellow
  code point is returned and pBucket is decremented.  Otherwise, the packet		  code point is returned and pBucket is decremented.  Otherwise, the packet
  remains green and both buckets are decremented.					  remains green and both buckets are decremented.
Returns: A code point to apply to the current packet.					Returns: A code point to apply to the current packet.
Uses: Method downgradeOne() and downgradeTwo().						Uses: Method downgradeOne() and downgradeTwo().
Note: See the Internet Draft, "A Two Rate Three Color Marker" (Heinanen et al;		Note: See the Internet Draft, "A Two Rate Three Color Marker" (Heinanen et al;
  May, 1999) for a description of the srTCM.						  May, 1999) for a description of the srTCM.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int TRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, P	int TRTCMPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, P
  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);
  double size = (double) hdr->size();							  double size = (double) hdr->size();
  											  
  if ((policy->pBucket - size) < 0)							  if ((policy->pBucket - size) < 0)
    return(policer->downgrade2);							    return(policer->downgrade2);
  else {										  else {
    if ((policy->cBucket - size) < 0) {							    if ((policy->cBucket - size) < 0) {
      policy->pBucket -= size;								      policy->pBucket -= size;
      return(policer->downgrade1);							      return(policer->downgrade1);
    } else {										    } else {
      policy->cBucket -= size;								      policy->cBucket -= size;
      policy->pBucket -= size;								      policy->pBucket -= size;
      return(policer->initialCodePt);							      return(policer->initialCodePt);
    }											    }
  }											  }
}											}
// End of TRTCM										// End of TRTCM

// Beginning of SFD									// Beginning of SFD
//Constructor.										//Constructor.
SFDPolicy::SFDPolicy() : Policy() {							SFDPolicy::SFDPolicy() : Policy() {
  flow_table.head = NULL;								  flow_table.head = NULL;
  flow_table.tail = NULL;								  flow_table.tail = NULL;
}											}

//Deconstructor.									//Deconstructor.
SFDPolicy::~SFDPolicy(){								SFDPolicy::~SFDPolicy(){
  struct flow_entry *p, *q;								  struct flow_entry *p, *q;
  p = q = flow_table.head;								  p = q = flow_table.head;
  while (p) {										  while (p) {
    printf("free flow: %d\n", p->fid);							    printf("free flow: %d\n", p->fid);
    q = p;										    q = p;
    p = p->next;									    p = p->next;
    free(q);										    free(q);
  }											  }

  p = q = NULL;										  p = q = NULL;
  flow_table.head = flow_table.tail = NULL;						  flow_table.head = flow_table.tail = NULL;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
 void SFDPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)			 void SFDPolicy::applyMeter(policyTableEntry *policy, Packet *pkt)
 Flow states are kept in a linked list.							 Flow states are kept in a linked list.
 Record how many bytes has been sent per flow and check if there is any flow		 Record how many bytes has been sent per flow and check if there is any flow
 timeout.										 timeout.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
void SFDPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {			void SFDPolicy::applyMeter(policyTableEntry *policy, Packet *pkt) {
  int fid;									      |	  int fid, src_id, dst_id;
  struct flow_entry *p, *q, *new_entry;							  struct flow_entry *p, *q, *new_entry;

  double now = Scheduler::instance().clock();						  double now = Scheduler::instance().clock();
  hdr_cmn* hdr = hdr_cmn::access(pkt);							  hdr_cmn* hdr = hdr_cmn::access(pkt);
  hdr_ip* iph = hdr_ip::access(pkt);							  hdr_ip* iph = hdr_ip::access(pkt);
  fid = iph->flowid();									  fid = iph->flowid();
  										      |	  dst_id = iph->daddr();
										      >	  src_id = iph->saddr();
										      >
  //  printf("enter applyMeter\n");							  //  printf("enter applyMeter\n");
										      >	  //  printFlowTable();

  p = q = flow_table.head;								  p = q = flow_table.head;
  while (p) {										  while (p) {
    // Check if the flow has been recorded before.					    // Check if the flow has been recorded before.
    if (p->fid == fid) {								    if (p->fid == fid) {
										      >	    //if (p->src_id == src_id && p->dst_id == dst_id ) {
      p->last_update = now;								      p->last_update = now;
      p->bytes_sent += hdr->size();							      p->bytes_sent += hdr->size();
      return;										      return;
    } else if (p->last_update + FLOW_TIME_OUT < now){					    } else if (p->last_update + FLOW_TIME_OUT < now){
      // The coresponding flow is expired.      					      // The coresponding flow is expired.      
      if (p == flow_table.head){							      if (p == flow_table.head){
	if (p == flow_table.tail) {								if (p == flow_table.tail) {
	  flow_table.head = flow_table.tail = NULL;						  flow_table.head = flow_table.tail = NULL;
	  free(p);										  free(p);
	  p = q = NULL;										  p = q = NULL;
	} else {										} else {
	  flow_table.head = p->next;								  flow_table.head = p->next;
	  free(p);										  free(p);
	  p = q = flow_table.head;								  p = q = flow_table.head;
	}											}
      } else {										      } else {
	q->next = p->next;									q->next = p->next;
	if (p == flow_table.tail)								if (p == flow_table.tail)
	  flow_table.tail = q;									  flow_table.tail = q;
	free(p);										free(p);
	p = q->next;										p = q->next;
      }											      }
    } else {										    } else {
      q = p;										      q = p;
      p = q->next;									      p = q->next;
    }											    }
  }											  }
  											  
  // This is the firt time the flow shown up						  // This is the firt time the flow shown up
  if (!p) {										  if (!p) {
    new_entry = new flow_entry;								    new_entry = new flow_entry;
    new_entry->fid = fid;								    new_entry->fid = fid;
										      >	    new_entry->src_id = src_id;
										      >	    new_entry->dst_id = dst_id;
    new_entry->last_update = now;							    new_entry->last_update = now;
    new_entry->bytes_sent = hdr->size();						    new_entry->bytes_sent = hdr->size();
    new_entry->count = 0;								    new_entry->count = 0;
    new_entry->next = NULL;								    new_entry->next = NULL;
    											    
    // always insert the new entry to the tail.						    // always insert the new entry to the tail.
    if (flow_table.tail)								    if (flow_table.tail)
      flow_table.tail->next = new_entry;						      flow_table.tail->next = new_entry;
    else										    else
      flow_table.head = new_entry;							      flow_table.head = new_entry;
    flow_table.tail = new_entry;							    flow_table.tail = new_entry;
  }											  }
  											  
  //  printf("leave applyMeter\n");							  //  printf("leave applyMeter\n");
  return;										  return;
}											}

/*-----------------------------------------------------------------------------		/*-----------------------------------------------------------------------------
void SFDPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *pkt	void SFDPolicy::applyPolicer(policyTableEntry *policy, int initialCodePt, Packet *pkt
    Prints the policyTable, one entry per line.						    Prints the policyTable, one entry per line.
-----------------------------------------------------------------------------*/		-----------------------------------------------------------------------------*/
int SFDPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pac	int SFDPolicy::applyPolicer(policyTableEntry *policy, policerTableEntry *policer, Pac
										      >	  int fid, src_id, dst_id;
  struct flow_entry *p;									  struct flow_entry *p;
										      >
  hdr_ip* iph = hdr_ip::access(pkt);							  hdr_ip* iph = hdr_ip::access(pkt);
  										      |	  fid = iph->flowid();
										      >	  dst_id = iph->daddr();
										      >	  src_id = iph->saddr();
										      >
  //  printf("enter applyPolicer\n");							  //  printf("enter applyPolicer\n");
  //  printFlowTable();								      |	  //printFlowTable();
  											  
  p = flow_table.head;									  p = flow_table.head;
  while (p) {										  while (p) {
    // Check if the flow has been recorded before.					    // Check if the flow has been recorded before.
    if (p->fid == iph->flowid()) {							    if (p->fid == iph->flowid()) {
										      >	      //if (p->src_id == src_id && p->dst_id == dst_id) {
      if (p->bytes_sent > policy->cir) {						      if (p->bytes_sent > policy->cir) {
	// Use downgrade2 code to judge how to penalize out-profile packets.			// Use downgrade2 code to judge how to penalize out-profile packets.
	if (policer->downgrade2 == 0) {								if (policer->downgrade2 == 0) {
	  // Penalize every packet beyond th.							  // Penalize every packet beyond th.
	  //printf("leave applyPolicer  %d, every downgrade\n", p->fid);			  //printf("leave applyPolicer  %d, every downgrade\n", p->fid);
	  return(policer->downgrade1);								  return(policer->downgrade1);
	} else if (policer->downgrade2 == 1) {							} else if (policer->downgrade2 == 1) {
	  // Randomized penalization.								  // Randomized penalization.
	  if (Random::uniform(0.0, 1.0) > (1 - (policy->cir/p->bytes_sent))) {			  if (Random::uniform(0.0, 1.0) > (1 - (policy->cir/p->bytes_sent))) {
	    //printf("leave applyPolicer %d, random initial.\n", p->fid);			    //printf("leave applyPolicer %d, random initial.\n", p->fid);
	    return(policer->initialCodePt);							    return(policer->initialCodePt);
	  } else {										  } else {
	    //printf("leave applyPolicer %d, random, downgrade\n", p->fid);			    //printf("leave applyPolicer %d, random, downgrade\n", p->fid);
	    return(policer->downgrade1);							    return(policer->downgrade1);
	  }											  }
	} else {										} else {
	  // Simple scheduling on penalization.							  // Simple scheduling on penalization.
	  if (p->count == 5) {									  if (p->count == 5) {
	    // Penalize 4 out of every 5 packets.						    // Penalize 4 out of every 5 packets.
	    p->count = 0;									    p->count = 0;
	    //printf("leave applyPolicer %d, initial, %d\n", p->fid, p->count);			    //printf("leave applyPolicer %d, initial, %d\n", p->fid, p->count);
	    return(policer->initialCodePt);							    return(policer->initialCodePt);
	  } else {										  } else {
	    p->count++;										    p->count++;
	    //printf("leave applyPolicer %d, downgrade, %d\n", p->fid, p->count);		    //printf("leave applyPolicer %d, downgrade, %d\n", p->fid, p->count);
	    return(policer->downgrade1);							    return(policer->downgrade1);
	  }											  }
	}											}
      } else {										      } else {
	//	printf("leave applyPolicer, initial\n");					//	printf("leave applyPolicer, initial\n");
	return(policer->initialCodePt);								return(policer->initialCodePt);
      }											      }
    }											    }
    p = p->next;									    p = p->next;
  }											  }
  											  
  // Can't find the record for this flow.						  // Can't find the record for this flow.
  if (!p) {										  if (!p) {
    printf ("MISS: no flow %d in the table\n", iph->flowid());			      |	    printf ("MISS: no flow %d (%d, %d) in the table\n", fid, src_id, dst_id);
    printFlowTable();									    printFlowTable();
};											};
  											  
  //  printf("leave applyPolicer, init but problem...\n");				  //  printf("leave applyPolicer, init but problem...\n");
  return(policer->initialCodePt);							  return(policer->initialCodePt);
}											}

//    Prints the flowTable, one entry per line.						//    Prints the flowTable, one entry per line.
void SFDPolicy::printFlowTable() {							void SFDPolicy::printFlowTable() {
  struct flow_entry *p;									  struct flow_entry *p;
  printf("Flow table:\n");								  printf("Flow table:\n");

  p = flow_table.head;									  p = flow_table.head;
  while (p) {										  while (p) {
    printf("flow id: %d, bytesSent: %d, last_update: %f\n", p->fid, p->bytes_sent, p- |	    printf("flow id: %d [%d %d], bytesSent: %d, last_update: %f\n", 
										      >		   p->fid, p->src_id, p->dst_id, p->bytes_sent, p->last_update);
    p = p-> next;									    p = p-> next;
  }											  }
  p = NULL;										  p = NULL;
  printf("\n");										  printf("\n");
}											}
// End of SFD										// End of SFD
