blob: 9883a67fed60be983d46794a99cb421019654c5f [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Thomas Klausner <tk@giga.or.at> *
4* *
5* Copyright (c) 2013, 2014 Thomas Klausner *
6* All rights reserved. *
7* *
8* Written under contract by nfotex IT GmbH, http://nfotex.com/ *
9* *
10* Redistribution and use of this software in source and binary forms, with or without modification, are *
11* permitted provided that the following conditions are met: *
12* *
13* * Redistributions of source code must retain the above *
14* copyright notice, this list of conditions and the *
15* following disclaimer. *
16* *
17* * Redistributions in binary form must reproduce the above *
18* copyright notice, this list of conditions and the *
19* following disclaimer in the documentation and/or other *
20* materials provided with the distribution. *
21* *
22* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
23* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
25* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
26* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
28* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
29* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
30*********************************************************************************************************/
31
32#include <freeDiameter/extension.h>
33
34/*
35 * Load balancing extension. Send request to least-loaded node.
36 */
37
38/* The callback for load balancing the requests across the peers */
39static int rt_load_balancing(void * cbdata, struct msg ** pmsg, struct fd_list * candidates)
40{
41 struct fd_list *lic;
42 struct msg * msg = *pmsg;
43
44 TRACE_ENTRY("%p %p %p", cbdata, msg, candidates);
45
46 CHECK_PARAMS(msg && candidates);
47
48 /* Check if it is worth processing the message */
49 if (FD_IS_LIST_EMPTY(candidates))
50 return 0;
51
52 /* load balancing */
53 for (lic = candidates->next; lic != candidates; lic = lic->next) {
54 struct rtd_candidate * cand = (struct rtd_candidate *) lic;
55 struct peer_hdr *peer;
56 long to_receive, to_send, load;
57 int score;
58 CHECK_FCT(fd_peer_getbyid(cand->diamid, cand->diamidlen, 0, &peer));
59 CHECK_FCT(fd_peer_get_load_pending(peer, &to_receive, &to_send));
60 load = to_receive + to_send;
61 /* other routing mechanisms need to add to the
62 * appropriate entries so their base value is high
63 * enough that they are considered */
64
65 /* logarithmic scaling */
66 int load_log = 0;
67 while (load > 0) {
68 load_log++;
69 load /= 2;
70 }
71 score = cand->score;
72 cand->score -= load_log;
73 TRACE_DEBUG(FULL, "evaluated peer `%.*s', score was %d, now %d", (int)cand->diamidlen, cand->diamid, score, cand->score);
74 }
75
76 return 0;
77}
78
79/* handler */
80static struct fd_rt_out_hdl * rt_load_balancing_hdl = NULL;
81
82/* entry point */
83static int rt_load_balance_entry(char * conffile)
84{
85 /* Register the callback */
86 CHECK_FCT(fd_rt_out_register(rt_load_balancing, NULL, 10, &rt_load_balancing_hdl));
87
88 TRACE_DEBUG(INFO, "Extension 'Load Balancing' initialized");
89 return 0;
90}
91
92/* Unload */
93void fd_ext_fini(void)
94{
95 /* Unregister the callbacks */
96 CHECK_FCT_DO(fd_rt_out_unregister(rt_load_balancing_hdl, NULL), /* continue */);
97 return ;
98}
99
100EXTENSION_ENTRY("rt_load_balance", rt_load_balance_entry);