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