Skip to content

Commit

Permalink
[NF] Cache IRRDB ASNs/Prefixes to speed up router configuration gener…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
barryo committed Sep 14, 2021
1 parent 1c359f6 commit b852c1f
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 6 deletions.
9 changes: 8 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ IXP_FE_FRONTEND_DISABLED_LOGO=false
# MAIL_PORT=25
# MAIL_ENCRYPTION="tls"

MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=Inbox-Name
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null


#######################################################################################
### Graphing - see https://docs.ixpmanager.org/grapher/introduction
Expand Down Expand Up @@ -256,7 +263,7 @@ DOCTRINE_CACHE_NAMESPACE=IXPMANAGERNAMESPACE
# Utility paths

# See: https://docs.ixpmanager.org/features/irrdb/
IXP_IRRDB_BGPQ3_PATH=/usr/bin/bgpq3
IXP_IRRDB_BGPQ3_PATH=/usr/local/bin/bgpq3

# See: https://docs.ixpmanager.org/features/rpki/
# IXP_RPKI_RTR1_HOST=192.0.2.11
Expand Down
1 change: 1 addition & 0 deletions .env.travisci
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ GRAPHER_BACKEND_SFLOW_ENABLED=true

#GRAPHER_ACCESS_IXP=1

CACHE_DRIVER=null

IXP_API_JSONEXPORTSCHEMA_PUBLIC=true

Expand Down
54 changes: 54 additions & 0 deletions app/Models/Aggregators/IrrdbAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use IXP\Models\Customer;
use IXP\Models\IPv4Address;
Expand Down Expand Up @@ -72,4 +73,57 @@ public static function forCustomerAndProtocol( int $custid, int $protocol, strin

return $results->keyBy( 'id' )->pluck( $field, 'id' )->toArray();
}

/**
* Utility function to get the prefixes/ASN a customer has for a given protocol
* for the purpose of generating router configuration
*
* Returns an array of prefixes.
*
* @param int $custid The customer entity
* @param int $protocol The IP protocol (4/6)
*
* @return array The prefixes found
*/
public static function prefixesForRouterConfiguration( int $custid, int $protocol ): array
{
// Pull these out of the cache if possible, otherwise the database.
$c = Customer::find($custid);

return Cache::store('file')->rememberForever( 'irrdb:prefix:ipv' . $protocol . ':' . $c->asMacro( $protocol ), function() use ($custid,$protocol) {
return IrrdbPrefix::select('prefix')
->where( 'customer_id', $custid )
->where('protocol', $protocol )
->orderBy( 'id', 'ASC' )
->pluck('prefix')
->toArray();
});
}


/**
* Utility function to get the prefixes/ASN a customer has for a given protocol
* for the purpose of generating router configuration
*
* Returns an array of prefixes.
*
* @param int $custid The customer entity
* @param int $protocol The IP protocol (4/6)
*
* @return array The prefixes found
*/
public static function asnsForRouterConfiguration( int $custid, int $protocol ): array
{
// Pull these out of the cache if possible, otherwise the database.
$c = Customer::find($custid);

return Cache::store('file')->rememberForever( 'irrdb:asn:ipv' . $protocol . ':' . $c->asMacro( $protocol ), function() use ($custid,$protocol) {
return IrrdbAsn::select('asn')
->where( 'customer_id', $custid )
->where('protocol', $protocol )
->orderBy( 'id', 'ASC' )
->pluck('asn')
->toArray();
});
}
}
4 changes: 2 additions & 2 deletions app/Models/Aggregators/VlanInterfaceAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ public static function sanitiseVlanInterfaces( Vlan $vlan, int $protocol = 4, in
$int['allpeeringips'] = self::getAllIPsForASN( $vlan, $int['autsys'], $protocol );

if( $int['irrdbfilter'] ) {
$int['irrdbfilter_prefixes'] = IrrdbAggregator::forCustomerAndProtocol( $int[ 'cid' ], $protocol, 'prefix', true );
$int['irrdbfilter_asns' ] = IrrdbAggregator::forCustomerAndProtocol( $int[ 'cid' ], $protocol, 'asn', true );
$int['irrdbfilter_prefixes'] = IrrdbAggregator::prefixesForRouterConfiguration( $int[ 'cid' ], $protocol );
$int['irrdbfilter_asns' ] = IrrdbAggregator::asnsForRouterConfiguration( $int[ 'cid' ], $protocol );
}

$newints[ $int['address'] ] = $int;
Expand Down
5 changes: 4 additions & 1 deletion app/Tasks/Irrdb/UpdateAsnDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

use D2EM;
use Illuminate\Support\Facades\Cache;
use IXP\Models\IrrdbAsn;
use Log;

Expand Down Expand Up @@ -54,7 +55,6 @@ public function update(): array

$this->startTimer();
$asns = $this->bgpq3()->getAsnList( $this->customer()->asMacro( $protocol, 'as' ), $protocol );

$this->result[ 'netTime' ] += $this->timeElapsed();

$this->result[ 'v' . $protocol ][ 'count' ] = count( $asns );
Expand All @@ -66,6 +66,9 @@ public function update(): array
// This customer is not appropriate for IRRDB filtering.
// Delete any pre-existing entries just in case this has changed recently:
$this->startTimer();

Cache::store('file')->forget( 'irrdb:asns:ipv' . $protocol . ':' . $this->customer()->asMacro( $protocol ) );

IrrdbAsn::whereCustomerId( $this->customer()->id )
->whereProtocol( $protocol )->delete();

Expand Down
20 changes: 20 additions & 0 deletions app/Tasks/Irrdb/UpdateDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
IrrdbPrefix
};

