Encoding of SNMP Counter64


I wonder about the encoding of the SNMP Counter64 value.
The RFC 2578 says it should range between Zero and
2^64-1 (18446744073709551615 decimal).

But then I found this in the OTP code of snmp_pdus.erl:

%% Counter64         
          dec_value([70 | Bytes]) ->
              %% Counter64 is an unsigned 64 but is actually encoded as 
              %% a signed integer 64.

So the maximum value seem to be: 18410715276690587647

3> Val1 = 18410715276690587647.
4> snmp_pdus:enc_value('Counter64', Val1).

6> Val2 = 18410715276690587648.
7> snmp_pdus:enc_value('Counter64', Val2).

So my question is if this is correct?

Cheers, Tobbe


Bumping up this question.
I have tested this with net-snmp and it seem to be able to handle the (according to the RFC) max Counter64 value:

he Counter64 type represents a non-negative integer which
   monotonically increases until it reaches a maximum value of 2^64-1
   (18446744073709551615 decimal), when it wraps around and starts
   increasing again from zero.
snmpwalk -v2c -c testing -m +TAILF-TOP-MIB:TAILF-TOBBE-MIB TAILF-TOBBE-MIB::tfTobbeMIB
TAILF-TOBBE-MIB::tfTobbeNumber.0 = Counter64: 18446744073709551615
1 Like

I don’t think there’s a problem. SNMP is encoded using the ASN.1 Basic Encoding Rules (BER). In RFC2578 you’ll find the ASN.1 which specifies:

Counter64 ::=
        IMPLICIT INTEGER (0..18446744073709551615)

So a Counter64 value is BER encoded as an INTEGER however it has a subtype constraint on the value. ASN.1 is a specification language. The above says that an implementation of the protocol should BER encode an INTEGER for transmission but internally limit it’s use to values between 0 and 18446744073709551615.


So then there is a bug in the Erlang implementation, which apparently
rolls over to zero at: 18410715276690587648


That… uh… is not quite 2^64… Where’d that number come from? o.O

1 Like

See the comment in snmp_pdus.erl as shown in my initial post.

Also, if anyone would like to repeat my test with net-snmp, you’ll find it here:

I also created a bug-report at: SNMP Counter64 is encoded wrong !? · Issue #5756 · erlang/otp · GitHub


Yeah I’m just really curious as to the reasoning of why it was done that way…

1 Like