blob: 0c31338fd887c8e904dc7ff415848ec3b15589d4 [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"
NITIN SHARMAb541a5a2019-07-18 12:46:59 +000019#include "ipmid/api-types.hpp"
AppaRao Puli071f3f22018-05-24 16:45:30 +053020
21#include <boost/interprocess/sync/file_lock.hpp>
22#include <boost/interprocess/sync/named_recursive_mutex.hpp>
23#include <cstdint>
24#include <ctime>
25#include <nlohmann/json.hpp>
26#include <sdbusplus/bus.hpp>
Vernon Mauery16b86932019-05-01 08:36:11 -070027#include <variant>
AppaRao Puli071f3f22018-05-24 16:45:30 +053028
29namespace ipmi
30{
31
32using Json = nlohmann::json;
33
Vernon Mauery16b86932019-05-01 08:36:11 -070034using DbusVariant = std::variant<std::vector<std::string>, std::string, bool>;
AppaRao Puli9613ed72018-09-01 23:46:44 +053035
36using DbusChObjProperties = std::vector<std::pair<std::string, DbusVariant>>;
37
AppaRao Puli071f3f22018-05-24 16:45:30 +053038static constexpr const char* ipmiChannelMutex = "ipmi_channel_mutex";
39static constexpr const char* ipmiChMutexCleanupLockFile =
40 "/var/lib/ipmi/ipmi_channel_mutex_cleanup";
41
Richard Marian Thomaiyar6e1ba9e2018-11-29 06:29:21 +053042/** @struct ChannelAccessData
43 *
44 * Structure to store both non-volatile and volatile channel access information
45 * as used by IPMI specification (refer spec sec 22.22 to 22.24)
46 */
AppaRao Puli071f3f22018-05-24 16:45:30 +053047struct ChannelAccessData
48{
49 ChannelAccess chNonVolatileData;
50 ChannelAccess chVolatileData;
51};
52
Johnathan Mantey4c0435a2018-12-11 13:17:55 -080053/** @struct ChannelProperties
Richard Marian Thomaiyar6e1ba9e2018-11-29 06:29:21 +053054 *
55 * Structure for channel information - base structure to get all information
56 * about the channel.(refer spec sec 22.22 to 22.24)
57 */
Johnathan Mantey4c0435a2018-12-11 13:17:55 -080058struct ChannelProperties
AppaRao Puli071f3f22018-05-24 16:45:30 +053059{
60 std::string chName;
61 uint8_t chID;
62 bool isChValid;
63 uint8_t activeSessCount;
64 ChannelInfo chInfo;
65 ChannelAccessData chAccess;
Vernon Mauery58317122018-11-28 11:02:43 -080066 size_t maxTransferSize;
AppaRao Puli071f3f22018-05-24 16:45:30 +053067};
68
69class ChannelConfig;
70
71ChannelConfig& getChannelConfigObject();
72
73class ChannelConfig
74{
75 public:
76 ChannelConfig(const ChannelConfig&) = delete;
77 ChannelConfig& operator=(const ChannelConfig&) = delete;
78 ChannelConfig(ChannelConfig&&) = delete;
79 ChannelConfig& operator=(ChannelConfig&&) = delete;
80
AppaRao Puli9613ed72018-09-01 23:46:44 +053081 ~ChannelConfig();
AppaRao Puli071f3f22018-05-24 16:45:30 +053082 ChannelConfig();
83
84 /** @brief determines valid channel
85 *
86 * @param[in] chNum - channel number
87 *
88 * @return true if valid, false otherwise
89 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +053090 bool isValidChannel(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +053091
92 /** @brief determines valid authentication type
93 *
94 * @param[in] chNum - channel number
95 * @param[in] authType - authentication type
96 *
97 * @return true if valid, false otherwise
98 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +053099 bool isValidAuthType(const uint8_t chNum, const EAuthType& authType);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530100
Richard Marian Thomaiyar73906b92019-01-04 23:48:02 +0530101 /** @brief function to get channel name from channel number
102 *
103 * @param[in] chNum - channel number index
104 *
105 * @return network channel interface name
106 */
107 std::string getChannelName(const uint8_t chNum);
108
Vernon Mauery735ee952019-02-15 13:38:52 -0800109 /** @brief function to get channel number from channel name
110 *
111 * @param[in] chName - channel name
112 *
113 * @return network channel interface number
114 */
115
116 uint8_t getChannelByName(const std::string& chName)
117 {
118 return convertToChannelNumberFromChannelName(chName);
119 }
120
AppaRao Puli071f3f22018-05-24 16:45:30 +0530121 /** @brief determines supported session type of a channel
122 *
123 * @param[in] chNum - channel number
124 *
125 * @return EChannelSessSupported - supported session type
126 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530127 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530128
129 /** @brief determines number of active sessions on a channel
130 *
131 * @param[in] chNum - channel number
132 *
133 * @return numer of active sessions
134 */
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530135 int getChannelActiveSessions(const uint8_t chNum);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530136
Vernon Mauery58317122018-11-28 11:02:43 -0800137 /** @brief determines maximum transfer size for a channel
138 *
139 * @param[in] chNum - channel number
140 *
141 * @return maximum bytes that can be transferred on this channel
142 */
143 size_t getChannelMaxTransferSize(uint8_t chNum);
144
AppaRao Puli071f3f22018-05-24 16:45:30 +0530145 /** @brief provides channel info details
146 *
147 * @param[in] chNum - channel number
148 * @param[out] chInfo - channel info details
149 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000150 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530151 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000152 Cc getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530153
154 /** @brief provides channel access data
155 *
156 * @param[in] chNum - channel number
157 * @param[out] chAccessData - channel access data
158 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000159 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530160 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000161 Cc getChannelAccessData(const uint8_t chNum, ChannelAccess& chAccessData);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530162
163 /** @brief to set channel access data
164 *
165 * @param[in] chNum - channel number
166 * @param[in] chAccessData - channel access data
167 * @param[in] setFlag - flag to indicate updatable fields
168 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000169 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530170 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000171 Cc setChannelAccessData(const uint8_t chNum,
172 const ChannelAccess& chAccessData,
173 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530174
175 /** @brief to get channel access data persistent data
176 *
177 * @param[in] chNum - channel number
178 * @param[out] chAccessData - channel access data
179 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000180 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530181 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000182 Cc getChannelAccessPersistData(const uint8_t chNum,
183 ChannelAccess& chAccessData);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530184
185 /** @brief to set channel access data persistent data
186 *
187 * @param[in] chNum - channel number
188 * @param[in] chAccessData - channel access data
189 * @param[in] setFlag - flag to indicate updatable fields
190 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000191 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530192 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000193 Cc setChannelAccessPersistData(const uint8_t chNum,
194 const ChannelAccess& chAccessData,
195 const uint8_t setFlag);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530196
197 /** @brief provides supported authentication type for the channel
198 *
199 * @param[in] chNum - channel number
200 * @param[out] authTypeSupported - supported authentication type
201 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000202 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530203 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000204 Cc getChannelAuthTypeSupported(const uint8_t chNum,
205 uint8_t& authTypeSupported);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530206
207 /** @brief provides enabled authentication type for the channel
208 *
209 * @param[in] chNum - channel number
210 * @param[in] priv - privilege
211 * @param[out] authType - enabled authentication type
212 *
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000213 * @return ccSuccess for success, others for failure.
AppaRao Puli071f3f22018-05-24 16:45:30 +0530214 */
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000215 Cc getChannelEnabledAuthType(const uint8_t chNum, const uint8_t priv,
216 EAuthType& authType);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530217
AppaRao Puli9613ed72018-09-01 23:46:44 +0530218 /** @brief conver to channel privilege from system privilege
219 *
220 * @param[in] value - privilege value
221 *
222 * @return Channel privilege
223 */
224 CommandPrivilege convertToPrivLimitIndex(const std::string& value);
225
226 /** @brief function to write persistent channel configuration to config file
227 *
228 * @return 0 for success, -errno for failure.
229 */
230 int writeChannelPersistData();
231
232 /** @brief function to write volatile channel configuration to config file
233 *
234 * @return 0 for success, -errno for failure.
235 */
236 int writeChannelVolatileData();
237
Johnathan Mantey2dfe7f82018-12-11 13:53:33 -0800238 private:
AppaRao Puli9613ed72018-09-01 23:46:44 +0530239 uint32_t signalFlag = 0;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530240 std::unique_ptr<boost::interprocess::named_recursive_mutex> channelMutex{
241 nullptr};
Johnathan Mantey4c0435a2018-12-11 13:17:55 -0800242 std::array<ChannelProperties, maxIpmiChannels> channelData;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530243 std::time_t nvFileLastUpdatedTime;
244 std::time_t voltFileLastUpdatedTime;
AppaRao Puli071f3f22018-05-24 16:45:30 +0530245 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
Johnathan Mantey74a21022018-12-13 13:17:56 -0800396 /** @brief function to convert channel name to the IPMI channel number.
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800397 *
Johnathan Mantey74a21022018-12-13 13:17:56 -0800398 * @param[in] chName - the channel name defined in the JSON input file
399 * (i.e. LAN1)
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800400 *
Johnathan Mantey74a21022018-12-13 13:17:56 -0800401 * @return IPMI channel number
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800402 */
Johnathan Mantey74a21022018-12-13 13:17:56 -0800403 int convertToChannelNumberFromChannelName(const std::string& chName);
Johnathan Manteye5c4f1d2018-12-10 16:24:26 -0800404
405 /** @brief function to handle Channel access property update through the
406 * D-Bus handler.
407 *
408 * @param[in] path - D-Bus path to the network element (i.e. eth0)
409 * @param[in] chProperties - D-Bus channel properties
410 */
411 void processChAccessPropChange(const std::string& path,
412 const DbusChObjProperties& chProperties);
Johnathan Mantey2dfe7f82018-12-11 13:53:33 -0800413
414 /** @brief function to retrieve last modification time for the named file
415 *
416 * @param[in] fileName - the name of the file for which to acquire
417 * timestamp data
418 *
419 * @return time the file was last modified
420 */
421 std::time_t getUpdatedFileTime(const std::string& fileName);
Johnathan Mantey74a21022018-12-13 13:17:56 -0800422
423 /** @brief function to convert the DBus path to a network channel name
424 *
425 * @param[in] path - The DBus path to the device
426 *
427 * @return network channel name (i.e. eth0)
428 */
429 std::string getChannelNameFromPath(const std::string& path);
AppaRao Puli071f3f22018-05-24 16:45:30 +0530430};
431
432} // namespace ipmi