MetroCollect  2.3.4
SourceEthtool.h
Go to the documentation of this file.
1 //
2 // SourceEthtool.h
3 //
4 // Created on August 8th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <net/if.h>
24 
25 #include <array>
26 #include <optional>
27 #include <string>
28 #include <string_view>
29 #include <vector>
30 
31 #include "Source.h"
32 
33 using namespace std::literals;
34 
35 
40  class SourceEthtool : public SourceBase {
41  protected:
42  static constexpr std::string_view sourcePrefix = "ethtool"sv;
43  static constexpr std::string_view fieldNameAll = "*"sv;
44  static constexpr std::string_view fieldNameInterfaceDescription = "interface"sv;
45  static constexpr std::string_view fieldNameIndexDescription = "index"sv;
46  static constexpr std::array fieldUnitsAssociation = { KeyUnit{"bytes"sv, "bytes"sv}, KeyUnit{"packets"sv, "packets"sv}, KeyUnit{"pkts"sv, "packets"sv} };
47  static constexpr std::string_view defaultUnit = ""sv;
48 
49 
56  template<typename T, typename P = char>
58  public:
59  using ValueType = T;
60  using PaddingType = P;
61 
62  protected:
63  size_t paddingSize_;
64  T* pointer_ = nullptr;
65 
66  public:
70  EthtoolPointer() noexcept : pointer_(nullptr) { }
71 
78  EthtoolPointer(uint32_t cmd, size_t padding = 0) noexcept {
79  this->paddingSize_ = padding;
80  this->pointer_ = static_cast<T*>(calloc(1, sizeof(T) + padding * sizeof(P)));
81  this->pointer_->cmd = cmd;
82  }
83 
90  template<typename Q>
92  *this = std::move(other);
93  }
94 
98  EthtoolPointer(EthtoolPointer&) = delete;
99 
107  template<typename Q>
109  if (this->pointer_ != other.pointer_) {
110  free(static_cast<void*>(this->pointer_));
111  this->pointer_ = nullptr;
112  std::swap(this->pointer_, other.pointer_);
113  }
114  return *this;
115  }
116 
120  EthtoolPointer& operator=(EthtoolPointer&) = delete;
121 
126  free(static_cast<void*>(this->pointer_));
127  }
128 
129 
135  T& operator*() {
136  return *this->pointer_;
137  }
138 
144  const T& operator*() const {
145  return *this->pointer_;
146  }
147 
153  T* operator->() noexcept {
154  return this->pointer_;
155  }
156 
162  const T* operator->() const noexcept {
163  return this->pointer_;
164  }
165 
166 
172  size_t paddingSize() const noexcept {
173  return this->paddingSize_;
174  }
175 
181  size_t totalSizeInBytes() const noexcept {
182  return sizeof(T) + this->paddingSize_ * sizeof(P);
183  }
184 
190  inline caddr_t raw() const noexcept {
191  return reinterpret_cast<caddr_t>(this->pointer_);
192  }
193  };
194 
195 
199  struct InterfaceInfo {
203  struct NameAndIndex {
204  std::string name;
205  std::optional<std::string> index;
206  };
207 
208  std::string name;
209  std::string driver;
210  size_t fieldCount;
211  std::vector<NameAndIndex> fieldNames;
212  std::vector<size_t> fieldIndexes;
213  struct ifreq ifr;
214 
216 
217  explicit InterfaceInfo(const char* aName);
218 
219  InterfaceInfo() = delete;
220  };
221 
222 
226  struct Socketfd {
227  int fd;
228 
229  Socketfd();
230  ~Socketfd();
231  Socketfd(const Socketfd&) = delete;
232  Socketfd& operator=(const Socketfd&) = delete;
233  };
234 
235  std::vector<InterfaceInfo> ifInfo_;
237 
238  SourceEthtool();
239  void gatherIfData();
240  InterfaceInfo::NameAndIndex parseEthtoolString(std::string ethtoolString);
241  void ixgbeIfFilter(InterfaceInfo& ifInfo);
242 
243  public:
247  static SourceEthtool& get() {
248  static SourceEthtool instance;
249  return instance;
250  }
251 
252  size_t fieldCount() const noexcept override final;
253  const std::vector<size_t> indexesOfFieldName(const FieldName& fieldName, Interests* interests = nullptr) const noexcept override final;
254  const std::string fieldNameSourcePrefix() const noexcept override final;
255  const FieldInfo fieldInfoAtIndex(size_t index) const noexcept override final;
256  const std::vector<FieldInfo> allFieldsInfo() const noexcept override final;
257 
258  void fetchData(const Interests& interests, DataArray::Iterator current) override final;
259  void computeDiff(const Interests& interests, DiffArray::Iterator diff, DataArray::ConstIterator current, DataArray::ConstIterator previous, double factor = 1) noexcept override final;
260  };
261 }
Object used to describe a field (or metric)
Definition: SourceField.h:37
EthtoolPointer & operator=(EthtoolPointer< T, Q > &&other) noexcept
Move assignment operator.
const T & operator*() const
Dereferences pointer to the managed object.
Stuct to associate a field name containing a certain key with a unit.
Definition: SourceTools.h:78
Socketfd socketfd_
Ethtool socket manager.
std::vector< std::string > FieldName
Type used for field names (an array of strings)
Definition: SourceField.h:31
Namespace for sources of metrics objects and operations.
std::vector< InterfaceInfo > ifInfo_
Array holding details of each network interface.
std::optional< std::string > index
index of the metric if the metric name has an index
Class managing a pointer to a dynamically sized type.
Definition: SourceEthtool.h:57
std::vector< DataValueType >::const_iterator ConstIterator
Const iterator type of MetricsDataArray.
Definition: MetricTypes.h:37
~EthtoolPointer()
Destroy the Ethtool Pointer object.
EthtoolPointer() noexcept
Construct a new, empty Ethtool Pointer object.
Definition: SourceEthtool.h:70
Sources of network metrics extracted from Ethtool interface.
Definition: SourceEthtool.h:40
STL namespace.
size_t totalSizeInBytes() const noexcept
Total size of the managed object.
EthtoolPointer< struct ethtool_stats, uint64_t > statsValues
Ethtool statistics array.
EthtoolPointer(EthtoolPointer< T, Q > &&other) noexcept
Move constructor.
Definition: SourceEthtool.h:91
Boolean array to keep track of which fields a source has to fetch metrics for.
std::vector< size_t > fieldIndexes
Indexes of relevant metrics (some may be skipped)
size_t paddingSize() const noexcept
Padding size appended after the object.
std::string driver
Driver name of the interface.
std::vector< NameAndIndex > fieldNames
Names of the metrics.
caddr_t raw() const noexcept
Returns the raw managed pointer with Ethtool-specific type.
Singleton abstract class, base class of all metric sources classes.
Definition: Source.h:42
size_t fieldCount
Number of metrics of the interface.
Class to store the details and metrics names of a network interface.
size_t paddingSize_
Size of added dynamic padding, in multiples of type P size.
Definition: SourceEthtool.h:63
const T * operator->() const noexcept
Dereferences pointer to the managed object.
std::vector< DataValueType >::iterator Iterator
Iterator type of MetricsDataArray.
Definition: MetricTypes.h:36
T & operator*()
Dereferences pointer to the managed object.
T * operator->() noexcept
Dereferences pointer to the managed object.
EthtoolPointer(uint32_t cmd, size_t padding=0) noexcept
Construct a new Ethtool Pointer object.
Definition: SourceEthtool.h:78