manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 3 | #include "ibm/utils.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 4 | #include "logging.hpp" |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 5 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 6 | #include <boost/algorithm/string/predicate.hpp> |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 7 | #include <boost/container/flat_map.hpp> |
Manojkiran Eda | 55fd1a9 | 2020-04-30 19:06:48 +0530 | [diff] [blame] | 8 | #include <boost/endian/conversion.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
| 10 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 11 | #include <filesystem> |
Sunitha Harish | 8a3bb71 | 2019-12-13 03:48:09 -0600 | [diff] [blame] | 12 | #include <fstream> |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 13 | #include <variant> |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 14 | |
| 15 | namespace crow |
| 16 | { |
| 17 | namespace ibm_mc_lock |
| 18 | { |
| 19 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 20 | using SType = std::string; |
| 21 | |
| 22 | /*---------------------------------------- |
| 23 | |Segment flags : LockFlag | SegmentLength| |
| 24 | ------------------------------------------*/ |
| 25 | |
| 26 | using SegmentFlags = std::vector<std::pair<SType, uint32_t>>; |
| 27 | |
| 28 | // Lockrequest = session-id | hmc-id | locktype | resourceid | segmentinfo |
| 29 | using LockRequest = std::tuple<SType, SType, SType, uint64_t, SegmentFlags>; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 30 | using LockRequests = std::vector<LockRequest>; |
| 31 | using Rc = |
| 32 | std::pair<bool, std::variant<uint32_t, std::pair<uint32_t, LockRequest>>>; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 33 | using RcRelaseLock = std::pair<bool, std::pair<uint32_t, LockRequest>>; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 34 | using RcGetLockList = |
| 35 | std::variant<std::string, std::vector<std::pair<uint32_t, LockRequests>>>; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 36 | using ListOfTransactionIds = std::vector<uint32_t>; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 37 | using RcAcquireLock = std::pair<bool, std::variant<Rc, std::pair<bool, int>>>; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 38 | using RcReleaseLockApi = std::pair<bool, std::variant<bool, RcRelaseLock>>; |
| 39 | using SessionFlags = std::pair<SType, SType>; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 40 | using ListOfSessionIds = std::vector<std::string>; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 41 | |
| 42 | class Lock |
| 43 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 44 | uint32_t transactionId = 0; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 45 | boost::container::flat_map<uint32_t, LockRequests> lockTable; |
| 46 | |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 47 | 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 56 | virtual bool isValidLockRequest(const LockRequest& refLockRecord); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 57 | |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 66 | virtual bool isConflictRequest(const LockRequests& refLockRequestStructure); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 67 | /* |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 77 | virtual bool isConflictRecord(const LockRequest& refLockRecord1, |
| 78 | const LockRequest& refLockRecord2); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 79 | |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 87 | virtual Rc isConflictWithTable(const LockRequests& refLockRequestStructure); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 88 | /* |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 96 | virtual RcRelaseLock isItMyLock(const ListOfTransactionIds& refRids, |
| 97 | const SessionFlags& ids); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 98 | |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 104 | virtual bool validateRids(const ListOfTransactionIds& refRids); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * This function releases the locks that are already obtained by the |
| 108 | * requesting Management console. |
| 109 | */ |
| 110 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 111 | void releaseLock(const ListOfTransactionIds& refRids); |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 112 | |
| 113 | Lock() |
| 114 | { |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 115 | transactionId = lockTable.empty() ? 0 : prev(lockTable.end())->first; |
| 116 | } |
| 117 | |
Sunitha Harish | 8a3bb71 | 2019-12-13 03:48:09 -0600 | [diff] [blame] | 118 | /* |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 119 | * This function implements the algorithm for checking the respective |
| 120 | * bytes of the resource id based on the lock management algorithm. |
| 121 | */ |
| 122 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 123 | static bool checkByte(uint64_t resourceId1, uint64_t resourceId2, |
| 124 | uint32_t position); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 125 | |
| 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 | */ |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 131 | virtual uint32_t generateTransactionId(); |
Sunitha Harish | 8a3bb71 | 2019-12-13 03:48:09 -0600 | [diff] [blame] | 132 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 133 | public: |
| 134 | /* |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 135 | * 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 | /* |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 143 | * This function implements the logic for acquiring a lock on a |
Manojkiran Eda | 5bb0ece | 2020-01-20 20:22:36 +0530 | [diff] [blame] | 144 | * resource if the incoming request is legitimate without any |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 145 | * conflicting requirements & without any conflicting requirement |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 146 | * with the existing locks in the lock table. |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 147 | * |
| 148 | */ |
| 149 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 150 | RcAcquireLock acquireLock(const LockRequests& lockRequestStructure); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 151 | |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 152 | /* |
| 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 Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 163 | RcReleaseLockApi releaseLock(const ListOfTransactionIds& p, |
| 164 | const SessionFlags& ids); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 165 | |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 166 | /* |
| 167 | * This function implements the logic for getting the list of locks obtained |
| 168 | * by a particular management console. |
| 169 | */ |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 170 | RcGetLockList getLockList(const ListOfSessionIds& listSessionId); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 171 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 172 | /* |
| 173 | * This function is releases all the locks obtained by a particular |
| 174 | * session. |
| 175 | */ |
| 176 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 177 | void releaseLock(const std::string& sessionId); |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 178 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 179 | static Lock& getInstance() |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 180 | { |
| 181 | static Lock lockObject; |
| 182 | return lockObject; |
| 183 | } |
manojkiraneda | 4eaf2ee | 2019-12-13 17:10:41 +0530 | [diff] [blame] | 184 | |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 185 | virtual ~Lock() = default; |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 186 | }; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 187 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 188 | inline RcGetLockList Lock::getLockList(const ListOfSessionIds& listSessionId) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 189 | { |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 190 | std::vector<std::pair<uint32_t, LockRequests>> lockList; |
| 191 | |
| 192 | if (!lockTable.empty()) |
| 193 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 194 | for (const auto& i : listSessionId) |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 195 | { |
| 196 | auto it = lockTable.begin(); |
| 197 | while (it != lockTable.end()) |
| 198 | { |
| 199 | // Check if session id of this entry matches with session id |
| 200 | // given |
| 201 | if (std::get<0>(it->second[0]) == i) |
| 202 | { |
| 203 | BMCWEB_LOG_DEBUG << "Session id is found in the locktable"; |
| 204 | |
| 205 | // Push the whole lock record into a vector for returning |
| 206 | // the json |
Ed Tanous | 4e08751 | 2020-09-28 18:41:25 -0700 | [diff] [blame] | 207 | lockList.emplace_back(it->first, it->second); |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 208 | } |
| 209 | // Go to next entry in map |
| 210 | it++; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | // we may have found at least one entry with the given session id |
| 215 | // return the json list of lock records pertaining to the given |
| 216 | // session id, or send an empty list if lock table is empty |
Ed Tanous | 02379d3 | 2020-09-15 21:15:44 -0700 | [diff] [blame] | 217 | return {lockList}; |
manojkiraneda | 402b571 | 2019-12-13 17:07:09 +0530 | [diff] [blame] | 218 | } |
| 219 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 220 | inline RcReleaseLockApi Lock::releaseLock(const ListOfTransactionIds& p, |
| 221 | const SessionFlags& ids) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 222 | { |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 223 | bool status = validateRids(p); |
| 224 | |
| 225 | if (!status) |
| 226 | { |
| 227 | // Validation of rids failed |
Sunitha Harish | 3e7ab70 | 2022-08-08 02:08:39 -0500 | [diff] [blame] | 228 | BMCWEB_LOG_ERROR << "releaseLock: Contains invalid request id"; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 229 | return std::make_pair(false, status); |
| 230 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 231 | // Validation passed, check if all the locks are owned by the |
| 232 | // requesting HMC |
| 233 | auto status2 = isItMyLock(p, ids); |
Sunitha Harish | 3e7ab70 | 2022-08-08 02:08:39 -0500 | [diff] [blame] | 234 | if (!status2.first) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 235 | { |
Sunitha Harish | 3e7ab70 | 2022-08-08 02:08:39 -0500 | [diff] [blame] | 236 | return std::make_pair(false, status2); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 237 | } |
Manojkiran Eda | a1ffbb8 | 2020-10-28 17:42:21 +0530 | [diff] [blame] | 238 | return std::make_pair(true, status2); |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 239 | } |
| 240 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 241 | inline RcAcquireLock Lock::acquireLock(const LockRequests& lockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 242 | { |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 243 | // validate the lock request |
| 244 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 245 | for (const auto& lockRecord : lockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 246 | { |
| 247 | bool status = isValidLockRequest(lockRecord); |
| 248 | if (!status) |
| 249 | { |
| 250 | BMCWEB_LOG_DEBUG << "Not a Valid record"; |
| 251 | BMCWEB_LOG_DEBUG << "Bad json in request"; |
| 252 | return std::make_pair(true, std::make_pair(status, 0)); |
| 253 | } |
| 254 | } |
| 255 | // check for conflict record |
| 256 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 257 | const LockRequests& multiRequest = lockRequestStructure; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 258 | bool status = isConflictRequest(multiRequest); |
| 259 | |
| 260 | if (status) |
| 261 | { |
| 262 | BMCWEB_LOG_DEBUG << "There is a conflict within itself"; |
| 263 | return std::make_pair(true, std::make_pair(status, 1)); |
| 264 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 265 | BMCWEB_LOG_DEBUG << "The request is not conflicting within itself"; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 266 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 267 | // Need to check for conflict with the locktable entries. |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 268 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 269 | auto conflict = isConflictWithTable(multiRequest); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 270 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 271 | BMCWEB_LOG_DEBUG << "Done with checking conflict with the locktable"; |
| 272 | return std::make_pair(false, conflict); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 273 | } |
| 274 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 275 | inline void Lock::releaseLock(const std::string& sessionId) |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 276 | { |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 277 | if (!lockTable.empty()) |
| 278 | { |
| 279 | auto it = lockTable.begin(); |
| 280 | while (it != lockTable.end()) |
| 281 | { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 282 | if (!it->second.empty()) |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 283 | { |
| 284 | // Check if session id of this entry matches with session id |
| 285 | // given |
| 286 | if (std::get<0>(it->second[0]) == sessionId) |
| 287 | { |
| 288 | BMCWEB_LOG_DEBUG << "Remove the lock from the locktable " |
| 289 | "having sessionID=" |
| 290 | << sessionId; |
| 291 | BMCWEB_LOG_DEBUG << "TransactionID =" << it->first; |
| 292 | it = lockTable.erase(it); |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
| 296 | it++; |
| 297 | } |
| 298 | } |
| 299 | } |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 300 | } |
| 301 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 302 | inline RcRelaseLock Lock::isItMyLock(const ListOfTransactionIds& refRids, |
| 303 | const SessionFlags& ids) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 304 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 305 | for (const auto& id : refRids) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 306 | { |
| 307 | // Just need to compare the client id of the first lock records in the |
| 308 | // complete lock row(in the map), because the rest of the lock records |
| 309 | // would have the same client id |
| 310 | |
| 311 | std::string expectedClientId = std::get<1>(lockTable[id][0]); |
| 312 | std::string expectedSessionId = std::get<0>(lockTable[id][0]); |
| 313 | |
| 314 | if ((expectedClientId == ids.first) && |
| 315 | (expectedSessionId == ids.second)) |
| 316 | { |
| 317 | // It is owned by the currently request hmc |
| 318 | BMCWEB_LOG_DEBUG << "Lock is owned by the current hmc"; |
Sunitha Harish | 3e7ab70 | 2022-08-08 02:08:39 -0500 | [diff] [blame] | 319 | // remove the lock |
| 320 | if (lockTable.erase(id) != 0U) |
| 321 | { |
| 322 | BMCWEB_LOG_DEBUG << "Removing the locks with transaction ID : " |
| 323 | << id; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | BMCWEB_LOG_ERROR << "Removing the locks from the lock table " |
| 328 | "failed, transaction ID: " |
| 329 | << id; |
| 330 | } |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 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 Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 341 | inline bool Lock::validateRids(const ListOfTransactionIds& refRids) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 342 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 343 | for (const auto& id : refRids) |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 344 | { |
| 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 | { |
Sunitha Harish | 3e7ab70 | 2022-08-08 02:08:39 -0500 | [diff] [blame] | 354 | BMCWEB_LOG_ERROR << "validateRids: At least 1 inValid Request id: " |
| 355 | << id; |
manojkiraneda | 3b6dea6 | 2019-12-13 17:05:36 +0530 | [diff] [blame] | 356 | return false; |
| 357 | } |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 362 | inline bool Lock::isValidLockRequest(const LockRequest& refLockRecord) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 363 | { |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 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 Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 381 | BMCWEB_LOG_DEBUG << "Validation of Number of Segments Failed"; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 382 | BMCWEB_LOG_DEBUG << "Number of Segments provied : " |
Ed Tanous | 7cd94e4 | 2020-09-29 16:03:02 -0700 | [diff] [blame] | 383 | << std::get<4>(refLockRecord).size(); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
| 387 | int lockFlag = 0; |
| 388 | // validate the lockflags & segment length |
| 389 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 390 | for (const auto& p : std::get<4>(refLockRecord)) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 391 | { |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 392 | // validate the lock flags |
| 393 | // Allowed lockflags are locksame,lockall & dontlock |
| 394 | |
| 395 | if (!((boost::equals(p.first, "LockSame") || |
| 396 | (boost::equals(p.first, "LockAll")) || |
| 397 | (boost::equals(p.first, "DontLock"))))) |
| 398 | { |
| 399 | BMCWEB_LOG_DEBUG << "Validation of lock flags failed"; |
| 400 | BMCWEB_LOG_DEBUG << p.first; |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | // validate the segment length |
| 405 | // Allowed values of segment length are between 1 and 4 |
| 406 | |
| 407 | if (p.second < 1 || p.second > 4) |
| 408 | { |
| 409 | BMCWEB_LOG_DEBUG << "Validation of Segment Length Failed"; |
| 410 | BMCWEB_LOG_DEBUG << p.second; |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if ((boost::equals(p.first, "LockSame") || |
| 415 | (boost::equals(p.first, "LockAll")))) |
| 416 | { |
| 417 | ++lockFlag; |
| 418 | if (lockFlag >= 2) |
| 419 | { |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 428 | inline Rc Lock::isConflictWithTable(const LockRequests& refLockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 429 | { |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 430 | if (lockTable.empty()) |
| 431 | { |
Ed Tanous | f8fe53e | 2022-06-30 15:55:45 -0700 | [diff] [blame] | 432 | uint32_t thisTransactionId = generateTransactionId(); |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 433 | BMCWEB_LOG_DEBUG << thisTransactionId; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 434 | // Lock table is empty, so we are safe to add the lockrecords |
| 435 | // as there will be no conflict |
| 436 | BMCWEB_LOG_DEBUG << "Lock table is empty, so adding the lockrecords"; |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 437 | lockTable.emplace(thisTransactionId, refLockRequestStructure); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 438 | |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 439 | return std::make_pair(false, thisTransactionId); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 440 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 441 | BMCWEB_LOG_DEBUG |
| 442 | << "Lock table is not empty, check for conflict with lock table"; |
| 443 | // Lock table is not empty, compare the lockrequest entries with |
| 444 | // the entries in the lock table |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 445 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 446 | for (const auto& lockRecord1 : refLockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 447 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 448 | for (const auto& map : lockTable) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 449 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 450 | for (const auto& lockRecord2 : map.second) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 451 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 452 | bool status = isConflictRecord(lockRecord1, lockRecord2); |
| 453 | if (status) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 454 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 455 | return std::make_pair( |
| 456 | true, std::make_pair(map.first, lockRecord2)); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 460 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 461 | |
| 462 | // Reached here, so no conflict with the locktable, so we are safe to |
| 463 | // add the request records into the lock table |
| 464 | |
| 465 | // Lock table is empty, so we are safe to add the lockrecords |
| 466 | // as there will be no conflict |
| 467 | BMCWEB_LOG_DEBUG << " Adding elements into lock table"; |
| 468 | transactionId = generateTransactionId(); |
| 469 | lockTable.emplace(std::make_pair(transactionId, refLockRequestStructure)); |
| 470 | |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 471 | return std::make_pair(false, transactionId); |
| 472 | } |
| 473 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 474 | inline bool Lock::isConflictRequest(const LockRequests& refLockRequestStructure) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 475 | { |
| 476 | // check for all the locks coming in as a part of single request |
| 477 | // return conflict if any two lock requests are conflicting |
| 478 | |
| 479 | if (refLockRequestStructure.size() == 1) |
| 480 | { |
| 481 | BMCWEB_LOG_DEBUG << "Only single lock request, so there is no conflict"; |
| 482 | // This means , we have only one lock request in the current |
| 483 | // request , so no conflict within the request |
| 484 | return false; |
| 485 | } |
| 486 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 487 | BMCWEB_LOG_DEBUG |
| 488 | << "There are multiple lock requests coming in a single request"; |
| 489 | |
| 490 | // There are multiple requests a part of one request |
| 491 | |
| 492 | for (uint32_t i = 0; i < refLockRequestStructure.size(); i++) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 493 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 494 | for (uint32_t j = i + 1; j < refLockRequestStructure.size(); j++) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 495 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 496 | const LockRequest& p = refLockRequestStructure[i]; |
| 497 | const LockRequest& q = refLockRequestStructure[j]; |
| 498 | bool status = isConflictRecord(p, q); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 499 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 500 | if (status) |
| 501 | { |
| 502 | return true; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // This function converts the provided uint64_t resource id's from the two |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 510 | // lock requests subjected for comparison, and this function also compares |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 511 | // the content by bytes mentioned by a uint32_t number. |
| 512 | |
| 513 | // If all the elements in the lock requests which are subjected for comparison |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 514 | // are same, then the last comparison would be to check for the respective |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 515 | // bytes in the resourceid based on the segment length. |
| 516 | |
Patrick Williams | 26b3630 | 2023-05-10 17:29:00 -0500 | [diff] [blame^] | 517 | inline bool Lock::checkByte(uint64_t /*resourceId1*/, uint64_t /*resourceId2*/, |
| 518 | uint32_t /*position*/) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 519 | { |
Patrick Williams | 26b3630 | 2023-05-10 17:29:00 -0500 | [diff] [blame^] | 520 | BMCWEB_LOG_ERROR |
| 521 | << "This code is disabled due to clang-tidy issues that prevent CI " |
| 522 | "from passing."; |
| 523 | std::terminate(); |
| 524 | |
| 525 | #if 0 |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 526 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 527 | uint8_t* p = reinterpret_cast<uint8_t*>(&resourceId1); |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 528 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 529 | uint8_t* q = reinterpret_cast<uint8_t*>(&resourceId2); |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 530 | |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 531 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 532 | uint8_t pPosition = p[position]; |
| 533 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
| 534 | uint8_t qPosition = q[position]; |
| 535 | |
| 536 | BMCWEB_LOG_DEBUG << "Comparing bytes " << std::to_string(pPosition) << "," |
| 537 | << std::to_string(qPosition); |
Ed Tanous | dcf2ebc | 2022-01-25 10:07:45 -0800 | [diff] [blame] | 538 | return pPosition == qPosition; |
Patrick Williams | 26b3630 | 2023-05-10 17:29:00 -0500 | [diff] [blame^] | 539 | #endif |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 540 | } |
| 541 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 542 | inline bool Lock::isConflictRecord(const LockRequest& refLockRecord1, |
| 543 | const LockRequest& refLockRecord2) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 544 | { |
| 545 | // No conflict if both are read locks |
| 546 | |
| 547 | if (boost::equals(std::get<2>(refLockRecord1), "Read") && |
| 548 | boost::equals(std::get<2>(refLockRecord2), "Read")) |
| 549 | { |
| 550 | BMCWEB_LOG_DEBUG << "Both are read locks, no conflict"; |
| 551 | return false; |
| 552 | } |
| 553 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 554 | uint32_t i = 0; |
| 555 | for (const auto& p : std::get<4>(refLockRecord1)) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 556 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 557 | // return conflict when any of them is try to lock all resources |
| 558 | // under the current resource level. |
| 559 | if (boost::equals(p.first, "LockAll") || |
| 560 | boost::equals(std::get<4>(refLockRecord2)[i].first, "LockAll")) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 561 | { |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 562 | BMCWEB_LOG_DEBUG |
| 563 | << "Either of the Comparing locks are trying to lock all " |
| 564 | "resources under the current resource level"; |
| 565 | return true; |
| 566 | } |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 567 | |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 568 | // determine if there is a lock-all-with-same-segment-size. |
| 569 | // If the current segment sizes are the same,then we should fail. |
| 570 | |
| 571 | if ((boost::equals(p.first, "LockSame") || |
| 572 | boost::equals(std::get<4>(refLockRecord2)[i].first, "LockSame")) && |
| 573 | (p.second == std::get<4>(refLockRecord2)[i].second)) |
| 574 | { |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | // if segment lengths are not the same, it means two different locks |
| 579 | // So no conflict |
| 580 | if (p.second != std::get<4>(refLockRecord2)[i].second) |
| 581 | { |
| 582 | BMCWEB_LOG_DEBUG << "Segment lengths are not same"; |
| 583 | BMCWEB_LOG_DEBUG << "Segment 1 length : " << p.second; |
| 584 | BMCWEB_LOG_DEBUG << "Segment 2 length : " |
| 585 | << std::get<4>(refLockRecord2)[i].second; |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | // compare segment data |
| 590 | |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 591 | for (uint32_t j = 0; j < p.second; j++) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 592 | { |
Ali Ahmed | d3d26ba | 2021-04-30 09:13:53 -0500 | [diff] [blame] | 593 | // if the segment data is different, then the locks is on a |
| 594 | // different resource so no conflict between the lock |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 595 | // records. |
Ali Ahmed | d3d26ba | 2021-04-30 09:13:53 -0500 | [diff] [blame] | 596 | // BMC is little endian, but the resourceID is formed by |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 597 | // the Management Console in such a way that, the first byte |
| 598 | // from the MSB Position corresponds to the First Segment |
Ali Ahmed | d3d26ba | 2021-04-30 09:13:53 -0500 | [diff] [blame] | 599 | // data. Therefore we need to convert the incoming |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 600 | // resourceID into Big Endian before processing further. |
| 601 | if (!(checkByte( |
| 602 | boost::endian::endian_reverse(std::get<3>(refLockRecord1)), |
| 603 | boost::endian::endian_reverse(std::get<3>(refLockRecord2)), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 604 | j))) |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 605 | { |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 606 | return false; |
| 607 | } |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 608 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 609 | |
| 610 | ++i; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 611 | } |
| 612 | |
Sunitha Harish | 41bb02b | 2022-08-08 03:12:15 -0500 | [diff] [blame] | 613 | return true; |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 614 | } |
| 615 | |
Ratan Gupta | 07386c6 | 2019-12-14 14:06:09 +0530 | [diff] [blame] | 616 | inline uint32_t Lock::generateTransactionId() |
manojkiraneda | 0b631ae | 2019-12-03 17:54:28 +0530 | [diff] [blame] | 617 | { |
| 618 | ++transactionId; |
| 619 | return transactionId; |
| 620 | } |
| 621 | |
| 622 | } // namespace ibm_mc_lock |
| 623 | } // namespace crow |