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