]> git.proxmox.com Git - ceph.git/blob - ceph/src/librbd/crypto/CryptoContextPool.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / librbd / crypto / CryptoContextPool.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_LIBRBD_CRYPTO_CRYPTO_CONTEXT_POOL_H
5 #define CEPH_LIBRBD_CRYPTO_CRYPTO_CONTEXT_POOL_H
6
7 #include "librbd/crypto/DataCryptor.h"
8 #include "include/ceph_assert.h"
9 #include <boost/lockfree/queue.hpp>
10
11 namespace librbd {
12 namespace crypto {
13
14 template <typename T>
15 class CryptoContextPool : public DataCryptor<T> {
16
17 public:
18 CryptoContextPool(DataCryptor<T>* data_cryptor, uint32_t pool_size);
19 ~CryptoContextPool();
20
21 T* get_context(CipherMode mode) override;
22 void return_context(T* ctx, CipherMode mode) override;
23
24 inline uint32_t get_block_size() const override {
25 return m_data_cryptor->get_block_size();
26 }
27 inline uint32_t get_iv_size() const override {
28 return m_data_cryptor->get_iv_size();
29 }
30 inline int get_key_length() const override {
31 return m_data_cryptor->get_key_length();
32 }
33 inline const unsigned char* get_key() const override {
34 return m_data_cryptor->get_key();
35 }
36 inline int init_context(T* ctx, const unsigned char* iv,
37 uint32_t iv_length) const override {
38 return m_data_cryptor->init_context(ctx, iv, iv_length);
39 }
40 inline int update_context(T* ctx, const unsigned char* in,
41 unsigned char* out,
42 uint32_t len) const override {
43 return m_data_cryptor->update_context(ctx, in, out, len);
44 }
45
46 using ContextQueue = boost::lockfree::queue<T*>;
47
48 private:
49 DataCryptor<T>* m_data_cryptor;
50 ContextQueue m_encrypt_contexts;
51 ContextQueue m_decrypt_contexts;
52
53 inline ContextQueue& get_contexts(CipherMode mode) {
54 switch(mode) {
55 case CIPHER_MODE_ENC:
56 return m_encrypt_contexts;
57 case CIPHER_MODE_DEC:
58 return m_decrypt_contexts;
59 default:
60 ceph_assert(false);
61 }
62 }
63 };
64
65 } // namespace crypto
66 } // namespace librbd
67
68 #endif // CEPH_LIBRBD_CRYPTO_CRYPTO_CONTEXT_POOL_H