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 1
vmod_hash_release(VCL_BACKEND dir)
49
{
50
        struct vmod_directors_hash *hash;
51
52 1
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
53 1
        CAST_OBJ_NOTNULL(hash, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
54 1
        vdir_release(hash->vd);
55 1
}
56
57
static void v_matchproto_(vdi_destroy_f)
58 1
vmod_hash_destroy(VCL_BACKEND dir)
59
{
60
        struct vmod_directors_hash *rr;
61
62 1
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
63 1
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
64 1
        vdir_delete(&rr->vd);
65 1
        FREE_OBJ(rr);
66 1
}
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 3
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
78
    const char *vcl_name)
79
{
80
        struct vmod_directors_hash *rr;
81
82 3
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
83 3
        AN(rrp);
84 3
        AZ(*rrp);
85 3
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
86 3
        AN(rr);
87 3
        *rrp = rr;
88 3
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
89 3
}
90
91
VCL_VOID v_matchproto_()
92 1
vmod_hash__fini(struct vmod_directors_hash **rrp)
93
{
94
        struct vmod_directors_hash *rr;
95
96 1
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
97 1
        VRT_DelDirector(&rr->vd->dir);
98 1
}
99
100
VCL_VOID v_matchproto_()
101 7
vmod_hash_add_backend(VRT_CTX,
102
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
103
{
104
105 7
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
106 7
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
107 7
        vdir_add_backend(ctx, rr->vd, be, w);
108 7
}
109
110
VCL_VOID v_matchproto_()
111 1
vmod_hash_remove_backend(VRT_CTX,
112
    struct vmod_directors_hash *rr, VCL_BACKEND be)
113
{
114
115 1
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
116 1
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
117 1
        vdir_remove_backend(ctx, rr->vd, be, NULL);
118 1
}
119
120
VCL_BACKEND v_matchproto_()
121 11
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr, VCL_STRANDS s)
122
{
123
        VCL_BACKEND be;
124
        double r;
125
126 11
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
127 11
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
128 11
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
129 11
        AN(s);
130
131 11
        r = VRT_HashStrands32(s);
132 11
        r = scalbn(r, -32);
133 11
        assert(r >= 0 && r <= 1.0);
134 11
        be = vdir_pick_be(ctx, rr->vd, r);
135 11
        return (be);
136
}