EpcTools
An event based multi-threaded C++ development framework.
estats.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2019 Sprint
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #ifndef __ESTATS_H
18 #define __ESTATS_H
19 
23 
24 #include <atomic>
25 #include <unordered_map>
26 
27 #include "ebase.h"
28 #include "eerror.h"
29 #include "efd.h"
30 #include "elogger.h"
31 #include "estring.h"
32 #include "etime.h"
33 #include "esynch.h"
34 
35 //
36 // According to IANA, the current diameter application ID range is from 0 to 16777361.
37 // Diameter application ID's start at 0 and appear to be "reserved" through 16777215 (0x00FFFFFF).
38 // 3GPP diameter applications ID's appear to have the low order bit of the high order word set to 1 (16777216 decimal or 0x1000000 hex).
39 //
41 #define GTPV2C_BASE (0xff000000)
42 #define GTPV1U_BASE (0xfe000000)
43 #define PFCP_BASE (0xfd000000)
44 
45 const UInt INTERFACE_S11C = (GTPV2C_BASE + 1);
46 const UInt INTERFACE_S5S8C = (GTPV2C_BASE + 2);
47 const UInt INTERFACE_S4 = (GTPV2C_BASE + 3);
48 const UInt INTERFACE_S2b = (GTPV2C_BASE + 4);
49 
50 const UInt INTERFACE_S1U = (GTPV1U_BASE + 1);
51 const UInt INTERFACE_S11U = (GTPV1U_BASE + 2);
52 const UInt INTERFACE_S5S8U = (GTPV1U_BASE + 3);
53 const UInt INTERFACE_SWu = (GTPV1U_BASE + 4);
54 
55 const UInt INTERFACE_Sxa = (PFCP_BASE + 1);
56 const UInt INTERFACE_Sxb = (PFCP_BASE + 2);
57 const UInt INTERFACE_Sxc = (PFCP_BASE + 3);
58 const UInt INTERFACE_SxaSxb = (PFCP_BASE + 4);
59 
60 const UInt DIAMETER_ANSWER_BIT = 0x80000000;
62 
64 {
65 public:
66  enum class ProtocolType
67  {
68  diameter,
69  gtpv2c,
70  gtpv1u,
71  pfcp,
72  ikev2
73  };
74 
75  typedef UInt InterfaceId;
76  typedef UInt MessageId;
77 
80  class DiameterHook : public FDHook
81  {
82  public:
84  DiameterHook()
85  : m_logger( NULL )
86  {
87  }
88 
89  Void process(enum fd_hook_type type, struct msg * msg, struct peer_hdr * peer,
90  Void * other, struct fd_hook_permsgdata *pmd);
91 
92  Void setLogger(ELogger &logger) { m_logger = &logger; }
94 
95  private:
96  Bool getResult(struct msg *m);
97 
98  ELogger *m_logger;
99  };
100 
103  {
104  public:
105  MessageStats(EStatistics::MessageId id, cpStr name);
107  MessageStats(const MessageStats &m);
108 
109  Void reset();
110 
111  EStatistics::MessageId getId() { return m_id; }
112  const EString &getName() { return m_name; }
113 
116  UInt getRequestSentErrors() { return m_rqst_sent_err; }
119  UInt getRequestReceivedErrors() { return m_rqst_rcvd_err; }
122  UInt getRequestSentOk() { return m_rqst_sent_ok; }
125  UInt getRequestReceivedOk() { return m_rqst_rcvd_ok; }
126 
129  UInt getResponseSentErrors() { return m_resp_sent_err; }
132  UInt getResponseReceivedErrors() { return m_resp_rcvd_err; }
135  UInt getResponseSentOkAccepted() { return m_resp_sent_accept; }
138  UInt getResponseSentOkRejected() { return m_resp_sent_reject; }
141  UInt getResponseReceivedOkAccepted() { return m_resp_rcvd_accept; }
144  UInt getResponseReceivedOkRejected() { return m_resp_rcvd_reject; }
145 
148  UInt incRequestSentErrors() { return ++m_rqst_sent_err; }
151  UInt incRequestReceivedErrors() { return ++m_rqst_rcvd_err; }
154  UInt incRequestSentOk() { return ++m_rqst_sent_ok; }
157  UInt incRequestReceivedOk() { return ++m_rqst_rcvd_ok; }
158 
161  UInt incResponseSentErrors() { return ++m_resp_sent_err; }
164  UInt incResponseReceivedErrors() { return ++m_resp_rcvd_err; }
167  UInt incResponseSentOkAccepted() { return ++m_resp_sent_accept; }
170  UInt incResponseSentOkRejected() { return ++m_resp_sent_reject; }
173  UInt incResponseReceivedOkAccepted() { return ++m_resp_rcvd_accept; }
176  UInt incResponseReceivedOkRejected() { return ++m_resp_rcvd_reject; }
177 
178  private:
179  MessageStats();
180 
182  EString m_name;
183 
184  std::atomic<UInt> m_rqst_sent_err;
185  std::atomic<UInt> m_rqst_rcvd_err;
186  std::atomic<UInt> m_rqst_sent_ok;
187  std::atomic<UInt> m_rqst_rcvd_ok;
188 
189  std::atomic<UInt> m_resp_sent_err;
190  std::atomic<UInt> m_resp_rcvd_err;
191  std::atomic<UInt> m_resp_sent_accept;
192  std::atomic<UInt> m_resp_sent_reject;
193  std::atomic<UInt> m_resp_rcvd_accept;
194  std::atomic<UInt> m_resp_rcvd_reject;
195  };
196 
197  typedef std::unordered_map<EStatistics::MessageId,EStatistics::MessageStats> MessageStatsMap;
198 
200  class Peer
201  {
202  public:
206  Peer(cpStr name, const EStatistics::MessageStatsMap &tmplt);
210  Peer(const EString &name, const EStatistics::MessageStatsMap &tmplt);
213  Peer(const Peer &p);
214 
217  EStatistics::MessageStats &getMessageStats(UInt msgid);
218 
221  EString &getName() { return m_name; }
224  ETime &getLastActivity() { return m_lastactivity; }
227  ETime &setLastActivity() { return m_lastactivity = ETime::Now(); }
232  Void reset();
233 
235  #define INCREMENT_MESSAGE_STAT(__id,__func) \
236  { \
237  auto srch = m_msgstats.find(__id); \
238  if (srch == m_msgstats.end() ) \
239  return 0; \
240  setLastActivity(); \
241  return srch->second.__func(); \
242  }
243 
248  UInt incRequestSentErrors(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incRequestSentErrors); }
252  UInt incRequestReceivedErrors(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incRequestReceivedErrors); }
256  UInt incRequestSentOk(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incRequestSentOk); }
260  UInt incRequestReceivedOk(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incRequestReceivedOk); }
261 
265  UInt incResponseSentErrors(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseSentErrors); }
269  UInt incResponseReceivedErrors(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseReceivedErrors); }
273  UInt incResponseSentOkAccepted(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseSentOkAccepted); }
277  UInt incResponseSentOkRejected(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseSentOkRejected); }
281  UInt incResponseReceivedOkAccepted(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseReceivedOkAccepted); }
285  UInt incResponseReceivedOkRejected(UInt msgid) { INCREMENT_MESSAGE_STAT(msgid, incResponseReceivedOkRejected); }
286 
288  #undef INCREMENT_MESSAGE_STAT
289 
291  private:
292  EString m_name;
293  ETime m_lastactivity;
294  ERWLock m_lock;
295  EStatistics::MessageStatsMap m_msgstats;
296  };
298  typedef std::unordered_map<std::string,EStatistics::Peer> PeerMap;
299 
301  class Interface
302  {
303  public:
316  Interface(const Interface &i);
317 
320  EStatistics::InterfaceId getId() { return m_id; }
323  EString &getName() { return m_name; }
326  ProtocolType getProtocol() { return m_protocol; }
329  PeerMap &getPeers() { return m_peers; }
330 
333  Peer &getPeer(const EString &peer, Bool addFlag = True);
336  Peer &addPeer(const EString &peer);
338  Void removePeer(const EString &peer);
340  Void reset();
341 
344  MessageStats &addMessageStatsTemplate(EStatistics::MessageId msgid, const EString &name);
347  MessageStatsMap &getMessageStatsTemplate() { return m_msgstats_template; }
348 
350  #define INCREMENT_MESSAGE_STAT(__peer,__id,__func) \
351  { \
352  EStatistics::Peer &__p( getPeer(__peer) ); \
353  return __p.__func(__id); \
354  }
355 
361  UInt incRequestSentErrors(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incRequestSentErrors); }
366  UInt incRequestReceivedErrors(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incRequestReceivedErrors); }
371  UInt incRequestSentOk(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incRequestSentOk); }
376  UInt incRequestReceivedOk(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incRequestReceivedOk); }
377 
382  UInt incResponseSentErrors(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseSentErrors); }
387  UInt incResponseReceivedErrors(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseReceivedErrors); }
392  UInt incResponseSentOkAccepted(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseSentOkAccepted); }
397  UInt incResponseSentOkRejected(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseSentOkRejected); }
402  UInt incResponseReceivedOkAccepted(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseReceivedOkAccepted); }
407  UInt incResponseReceivedOkRejected(cpStr peer, UInt msgid) { EString p(peer); INCREMENT_MESSAGE_STAT(p, msgid, incResponseReceivedOkRejected); }
408 
413  UInt incRequestSentErrors(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incRequestSentErrors); }
418  UInt incRequestReceivedErrors(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incRequestReceivedErrors); }
423  UInt incRequestSentOk(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incRequestSentOk); }
428  UInt incRequestReceivedOk(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incRequestReceivedOk); }
429 
434  UInt incResponseSentErrors(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseSentErrors); }
439  UInt incResponseReceivedErrors(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseReceivedErrors); }
444  UInt incResponseSentOkAccepted(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseSentOkAccepted); }
449  UInt incResponseSentOkRejected(const std::string &peer, Int msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseSentOkRejected); }
454  UInt incResponseReceivedOkAccepted(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseReceivedOkAccepted); }
459  UInt incResponseReceivedOkRejected(const std::string &peer, UInt msgid) { INCREMENT_MESSAGE_STAT(peer, msgid, incResponseReceivedOkRejected); }
460 
462  #undef INCREMENT_MESSAGE_STAT
463 
465  private:
466  Interface();
467 
468  Peer &_addPeer(const EString &peer);
469 
471  ProtocolType m_protocol;
472  EString m_name;
473  ERWLock m_lock;
474  PeerMap m_peers;
475  MessageStatsMap m_msgstats_template;
476  };
477  typedef std::unordered_map<EStatistics::InterfaceId,EStatistics::Interface> InterfaceMap;
478 
484  {
485  ERDLock l(m_lock);
486  auto srch = m_interfaces.find(id);
487  if (srch == m_interfaces.end())
488  {
489  EString s;
490  s.format("Unknown interface ID [%u]", id);
491  throw EError(EError::Warning, s);
492  }
493  return srch->second;
494  }
495 
502  {
503  EWRLock l(m_lock);
504  auto it = m_interfaces.emplace(id, Interface(id,protocol,intfc));
505  return it.first->second;
506  }
507 
511  {
512  EWRLock l(m_lock);
513  auto srch = m_interfaces.find(id);
514  if (srch != m_interfaces.end())
515  m_interfaces.erase( srch );
516  }
517 
520  static InterfaceMap &getInterfaces() { return m_interfaces; }
521 
524  static Void init(ELogger &logger);
526  static Void uninit();
528  static Void reset();
529 
530 private:
531  static DiameterHook m_hook_error;
532  static DiameterHook m_hook_success;
533 
534  static ERWLock m_lock;
535  static EStatistics::InterfaceMap m_interfaces;
536 };
537 
538 #endif // #ifndef __ESTATS_H
freeDiameter wrapper classes.
UInt incResponseSentErrors(UInt msgid)
Increments the response send errors for the specified message.
Definition: estats.h:265
Encapsulates and extends a std::string object.
#define True
True.
Definition: ebase.h:25
Request a read lock for the specified read-write lock object.
Definition: esynch.h:535
UInt getRequestReceivedErrors()
Retrieves the request receive errors for this message.
Definition: estats.h:119
UInt incResponseReceivedOkRejected()
Increments the response receive successes that were rejected for this message.
Definition: estats.h:176
UInt getResponseReceivedOkRejected()
Retrieves the response receive successes that were rejected for this message.
Definition: estats.h:144
static ETime Now()
Retrieves the current time.
Definition: etime.cpp:1147
UInt incResponseReceivedOkAccepted(cpStr peer, UInt msgid)
Increments the response receive success accepted for the specified peer and message.
Definition: estats.h:402
Macros for various standard C library functions and standard includes.
UInt incRequestSentErrors(cpStr peer, UInt msgid)
Increments the request send errors for the specified peer and message.
Definition: estats.h:361
UInt incResponseReceivedErrors(const std::string &peer, UInt msgid)
Increments the response receive errors for the specified peer and message.
Definition: estats.h:439
UInt incResponseSentOkRejected()
Increments the response send successes that were rejected for this message.
Definition: estats.h:170
Definition: estats.h:63
Hooks into the freeDiameter internals to increment the message statistics.
Definition: estats.h:80
UInt incRequestReceivedOk(const std::string &peer, UInt msgid)
Increments the request receive successes for the specified peer and message.
Definition: estats.h:428
static Void init(ELogger &logger)
Initializes the statistics class.
Definition: estats.cpp:24
Warning.
Definition: eerror.h:103
UInt incRequestSentOk(const std::string &peer, UInt msgid)
Increments the request send successes for the specified peer and message.
Definition: estats.h:423
UInt incResponseSentOkRejected(UInt msgid)
Increments the response send success rejected for the specified message.
Definition: estats.h:277
Request a write lock for the specified read-write lock object.
Definition: esynch.h:574
UInt incRequestReceivedOk(UInt msgid)
Increments the request receive successes for the specified message.
Definition: estats.h:260
ProtocolType
Definition: estats.h:66
UInt incResponseSentOkRejected(cpStr peer, UInt msgid)
Increments the response send success rejected for the specified peer and message. ...
Definition: estats.h:397
UInt incRequestSentOk(cpStr peer, UInt msgid)
Increments the request send successes for the specified peer and message.
Definition: estats.h:371
UInt incResponseSentOkAccepted(UInt msgid)
Increments the response send success aceepted for the specified message.
Definition: estats.h:273
UInt incRequestReceivedErrors()
Increments the request receive errors for this message.
Definition: estats.h:151
UInt incRequestReceivedErrors(UInt msgid)
Increments the request receive errors for the specified message.
Definition: estats.h:252
const EString & getName()
Definition: estats.h:112
UInt getRequestReceivedOk()
Retrieves the request received successes for this message.
Definition: estats.h:125
Provides class for manipulating time of day values.
UInt incResponseReceivedErrors(UInt msgid)
Increments the response receive errors for the specified message.
Definition: estats.h:269
UInt incRequestSentOk()
Increments the request send successes for this message.
Definition: estats.h:154
UInt incResponseReceivedOkAccepted(UInt msgid)
Increments the response received success aceepted for the specified message.
Definition: estats.h:281
static InterfaceMap & getInterfaces()
Retrieves the interface collection.
Definition: estats.h:520
UInt incResponseSentOkAccepted(const std::string &peer, UInt msgid)
Increments the response send success accepted for the specified peer and message. ...
Definition: estats.h:444
UInt incResponseSentErrors()
Increments the response send errors for this message.
Definition: estats.h:161
UInt getRequestSentErrors()
Retrieves the request send errors for this message.
Definition: estats.h:116
Defines the logging related classes.
Class for manipulating date and time of day values.
Definition: etime.h:199
UInt incResponseReceivedOkAccepted()
Increments the response receive successes that were accepted for this message.
Definition: estats.h:173
PeerMap & getPeers()
Retrieves the collection of peers for this interface.
Definition: estats.h:329
UInt getResponseSentErrors()
Retrieves the response send errors for this message.
Definition: estats.h:129
UInt getResponseReceivedOkAccepted()
Retrieves the response receive successes that were accepted for this message.
Definition: estats.h:141
UInt incResponseSentOkRejected(const std::string &peer, Int msgid)
Increments the response send success rejected for the specified peer and message. ...
Definition: estats.h:449
Contains the peer information including the statistics for the individual messages.
Definition: estats.h:200
UInt incRequestSentErrors(UInt msgid)
Increments the request send errors for the specified message.
Definition: estats.h:248
static Interface & addInterface(EStatistics::InterfaceId id, ProtocolType protocol, const EString &intfc)
Adds/updates the interface.
Definition: estats.h:501
UInt incResponseSentErrors(const std::string &peer, UInt msgid)
Increments the response send errors for the specified peer and message.
Definition: estats.h:434
UInt getResponseSentOkAccepted()
Retrieves the response send successes that were accepted for this message.
Definition: estats.h:135
std::unordered_map< EStatistics::MessageId, EStatistics::MessageStats > MessageStatsMap
Definition: estats.h:197
ProtocolType getProtocol()
Retrieves the interface protocol.
Definition: estats.h:326
MessageStatsMap & getMessageStatsTemplate()
Retrieves the collection of message statistics.
Definition: estats.h:347
UInt getRequestSentOk()
Retrieves the request send successes for this message.
Definition: estats.h:122
Defines base class for exceptions and declaration helper macros.
EStatistics::InterfaceId getId()
Retrieves the interface ID.
Definition: estats.h:320
UInt InterfaceId
Definition: estats.h:75
UInt incRequestReceivedOk()
Increments the request received successes for this message.
Definition: estats.h:157
std::unordered_map< EStatistics::InterfaceId, EStatistics::Interface > InterfaceMap
Definition: estats.h:477
UInt incRequestReceivedErrors(const std::string &peer, UInt msgid)
Increments the request receive errors for the specified peer and message.
Definition: estats.h:418
UInt incResponseReceivedOkRejected(UInt msgid)
Increments the response received success rejected for the specified message.
Definition: estats.h:285
The base class for exceptions derived from std::exception.
Definition: eerror.h:94
EStatistics::MessageStatsMap & getMessageStats()
Retrieves the message statistics collection for this peer.
Definition: estats.h:230
EString & getName()
Retrieves the interface name.
Definition: estats.h:323
Contains the statistics for an individual message.
Definition: estats.h:102
UInt incRequestSentErrors(const std::string &peer, UInt msgid)
Increments the request send errors for the specified peer and message.
Definition: estats.h:413
Represents an interface to track peer and message statistics for.
Definition: estats.h:301
std::unordered_map< std::string, EStatistics::Peer > PeerMap
Defines a collection of peers.
Definition: estats.h:298
UInt getResponseSentOkRejected()
Retrieves the response send successes that were rejected for this message.
Definition: estats.h:138
UInt incResponseSentOkAccepted(cpStr peer, UInt msgid)
Increments the response send success accepted for the specified peer and message. ...
Definition: estats.h:392
static Void removeInterface(EStatistics::InterfaceId id)
Removes the specified interface.
Definition: estats.h:510
Defines a logger.
Definition: elogger.h:76
static Interface & getInterface(EStatistics::InterfaceId id)
Retrieves the requested interface object.
Definition: estats.h:483
Registers, unregisters and process freeDiameter hooks. See "MONITORING" in libfdcore.h in the freeDiameter library.
Definition: efd.h:1792
EString & getName()
Retrieves the peer name.
Definition: estats.h:221
UInt incResponseSentOkAccepted()
Increments the response send successes that were accepted for this message.
Definition: estats.h:167
UInt incResponseReceivedOkAccepted(const std::string &peer, UInt msgid)
Increments the response receive success accepted for the specified peer and message.
Definition: estats.h:454
static Void uninit()
Uninitializes teh statistics class.
Definition: estats.cpp:36
ETime & setLastActivity()
Assigns the time stamp of the last activity.
Definition: estats.h:227
static Void reset()
Sets the message counters to zero for all interfaces, peers and messages.
Definition: estats.cpp:42
UInt incRequestSentErrors()
Increments the request send errors for this message.
Definition: estats.h:148
UInt incRequestSentOk(UInt msgid)
Increments the request send successes for the specified message.
Definition: estats.h:256
UInt getResponseReceivedErrors()
Retrieves the response receive errors for this message.
Definition: estats.h:132
UInt incRequestReceivedErrors(cpStr peer, UInt msgid)
Increments the request receive errors for the specified peer and message.
Definition: estats.h:366
UInt MessageId
Definition: estats.h:76
UInt incRequestReceivedOk(cpStr peer, UInt msgid)
Increments the request receive successes for the specified peer and message.
Definition: estats.h:376
UInt incResponseReceivedOkRejected(const std::string &peer, UInt msgid)
Increments the response receive rejected accepted for the specified peer and message.
Definition: estats.h:459
ETime & getLastActivity()
Retrieves the time stamp of the last activity.
Definition: estats.h:224
UInt incResponseReceivedErrors(cpStr peer, UInt msgid)
Increments the response receive errors for the specified peer and message.
Definition: estats.h:387
EString & format(cpChar pszFormat,...)
Sets the value to the string using a "printf" style format string and arguments.
Definition: estring.cpp:38
EStatistics::MessageId getId()
Definition: estats.h:111
UInt incResponseReceivedOkRejected(cpStr peer, UInt msgid)
Increments the response receive rejected accepted for the specified peer and message.
Definition: estats.h:407
String class.
Definition: estring.h:31
Contains definitions for synchronization objects.
Encapsulates a read-write lock object.
Definition: esynch.h:507
UInt incResponseReceivedErrors()
Increments the response receive errors for this message.
Definition: estats.h:164
UInt incResponseSentErrors(cpStr peer, UInt msgid)
Increments the response send errors for the specified peer and message.
Definition: estats.h:382