summaryrefslogtreecommitdiffstats
path: root/lib/admin_group.c
blob: 9c2c2c08ee7e32f0fc3a26539924628e8d4c11fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Administrative-group library (RFC3630, RFC5305, RFC5329, RFC7308)
 *
 * Copyright 2022 Hiroki Shirokura, LINE Corporation
 * Copyright 2022 Masakazu Asama
 * Copyright 2022 6WIND S.A.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "admin_group.h"
#include "bitfield.h"

char *admin_group_string(char *out, size_t sz, int indent,
			 const struct admin_group *ag)
{
	bool printed = false;
	size_t index = 2;
	int nb_print = 0;

	if (sz < index)
		return out;

	if (admin_group_explicit_zero(ag)) {
		snprintf(out, sz, "0x00000000");
		return out;
	}

	if (admin_group_zero(ag)) {
		snprintf(out, sz, "not-set");
		return out;
	}

	snprintf(out, sz, "0x");
	for (ssize_t i = ag->bitmap.m - 1; i >= 0; i--) {
		if (sz - index <= 0)
			break;
		if (ag->bitmap.data[i] == 0 && !printed)
			continue;
		if (nb_print != 0 && (nb_print % 4) == 0) {
			snprintf(&out[index], sz - index, "\n%*s", indent, "");
			index += indent + 1;
			snprintf(&out[index], sz - index, "0x%08x ",
				 ag->bitmap.data[i]);
			index += 2;
		} else
			snprintf(&out[index], sz - index, "%08x ",
				 ag->bitmap.data[i]);
		index += 9;
		nb_print++;
		printed = true;
	}
	return out;
}

char *admin_group_standard_print(char *out, int indent, uint32_t bitmap)
{
	bool first = true;
	int bit, i;
	size_t ret, line_sz = 0, line_max_sz;

	out[0] = '\0';

	if (bitmap == 0) {
		snprintf(out, ADMIN_GROUP_PRINT_MAX_SIZE, "not-set");
		return out;
	}

	line_max_sz = strlen("0xffffffff ffffffff ffffffff ffffffff");

	for (i = 0; i < 32; i++) {
		bit = bitmap >> i & 1;
		if (bit == 0)
			continue;
		if (!first) {
			ret = snprintf(&out[strlen(out)],
				       ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out),
				       ", ");
			line_sz += ret;
		}
		if (line_sz >= line_max_sz) {
			snprintf(&out[strlen(out)],
				 ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out),
				 "\n%*s", indent, "");

			line_sz = 0;
		}
		ret = snprintf(&out[strlen(out)],
			       ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out), "%d",
			       i);
		line_sz += ret;
		first = false;
	}

	return out;
}

char *admin_group_print(char *out, int indent, const struct admin_group *ag)
{
	bool first = true;
	uint32_t i;
	size_t ret, line_sz = 0, line_max_sz;

	out[0] = '\0';

	if (admin_group_size(ag) == 0) {
		snprintf(out, ADMIN_GROUP_PRINT_MAX_SIZE, "not-set");
		return out;
	}

	line_max_sz = strlen("0xffffffff ffffffff ffffffff ffffffff");

	for (i = 0; i < (admin_group_size(ag) * WORD_SIZE); i++) {
		if (!admin_group_get(ag, i))
			continue;
		if (!first) {
			ret = snprintf(&out[strlen(out)],
				       ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out),
				       ", ");
			line_sz += ret;
		}
		if (line_sz >= line_max_sz) {
			snprintf(&out[strlen(out)],
				 ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out),
				 "\n%*s", indent, "");

			line_sz = 0;
		}
		ret = snprintf(&out[strlen(out)],
			       ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out), "%d",
			       i);
		line_sz += ret;
		if (ret >= (ADMIN_GROUP_PRINT_MAX_SIZE - strlen(out))) {
			out[0] = '\0';
			return out;
		}
		first = false;
	}

	return out;
}

