1/*
2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "vom/stat_reader.hpp"
17#include "vom/interface.hpp"
18
19namespace VOM {
20
21stat_reader::stat_indexes_t stat_reader::m_stat_itf_indexes;
22
23stat_reader::stat_reader()
24  : m_client()
25{}
26
27stat_reader::stat_reader(stat_client sc)
28  : m_client(sc)
29{}
30
31stat_reader::~stat_reader() {}
32
33int
34stat_reader::connect()
35{
36  return m_client.connect();
37}
38
39void
40stat_reader::disconnect()
41{
42  m_client.disconnect();
43}
44
45void
46stat_reader::registers(const interface& intf)
47{
48  m_stat_itf_indexes.insert(intf.handle_i().value());
49}
50
51void
52stat_reader::unregisters(const interface& intf)
53{
54  m_stat_itf_indexes.erase(intf.handle_i().value());
55}
56
57void
58stat_reader::read()
59{
60  std::set<std::shared_ptr<interface>> itfs_w_stats;
61  const stat_client::stat_data_vec_t& sd = m_client.dump();
62
63  for (auto& sde : sd) {
64    std::string name;
65
66    if (sde.name().empty())
67      continue;
68
69    name = sde.name();
70
71    if (name.find("/if") != std::string::npos)
72      name.erase(0, 4);
73
74    switch (sde.type()) {
75      case STAT_DIR_TYPE_ERROR_INDEX:
76      case STAT_DIR_TYPE_SCALAR_INDEX:
77      case STAT_DIR_TYPE_NAME_VECTOR:
78      case STAT_DIR_TYPE_ILLEGAL:
79      case STAT_DIR_TYPE_EMPTY:
80        break;
81
82      case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE: {
83        uint64_t** data;
84
85        data = sde.get_stat_segment_simple_counter_data();
86
87        for (auto& i : m_stat_itf_indexes) {
88          counter_t count;
89
90          for (int k = 0; k < m_client.vec_len(data); k++) {
91            count.packets += data[k][i];
92          }
93
94          std::shared_ptr<interface> itf = interface::find(i);
95          if (itf) {
96            itf->set(count, name);
97            itfs_w_stats.insert(itf);
98          }
99        }
100        break;
101      }
102
103      case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED: {
104        vlib_counter_t** data;
105
106        data = sde.get_stat_segment_combined_counter_data();
107
108        for (auto& i : m_stat_itf_indexes) {
109          counter_t count;
110
111          for (int k = 0; k < m_client.vec_len(data); k++) {
112            count.packets += data[k][i].packets;
113            count.bytes += data[k][i].bytes;
114          }
115
116          std::shared_ptr<interface> itf = interface::find(i);
117          if (itf) {
118            itf->set(count, name);
119            itfs_w_stats.insert(itf);
120          }
121        }
122        break;
123      }
124    }
125  }
126  for (auto itf : itfs_w_stats) {
127    itf->publish_stats();
128  }
129}
130
131} // namespace VOM
132
133/*
134 * fd.io coding-style-patch-verification: ON
135 *
136 * Local Variables:
137 * eval: (c-set-style "mozilla")
138 * End:
139 */
140