]> git.proxmox.com Git - ceph.git/blob - ceph/src/msg/Policy.h
0cfa03aee5701c197fde1985f175d5d26adfaf70
[ceph.git] / ceph / src / msg / Policy.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #pragma once
5
6 #include "include/ceph_features.h"
7
8 namespace ceph::net {
9
10 using peer_type_t = int;
11
12 /**
13 * A Policy describes the rules of a Connection. Is there a limit on how
14 * much data this Connection can have locally? When the underlying connection
15 * experiences an error, does the Connection disappear? Can this Messenger
16 * re-establish the underlying connection?
17 */
18 template<class ThrottleType>
19 struct Policy {
20 /// If true, the Connection is tossed out on errors.
21 bool lossy;
22 /// If true, the underlying connection can't be re-established from this end.
23 bool server;
24 /// If true, we will standby when idle
25 bool standby;
26 /// If true, we will try to detect session resets
27 bool resetcheck;
28
29 /// Server: register lossy client connections.
30 bool register_lossy_clients = true;
31 // The net result of this is that a given client can only have one
32 // open connection with the server. If a new connection is made,
33 // the old (registered) one is closed by the messenger during the accept
34 // process.
35
36 /**
37 * The throttler is used to limit how much data is held by Messages from
38 * the associated Connection(s). When reading in a new Message, the Messenger
39 * will call throttler->throttle() for the size of the new Message.
40 */
41 ThrottleType* throttler_bytes;
42 ThrottleType* throttler_messages;
43
44 /// Specify features supported locally by the endpoint.
45 #ifdef MSG_POLICY_UNIT_TESTING
46 uint64_t features_supported{CEPH_FEATURES_SUPPORTED_DEFAULT};
47 #else
48 static constexpr uint64_t features_supported{CEPH_FEATURES_SUPPORTED_DEFAULT};
49 #endif
50
51 /// Specify features any remotes must have to talk to this endpoint.
52 uint64_t features_required;
53
54 Policy()
55 : lossy(false), server(false), standby(false), resetcheck(true),
56 throttler_bytes(NULL),
57 throttler_messages(NULL),
58 features_required(0) {}
59 private:
60 Policy(bool l, bool s, bool st, bool r, bool rlc, uint64_t req)
61 : lossy(l), server(s), standby(st), resetcheck(r),
62 register_lossy_clients(rlc),
63 throttler_bytes(NULL),
64 throttler_messages(NULL),
65 features_required(req) {}
66
67 public:
68 static Policy stateful_server(uint64_t req) {
69 return Policy(false, true, true, true, true, req);
70 }
71 static Policy stateless_registered_server(uint64_t req) {
72 return Policy(true, true, false, false, true, req);
73 }
74 static Policy stateless_server(uint64_t req) {
75 return Policy(true, true, false, false, false, req);
76 }
77 static Policy lossless_peer(uint64_t req) {
78 return Policy(false, false, true, false, true, req);
79 }
80 static Policy lossless_peer_reuse(uint64_t req) {
81 return Policy(false, false, true, true, true, req);
82 }
83 static Policy lossy_client(uint64_t req) {
84 return Policy(true, false, false, false, true, req);
85 }
86 static Policy lossless_client(uint64_t req) {
87 return Policy(false, false, false, true, true, req);
88 }
89 };
90
91 template<class ThrottleType>
92 class PolicySet {
93 using policy_t = Policy<ThrottleType> ;
94 /// the default Policy we use for Pipes
95 policy_t default_policy;
96 /// map specifying different Policies for specific peer types
97 std::map<int, policy_t> policy_map; // entity_name_t::type -> Policy
98
99 public:
100 const policy_t& get(peer_type_t peer_type) const {
101 if (auto found = policy_map.find(peer_type); found != policy_map.end()) {
102 return found->second;
103 } else {
104 return default_policy;
105 }
106 }
107 policy_t& get(peer_type_t peer_type) {
108 if (auto found = policy_map.find(peer_type); found != policy_map.end()) {
109 return found->second;
110 } else {
111 return default_policy;
112 }
113 }
114 void set(peer_type_t peer_type, const policy_t& p) {
115 policy_map[peer_type] = p;
116 }
117 const policy_t& get_default() const {
118 return default_policy;
119 }
120 void set_default(const policy_t& p) {
121 default_policy = p;
122 }
123 void set_throttlers(peer_type_t peer_type,
124 ThrottleType* byte_throttle,
125 ThrottleType* msg_throttle) {
126 auto& policy = get(peer_type);
127 policy.throttler_bytes = byte_throttle;
128 policy.throttler_messages = msg_throttle;
129 }
130 };
131
132 }