bool admin_group_cmp(const struct admin_group *ag1,
		     const struct admin_group *ag2)
{
	size_t i;

	for (i = 0; i < ag1->bitmap.m || i < ag2->bitmap.m; i++) {
		if (i >= ag1->bitmap.m) {
			if (ag2->bitmap.data[i] != 0)
				return false;
		} else if (i >= ag2->bitmap.m) {
			if (ag1->bitmap.data[i] != 0)
				return false;
		} else if (memcmp(&ag1->bitmap.data[i], &ag2->bitmap.data[i],
				  sizeof(word_t)) != 0)
			return false;
	}

	return true;
}

void admin_group_copy(struct admin_group *dst, const struct admin_group *src)
{
	assert(bf_is_inited(src->bitmap));
	if (bf_is_inited(dst->bitmap))
		bf_free(dst->bitmap);
	dst->bitmap = bf_copy(src->bitmap);
}

void admin_group_init(struct admin_group *ag)
{
	assert(!bf_is_inited(ag->bitmap));
	bf_init(ag->bitmap, WORD_SIZE);
}

void admin_group_term(struct admin_group *ag)
{
	assert(bf_is_inited(ag->bitmap));
	bf_free(ag->bitmap);
}

word_t admin_group_get_offset(const struct admin_group *ag, size_t oct_offset)
{
	assert(bf_is_inited(ag->bitmap));
	if (ag->bitmap.m < oct_offset)
		return 0;
	return ag->bitmap.data[oct_offset];
}

static void admin_group_extend(struct admin_group *ag, size_t idx)
{
	size_t old_m, m;

	old_m = ag->bitmap.m;
	m = idx + 1;
	ag->bitmap.m = m;
	ag->bitmap.data =
		XREALLOC(MTYPE_BITFIELD, ag->bitmap.data, m * sizeof(word_t));
	memset(&ag->bitmap.data[old_m], 0, (m - old_m) * sizeof(word_t));
}

void admin_group_set(struct admin_group *ag, size_t pos)
{
	size_t idx = bf_index(pos);

	if (idx >= ag->bitmap.m)
		admin_group_extend(ag, idx);

	ag->bitmap.data[idx] |= 1 << (bf_offset(pos));

	if (idx >= ag->bitmap.n)
		ag->bitmap.n = idx + 1;
}

void admin_group_unset(struct admin_group *ag, size_t pos)
{
	if (bf_index(pos) > (ag->bitmap.m - 1))
		return;
	bf_release_index(ag->bitmap, pos);
	ag->bitmap.n = admin_group_size(ag);
}

int admin_group_get(const struct admin_group *ag, size_t pos)
{
	size_t admin_group_length = admin_group_size(ag);
	uint32_t oct_offset;
	size_t idx;

	if (admin_group_length == 0)
		return 0;

	idx = bf_index(pos);

	if (idx >= admin_group_length)
		return 0;

	oct_offset = admin_group_get_offset(ag, idx);
	return oct_offset >> pos & 1;
}

void admin_group_bulk_set(struct admin_group *ag, uint32_t bitmap,
			  size_t oct_offset)
{

	if (bitmap == 0 && oct_offset == 0) {
		admin_group_allow_explicit_zero(ag);
		return;
	}

	if (oct_offset >= ag->bitmap.m)
		admin_group_extend(ag, oct_offset);

	ag->bitmap.data[oct_offset] = bitmap;

	if (oct_offset >= ag->bitmap.n)
		ag->bitmap.n = oct_offset + 1;
}

size_t admin_group_size(const struct admin_group *ag)
{
	size_t size = 0;

	for (size_t i = 0; i < ag->bitmap.m; i++)
		if (ag->bitmap.data[i] != 0)
			size = i + 1;
	return size;
}

size_t admin_group_nb_words(const struct admin_group *ag)
{
	return ag->bitmap.n;
}

