Coverage Report

Created: 2026-01-01 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/frr/ospfd/ospf_lsdb.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * OSPF LSDB support.
4
 * Copyright (C) 1999, 2000 Alex Zinin, Kunihiro Ishiguro, Toshiaki Takada
5
 */
6
7
#include <zebra.h>
8
9
#include "prefix.h"
10
#include "table.h"
11
#include "memory.h"
12
#include "log.h"
13
14
#include "ospfd/ospfd.h"
15
#include "ospfd/ospf_asbr.h"
16
#include "ospfd/ospf_lsa.h"
17
#include "ospfd/ospf_lsdb.h"
18
19
struct ospf_lsdb *ospf_lsdb_new(void)
20
2
{
21
2
  struct ospf_lsdb *new;
22
23
2
  new = XCALLOC(MTYPE_OSPF_LSDB, sizeof(struct ospf_lsdb));
24
2
  ospf_lsdb_init(new);
25
26
2
  return new;
27
2
}
28
29
void ospf_lsdb_init(struct ospf_lsdb *lsdb)
30
722
{
31
722
  int i;
32
33
8.66k
  for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
34
7.94k
    lsdb->type[i].db = route_table_init();
35
722
}
36
37
void ospf_lsdb_free(struct ospf_lsdb *lsdb)
38
0
{
39
0
  ospf_lsdb_cleanup(lsdb);
40
0
  XFREE(MTYPE_OSPF_LSDB, lsdb);
41
0
}
42
43
void ospf_lsdb_cleanup(struct ospf_lsdb *lsdb)
44
0
{
45
0
  int i;
46
0
  assert(lsdb);
47
0
  assert(lsdb->total == 0);
48
49
0
  ospf_lsdb_delete_all(lsdb);
50
51
0
  for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
52
0
    route_table_finish(lsdb->type[i].db);
53
0
}
54
55
void ls_prefix_set(struct prefix_ls *lp, struct ospf_lsa *lsa)
56
12.9k
{
57
12.9k
  if (lp && lsa && lsa->data) {
58
12.9k
    lp->family = AF_UNSPEC;
59
12.9k
    lp->prefixlen = 64;
60
12.9k
    lp->id = lsa->data->id;
61
12.9k
    lp->adv_router = lsa->data->adv_router;
62
12.9k
  }
63
12.9k
}
64
65
static void ospf_lsdb_delete_entry(struct ospf_lsdb *lsdb,
66
           struct route_node *rn)
