Set my own value in C for use in VCL

Kristian Lyngstol kristian at varnish-software.com
Thu Jan 20 14:47:56 CET 2011


On Thu, Jan 20, 2011 at 01:26:37PM +0000, Philip Prince wrote:
> I would like to use a C routine to set a value which I could pass on to
> VCL. I have made this simple start derived from examples I have seen on
> the web. I direct the request to a PHP file that streams out simulated
> data. If I comment out the VRT_SetHdr I get the expected data, otherwise
> I get an empty file.
> 
> C{
>   #include <stdlib.h>
> }C

(...)

> sub vcl_miss {
> 
>   C{
>     void setmyownvalue () {
>       VRT_SetHdr(sp, HDR_REQ, "\013X-My-Own-Value:", "1", vrt_magic_string_end);
>     }
>     setmyownvalue();
>   }C

You shouldn't define a function inside vcl_miss. And that function has no
knowledge of (sp).

You have to move the function definition out of vcl_miss if you intend to
use a function, then prototype it properly and pass sp along.

As for the actual VRT_SetHdr, I didn't really verify it, but the easiest
test is to try exactly what you want in normal VCL and run "varnishd -C -f
foo.vcl" which will give the resulting C code.

> It has to be easy; my apologies for being so thick. I have looked for VRT
> documentation but I must be looking in all the wrong places...

There's a reason for that: We don't encourage in-line C unless you:

A. Know C
B. Understand Varnish
C. Are willing to look through the source code.

This is likely to change somewhat with Varnish 3.0 and vmods.

As for A, the majority of issues I've seen people have with in-line C is a
lack of basic C experience. Not prototyping would be one such thing (which
any compiler will warn you about). Almost every time I get asked to look at
in-line C, there's also a NULL-check missing which would result in a
segfault.

And for B... Well, if you don't properly understand Varnish, it's unfair to
assume you will be able to write C code for it in a manner that wont
increase deforestation, segfault varnish or harm little children. And it's
likely that you can do what you want without in-line C.

The last one, C, might require some more explanation. In-line C was
designed as an emergency escape hatch that we got for free. Because VCL is
translated to C, in-line C isn't so much a "feature" as a short-cut. The
interfaces of VRT (Varnish Run Time, which VCL and in-line C use) are not
guaranteed to be stable, even between stable version of Varnish. It's
simply not a design goal. If we create extensive documentation for it, we
create an expectation that any in-line C you write will work on the next
release too. That makes development harder.

Vmods are designed to solve that problem: We make it easier to write _good_
in-line C code for Varnish that can be re-used and maintained. It will also
make it easier to see what interfaces we need to keep and which ones we can
drop.

Hope this both helped you with the original problem and explained why
in-line C documentation isn't available :)

- Kristian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20110120/fec55800/attachment-0003.pgp>


More information about the varnish-misc mailing list