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