]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_instance.c
*: Add a hash_clean_and_free() function
[mirror_frr.git] / pimd / pim_instance.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PIM for FRR - PIM Instance
4 * Copyright (C) 2017 Cumulus Networks, Inc.
5 * Donald Sharp
6 */
7 #include <zebra.h>
8
9 #include "hash.h"
10 #include "vrf.h"
11 #include "lib_errors.h"
12
13 #include "pimd.h"
14 #include "pim_instance.h"
15 #include "pim_ssm.h"
16 #include "pim_rpf.h"
17 #include "pim_rp.h"
18 #include "pim_mroute.h"
19 #include "pim_oil.h"
20 #include "pim_static.h"
21 #include "pim_ssmpingd.h"
22 #include "pim_vty.h"
23 #include "pim_bsm.h"
24 #include "pim_mlag.h"
25 #include "pim_sock.h"
26
27 static void pim_instance_terminate(struct pim_instance *pim)
28 {
29 pim_vxlan_exit(pim);
30
31 if (pim->ssm_info) {
32 pim_ssm_terminate(pim->ssm_info);
33 pim->ssm_info = NULL;
34 }
35
36 if (pim->static_routes)
37 list_delete(&pim->static_routes);
38
39 pim_instance_mlag_terminate(pim);
40
41 pim_upstream_terminate(pim);
42
43 pim_rp_free(pim);
44
45 pim_bsm_proc_free(pim);
46
47 /* Traverse and cleanup rpf_hash */
48 hash_clean_and_free(&pim->rpf_hash, (void *)pim_rp_list_hash_clean);
49
50 pim_if_terminate(pim);
51
52 pim_oil_terminate(pim);
53
54 pim_msdp_exit(pim);
55
56 close(pim->reg_sock);
57
58 pim_mroute_socket_disable(pim);
59
60 XFREE(MTYPE_PIM_PLIST_NAME, pim->spt.plist);
61 XFREE(MTYPE_PIM_PLIST_NAME, pim->register_plist);
62
63 pim->vrf = NULL;
64 XFREE(MTYPE_PIM_PIM_INSTANCE, pim);
65 }
66
67 static struct pim_instance *pim_instance_init(struct vrf *vrf)
68 {
69 struct pim_instance *pim;
70 char hash_name[64];
71
72 pim = XCALLOC(MTYPE_PIM_PIM_INSTANCE, sizeof(struct pim_instance));
73
74 pim_if_init(pim);
75
76 pim->mcast_if_count = 0;
77 pim->keep_alive_time = PIM_KEEPALIVE_PERIOD;
78 pim->rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;
79
80 pim->ecmp_enable = false;
81 pim->ecmp_rebalance_enable = false;
82
83 pim->vrf = vrf;
84
85 pim->spt.switchover = PIM_SPT_IMMEDIATE;
86 pim->spt.plist = NULL;
87
88 pim_msdp_init(pim, router->master);
89 pim_vxlan_init(pim);
90
91 snprintf(hash_name, sizeof(hash_name), "PIM %s RPF Hash", vrf->name);
92 pim->rpf_hash = hash_create_size(256, pim_rpf_hash_key, pim_rpf_equal,
93 hash_name);
94
95 if (PIM_DEBUG_ZEBRA)
96 zlog_debug("%s: NHT rpf hash init ", __func__);
97
98 pim->ssm_info = pim_ssm_init();
99
100 pim->static_routes = list_new();
101 pim->static_routes->del = (void (*)(void *))pim_static_route_free;
102
103 pim->send_v6_secondary = 1;
104
105 pim->gm_socket = -1;
106
107 pim_rp_init(pim);
108
109 pim_bsm_proc_init(pim);
110
111 pim_oil_init(pim);
112
113 pim_upstream_init(pim);
114
115 pim_instance_mlag_init(pim);
116
117 pim->last_route_change_time = -1;
118
119 pim->reg_sock = pim_reg_sock();
120 if (pim->reg_sock < 0)
121 assert(0);
122
123 /* MSDP global timer defaults. */
124 pim->msdp.hold_time = PIM_MSDP_PEER_HOLD_TIME;
125 pim->msdp.keep_alive = PIM_MSDP_PEER_KA_TIME;
126 pim->msdp.connection_retry = PIM_MSDP_PEER_CONNECT_RETRY_TIME;
127
128 return pim;
129 }
130
131 struct pim_instance *pim_get_pim_instance(vrf_id_t vrf_id)
132 {
133 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
134
135 if (vrf)
136 return vrf->info;
137
138 return NULL;
139 }
140
141 static int pim_vrf_new(struct vrf *vrf)
142 {
143 struct pim_instance *pim = pim_instance_init(vrf);
144
145 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
146
147 vrf->info = (void *)pim;
148
149 pim_ssmpingd_init(pim);
150 return 0;
151 }
152
153 static int pim_vrf_delete(struct vrf *vrf)
154 {
155 struct pim_instance *pim = vrf->info;
156
157 if (!pim)
158 return 0;
159
160 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
161
162 pim_ssmpingd_destroy(pim);
163 pim_instance_terminate(pim);
164
165 vrf->info = NULL;
166
167 return 0;
168 }
169
170 /*
171 * Code to turn on the pim instance that
172 * we have created with new
173 */
174 static int pim_vrf_enable(struct vrf *vrf)
175 {
176 struct pim_instance *pim = (struct pim_instance *)vrf->info;
177 struct interface *ifp;
178
179 zlog_debug("%s: for %s %u", __func__, vrf->name, vrf->vrf_id);
180
181 FOR_ALL_INTERFACES (vrf, ifp) {
182 if (!ifp->info)
183 continue;
184
185 pim_if_create_pimreg(pim);
186 break;
187 }
188
189 pim_mroute_socket_enable(pim);
190
191 return 0;
192 }
193
194 static int pim_vrf_disable(struct vrf *vrf)
195 {
196 /* Note: This is a callback, the VRF will be deleted by the caller. */
197 return 0;
198 }
199
200 static int pim_vrf_config_write(struct vty *vty)
201 {
202 struct vrf *vrf;
203 struct pim_instance *pim;
204
205 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
206 pim = vrf->info;
207
208 if (!pim)
209 continue;
210
211 if (vrf->vrf_id != VRF_DEFAULT)
212 vty_frame(vty, "vrf %s\n", vrf->name);
213
214 pim_global_config_write_worker(pim, vty);
215
216 if (vrf->vrf_id != VRF_DEFAULT)
217 vty_endframe(vty, "exit-vrf\n!\n");
218 }
219
220 return 0;
221 }
222
223 void pim_vrf_init(void)
224 {
225 vrf_init(pim_vrf_new, pim_vrf_enable, pim_vrf_disable, pim_vrf_delete);
226
227 vrf_cmd_init(pim_vrf_config_write);
228 }
229
230 void pim_vrf_terminate(void)
231 {
232 struct vrf *vrf;
233
234 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
235 struct pim_instance *pim;
236
237 pim = vrf->info;
238 if (!pim)
239 continue;
240
241 pim_ssmpingd_destroy(pim);
242 pim_instance_terminate(pim);
243
244 vrf->info = NULL;
245 }
246
247 vrf_terminate();
248 }