Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Patrick Venture | cd8dab4 | 2019-01-15 19:57:38 -0800 | [diff] [blame] | 17 | #include "manager.hpp" |
| 18 | |
Patrick Venture | e50d4e4 | 2018-10-23 09:50:27 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 20 | #include <iostream> |
Patrick Venture | b3e07e2 | 2018-09-27 15:11:57 -0700 | [diff] [blame] | 21 | #include <memory> |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace blobs |
| 26 | { |
| 27 | |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 28 | void BlobManager::eraseSession(GenericBlobInterface* handler, uint16_t session) |
| 29 | { |
Kun Yi | beee9e5 | 2019-11-22 20:01:28 -0800 | [diff] [blame^] | 30 | if (auto item = sessions.find(session); item != sessions.end()) |
Kun Yi | 9ce8348 | 2019-11-14 13:08:39 -0800 | [diff] [blame] | 31 | { |
Kun Yi | beee9e5 | 2019-11-22 20:01:28 -0800 | [diff] [blame^] | 32 | const auto& blobId = item->second.blobId; |
| 33 | |
| 34 | /* Ok for openSessions[handler] to be an empty set */ |
| 35 | openSessions[handler].erase(session); |
| 36 | openFiles[blobId]--; |
| 37 | if (openFiles[blobId] == 0) |
| 38 | { |
| 39 | openFiles.erase(blobId); |
| 40 | } |
| 41 | |
| 42 | /* Erase at the end after using the session info */ |
| 43 | sessions.erase(session); |
Kun Yi | 9ce8348 | 2019-11-14 13:08:39 -0800 | [diff] [blame] | 44 | } |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void BlobManager::cleanUpStaleSessions(GenericBlobInterface* handler) |
| 48 | { |
| 49 | if (openSessions.count(handler) == 0) |
| 50 | { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | auto timeNow = std::chrono::steady_clock::now(); |
| 55 | std::set<uint16_t> expiredSet; |
| 56 | |
| 57 | for (auto sessionId : openSessions[handler]) |
| 58 | { |
| 59 | if (timeNow - sessions[sessionId].lastActionTime >= sessionTimeout) |
| 60 | { |
| 61 | expiredSet.insert(sessionId); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | for (auto sessionId : expiredSet) |
| 66 | { |
| 67 | std::cerr << "phosphor-ipmi-blobs: expiring stale session " << sessionId |
| 68 | << std::endl; |
| 69 | |
| 70 | /* We do a best case recovery by issuing an expire call. If it fails |
| 71 | * don't erase sessions since the handler side might be still tracking |
| 72 | * it as open. */ |
| 73 | if (handler->expire(sessionId)) |
| 74 | { |
| 75 | eraseSession(handler, sessionId); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | std::cerr << "phosphor-ipmi-blobs: failed to expire session " |
| 80 | << sessionId << std::endl; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 85 | bool BlobManager::registerHandler(std::unique_ptr<GenericBlobInterface> handler) |
| 86 | { |
| 87 | if (!handler) |
| 88 | { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | handlers.push_back(std::move(handler)); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | uint32_t BlobManager::buildBlobList() |
| 97 | { |
| 98 | /* Clear out the current list (IPMI handler is presently single-threaded). |
| 99 | */ |
| 100 | ids.clear(); |
| 101 | |
| 102 | /* Grab the list of blobs and extend the local list */ |
Patrick Venture | a6e21a0 | 2018-10-23 09:45:04 -0700 | [diff] [blame] | 103 | for (const auto& h : handlers) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 104 | { |
| 105 | std::vector<std::string> blobs = h->getBlobIds(); |
| 106 | ids.insert(ids.end(), blobs.begin(), blobs.end()); |
| 107 | } |
| 108 | |
| 109 | return ids.size(); |
| 110 | } |
| 111 | |
| 112 | std::string BlobManager::getBlobId(uint32_t index) |
| 113 | { |
| 114 | /* Range check. */ |
| 115 | if (index >= ids.size()) |
| 116 | { |
| 117 | return ""; |
| 118 | } |
| 119 | |
| 120 | return ids[index]; |
| 121 | } |
| 122 | |
| 123 | bool BlobManager::open(uint16_t flags, const std::string& path, |
| 124 | uint16_t* session) |
| 125 | { |
| 126 | GenericBlobInterface* handler = getHandler(path); |
| 127 | |
| 128 | /* No handler found. */ |
| 129 | if (!handler) |
| 130 | { |
| 131 | return false; |
| 132 | } |
| 133 | |
Patrick Venture | 2f58151 | 2019-01-10 09:30:36 -0800 | [diff] [blame] | 134 | /* No sessions available... */ |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 135 | if (!getSession(session)) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /* Verify flags - must be at least read or write */ |
| 141 | if (!(flags & (OpenFlags::read | OpenFlags::write))) |
| 142 | { |
| 143 | /* Neither read not write set, which means calls to Read/Write will |
| 144 | * reject. */ |
| 145 | return false; |
| 146 | } |
| 147 | |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 148 | /* Try to clean up anything that's falling out of cleanup timeout for this |
| 149 | * handler */ |
| 150 | cleanUpStaleSessions(handler); |
| 151 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 152 | if (!handler->open(*session, flags, path)) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | /* Associate session with handler */ |
| 158 | sessions[*session] = SessionInfo(path, handler, flags); |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 159 | openSessions[handler].insert(*session); |
Kun Yi | 9ce8348 | 2019-11-14 13:08:39 -0800 | [diff] [blame] | 160 | openFiles[path]++; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | GenericBlobInterface* BlobManager::getHandler(const std::string& path) |
| 165 | { |
| 166 | /* Find a handler. */ |
Patrick Venture | e50d4e4 | 2018-10-23 09:50:27 -0700 | [diff] [blame] | 167 | auto h = std::find_if( |
| 168 | handlers.begin(), handlers.end(), |
| 169 | [&path](const auto& iter) { return (iter->canHandleBlob(path)); }); |
| 170 | if (h != handlers.end()) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 171 | { |
Patrick Venture | e50d4e4 | 2018-10-23 09:50:27 -0700 | [diff] [blame] | 172 | return h->get(); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Patrick Venture | e50d4e4 | 2018-10-23 09:50:27 -0700 | [diff] [blame] | 175 | return nullptr; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Kun Yi | fea1d81 | 2019-11-14 11:38:44 -0800 | [diff] [blame] | 178 | GenericBlobInterface* BlobManager::getActionHandle(uint16_t session, |
| 179 | uint16_t requiredFlags) |
| 180 | { |
| 181 | if (auto item = sessions.find(session); |
| 182 | item != sessions.end() && (item->second.flags & requiredFlags)) |
| 183 | { |
| 184 | item->second.lastActionTime = std::chrono::steady_clock::now(); |
| 185 | return item->second.handler; |
| 186 | } |
| 187 | return nullptr; |
| 188 | } |
| 189 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 190 | bool BlobManager::stat(const std::string& path, BlobMeta* meta) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 191 | { |
| 192 | /* meta should never be NULL. */ |
| 193 | GenericBlobInterface* handler = getHandler(path); |
| 194 | |
| 195 | /* No handler found. */ |
| 196 | if (!handler) |
| 197 | { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return handler->stat(path, meta); |
| 202 | } |
| 203 | |
Patrick Venture | 8bc1177 | 2019-06-04 07:20:24 -0700 | [diff] [blame] | 204 | bool BlobManager::stat(uint16_t session, BlobMeta* meta) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 205 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 206 | if (auto handler = getActionHandle(session)) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 207 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 208 | return handler->stat(session, meta); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 209 | } |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 210 | return false; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool BlobManager::commit(uint16_t session, const std::vector<uint8_t>& data) |
| 214 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 215 | if (auto handler = getActionHandle(session)) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 216 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 217 | return handler->commit(session, data); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 218 | } |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 219 | return false; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool BlobManager::close(uint16_t session) |
| 223 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 224 | if (auto handler = getActionHandle(session)) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 225 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 226 | if (!handler->close(session)) |
| 227 | { |
| 228 | return false; |
| 229 | } |
Kun Yi | b61a88b | 2019-11-12 22:50:34 -0800 | [diff] [blame] | 230 | eraseSession(handler, session); |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 231 | return true; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 232 | } |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 233 | return false; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | std::vector<uint8_t> BlobManager::read(uint16_t session, uint32_t offset, |
| 237 | uint32_t requestedSize) |
| 238 | { |
Patrick Venture | 86c87f5 | 2019-02-01 14:44:15 -0800 | [diff] [blame] | 239 | /* TODO: Currently, configure_ac isn't finding libuserlayer, w.r.t the |
| 240 | * symbols I need. |
| 241 | */ |
| 242 | |
| 243 | /** The channel to use for now. |
| 244 | * TODO: We will receive this information through the IPMI message call. |
| 245 | */ |
| 246 | // const int ipmiChannel = ipmi::currentChNum; |
| 247 | /** This information is transport specific. |
| 248 | * TODO: We need a way to know this dynamically. |
| 249 | * on BT, 4 bytes of header, and 1 reply code. |
| 250 | */ |
| 251 | // uint32_t maxTransportSize = ipmi::getChannelMaxTransferSize(ipmiChannel); |
| 252 | |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 253 | if (auto handler = getActionHandle(session, OpenFlags::read)) |
| 254 | { |
| 255 | return handler->read(session, offset, |
| 256 | std::min(maximumReadSize, requestedSize)); |
| 257 | } |
| 258 | return {}; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | bool BlobManager::write(uint16_t session, uint32_t offset, |
| 262 | const std::vector<uint8_t>& data) |
| 263 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 264 | if (auto handler = getActionHandle(session, OpenFlags::write)) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 265 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 266 | return handler->write(session, offset, data); |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 267 | } |
Kun Yi | fea1d81 | 2019-11-14 11:38:44 -0800 | [diff] [blame] | 268 | return false; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool BlobManager::deleteBlob(const std::string& path) |
| 272 | { |
| 273 | GenericBlobInterface* handler = getHandler(path); |
| 274 | |
| 275 | /* No handler found. */ |
| 276 | if (!handler) |
| 277 | { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | /* Check if the file has any open handles. */ |
Kun Yi | 9ce8348 | 2019-11-14 13:08:39 -0800 | [diff] [blame] | 282 | if (openFiles[path] > 0) |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 283 | { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | /* Try deleting it. */ |
| 288 | return handler->deleteBlob(path); |
| 289 | } |
| 290 | |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 291 | bool BlobManager::writeMeta(uint16_t session, uint32_t offset, |
| 292 | const std::vector<uint8_t>& data) |
| 293 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 294 | if (auto handler = getActionHandle(session)) |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 295 | { |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 296 | return handler->writeMeta(session, offset, data); |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 297 | } |
Kun Yi | c892f4a | 2019-11-19 13:42:46 -0800 | [diff] [blame] | 298 | return false; |
Patrick Venture | 5c4b17b | 2018-10-04 10:32:22 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 301 | bool BlobManager::getSession(uint16_t* sess) |
| 302 | { |
| 303 | uint16_t tries = 0; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 304 | |
| 305 | if (!sess) |
| 306 | { |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /* This is not meant to fail as you have 64KiB values available. */ |
| 311 | |
| 312 | /* TODO(venture): We could just count the keys in the session map to know |
| 313 | * if it's full. |
| 314 | */ |
| 315 | do |
| 316 | { |
Patrick Venture | c9ad5ff | 2018-10-12 17:05:49 -0700 | [diff] [blame] | 317 | uint16_t lsess = next++; |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 318 | if (!sessions.count(lsess)) |
| 319 | { |
| 320 | /* value not in use, return it. */ |
| 321 | (*sess) = lsess; |
| 322 | return true; |
| 323 | } |
| 324 | } while (++tries < 0xffff); |
| 325 | |
| 326 | return false; |
| 327 | } |
Patrick Venture | b3e07e2 | 2018-09-27 15:11:57 -0700 | [diff] [blame] | 328 | |
| 329 | static std::unique_ptr<BlobManager> manager; |
| 330 | |
Patrick Venture | 73eb687 | 2018-10-01 18:37:34 -0700 | [diff] [blame] | 331 | ManagerInterface* getBlobManager() |
Patrick Venture | b3e07e2 | 2018-09-27 15:11:57 -0700 | [diff] [blame] | 332 | { |
| 333 | if (manager == nullptr) |
| 334 | { |
| 335 | manager = std::make_unique<BlobManager>(); |
| 336 | } |
| 337 | |
| 338 | return manager.get(); |
| 339 | } |
| 340 | |
Patrick Venture | ef3aead | 2018-09-12 08:53:29 -0700 | [diff] [blame] | 341 | } // namespace blobs |