Directory: | cvmfs/ |
---|---|
File: | cvmfs/cache_transport.cc |
Date: | 2025-06-29 02:35:41 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 328 | 372 | 88.2% |
Branches: | 168 | 228 | 73.7% |
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 | 116307 | cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() { | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116307 times.
|
116307 | assert(msg_typed_ != NULL); |
35 |
1/2✓ Branch 0 taken 116307 times.
✗ Branch 1 not taken.
|
116307 | if (!is_wrapped_) |
36 | 116307 | WrapMsg(); | |
37 | 116307 | 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 | 144619 | google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() { | |
46 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 144605 times.
|
144619 | assert(msg_rpc_.IsInitialized()); |
47 |
2/2✓ Branch 0 taken 119432 times.
✓ Branch 1 taken 25173 times.
|
144605 | if (msg_typed_ == NULL) |
48 | 119432 | UnwrapMsg(); | |
49 | 144507 | return msg_typed_; | |
50 | } | ||
51 | |||
52 | |||
53 | 144647 | CacheTransport::Frame::Frame() | |
54 | 144661 | : owns_msg_typed_(false) | |
55 | 144661 | , msg_typed_(NULL) | |
56 | 144661 | , attachment_(NULL) | |
57 | 144661 | , att_size_(0) | |
58 | 144661 | , is_wrapped_(false) | |
59 | 144647 | , is_msg_out_of_band_(false) { } | |
60 | |||
61 | |||
62 | 116293 | CacheTransport::Frame::Frame(google::protobuf::MessageLite *m) | |
63 | 116293 | : owns_msg_typed_(false) | |
64 | 116293 | , msg_typed_(m) | |
65 | 116293 | , attachment_(NULL) | |
66 | 116293 | , att_size_(0) | |
67 | 116293 | , is_wrapped_(false) | |
68 | 116293 | , is_msg_out_of_band_(false) { } | |
69 | |||
70 | |||
71 | 260898 | CacheTransport::Frame::~Frame() { Reset(0); } | |
72 | |||
73 | |||
74 | 25173 | bool CacheTransport::Frame::IsMsgOutOfBand() { | |
75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 25173 times.
|
25173 | assert(msg_rpc_.IsInitialized()); |
76 |
1/2✓ Branch 0 taken 25173 times.
✗ Branch 1 not taken.
|
25173 | if (msg_typed_ == NULL) |
77 | 25173 | UnwrapMsg(); | |
78 | 25173 | return is_msg_out_of_band_; | |
79 | } | ||
80 | |||
81 | |||
82 | 28392 | void CacheTransport::Frame::MergeFrom(const Frame &other) { | |
83 | 28392 | msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_); | |
84 | 28392 | owns_msg_typed_ = true; | |
85 |
2/2✓ Branch 0 taken 25200 times.
✓ Branch 1 taken 3192 times.
|
28392 | if (other.att_size_ > 0) { |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25200 times.
|
25200 | assert(att_size_ >= other.att_size_); |
87 | 25200 | memcpy(attachment_, other.attachment_, other.att_size_); | |
88 | 25200 | att_size_ = other.att_size_; | |
89 | } | ||
90 | 28392 | } | |
91 | |||
92 | |||
93 | 116241 | bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) { | |
94 | 116241 | const bool retval = msg_rpc_.ParseFromArray(buffer, size); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116199 times.
|
116199 | if (!retval) |
96 | ✗ | return false; | |
97 | |||
98 | // Cleanup typed message when Frame leaves scope | ||
99 | 116199 | owns_msg_typed_ = true; | |
100 | 116199 | return true; | |
101 | } | ||
102 | |||
103 | |||
104 | 260940 | void CacheTransport::Frame::Release() { | |
105 |
2/2✓ Branch 0 taken 144633 times.
✓ Branch 1 taken 116307 times.
|
260940 | if (owns_msg_typed_) |
106 | 144633 | return; | |
107 | |||
108 | 116307 | msg_rpc_.release_msg_refcount_req(); | |
109 | 116293 | msg_rpc_.release_msg_refcount_reply(); | |
110 | 116321 | msg_rpc_.release_msg_read_req(); | |
111 | 116321 | msg_rpc_.release_msg_read_reply(); | |
112 | 116307 | msg_rpc_.release_msg_object_info_req(); | |
113 | 116307 | msg_rpc_.release_msg_object_info_reply(); | |
114 | 116307 | msg_rpc_.release_msg_store_req(); | |
115 | 116307 | msg_rpc_.release_msg_store_abort_req(); | |
116 | 116293 | msg_rpc_.release_msg_store_reply(); | |
117 | 116279 | msg_rpc_.release_msg_handshake(); | |
118 | 116279 | msg_rpc_.release_msg_handshake_ack(); | |
119 | 116307 | msg_rpc_.release_msg_quit(); | |
120 | 116293 | msg_rpc_.release_msg_ioctl(); | |
121 | 116293 | msg_rpc_.release_msg_info_req(); | |
122 | 116307 | msg_rpc_.release_msg_info_reply(); | |
123 | 116293 | msg_rpc_.release_msg_shrink_req(); | |
124 | 116279 | msg_rpc_.release_msg_shrink_reply(); | |
125 | 116307 | msg_rpc_.release_msg_list_req(); | |
126 | 116293 | msg_rpc_.release_msg_list_reply(); | |
127 | 116307 | msg_rpc_.release_msg_detach(); | |
128 | 116307 | msg_rpc_.release_msg_breadcrumb_store_req(); | |
129 | 116307 | msg_rpc_.release_msg_breadcrumb_load_req(); | |
130 | 116307 | msg_rpc_.release_msg_breadcrumb_reply(); | |
131 | } | ||
132 | |||
133 | |||
134 | 260926 | void CacheTransport::Frame::Reset(uint32_t original_att_size) { | |
135 | 260926 | msg_typed_ = NULL; | |
136 | 260926 | att_size_ = original_att_size; | |
137 | 260926 | is_wrapped_ = false; | |
138 | 260926 | is_msg_out_of_band_ = false; | |
139 | 260926 | Release(); | |
140 | 260842 | msg_rpc_.Clear(); | |
141 | 260940 | owns_msg_typed_ = false; | |
142 | 260940 | } | |
143 | |||
144 | |||
145 | 116307 | void CacheTransport::Frame::WrapMsg() { | |
146 |
2/2✓ Branch 3 taken 278 times.
✓ Branch 4 taken 116029 times.
|
116307 | if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") { |
147 | 278 | msg_rpc_.set_allocated_msg_handshake( | |
148 | 278 | reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_)); | |
149 |
2/2✓ Branch 3 taken 252 times.
✓ Branch 4 taken 115777 times.
|
116029 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") { |
150 | 252 | msg_rpc_.set_allocated_msg_handshake_ack( | |
151 | 252 | reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_)); | |
152 |
2/2✓ Branch 3 taken 276 times.
✓ Branch 4 taken 115501 times.
|
115777 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") { |
153 | 276 | msg_rpc_.set_allocated_msg_quit( | |
154 | 276 | reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_)); | |
155 |
2/2✓ Branch 3 taken 56 times.
✓ Branch 4 taken 115417 times.
|
115501 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") { |
156 | 56 | msg_rpc_.set_allocated_msg_ioctl( | |
157 | 56 | reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_)); | |
158 |
2/2✓ Branch 3 taken 10400 times.
✓ Branch 4 taken 105017 times.
|
115417 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") { |
159 | 10400 | msg_rpc_.set_allocated_msg_refcount_req( | |
160 | 10400 | reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_)); | |
161 |
2/2✓ Branch 3 taken 7896 times.
✓ Branch 4 taken 97135 times.
|
105017 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") { |
162 | 7896 | msg_rpc_.set_allocated_msg_refcount_reply( | |
163 | 7896 | reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_)); | |
164 |
2/2✓ Branch 3 taken 248 times.
✓ Branch 4 taken 96887 times.
|
97135 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") { |
165 | 248 | msg_rpc_.set_allocated_msg_object_info_req( | |
166 | 248 | reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_)); | |
167 |
2/2✓ Branch 3 taken 224 times.
✓ Branch 4 taken 96677 times.
|
96887 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") { |
168 | 224 | msg_rpc_.set_allocated_msg_object_info_reply( | |
169 | 224 | reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_)); | |
170 |
2/2✓ Branch 3 taken 31651 times.
✓ Branch 4 taken 65026 times.
|
96677 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") { |
171 | 31651 | msg_rpc_.set_allocated_msg_read_req( | |
172 | 31651 | reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_)); | |
173 |
2/2✓ Branch 3 taken 30954 times.
✓ Branch 4 taken 34072 times.
|
65026 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") { |
174 | 30954 | msg_rpc_.set_allocated_msg_read_reply( | |
175 | 30954 | reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_)); | |
176 |
2/2✓ Branch 3 taken 10926 times.
✓ Branch 4 taken 23146 times.
|
34072 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") { |
177 | 10926 | msg_rpc_.set_allocated_msg_store_req( | |
178 | 10926 | reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_)); | |
179 |
2/2✓ Branch 3 taken 14 times.
✓ Branch 4 taken 23132 times.
|
23146 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") { |
180 | 14 | msg_rpc_.set_allocated_msg_store_abort_req( | |
181 | 14 | reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_)); | |
182 |
2/2✓ Branch 3 taken 8526 times.
✓ Branch 4 taken 14606 times.
|
23132 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") { |
183 | 8526 | msg_rpc_.set_allocated_msg_store_reply( | |
184 | 8526 | reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_)); | |
185 |
2/2✓ Branch 3 taken 80 times.
✓ Branch 4 taken 14526 times.
|
14606 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") { |
186 | 80 | msg_rpc_.set_allocated_msg_info_req( | |
187 | 80 | reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_)); | |
188 |
2/2✓ Branch 3 taken 56 times.
✓ Branch 4 taken 14470 times.
|
14526 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") { |
189 | 56 | msg_rpc_.set_allocated_msg_info_reply( | |
190 | 56 | reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_)); | |
191 |
2/2✓ Branch 3 taken 36 times.
✓ Branch 4 taken 14434 times.
|
14470 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") { |
192 | 36 | msg_rpc_.set_allocated_msg_shrink_req( | |
193 | 36 | reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_)); | |
194 |
2/2✓ Branch 3 taken 28 times.
✓ Branch 4 taken 14406 times.
|
14434 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") { |
195 | 28 | msg_rpc_.set_allocated_msg_shrink_reply( | |
196 | 28 | reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_)); | |
197 |
2/2✓ Branch 3 taken 148 times.
✓ Branch 4 taken 14258 times.
|
14406 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") { |
198 | 148 | msg_rpc_.set_allocated_msg_list_req( | |
199 | 148 | reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_)); | |
200 |
2/2✓ Branch 3 taken 140 times.
✓ Branch 4 taken 14118 times.
|
14258 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") { |
201 | 140 | msg_rpc_.set_allocated_msg_list_reply( | |
202 | 140 | reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_)); | |
203 |
2/2✓ Branch 3 taken 16 times.
✓ Branch 4 taken 14102 times.
|
14118 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") { |
204 | 16 | msg_rpc_.set_allocated_msg_breadcrumb_store_req( | |
205 | 16 | reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_)); | |
206 |
2/2✓ Branch 3 taken 32 times.
✓ Branch 4 taken 14070 times.
|
14102 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") { |
207 | 32 | msg_rpc_.set_allocated_msg_breadcrumb_load_req( | |
208 | 32 | reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_)); | |
209 |
2/2✓ Branch 3 taken 42 times.
✓ Branch 4 taken 14028 times.
|
14070 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") { |
210 | 42 | msg_rpc_.set_allocated_msg_breadcrumb_reply( | |
211 | 42 | reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_)); | |
212 |
1/2✓ Branch 3 taken 14028 times.
✗ Branch 4 not taken.
|
14028 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") { |
213 | 14028 | msg_rpc_.set_allocated_msg_detach( | |
214 | 14028 | reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_)); | |
215 | 14028 | is_msg_out_of_band_ = true; | |
216 | } else { | ||
217 | // Unexpected message type, should never happen | ||
218 | ✗ | PANIC(NULL); | |
219 | } | ||
220 | 116307 | is_wrapped_ = true; | |
221 | 116307 | } | |
222 | |||
223 | |||
224 | 144591 | void CacheTransport::Frame::UnwrapMsg() { | |
225 |
2/2✓ Branch 1 taken 252 times.
✓ Branch 2 taken 144283 times.
|
144591 | if (msg_rpc_.has_msg_handshake()) { |
226 | 252 | msg_typed_ = msg_rpc_.mutable_msg_handshake(); | |
227 |
2/2✓ Branch 1 taken 278 times.
✓ Branch 2 taken 144033 times.
|
144283 | } else if (msg_rpc_.has_msg_handshake_ack()) { |
228 | 278 | msg_typed_ = msg_rpc_.mutable_msg_handshake_ack(); | |
229 |
2/2✓ Branch 1 taken 252 times.
✓ Branch 2 taken 143823 times.
|
144033 | } else if (msg_rpc_.has_msg_quit()) { |
230 | 252 | msg_typed_ = msg_rpc_.mutable_msg_quit(); | |
231 |
2/2✓ Branch 1 taken 56 times.
✓ Branch 2 taken 143753 times.
|
143823 | } else if (msg_rpc_.has_msg_ioctl()) { |
232 | 56 | msg_typed_ = msg_rpc_.mutable_msg_ioctl(); | |
233 |
2/2✓ Branch 1 taken 7896 times.
✓ Branch 2 taken 135759 times.
|
143753 | } else if (msg_rpc_.has_msg_refcount_req()) { |
234 | 7896 | msg_typed_ = msg_rpc_.mutable_msg_refcount_req(); | |
235 |
2/2✓ Branch 1 taken 10652 times.
✓ Branch 2 taken 125121 times.
|
135759 | } else if (msg_rpc_.has_msg_refcount_reply()) { |
236 | 10652 | msg_typed_ = msg_rpc_.mutable_msg_refcount_reply(); | |
237 |
2/2✓ Branch 1 taken 224 times.
✓ Branch 2 taken 124841 times.
|
125121 | } else if (msg_rpc_.has_msg_object_info_req()) { |
238 | 224 | msg_typed_ = msg_rpc_.mutable_msg_object_info_req(); | |
239 |
2/2✓ Branch 1 taken 374 times.
✓ Branch 2 taken 124523 times.
|
124841 | } else if (msg_rpc_.has_msg_object_info_reply()) { |
240 | 374 | msg_typed_ = msg_rpc_.mutable_msg_object_info_reply(); | |
241 |
2/2✓ Branch 1 taken 30954 times.
✓ Branch 2 taken 93541 times.
|
124523 | } else if (msg_rpc_.has_msg_read_req()) { |
242 | 30954 | msg_typed_ = msg_rpc_.mutable_msg_read_req(); | |
243 |
2/2✓ Branch 1 taken 56851 times.
✓ Branch 2 taken 36858 times.
|
93541 | } else if (msg_rpc_.has_msg_read_reply()) { |
244 | 56851 | msg_typed_ = msg_rpc_.mutable_msg_read_reply(); | |
245 |
2/2✓ Branch 1 taken 8512 times.
✓ Branch 2 taken 28346 times.
|
36858 | } else if (msg_rpc_.has_msg_store_req()) { |
246 | 8512 | msg_typed_ = msg_rpc_.mutable_msg_store_req(); | |
247 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 28332 times.
|
28346 | } else if (msg_rpc_.has_msg_store_abort_req()) { |
248 | 14 | msg_typed_ = msg_rpc_.mutable_msg_store_abort_req(); | |
249 |
2/2✓ Branch 1 taken 13740 times.
✓ Branch 2 taken 14592 times.
|
28332 | } else if (msg_rpc_.has_msg_store_reply()) { |
250 | 13740 | msg_typed_ = msg_rpc_.mutable_msg_store_reply(); | |
251 |
2/2✓ Branch 1 taken 56 times.
✓ Branch 2 taken 14536 times.
|
14592 | } else if (msg_rpc_.has_msg_info_req()) { |
252 | 56 | msg_typed_ = msg_rpc_.mutable_msg_info_req(); | |
253 |
2/2✓ Branch 1 taken 80 times.
✓ Branch 2 taken 14456 times.
|
14536 | } else if (msg_rpc_.has_msg_info_reply()) { |
254 | 80 | msg_typed_ = msg_rpc_.mutable_msg_info_reply(); | |
255 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 14428 times.
|
14456 | } else if (msg_rpc_.has_msg_shrink_req()) { |
256 | 28 | msg_typed_ = msg_rpc_.mutable_msg_shrink_req(); | |
257 |
2/2✓ Branch 1 taken 36 times.
✓ Branch 2 taken 14392 times.
|
14428 | } else if (msg_rpc_.has_msg_shrink_reply()) { |
258 | 36 | msg_typed_ = msg_rpc_.mutable_msg_shrink_reply(); | |
259 |
2/2✓ Branch 1 taken 140 times.
✓ Branch 2 taken 14252 times.
|
14392 | } else if (msg_rpc_.has_msg_list_req()) { |
260 | 140 | msg_typed_ = msg_rpc_.mutable_msg_list_req(); | |
261 |
2/2✓ Branch 1 taken 148 times.
✓ Branch 2 taken 14104 times.
|
14252 | } else if (msg_rpc_.has_msg_list_reply()) { |
262 | 148 | msg_typed_ = msg_rpc_.mutable_msg_list_reply(); | |
263 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14090 times.
|
14104 | } else if (msg_rpc_.has_msg_breadcrumb_store_req()) { |
264 | 14 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req(); | |
265 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 14062 times.
|
14090 | } else if (msg_rpc_.has_msg_breadcrumb_load_req()) { |
266 | 28 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req(); | |
267 |
2/2✓ Branch 1 taken 48 times.
✓ Branch 2 taken 14014 times.
|
14062 | } else if (msg_rpc_.has_msg_breadcrumb_reply()) { |
268 | 48 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply(); | |
269 |
1/2✓ Branch 1 taken 14014 times.
✗ Branch 2 not taken.
|
14014 | } else if (msg_rpc_.has_msg_detach()) { |
270 | 14014 | msg_typed_ = msg_rpc_.mutable_msg_detach(); | |
271 | 14014 | is_msg_out_of_band_ = true; | |
272 | } else { | ||
273 | // Unexpected message type, should never happen | ||
274 | ✗ | PANIC(NULL); | |
275 | } | ||
276 | 144647 | } | |
277 | |||
278 | |||
279 | //------------------------------------------------------------------------------ | ||
280 | |||
281 | |||
282 | 278 | CacheTransport::CacheTransport(int fd_connection) | |
283 | 278 | : fd_connection_(fd_connection), flags_(0) { | |
284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 278 times.
|
278 | assert(fd_connection_ >= 0); |
285 | 278 | } | |
286 | |||
287 | |||
288 | 62454 | CacheTransport::CacheTransport(int fd_connection, uint32_t flags) | |
289 | 62454 | : fd_connection_(fd_connection), flags_(flags) { | |
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62454 times.
|
62454 | assert(fd_connection_ >= 0); |
291 | 62454 | } | |
292 | |||
293 | |||
294 | 2822090 | void CacheTransport::FillMsgHash(const shash::Any &hash, | |
295 | cvmfs::MsgHash *msg_hash) { | ||
296 |
3/4✓ Branch 0 taken 2822036 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
|
2822090 | switch (hash.algorithm) { |
297 | 2822036 | case shash::kSha1: | |
298 | 2822036 | msg_hash->set_algorithm(cvmfs::HASH_SHA1); | |
299 | 2822036 | break; | |
300 | 12 | case shash::kRmd160: | |
301 | 12 | msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160); | |
302 | 12 | break; | |
303 | 42 | case shash::kShake128: | |
304 | 42 | msg_hash->set_algorithm(cvmfs::HASH_SHAKE128); | |
305 | 42 | break; | |
306 | ✗ | default: | |
307 | ✗ | PANIC(NULL); | |
308 | } | ||
309 | 2822090 | msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]); | |
310 | 2822076 | } | |
311 | |||
312 | |||
313 | 10918 | void CacheTransport::FillObjectType(int object_flags, | |
314 | cvmfs::EnumObjectType *wire_type) { | ||
315 | 10918 | *wire_type = cvmfs::OBJECT_REGULAR; | |
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10918 times.
|
10918 | if (object_flags & CacheManager::kLabelCatalog) |
317 | ✗ | *wire_type = cvmfs::OBJECT_CATALOG; | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10918 times.
|
10918 | if (object_flags & CacheManager::kLabelVolatile) |
319 | ✗ | *wire_type = cvmfs::OBJECT_VOLATILE; | |
320 | 10918 | } | |
321 | |||
322 | |||
323 | 47616 | bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash, | |
324 | shash::Any *hash) { | ||
325 |
2/4✓ Branch 1 taken 47586 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
|
47616 | switch (msg_hash.algorithm()) { |
326 | 47586 | case cvmfs::HASH_SHA1: | |
327 | 47586 | hash->algorithm = shash::kSha1; | |
328 | 47586 | break; | |
329 | ✗ | case cvmfs::HASH_RIPEMD160: | |
330 | ✗ | hash->algorithm = shash::kRmd160; | |
331 | ✗ | break; | |
332 | 30 | case cvmfs::HASH_SHAKE128: | |
333 | 30 | hash->algorithm = shash::kShake128; | |
334 | 30 | break; | |
335 | ✗ | default: | |
336 | ✗ | return false; | |
337 | } | ||
338 | 47616 | const unsigned digest_size = shash::kDigestSizes[hash->algorithm]; | |
339 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 47616 times.
|
47616 | if (msg_hash.digest().length() != digest_size) |
340 | ✗ | return false; | |
341 | 47616 | memcpy(hash->digest, msg_hash.digest().data(), digest_size); | |
342 | 47616 | 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 | 116283 | bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) { | |
365 | uint32_t size; | ||
366 | bool has_attachment; | ||
367 |
1/2✓ Branch 1 taken 116283 times.
✗ Branch 2 not taken.
|
116283 | bool retval = RecvHeader(&size, &has_attachment); |
368 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 116269 times.
|
116283 | if (!retval) |
369 | 14 | return false; | |
370 | |||
371 | void *buffer; | ||
372 |
2/2✓ Branch 0 taken 76376 times.
✓ Branch 1 taken 39893 times.
|
116269 | if (size <= kMaxStackAlloc) |
373 | 76376 | buffer = alloca(size); | |
374 | else | ||
375 | 39893 | buffer = smalloc(size); | |
376 |
1/2✓ Branch 1 taken 116269 times.
✗ Branch 2 not taken.
|
116269 | const ssize_t nbytes = SafeRead(fd_connection_, buffer, size); |
377 |
3/4✓ Branch 0 taken 116255 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116255 times.
|
116269 | if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) { |
378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (size > kMaxStackAlloc) { |
379 | ✗ | free(buffer); | |
380 | } | ||
381 | 14 | return false; | |
382 | } | ||
383 | |||
384 | 116255 | uint32_t msg_size = size; | |
385 |
2/2✓ Branch 0 taken 40129 times.
✓ Branch 1 taken 76126 times.
|
116255 | if (has_attachment) { |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40129 times.
|
40129 | 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 | 40129 | msg_size = (*reinterpret_cast<unsigned char *>(buffer)) | |
395 | 40129 | + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8); | |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40129 times.
|
40129 | if ((msg_size + kInnerHeaderSize) > size) { |
397 | ✗ | if (size > kMaxStackAlloc) { | |
398 | ✗ | free(buffer); | |
399 | } | ||
400 | ✗ | return false; | |
401 | } | ||
402 | } | ||
403 | |||
404 | 116255 | void *ptr_msg = has_attachment | |
405 |
2/2✓ Branch 0 taken 40129 times.
✓ Branch 1 taken 76126 times.
|
116255 | ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize) |
406 | : buffer; | ||
407 |
1/2✓ Branch 1 taken 116185 times.
✗ Branch 2 not taken.
|
116255 | retval = frame->ParseMsgRpc(ptr_msg, msg_size); |
408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116185 times.
|
116185 | if (!retval) { |
409 | ✗ | if (size > kMaxStackAlloc) { | |
410 | ✗ | free(buffer); | |
411 | } | ||
412 | ✗ | return false; | |
413 | } | ||
414 | |||
415 |
2/2✓ Branch 0 taken 40129 times.
✓ Branch 1 taken 76056 times.
|
116185 | if (has_attachment) { |
416 | 40129 | const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize); | |
417 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 40129 times.
|
40129 | if (frame->att_size() < attachment_size) { |
418 | ✗ | if (size > kMaxStackAlloc) { | |
419 | ✗ | free(buffer); | |
420 | } | ||
421 | ✗ | return false; | |
422 | } | ||
423 | 40129 | void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize | |
424 | 40129 | + msg_size; | |
425 | 40129 | memcpy(frame->attachment(), ptr_attachment, attachment_size); | |
426 | 40129 | frame->set_att_size(attachment_size); | |
427 | } else { | ||
428 | 76056 | frame->set_att_size(0); | |
429 | } | ||
430 |
2/2✓ Branch 0 taken 39893 times.
✓ Branch 1 taken 76376 times.
|
116269 | if (size > kMaxStackAlloc) { |
431 | 39893 | free(buffer); | |
432 | } | ||
433 | 116269 | return true; | |
434 | } | ||
435 | |||
436 | |||
437 | 116283 | bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) { | |
438 | unsigned char header[kHeaderSize]; | ||
439 |
1/2✓ Branch 1 taken 116283 times.
✗ Branch 2 not taken.
|
116283 | const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize); |
440 |
3/4✓ Branch 0 taken 116283 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 116269 times.
|
116283 | if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize)) |
441 | 14 | return false; | |
442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116269 times.
|
116269 | if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion) |
443 | ✗ | return false; | |
444 | 116269 | *has_attachment = header[0] & kFlagHasAttachment; | |
445 | 116269 | *size = header[1] + (header[2] << 8) + (header[3] << 16); | |
446 |
2/4✓ Branch 0 taken 116269 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116269 times.
✗ Branch 3 not taken.
|
116269 | return (*size > 0) && (*size <= kMaxMsgSize); |
447 | } | ||
448 | |||
449 | |||
450 | 116279 | void CacheTransport::SendData(void *message, | |
451 | uint32_t msg_size, | ||
452 | void *attachment, | ||
453 | uint32_t att_size) { | ||
454 | 232558 | const uint32_t total_size = msg_size + att_size | |
455 |
2/2✓ Branch 0 taken 41850 times.
✓ Branch 1 taken 74429 times.
|
116279 | + ((att_size > 0) ? kInnerHeaderSize : 0); |
456 | |||
457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116279 times.
|
116279 | assert(total_size > 0); |
458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116279 times.
|
116279 | assert(total_size <= kMaxMsgSize); |
459 |
1/2✓ Branch 1 taken 110558 times.
✗ Branch 2 not taken.
|
110544 | 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 74457 times.
✓ Branch 1 taken 41836 times.
|
116293 | header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment); |
464 | 116293 | header[1] = (total_size & 0x000000FF); | |
465 | 116293 | header[2] = (total_size & 0x0000FF00) >> 8; | |
466 | 116293 | 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 | 116293 | iov[0].iov_base = header; | |
473 | 116293 | iov[0].iov_len = kHeaderSize; | |
474 | |||
475 |
2/2✓ Branch 0 taken 41850 times.
✓ Branch 1 taken 74443 times.
|
116293 | if (att_size > 0) { |
476 | 41850 | inner_header[0] = (msg_size & 0x000000FF); | |
477 | 41850 | inner_header[1] = (msg_size & 0x0000FF00) >> 8; | |
478 | 41850 | iov[1].iov_base = inner_header; | |
479 | 41850 | iov[1].iov_len = kInnerHeaderSize; | |
480 | 41850 | iov[2].iov_base = message; | |
481 | 41850 | iov[2].iov_len = msg_size; | |
482 | 41850 | iov[3].iov_base = attachment; | |
483 | 41850 | iov[3].iov_len = att_size; | |
484 | } else { | ||
485 | 74443 | iov[1].iov_base = message; | |
486 | 74443 | iov[1].iov_len = msg_size; | |
487 | } | ||
488 |
2/2✓ Branch 0 taken 14028 times.
✓ Branch 1 taken 102265 times.
|
116293 | if (flags_ & kFlagSendNonBlocking) { |
489 |
2/4✓ Branch 0 taken 14028 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14028 times.
✗ Branch 4 not taken.
|
14028 | SendNonBlocking(iov, (att_size == 0) ? 2 : 4); |
490 | 14028 | return; | |
491 | } | ||
492 |
3/4✓ Branch 0 taken 60429 times.
✓ Branch 1 taken 41836 times.
✓ Branch 3 taken 102265 times.
✗ Branch 4 not taken.
|
102265 | const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4); |
493 | |||
494 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 102265 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
102265 | if (!retval && !(flags_ & kFlagSendIgnoreFailure)) { |
495 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
496 | "failed to write to external cache transport (%d), aborting", errno); | ||
497 | } | ||
498 | } | ||
499 | |||
500 | 14028 | void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) { | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14028 times.
|
14028 | assert(iovcnt > 0); |
502 | 14028 | unsigned total_size = 0; | |
503 |
2/2✓ Branch 0 taken 28056 times.
✓ Branch 1 taken 14028 times.
|
42084 | for (unsigned i = 0; i < iovcnt; ++i) |
504 | 28056 | total_size += iov[i].iov_len; | |
505 | 14028 | unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size)); | |
506 | |||
507 | 14028 | unsigned pos = 0; | |
508 |
2/2✓ Branch 0 taken 28056 times.
✓ Branch 1 taken 14028 times.
|
42084 | for (unsigned i = 0; i < iovcnt; ++i) { |
509 | 28056 | memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len); | |
510 | 28056 | pos += iov[i].iov_len; | |
511 | } | ||
512 | |||
513 | 14028 | const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT); | |
514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14028 times.
|
14028 | 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 | 14028 | } | |
523 | |||
524 | |||
525 | 116307 | void CacheTransport::SendFrame(CacheTransport::Frame *frame) { | |
526 | 116307 | cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc(); | |
527 | 116293 | const int32_t size = msg_rpc->ByteSize(); | |
528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116251 times.
|
116251 | assert(size > 0); |
529 | #ifdef __APPLE__ | ||
530 | void *buffer = smalloc(size); | ||
531 | #else | ||
532 | 116251 | void *buffer = alloca(size); | |
533 | #endif | ||
534 | 116251 | const bool retval = msg_rpc->SerializeToArray(buffer, size); | |
535 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 116293 times.
|
116293 | assert(retval); |
536 | 116293 | SendData(buffer, size, frame->attachment(), frame->att_size()); | |
537 | #ifdef __APPLE__ | ||
538 | free(buffer); | ||
539 | #endif | ||
540 | 116293 | } | |
541 |