blob: 3de5fa812da35489d32dd8edcd3b8657124cf8d3 [file] [log] [blame]
AppaRao Puli071f3f22018-05-24 16:45:30 +05301/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
18#include "channel_layer.hpp"
19
20#include <boost/interprocess/sync/file_lock.hpp>
21#include <boost/interprocess/sync/named_recursive_mutex.hpp>
22#include <cstdint>
23#include <ctime>
24#include <nlohmann/json.hpp>
25#include <sdbusplus/bus.hpp>
26
27namespace ipmi
28{
29
30using Json = nlohmann::json;
31
AppaRao Puli9613ed72018-09-01 23:46:44 +053032using DbusVariant =
33 sdbusplus::message::variant<std::vector<std::string>, std::string, bool>;
34
35using DbusChObjProperties = std::vector<std::pair<std::string, DbusVariant>>;
36
AppaRao Puli071f3f22018-05-24 16:45:30 +053037static constexpr const char* ipmiChannelMutex = "ipmi_channel_mutex";
38static constexpr const char* ipmiChMutexCleanupLockFile =
39 "/var/lib/ipmi/ipmi_channel_mutex_cleanup";
40
Richard Marian Thomaiyar6e1ba9e2018-11-29 06:29:21 +053041/** @struct ChannelAccessData
42 *
43 * Structure to store both non-volatile and volatile channel access information
44 * as used by IPMI specification (refer spec sec 22.22 to 22.24)
45 */
AppaRao Puli071f3f22018-05-24 16:45:30 +053046struct ChannelAccessData
47{
48 ChannelAccess chNonVolatileData;
49 ChannelAccess chVolatileData;
50};
51
Richard Marian Thomaiyar6e1ba9e2018-11-29 06:29:21 +053052/** @struct ChannelData
53 *
54 * Structure for channel information - base structure to get all information
55 * about the channel.(refer spec sec 22.22 to 22.24)
56 */
AppaRao Puli071f3f22018-05-24 16:45:30 +053057struct ChannelData
58{
59 std::string chName;
60 uint8_t chID;
61 bool isChValid;
62 uint8_t activeSessCount;
63 ChannelInfo chInfo;
64 ChannelAccessData chAccess;
Vernon Mauery58317122018-11-28 11:02:43 -080065 size_t maxTransferSize;
AppaRao Puli071f3f22018-05-24 16:45:30 +053066};
67
68class ChannelConfig;
69
70ChannelConfig& getChannelConfigObject();
71
72class ChannelConfig
73{
74 public:
75 ChannelConfig(const ChannelConfig&) = delete;
76 ChannelConfig& operator=(const ChannelConfig&) = delete;
77 ChannelConfig(ChannelConfig&&) = delete;
78 ChannelConfig& operator=(ChannelConfig&&) = delete;
79
AppaRao Puli9613ed72018-09-01 23:46:44 +053080 ~ChannelConfig();
AppaRao Puli071f3f22018-05-24 16:45:30 +053081 ChannelConfig();
82
83 /** @brief determines valid channel
84 *
85 * @param[in] chNum - channel number
86 *
87 * @return true if valid, false otherwise
88 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +053089 bool isValidChannel(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +053090
91 /** @brief determines valid authentication type
92 *
93 * @param[in] chNum - channel number
94 * @param[in] authType - authentication type
95 *
96 * @return true if valid, false otherwise
97 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +053098 bool isValidAuthType(const uint8_t chNum, const EAuthType& authType);
AppaRao Puli071f3f22018-05-24 16:45:30 +053099
100 /** @brief determines supported session type of a channel
101 *
102 * @param[in] chNum - channel number
103 *
104 * @return EChannelSessSupported - supported session type
105 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530106 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530107
108 /** @brief determines number of active sessions on a channel
109 *
110 * @param[in] chNum - channel number
111 *
112 * @return numer of active sessions
113 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530114 int getChannelActiveSessions(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530115
Vernon Mauery58317122018-11-28 11:02:43 -0800116 /** @brief determines maximum transfer size for a channel
117 *
118 * @param[in] chNum - channel number
119 *
120 * @return maximum bytes that can be transferred on this channel
121 */
122 size_t getChannelMaxTransferSize(uint8_t chNum);
123
AppaRao Puli071f3f22018-05-24 16:45:30 +0530124 /** @brief provides channel info details
125 *
126 * @param[in] chNum - channel number
127 * @param[out] chInfo - channel info details
128 *
129 * @return IPMI_CC_OK for success, others for failure.
130 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530131 ipmi_ret_t getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530132
133 /** @brief provides channel access data
134 *
135 * @param[in] chNum - channel number
136 * @param[out] chAccessData - channel access data
137 *
138 * @return IPMI_CC_OK for success, others for failure.
139 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530140 ipmi_ret_t getChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530141 ChannelAccess& chAccessData);
142
143 /** @brief to set channel access data
144 *
145 * @param[in] chNum - channel number
146 * @param[in] chAccessData - channel access data
147 * @param[in] setFlag - flag to indicate updatable fields
148 *
149 * @return IPMI_CC_OK for success, others for failure.
150 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530151 ipmi_ret_t setChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530152 const ChannelAccess& chAccessData,
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530153 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530154
155 /** @brief to get channel access data persistent data
156 *
157 * @param[in] chNum - channel number
158 * @param[out] chAccessData - channel access data
159 *
160 * @return IPMI_CC_OK for success, others for failure.
161 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530162 ipmi_ret_t getChannelAccessPersistData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530163 ChannelAccess& chAccessData);
164
165 /** @brief to set channel access data persistent data
166 *
167 * @param[in] chNum - channel number
168 * @param[in] chAccessData - channel access data
169 * @param[in] setFlag - flag to indicate updatable fields
170 *
171 * @return IPMI_CC_OK for success, others for failure.
172 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530173 ipmi_ret_t setChannelAccessPersistData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530174 const ChannelAccess& chAccessData,
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530175 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530176
177 /** @brief provides supported authentication type for the channel
178 *
179 * @param[in] chNum - channel number
180 * @param[out] authTypeSupported - supported authentication type
181 *
182 * @return IPMI_CC_OK for success, others for failure.
183 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530184 ipmi_ret_t getChannelAuthTypeSupported(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530185 uint8_t& authTypeSupported);
186
187 /** @brief provides enabled authentication type for the channel
188 *
189 * @param[in] chNum - channel number
190 * @param[in] priv - privilege
191 * @param[out] authType - enabled authentication type
192 *
193 * @return IPMI_CC_OK for success, others for failure.
194 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530195 ipmi_ret_t getChannelEnabledAuthType(const uint8_t chNum,
196 const uint8_t priv,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530197 EAuthType& authType);
198
AppaRao Puli9613ed72018-09-01 23:46:44 +0530199 /** @brief conver to channel privilege from system privilege
200 *
201 * @param[in] value - privilege value
202 *
203 * @return Channel privilege
204 */
205 CommandPrivilege convertToPrivLimitIndex(const std::string& value);
206
Richard Marian Thomaiyara39208e2018-12-08 17:27:11 +0530207 /** @brief function to convert channel number to channel index
208 *
209 * @param[in] chNum - channel number
210 *
211 * @return channel index
212 */
213 uint8_t convertToChannelIndexNumber(const uint8_t chNum);
214
AppaRao Puli9613ed72018-09-01 23:46:44 +0530215 /** @brief function to write persistent channel configuration to config file
216 *
217 * @return 0 for success, -errno for failure.
218 */
219 int writeChannelPersistData();
220
221 /** @brief function to write volatile channel configuration to config file
222 *
223 * @return 0 for success, -errno for failure.
224 */
225 int writeChannelVolatileData();
226
227 /** @brief function to get channel data based on channel number
228 *
229 * @param[in] chNum - channel number
230 *
231 * @return 0 for success, -errno for failure.
232 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530233 ChannelData* getChannelDataPtr(const uint8_t chNum);
AppaRao Puli9613ed72018-09-01 23:46:44 +0530234
235 uint32_t signalFlag = 0;
236
AppaRao Puli071f3f22018-05-24 16:45:30 +0530237 std::unique_ptr<boost::interprocess::named_recursive_mutex> channelMutex{
238 nullptr};
239
240 private:
241 ChannelData channelData[maxIpmiChannels];
242 std::time_t nvFileLastUpdatedTime;
243 std::time_t voltFileLastUpdatedTime;
244 std::time_t getUpdatedFileTime(const std::string& fileName);
245 boost::interprocess::file_lock mutexCleanupLock;
246 sdbusplus::bus::bus bus;
AppaRao Puli9613ed72018-09-01 23:46:44 +0530247 bool signalHndlrObjectState = false;
248 boost::interprocess::file_lock sigHndlrLock;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530249
250 /** @brief function to initialize persistent channel configuration
251 *
252 */
253 void initChannelPersistData();
254
255 /** @brief function to set default channel configuration based on channel
256 * number
257 *
258 * @param[in] chNum - channel number
259 * @param[in] chName - channel name
260 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530261 void setDefaultChannelConfig(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530262 const std::string& chName);
263
264 /** @brief function to load all channel configuration
265 *
266 * @return 0 for success, -errno for failure.
267 */
268 int loadChannelConfig();
269
270 /** @brief function to read persistent channel data
271 *
272 * @return 0 for success, -errno for failure.
273 */
274 int readChannelPersistData();
275
AppaRao Puli071f3f22018-05-24 16:45:30 +0530276 /** @brief function to read volatile channel data
277 *
278 * @return 0 for success, -errno for failure.
279 */
280 int readChannelVolatileData();
281
AppaRao Puli071f3f22018-05-24 16:45:30 +0530282 /** @brief function to check and reload persistent channel data
283 *
284 * @return 0 for success, -errno for failure.
285 */
286 int checkAndReloadNVData();
287
288 /** @brief function to check and reload volatile channel data
289 *
290 * @return 0 for success, -errno for failure.
291 */
292 int checkAndReloadVolatileData();
293
AppaRao Puli9613ed72018-09-01 23:46:44 +0530294 /** @brief function to sync channel privilege with system network channel
295 * privilege
296 *
297 * @return 0 for success, -errno for failure.
298 */
299 int syncNetworkChannelConfig();
300
301 /** @brief function to set D-Bus property value
302 *
303 * @param[in] bus - bus
304 * @param[in] service - service name
305 * @param[in] objPath - object path
306 * @param[in] interface - interface
307 * @param[in] property - property name
308 * @param[in] value - property value
309 *
310 * @return 0 for success, -errno for failure.
311 */
312 int setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
313 const std::string& objPath,
314 const std::string& interface,
315 const std::string& property, const DbusVariant& value);
316
317 /** @brief function to get D-Bus property value
318 *
319 * @param[in] bus - bus
320 * @param[in] service - service name
321 * @param[in] objPath - object path
322 * @param[in] interface - interface
323 * @param[in] property - property name
324 * @param[out] value - property value
325 *
326 * @return 0 for success, -errno for failure.
327 */
328 int getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
329 const std::string& objPath,
330 const std::string& interface,
331 const std::string& property, DbusVariant& value);
332
AppaRao Puli071f3f22018-05-24 16:45:30 +0530333 /** @brief function to read json config file
334 *
335 * @param[in] configFile - configuration file name
336 *
337 * @return Json object
338 */
339 Json readJsonFile(const std::string& configFile);
340
341 /** @brief function to write json config file
342 *
343 * @param[in] configFile - configuration file name
344 * @param[in] jsonData - json object
345 *
346 * @return 0 for success, -errno for failure.
347 */
348 int writeJsonFile(const std::string& configFile, const Json& jsonData);
349
350 /** @brief function to convert system access mode to Channel access mode
351 * type
352 *
353 * @param[in] mode - access mode in string
354 *
355 * @return Channel access mode.
356 */
357 EChannelAccessMode convertToAccessModeIndex(const std::string& mode);
358
359 /** @brief function to convert access mode value to string
360 *
361 * @param[in] value - acess mode value
362 *
363 * @return access mode in string
364 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530365 std::string convertToAccessModeString(const uint8_t value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530366
AppaRao Puli071f3f22018-05-24 16:45:30 +0530367 /** @brief function to convert privilege value to string
368 *
369 * @param[in] value - privilege value
370 *
371 * @return privilege in string
372 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530373 std::string convertToPrivLimitString(const uint8_t value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530374
375 /** @brief function to convert session support string to value type
376 *
377 * @param[in] value - session support type in string
378 *
379 * @return support session type
380 */
381 EChannelSessSupported
382 convertToSessionSupportIndex(const std::string& value);
383
384 /** @brief function to convert medium type string to value type
385 *
386 * @param[in] value - medium type in string
387 *
388 * @return channel medium type
389 */
390 EChannelMediumType convertToMediumTypeIndex(const std::string& value);
391
392 /** @brief function to convert protocol type string to value type
393 *
394 * @param[in] value - protocol type in string
395 *
396 * @return channel protocol type
397 */
398 EChannelProtocolType convertToProtocolTypeIndex(const std::string& value);
399
AppaRao Puli071f3f22018-05-24 16:45:30 +0530400 /** @brief function to convert channel name to network interface name
401 *
402 * @param[in] value - channel interface name - ipmi centric
403 *
404 * @return network channel interface name
405 */
406 std::string convertToNetInterface(const std::string& value);
407};
408
409} // namespace ipmi