/
connection.h
169 lines (137 loc) · 4.27 KB
/
connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* \brief Connection to a service
* \author Norman Feske
* \date 2008-08-22
*/
/*
* Copyright (C) 2008-2023 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__BASE__CONNECTION_H_
#define _INCLUDE__BASE__CONNECTION_H_
#include <util/retry.h>
#include <base/env.h>
#include <base/capability.h>
#include <base/log.h>
namespace Genode {
class Connection_base;
template <typename> class Connection;
}
class Genode::Connection_base : Noncopyable, Interface
{
protected:
Env &_env;
Parent::Client _parent_client { };
Id_space<Parent::Client>::Element const _id_space_element;
void _block_for_session_response();
public:
Connection_base(Env &env)
:
_env(env),
_id_space_element(_parent_client, _env.id_space())
{ }
void upgrade(Session::Resources resources)
{
String<80> const args("ram_quota=", resources.ram_quota, ", "
"cap_quota=", resources.cap_quota);
_env.upgrade(_id_space_element.id(), args.string());
}
void upgrade_ram(size_t bytes)
{
upgrade(Session::Resources { Ram_quota{bytes}, Cap_quota{0} });
}
void upgrade_caps(size_t caps)
{
upgrade(Session::Resources { Ram_quota{0}, Cap_quota{caps} });
}
/**
* Extend session quota on demand while calling an RPC function
*
* \param ram amount of RAM to upgrade as response to 'Out_of_ram'
* \param caps amount of caps to upgrade as response to 'Out_of_caps'
*
* \noapi
*/
template <typename FUNC>
auto retry_with_upgrade(Ram_quota ram, Cap_quota caps, FUNC func) -> decltype(func())
{
enum { UPGRADE_ATTEMPTS = ~0U };
return Genode::retry<Out_of_ram>(
[&] () {
return Genode::retry<Out_of_caps>(
[&] () { return func(); },
[&] () { upgrade_caps(caps.value); },
UPGRADE_ATTEMPTS);
},
[&] () { upgrade_ram(ram.value); },
UPGRADE_ATTEMPTS);
}
};
/**
* Representation of an open connection to a service
*/
template <typename SESSION_TYPE>
class Genode::Connection : public Connection_base
{
public:
using Args = String<Parent::Session_args::MAX_SIZE>;
using Session_type = SESSION_TYPE;
using Ram_quota = Genode::Ram_quota;
private:
using Client_id = Parent::Client::Id;
static Capability<SESSION_TYPE> _request(Env &env,
Client_id const &id,
Session_label const &label,
Ram_quota const &ram_quota,
Affinity const &affinity,
Args const &args)
{
/* supplement session quotas and label as session arguments */
Args const complete_args("label=\"", label, "\", "
"ram_quota=", ram_quota, ", "
"cap_quota=", unsigned(SESSION_TYPE::CAP_QUOTA), ", ",
args);
if (complete_args.length() == Args::capacity())
warning("truncated arguments of ",
SESSION_TYPE::service_name(), " session");
try {
return env.session<SESSION_TYPE>(id, complete_args.string(),
affinity);
}
catch (...) {
error(SESSION_TYPE::service_name(), "-session creation failed "
"(", complete_args, ")");
throw;
}
}
Capability<SESSION_TYPE> _cap;
public:
Connection(Env &env,
Session_label const &label,
Ram_quota const &ram_quota,
Affinity const &affinity,
Args const &args)
:
Connection_base(env),
_cap(_request(env, _id_space_element.id(),
label, ram_quota, affinity, args))
{ }
/**
* Constructor
*
* Shortcut for the common case where the affinity is not specified.
*/
Connection(Env &env, Session_label const &label,
Ram_quota const &ram_quota, Args const &args)
:
Connection(env, label, ram_quota, Affinity(), args)
{ }
~Connection() { _env.close(_id_space_element.id()); }
/**
* Return session capability
*/
Capability<SESSION_TYPE> cap() const { return _cap; }
};
#endif /* _INCLUDE__BASE__CONNECTION_H_ */