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