varnish-cache/vmod/vmod_directors_hash.c
0
/*-
1
 * Copyright (c) 2013-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <stdlib.h>
33
#include <string.h>
34
35
#include "cache/cache.h"
36
37
#include "vmod_directors.h"
38
39
#include "vcc_directors_if.h"
40
41
struct vmod_directors_hash {
42
        unsigned                                magic;
43
#define VMOD_DIRECTORS_HASH_MAGIC               0xc08dd611
44
        struct vdir                             *vd;
45
};
46
47
static void v_matchproto_(vdi_release_f)
48 4
vmod_hash_release(VCL_BACKEND dir)
49
{
50
        struct vmod_directors_hash *hash;
51
52 4
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
53 4
        CAST_OBJ_NOTNULL(hash, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
54 4
        vdir_release(hash->vd);
55 4
}
56
57
static void v_matchproto_(vdi_destroy_f)
58 4
vmod_hash_destroy(VCL_BACKEND dir)
59
{
60
        struct vmod_directors_hash *rr;
61
62 4
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
63 4
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
64 4
        vdir_delete(&rr->vd);
65 4
        FREE_OBJ(rr);
66 4
}
67
68
static const struct vdi_methods vmod_hash_methods[1] = {{
69
        .magic =                VDI_METHODS_MAGIC,
70
        .type =                 "hash",
71
        .release =              vmod_hash_release,
72
        .destroy =              vmod_hash_destroy
73
}};
74
75
76
VCL_VOID v_matchproto_()
77 12
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
78
    const char *vcl_name)
79
{
80
        struct vmod_directors_hash *rr;
81
82 12
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
83 12
        AN(rrp);
84 12
        AZ(*rrp);
85 12
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
86 12
        AN(rr);
87 12
        *rrp = rr;
88 12
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
89 12
}
90
91
VCL_VOID v_matchproto_()
92 4
vmod_hash__fini(struct vmod_directors_hash **rrp)
93
{
94
        struct vmod_directors_hash *rr;
95
96 4
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
97 4
        VRT_DelDirector(&rr->vd->dir);
98 4
}
99
100
VCL_VOID v_matchproto_()
101 28
vmod_hash_add_backend(VRT_CTX,
102
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
103
{
104
105 28
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
106 28
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
107 28
        vdir_add_backend(ctx, rr->vd, be, w);
108 28
}
109
110
VCL_VOID v_matchproto_()
111 4
vmod_hash_remove_backend(VRT_CTX,
112
    struct vmod_directors_hash *rr, VCL_BACKEND be)
113
{
114
115 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
116 4
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
117 4
        vdir_remove_backend(ctx, rr->vd, be, NULL);
118 4
}
119
120
VCL_BACKEND v_matchproto_()
121 44
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr, VCL_STRANDS s)
122
{
123
        VCL_BACKEND be;
124
        double r;
125
126 44
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
127 44
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
128 44
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
129 44
        AN(s);
130
131 44
        r = VRT_HashStrands32(s);
132 44
        r = scalbn(r, -32);
133 44
        assert(r >= 0 && r <= 1.0);
134 44
        be = vdir_pick_be(ctx, rr->vd, r);
135 44
        return (be);
136
}