| 1 | #!/usr/bin/perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # $Id: Policy.pm,v 1.3 2003/06/02 11:50:12 unimlo Exp $ | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | package Net::BGP::Policy; | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use vars qw( $VERSION @ISA ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | ## Inheritance and Versioning ## | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | @ISA = qw ( ); | |||||||||||||||||||
| 13 | 2 | $VERSION = '0.04'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | ## Module Imports ## | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 2 | ▶ | use Net::BGP::Peer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | ## Public Class Methods ## | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | sub new | ||||||||||||||||||||
| 24 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 25 | 1 | ▶ | my $proto = shift || __PACKAGE__; | ||||||||||||||||||
|
Condition50%1/2
shift() || __PACKAGE__
MC/DC0%0/1
shift() || __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 26 | 1 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 1 | my $this = { | |||||||||||||||||||
| 29 | _in => {}, | ||||||||||||||||||||
| 30 | _out => {}, | ||||||||||||||||||||
| 31 | _peer => {} | ||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 1 | bless($this, $class); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | return $this; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | ## Public Object Methods ## | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub set | ||||||||||||||||||||
| 42 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
set: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||
| 43 | 4 | my ($this,$peer,$dir,$policy) = @_; | |||||||||||||||||||
| 44 | 4 | ▶ | croak "Set policy need 3 arguments!" if (scalar @_ < 3); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 4 | ▶ | $dir = $dir =~ /in/i ? '_in' : '_out'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 4 | ▶ | if (scalar @_ == 3) # Remove peer if no 3rd argument | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | { | ||||||||||||||||||||
| 48 | 0 | delete $this->{$dir}->{$peer}; | |||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | 4 | ▶ | croak "Unknown policy type - Should be a Net::ACL object" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | unless $policy->isa('Net::ACL'); | ||||||||||||||||||||
| 53 | 4 | ▶ | $peer = renew Net::BGP::Peer($peer) unless ref $peer; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 4 | ▶ | croak "Peer unknown - Should be a Net::BGP::Peer object" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | unless $peer->isa('Net::BGP::Peer'); | ||||||||||||||||||||
| 56 | 4 | $this->{$dir}->{$peer} = $policy; | |||||||||||||||||||
| 57 | 4 | weaken($this->{_peer}->{$peer} = $peer); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub delete | ||||||||||||||||||||
| 61 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 62 | 0 | ▶ | croak "delete method needs 2 arguments: The peer and the direction" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | unless scalar @_ == 3; | ||||||||||||||||||||
| 64 | 0 | shift->set(@_); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | sub out | ||||||||||||||||||||
| 68 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
out: called CC=5 SCAR=17.8
Pod
documented
| |||||||||||||||||||||
| 69 | 2 | my ($this,$prefix,$nlri) = @_; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 2 | my %newout; | |||||||||||||||||||
| 72 | 2 | ▶ | unless (defined $nlri) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | { | ||||||||||||||||||||
| 74 | 0 | foreach my $peer (keys %{$this->{_out}}) | |||||||||||||||||||
| 75 | { | ||||||||||||||||||||
| 76 | 0 | $newout{$peer} = undef; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | else | ||||||||||||||||||||
| 80 | { | ||||||||||||||||||||
| 81 | 2 | foreach my $peer (keys %{$this->{_out}}) | |||||||||||||||||||
| 82 | { | ||||||||||||||||||||
| 83 | 4 | my $p = $this->{_out}->{$peer}; | |||||||||||||||||||
| 84 | # query should NOT modify the $nlri object itself! | ||||||||||||||||||||
| 85 | $newout{$peer} = defined $p | ||||||||||||||||||||
| 86 | 4 | ▶ | ? ($p->query($prefix,$nlri,$this->{_peer}->{$peer}))[2] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | : $nlri; | ||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||
| 90 | 2 | return \%newout; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub in | ||||||||||||||||||||
| 94 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
in: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 95 | 2 | my ($this,$prefix,$nlri) = @_; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | my @nlri; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | foreach my $peer (keys %{$nlri}) | |||||||||||||||||||
| 100 | { | ||||||||||||||||||||
| 101 | 4 | my $n = $nlri->{$peer}; | |||||||||||||||||||
| 102 | 4 | ▶ | next unless defined($n); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 4 | my $p = $this->{_in}->{$peer}; | |||||||||||||||||||
| 104 | # query should NOT modify the $n(lri) object itself! | ||||||||||||||||||||
| 105 | 4 | ▶ | $n = ($p->query($prefix,$n,$this->{_peer}->{$peer}))[2] if defined($p); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 4 | ▶ | push(@nlri,$n) if defined $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | }; | ||||||||||||||||||||
| 108 | 2 | return \@nlri; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =pod | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | =head1 NAME | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | Net::BGP::Policy - Class representing a Global BGP Routing Policy | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =head1 SYNOPSIS | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | use Net::BGP::Policy; | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # Constructor | ||||||||||||||||||||
| 122 | $policy = new Net::BGP::Policy(); | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # Accessor Methods | ||||||||||||||||||||
| 125 | $policy->set($peer, 'in', $acl); | ||||||||||||||||||||
| 126 | $policy->delete($peer,'out'); | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | $nlri_array_ref = $policy->in($prefix, { $peer => $nlri, ... } ); | ||||||||||||||||||||
| 129 | $out_hash_ref = $policy->out($prefix, $nlri ); | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 DESCRIPTION | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | This module implement a class representing a global BGP Routing Policy. It | ||||||||||||||||||||
| 134 | does so using L<Net::ACL route-maps|Net::ACL::RouteMapRule>. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =item new() - create a new Net::BGP::Policy object | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | $policy = new Net::BGP::Policy(); | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This is the constructor for Net::BGP::Policy object. It returns a | ||||||||||||||||||||
| 145 | reference to the newly created object. It ignores all arguments. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =back | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head1 ACCESSOR METHODS | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =over 4 | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =item set() | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | This method is used to configure a policy for a peer in a direction. It takes | ||||||||||||||||||||
| 156 | two or three arguments. The first is the peer, the second is the direction | ||||||||||||||||||||
| 157 | (C<in> or C<out>). The third is the policy which should be a Net::ACL | ||||||||||||||||||||
| 158 | route-map (or an object inherited from Net::ACL). The rules of the route-map | ||||||||||||||||||||
| 159 | should be like Net::ACL::RouteMapRule objects. If the third parameter is | ||||||||||||||||||||
| 160 | undefined, no policy will be used for the peer. If the third parameter is not | ||||||||||||||||||||
| 161 | pressent, the peer will not get updates. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =item delete() | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | This method is used to remove a peer from the policy in a direction. It takes | ||||||||||||||||||||
| 166 | two arguments. The first is the peer, the second is the direction (C<in> or | ||||||||||||||||||||
| 167 | C<out>). | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =item in() | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | The in() method executes the policy for incomming updates. The first argument | ||||||||||||||||||||
| 172 | is the prefix, the second should be an hash reference. The hash reference | ||||||||||||||||||||
| 173 | is indexed on peers with values of NLRI objects avaible from that peer. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | The method returns a list of NLRIs. | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | =item out() | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | The out() method executes the policy for outgoing updates. The first argument | ||||||||||||||||||||
| 180 | is the prefix, the second is the NLRI object. | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | The method returns a reference to a hash of NLRIs indexed on peers. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =back | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 SEE ALSO | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | Net::BGP, Net::BGP::RIB, Net::BGP::NLRI, Net::BGP::Router, Net::ACL | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Martin Lorensen <bgp@martin.lorensen.dk> | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | ## End Package Net::BGP::Policy ## | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 2 | 1; | |||||||||||||||||||