3 Copyright (C) 2008 Everton da Silva Marques
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
34 #include "pim_iface.h"
35 #include "pim_zebra.h"
39 #include "pim_upstream.h"
41 #include "pim_ssmpingd.h"
42 #include "pim_static.h"
45 #include "pim_zlookup.h"
48 const char *const PIM_ALL_SYSTEMS
= MCAST_ALL_SYSTEMS
;
49 const char *const PIM_ALL_ROUTERS
= MCAST_ALL_ROUTERS
;
50 const char *const PIM_ALL_PIM_ROUTERS
= MCAST_ALL_PIM_ROUTERS
;
51 const char *const PIM_ALL_IGMP_ROUTERS
= MCAST_ALL_IGMP_ROUTERS
;
53 struct thread_master
*master
= NULL
;
54 uint32_t qpim_debugs
= 0;
55 int qpim_mroute_socket_fd
= -1;
56 int64_t qpim_mroute_socket_creation
= 0; /* timestamp of creation */
58 PIM_DEFAULT_T_PERIODIC
; /* Period between Join/Prune Messages */
59 struct pim_assert_metric qpim_infinite_assert_metric
;
60 long qpim_rpf_cache_refresh_delay_msec
= 50;
61 struct thread
*qpim_rpf_cache_refresher
= NULL
;
62 int64_t qpim_rpf_cache_refresh_requests
= 0;
63 int64_t qpim_rpf_cache_refresh_events
= 0;
64 int64_t qpim_rpf_cache_refresh_last
= 0;
65 struct in_addr qpim_inaddr_any
;
66 struct list
*qpim_ssmpingd_list
= NULL
;
67 struct in_addr qpim_ssmpingd_group_addr
;
68 int64_t qpim_scan_oil_events
= 0;
69 int64_t qpim_scan_oil_last
= 0;
70 int64_t qpim_mroute_add_events
= 0;
71 int64_t qpim_mroute_add_last
= 0;
72 int64_t qpim_mroute_del_events
= 0;
73 int64_t qpim_mroute_del_last
= 0;
74 struct list
*qpim_static_route_list
= NULL
;
75 unsigned int qpim_keep_alive_time
= PIM_KEEPALIVE_PERIOD
;
76 signed int qpim_rp_keep_alive_time
= 0;
77 int64_t qpim_nexthop_lookups
= 0;
78 int qpim_packet_process
= PIM_DEFAULT_PACKET_PROCESS
;
79 uint8_t qpim_ecmp_enable
= 0;
80 uint8_t qpim_ecmp_rebalance_enable
= 0;
81 struct pim_instance
*pimg
= NULL
;
83 int32_t qpim_register_suppress_time
= PIM_REGISTER_SUPPRESSION_TIME_DEFAULT
;
84 int32_t qpim_register_probe_time
= PIM_REGISTER_PROBE_TIME_DEFAULT
;
86 static struct pim_instance
*pim_instance_init(vrf_id_t vrf_id
, afi_t afi
);
87 static void pim_instance_terminate(void);
89 static int pim_vrf_new(struct vrf
*vrf
)
91 zlog_debug("VRF Created: %s(%d)", vrf
->name
, vrf
->vrf_id
);
95 static int pim_vrf_delete(struct vrf
*vrf
)
97 zlog_debug("VRF Deletion: %s(%d)", vrf
->name
, vrf
->vrf_id
);
101 static int pim_vrf_enable(struct vrf
*vrf
)
104 if (!vrf
) // unexpected
107 if (vrf
->vrf_id
== VRF_DEFAULT
) {
108 pimg
= pim_instance_init(VRF_DEFAULT
, AFI_IP
);
110 zlog_err("%s %s: pim class init failure ", __FILE__
,
111 __PRETTY_FUNCTION__
);
113 * We will crash and burn otherwise
121 static int pim_vrf_disable(struct vrf
*vrf
)
123 if (vrf
->vrf_id
== VRF_DEFAULT
)
126 if (vrf
->vrf_id
== VRF_DEFAULT
)
127 pim_instance_terminate();
129 /* Note: This is a callback, the VRF will be deleted by the caller. */
133 void pim_vrf_init(void)
135 vrf_add_hook(VRF_NEW_HOOK
, pim_vrf_new
);
136 vrf_add_hook(VRF_ENABLE_HOOK
, pim_vrf_enable
);
137 vrf_add_hook(VRF_DISABLE_HOOK
, pim_vrf_disable
);
138 vrf_add_hook(VRF_DELETE_HOOK
, pim_vrf_delete
);
143 static void pim_vrf_terminate(void)
145 vrf_add_hook(VRF_NEW_HOOK
, NULL
);
146 vrf_add_hook(VRF_ENABLE_HOOK
, NULL
);
147 vrf_add_hook(VRF_DISABLE_HOOK
, NULL
);
148 vrf_add_hook(VRF_DELETE_HOOK
, NULL
);
153 /* Key generate for pim->rpf_hash */
154 static unsigned int pim_rpf_hash_key(void *arg
)
156 struct pim_nexthop_cache
*r
= (struct pim_nexthop_cache
*)arg
;
158 return jhash_1word(r
->rpf
.rpf_addr
.u
.prefix4
.s_addr
, 0);
161 /* Compare pim->rpf_hash node data */
162 static int pim_rpf_equal(const void *arg1
, const void *arg2
)
164 const struct pim_nexthop_cache
*r1
=
165 (const struct pim_nexthop_cache
*)arg1
;
166 const struct pim_nexthop_cache
*r2
=
167 (const struct pim_nexthop_cache
*)arg2
;
169 return prefix_same(&r1
->rpf
.rpf_addr
, &r2
->rpf
.rpf_addr
);
172 /* Cleanup pim->rpf_hash each node data */
173 static void pim_rp_list_hash_clean(void *data
)
175 struct pim_nexthop_cache
*pnc
;
177 pnc
= (struct pim_nexthop_cache
*)data
;
178 if (pnc
->rp_list
->count
)
179 list_delete_all_node(pnc
->rp_list
);
180 if (pnc
->upstream_list
->count
)
181 list_delete_all_node(pnc
->upstream_list
);
184 void pim_prefix_list_update(struct prefix_list
*plist
)
186 pim_rp_prefix_list_update(plist
);
187 pim_ssm_prefix_list_update(plist
);
188 pim_upstream_spt_prefix_list_update(plist
);
191 static void pim_instance_terminate(void)
193 /* Traverse and cleanup rpf_hash */
194 if (pimg
->rpf_hash
) {
195 hash_clean(pimg
->rpf_hash
, (void *)pim_rp_list_hash_clean
);
196 hash_free(pimg
->rpf_hash
);
197 pimg
->rpf_hash
= NULL
;
200 if (pimg
->ssm_info
) {
201 pim_ssm_terminate(pimg
->ssm_info
);
202 pimg
->ssm_info
= NULL
;
205 XFREE(MTYPE_PIM_PIM_INSTANCE
, pimg
);
208 static void pim_free()
210 pim_ssmpingd_destroy();
214 pim_upstream_terminate();
216 if (qpim_static_route_list
)
217 list_free(qpim_static_route_list
);
222 pim_route_map_terminate();
224 zclient_lookup_free();
226 zprivs_terminate(&pimd_privs
);
229 static struct pim_instance
*pim_instance_init(vrf_id_t vrf_id
, afi_t afi
)
231 struct pim_instance
*pim
;
233 pim
= XCALLOC(MTYPE_PIM_PIM_INSTANCE
, sizeof(struct pim_instance
));
237 pim
->vrf_id
= vrf_id
;
240 pim
->spt
.switchover
= PIM_SPT_IMMEDIATE
;
241 pim
->spt
.plist
= NULL
;
243 pim
->rpf_hash
= hash_create_size(256, pim_rpf_hash_key
, pim_rpf_equal
);
246 zlog_debug("%s: NHT rpf hash init ", __PRETTY_FUNCTION__
);
248 pim
->ssm_info
= pim_ssm_init(vrf_id
);
249 if (!pim
->ssm_info
) {
250 pim_instance_terminate();
259 qpim_rp_keep_alive_time
= PIM_RP_KEEPALIVE_PERIOD
;
263 if (!inet_aton(PIM_ALL_PIM_ROUTERS
, &qpim_all_pim_routers_addr
)) {
265 "%s %s: could not solve %s to group address: errno=%d: %s",
266 __FILE__
, __PRETTY_FUNCTION__
, PIM_ALL_PIM_ROUTERS
,
267 errno
, safe_strerror(errno
));
276 qpim_static_route_list
= list_new();
277 if (!qpim_static_route_list
) {
278 zlog_err("%s %s: failure: static_route_list=list_new()",
279 __FILE__
, __PRETTY_FUNCTION__
);
282 qpim_static_route_list
->del
= (void (*)(void *))pim_static_route_free
;
284 pim_mroute_socket_enable();
286 qpim_inaddr_any
.s_addr
= PIM_NET_INADDR_ANY
;
289 RFC 4601: 4.6.3. Assert Metrics
292 infinite_assert_metric() {
293 return {1,infinity,infinity,0}
296 qpim_infinite_assert_metric
.rpt_bit_flag
= 1;
297 qpim_infinite_assert_metric
.metric_preference
=
298 PIM_ASSERT_METRIC_PREFERENCE_MAX
;
299 qpim_infinite_assert_metric
.route_metric
= PIM_ASSERT_ROUTE_METRIC_MAX
;
300 qpim_infinite_assert_metric
.ip_address
= qpim_inaddr_any
;
311 /* reverse prefix_list_init */
312 prefix_list_add_hook(NULL
);
313 prefix_list_delete_hook(NULL
);