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