summaryrefslogtreecommitdiffstats
path: root/zebra/zebra_affinitymap.c
blob: ae0f9a8a354d8ce6aa475b3f6d6cc825358433dc (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
/*
 * zebra affinity-map.
 *
 * 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 "lib/if.h"
#include "lib/vrf.h"
#include "zebra/redistribute.h"
#include "zebra/zebra_affinitymap.h"

static bool zebra_affinity_map_check_use(const char *affmap_name)
{
	char xpath[XPATH_MAXLEN];
	struct interface *ifp;
	struct vrf *vrf;

	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
		FOR_ALL_INTERFACES (vrf, ifp) {
			snprintf(xpath, sizeof(xpath),
				 "/frr-interface:lib/interface[name='%s']",
				 ifp->name);
			if (!yang_dnode_exists(running_config->dnode, xpath))
				continue;
			snprintf(
				xpath, sizeof(xpath),
				"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']",
				ifp->name, affmap_name);
			if (yang_dnode_exists(running_config->dnode, xpath))
				return true;
		}
	}
	return false;
}

static bool zebra_affinity_map_check_update(const char *affmap_name,
					    uint16_t new_pos)
{
	char xpath[XPATH_MAXLEN];
	struct interface *ifp;
	struct vrf *vrf;

	/* check whether the affinity-map new bit position is upper than 31
	 * but is used on an interface on which affinity-mode is standard.
	 * Return false if the change is not possible.
	 */
	if (new_pos < 32)
		return true;

	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
		FOR_ALL_INTERFACES (vrf, ifp) {
			snprintf(xpath, sizeof(xpath),
				 "/frr-interface:lib/interface[name='%s']",
				 ifp->name);
			if (!yang_dnode_exists(running_config->dnode, xpath))
				continue;
			snprintf(
				xpath, sizeof(xpath),
				"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']",
				ifp->name, affmap_name);
			if (!yang_dnode_exists(running_config->dnode, xpath))
				continue;
			if (yang_dnode_get_enum(
				    running_config->dnode,
				    "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode",
				    ifp->name) == AFFINITY_MODE_STANDARD)
				return false;
		}
	}
	return true;
}

static void zebra_affinity_map_update(const char *affmap_name, uint16_t old_pos,
				      uint16_t new_pos)
{
	struct if_link_params *iflp;
	enum affinity_mode aff_mode;
	char xpath[XPATH_MAXLEN];
	struct interface *ifp;
	struct vrf *vrf;

	RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
		FOR_ALL_INTERFACES (vrf, ifp) {
			snprintf(xpath, sizeof(xpath),
				 "/frr-interface:lib/interface[name='%s']",
				 ifp->name);
			if (!yang_dnode_exists(running_config->dnode, xpath))
				continue;
			snprintf(
				xpath, sizeof(xpath),
				"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']",
				ifp->name, affmap_name);
			if (!yang_dnode_exists(running_config->dnode, xpath))
				continue;
			aff_mode = yang_dnode_get_enum(
				running_config->dnode,
				"/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode",
				ifp->name);
			iflp = if_link_params_get(ifp);
			if (aff_mode == AFFINITY_MODE_EXTENDED ||
			    aff_mode == AFFINITY_MODE_BOTH) {
				admin_group_unset(&iflp->ext_admin_grp,
						  old_pos);
				admin_group_set(&iflp->ext_admin_grp, new_pos);
			}
			if (aff_mode == AFFINITY_MODE_STANDARD ||
			    aff_mode == AFFINITY_MODE_BOTH) {
				iflp->admin_grp &= ~(1 << old_pos);
				if (new_pos < 32)
					iflp->admin_grp |= 1 << new_pos;
				if (iflp->admin_grp == 0)
					UNSET_PARAM(iflp, LP_ADM_GRP);
			}
			if (if_is_operative(ifp))
				zebra_interface_parameters_update(ifp);
		}
	}
}

void zebra_affinity_map_init(void)
{
	affinity_map_init();

	affinity_map_set_check_use_hook(zebra_affinity_map_check_use);
	affinity_map_set_check_update_hook(zebra_affinity_map_check_update);
	affinity_map_set_update_hook(zebra_affinity_map_update);
}