RIBEntry.pm

filter: Statementstmt 75.7% 78 / 103 Branchbran 47.5% 19 / 40 Conditioncond 45.8% 11 / 24 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 82.3% 14 / 17 Podpod 100% 13 / 13 totaltotal 64.4% 136 / 211 CC 35 SCAR 22.6
CC
35
Cov
65%
handle_changes
30.0
update_out
14.5
add_peer
12.0
CRAP 9.6
« Router.pm ↑ Summary ContextRouter.pm »
1 #!/usr/bin/perl
2  
3 # $Id: RIBEntry.pm,v 1.5 2003/06/02 11:50:12 unimlo Exp $
4  
5 package Net::BGP::RIBEntry;
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 Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ## Public Class Methods ##
21  
22 sub new
23 2 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
24 2 my $class = shift();
25 2 my ($arg, $value);
26  
27 2 my $this = {
28 _prefix => undef,
29 _in => {},
30 _local => undef,
31 _out => {},
32 _peers => {},
33 _force => 0
34 };
35  
36 2 bless($this, $class);
37  
38 2 ▶ while ( defined($arg = shift()) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($arg = shift())) 1 2 3
39 {
40 1 $value = shift();
41  
42 1 ▶ if ( $arg =~ /prefix/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /prefix/i) { } 1 0 1
43 {
44 1 $this->{_prefix} = $value;
45 }
46 else
47 {
48 0 confess("unrecognized argument $arg\n");
49 }
50 }
51 2 return $this;
52 }
53  
54 sub clone
55 2 ▶{
Subroutine clone: called CC=6 SCAR=18.5
Pod documented
56 2 my $proto = shift;
57 2 ▶ my $class = ref $proto || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
58 2 ▶ $proto = shift unless ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $proto 1 1 2
59  
60 2 my $clone = {};
61  
62 2 $clone->{_prefix} = $proto->{_prefix};
63 2 foreach my $key (qw( _in _out ))
64 {
65 4 @{$clone->{$key}}{keys %{$proto->{$key}}} =
66 0 ▶ map { defined $_ ? $_->clone : undef; }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
67 4 values(%{$proto->{$key}});
68 };
69 2 ▶ $clone->{_local} = defined $proto->{_local} ? $proto->{_local}->clone : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $proto->{'_local'} ? : 0 2 2
70  
71 2 $clone->{_peers} = $proto->{_peers}; # No reason to copy!
72  
73 2 return ( bless($clone, $class) );
74 }
75  
76 ## Public Object Methods ##
77  
78 sub add_peer
79 0 ▶{
Subroutine add_peer: not called CC=3 SCAR=24.8
Pod documented
80 0 my ($this,$peer,$dir) = @_;
81 0 ▶ $dir = $dir =~ /in/i ? '_in' : '_out';
Branch: 1 branch
BranchTrueFalseTotal
$dir =~ /in/i ? : 0 0 0
82 0 $this->{$dir}->{$peer} = undef;
83 0 ▶ $this->{_force} = 1 if $dir eq '_out';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "_out" 0 0 0
84 }
85  
86 sub remove_peer
87 0 ▶{
Subroutine remove_peer: not called CC=2 SCAR=17.9
Pod documented
88 0 my ($this,$peer,$dir) = @_;
89 0 ▶ $dir = $dir =~ /in/i ? '_in' : '_out';
Branch: 1 branch
BranchTrueFalseTotal
$dir =~ /in/i ? : 0 0 0
90 0 delete $this->{$dir}->{$peer};
91 }
92  
93 sub prefix
94 3 ▶{
Subroutine prefix: called CC=2 SCAR=6.9
Pod documented
95 3 my $this = shift;
96  
97 3 ▶ $this->{_prefix} = @_ ? shift : $this->{_prefix};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 2 3
98  
99 3 return ( $this->{_prefix} );
100 }
101  
102 sub local
103 1 ▶{
Subroutine local: called CC=1 SCAR=0.0
Pod documented
104 1 return shift->{_local};
105 };
106  
107 sub in
108 2 ▶{
Subroutine in: called CC=1 SCAR=0.0
Pod documented
109 2 return shift->{_in};
110 };
111  
112 sub out
113 3 ▶{
Subroutine out: called CC=1 SCAR=0.0
Pod documented
114 3 return shift->{_out};
115 };
116  
117 sub update_in
118 2 ▶{
Subroutine update_in: called CC=1 SCAR=0.0
Pod documented
119 2 my ($this,$peer,$nlri) = @_;
120 2 $this->{_in}->{$peer} = $nlri;
121 2 return $this;
122 }
123  
124 sub update_local
125 2 ▶{
Subroutine update_local: called CC=6 SCAR=20.5
Pod documented
126 2 my ($this,$policy) = @_;
127  
128 2 ▶ croak "argument should be a Net::BGP::Policy"
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $policy or builtin::blessed($policy) and $policy->isa("Net::BGP::Policy") 0 2 2
Condition33%2/6
not defined $policy or builtin::blessed($policy) and $policy->isa("Net::BGP::Policy")
l!l&&r!l&&!r
020
builtin::blessed($policy) and $policy->isa("Net::BGP::Policy")
!ll&&!rl&&r
002
MC/DC0%0/3
not defined $policy or builtin::blessed($policy) and $policy->isa("Net::BGP::Policy")
not defined $policy builtin::blessed($policy) $policy->isa("Net::BGP::Policy")
Truth table25%1/4
not(A) or B and C
A= not defined $policyB= builtin::blessed($policy)C= $policy->isa("Net::BGP::Policy")
ABCresult
1XX1
0111
00X0
0100
129 unless (! defined $policy) || ((blessed $policy) && $policy->isa('Net::BGP::Policy'));
130  
131 my @nlris = defined $policy
132 2 ? @{$policy->in($this->{_prefix},$this->{_in})}
133 0 ▶ : grep { $_; } values %{$this->{_in}}; # grep removes undef!
Branch: 1 branch
BranchTrueFalseTotal
defined $policy ? : 2 0 2
134  
135  
136 2 my $old = $this->{_local};
137  
138 2 ($this->{_local}) = sort { $a <=> $b } @nlris;
139  
140 2 ▶ return ($old eq $this->{_local}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$old eq $this->{'_local'} ? : 1 1 2
141 }
142  
143 sub update_out
144 2 ▶{
Subroutine update_out: called CC=11 SCAR=26.7
Pod documented
145 2 my ($this,$policy) = @_;
146  
147 2 my $newout_hr;
148 2 ▶ if (defined $policy)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $policy) { } 2 0 2
149 {
150 2 ▶ croak "argument should be a Net::BGP::Policy"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($policy) and $policy->isa("Net::BGP::Policy") 0 2 2
Condition33%1/3
builtin::blessed($policy) and $policy->isa("Net::BGP::Policy")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($policy) and $policy->isa("Net::BGP::Policy")
builtin::blessed($policy) $policy->isa("Net::BGP::Policy")
Truth table33%1/3
A and B
A= builtin::blessed($policy)B= $policy->isa("Net::BGP::Policy")
ABresult
0X0
100
111
151 unless blessed $policy && $policy->isa('Net::BGP::Policy');
152 2 $newout_hr = $policy->out($this->{_prefix},$this->{_local});
153 }
154 else
155 {
156 0 foreach my $peer (keys %{$this->{_out}})
157 {
158 0 $newout_hr->{$peer} = $this->{_local};
159 };
160 };
161  
162 2 my $changes_hr;
163  
164 # Peers not returned are not changed! Thats a feature - not a bug!
165  
166 # Look for changes!
167 2 foreach my $peer (keys %{$newout_hr})
168 {
169 # Was not and is not!
170 next if ((! defined $this->{_out}->{$peer})
171 4 ▶ && (! defined $newout_hr->{$peer}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"_out"}{$peer} or defined $newout_hr->{$peer} 0 4 4
Condition66%2/3
defined $this->{"_out"}{$peer} or defined $newout_hr->{$peer}
l!l&&r!l&&!r
220
MC/DC0%0/2
defined $this->{"_out"}{$peer} or defined $newout_hr->{$peer}
defined $this->{"_out"}{$peer} defined $newout_hr->{$peer}
Truth table66%2/3
A or B
A= defined $this->{"_out"}{$peer}B= defined $newout_hr->{$peer}
ABresult
1X1
011
000
172  
173 # Are both there - and they are the same!
174 next if ((defined $this->{_out}->{$peer})
175 && (defined $newout_hr->{$peer})
176 4 ▶ && ($this->{_out}->{$peer} eq $newout_hr->{$peer}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->{"_out"}{$peer} and defined $newout_hr->{$peer} and $this->{"_out"}{$peer} eq $newout_hr->{$peer} 2 2 4
Condition66%4/6
defined $this->{"_out"}{$peer} and defined $newout_hr->{$peer} and $this->{"_out"}{$peer} eq $newout_hr->{$peer}
!ll&&!rl&&r
202
defined $this->{"_out"}{$peer} and defined $newout_hr->{$peer}
!ll&&!rl&&r
202
MC/DC33%1/3
defined $this->{"_out"}{$peer} and defined $newout_hr->{$peer} and $this->{"_out"}{$peer} eq $newout_hr->{$peer}
defined $this->{"_out"}{$peer} defined $newout_hr->{$peer} $this->{"_out"}{$peer} eq $newout_hr->{$peer}
Truth table50%2/4
A and B and C
A= defined $this->{"_out"}{$peer}B= defined $newout_hr->{$peer}C= $this->{"_out"}{$peer} eq $newout_hr->{$peer}
ABCresult
0XX0
10X0
1100
1111
177  
178 # We got a change!
179 2 $changes_hr->{$peer} = $newout_hr->{$peer};
180 2 $this->{_out}->{$peer} = $newout_hr->{$peer};
181 };
182  
183 2 return $changes_hr;
184 }
185  
186 sub handle_changes
187 0 ▶{
Subroutine handle_changes: not called CC=5 SCAR=34.0
Pod documented
188 0 my ($this,$policy) = @_;
189 0 ▶ return -1 unless $this->update_local($policy) || $this->{_force};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->update_local($policy) or $this->{"_force"} 0 0 0
Condition0%0/3
$this->update_local($policy) or $this->{"_force"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->update_local($policy) or $this->{"_force"}
$this->update_local($policy) $this->{"_force"}
Truth table0%0/3
A or B
A= $this->update_local($policy)B= $this->{"_force"}
ABresult
1X1
011
000
190 0 $this->{_force} = 0;
191 0 my $changes_hr = $this->update_out($policy);
192 0 my $changes = 0;
193 0 foreach my $to_peer (keys %{$changes_hr})
194 {
195 my $update = defined $changes_hr->{$to_peer}
196 ? new Net::BGP::Update($changes_hr->{$to_peer},[$this->{_prefix}],undef)
197 0 ▶ : new Net::BGP::Update(Withdraw => [$this->{_prefix}]);
Branch: 1 branch
BranchTrueFalseTotal
defined $changes_hr->{$to_peer} ? : 0 0 0
198 0 renew Net::BGP::Peer($to_peer)->update($update);
199 0 $changes += 1;
200 };
201 0 return $changes;
202 }
203  
204 sub asstring
205 1 ▶{
Subroutine asstring: called CC=5 SCAR=16.2
Pod documented
206 1 my $this = shift;
207 1 my $res = $this->{_prefix} . ":\tLocal:\t" . $this->{_local}->asstring . "\n";
208 1 foreach my $peer (keys %{$this->{_in}})
209 {
210 2 my $n = $this->{_in}->{$peer};
211 2 ▶ $res .= "\tIN:\t" . renew Net::BGP::Peer($peer)->asstring . ': ' .
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 2 0 2
212 (defined $n ? $n->asstring : 'n/a') . "\n";
213 };
214 1 foreach my $peer (keys %{$this->{_out}})
215 {
216 2 my $n = $this->{_out}->{$peer};
217 2 ▶ $res .= "\tOUT:\t" . renew Net::BGP::Peer($peer)->asstring . ': ' .
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 2 0 2
218 (defined $n ? $n->asstring : 'n/a') . "\n";
219 };
220 1 return $res;
221 }
222  
223 =pod
224  
225 =head1 NAME
226  
227 Net::BGP::RIBEntry - Class representing an entry in a BGP RIB
228  
229 =head1 SYNOPSIS
230  
231 use Net::BGP::RIBEntry;
232  
233 # Constructor
234 $entry = new Net::BGP::RIBEntry(
235 Prefix => '10.0.0.1'
236 );
237  
238 # Object Copy
239 $clone = $entry->clone();
240  
241 # Accessor Methods
242 $entry->add_peer($peer,$dir);
243 $entry->remove_peer($peer,$dir);
244  
245 $entry = $entry->update_in($peer,$nlri);
246 $has_changed = $entry->update_local($policy);
247 $changes_hashref = $entry->update_out($policy);
248  
249 $has_changed = $entry->handle_changes($policy)
250  
251 $prefix = $entry->prefix($prefix);
252  
253 $nlri = $entry->local;
254 $nlri_hashref = $entry->in;
255 $nlri_hashref = $entry->out;
256  
257 $string = $entry->asstring;
258  
259  
260 =head1 DESCRIPTION
261  
262 This module implement a class representing an entry in a BGP Routing
263 Information Base.
264 It stores the prefix that the entry represents as well as 3 categories of
265 network layer reachability information (NLRI). Each NLRI is represented as
266 an L<Net::BGP::NLRI|Net::BGP::NLRI> object:
267  
268 =over 4
269  
270 =item IN - An NLRI object for each peer that has sent an UPDATE regarding this prefix.
271  
272 =item Local - The preferred of the policy processed available NLRIs from the I<IN> RIB.
273  
274 =item OUT - An NLRI object for each outgoing peer representing the processed I<Local> NLRI.
275  
276 =back
277  
278 =head1 CONSTRUCTOR
279  
280 =over 4
281  
282 =item new() - create a new Net::BGP::RIBEntry object
283  
284 $entry = new Net::BGP::RIBEntry(
285 Prefix => '10.0.0.1'
286 );
287  
288 This is the constructor for Net::BGP::RIBEntry object. It returns a
289 reference to the newly created object. The following named parameter may
290 be passed to the constructor:
291  
292 =over 4
293  
294 =item Prefix
295  
296 This parameter corresponds to the prefix the RIB Entry represents.
297  
298 =back
299  
300 =back
301  
302 =head1 OBJECT COPY
303  
304 =over 4
305  
306 =item clone() - clone a Net::BGP::RIBEntry object
307  
308 $clone = $nlri->clone();
309  
310 This method creates an exact copy of the Net::BGP::RIBEntry object.
311  
312 =back
313  
314 =head1 ACCESSOR METHODS
315  
316 =over 4
317  
318 =item add_peer()
319  
320 =item remove_peer()
321  
322 Both add_peer() and remove_peer() takes two arguments: The peer object and
323 the direction of the peer (C<in> or C<out>).
324  
325 =item update_in()
326  
327 This method updates the RIB IN part of the object. The first argument is the
328 peer object that received the BGP UPDATE message and the second argument is
329 an NLRI object corresponding the received UPDATE message.
330  
331 The method returns the RIBEntry object.
332  
333 =item update_local()
334  
335 This method applies the incoming policy and executes the route selection
336 process of BGP. If no arguments (or a undefined value), no policy will be
337 used. Otherwise the argument should be a Net::BGP::Policy object - or
338 something inherited from that.
339  
340 After applying the given policy, the selection process updates the Local RIB
341 with the best available NLRI.
342  
343 The return value is true if the Local RIB is changed, otherwise false.
344  
345 =item update_out()
346  
347 This method applies the outgoing policy to the Local RIB and updates the
348 OUT RIB accordingly. If no arguments (or a undefined value), no policy will be
349 used. Otherwise the argument should be a Net::BGP::Policy object - or
350 something inherited from that.
351  
352 =item handle_changes()
353  
354 This method combines the update_local() and update_out() methods and generates
355 UPDATE messages for each change and sends them to the peers. It takes a
356 optional policy as first argument which are used in the calls to
357 update_local() and update_out().
358  
359 It returns -1 if no changes has happend. Otherwise it returns the number of
360 changes send.
361  
362 =item prefix()
363  
364 This mothod returns the prefix the RIB Entry represent. It an argument is
365 given, the prefix will be replaced with that value.
366  
367 =item local()
368  
369 This method returns the currently selected NLRI or undefined if no NLRIs are
370 available.
371  
372 =item in()
373  
374 =item out()
375  
376 Both the in() and out() method returns a reference to a hash indexed on peers
377 containing Net::BGP::NLRI objects coresponding the the incoming or outgoing
378 UPDATE message data.
379  
380 =item asstring()
381  
382 This method returns a print-friendly string describing the RIB entry.
383  
384 =back
385  
386 =head1 SEE ALSO
387  
388 Net::BGP, Net::BGP::RIB, Net::BGP::NLRI, Net::BGP::Update, Net::BGP::Policy
389  
390 =head1 AUTHOR
391  
392 Martin Lorensen <bgp@martin.lorensen.dk>
393  
394 =cut
395  
396 ## End Package Net::BGP::RIBEntry ##
397  
398 2 1;
« Router.pm ↑ Summary ContextRouter.pm »