CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_transport.h
Go to the documentation of this file.
1 
4 #ifndef CVMFS_CACHE_TRANSPORT_H_
5 #define CVMFS_CACHE_TRANSPORT_H_
6 
7 #include <stdint.h>
8 
9 #include <cstdlib>
10 
11 #include "cache.h"
12 #include "cache.pb.h"
13 #include "util/single_copy.h"
14 
15 namespace shash {
16 struct Any;
17 }
18 
19 inline const char *CacheTransportCode2Ascii(const cvmfs::EnumStatus code) {
20  switch (code) {
21  case cvmfs::STATUS_UNKNOWN: return "unknown cache protocol error";
22  case cvmfs::STATUS_OK: return "OK";
23  case cvmfs::STATUS_NOSUPPORT:
24  return "operation not implemented by cache plugin";
25  case cvmfs::STATUS_FORBIDDEN: return "cache plugin denied the operation";
26  case cvmfs::STATUS_NOSPACE: return "no space in cache";
27  case cvmfs::STATUS_NOENTRY: return "object not found in cache";
28  case cvmfs::STATUS_MALFORMED: return "malformed cache protocol message";
29  case cvmfs::STATUS_IOERR: return "I/O error";
30  case cvmfs::STATUS_CORRUPTED: return "corrupted data detected";
31  case cvmfs::STATUS_TIMEOUT: return "multipart request timed out";
32  case cvmfs::STATUS_BADCOUNT:
33  return "invalid attempt to set negative reference count";
34  case cvmfs::STATUS_OUTOFBOUNDS: return "out of bounds";
35  case cvmfs::STATUS_PARTIAL:
36  return "cache could not be cleaned up to the given limit";
37  default: return "unexpected cache protocol error";
38  }
39 }
40 
46  public:
51  static const char *kEnvReadyNotifyFd; // __CVMFS_CACHE_EXTERNAL_PIPE_READY__
52  static const char kReadyNotification = 'C';
53  static const char kFailureNotification = 'F';
58  static const unsigned char kWireProtocolVersion = 0x01;
64  static const unsigned char kFlagHasAttachment = 0x80;
68  static const uint32_t kMaxMsgSize = (2 << 24) - 1; // 24MB (3 bytes)
74  static const unsigned kHeaderSize = 4;
80  static const unsigned kInnerHeaderSize = 2;
81 
82  static const uint32_t kFlagSendIgnoreFailure = 0x01;
83  static const uint32_t kFlagSendNonBlocking = 0x02;
84 
85 
94  class Frame : SingleCopy {
95  public:
96  Frame();
97  explicit Frame(google::protobuf::MessageLite *m);
98  ~Frame();
99  void MergeFrom(const Frame &other);
100  void Reset(uint32_t original_att_size);
101 
102  void *attachment() const { return attachment_; }
103  uint32_t att_size() const { return att_size_; }
104  void set_att_size(uint32_t size) { att_size_ = size; }
105  void set_attachment(void *attachment, uint32_t att_size) {
108  }
109 
110  bool ParseMsgRpc(void *buffer, uint32_t size);
111  cvmfs::MsgRpc *GetMsgRpc();
112  google::protobuf::MessageLite *GetMsgTyped();
113  // The Detach message does not follow the command-reply pattern
114  bool IsMsgOutOfBand();
115 
116  private:
117  void WrapMsg();
118  void UnwrapMsg();
119  void Release();
120 
121  cvmfs::MsgRpc msg_rpc_;
127  google::protobuf::MessageLite *msg_typed_;
128  void *attachment_;
129  uint32_t att_size_;
132  }; // class CacheTransport::Frame
133 
134 
135  explicit CacheTransport(int fd_connection);
136  CacheTransport(int fd_connection, uint32_t flags);
138 
139  void SendFrame(Frame *frame);
140  bool RecvFrame(Frame *frame);
141 
142  void FillMsgHash(const shash::Any &hash, cvmfs::MsgHash *msg_hash);
143  bool ParseMsgHash(const cvmfs::MsgHash &msg_hash, shash::Any *hash);
144  void FillObjectType(CacheManager::ObjectType object_type,
145  cvmfs::EnumObjectType *wire_type);
146  bool ParseObjectType(cvmfs::EnumObjectType wire_type,
147  CacheManager::ObjectType *object_type);
148 
149  int fd_connection() const { return fd_connection_; }
150 
151  private:
152  static const unsigned kMaxStackAlloc = 256 * 1024; // 256 kB
153 
154  void SendData(void *message,
155  uint32_t msg_size,
156  void *attachment = NULL,
157  uint32_t att_size = 0);
158  void SendNonBlocking(struct iovec *iov, unsigned iovcnt);
159  bool RecvHeader(uint32_t *size, bool *has_attachment);
160 
162  uint32_t flags_;
163 }; // class CacheTransport
164 
165 #endif // CVMFS_CACHE_TRANSPORT_H_
static const unsigned kMaxStackAlloc
static const unsigned char kWireProtocolVersion
void SendNonBlocking(struct iovec *iov, unsigned iovcnt)
void SendFrame(Frame *frame)
static const char kReadyNotification
static const uint32_t kFlagSendIgnoreFailure
void FillMsgHash(const shash::Any &hash, cvmfs::MsgHash *msg_hash)
void MergeFrom(const Frame &other)
static const uint32_t kMaxMsgSize
void * attachment() const
bool ParseMsgHash(const cvmfs::MsgHash &msg_hash, shash::Any *hash)
cvmfs::MsgRpc * GetMsgRpc()
uint32_t att_size() const
void set_att_size(uint32_t size)
google::protobuf::MessageLite * msg_typed_
static const unsigned char kFlagHasAttachment
google::protobuf::MessageLite * GetMsgTyped()
void SendData(void *message, uint32_t msg_size, void *attachment=NULL, uint32_t att_size=0)
static const char kFailureNotification
bool ParseMsgRpc(void *buffer, uint32_t size)
bool ParseObjectType(cvmfs::EnumObjectType wire_type, CacheManager::ObjectType *object_type)
bool RecvFrame(Frame *frame)
void set_attachment(void *attachment, uint32_t att_size)
static const unsigned kInnerHeaderSize
void Reset(uint32_t original_att_size)
static void size_t size
Definition: smalloc.h:47
void FillObjectType(CacheManager::ObjectType object_type, cvmfs::EnumObjectType *wire_type)
const char * CacheTransportCode2Ascii(const cvmfs::EnumStatus code)
static const unsigned kHeaderSize
int fd_connection() const
static const char * kEnvReadyNotifyFd
static const uint32_t kFlagSendNonBlocking
CacheTransport(int fd_connection)
bool RecvHeader(uint32_t *size, bool *has_attachment)