Default Error Allocation Model (dbu_error)
This section describes the functionality and implementation of the default model occupying stage four of the Bus Link Transceiver Pipeline. This model is named dbu_error. Source code is provided in the file dbu_error.ps.c, which resides in the <reldir>/models/std/links directory.
Invocation Context
Packets transmitted on a bus link may be subjected to a stochastic application of bit-errors. Bit errors and collisions are typically the two sources of transmission failure on bus links. Stage four of the transceiver pipeline is used to allocate errors for each transmitted packet. The procedure installed in this stage is called at the time where packet reception completes (i.e., the last bit of the packet is received). The Simulation Kernel expects this stage to compute the number of bit errors affecting the packet and return this integer value in the packet's OPC_TDA_BU_NUM_ERRORS transmission data attribute.
Error Allocation Algorithm
dbu_error is the default pipeline model installed in stage four of the Bus Link Transceiver Pipeline. Allocation of bit errors is based on the value assigned to the bus link's ber attribute. Given this error rate and the number of bits in the packet, dbu_error determines the total number of bit errors present in the packet. Prior to explaining the mechanism used to evaluate this quantity, it is interesting to mention an alternative method which is simple and accurate, but inefficient.
The most realistic model for determining the number of bit errors within a given packet would mimic the behavior of a receiver, which must decide the correctness of each bit based upon the probability of a bit error. Such a model would loop through each bit within the packet, and based on the value of a uniformly distributed random number, decide a bit correctly or incorrectly. This basic algorithm is shown the following figure.
Figure 7: Possible Implementation of Error Allocation
brute_force_err_alloc (num_bits, bit_err_rate)
int num_bits;
double bit_err_rate;
{
int err_accum;
/* compute the number of bit errors in a segment of num_bits length */
/* affected by a probability of bit error bit_err_rate */
FIN
/* reset accumulator for bit errors */
err_accum = 0;
/* test each bit in the segment */
for (i = 0; i < num_bits ; i++)
{
/* obtain a uniformly distributed random */
/* number between 0.0 and 1.0 */
r = random_number (0.0, 1.0);
/* compare the number against the probability of bit error */
/* to evaluate the correctness of the bit */
if (r < bit_err_rate)
{
/* augment the bit error accumulator */
err_accum++;
}
}
/* return the total number of bit errors */
FRET ( err_accum);
}
Because this algorithm tests each bit, it is extremely accurate. Also, in addition to computing the total number of bit errors in the packet, it is capable of providing information regarding the position of the affected bits. However, it is clear that the time required to execute grows linearly with the length of the packet. In addition, even for systems with low bit error rates, every bit must still be tested. The algorithm is therefore not efficient enough for a wide class of modeled systems, and consequently not an appropriate default model.
The approach taken in dbu_error is to avoid sequencing through all the bits in the packet and to therefore not generate positional information about bit errors, but to still accurately compute the overall number of bit errors. This should be done with the minimum number of computations for simulation efficiency considerations.
The algorithm used by dbu_error is based on the expression for the probability of exactly k bit errors occurring in a segment of length N. This probability is denoted as Pk. The probability that no errors occur corresponds to N successive events with probability (1-p) and is given by the following expression:

The probability that exactly one error occurs corresponds to a number of different arrangements of the bits in the segment as expressed below:

And in general,

