back to topotato report
topotato coverage report
Current view: top level - pimd - pim_instance.c (source / functions) Hit Total Coverage
Test: test_pim_cbsr.py::PIMCandidateBSRTest Lines: 97 122 79.5 %
Date: 2023-02-16 02:09:14 Functions: 9 10 90.0 %

          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           3 : static void pim_instance_terminate(struct pim_instance *pim)
      42             : {
      43           3 :         pim_vxlan_exit(pim);
      44             : 
      45           3 :         if (pim->ssm_info) {
      46           3 :                 pim_ssm_terminate(pim->ssm_info);
      47           3 :                 pim->ssm_info = NULL;
      48             :         }
      49             : 
      50           3 :         if (pim->static_routes)
      51           3 :                 list_delete(&pim->static_routes);
      52             : 
      53           3 :         pim_instance_mlag_terminate(pim);
      54             : 
      55           3 :         pim_upstream_terminate(pim);
      56             : 
      57           3 :         pim_rp_free(pim);
      58             : 
      59           3 :         pim_bsm_proc_free(pim);
      60             : 
      61             :         /* Traverse and cleanup rpf_hash */
      62           3 :         if (pim->rpf_hash) {
      63           3 :                 hash_clean(pim->rpf_hash, (void *)pim_rp_list_hash_clean);
      64           3 :                 hash_free(pim->rpf_hash);
      65           3 :                 pim->rpf_hash = NULL;
      66             :         }
      67             : 
      68           3 :         pim_if_terminate(pim);
      69             : 
      70           3 :         pim_oil_terminate(pim);
      71             : 
      72           3 :         pim_msdp_exit(pim);
      73             : 
      74           3 :         close(pim->reg_sock);
      75             : 
      76           3 :         pim_mroute_socket_disable(pim);
      77             : 
      78           3 :         XFREE(MTYPE_PIM_PLIST_NAME, pim->spt.plist);
      79           3 :         XFREE(MTYPE_PIM_PLIST_NAME, pim->register_plist);
      80             : 
      81           3 :         pim->vrf = NULL;
      82           3 :         XFREE(MTYPE_PIM_PIM_INSTANCE, pim);
      83           3 : }
      84             : 
      85           3 : static struct pim_instance *pim_instance_init(struct vrf *vrf)
      86             : {
      87           3 :         struct pim_instance *pim;
      88           3 :         char hash_name[64];
      89             : 
      90           3 :         pim = XCALLOC(MTYPE_PIM_PIM_INSTANCE, sizeof(struct pim_instance));
      91             : 
      92           3 :         pim_if_init(pim);
      93             : 
      94           3 :         pim->mcast_if_count = 0;
      95           3 :         pim->keep_alive_time = PIM_KEEPALIVE_PERIOD;
      96           3 :         pim->rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;
      97             : 
      98           3 :         pim->ecmp_enable = false;
      99           3 :         pim->ecmp_rebalance_enable = false;
     100             : 
     101           3 :         pim->vrf = vrf;
     102             : 
     103           3 :         pim->spt.switchover = PIM_SPT_IMMEDIATE;
     104           3 :         pim->spt.plist = NULL;
     105             : 
     106           3 :         pim_msdp_init(pim, router->master);
     107           3 :         pim_vxlan_init(pim);
     108             : 
     109           3 :         snprintf(hash_name, sizeof(hash_name), "PIM %s RPF Hash", vrf->name);
     110           3 :         pim->rpf_hash = hash_create_size(256, pim_rpf_hash_key, pim_rpf_equal,
     111             :                                          hash_name);
     112             : 
     113           3 :         if (PIM_DEBUG_ZEBRA)
     114           0 :                 zlog_debug("%s: NHT rpf hash init ", __func__);
     115             : 
     116           3 :         pim->ssm_info = pim_ssm_init();
     117             : 
     118           3 :         pim->static_routes = list_new();
     119           3 :         pim->static_routes->del = (void (*)(void *))pim_static_route_free;
     120             : 
     121           3 :         pim->send_v6_secondary = 1;
     122             : 
     123           3 :         pim->gm_socket = -1;
     124             : 
     125           3 :         pim_rp_init(pim);
     126             : 
     127           3 :         pim_bsm_proc_init(pim);
     128             : 
     129           3 :         pim_oil_init(pim);
     130             : 
     131           3 :         pim_upstream_init(pim);
     132             : 
     133           3 :         pim_instance_mlag_init(pim);
     134             : 
     135           3 :         pim->last_route_change_time = -1;
     136             : 
     137           3 :         pim->reg_sock = pim_reg_sock();
     138           3 :         if (pim->reg_sock < 0)
     139           0 :                 assert(0);
     140             : 
     141             :         /* MSDP global timer defaults. */
     142           3 :         pim->msdp.hold_time = PIM_MSDP_PEER_HOLD_TIME;
     143           3 :         pim->msdp.keep_alive = PIM_MSDP_PEER_KA_TIME;
     144           3 :         pim->msdp.connection_retry = PIM_MSDP_PEER_CONNECT_RETRY_TIME;
     145             : 
     146           3 :         return pim;
     147             : }
     148             : 
     149          38 : struct pim_instance *pim_get_pim_instance(vrf_id_t vrf_id)
     150             : {
     151          38 :         struct vrf *vrf = vrf_lookup_by_id(vrf_id);
     152             : 
     153          38 :         if (vrf)
     154          38 :                 return vrf->info;
     155             : 
     156             :         return NULL;
     157             : }
     158             : 
     159           3 : static int pim_vrf_new(struct vrf *vrf)
     160             : {
     161           3 :         struct pim_instance *pim = pim_instance_init(vrf);
     162             : 
     163           3 :         zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
     164             : 
     165           3 :         vrf->info = (void *)pim;
     166             : 
     167           3 :         pim_ssmpingd_init(pim);
     168           3 :         return 0;
     169             : }
     170             : 
     171           3 : static int pim_vrf_delete(struct vrf *vrf)
     172             : {
     173           3 :         struct pim_instance *pim = vrf->info;
     174             : 
     175           3 :         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           3 : static int pim_vrf_enable(struct vrf *vrf)
     193             : {
     194           3 :         struct pim_instance *pim = (struct pim_instance *)vrf->info;
     195           3 :         struct interface *ifp;
     196             : 
     197           3 :         zlog_debug("%s: for %s %u", __func__, vrf->name, vrf->vrf_id);
     198             : 
     199           6 :         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           3 :         pim_mroute_socket_enable(pim);
     208             : 
     209           3 :         return 0;
     210             : }
     211             : 
     212           3 : static int pim_vrf_disable(struct vrf *vrf)
     213             : {
     214             :         /* Note: This is a callback, the VRF will be deleted by the caller. */
     215           3 :         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           3 : void pim_vrf_init(void)
     242             : {
     243           3 :         vrf_init(pim_vrf_new, pim_vrf_enable, pim_vrf_disable, pim_vrf_delete);
     244             : 
     245           3 :         vrf_cmd_init(pim_vrf_config_write);
     246           3 : }
     247             : 
     248           3 : void pim_vrf_terminate(void)
     249             : {
     250           3 :         struct vrf *vrf;
     251             : 
     252           9 :         RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
     253           3 :                 struct pim_instance *pim;
     254             : 
     255           3 :                 pim = vrf->info;
     256           3 :                 if (!pim)
     257           0 :                         continue;
     258             : 
     259           3 :                 pim_ssmpingd_destroy(pim);
     260           3 :                 pim_instance_terminate(pim);
     261             : 
     262           3 :                 vrf->info = NULL;
     263             :         }
     264             : 
     265           3 :         vrf_terminate();
     266           3 : }

Generated by: LCOV version v1.16-topotato