ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/FastPing.pm
(Generate patch)

Comparing AnyEvent-FastPing/FastPing.pm (file contents):
Revision 1.12 by root, Tue Feb 1 04:06:24 2011 UTC vs.
Revision 1.13 by root, Wed Feb 2 19:26:45 2011 UTC

45our $ICMP4_FH; our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; 45our $ICMP4_FH; our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4;
46our $ICMP6_FH; our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; 46our $ICMP6_FH; our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6;
47 47
48=item AnyEvent::FastPing::ipv4_supported 48=item AnyEvent::FastPing::ipv4_supported
49 49
50Returns true if IPv4 is supported in this module and on this system. 50Returns true iff IPv4 is supported in this module and on this system.
51 51
52=item AnyEvent::FastPing::ipv6_supported 52=item AnyEvent::FastPing::ipv6_supported
53 53
54Returns true if IPv6 is supported in this module and on this system. 54Returns true iff IPv6 is supported in this module and on this system.
55 55
56=item AnyEvent::FastPing::icmp4_pktsize 56=item AnyEvent::FastPing::icmp4_pktsize
57 57
58Returns the number of bytes each IPv4 ping packet has. 58Returns the number of octets per IPv4 ping packet (the whole IP packet
59including headers, excluding lower-level headers or trailers such as
60Ethernet).
61
62Can be used to calculate e.g. octets/s from rate ...
63
64 my $octets_per_second = $packets_per_second * AnyEvent::FastPing::icmp4_pktsize;
65
66... or convert kilobit/second to packet rate ...
67
68 my $packets_per_second = $kilobit_per_second
69 * (1000 / 8 / AnyEvent::FastPing::icmp4_pktsize);
70
71etc.
59 72
60=item AnyEvent::FastPing::icmp6_pktsize 73=item AnyEvent::FastPing::icmp6_pktsize
61 74
62Returns the number of bytes each IPv4 ping packet has. 75Like AnyEvent::FastPing::icmp4_pktsize, but for IPv6.
76
77=back
78
79=head1 THE AnyEvent::FastPing CLASS
80
81The AnyEvent::FastPing class represents a single "pinger". A "pinger"
82comes with its own thread to send packets in the background, a rate-limit
83machinery and separate idle/receive callbacks.
84
85The recommended workflow (there are others) is this: 1. create a new
86AnyEvent::FastPing object 2. configure the address lists and ranges to
87ping, also configure an idle callback and optionally a receive callback
883. C<start> the pinger.
89
90When the pinger has finished pinging all the configured addresses it will
91call the idle callback.
92
93The pinging process works like this: every range has a minimum interval
94between sends, which is used to limit the rate at which hosts in that
95range are being pinged. Distinct ranges are independent of each other,
96which is why there is a per-pinger "global" minimum interval as well.
97
98The pinger sends pings as fats as possible, while both obeying the pinger
99rate limit as well as range limits.
100
101When a range is exhausted, it is removed. When all ranges are exhausted,
102the pinger waits another C<max_rtt> seconds and then exits, causing the
103idle callback to trigger.
104
105Performance: On my 2 GHz Opteron system with a pretty average nvidia
106gigabit network card I can ping around 60k to 200k adresses per second,
107depending on routing decisions.
108
109Example: ping 10.0.0.1-10.0.0.15 with at most 100 packets/s, and
11011.0.0.1-11.0.255.255 with at most 1000 packets/s. Also ping the IPv6
111loopback address 5 times as fast as possible. Do not, however, exceed 1000
112packets/s overall. Dump each received reply:
113
114 use AnyEvent::Socket;
115 use AnyEvent::FastPing;
116
117 my $done = AnyEvent->condvar;
118
119 my $pinger = new AnyEvent::FastPing;
120
121 $pinger->interval (1/1000);
122 $pinger->max_rtt (0.1); # reasonably fast/reliable network
123
124 $pinger->add_range (v10.0.0.1, v10.0.0.15, 1/100);
125 $pinger->add_range (v11.0.0.1, v11.0.255.255, 1/1000);
126 $pinger->add_hosts ([ (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1) x 5 ]);
127
128 $pinger->on_recv (sub {
129 for (@{ $_[0] }) {
130 printf "%s %g\n", (AnyEvent::Socket::format_address $_->[0]), $_->[1];
131 }
132 });
133
134 $pinger->on_idle (sub {
135 print "done\n";
136 undef $pinger;
137 });
138
139 $pinger->start;
140 $done->wait;
141
142=head2 METHODS
143
144=over 4
145
146=item $pinger = new AnyEvent::FastPing
147
148Creates a new pinger - right now there can be at most C<65536> pingers in
149a process, although that limit might change to somethind drastically lower
150- you should be stringy with your pinger objects.
63 151
64=cut 152=cut
65 153
66sub new { 154sub new {
67 my ($klass) = @_; 155 my ($klass) = @_;
73 161
74sub DESTROY { 162sub DESTROY {
75 undef $IDLE_CB[ &id ]; 163 undef $IDLE_CB[ &id ];
76 &_free; 164 &_free;
77} 165}
166
167=item $pinger->on_recv ($callback->([[$host, $rtt], ...]))
168
169Registeres a callback to be called for ping replies. If no callback has
170been registered than ping replies will be ignored, otherwise this module
171calculates the round trip time, in seconds, for each reply and calls this
172callback.
173
174The callback receives a single argument, which is an array reference
175with an entry for each reply packet (the replies will be batched for
176efficiency). Each member in the array reference is again an array
177reference with exactly two members: the binary host addresss (4 octets for
178IPv4, 16 for IPv6) and the approximate round trip time, in seconds.
179
180The replies will be passed to the callback as soon as they arrive, and
181this callback can be called many times with batches of replies.
182
183The receive callback will be called whenever a suitable reply arrives,
184whether generated by this pinger or not, whether this pinger is started
185or not. The packets will have a unique 64 bit ID to distinguish them from
186other pinger objects and other generators, but this doesn't help against
187malicious replies.
188
189Note that very high packet rates can overwhelm your process, causing
190replies to be dropped (configure your kernel with long receive queues for
191raw sockets if this is a problem).
192
193Example: register a callback which simply dumps the received data.
194
195 use AnyEvent::Socket;
196
197 $pinger->on_recv (sub {
198 for (@{ $_[0] }) {
199 printf "%s %g\n", (AnyEvent::Socket::format_address $_->[0]), $_->[1];
200 }
201 };
202
203Example: a single ping reply with payload of 1 from C<::1> gets passed
204like this:
205
206 [
207 [ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 0.000280141830444336 ]
208 ]
209
210Example: ping replies for C<127.0.0.1> and C<127.0.0.2>:
211
212 [
213 [ "\177\0\0\1", 0.00015711784362793 ],
214 [ "\177\0\0\2", 0.00090184211731 ]
215 ]
216
217=item $pinger->on_idle ($callback->())
218
219Registers a callback to be called when the pinger becomes I<idle>, that
220is, it has been started, has exhausted all ping ranges and waited for
221the C<max_rtt> time. An idle pinger is also stopped, so the callback can
222instantly add new ranges, if it so desires.
223
224=cut
78 225
79sub on_idle { 226sub on_idle {
80 $IDLE_CB[ &id ] = $_[1]; 227 $IDLE_CB[ &id ] = $_[1];
81} 228}
82 229
87 _stop_id $id; 234 _stop_id $id;
88 ($IDLE_CB[$id] || sub { })->(); 235 ($IDLE_CB[$id] || sub { })->();
89 } 236 }
90}; 237};
91 238
92for(1..10) { 239=item $pinger->interval ($seconds)
93my $p = new AnyEvent::FastPing;#d#
94$p->interval (0);
95$p->max_rtt (0.5);
96#$p->add_range (v127.0.0.1, v127.255.255.254, 0);
97$p->add_range (v127.0.0.1, v127.0.0.1, 0);
98#$p->add_range (v1.0.0.1, v1.255.255.254, 0);
99$p->on_idle (my $cv = AE::cv);
100my $cnt;
101$p->on_recv (sub {
102 $cnt++;
103});
104$p->start;
105 240
106{ 241Configures the minimum interval between packet sends for this pinger - the
107 my $p = new AnyEvent::FastPing;#d# 242pinger will not send packets faster than this rate (or atcually 1 / rate),
108 $p->interval (0); 243even if individual ranges have a lower interval.
109 $p->max_rtt (0.5);
110 $p->add_hosts ([v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2, (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1)x8, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.3], 0);
111 my $cnt;
112 $p->on_recv (sub {
113 use Data::Dump; ddx \@_;
114 });
115 $p->on_idle (sub {
116 undef $p;
117 });
118 $p->start;
119}
120 244
121$cv->recv; 245A value of C<0> selects the fastests possible speed (currently no faster
122warn $cnt; 246than 1_000_000 packets/s).
123}
124 247
125=item AnyEvent::FastPing::icmp_ping [ranges...], $send_interval, $payload, \&callback 248=item $pinger->max_rtt ($seconds)
126 249
127Ping the given IPv4 address ranges. Each range is an arrayref of the 250If your idle callback were called instantly after all ranges were
128form C<[lo, hi, interval]>, where C<lo> and C<hi> are octet strings with 251exhausted and you destroyed the object inside (which is common), then
129either 4 octets (for IPv4 addresses) or 16 octets (for IPV6 addresses), 252there would be no chance to receive some replies, as there would be no
130representing the lowest and highest address to ping (you can convert a 253time fo the packet to travel over the network.
131dotted-quad IPv4 address to this format by using C<inet_aton $address>. The
132range C<interval> is the minimum time in seconds between pings to the
133given range. If omitted, defaults to C<$send_interval>.
134 254
135The C<$send_interval> is the minimum interval between sending any two 255This can be fixed by starting a timer in the idle callback, or more simply
136packets and is a way to make an overall rate limit. If omitted, pings will 256by selecting a suitable C<max_rtt> value, which should be the maximum time
137be sent as fast as possible. 257you allow a ping packet to travel to its destinazion and back.
138 258
139The C<$payload> is a 32 bit unsigned integer given as the ICMP ECHO 259The pinger thread automatically waits for this amount of time before becoming idle.
140REQUEST ident and sequence numbers (in unspecified order :).
141 260
142The request will be queued and all requests will be served by a background 261The default is currently C<0.5> seconds, which is usually plenty.
143thread in order. When all ranges have been pinged, the C<callback> will be
144called.
145 262
146Algorithm: Each range has an associated "next time to send packet" 263=item $pinger->add_range ($lo, $hi[, $interval])
147time. The algorithm loops as long as there are ranges with hosts to be
148pinged and always serves the range with the most urgent packet send
149time. It will at most send one packet every C<$send_interval> seconds.
150 264
151This will ensure that pings to the same range are nicely interleaved with 265Ping the IPv4 (or IPv6, but see below) address range, starting at binary
152other ranges - this can help reduce per-subnet bandwidth while maintaining 266address C<$lo> and ending at C<$hi> (both C<$lo> and C<$hi> will be
153an overall high packet rate. 267pinged), generating no more than one ping per C<$interval> seconds (or as
268fast as possible if omitted).
154 269
155The algorithm to send each packet is O(log n) on the number of ranges, so 270You can convert IP addresses from text to binary form by
271using C<AnyEvent::Util::parse_address>, C<Socket::inet_aton>,
272C<Socket6::inet_pton> or any other method that you like :)
273
274The algorithm to select the next address is O(log n) on the number of
156even a large number of ranges (many thousands) is managable. 275ranges, so even a large number of ranges (many thousands) is managable.
157 276
158No storage is allocated per address. 277No storage is allocated per address.
159 278
160Performance: On my 2 GHz Opteron system with a pretty average nvidia 279Note that, while IPv6 addresses are currently supported, the usefulness of
161gigabit network card I can ping around 60k to 200k adresses per second, 280this option is extremely limited and might be gone in future versions - if
162depending on routing decisions. 281you want to ping a number of IPv6 hosts, better specify them individually
282using the C<add_hosts> method.
163 283
164Example: ping 10.0.0.1-10.0.0.15 with at most 100 packets/s, and 284=item $pinger->add_hosts ([$host...], $interval, $interleave)
16511.0.0.1-11.0.255.255 with at most 1000 packets/s. Do not, however, exceed
1661000 packets/s overall:
167 285
168 my $done = AnyEvent->condvar; 286Similar to C<add_range>, but uses a list of single addresses instead. The
287list is specified as an array reference as first argument. Each entry in
288the array should be a binary host address, either IPv4 or IPv6. Currently,
289all entries in the list must be either IPv4 B<OR> IPv6, so you have to
290create two host ranges if you have mixed addresses.
169 291
170 AnyEvent::FastPing::icmp_ping 292Minimum C<$interval> is the same as for C<add_range> and can be left out.
171 [
172 [v10.0.0.1, v10.0.0.15, .01],
173 [v11.0.0.1, v11.0.255.255, .001],
174 ],
175 .001, 0x12345678,
176 sub {
177 warn "all ranges pinged\n";
178 $done->broadcast;
179 }
180 ;
181 293
182 $done->wait; 294C<$interlave> specifies an increment between addresses: often address
295lists are generated in a way that results in clustering - first all
296addresses from one subnet, then from the next, and so on. To avoid this,
297you can specify an interleave factor. If it is C<1> (the default), then
298every address is pinged in the order specified. If it is C<2>, then only
299every second address will be pinged in the first round, followed by a
300second round with the others. Higher factors will create C<$interleave>
301runs of addresses spaced C<$interleave> indices in the list.
183 302
184=cut 303The special value C<0> selects a (hopefully) suitable interleave factor
304automatically - currently C<256> for lists with less than 65536 addresses,
305and the square root of the list length otherwise.
185 306
186sub icmp_ping($$$&) { 307=item $pinger->start
187# _send_req _req_icmp_ping @_;
188}
189 308
190=item AnyEvent::FastPing::register_cb \&cb 309Start the pinger, unless it is running already. While a pinger is running
310you must not modify the pinger. If you want to change a parameter, you
311have to C<stop> the pinger first.
191 312
192Register a callback that is called for every received ping reply 313The pinger will automatically stop when destroyed.
193(regardless of whether a ping is still in process or not and regardless of
194whether the reply is actually a reply to a ping sent earlier).
195 314
196The code reference gets a single parameter - an arrayref with an 315=item $pinger->stop
197entry for each received packet (replies are being batched for greater
198efficiency). Each packet is represented by an arrayref with three members:
199the source address (an octet string of either 4 (IPv4) or 16 (IPv6) octets
200length), the payload as passed to C<icmp_ping> and the round trip time in
201seconds.
202 316
203Example: register a callback which simply dumps the received data. Since 317Stop the pinger, if it is running. A pinger can be stopped at any time,
204the coderef is created on the fly via sub, it would be hard to unregister 318after which it's current state is preserved - starting it again will
205this callback again :) 319continue where it left off.
206 320
207 AnyEvent::FastPing::register_cb sub {
208 for (@{$_[0]}) {
209 printf "%s %d %g\n",
210 (4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])),
211 $_->[2],
212 $_->[1];
213 }
214 };
215
216Example: a single ping reply with payload of 1 from C<::1> gets passed
217like this:
218
219 [ [
220 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1",
221 "0.000280141830444336",
222 1
223 ] ]
224
225Example: ping replies for C<127.0.0.1> and C<127.0.0.2>, with a payload of
226C<0x12345678>:
227
228 [
229 [
230 "\177\0\0\1",
231 "0.00015711784362793",
232 305419896
233 ],
234 [
235 "\177\0\0\2",
236 "0.00090184211731",
237 305419896
238 ]
239 ]
240
241=item AnyEvent::FastPing::unregister_cb \&cb
242
243Unregister the callback again (make sure you pass the same codereference
244as to C<register_cb>).
245
246=cut 321=cut
247
248our @CB;
249
250sub register_cb($) {
251 push @CB, $_[0];
252}
253
254sub unregister_cb($) {
255 @CB = grep $_ != $_[0], @CB;
256}
257 322
2581; 3231;
259 324
260=back 325=back
261 326

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines