Fix compilation on centos 7
[fio.git] / engines / http.c
1 /*
2  * HTTP GET/PUT IO engine
3  *
4  * IO engine to perform HTTP(S) GET/PUT requests via libcurl-easy.
5  *
6  * Copyright (C) 2018 SUSE LLC
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License,
10  * version 2 as published by the Free Software Foundation..
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include <pthread.h>
24 #include <time.h>
25 #include <curl/curl.h>
26 #include <openssl/hmac.h>
27 #include <openssl/sha.h>
28 #include "fio.h"
29 #include "../optgroup.h"
30
31
32 struct http_data {
33         CURL *curl;
34 };
35
36 struct http_options {
37         void *pad;
38         int  https;
39         char *host;
40         char *user;
41         char *pass;
42         char *s3_key;
43         char *s3_keyid;
44         char *s3_region;
45         int verbose;
46         int s3;
47 };
48
49 struct http_curl_stream {
50         char *buf;
51         size_t pos;
52         size_t max;
53 };
54
55 static struct fio_option options[] = {
56         {
57                 .name     = "https",
58                 .lname    = "https",
59                 .type     = FIO_OPT_BOOL,
60                 .help     = "Enable https",
61                 .off1     = offsetof(struct http_options, https),
62                 .def      = "0",
63                 .category = FIO_OPT_C_ENGINE,
64                 .group    = FIO_OPT_G_HTTP,
65         },
66         {
67                 .name     = "http_host",
68                 .lname    = "http_host",
69                 .type     = FIO_OPT_STR_STORE,
70                 .help     = "Hostname (S3 bucket)",
71                 .off1     = offsetof(struct http_options, host),
72                 .def      = "localhost",
73                 .category = FIO_OPT_C_ENGINE,
74                 .group    = FIO_OPT_G_HTTP,
75         },
76         {
77                 .name     = "http_user",
78                 .lname    = "http_user",
79                 .type     = FIO_OPT_STR_STORE,
80                 .help     = "HTTP user name",
81                 .off1     = offsetof(struct http_options, user),
82                 .category = FIO_OPT_C_ENGINE,
83                 .group    = FIO_OPT_G_HTTP,
84         },
85         {
86                 .name     = "http_pass",
87                 .lname    = "http_pass",
88                 .type     = FIO_OPT_STR_STORE,
89                 .help     = "HTTP password",
90                 .off1     = offsetof(struct http_options, pass),
91                 .category = FIO_OPT_C_ENGINE,
92                 .group    = FIO_OPT_G_HTTP,
93         },
94         {
95                 .name     = "http_s3_key",
96                 .lname    = "S3 secret key",
97                 .type     = FIO_OPT_STR_STORE,
98                 .help     = "S3 secret key",
99                 .off1     = offsetof(struct http_options, s3_key),
100                 .def      = "",
101                 .category = FIO_OPT_C_ENGINE,
102                 .group    = FIO_OPT_G_HTTP,
103         },
104         {
105                 .name     = "http_s3_keyid",
106                 .lname    = "S3 key id",
107                 .type     = FIO_OPT_STR_STORE,
108                 .help     = "S3 key id",
109                 .off1     = offsetof(struct http_options, s3_keyid),
110                 .def      = "",
111                 .category = FIO_OPT_C_ENGINE,
112                 .group    = FIO_OPT_G_HTTP,
113         },
114         {
115                 .name     = "http_s3_region",
116                 .lname    = "S3 region",
117                 .type     = FIO_OPT_STR_STORE,
118                 .help     = "S3 region",
119                 .off1     = offsetof(struct http_options, s3_region),
120                 .def      = "us-east-1",
121                 .category = FIO_OPT_C_ENGINE,
122                 .group    = FIO_OPT_G_HTTP,
123         },
124         {
125                 .name     = "http_s3",
126                 .lname    = "S3 extensions",
127                 .type     = FIO_OPT_BOOL,
128                 .help     = "Whether to enable S3 specific headers",
129                 .off1     = offsetof(struct http_options, s3),
130                 .def      = "0",
131                 .category = FIO_OPT_C_ENGINE,
132                 .group    = FIO_OPT_G_HTTP,
133         },
134         {
135                 .name     = "http_verbose",
136                 .lname    = "CURL verbosity",
137                 .type     = FIO_OPT_INT,
138                 .help     = "increase http engine verbosity",
139                 .off1     = offsetof(struct http_options, verbose),
140                 .def      = "0",
141                 .category = FIO_OPT_C_ENGINE,
142                 .group    = FIO_OPT_G_HTTP,
143         },
144         {
145                 .name     = NULL,
146         },
147 };
148
149 static char *_aws_uriencode(const char *uri)
150 {
151         size_t bufsize = 1024;
152         char *r = malloc(bufsize);
153         char c;
154         int i, n;
155         const char *hex = "0123456789ABCDEF";
156
157         if (!r) {
158                 log_err("malloc failed\n");
159                 return NULL;
160         }
161
162         n = 0;
163         for (i = 0; (c = uri[i]); i++) {
164                 if (n > bufsize-5) {
165                         log_err("encoding the URL failed\n");
166                         return NULL;
167                 }
168
169                 if ( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
170                 || (c >= '0' && c <= '9') || c == '_' || c == '-'
171                 || c == '~' || c == '.' || c == '/')
172                         r[n++] = c;
173                 else {
174                         r[n++] = '%';
175                         r[n++] = hex[(c >> 4 ) & 0xF];
176                         r[n++] = hex[c & 0xF];
177                 }
178         }
179         r[n++] = 0;
180         return r;
181 }
182
183 static char *_conv_hex(const unsigned char *p, size_t len)
184 {
185         char *r;
186         int i,n;
187         const char *hex = "0123456789abcdef";
188         r = malloc(len * 2 + 1);
189         n = 0;
190         for (i = 0; i < len; i++) {
191                 r[n++] = hex[(p[i] >> 4 ) & 0xF];
192                 r[n++] = hex[p[i] & 0xF];
193         }
194         r[n] = 0;
195
196         return r;
197 }
198
199 static char *_gen_hex_sha256(const char *p, size_t len)
200 {
201         unsigned char hash[SHA256_DIGEST_LENGTH];
202
203         SHA256((unsigned char*)p, len, hash);
204         return _conv_hex(hash, SHA256_DIGEST_LENGTH);
205 }
206
207 static void _hmac(unsigned char *md, void *key, int key_len, char *data) {
208         HMAC_CTX *ctx;
209         unsigned int hmac_len;
210
211         ctx = HMAC_CTX_new();
212         HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), NULL);
213         HMAC_Update(ctx, (unsigned char*)data, strlen(data));
214         HMAC_Final(ctx, md, &hmac_len);
215         HMAC_CTX_free(ctx);
216 }
217
218 static int _curl_trace(CURL *handle, curl_infotype type,
219              char *data, size_t size,
220              void *userp)
221 {
222         const char *text;
223         (void)handle; /* prevent compiler warning */
224         (void)userp;
225
226         switch (type) {
227         case CURLINFO_TEXT:
228         fprintf(stderr, "== Info: %s", data);
229         default:
230         case CURLINFO_SSL_DATA_OUT:
231         case CURLINFO_SSL_DATA_IN:
232                 return 0;
233
234         case CURLINFO_HEADER_OUT:
235                 text = "=> Send header";
236                 break;
237         case CURLINFO_DATA_OUT:
238                 text = "=> Send data";
239                 break;
240         case CURLINFO_HEADER_IN:
241                 text = "<= Recv header";
242                 break;
243         case CURLINFO_DATA_IN:
244                 text = "<= Recv data";
245                 break;
246         }
247
248         log_info("%s: %s", text, data);
249         return 0;
250 }
251
252 /* https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
253  * https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html#signing-request-intro
254  */
255 static void _add_aws_auth_header(CURL *curl, struct curl_slist *slist, struct http_options *o,
256                 int op, const char *uri, char *buf, size_t len)
257 {
258         char date_short[16];
259         char date_iso[32];
260         char method[8];
261         char dkey[128];
262         char creq[512];
263         char sts[256];
264         char s[512];
265         char *uri_encoded = NULL;
266         char *dsha = NULL;
267         char *csha = NULL;
268         char *signature = NULL;
269         const char *service = "s3";
270         const char *aws = "aws4_request";
271         unsigned char md[SHA256_DIGEST_LENGTH];
272
273         time_t t = time(NULL);
274         struct tm *gtm = gmtime(&t);
275
276         strftime (date_short, sizeof(date_short), "%Y%m%d", gtm);
277         strftime (date_iso, sizeof(date_iso), "%Y%m%dT%H%M%SZ", gtm);
278         uri_encoded = _aws_uriencode(uri);
279
280         if (op == DDIR_WRITE) {
281                 dsha = _gen_hex_sha256(buf, len);
282                 sprintf(method, "PUT");
283         } else {
284                 /* DDIR_READ && DDIR_TRIM supply an empty body */
285                 if (op == DDIR_READ)
286                         sprintf(method, "GET");
287                 else
288                         sprintf(method, "DELETE");
289                 dsha = _gen_hex_sha256("", 0);
290         }
291
292         /* Create the canonical request first */
293         snprintf(creq, sizeof(creq),
294         "%s\n"
295         "%s\n"
296         "\n"
297         "host:%s\n"
298         "x-amz-content-sha256:%s\n"
299         "x-amz-date:%s\n"
300         "\n"
301         "host;x-amz-content-sha256;x-amz-date\n"
302         "%s"
303         , method
304         , uri_encoded, o->host, dsha, date_iso, dsha);
305
306         csha = _gen_hex_sha256(creq, strlen(creq));
307         snprintf(sts, sizeof(sts), "AWS4-HMAC-SHA256\n%s\n%s/%s/%s/%s\n%s",
308                 date_iso, date_short, o->s3_region, service, aws, csha);
309
310         snprintf((char *)dkey, sizeof(dkey), "AWS4%s", o->s3_key);
311         _hmac(md, dkey, strlen(dkey), date_short);
312         _hmac(md, md, SHA256_DIGEST_LENGTH, o->s3_region);
313         _hmac(md, md, SHA256_DIGEST_LENGTH, (char*) service);
314         _hmac(md, md, SHA256_DIGEST_LENGTH, (char*) aws);
315         _hmac(md, md, SHA256_DIGEST_LENGTH, sts);
316
317         signature = _conv_hex(md, SHA256_DIGEST_LENGTH);
318
319         /* Surpress automatic Accept: header */
320         slist = curl_slist_append(slist, "Accept:");
321
322         snprintf(s, sizeof(s), "x-amz-content-sha256: %s", dsha);
323         slist = curl_slist_append(slist, s);
324
325         snprintf(s, sizeof(s), "x-amz-date: %s", date_iso);
326         slist = curl_slist_append(slist, s);
327
328         snprintf(s, sizeof(s), "Authorization: AWS4-HMAC-SHA256 Credential=%s/%s/%s/s3/aws4_request,"
329         "SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=%s",
330         o->s3_keyid, date_short, o->s3_region, signature);
331         slist = curl_slist_append(slist, s);
332
333         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
334
335         free(uri_encoded);
336         free(csha);
337         free(dsha);
338         free(signature);
339 }
340
341 static void fio_http_cleanup(struct thread_data *td)
342 {
343         struct http_data *http = td->io_ops_data;
344
345         if (http) {
346                 curl_easy_cleanup(http->curl);
347                 free(http);
348         }
349 }
350
351 static size_t _http_read(void *ptr, size_t size, size_t nmemb, void *stream)
352 {
353         struct http_curl_stream *state = stream;
354         size_t len = size * nmemb;
355         /* We're retrieving; nothing is supposed to be read locally */
356         if (!stream)
357                 return 0;
358         if (len+state->pos > state->max)
359                 len = state->max - state->pos;
360         memcpy(ptr, &state->buf[state->pos], len);
361         state->pos += len;
362         return len;
363 }
364
365 static size_t _http_write(void *ptr, size_t size, size_t nmemb, void *stream)
366 {
367         struct http_curl_stream *state = stream;
368         /* We're just discarding the returned body after a PUT */
369         if (!stream)
370                 return nmemb;
371         if (size != 1)
372                 return CURLE_WRITE_ERROR;
373         if (nmemb + state->pos > state->max)
374                 return CURLE_WRITE_ERROR;
375         memcpy(&state->buf[state->pos], ptr, nmemb);
376         state->pos += nmemb;
377         return nmemb;
378 }
379
380 static int _http_seek(void *stream, curl_off_t offset, int origin)
381 {
382         struct http_curl_stream *state = stream;
383         if (offset < state->max && origin == SEEK_SET) {
384                 state->pos = offset;
385                 return CURL_SEEKFUNC_OK;
386         } else
387                 return CURL_SEEKFUNC_FAIL;
388 }
389
390 static enum fio_q_status fio_http_queue(struct thread_data *td,
391                                          struct io_u *io_u)
392 {
393         struct http_data *http = td->io_ops_data;
394         struct http_options *o = td->eo;
395         struct http_curl_stream _curl_stream;
396         struct curl_slist *slist = NULL;
397         char object[512];
398         char url[1024];
399         long status;
400         CURLcode res;
401         int r = -1;
402
403         fio_ro_check(td, io_u);
404         memset(&_curl_stream, 0, sizeof(_curl_stream));
405         snprintf(object, sizeof(object), "%s_%llu_%llu", td->files[0]->file_name, io_u->offset, io_u->xfer_buflen);
406         snprintf(url, sizeof(url), "%s://%s%s", o->https ? "https" : "http", o->host, object);
407         curl_easy_setopt(http->curl, CURLOPT_URL, url);
408         _curl_stream.buf = io_u->xfer_buf;
409         _curl_stream.max = io_u->xfer_buflen;
410         curl_easy_setopt(http->curl, CURLOPT_SEEKDATA, &_curl_stream);
411         curl_easy_setopt(http->curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)io_u->xfer_buflen);
412
413         if (o->s3)
414                 _add_aws_auth_header(http->curl, slist, o, io_u->ddir, object,
415                         io_u->xfer_buf, io_u->xfer_buflen);
416
417         if (io_u->ddir == DDIR_WRITE) {
418                 curl_easy_setopt(http->curl, CURLOPT_READDATA, &_curl_stream);
419                 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, NULL);
420                 curl_easy_setopt(http->curl, CURLOPT_UPLOAD, 1L);
421                 res = curl_easy_perform(http->curl);
422                 if (res == CURLE_OK) {
423                         curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
424                         if (status == 100 || (status >= 200 && status <= 204))
425                                 goto out;
426                         log_err("DDIR_WRITE failed with HTTP status code %ld\n", status);
427                         goto err;
428                 }
429         } else if (io_u->ddir == DDIR_READ) {
430                 curl_easy_setopt(http->curl, CURLOPT_READDATA, NULL);
431                 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, &_curl_stream);
432                 curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
433                 res = curl_easy_perform(http->curl);
434                 if (res == CURLE_OK) {
435                         curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
436                         if (status == 200)
437                                 goto out;
438                         else if (status == 404) {
439                                 /* Object doesn't exist. Pretend we read
440                                  * zeroes */
441                                 memset(io_u->xfer_buf, 0, io_u->xfer_buflen);
442                                 goto out;
443                         }
444                         log_err("DDIR_READ failed with HTTP status code %ld\n", status);
445                 }
446                 goto err;
447         } else if (io_u->ddir == DDIR_TRIM) {
448                 curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
449                 curl_easy_setopt(http->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
450                 curl_easy_setopt(http->curl, CURLOPT_INFILESIZE_LARGE, 0);
451                 curl_easy_setopt(http->curl, CURLOPT_READDATA, NULL);
452                 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, NULL);
453                 res = curl_easy_perform(http->curl);
454                 if (res == CURLE_OK) {
455                         curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
456                         if (status == 200 || status == 202 || status == 204 || status == 404)
457                                 goto out;
458                         log_err("DDIR_TRIM failed with HTTP status code %ld\n", status);
459                 }
460                 goto err;
461         }
462
463         log_err("WARNING: Only DDIR_READ/DDIR_WRITE/DDIR_TRIM are supported!\n");
464
465 err:
466         io_u->error = r;
467         td_verror(td, io_u->error, "transfer");
468 out:
469         curl_slist_free_all(slist);
470         return FIO_Q_COMPLETED;
471 }
472
473 static struct io_u *fio_http_event(struct thread_data *td, int event)
474 {
475         /* sync IO engine - never any outstanding events */
476         return NULL;
477 }
478
479 int fio_http_getevents(struct thread_data *td, unsigned int min,
480         unsigned int max, const struct timespec *t)
481 {
482         /* sync IO engine - never any outstanding events */
483         return 0;
484 }
485
486 static int fio_http_setup(struct thread_data *td)
487 {
488         struct http_data *http = NULL;
489         struct http_options *o = td->eo;
490         int r;
491         /* allocate engine specific structure to deal with libhttp. */
492         http = calloc(1, sizeof(*http));
493         if (!http) {
494                 log_err("calloc failed.\n");
495                 goto cleanup;
496         }
497
498         http->curl = curl_easy_init();
499         if (o->verbose)
500                 curl_easy_setopt(http->curl, CURLOPT_VERBOSE, 1L);
501         if (o->verbose > 1)
502                 curl_easy_setopt(http->curl, CURLOPT_DEBUGFUNCTION, &_curl_trace);
503         curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, 1L);
504         curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 1L);
505         curl_easy_setopt(http->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
506         curl_easy_setopt(http->curl, CURLOPT_READFUNCTION, _http_read);
507         curl_easy_setopt(http->curl, CURLOPT_WRITEFUNCTION, _http_write);
508         curl_easy_setopt(http->curl, CURLOPT_SEEKFUNCTION, _http_seek);
509         if (o->user && o->pass) {
510                 curl_easy_setopt(http->curl, CURLOPT_USERNAME, o->user);
511                 curl_easy_setopt(http->curl, CURLOPT_PASSWORD, o->pass);
512                 curl_easy_setopt(http->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
513         }
514
515         td->io_ops_data = http;
516
517         /* Force single process mode. */
518         td->o.use_thread = 1;
519
520         return 0;
521 cleanup:
522         fio_http_cleanup(td);
523         return r;
524 }
525
526 static int fio_http_open(struct thread_data *td, struct fio_file *f)
527 {
528         return 0;
529 }
530 static int fio_http_invalidate(struct thread_data *td, struct fio_file *f)
531 {
532         return 0;
533 }
534
535 static struct ioengine_ops ioengine = {
536         .name = "http",
537         .version                = FIO_IOOPS_VERSION,
538         .flags                  = FIO_DISKLESSIO,
539         .setup                  = fio_http_setup,
540         .queue                  = fio_http_queue,
541         .getevents              = fio_http_getevents,
542         .event                  = fio_http_event,
543         .cleanup                = fio_http_cleanup,
544         .open_file              = fio_http_open,
545         .invalidate             = fio_http_invalidate,
546         .options                = options,
547         .option_struct_size     = sizeof(struct http_options),
548 };
549
550 static void fio_init fio_http_register(void)
551 {
552         register_ioengine(&ioengine);
553 }
554
555 static void fio_exit fio_http_unregister(void)
556 {
557         unregister_ioengine(&ioengine);
558 }