]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_instance.c
pimd: Remove unnecessary alloc failures
[mirror_frr.git] / pimd / pim_instance.c
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
26 #include "pimd.h"
27 #include "pim_ssm.h"
28 #include "pim_rpf.h"
29 #include "pim_rp.h"
30 #include "pim_mroute.h"
31 #include "pim_oil.h"
32 #include "pim_static.h"
33 #include "pim_ssmpingd.h"
34 #include "pim_vty.h"
35
36 static void pim_instance_terminate(struct pim_instance *pim)
37 {
38 if (pim->ssm_info) {
39 pim_ssm_terminate(pim->ssm_info);
40 pim->ssm_info = NULL;
41 }
42
43 if (pim->static_routes)
44 list_delete_and_null(&pim->static_routes);
45
46 pim_rp_free(pim);
47
48 pim_upstream_terminate(pim);
49
50 /* Traverse and cleanup rpf_hash */
51 if (pim->rpf_hash) {
52 hash_clean(pim->rpf_hash, (void *)pim_rp_list_hash_clean);
53 hash_free(pim->rpf_hash);
54 pim->rpf_hash = NULL;
55 }
56
57 pim_oil_terminate(pim);
58
59 pim_if_terminate(pim);
60
61 pim_msdp_exit(pim);
62
63 XFREE(MTYPE_PIM_PIM_INSTANCE, pim);
64 }
65
66 static struct pim_instance *pim_instance_init(struct vrf *vrf)
67 {
68 struct pim_instance *pim;
69 char hash_name[64];
70
71 pim = XCALLOC(MTYPE_PIM_PIM_INSTANCE, sizeof(struct pim_instance));
72
73 pim_if_init(pim);
74
75 pim->keep_alive_time = PIM_KEEPALIVE_PERIOD;
76 pim->rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;
77
78 pim->ecmp_enable = false;
79 pim->ecmp_rebalance_enable = false;
80
81 pim->vrf_id = vrf->vrf_id;
82 pim->vrf = vrf;
83
84 pim->spt.switchover = PIM_SPT_IMMEDIATE;
85 pim->spt.plist = NULL;
86
87 pim_msdp_init(pim, master);
88
89 snprintf(hash_name, 64, "PIM %s RPF Hash", vrf->name);
90 pim->rpf_hash = hash_create_size(256, pim_rpf_hash_key, pim_rpf_equal,
91 hash_name);
92
93 if (PIM_DEBUG_ZEBRA)
94 zlog_debug("%s: NHT rpf hash init ", __PRETTY_FUNCTION__);
95
96 pim->ssm_info = pim_ssm_init();
97 if (!pim->ssm_info) {
98 pim_instance_terminate(pim);
99 return NULL;
100 }
101
102 pim->static_routes = list_new();
103 pim->static_routes->del = (void (*)(void *))pim_static_route_free;
104
105 pim->send_v6_secondary = 1;
106
107 if (vrf->vrf_id == VRF_DEFAULT)
108 pimg = pim;
109
110 pim_rp_init(pim);
111
112 pim_oil_init(pim);
113
114 pim_upstream_init(pim);
115
116 pim->last_route_change_time = -1;
117 return pim;
118 }
119
120 struct pim_instance *pim_get_pim_instance(vrf_id_t vrf_id)
121 {
122 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
123
124 if (vrf)
125 return vrf->info;
126
127 return NULL;
128 }
129
130 static int pim_vrf_new(struct vrf *vrf)
131 {
132 struct pim_instance *pim = pim_instance_init(vrf);
133
134 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
135 if (pim == NULL) {
136 zlog_err("%s %s: pim class init failure ", __FILE__,
137 __PRETTY_FUNCTION__);
138 /*
139 * We will crash and burn otherwise
140 */
141 exit(1);
142 }
143
144 vrf->info = (void *)pim;
145
146 if (vrf->vrf_id == VRF_DEFAULT)
147 pimg = 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 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
158
159 pim_ssmpingd_destroy(pim);
160 pim_instance_terminate(pim);
161 return 0;
162 }
163
164 /*
165 * Code to turn on the pim instance that
166 * we have created with new
167 */
168 static int pim_vrf_enable(struct vrf *vrf)
169 {
170 struct pim_instance *pim = (struct pim_instance *)vrf->info;
171
172 zlog_debug("%s: for %s", __PRETTY_FUNCTION__, vrf->name);
173
174 pim_mroute_socket_enable(pim);
175
176 return 0;
177 }
178
179 static int pim_vrf_disable(struct vrf *vrf)
180 {
181 /* Note: This is a callback, the VRF will be deleted by the caller. */
182 return 0;
183 }
184
185 static int pim_vrf_config_write(struct vty *vty)
186 {
187 struct vrf *vrf;
188 struct pim_instance *pim;
189
190 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
191 pim = vrf->info;
192
193 if (!pim)
194 continue;
195
196 if (vrf->vrf_id != VRF_DEFAULT)
197 vty_frame(vty, "vrf %s\n", vrf->name);
198
199 pim_global_config_write_worker(pim, vty);
200
201 if (vrf->vrf_id != VRF_DEFAULT)
202 vty_endframe(vty, "!\n");
203 }
204
205 return 0;
206 }
207
208 void pim_vrf_init(void)
209 {
210 vrf_init(pim_vrf_new, pim_vrf_enable, pim_vrf_disable, pim_vrf_delete);
211
212 vrf_cmd_init(pim_vrf_config_write, &pimd_privs);
213 }
214
215 void pim_vrf_terminate(void)
216 {
217 vrf_terminate();
218 }