blob: 800ee484e85d69a1acaece07276a2886e39cc65f [file] [log] [blame]
manojkiraneda0b631ae2019-12-03 17:54:28 +05301#pragma once
2
manojkiraneda0b631ae2019-12-03 17:54:28 +05303#include <boost/algorithm/string.hpp>
4#include <boost/container/flat_map.hpp>
Manojkiran Eda55fd1a92020-04-30 19:06:48 +05305#include <boost/endian/conversion.hpp>
Sunitha Harish3e919b52020-10-13 01:21:48 -05006#include <include/ibm/utils.hpp>
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include <logging.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05008#include <nlohmann/json.hpp>
9
manojkiraneda0b631ae2019-12-03 17:54:28 +053010#include <filesystem>
Sunitha Harish8a3bb712019-12-13 03:48:09 -060011#include <fstream>
manojkiraneda0b631ae2019-12-03 17:54:28 +053012
13namespace crow
14{
15namespace ibm_mc_lock
16{
17
manojkiraneda0b631ae2019-12-03 17:54:28 +053018using SType = std::string;
19
20/*----------------------------------------
21|Segment flags : LockFlag | SegmentLength|
22------------------------------------------*/
23
24using SegmentFlags = std::vector<std::pair<SType, uint32_t>>;
25
26// Lockrequest = session-id | hmc-id | locktype | resourceid | segmentinfo
27using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053028using LockRequests = std::vector<LockRequest>;
29using Rc =
30 std::pair<bool, std::variant<uint32_t, std::pair<uint32_t, LockRequest>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053031using RcRelaseLock = std::pair<bool, std::pair<uint32_t, LockRequest>>;
manojkiraneda402b5712019-12-13 17:07:09 +053032using RcGetLockList =
33 std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053034using ListOfTransactionIds = std::vector<uint32_t>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053035using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>;
manojkiraneda3b6dea62019-12-13 17:05:36 +053036using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>;
37using SessionFlags = std::pair<SType, SType>;
manojkiraneda402b5712019-12-13 17:07:09 +053038using ListOfSessionIds = std::vector<std::string>;
manojkiraneda0b631ae2019-12-03 17:54:28 +053039
40class Lock
41{
42 uint32_t transactionId;
43 boost::container::flat_map<uint32_t, LockRequests> lockTable;
44
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053045 protected:
46 /*
47 * This function implements the logic for validating an incoming
48 * lock request/requests.
49 *
50 * Returns : True (if Valid)
51 * Returns : False (if not a Valid lock request)
52 */
53
Ed Tanousb5a76932020-09-29 16:16:58 -070054 virtual bool isValidLockRequest(const LockRequest&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053055
56 /*
57 * This function implements the logic of checking if the incoming
58 * multi-lock request is not having conflicting requirements.
59 *
60 * Returns : True (if conflicting)
61 * Returns : False (if not conflicting)
62 */
63
Ed Tanousb5a76932020-09-29 16:16:58 -070064 virtual bool isConflictRequest(const LockRequests&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053065 /*
66 * Implements the core algorithm to find the conflicting
67 * lock requests.
68 *
69 * This functions takes two lock requests and check if both
70 * are conflicting to each other.
71 *
72 * Returns : True (if conflicting)
73 * Returns : False (if not conflicting)
74 */
Ed Tanousb5a76932020-09-29 16:16:58 -070075 virtual bool isConflictRecord(const LockRequest&, const LockRequest&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053076
77 /*
78 * This function implements the logic of checking the conflicting
79 * locks from a incoming single/multi lock requests with the already
80 * existing lock request in the lock table.
81 *
82 */
83
Ed Tanousb5a76932020-09-29 16:16:58 -070084 virtual Rc isConflictWithTable(const LockRequests&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053085 /*
86 * This function implements the logic of checking the ownership of the
87 * lock from the releaselock request.
88 *
89 * Returns : True (if the requesting HMC & Session owns the lock(s))
90 * Returns : False (if the request HMC or Session does not own the lock(s))
91 */
92
Gunnar Mills1214b7e2020-06-04 10:11:30 -050093 virtual RcRelaseLock isItMyLock(const ListOfTransactionIds&,
94 const SessionFlags&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +053095
96 /*
97 * This function validates the the list of transactionID's and returns false
98 * if the transaction ID is not valid & not present in the lock table
99 */
100
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500101 virtual bool validateRids(const ListOfTransactionIds&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530102
103 /*
104 * This function releases the locks that are already obtained by the
105 * requesting Management console.
106 */
107
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500108 void releaseLock(const ListOfTransactionIds&);
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530109
110 Lock()
111 {
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530112 transactionId = lockTable.empty() ? 0 : prev(lockTable.end())->first;
113 }
114
Sunitha Harish8a3bb712019-12-13 03:48:09 -0600115 /*
manojkiraneda0b631ae2019-12-03 17:54:28 +0530116 * This function implements the algorithm for checking the respective
117 * bytes of the resource id based on the lock management algorithm.
118 */
119
120 bool checkByte(uint64_t, uint64_t, uint32_t);
121
122 /*
123 * This functions implements a counter that generates a unique 32 bit
124 * number for every successful transaction. This number will be used by
125 * the Management Console for debug.
126 */
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530127 virtual uint32_t generateTransactionId();
Sunitha Harish8a3bb712019-12-13 03:48:09 -0600128
manojkiraneda0b631ae2019-12-03 17:54:28 +0530129 public:
130 /*
131 * This function implements the logic for acquiring a lock on a
Manojkiran Eda5bb0ece2020-01-20 20:22:36 +0530132 * resource if the incoming request is legitimate without any
manojkiraneda0b631ae2019-12-03 17:54:28 +0530133 * conflicting requirements & without any conflicting requirement
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500134 * with the existing locks in the lock table.
manojkiraneda0b631ae2019-12-03 17:54:28 +0530135 *
136 */
137
Ed Tanousb5a76932020-09-29 16:16:58 -0700138 RcAcquireLock acquireLock(const LockRequests&);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530139
manojkiraneda3b6dea62019-12-13 17:05:36 +0530140 /*
141 * This function implements the logic for releasing the lock that are
142 * owned by a management console session.
143 *
144 * The locks can be released by two ways
145 * - Using list of transaction ID's
146 * - Using a Session ID
147 *
148 * Client can choose either of the ways by using `Type` JSON key.
149 *
150 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500151 RcReleaseLockApi releaseLock(const ListOfTransactionIds&,
152 const SessionFlags&);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530153
manojkiraneda402b5712019-12-13 17:07:09 +0530154 /*
155 * This function implements the logic for getting the list of locks obtained
156 * by a particular management console.
157 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500158 RcGetLockList getLockList(const ListOfSessionIds&);
manojkiraneda402b5712019-12-13 17:07:09 +0530159
Ratan Gupta07386c62019-12-14 14:06:09 +0530160 /*
161 * This function is releases all the locks obtained by a particular
162 * session.
163 */
164
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500165 void releaseLock(const std::string&);
Ratan Gupta07386c62019-12-14 14:06:09 +0530166
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500167 static Lock& getInstance()
Ratan Gupta07386c62019-12-14 14:06:09 +0530168 {
169 static Lock lockObject;
170 return lockObject;
171 }
manojkiraneda4eaf2ee2019-12-13 17:10:41 +0530172
Ed Tanous4e087512020-09-28 18:41:25 -0700173 virtual ~Lock() = default;
Ratan Gupta07386c62019-12-14 14:06:09 +0530174};
manojkiraneda0b631ae2019-12-03 17:54:28 +0530175
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500176inline RcGetLockList Lock::getLockList(const ListOfSessionIds& listSessionId)
manojkiraneda402b5712019-12-13 17:07:09 +0530177{
178
179 std::vector<std::pair<uint32_t, LockRequests>> lockList;
180
181 if (!lockTable.empty())
182 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500183 for (const auto& i : listSessionId)
manojkiraneda402b5712019-12-13 17:07:09 +0530184 {
185 auto it = lockTable.begin();
186 while (it != lockTable.end())
187 {
188 // Check if session id of this entry matches with session id
189 // given
190 if (std::get<0>(it->second[0]) == i)
191 {
192 BMCWEB_LOG_DEBUG << "Session id is found in the locktable";
193
194 // Push the whole lock record into a vector for returning
195 // the json
Ed Tanous4e087512020-09-28 18:41:25 -0700196 lockList.emplace_back(it->first, it->second);
manojkiraneda402b5712019-12-13 17:07:09 +0530197 }
198 // Go to next entry in map
199 it++;
200 }
201 }
202 }
203 // we may have found at least one entry with the given session id
204 // return the json list of lock records pertaining to the given
205 // session id, or send an empty list if lock table is empty
Ed Tanous02379d32020-09-15 21:15:44 -0700206 return {lockList};
manojkiraneda402b5712019-12-13 17:07:09 +0530207}
208
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500209inline RcReleaseLockApi Lock::releaseLock(const ListOfTransactionIds& p,
210 const SessionFlags& ids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530211{
212
213 bool status = validateRids(p);
214
215 if (!status)
216 {
217 // Validation of rids failed
218 BMCWEB_LOG_DEBUG << "Not a Valid request id";
219 return std::make_pair(false, status);
220 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700221 // Validation passed, check if all the locks are owned by the
222 // requesting HMC
223 auto status2 = isItMyLock(p, ids);
224 if (status2.first)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530225 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700226 // The current hmc owns all the locks, so we can release
227 // them
228 releaseLock(p);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530229 }
Manojkiran Edaa1ffbb82020-10-28 17:42:21 +0530230 return std::make_pair(true, status2);
manojkiraneda3b6dea62019-12-13 17:05:36 +0530231}
232
Ed Tanousb5a76932020-09-29 16:16:58 -0700233inline RcAcquireLock Lock::acquireLock(const LockRequests& lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530234{
235
236 // validate the lock request
237
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500238 for (auto& lockRecord : lockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530239 {
240 bool status = isValidLockRequest(lockRecord);
241 if (!status)
242 {
243 BMCWEB_LOG_DEBUG << "Not a Valid record";
244 BMCWEB_LOG_DEBUG << "Bad json in request";
245 return std::make_pair(true, std::make_pair(status, 0));
246 }
247 }
248 // check for conflict record
249
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500250 const LockRequests& multiRequest = lockRequestStructure;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530251 bool status = isConflictRequest(multiRequest);
252
253 if (status)
254 {
255 BMCWEB_LOG_DEBUG << "There is a conflict within itself";
256 return std::make_pair(true, std::make_pair(status, 1));
257 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700258 BMCWEB_LOG_DEBUG << "The request is not conflicting within itself";
manojkiraneda0b631ae2019-12-03 17:54:28 +0530259
Ed Tanous3174e4d2020-10-07 11:41:22 -0700260 // Need to check for conflict with the locktable entries.
manojkiraneda0b631ae2019-12-03 17:54:28 +0530261
Ed Tanous3174e4d2020-10-07 11:41:22 -0700262 auto conflict = isConflictWithTable(multiRequest);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530263
Ed Tanous3174e4d2020-10-07 11:41:22 -0700264 BMCWEB_LOG_DEBUG << "Done with checking conflict with the locktable";
265 return std::make_pair(false, conflict);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530266}
267
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500268inline void Lock::releaseLock(const ListOfTransactionIds& refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530269{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 for (const auto& id : refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530271 {
272 if (lockTable.erase(id))
273 {
274 BMCWEB_LOG_DEBUG << "Removing the locks with transaction ID : "
275 << id;
276 }
277
278 else
279 {
280 BMCWEB_LOG_DEBUG << "Removing the locks from the lock table "
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500281 "failed, transaction ID: "
manojkiraneda3b6dea62019-12-13 17:05:36 +0530282 << id;
283 }
284 }
285}
286
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287inline void Lock::releaseLock(const std::string& sessionId)
Ratan Gupta07386c62019-12-14 14:06:09 +0530288{
Ratan Gupta07386c62019-12-14 14:06:09 +0530289 if (!lockTable.empty())
290 {
291 auto it = lockTable.begin();
292 while (it != lockTable.end())
293 {
294 if (it->second.size() != 0)
295 {
296 // Check if session id of this entry matches with session id
297 // given
298 if (std::get<0>(it->second[0]) == sessionId)
299 {
300 BMCWEB_LOG_DEBUG << "Remove the lock from the locktable "
301 "having sessionID="
302 << sessionId;
303 BMCWEB_LOG_DEBUG << "TransactionID =" << it->first;
304 it = lockTable.erase(it);
Ratan Gupta07386c62019-12-14 14:06:09 +0530305 }
306 else
307 {
308 it++;
309 }
310 }
311 }
Ratan Gupta07386c62019-12-14 14:06:09 +0530312 }
313}
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500314inline RcRelaseLock Lock::isItMyLock(const ListOfTransactionIds& refRids,
315 const SessionFlags& ids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530316{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 for (const auto& id : refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530318 {
319 // Just need to compare the client id of the first lock records in the
320 // complete lock row(in the map), because the rest of the lock records
321 // would have the same client id
322
323 std::string expectedClientId = std::get<1>(lockTable[id][0]);
324 std::string expectedSessionId = std::get<0>(lockTable[id][0]);
325
326 if ((expectedClientId == ids.first) &&
327 (expectedSessionId == ids.second))
328 {
329 // It is owned by the currently request hmc
330 BMCWEB_LOG_DEBUG << "Lock is owned by the current hmc";
331 }
332 else
333 {
334 BMCWEB_LOG_DEBUG << "Lock is not owned by the current hmc";
335 return std::make_pair(false, std::make_pair(id, lockTable[id][0]));
336 }
337 }
338 return std::make_pair(true, std::make_pair(0, LockRequest()));
339}
340
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500341inline bool Lock::validateRids(const ListOfTransactionIds& refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530342{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343 for (const auto& id : refRids)
manojkiraneda3b6dea62019-12-13 17:05:36 +0530344 {
345 auto search = lockTable.find(id);
346
347 if (search != lockTable.end())
348 {
349 BMCWEB_LOG_DEBUG << "Valid Transaction id";
350 // continue for the next rid
351 }
352 else
353 {
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500354 BMCWEB_LOG_DEBUG << "At least 1 inValid Request id";
manojkiraneda3b6dea62019-12-13 17:05:36 +0530355 return false;
356 }
357 }
358 return true;
359}
360
Ed Tanousb5a76932020-09-29 16:16:58 -0700361inline bool Lock::isValidLockRequest(const LockRequest& refLockRecord)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530362{
363
364 // validate the locktype
365
366 if (!((boost::equals(std::get<2>(refLockRecord), "Read") ||
367 (boost::equals(std::get<2>(refLockRecord), "Write")))))
368 {
369 BMCWEB_LOG_DEBUG << "Validation of LockType Failed";
370 BMCWEB_LOG_DEBUG << "Locktype : " << std::get<2>(refLockRecord);
371 return false;
372 }
373
374 BMCWEB_LOG_DEBUG << static_cast<int>(std::get<4>(refLockRecord).size());
375
376 // validate the number of segments
377 // Allowed No of segments are between 2 and 6
378 if ((static_cast<int>(std::get<4>(refLockRecord).size()) > 6) ||
379 (static_cast<int>(std::get<4>(refLockRecord).size()) < 2))
380 {
Gunnar Millscaa3ce32020-07-08 14:46:53 -0500381 BMCWEB_LOG_DEBUG << "Validation of Number of Segments Failed";
manojkiraneda0b631ae2019-12-03 17:54:28 +0530382 BMCWEB_LOG_DEBUG << "Number of Segments provied : "
Ed Tanous7cd94e42020-09-29 16:03:02 -0700383 << std::get<4>(refLockRecord).size();
manojkiraneda0b631ae2019-12-03 17:54:28 +0530384 return false;
385 }
386
387 int lockFlag = 0;
388 // validate the lockflags & segment length
389
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500390 for (const auto& p : std::get<4>(refLockRecord))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530391 {
392
393 // validate the lock flags
394 // Allowed lockflags are locksame,lockall & dontlock
395
396 if (!((boost::equals(p.first, "LockSame") ||
397 (boost::equals(p.first, "LockAll")) ||
398 (boost::equals(p.first, "DontLock")))))
399 {
400 BMCWEB_LOG_DEBUG << "Validation of lock flags failed";
401 BMCWEB_LOG_DEBUG << p.first;
402 return false;
403 }
404
405 // validate the segment length
406 // Allowed values of segment length are between 1 and 4
407
408 if (p.second < 1 || p.second > 4)
409 {
410 BMCWEB_LOG_DEBUG << "Validation of Segment Length Failed";
411 BMCWEB_LOG_DEBUG << p.second;
412 return false;
413 }
414
415 if ((boost::equals(p.first, "LockSame") ||
416 (boost::equals(p.first, "LockAll"))))
417 {
418 ++lockFlag;
419 if (lockFlag >= 2)
420 {
421 return false;
422 }
423 }
424 }
425
manojkiraneda0b631ae2019-12-03 17:54:28 +0530426 return true;
427}
428
Ed Tanousb5a76932020-09-29 16:16:58 -0700429inline Rc Lock::isConflictWithTable(const LockRequests& refLockRequestStructure)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530430{
431
432 uint32_t transactionId;
433
434 if (lockTable.empty())
435 {
436 transactionId = generateTransactionId();
437 BMCWEB_LOG_DEBUG << transactionId;
438 // 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>(
442 transactionId, refLockRequestStructure));
443
444 return std::make_pair(false, transactionId);
445 }
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{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 uint8_t* p = reinterpret_cast<uint8_t*>(&resourceId1);
526 uint8_t* q = reinterpret_cast<uint8_t*>(&resourceId2);
manojkiraneda0b631ae2019-12-03 17:54:28 +0530527
528 BMCWEB_LOG_DEBUG << "Comparing bytes " << std::to_string(p[position]) << ","
529 << std::to_string(q[position]);
530 if (p[position] != q[position])
531 {
532 return false;
533 }
534
manojkiraneda0b631ae2019-12-03 17:54:28 +0530535 return true;
536}
537
Ed Tanousb5a76932020-09-29 16:16:58 -0700538inline bool Lock::isConflictRecord(const LockRequest& refLockRecord1,
539 const LockRequest& refLockRecord2)
manojkiraneda0b631ae2019-12-03 17:54:28 +0530540{
541 // No conflict if both are read locks
542
543 if (boost::equals(std::get<2>(refLockRecord1), "Read") &&
544 boost::equals(std::get<2>(refLockRecord2), "Read"))
545 {
546 BMCWEB_LOG_DEBUG << "Both are read locks, no conflict";
547 return false;
548 }
549
Ed Tanous3174e4d2020-10-07 11:41:22 -0700550 uint32_t i = 0;
551 for (const auto& p : std::get<4>(refLockRecord1))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530552 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700553
554 // return conflict when any of them is try to lock all resources
555 // under the current resource level.
556 if (boost::equals(p.first, "LockAll") ||
557 boost::equals(std::get<4>(refLockRecord2)[i].first, "LockAll"))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530558 {
Ed Tanous3174e4d2020-10-07 11:41:22 -0700559 BMCWEB_LOG_DEBUG
560 << "Either of the Comparing locks are trying to lock all "
561 "resources under the current resource level";
562 return true;
563 }
manojkiraneda0b631ae2019-12-03 17:54:28 +0530564
Ed Tanous3174e4d2020-10-07 11:41:22 -0700565 // determine if there is a lock-all-with-same-segment-size.
566 // If the current segment sizes are the same,then we should fail.
567
568 if ((boost::equals(p.first, "LockSame") ||
569 boost::equals(std::get<4>(refLockRecord2)[i].first, "LockSame")) &&
570 (p.second == std::get<4>(refLockRecord2)[i].second))
571 {
572 return true;
573 }
574
575 // if segment lengths are not the same, it means two different locks
576 // So no conflict
577 if (p.second != std::get<4>(refLockRecord2)[i].second)
578 {
579 BMCWEB_LOG_DEBUG << "Segment lengths are not same";
580 BMCWEB_LOG_DEBUG << "Segment 1 length : " << p.second;
581 BMCWEB_LOG_DEBUG << "Segment 2 length : "
582 << std::get<4>(refLockRecord2)[i].second;
583 return false;
584 }
585
586 // compare segment data
587
588 for (uint32_t i = 0; i < p.second; i++)
589 {
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500590 // if the segment data is different, then the locks is on a
591 // different resource so no conflict between the lock
Ed Tanous3174e4d2020-10-07 11:41:22 -0700592 // records.
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500593 // BMC is little endian, but the resourceID is formed by
Ed Tanous3174e4d2020-10-07 11:41:22 -0700594 // the Management Console in such a way that, the first byte
595 // from the MSB Position corresponds to the First Segment
Ali Ahmedd3d26ba2021-04-30 09:13:53 -0500596 // data. Therefore we need to convert the incoming
Ed Tanous3174e4d2020-10-07 11:41:22 -0700597 // resourceID into Big Endian before processing further.
598 if (!(checkByte(
599 boost::endian::endian_reverse(std::get<3>(refLockRecord1)),
600 boost::endian::endian_reverse(std::get<3>(refLockRecord2)),
601 i)))
manojkiraneda0b631ae2019-12-03 17:54:28 +0530602 {
manojkiraneda0b631ae2019-12-03 17:54:28 +0530603 return false;
604 }
manojkiraneda0b631ae2019-12-03 17:54:28 +0530605 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700606
607 ++i;
manojkiraneda0b631ae2019-12-03 17:54:28 +0530608 }
609
610 return false;
611}
612
Ratan Gupta07386c62019-12-14 14:06:09 +0530613inline uint32_t Lock::generateTransactionId()
manojkiraneda0b631ae2019-12-03 17:54:28 +0530614{
615 ++transactionId;
616 return transactionId;
617}
618
619} // namespace ibm_mc_lock
620} // namespace crow