blob: 16a31fd9c6cbd84fa1f57b412eb4772df60e0979 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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 Afanasyev56f79ea2011-08-17 23:54:27 -070018 * Authors: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070020
Alexander Afanasyev98256102011-08-14 01:00:02 -070021#ifndef CCNX_FACE_H
22#define CCNX_FACE_H
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev98256102011-08-14 01:00:02 -070024#include <ostream>
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080025#include <algorithm>
Alexander Afanasyev98256102011-08-14 01:00:02 -070026
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070027#include "ns3/ptr.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080028#include "ns3/ccnx.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080029#include "ns3/nstime.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070030#include "ns3/type-id.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
32namespace ns3 {
33
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034class Packet;
35class Node;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070036
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070037/**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070038 * \ingroup ccnx
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070039 * \defgroup ccnx-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070040 */
41/**
42 * \ingroup ccnx-face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070043 * \brief Virtual class defining CCNx face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070044 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070045 * This class defines basic functionality of CCNx face. Face is core
46 * component responsible for actual delivery of data packet to and
47 * from CCNx stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070049 * \see CcnxLocalFace, CcnxNetDeviceFace, CcnxIpv4Face, CcnxUdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050 */
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080051class CcnxFace : public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052{
53public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080054 static TypeId
55 GetTypeId ();
56
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070057 /**
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080058 * \brief Ccnx protocol handler
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070059 *
60 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070062 */
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070063 typedef Callback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070065 /**
66 * \brief Default constructor
67 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068 CcnxFace (Ptr<Node> node);
Alexander Afanasyev98256102011-08-14 01:00:02 -070069 virtual ~CcnxFace();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070070
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080071 Ptr<Node>
72 GetNode () const;
73
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070074 ////////////////////////////////////////////////////////////////////
75
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070076 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070077 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070078 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070079 * This method should call protocol-dependent registration function
80 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080081 virtual void
82 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070083
84 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080085 * @brief Check if Interest limit is reached
86 *
87 * Side effect: if limit is not yet reached, the number of outstanding packets will be increased
88 *
89 * @returns true if Interest limit is not yet reached
90 */
91 bool
92 IsBelowLimit ();
93
94 /**
95 * \brief Send packet on a face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070096 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 * This method will be called by lower layers to send data to device or application
98 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 * \param p smart pointer to a packet to send
100 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800101 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102 */
103 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105
106 /**
107 * \brief Receive packet from application or another node and forward it to the CCNx stack
108 *
109 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700110 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111 bool
112 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700114
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800115 /**
116 * \Brief Assign routing/forwarding metric with face
117 *
118 * \param metric configured routing metric (cost) of this face
119 */
120 virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800122 /**
123 * \brief Get routing/forwarding metric assigned to the face
124 *
125 * \returns configured routing/forwarding metric (cost) of this face
126 */
127 virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128
129 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700130 * These are face states and may be distinct from actual lower-layer
131 * device states, such as found in real implementations (where the
132 * device may be down but ccnx face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700134
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800138 virtual void
139 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140
141 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700142 * \brief Returns true if this face is enabled, false otherwise.
143 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144 virtual bool
145 IsUp () const;
Ilya Moiseenkoacac1ea2011-10-28 13:16:53 -0700146
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700147 virtual std::ostream&
148 Print (std::ostream &os) const;
149
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700150 /**
151 * \brief Set node Id
152 *
153 * Id is purely informative and should not be used for any other purpose
154 *
155 * \param id id to set
156 */
157 inline void
158 SetId (uint32_t id);
159
160 /**
161 * \brief Get node Id
162 *
163 * Id is purely informative and should not be used for any other purpose
164 *
165 * \returns id id to set
166 */
167 inline uint32_t
168 GetId () const;
169
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700170 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800171 * @brief Set maximum value for Interest allowance
172 *
173 * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled
174 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800175 void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800176 SetBucketMax (double bucket);
177
178 /**
Alexander Afanasyev11453142011-11-25 16:13:33 -0800179 * @brief Set a normalized value (one second) for Interest allowance bucket leak
180 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800181 void
Alexander Afanasyev11453142011-11-25 16:13:33 -0800182 SetBucketLeak (double leak);
183
184 /**
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800185 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount,
186 * where interval is time between two consecutive calls of LeakBucket
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800187 */
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800188 void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800189 LeakBucket ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800190
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800191 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700192 * \brief Compare two faces. Only two faces on the same node could be compared.
193 *
194 * Internal index is used for comparison.
195 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700196 bool
197 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700198
199 /**
200 * \brief Compare two faces. Only two faces on the same node could be compared.
201 *
202 * Internal index is used for comparison.
203 */
204 bool
205 operator< (const CcnxFace &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800207protected:
208 /**
209 * \brief Send packet on a face (actual implementation)
210 *
211 * \param p smart pointer to a packet to send
212 */
213 virtual void
214 SendImpl (Ptr<Packet> p) = 0;
215
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700217 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
218 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700219
220protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700221 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700222 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800223
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800224 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
225 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
226 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
227
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700228private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800229 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700230 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800231 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800232 Time m_lastLeakTime;
233 uint32_t m_metric; ///< \brief metric of the face
234
235 bool m_enableMetricTagging;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700236};
237
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700238std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
239
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700240inline bool
241operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
242{
243 return *lhs < *rhs;
244}
245
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700246void
247CcnxFace::SetId (uint32_t id)
248{
249 m_id = id;
250}
251
252uint32_t
253CcnxFace::GetId () const
254{
255 return m_id;
256}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700257
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258} // namespace ns3
259
Alexander Afanasyev98256102011-08-14 01:00:02 -0700260#endif //CCNX_FACE_H