Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2005,2006,2007 INRIA |
| 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 | * |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 18 | * Authors: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 19 | */ |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 21 | #ifndef CCNX_FACE_H |
| 22 | #define CCNX_FACE_H |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 24 | #include <ostream> |
| 25 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 26 | #include "ns3/ptr.h" |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 27 | #include "ns3/ccnx.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ns3 { |
| 30 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 31 | class Packet; |
| 32 | class Node; |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 34 | /** |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 35 | * \ingroup ccnx |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 36 | * \defgroup ccnx-face Faces |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 37 | */ |
| 38 | /** |
| 39 | * \ingroup ccnx-face |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 40 | * \brief Virtual class defining CCNx face |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 41 | * |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 42 | * This class defines basic functionality of CCNx face. Face is core |
| 43 | * component responsible for actual delivery of data packet to and |
| 44 | * from CCNx stack |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 45 | * |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 46 | * \see CcnxLocalFace, CcnxNetDeviceFace, CcnxIpv4Face, CcnxUdpFace |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 47 | */ |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 48 | class CcnxFace : public SimpleRefCount<CcnxFace> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 51 | /** |
| 52 | * \brief Ccnx protocol hanler |
| 53 | * |
| 54 | * \param face Face from which packet has been received |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 55 | * \param packet Original packet |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 56 | */ |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 57 | typedef Callback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>& > ProtocolHandler; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 59 | /** |
| 60 | * \brief Default constructor |
| 61 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 62 | CcnxFace (Ptr<Node> node); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 63 | virtual ~CcnxFace(); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 65 | //////////////////////////////////////////////////////////////////// |
| 66 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 67 | /** |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 68 | * \brief Register callback to call when new packet arrives on the face |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 69 | * |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 70 | * This method should call protocol-dependent registration function |
| 71 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 72 | virtual void |
| 73 | RegisterProtocolHandler (ProtocolHandler handler); |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 76 | * \brief Send packet on a face with regard Interest limits |
| 77 | * |
| 78 | * This method will be called by lower layers to send data to device or application |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 79 | * |
| 80 | * \param p smart pointer to a packet to send |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 81 | * |
| 82 | * @return false if either limit is reached or face is down |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 83 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 84 | bool |
| 85 | SendWithLimit (Ptr<Packet> p); |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 88 | * \brief Send content packet on a face without regard to limits |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 89 | * |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 90 | * This method will be called by lower layers to send data to device or application |
| 91 | * |
| 92 | * !!! The only difference between this call and SendInterest is that the former check Interest limit !!! |
| 93 | * |
| 94 | * \param p smart pointer to a packet to send |
| 95 | * |
| 96 | * @return false if face is down |
| 97 | */ |
| 98 | bool |
| 99 | SendWithoutLimits (Ptr<Packet> p); |
| 100 | |
| 101 | /** |
| 102 | * \brief Receive packet from application or another node and forward it to the CCNx stack |
| 103 | * |
| 104 | * \todo The only reason for this call is to handle tracing, if requested |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 105 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 106 | void |
| 107 | Receive (Ptr<const Packet> p); |
| 108 | //////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 110 | // /** |
| 111 | // * \Brief Assign routing/forwarding metric with face |
| 112 | // * |
| 113 | // * \param metric configured routing metric (cost) of this face |
| 114 | // */ |
| 115 | // virtual void SetMetric (uint16_t metric); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 117 | // /** |
| 118 | // * \brief Get routing/forwarding metric assigned to the face |
| 119 | // * |
| 120 | // * \returns configured routing/forwarding metric (cost) of this face |
| 121 | // */ |
| 122 | // virtual uint16_t GetMetric (void) const; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 125 | * These are face states and may be distinct from actual lower-layer |
| 126 | * device states, such as found in real implementations (where the |
| 127 | * device may be down but ccnx face state is still up). |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 128 | */ |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 130 | /** |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 131 | * \brief Enable or disable this face |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 132 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 133 | virtual void |
| 134 | SetUp (bool up = true); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 137 | * \brief Returns true if this face is enabled, false otherwise. |
| 138 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 139 | virtual bool |
| 140 | IsUp () const; |
Ilya Moiseenko | acac1ea | 2011-10-28 13:16:53 -0700 | [diff] [blame] | 141 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 142 | virtual std::ostream& |
| 143 | Print (std::ostream &os) const; |
| 144 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 145 | /** |
| 146 | * \brief Set node Id |
| 147 | * |
| 148 | * Id is purely informative and should not be used for any other purpose |
| 149 | * |
| 150 | * \param id id to set |
| 151 | */ |
| 152 | inline void |
| 153 | SetId (uint32_t id); |
| 154 | |
| 155 | /** |
| 156 | * \brief Get node Id |
| 157 | * |
| 158 | * Id is purely informative and should not be used for any other purpose |
| 159 | * |
| 160 | * \returns id id to set |
| 161 | */ |
| 162 | inline uint32_t |
| 163 | GetId () const; |
| 164 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 165 | /** |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 166 | * @brief Set maximum value for Interest allowance |
| 167 | * |
| 168 | * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled |
| 169 | */ |
| 170 | inline void |
| 171 | SetBucketMax (double bucket); |
| 172 | |
| 173 | /** |
| 174 | * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount |
| 175 | * |
| 176 | * @param interval Time interval with which the bucket is leaked |
| 177 | */ |
| 178 | inline void |
| 179 | LeakBucket (const Time &interval); |
| 180 | |
| 181 | /** |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 182 | * \brief Compare two faces. Only two faces on the same node could be compared. |
| 183 | * |
| 184 | * Internal index is used for comparison. |
| 185 | */ |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 186 | bool |
| 187 | operator== (const CcnxFace &face) const; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * \brief Compare two faces. Only two faces on the same node could be compared. |
| 191 | * |
| 192 | * Internal index is used for comparison. |
| 193 | */ |
| 194 | bool |
| 195 | operator< (const CcnxFace &face) const; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 196 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 197 | protected: |
| 198 | /** |
| 199 | * \brief Send packet on a face (actual implementation) |
| 200 | * |
| 201 | * \param p smart pointer to a packet to send |
| 202 | */ |
| 203 | virtual void |
| 204 | SendImpl (Ptr<Packet> p) = 0; |
| 205 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 206 | private: |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 207 | CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor |
| 208 | CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator |
Alexander Afanasyev | ab1d560 | 2011-08-17 19:17:18 -0700 | [diff] [blame] | 209 | |
| 210 | protected: |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 211 | // uint16_t m_metric; ///< \brief Routing/forwarding metric |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 212 | Ptr<Node> m_node; ///< \brief Smart pointer to Node |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 214 | double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face |
| 215 | double m_bucketMax; ///< \brief Maximum Interest allowance for this face |
| 216 | double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second |
| 217 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 218 | private: |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 219 | ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 220 | bool m_ifup; ///< \brief flag indicating that the interface is UP |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 221 | uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 224 | std::ostream& operator<< (std::ostream& os, const CcnxFace &face); |
| 225 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 226 | inline bool |
| 227 | operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs) |
| 228 | { |
| 229 | return *lhs < *rhs; |
| 230 | } |
| 231 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 232 | void |
| 233 | CcnxFace::SetId (uint32_t id) |
| 234 | { |
| 235 | m_id = id; |
| 236 | } |
| 237 | |
| 238 | uint32_t |
| 239 | CcnxFace::GetId () const |
| 240 | { |
| 241 | return m_id; |
| 242 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 243 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame^] | 244 | void |
| 245 | CcnxFace::SetBucketMax (double bucket) |
| 246 | { |
| 247 | m_bucketMax = bucket; |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | CcnxFace::LeakBucket (const Time &interval) |
| 252 | { |
| 253 | const double leak = m_bucketLeak * 1.0 / interval.ToDouble (Time::S); |
| 254 | m_bucket -= std::max (0, m_bucket-leak); |
| 255 | } |
| 256 | |
| 257 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 258 | } // namespace ns3 |
| 259 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 260 | #endif //CCNX_FACE_H |