L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
stats.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020, 2025 Kernkonzept GmbH.
3 * Author(s): Jan Klötzke <jan.kloetzke@kernkonzept.com>
4 *
5 * This file is distributed under the terms of the GNU General Public
6 * License, version 2. Please see the COPYING-GPL-2 file for details.
7 */
8#pragma once
9
10#include <l4/sys/capability>
11#include <l4/sys/cxx/ipc_iface>
12#include <l4/re/dataspace>
13#include <l4/re/util/unique_cap>
14
15#include <cstring>
16#include <memory>
17
23
24namespace Virtio_net_switch {
25
40
51
58class Statistics_if : public L4::Kobject_t<Statistics_if, L4::Kobject>
59{
60public:
70
78 L4_INLINE_RPC(long, sync, ());
79
81};
82
91class Monitor
92{
93public:
95 : _cap(std::move(cap))
96 {
98 L4Re::chksys(_cap->get_buffer(_ds.get()),
99 "Could not get stats dataspace from switch.");
100 void *addr;
101 L4Re::chksys(L4Re::Env::env()->rm()->attach(&addr, _ds->size(),
104 "Could not attach stats dataspace.");
105 _stats = reinterpret_cast<Virtio_net_switch::Statistics *>(addr);
106 }
107
108 ~Monitor()
109 {
110 L4Re::Env::env()->rm()->detach(reinterpret_cast<l4_addr_t>(_stats), 0);
111 }
112
120 Port_statistics *get_port_stats(char const *name) const
121 {
122 for(size_t i = 0; i < _stats->max_ports; ++i)
123 {
124 if (_stats->port_stats[i].in_use
125 && !strncmp (_stats->port_stats[i].name, name,
126 sizeof(_stats->port_stats[i].name)))
127 return &_stats->port_stats[i];
128 }
129 return nullptr;
130 }
131
139 bool get_port_mac(char const *name, unsigned char *mac) const
140 {
141 for (l4_uint64_t i = 0; i < _stats->max_ports; ++i)
142 {
143 if (_stats->port_stats[i].in_use
144 && !strncmp (_stats->port_stats[i].name, name, 20))
145 {
146 std::memcpy(mac, _stats->port_stats[i].mac, 6);
147 return true;
148 }
149 }
150 return false;
151 }
152
156 void sync() const
157 {
158 L4Re::chksys(_cap->sync(),
159 "Synchronizing statistics information failed.\n");
160 }
161
162 l4_uint64_t age() const
163 { return _stats->age; }
164
165private:
168 Statistics *_stats;
169};
170
171class Port_monitor
172{
173private:
174 std::shared_ptr<Monitor> _m;
175 char _name[20];
176 l4_uint64_t _age;
177 Port_statistics *_stats; //only valid for the given age
178
179public:
183 Port_monitor(std::shared_ptr<Monitor> m, char const *name)
184 : _m(m)
185 {
186 strncpy(_name, name, sizeof(_name) - 1);
187 _name[sizeof(_name) - 1] = '\0';
188 _age = _m->age();
189 _stats = _m->get_port_stats(_name);
190 }
191
198 void stats(l4_uint64_t *tx_num,
199 l4_uint64_t *tx_dropped,
200 l4_uint64_t *tx_bytes,
201 l4_uint64_t *rx_num,
202 l4_uint64_t *rx_dropped,
203 l4_uint64_t *rx_bytes)
204 {
205 // ports have changed
206 if (_age != _m->age())
207 {
208 _stats = _m->get_port_stats(_name);
209 _age = _m->age();
210 }
211
212 // no port found
213 if (!_stats)
214 {
215 tx_num = tx_dropped = tx_bytes = rx_num = rx_dropped = rx_bytes = 0;
216 return;
217 }
218
219 *tx_num = _stats->tx_num;
220 *tx_dropped = _stats->tx_dropped;
221 *tx_bytes = _stats->tx_bytes;
222 *rx_num = _stats->rx_num;
223 *rx_dropped = _stats->rx_dropped;
224 *rx_bytes = _stats->rx_bytes;
225 }
226};
227}
L4::Cap related definitions.
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4::Cap< Rm > rm() const noexcept
Object-capability to the region map.
Definition env:120
C++ interface for capabilities.
Definition capability.h:249
Helper class to create an L4Re interface class that is derived from a single base class.
Definition __typeinfo.h:750
bool get_port_mac(char const *name, unsigned char *mac) const
Retrieve the MAC address of a port.
Definition stats.h:139
void sync() const
Instruct the network switch to update the statistics information.
Definition stats.h:156
Port_statistics * get_port_stats(char const *name) const
Retrieve pointer to Port_statistics structure.
Definition stats.h:120
IPC interface for the statistics interface.
Definition stats.h:59
long sync()
Instruct the switch to update the statistics information in the shared memory buffer.
long get_buffer(L4::Ipc::Out< L4::Cap< L4Re::Dataspace > > ds)
Get shared memory buffer containing port statistics.
Dataspace interface.
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
unsigned long long l4_uint64_t
Unsigned 64bit value.
Definition l4int.h:31
@ L4_CAP_FPAGE_RO
Read right for capability flexpages.
Definition __l4_fpage.h:176
Interface Definition Language.
#define L4_INLINE_RPC(res, name, args, attr...)
Define an inline RPC call (type and callable).
Definition ipc_iface:483
Unique_cap< T > make_unique_cap()
Allocate a capability slot and wrap it in an Unique_cap.
Definition unique_cap:56
L4::Detail::Unique_cap_impl< T, L4Re::Util::Smart_cap_auto< L4_FP_DELETE_OBJ > > Unique_del_cap
Unique capability that implements automatic free and unmap+delete of the capability slot.
Definition unique_cap:87
L4::Detail::Unique_cap_impl< T, L4Re::Util::Smart_cap_auto< L4_FP_ALL_SPACES > > Unique_cap
Unique capability that implements automatic free and unmap of the capability slot.
Definition unique_cap:43
l4_ret_t chksys(l4_ret_t err, char const *extra="", l4_ret_t ret=0)
Generate C++ exception on error.
Definition error_helper:72
Cap< T > make_cap(L4::Cap< T > cap, unsigned rights) noexcept
Make an L4::Ipc::Cap<T> for the given capability and rights.
Definition ipc_types:845
@ R
Readable region.
Definition rm:135
@ Search_addr
Search for a suitable address range.
Definition rm:115
Mark an argument as a output value in an RPC signature.
Definition ipc_types:31
Standard list of RPCs of an interface.
Definition __typeinfo.h:428
Statistics for one port.
Definition stats.h:28
unsigned char mac[6]
MAC address of a port.
Definition stats.h:35
l4_uint64_t rx_bytes
bytes successfully received
Definition stats.h:34
l4_uint64_t tx_bytes
bytes successfully sent
Definition stats.h:31
char name[20]
name of a port
Definition stats.h:36
unsigned char in_use
1 iff the data structure is currently in use, 0 otherwise
Definition stats.h:37
l4_uint64_t rx_num
number of successful receive requests
Definition stats.h:32
l4_uint64_t rx_dropped
number of dropped receive requests
Definition stats.h:33
l4_uint64_t tx_dropped
number of dropped send request
Definition stats.h:30
l4_uint64_t tx_num
number of successful send requests
Definition stats.h:29
Base statistics data structure, resides at the beginning of shared memory.
Definition stats.h:43
l4_uint64_t max_ports
The maximum number of ports that the switch supports.
Definition stats.h:47
struct Port_statistics port_stats[]
Array of Port_statistics.
Definition stats.h:49
l4_uint64_t age
This value increases on any change in the data structure.
Definition stats.h:44
Unique_cap / Unique_del_cap.