67
2.03k
{
68
2.03k
  struct ospf_lsa *lsa = rn->info;
69
70
2.03k
  if (!lsa)
71
0
    return;
72
73
2.03k
  assert(rn->table == lsdb->type[lsa->data->type].db);
74
75
2.03k
  if (IS_LSA_SELF(lsa))
76
0
    lsdb->type[lsa->data->type].count_self--;
77
2.03k
  lsdb->type[lsa->data->type].count--;
78
2.03k
  lsdb->type[lsa->data->type].checksum -= ntohs(lsa->data->checksum);
79
2.03k
  lsdb->total--;
80
81
  /* Decrement number of router LSAs received with DC bit set */
82
2.03k
  if (lsa->area && (lsa->area->lsdb == lsdb) && !IS_LSA_SELF(lsa) &&
83
0
      (lsa->data->type == OSPF_ROUTER_LSA) &&
84
0
      CHECK_FLAG(lsa->data->options, OSPF_OPTION_DC))
85
0
    lsa->area->fr_info.router_lsas_recv_dc_bit--;
86
87
  /*
88
   * If the LSA being deleted is indication LSA, then set the
89
   * pointer to NULL.
90
   */
91
2.03k
  if (lsa->area && lsa->area->fr_info.indication_lsa_self &&
92
0
      (lsa->area->fr_info.indication_lsa_self == lsa))
93
0
    lsa->area->fr_info.indication_lsa_self = NULL;
94
95
2.03k
  rn->info = NULL;
96
2.03k
  route_unlock_node(rn);
97
2.03k
#ifdef MONITOR_LSDB_CHANGE
98
2.03k
  if (lsdb->del_lsa_hook != NULL)
99
0
    (*lsdb->del_lsa_hook)(lsa);
100
2.03k
#endif             /* MONITOR_LSDB_CHANGE */
101
2.03k
  ospf_lsa_unlock(&lsa); /* lsdb */
102
2.03k
  return;
103
2.03k
}
104
105
/* Add new LSA to lsdb. */
106
void ospf_lsdb_add(struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
107
5.20k
{
108
5.20k
  struct route_table *table;
109
5.20k
  struct prefix_ls lp;
110
5.20k
  struct route_node *rn;
111
112
5.20k
  table = lsdb->type[lsa->data->type].db;
113
5.20k
  ls_prefix_set(&lp, lsa);
114
5.20k
  rn = route_node_get(table, (struct prefix *)&lp);
115
116
  /* nothing to do? */
117
5.20k
  if (rn->info && rn->info == lsa) {
118
0
    route_unlock_node(rn);
119
0
    return;
120
0
  }
121
122
  /* purge old entry? */
123
5.20k
  if (rn->info)
124
2.03k
    ospf_lsdb_delete_entry(lsdb, rn);
125
126
5.20k
  if (IS_LSA_SELF(lsa))
127
336
    lsdb->type[lsa->data->type].count_self++;
128
5.20k
  lsdb->type[lsa->data->type].count++;
129
5.20k
  lsdb->total++;
130
131
  /* Increment number of router LSAs received with DC bit set */
132
5.20k
  if (lsa->area && (lsa->area->lsdb == lsdb) && !IS_LSA_SELF(lsa) &&
133
607
      (lsa->data->type == OSPF_ROUTER_LSA) &&
134
41
      CHECK_FLAG(lsa->data->options, OSPF_OPTION_DC))
135
17
    lsa->area->fr_info.router_lsas_recv_dc_bit++;
136
137
5.20k
#ifdef MONITOR_LSDB_CHANGE
138
5.20k
  if (lsdb->new_lsa_hook != NULL)
139
1.00k
    (*lsdb->new_lsa_hook)(lsa);
140
5.20k
#endif /* MONITOR_LSDB_CHANGE */
141
5.20k
  lsdb->type[lsa->data->type].checksum += ntohs(lsa->data->checksum);
142
5.20k
  rn->info = ospf_lsa_lock(lsa); /* lsdb */
143
5.20k
}
144
145
void ospf_lsdb_delete(struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
146
139
{
147
139
  struct route_table *table;
148
139
  struct prefix_ls lp;
149
139
  struct route_node *rn;
150
151
139
  if (!lsdb || !lsa)
152
0
    return;
153
154
139
  assert(lsa->data->type < OSPF_MAX_LSA);
155
139
  table = lsdb->type[lsa->data->type].db;
156
139
  ls_prefix_set(&lp, lsa);
157
139
  if ((rn = route_node_lookup(table, (struct prefix *)&lp))) {
158
0
    if (rn->info == lsa)
159
0
      ospf_lsdb_delete_entry(lsdb, rn);
160
0
    route_unlock_node(rn); /* route_node_lookup */
161
0
  }
162
139
}
163
164
void ospf_lsdb_delete_all(struct ospf_lsdb *lsdb)
165
0
{
166
0
  struct route_table *table;
167
0
  struct route_node *rn;
168
0
  int i;
169
170
0
  for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
171
0
    table = lsdb->type[i].db;
172
0
    for (rn = route_top(table); rn; rn = route_next(rn))
173
0
      if (rn->info != NULL)
174
0
        ospf_lsdb_delete_entry(lsdb, rn);
175
0
  }
176
0
}
177
178
struct ospf_lsa *ospf_lsdb_lookup(struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
179
7.60k
{
180
7.60k
  struct route_table *table;
181
7.60k
  struct prefix_ls lp;
182
7.60k
  struct route_node *rn;
183
7.60k
  struct ospf_lsa *find;
184
185
7.60k
  table = lsdb->type[lsa->data->type].db;
186
7.60k
  ls_prefix_set(&lp, lsa);
187
7.60k
  rn = route_node_lookup(table, (struct prefix *)&lp);
188
7.60k
  if (rn) {
189
0
    find = rn->info;
190
0
    route_unlock_node(rn);
191
0
    return find;
192
0
  }
193
7.60k
  return NULL;
194
7.60k
}
195
196
struct ospf_lsa *ospf_lsdb_lookup_by_id(struct ospf_lsdb *lsdb, uint8_t type,
197
          struct in_addr id,
198
          struct in_addr adv_router)
199
36.5k
{
200
36.5k
  struct route_table *table;
201
36.5k
  struct prefix_ls lp;
202
36.5k
  struct route_node *rn;
203
36.5k
  struct ospf_lsa *find;
204
205
36.5k
  table = lsdb->type[type].db;
206
207
36.5k
  memset(&lp, 0, sizeof(lp));
208
36.5k
  lp.family = AF_UNSPEC;
209
36.5k
  lp.prefixlen = 64;
210
36.5k
  lp.id = id;
211
36.5k
  lp.adv_router = adv_router;
212
213
36.5k
  rn = route_node_lookup(table, (struct prefix *)&lp);
214
36.5k
  if (rn) {
215
11.6k
    find = rn->info;
216
11.6k
    route_unlock_node(rn);
217
11.6k
    return find;
218
11.6k
  }
219
24.8k
  return NULL;
220
36.5k
}
221
222
struct ospf_lsa *ospf_lsdb_lookup_by_id_next(struct ospf_lsdb *lsdb,
223
               uint8_t type, struct in_addr id,
224
               struct in_addr adv_router,
225
               int first)
226
0
{
227
0
  struct route_table *table;
228
0
  struct prefix_ls lp;
229
0
  struct route_node *rn;
230
0
  struct ospf_lsa *find;
231
232
0
  table = lsdb->type[type].db;
233
234
0
  memset(&lp, 0, sizeof(lp));
235
0
  lp.family = AF_UNSPEC;
236
0
  lp.prefixlen = 64;
237
0
  lp.id = id;
238
0
  lp.adv_router = adv_router;
239
240
0
  if (first)
241
0
    rn = route_top(table);
242
0
  else {
243
0
    if ((rn = route_node_lookup(table, (struct prefix *)&lp))
244
0
        == NULL)
245
0
      return NULL;
246
0
    rn = route_next(rn);
247
0
  }
248
249
0
  for (; rn; rn = route_next(rn))
250
0
    if (rn->info)
251
0
      break;
252
253
0
  if (rn && rn->info) {
254
0
    find = rn->info;
255
0
    route_unlock_node(rn);
256
0
    return find;
257
0
  }
258
0
  return NULL;
259
0
}
260
261
unsigned long ospf_lsdb_count_all(struct ospf_lsdb *lsdb)
262
0
{
263
0
  return lsdb->total;
264
0
}
265
266
unsigned long ospf_lsdb_count(struct ospf_lsdb *lsdb, int type)
267
0
{
268
0
  return lsdb->type[type].count;
269
0
}
270
271
unsigned long ospf_lsdb_count_self(struct ospf_lsdb *lsdb, int type)
272
0
{
273
0
  return lsdb->type[type].count_self;
274
0
}
275
276
unsigned int ospf_lsdb_checksum(struct ospf_lsdb *lsdb, int type)
277
0
{
278
0
  return lsdb->type[type].checksum;
279
0
}
280
281
unsigned long ospf_lsdb_isempty(struct ospf_lsdb *lsdb)
282
65
{
283
65
  return (lsdb->total == 0);
284
65
}