Fix extrapolation used in latency measurements
[samplevnf.git] / VNFs / DPPD-PROX / eld.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #ifndef _ELD_H_
18 #define _ELD_H_
19
20 #define PACKET_QUEUE_BITS      14
21 #define PACKET_QUEUE_SIZE      (1 << PACKET_QUEUE_BITS)
22 #define PACKET_QUEUE_MASK      (PACKET_QUEUE_SIZE - 1)
23
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)
27
28 struct early_loss_detect {
29         uint32_t entries[PACKET_QUEUE_SIZE];
30         uint32_t last_pkt_idx;
31 };
32
33 static void early_loss_detect_reset(struct early_loss_detect *eld)
34 {
35         for (size_t i = 0; i < PACKET_QUEUE_SIZE; i++) {
36                 eld->entries[i] = -1;
37         }
38 }
39
40 static uint32_t early_loss_detect_count_remaining_loss(struct early_loss_detect *eld)
41 {
42         uint32_t queue_id;
43         uint32_t n_loss;
44         uint32_t n_loss_total = 0;
45
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;
56                 }
57         }
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;
63                 }
64         }
65
66         eld->entries[eld->last_pkt_idx & PACKET_QUEUE_MASK] = -1;
67         return n_loss_total;
68 }
69
70 static uint32_t early_loss_detect_add(struct early_loss_detect *eld, uint32_t packet_index)
71 {
72         uint32_t old_queue_id, queue_pos, n_loss;
73
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;
78
79         return (eld->entries[queue_pos] - old_queue_id - 1) & QUEUE_ID_MASK;
80 }
81
82 #endif /* _ELD_H_ */