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 21
vmod_hash_destroy(VCL_BACKEND dir)
50
{
51
        struct vmod_directors_hash *rr;
52
53 21
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
54 21
        vdir_delete(&rr->vd);
55 21
        FREE_OBJ(rr);
56 21
}
57
58
static const struct vdi_methods vmod_hash_methods[1] = {{
59
        .magic =                VDI_METHODS_MAGIC,
60
        .type =                 "hash",
61
        .destroy =              vmod_hash_destroy
62
}};
63
64
65
VCL_VOID v_matchproto_()
66 63
vmod_hash__init(VRT_CTX, struct vmod_directors_hash **rrp,
67
    const char *vcl_name)
68
{
69
        struct vmod_directors_hash *rr;
70
71 63
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
72 63
        AN(rrp);
73 63
        AZ(*rrp);
74 63
        ALLOC_OBJ(rr, VMOD_DIRECTORS_HASH_MAGIC);
75 63
        AN(rr);
76 63
        *rrp = rr;
77 63
        vdir_new(ctx, &rr->vd, vcl_name, vmod_hash_methods, rr);
78 63
}
79
80
VCL_VOID v_matchproto_()
81 21
vmod_hash__fini(struct vmod_directors_hash **rrp)
82
{
83
        struct vmod_directors_hash *rr;
84
85 21
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
86 21
        VRT_DelDirector(&rr->vd->dir);
87 21
}
88
89
VCL_VOID v_matchproto_()
90 147
vmod_hash_add_backend(VRT_CTX,
91
    struct vmod_directors_hash *rr, VCL_BACKEND be, double w)
92
{
93
94 147
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
95 147
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
96 147
        vdir_add_backend(ctx, rr->vd, be, w);
97 147
}
98
99
VCL_VOID v_matchproto_()
100 21
vmod_hash_remove_backend(VRT_CTX,
101
    struct vmod_directors_hash *rr, VCL_BACKEND be)
102
{
103
104 21
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
105 21
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
106 21
        vdir_remove_backend(ctx, rr->vd, be, NULL);
107 21
}
108
109
VCL_BACKEND v_matchproto_()
110 231
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr, VCL_STRANDS s)
111
{
112
        VCL_BACKEND be;
113
        double r;
114
115 231
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
116 231
        CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
117 231
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
118 231
        AN(s);
119
120 231
        r = VRT_HashStrands32(s);
121 231
        r = scalbn(r, -32);
122 231
        assert(r >= 0 && r <= 1.0);
123 231
        be = vdir_pick_be(ctx, rr->vd, r);
124 231
        return (be);
125
}