2 * Affinity map function.
4 * Copyright 2022 Hiroki Shirokura, LINE Corporation
5 * Copyright 2022 Masakazu Asama
6 * Copyright 2022 6WIND S.A.
8 * This file is part of Free Range Routing (FRR).
10 * FRR is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * FRR is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "affinitymap.h"
38 #include "lib_errors.h"
43 DEFINE_MTYPE_STATIC(LIB
, AFFINITY_MAP
, "Affinity map");
44 DEFINE_MTYPE(LIB
, AFFINITY_MAP_NAME
, "Affinity map name");
45 DEFINE_MTYPE_STATIC(LIB
, AFFINITY_MAP_INDEX
, "Affinity map index");
47 DEFINE_QOBJ_TYPE(affinity_maps
);
48 DEFINE_QOBJ_TYPE(affinity_map
);
50 struct affinity_maps affinity_map_master
= {NULL
, NULL
, NULL
, NULL
};
52 static void affinity_map_free(struct affinity_map
*map
)
54 XFREE(MTYPE_AFFINITY_MAP
, map
);
57 void affinity_map_set(const char *name
, int pos
)
59 struct listnode
*node
;
60 struct affinity_map
*map
;
62 if (!affinity_map_master
.maps
)
63 affinity_map_master
.maps
= list_new();
65 for (ALL_LIST_ELEMENTS_RO(affinity_map_master
.maps
, node
, map
)) {
66 if (strncmp(name
, map
->name
, AFFINITY_NAME_SIZE
) != 0)
68 map
->bit_position
= pos
;
72 map
= XCALLOC(MTYPE_AFFINITY_MAP
, sizeof(*map
));
73 map
->bit_position
= pos
;
74 snprintf(map
->name
, sizeof(map
->name
), "%s", name
);
75 listnode_add(affinity_map_master
.maps
, map
);
78 void affinity_map_unset(const char *name
)
80 struct listnode
*node
, *nnode
;
81 struct affinity_map
*map
;
83 if (!affinity_map_master
.maps
)
86 for (ALL_LIST_ELEMENTS(affinity_map_master
.maps
, node
, nnode
, map
)) {
87 if (strncmp(name
, map
->name
, AFFINITY_NAME_SIZE
) != 0)
89 listnode_delete(affinity_map_master
.maps
, map
);
90 affinity_map_free(map
);
95 struct affinity_map
*affinity_map_get(const char *name
)
97 struct listnode
*node
;
98 struct affinity_map
*map
;
100 if (!affinity_map_master
.maps
)
103 for (ALL_LIST_ELEMENTS_RO(affinity_map_master
.maps
, node
, map
))
104 if (strncmp(name
, map
->name
, AFFINITY_NAME_SIZE
) == 0)
110 char *affinity_map_name_get(int pos
)
112 struct listnode
*node
;
113 struct affinity_map
*map
;
115 if (!affinity_map_master
.maps
)
118 for (ALL_LIST_ELEMENTS_RO(affinity_map_master
.maps
, node
, map
))
119 if (map
->bit_position
== pos
)
124 bool affinity_map_check_use_hook(const char *affmap_name
)
126 if (affinity_map_master
.check_use_hook
)
127 return (*affinity_map_master
.check_use_hook
)(affmap_name
);
131 bool affinity_map_check_update_hook(const char *affmap_name
, uint16_t new_pos
)
133 if (affinity_map_master
.check_update_hook
)
134 return (*affinity_map_master
.check_update_hook
)(affmap_name
,
139 void affinity_map_update_hook(const char *affmap_name
, uint16_t new_pos
)
141 struct affinity_map
*map
;
143 if (!affinity_map_master
.update_hook
)
146 map
= affinity_map_get(affmap_name
);
149 /* Affinity-map creation */
152 (*affinity_map_master
.update_hook
)(affmap_name
, map
->bit_position
,
157 void affinity_map_set_check_use_hook(bool (*func
)(const char *affmap_name
))
159 affinity_map_master
.check_use_hook
= func
;
162 void affinity_map_set_check_update_hook(bool (*func
)(const char *affmap_name
,
165 affinity_map_master
.check_update_hook
= func
;
168 void affinity_map_set_update_hook(void (*func
)(const char *affmap_name
,
172 affinity_map_master
.update_hook
= func
;