Directory: | cvmfs/ |
---|---|
File: | cvmfs/cache_transport.cc |
Date: | 2025-06-08 02:35:55 |
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 | 13640 | cvmfs::MsgRpc *CacheTransport::Frame::GetMsgRpc() { | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13640 times.
|
13640 | assert(msg_typed_ != NULL); |
35 |
1/2✓ Branch 0 taken 13640 times.
✗ Branch 1 not taken.
|
13640 | if (!is_wrapped_) |
36 | 13640 | WrapMsg(); | |
37 | 13639 | 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 | 15640 | google::protobuf::MessageLite *CacheTransport::Frame::GetMsgTyped() { | |
46 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15641 times.
|
15640 | assert(msg_rpc_.IsInitialized()); |
47 |
2/2✓ Branch 0 taken 8557 times.
✓ Branch 1 taken 7084 times.
|
15641 | if (msg_typed_ == NULL) |
48 | 8557 | UnwrapMsg(); | |
49 | 15633 | return msg_typed_; | |
50 | } | ||
51 | |||
52 | |||
53 | 15643 | CacheTransport::Frame::Frame() | |
54 | 15643 | : owns_msg_typed_(false) | |
55 | 15643 | , msg_typed_(NULL) | |
56 | 15643 | , attachment_(NULL) | |
57 | 15643 | , att_size_(0) | |
58 | 15643 | , is_wrapped_(false) | |
59 | 15643 | , is_msg_out_of_band_(false) { } | |
60 | |||
61 | |||
62 | 13640 | CacheTransport::Frame::Frame(google::protobuf::MessageLite *m) | |
63 | 13640 | : owns_msg_typed_(false) | |
64 | 13640 | , msg_typed_(m) | |
65 | 13640 | , attachment_(NULL) | |
66 | 13640 | , att_size_(0) | |
67 | 13640 | , is_wrapped_(false) | |
68 | 13640 | , is_msg_out_of_band_(false) { } | |
69 | |||
70 | |||
71 | 29281 | CacheTransport::Frame::~Frame() { Reset(0); } | |
72 | |||
73 | |||
74 | 7084 | bool CacheTransport::Frame::IsMsgOutOfBand() { | |
75 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7084 times.
|
7084 | assert(msg_rpc_.IsInitialized()); |
76 |
1/2✓ Branch 0 taken 7084 times.
✗ Branch 1 not taken.
|
7084 | if (msg_typed_ == NULL) |
77 | 7084 | UnwrapMsg(); | |
78 | 7084 | return is_msg_out_of_band_; | |
79 | } | ||
80 | |||
81 | |||
82 | 2028 | void CacheTransport::Frame::MergeFrom(const Frame &other) { | |
83 | 2028 | msg_rpc_.CheckTypeAndMergeFrom(other.msg_rpc_); | |
84 | 2028 | owns_msg_typed_ = true; | |
85 |
2/2✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 228 times.
|
2028 | if (other.att_size_ > 0) { |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1800 times.
|
1800 | assert(att_size_ >= other.att_size_); |
87 | 1800 | memcpy(attachment_, other.attachment_, other.att_size_); | |
88 | 1800 | att_size_ = other.att_size_; | |
89 | } | ||
90 | 2028 | } | |
91 | |||
92 | |||
93 | 13613 | bool CacheTransport::Frame::ParseMsgRpc(void *buffer, uint32_t size) { | |
94 | 13613 | const bool retval = msg_rpc_.ParseFromArray(buffer, size); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13614 times.
|
13614 | if (!retval) |
96 | ✗ | return false; | |
97 | |||
98 | // Cleanup typed message when Frame leaves scope | ||
99 | 13614 | owns_msg_typed_ = true; | |
100 | 13614 | return true; | |
101 | } | ||
102 | |||
103 | |||
104 | 29284 | void CacheTransport::Frame::Release() { | |
105 |
2/2✓ Branch 0 taken 15643 times.
✓ Branch 1 taken 13641 times.
|
29284 | if (owns_msg_typed_) |
106 | 15643 | return; | |
107 | |||
108 | 13641 | msg_rpc_.release_msg_refcount_req(); | |
109 | 13640 | msg_rpc_.release_msg_refcount_reply(); | |
110 | 13641 | msg_rpc_.release_msg_read_req(); | |
111 | 13641 | msg_rpc_.release_msg_read_reply(); | |
112 | 13641 | msg_rpc_.release_msg_object_info_req(); | |
113 | 13641 | msg_rpc_.release_msg_object_info_reply(); | |
114 | 13640 | msg_rpc_.release_msg_store_req(); | |
115 | 13640 | msg_rpc_.release_msg_store_abort_req(); | |
116 | 13640 | msg_rpc_.release_msg_store_reply(); | |
117 | 13640 | msg_rpc_.release_msg_handshake(); | |
118 | 13640 | msg_rpc_.release_msg_handshake_ack(); | |
119 | 13640 | msg_rpc_.release_msg_quit(); | |
120 | 13640 | msg_rpc_.release_msg_ioctl(); | |
121 | 13640 | msg_rpc_.release_msg_info_req(); | |
122 | 13640 | msg_rpc_.release_msg_info_reply(); | |
123 | 13640 | msg_rpc_.release_msg_shrink_req(); | |
124 | 13639 | msg_rpc_.release_msg_shrink_reply(); | |
125 | 13639 | msg_rpc_.release_msg_list_req(); | |
126 | 13639 | msg_rpc_.release_msg_list_reply(); | |
127 | 13639 | msg_rpc_.release_msg_detach(); | |
128 | 13639 | msg_rpc_.release_msg_breadcrumb_store_req(); | |
129 | 13639 | msg_rpc_.release_msg_breadcrumb_load_req(); | |
130 | 13639 | msg_rpc_.release_msg_breadcrumb_reply(); | |
131 | } | ||
132 | |||
133 | |||
134 | 29283 | void CacheTransport::Frame::Reset(uint32_t original_att_size) { | |
135 | 29283 | msg_typed_ = NULL; | |
136 | 29283 | att_size_ = original_att_size; | |
137 | 29283 | is_wrapped_ = false; | |
138 | 29283 | is_msg_out_of_band_ = false; | |
139 | 29283 | Release(); | |
140 | 29282 | msg_rpc_.Clear(); | |
141 | 29280 | owns_msg_typed_ = false; | |
142 | 29280 | } | |
143 | |||
144 | |||
145 | 13640 | void CacheTransport::Frame::WrapMsg() { | |
146 |
2/2✓ Branch 3 taken 44 times.
✓ Branch 4 taken 13595 times.
|
13640 | if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshake") { |
147 | 44 | msg_rpc_.set_allocated_msg_handshake( | |
148 | 44 | reinterpret_cast<cvmfs::MsgHandshake *>(msg_typed_)); | |
149 |
2/2✓ Branch 3 taken 18 times.
✓ Branch 4 taken 13578 times.
|
13595 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgHandshakeAck") { |
150 | 18 | msg_rpc_.set_allocated_msg_handshake_ack( | |
151 | 18 | reinterpret_cast<cvmfs::MsgHandshakeAck *>(msg_typed_)); | |
152 |
2/2✓ Branch 3 taken 42 times.
✓ Branch 4 taken 13536 times.
|
13578 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgQuit") { |
153 | 42 | msg_rpc_.set_allocated_msg_quit( | |
154 | 42 | reinterpret_cast<cvmfs::MsgQuit *>(msg_typed_)); | |
155 |
2/2✓ Branch 3 taken 4 times.
✓ Branch 4 taken 13532 times.
|
13536 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgIoctl") { |
156 | 4 | msg_rpc_.set_allocated_msg_ioctl( | |
157 | 4 | reinterpret_cast<cvmfs::MsgIoctl *>(msg_typed_)); | |
158 |
2/2✓ Branch 3 taken 3068 times.
✓ Branch 4 taken 10463 times.
|
13532 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReq") { |
159 | 3068 | msg_rpc_.set_allocated_msg_refcount_req( | |
160 | 3068 | reinterpret_cast<cvmfs::MsgRefcountReq *>(msg_typed_)); | |
161 |
2/2✓ Branch 3 taken 564 times.
✓ Branch 4 taken 9900 times.
|
10463 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgRefcountReply") { |
162 | 564 | msg_rpc_.set_allocated_msg_refcount_reply( | |
163 | 564 | reinterpret_cast<cvmfs::MsgRefcountReply *>(msg_typed_)); | |
164 |
2/2✓ Branch 3 taken 40 times.
✓ Branch 4 taken 9860 times.
|
9900 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReq") { |
165 | 40 | msg_rpc_.set_allocated_msg_object_info_req( | |
166 | 40 | reinterpret_cast<cvmfs::MsgObjectInfoReq *>(msg_typed_)); | |
167 |
2/2✓ Branch 3 taken 16 times.
✓ Branch 4 taken 9844 times.
|
9860 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgObjectInfoReply") { |
168 | 16 | msg_rpc_.set_allocated_msg_object_info_reply( | |
169 | 16 | reinterpret_cast<cvmfs::MsgObjectInfoReply *>(msg_typed_)); | |
170 |
2/2✓ Branch 3 taken 2915 times.
✓ Branch 4 taken 6929 times.
|
9844 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReq") { |
171 | 2915 | msg_rpc_.set_allocated_msg_read_req( | |
172 | 2915 | reinterpret_cast<cvmfs::MsgReadReq *>(msg_typed_)); | |
173 |
2/2✓ Branch 3 taken 2211 times.
✓ Branch 4 taken 4718 times.
|
6929 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgReadReply") { |
174 | 2211 | msg_rpc_.set_allocated_msg_read_reply( | |
175 | 2211 | reinterpret_cast<cvmfs::MsgReadReply *>(msg_typed_)); | |
176 |
2/2✓ Branch 3 taken 3022 times.
✓ Branch 4 taken 1696 times.
|
4718 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReq") { |
177 | 3022 | msg_rpc_.set_allocated_msg_store_req( | |
178 | 3022 | reinterpret_cast<cvmfs::MsgStoreReq *>(msg_typed_)); | |
179 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1695 times.
|
1696 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreAbortReq") { |
180 | 1 | msg_rpc_.set_allocated_msg_store_abort_req( | |
181 | 1 | reinterpret_cast<cvmfs::MsgStoreAbortReq *>(msg_typed_)); | |
182 |
2/2✓ Branch 3 taken 609 times.
✓ Branch 4 taken 1086 times.
|
1695 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgStoreReply") { |
183 | 609 | msg_rpc_.set_allocated_msg_store_reply( | |
184 | 609 | reinterpret_cast<cvmfs::MsgStoreReply *>(msg_typed_)); | |
185 |
2/2✓ Branch 3 taken 28 times.
✓ Branch 4 taken 1058 times.
|
1086 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReq") { |
186 | 28 | msg_rpc_.set_allocated_msg_info_req( | |
187 | 28 | reinterpret_cast<cvmfs::MsgInfoReq *>(msg_typed_)); | |
188 |
2/2✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1054 times.
|
1058 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgInfoReply") { |
189 | 4 | msg_rpc_.set_allocated_msg_info_reply( | |
190 | 4 | reinterpret_cast<cvmfs::MsgInfoReply *>(msg_typed_)); | |
191 |
2/2✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1044 times.
|
1054 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReq") { |
192 | 10 | msg_rpc_.set_allocated_msg_shrink_req( | |
193 | 10 | reinterpret_cast<cvmfs::MsgShrinkReq *>(msg_typed_)); | |
194 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1042 times.
|
1044 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgShrinkReply") { |
195 | 2 | msg_rpc_.set_allocated_msg_shrink_reply( | |
196 | 2 | reinterpret_cast<cvmfs::MsgShrinkReply *>(msg_typed_)); | |
197 |
2/2✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1024 times.
|
1042 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReq") { |
198 | 18 | msg_rpc_.set_allocated_msg_list_req( | |
199 | 18 | reinterpret_cast<cvmfs::MsgListReq *>(msg_typed_)); | |
200 |
2/2✓ Branch 3 taken 10 times.
✓ Branch 4 taken 1014 times.
|
1024 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgListReply") { |
201 | 10 | msg_rpc_.set_allocated_msg_list_reply( | |
202 | 10 | reinterpret_cast<cvmfs::MsgListReply *>(msg_typed_)); | |
203 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1011 times.
|
1014 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbStoreReq") { |
204 | 3 | msg_rpc_.set_allocated_msg_breadcrumb_store_req( | |
205 | 3 | reinterpret_cast<cvmfs::MsgBreadcrumbStoreReq *>(msg_typed_)); | |
206 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1005 times.
|
1011 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbLoadReq") { |
207 | 6 | msg_rpc_.set_allocated_msg_breadcrumb_load_req( | |
208 | 6 | reinterpret_cast<cvmfs::MsgBreadcrumbLoadReq *>(msg_typed_)); | |
209 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1002 times.
|
1005 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgBreadcrumbReply") { |
210 | 3 | msg_rpc_.set_allocated_msg_breadcrumb_reply( | |
211 | 3 | reinterpret_cast<cvmfs::MsgBreadcrumbReply *>(msg_typed_)); | |
212 |
1/2✓ Branch 3 taken 1002 times.
✗ Branch 4 not taken.
|
1002 | } else if (msg_typed_->GetTypeName() == "cvmfs.MsgDetach") { |
213 | 1002 | msg_rpc_.set_allocated_msg_detach( | |
214 | 1002 | reinterpret_cast<cvmfs::MsgDetach *>(msg_typed_)); | |
215 | 1002 | is_msg_out_of_band_ = true; | |
216 | } else { | ||
217 | // Unexpected message type, should never happen | ||
218 | ✗ | PANIC(NULL); | |
219 | } | ||
220 | 13640 | is_wrapped_ = true; | |
221 | 13640 | } | |
222 | |||
223 | |||
224 | 15641 | void CacheTransport::Frame::UnwrapMsg() { | |
225 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 15621 times.
|
15641 | if (msg_rpc_.has_msg_handshake()) { |
226 | 18 | msg_typed_ = msg_rpc_.mutable_msg_handshake(); | |
227 |
2/2✓ Branch 1 taken 44 times.
✓ Branch 2 taken 15577 times.
|
15621 | } else if (msg_rpc_.has_msg_handshake_ack()) { |
228 | 44 | msg_typed_ = msg_rpc_.mutable_msg_handshake_ack(); | |
229 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 15559 times.
|
15577 | } else if (msg_rpc_.has_msg_quit()) { |
230 | 18 | msg_typed_ = msg_rpc_.mutable_msg_quit(); | |
231 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 15557 times.
|
15559 | } else if (msg_rpc_.has_msg_ioctl()) { |
232 | 4 | msg_typed_ = msg_rpc_.mutable_msg_ioctl(); | |
233 |
2/2✓ Branch 1 taken 564 times.
✓ Branch 2 taken 14989 times.
|
15557 | } else if (msg_rpc_.has_msg_refcount_req()) { |
234 | 564 | msg_typed_ = msg_rpc_.mutable_msg_refcount_req(); | |
235 |
2/2✓ Branch 1 taken 3086 times.
✓ Branch 2 taken 11901 times.
|
14989 | } else if (msg_rpc_.has_msg_refcount_reply()) { |
236 | 3086 | msg_typed_ = msg_rpc_.mutable_msg_refcount_reply(); | |
237 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 11890 times.
|
11901 | } else if (msg_rpc_.has_msg_object_info_req()) { |
238 | 16 | msg_typed_ = msg_rpc_.mutable_msg_object_info_req(); | |
239 |
2/2✓ Branch 1 taken 49 times.
✓ Branch 2 taken 11841 times.
|
11890 | } else if (msg_rpc_.has_msg_object_info_reply()) { |
240 | 49 | msg_typed_ = msg_rpc_.mutable_msg_object_info_reply(); | |
241 |
2/2✓ Branch 1 taken 2211 times.
✓ Branch 2 taken 9625 times.
|
11841 | } else if (msg_rpc_.has_msg_read_req()) { |
242 | 2211 | msg_typed_ = msg_rpc_.mutable_msg_read_req(); | |
243 |
2/2✓ Branch 1 taken 4715 times.
✓ Branch 2 taken 4917 times.
|
9625 | } else if (msg_rpc_.has_msg_read_reply()) { |
244 | 4715 | msg_typed_ = msg_rpc_.mutable_msg_read_reply(); | |
245 |
2/2✓ Branch 1 taken 608 times.
✓ Branch 2 taken 4309 times.
|
4917 | } else if (msg_rpc_.has_msg_store_req()) { |
246 | 608 | msg_typed_ = msg_rpc_.mutable_msg_store_req(); | |
247 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4308 times.
|
4309 | } else if (msg_rpc_.has_msg_store_abort_req()) { |
248 | 1 | msg_typed_ = msg_rpc_.mutable_msg_store_abort_req(); | |
249 |
2/2✓ Branch 1 taken 3223 times.
✓ Branch 2 taken 1085 times.
|
4308 | } else if (msg_rpc_.has_msg_store_reply()) { |
250 | 3223 | msg_typed_ = msg_rpc_.mutable_msg_store_reply(); | |
251 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1081 times.
|
1085 | } else if (msg_rpc_.has_msg_info_req()) { |
252 | 4 | msg_typed_ = msg_rpc_.mutable_msg_info_req(); | |
253 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1053 times.
|
1081 | } else if (msg_rpc_.has_msg_info_reply()) { |
254 | 28 | msg_typed_ = msg_rpc_.mutable_msg_info_reply(); | |
255 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1051 times.
|
1053 | } else if (msg_rpc_.has_msg_shrink_req()) { |
256 | 2 | msg_typed_ = msg_rpc_.mutable_msg_shrink_req(); | |
257 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1041 times.
|
1051 | } else if (msg_rpc_.has_msg_shrink_reply()) { |
258 | 10 | msg_typed_ = msg_rpc_.mutable_msg_shrink_reply(); | |
259 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1031 times.
|
1041 | } else if (msg_rpc_.has_msg_list_req()) { |
260 | 10 | msg_typed_ = msg_rpc_.mutable_msg_list_req(); | |
261 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 1013 times.
|
1031 | } else if (msg_rpc_.has_msg_list_reply()) { |
262 | 18 | msg_typed_ = msg_rpc_.mutable_msg_list_reply(); | |
263 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1012 times.
|
1013 | } else if (msg_rpc_.has_msg_breadcrumb_store_req()) { |
264 | 1 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_store_req(); | |
265 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1010 times.
|
1012 | } else if (msg_rpc_.has_msg_breadcrumb_load_req()) { |
266 | 2 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_load_req(); | |
267 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1001 times.
|
1010 | } else if (msg_rpc_.has_msg_breadcrumb_reply()) { |
268 | 9 | msg_typed_ = msg_rpc_.mutable_msg_breadcrumb_reply(); | |
269 |
1/2✓ Branch 1 taken 1001 times.
✗ Branch 2 not taken.
|
1001 | } else if (msg_rpc_.has_msg_detach()) { |
270 | 1001 | msg_typed_ = msg_rpc_.mutable_msg_detach(); | |
271 | 1001 | is_msg_out_of_band_ = true; | |
272 | } else { | ||
273 | // Unexpected message type, should never happen | ||
274 | ✗ | PANIC(NULL); | |
275 | } | ||
276 | 15642 | } | |
277 | |||
278 | |||
279 | //------------------------------------------------------------------------------ | ||
280 | |||
281 | |||
282 | 44 | CacheTransport::CacheTransport(int fd_connection) | |
283 | 44 | : fd_connection_(fd_connection), flags_(0) { | |
284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | assert(fd_connection_ >= 0); |
285 | 44 | } | |
286 | |||
287 | |||
288 | 4461 | CacheTransport::CacheTransport(int fd_connection, uint32_t flags) | |
289 | 4461 | : fd_connection_(fd_connection), flags_(flags) { | |
290 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4461 times.
|
4461 | assert(fd_connection_ >= 0); |
291 | 4461 | } | |
292 | |||
293 | |||
294 | 206383 | void CacheTransport::FillMsgHash(const shash::Any &hash, | |
295 | cvmfs::MsgHash *msg_hash) { | ||
296 |
3/4✓ Branch 0 taken 206355 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
206383 | switch (hash.algorithm) { |
297 | 206355 | case shash::kSha1: | |
298 | 206355 | msg_hash->set_algorithm(cvmfs::HASH_SHA1); | |
299 | 206355 | break; | |
300 | 12 | case shash::kRmd160: | |
301 | 12 | msg_hash->set_algorithm(cvmfs::HASH_RIPEMD160); | |
302 | 12 | break; | |
303 | 16 | case shash::kShake128: | |
304 | 16 | msg_hash->set_algorithm(cvmfs::HASH_SHAKE128); | |
305 | 16 | break; | |
306 | ✗ | default: | |
307 | ✗ | PANIC(NULL); | |
308 | } | ||
309 | 206383 | msg_hash->set_digest(hash.digest, shash::kDigestSizes[hash.algorithm]); | |
310 | 206383 | } | |
311 | |||
312 | |||
313 | 3014 | void CacheTransport::FillObjectType(int object_flags, | |
314 | cvmfs::EnumObjectType *wire_type) { | ||
315 | 3014 | *wire_type = cvmfs::OBJECT_REGULAR; | |
316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3014 times.
|
3014 | if (object_flags & CacheManager::kLabelCatalog) |
317 | ✗ | *wire_type = cvmfs::OBJECT_CATALOG; | |
318 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3014 times.
|
3014 | if (object_flags & CacheManager::kLabelVolatile) |
319 | ✗ | *wire_type = cvmfs::OBJECT_VOLATILE; | |
320 | 3014 | } | |
321 | |||
322 | |||
323 | 3403 | bool CacheTransport::ParseMsgHash(const cvmfs::MsgHash &msg_hash, | |
324 | shash::Any *hash) { | ||
325 |
2/4✓ Branch 1 taken 3399 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
3403 | switch (msg_hash.algorithm()) { |
326 | 3399 | case cvmfs::HASH_SHA1: | |
327 | 3399 | hash->algorithm = shash::kSha1; | |
328 | 3399 | break; | |
329 | ✗ | case cvmfs::HASH_RIPEMD160: | |
330 | ✗ | hash->algorithm = shash::kRmd160; | |
331 | ✗ | break; | |
332 | 4 | case cvmfs::HASH_SHAKE128: | |
333 | 4 | hash->algorithm = shash::kShake128; | |
334 | 4 | break; | |
335 | ✗ | default: | |
336 | ✗ | return false; | |
337 | } | ||
338 | 3403 | const unsigned digest_size = shash::kDigestSizes[hash->algorithm]; | |
339 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 3403 times.
|
3403 | if (msg_hash.digest().length() != digest_size) |
340 | ✗ | return false; | |
341 | 3403 | memcpy(hash->digest, msg_hash.digest().data(), digest_size); | |
342 | 3403 | 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 | 13616 | bool CacheTransport::RecvFrame(CacheTransport::Frame *frame) { | |
365 | uint32_t size; | ||
366 | bool has_attachment; | ||
367 |
1/2✓ Branch 1 taken 13616 times.
✗ Branch 2 not taken.
|
13616 | bool retval = RecvHeader(&size, &has_attachment); |
368 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13615 times.
|
13616 | if (!retval) |
369 | 1 | return false; | |
370 | |||
371 | void *buffer; | ||
372 |
2/2✓ Branch 0 taken 10320 times.
✓ Branch 1 taken 3295 times.
|
13615 | if (size <= kMaxStackAlloc) |
373 | 10320 | buffer = alloca(size); | |
374 | else | ||
375 | 3295 | buffer = smalloc(size); | |
376 |
1/2✓ Branch 1 taken 13615 times.
✗ Branch 2 not taken.
|
13615 | const ssize_t nbytes = SafeRead(fd_connection_, buffer, size); |
377 |
2/4✓ Branch 0 taken 13615 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13615 times.
|
13615 | if ((nbytes < 0) || (static_cast<uint32_t>(nbytes) != size)) { |
378 | ✗ | if (size > kMaxStackAlloc) { | |
379 | ✗ | free(buffer); | |
380 | } | ||
381 | ✗ | return false; | |
382 | } | ||
383 | |||
384 | 13615 | uint32_t msg_size = size; | |
385 |
2/2✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 10100 times.
|
13615 | if (has_attachment) { |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3515 times.
|
3515 | 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 | 3515 | msg_size = (*reinterpret_cast<unsigned char *>(buffer)) | |
395 | 3515 | + ((*(reinterpret_cast<unsigned char *>(buffer) + 1)) << 8); | |
396 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3515 times.
|
3515 | if ((msg_size + kInnerHeaderSize) > size) { |
397 | ✗ | if (size > kMaxStackAlloc) { | |
398 | ✗ | free(buffer); | |
399 | } | ||
400 | ✗ | return false; | |
401 | } | ||
402 | } | ||
403 | |||
404 | 13615 | void *ptr_msg = has_attachment | |
405 |
2/2✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 10100 times.
|
13615 | ? (reinterpret_cast<char *>(buffer) + kInnerHeaderSize) |
406 | : buffer; | ||
407 |
1/2✓ Branch 1 taken 13614 times.
✗ Branch 2 not taken.
|
13615 | retval = frame->ParseMsgRpc(ptr_msg, msg_size); |
408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13614 times.
|
13614 | if (!retval) { |
409 | ✗ | if (size > kMaxStackAlloc) { | |
410 | ✗ | free(buffer); | |
411 | } | ||
412 | ✗ | return false; | |
413 | } | ||
414 | |||
415 |
2/2✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 10099 times.
|
13614 | if (has_attachment) { |
416 | 3515 | const uint32_t attachment_size = size - (msg_size + kInnerHeaderSize); | |
417 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3515 times.
|
3515 | if (frame->att_size() < attachment_size) { |
418 | ✗ | if (size > kMaxStackAlloc) { | |
419 | ✗ | free(buffer); | |
420 | } | ||
421 | ✗ | return false; | |
422 | } | ||
423 | 3515 | void *ptr_attachment = reinterpret_cast<char *>(buffer) + kInnerHeaderSize | |
424 | 3515 | + msg_size; | |
425 | 3515 | memcpy(frame->attachment(), ptr_attachment, attachment_size); | |
426 | 3515 | frame->set_att_size(attachment_size); | |
427 | } else { | ||
428 | 10099 | frame->set_att_size(0); | |
429 | } | ||
430 |
2/2✓ Branch 0 taken 3295 times.
✓ Branch 1 taken 10317 times.
|
13612 | if (size > kMaxStackAlloc) { |
431 | 3295 | free(buffer); | |
432 | } | ||
433 | 13612 | return true; | |
434 | } | ||
435 | |||
436 | |||
437 | 13616 | bool CacheTransport::RecvHeader(uint32_t *size, bool *has_attachment) { | |
438 | unsigned char header[kHeaderSize]; | ||
439 |
1/2✓ Branch 1 taken 13616 times.
✗ Branch 2 not taken.
|
13616 | const ssize_t nbytes = SafeRead(fd_connection_, header, kHeaderSize); |
440 |
3/4✓ Branch 0 taken 13616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 13615 times.
|
13616 | if ((nbytes < 0) || (static_cast<unsigned>(nbytes) != kHeaderSize)) |
441 | 1 | return false; | |
442 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13615 times.
|
13615 | if ((header[0] & (~kFlagHasAttachment)) != kWireProtocolVersion) |
443 | ✗ | return false; | |
444 | 13615 | *has_attachment = header[0] & kFlagHasAttachment; | |
445 | 13615 | *size = header[1] + (header[2] << 8) + (header[3] << 16); | |
446 |
2/4✓ Branch 0 taken 13615 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13615 times.
✗ Branch 3 not taken.
|
13615 | return (*size > 0) && (*size <= kMaxMsgSize); |
447 | } | ||
448 | |||
449 | |||
450 | 13639 | void CacheTransport::SendData(void *message, | |
451 | uint32_t msg_size, | ||
452 | void *attachment, | ||
453 | uint32_t att_size) { | ||
454 | 13639 | const uint32_t total_size = | |
455 |
2/2✓ Branch 0 taken 5229 times.
✓ Branch 1 taken 8410 times.
|
13639 | msg_size + att_size + ((att_size > 0) ? kInnerHeaderSize : 0); |
456 | |||
457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13639 times.
|
13639 | assert(total_size > 0); |
458 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13639 times.
|
13639 | assert(total_size <= kMaxMsgSize); |
459 |
1/2✓ Branch 1 taken 7898 times.
✗ Branch 2 not taken.
|
7897 | 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 8411 times.
✓ Branch 1 taken 5229 times.
|
13640 | header[0] = kWireProtocolVersion | ((att_size == 0) ? 0 : kFlagHasAttachment); |
464 | 13640 | header[1] = (total_size & 0x000000FF); | |
465 | 13640 | header[2] = (total_size & 0x0000FF00) >> 8; | |
466 | 13640 | 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 | 13640 | iov[0].iov_base = header; | |
473 | 13640 | iov[0].iov_len = kHeaderSize; | |
474 | |||
475 |
2/2✓ Branch 0 taken 5229 times.
✓ Branch 1 taken 8411 times.
|
13640 | if (att_size > 0) { |
476 | 5229 | inner_header[0] = (msg_size & 0x000000FF); | |
477 | 5229 | inner_header[1] = (msg_size & 0x0000FF00) >> 8; | |
478 | 5229 | iov[1].iov_base = inner_header; | |
479 | 5229 | iov[1].iov_len = kInnerHeaderSize; | |
480 | 5229 | iov[2].iov_base = message; | |
481 | 5229 | iov[2].iov_len = msg_size; | |
482 | 5229 | iov[3].iov_base = attachment; | |
483 | 5229 | iov[3].iov_len = att_size; | |
484 | } else { | ||
485 | 8411 | iov[1].iov_base = message; | |
486 | 8411 | iov[1].iov_len = msg_size; | |
487 | } | ||
488 |
2/2✓ Branch 0 taken 1002 times.
✓ Branch 1 taken 12638 times.
|
13640 | if (flags_ & kFlagSendNonBlocking) { |
489 |
2/4✓ Branch 0 taken 1002 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1002 times.
✗ Branch 4 not taken.
|
1002 | SendNonBlocking(iov, (att_size == 0) ? 2 : 4); |
490 | 1002 | return; | |
491 | } | ||
492 |
3/4✓ Branch 0 taken 7409 times.
✓ Branch 1 taken 5229 times.
✓ Branch 3 taken 12637 times.
✗ Branch 4 not taken.
|
12638 | const bool retval = SafeWriteV(fd_connection_, iov, (att_size == 0) ? 2 : 4); |
493 | |||
494 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 12637 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
12637 | if (!retval && !(flags_ & kFlagSendIgnoreFailure)) { |
495 | ✗ | PANIC(kLogSyslogErr | kLogDebug, | |
496 | "failed to write to external cache transport (%d), aborting", errno); | ||
497 | } | ||
498 | } | ||
499 | |||
500 | 1002 | void CacheTransport::SendNonBlocking(struct iovec *iov, unsigned iovcnt) { | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1002 times.
|
1002 | assert(iovcnt > 0); |
502 | 1002 | unsigned total_size = 0; | |
503 |
2/2✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 1002 times.
|
3006 | for (unsigned i = 0; i < iovcnt; ++i) |
504 | 2004 | total_size += iov[i].iov_len; | |
505 | 1002 | unsigned char *buffer = reinterpret_cast<unsigned char *>(alloca(total_size)); | |
506 | |||
507 | 1002 | unsigned pos = 0; | |
508 |
2/2✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 1002 times.
|
3006 | for (unsigned i = 0; i < iovcnt; ++i) { |
509 | 2004 | memcpy(buffer + pos, iov[i].iov_base, iov[i].iov_len); | |
510 | 2004 | pos += iov[i].iov_len; | |
511 | } | ||
512 | |||
513 | 1002 | const int retval = send(fd_connection_, buffer, total_size, MSG_DONTWAIT); | |
514 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1002 times.
|
1002 | 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 | 1002 | } | |
523 | |||
524 | |||
525 | 13640 | void CacheTransport::SendFrame(CacheTransport::Frame *frame) { | |
526 | 13640 | cvmfs::MsgRpc *msg_rpc = frame->GetMsgRpc(); | |
527 | 13639 | const int32_t size = msg_rpc->ByteSize(); | |
528 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13639 times.
|
13639 | assert(size > 0); |
529 | #ifdef __APPLE__ | ||
530 | void *buffer = smalloc(size); | ||
531 | #else | ||
532 | 13639 | void *buffer = alloca(size); | |
533 | #endif | ||
534 | 13639 | const bool retval = msg_rpc->SerializeToArray(buffer, size); | |
535 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13639 times.
|
13639 | assert(retval); |
536 | 13639 | SendData(buffer, size, frame->attachment(), frame->att_size()); | |
537 | #ifdef __APPLE__ | ||
538 | free(buffer); | ||
539 | #endif | ||
540 | 13639 | } | |
541 |