A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
q-buffer.cc
Go to the documentation of this file.
1/*
2 * Copyright(c) 2022 DOTFEESA www.tk.etf.unsa.ba
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 *
7 *
8 * Authors: Miralem Mehic <miralem.mehic@ieee.org>
9 * Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
10 */
11
12
13#include <algorithm>
14#include <numeric>
15#include <random>
16#include "ns3/packet.h"
17#include "ns3/simulator.h"
18#include "ns3/log.h"
19#include "ns3/boolean.h"
20#include "ns3/double.h"
21#include "ns3/uinteger.h"
22
23#include "q-buffer.h"
24
25namespace ns3 {
26
27 NS_LOG_COMPONENT_DEFINE("QBuffer");
28
30
31 TypeId
33 {
34 static TypeId tid = TypeId("ns3::QBuffer")
37 .AddAttribute("Minimal",
38 "The minimal amount of key material in QKD storage(bits)",
39 UintegerValue(1000000), //1Mb
42 .AddAttribute("Maximal",
43 "The maximal amount of key material in QKD storage(bits)",
44 UintegerValue(1000000000), //1Gb
47 .AddAttribute("Threshold",
48 "The threshold amount of key material in QKD(bits)",
49 UintegerValue(2000000), //2Mb
52
53 .AddAttribute("CalculationTimePeriod",
54 "The period of time(in seconds) to calculate average amount of the key in the buffer",
55 UintegerValue(5), // in seconds
58 .AddAttribute("MaxNumberOfRecordedKeyCharingTimePeriods",
59 "The maximal number of values which are stored for calculation of average key charging time period",
63 .AddAttribute("DefaultKeySize",
64 "The default key size",
65 UintegerValue(512),
68
69 .AddTraceSource("ThresholdChange",
70 "The change trace for threshold amount of key material in QKD storage",
72 "ns3::QBuffer::ThresholdChange")
73
74 .AddTraceSource("CurrentChange",
75 "The change trace for current amount of key material in QKD storage",
77 "ns3::QBuffer::CurrentChange")
78
79 .AddTraceSource("CurrentIncrease",
80 "The increase trace for current amount of key material in QKD storage - TOTAL GRAPH",
82 "ns3::QBuffer::CurrentIncrease")
83 .AddTraceSource("CurrentDecrease",
84 "The decrease trace for current amount of key material in QKD storage - TOTAL GRAPH",
86 "ns3::QBuffer::CurrentDecrease")
87
88 .AddTraceSource("StatusChange",
89 "The change trace for current status of QKD storage",
91 "ns3::QBuffer::StatusChange")
92 .AddTraceSource("CMetricChange",
93 "The change trace for current status of QKD storage",
95 "ns3::QBuffer::CMetricChange")
96 .AddTraceSource("AverageKeyChargingTimePeriod",
97 "The change trace for current status of QKD storage",
99 "ns3::QBuffer::AverageKeyChargingTimePeriod")
100
101 .AddTraceSource("NewKeyAdded",
102 "The trace to monitor adding new key material to the buffer",
104 "ns3::QBuffer::AverageKeyChargingTimePeriod")
105 .AddTraceSource("KeyServed", //toDo: this should be monitored maybe on KMS! Due to transform functions!
106 "The threce to monitor key usage",
108 "ns3::QBuffer::AverageKeyChargingTimePeriod")
109 ;
110 return tid;
111 }
112
114 {
115 NS_LOG_FUNCTION(this);
116 }
117
118 void
134
135 void
143 )
144 {
145 NS_LOG_FUNCTION(this
146 << "\nDestination KM Node Id:" << dstKmNodeId
147 << "\nMmin:" << Mmin << "\nMthr:" << Mthr
148 << "\nMmax:" << Mmax << "\nMcurr:" << Mcurrent
149 << "\nDefault key size:" << defaultKeySize
150 );
157
160 m_noEntry = 0;
161 m_period = 5;
162 m_noAddNewValue = 0;
164
167 m_c = 0;
169 m_previousStatus = 0;
170 CheckState();
171 }
172
174
175
177 {
178 NS_LOG_FUNCTION(this);
179 m_keys.clear();
180 }
181
182 void
188
191 {
192 return m_defaultKeySize;
193 }
194
196 {
197 return a.value > b.value;
198 }
199
200 void
202 {
203 NS_LOG_FUNCTION(this);
205
206 struct QBuffer::data q;
209
210 while( m_previousValues.size() > m_period )
211 m_previousValues.pop_back();
212
213 m_previousValues.insert( m_previousValues.begin() ,q);
214
215 //sort elements in descending order, first element has the maximum value
216 std::sort(m_previousValues.begin(), m_previousValues.end(), compareByData);
217
218
219 /*
220 * If maximal value is on the current location then it means that the current value is the highest in the period => function is rising
221 * Otherwise, function is going down
222 */
224 CheckState();
225
226 m_noEntry++;
227 }
228
229 void
231 {
233
234 ///////////////////////////////// TEMP TEMP TEMP /////////////////////////////////
235 // Collect all keys in READY state
236 /*
237 NS_LOG_FUNCTION(this << "m_keys.size(): " << m_keys.size());
238 uint32_t totalReadyKeyCount = 0;
239 for (auto it = m_keys.begin(); it != m_keys.end(); ++it) {
240 if (it->second->GetState() == QKDKey::READY) {
241 totalReadyKeyCount += it->second->GetSizeInBits();
242 NS_LOG_FUNCTION(this << "id:" << it->second->GetId() << "\t size:" << it->second->GetSizeInBits());
243 }
244 }
245 NS_LOG_FUNCTION(this << "m_currentKeyBit: " << m_currentKeyBit);
246 NS_LOG_FUNCTION(this << "totalReadyKeyCount: " << totalReadyKeyCount);
247 */
248 ///////////////////////////////// TEMP TEMP TEMP /////////////////////////////////
249
250
251
252 if(positive)
253 {
257 }else{
261 }
262
263 NS_LOG_FUNCTION(this << "m_currentKeyBit: " << m_currentKeyBit);
264 CheckState();
265 }
266
267
270 return m_minKeyBit;
271 }
272
275 return m_maxKeyBit;
276 }
277
280 {
281 return m_keys.size();
282 }
283
284 bool
286 {
287 NS_ASSERT(!key->GetId().empty()); //Unknown bug!
288
289 NS_LOG_FUNCTION(this
290 << "\nKey ID:\t" << key->GetId()
291 << "\nKey Size:\t" << key->GetSizeInBits()
292 << "\nKey Value:\t" << key->ToString()
293 << "\nGetBitCount():\t" << GetBitCount()
294 );
295
296 if(GetBitCount() + key->GetSizeInBits() > GetMmax()){
297 NS_LOG_FUNCTION(this << "Buffer is full! Not able to add new "
298 << key->GetSizeInBits() << "bits, since the current is "
299 << GetBitCount() << " and max is " << GetMmax()
300 );
303 return false;
304 }
305
306 m_keys.insert( std::make_pair( key->GetId() , key) );
307 NS_LOG_FUNCTION(this << "Key" << key->GetId() << "added to QBuffer");
308
309 if(fireTraces){
310 LogUpdate(key->GetSizeInBits(), true);
311 //Fire total graph traces
312 m_currentKeyBitIncreaseTrace(key->GetSizeInBits());
313
314
315 /*
316 * First CALCULATE AVERAGE TIME PERIOD OF KEY CHARGING VALUE
317 */
318 if(!m_chargingTimePeriods.empty()){
320 m_chargingTimePeriods.begin(),
321 m_chargingTimePeriods.end(), 0.0
322 ) / m_chargingTimePeriods.size();
323 }else
325
327 NS_LOG_DEBUG(this << " m_averageKeyChargingTimePeriod: " << m_averageKeyChargingTimePeriod );
328 NS_LOG_DEBUG(this << " m_chargingTimePeriods.size(): " << m_chargingTimePeriods.size() );
329
330 /**
331 * Second, add new value to vector of previous values
332 */
335 m_chargingTimePeriods.pop_back();
336 }
337 }
338
344 NS_LOG_DEBUG(this << " m_lastKeyChargingTimeStamp: " << m_lastKeyChargingTimeStamp );
345 NS_LOG_DEBUG(this << " m_lastKeyChargingTimeDuration: " << m_lastKeyChargingTimeDuration );
346
347 //////////////////////////////////////////////////////////////////////////////////
348
351 m_bitsChargedInTimePeriod += key->GetSizeInBits();
353 }
354
355 return true;
356 }
357
358 //NOTE: Function is allowed to return NULL value. Processing is left to the KM.
361 {
362 Ptr<QKDKey> key {NULL};
363 if(!keyId.empty()){ //Return requested key if found
364 NS_LOG_FUNCTION(this << "keyId:\t" << keyId);
365 auto a = m_keys.find(keyId);
366 if(a != m_keys.end()){
367 key = a->second;
369 //Fire traces
370 NS_LOG_FUNCTION(this << key->GetId() << key->GetSizeInBits());
371 if(fireTraces) {
372 LogUpdate(key->GetSizeInBits(), false);
373 //Fire total graph traces
374 m_currentKeyBitDecreaseTrace(key->GetSizeInBits());
375 }
376 }else
377 NS_LOG_DEBUG(this << "not found " << keyId);
378
379 }else{ //Return random key from QBuffer
380 NS_LOG_FUNCTION(this << "keyId:\t" << keyId << "\t *random" << GetKeyCount());
381 std::unordered_map< std::string, Ptr <QKDKey> >::iterator random_it;
383 if(keyCount >= 1)
384 { //If QBuffer is not empty select a random key
385 if(keyCount == 1)
386 {
387 random_it = m_keys.begin();
388 } else{
389 random_it = std::next(std::begin(m_keys), std::rand()%keyCount);
390 }
391 key = random_it->second;
392 DestroyKey(key->GetId());
393 //Fire traces
394 NS_LOG_FUNCTION(this << key->GetId() << key->GetSizeInBits());
395 if(fireTraces) {
396 LogUpdate(key->GetSizeInBits(), false);
397 //Fire total graph traces
398 m_currentKeyBitDecreaseTrace(key->GetSizeInBits());
399 }
400 }else
401 NS_LOG_FUNCTION(this << "QBuffer is empty");
402
403 }
404
405 return key;
406 }
407
408 bool
410 {
411 NS_LOG_FUNCTION(this << keyId);
412 auto it = m_keys.find(keyId);
413 if(it != m_keys.end()){
414 m_keys.erase(it);
415 return true;
416 }else{
417 NS_LOG_FUNCTION(this << "Key" << keyId << "does not exist");
418 return false;
419 }
420 }
421
427
428 void
430 {
432
434 NS_LOG_FUNCTION("case 1");
436
439 ){
440 NS_LOG_FUNCTION("case 2");
442
445 ){
446 NS_LOG_FUNCTION("case 3");
448
449 }else if(m_currentKeyBit <= m_minKeyBit){
450 NS_LOG_FUNCTION("case 4");
452 }else{
454 }
455
457 NS_LOG_FUNCTION(this << "STATUS IS NOT EQUAL TO PREVIOUS STATUS" << m_previousStatus << m_status);
459
463 }
464 }
465
466 bool
468 {
469 return(m_bufferID == o.m_bufferID);
470 }
471
472
476 return m_bufferID ;
477 }
478
479 void
485
486 /**
487 * Return time value about the time duration of last key charging process
488 */
489 int64_t
495
496 /*
497 * Return time difference between the current time and time at which
498 * last key charging process finished
499 */
500 int64_t
507
508 double
509 QBuffer::GetAverageKeyChargingTimePeriod() //@toDo nema smisla ovdje to pratiti!
510 {
513 }
514
517 {
518 NS_LOG_FUNCTION(this << m_status);
519 return m_status;
520 }
521
528
535
538 {
540 return m_thresholdKeyBit;
541 }
542 void
549
550 void
552 NS_LOG_FUNCTION(this << index);
554 }
555
556 void
560
566
567} // namespace ns3
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
Qbuffer is a secure storage for QKD keys.
Definition q-buffer.h:71
void Dispose()
destroy a QBuffer
Definition q-buffer.cc:183
virtual void Configure(uint32_t Mmin, uint32_t Mthr, uint32_t Mmax, uint32_t Mcurrent, uint32_t defaultKeySize)
create QKD buffer configuration
Definition q-buffer.cc:119
static const uint32_t QSTATUS_WARNING
QStatus WARNING.
Definition q-buffer.h:75
uint32_t m_thresholdKeyBit
Definition q-buffer.h:443
uint32_t GetMaxNumberOfRecordedKeyChargingTimePeriods() const
Return the maximal number of values which are used for calculation of average key charging time perio...
Definition q-buffer.cc:423
void SetIndex(uint32_t)
Set the index of the buffer per local node.
Definition q-buffer.cc:551
uint32_t m_recalculateTimePeriod
The period of time(in seconds) to calculate average amount of the key in the buffer Default value 5 -...
Definition q-buffer.h:424
TracedCallback< uint32_t > m_currentKeyBitDecreaseTrace
Definition q-buffer.h:497
bool operator==(QBuffer const &o) const
Assign operator.
Definition q-buffer.cc:467
uint32_t m_noEntry
Help value used for graph ploting.
Definition q-buffer.h:397
uint32_t m_maxKeyBit
Definition q-buffer.h:439
void SetMthr(uint32_t thr)
Set the threshold value of QKD storage.
Definition q-buffer.cc:543
uint32_t m_minKeyBit
Definition q-buffer.h:437
uint32_t m_maxNumberOfRecordedKeyChargingTimePeriods
The maximal number of values which are used for stored for calculation of average key charging time p...
Definition q-buffer.h:473
TracedCallback< uint32_t > m_currentKeyBitIncreaseTrace
Definition q-buffer.h:496
void Init(uint32_t dstKmNodeId, uint32_t Mmin, uint32_t Mthr, uint32_t Mmax, uint32_t Mcurrent, uint32_t defaultKeySize)
initialize QBuffer
Definition q-buffer.cc:136
static const uint32_t QSTATUS_CHARGING
QStatus CHARGING.
Definition q-buffer.h:76
int64_t GetLastKeyChargingTimeDuration()
Return time value about the time duration of last key charging process.
Definition q-buffer.cc:490
uint32_t m_period
Help value used for graph ploting.
Definition q-buffer.h:402
uint32_t GetMmax() const
Definition q-buffer.cc:274
QBuffer()
QBuffer constructor.
Definition q-buffer.cc:113
std::unordered_map< std::string, Ptr< QKDKey > > m_keys
key database
Definition q-buffer.h:383
static const uint32_t QSTATUS_READY
QStatus READY.
Definition q-buffer.h:74
void SetKeySize(uint32_t size)
Set default key size.
Definition q-buffer.cc:557
bool m_isRisingCurve
whether curve on graph is rising or not
Definition q-buffer.h:433
uint32_t GetKeySize() const
Get default size of keys stored in QBuffer.
Definition q-buffer.cc:190
uint32_t m_currentKeyBit
The current amount of key material in QKD key storage.
Definition q-buffer.h:452
std::vector< struct QBuffer::data > m_previousValues
Help vector used for graph ploting.
Definition q-buffer.h:429
uint32_t m_dstKmNodeId
Definition q-buffer.h:375
std::vector< int64_t > m_chargingTimePeriods
Vector of durations of several last charging time periods.
Definition q-buffer.h:478
uint32_t GetMthr() const
Get the threshold value of QKD storage The threshold value Mthr(t) at the time of measurement t is us...
Definition q-buffer.cc:537
uint32_t m_bitsUsedInTimePeriod
Help value used for detection of average key usage.
Definition q-buffer.h:418
static TypeId GetTypeId()
Get the TypeId.
Definition q-buffer.cc:32
int64_t m_lastKeyChargingTimeDuration
The timestamp of last key usage.
Definition q-buffer.h:467
uint32_t m_bitsChargedInTimePeriod
Help value used for graph ploting and calculation of average post-processing duration.
Definition q-buffer.h:413
TracedCallback< double > m_CMetricChangeTrace
Definition q-buffer.h:499
virtual Ptr< QKDKey > GetKey(std::string keyID="", bool fireTraces=true)
get key from QBuffer
Definition q-buffer.cc:360
virtual uint32_t GetBitCount() const
get amount of stored key material in bits
Definition q-buffer.h:167
double GetAverageKeyChargingTimePeriod()
Return average duration of key charging process in the long run.
Definition q-buffer.cc:509
double m_c
average amount of key in the buffer during the recalculate time period
Definition q-buffer.h:431
uint32_t m_status
The state of the Net Device transmit state machine.
Definition q-buffer.h:483
TracedCallback< Ptr< QKDKey > > m_newKeyAddedTrace
Definition q-buffer.h:492
uint32_t GetId() const
Get the QKD Storage/Buffer ID.
Definition q-buffer.cc:474
uint32_t GetIndex()
Get the index of the buffer per local node.
Definition q-buffer.cc:562
int64_t m_lastKeyChargingTimeStamp
The timestamp of last key charging(when the new key material was added)
Definition q-buffer.h:462
uint32_t m_bufferID
unique buffer ID
Definition q-buffer.h:379
virtual void LogUpdate(uint32_t diffValue, bool positive)
Log key consumption.
Definition q-buffer.cc:230
uint32_t m_srcNodeBufferListIndex
Definition q-buffer.h:502
uint32_t m_currentKeyBitPrevious
The previous value of current amount of key material in QKD key storage.
Definition q-buffer.h:457
uint32_t m_defaultKeySize
Definition q-buffer.h:445
static uint32_t nBuffers
number of created buffers - static value
Definition q-buffer.h:381
void InitTotalGraph() const
Help function for total graph ploting.
Definition q-buffer.cc:480
uint32_t GetMmin() const
Definition q-buffer.cc:269
double m_averageKeyChargingTimePeriod
The average duration of key charging time period.
Definition q-buffer.h:488
TracedCallback< uint32_t > m_currentKeyBitChangeTrace
Definition q-buffer.h:495
uint32_t GetKeyCount() const
get number of stored keys
Definition q-buffer.cc:279
~QBuffer() override
QBuffer destructor.
Definition q-buffer.cc:176
static const uint32_t QSTATUS_EMPTY
QStatus EMPTY.
Definition q-buffer.h:77
void CheckState()
Update the state after some changes on the QBuffer.
Definition q-buffer.cc:429
uint32_t GetPreviousState()
Fetch the previous state of the QBuffer.
Definition q-buffer.cc:523
uint32_t GetState()
Fetch the current state of the QBuffer.
Definition q-buffer.cc:516
uint32_t GetMCurrentPrevious() const
Help function used for ploting graphs; Previous - before latest change.
Definition q-buffer.cc:530
uint32_t m_noAddNewValue
Help value used for graph ploting.
Definition q-buffer.h:407
EventId m_calculateRoutingMetric
Definition q-buffer.h:490
TracedCallback< uint32_t > m_StatusChangeTrace
Definition q-buffer.h:498
uint32_t m_previousStatus
Definition q-buffer.h:435
TracedCallback< Ptr< QKDKey > > m_keyServedTrace
Definition q-buffer.h:493
int64_t GetDeltaTime()
Definition q-buffer.cc:501
TracedCallback< uint32_t > m_thresholdKeyBitChangeTrace
Definition q-buffer.h:447
bool DestroyKey(std::string keyId)
destroy key from the QBuffer
Definition q-buffer.cc:409
void KeyCalculation()
Help function used for ploting graphs.
Definition q-buffer.cc:201
virtual bool StoreKey(Ptr< QKDKey > key=nullptr, bool fireTraces=true)
store key in QBuffer
Definition q-buffer.cc:285
TracedCallback< double > m_averageKeyChargingTimePeriodTrace
Definition q-buffer.h:500
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...
Definition simulator.cc:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:397
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
bool compareByData(const QBuffer::data &a, const QBuffer::data &b)
Definition q-buffer.cc:195
uint32_t position
Definition q-buffer.h:81