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

Generated by: LCOV version v1.16-topotato