]> git.proxmox.com Git - mirror_frr.git/blob - babeld/source.c
Merge pull request #2846 from donaldsharp/backet_data
[mirror_frr.git] / babeld / source.c
1 /*
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/time.h>
27
28 #include "babel_main.h"
29 #include "babeld.h"
30 #include "util.h"
31 #include "source.h"
32 #include "babel_interface.h"
33 #include "route.h"
34 #include "babel_errors.h"
35
36 struct source *srcs = NULL;
37
38 struct source*
39 find_source(const unsigned char *id, const unsigned char *p, unsigned char plen,
40 int create, unsigned short seqno)
41 {
42 struct source *src;
43
44 for(src = srcs; src; src = src->next) {
45 /* This should really be a hash table. For now, check the
46 last byte first. */
47 if(src->id[7] != id[7])
48 continue;
49 if(memcmp(src->id, id, 8) != 0)
50 continue;
51 if(src->plen != plen)
52 continue;
53 if(memcmp(src->prefix, p, 16) == 0)
54 return src;
55 }
56
57 if(!create)
58 return NULL;
59
60 src = malloc(sizeof(struct source));
61 if(src == NULL) {
62 flog_err(BABEL_ERR_MEMORY, "malloc(source): %s", safe_strerror(errno));
63 return NULL;
64 }
65
66 memcpy(src->id, id, 8);
67 memcpy(src->prefix, p, 16);
68 src->plen = plen;
69 src->seqno = seqno;
70 src->metric = INFINITY;
71 src->time = babel_now.tv_sec;
72 src->route_count = 0;
73 src->next = srcs;
74 srcs = src;
75 return src;
76 }
77
78 struct source *
79 retain_source(struct source *src)
80 {
81 assert(src->route_count < 0xffff);
82 src->route_count++;
83 return src;
84 }
85
86 void
87 release_source(struct source *src)
88 {
89 assert(src->route_count > 0);
90 src->route_count--;
91 }
92
93 int
94 flush_source(struct source *src)
95 {
96 if(src->route_count > 0)
97 /* The source is in use by a route. */
98 return 0;
99
100 if(srcs == src) {
101 srcs = src->next;
102 } else {
103 struct source *previous = srcs;
104 while(previous->next != src)
105 previous = previous->next;
106 previous->next = src->next;
107 }
108
109 free(src);
110 return 1;
111 }
112
113 void
114 update_source(struct source *src,
115 unsigned short seqno, unsigned short metric)
116 {
117 if(metric >= INFINITY)
118 return;
119
120 /* If a source is expired, pretend that it doesn't exist and update
121 it unconditionally. This makes ensures that old data will
122 eventually be overridden, and prevents us from getting stuck if
123 a router loses its sequence number. */
124 if(src->time < babel_now.tv_sec - SOURCE_GC_TIME ||
125 seqno_compare(src->seqno, seqno) < 0 ||
126 (src->seqno == seqno && src->metric > metric)) {
127 src->seqno = seqno;
128 src->metric = metric;
129 }
130 src->time = babel_now.tv_sec;
131 }
132
133 void
134 expire_sources()
135 {
136 struct source *src;
137
138 src = srcs;
139 while(src) {
140 if(src->time > babel_now.tv_sec)
141 /* clock stepped */
142 src->time = babel_now.tv_sec;
143 if(src->time < babel_now.tv_sec - SOURCE_GC_TIME) {
144 struct source *old = src;
145 src = src->next;
146 flush_source(old);
147 continue;
148 }
149 src = src->next;
150 }
151 }
152
153 void
154 check_sources_released(void)
155 {
156 struct source *src;
157
158 for(src = srcs; src; src = src->next) {
159 if(src->route_count != 0)
160 fprintf(stderr, "Warning: source %s %s has refcount %d.\n",
161 format_eui64(src->id),
162 format_prefix(src->prefix, src->plen),
163 (int)src->route_count);
164 }
165 }