Serial Number Arithmetic
   HOME

TheInfoList



OR:

Many
protocols Protocol may refer to: Sociology and politics * Protocol (politics), a formal agreement between nation states * Protocol (diplomacy), the etiquette of diplomacy and affairs of state * Etiquette, a code of personal behavior Science and technology ...
and
algorithms In mathematics and computer science, an algorithm () is a finite sequence of mathematically rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for per ...
require the serialization or enumeration of related entities. For example, a
communication protocol A communication protocol is a system of rules that allows two or more entities of a communications system to transmit information via any variation of a physical quantity. The protocol defines the rules, syntax, semantics (computer science), sem ...
must know whether some packet comes "before" or "after" some other packet. The
IETF The Internet Engineering Task Force (IETF) is a standards organization for the Internet standard, Internet and is responsible for the technical standards that make up the Internet protocol suite (TCP/IP). It has no formal membership roster ...
(
Internet Engineering Task Force The Internet Engineering Task Force (IETF) is a standards organization for the Internet standard, Internet and is responsible for the technical standards that make up the Internet protocol suite (TCP/IP). It has no formal membership roster ...
) attempts to define "serial number arithmetic" for the purposes of manipulating and comparing these
sequence number A sequence number is a consecutive number in a sequence of numbers, usually of real integers (natural numbers). Sequence numbers have many practical applications. They can be used, among other things, as part of serial numbers on manufactured part ...
s. In short, when the absolute serial number value decreases by more than half of the maximum value (e.g. 128 in an 8-bit value), it is considered to be "after" the former, whereas other decreases are considered to be "before". This task is rather more complex than it might first appear, because most algorithms use fixed-size (
binary Binary may refer to: Science and technology Mathematics * Binary number, a representation of numbers using only two values (0 and 1) for each digit * Binary function, a function that takes two arguments * Binary operation, a mathematical op ...
) representations for sequence numbers. It is often important for the algorithm not to "break down" when the numbers become so large that they are incremented one last time and "wrap" around their maximum numeric ranges (go instantly from a large positive number to 0 or a large negative number). Some protocols choose to ignore these issues and simply use very large integers for their counters, in the hope that the program will be replaced (or they will retire) before the problem occurs (see
Y2K Y2K may refer to: * Y2K problem, a computer issue related to the year 2000 * Year 2K, the year 2000 2000 was designated as the International Year for the Culture of Peace and the World Mathematics, Mathematical Year. Popular cultu ...
). Many communication protocols apply serial number arithmetic to packet sequence numbers in their implementation of a
sliding window protocol A sliding window protocol is a feature of packet-based data transmission protocols. Sliding window protocols are used where reliable in-order delivery of packets is required, such as in the data link layer (OSI layer 2) as well as in the Trans ...
. Some versions of TCP use protection against wrapped sequence numbers (PAWS). PAWS applies the same serial number arithmetic to packet timestamps, using the timestamp as an extension of the high-order bits of the sequence number.: "TCP Extensions for High Performance", section 4.2.


Operations on sequence numbers

Only addition of a small positive
integer An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
to a sequence number and comparison of two sequence numbers are discussed. Only unsigned binary implementations are discussed, with an arbitrary size in bits noted throughout the RFC (and below) as "SERIAL_BITS".


Addition

