neighbour.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. Copyright (c) 2007, 2008 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <sys/time.h>
  23. #include <time.h>
  24. #include <assert.h>
  25. #include "babeld.h"
  26. #include "util.h"
  27. #include "interface.h"
  28. #include "neighbour.h"
  29. #include "source.h"
  30. #include "route.h"
  31. #include "message.h"
  32. #include "resend.h"
  33. #include "local.h"
  34. struct neighbour *neighs = NULL;
  35. static struct neighbour *
  36. find_neighbour_nocreate(const unsigned char *address, struct interface *ifp)
  37. {
  38. struct neighbour *neigh;
  39. FOR_ALL_NEIGHBOURS(neigh) {
  40. if(memcmp(address, neigh->address, 16) == 0 &&
  41. neigh->ifp == ifp)
  42. return neigh;
  43. }
  44. return NULL;
  45. }
  46. void
  47. flush_neighbour(struct neighbour *neigh)
  48. {
  49. flush_neighbour_routes(neigh);
  50. if(unicast_neighbour == neigh)
  51. flush_unicast(1);
  52. flush_resends(neigh);
  53. if(neighs == neigh) {
  54. neighs = neigh->next;
  55. } else {
  56. struct neighbour *previous = neighs;
  57. while(previous->next != neigh)
  58. previous = previous->next;
  59. previous->next = neigh->next;
  60. }
  61. local_notify_neighbour(neigh, LOCAL_FLUSH);
  62. free(neigh);
  63. }
  64. struct neighbour *
  65. find_neighbour(const unsigned char *address, struct interface *ifp)
  66. {
  67. struct neighbour *neigh;
  68. const struct timeval zero = {0, 0};
  69. neigh = find_neighbour_nocreate(address, ifp);
  70. if(neigh)
  71. return neigh;
  72. debugf("Creating neighbour %s on %s.\n",
  73. format_address(address), ifp->name);
  74. neigh = calloc(1, sizeof(struct neighbour));
  75. if(neigh == NULL) {
  76. perror("malloc(neighbour)");
  77. return NULL;
  78. }
  79. neigh->hello_seqno = -1;
  80. memcpy(neigh->address, address, 16);
  81. neigh->txcost = INFINITY;
  82. neigh->ihu_time = now;
  83. neigh->hello_time = zero;
  84. neigh->hello_rtt_receive_time = zero;
  85. neigh->rtt_time = zero;
  86. neigh->ifp = ifp;
  87. neigh->next = neighs;
  88. neighs = neigh;
  89. local_notify_neighbour(neigh, LOCAL_ADD);
  90. send_hello(ifp);
  91. return neigh;
  92. }
  93. /* Recompute a neighbour's rxcost. Return true if anything changed.
  94. This does not call local_notify_neighbour, see update_neighbour_metric. */
  95. int
  96. update_neighbour(struct neighbour *neigh, int hello, int hello_interval)
  97. {
  98. int missed_hellos;
  99. int rc = 0;
  100. if(hello < 0) {
  101. if(neigh->hello_interval <= 0)
  102. return rc;
  103. missed_hellos =
  104. ((int)timeval_minus_msec(&now, &neigh->hello_time) -
  105. neigh->hello_interval * 7) /
  106. (neigh->hello_interval * 10);
  107. if(missed_hellos <= 0)
  108. return rc;
  109. timeval_add_msec(&neigh->hello_time, &neigh->hello_time,
  110. missed_hellos * neigh->hello_interval * 10);
  111. } else {
  112. if(neigh->hello_seqno >= 0 && neigh->reach > 0) {
  113. missed_hellos = seqno_minus(hello, neigh->hello_seqno) - 1;
  114. if(missed_hellos < -8) {
  115. /* Probably a neighbour that rebooted and lost its seqno.
  116. Reboot the universe. */
  117. neigh->reach = 0;
  118. missed_hellos = 0;
  119. rc = 1;
  120. } else if(missed_hellos < 0) {
  121. if(hello_interval > neigh->hello_interval) {
  122. /* This neighbour has increased its hello interval,
  123. and we didn't notice. */
  124. neigh->reach <<= -missed_hellos;
  125. missed_hellos = 0;
  126. } else {
  127. /* Late hello. Probably due to the link layer buffering
  128. packets during a link outage. Ignore it, but reset
  129. the expected seqno. */
  130. neigh->hello_seqno = hello;
  131. hello = -1;
  132. missed_hellos = 0;
  133. }
  134. rc = 1;
  135. }
  136. } else {
  137. missed_hellos = 0;
  138. }
  139. neigh->hello_time = now;
  140. neigh->hello_interval = hello_interval;
  141. }
  142. if(missed_hellos > 0) {
  143. neigh->reach >>= missed_hellos;
  144. neigh->hello_seqno = seqno_plus(neigh->hello_seqno, missed_hellos);
  145. missed_hellos = 0;
  146. rc = 1;
  147. }
  148. if(hello >= 0) {
  149. neigh->hello_seqno = hello;
  150. neigh->reach >>= 1;
  151. neigh->reach |= 0x8000;
  152. if((neigh->reach & 0xFC00) != 0xFC00)
  153. rc = 1;
  154. }
  155. /* Make sure to give neighbours some feedback early after association */
  156. if((neigh->reach & 0xBF00) == 0x8000) {
  157. /* A new neighbour */
  158. send_hello(neigh->ifp);
  159. } else {
  160. /* Don't send hellos, in order to avoid a positive feedback loop. */
  161. int a = (neigh->reach & 0xC000);
  162. int b = (neigh->reach & 0x3000);
  163. if((a == 0xC000 && b == 0) || (a == 0 && b == 0x3000)) {
  164. /* Reachability is either 1100 or 0011 */
  165. send_self_update(neigh->ifp);
  166. }
  167. }
  168. if((neigh->reach & 0xFC00) == 0xC000) {
  169. /* This is a newish neighbour, let's request a full route dump.
  170. We ought to avoid this when the network is dense */
  171. send_unicast_request(neigh, NULL, 0, NULL, 0);
  172. send_ihu(neigh, NULL);
  173. }
  174. return rc;
  175. }
  176. static int
  177. reset_txcost(struct neighbour *neigh)
  178. {
  179. unsigned delay;
  180. delay = timeval_minus_msec(&now, &neigh->ihu_time);
  181. if(neigh->ihu_interval > 0 && delay < neigh->ihu_interval * 10 * 3)
  182. return 0;
  183. /* If we're losing a lot of packets, we probably lost an IHU too */
  184. if(delay >= 180000 || (neigh->reach & 0xFFF0) == 0 ||
  185. (neigh->ihu_interval > 0 &&
  186. delay >= neigh->ihu_interval * 10 * 10)) {
  187. neigh->txcost = INFINITY;
  188. neigh->ihu_time = now;
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. unsigned
  194. neighbour_txcost(struct neighbour *neigh)
  195. {
  196. return neigh->txcost;
  197. }
  198. unsigned
  199. check_neighbours()
  200. {
  201. struct neighbour *neigh;
  202. int changed, rc;
  203. unsigned msecs = 50000;
  204. debugf("Checking neighbours.\n");
  205. neigh = neighs;
  206. while(neigh) {
  207. changed = update_neighbour(neigh, -1, 0);
  208. if(neigh->reach == 0 ||
  209. neigh->hello_time.tv_sec > now.tv_sec || /* clock stepped */
  210. timeval_minus_msec(&now, &neigh->hello_time) > 300000) {
  211. struct neighbour *old = neigh;
  212. neigh = neigh->next;
  213. flush_neighbour(old);
  214. continue;
  215. }
  216. rc = reset_txcost(neigh);
  217. changed = changed || rc;
  218. update_neighbour_metric(neigh, changed);
  219. if(neigh->hello_interval > 0)
  220. msecs = MIN(msecs, neigh->hello_interval * 10);
  221. if(neigh->ihu_interval > 0)
  222. msecs = MIN(msecs, neigh->ihu_interval * 10);
  223. neigh = neigh->next;
  224. }
  225. return msecs;
  226. }
  227. unsigned
  228. neighbour_rxcost(struct neighbour *neigh)
  229. {
  230. unsigned delay;
  231. unsigned short reach = neigh->reach;
  232. delay = timeval_minus_msec(&now, &neigh->hello_time);
  233. if((reach & 0xFFF0) == 0 || delay >= 180000) {
  234. return INFINITY;
  235. } else if((neigh->ifp->flags & IF_LQ)) {
  236. int sreach =
  237. ((reach & 0x8000) >> 2) +
  238. ((reach & 0x4000) >> 1) +
  239. (reach & 0x3FFF);
  240. /* 0 <= sreach <= 0x7FFF */
  241. int cost = (0x8000 * neigh->ifp->cost) / (sreach + 1);
  242. /* cost >= interface->cost */
  243. if(delay >= 40000)
  244. cost = (cost * (delay - 20000) + 10000) / 20000;
  245. return MIN(cost, INFINITY);
  246. } else {
  247. /* To lose one hello is a misfortune, to lose two is carelessness. */
  248. if((reach & 0xC000) == 0xC000)
  249. return neigh->ifp->cost;
  250. else if((reach & 0xC000) == 0)
  251. return INFINITY;
  252. else if((reach & 0x2000))
  253. return neigh->ifp->cost;
  254. else
  255. return INFINITY;
  256. }
  257. }
  258. unsigned
  259. neighbour_rttcost(struct neighbour *neigh)
  260. {
  261. struct interface *ifp = neigh->ifp;
  262. if(!ifp->max_rtt_penalty || !valid_rtt(neigh))
  263. return 0;
  264. /* Function: linear behaviour between rtt_min and rtt_max. */
  265. if(neigh->rtt <= ifp->rtt_min) {
  266. return 0;
  267. } else if(neigh->rtt <= ifp->rtt_max) {
  268. unsigned long long tmp =
  269. (unsigned long long)ifp->max_rtt_penalty *
  270. (neigh->rtt - ifp->rtt_min) /
  271. (ifp->rtt_max - ifp->rtt_min);
  272. assert((tmp & 0x7FFFFFFF) == tmp);
  273. return tmp;
  274. } else {
  275. return ifp->max_rtt_penalty;
  276. }
  277. }
  278. unsigned
  279. neighbour_cost(struct neighbour *neigh)
  280. {
  281. unsigned a, b, cost;
  282. if(!if_up(neigh->ifp))
  283. return INFINITY;
  284. a = neighbour_txcost(neigh);
  285. if(a >= INFINITY)
  286. return INFINITY;
  287. b = neighbour_rxcost(neigh);
  288. if(b >= INFINITY)
  289. return INFINITY;
  290. if(!(neigh->ifp->flags & IF_LQ) || (a < 256 && b < 256)) {
  291. cost = a;
  292. } else {
  293. /* a = 256/alpha, b = 256/beta, where alpha and beta are the expected
  294. probabilities of a packet getting through in the direct and reverse
  295. directions. */
  296. a = MAX(a, 256);
  297. b = MAX(b, 256);
  298. /* 1/(alpha * beta), which is just plain ETX. */
  299. /* Since a and b are capped to 16 bits, overflow is impossible. */
  300. cost = (a * b + 128) >> 8;
  301. }
  302. cost += neighbour_rttcost(neigh);
  303. return MIN(cost, INFINITY);
  304. }
  305. int
  306. valid_rtt(struct neighbour *neigh)
  307. {
  308. return (timeval_minus_msec(&now, &neigh->rtt_time) < 180000) ? 1 : 0;
  309. }