1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Universita' degli Studi di Napoli Federico II
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Authors: Pasquale Imputato <p.imputato@gmail.com>
19  *          Stefano Avallone <stefano.avallone@unina.it>
20  */
21 
22 #ifndef FQ_CODEL_QUEUE_DISC
23 #define FQ_CODEL_QUEUE_DISC
24 
25 #include "ns3/queue-disc.h"
26 #include "ns3/object-factory.h"
27 #include <list>
28 #include <map>
29 
30 namespace ns3 {
31 
32 /**
33  * \ingroup traffic-control
34  *
35  * \brief A flow queue used by the FqCoDel queue disc
36  */
37 
38 class FqCoDelFlow : public QueueDiscClass {
39 public:
40   /**
41    * \brief Get the type ID.
42    * \return the object TypeId
43    */
44   static TypeId GetTypeId (void);
45   /**
46    * \brief FqCoDelFlow constructor
47    */
48   FqCoDelFlow ();
49 
50   virtual ~FqCoDelFlow ();
51 
52   /**
53    * \enum FlowStatus
54    * \brief Used to determine the status of this flow queue
55    */
56   enum FlowStatus
57     {
58       INACTIVE,
59       NEW_FLOW,
60       OLD_FLOW
61     };
62 
63   /**
64    * \brief Set the deficit for this flow
65    * \param deficit the deficit for this flow
66    */
67   void SetDeficit (uint32_t deficit);
68   /**
69    * \brief Get the deficit for this flow
70    * \return the deficit for this flow
71    */
72   int32_t GetDeficit (void) const;
73   /**
74    * \brief Increase the deficit for this flow
75    * \param deficit the amount by which the deficit is to be increased
76    */
77   void IncreaseDeficit (int32_t deficit);
78   /**
79    * \brief Set the status for this flow
80    * \param status the status for this flow
81    */
82   void SetStatus (FlowStatus status);
83   /**
84    * \brief Get the status of this flow
85    * \return the status of this flow
86    */
87   FlowStatus GetStatus (void) const;
88   /**
89    * \brief Set the index for this flow
90    * \param index the index for this flow
91    */
92   void SetIndex (uint32_t index);
93   /**
94    * \brief Get the index of this flow
95    * \return the index of this flow
96    */
97   uint32_t GetIndex (void) const;
98 
99 private:
100   int32_t m_deficit;    //!< the deficit for this flow
101   FlowStatus m_status;  //!< the status of this flow
102   uint32_t m_index;     //!< the index for this flow
103 };
104 
105 
106 /**
107  * \ingroup traffic-control
108  *
109  * \brief A FqCoDel packet queue disc
110  */
111 
112 class FqCoDelQueueDisc : public QueueDisc {
113 public:
114   /**
115    * \brief Get the type ID.
116    * \return the object TypeId
117    */
118   static TypeId GetTypeId (void);
119   /**
120    * \brief FqCoDelQueueDisc constructor
121    */
122   FqCoDelQueueDisc ();
123 
124   virtual ~FqCoDelQueueDisc ();
125 
126    /**
127     * \brief Set the quantum value.
128     *
129     * \param quantum The number of bytes each queue gets to dequeue on each round of the scheduling algorithm
130     */
131    void SetQuantum (uint32_t quantum);
132 
133    /**
134     * \brief Get the quantum value.
135     *
136     * \returns The number of bytes each queue gets to dequeue on each round of the scheduling algorithm
137     */
138    uint32_t GetQuantum (void) const;
139 
140   // Reasons for dropping packets
141   static constexpr const char* UNCLASSIFIED_DROP = "Unclassified drop";  //!< No packet filter able to classify packet
142   static constexpr const char* OVERLIMIT_DROP = "Overlimit drop";        //!< Overlimit dropped packets
143 
144 private:
145   virtual bool DoEnqueue (Ptr<QueueDiscItem> item);
146   virtual Ptr<QueueDiscItem> DoDequeue (void);
147   virtual bool CheckConfig (void);
148   virtual void InitializeParams (void);
149 
150   /**
151    * \brief Drop a packet from the head of the queue with the largest current byte count
152    * \return the index of the queue with the largest current byte count
153    */
154   uint32_t FqCoDelDrop (void);
155 
156   bool m_useEcn;             //!< True if ECN is used (packets are marked instead of being dropped)
157   /**
158    * Compute the index of the queue for the flow having the given flowHash,
159    * according to the set associative hash approach.
160    *
161    * \param flowHash the hash of the flow 5-tuple
162    * \return the index of the queue for the given flow
163    */
164   uint32_t SetAssociativeHash (uint32_t flowHash);
165 
166   std::string m_interval;    //!< CoDel interval attribute
167   std::string m_target;      //!< CoDel target attribute
168   uint32_t m_quantum;        //!< Deficit assigned to flows at each round
169   uint32_t m_flows;          //!< Number of flow queues
170   uint32_t m_setWays;        //!< size of a set of queues (used by set associative hash)
171   uint32_t m_dropBatchSize;  //!< Max number of packets dropped from the fat flow
172   uint32_t m_perturbation;   //!< hash perturbation value
173   Time m_ceThreshold;        //!< Threshold above which to CE mark
174   bool m_enableSetAssociativeHash; //!< whether to enable set associative hash
175   bool m_useL4s;             //!< True if L4S is used (ECT1 packets are marked at CE threshold)
176 
177   std::list<Ptr<FqCoDelFlow> > m_newFlows;    //!< The list of new flows
178   std::list<Ptr<FqCoDelFlow> > m_oldFlows;    //!< The list of old flows
179 
180   std::map<uint32_t, uint32_t> m_flowsIndices;    //!< Map with the index of class for each flow
181   std::map<uint32_t, uint32_t> m_tags;            //!< Tags used by set associative hash
182 
183   ObjectFactory m_flowFactory;         //!< Factory to create a new flow
184   ObjectFactory m_queueDiscFactory;    //!< Factory to create a new queue
185 };
186 
187 } // namespace ns3
188 
189 #endif /* FQ_CODEL_QUEUE_DISC */
190 
191