Skip to content

Commit c21c088

Browse files
committed
Update the KVM API to add extra argument to backend_response
1 parent 18c986a commit c21c088

File tree

9 files changed

+127
-18
lines changed

9 files changed

+127
-18
lines changed

Diff for: go/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
pushd example
4+
pushd goexample
55
go build
66
popd
77

Diff for: go/goexample/goexample.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"strings"
78

89
"varnish"
@@ -52,6 +53,6 @@ func main() {
5253

5354
varnish.StorageRegister("my_storage", my_storage)
5455

55-
fmt.Println("Go Compute Example ready")
56+
fmt.Println("Go Compute Example ready (", os.Args[2], ")")
5657
varnish.WaitForRequests()
5758
}

Diff for: go/varnish/kvm_api.c

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern void go_backend_response(int16_t status,
66
const char *ctype, size_t ctlen, const uint8_t *data, size_t datalen)
77
{
8-
backend_response(status, ctype, ctlen, data, datalen);
8+
sys_backend_response(status, ctype, ctlen, data, datalen, NULL);
99
}
1010

1111
extern long go_set_cacheable(bool cached, float ttl, float grace, float keep)
@@ -60,19 +60,27 @@ asm(".global wait_for_requests\n"
6060
" mov $0x10001, %eax\n"
6161
" out %eax, $0\n");
6262

63+
asm(".global wait_for_requests_paused\n"
64+
".type wait_for_requests_paused, @function\n"
65+
"wait_for_requests_paused:\n"
66+
" mov $0x10002, %eax\n"
67+
" out %eax, $0\n"
68+
" ret\n");
69+
6370
asm(".global sys_set_cacheable\n"
6471
".type sys_set_cacheable, @function\n"
6572
"sys_set_cacheable:\n"
6673
" mov $0x10005, %eax\n"
6774
" out %eax, $0\n"
6875
" ret\n");
6976

70-
asm(".global backend_response\n"
71-
".type backend_response, @function\n"
72-
"backend_response:\n"
77+
asm(".global sys_backend_response\n"
78+
".type sys_backend_response, @function\n"
79+
"sys_backend_response:\n"
7380
".cfi_startproc\n"
7481
" mov $0x10010, %eax\n"
7582
" out %eax, $0\n"
83+
" ret\n"
7684
".cfi_endproc\n");
7785

7886
asm(".global begin_streaming_response\n"

Diff for: javascript/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 3.0.2)
1+
cmake_minimum_required (VERSION 3.5)
22
project (qjs_app C)
33

44
set(JSFILE "src/my.js" CACHE STRING "JavaScript source file")

Diff for: kvm_api.h

+37-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,43 @@ extern void wait_for_requests_paused(struct backend_request* req);
115115
* backend_response(200, ctype, strlen(ctype), cont, strlen(cont));
116116
* }
117117
**/
118+
struct ResponseHeader {
119+
const char *field;
120+
size_t field_len;
121+
};
122+
struct BackendResponseExtra {
123+
const struct ResponseHeader *headers;
124+
uint16_t num_headers;
125+
bool cached;
126+
float ttl;
127+
float grace;
128+
float keep;
129+
uint64_t reserved[4]; /* Reserved for future use. */
130+
};
118131
extern void __attribute__((used))
119-
backend_response(int16_t status, const void *t, size_t, const void *c, size_t);
132+
sys_backend_response(int16_t status, const void *t, size_t, const void *c, size_t,
133+
const struct BackendResponseExtra *extra);
134+
135+
static inline void
136+
backend_response(int16_t status, const void *t, size_t tlen, const void *c, size_t clen) {
137+
sys_backend_response(status, t, tlen, c, clen, NULL);
138+
}
120139

121140
static inline void
122141
backend_response_str(int16_t status, const char *ctype, const char *content)
123142
{
124-
backend_response(status, ctype, __builtin_strlen(ctype), content, __builtin_strlen(content));
143+
sys_backend_response(status, ctype, __builtin_strlen(ctype), content, __builtin_strlen(content), NULL);
144+
}
145+
146+
/**
147+
* @brief Create a response that includes HTTP headers, a status code, a body and
148+
* a content type. The response is sent back to the client (or cached in Varnish).
149+
*/
150+
static inline void
151+
backend_response_extra(int16_t status, const void *t, size_t tlen, const void *c, size_t clen,
152+
const struct BackendResponseExtra *extra)
153+
{
154+
sys_backend_response(status, t, tlen, c, clen, extra);
125155
}
126156

