/src/frr/zebra/zebra_affinitymap.c
Line | Count | Source |
1 | | /* |
2 | | * zebra affinity-map. |
3 | | * |
4 | | * Copyright 2022 6WIND S.A. |
5 | | * |
6 | | * This file is part of Free Range Routing (FRR). |
7 | | * |
8 | | * FRR is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU General Public License as published by the |
10 | | * Free Software Foundation; either version 2, or (at your option) any |
11 | | * later version. |
12 | | * |
13 | | * FRR is distributed in the hope that it will be useful, but |
14 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License along |
19 | | * with this program; see the file COPYING; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include <zebra.h> |
24 | | #include "lib/if.h" |
25 | | #include "lib/vrf.h" |
26 | | #include "zebra/redistribute.h" |
27 | | #include "zebra/zebra_affinitymap.h" |
28 | | |
29 | | static bool zebra_affinity_map_check_use(const char *affmap_name) |
30 | 0 | { |
31 | 0 | char xpath[XPATH_MAXLEN]; |
32 | 0 | struct interface *ifp; |
33 | 0 | struct vrf *vrf; |
34 | |
|
35 | 0 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
36 | 0 | FOR_ALL_INTERFACES (vrf, ifp) { |
37 | 0 | snprintf(xpath, sizeof(xpath), |
38 | 0 | "/frr-interface:lib/interface[name='%s']", |
39 | 0 | ifp->name); |
40 | 0 | if (!yang_dnode_exists(running_config->dnode, xpath)) |
41 | 0 | continue; |
42 | 0 | snprintf( |
43 | 0 | xpath, sizeof(xpath), |
44 | 0 | "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", |
45 | 0 | ifp->name, affmap_name); |
46 | 0 | if (yang_dnode_exists(running_config->dnode, xpath)) |
47 | 0 | return true; |
48 | 0 | } |
49 | 0 | } |
50 | 0 | return false; |
51 | 0 | } |
52 | | |
53 | | static bool zebra_affinity_map_check_update(const char *affmap_name, |
54 | | uint16_t new_pos) |
55 | 0 | { |
56 | 0 | char xpath[XPATH_MAXLEN]; |
57 | 0 | struct interface *ifp; |
58 | 0 | struct vrf *vrf; |
59 | | |
60 | | /* check whether the affinity-map new bit position is upper than 31 |
61 | | * but is used on an interface on which affinity-mode is standard. |
62 | | * Return false if the change is not possible. |
63 | | */ |
64 | 0 | if (new_pos < 32) |
65 | 0 | return true; |
66 | | |
67 | 0 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
68 | 0 | FOR_ALL_INTERFACES (vrf, ifp) { |
69 | 0 | snprintf(xpath, sizeof(xpath), |
70 | 0 | "/frr-interface:lib/interface[name='%s']", |
71 | 0 | ifp->name); |
72 | 0 | if (!yang_dnode_exists(running_config->dnode, xpath)) |
73 | 0 | continue; |
74 | 0 | snprintf( |
75 | 0 | xpath, sizeof(xpath), |
76 | 0 | "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", |
77 | 0 | ifp->name, affmap_name); |
78 | 0 | if (!yang_dnode_exists(running_config->dnode, xpath)) |
79 | 0 | continue; |
80 | 0 | if (yang_dnode_get_enum( |
81 | 0 | running_config->dnode, |
82 | 0 | "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode", |
83 | 0 | ifp->name) == AFFINITY_MODE_STANDARD) |
84 | 0 | return false; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | return true; |
88 | 0 | } |
89 | | |
90 | | static void zebra_affinity_map_update(const char *affmap_name, uint16_t old_pos, |
91 | | uint16_t new_pos) |
92 | 0 | { |
93 | 0 | struct if_link_params *iflp; |
94 | 0 | enum affinity_mode aff_mode; |
95 | 0 | char xpath[XPATH_MAXLEN]; |
96 | 0 | struct interface *ifp; |
97 | 0 | struct vrf *vrf; |
98 | |
|
99 | 0 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) { |
100 | 0 | FOR_ALL_INTERFACES (vrf, ifp) { |
101 | 0 | snprintf(xpath, sizeof(xpath), |
102 | 0 | "/frr-interface:lib/interface[name='%s']", |
103 | 0 | ifp->name); |
104 | 0 | if (!yang_dnode_exists(running_config->dnode, xpath)) |
105 | 0 | continue; |
106 | 0 | snprintf( |
107 | 0 | xpath, sizeof(xpath), |
108 | 0 | "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinities[affinity='%s']", |
109 | 0 | ifp->name, affmap_name); |
110 | 0 | if (!yang_dnode_exists(running_config->dnode, xpath)) |
111 | 0 | continue; |
112 | 0 | aff_mode = yang_dnode_get_enum( |
113 | 0 | running_config->dnode, |
114 | 0 | "/frr-interface:lib/interface[name='%s']/frr-zebra:zebra/link-params/affinity-mode", |
115 | 0 | ifp->name); |
116 | 0 | iflp = if_link_params_get(ifp); |
117 | 0 | if (aff_mode == AFFINITY_MODE_EXTENDED || |
118 | 0 | aff_mode == AFFINITY_MODE_BOTH) { |
119 | 0 | admin_group_unset(&iflp->ext_admin_grp, |
120 | 0 | old_pos); |
121 | 0 | admin_group_set(&iflp->ext_admin_grp, new_pos); |
122 | 0 | } |
123 | 0 | if (aff_mode == AFFINITY_MODE_STANDARD || |
124 | 0 | aff_mode == AFFINITY_MODE_BOTH) { |
125 | 0 | iflp->admin_grp &= ~(1 << old_pos); |
126 | 0 | if (new_pos < 32) |
127 | 0 | iflp->admin_grp |= 1 << new_pos; |
128 | 0 | if (iflp->admin_grp == 0) |
129 | 0 | UNSET_PARAM(iflp, LP_ADM_GRP); |
130 | 0 | } |
131 | 0 | if (if_is_operative(ifp)) |
132 | 0 | zebra_interface_parameters_update(ifp); |
133 | 0 | } |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | | void zebra_affinity_map_init(void) |
138 | 1 | { |
139 | 1 | affinity_map_init(); |
140 | | |
141 | 1 | affinity_map_set_check_use_hook(zebra_affinity_map_check_use); |
142 | 1 | affinity_map_set_check_update_hook(zebra_affinity_map_check_update); |
143 | 1 | affinity_map_set_update_hook(zebra_affinity_map_update); |
144 | 1 | } |