]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_process.cc
f8d43a3258d225ee735018bb5ee11168a504ff8f
[ceph.git] / ceph / src / rgw / rgw_process.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #include "common/errno.h"
5 #include "common/Throttle.h"
6 #include "common/WorkQueue.h"
7 #include "include/scope_guard.h"
8
9 #include <utility>
10 #include "rgw_dmclock_scheduler.h"
11 #include "rgw_rest.h"
12 #include "rgw_frontend.h"
13 #include "rgw_request.h"
14 #include "rgw_process.h"
15 #include "rgw_loadgen.h"
16 #include "rgw_client_io.h"
17 #include "rgw_opa.h"
18 #include "rgw_perf_counters.h"
19 #include "rgw_lua.h"
20 #include "rgw_lua_request.h"
21 #include "rgw_tracer.h"
22 #include "rgw_ratelimit.h"
23
24 #include "services/svc_zone_utils.h"
25
26 #define dout_subsys ceph_subsys_rgw
27
28 using namespace std;
29 using rgw::dmclock::Scheduler;
30
31 void RGWProcess::RGWWQ::_dump_queue()
32 {
33 if (!g_conf()->subsys.should_gather<ceph_subsys_rgw, 20>()) {
34 return;
35 }
36 deque<RGWRequest *>::iterator iter;
37 if (process->m_req_queue.empty()) {
38 dout(20) << "RGWWQ: empty" << dendl;
39 return;
40 }
41 dout(20) << "RGWWQ:" << dendl;
42 for (iter = process->m_req_queue.begin();
43 iter != process->m_req_queue.end(); ++iter) {
44 dout(20) << "req: " << hex << *iter << dec << dendl;
45 }
46 } /* RGWProcess::RGWWQ::_dump_queue */
47
48 auto schedule_request(Scheduler *scheduler, req_state *s, RGWOp *op)
49 {
50 using rgw::dmclock::SchedulerCompleter;
51 if (!scheduler)
52 return std::make_pair(0,SchedulerCompleter{});
53
54 const auto client = op->dmclock_client();
55 const auto cost = op->dmclock_cost();
56 if (s->cct->_conf->subsys.should_gather(ceph_subsys_rgw, 10)) {
57 ldpp_dout(op,10) << "scheduling with "
58 << s->cct->_conf.get_val<std::string>("rgw_scheduler_type")
59 << " client=" << static_cast<int>(client)
60 << " cost=" << cost << dendl;
61 }
62 return scheduler->schedule_request(client, {},
63 req_state::Clock::to_double(s->time),
64 cost,
65 s->yield);
66 }
67
68 bool RGWProcess::RGWWQ::_enqueue(RGWRequest* req) {
69 process->m_req_queue.push_back(req);
70 perfcounter->inc(l_rgw_qlen);
71 dout(20) << "enqueued request req=" << hex << req << dec << dendl;
72 _dump_queue();
73 return true;
74 }
75
76 RGWRequest* RGWProcess::RGWWQ::_dequeue() {
77 if (process->m_req_queue.empty())
78 return NULL;
79 RGWRequest *req = process->m_req_queue.front();
80 process->m_req_queue.pop_front();
81 dout(20) << "dequeued request req=" << hex << req << dec << dendl;
82 _dump_queue();
83 perfcounter->inc(l_rgw_qlen, -1);
84 return req;
85 }
86
87 void RGWProcess::RGWWQ::_process(RGWRequest *req, ThreadPool::TPHandle &) {
88 perfcounter->inc(l_rgw_qactive);
89 process->handle_request(this, req);
90 process->req_throttle.put(1);
91 perfcounter->inc(l_rgw_qactive, -1);
92 }
93 bool rate_limit(rgw::sal::Store* store, req_state* s) {
94 // we dont want to limit health check or system or admin requests
95 const auto& is_admin_or_system = s->user->get_info();
96 if ((s->op_type == RGW_OP_GET_HEALTH_CHECK) || is_admin_or_system.admin || is_admin_or_system.system)
97 return false;
98 std::string userfind;
99 RGWRateLimitInfo global_user;
100 RGWRateLimitInfo global_bucket;
101 RGWRateLimitInfo global_anon;
102 RGWRateLimitInfo* bucket_ratelimit;
103 RGWRateLimitInfo* user_ratelimit;
104 store->get_ratelimit(global_bucket, global_user, global_anon);
105 bucket_ratelimit = &global_bucket;
106 user_ratelimit = &global_user;
107 s->user->get_id().to_str(userfind);
108 userfind = "u" + userfind;
109 s->ratelimit_user_name = userfind;
110 std::string bucketfind = !rgw::sal::Bucket::empty(s->bucket.get()) ? "b" + s->bucket->get_marker() : "";
111 s->ratelimit_bucket_marker = bucketfind;
112 const char *method = s->info.method;
113
114 auto iter = s->user->get_attrs().find(RGW_ATTR_RATELIMIT);
115 if(iter != s->user->get_attrs().end()) {
116 try {
117 RGWRateLimitInfo user_ratelimit_temp;
118 bufferlist& bl = iter->second;
119 auto biter = bl.cbegin();
120 decode(user_ratelimit_temp, biter);
121 // override global rate limiting only if local rate limiting is enabled
122 if (user_ratelimit_temp.enabled)
123 *user_ratelimit = user_ratelimit_temp;
124 } catch (buffer::error& err) {
125 ldpp_dout(s, 0) << "ERROR: failed to decode rate limit" << dendl;
126 return -EIO;
127 }
128 }
129 if (s->user->get_id().id == RGW_USER_ANON_ID && global_anon.enabled) {
130 *user_ratelimit = global_anon;
131 }
132 bool limit_bucket = false;
133 bool limit_user = s->ratelimit_data->should_rate_limit(method, s->ratelimit_user_name, s->time, user_ratelimit);
134
135 if(!rgw::sal::Bucket::empty(s->bucket.get()))
136 {
137 iter = s->bucket->get_attrs().find(RGW_ATTR_RATELIMIT);
138 if(iter != s->bucket->get_attrs().end()) {
139 try {
140 RGWRateLimitInfo bucket_ratelimit_temp;
141 bufferlist& bl = iter->second;
142 auto biter = bl.cbegin();
143 decode(bucket_ratelimit_temp, biter);
144 // override global rate limiting only if local rate limiting is enabled
145 if (bucket_ratelimit_temp.enabled)
146 *bucket_ratelimit = bucket_ratelimit_temp;
147 } catch (buffer::error& err) {
148 ldpp_dout(s, 0) << "ERROR: failed to decode rate limit" << dendl;
149 return -EIO;
150 }
151 }
152 if (!limit_user) {
153 limit_bucket = s->ratelimit_data->should_rate_limit(method, s->ratelimit_bucket_marker, s->time, bucket_ratelimit);
154 }
155 }
156 if(limit_bucket && !limit_user) {
157 s->ratelimit_data->giveback_tokens(method, s->ratelimit_user_name);
158 }
159 s->user_ratelimit = *user_ratelimit;
160 s->bucket_ratelimit = *bucket_ratelimit;
161 return (limit_user || limit_bucket);
162 }
163
164 int rgw_process_authenticated(RGWHandler_REST * const handler,
165 RGWOp *& op,
166 RGWRequest * const req,
167 req_state * const s,
168 optional_yield y,
169 rgw::sal::Store* store,
170 const bool skip_retarget)
171 {
172 ldpp_dout(op, 2) << "init permissions" << dendl;
173 int ret = handler->init_permissions(op, y);
174 if (ret < 0) {
175 return ret;
176 }
177
178 /**
179 * Only some accesses support website mode, and website mode does NOT apply
180 * if you are using the REST endpoint either (ergo, no authenticated access)
181 */
182 if (! skip_retarget) {
183 ldpp_dout(op, 2) << "recalculating target" << dendl;
184 ret = handler->retarget(op, &op, y);
185 if (ret < 0) {
186 return ret;
187 }
188 req->op = op;
189 } else {
190 ldpp_dout(op, 2) << "retargeting skipped because of SubOp mode" << dendl;
191 }
192
193 /* If necessary extract object ACL and put them into req_state. */
194 ldpp_dout(op, 2) << "reading permissions" << dendl;
195 ret = handler->read_permissions(op, y);
196 if (ret < 0) {
197 return ret;
198 }
199
200 ldpp_dout(op, 2) << "init op" << dendl;
201 ret = op->init_processing(y);
202 if (ret < 0) {
203 return ret;
204 }
205
206 ldpp_dout(op, 2) << "verifying op mask" << dendl;
207 ret = op->verify_op_mask();
208 if (ret < 0) {
209 return ret;
210 }
211
212 /* Check if OPA is used to authorize requests */
213 if (s->cct->_conf->rgw_use_opa_authz) {
214 ret = rgw_opa_authorize(op, s);
215 if (ret < 0) {
216 return ret;
217 }
218 }
219
220 ldpp_dout(op, 2) << "verifying op permissions" << dendl;
221 {
222 auto span = tracing::rgw::tracer.add_span("verify_permission", s->trace);
223 std::swap(span, s->trace);
224 ret = op->verify_permission(y);
225 std::swap(span, s->trace);
226 }
227 if (ret < 0) {
228 if (s->system_request) {
229 dout(2) << "overriding permissions due to system operation" << dendl;
230 } else if (s->auth.identity->is_admin_of(s->user->get_id())) {
231 dout(2) << "overriding permissions due to admin operation" << dendl;
232 } else {
233 return ret;
234 }
235 }
236
237 ldpp_dout(op, 2) << "verifying op params" << dendl;
238 ret = op->verify_params();
239 if (ret < 0) {
240 return ret;
241 }
242
243 ldpp_dout(op, 2) << "pre-executing" << dendl;
244 op->pre_exec();
245
246 ldpp_dout(op, 2) << "check rate limiting" << dendl;
247 if (rate_limit(store, s)) {
248 return -ERR_RATE_LIMITED;
249 }
250 ldpp_dout(op, 2) << "executing" << dendl;
251 {
252 auto span = tracing::rgw::tracer.add_span("execute", s->trace);
253 std::swap(span, s->trace);
254 op->execute(y);
255 std::swap(span, s->trace);
256 }
257
258 ldpp_dout(op, 2) << "completing" << dendl;
259 op->complete();
260
261 return 0;
262 }
263
264 int process_request(rgw::sal::Store* const store,
265 RGWREST* const rest,
266 RGWRequest* const req,
267 const std::string& frontend_prefix,
268 const rgw_auth_registry_t& auth_registry,
269 RGWRestfulIO* const client_io,
270 OpsLogSink* const olog,
271 optional_yield yield,
272 rgw::dmclock::Scheduler *scheduler,
273 string* user,
274 ceph::coarse_real_clock::duration* latency,
275 std::shared_ptr<RateLimiter> ratelimit,
276 int* http_ret)
277 {
278 int ret = client_io->init(g_ceph_context);
279
280 dout(1) << "====== starting new request req=" << hex << req << dec
281 << " =====" << dendl;
282 perfcounter->inc(l_rgw_req);
283
284 RGWEnv& rgw_env = client_io->get_env();
285
286 struct req_state rstate(g_ceph_context, &rgw_env, req->id);
287 struct req_state *s = &rstate;
288
289 s->ratelimit_data = ratelimit;
290 std::unique_ptr<rgw::sal::User> u = store->get_user(rgw_user());
291 s->set_user(u);
292
293 RGWObjectCtx rados_ctx(store, s);
294 s->obj_ctx = &rados_ctx;
295
296 if (ret < 0) {
297 s->cio = client_io;
298 abort_early(s, nullptr, ret, nullptr, yield);
299 return ret;
300 }
301
302 s->req_id = store->zone_unique_id(req->id);
303 s->trans_id = store->zone_unique_trans_id(req->id);
304 s->host_id = store->get_host_id();
305 s->yield = yield;
306
307 ldpp_dout(s, 2) << "initializing for trans_id = " << s->trans_id << dendl;
308
309 RGWOp* op = nullptr;
310 int init_error = 0;
311 bool should_log = false;
312 RGWRESTMgr *mgr;
313 RGWHandler_REST *handler = rest->get_handler(store, s,
314 auth_registry,
315 frontend_prefix,
316 client_io, &mgr, &init_error);
317 rgw::dmclock::SchedulerCompleter c;
318
319 if (init_error != 0) {
320 abort_early(s, nullptr, init_error, nullptr, yield);
321 goto done;
322 }
323 ldpp_dout(s, 10) << "handler=" << typeid(*handler).name() << dendl;
324
325 should_log = mgr->get_logging();
326
327 ldpp_dout(s, 2) << "getting op " << s->op << dendl;
328 op = handler->get_op();
329 if (!op) {
330 abort_early(s, NULL, -ERR_METHOD_NOT_ALLOWED, handler, yield);
331 goto done;
332 }
333 {
334 std::string script;
335 auto rc = rgw::lua::read_script(s, store, s->bucket_tenant, s->yield, rgw::lua::context::preRequest, script);
336 if (rc == -ENOENT) {
337 // no script, nothing to do
338 } else if (rc < 0) {
339 ldpp_dout(op, 5) << "WARNING: failed to read pre request script. error: " << rc << dendl;
340 } else {
341 rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script);
342 if (rc < 0) {
343 ldpp_dout(op, 5) << "WARNING: failed to execute pre request script. error: " << rc << dendl;
344 }
345 }
346 }
347 std::tie(ret,c) = schedule_request(scheduler, s, op);
348 if (ret < 0) {
349 if (ret == -EAGAIN) {
350 ret = -ERR_RATE_LIMITED;
351 }
352 ldpp_dout(op,0) << "Scheduling request failed with " << ret << dendl;
353 abort_early(s, op, ret, handler, yield);
354 goto done;
355 }
356 req->op = op;
357 ldpp_dout(op, 10) << "op=" << typeid(*op).name() << dendl;
358 s->op_type = op->get_type();
359
360 try {
361 ldpp_dout(op, 2) << "verifying requester" << dendl;
362 ret = op->verify_requester(auth_registry, yield);
363 if (ret < 0) {
364 dout(10) << "failed to authorize request" << dendl;
365 abort_early(s, op, ret, handler, yield);
366 goto done;
367 }
368
369 /* FIXME: remove this after switching all handlers to the new authentication
370 * infrastructure. */
371 if (nullptr == s->auth.identity) {
372 s->auth.identity = rgw::auth::transform_old_authinfo(s);
373 }
374
375 ldpp_dout(op, 2) << "normalizing buckets and tenants" << dendl;
376 ret = handler->postauth_init(yield);
377 if (ret < 0) {
378 dout(10) << "failed to run post-auth init" << dendl;
379 abort_early(s, op, ret, handler, yield);
380 goto done;
381 }
382
383 if (s->user->get_info().suspended) {
384 dout(10) << "user is suspended, uid=" << s->user->get_id() << dendl;
385 abort_early(s, op, -ERR_USER_SUSPENDED, handler, yield);
386 goto done;
387 }
388
389 const auto trace_name = std::string(op->name()) + " " + s->trans_id;
390 s->trace = tracing::rgw::tracer.start_trace(trace_name);
391 s->trace->SetAttribute(tracing::rgw::OP, op->name());
392 s->trace->SetAttribute(tracing::rgw::TYPE, tracing::rgw::REQUEST);
393
394 ret = rgw_process_authenticated(handler, op, req, s, yield, store);
395 if (ret < 0) {
396 abort_early(s, op, ret, handler, yield);
397 goto done;
398 }
399 } catch (const ceph::crypto::DigestException& e) {
400 dout(0) << "authentication failed" << e.what() << dendl;
401 abort_early(s, op, -ERR_INVALID_SECRET_KEY, handler, yield);
402 }
403
404 done:
405 if (op) {
406 s->trace->SetAttribute(tracing::rgw::RETURN, op->get_ret());
407 if (s->user) {
408 s->trace->SetAttribute(tracing::rgw::USER_ID, s->user->get_id().id);
409 }
410 if (s->bucket) {
411 s->trace->SetAttribute(tracing::rgw::BUCKET_NAME, s->bucket->get_name());
412 }
413 if (s->object) {
414 s->trace->SetAttribute(tracing::rgw::OBJECT_NAME, s->object->get_name());
415 }
416 std::string script;
417 auto rc = rgw::lua::read_script(s, store, s->bucket_tenant, s->yield, rgw::lua::context::postRequest, script);
418 if (rc == -ENOENT) {
419 // no script, nothing to do
420 } else if (rc < 0) {
421 ldpp_dout(op, 5) << "WARNING: failed to read post request script. error: " << rc << dendl;
422 } else {
423 rc = rgw::lua::request::execute(store, rest, olog, s, op->name(), script);
424 if (rc < 0) {
425 ldpp_dout(op, 5) << "WARNING: failed to execute post request script. error: " << rc << dendl;
426 }
427 }
428 }
429
430 try {
431 client_io->complete_request();
432 } catch (rgw::io::Exception& e) {
433 dout(0) << "ERROR: client_io->complete_request() returned "
434 << e.what() << dendl;
435 }
436 if (should_log) {
437 rgw_log_op(rest, s, (op ? op->name() : "unknown"), olog);
438 }
439
440 if (http_ret != nullptr) {
441 *http_ret = s->err.http_ret;
442 }
443 int op_ret = 0;
444
445 if (user && !rgw::sal::User::empty(s->user.get())) {
446 *user = s->user->get_id().to_str();
447 }
448
449 if (op) {
450 op_ret = op->get_ret();
451 ldpp_dout(op, 2) << "op status=" << op_ret << dendl;
452 ldpp_dout(op, 2) << "http status=" << s->err.http_ret << dendl;
453 } else {
454 ldpp_dout(s, 2) << "http status=" << s->err.http_ret << dendl;
455 }
456 if (handler)
457 handler->put_op(op);
458 rest->put_handler(handler);
459
460 const auto lat = s->time_elapsed();
461 if (latency) {
462 *latency = lat;
463 }
464 dout(1) << "====== req done req=" << hex << req << dec
465 << " op status=" << op_ret
466 << " http_status=" << s->err.http_ret
467 << " latency=" << lat
468 << " ======"
469 << dendl;
470
471 return (ret < 0 ? ret : s->err.ret);
472 } /* process_request */