varnish-cache/vmod/vmod_directors_hash.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
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
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "cache/cache.h"
37
38
#include "vmod_directors.h"
39
40
#include "vcc_directors_if.h"
41
42
struct vmod_directors_hash {
43
        unsigned                                magic;
44
#define VMOD_DIRECTORS_HASH_MAGIC               0xc08dd611
45
        struct vdir                             *vd;
46
};
47
48
static void v_matchproto_(vdi_destroy_f)
49 10
vmod_hash_destroy(VCL_BACKEND dir)
50
{
51
        struct vmod_directors_hash *rr;
52
53 10
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
54 10
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
55 10
        vdir_delete(&rr->vd);
56 10
        FREE_OBJ(rr);
57 10
}
58
59
static const struct vdi_methods vmod_hash_methods[1] = {{
60
        .magic =                VDI_METHODS_MAGIC,
61
        .type =                 "hash",
62
        .destroy =              vmod_hash_destroy
63
}};
64
65
66
VCL_VOID v_matchproto_()
67 30
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
68
    const char *vcl_name)
69
{
70
        struct vmod_directors_hash *rr;
71
72 30
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
73 30
        AN(rrp);
74 30
        AZ(*rrp);
75 30
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
76 30
        AN(rr);
77 30
        *rrp = rr;
78 30
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
79 30
}
80
81
VCL_VOID v_matchproto_()
82 10
vmod_hash__fini(struct vmod_directors_hash **rrp)
83
{
84
        struct vmod_directors_hash *rr;
85
86 10
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
87 10
        VRT_DelDirector(&rr->vd->dir);
88 10
}
89
90
VCL_VOID v_matchproto_()
91 70
vmod_hash_add_backend(VRT_CTX,
92
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
93
{
94
95 70
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
96 70
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
97 70
        vdir_add_backend(ctx, rr->vd, be, w);
98 70
}
99
100
VCL_VOID v_matchproto_()
101 10
vmod_hash_remove_backend(VRT_CTX,
102
    struct vmod_directors_hash *rr, VCL_BACKEND be)
103
{
104
105 10
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
106 10
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
107 10
        vdir_remove_backend(ctx, rr->vd, be, NULL);
108 10
}
109
110
VCL_BACKEND v_matchproto_()
111 110
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr, VCL_STRANDS s)
112
{
113
        VCL_BACKEND be;
114
        double r;
115
116 110
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
117 110
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
118 110
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
119 110
        AN(s);
120
121 110
        r = VRT_HashStrands32(s);
122 110
        r = scalbn(r, -32);
123 110
        assert(r >= 0 && r <= 1.0);
124 110
        be = vdir_pick_be(ctx, rr->vd, r);
125 110
        return (be);
126
}