varnish-cache/vmod/vmod_directors_random.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
34
#include "cache/cache.h"
35
36
#include "vbm.h"
37
#include "vrnd.h"
38
39
#include "vmod_directors.h"
40
41
#include "vcc_directors_if.h"
42
43
struct vmod_directors_random {
44
        unsigned                                magic;
45
#define VMOD_DIRECTORS_RANDOM_MAGIC             0x4732d092
46
        struct vdir                             *vd;
47
};
48
49
static VCL_BOOL v_matchproto_(vdi_healthy)
50 8
vmod_random_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
51
{
52
        struct vmod_directors_random *rr;
53
54 8
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
55 8
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
56 8
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
57 8
        return (vdir_any_healthy(ctx, rr->vd, changed));
58
}
59
60
static void v_matchproto_(vdi_list_f)
61 64
vmod_random_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag,
62
    int jflag)
63
{
64
        struct vmod_directors_random *rr;
65
66 64
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
67 64
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
68 64
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
69 64
        vdir_list(ctx, rr->vd, vsb, pflag, jflag, 1);
70 64
}
71
72
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
73 144
vmod_random_resolve(VRT_CTX, VCL_BACKEND dir)
74
{
75
        struct vmod_directors_random *rr;
76
        VCL_BACKEND be;
77
        double r;
78
79 144
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
80 144
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
81 144
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
82 144
        r = scalbn(VRND_RandomTestable(), -31);
83 144
        assert(r >= 0 && r < 1.0);
84 144
        be = vdir_pick_be(ctx, rr->vd, r);
85 144
        return (be);
86
}
87
88
static void v_matchproto_(vdi_release_f)
89 4
vmod_random_release(VCL_BACKEND dir)
90
{
91
        struct vmod_directors_random *rr;
92
93 4
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
94 4
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
95 4
        vdir_release(rr->vd);
96 4
}
97
98
static void v_matchproto_(vdi_destroy_f)
99 4
vmod_random_destroy(VCL_BACKEND dir)
100
{
101
        struct vmod_directors_random *rr;
102
103 4
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
104 4
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
105 4
        vdir_delete(&rr->vd);
106 4
        FREE_OBJ(rr);
107 4
}
108
109
static const struct vdi_methods vmod_random_methods[1] = {{
110
        .magic =                VDI_METHODS_MAGIC,
111
        .type =                 "random",
112
        .healthy =              vmod_random_healthy,
113
        .resolve =              vmod_random_resolve,
114
        .release =              vmod_random_release,
115
        .destroy =              vmod_random_destroy,
116
        .list =                 vmod_random_list
117
}};
118
119
120
VCL_VOID v_matchproto_()
121 36
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp,
122
    const char *vcl_name)
123
{
124
        struct vmod_directors_random *rr;
125
126 36
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
127 36
        AN(rrp);
128 36
        AZ(*rrp);
129 36
        ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
130 36
        AN(rr);
131 36
        *rrp = rr;
132 36
        vdir_new(ctx, &rr->vd, vcl_name, vmod_random_methods, rr);
133 36
}
134
135
VCL_VOID v_matchproto_()
136 4
vmod_random__fini(struct vmod_directors_random **rrp)
137
{
138
        struct vmod_directors_random *rr;
139
140 4
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC);
141 4
        VRT_DelDirector(&rr->vd->dir);
142 4
}
143
144
VCL_VOID v_matchproto_()
145 104
vmod_random_add_backend(VRT_CTX,
146
    struct vmod_directors_random *rr, VCL_BACKEND be, double w)
147
{
148
149 104
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
150 104
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
151 104
        vdir_add_backend(ctx, rr->vd, be, w);
152 104
}
153
154 4
VCL_VOID vmod_random_remove_backend(VRT_CTX,
155
    struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_()
156
{
157 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158 4
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
159 4
        vdir_remove_backend(ctx, rr->vd, be, NULL);
160 4
}
161
162
VCL_BACKEND v_matchproto_()
163 152
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
164
{
165 152
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
166 152
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
167 152
        return (rr->vd->dir);
168
}