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