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