The algorithm used by dbu_error to compute the number of bit errors in a packet utilizes the above formula. The algorithm first generates a random number between 0 and 1, to provide a value against which probabilities of occurrences of different numbers of bit errors will be tested. Next the algorithm begins iteration. First the probability that 0 bit errors occur is computed according to the formula above, and compared to the random number. If the random number is lower than this probability, then 0 is the number of bit errors in the packet. Else, the probability of occurrence of 1 or fewer bit errors is computed. If the random number is less than this probability, yet higher than the probability of occurrence of the previous number of bit errors, then 1 is the number of bit errors allocated to the packet. The algorithm continues iterating in this manner until a value k is found for which the probability of k or fewer errors occurring is greater than the initial random number. Then the number of errors assigned to the packet is k.
Error Model Implementation
dbu_error determines the number of bit errors for a packet based upon the bit error rate of the bus and the length of the packet. Therefore, the Error Model must first retrieve the bit error rate and the packet length. The bit error rate is given by the ber attribute of the bus link, which is determined after obtaining the bus link's object ID. The packet's length (in bits) is obtained with the op_pk_total_size_get() Kernel Procedure.
Two special cases are distinguished before actually entering the main body of the error allocation algorithm. The first cases have to do with bit error rates which will produce a deterministic number of errors (i.e, rates of 0.0 or 1.0). If the bit error rate is 0.0, then the number of errors generated is directly assigned to be 0, without entering the main body of the algorithm. Likewise, if the bit error rate is 1.0, then the number of errors generated is directly assigned to be the number of bits in the packet.
The remaining case is the most general one and is handled by the iterative algorithm described above. The source code for this model appears in the following figure.
Figure 8: Source Code for Pipeline Procedure dbu_error
/* dbu_error.ps.c */
/* Default error allocation model for bus link transceiver pipeline */
/**********************************/
/* Copyright (c) 2013 */
/* Riverbed Technology, Inc. */
/* All Rights Reserved. */
/**********************************/
#include "opnet.h"
#include <math.h>
/* Define a convenient macro for computing */
/* factorials using the gamma function. */
#define log_factorial(n) lgamma ((double) n + 1.0)
extern double lgamma (double);
#if defined (__cplusplus)
extern "C"
#endif
void
dbu_error (Packet * pkptr)
{
Objid link_objid;
double pe, r, p_accum, p_exact;
double log_p1, log_p2, log_arrange;
int invert_errors = OPC_FALSE;
OpT_Packet_Size seg_size, num_errs;
/** Compute the number of errors assigned to the given packet **/
/** based on its length and the bit error probability. **/
FIN (dbu_error (pkptr));
/* Obtain object id of bus link carrying transmission. */
link_objid = op_td_get_int (pkptr, OPC_TDA_BU_LINK_OBJID);
/* Obtain the bit error probability of the channel. */
if (op_ima_obj_attr_get (link_objid, "ber", &pe) == OPC_COMPCODE_FAILURE)
op_sim_end ("Error in bus error computation pipeline stage (dbu_error):",
"Unable to get bit error probability from bus attribute.",
OPC_NIL, OPC_NIL);
/* Obtain the length of the packet. */
seg_size = op_pk_total_size_get (pkptr);
/* Case 1: if the bit error rate is zero, so is the number of errors. */
if (pe == 0.0 || seg_size == 0)
num_errs = 0;
/* Case 2: if the bit error rate is 1.0, then all the bits are in error. */
/* (note however, that bit error rates should not normally exceed 0.5). */
else if (pe >= 1.0)
num_errs = seg_size;
/* Case 3: The bit-error rate is not zero or one. */
else
{
/* If the bit error rate is greater than 0.5 and less than 1.0, */
/* invert the problem to find instead the number of bits that are */
/* not in error in order to accelerate the performance of the */
/* algorithm. Set a flag to indicate that the result will then */
/* have to be inverted. */
if (pe > 0.5)
{
pe = 1.0 - pe;
invert_errors = OPC_TRUE;
}
/* The error count can be obtained by mapping a uniform random */
/* number in [0, 1[ via the inverse of the cumulative mass function */
/* (CMF) for the bit error count distribution. */
/* Obtain a uniform random number in [0, 1[ to represent */
/* the value of the CMF at the outcome that will be produced. */
r = op_dist_uniform (1.0);
/* Integrate probability mass over possible outcomes until r is */
/* exceeded. The loop iteratively corresponds to "inverting" the CMF */
/* because it finds the bit error count at which the CMF first meets */
/* or exceeds the value r. */
for (p_accum = 0.0, num_errs = 0; num_errs <= seg_size; num_errs++)
{
/* Compute the probability of exactly 'num_errs' */
/* bit errors occurring. */
/* The probability that the first 'num_errs' bits will be in */
/* error is given by pow (pe, num_errs). Here it is obtained */
/* in logarithmic form to avoid underflow for small 'pe' or large */
/* 'num_errs'. */
log_p1 = (double) num_errs * log (pe);
/* Similarly, obtain the probability that the remaining bits */
/* will not be in error. The combination of these two events */
/* represents one possible configuration of bits yielding a total */
/* of 'num_errs' errors. */
log_p2 = (double) (seg_size - num_errs) * log (1.0 - pe);
/* Compute the number of arrangements that are possible with the */
/* same number of bits in error as the particular case above. Again*/
/* obtain this number in logarithmic form (to avoid overflow in */
/* this case). This result is expressed as the logarithmic form of */
/* the formula for the number N of combinations of k items from n: */
/* N = n!/(n-k)!k! */
log_arrange = log_factorial (seg_size) -
log_factorial (num_errs) -
log_factorial (seg_size - num_errs);
/* Compute the probability that exactly 'num_errs' are present */
/* in the segment of bits, in any arrangement. */
p_exact = exp (log_arrange + log_p1 + log_p2);
/* Add this to the probability mass accumulated so far for */
/* previously tested outcomes to obtain the value of the CMF at */
/* outcome = num_errs. */
p_accum += p_exact;
/*'num_errs' is the outcome for this trial if the CMF meets */
/* or exceeds the uniform random value selected earlier. */
if (p_accum >= r)
break;
}
/* If the bit error rate was inverted to compute correct bits instead, */
/* then reinvert the result to obtain the number of bits in error. */
if (invert_errors == OPC_TRUE)
num_errs = seg_size - num_errs;
}
/* Set number of bit errors in packet transmission data attribute. */
/* Limit to 32-bit value. */
op_td_set_int (pkptr, OPC_TDA_BU_NUM_ERRORS, ((int)num_errs));
FOUT
}