ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-SNMP-XS/XS.pm
(Generate patch)

Comparing Net-SNMP-XS/XS.pm (file contents):
Revision 1.2 by root, Wed Apr 8 10:39:32 2009 UTC vs.
Revision 1.8 by root, Sat Apr 11 06:24:18 2009 UTC

13This module tries to speed up Net::SNMP response packet decoding. 13This module tries to speed up Net::SNMP response packet decoding.
14 14
15It does this by overriding a few selected internal method by (almost) 15It does this by overriding a few selected internal method by (almost)
16equivalent XS methods. 16equivalent XS methods.
17 17
18This currently reduces decode time by some 70%. 18This currently reduces decode time by a factor of ten for typical bulk
19responses.
19 20
20There are currently the following limitations when using this module: 21There are currently the following limitations when using this module:
21 22
22=over 4 23=over 4
23 24
24=item leading dots are required for iods 25=item overriding internal functions might cause the module to
26malfunction with future versions of Net::SNMP
25 27
26=item error handling is currently broken (but parse errors will be detected) 28=item only leading dots for oids are supported
27 29
28=item oid components are limited to unsigned 32 bit integers 30=item error messages will be simpler/different
29 31
30=item translation will be ignored (all values will be delivered "raw") 32=item translation will be ignored (all values will be delivered "raw")
31 33
32=item a 64 bit perl is required
33
34=item a moderately modern (>= C99) C compiler is required 34=item a moderately modern (>= C99) C compiler is required
35 35
36=item only tested with 5.10 36=item only tested with 5.10, no intentions to port to older perls
37
38=item duplicate OIDs are not supported
39
40=item REPORT PDUs are not supported
37 41
38=back 42=back
39 43
40=cut 44=cut
41 45
44use strict qw(vars subs); 48use strict qw(vars subs);
45no warnings; 49no warnings;
46 50
47use Guard; 51use Guard;
48 52
53use Net::SNMP ();
49use Net::SNMP::PDU (); 54use Net::SNMP::PDU ();
50use Net::SNMP::Message (); 55use Net::SNMP::Message ();
51use Net::SNMP::MessageProcessing (); 56use Net::SNMP::MessageProcessing ();
52 57
53our $VERSION; 58our $VERSION;
54our $old_prepare; 59our $old_prepare;
55 60
56BEGIN { 61BEGIN {
57 $VERSION = '0.01'; 62 $VERSION = '0.02';
58 63
59 $old_prepare = \&Net::SNMP::MessageProcessing::prepare_data_elements; 64 $old_prepare = \&Net::SNMP::MessageProcessing::prepare_data_elements;
60 65
61 # this overrides many methods inside 66 # this overrides many methods inside
62 require XSLoader; 67 require XSLoader;
69 set_msg $msg, $msg->{_buffer}; 74 set_msg $msg, $msg->{_buffer};
70 scope_guard \&clr_msg; 75 scope_guard \&clr_msg;
71 &$old_prepare 76 &$old_prepare
72} 77}
73 78
74# almost direct copy from Net::SNMP::Message, as we cannot access $process_methods 79package Net::SNMP::Message;
80
81Net::SNMP::XS::set_type INTEGER , \&_process_integer32;
82Net::SNMP::XS::set_type OCTET_STRING , \&_process_octet_string;
83Net::SNMP::XS::set_type NULL , \&_process_null;
84Net::SNMP::XS::set_type OBJECT_IDENTIFIER, \&_process_object_identifier;
85Net::SNMP::XS::set_type SEQUENCE , \&_process_sequence;
86Net::SNMP::XS::set_type IPADDRESS , \&_process_ipaddress;
87Net::SNMP::XS::set_type COUNTER , \&_process_counter;
88Net::SNMP::XS::set_type GAUGE , \&_process_gauge;
89Net::SNMP::XS::set_type TIMETICKS , \&_process_timeticks;
90Net::SNMP::XS::set_type OPAQUE , \&_process_opaque;
91Net::SNMP::XS::set_type COUNTER64 , \&_process_counter64;
92Net::SNMP::XS::set_type NOSUCHOBJECT , \&_process_nosuchobject;
93Net::SNMP::XS::set_type NOSUCHINSTANCE , \&_process_nosuchinstance;
94Net::SNMP::XS::set_type ENDOFMIBVIEW , \&_process_endofmibview;
95Net::SNMP::XS::set_type GET_REQUEST , \&_process_get_request;
96Net::SNMP::XS::set_type GET_NEXT_REQUEST , \&_process_get_next_request;
97Net::SNMP::XS::set_type GET_RESPONSE , \&_process_get_response;
98Net::SNMP::XS::set_type SET_REQUEST , \&_process_set_request;
99Net::SNMP::XS::set_type TRAP , \&_process_trap;
100Net::SNMP::XS::set_type GET_BULK_REQUEST , \&_process_get_bulk_request;
101Net::SNMP::XS::set_type INFORM_REQUEST , \&_process_inform_request;
102Net::SNMP::XS::set_type SNMPV2_TRAP , \&_process_v2_trap;
103Net::SNMP::XS::set_type REPORT , \&_process_report;
104
105package Net::SNMP::PDU;
106
107# var_bind_list hardcodes oid_lext_sort. *sigh*
108# we copy it 1:1, except for using oid_lex_sort.
109
110sub var_bind_list
75{ 111{
76 package Net::SNMP::Message; 112 my ($this, $vbl, $types) = @_;
77 113
78 my @process_methods; 114 return if defined($this->{_error});
79 115
80 $process_methods [INTEGER ] = \&_process_integer32; 116 if (@_ > 1) {
81 $process_methods [OCTET_STRING ] = \&_process_octet_string; 117 # The VarBindList HASH is being updated from an external
82 $process_methods [NULL ] = \&_process_null; 118 # source. We need to update the VarBind names ARRAY to
83 $process_methods [OBJECT_IDENTIFIER] = \&_process_object_identifier; 119 # correspond to the new keys of the HASH. If the updated
84 $process_methods [SEQUENCE ] = \&_process_sequence; 120 # information is valid, we will use lexicographical ordering
85 $process_methods [IPADDRESS ] = \&_process_ipaddress; 121 # for the ARRAY entries since we do not have a PDU to use
86 $process_methods [COUNTER ] = \&_process_counter; 122 # to determine the ordering. The ASN.1 types HASH is also
87 $process_methods [GAUGE ] = \&_process_gauge; 123 # updated here if a cooresponding HASH is passed. We double
88 $process_methods [TIMETICKS ] = \&_process_timeticks; 124 # check the mapping by populating the hash with the keys of
89 $process_methods [OPAQUE ] = \&_process_opaque; 125 # the VarBindList HASH.
90 $process_methods [COUNTER64 ] = \&_process_counter64;
91 $process_methods [NOSUCHOBJECT ] = \&_process_nosuchobject;
92 $process_methods [NOSUCHINSTANCE ] = \&_process_nosuchinstance;
93 $process_methods [ENDOFMIBVIEW ] = \&_process_endofmibview;
94 $process_methods [GET_REQUEST ] = \&_process_get_request;
95 $process_methods [GET_NEXT_REQUEST ] = \&_process_get_next_request;
96 $process_methods [GET_RESPONSE ] = \&_process_get_response;
97 $process_methods [SET_REQUEST ] = \&_process_set_request;
98 $process_methods [TRAP ] = \&_process_trap;
99 $process_methods [GET_BULK_REQUEST ] = \&_process_get_bulk_request;
100 $process_methods [INFORM_REQUEST ] = \&_process_inform_request;
101 $process_methods [SNMPV2_TRAP ] = \&_process_v2_trap;
102 $process_methods [REPORT ] = \&_process_report;
103 126
104 # should be done in XS 127 if (!defined($vbl) || (ref($vbl) ne 'HASH')) {
105 sub process
106 {
107# my ($this, $expected, $found) = @_;
108 128
109 return $_[0]->_error if defined($_[0]->{_error}); 129 $this->{_var_bind_list} = undef;
110 return $_[0]->_error unless defined(my $type = _buffer_get($_[0], 1)); 130 $this->{_var_bind_names} = [];
131 $this->{_var_bind_types} = undef;
111 132
112 $type = unpack 'C', $type; 133 } else {
113 134
114 if ($process_methods[$type]) { 135 $this->{_var_bind_list} = $vbl;
115 if (@_ >= 2 && (defined $_[1]) && $type != $_[1]) { 136
116 return $_[0]->_error( 137 @{$this->{_var_bind_names}} = Net::SNMP::oid_lex_sort keys %$vbl;
117 'Expected %s, but found %s', asn1_itoa($_[1]), asn1_itoa($type) 138
118 ); 139 if (!defined($types) || (ref($types) ne 'HASH')) {
140 $types = {};
119 } 141 }
120 $_[2] = $type if (@_ == 3); 142
121 $process_methods[$type]->($_[0], $type); 143 map {
122 } else { 144 $this->{_var_bind_types}->{$_} =
123 $_[0]->_error('Unknown ASN.1 type [0x%02x]', $type); 145 exists($types->{$_}) ? $types->{$_} : undef;
146 } keys(%{$vbl});
147
124 } 148 }
149
125 } 150 }
151
152 $this->{_var_bind_list};
126} 153}
127 154
1281; 1551;
129 156
130=head1 AUTHOR 157=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines