-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.cc
200 lines (180 loc) · 6.34 KB
/
http.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2019-2020 Fastly, Inc., Toru Maesaka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "h2olog.h"
const char *HTTP_BPF = R"(
#include <linux/sched.h>
#define MAX_HDR_LEN 128
enum {
HTTP_EVENT_RECEIVE_REQ,
HTTP_EVENT_RECEIVE_REQ_HDR,
HTTP_EVENT_SEND_RESP,
HTTP_EVENT_SEND_RESP_HDR,
SCHED_PROCESS_EXIT,
};
typedef struct st_http_event_t {
uint8_t type;
uint64_t conn_id;
uint64_t req_id;
union {
uint32_t http_version;
uint32_t http_status;
struct {
uint64_t name_len;
uint64_t value_len;
char name[MAX_HDR_LEN];
char value[MAX_HDR_LEN];
} header;
};
} http_event_t;
BPF_PERF_OUTPUT(events);
int trace_sched_process_exit(struct tracepoint__sched__sched_process_exit *ctx) {
const struct task_struct *task = (const struct task_struct*)bpf_get_current_task();
pid_t h2o_pid = task->tgid;
pid_t h2o_tid = task->pid;
if (!(h2o_pid == H2OLOG_H2O_PID && h2o_tid == H2OLOG_H2O_PID)) {
return 0;
}
http_event_t ev = { .type = SCHED_PROCESS_EXIT };
events.perf_submit(ctx, &ev, sizeof(ev));
return 0;
}
int trace_receive_request(struct pt_regs *ctx) {
http_event_t ev = { .type = HTTP_EVENT_RECEIVE_REQ };
bpf_usdt_readarg(1, ctx, &ev.conn_id);
bpf_usdt_readarg(2, ctx, &ev.req_id);
bpf_usdt_readarg(3, ctx, &ev.http_version);
events.perf_submit(ctx, &ev, sizeof(ev));
return 0;
}
int trace_receive_request_header(struct pt_regs *ctx) {
http_event_t ev = { .type = HTTP_EVENT_RECEIVE_REQ_HDR };
void *pos = NULL;
bpf_usdt_readarg(1, ctx, &ev.conn_id);
bpf_usdt_readarg(2, ctx, &ev.req_id);
bpf_usdt_readarg(3, ctx, &pos);
bpf_usdt_readarg(4, ctx, &ev.header.name_len);
bpf_probe_read(&ev.header.name, sizeof(ev.header.name), pos);
bpf_usdt_readarg(5, ctx, &pos);
bpf_usdt_readarg(6, ctx, &ev.header.value_len);
bpf_probe_read(&ev.header.value, sizeof(ev.header.value), pos);
events.perf_submit(ctx, &ev, sizeof(ev));
return 0;
}
int trace_send_response(struct pt_regs *ctx) {
http_event_t ev = { .type = HTTP_EVENT_SEND_RESP };
bpf_usdt_readarg(1, ctx, &ev.conn_id);
bpf_usdt_readarg(2, ctx, &ev.req_id);
bpf_usdt_readarg(3, ctx, &ev.http_status);
events.perf_submit(ctx, &ev, sizeof(ev));
return 0;
}
int trace_send_response_header(struct pt_regs *ctx) {
http_event_t ev = { .type = HTTP_EVENT_SEND_RESP_HDR };
void *pos = NULL;
bpf_usdt_readarg(1, ctx, &ev.conn_id);
bpf_usdt_readarg(2, ctx, &ev.req_id);
bpf_usdt_readarg(3, ctx, &pos);
bpf_usdt_readarg(4, ctx, &ev.header.name_len);
bpf_probe_read(&ev.header.name, sizeof(ev.header.name), pos);
bpf_usdt_readarg(5, ctx, &pos);
bpf_usdt_readarg(6, ctx, &ev.header.value_len);
bpf_probe_read(&ev.header.value, sizeof(ev.header.value), pos);
events.perf_submit(ctx, &ev, sizeof(ev));
return 0;
}
)";
#define MAX_HDR_LEN 128
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
enum {
HTTP_EVENT_RECEIVE_REQ,
HTTP_EVENT_RECEIVE_REQ_HDR,
HTTP_EVENT_SEND_RESP,
HTTP_EVENT_SEND_RESP_HDR,
SCHED_PROCESS_EXIT,
};
typedef struct st_http_event_t {
uint8_t type;
uint64_t conn_id;
uint64_t req_id;
union {
uint32_t http_version;
uint32_t http_status;
struct {
uint64_t name_len;
uint64_t value_len;
char name[MAX_HDR_LEN];
char value[MAX_HDR_LEN];
} header;
};
} http_event_t;
static void handle_event(h2o_tracer_t *tracer, const void *data, int len)
{
const http_event_t *ev = (const http_event_t *)data;
FILE *out = tracer->out;
switch (ev->type) {
case HTTP_EVENT_RECEIVE_REQ:
fprintf(out, "%" PRIu64 " %" PRIu64 " RxProtocol HTTP/%" PRIu32 ".%" PRIu32 "\n", ev->conn_id, ev->req_id,
ev->http_version / 256, ev->http_version % 256);
break;
case HTTP_EVENT_SEND_RESP:
fprintf(out, "%" PRIu64 " %" PRIu64 " TxStatus %" PRIu32 "\n", ev->conn_id, ev->req_id, ev->http_status);
break;
case HTTP_EVENT_RECEIVE_REQ_HDR:
case HTTP_EVENT_SEND_RESP_HDR: {
int n_len = MIN(ev->header.name_len, MAX_HDR_LEN);
int v_len = MIN(ev->header.value_len, MAX_HDR_LEN);
const char *label = (ev->type == HTTP_EVENT_RECEIVE_REQ_HDR) ? "RxHeader" : "TxHeader";
fprintf(out, "%" PRIu64 " %" PRIu64 " %s %.*s %.*s\n", ev->conn_id, ev->req_id, label, n_len, ev->header.name, v_len,
ev->header.value);
} break;
case SCHED_PROCESS_EXIT: {
exit(0);
} break;
default:
fprintf(out, "unknown event: %u\n", ev->type);
}
}
static void handle_lost(h2o_tracer_t *tracer, uint64_t lost)
{
fprintf(stderr, "Possibly lost %" PRIu64 " events\n", lost);
}
static std::vector<ebpf::USDT> init_usdt_probes(pid_t h2o_pid)
{
const std::vector<ebpf::USDT> vec{
ebpf::USDT(h2o_pid, "h2o", "receive_request", "trace_receive_request"),
ebpf::USDT(h2o_pid, "h2o", "receive_request_header", "trace_receive_request_header"),
ebpf::USDT(h2o_pid, "h2o", "send_response", "trace_send_response"),
ebpf::USDT(h2o_pid, "h2o", "send_response_header", "trace_send_response_header"),
};
return vec;
}
static const char *bpf_text(void)
{
return HTTP_BPF;
}
void init_http_tracer(h2o_tracer_t *tracer)
{
tracer->handle_event = handle_event;
tracer->handle_lost = handle_lost;
tracer->init_usdt_probes = init_usdt_probes;
tracer->bpf_text = bpf_text;
}