Adding an integer to a sequence number is simple unsigned integer addition, followed by unsigned
modulo operation In computing and mathematics, the modulo operation returns the remainder or signed remainder of a Division (mathematics), division, after one number is divided by another, the latter being called the ''modular arithmetic, modulus'' of the operatio ...
to bring the result back into range (usually implicit in the unsigned addition, on most architectures): : ''s''' = (''s'' + ''n'') modulo 2 Addition of a value below 0 or above 2 − 1 is undefined. Basically, adding values beyond this range will cause the resultant sequence number to "wrap", and (often) result in a number that is considered "less than" the original sequence number.


Comparison

A means of comparing two sequence numbers ''i'' and ''i'' (the unsigned integer representations of sequence numbers ''s''1 and ''s''2) is presented. Equality is defined as simple numeric equality. The algorithm presented for comparison is complex, having to take into account whether the first sequence number is close to the "end" of its range of values, and thus a smaller "wrapped" number may actually be considered "greater" than the first sequence number. Thus ''i'' is considered less than ''i'' only if : (''i'' < ''i'' and ''i'' − ''i'' < 2) or : (''i'' > ''i'' and ''i'' − ''i'' > 2)


Shortfalls

The algorithms presented by the RFC have at least one significant shortcoming: there are sequence numbers for which comparison is undefined. Since many algorithms are implemented independently by multiple independent cooperating parties, it is often impossible to prevent all such situations from occurring. The authors of acknowledge this without offering a general solution:
While it would be possible to define the test in such a way that the inequality would not have this surprising property, while being defined for all pairs of values, such a definition would be unnecessarily burdensome to implement, and difficult to understand, and would still allow cases where s1 < s2 and (s1 + 1) > (s2 + 1) which is just as non-intuitive. Thus the problem case is left undefined, implementations are free to return either result, or to flag an error, and users must take care not to depend on any particular outcome. Usually this will mean avoiding allowing those particular pairs of numbers to co-exist.
Thus, it is often difficult or impossible to avoid all "undefined" comparisons of sequence numbers. However, a relatively simple solution is available. By mapping the unsigned sequence numbers onto signed
two's complement Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
arithmetic operations, every comparison of any sequence number is defined, and the comparison operation itself is dramatically simplified. All comparisons specified by the RFC retain their original truth values; only the formerly "undefined" comparisons are affected.


General solution

The algorithm specifies that, for ''N''-bit sequence numbers, there are 2''N''−1 − 1 values considered "greater than" and 2''N''−1 − 1 considered "less than". Comparison against the remaining value (exactly 2''N''−1-distant) is deemed to be "undefined". Most modern hardware implements signed
two's complement Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. Two's complement uses the binary digit with the ''greatest'' value as the ''s ...
binary arithmetic operations. These operations are fully defined for the entire range of values for any operands they are given, since any ''N''-bit binary number can contain 2''N'' distinct values, and since one of them is taken up by the value 0, there are an odd number of spots left for all the non-zero positive and negative numbers. There is simply one more negative number representable than there are positive. For example, a 16-bit 2's complement value may contain numbers ranging from to . So, if we simply re-cast sequence numbers as 2's complement integers and allow there to be one more sequence number considered "less than" than there are sequence numbers considered "greater than", we should be able to use simple signed arithmetic comparisons instead of the logically incomplete formula proposed by the RFC. Here are some examples (in 16 bits, again), comparing some random sequence numbers, against the sequence number with the value 0: unsigned binary signed sequence value distance -------- ------ -------- 32767

0x7FFF

32767 1

0x0001

1 0

0x0000

0 65535

0xFFFF

−1 65534

0xFFFE

−2 32768

0x8000

−32768 It is easy to see that the signed interpretation of the sequence numbers are in the correct order, so long as we "rotate" the sequence number in question so that its 0 matches up with the sequence number we are comparing it against. It turns out that this is simply done using an unsigned subtraction and simply interpreting the result as a signed two's complement number. The result is the signed "distance" between the two sequence numbers. Once again, if i1 and i2 are the unsigned binary representations of the sequence numbers ''s''1 and ''s''2, the distance from ''s''1 to ''s''2 is distance = (signed)(i1 - i2) If distance is 0, the numbers are equal. If it is < 0, then ''s''1 is "less than" or "before" ''s''2. Simple, clean and efficient, and fully defined. However, not without surprises. All sequence number arithmetic must deal with "wrapping" of sequence numbers; the number 2''N''−1 is equidistant in both directions, in sequence number terms. In our math, they are both considered to be "less than" each other: distance1 = (signed)(0x8000 - 0x0)

(signed)0x8000

-32768 < 0 distance2 = (signed)(0x0 - 0x8000)

(signed)0x8000

-32768 < 0
This is obviously true for any two sequence numbers with distance of 0x8000 between them. Furthermore, implementing serial number arithmetic using two's complement arithmetic implies serial numbers of a bit-length matching the machine's integer sizes; usually 16-bit, 32-bit and 64-bit. Implementing 20-bit serial numbers needs shifts (assuming 32-bit ints): distance = (signed)((i1 << 12) - (i2 << 12))


See also

* Date windowing * Lollipop sequence numbering *
Modular arithmetic In mathematics, modular arithmetic is a system of arithmetic operations for integers, other than the usual ones from elementary arithmetic, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to mo ...


References


External links

* * {{IETF RFC, 1982 Domain Name System Serial numbers