blob: f1d060e019736ce34e285fcfdf831c5b1ce77b63 [file] [log] [blame]
Tom Joseph3e61aa02016-08-08 08:42:39 -05001#pragma once
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include "session.hpp"
4
Vernon Maueryecc8efa2021-06-12 12:52:23 -07005#include <boost/asio/steady_timer.hpp>
6#include <chrono>
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +05307#include <ipmid/api.hpp>
8#include <ipmid/sessiondef.hpp>
Tom Joseph3e61aa02016-08-08 08:42:39 -05009#include <map>
10#include <memory>
11#include <mutex>
Andrew Geissler7408e762020-05-17 08:56:05 -050012#include <string>
Tom Joseph3e61aa02016-08-08 08:42:39 -050013
Tom Joseph3e61aa02016-08-08 08:42:39 -050014namespace session
15{
16
17enum class RetrieveOption
18{
19 BMC_SESSION_ID,
20 RC_SESSION_ID,
21};
22
Vernon Maueryecc8efa2021-06-12 12:52:23 -070023static constexpr size_t maxSessionHandles = multiIntfaceSessionHandleMask;
24
Tom Joseph3563f8f2017-05-08 15:42:54 +053025/**
Tom Joseph3e61aa02016-08-08 08:42:39 -050026 * @class Manager
27 *
28 * Manager class acts a manager for the IPMI sessions and provides interfaces
29 * to start a session, stop a session and get reference to the session objects.
30 *
31 */
32
33class Manager
34{
Vernon Mauery9e801a22018-10-12 13:20:49 -070035 public:
36 // BMC Session ID is the key for the map
37 using SessionMap = std::map<SessionID, std::shared_ptr<Session>>;
Tom Joseph3e61aa02016-08-08 08:42:39 -050038
Vernon Maueryecc8efa2021-06-12 12:52:23 -070039 Manager() = delete;
40 explicit Manager(std::shared_ptr<boost::asio::io_context>& io) :
41 io(io), timer(*io){};
Vernon Mauery9e801a22018-10-12 13:20:49 -070042 ~Manager() = default;
43 Manager(const Manager&) = delete;
44 Manager& operator=(const Manager&) = delete;
45 Manager(Manager&&) = default;
46 Manager& operator=(Manager&&) = default;
Tom Joseph3e61aa02016-08-08 08:42:39 -050047
Vernon Mauery9e801a22018-10-12 13:20:49 -070048 /**
49 * @brief Start an IPMI session
50 *
51 * @param[in] remoteConsoleSessID - Remote Console Session ID mentioned
52 * in the Open SessionRequest Command
53 * @param[in] priv - Privilege level requested
54 * @param[in] authAlgo - Authentication Algorithm
55 * @param[in] intAlgo - Integrity Algorithm
56 * @param[in] cryptAlgo - Confidentiality Algorithm
57 *
58 * @return session handle on success and nullptr on failure
59 *
60 */
Vernon Maueryae1fda42018-10-15 12:55:34 -070061 std::shared_ptr<Session>
62 startSession(SessionID remoteConsoleSessID, Privilege priv,
63 cipher::rakp_auth::Algorithms authAlgo,
64 cipher::integrity::Algorithms intAlgo,
65 cipher::crypt::Algorithms cryptAlgo);
Tom Joseph3e61aa02016-08-08 08:42:39 -050066
Vernon Mauery9e801a22018-10-12 13:20:49 -070067 /**
68 * @brief Stop IPMI Session
69 *
70 * @param[in] bmcSessionID - BMC Session ID
71 *
72 * @return true on success and failure if session ID is invalid
73 *
74 */
75 bool stopSession(SessionID bmcSessionID);
Tom Joseph3e61aa02016-08-08 08:42:39 -050076
Vernon Mauery9e801a22018-10-12 13:20:49 -070077 /**
78 * @brief Get Session Handle
79 *
80 * @param[in] sessionID - Session ID
81 * @param[in] option - Select between BMC Session ID and Remote Console
82 * Session ID, Default option is BMC Session ID
83 *
84 * @return session handle on success and nullptr on failure
85 *
86 */
Vernon Maueryae1fda42018-10-15 12:55:34 -070087 std::shared_ptr<Session>
Vernon Mauery9e801a22018-10-12 13:20:49 -070088 getSession(SessionID sessionID,
89 RetrieveOption option = RetrieveOption::BMC_SESSION_ID);
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +053090 uint8_t getActiveSessionCount() const;
91 uint8_t getSessionHandle(SessionID bmcSessionID) const;
92 uint8_t storeSessionHandle(SessionID bmcSessionID);
93 uint32_t getSessionIDbyHandle(uint8_t sessionHandle) const;
94
95 void managerInit(const std::string& channel);
96
97 uint8_t getNetworkInstance(void);
Tom Joseph3e61aa02016-08-08 08:42:39 -050098
Vernon Maueryecc8efa2021-06-12 12:52:23 -070099 /**
100 * @brief Clean Session Stale Entries
101 *
102 * Schedules cleaning the inactive sessions entries from the Session Map
103 */
104 void scheduleSessionCleaner(const std::chrono::microseconds& grace);
105
Vernon Mauery9e801a22018-10-12 13:20:49 -0700106 private:
Vernon Maueryecc8efa2021-06-12 12:52:23 -0700107 /**
108 * @brief reclaim system resources by limiting idle sessions
109 *
110 * Limits on active, authenticated sessions are calculated independently
111 * from in-setup sessions, which are not required to be authenticated. This
112 * will prevent would-be DoS attacks by calling a bunch of Open Session
113 * requests to fill up all available sessions. Too many active sessions will
114 * trigger a shorter timeout, but is unaffected by setup session counts.
115 *
116 * For active sessions, grace time is inversely proportional to (the number
117 * of active sessions beyond max sessions per channel)^3
118 *
119 * For sessions in setup, grace time is inversely proportional to (the
120 * number of total sessions beyond max sessions per channel)^3, with a max
121 * of 3 seconds
122 */
123 void cleanStaleEntries();
124
125 std::shared_ptr<boost::asio::io_context> io;
126 boost::asio::steady_timer timer;
127
128 std::array<uint32_t, session::maxSessionHandles> sessionHandleMap = {0};
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530129
Vernon Mauery9e801a22018-10-12 13:20:49 -0700130 /**
131 * @brief Session Manager keeps the session objects as a sorted
132 * associative container with Session ID as the unique key
133 */
134 SessionMap sessionsMap;
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530135 std::unique_ptr<sdbusplus::server::manager::manager> objManager = nullptr;
136 std::string chName{}; // Channel Name
137 uint8_t ipmiNetworkInstance;
Suryakanth Sekarf8a34fc2019-06-12 20:59:18 +0530138 void setNetworkInstance(void);
Tom Joseph3e61aa02016-08-08 08:42:39 -0500139};
140
141} // namespace session