127157
/**
@@ -150,8 +180,8 @@ static const int REQ = 0;
150180
static const int RESP = 1;
151181
static const int REQUEST = REQ;
152182
static const int RESPONSE = RESP;
153-
static const int BEREQ = 4;
154-
static const int BERESP = 5;
183+
static const int BEREQ = 0;
184+
static const int BERESP = 1;
155185
static const unsigned HTTP_FMT_SIZE = 4096; /* Most header fields fit. */
156186

157187
extern long
@@ -782,9 +812,9 @@ asm(".global sys_set_cacheable\n"
782812
" out %eax, $0\n"
783813
" ret\n");
784814

785-
asm(".global backend_response\n"
786-
".type backend_response, @function\n"
787-
"backend_response:\n"
815+
asm(".global sys_backend_response\n"
816+
".type sys_backend_response, @function\n"
817+
"sys_backend_response:\n"
788818
".cfi_startproc\n"
789819
" mov $0x10010, %eax\n"
790820
" out %eax, $0\n"

Diff for: nelua/env/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
#include "kvm_api.h"
22

3+
void backend_response1(int16_t status, const char *t, size_t tlen, const char *c, size_t clen) {
4+
sys_backend_response(status, t, tlen, c, clen, NULL);
5+
}

Diff for: nelua/varnish.nelua

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ global varnish.set: function(field: string)
1414
local function register_func(id: cint, func: function(cstring, cstring)): void <cimport>
1515
end
1616

17-
local function backend_response(status: cint, ct: cstring, ctlen: cint, cont: cstring, contlen: cint): void <cimport>
17+
local function backend_response1(status: cint, ct: cstring, ctlen: cint, cont: cstring, contlen: cint): void <cimport>
1818
end
1919

2020
local backend_get: function(string, string)
@@ -28,7 +28,7 @@ varnish.set_backend_get = function(func: function(string, string))
2828
end
2929

3030
varnish.response = function(status: integer, content_type: string, content: string)
31-
backend_response(status, content_type, #content_type, content, #content)
31+
backend_response1(status, content_type, #content_type, content, #content)
3232
end
3333

3434
local function sys_http_set(id: cint, f: cstring, l: cint): void <cimport>

Diff for: rust/api/varnish.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ use core::ffi::CStr;
55
use core::ffi::c_char;
66

77
/** HTTP **/
8+
#[allow(dead_code)]
89
pub static REQ: u8 = 0;
10+
#[allow(dead_code)]
911
pub static RESP: u8 = 1;
1012

1113
#[inline]
14+
#[allow(dead_code)]
1215
pub fn log(field: &str) -> i32
1316
{
1417
let mut result = 0x7F000;
@@ -23,6 +26,7 @@ pub fn log(field: &str) -> i32
2326
}
2427

2528
#[inline]
29+
#[allow(dead_code)]
2630
pub fn append(hp: u8, field: &str) -> i32
2731
{
2832
let mut result = 0x10020;
@@ -38,6 +42,7 @@ pub fn append(hp: u8, field: &str) -> i32
3842
}
3943

4044
#[inline]
45+
#[allow(dead_code)]
4146
pub fn set_cacheable(cached: bool, ttl: f32, grace: f32, keep: f32)
4247
{
4348
unsafe {
@@ -52,6 +57,7 @@ pub fn set_cacheable(cached: bool, ttl: f32, grace: f32, keep: f32)
5257
}
5358

5459
#[inline]
60+
#[allow(dead_code)]
5561
pub fn backend_response(status: u16, ctype: &str, data: &[u8])
5662
{
5763
unsafe {
@@ -61,12 +67,46 @@ pub fn backend_response(status: u16, ctype: &str, data: &[u8])
6167
in("rsi") ctype.as_ptr(),
6268
in("rdx") ctype.len(),
6369
in("rcx") data.as_ptr(),
64-
in("r8") data.len()
70+
in("r8") data.len(),
71+
in("r9") 0 // Extra data
72+
);
73+
}
74+
}
75+
76+
#[allow(dead_code)]
77+
pub struct ResponseHeader {
78+
pub data: *const u8,
79+
pub len: usize,
80+
}
81+
#[allow(dead_code)]
82+
pub struct ExtraResponseData {
83+
pub headers: *const ResponseHeader,
84+
pub num_headers: usize,
85+
pub cached: bool,
86+
pub ttl: f32,
87+
pub grace: f32,
88+
pub keep: f32,
89+
}
90+
91+
#[inline]
92+
#[allow(dead_code)]
93+
pub fn backend_response_full(status: u16, ctype: &str, data: &[u8], extra: &ExtraResponseData)
94+
{
95+
unsafe {
96+
asm!("out 0x0, eax",
97+
in("eax") 0x10010,
98+
in("rdi") status,
99+
in("rsi") ctype.as_ptr(),
100+
in("rdx") ctype.len(),
101+
in("rcx") data.as_ptr(),
102+
in("r8") data.len(),
103+
in("r9") extra as *const ExtraResponseData
65104
);
66105
}
67106
}
68107

69108
#[inline]
109+
#[allow(dead_code)]
70110
pub fn backend_response_str(status: u16, ctype: &str, data: &str)
71111
{
72112
backend_response(status, ctype, data.as_bytes());
@@ -135,6 +175,7 @@ pub fn set_backend_post(cb: PostHandler)
135175
}
136176

137177
#[inline]
178+
#[allow(dead_code)]
138179
pub fn wait_for_requests() -> !
139180
{
140181
unsafe {
@@ -159,6 +200,7 @@ struct backend_request {
159200
size_t content_len;
160201
};
161202
*/
203+
#[allow(dead_code)]
162204
pub struct Request {
163205
pub method: *const c_char,
164206
pub url: *const c_char,
@@ -173,24 +215,30 @@ pub struct Request {
173215
}
174216

175217
impl Request {
218+
#[allow(dead_code)]
176219
pub fn method(&self) -> &str {
177220
unsafe { CStr::from_ptr(self.method).to_str().unwrap() }
178221
}
222+
#[allow(dead_code)]
179223
pub fn url(&self) -> &str {
180224
unsafe { CStr::from_ptr(self.url).to_str().unwrap() }
181225
}
226+
#[allow(dead_code)]
182227
pub fn arg(&self) -> &str {
183228
unsafe { CStr::from_ptr(self.arg).to_str().unwrap() }
184229
}
230+
#[allow(dead_code)]
185231
pub fn content_type(&self) -> &str {
186232
unsafe { CStr::from_ptr(self.content_type).to_str().unwrap() }
187233
}
234+
#[allow(dead_code)]
188235
pub fn content(&self) -> &[u8] {
189236
unsafe { std::slice::from_raw_parts(self.content, self.content_len) }
190237
}
191238
}
192239

193240
#[inline]
241+
#[allow(dead_code)]
194242
pub fn wait_for_requests_paused() -> Request
195243
{
196244
unsafe {
@@ -206,6 +254,7 @@ pub fn wait_for_requests_paused() -> Request
206254
/** Debugging and introspection **/
207255

208256
#[inline]
257+
#[allow(dead_code)]
209258
pub fn breakpoint()
210259
{
211260
unsafe {

Diff for: rust/static/src/main.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod varnish;
2+
use crate::varnish::*;
23

34
fn on_get(_url: &str, _arg: &str)
45
{
@@ -26,7 +27,24 @@ fn main()
2627
let mut resp : String = "Hello, world! URL=".to_string();
2728
resp.push_str(&request.url());
2829

29-
varnish::backend_response_str(200, "text/plain", &resp);
30+
let header1 = ResponseHeader {
31+
data: "X-Header: Header value".as_ptr(),
32+
len: "X-Header: Header value".len(),
33+
};
34+
let header2 = ResponseHeader {
35+
data: "X-Header2: Header value".as_ptr(),
36+
len: "X-Header2: Header value".len(),
37+
};
38+
let headers = [header1, header2];
39+
let extra = ExtraResponseData {
40+
headers: headers.as_ptr(),
41+
num_headers: headers.len(),
42+
cached: false,
43+
ttl: 10.0,
44+
grace: 0.0,
45+
keep: 0.0,
46+
};
47+
varnish::backend_response_full(200, "text/plain", resp.as_bytes(), &extra);
3048
}
3149
}
3250
}

0 commit comments

Comments
 (0)