1 /*-
2 * Copyright (c) 2006 Verdens Gang AS
3 * Copyright (c) 2006-2015 Varnish Software AS
4 * All rights reserved.
5 *
6 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * Stuff that should *never* be exposed to a VMOD
32 */
33
34 #include "cache.h"
35
36 #include "vsb.h"
37
38 #include <sys/socket.h>
39
40 #include <string.h>
41 #include <limits.h>
42 #include <unistd.h>
43
44 #include "common/common_param.h"
45
46 #ifdef NOT_IN_A_VMOD
47 # include "VSC_main.h"
48 #endif
49
50 /*--------------------------------------------------------------------*/
51
52 struct vfp;
53 struct vdp;
54 struct cli_proto;
55 struct poolparam;
56
57 /*--------------------------------------------------------------------*/
58
59 typedef enum req_fsm_nxt req_state_f(struct worker *, struct req *);
60 struct req_step {
61 const char *name;
62 req_state_f *func;
63 };
64
65 extern const struct req_step R_STP_TRANSPORT[1];
66 extern const struct req_step R_STP_RECV[1];
67
68 /*--------------------------------------------------------------------
69 * HTTP Protocol connection structure
70 *
71 * This is the protocol independent object for a HTTP connection, used
72 * both for backend and client sides.
73 *
74 */
75
76 struct http_conn {
77 unsigned magic;
78 #define HTTP_CONN_MAGIC 0x3e19edd1
79
80 int *rfd;
81 enum sess_close doclose;
82 body_status_t body_status;
83 struct ws *ws;
84 char *rxbuf_b;
85 char *rxbuf_e;
86 char *pipeline_b;
87 char *pipeline_e;
88 ssize_t content_length;
89 void *priv;
90
91 /* Timeouts */
92 vtim_dur first_byte_timeout;
93 vtim_dur between_bytes_timeout;
94 };
95
96 enum htc_status_e {
97 #define HTC_STATUS(e, n, s, l) HTC_S_ ## e = n,
98 #include "tbl/htc.h"
99 };
100
101 typedef enum htc_status_e htc_complete_f(struct http_conn *);
102
103 /* -------------------------------------------------------------------*/
104
105 extern volatile struct params * cache_param;
106
107 /* -------------------------------------------------------------------
108 * The VCF facility is deliberately undocumented, use at your peril.
109 */
110
111 struct vcf_return {
112 const char *name;
113 };
114
115 #define VCF_RETURNS() \
116 VCF_RETURN(CONTINUE) \
117 VCF_RETURN(DEFAULT) \
118 VCF_RETURN(MISS) \
119 VCF_RETURN(HIT)
120
121 #define VCF_RETURN(x) extern const struct vcf_return VCF_##x[1];
122 VCF_RETURNS()
123 #undef VCF_RETURN
124
125 typedef const struct vcf_return *vcf_func_f(
126 struct req *req,
127 struct objcore **oc,
128 struct objcore **oc_exp,
129 int state);
130
131 struct vcf {
132 unsigned magic;
133 #define VCF_MAGIC 0x183285d1
134 vcf_func_f *func;
135 void *priv;
136 };
137
138 /* Prototypes etc ----------------------------------------------------*/
139
140 /* cache_acceptor.c */
141 void VCA_Init(void);
142 void VCA_Shutdown(void);
143
144 /* cache_backend_cfg.c */
145 void VBE_InitCfg(void);
146 void VBE_Poll(void);
147
148 void VBP_Init(void);
149
150 /* cache_ban.c */
151
152 /* for stevedoes resurrecting bans */
153 void BAN_Hold(void);
154 void BAN_Release(void);
155 void BAN_Reload(const uint8_t *ban, unsigned len);
156 struct ban *BAN_FindBan(vtim_real t0);
157 void BAN_RefBan(struct objcore *oc, struct ban *);
158 vtim_real BAN_Time(const struct ban *ban);
159
160 /* cache_busyobj.c */
161 struct busyobj *VBO_GetBusyObj(struct worker *, const struct req *);
162 void VBO_ReleaseBusyObj(struct worker *wrk, struct busyobj **busyobj);
163
164 /* cache_director.c */
165 int VDI_GetHdr(struct busyobj *);
166 VCL_IP VDI_GetIP(struct busyobj *);
167 void VDI_Finish(struct busyobj *bo);
168 enum sess_close VDI_Http1Pipe(struct req *, struct busyobj *);
169 void VDI_Panic(const struct director *, struct vsb *, const char *nm);
170 void VDI_Event(const struct director *d, enum vcl_event_e ev);
171 void VDI_Init(void);
172
173 /* cache_deliver_proc.c */
174 void VDP_Init(struct vdp_ctx *vdx, struct worker *wrk, struct vsl_log *vsl,
175 struct req *req);
176 uint64_t VDP_Close(struct vdp_ctx *);
177 void VDP_Panic(struct vsb *vsb, const struct vdp_ctx *vdc);
178 int VDP_Push(struct vdp_ctx *, struct ws *, const struct vdp *, void *priv);
179 int VDP_DeliverObj(struct vdp_ctx *vdc, struct objcore *oc);
180 extern const struct vdp VDP_gunzip;
181 extern const struct vdp VDP_esi;
182 extern const struct vdp VDP_range;
183
184
185 /* cache_exp.c */
186 vtim_real EXP_Ttl(const struct req *, const struct objcore *);
187 vtim_real EXP_Ttl_grace(const struct req *, const struct objcore *oc);
188 void EXP_RefNewObjcore(struct objcore *);
189 void EXP_Insert(struct worker *wrk, struct objcore *oc);
190 void EXP_Remove(struct objcore *);
191
192 #define EXP_Dttl(req, oc) (oc->ttl - (req->t_req - oc->t_origin))
193
194 /* cache_expire.c */
195
196 /*
197 * The set of variables which control object expiry are inconveniently
198 * 24 bytes long (double+3*float) and this causes alignment waste if
199 * we put then in a struct.
200 * These three macros operate on the struct we don't use.
201 */
202
203 #define EXP_ZERO(xx) \
204 do { \
205 (xx)->t_origin = 0.0; \
206 (xx)->ttl = 0.0; \
207 (xx)->grace = 0.0; \
208 (xx)->keep = 0.0; \
209 } while (0)
210
211 #define EXP_COPY(to,fm) \
212 do { \
213 (to)->t_origin = (fm)->t_origin; \
214 (to)->ttl = (fm)->ttl; \
215 (to)->grace = (fm)->grace; \
216 (to)->keep = (fm)->keep; \
217 } while (0)
218
219 #define EXP_WHEN(to) \
220 ((to)->t_origin + (to)->ttl + (to)->grace + (to)->keep)
221
222 /* cache_exp.c */
223 void EXP_Rearm(struct objcore *oc, vtim_real now,
224 vtim_dur ttl, vtim_dur grace, vtim_dur keep);
225
226 /* From cache_main.c */
227 extern int cache_shutdown;
228 void BAN_Init(void);
229 void BAN_Compile(void);
230 void BAN_Shutdown(void);
231
232 /* From cache_hash.c */
233 void BAN_NewObjCore(struct objcore *oc);
234 void BAN_DestroyObj(struct objcore *oc);
235 int BAN_CheckObject(struct worker *, struct objcore *, struct req *);
236
237 /* cache_busyobj.c */
238 void VBO_Init(void);
239
240 /* cache_cli.c [CLI] */
241 void CLI_Init(void);
242 void CLI_Run(void);
243 void CLI_AddFuncs(struct cli_proto *p);
244
245 /* cache_expire.c */
246 void EXP_Init(void);
247 void EXP_Shutdown(void);
248
249 /* cache_fetch.c */
250 enum vbf_fetch_mode_e {
251 VBF_NORMAL = 0,
252 VBF_PASS = 1,
253 VBF_BACKGROUND = 2,
254 };
255 void VBF_Fetch(struct worker *wrk, struct req *req,
256 struct objcore *oc, struct objcore *oldoc, enum vbf_fetch_mode_e);
257 const char *VBF_Get_Filter_List(struct busyobj *);
258 void Bereq_Rollback(VRT_CTX);
259
260 /* cache_fetch_proc.c */
261 void VFP_Init(void);
262 struct vfp_entry *VFP_Push(struct vfp_ctx *, const struct vfp *);
263 enum vfp_status VFP_GetStorage(struct vfp_ctx *, ssize_t *sz, uint8_t **ptr);
264 void VFP_Extend(const struct vfp_ctx *, ssize_t sz, enum vfp_status);
265 void VFP_Setup(struct vfp_ctx *vc, struct worker *wrk);
266 int VFP_Open(struct vfp_ctx *);
267 uint64_t VFP_Close(struct vfp_ctx *);
268
269 extern const struct vfp VFP_gunzip;
270 extern const struct vfp VFP_gzip;
271 extern const struct vfp VFP_testgunzip;
272 extern const struct vfp VFP_esi;
273 extern const struct vfp VFP_esi_gzip;
274
275 /* cache_http.c */
276 void HTTP_Init(void);
277
278 /* cache_http1_proto.c */
279
280 htc_complete_f HTTP1_Complete;
281 uint16_t HTTP1_DissectRequest(struct http_conn *, struct http *);
282 uint16_t HTTP1_DissectResponse(struct http_conn *, struct http *resp,
283 const struct http *req);
284 unsigned HTTP1_Write(const struct worker *w, const struct http *hp, const int*);
285
286 /* cache_main.c */
287 void THR_SetName(const char *name);
288 const char* THR_GetName(void);
289 void THR_SetBusyobj(const struct busyobj *);
290 struct busyobj * THR_GetBusyobj(void);
291 void THR_SetRequest(const struct req *);
292 struct req * THR_GetRequest(void);
293 void THR_Init(void);
294
295 /* cache_lck.c */
296 void LCK_Init(void);
297
298 /* cache_mempool.c */
299 void MPL_AssertSane(const void *item);
300 struct mempool * MPL_New(const char *name, volatile struct poolparam *pp,
301 volatile unsigned *cur_size);
302 void MPL_Destroy(struct mempool **mpp);
303 void *MPL_Get(struct mempool *mpl, unsigned *size);
304 void MPL_Free(struct mempool *mpl, void *item);
305
306 /* cache_obj.c */
307 void ObjInit(void);
308 struct objcore * ObjNew(const struct worker *);
309 void ObjDestroy(const struct worker *, struct objcore **);
310 int ObjGetSpace(struct worker *, struct objcore *, ssize_t *sz, uint8_t **ptr);
311 void ObjExtend(struct worker *, struct objcore *, ssize_t l, int final);
312 uint64_t ObjWaitExtend(const struct worker *, const struct objcore *,
313 uint64_t l);
314 void ObjSetState(struct worker *, const struct objcore *,
315 enum boc_state_e next);
316 void ObjWaitState(const struct objcore *, enum boc_state_e want);
317 void ObjTouch(struct worker *, struct objcore *, vtim_real now);
318 void ObjFreeObj(struct worker *, struct objcore *);
319 void ObjSlim(struct worker *, struct objcore *);
320 void *ObjSetAttr(struct worker *, struct objcore *, enum obj_attr,
321 ssize_t len, const void *);
322 int ObjCopyAttr(struct worker *, struct objcore *, struct objcore *,
323 enum obj_attr attr);
324 void ObjBocDone(struct worker *, struct objcore *, struct boc **);
325
326 int ObjSetDouble(struct worker *, struct objcore *, enum obj_attr, double);
327 int ObjSetU32(struct worker *, struct objcore *, enum obj_attr, uint32_t);
328 int ObjSetU64(struct worker *, struct objcore *, enum obj_attr, uint64_t);
329
330 void ObjSetFlag(struct worker *, struct objcore *, enum obj_flags of, int val);
331
332 void ObjSendEvent(struct worker *, struct objcore *oc, unsigned event);
333
334 #define OEV_INSERT (1U<<1)
335 #define OEV_BANCHG (1U<<2)
336 #define OEV_TTLCHG (1U<<3)
337 #define OEV_EXPIRE (1U<<4)
338
339 #define OEV_MASK (OEV_INSERT|OEV_BANCHG|OEV_TTLCHG|OEV_EXPIRE)
340
341 typedef void obj_event_f(struct worker *, void *priv, struct objcore *,
342 unsigned);
343
344 uintptr_t ObjSubscribeEvents(obj_event_f *, void *, unsigned mask);
345 void ObjUnsubscribeEvents(uintptr_t *);
346
347 /* cache_panic.c */
348 void PAN_Init(void);
349 const char *sess_close_2str(enum sess_close sc, int want_desc);
350
351 /* cache_pool.c */
352 void Pool_Init(void);
353 int Pool_Task(struct pool *pp, struct pool_task *task, enum task_prio prio);
354 int Pool_Task_Arg(struct worker *, enum task_prio, task_func_t *,
355 const void *arg, size_t arg_len);
356 void Pool_Sumstat(const struct worker *w);
357 int Pool_TrySumstat(const struct worker *wrk);
358 void Pool_PurgeStat(unsigned nobj);
359 int Pool_Task_Any(struct pool_task *task, enum task_prio prio);
360 void pan_pool(struct vsb *);
361
362 /* cache_req.c */
363 struct req *Req_New(const struct worker *, struct sess *);
364 void Req_Release(struct req *);
365 void Req_Rollback(VRT_CTX);
366 void Req_Cleanup(struct sess *sp, struct worker *wrk, struct req *req);
367 void Req_Fail(struct req *req, enum sess_close reason);
368 void Req_AcctLogCharge(struct VSC_main_wrk *, struct req *);
369
370 /* cache_req_body.c */
371 int VRB_Ignore(struct req *);
372 ssize_t VRB_Cache(struct req *, ssize_t maxsize);
373 void VRB_Free(struct req *);
374
375 /* cache_req_fsm.c [CNT] */
376
377 int Resp_Setup_Deliver(struct req *);
378 void Resp_Setup_Synth(struct req *);
379
380 enum req_fsm_nxt {
381 REQ_FSM_MORE,
382 REQ_FSM_DONE,
383 REQ_FSM_DISEMBARK,
384 };
385
386 void CNT_Embark(struct worker *, struct req *);
387 enum req_fsm_nxt CNT_Request(struct req *);
388
389 /* cache_session.c */
390 void SES_NewPool(struct pool *, unsigned pool_no);
391 void SES_DestroyPool(struct pool *);
392 void SES_Wait(struct sess *, const struct transport *);
393 void SES_Ref(struct sess *sp);
394 void SES_Rel(struct sess *sp);
395
396 const char * HTC_Status(enum htc_status_e);
397 void HTC_RxInit(struct http_conn *htc, struct ws *ws);
398 void HTC_RxPipeline(struct http_conn *htc, void *);
399 enum htc_status_e HTC_RxStuff(struct http_conn *, htc_complete_f *,
400 vtim_real *t1, vtim_real *t2, vtim_real ti, vtim_real tn, vtim_dur td,
401 int maxbytes);
402
403 #define SESS_ATTR(UP, low, typ, len) \
404 int SES_Set_##low(const struct sess *sp, const typ *src); \
405 int SES_Reserve_##low(struct sess *sp, typ **dst, ssize_t *sz);
406 #include "tbl/sess_attr.h"
407 int SES_Set_String_Attr(struct sess *sp, enum sess_attr a, const char *src);
408
409 /* cache_shmlog.c */
410 extern struct VSC_main *VSC_C_main;
411 void VSM_Init(void);
412 void VSL_Setup(struct vsl_log *vsl, void *ptr, size_t len);
413 void VSL_ChgId(struct vsl_log *vsl, const char *typ, const char *why,
414 uint32_t vxid);
415 void VSL_End(struct vsl_log *vsl);
416 void VSL_Flush(struct vsl_log *, int overflow);
417
418 /* cache_conn_pool.c */
419 struct conn_pool;
420 void VCP_Init(void);
421 void VCP_Panic(struct vsb *, struct conn_pool *);
422
423 /* cache_backend_poll.c */
424
425 /* cache_vary.c */
426 int VRY_Create(struct busyobj *bo, struct vsb **psb);
427 int VRY_Match(struct req *, const uint8_t *vary);
428 void VRY_Prep(struct req *);
429 void VRY_Clear(struct req *);
430 enum vry_finish_flag { KEEP, DISCARD };
431 void VRY_Finish(struct req *req, enum vry_finish_flag);
432
433 /* cache_vcl.c */
434 void VCL_Bo2Ctx(struct vrt_ctx *, struct busyobj *);
435 void VCL_Req2Ctx(struct vrt_ctx *, struct req *);
436 struct vrt_ctx *VCL_Get_CliCtx(int);
437 struct vsb *VCL_Rel_CliCtx(struct vrt_ctx **);
438 void VCL_Panic(struct vsb *, const char *nm, const struct vcl *);
439 void VCL_Poll(void);
440 void VCL_Init(void);
441
442 #define VCL_MET_MAC(l,u,t,b) \
443 void VCL_##l##_method(struct vcl *, struct worker *, struct req *, \
444 struct busyobj *bo, void *specific);
445 #include "tbl/vcl_returns.h"
446
447
448 typedef int vcl_be_func(struct cli *, struct director *, void *);
449
450 int VCL_IterDirector(struct cli *, const char *, vcl_be_func *, void *);
451
452 /* cache_vrt.c */
453 void pan_privs(struct vsb *, const struct vrt_privs *);
454
455 /* cache_vrt_filter.c */
456 int VCL_StackVFP(struct vfp_ctx *, const struct vcl *, const char *);
457 int VCL_StackVDP(struct req *, const struct vcl *, const char *);
458 const char *resp_Get_Filter_List(struct req *req);
459 void VCL_VRT_Init(void);
460
461 /* cache_vrt_vcl.c */
462 const char *VCL_Return_Name(unsigned);
463 const char *VCL_Method_Name(unsigned);
464 void VCL_Refresh(struct vcl **);
465 void VCL_Recache(struct worker *, struct vcl **);
466 void VCL_Ref(struct vcl *);
467 void VCL_Rel(struct vcl **);
468 VCL_BACKEND VCL_DefaultDirector(const struct vcl *);
469 const struct vrt_backend_probe *VCL_DefaultProbe(const struct vcl *);
470
471 /* cache_vrt_priv.c */
472 extern struct vrt_privs cli_task_privs[1];
473 void VCL_TaskEnter(struct vrt_privs *);
474 void VCL_TaskLeave(VRT_CTX, struct vrt_privs *);
475
476 /* cache_vrt_vmod.c */
477 void VMOD_Init(void);
478 void VMOD_Panic(struct vsb *);
479
480 /* cache_wrk.c */
481 void WRK_Init(void);
482 void WRK_AddStat(const struct worker *);
483
484 /* cache_ws.c */
485 void WS_Id(const struct ws *ws, char *id);
486 void WS_Panic(struct vsb *, const struct ws *);
487 static inline int
WS_IsReserved(const struct ws * ws)488 WS_IsReserved(const struct ws *ws)
489 {
490
491 return (ws->r != NULL);
492 }
493
494 void WS_Rollback(struct ws *, uintptr_t);
495 void *WS_AtOffset(const struct ws *ws, unsigned off, unsigned len);
496
497 static inline unsigned
WS_ReservationOffset(const struct ws * ws)498 WS_ReservationOffset(const struct ws *ws)
499 {
500
501 AN(ws->r);
502 return (ws->f - ws->s);
503 }
504
505 /* http1/cache_http1_pipe.c */
506 void V1P_Init(void);
507
508 /* cache_http2_deliver.c */
509 void V2D_Init(void);
510
511 /* stevedore.c */
512 void STV_open(void);
513 void STV_close(void);
514 const struct stevedore *STV_next(void);
515 int STV_BanInfoDrop(const uint8_t *ban, unsigned len);
516 int STV_BanInfoNew(const uint8_t *ban, unsigned len);
517 void STV_BanExport(const uint8_t *banlist, unsigned len);
518 int STV_NewObject(struct worker *, struct objcore *,
519 const struct stevedore *, unsigned len);
520
521
522 #if WITH_PERSISTENT_STORAGE
523 /* storage_persistent.c */
524 void SMP_Ready(void);
525 #endif
526
527 #define FEATURE(x) COM_FEATURE(cache_param->feature_bits, x)
528 #define DO_DEBUG(x) COM_DO_DEBUG(cache_param->debug_bits, x)
529
530 #define DSL(debug_bit, id, ...) \
531 do { \
532 if (DO_DEBUG(debug_bit)) \
533 VSL(SLT_Debug, (id), __VA_ARGS__); \
534 } while (0)
535