summaryrefslogtreecommitdiffstats
path: root/lib/affinitymap.c
blob: 17e1b2cc01692fdf9df79a5c30004023a9fe1456 (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
/*
 * Affinity map function.
 *
 * Copyright 2022 Hiroki Shirokura, LINE Corporation
 * Copyright 2022 Masakazu Asama
 * Copyright 2022 6WIND S.A.
 *
 * This file is part of Free Range Routing (FRR).
 *
 * FRR 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, or (at your option) any
 * later version.
 *
 * FRR 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 <zebra.h>

#include "linklist.h"
#include "memory.h"
#include "command.h"
#include "vector.h"
#include "prefix.h"
#include "vty.h"
#include "affinitymap.h"
#include "command.h"
#include "log.h"
#include "hash.h"
#include "libfrr.h"
#include "lib_errors.h"
#include "table.h"
#include "json.h"
#include "jhash.h"

DEFINE_MTYPE_STATIC(LIB, AFFINITY_MAP, "Affinity map");
DEFINE_MTYPE(LIB, AFFINITY_MAP_NAME, "Affinity map name");
DEFINE_MTYPE_STATIC(LIB, AFFINITY_MAP_INDEX, "Affinity map index");

DEFINE_QOBJ_TYPE(affinity_maps);
DEFINE_QOBJ_TYPE(affinity_map);

struct affinity_maps affinity_map_master = {NULL, NULL, NULL, NULL};

static void affinity_map_free(struct affinity_map *map)
{
	XFREE(MTYPE_AFFINITY_MAP, map);
}

void affinity_map_set(const char *name, int pos)
{
	struct listnode *node;
	struct affinity_map *map;

	if (!affinity_map_master.maps)
		affinity_map_master.maps = list_new();

	for (ALL_LIST_ELEMENTS_RO(affinity_map_master.maps, node, map)) {
		if (strncmp(name, map->name, AFFINITY_NAME_SIZE) != 0)
			continue;
		map->bit_position = pos;
		return;
	}

	map = XCALLOC(MTYPE_AFFINITY_MAP, sizeof(*map));
	map->bit_position = pos;
	snprintf(map->name, sizeof(map->name), "%s", name);
	listnode_add(affinity_map_master.maps, map);
}

void affinity_map_unset(const char *name)
{
	struct listnode *node, *nnode;
	struct affinity_map *map;

	if (!affinity_map_master.maps)
		return;

	for (ALL_LIST_ELEMENTS(affinity_map_master.maps, node, nnode, map)) {
		if (strncmp(name, map->name, AFFINITY_NAME_SIZE) != 0)
			continue;
		listnode_delete(affinity_map_master.maps, map);
		affinity_map_free(map);
		return;
	}
}

struct affinity_map *affinity_map_get(const char *name)
{
	struct listnode *node;
	struct affinity_map *map;

	if (!affinity_map_master.maps)
		return NULL;

	for (ALL_LIST_ELEMENTS_RO(affinity_map_master.maps, node, map))
		if (strncmp(name, map->name, AFFINITY_NAME_SIZE) == 0)
			return map;
	return NULL;
}


char *affinity_map_name_get(int pos)
{
	struct listnode *node;
	struct affinity_map *map;

	if (!affinity_map_master.maps)
		return NULL;

	for (ALL_LIST_ELEMENTS_RO(affinity_map_master.maps, node, map))
		if (map->bit_position == pos)
			return map->name;
	return NULL;
}

bool affinity_map_check_use_hook(const char *affmap_name)
{
	if (affinity_map_master.check_use_hook)
		return (*affinity_map_master.check_use_hook)(affmap_name);
	return false;
}

bool affinity_map_check_update_hook(const char *affmap_name, uint16_t new_pos)
{
	if (affinity_map_master.check_update_hook)
		return (*affinity_map_master.check_update_hook)(affmap_name,
								new_pos);
	return true;
}

void affinity_map_update_hook(const char *affmap_name, uint16_t new_pos)
{
	struct affinity_map *map;

	if (!affinity_map_master.update_hook)
		return;

	map = affinity_map_get(affmap_name);

	if (!map)
		/* Affinity-map creation */
		return;

	(*affinity_map_master.update_hook)(affmap_name, map->bit_position,
					   new_pos);
}


void affinity_map_set_check_use_hook(bool (*func)(const char *affmap_name))
{
	affinity_map_master.check_use_hook = func;
}

void affinity_map_set_check_update_hook(bool (*func)(const char *affmap_name,
						     uint16_t new_pos))
{
	affinity_map_master.check_update_hook = func;
}

void affinity_map_set_update_hook(void (*func)(const char *affmap_name,
					       uint16_t old_pos,
					       uint16_t new_pos))
{
	affinity_map_master.update_hook = func;
}