24 #include "ns3/packet.h"
25 #include "ns3/simulator.h"
27 #include "ns3/boolean.h"
28 #include "ns3/double.h"
29 #include "ns3/uinteger.h"
44 .AddConstructor<QKDBuffer> ()
45 .AddAttribute (
"Minimal",
46 "The minimal amount of key material in QKD storage (bits)",
49 MakeUintegerChecker<uint32_t> ())
50 .AddAttribute (
"Maximal",
51 "The maximal amount of key material in QKD storage (bits)",
54 MakeUintegerChecker<uint32_t> ())
55 .AddAttribute (
"Threshold",
56 "The threshold amount of key material in QKD (bits)",
59 MakeUintegerChecker<uint32_t> ())
60 .AddAttribute (
"Current",
61 "The current amount of key material in QKD storage (bits)",
64 MakeUintegerChecker<uint32_t> ())
66 .AddAttribute (
"CalculationTimePeriod",
67 "The period of time (in seconds) to calculate average amount of the key in the buffer",
70 MakeUintegerChecker<uint32_t> ())
71 .AddAttribute (
"MaxNumberOfRecordedKeyCharingTimePeriods",
72 "The maximal number of values which are stored for calculation of average key charging rate",
75 MakeUintegerChecker<uint32_t> ())
76 .AddAttribute (
"MaxNumberOfRecordedKeyConsumptionTimePeriods",
79 "The maximal number of values which are stored for calculation of average key consumption rate.",
82 MakeUintegerChecker<uint32_t> ())
83 .AddAttribute (
"DefaultKeySize",
84 "The default key size",
87 MakeUintegerChecker<uint32_t> ())
88 .AddAttribute (
"MinimalKeyCount",
89 "The minimal number of keys to be stored in the buffer (count of keys)",
92 MakeUintegerChecker<uint32_t> ())
93 .AddAttribute (
"MaximalKeyCount",
94 "The maximal number of keys to be stored in the buffer (count of keys)",
97 MakeUintegerChecker<uint32_t> ())
99 .AddTraceSource (
"ThresholdChange",
100 "The change trace for threshold amount of key material in QKD storage",
102 "ns3::QKDBuffer::ThresholdChange")
104 .AddTraceSource (
"ThresholdIncrease",
105 "The increase trace for threshold amount of key material in QKD storage",
107 "ns3::QKDBuffer::ThresholdIncrease")
109 .AddTraceSource (
"ThresholdDecrease",
110 "The decrease trace for threshold amount of key material in QKD storage",
112 "ns3::QKDBuffer::ThresholdDecrease")
114 .AddTraceSource (
"CurrentChange",
115 "The change trace for current amount of key material in QKD storage",
117 "ns3::QKDBuffer::CurrentChange")
119 .AddTraceSource (
"CurrentIncrease",
120 "The increase trace for current amount of key material in QKD storage",
122 "ns3::QKDBuffer::CurrentIncrease")
124 .AddTraceSource (
"CurrentDecrease",
125 "The decrease trace for current amount of key material in QKD storage",
127 "ns3::QKDBuffer::CurrentDecrease")
129 .AddTraceSource (
"StatusChange",
130 "The change trace for current status of QKD storage",
132 "ns3::QKDBuffer::StatusChange")
133 .AddTraceSource (
"CMetricChange",
134 "The change trace for current status of QKD storage",
136 "ns3::QKDBuffer::CMetricChange")
137 .AddTraceSource (
"AverageKeyGenerationRate",
138 "The average key rate of the QKD storage",
140 "ns3::QKDBuffer::AverageKeyGenerationRate")
141 .AddTraceSource (
"AverageKeyConsumptionRate",
142 "The average key rate of the QKD storage",
144 "ns3::QKDBuffer::AverageKeyConsumptionRate")
146 .AddTraceSource (
"NewKeyAdded",
147 "The trace to monitor adding new key material to the buffer",
149 "ns3::QKDBuffer::NewKeyAdded")
150 .AddTraceSource (
"TransformedKeyAdded",
151 "The trace to monitor adding transformed key material to the buffer",
153 "ns3::QKDBuffer::TransformedKeyAdded")
154 .AddTraceSource (
"KeyServed",
155 "The trace to monitor key usage",
157 "ns3::QKDBuffer::KeyServed")
158 .AddTraceSource (
"KeyReserved",
159 "The trace to monitor key reservation",
161 "ns3::QKDBuffer::KeyReserved")
320 NS_LOG_FUNCTION (
this << key->GetId() << key->GetSizeInBits() << keyTransformed );
327 << key->GetSizeInBits() <<
"bits, since the current is "
335 m_keys.insert( std::make_pair( key->GetId() , key) );
356 if(keyTransformed == 0)
379 double m_averageKeyConsumptionTimePeriodPeek = 0;
380 double m_averageKeyConsumptionRatePeek = 0;
395 m_consumptionTimePeriodsPeek.insert( m_consumptionTimePeriodsPeek.begin(), currentTime );
401 m_averageKeyConsumptionTimePeriodPeek = sum / m_consumptionTimePeriodsPeek.size();
404 m_averageKeyConsumptionTimePeriodPeek = 0;
406 NS_LOG_DEBUG(
this <<
" m_averageKeyConsumptionTimePeriodPeek (micro seconds): " << m_averageKeyConsumptionTimePeriodPeek );
407 NS_LOG_DEBUG(
this <<
" m_consumptionTimePeriodsPeek.size(): " << m_consumptionTimePeriodsPeek.size() );
420 NS_LOG_DEBUG (
this <<
" m_averageKeyConsumptionTimePeriodPeek (seconds): " << m_averageKeyConsumptionTimePeriodPeek );
423 NS_LOG_DEBUG (
this <<
" m_averageKeyConsumptionRatePeek (bps): " << m_averageKeyConsumptionRatePeek);
427 return round(m_averageKeyConsumptionRatePeek);
445 uint32_t keyCount {0};
447 if(it->second->GetSizeInBits() == keySize && it->second->GetState() ==
QKDKey::READY)
457 std::map<std::string, Ptr<QKDKey> >::iterator it =
m_keys.find (keyId);
459 if(it->second->GetState() == keyState)
481 if(it->second->GetState() ==
QKDKey::READY && it->second->GetSizeInBits() == keySize){
492 NS_FATAL_ERROR(
this <<
"Key of desired length is not available in the QKD buffer!");
512 std::map<std::string, Ptr<QKDKey> >::iterator a =
m_keys.find (keyID);
515 NS_LOG_FUNCTION(
this <<
"Key is found " << key->GetId() << key->GetSizeInBits());
517 NS_LOG_FUNCTION(
this <<
"Key has been erased from the QKDBuffer " << key->GetId());
519 NS_LOG_FUNCTION(
this <<
"Returning key " << key->GetId() <<
" as the funtion output ...");
525 NS_LOG_FUNCTION(
this <<
"Key is not found ... Returning NULL value ...");
668 std::map<std::string, Ptr<QKDKey> >::iterator it =
m_keys.find(keyId);
672 NS_LOG_FUNCTION(
this <<
"Reserving key " << keyId <<
" of size " << it->second->GetSizeInBits());
675 it->second->MarkReserved();
688 NS_FATAL_ERROR(
this <<
"Key reservation failed ... Error message: Key "
689 << keyId <<
" has been already reserved or served for another purpose.");
691 NS_FATAL_ERROR(
this <<
"Key reservation failed ... Error message: Key "
692 << keyId <<
"cannot be found in the QKDBuffer.");
699 NS_LOG_FUNCTION(
this <<
"Releasing reservation of the key" << keyId <<
" ...");
700 std::map<std::string, Ptr<QKDKey> >::iterator it =
m_keys.find(keyId);
703 it->second->MarkReady();
707 NS_FATAL_ERROR(
this <<
"Release reservation failed ... Error message: Key "
708 << keyId <<
"has not been in the RESRVED state.");
710 NS_FATAL_ERROR(
this <<
"Release reservation failed ... Error message: Key "
711 << keyId <<
"cannot be found in the QKDBuffer.");
722 std::map<std::string, Ptr<QKDKey> >::iterator it =
m_keys.begin();
723 while(it !=
m_keys.end()){
725 NS_LOG_FUNCTION (
this << it->second->GetId() << it->second->GetSize() << it->second->GetSizeInBits() << it->second->GetState());
728 it->second->GetSizeInBits() >= targetSize &&
733 optimalKey = it->second;
734 NS_LOG_FUNCTION(
this <<
"Starting optimal key " << optimalKey->GetId());
737 it->second->GetSizeInBits() >= targetSize &&
740 it->second->GetSizeInBits() < optimalKey->GetSizeInBits()
742 optimalKey = it->second;
744 optimalKeySecond == NULL &&
748 optimalKeySecond = it->second;
750 optimalKeySecond != NULL &&
751 optimalKeySecond->GetSizeInBits() < it->second->GetSizeInBits() &&
755 optimalKeySecond = it->second;
762 return optimalKeySecond;
786 }
else if(m_currentKeyBit < m_thresholdKeyBit && m_currentKeyBit >
m_minKeyBit &&
792 }
else if(m_currentKeyBit < m_thresholdKeyBit && m_currentKeyBit >
m_minKeyBit &&
A base class which provides memory management and object aggregation.
QKD buffer is a secure storage for QKD keys.
uint32_t m_srcNodeBufferListIndex
The index in the source node buffer list.
double m_averageKeyChargingTimePeriod
The average duration of the key charging time period.
double m_averageKeyConsumptionSize
The average key consumption size.
int64_t m_lastKeyChargingTimeStamp
The timestamp of a last key charging (when the new key material was added).
uint32_t m_noEntry
Help value used for graph ploting.
double m_averageKeyConsumptionRate
The average key consumption rate.
double m_averageKeyGenerationRate
The average key generation rate.
TracedCallback< Ptr< QKDKey > > m_keyReservedTrace
A trace for reserved keys.
static const uint32_t QKDSTATUS_WARNING
QKDStatus WARNING.
std::vector< int64_t > m_lastChargedKeySizes
The size of the several last generated keys.
void UpdateKeyConsumptionStatistics(Ptr< QKDKey > key)
Update key consumption statistics.
uint32_t m_bufferID
The unique buffer identifier.
bool operator==(QKDBuffer const &o) const
Define equal operator on the QKD buffer object.
void UpdateKeyGenerationStatistics(Ptr< QKDKey > key)
Update key generation statistics.
uint64_t GetMinKeySizeBit()
Get the minimum key size that can be served in bits.
TracedCallback< double > m_CMetricChangeTrace
A trace of c metric changes.
TracedCallback< uint32_t > m_currentKeyBitDecreaseTrace
A trace of decrease in amount of key bits.
std::map< std::string, Ptr< QKDKey > > m_keys
The list of available keys with their identifiers.
uint32_t GetMaxKeyCount()
Get the maximum number of stored keys.
uint32_t GetKeyCount()
Get the number of stored keys.
uint32_t m_thresholdKeyBit
The threshold amount of key material in the QKD key storage.
Ptr< QKDKey > SearchOptimalKeyToTransform(uint32_t targetSize)
Serach for the optimal key to transform.
uint32_t m_maxKeyBit
The maximal amount of key material in the QKD key storage.
double FetchAverageKeyChargingTimePeriod()
Get the average duration of key charging process in the long run.
uint32_t m_maxNumberOfRecordedKeyChargingTimePeriods
The maximal number of values which are used for calculation of the average key charging time period.
uint32_t m_period
Help value used for graph ploting.
uint32_t m_minimalKeyCount
The minimal number of stored keys.
uint32_t m_status
The state of the Net Device transmit state machine.
Ptr< QKDKey > FetchKeyBySize(const uint32_t &keySize)
Get the key of a given size.
uint32_t m_currentKeyBitReally
The current amount of key material used for real tracking of storage (transform!).
int64_t m_lastKeyChargingTimeDuration
The timestamp of a last key usage.
TracedCallback< uint32_t > m_thresholdKeyBitChangeTrace
A traceback for available key bits.
static const uint32_t QKDSTATUS_CHARGING
QKDStatus CHARGING.
std::vector< int64_t > m_lastConsumedKeySizes
The size of the several last consumed keys.
uint64_t GetMaxKeySizeBit()
Get the maximal key size that can be served in bits.
virtual ~QKDBuffer()
Destructor.
uint32_t GetIndex()
Get the index of the buffer per local node.
Ptr< Node > GetSrcNode()
Get the QKD source node.
double m_averageKeyConsumptionTimePeriod
The average duration of the key consumption time period.
Ptr< Node > GetDstNode()
Get the QKD destination node.
TracedCallback< uint32_t > m_thresholdKeyBitDecreaseTrace
A traceback for consumed key bits.
double m_averageKeyChargingSize
The average key charging size.
uint32_t m_bitsUsedInTimePeriod
Help value used for graph ploting.
TracedCallback< uint32_t > m_currentKeyBitChangeTrace
A trace for current bit change.
int64_t m_lastKeyConsumptionTimeStamp
The timestamp of a last key consumption (when the last key material was fetched).
void Dispose(void)
Destroy a QKD buffer.
bool AddNewKey(Ptr< QKDKey > key, uint32_t keyTransformed)
Add new key to the storage.
uint32_t m_maximalKeyCount
The maximal number of stored keys.
std::vector< struct QKDBuffer::data > m_previousValues
Help vector used for graph ploting.
uint32_t m_maxNumberOfRecordedKeyConsumptionTimePeriods
The maximal number of values which are used for calculation of the average key consumption time perio...
uint32_t FetchPreviousState(void)
Get the previous state of the QKD buffer.
bool ProbeKeyStatus(std::string keyId, QKDKey::QKDKeyState_e keyState)
Check if the state of the key equals to a given state.
std::vector< int64_t > m_chargingTimePeriods
The durations of the serveral last charging time periods.
uint32_t m_defaultKeySize
The default key size as required by the ETSI QKD 014 interface details.
TracedCallback< Ptr< QKDKey > > m_newKeyAddedTrace
A trace of newly added keys.
void SetIndex(uint32_t)
Set the index of the buffer per local node.
uint64_t GetTargetKeyCountBit()
Get tge amount of key material in bits that is ready to be served and belongs to a set of targeted ke...
void KeyCalculation()
Help function used for ploting graphs.
uint32_t m_currentKeyBit
The current amount of key material in the QKD key storage.
std::map< uint32_t, QKDBuffer::KeyStorage > m_destinations
uint32_t GetId(void) const
Get the QKD buffer identifier.
void ReserveKey(std::string keyId)
Reserve the QKD key.
TracedCallback< double > m_averageKeyConsumptionRateTrace
A trace of the average key consumption rate.
TracedCallback< uint32_t > m_currentKeyBitIncreaseTrace
A trace of increase in amount of key bits.
static const uint32_t QKDSTATUS_READY
QKDStatus READY.
void SetSrcNode(Ptr< Node >)
Set the QKD source node.
TracedCallback< double > m_averageKeyGenerationRateTrace
A trace of the average key generation rate.
void SetMthr(uint32_t thr)
Set the threshold value of the QKD storage.
uint32_t GetMinKeyCount()
Get the minimum number of stored keys.
double GetAverageKeyGenerationRate()
Get average key generation rate.
uint32_t FetchMaxNumberOfRecordedKeyChargingTimePeriods()
Get the maximal number of values which are used for calculation of average key charging time period.
TracedCallback< Ptr< QKDKey > > m_transformedKeyAddedTrace
A trace for tranformed keys.
static uint32_t nBuffers
The number of the created buffers - a static value.
Ptr< QKDKey > FetchKeyByID(std::string keyID)
Get the key of a given identifier.
std::vector< int64_t > m_consumptionTimePeriods
The durations of the serveral last consumption time periods.
uint32_t GetKeySize()
Get default size of the key (ETSI QKD 014).
int64_t FetchLastKeyChargingTimeDuration()
Get the time value about the time duration of last key charging process.
std::vector< uint32_t > m_targetSizeSet
The list of requested key sizes.
static const uint32_t QKDSTATUS_EMPTY
QKDStatus EMPTY.
void ReleaseReservation(std::string keyId)
Release the key reservation.
int64_t m_lastKeyConsumptionTimeDuration
The timestamp of a last key fetch operation.
Ptr< Node > m_srcNode
The source node.
uint32_t m_previousStatus
The previous status; important for deciding about further status that can be selected.
uint32_t GetMthr(void) const
Get the threshold value of QKD storage.
uint32_t m_recalculateTimePeriod
The period of time (in seconds) to calculate average amount of the key in the buffer.
TracedCallback< uint32_t > m_StatusChangeTrace
A trace of status changes.
bool m_isRisingCurve
Whether curve on graph is rising or not.
uint64_t m_currentReadyKeyBit
The current amount of key material in the QKD buffer that are ready to be served.
Ptr< Node > m_dstNode
The destination node.
void SetDstNode(Ptr< Node >)
Set the QKD destination node.
TracedCallback< Ptr< QKDKey > > m_keyServedTrace
A trace for served keys.
double GetAverageKeyConsumptionRate()
Get average key consumption rate.
uint32_t GetMCurrentPrevious(void) const
Get previous - before latest change.
static TypeId GetTypeId(void)
Get the TypeId.
TracedCallback< uint32_t > m_thresholdKeyBitIncreaseTrace
A traceback for generated key bits.
EventId m_calculateRoutingMetric
The event to calculate routing metric.
void InitTotalGraph() const
Initialize total graph.
uint32_t m_minKeyBit
The minimal amount of key material in the QKD key storage.
void CheckState(void)
Update the state of the buffer.
uint32_t m_nextKeyID
The identifie of the next key to be generated.
uint32_t m_noAddNewValue
Help value used for graph ploting.
uint32_t FetchState(void)
Get the current state of the QKD buffer.
void RecordTargetSize(uint32_t size)
Record the key target size.
uint64_t m_currentTargetKeyBit
The current amount of key material in the QKD buffer that is already targeted.
void Init(Ptr< Node > srcNode, Ptr< Node > dstNode, uint32_t Mmin, uint32_t Mthr, uint32_t Mmax, uint32_t Mcurrent, bool useRealStorages)
Initialize a QKD buffer.
uint32_t m_currentKeyBitPrevious
The previous value of current amount of key material in the QKD key storage.
uint64_t GetReadyKeyCountBit()
Get the amount of the key material in bits that is ready to be served.
uint64_t GetKeyCountBit()
Get the amount of stored keys in bits.
QKDKeyState_e
The QKD key states.
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static Time Now(void)
Return the current simulation virtual time.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool compareByData(const QKDBuffer::data &a, const QKDBuffer::data &b)