2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 #define PACKET_QUEUE_BITS 14
21 #define PACKET_QUEUE_SIZE (1 << PACKET_QUEUE_BITS)
22 #define PACKET_QUEUE_MASK (PACKET_QUEUE_SIZE - 1)
24 #define QUEUE_ID_BITS (32 - PACKET_QUEUE_BITS)
25 #define QUEUE_ID_SIZE (1 << QUEUE_ID_BITS)
26 #define QUEUE_ID_MASK (QUEUE_ID_SIZE - 1)
28 struct early_loss_detect {
29 uint32_t entries[PACKET_QUEUE_SIZE];
30 uint32_t last_pkt_idx;
33 static void early_loss_detect_reset(struct early_loss_detect *eld)
35 for (size_t i = 0; i < PACKET_QUEUE_SIZE; i++) {
40 static uint32_t early_loss_detect_count_remaining_loss(struct early_loss_detect *eld)
44 uint32_t n_loss_total = 0;
46 /* Need to check if we lost any packet before last packet
47 received Any packet lost AFTER the last packet received
48 cannot be counted. Such a packet will be counted after both
49 lat and gen restarted */
50 queue_id = eld->last_pkt_idx >> PACKET_QUEUE_BITS;
51 for (uint32_t i = (eld->last_pkt_idx + 1) & PACKET_QUEUE_MASK; i < PACKET_QUEUE_SIZE; i++) {
52 // We ** might ** have received OOO packets; do not count them as lost next time...
53 if (queue_id - eld->entries[i] != 0) {
54 n_loss = (queue_id - eld->entries[i] - 1) & QUEUE_ID_MASK;
55 n_loss_total += n_loss;
58 for (uint32_t i = 0; i < (eld->last_pkt_idx & PACKET_QUEUE_MASK); i++) {
59 // We ** might ** have received OOO packets; do not count them as lost next time...
60 if (eld->entries[i] - queue_id != 1) {
61 n_loss = (queue_id - eld->entries[i]) & QUEUE_ID_MASK;
62 n_loss_total += n_loss;
66 eld->entries[eld->last_pkt_idx & PACKET_QUEUE_MASK] = -1;
70 static uint32_t early_loss_detect_add(struct early_loss_detect *eld, uint32_t packet_index)
72 uint32_t old_queue_id, queue_pos, n_loss;
74 eld->last_pkt_idx = packet_index;
75 queue_pos = packet_index & PACKET_QUEUE_MASK;
76 old_queue_id = eld->entries[queue_pos];
77 eld->entries[queue_pos] = packet_index >> PACKET_QUEUE_BITS;
79 return (eld->entries[queue_pos] - old_queue_id - 1) & QUEUE_ID_MASK;