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