Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: ndn_fib.h |
| 3 | * Author: cawka |
| 4 | * |
| 5 | * Created on December 15, 2010, 1:54 PM |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_FIB_H |
| 9 | #define NDN_FIB_H |
| 10 | |
| 11 | #include "hash_helper.h" |
| 12 | #include <clock.h> |
| 13 | #include <main.h> |
| 14 | |
| 15 | class Ndn; |
| 16 | |
| 17 | const int NDN_FIB_GREEN = 1; |
| 18 | const int NDN_FIB_YELLOW = 2; |
| 19 | const int NDN_FIB_RED = 3; |
| 20 | |
| 21 | //structure for Fib outgoing interface |
| 22 | struct FibNexthop |
| 23 | { |
| 24 | int interfaceIndex; //interface index of the node |
| 25 | NodeAddress nextHop; //next-hop |
| 26 | int cost; //routing protocol cost to route an interest via this interface |
| 27 | int packetCount; //record the number of packets forwarded using this interface |
| 28 | |
| 29 | clocktype srtt; //smoothed round-trip time |
| 30 | clocktype rttvar; //round-trip time variation |
| 31 | |
| 32 | int status; // Status of the next hop: |
| 33 | // - #NDN_FIB_GREEN |
| 34 | // - #NDN_FIB_YELLOW |
| 35 | // - #NDN_FIB_RED |
| 36 | |
| 37 | bool operator==( int interface ) const { return interfaceIndex==interface; } |
| 38 | FibNexthop( ) {} |
| 39 | FibNexthop( int _interface, int _nextHop, int _cost ) |
| 40 | : interfaceIndex(_interface), nextHop(_nextHop), cost(_cost) |
| 41 | , packetCount(1), srtt(0), rttvar(0), status(NDN_FIB_YELLOW) { } |
| 42 | }; |
| 43 | |
| 44 | typedef list<FibNexthop>::iterator FibNexthopIterator; |
| 45 | typedef list<FibNexthop>::const_iterator FibNexthopConstIterator; |
| 46 | |
| 47 | //structure for FIB table entry |
| 48 | struct FibEntry |
| 49 | { |
| 50 | list<FibNexthop> forwardingList; |
| 51 | clocktype sto; //retransmission time out |
| 52 | |
| 53 | bool needsProbing; //set to true when probing timer goes out |
| 54 | |
| 55 | FibEntry( ) : sto(0), needsProbing(false) { } |
| 56 | |
| 57 | // Find nexthop record |
| 58 | inline FibNexthopIterator findNexthop( int interfaceIndex ); |
| 59 | bool isValid( const FibNexthopIterator &nh ) |
| 60 | { return nh!=forwardingList.end(); } |
| 61 | |
| 62 | // Compute and update RTO value for Fib Entry (RFC 2988) |
| 63 | // (for now we pick the maximum RTO of all forwardings) |
| 64 | void updateSto( ); |
| 65 | |
| 66 | // Update status of FIB next hop |
| 67 | inline void updateStatus( int interface, int status ); |
| 68 | }; |
| 69 | |
| 70 | typedef string_key_hash_t<FibEntry>::point_iterator FibIterator; |
| 71 | typedef string_key_hash_t<FibEntry>::iterator FibRangeIterator; |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | // Class implementing FIB functionality |
| 77 | class NdnFib |
| 78 | { |
| 79 | public: |
| 80 | NdnFib( Ndn &node ); |
| 81 | virtual ~NdnFib( ); |
| 82 | |
| 83 | // Invalidate entries in FIB |
| 84 | // Will leave FIB records in hash, but assign metric=NETWORK_UNREACHABLE |
| 85 | void invalidate( ); |
| 86 | |
| 87 | //Find corresponding FIB entry for the given content name |
| 88 | //Longest match is performed |
| 89 | FibIterator lookup( const string &name ); |
| 90 | bool isValid( const FibIterator &it ) { return it!=_fib.end(); } |
| 91 | |
| 92 | /** |
| 93 | * Update FIB entry |
| 94 | * If the entry exists, metric will be updated. Otherwise, new entry will be created |
| 95 | * |
| 96 | * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE |
| 97 | * |
| 98 | * @param name Prefix |
| 99 | * @param interfaceIndex Forwarding interface |
| 100 | * @param metric Routing metric |
| 101 | * @param nextHop Nexthop node address (IPv4) |
| 102 | * @return true if a new entry created, false otherwise |
| 103 | */ |
| 104 | bool update( const string &name, int interfaceIndex, int metric, NodeAddress nextHop ); |
| 105 | bool update( NodeAddress nodeId, int interfaceIndex, int metric, NodeAddress nextHop ); |
| 106 | bool update( NodeAddress nodeId, int metric, NodeAddress nextHop ); |
| 107 | |
| 108 | // Update Fib from OSPF routing table (through a hack in OSPF algorithm) |
| 109 | void updateFibFromOSPFv2( int interface ); |
| 110 | |
| 111 | // Update Fib from BGP routing table (using info from RibIn) |
| 112 | void updateFibFromBGP( ); |
| 113 | |
| 114 | // Update Fib from IP routing table |
| 115 | void updateFibFromIpRouting( ); |
| 116 | |
| 117 | // Update the status for all FIB records for the specified interface |
| 118 | void updateInterfaceStatus( int interface, int status ); |
| 119 | |
| 120 | void dump( ); |
| 121 | void dump( const FibIterator &fib ); |
| 122 | |
| 123 | void resetProbing(); //reset needsProbing field for every FibEntry |
| 124 | private: |
| 125 | |
| 126 | private: |
| 127 | Ndn &_node; |
| 128 | |
| 129 | string_key_hash_t<FibEntry> _fib; |
| 130 | }; |
| 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | class NdnFibNexthopSorter |
| 136 | { |
| 137 | public: |
| 138 | bool operator()( const FibNexthop &first, const FibNexthop &second ); |
| 139 | }; |
| 140 | |
| 141 | FibNexthopIterator FibEntry::findNexthop( int interfaceIndex ) |
| 142 | { |
| 143 | return find( forwardingList.begin( ), |
| 144 | forwardingList.end( ), |
| 145 | interfaceIndex ); |
| 146 | } |
| 147 | |
| 148 | //inline FibNexthopIterator FibEntry::findBestNexthop( int bestNum, int excludeInterface ) |
| 149 | //{ |
| 150 | // // First adjust number of available interfaces (to make sure we have correct ranking function) |
| 151 | // int num_valid_interfaces = forwardingList.size(); |
| 152 | // FibNexthopIterator nh; |
| 153 | // for( nh=forwardingList.begin(); nh!=forwardingList.end(); nh++ ) |
| 154 | // { |
| 155 | // if( nh->interfaceIndex==excludeInterface ) num_valid_interfaces--; |
| 156 | // } |
| 157 | // |
| 158 | // if( num_valid_interfaces==0 ) return forwardingList.end(); |
| 159 | // |
| 160 | // bestNum = bestNum % num_valid_interfaces; |
| 161 | // int i=0; |
| 162 | // for( nh=forwardingList.begin(); nh!=forwardingList.end(); nh++ ) // skip bestNum % size() FIB records |
| 163 | // { |
| 164 | // if( nh->interfaceIndex==excludeInterface ) continue; |
| 165 | // if( i==bestNum ) break; |
| 166 | // |
| 167 | // i++; |
| 168 | // } |
| 169 | // |
| 170 | // if( nh!=forwardingList.end() ) |
| 171 | // { |
| 172 | // assert( nh->interfaceIndex!=excludeInterface ); |
| 173 | //// printf( "%d best => i%d\n", bestNum, nh->interfaceIndex ); |
| 174 | // } |
| 175 | //// else |
| 176 | //// { |
| 177 | //// printf( "No other hops available\n" ); |
| 178 | //// } |
| 179 | // |
| 180 | // |
| 181 | // return nh; |
| 182 | //} |
| 183 | |
| 184 | void FibEntry::updateStatus( int interface, int status ) |
| 185 | { |
| 186 | FibNexthopIterator nh = findNexthop( interface ); |
| 187 | if( isValid(nh) ) |
| 188 | { |
| 189 | nh->status = status; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #endif /* NDN_FIB_H */ |