blob: fd27ef91cb1ecbaf62f318c4dae7703743225c9e [file] [log] [blame]
manojkiraneda0b631ae2019-12-03 17:54:28 +05301#pragma once
2
Nan Zhou77665bd2022-10-12 20:28:58 +00003#include "ibm/utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08004#include "logging.hpp"
Nan Zhou77665bd2022-10-12 20:28:58 +00005
Ed Tanous11ba3972022-07-11 09:50:41 -07006#include <boost/algorithm/string/predicate.hpp>
manojkiraneda0b631ae2019-12-03 17:54:28 +05307#include <boost/container/flat_map.hpp>
Manojkiran Eda55fd1a92020-04-30 19:06:48 +05308#include <boost/endian/conversion.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05009#include <nlohmann/json.hpp>
10
manojkiraneda0b631ae2019-12-03 17:54:28 +053011#include <filesystem>
Sunitha Harish8a3bb712019-12-13 03:48:09 -060012#include <fstream>
Ed Tanous11ba3972022-07-11 09:50:41 -070013#include <variant>
manojkiraneda0b631ae2019-12-03 17:54:28 +053014
15namespace crow
16{
17namespace ibm_mc_lock
18{
19
manojkiraneda0b631ae2019-12-03 17:54:28 +053020using SType = std::string;
21
22/*----------------------------------------
23|Segment flags : LockFlag | SegmentLength|
24------------------------------------------*/
25
26using SegmentFlags = std::vector<std::pair<SType, uint32_t>>;
27
28// Lockrequest = session-id | hmc-id | locktype | resourceid | segmentinfo
29using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053030using LockRequests = std::vector<LockRequest>;
31using Rc =
32 std::pair<bool, std::variant<uint32_t, std::pair<uint32_t, LockRequest>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053033using RcRelaseLock = std::pair<bool, std::pair<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053034using RcGetLockList =
35 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053036using ListOfTransactionIds = std::vector<uint32_t>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053037using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053038using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>;
39using SessionFlags = std::pair<SType, SType>;
manojkiraneda402b5712019-12-13 17:07:09 +053040using ListOfSessionIds = std::vector<std::string>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053041
42class Lock
43{
Ed Tanous543f4402022-01-06 13:12:53 -080044 uint32_t transactionId = 0;
manojkiraneda0b631ae2019-12-03 17:54:28 +053045 boost::container::flat_map<uint32_t, LockRequests> lockTable;
46
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053047 protected:
48 /*
49 * This function implements the logic for validating an incoming
50 * lock request/requests.
51 *
52 * Returns : True (if Valid)
53 * Returns : False (if not a Valid lock request)
54 */
55
Ed Tanous104f09c2022-01-25 09:56:04 -080056 virtual bool isValidLockRequest(const LockRequest& refLockRecord);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053057
58 /*
59 * This function implements the logic of checking if the incoming
60 * multi-lock request is not having conflicting requirements.
61 *
62 * Returns : True (if conflicting)
63 * Returns : False (if not conflicting)
64 */
65
Ed Tanous104f09c2022-01-25 09:56:04 -080066 virtual bool isConflictRequest(const LockRequests& refLockRequestStructure);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053067 /*
68 * Implements the core algorithm to find the conflicting
69 * lock requests.
70 *
71 * This functions takes two lock requests and check if both
72 * are conflicting to each other.
73 *
74 * Returns : True (if conflicting)
75 * Returns : False (if not conflicting)
76 */
Ed Tanous104f09c2022-01-25 09:56:04 -080077 virtual bool isConflictRecord(const LockRequest& refLockRecord1,
78 const LockRequest& refLockRecord2);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053079
80 /*
81 * This function implements the logic of checking the conflicting
82 * locks from a incoming single/multi lock requests with the already
83 * existing lock request in the lock table.
84 *
85 */
86
Ed Tanous104f09c2022-01-25 09:56:04 -080087 virtual Rc isConflictWithTable(const LockRequests& refLockRequestStructure);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053088 /*
89 * This function implements the logic of checking the ownership of the
90 * lock from the releaselock request.
91 *
92 * Returns : True (if the requesting HMC & Session owns the lock(s))
93 * Returns : False (if the request HMC or Session does not own the lock(s))
94 */
95
Ed Tanous104f09c2022-01-25 09:56:04 -080096 virtual RcRelaseLock isItMyLock(const ListOfTransactionIds& refRids,
97 const SessionFlags& ids);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053098
99 /*
100 * This function validates the the list of transactionID's and returns false
101 * if the transaction ID is not valid & not present in the lock table
102 */
103
Ed Tanous104f09c2022-01-25 09:56:04 -0800104 virtual bool validateRids(const ListOfTransactionIds& refRids);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530105
106 /*
107 * This function releases the locks that are already obtained by the
108 * requesting Management console.
109 */
110
Ed Tanous104f09c2022-01-25 09:56:04 -0800111 void releaseLock(const ListOfTransactionIds& refRids);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530112
113 Lock()
114 {
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530115 transactionId = lockTable.empty() ? 0 : prev(lockTable.end())->first;
116 }
117
Sunitha Harish8a3bb712019-12-13 03:48:09 -0600118 /*
manojkiraneda0b631ae2019-12-03 17:54:28 +0530119 * This function implements the algorithm for checking the respective
120 * bytes of the resource id based on the lock management algorithm.
121 */
122
Ed Tanous104f09c2022-01-25 09:56:04 -0800123 static bool checkByte(uint64_t resourceId1, uint64_t resourceId2,
124 uint32_t position);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530125
126 /*
127 * This functions implements a counter that generates a unique 32 bit
128 * number for every successful transaction. This number will be used by
129 * the Management Console for debug.
130 */
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530131 virtual uint32_t generateTransactionId();
Sunitha Harish8a3bb712019-12-13 03:48:09 -0600132
manojkiraneda0b631ae2019-12-03 17:54:28 +0530133 public:
134 /*
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800135 * Explicitly deleted copy and move constructors
136 */
137 Lock(const Lock&) = delete;
138 Lock(Lock&&) = delete;
139 Lock& operator=(const Lock&) = delete;
140 Lock& operator=(Lock&&) = delete;
141
142 /*
manojkiraneda0b631ae2019-12-03 17:54:28 +0530143 * This function implements the logic for acquiring a lock on a
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530144 * resource if the incoming request is legitimate without any
manojkiraneda0b631ae2019-12-03 17:54:28 +0530145 * conflicting requirements & without any conflicting requirement
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500146 * with the existing locks in the lock table.
manojkiraneda0b631ae2019-12-03 17:54:28 +0530147 *
148 */
149
Ed Tanous104f09c2022-01-25 09:56:04 -0800150 RcAcquireLock acquireLock(const LockRequests& lockRequestStructure);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530151
manojkiraneda3b6dea62019-12-13 17:05:36 +0530152 /*
153 * This function implements the logic for releasing the lock that are
154 * owned by a management console session.
155 *
156 * The locks can be released by two ways
157 * - Using list of transaction ID's
158 * - Using a Session ID
159 *
160 * Client can choose either of the ways by using `Type` JSON key.
161 *
162 */
Ed Tanous104f09c2022-01-25 09:56:04 -0800163 RcReleaseLockApi releaseLock(const ListOfTransactionIds& p,
164 const SessionFlags& ids);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530165
manojkiraneda402b5712019-12-13 17:07:09 +0530166 /*
167 * This function implements the logic for getting the list of locks obtained
168 * by a particular management console.
169 */
Ed Tanous104f09c2022-01-25 09:56:04 -0800170 RcGetLockList getLockList(const ListOfSessionIds& listSessionId);
manojkiraneda402b5712019-12-13 17:07:09 +0530171
Ratan Gupta07386c62019-12-14 14:06:09 +0530172 /*
173 * This function is releases all the locks obtained by a particular
174 * session.
175 */
176
Ed Tanous104f09c2022-01-25 09:56:04 -0800177 void releaseLock(const std::string& sessionId);
Ratan Gupta07386c62019-12-14 14:06:09 +0530178
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500179 static Lock& getInstance()
Ratan Gupta07386c62019-12-14 14:06:09 +0530180 {
181 static Lock lockObject;
182 return lockObject;
183 }
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530184
Ed Tanous4e087512020-09-28 18:41:25 -0700185 virtual ~Lock() = default;
Ratan Gupta07386c62019-12-14 14:06:09 +0530186};
manojkiraneda0b631ae2019-12-03 17:54:28 +0530187
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500188inline RcGetLockList Lock::getLockList(const ListOfSessionIds& listSessionId)
manojkiraneda402b5712019-12-13 17:07:09 +0530189{
190
191 std::vector<std::pair<uint32_t, LockRequests>> lockList;
192
193 if (!lockTable.empty())
194 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500195 for (const auto& i : listSessionId)
manojkiraneda402b5712019-12-13 17:07:09 +0530196 {
197 auto it = lockTable.begin();
198 while (it != lockTable.end())
199 {
200 // Check if session id of this entry matches with session id
201 // given
202 if (std::get<0>(it->second[0]) == i)
203 {
204 BMCWEB_LOG_DEBUG << "Session id is found in the locktable";
205
206 // Push the whole lock record into a vector for returning
207 // the json
Ed Tanous4e087512020-09-28 18:41:25 -0700208 lockList.emplace_back(it->first, it->second);
manojkiraneda402b5712019-12-13 17:07:09 +0530209 }
210 // Go to next entry in map
211 it++;
212 }
213 }
214 }
215 // we may have found at least one entry with the given session id
216 // return the json list of lock records pertaining to the given
217 // session id, or send an empty list if lock table is empty
Ed Tanous02379d32020-09-15 21:15:44 -0700218 return {lockList};
manojkiraneda402b5712019-12-13 17:07:09 +0530219}
220
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221inline RcReleaseLockApi Lock::releaseLock(const ListOfTransactionIds& p,
222 const SessionFlags& ids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530223{
manojkiraneda3b6dea62019-12-13 17:05:36 +0530224 bool status = validateRids(p);
225
226 if (!status)
227 {
228 // Validation of rids failed
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500229 BMCWEB_LOG_ERROR << "releaseLock: Contains invalid request id";
manojkiraneda3b6dea62019-12-13 17:05:36 +0530230 return std::make_pair(false, status);
231 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700232 // Validation passed, check if all the locks are owned by the
233 // requesting HMC
234 auto status2 = isItMyLock(p, ids);
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500235 if (!status2.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530236 {
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500237 return std::make_pair(false, status2);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530238 }
Manojkiran Edaa1ffbb82020-10-28 17:42:21 +0530239 return std::make_pair(true, status2);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530240}
241
Ed Tanousb5a76932020-09-29 16:16:58 -0700242inline RcAcquireLock Lock::acquireLock(const LockRequests& lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530243{
244
245 // validate the lock request
246
Ed Tanous9eb808c2022-01-25 10:19:23 -0800247 for (const auto& lockRecord : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530248 {
249 bool status = isValidLockRequest(lockRecord);
250 if (!status)
251 {
252 BMCWEB_LOG_DEBUG << "Not a Valid record";
253 BMCWEB_LOG_DEBUG << "Bad json in request";
254 return std::make_pair(true, std::make_pair(status, 0));
255 }
256 }
257 // check for conflict record
258
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500259 const LockRequests& multiRequest = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530260 bool status = isConflictRequest(multiRequest);
261
262 if (status)
263 {
264 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
265 return std::make_pair(true, std::make_pair(status, 1));
266 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700267 BMCWEB_LOG_DEBUG << "The request is not conflicting within itself";
manojkiraneda0b631ae2019-12-03 17:54:28 +0530268
Ed Tanous3174e4d2020-10-07 11:41:22 -0700269 // Need to check for conflict with the locktable entries.
manojkiraneda0b631ae2019-12-03 17:54:28 +0530270
Ed Tanous3174e4d2020-10-07 11:41:22 -0700271 auto conflict = isConflictWithTable(multiRequest);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530272
Ed Tanous3174e4d2020-10-07 11:41:22 -0700273 BMCWEB_LOG_DEBUG << "Done with checking conflict with the locktable";
274 return std::make_pair(false, conflict);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530275}
276
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500277inline void Lock::releaseLock(const std::string& sessionId)
Ratan Gupta07386c62019-12-14 14:06:09 +0530278{
Ratan Gupta07386c62019-12-14 14:06:09 +0530279 if (!lockTable.empty())
280 {
281 auto it = lockTable.begin();
282 while (it != lockTable.end())
283 {
Ed Tanous26f69762022-01-25 09:49:11 -0800284 if (!it->second.empty())
Ratan Gupta07386c62019-12-14 14:06:09 +0530285 {
286 // Check if session id of this entry matches with session id
287 // given
288 if (std::get<0>(it->second[0]) == sessionId)
289 {
290 BMCWEB_LOG_DEBUG << "Remove the lock from the locktable "
291 "having sessionID="
292 << sessionId;
293 BMCWEB_LOG_DEBUG << "TransactionID =" << it->first;
294 it = lockTable.erase(it);
Ratan Gupta07386c62019-12-14 14:06:09 +0530295 }
296 else
297 {
298 it++;
299 }
300 }
301 }
Ratan Gupta07386c62019-12-14 14:06:09 +0530302 }
303}
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500304inline RcRelaseLock Lock::isItMyLock(const ListOfTransactionIds& refRids,
305 const SessionFlags& ids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530306{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500307 for (const auto& id : refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530308 {
309 // Just need to compare the client id of the first lock records in the
310 // complete lock row(in the map), because the rest of the lock records
311 // would have the same client id
312
313 std::string expectedClientId = std::get<1>(lockTable[id][0]);
314 std::string expectedSessionId = std::get<0>(lockTable[id][0]);
315
316 if ((expectedClientId == ids.first) &&
317 (expectedSessionId == ids.second))
318 {
319 // It is owned by the currently request hmc
320 BMCWEB_LOG_DEBUG << "Lock is owned by the current hmc";
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500321 // remove the lock
322 if (lockTable.erase(id) != 0U)
323 {
324 BMCWEB_LOG_DEBUG << "Removing the locks with transaction ID : "
325 << id;
326 }
327 else
328 {
329 BMCWEB_LOG_ERROR << "Removing the locks from the lock table "
330 "failed, transaction ID: "
331 << id;
332 }
manojkiraneda3b6dea62019-12-13 17:05:36 +0530333 }
334 else
335 {
336 BMCWEB_LOG_DEBUG << "Lock is not owned by the current hmc";
337 return std::make_pair(false, std::make_pair(id, lockTable[id][0]));
338 }
339 }
340 return std::make_pair(true, std::make_pair(0, LockRequest()));
341}
342
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343inline bool Lock::validateRids(const ListOfTransactionIds& refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530344{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500345 for (const auto& id : refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530346 {
347 auto search = lockTable.find(id);
348
349 if (search != lockTable.end())
350 {
351 BMCWEB_LOG_DEBUG << "Valid Transaction id";
352 // continue for the next rid
353 }
354 else
355 {
Sunitha Harish3e7ab702022-08-08 02:08:39 -0500356 BMCWEB_LOG_ERROR << "validateRids: At least 1 inValid Request id: "
357 << id;
manojkiraneda3b6dea62019-12-13 17:05:36 +0530358 return false;
359 }
360 }
361 return true;
362}
363
Ed Tanousb5a76932020-09-29 16:16:58 -0700364inline bool Lock::isValidLockRequest(const LockRequest& refLockRecord)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530365{
366
367 // validate the locktype
368
369 if (!((boost::equals(std::get<2>(refLockRecord), "Read") ||
370 (boost::equals(std::get<2>(refLockRecord), "Write")))))
371 {
372 BMCWEB_LOG_DEBUG << "Validation of LockType Failed";
373 BMCWEB_LOG_DEBUG << "Locktype : " << std::get<2>(refLockRecord);
374 return false;
375 }
376
377 BMCWEB_LOG_DEBUG << static_cast<int>(std::get<4>(refLockRecord).size());
378
379 // validate the number of segments
380 // Allowed No of segments are between 2 and 6
381 if ((static_cast<int>(std::get<4>(refLockRecord).size()) > 6) ||
382 (static_cast<int>(std::get<4>(refLockRecord).size()) < 2))
383 {
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500384 BMCWEB_LOG_DEBUG << "Validation of Number of Segments Failed";
manojkiraneda0b631ae2019-12-03 17:54:28 +0530385 BMCWEB_LOG_DEBUG << "Number of Segments provied : "
Ed Tanous7cd94e42020-09-29 16:03:02 -0700386 << std::get<4>(refLockRecord).size();
manojkiraneda0b631ae2019-12-03 17:54:28 +0530387 return false;
388 }
389
390 int lockFlag = 0;
391 // validate the lockflags & segment length
392
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500393 for (const auto& p : std::get<4>(refLockRecord))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530394 {
395
396 // validate the lock flags
397 // Allowed lockflags are locksame,lockall & dontlock
398
399 if (!((boost::equals(p.first, "LockSame") ||
400 (boost::equals(p.first, "LockAll")) ||
401 (boost::equals(p.first, "DontLock")))))
402 {
403 BMCWEB_LOG_DEBUG << "Validation of lock flags failed";
404 BMCWEB_LOG_DEBUG << p.first;
405 return false;
406 }
407
408 // validate the segment length
409 // Allowed values of segment length are between 1 and 4
410
411 if (p.second < 1 || p.second > 4)
412 {
413 BMCWEB_LOG_DEBUG << "Validation of Segment Length Failed";
414 BMCWEB_LOG_DEBUG << p.second;
415 return false;
416 }
417
418 if ((boost::equals(p.first, "LockSame") ||
419 (boost::equals(p.first, "LockAll"))))
420 {
421 ++lockFlag;
422 if (lockFlag >= 2)
423 {
424 return false;
425 }
426 }
427 }
428
manojkiraneda0b631ae2019-12-03 17:54:28 +0530429 return true;
430}
431
Ed Tanousb5a76932020-09-29 16:16:58 -0700432inline Rc Lock::isConflictWithTable(const LockRequests& refLockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530433{
manojkiraneda0b631ae2019-12-03 17:54:28 +0530434 if (lockTable.empty())
435 {
Ed Tanousf8fe53e2022-06-30 15:55:45 -0700436 uint32_t thisTransactionId = generateTransactionId();
Ed Tanous8a592812022-06-04 09:06:59 -0700437 BMCWEB_LOG_DEBUG << thisTransactionId;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530438 // Lock table is empty, so we are safe to add the lockrecords
439 // as there will be no conflict
440 BMCWEB_LOG_DEBUG << "Lock table is empty, so adding the lockrecords";
441 lockTable.emplace(std::pair<uint32_t, LockRequests>(
Ed Tanous8a592812022-06-04 09:06:59 -0700442 thisTransactionId, refLockRequestStructure));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530443
Ed Tanous8a592812022-06-04 09:06:59 -0700444 return std::make_pair(false, thisTransactionId);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530445 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700446 BMCWEB_LOG_DEBUG
447 << "Lock table is not empty, check for conflict with lock table";
448 // Lock table is not empty, compare the lockrequest entries with
449 // the entries in the lock table
manojkiraneda0b631ae2019-12-03 17:54:28 +0530450
Ed Tanous3174e4d2020-10-07 11:41:22 -0700451 for (const auto& lockRecord1 : refLockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530452 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700453 for (const auto& map : lockTable)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530454 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700455 for (const auto& lockRecord2 : map.second)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530456 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700457 bool status = isConflictRecord(lockRecord1, lockRecord2);
458 if (status)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530459 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700460 return std::make_pair(
461 true, std::make_pair(map.first, lockRecord2));
manojkiraneda0b631ae2019-12-03 17:54:28 +0530462 }
463 }
464 }
manojkiraneda0b631ae2019-12-03 17:54:28 +0530465 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700466
467 // Reached here, so no conflict with the locktable, so we are safe to
468 // add the request records into the lock table
469
470 // Lock table is empty, so we are safe to add the lockrecords
471 // as there will be no conflict
472 BMCWEB_LOG_DEBUG << " Adding elements into lock table";
473 transactionId = generateTransactionId();
474 lockTable.emplace(std::make_pair(transactionId, refLockRequestStructure));
475
manojkiraneda0b631ae2019-12-03 17:54:28 +0530476 return std::make_pair(false, transactionId);
477}
478
Ed Tanousb5a76932020-09-29 16:16:58 -0700479inline bool Lock::isConflictRequest(const LockRequests& refLockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530480{
481 // check for all the locks coming in as a part of single request
482 // return conflict if any two lock requests are conflicting
483
484 if (refLockRequestStructure.size() == 1)
485 {
486 BMCWEB_LOG_DEBUG << "Only single lock request, so there is no conflict";
487 // This means , we have only one lock request in the current
488 // request , so no conflict within the request
489 return false;
490 }
491
Ed Tanous3174e4d2020-10-07 11:41:22 -0700492 BMCWEB_LOG_DEBUG
493 << "There are multiple lock requests coming in a single request";
494
495 // There are multiple requests a part of one request
496
497 for (uint32_t i = 0; i < refLockRequestStructure.size(); i++)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530498 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700499 for (uint32_t j = i + 1; j < refLockRequestStructure.size(); j++)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530500 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700501 const LockRequest& p = refLockRequestStructure[i];
502 const LockRequest& q = refLockRequestStructure[j];
503 bool status = isConflictRecord(p, q);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530504
Ed Tanous3174e4d2020-10-07 11:41:22 -0700505 if (status)
506 {
507 return true;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530508 }
509 }
510 }
511 return false;
512}
513
514// This function converts the provided uint64_t resource id's from the two
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500515// lock requests subjected for comparison, and this function also compares
manojkiraneda0b631ae2019-12-03 17:54:28 +0530516// the content by bytes mentioned by a uint32_t number.
517
518// If all the elements in the lock requests which are subjected for comparison
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500519// are same, then the last comparison would be to check for the respective
manojkiraneda0b631ae2019-12-03 17:54:28 +0530520// bytes in the resourceid based on the segment length.
521
Ratan Gupta07386c62019-12-14 14:06:09 +0530522inline bool Lock::checkByte(uint64_t resourceId1, uint64_t resourceId2,
523 uint32_t position)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530524{
Ed Tanous46ff87b2022-01-07 09:25:51 -0800525 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500526 uint8_t* p = reinterpret_cast<uint8_t*>(&resourceId1);
Ed Tanous46ff87b2022-01-07 09:25:51 -0800527 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500528 uint8_t* q = reinterpret_cast<uint8_t*>(&resourceId2);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530529
Ed Tanousca45aa32022-01-07 09:28:45 -0800530 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
531 uint8_t pPosition = p[position];
532 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
533 uint8_t qPosition = q[position];
534
535 BMCWEB_LOG_DEBUG << "Comparing bytes " << std::to_string(pPosition) << ","
536 << std::to_string(qPosition);
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800537 return pPosition == qPosition;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530538}
539
Ed Tanousb5a76932020-09-29 16:16:58 -0700540inline bool Lock::isConflictRecord(const LockRequest& refLockRecord1,
541 const LockRequest& refLockRecord2)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530542{
543 // No conflict if both are read locks
544
545 if (boost::equals(std::get<2>(refLockRecord1), "Read") &&
546 boost::equals(std::get<2>(refLockRecord2), "Read"))
547 {
548 BMCWEB_LOG_DEBUG << "Both are read locks, no conflict";
549 return false;
550 }
551
Ed Tanous3174e4d2020-10-07 11:41:22 -0700552 uint32_t i = 0;
553 for (const auto& p : std::get<4>(refLockRecord1))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530554 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700555
556 // return conflict when any of them is try to lock all resources
557 // under the current resource level.
558 if (boost::equals(p.first, "LockAll") ||
559 boost::equals(std::get<4>(refLockRecord2)[i].first, "LockAll"))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530560 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700561 BMCWEB_LOG_DEBUG
562 << "Either of the Comparing locks are trying to lock all "
563 "resources under the current resource level";
564 return true;
565 }
manojkiraneda0b631ae2019-12-03 17:54:28 +0530566
Ed Tanous3174e4d2020-10-07 11:41:22 -0700567 // determine if there is a lock-all-with-same-segment-size.
568 // If the current segment sizes are the same,then we should fail.
569
570 if ((boost::equals(p.first, "LockSame") ||
571 boost::equals(std::get<4>(refLockRecord2)[i].first, "LockSame")) &&
572 (p.second == std::get<4>(refLockRecord2)[i].second))
573 {
574 return true;
575 }
576
577 // if segment lengths are not the same, it means two different locks
578 // So no conflict
579 if (p.second != std::get<4>(refLockRecord2)[i].second)
580 {
581 BMCWEB_LOG_DEBUG << "Segment lengths are not same";
582 BMCWEB_LOG_DEBUG << "Segment 1 length : " << p.second;
583 BMCWEB_LOG_DEBUG << "Segment 2 length : "
584 << std::get<4>(refLockRecord2)[i].second;
585 return false;
586 }
587
588 // compare segment data
589
Ed Tanous8a592812022-06-04 09:06:59 -0700590 for (uint32_t j = 0; j < p.second; j++)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700591 {
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500592 // if the segment data is different, then the locks is on a
593 // different resource so no conflict between the lock
Ed Tanous3174e4d2020-10-07 11:41:22 -0700594 // records.
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500595 // BMC is little endian, but the resourceID is formed by
Ed Tanous3174e4d2020-10-07 11:41:22 -0700596 // the Management Console in such a way that, the first byte
597 // from the MSB Position corresponds to the First Segment
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500598 // data. Therefore we need to convert the incoming
Ed Tanous3174e4d2020-10-07 11:41:22 -0700599 // resourceID into Big Endian before processing further.
600 if (!(checkByte(
601 boost::endian::endian_reverse(std::get<3>(refLockRecord1)),
602 boost::endian::endian_reverse(std::get<3>(refLockRecord2)),
Ed Tanous8a592812022-06-04 09:06:59 -0700603 j)))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530604 {
manojkiraneda0b631ae2019-12-03 17:54:28 +0530605 return false;
606 }
manojkiraneda0b631ae2019-12-03 17:54:28 +0530607 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700608
609 ++i;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530610 }
611
Sunitha Harish41bb02b2022-08-08 03:12:15 -0500612 return true;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530613}
614
Ratan Gupta07386c62019-12-14 14:06:09 +0530615inline uint32_t Lock::generateTransactionId()
manojkiraneda0b631ae2019-12-03 17:54:28 +0530616{
617 ++transactionId;
618 return transactionId;
619}
620
621} // namespace ibm_mc_lock
622} // namespace crow