Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 1 | /* gprs_codel.h |
| 2 | * |
| 3 | * This is an implementation of the CoDel algorithm based on the reference |
| 4 | * pseudocode (see http://queue.acm.org/appendices/codel.html). |
| 5 | * Instead of abstracting the queue itself, the following implementation |
| 6 | * provides a time stamp based automaton. The main work is done by a single |
| 7 | * decision function which updates the state and tells whether to pass or to |
| 8 | * drop a packet after it has been taken from the queue. |
| 9 | * |
| 10 | * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 11 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #pragma once |
| 25 | |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 26 | #include <time.h> |
Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 27 | |
| 28 | /* Spec default values */ |
| 29 | #define GPRS_CODEL_DEFAULT_INTERVAL_MS 100 |
| 30 | #define GPRS_CODEL_DEFAULT_MAXPACKET 512 |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | struct gprs_codel { |
| 37 | int dropping; |
| 38 | unsigned count; |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 39 | struct timespec first_above_time; |
| 40 | struct timespec drop_next; |
| 41 | struct timespec target; |
| 42 | struct timespec interval; |
Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 43 | unsigned maxpacket; |
| 44 | }; |
| 45 | |
| 46 | /*! |
| 47 | * \brief Decide about packet drop and update CoDel state |
| 48 | * |
| 49 | * This function takes timing information and decides whether the packet in |
| 50 | * question should be dropped in order to keep related queue in a 'good' state. |
| 51 | * The function is meant to be called when the packet is dequeued. |
| 52 | * |
| 53 | * The CoDel state is updated by this function. |
| 54 | * |
| 55 | * \param state A pointer to the CoDel state of this queue |
| 56 | * \param recv The time when the packet has entered the queue, |
| 57 | * use NULL if dequeueing was not possible because the queue is |
| 58 | * empty |
| 59 | * \param now The current (dequeueing) time |
| 60 | * \param bytes The number of bytes currently stored in the queue (-1 if |
| 61 | * unknown) |
| 62 | * |
| 63 | * \return != 0 if the packet should be dropped, 0 otherwise |
| 64 | */ |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 65 | int gprs_codel_control(struct gprs_codel *state, const struct timespec *recv, |
| 66 | const struct timespec *now, int bytes); |
Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 67 | |
| 68 | /*! |
| 69 | * \brief Initialise CoDel state |
| 70 | * |
| 71 | * This function initialises the CoDel state object. It sets the interval time |
| 72 | * to the default value (GPRS_CODEL_DEFAULT_INTERVAL_MS). |
| 73 | * |
| 74 | * \param state A pointer to the CoDel state of this queue |
| 75 | */ |
| 76 | void gprs_codel_init(struct gprs_codel *state); |
| 77 | |
| 78 | /*! |
| 79 | * \brief Set interval time |
| 80 | * |
| 81 | * This function changes the interval time. |
| 82 | * The target time is derived from the interval time as proposed in the spec |
| 83 | * (5% of interval time). |
| 84 | * |
| 85 | * \param state A pointer to the CoDel state of this queue |
| 86 | * \param interval_ms The initial interval in ms to be used (<= 0 selects the |
| 87 | * default value) |
| 88 | */ |
| 89 | void gprs_codel_set_interval(struct gprs_codel *state, int interval_ms); |
| 90 | |
| 91 | /*! |
| 92 | * \brief Set max packet size |
| 93 | * |
| 94 | * This function changes the maxpacket value. If no more than this number of |
| 95 | * bytes are still stored in the queue, no dropping will be done. |
| 96 | * |
| 97 | * \param state A pointer to the CoDel state of this queue |
| 98 | * \param maxpacket The value in bytes |
| 99 | */ |
| 100 | void gprs_codel_set_maxpacket(struct gprs_codel *state, int maxpacket); |
| 101 | |
| 102 | #ifdef __cplusplus |
| 103 | } |
| 104 | #endif |