void admin_group_clear(struct admin_group *ag)
{
	for (size_t i = 0; i < ag->bitmap.m; i++)
		ag->bitmap.data[i] = 0;
	ag->bitmap.n = 0;
}

bool admin_group_zero(const struct admin_group *ag)
{
	for (size_t i = 0; i < ag->bitmap.m; i++)
		if (ag->bitmap.data[i] != 0)
			return false;
	return true;
}


bool admin_group_explicit_zero(const struct admin_group *ag)
{
	return ag->bitmap.n == 1 && ag->bitmap.data[0] == 0;
}

void admin_group_allow_explicit_zero(struct admin_group *ag)
{
	if (admin_group_zero(ag))
		ag->bitmap.n = 1;
}

void admin_group_disallow_explicit_zero(struct admin_group *ag)
{
	if (admin_group_zero(ag))
		ag->bitmap.n = 0;
}

/* link_std_ag: admin-group in the RFC5305 section 3.1 format
 * link_ext_ag: admin-group in the RFC7308 format
 * RFC7308 specifies in section 2.3.1 that:
 * "If both an AG and EAG are present, a receiving node MUST use the AG
 * as the first 32 bits (0-31) of administrative color and use the EAG
 * for bits 32 and higher, if present."
 */
bool admin_group_match_any(const struct admin_group *fad_ag,
			   const uint32_t *link_std_ag,
			   const struct admin_group *link_ext_ag)
{
	size_t fad_ag_sz, link_ag_sz, i;
	uint32_t link_ag_bitmap, fad_ag_bitmap;

	assert(fad_ag);

	/* get the size of admin-groups: i.e. number of used words */
	fad_ag_sz = admin_group_size(fad_ag);
	if (link_std_ag && link_ext_ag) {
		link_ag_sz = admin_group_size(link_ext_ag);
		if (link_ag_sz == 0)
			link_ag_sz = 1;
	} else if (link_std_ag && !link_ext_ag)
		link_ag_sz = 1;
	else if (!link_std_ag && link_ext_ag)
		link_ag_sz = admin_group_size(link_ext_ag);
	else
		link_ag_sz = 0;

	for (i = 0; i < fad_ag_sz && i < link_ag_sz; i++) {
		fad_ag_bitmap = fad_ag->bitmap.data[i];
		if (i == 0 && link_std_ag)
			link_ag_bitmap = *link_std_ag;
		else
			link_ag_bitmap = link_ext_ag->bitmap.data[i];

		if (fad_ag_bitmap & link_ag_bitmap)
			return true;
	}
	return false;
}

/* same comments as admin_group_match_any() */
bool admin_group_match_all(const struct admin_group *fad_ag,
			   const uint32_t *link_std_ag,
			   const struct admin_group *link_ext_ag)
{
	size_t fad_ag_sz, link_ag_sz, i;
	uint32_t link_ag_bitmap, fad_ag_bitmap;

	assert(fad_ag);

	/* get the size of admin-groups: i.e. number of used words */
	fad_ag_sz = admin_group_size(fad_ag);
	if (link_std_ag && link_ext_ag) {
		link_ag_sz = admin_group_size(link_ext_ag);
		if (link_ag_sz == 0)
			link_ag_sz = 1;
	} else if (link_std_ag && !link_ext_ag)
		link_ag_sz = 1;
	else if (!link_std_ag && link_ext_ag)
		link_ag_sz = admin_group_size(link_ext_ag);
	else
		link_ag_sz = 0;

	if (fad_ag_sz > link_ag_sz)
		return false;

	for (i = 0; i < fad_ag_sz; i++) {
		fad_ag_bitmap = fad_ag->bitmap.data[i];
		if (fad_ag_bitmap == 0)
			continue;

		if (i == 0 && link_std_ag)
			link_ag_bitmap = *link_std_ag;
		else
			link_ag_bitmap = link_ext_ag->bitmap.data[i];

		if ((fad_ag_bitmap & link_ag_bitmap) != fad_ag_bitmap)
			return false;
	}
	return true;
}