use Illuminate\Support\Facades\Cache;
use IXP\Utils\Bgpq3;

/**
Expand Down Expand Up @@ -301,6 +302,25 @@ protected function updateDb( array $fromIrrdb, int $protocol, $type = 'prefix' )
->where( 'protocol', $protocol )
->update( [ 'last_seen' => $now ] );

// Store the prefixes to cache to speed up route server configuration generation.
// At the end of the day, this is what we pull out of the database.
Cache::store('file')->forget( "irrdb:{$type}:ipv{$protocol}:" . $this->customer()->asMacro( $protocol ) );
Cache::store('file')->rememberForever( "irrdb:{$type}:ipv{$protocol}:" . $this->customer()->asMacro( $protocol ), function() use ($model,$type,$protocol) {

$orderBy = 'asn';
if( $type == 'prefix' ) {
$orderBy = 'INET' . ( $protocol === 6 ? '6' : '' ) . '_ATON( prefix )';
}

return $model::select($type)
->where( 'customer_id', $this->customer()->id )
->where('protocol', $protocol )
->orderBy( $orderBy, 'ASC' )
->orderBy( 'id', 'ASC' )
->pluck($type)
->toArray();
});

DB::commit();

$this->result['dbTime'] += $this->timeElapsed();
Expand Down
7 changes: 6 additions & 1 deletion app/Tasks/Irrdb/UpdatePrefixDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/

use Illuminate\Support\Facades\Cache;
use Log;

use IXP\Models\IrrdbPrefix;
Expand Down Expand Up @@ -69,12 +71,15 @@ public function update(): array
// This customer is not appropriate for IRRDB filtering.
// Delete any pre-existing entries just in case this has changed recently:
$this->startTimer();

Cache::store('file')->forget( 'irrdb:prefix:ipv' . $protocol . ':' . $this->customer()->asMacro( $protocol ) );

IrrdbPrefix::whereCustomerId( $this->customer()->id )
->whereProtocol( $protocol )->delete();

$this->result[ 'dbTime' ] += $this->timeElapsed();
$this->result[ 'v' . $protocol ][ 'dbUpdated' ] = true;
$this->result[ 'msg' ] = "Customer not a RS client or IRRDB filtered for IPv{$protocol}. IPv{$protocol} prefixes, if any, wiped from database.";
$this->result[ 'msg' ] = "{$this->customer()->name} not a RS client or IRRDB filtered for IPv{$protocol}. IPv{$protocol} prefixes, if any, wiped from database.";
}
}

Expand Down
5 changes: 4 additions & 1 deletion app/Tasks/Router/ConfigurationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public function render(): ViewContract
]
);

Log::info( 'Generated router configuration for ' . $this->router()->handle . ' and used ' . memory_get_peak_usage() . ' bytes (' . memory_get_peak_usage( true ) . ' real) of memory.' );
Log::info( sprintf( "Generated router configuration for %s and used %0.1f MB ( %0.1f MB real) of memory in %0.3f seconds.",
$this->router()->handle, memory_get_peak_usage()/1024/1024, memory_get_peak_usage( true )/1024/1024,
microtime(true) - LARAVEL_START )
);

return $v;
}
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ abstract class TestCase extends BaseTestCase
public function __construct( $name = null, array $data = [], $dataName = '' )
{
date_default_timezone_set('Europe/Dublin');
define( 'LARAVEL_START', microtime(true ) );
parent::__construct( $name, $data, $dataName );
}

Expand Down

0 comments on commit b852c1f

Please sign in to comment.