L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
stats.h
1#include <l4/re/env>
2#include <l4/re/dataspace>
3#include <l4/re/error_helper>
5#include <l4/virtio-net-switch/stats.h>
6
7class Switch_statistics
8{
9private:
10 Virtio_net_switch::Statistics *_stats;
11 bool _initialized = false;
12
13 Switch_statistics() {}
14
15 ~Switch_statistics()
16 {
17 if (_initialized)
18 L4Re::Env::env()->rm()->detach(reinterpret_cast<l4_addr_t>(_stats), 0);
19 }
20
21 l4_size_t _size;
22
23public:
24 Virtio_net_switch::Statistics *stats()
25 {
26 if (_initialized)
27 return _stats;
28 else
29 throw L4::Runtime_error(-L4_EAGAIN, "Statistics not set up.");
30 }
31
32 static Switch_statistics& get_instance()
33 {
34 static Switch_statistics instance;
35 return instance;
36 }
37
38 void initialize(l4_uint64_t num_max_ports)
39 {
40 _size = l4_round_page(sizeof(Virtio_net_switch::Statistics)
41 + sizeof(Virtio_net_switch::Port_statistics) * num_max_ports);
42 void *addr = malloc(_size);
43 if (!addr)
44 throw L4::Runtime_error(-L4_ENOMEM,
45 "Could not allocate statistics memory.");
46
47 memset(addr, 0, _size);
48 _stats = reinterpret_cast<Virtio_net_switch::Statistics *>(addr);
49 _initialized = true;
50 _stats->max_ports = num_max_ports;
51 }
52
53 Virtio_net_switch::Port_statistics *
54 allocate_port_statistics(char const* name)
55 {
56 for (unsigned i = 0; i < _stats->max_ports; ++i)
57 {
58 if (!_stats->port_stats[i].in_use)
59 {
60 memset(reinterpret_cast<void*>(&_stats->port_stats[i]), 0,
61 sizeof(Virtio_net_switch::Port_statistics));
62 _stats->port_stats[i].in_use = 1;
63 size_t len = std::min(strlen(name), sizeof(_stats->port_stats[i].name) - 1);
64 memcpy(_stats->port_stats[i].name, name, len);
65 _stats->port_stats[i].name[len] = '\0';
66 _stats->age++;
67 return &_stats->port_stats[i];
68 }
69 }
70 return nullptr;
71 }
72
73 inline l4_size_t size()
74 { return _size; }
75
76 Switch_statistics(Switch_statistics const&) = delete;
77 void operator=(Switch_statistics const &) = delete;
78};
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
Dataspace interface.
Environment interface.
Error helper.
unsigned int l4_size_t
Unsigned size type.
Definition l4int.h:22
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_EAGAIN
Try again.
Definition err.h:39
@ L4_ENOMEM
No memory.
Definition err.h:40
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
Definition consts.h:490
Capability allocator.