1 /*-
2  * Copyright (c) 2013-2015 Varnish Software AS
3  * All rights reserved.
4  *
5  * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6  *
7  * SPDX-License-Identifier: BSD-2-Clause
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 struct req;
33 struct vfp_entry;
34 struct vfp_ctx;
35 struct vdp_ctx;
36 
37 /* Fetch processors --------------------------------------------------*/
38 
39 enum vfp_status {
40 	VFP_ERROR = -1,
41 	VFP_OK = 0,
42 	VFP_END = 1,
43 	VFP_NULL = 2,
44 };
45 
46 typedef enum vfp_status vfp_init_f(struct vfp_ctx *, struct vfp_entry *);
47 typedef enum vfp_status
48     vfp_pull_f(struct vfp_ctx *, struct vfp_entry *, void *ptr, ssize_t *len);
49 typedef void vfp_fini_f(struct vfp_ctx *, struct vfp_entry *);
50 
51 struct vfp {
52 	const char		*name;
53 	vfp_init_f		*init;
54 	vfp_pull_f		*pull;
55 	vfp_fini_f		*fini;
56 	const void		*priv1;
57 };
58 
59 struct vfp_entry {
60 	unsigned		magic;
61 #define VFP_ENTRY_MAGIC		0xbe32a027
62 	enum vfp_status		closed;
63 	const struct vfp	*vfp;
64 	void			*priv1;
65 	intptr_t		priv2;
66 	VTAILQ_ENTRY(vfp_entry)	list;
67 	uint64_t		calls;
68 	uint64_t		bytes_out;
69 };
70 
71 /*--------------------------------------------------------------------
72  * VFP filter state
73  */
74 
75 VTAILQ_HEAD(vfp_entry_s, vfp_entry);
76 
77 struct vfp_ctx {
78 	unsigned		magic;
79 #define VFP_CTX_MAGIC		0x61d9d3e5
80 	int			failed;
81 	struct http		*req;
82 	struct http		*resp;
83 	struct worker		*wrk;
84 	struct objcore		*oc;
85 
86 	struct vfp_entry_s	vfp;
87 	struct vfp_entry	*vfp_nxt;
88 	unsigned		obj_flags;
89 };
90 
91 enum vfp_status VFP_Suck(struct vfp_ctx *, void *p, ssize_t *lp);
92 enum vfp_status VFP_Error(struct vfp_ctx *, const char *fmt, ...)
93     v_printflike_(2, 3);
94 void VRT_AddVFP(VRT_CTX, const struct vfp *);
95 void VRT_RemoveVFP(VRT_CTX, const struct vfp *);
96 
97 /* Deliver processors ------------------------------------------------*/
98 
99 enum vdp_action {
100 	VDP_NULL,		/* Input buffer valid after call */
101 	VDP_FLUSH,		/* Input buffer will be invalidated */
102 	VDP_END,		/* Last buffer or after, implies VDP_FLUSH */
103 };
104 
105 typedef int vdp_init_f(struct vdp_ctx *, void **priv, struct objcore *);
106 /*
107  * Return value:
108  *	negative:	Error - abandon delivery
109  *	zero:		OK
110  *	positive:	Don't push this VDP anyway
111  */
112 
113 typedef int vdp_fini_f(struct vdp_ctx *, void **priv);
114 typedef int vdp_bytes_f(struct vdp_ctx *, enum vdp_action, void **priv,
115     const void *ptr, ssize_t len);
116 
117 struct vdp {
118 	const char		*name;
119 	vdp_init_f		*init;
120 	vdp_bytes_f		*bytes;
121 	vdp_fini_f		*fini;
122 };
123 
124 struct vdp_entry {
125 	unsigned		magic;
126 #define VDP_ENTRY_MAGIC		0x353eb781
127 	enum vdp_action		end;	// VDP_NULL or VDP_END
128 	const struct vdp	*vdp;
129 	void			*priv;
130 	VTAILQ_ENTRY(vdp_entry)	list;
131 	uint64_t		calls;
132 	uint64_t		bytes_in;
133 };
134 
135 VTAILQ_HEAD(vdp_entry_s, vdp_entry);
136 
137 struct vdp_ctx {
138 	unsigned		magic;
139 #define VDP_CTX_MAGIC		0xee501df7
140 	int			retval;
141 	uint64_t		bytes_done;
142 	struct vdp_entry_s	vdp;
143 	struct vdp_entry	*nxt;
144 	struct worker		*wrk;
145 	struct vsl_log		*vsl;
146 	struct req		*req;
147 };
148 
149 int VDP_bytes(struct vdp_ctx *, enum vdp_action act, const void *, ssize_t);
150 void VRT_AddVDP(VRT_CTX, const struct vdp *);
151 void VRT_RemoveVDP(VRT_CTX, const struct vdp *);
152