xorp

mrib_table.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/mrt/mrib_table.hh,v 1.17 2008/10/02 21:57:45 bms Exp $
00022 
00023 #ifndef __MRT_MRIB_TABLE_HH__
00024 #define __MRT_MRIB_TABLE_HH__
00025 
00026 
00027 //
00028 // Multicast Routing Information Base Table header file.
00029 //
00030 
00031 
00032 
00033 
00034 
00035 #include "libxorp/ipvx.hh"
00036 #include "libxorp/ipvxnet.hh"
00037 
00038 
00039 //
00040 // Constants definitions
00041 //
00042 enum {
00043     MRIB_DONT_CREATE = false,
00044     MRIB_DO_CREATE = true
00045 };
00046 
00047 //
00048 // Structures/classes, typedefs and macros
00049 //
00050 
00051 class MribTable;
00052 class MribTableIterator;
00053 class MribLookup;
00054 class Mrib;
00055 
00056 
00060 class Mrib {
00061 public:
00067     Mrib(int family);
00068 
00074     Mrib(const IPvXNet& dest_prefix);
00075 
00081     Mrib(const Mrib& mrib);
00082     
00090     bool operator==(const Mrib& other) const;
00091     
00097     const IPvXNet& dest_prefix() const { return (_dest_prefix); }
00098 
00104     void    set_dest_prefix(const IPvXNet& v) { _dest_prefix = v; }
00105     
00111     const IPvX& next_hop_router_addr() const { return (_next_hop_router_addr); }
00112     
00118     void    set_next_hop_router_addr(const IPvX& v) { _next_hop_router_addr = v; }
00119     
00125     uint32_t    next_hop_vif_index() const { return (_next_hop_vif_index); }
00126     
00132     void    set_next_hop_vif_index(uint32_t v) { _next_hop_vif_index = v; }
00133     
00139     uint32_t    metric_preference() const { return (_metric_preference); }
00140     
00146     void    set_metric_preference(uint32_t v) { _metric_preference = v; }
00147 
00153     uint32_t    metric() const { return (_metric); }
00154     
00160     void    set_metric(uint32_t v) { _metric = v; }
00161     
00168     string str() const;
00169     
00170 private:
00171     IPvXNet _dest_prefix;       // The destination prefix address
00172     IPvX    _next_hop_router_addr;  // The address of the next-hop router
00173     uint32_t    _next_hop_vif_index;    // The vif index to the next-hop router
00174     uint32_t    _metric_preference; // The metric preference to the
00175                     // destination
00176     uint32_t    _metric;        // The metric to the destination
00177 };
00178 
00182 class MribTableIterator {
00183 public:
00189     MribTableIterator(MribLookup *mrib_lookup) : _mrib_lookup(mrib_lookup) {}
00190     
00194     MribTableIterator() {}
00195     
00204     MribTableIterator& operator++();
00205 
00214     MribTableIterator operator++(int);
00215     
00223     bool operator==(const MribTableIterator& other) const {
00224     return (_mrib_lookup == other._mrib_lookup);
00225     }
00226     
00234     bool operator!=(const MribTableIterator& other) const {
00235     return (_mrib_lookup != other._mrib_lookup);
00236     }
00237     
00244     Mrib* operator*() const;
00245     
00246 private:
00247     MribLookup *_mrib_lookup;   // The MribLookup entry for this iterator
00248 };
00249 
00253 class MribTable {
00254 public:
00260     MribTable(int family);
00261     
00265     ~MribTable();
00266     
00267     typedef MribTableIterator   iterator;
00268 
00275     int     family() const { return (_family); }
00276 
00280     void    clear();
00281 
00285     void    remove_all_entries();
00286 
00292     list<Mrib *>& removed_mrib_entries() {
00293     return (_removed_mrib_entries);
00294     }
00295 
00302     bool is_preserving_removed_mrib_entries() const {
00303     return (_is_preserving_removed_mrib_entries);
00304     }
00305 
00312     void set_is_preserving_removed_mrib_entries(bool v) {
00313     _is_preserving_removed_mrib_entries = v;
00314     }
00315 
00325     Mrib    *insert(const Mrib& mrib);
00326     
00332     void    remove(const IPvXNet& dest_prefix);
00333     
00339     void    remove(const Mrib& mrib);
00340     
00348     Mrib    *find(const IPvX& address) const;
00349     
00357     Mrib    *find_exact(const IPvXNet& dest_prefix) const;
00358     
00364     iterator    begin() const { return (_mrib_lookup_root); }
00365     
00371     iterator    end() const { return (NULL); }
00372 
00380     void update_entry_vif_index(const IPvXNet& dest_prefix,
00381                 uint32_t vif_index);
00382 
00383     //
00384     // Pending transactions related methods
00385     //
00397     void    add_pending_insert(uint32_t tid, const Mrib& mrib);
00398 
00410     void    add_pending_remove(uint32_t tid, const Mrib& mrib);
00411 
00422     void    add_pending_remove_all_entries(uint32_t tid);
00423 
00435     void    commit_pending_transactions(uint32_t tid);
00436     
00443     void    abort_pending_transactions(uint32_t tid);
00444     
00448     void    abort_all_pending_transactions() {
00449     _mrib_pending_transactions.clear();
00450     }
00451     
00457     size_t  size()   const  { return (_mrib_size);  }
00458     
00459 private:
00468     void remove_mrib_entry(Mrib *mrib);
00469 
00477     MribLookup  *find_prefix_mrib_lookup(const IPvXNet& addr_prefix) const;
00478     
00488     void    remove_mrib_lookup(MribLookup *mrib_lookup);
00489     
00490     //
00491     // Private class used to keep track of pending transactions
00492     //
00493     class PendingTransaction {
00494     public:
00503     PendingTransaction(uint32_t tid, const Mrib& mrib, bool is_insert)
00504         : _tid(tid),
00505           _mrib(mrib),
00506           _is_insert(is_insert),
00507           _is_remove_all(false)
00508     {}
00509 
00510 #ifdef XORP_USE_USTL
00511     PendingTransaction(): _mrib(AF_INET) { }
00512 #endif
00513 
00517     PendingTransaction(const MribTable& mrib_table, uint32_t tid)
00518         : _tid(tid),
00519           _mrib(Mrib(IPvXNet(IPvX::ZERO(mrib_table.family()), 0))),
00520           _is_insert(false),
00521           _is_remove_all(true)
00522     {}
00523     uint32_t    tid() const { return (_tid); }
00524     const Mrib& mrib() const { return (_mrib); }
00525     bool        is_insert() const { return (_is_insert); }
00526     bool        is_remove_all() const { return (_is_remove_all); }
00527     void        update_entry_vif_index(uint32_t vif_index) {
00528         _mrib.set_next_hop_vif_index(vif_index);
00529     }
00530     
00531     private:
00532     uint32_t    _tid;       // The transaction ID
00533     Mrib        _mrib;      // The MRIB to add or remove
00534     bool        _is_insert; // If true, insert, otherwise remove
00535     bool        _is_remove_all; // If true, remove all entries
00536     };
00537     
00538     //
00539     // The private state
00540     //
00541     int     _family;        // The address family of this table
00542     MribLookup  *_mrib_lookup_root; // The root of the MRIB lookup tree
00543     size_t  _mrib_lookup_size;  // The number of MribLookup entries
00544     size_t  _mrib_size;     // The number of Mrib entries
00545     
00546     //
00547     // The list of pending transactions
00548     //
00549     list<PendingTransaction> _mrib_pending_transactions;
00550 
00551     //
00552     // A flag to indicate whether the removed Mrib entries are preserved
00553     // on the _removed_mrib_entries list, or are deleted.
00554     //
00555     bool        _is_preserving_removed_mrib_entries;
00556 
00557     //
00558     // The list of removed Mrib entries that may be still in use.
00559     //
00560     list<Mrib *>    _removed_mrib_entries;
00561 };
00562 
00566 class MribLookup {
00567 public:
00573     MribLookup(MribLookup *parent)
00574     : _parent(parent),
00575       _left_child(NULL),
00576       _right_child(NULL),
00577       _mrib(NULL)
00578     {}
00579     
00583     ~MribLookup() { if (_mrib != NULL) delete _mrib; }
00584     
00590     Mrib *mrib() const { return (_mrib); }
00591 
00600     void set_mrib(Mrib *v) { _mrib = v; }
00601 
00607     MribLookup *parent() { return (_parent); }
00608 
00616     void set_parent(MribLookup *v) { _parent = v; }
00617 
00625     MribLookup *left_child() { return (_left_child); }
00626 
00632     void set_left_child(MribLookup *v) { _left_child = v; }
00633 
00639     MribLookup *right_child() { return (_right_child); }
00640 
00648     void set_right_child(MribLookup *v) { _right_child = v; }
00649 
00661     MribLookup *get_next() const;
00662     
00663 private:
00664     MribLookup  *_parent;   // The parent in the lookup tree
00665     MribLookup  *_left_child;   // The left child in the lookup tree
00666     MribLookup  *_right_child;  // The right child in the lookup tree
00667     Mrib    *_mrib;     // A pointer to the MRIB info
00668 };
00669 
00670 //
00671 // Global variables
00672 //
00673 
00674 //
00675 // Global functions prototypes
00676 //
00677 
00678 #endif // __MRT_MRIB_TABLE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations