varnish-cache/lib/libvmod_directors/random.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
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <stdlib.h>
32
33
#include "cache/cache.h"
34
35
#include "vbm.h"
36
#include "vrnd.h"
37
38
#include "vdir.h"
39
40
#include "vcc_if.h"
41
42
struct vmod_directors_random {
43
        unsigned                                magic;
44
#define VMOD_DIRECTORS_RANDOM_MAGIC             0x4732d092
45
        struct vdir                             *vd;
46
};
47
48
static VCL_BOOL v_matchproto_(vdi_healthy)
49 0
vmod_random_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
50
{
51
        struct vmod_directors_random *rr;
52
53 0
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
54 0
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
55 0
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
56 0
        return (vdir_any_healthy(ctx, rr->vd, changed));
57
}
58
59
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
60 140
vmod_random_resolve(VRT_CTX, VCL_BACKEND dir)
61
{
62
        struct vmod_directors_random *rr;
63
        VCL_BACKEND be;
64
        double r;
65
66 140
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
67 140
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
68 140
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
69 140
        r = scalbn(VRND_RandomTestable(), -31);
70 140
        assert(r >= 0 && r < 1.0);
71 140
        be = vdir_pick_be(ctx, rr->vd, r);
72 140
        return (be);
73
}
74
75
static void v_matchproto_(vdi_destroy_f)
76 4
vmod_random_destroy(VCL_BACKEND dir)
77
{
78
        struct vmod_directors_random *rr;
79
80 4
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
81 4
        vdir_delete(&rr->vd);
82 4
        FREE_OBJ(rr);
83 4
}
84
85
static const struct vdi_methods vmod_random_methods[1] = {{
86
        .magic =                VDI_METHODS_MAGIC,
87
        .type =                 "random",
88
        .healthy =              vmod_random_healthy,
89
        .resolve =              vmod_random_resolve,
90
        .destroy =              vmod_random_destroy
91
}};
92
93
94
VCL_VOID v_matchproto_()
95 32
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp,
96
    const char *vcl_name)
97
{
98
        struct vmod_directors_random *rr;
99
100 32
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
101 32
        AN(rrp);
102 32
        AZ(*rrp);
103 32
        ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
104 32
        AN(rr);
105 32
        *rrp = rr;
106 32
        vdir_new(ctx, &rr->vd, vcl_name, vmod_random_methods, rr);
107 32
}
108
109
VCL_VOID v_matchproto_()
110 4
vmod_random__fini(struct vmod_directors_random **rrp)
111
{
112
        struct vmod_directors_random *rr;
113
114
        // XXX 2297
115 4
        if (*rrp == NULL)
116 0
                return;
117
118 4
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC);
119 4
        VRT_DelDirector(&rr->vd->dir);
120
}
121
122
VCL_VOID v_matchproto_()
123 88
vmod_random_add_backend(VRT_CTX,
124
    struct vmod_directors_random *rr, VCL_BACKEND be, double w)
125
{
126
127 88
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
128 88
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
129 88
        vdir_add_backend(ctx, rr->vd, be, w);
130 88
}
131
132 4
VCL_VOID vmod_random_remove_backend(VRT_CTX,
133
    struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_()
134
{
135 4
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
136 4
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
137 4
        vdir_remove_backend(ctx, rr->vd, be, NULL);
138 4
}
139
140
VCL_BACKEND v_matchproto_()
141 144
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
142
{
143 144
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
144 144
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
145 144
        return (rr->vd->dir);
146
}