back to topotato report
topotato coverage report
Current view: top level - bgpd - bgp_advertise.c (source / functions) Hit Total Coverage
Test: aggregated run ( view descriptions ) Lines: 86 122 70.5 %
Date: 2023-02-24 14:41:08 Functions: 12 17 70.6 %

          Line data    Source code
       1             : /* BGP advertisement and adjacency
       2             :  * Copyright (C) 1996, 97, 98, 99, 2000 Kunihiro Ishiguro
       3             :  *
       4             :  * This file is part of GNU Zebra.
       5             :  *
       6             :  * GNU Zebra 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
       8             :  * Free Software Foundation; either version 2, or (at your option) any
       9             :  * later version.
      10             :  *
      11             :  * GNU Zebra 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 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             : 
      21             : #include <zebra.h>
      22             : 
      23             : #include "command.h"
      24             : #include "memory.h"
      25             : #include "prefix.h"
      26             : #include "hash.h"
      27             : #include "thread.h"
      28             : #include "queue.h"
      29             : #include "filter.h"
      30             : 
      31             : #include "bgpd/bgpd.h"
      32             : #include "bgpd/bgp_table.h"
      33             : #include "bgpd/bgp_route.h"
      34             : #include "bgpd/bgp_advertise.h"
      35             : #include "bgpd/bgp_attr.h"
      36             : #include "bgpd/bgp_debug.h"
      37             : #include "bgpd/bgp_aspath.h"
      38             : #include "bgpd/bgp_packet.h"
      39             : #include "bgpd/bgp_fsm.h"
      40             : #include "bgpd/bgp_mplsvpn.h"
      41             : #include "bgpd/bgp_updgrp.h"
      42             : 
      43             : /* BGP advertise attribute is used for pack same attribute update into
      44             :    one packet.  To do that we maintain attribute hash in struct
      45             :    peer.  */
      46          70 : struct bgp_advertise_attr *bgp_advertise_attr_new(void)
      47             : {
      48           0 :         return XCALLOC(MTYPE_BGP_ADVERTISE_ATTR,
      49             :                        sizeof(struct bgp_advertise_attr));
      50             : }
      51             : 
      52          70 : void bgp_advertise_attr_free(struct bgp_advertise_attr *baa)
      53             : {
      54           0 :         XFREE(MTYPE_BGP_ADVERTISE_ATTR, baa);
      55          70 : }
      56             : 
      57          70 : static void *bgp_advertise_attr_hash_alloc(void *p)
      58             : {
      59          70 :         struct bgp_advertise_attr *ref = (struct bgp_advertise_attr *)p;
      60          70 :         struct bgp_advertise_attr *baa;
      61             : 
      62          70 :         baa = bgp_advertise_attr_new();
      63          70 :         baa->attr = ref->attr;
      64          70 :         return baa;
      65             : }
      66             : 
      67         158 : unsigned int bgp_advertise_attr_hash_key(const void *p)
      68             : {
      69         158 :         const struct bgp_advertise_attr *baa = p;
      70             : 
      71         158 :         return attrhash_key_make(baa->attr);
      72             : }
      73             : 
      74          88 : bool bgp_advertise_attr_hash_cmp(const void *p1, const void *p2)
      75             : {
      76          88 :         const struct bgp_advertise_attr *baa1 = p1;
      77          88 :         const struct bgp_advertise_attr *baa2 = p2;
      78             : 
      79          88 :         return attrhash_cmp(baa1->attr, baa2->attr);
      80             : }
      81             : 
      82             : /* BGP update and withdraw information is stored in BGP advertise
      83             :    structure.  This structure is referred from BGP adjacency
      84             :    information.  */
      85          88 : struct bgp_advertise *bgp_advertise_new(void)
      86             : {
      87          88 :         return XCALLOC(MTYPE_BGP_ADVERTISE, sizeof(struct bgp_advertise));
      88             : }
      89             : 
      90          88 : void bgp_advertise_free(struct bgp_advertise *adv)
      91             : {
      92          88 :         if (adv->pathi)
      93             :                 /* bgp_advertise bgp_path_info reference */
      94          88 :                 bgp_path_info_unlock(adv->pathi);
      95          88 :         XFREE(MTYPE_BGP_ADVERTISE, adv);
      96          88 : }
      97             : 
      98          88 : void bgp_advertise_add(struct bgp_advertise_attr *baa,
      99             :                        struct bgp_advertise *adv)
     100             : {
     101          88 :         adv->next = baa->adv;
     102          88 :         if (baa->adv)
     103          18 :                 baa->adv->prev = adv;
     104          88 :         baa->adv = adv;
     105          88 : }
     106             : 
     107          88 : void bgp_advertise_delete(struct bgp_advertise_attr *baa,
     108             :                           struct bgp_advertise *adv)
     109             : {
     110          88 :         if (adv->next)
     111           5 :                 adv->next->prev = adv->prev;
     112          88 :         if (adv->prev)
     113          13 :                 adv->prev->next = adv->next;
     114             :         else
     115          75 :                 baa->adv = adv->next;
     116          88 : }
     117             : 
     118          88 : struct bgp_advertise_attr *bgp_advertise_attr_intern(struct hash *hash,
     119             :                                                      struct attr *attr)
     120             : {
     121          88 :         struct bgp_advertise_attr ref;
     122          88 :         struct bgp_advertise_attr *baa;
     123             : 
     124          88 :         ref.attr = bgp_attr_intern(attr);
     125          88 :         baa = (struct bgp_advertise_attr *)hash_get(
     126             :                 hash, &ref, bgp_advertise_attr_hash_alloc);
     127          88 :         baa->refcnt++;
     128             : 
     129          88 :         return baa;
     130             : }
     131             : 
     132          88 : void bgp_advertise_attr_unintern(struct hash *hash,
     133             :                                  struct bgp_advertise_attr *baa)
     134             : {
     135          88 :         if (baa->refcnt)
     136          88 :                 baa->refcnt--;
     137             : 
     138          88 :         if (baa->refcnt && baa->attr)
     139          18 :                 bgp_attr_unintern(&baa->attr);
     140             :         else {
     141          70 :                 if (baa->attr) {
     142          70 :                         hash_release(hash, baa);
     143          70 :                         bgp_attr_unintern(&baa->attr);
     144             :                 }
     145          70 :                 bgp_advertise_attr_free(baa);
     146             :         }
     147          88 : }
     148             : 
     149          10 : bool bgp_adj_out_lookup(struct peer *peer, struct bgp_dest *dest,
     150             :                         uint32_t addpath_tx_id)
     151             : {
     152          10 :         struct bgp_adj_out *adj;
     153          10 :         struct peer_af *paf;
     154          10 :         afi_t afi;
     155          10 :         safi_t safi;
     156          10 :         bool addpath_capable;
     157             : 
     158          20 :         RB_FOREACH (adj, bgp_adj_out_rb, &dest->adj_out)
     159           6 :                 SUBGRP_FOREACH_PEER (adj->subgroup, paf)
     160           6 :                         if (paf->peer == peer) {
     161           6 :                                 afi = SUBGRP_AFI(adj->subgroup);
     162           6 :                                 safi = SUBGRP_SAFI(adj->subgroup);
     163           6 :                                 addpath_capable =
     164           6 :                                         bgp_addpath_encode_tx(peer, afi, safi);
     165             : 
     166             :                                 /* Match on a specific addpath_tx_id if we are
     167             :                                  * using addpath for
     168             :                                  * this
     169             :                                  * peer and if an addpath_tx_id was specified */
     170           6 :                                 if (addpath_capable && addpath_tx_id
     171           0 :                                     && adj->addpath_tx_id != addpath_tx_id)
     172           0 :                                         continue;
     173             : 
     174           6 :                                 return (adj->adv
     175           0 :                                                 ? (adj->adv->baa ? true : false)
     176           6 :                                                 : (adj->attr ? true : false));
     177             :                         }
     178             : 
     179             :         return false;
     180             : }
     181             : 
     182             : 
     183           0 : void bgp_adj_in_set(struct bgp_dest *dest, struct peer *peer, struct attr *attr,
     184             :                     uint32_t addpath_id)
     185             : {
     186           0 :         struct bgp_adj_in *adj;
     187             : 
     188           0 :         for (adj = dest->adj_in; adj; adj = adj->next) {
     189           0 :                 if (adj->peer == peer && adj->addpath_rx_id == addpath_id) {
     190           0 :                         if (adj->attr != attr) {
     191           0 :                                 bgp_attr_unintern(&adj->attr);
     192           0 :                                 adj->attr = bgp_attr_intern(attr);
     193             :                         }
     194           0 :                         return;
     195             :                 }
     196             :         }
     197           0 :         adj = XCALLOC(MTYPE_BGP_ADJ_IN, sizeof(struct bgp_adj_in));
     198           0 :         adj->peer = peer_lock(peer); /* adj_in peer reference */
     199           0 :         adj->attr = bgp_attr_intern(attr);
     200           0 :         adj->uptime = monotime(NULL);
     201           0 :         adj->addpath_rx_id = addpath_id;
     202           0 :         BGP_ADJ_IN_ADD(dest, adj);
     203           0 :         bgp_dest_lock_node(dest);
     204             : }
     205             : 
     206           0 : void bgp_adj_in_remove(struct bgp_dest *dest, struct bgp_adj_in *bai)
     207             : {
     208           0 :         bgp_attr_unintern(&bai->attr);
     209           0 :         BGP_ADJ_IN_DEL(dest, bai);
     210           0 :         bgp_dest_unlock_node(dest);
     211           0 :         peer_unlock(bai->peer); /* adj_in peer reference */
     212           0 :         XFREE(MTYPE_BGP_ADJ_IN, bai);
     213           0 : }
     214             : 
     215           0 : bool bgp_adj_in_unset(struct bgp_dest *dest, struct peer *peer,
     216             :                       uint32_t addpath_id)
     217             : {
     218           0 :         struct bgp_adj_in *adj;
     219           0 :         struct bgp_adj_in *adj_next;
     220             : 
     221           0 :         adj = dest->adj_in;
     222             : 
     223           0 :         if (!adj)
     224             :                 return false;
     225             : 
     226           0 :         while (adj) {
     227           0 :                 adj_next = adj->next;
     228             : 
     229           0 :                 if (adj->peer == peer && adj->addpath_rx_id == addpath_id)
     230           0 :                         bgp_adj_in_remove(dest, adj);
     231             : 
     232             :                 adj = adj_next;
     233             :         }
     234             : 
     235             :         return true;
     236             : }
     237             : 
     238          90 : void bgp_sync_init(struct peer *peer)
     239             : {
     240          90 :         afi_t afi;
     241          90 :         safi_t safi;
     242          90 :         struct bgp_synchronize *sync;
     243             : 
     244        2250 :         FOREACH_AFI_SAFI (afi, safi) {
     245        1890 :                 sync = XCALLOC(MTYPE_BGP_SYNCHRONISE,
     246             :                                sizeof(struct bgp_synchronize));
     247        1890 :                 bgp_adv_fifo_init(&sync->update);
     248        1890 :                 bgp_adv_fifo_init(&sync->withdraw);
     249        1890 :                 bgp_adv_fifo_init(&sync->withdraw_low);
     250        1890 :                 peer->sync[afi][safi] = sync;
     251             :         }
     252          90 : }
     253             : 
     254          68 : void bgp_sync_delete(struct peer *peer)
     255             : {
     256          68 :         afi_t afi;
     257          68 :         safi_t safi;
     258             : 
     259        1700 :         FOREACH_AFI_SAFI (afi, safi) {
     260        1428 :                 XFREE(MTYPE_BGP_SYNCHRONISE, peer->sync[afi][safi]);
     261             :         }
     262          68 : }

Generated by: LCOV version v1.16-topotato