back to topotato report
topotato coverage report
Current view: top level - staticd - static_nht.c (source / functions) Hit Total Coverage
Test: test_pim_cbsr.py::PIMCandidateBSRTest Lines: 90 105 85.7 %
Date: 2023-02-16 02:09:14 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Static NHT code.
       3             :  * Copyright (C) 2018 Cumulus Networks, Inc.
       4             :  *               Donald Sharp
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify it
       7             :  * under the terms of the GNU General Public License as published by the Free
       8             :  * Software Foundation; either version 2 of the License, or (at your option)
       9             :  * any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful, but WITHOUT
      12             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13             :  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      14             :  * more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License along
      17             :  * with this program; see the file COPYING; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : #include <zebra.h>
      21             : 
      22             : #include "prefix.h"
      23             : #include "table.h"
      24             : #include "vrf.h"
      25             : #include "nexthop.h"
      26             : #include "srcdest_table.h"
      27             : 
      28             : #include "static_vrf.h"
      29             : #include "static_routes.h"
      30             : #include "static_zebra.h"
      31             : #include "static_nht.h"
      32             : 
      33          16 : static void static_nht_update_path(struct static_path *pn, struct prefix *nhp,
      34             :                                    uint32_t nh_num, vrf_id_t nh_vrf_id,
      35             :                                    struct vrf *vrf)
      36             : {
      37          16 :         struct static_nexthop *nh;
      38             : 
      39          64 :         frr_each(static_nexthop_list, &pn->nexthop_list, nh) {
      40          16 :                 if (nh->nh_vrf_id != nh_vrf_id)
      41           0 :                         continue;
      42             : 
      43          16 :                 if (nh->type != STATIC_IPV4_GATEWAY
      44             :                     && nh->type != STATIC_IPV4_GATEWAY_IFNAME
      45             :                     && nh->type != STATIC_IPV6_GATEWAY
      46          16 :                     && nh->type != STATIC_IPV6_GATEWAY_IFNAME)
      47           0 :                         continue;
      48             : 
      49          16 :                 if (nhp->family == AF_INET
      50          16 :                     && nhp->u.prefix4.s_addr == nh->addr.ipv4.s_addr)
      51           8 :                         nh->nh_valid = !!nh_num;
      52             : 
      53          16 :                 if (nhp->family == AF_INET6
      54           0 :                     && memcmp(&nhp->u.prefix6, &nh->addr.ipv6, IPV6_MAX_BYTELEN)
      55             :                                == 0)
      56           0 :                         nh->nh_valid = !!nh_num;
      57             : 
      58          16 :                 if (nh->state == STATIC_START)
      59          11 :                         static_zebra_route_add(pn, true);
      60             :         }
      61          16 : }
      62             : 
      63           8 : static void static_nht_update_safi(struct prefix *sp, struct prefix *nhp,
      64             :                                    uint32_t nh_num, afi_t afi, safi_t safi,
      65             :                                    struct vrf *vrf, vrf_id_t nh_vrf_id)
      66             : {
      67           8 :         struct route_table *stable;
      68           8 :         struct static_vrf *svrf;
      69           8 :         struct route_node *rn;
      70           8 :         struct static_path *pn;
      71           8 :         struct static_route_info *si;
      72             : 
      73           8 :         svrf = vrf->info;
      74           8 :         if (!svrf)
      75             :                 return;
      76             : 
      77           8 :         stable = static_vrf_static_table(afi, safi, svrf);
      78           8 :         if (!stable)
      79             :                 return;
      80             : 
      81           8 :         if (sp) {
      82           0 :                 rn = srcdest_rnode_lookup(stable, sp, NULL);
      83           0 :                 if (rn && rn->info) {
      84           0 :                         si = static_route_info_from_rnode(rn);
      85           0 :                         frr_each(static_path_list, &si->path_list, pn) {
      86           0 :                                 static_nht_update_path(pn, nhp, nh_num,
      87             :                                                        nh_vrf_id, vrf);
      88             :                         }
      89           0 :                         route_unlock_node(rn);
      90             :                 }
      91           0 :                 return;
      92             :         }
      93             : 
      94          32 :         for (rn = route_top(stable); rn; rn = route_next(rn)) {
      95          24 :                 si = static_route_info_from_rnode(rn);
      96          24 :                 if (!si)
      97           8 :                         continue;
      98          64 :                 frr_each(static_path_list, &si->path_list, pn) {
      99          16 :                         static_nht_update_path(pn, nhp, nh_num, nh_vrf_id, vrf);
     100             :                 }
     101             :         }
     102             : }
     103             : 
     104           8 : void static_nht_update(struct prefix *sp, struct prefix *nhp, uint32_t nh_num,
     105             :                        afi_t afi, safi_t safi, vrf_id_t nh_vrf_id)
     106             : {
     107             : 
     108           8 :         struct vrf *vrf;
     109             : 
     110          24 :         RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
     111           8 :                 static_nht_update_safi(sp, nhp, nh_num, afi, safi, vrf,
     112             :                                        nh_vrf_id);
     113           8 : }
     114             : 
     115           8 : static void static_nht_reset_start_safi(struct prefix *nhp, afi_t afi,
     116             :                                         safi_t safi, struct vrf *vrf,
     117             :                                         vrf_id_t nh_vrf_id)
     118             : {
     119           8 :         struct static_vrf *svrf;
     120           8 :         struct route_table *stable;
     121           8 :         struct static_nexthop *nh;
     122           8 :         struct static_path *pn;
     123           8 :         struct route_node *rn;
     124           8 :         struct static_route_info *si;
     125             : 
     126           8 :         svrf = vrf->info;
     127           8 :         if (!svrf)
     128             :                 return;
     129             : 
     130           8 :         stable = static_vrf_static_table(afi, safi, svrf);
     131           8 :         if (!stable)
     132             :                 return;
     133             : 
     134          32 :         for (rn = route_top(stable); rn; rn = route_next(rn)) {
     135          24 :                 si = static_route_info_from_rnode(rn);
     136          24 :                 if (!si)
     137           8 :                         continue;
     138          64 :                 frr_each(static_path_list, &si->path_list, pn) {
     139          64 :                         frr_each(static_nexthop_list, &pn->nexthop_list, nh) {
     140          16 :                                 if (nh->nh_vrf_id != nh_vrf_id)
     141           0 :                                         continue;
     142             : 
     143          16 :                                 if (nhp->family == AF_INET
     144          24 :                                     && nhp->u.prefix4.s_addr
     145          16 :                                                != nh->addr.ipv4.s_addr)
     146           8 :                                         continue;
     147             : 
     148           8 :                                 if (nhp->family == AF_INET6
     149           0 :                                     && memcmp(&nhp->u.prefix6, &nh->addr.ipv6,
     150             :                                               16)
     151             :                                                != 0)
     152           0 :                                         continue;
     153             : 
     154             :                                 /*
     155             :                                  * We've been told that a nexthop we
     156             :                                  * depend on has changed in some manner,
     157             :                                  * so reset the state machine to allow
     158             :                                  * us to start over.
     159             :                                  */
     160           8 :                                 nh->state = STATIC_START;
     161             :                         }
     162             :                 }
     163             :         }
     164             : }
     165             : 
     166           8 : void static_nht_reset_start(struct prefix *nhp, afi_t afi, safi_t safi,
     167             :                             vrf_id_t nh_vrf_id)
     168             : {
     169           8 :         struct vrf *vrf;
     170             : 
     171          24 :         RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
     172           8 :                 static_nht_reset_start_safi(nhp, afi, safi, vrf, nh_vrf_id);
     173           8 : }
     174             : 
     175           6 : static void static_nht_mark_state_safi(struct prefix *sp, afi_t afi,
     176             :                                        safi_t safi, struct vrf *vrf,
     177             :                                        enum static_install_states state)
     178             : {
     179           6 :         struct static_vrf *svrf;
     180           6 :         struct route_table *stable;
     181           6 :         struct route_node *rn;
     182           6 :         struct static_nexthop *nh;
     183           6 :         struct static_path *pn;
     184           6 :         struct static_route_info *si;
     185             : 
     186           6 :         svrf = vrf->info;
     187           6 :         if (!svrf)
     188             :                 return;
     189             : 
     190           6 :         stable = static_vrf_static_table(afi, safi, svrf);
     191           6 :         if (!stable)
     192             :                 return;
     193             : 
     194           6 :         rn = srcdest_rnode_lookup(stable, sp, NULL);
     195           6 :         if (!rn)
     196             :                 return;
     197           6 :         si = rn->info;
     198           6 :         if (si) {
     199          24 :                 frr_each(static_path_list, &si->path_list, pn) {
     200          18 :                         frr_each(static_nexthop_list, &pn->nexthop_list, nh) {
     201           6 :                                 nh->state = state;
     202             :                         }
     203             :                 }
     204             :         }
     205             : 
     206           6 :         route_unlock_node(rn);
     207             : }
     208             : 
     209           6 : void static_nht_mark_state(struct prefix *sp, safi_t safi, vrf_id_t vrf_id,
     210             :                            enum static_install_states state)
     211             : {
     212           6 :         struct vrf *vrf;
     213             : 
     214           6 :         afi_t afi = AFI_IP;
     215             : 
     216           6 :         if (sp->family == AF_INET6)
     217           0 :                 afi = AFI_IP6;
     218             : 
     219           6 :         vrf = vrf_lookup_by_id(vrf_id);
     220           6 :         if (!vrf || !vrf->info)
     221             :                 return;
     222             : 
     223           6 :         static_nht_mark_state_safi(sp, afi, safi, vrf, state);
     224             : }

Generated by: LCOV version v1.16-topotato