After removing a packet from a queue, I get NIL packet errors.

Categories:
Solution Number:
S20661
Last Modified:
2013-08-20
Issue

After removing a packet from a queue, I get NIL packet errors.

Solution

This usually happens when you are iterating over a collection of packets and forget to update the loop index appropriately when removing an item from that collection.For example, the code may look like the following:num_cells = op_subq_stat(0, OPC_QSTAT_PKSIZE);for (i = 0; i < num_cells; i++) { pkptr = op_subq_pk_access(0, i); if (condition_is_met (pkptr) == OPC_TRUE) { op_pk_destroy (op_subq_pk_remove (0, i)); } }Consider the case where the subqueue has 3 packets in it: packets A, B, C are in positions 0, 1, 2 repectively.Assume that for A and C, condition_is_met() evaluates to OPC_FALSE.Assyme that for B, condition_is_met() evaluates to OPC_TRUE.When 'i' is 0, the packet in position 0 is accessed (packet A). No action is taken. At the end of the iteration, packets A, B, C are in positions 0, 1, 2 repectively.When 'i' is 1, the packet in position 1 is accessed (packet B). Packet B is *removed* and destroyed. At the end of the iteration, packets A, and C are in positions 0, and 1 repectively.When 'i' is 2, the packet in position 2 is accessed. But position 2 is out of range, so the pkptr returned is OPC_NIL by the op_subq_pk_access() Kernel Procedure. Hence the NIL packet errors.To fix the code, make sure to decrement the loop index if you have removed an item:num_cells = op_subq_stat(0, OPC_QSTAT_PKSIZE);for (i = 0; i < num_cells; i++) { pkptr = op_subq_pk_access(0, i); if (condition_is_met (pkptr) == OPC_TRUE) { op_pk_destroy (op_subq_pk_remove (0, i)); i--; num_cells--; } } Alternatively, you can loop over your collection starting from the end and moving towards the beginning. In this way, even if you remove items from the collection, they will only make things at the end (which you have already processed) change indices, but not the elements at the beginning of the collection (which remain in their positions and are yet to be processed).

Environment

DES Kernel->Process Modeling/Coding

Attachments
NOTICE: Riverbed® product names have changed. Please refer to the Product List for a complete list of product names.
Can't find an answer? Create a case