1 /* Copyright (c) 2011, 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Copyright (c) 2010 Regents of the University of California
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation;
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Author: Duy Nguyen<duy@soe.ucsc.edu>
28 #include "ns3/uinteger.h"
29 #include "ns3/double.h"
30 #include "red-queue.h"
31 #include "ns3/simulator.h"
32 #include "ns3/nstime.h"
36 #define RED_STATS_TABLE_SIZE 256
37 #define RED_STATS_MASK (RED_STATS_TABLE_SIZE - 1)
41 NS_OBJECT_ENSURE_REGISTERED (RedQueue);
43 TypeId RedQueue::GetTypeId (void)
45 ///< Note: these parameters must be worked out beforehand for RED to work correctly
46 ///< How these parameters are set up can affect RED performance greatly
47 static TypeId tid = TypeId ("ns3::RedQueue")
49 .AddConstructor<RedQueue> ()
50 .AddAttribute ("Mode",
51 "Whether to use Bytes (see MaxBytes) or Packets (see MaxPackets) as the maximum queue size metric.",
52 EnumValue (BYTES), ///> currently supports BYTES only
53 MakeEnumAccessor (&RedQueue::SetMode),
54 MakeEnumChecker (BYTES, "Bytes",
56 .AddAttribute ("MaxPackets",
57 "The maximum number of packets accepted by this RedQueue.",
59 MakeUintegerAccessor (&RedQueue::m_maxPackets),
60 MakeUintegerChecker<uint32_t> ())
61 .AddAttribute ("MaxBytes",
62 "The maximum number of bytes accepted by this RedQueue.",
63 UintegerValue (100000),
64 MakeUintegerAccessor (&RedQueue::m_maxBytes),
65 MakeUintegerChecker<uint32_t> ())
66 .AddAttribute ("m_burst",
67 "maximum number of m_burst packets accepted by this queue",
68 UintegerValue (6), ///> bursts must be > minTh/avpkt
69 MakeUintegerAccessor (&RedQueue::m_burst),
70 MakeUintegerChecker<uint32_t> ())
71 .AddAttribute ("m_avPkt",
72 "In bytes, use with m_burst to determine the time constant for average queue size calculations",
73 UintegerValue (1024), ///> average packet size
74 MakeUintegerAccessor (&RedQueue::m_avPkt),
75 MakeUintegerChecker<uint32_t> ())
76 .AddAttribute ("m_minTh",
77 "Average queue size at which marking becomes a m_prob",
78 UintegerValue (5120), ///> in bytes 1024x5
79 MakeUintegerAccessor (&RedQueue::m_minTh),
80 MakeUintegerChecker<uint32_t> ())
81 .AddAttribute ("m_maxTh",
82 "Maximal marking m_prob, should be at least twice min to prevent synchronous retransmits",
83 UintegerValue (15360), ///> in bytes 1024x15
84 MakeUintegerAccessor (&RedQueue::m_maxTh),
85 MakeUintegerChecker<uint32_t> ())
86 .AddAttribute ("m_rate",
87 "this m_rate is used for calculating the average queue size after some idle time.",
88 UintegerValue (1500000), ///> in bps, should be set to bandwidth of interface
89 MakeUintegerAccessor (&RedQueue::m_rate),
90 MakeUintegerChecker<uint64_t> ())
91 .AddAttribute ("m_prob",
92 "Probability for marking, suggested values are 0.01 and 0.02",
94 MakeDoubleAccessor (&RedQueue::m_prob),
95 MakeDoubleChecker <double> ())
101 RedQueue::RedQueue ()
113 m_initialized (false)
116 m_sTable = Uint32tVector (RED_STATS_TABLE_SIZE);
120 RedQueue::~RedQueue ()
125 RedQueue::SetMode (enum Mode mode)
131 RedQueue::GetMode (void)
137 RedQueue::GetAverageQueueSize (void)
145 * Given minimum threshold min_th and that we wish to allow bursts of L packets
146 * Then Wq should be chosen to satisfy avg_L < min_th
147 * L + 1 + [(1-Wq)^(L+1) - 1]/ Wq < min_th
148 * L + 1 - min_th < [1 - (1-Wq)^(L+1)]/Wq
149 * i.e. given min_th 5, L=50, necessary that Wq <= 0.0042
152 * burst + 1 - minTh/avPkt < (1-(1-W)^burst)/W
153 * this low-pass filter is used to calculate the avg queue size
157 RedQueue::evalEwma (uint32_t minTh, uint32_t burst, uint32_t avpkt)
167 ///< Note: bursts must be larger than minTh/avpkt for it to work
168 temp = (double)burst + 1 - (double)minTh / avpkt;
180 * wlog =4 , W = .0625
181 * wlog =5 , W = .03125
182 * wlog =6 , W = .015625
183 * wlog =7 , W = .0078125
184 * wlog =8 , W = .00390625
185 * wlog =9 , W = .001953125
186 * wlog =10, W = .0009765625
188 for (wlog = 1; wlog < 32; wlog++, W /= 2)
190 if (temp <= (1 - pow (1 - W, burst)) / W )
201 * Plog = log (prob / (maxTh -minTh) );
203 * Paper says: When a packet arrives at the gateway and the average queue size
204 * is between min_th and max_th, the initial packet marking probability is:
207 * C1 = maxP/(max_th - mint_th)
208 * C2 = maxP*min_th/(max_th - mint_th)
209 * maxP could be chosen so that C1 a power of two
212 RedQueue::evalP (uint32_t minTh, uint32_t maxTh, double prob)
215 uint32_t i = maxTh - minTh ;
224 ///< It returns the index that makes C1 a power of two
225 for (i = 0; i < 32; i++)
237 //NS_LOG_DEBUG ("i >= 32, this shouldn't happen");
241 //NS_LOG_DEBUG ("\t i(makes C1 power of two)=" << i);
247 * avg = avg*(1-W)^m where m = t/xmitTime
249 * m_sTable[ t/2^cellLog] = -log(1-W) * t/xmitTime
250 * m_sTable[ t >> cellLog]= -log(1-W) * t/xmitTime
252 * t is converted to t/2^cellLog for storage in the table
253 * find out what is cellLog and return it
257 RedQueue::evalIdleDamping (uint32_t wLog, uint32_t avpkt, uint32_t bps)
260 ///> in microsecond ticks: 1 sec = 1000000 microsecond ticks
261 double xmitTime = ((double) avpkt / bps) * 1000000;
263 ///> -log(1 - 1/2^wLog) / xmitTime
264 ///> note W = 1/2^wLog
265 double wLogTemp = -log (1.0 - 1.0 / (1 << wLog)) / xmitTime;
268 ///> the maximum allow idle time
269 double maxTime = 31 / wLogTemp;
271 //NS_LOG_DEBUG ("\t xmitTime=" << xmitTime << " wLogTemp=" << wLogTemp
272 // << " maxTime=" << maxTime);
277 for (cLog = 0; cLog < 32; cLog++)
280 ///> maxTime < 512* 2^cLog
281 ///> finds the cLog that's able to cover this maxTime
282 if (maxTime / (1 << cLog) < 512)
295 for (i = 1; i < 255; i++)
297 ///> wLogTemp * i * 2^cLog
298 m_sTable[i] = (i << cLog) * wLogTemp;
301 if (m_sTable[i] > 31)
309 //NS_LOG_DEBUG ("\t cLog=" << cLog);
316 RedQueue::Rmask (uint32_t pLog)
318 ///> ~OUL creates a 32 bit mask
320 return pLog < 32 ? ((1 << pLog) - 1) : (uint32_t) ~0UL;
326 RedQueue::SetParams (uint32_t minTh, uint32_t maxTh,
327 uint32_t wLog, uint32_t pLog, uint64_t scellLog)
336 m_rmask = Rmask (pLog);
337 m_scellLog = scellLog;
338 m_scellMax = (255 << m_scellLog);
340 //NS_LOG_DEBUG ("\t m_wLog" << m_wLog << " m_pLog" << m_pLog << " m_scellLog" << m_scellLog
341 // << " m_minTh" << m_minTh << " m_maxTh" << m_maxTh
342 // << " rmask=" << m_rmask << " m_scellMax=" << m_scellMax);
346 RedQueue::IsIdling ()
349 if ( m_idleStart.GetNanoSeconds () != 0)
351 //NS_LOG_DEBUG ("\t IsIdling");
354 return m_idleStart.GetNanoSeconds () != 0;
357 RedQueue::StartIdlePeriod ()
359 m_idleStart = Simulator::Now ();
362 RedQueue::EndIdlePeriod ()
364 m_idleStart = NanoSeconds (0);
379 * m is the number of pkts that might have been transmitted by the gateway
380 * during the time that the queue was free
381 * s is a typical transmission for a packet
383 * m = idletime / (average pkt size / bandwidth)
387 * We need to precompute a table for this calculation because of the exp power
391 RedQueue::AvgFromIdleTime ()
396 idleTime = ns3::Time(Simulator::Now() - m_idleStart).GetMicroSeconds();
397 //idleTime = RedTimeToInteger (Simulator::Now() - m_idleStart, Time::US);
399 if (idleTime > m_scellMax)
401 idleTime = m_scellMax;
404 //NS_LOG_DEBUG ("\t idleTime=" << idleTime);
407 shift = m_sTable [(idleTime >> m_scellLog) & RED_STATS_MASK];
411 //std::cout << "shift " << m_qavg << "=>" << (m_qavg >> shift) << std::endl;
412 return m_qavg >> shift;
416 idleTime = (m_qavg * idleTime) >> m_scellLog;
419 // NS_LOG_DEBUG ("\t idleus=" << idleTime);
421 if (idleTime < (m_qavg / 2))
423 //std::cout <<"idleus " << m_qavg << " - " << idleus << " = " << (m_qavg-idleus) << std::endl;
424 return m_qavg - idleTime;
428 //std:: cout <<"half " << m_qavg << "=>" << (m_qavg/2) << std::endl;
429 return (m_qavg / 2) ;
435 RedQueue::AvgFromNonIdleTime (uint32_t backlog)
437 //NS_LOG_FUNCTION (this << backlog);
439 //NS_LOG_DEBUG ("qavg " << m_qavg);
440 //NS_LOG_DEBUG ("backlog" << backlog);
443 * This is basically EWMA
444 * m_qavg = q_avg*(1-W) + backlog*W
445 * m_qavg = q_avg + W(backlog - q_avg)
448 return m_qavg + (backlog - (m_qavg >> m_wLog));
452 RedQueue::AvgCalc (uint32_t backlog)
454 //NS_LOG_FUNCTION (this << backlog);
460 qtemp = AvgFromNonIdleTime (backlog);
461 //NS_LOG_DEBUG ("NonIdle Avg " << qtemp);
462 //std::cout <<"n "<< qtemp << std::endl;
467 qtemp = AvgFromIdleTime ();
468 //NS_LOG_DEBUG ("Idle Avg" << qtemp);
469 //std::cout <<"i "<< qtemp << std::endl;
475 RedQueue::CheckThresh (uint64_t avg)
478 //NS_LOG_FUNCTION (this << avg);
479 //NS_LOG_DEBUG ("\t check threshold: min " << m_minTh << " max" << m_maxTh);
483 return BELOW_MIN_THRESH;
485 else if (avg >= m_maxTh)
487 return ABOVE_MAX_THRESH;
491 return BETWEEN_THRESH;
495 RedQueue::RedRandom ()
497 //NS_LOG_FUNCTION_NOARGS ();
499 ///> obtain a random u32 number
500 ///> return m_rmask & ran.GetInteger ();
502 return m_rmask & rand ();
505 RedQueue::MarkProbability (uint64_t avg)
507 //NS_LOG_FUNCTION (this << avg);
508 //NS_LOG_DEBUG ("\t m_randNum " << m_randNum);
509 //NS_LOG_DEBUG ("\t right\t" << m_randNum);
510 //NS_LOG_DEBUG ("\t left\t" << ((avg - m_minTh)*m_count));
512 ///> max_P* (qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
513 //return !((avg - m_minTh ) * m_count < m_randNum);
515 return !((avg - m_minTh )* m_count < m_randNum);
519 RedQueue::Processing (uint64_t qavg)
522 //NS_LOG_FUNCTION (this << "qavg" << qavg << " m_minTh" << m_minTh << " m_maxTh" << m_maxTh);
524 switch (CheckThresh (qavg))
526 case BELOW_MIN_THRESH:
527 //NS_LOG_DEBUG ("\t below threshold ");
533 //NS_LOG_DEBUG ("\t between threshold ");
537 //NS_LOG_DEBUG ("\t check Mark Prob");
538 if (MarkProbability (qavg))
541 m_randNum = RedRandom ();
543 //NS_LOG_DEBUG ("\t Marked Will Drop " << m_qavg);
547 //NS_LOG_DEBUG ("\t Marked Will Save " << m_qavg);
551 m_randNum = RedRandom ();
555 case ABOVE_MAX_THRESH:
557 //NS_LOG_DEBUG ("\t above threshold ");
563 //NS_LOG_DEBUG ("BUG HERE\n");
569 RedQueue::DoEnqueue (Ptr<Packet> p)
571 //NS_LOG_FUNCTION (this << p);
573 if (m_mode == PACKETS && (m_packets.size () >= m_maxPackets))
575 //NS_LOG_LOGIC ("Queue full (at max packets) -- droppping pkt");
580 if (m_mode == BYTES && (m_bytesInQueue + p->GetSize () >= m_maxBytes))
582 //NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- droppping pkt");
589 // making sure all the variables are initialized ok
590 //NS_LOG_DEBUG ("\t m_maxPackets" << m_maxPackets
591 // << " m_maxBytes" << m_maxBytes
592 // << " m_burst" << m_burst << " m_avPkt" << m_avPkt
593 // << " m_minTh" << m_minTh << " m_maxTh" << m_maxTh
594 // << " m_rate" << m_rate << " m_prob" << m_prob);
596 m_wLog = evalEwma (m_minTh, m_burst, m_avPkt);
597 m_pLog = evalP (m_minTh, m_maxTh, m_prob);
598 m_scellLog = evalIdleDamping (m_wLog, m_avPkt, m_rate);
600 SetParams (m_minTh, m_maxTh, m_wLog, m_pLog, m_scellLog);
602 // srand((unsigned)time(0));
603 m_initialized = true;
608 if (GetMode () == BYTES)
610 m_qavg = AvgCalc (m_bytesInQueue);
612 else if (GetMode () == PACKETS)
615 // m_qavg = AvgCalc (m_packets.size ());
618 //NS_LOG_DEBUG ("\t bytesInQueue " << m_bytesInQueue << "\tQavg " << m_qavg);
619 //NS_LOG_DEBUG ("\t packetsInQueue " << m_packets.size () << "\tQavg " << m_qavg);
627 switch (Processing (m_qavg) )
633 //NS_LOG_DEBUG ("\t Dropping due to Prob Mark " << m_qavg);
640 //NS_LOG_DEBUG ("\t Dropping due to Hard Mark " << m_qavg);
641 m_stats.forcedMark++;
648 m_bytesInQueue += p->GetSize ();
649 m_packets.push_back (p);
651 //NS_LOG_LOGIC ("Number packets " << m_packets.size ());
652 //NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue);
658 RedQueue::DoDequeue (void)
660 //NS_LOG_FUNCTION (this);
662 if (m_packets.empty ())
664 //NS_LOG_LOGIC ("Queue empty");
668 Ptr<Packet> p = m_packets.front ();
669 m_packets.pop_front ();
670 m_bytesInQueue -= p->GetSize ();
672 //NS_LOG_LOGIC ("Popped " << p);
674 //NS_LOG_LOGIC ("Number packets " << m_packets.size ());
675 //NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue);
677 if (m_bytesInQueue <= 0 && !IsIdling ())
685 ///> just for completeness
686 /// m_packets.remove (p) also works
688 RedQueue::DropPacket (Ptr<Packet> p)
691 //NS_LOG_FUNCTION (this << p);
693 //NS_LOG_DEBUG ("\t Dropping Packet p");
695 std::list<Ptr<Packet> >::iterator iter;
698 for (iter = m_packets.begin(); iter != m_packets.end(); ++iter)
702 packetSize= p->GetSize ();
703 m_packets.erase(iter);
704 m_bytesInQueue -= packetSize;
718 RedQueue::DoPeek (void) const
720 //NS_LOG_FUNCTION (this);
722 if (m_packets.empty ())
724 //NS_LOG_LOGIC ("Queue empty");
728 Ptr<Packet> p = m_packets.front ();
730 //NS_LOG_LOGIC ("Number packets " << m_packets.size ());
731 //NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue);
737 RedQueue::PrintTable ()
739 //NS_LOG_FUNCTION_NOARGS ();
741 for (uint32_t i = 0; i < RED_STATS_TABLE_SIZE; i++)
743 std::cout << m_sTable[i] << " ";
745 std::cout << std::endl;