blob: 44aaed619ec82cfdf34879213b8e88895ef3347e [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
Johnathan Mantey4c0435a2018-12-11 13:17:55 -080052/** @struct ChannelProperties
Richard Marian Thomaiyar6e1ba9e2018-11-29 06:29:21 +053053 *
54 * Structure for channel information - base structure to get all information
55 * about the channel.(refer spec sec 22.22 to 22.24)
56 */
Johnathan Mantey4c0435a2018-12-11 13:17:55 -080057struct ChannelProperties
AppaRao Puli071f3f22018-05-24 16:45:30 +053058{
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
Richard Marian Thomaiyar73906b92019-01-04 23:48:02 +0530100 /** @brief function to get channel name from channel number
101 *
102 * @param[in] chNum - channel number index
103 *
104 * @return network channel interface name
105 */
106 std::string getChannelName(const uint8_t chNum);
107
AppaRao Puli071f3f22018-05-24 16:45:30 +0530108 /** @brief determines supported session type of a channel
109 *
110 * @param[in] chNum - channel number
111 *
112 * @return EChannelSessSupported - supported session type
113 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530114 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530115
116 /** @brief determines number of active sessions on a channel
117 *
118 * @param[in] chNum - channel number
119 *
120 * @return numer of active sessions
121 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530122 int getChannelActiveSessions(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530123
Vernon Mauery58317122018-11-28 11:02:43 -0800124 /** @brief determines maximum transfer size for a channel
125 *
126 * @param[in] chNum - channel number
127 *
128 * @return maximum bytes that can be transferred on this channel
129 */
130 size_t getChannelMaxTransferSize(uint8_t chNum);
131
AppaRao Puli071f3f22018-05-24 16:45:30 +0530132 /** @brief provides channel info details
133 *
134 * @param[in] chNum - channel number
135 * @param[out] chInfo - channel info details
136 *
137 * @return IPMI_CC_OK for success, others for failure.
138 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530139 ipmi_ret_t getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530140
141 /** @brief provides channel access data
142 *
143 * @param[in] chNum - channel number
144 * @param[out] chAccessData - channel access data
145 *
146 * @return IPMI_CC_OK for success, others for failure.
147 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530148 ipmi_ret_t getChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530149 ChannelAccess& chAccessData);
150
151 /** @brief to set channel access data
152 *
153 * @param[in] chNum - channel number
154 * @param[in] chAccessData - channel access data
155 * @param[in] setFlag - flag to indicate updatable fields
156 *
157 * @return IPMI_CC_OK for success, others for failure.
158 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530159 ipmi_ret_t setChannelAccessData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530160 const ChannelAccess& chAccessData,
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530161 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530162
163 /** @brief to get channel access data persistent data
164 *
165 * @param[in] chNum - channel number
166 * @param[out] chAccessData - channel access data
167 *
168 * @return IPMI_CC_OK for success, others for failure.
169 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530170 ipmi_ret_t getChannelAccessPersistData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530171 ChannelAccess& chAccessData);
172
173 /** @brief to set channel access data persistent data
174 *
175 * @param[in] chNum - channel number
176 * @param[in] chAccessData - channel access data
177 * @param[in] setFlag - flag to indicate updatable fields
178 *
179 * @return IPMI_CC_OK for success, others for failure.
180 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530181 ipmi_ret_t setChannelAccessPersistData(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530182 const ChannelAccess& chAccessData,
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530183 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530184
185 /** @brief provides supported authentication type for the channel
186 *
187 * @param[in] chNum - channel number
188 * @param[out] authTypeSupported - supported authentication type
189 *
190 * @return IPMI_CC_OK for success, others for failure.
191 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530192 ipmi_ret_t getChannelAuthTypeSupported(const uint8_t chNum,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530193 uint8_t& authTypeSupported);
194
195 /** @brief provides enabled authentication type for the channel
196 *
197 * @param[in] chNum - channel number
198 * @param[in] priv - privilege
199 * @param[out] authType - enabled authentication type
200 *
201 * @return IPMI_CC_OK for success, others for failure.
202 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530203 ipmi_ret_t getChannelEnabledAuthType(const uint8_t chNum,
204 const uint8_t priv,
AppaRao Puli071f3f22018-05-24 16:45:30 +0530205 EAuthType& authType);
206
AppaRao Puli9613ed72018-09-01 23:46:44 +0530207 /** @brief conver to channel privilege from system privilege
208 *
209 * @param[in] value - privilege value
210 *
211 * @return Channel privilege
212 */
213 CommandPrivilege convertToPrivLimitIndex(const std::string& value);
214
Richard Marian Thomaiyara39208e2018-12-08 17:27:11 +0530215 /** @brief function to convert channel number to channel index
216 *
217 * @param[in] chNum - channel number
218 *
219 * @return channel index
220 */
221 uint8_t convertToChannelIndexNumber(const uint8_t chNum);
222
AppaRao Puli9613ed72018-09-01 23:46:44 +0530223 /** @brief function to write persistent channel configuration to config file
224 *
225 * @return 0 for success, -errno for failure.
226 */
227 int writeChannelPersistData();
228
229 /** @brief function to write volatile channel configuration to config file
230 *
231 * @return 0 for success, -errno for failure.
232 */
233 int writeChannelVolatileData();
234
AppaRao Puli9613ed72018-09-01 23:46:44 +0530235 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:
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800241 std::array<ChannelProperties, maxIpmiChannels> channelData;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530242 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 *
AppaRao Puli9613ed72018-09-01 23:46:44 +0530303 * @param[in] service - service name
304 * @param[in] objPath - object path
305 * @param[in] interface - interface
306 * @param[in] property - property name
307 * @param[in] value - property value
308 *
309 * @return 0 for success, -errno for failure.
310 */
Johnathan Manteyf92261d2018-12-10 15:49:34 -0800311 int setDbusProperty(const std::string& service, const std::string& objPath,
AppaRao Puli9613ed72018-09-01 23:46:44 +0530312 const std::string& interface,
313 const std::string& property, const DbusVariant& value);
314
315 /** @brief function to get D-Bus property value
316 *
AppaRao Puli9613ed72018-09-01 23:46:44 +0530317 * @param[in] service - service name
318 * @param[in] objPath - object path
319 * @param[in] interface - interface
320 * @param[in] property - property name
321 * @param[out] value - property value
322 *
323 * @return 0 for success, -errno for failure.
324 */
Johnathan Manteyf92261d2018-12-10 15:49:34 -0800325 int getDbusProperty(const std::string& service, const std::string& objPath,
AppaRao Puli9613ed72018-09-01 23:46:44 +0530326 const std::string& interface,
327 const std::string& property, DbusVariant& value);
328
AppaRao Puli071f3f22018-05-24 16:45:30 +0530329 /** @brief function to read json config file
330 *
331 * @param[in] configFile - configuration file name
332 *
333 * @return Json object
334 */
335 Json readJsonFile(const std::string& configFile);
336
337 /** @brief function to write json config file
338 *
339 * @param[in] configFile - configuration file name
340 * @param[in] jsonData - json object
341 *
342 * @return 0 for success, -errno for failure.
343 */
344 int writeJsonFile(const std::string& configFile, const Json& jsonData);
345
346 /** @brief function to convert system access mode to Channel access mode
347 * type
348 *
349 * @param[in] mode - access mode in string
350 *
351 * @return Channel access mode.
352 */
353 EChannelAccessMode convertToAccessModeIndex(const std::string& mode);
354
355 /** @brief function to convert access mode value to string
356 *
357 * @param[in] value - acess mode value
358 *
359 * @return access mode in string
360 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530361 std::string convertToAccessModeString(const uint8_t value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530362
AppaRao Puli071f3f22018-05-24 16:45:30 +0530363 /** @brief function to convert privilege value to string
364 *
365 * @param[in] value - privilege value
366 *
367 * @return privilege in string
368 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530369 std::string convertToPrivLimitString(const uint8_t value);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530370
371 /** @brief function to convert session support string to value type
372 *
373 * @param[in] value - session support type in string
374 *
375 * @return support session type
376 */
377 EChannelSessSupported
378 convertToSessionSupportIndex(const std::string& value);
379
380 /** @brief function to convert medium type string to value type
381 *
382 * @param[in] value - medium type in string
383 *
384 * @return channel medium type
385 */
386 EChannelMediumType convertToMediumTypeIndex(const std::string& value);
387
388 /** @brief function to convert protocol type string to value type
389 *
390 * @param[in] value - protocol type in string
391 *
392 * @return channel protocol type
393 */
394 EChannelProtocolType convertToProtocolTypeIndex(const std::string& value);
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800395
396 /** @brief function to convert channel name to network interface name
397 *
398 * @param[in] value - channel interface name - ipmi centric
399 *
400 * @return network channel interface name
401 */
402 std::string convertToNetInterface(const std::string& value);
403
404 /** @brief function to handle Channel access property update through the
405 * D-Bus handler.
406 *
407 * @param[in] path - D-Bus path to the network element (i.e. eth0)
408 * @param[in] chProperties - D-Bus channel properties
409 */
410 void processChAccessPropChange(const std::string& path,
411 const DbusChObjProperties& chProperties);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530412};
413
414} // namespace ipmi