3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2008 Henning Rogge <rogge@fgan.de>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
44 #include "lq_plugin.h"
46 #include "lq_packet.h"
49 #include "lq_plugin_default_fpm.h"
51 static void default_lq_initialize_fpm(void);
52 static olsr_linkcost default_lq_calc_cost_fpm(const void *lq);
53 static void default_lq_packet_loss_worker_fpm(struct link_entry *link, void *lq, bool lost);
54 static void default_lq_memorize_foreign_hello_fpm(void *local, void *foreign);
55 static int default_lq_serialize_hello_lq_pair_fpm(unsigned char *buff, void *lq);
56 static void default_lq_deserialize_hello_lq_pair_fpm(const uint8_t ** curr, void *lq);
57 static int default_lq_serialize_tc_lq_pair_fpm(unsigned char *buff, void *lq);
58 static void default_lq_deserialize_tc_lq_pair_fpm(const uint8_t ** curr, void *lq);
59 static void default_lq_copy_link2tc_fpm(void *target, void *source);
60 static void default_lq_clear_fpm(void *target);
61 static const char *default_lq_print_fpm(void *ptr, char separator, struct lqtextbuffer *buffer);
62 static const char *default_lq_print_cost_fpm(olsr_linkcost cost, struct lqtextbuffer *buffer);
65 /* etx lq plugin (fpm version) settings */
66 struct lq_handler lq_etx_fpm_handler = {
67 &default_lq_initialize_fpm,
69 &default_lq_calc_cost_fpm,
70 &default_lq_calc_cost_fpm,
72 &default_lq_packet_loss_worker_fpm,
73 &default_lq_memorize_foreign_hello_fpm,
74 &default_lq_copy_link2tc_fpm,
75 &default_lq_copy_link2tc_fpm,
76 &default_lq_clear_fpm,
77 &default_lq_clear_fpm,
79 &default_lq_serialize_hello_lq_pair_fpm,
80 &default_lq_serialize_tc_lq_pair_fpm,
81 &default_lq_deserialize_hello_lq_pair_fpm,
82 &default_lq_deserialize_tc_lq_pair_fpm,
84 &default_lq_print_fpm,
85 &default_lq_print_fpm,
86 &default_lq_print_cost_fpm,
88 sizeof(struct default_lq_fpm),
89 sizeof(struct default_lq_fpm),
94 uint32_t aging_factor_new, aging_factor_old;
95 uint32_t aging_quickstart_new, aging_quickstart_old;
98 default_lq_initialize_fpm(void)
100 aging_factor_new = (uint32_t) (olsr_cnf->lq_aging * LQ_FPM_INTERNAL_MULTIPLIER);
101 aging_factor_old = LQ_FPM_INTERNAL_MULTIPLIER - aging_factor_new;
103 aging_quickstart_new = (uint32_t) (LQ_QUICKSTART_AGING * LQ_FPM_INTERNAL_MULTIPLIER);
104 aging_quickstart_old = LQ_FPM_INTERNAL_MULTIPLIER - aging_quickstart_new;
108 default_lq_calc_cost_fpm(const void *ptr)
110 const struct default_lq_fpm *lq = ptr;
113 if (lq->valueLq < (unsigned int)(255 * MINIMAL_USEFUL_LQ) || lq->valueNlq < (unsigned int)(255 * MINIMAL_USEFUL_LQ)) {
114 return LINK_COST_BROKEN;
117 cost = LQ_FPM_LINKCOST_MULTIPLIER * 255 / (int)lq->valueLq * 255 / (int)lq->valueNlq;
119 if (cost > LINK_COST_BROKEN)
120 return LINK_COST_BROKEN;
127 default_lq_serialize_hello_lq_pair_fpm(unsigned char *buff, void *ptr)
129 struct default_lq_fpm *lq = ptr;
131 buff[0] = (unsigned char)lq->valueLq;
132 buff[1] = (unsigned char)lq->valueNlq;
133 buff[2] = (unsigned char)(0);
134 buff[3] = (unsigned char)(0);
140 default_lq_deserialize_hello_lq_pair_fpm(const uint8_t ** curr, void *ptr)
142 struct default_lq_fpm *lq = ptr;
144 pkt_get_u8(curr, &lq->valueLq);
145 pkt_get_u8(curr, &lq->valueNlq);
146 pkt_ignore_u16(curr);
150 default_lq_serialize_tc_lq_pair_fpm(unsigned char *buff, void *ptr)
152 struct default_lq_fpm *lq = ptr;
154 buff[0] = (unsigned char)lq->valueLq;
155 buff[1] = (unsigned char)lq->valueNlq;
156 buff[2] = (unsigned char)(0);
157 buff[3] = (unsigned char)(0);
163 default_lq_deserialize_tc_lq_pair_fpm(const uint8_t ** curr, void *ptr)
165 struct default_lq_fpm *lq = ptr;
167 pkt_get_u8(curr, &lq->valueLq);
168 pkt_get_u8(curr, &lq->valueNlq);
169 pkt_ignore_u16(curr);
173 default_lq_packet_loss_worker_fpm(struct link_entry *link __attribute__ ((unused)), void *ptr, bool lost)
175 struct default_lq_fpm *tlq = ptr;
176 uint32_t alpha_old = aging_factor_old;
177 uint32_t alpha_new = aging_factor_new;
181 if (tlq->quickstart < LQ_QUICKSTART_STEPS) {
182 alpha_new = aging_quickstart_new;
183 alpha_old = aging_quickstart_old;
186 // exponential moving average
187 value = (uint32_t) (tlq->valueLq) * LQ_FPM_INTERNAL_MULTIPLIER / 255;
189 value = (value * alpha_old + LQ_FPM_INTERNAL_MULTIPLIER - 1) / LQ_FPM_INTERNAL_MULTIPLIER;
194 ratio = (alpha_new * link->loss_link_multiplier + LINK_LOSS_MULTIPLIER - 1) / LINK_LOSS_MULTIPLIER;
197 tlq->valueLq = (value * 255 + LQ_FPM_INTERNAL_MULTIPLIER - 1) / LQ_FPM_INTERNAL_MULTIPLIER;
199 link->linkcost = default_lq_calc_cost_fpm(ptr);
200 olsr_relevant_linkcost_change();
204 default_lq_memorize_foreign_hello_fpm(void *ptrLocal, void *ptrForeign)
206 struct default_lq_fpm *local = ptrLocal;
207 struct default_lq_fpm *foreign = ptrForeign;
210 local->valueNlq = foreign->valueLq;
217 default_lq_copy_link2tc_fpm(void *target, void *source)
219 memcpy(target, source, sizeof(struct default_lq_fpm));
223 default_lq_clear_fpm(void *target)
225 memset(target, 0, sizeof(struct default_lq_fpm));
229 default_lq_print_fpm(void *ptr, char separator, struct lqtextbuffer *buffer)
231 struct default_lq_fpm *lq = ptr;
233 snprintf(buffer->buf, sizeof(buffer->buf), "%0.3f%c%0.3f", (double)(lq->valueLq) / (double)255.0, separator,
234 (double)(lq->valueNlq) / (double)255.0);
239 default_lq_print_cost_fpm(olsr_linkcost cost, struct lqtextbuffer *buffer)
241 snprintf(buffer->buf, sizeof(buffer->buf), "%.3f", (double)(cost) / (double)LQ_FPM_LINKCOST_MULTIPLIER);
248 * indent-tabs-mode: nil