Directory: | cvmfs/ |
---|---|
File: | cvmfs/cache_transport.cc |
Date: | 2025-07-06 02:35:01 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 326 | 372 | 87.6% |
Branches: | 166 | 228 | 72.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | #include "cache_transport.h" | ||
6 | |||
7 | #include <alloca.h> | ||
8 | #include <errno.h> | ||
9 | #include <sys/socket.h> | ||
10 | |||
11 | #include <cassert> | ||
12 | #include <cstdlib> | ||
13 | #include <cstring> | ||
14 | |||
15 | #include "crypto/hash.h" | ||
16 | #include "util/exception.h" | ||
17 | #include "util/logging.h" | ||
18 | #include "util/posix.h" | ||
19 | #include "util/smalloc.h" | ||
20 | |||
21 | // TODO(jblomer): Check for possible starvation of plugin by dying clients | ||
22 | // (blocking read). Probably only relevant for TCP sockets. | ||
23 | |||
24 | using namespace std; // NOLINT | ||
25 | |||
26 | const char | ||
27 | *CacheTransport::kEnvReadyNotifyFd = "__CVMFS_CACHE_EXTERNAL_PIPE_READY__"; | ||
28 | |||
29 | /** | ||
30 | * Called on the sender side to wrap a message into a MsgRpc message for wire | ||
31 | * transfer. | ||
32 | */ | ||
33 | 61013 | cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() { | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61013 times.
|
61013 | assert(msg_typed_ != NULL); |
35 |
1/2✓ Branch 0 taken 61013 times.
✗ Branch 1 not taken.
|
61013 | if (!is_wrapped_) |
36 | 61013 | WrapMsg(); | |
37 | 61013 | return &msg_rpc_; | |
38 | } | ||
39 | |||
40 | |||
41 | /** | ||
42 | * Called on the receiving end of an RPC to extract the actual message from the | ||
43 | * MsgRpc. | ||
44 | */ | ||
45 | 75157 | google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() { | |
46 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 75171 times.
|
75157 | assert(msg_rpc_.IsInitialized()); |
47 |
2/2✓ Branch 0 taken 59750 times.
✓ Branch 1 taken 15421 times.
|
75171 | if (msg_typed_ == NULL) |
48 | 59750 | UnwrapMsg(); | |
49 | 75150 | return msg_typed_; | |
50 | } | ||
51 | |||
52 | |||
53 | 75171 | CacheTransport::Frame::Frame() | |
54 | 75171 | : owns_msg_typed_(false) | |
55 | 75171 | , msg_typed_(NULL) | |
56 | 75171 | , attachment_(NULL) | |
57 | 75171 | , att_size_(0) | |
58 | 75171 | , is_wrapped_(false) | |
59 | 75171 | , is_msg_out_of_band_(false) { } | |
60 | |||
61 | |||
62 | 60999 | CacheTransport::Frame::Frame(google::protobuf::MessageLite *m) | |
63 | 60999 | : owns_msg_typed_(false) | |
64 | 60999 | , msg_typed_(m) | |
65 | 60999 | , attachment_(NULL) | |
66 | 60999 | , att_size_(0) | |
67 | 60999 | , is_wrapped_(false) | |
68 | 60999 | , is_msg_out_of_band_(false) { } | |
69 | |||
70 | |||
71 | 136177 | CacheTransport::Frame::~Frame() { Reset(0); } | |
72 | |||
73 | |||
74 | 15421 | bool CacheTransport::Frame::IsMsgOutOfBand() { | |
75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15421 times.
|
15421 | assert(msg_rpc_.IsInitialized()); |
76 |
1/2✓ Branch 0 taken 15421 times.
✗ Branch 1 not taken.
|
15421 | if (msg_typed_ == NULL) |
77 | 15421 | UnwrapMsg(); | |
78 | 15421 | return is_msg_out_of_band_; | |
79 | } | ||
80 | |||
81 | |||
82 | 14196 | void CacheTransport::Frame::MergeFrom(const Frame &other) { | |
83 | 14196 | msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_); | |
84 | 14196 | owns_msg_typed_ = true; | |
85 |
2/2✓ Branch 0 taken 12600 times.
✓ Branch 1 taken 1596 times.
|
14196 | if (other.att_size_ > 0) { |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12600 times.
|
12600 | assert(att_size_ >= other.att_size_); |
87 | 12600 | memcpy(attachment_, other.attachment_, other.att_size_); | |
88 | 12600 | att_size_ = other.att_size_; | |
89 | } | ||
90 | 14196 | } | |
91 | |||
92 | |||
93 | 60982 | bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) { | |
94 | 60982 | const bool retval = msg_rpc_.ParseFromArray(buffer, size); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60947 times.
|
60947 | if (!retval) |
96 | ✗ | return false; | |
97 | |||
98 | // Cleanup typed message when Frame leaves scope | ||
99 | 60947 | owns_msg_typed_ = true; | |
100 | 60947 | return true; | |
101 | } | ||
102 | |||
103 | |||
104 | 136184 | void CacheTransport::Frame::Release() { | |
105 |
2/2✓ Branch 0 taken 75164 times.
✓ Branch 1 taken 61020 times.
|
136184 | if (owns_msg_typed_) |
106 | 75164 | return; | |
107 | |||
108 | 61020 | msg_rpc_.release_msg_refcount_req(); | |
109 | 61013 | msg_rpc_.release_msg_refcount_reply(); | |
110 | 61020 | msg_rpc_.release_msg_read_req(); | |
111 | 61020 | msg_rpc_.release_msg_read_reply(); | |
112 | 61020 | msg_rpc_.release_msg_object_info_req(); | |
113 | 61020 | msg_rpc_.release_msg_object_info_reply(); | |
114 | 61013 | msg_rpc_.release_msg_store_req(); | |
115 | 61013 | msg_rpc_.release_msg_store_abort_req(); | |
116 | 61013 | msg_rpc_.release_msg_store_reply(); | |
117 | 61020 | msg_rpc_.release_msg_handshake(); | |
118 | 61020 | msg_rpc_.release_msg_handshake_ack(); | |
119 | 61020 | msg_rpc_.release_msg_quit(); | |
120 | 61020 | msg_rpc_.release_msg_ioctl(); | |
121 | 61020 | msg_rpc_.release_msg_info_req(); | |
122 | 61020 | msg_rpc_.release_msg_info_reply(); | |
123 | 61020 | msg_rpc_.release_msg_shrink_req(); | |
124 | 61020 | msg_rpc_.release_msg_shrink_reply(); | |
125 | 61020 | msg_rpc_.release_msg_list_req(); | |
126 | 61020 | msg_rpc_.release_msg_list_reply(); | |
127 | 61020 | msg_rpc_.release_msg_detach(); | |
128 | 61020 | msg_rpc_.release_msg_breadcrumb_store_req(); | |
129 | 61020 | msg_rpc_.release_msg_breadcrumb_load_req(); | |
130 | 61013 | msg_rpc_.release_msg_breadcrumb_reply(); | |
131 | } | ||
132 | |||
133 | |||
134 | 136184 | void CacheTransport::Frame::Reset(uint32_t original_att_size) { | |
135 | 136184 | msg_typed_ = NULL; | |
136 | 136184 | att_size_ = original_att_size; | |
137 | 136184 | is_wrapped_ = false; | |
138 | 136184 | is_msg_out_of_band_ = false; | |
139 | 136184 | Release(); | |
140 | 136184 | msg_rpc_.Clear(); | |
141 | 136198 | owns_msg_typed_ = false; | |
142 | 136198 | } | |
143 | |||
144 | |||
145 | 61013 | void CacheTransport::Frame::WrapMsg() { | |
146 |
2/2✓ Branch 3 taken 152 times.
✓ Branch 4 taken 60861 times.
|
61013 | if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") { |
147 | 152 | msg_rpc_.set_allocated_msg_handshake( | |
148 | 152 | reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_)); | |
149 |
2/2✓ Branch 3 taken 126 times.
✓ Branch 4 taken 60735 times.
|
60861 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") { |
150 | 126 | msg_rpc_.set_allocated_msg_handshake_ack( | |
151 | 126 | reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_)); | |
152 |
2/2✓ Branch 3 taken 150 times.
✓ Branch 4 taken 60585 times.
|
60735 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") { |
153 | 150 | msg_rpc_.set_allocated_msg_quit( | |
154 | 150 | reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_)); | |
155 |
2/2✓ Branch 3 taken 28 times.
✓ Branch 4 taken 60557 times.
|
60585 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") { |
156 | 28 | msg_rpc_.set_allocated_msg_ioctl( | |
157 | 28 | reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_)); | |
158 |
2/2✓ Branch 3 taken 6452 times.
✓ Branch 4 taken 54105 times.
|
60557 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") { |
159 | 6452 | msg_rpc_.set_allocated_msg_refcount_req( | |
160 | 6452 | reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_)); | |
161 |
2/2✓ Branch 3 taken 3948 times.
✓ Branch 4 taken 50157 times.
|
54105 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") { |
162 | 3948 | msg_rpc_.set_allocated_msg_refcount_reply( | |
163 | 3948 | reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_)); | |
164 |
2/2✓ Branch 3 taken 136 times.
✓ Branch 4 taken 50021 times.
|
50157 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") { |
165 | 136 | msg_rpc_.set_allocated_msg_object_info_req( | |
166 | 136 | reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_)); | |
167 |
2/2✓ Branch 3 taken 112 times.
✓ Branch 4 taken 49909 times.
|
50021 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") { |
168 | 112 | msg_rpc_.set_allocated_msg_object_info_reply( | |
169 | 112 | reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_)); | |
170 |
2/2✓ Branch 3 taken 16166 times.
✓ Branch 4 taken 33743 times.
|
49909 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") { |
171 | 16166 | msg_rpc_.set_allocated_msg_read_req( | |
172 | 16166 | reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_)); | |
173 |
2/2✓ Branch 3 taken 15477 times.
✓ Branch 4 taken 18266 times.
|
33743 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") { |
174 | 15477 | msg_rpc_.set_allocated_msg_read_reply( | |
175 | 15477 | reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_)); | |
176 |
2/2✓ Branch 3 taken 6670 times.
✓ Branch 4 taken 11596 times.
|
18266 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") { |
177 | 6670 | msg_rpc_.set_allocated_msg_store_req( | |
178 | 6670 | reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_)); | |
179 |
2/2✓ Branch 3 taken 7 times.
✓ Branch 4 taken 11589 times.
|
11596 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") { |
180 | 7 | msg_rpc_.set_allocated_msg_store_abort_req( | |
181 | 7 | reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_)); | |
182 |
2/2✓ Branch 3 taken 4263 times.
✓ Branch 4 taken 7326 times.
|
11589 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") { |
183 | 4263 | msg_rpc_.set_allocated_msg_store_reply( | |
184 | 4263 | reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_)); | |
185 |
2/2✓ Branch 3 taken 52 times.
✓ Branch 4 taken 7274 times.
|
7326 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") { |
186 | 52 | msg_rpc_.set_allocated_msg_info_req( | |
187 | 52 | reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_)); | |
188 |
2/2✓ Branch 3 taken 28 times.
✓ Branch 4 taken 7246 times.
|
7274 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") { |
189 | 28 | msg_rpc_.set_allocated_msg_info_reply( | |
190 | 28 | reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_)); | |
191 |
2/2✓ Branch 3 taken 22 times.
✓ Branch 4 taken 7224 times.
|
7246 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") { |
192 | 22 | msg_rpc_.set_allocated_msg_shrink_req( | |
193 | 22 | reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_)); | |
194 |
2/2✓ Branch 3 taken 14 times.
✓ Branch 4 taken 7210 times.
|
7224 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") { |
195 | 14 | msg_rpc_.set_allocated_msg_shrink_reply( | |
196 | 14 | reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_)); | |
197 |
2/2✓ Branch 3 taken 78 times.
✓ Branch 4 taken 7132 times.
|
7210 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") { |
198 | 78 | msg_rpc_.set_allocated_msg_list_req( | |
199 | 78 | reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_)); | |
200 |
2/2✓ Branch 3 taken 70 times.
✓ Branch 4 taken 7062 times.
|
7132 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") { |
201 | 70 | msg_rpc_.set_allocated_msg_list_reply( | |
202 | 70 | reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_)); | |
203 |
2/2✓ Branch 3 taken 9 times.
✓ Branch 4 taken 7053 times.
|
7062 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") { |
204 | 9 | msg_rpc_.set_allocated_msg_breadcrumb_store_req( | |
205 | 9 | reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_)); | |
206 |
2/2✓ Branch 3 taken 18 times.
✓ Branch 4 taken 7035 times.
|
7053 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") { |
207 | 18 | msg_rpc_.set_allocated_msg_breadcrumb_load_req( | |
208 | 18 | reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_)); | |
209 |
2/2✓ Branch 3 taken 21 times.
✓ Branch 4 taken 7014 times.
|
7035 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") { |
210 | 21 | msg_rpc_.set_allocated_msg_breadcrumb_reply( | |
211 | 21 | reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_)); | |
212 |
1/2✓ Branch 3 taken 7014 times.
✗ Branch 4 not taken.
|
7014 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") { |
213 | 7014 | msg_rpc_.set_allocated_msg_detach( | |
214 | 7014 | reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_)); | |
215 | 7014 | is_msg_out_of_band_ = true; | |
216 | } else { | ||
217 | // Unexpected message type, should never happen | ||
218 | ✗ | PANIC(NULL); | |
219 | } | ||
220 | 61013 | is_wrapped_ = true; | |
221 | 61013 | } | |
222 | |||
223 | |||
224 | 75171 | void CacheTransport::Frame::UnwrapMsg() { | |
225 |
2/2✓ Branch 1 taken 126 times.
✓ Branch 2 taken 75038 times.
|
75171 | if (msg_rpc_.has_msg_handshake()) { |
226 | 126 | msg_typed_ = msg_rpc_.mutable_msg_handshake(); | |
227 |
2/2✓ Branch 1 taken 152 times.
✓ Branch 2 taken 74879 times.
|
75038 | } else if (msg_rpc_.has_msg_handshake_ack()) { |
228 | 152 | msg_typed_ = msg_rpc_.mutable_msg_handshake_ack(); | |
229 |
2/2✓ Branch 1 taken 126 times.
✓ Branch 2 taken 74767 times.
|
74879 | } else if (msg_rpc_.has_msg_quit()) { |
230 | 126 | msg_typed_ = msg_rpc_.mutable_msg_quit(); | |
231 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 74711 times.
|
74767 | } else if (msg_rpc_.has_msg_ioctl()) { |
232 | 28 | msg_typed_ = msg_rpc_.mutable_msg_ioctl(); | |
233 |
2/2✓ Branch 1 taken 3948 times.
✓ Branch 2 taken 70770 times.
|
74711 | } else if (msg_rpc_.has_msg_refcount_req()) { |
234 | 3948 | msg_typed_ = msg_rpc_.mutable_msg_refcount_req(); | |
235 |
2/2✓ Branch 1 taken 6585 times.
✓ Branch 2 taken 64192 times.
|
70770 | } else if (msg_rpc_.has_msg_refcount_reply()) { |
236 | 6585 | msg_typed_ = msg_rpc_.mutable_msg_refcount_reply(); | |
237 |
2/2✓ Branch 1 taken 112 times.
✓ Branch 2 taken 64080 times.
|
64192 | } else if (msg_rpc_.has_msg_object_info_req()) { |
238 | 112 | msg_typed_ = msg_rpc_.mutable_msg_object_info_req(); | |
239 |
2/2✓ Branch 1 taken 199 times.
✓ Branch 2 taken 63881 times.
|
64080 | } else if (msg_rpc_.has_msg_object_info_reply()) { |
240 | 199 | msg_typed_ = msg_rpc_.mutable_msg_object_info_reply(); | |
241 |
2/2✓ Branch 1 taken 15477 times.
✓ Branch 2 taken 48397 times.
|
63881 | } else if (msg_rpc_.has_msg_read_req()) { |
242 | 15477 | msg_typed_ = msg_rpc_.mutable_msg_read_req(); | |
243 |
2/2✓ Branch 1 taken 28752 times.
✓ Branch 2 taken 19659 times.
|
48397 | } else if (msg_rpc_.has_msg_read_reply()) { |
244 | 28752 | msg_typed_ = msg_rpc_.mutable_msg_read_reply(); | |
245 |
2/2✓ Branch 1 taken 4256 times.
✓ Branch 2 taken 15403 times.
|
19659 | } else if (msg_rpc_.has_msg_store_req()) { |
246 | 4256 | msg_typed_ = msg_rpc_.mutable_msg_store_req(); | |
247 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 15396 times.
|
15403 | } else if (msg_rpc_.has_msg_store_abort_req()) { |
248 | 7 | msg_typed_ = msg_rpc_.mutable_msg_store_abort_req(); | |
249 |
2/2✓ Branch 1 taken 8077 times.
✓ Branch 2 taken 7319 times.
|
15396 | } else if (msg_rpc_.has_msg_store_reply()) { |
250 | 8077 | msg_typed_ = msg_rpc_.mutable_msg_store_reply(); | |
251 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7291 times.
|
7319 | } else if (msg_rpc_.has_msg_info_req()) { |
252 | 28 | msg_typed_ = msg_rpc_.mutable_msg_info_req(); | |
253 |
2/2✓ Branch 1 taken 52 times.
✓ Branch 2 taken 7239 times.
|
7291 | } else if (msg_rpc_.has_msg_info_reply()) { |
254 | 52 | msg_typed_ = msg_rpc_.mutable_msg_info_reply(); | |
255 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 7225 times.
|
7239 | } else if (msg_rpc_.has_msg_shrink_req()) { |
256 | 14 | msg_typed_ = msg_rpc_.mutable_msg_shrink_req(); | |
257 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 7203 times.
|
7225 | } else if (msg_rpc_.has_msg_shrink_reply()) { |
258 | 22 | msg_typed_ = msg_rpc_.mutable_msg_shrink_reply(); | |
259 |
2/2✓ Branch 1 taken 70 times.
✓ Branch 2 taken 7133 times.
|
7203 | } else if (msg_rpc_.has_msg_list_req()) { |
260 | 70 | msg_typed_ = msg_rpc_.mutable_msg_list_req(); | |
261 |
2/2✓ Branch 1 taken 78 times.
✓ Branch 2 taken 7055 times.
|
7133 | } else if (msg_rpc_.has_msg_list_reply()) { |
262 | 78 | msg_typed_ = msg_rpc_.mutable_msg_list_reply(); | |
263 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7048 times.
|
7055 | } else if (msg_rpc_.has_msg_breadcrumb_store_req()) { |
264 | 7 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req(); | |
265 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 7034 times.
|
7048 | } else if (msg_rpc_.has_msg_breadcrumb_load_req()) { |
266 | 14 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req(); | |
267 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 2 taken 7007 times.
|
7034 | } else if (msg_rpc_.has_msg_breadcrumb_reply()) { |
268 | 27 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply(); | |
269 |
1/2✓ Branch 1 taken 7007 times.
✗ Branch 2 not taken.
|
7007 | } else if (msg_rpc_.has_msg_detach()) { |
270 | 7007 | msg_typed_ = msg_rpc_.mutable_msg_detach(); | |
271 | 7007 | is_msg_out_of_band_ = true; | |
272 | } else { | ||
273 | // Unexpected message type, should never happen | ||
274 | ✗ | PANIC(NULL); | |
275 | } | ||
276 | 75171 | } | |
277 | |||
278 | |||
279 | //------------------------------------------------------------------------------ | ||
280 | |||
281 | |||
282 | 152 | CacheTransport::CacheTransport(int fd_connection) | |
283 | 152 | : fd_connection_(fd_connection), flags_(0) { | |
284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
|
152 | assert(fd_connection_ >= 0); |
285 | 152 | } | |
286 | |||
287 | |||
288 | 31227 | CacheTransport::CacheTransport(int fd_connection, uint32_t flags) | |
289 | 31227 | : fd_connection_(fd_connection), flags_(flags) { | |
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31227 times.
|
31227 | assert(fd_connection_ >= 0); |
291 | 31227 | } | |
292 | |||
293 | |||
294 | 1413625 | void CacheTransport::FillMsgHash(const shash::Any &hash, | |
295 | cvmfs::MsgHash *msg_hash) { | ||
296 |
3/4✓ Branch 0 taken 1413585 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
|
1413625 | switch (hash.algorithm) { |
297 | 1413585 | case shash::kSha1: | |
298 | 1413585 | msg_hash->set_algorithm(cvmfs::HASH_SHA1); | |
299 | 1413585 | break; | |
300 | 12 | case shash::kRmd160: | |
301 | 12 | msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160); | |
302 | 12 | break; | |
303 | 28 | case shash::kShake128: | |
304 | 28 | msg_hash->set_algorithm(cvmfs::HASH_SHAKE128); | |
305 | 28 | break; | |
306 | ✗ | default: | |
307 | ✗ | PANIC(NULL); | |
308 | } | ||
309 | 1413625 | msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]); | |
310 | 1413625 | } | |
311 | |||
312 | |||
313 | 6662 | void CacheTransport::FillObjectType(int object_flags, | |
314 | cvmfs::EnumObjectType *wire_type) { | ||
315 | 6662 | *wire_type = cvmfs::OBJECT_REGULAR; | |
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6662 times.
|
6662 | if (object_flags & CacheManager::kLabelCatalog) |
317 | ✗ | *wire_type = cvmfs::OBJECT_CATALOG; | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6662 times.
|
6662 | if (object_flags & CacheManager::kLabelVolatile) |
319 | ✗ | *wire_type = cvmfs::OBJECT_VOLATILE; | |
320 | 6662 | } | |
321 | |||
322 | |||
323 | 23809 | bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash, | |
324 | shash::Any *hash) { | ||
325 |
2/4✓ Branch 1 taken 23793 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
23809 | switch (msg_hash.algorithm()) { |
326 | 23793 | case cvmfs::HASH_SHA1: | |
327 | 23793 | hash->algorithm = shash::kSha1; | |
328 | 23793 | break; | |
329 | ✗ | case cvmfs::HASH_RIPEMD160: | |
330 | ✗ | hash->algorithm = shash::kRmd160; | |
331 | ✗ | break; | |
332 | 16 | case cvmfs::HASH_SHAKE128: | |
333 | 16 | hash->algorithm = shash::kShake128; | |
334 | 16 | break; | |
335 | ✗ | default: | |
336 | ✗ | return false; | |
337 | } | ||
338 | 23809 | const unsigned digest_size = shash::kDigestSizes[hash->algorithm]; | |
339 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 23809 times.
|
23809 | if (msg_hash.digest().length() != digest_size) |
340 | ✗ | return false; | |
341 | 23809 | memcpy(hash->digest, msg_hash.digest().data(), digest_size); | |
342 | 23809 | return true; | |
343 | } | ||
344 | |||
345 | |||
346 | ✗ | bool CacheTransport::ParseObjectType(cvmfs::EnumObjectType wire_type, | |
347 | int *object_flags) { | ||
348 | ✗ | *object_flags = 0; | |
349 | ✗ | switch (wire_type) { | |
350 | ✗ | case cvmfs::OBJECT_REGULAR: | |
351 | ✗ | return true; | |
352 | ✗ | case cvmfs::OBJECT_CATALOG: | |
353 | ✗ | *object_flags |= CacheManager::kLabelCatalog; | |
354 | ✗ | return true; | |
355 | ✗ | case cvmfs::OBJECT_VOLATILE: | |
356 | ✗ | *object_flags |= CacheManager::kLabelVolatile; | |
357 | ✗ | return true; | |
358 | ✗ | default: | |
359 | ✗ | return false; | |
360 | } | ||
361 | } | ||
362 | |||
363 | |||
364 | 60989 | bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) { | |
365 | uint32_t size; | ||
366 | bool has_attachment; | ||
367 |
1/2✓ Branch 1 taken 60982 times.
✗ Branch 2 not taken.
|
60989 | bool retval = RecvHeader(&size, &has_attachment); |
368 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 60975 times.
|
60982 | if (!retval) |
369 | 7 | return false; | |
370 | |||
371 | void *buffer; | ||
372 |
2/2✓ Branch 0 taken 40793 times.
✓ Branch 1 taken 20182 times.
|
60975 | if (size <= kMaxStackAlloc) |
373 | 40793 | buffer = alloca(size); | |
374 | else | ||
375 | 20182 | buffer = smalloc(size); | |
376 |
1/2✓ Branch 1 taken 60982 times.
✗ Branch 2 not taken.
|
60975 | const ssize_t nbytes = SafeRead(fd_connection_, buffer, size); |
377 |
2/4✓ Branch 0 taken 60982 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 60982 times.
|
60982 | if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) { |
378 | ✗ | if (size > kMaxStackAlloc) { | |
379 | ✗ | free(buffer); | |
380 | } | ||
381 | ✗ | return false; | |
382 | } | ||
383 | |||
384 | 60982 | uint32_t msg_size = size; | |
385 |
2/2✓ Branch 0 taken 20402 times.
✓ Branch 1 taken 40580 times.
|
60982 | if (has_attachment) { |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20402 times.
|
20402 | if (size < 2) { |
387 | // kMaxStackAlloc is > 2 (of course!) but we'll leave the condition here | ||
388 | // for consistency. | ||
389 | ✗ | if (size > kMaxStackAlloc) { | |
390 | ✗ | free(buffer); | |
391 | } | ||
392 | ✗ | return false; | |
393 | } | ||
394 | 20402 | msg_size = (*reinterpret_cast<unsigned char *>(buffer)) | |
395 | 20402 | + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8); | |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20402 times.
|
20402 | if ((msg_size + kInnerHeaderSize) > size) { |
397 | ✗ | if (size > kMaxStackAlloc) { | |
398 | ✗ | free(buffer); | |
399 | } | ||
400 | ✗ | return false; | |
401 | } | ||
402 | } | ||
403 | |||
404 | 60982 | void *ptr_msg = has_attachment | |
405 |
2/2✓ Branch 0 taken 20402 times.
✓ Branch 1 taken 40580 times.
|
60982 | ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize) |
406 | : buffer; | ||
407 |
1/2✓ Branch 1 taken 60954 times.
✗ Branch 2 not taken.
|
60982 | retval = frame->ParseMsgRpc(ptr_msg, msg_size); |
408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60954 times.
|
60954 | if (!retval) { |
409 | ✗ | if (size > kMaxStackAlloc) { | |
410 | ✗ | free(buffer); | |
411 | } | ||
412 | ✗ | return false; | |
413 | } | ||
414 | |||
415 |
2/2✓ Branch 0 taken 20402 times.
✓ Branch 1 taken 40552 times.
|
60954 | if (has_attachment) { |
416 | 20402 | const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize); | |
417 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20402 times.
|
20402 | if (frame->att_size() < attachment_size) { |
418 | ✗ | if (size > kMaxStackAlloc) { | |
419 | ✗ | free(buffer); | |
420 | } | ||
421 | ✗ | return false; | |
422 | } | ||
423 | 20402 | void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize | |
424 | 20402 | + msg_size; | |
425 | 20402 | memcpy(frame->attachment(), ptr_attachment, attachment_size); | |
426 | 20402 | frame->set_att_size(attachment_size); | |
427 | } else { | ||
428 | 40552 | frame->set_att_size(0); | |
429 | } | ||
430 |
2/2✓ Branch 0 taken 20182 times.
✓ Branch 1 taken 40800 times.
|
60982 | if (size > kMaxStackAlloc) { |
431 | 20182 | free(buffer); | |
432 | } | ||
433 | 60982 | return true; | |
434 | } | ||
435 | |||
436 | |||
437 | 60989 | bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) { | |
438 | unsigned char header[kHeaderSize]; | ||
439 |
1/2✓ Branch 1 taken 60989 times.
✗ Branch 2 not taken.
|
60989 | const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize); |
440 |
3/4✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 60982 times.
|
60989 | if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize)) |
441 | 7 | return false; | |
442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60982 times.
|
60982 | if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion) |
443 | ✗ | return false; | |
444 | 60982 | *has_attachment = header[0] & kFlagHasAttachment; | |
445 | 60982 | *size = header[1] + (header[2] << 8) + (header[3] << 16); | |
446 |
2/4✓ Branch 0 taken 60982 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60982 times.
✗ Branch 3 not taken.
|
60982 | return (*size > 0) && (*size <= kMaxMsgSize); |
447 | } | ||
448 | |||
449 | |||
450 | 61013 | void CacheTransport::SendData(void *message, | |
451 | uint32_t msg_size, | ||
452 | void *attachment, | ||
453 | uint32_t att_size) { | ||
454 | 122026 | const uint32_t total_size = msg_size + att_size | |
455 |
2/2✓ Branch 0 taken 22131 times.
✓ Branch 1 taken 38882 times.
|
61013 | + ((att_size > 0) ? kInnerHeaderSize : 0); |
456 | |||
457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61013 times.
|
61013 | assert(total_size > 0); |
458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61013 times.
|
61013 | assert(total_size <= kMaxMsgSize); |
459 |
1/2✓ Branch 1 taken 55286 times.
✗ Branch 2 not taken.
|
55286 | LogCvmfs(kLogCache, kLogDebug, |
460 | "sending message of size %u to cache transport", total_size); | ||
461 | |||
462 | unsigned char header[kHeaderSize]; | ||
463 |
2/2✓ Branch 0 taken 38882 times.
✓ Branch 1 taken 22131 times.
|
61013 | header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment); |
464 | 61013 | header[1] = (total_size & 0x000000FF); | |
465 | 61013 | header[2] = (total_size & 0x0000FF00) >> 8; | |
466 | 61013 | header[3] = (total_size & 0x00FF0000) >> 16; | |
467 | // Only transferred if an attachment is present. Otherwise the overall size | ||
468 | // is also the size of the protobuf message. | ||
469 | unsigned char inner_header[kInnerHeaderSize]; | ||
470 | |||
471 | struct iovec iov[4]; | ||
472 | 61013 | iov[0].iov_base = header; | |
473 | 61013 | iov[0].iov_len = kHeaderSize; | |
474 | |||
475 |
2/2✓ Branch 0 taken 22131 times.
✓ Branch 1 taken 38882 times.
|
61013 | if (att_size > 0) { |
476 | 22131 | inner_header[0] = (msg_size & 0x000000FF); | |
477 | 22131 | inner_header[1] = (msg_size & 0x0000FF00) >> 8; | |
478 | 22131 | iov[1].iov_base = inner_header; | |
479 | 22131 | iov[1].iov_len = kInnerHeaderSize; | |
480 | 22131 | iov[2].iov_base = message; | |
481 | 22131 | iov[2].iov_len = msg_size; | |
482 | 22131 | iov[3].iov_base = attachment; | |
483 | 22131 | iov[3].iov_len = att_size; | |
484 | } else { | ||
485 | 38882 | iov[1].iov_base = message; | |
486 | 38882 | iov[1].iov_len = msg_size; | |
487 | } | ||
488 |
2/2✓ Branch 0 taken 7014 times.
✓ Branch 1 taken 53999 times.
|
61013 | if (flags_ & kFlagSendNonBlocking) { |
489 |
2/4✓ Branch 0 taken 7014 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 7014 times.
✗ Branch 4 not taken.
|
7014 | SendNonBlocking(iov, (att_size == 0) ? 2 : 4); |
490 | 7014 | return; | |
491 | } | ||
492 |
3/4✓ Branch 0 taken 31868 times.
✓ Branch 1 taken 22131 times.
✓ Branch 3 taken 53971 times.
✗ Branch 4 not taken.
|
53999 | const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4); |
493 | |||
494 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 53971 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
53971 | if (!retval && !(flags_ & kFlagSendIgnoreFailure)) { |
495 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
496 | "failed to write to external cache transport (%d), aborting", errno); | ||
497 | } | ||
498 | } | ||
499 | |||
500 | 7014 | void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) { | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7014 times.
|
7014 | assert(iovcnt > 0); |
502 | 7014 | unsigned total_size = 0; | |
503 |
2/2✓ Branch 0 taken 14028 times.
✓ Branch 1 taken 7014 times.
|
21042 | for (unsigned i = 0; i < iovcnt; ++i) |
504 | 14028 | total_size += iov[i].iov_len; | |
505 | 7014 | unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size)); | |
506 | |||
507 | 7014 | unsigned pos = 0; | |
508 |
2/2✓ Branch 0 taken 14028 times.
✓ Branch 1 taken 7014 times.
|
21042 | for (unsigned i = 0; i < iovcnt; ++i) { |
509 | 14028 | memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len); | |
510 | 14028 | pos += iov[i].iov_len; | |
511 | } | ||
512 | |||
513 | 7014 | const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT); | |
514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7014 times.
|
7014 | if (retval < 0) { |
515 | ✗ | assert(errno != EMSGSIZE); | |
516 | ✗ | if (!(flags_ & kFlagSendIgnoreFailure)) { | |
517 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
518 | "failed to write to external cache transport (%d), aborting", | ||
519 | errno); | ||
520 | } | ||
521 | } | ||
522 | 7014 | } | |
523 | |||
524 | |||
525 | 61013 | void CacheTransport::SendFrame(CacheTransport::Frame *frame) { | |
526 | 61013 | cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc(); | |
527 | 61013 | const int32_t size = msg_rpc->ByteSize(); | |
528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61013 times.
|
61013 | assert(size > 0); |
529 | #ifdef __APPLE__ | ||
530 | void *buffer = smalloc(size); | ||
531 | #else | ||
532 | 61013 | void *buffer = alloca(size); | |
533 | #endif | ||
534 | 61013 | const bool retval = msg_rpc->SerializeToArray(buffer, size); | |
535 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61013 times.
|
61013 | assert(retval); |
536 | 61013 | SendData(buffer, size, frame->attachment(), frame->att_size()); | |
537 | #ifdef __APPLE__ | ||
538 | free(buffer); | ||
539 | #endif | ||
540 | 60943 | } | |
541 |