From 70e80f76e0398783537f0511f6fc4d293efd24dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Tue, 7 Jan 2020 11:07:13 +0000 Subject: [PATCH] Add a fixed rate mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- mud.c | 23 ++++++++++++++++------- mud.h | 3 ++- test.c | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/mud.c b/mud.c index 8979160..ac31bcc 100644 --- a/mud.c +++ b/mud.c @@ -125,6 +125,7 @@ struct mud_msg { unsigned char timeout[MUD_TIME_SIZE]; unsigned char mtu[2]; unsigned char loss; + unsigned char fixed_rate; struct mud_addr addr; }; @@ -1124,6 +1125,7 @@ mud_send_msg(struct mud *mud, struct mud_path *path, uint64_t now, MUD_STORE_MSG(msg->timeout, path->conf.msg_timeout); msg->loss = (unsigned char)path->tx.loss; + msg->fixed_rate = path->conf.fixed_rate; const struct mud_crypto_opt opt = { .dst = dst, @@ -1193,9 +1195,11 @@ mud_update_window(struct mud *mud, struct mud_path *path, uint64_t now, uint64_t rx_dt, uint64_t rx_bytes, uint64_t rx_pkt) { if (rx_dt && rx_dt > tx_dt + (tx_dt >> 3)) { - path->tx.rate = 1 + ((rx_bytes * MUD_ONE_SEC) - 1) / rx_dt; - if (path->tx.rate > path->conf.tx_max_rate) - path->tx.rate = path->conf.tx_max_rate; + if (!path->conf.fixed_rate) { + path->tx.rate = 1 + ((rx_bytes * MUD_ONE_SEC) - 1) / rx_dt; + if (path->tx.rate > path->conf.tx_max_rate) + path->tx.rate = path->conf.tx_max_rate; + } } else { uint64_t tx_acc = path->msg.tx.acc + tx_pkt; uint64_t rx_acc = path->msg.rx.acc + rx_pkt; @@ -1302,10 +1306,11 @@ mud_recv_msg(struct mud *mud, struct mud_path *path, const uint64_t max_rate = MUD_LOAD_MSG(msg->max_rate); - if (path->conf.tx_max_rate != max_rate) { - path->conf.tx_max_rate = max_rate; + if (path->conf.tx_max_rate != max_rate || msg->fixed_rate) path->tx.rate = max_rate; - } + + path->conf.tx_max_rate = max_rate; + path->conf.fixed_rate = msg->fixed_rate; path->msg.sent++; path->msg.time = now; @@ -1503,7 +1508,8 @@ mud_set_state(struct mud *mud, struct sockaddr *addr, enum mud_state state, unsigned long tx_max_rate, unsigned long rx_max_rate, - unsigned long msg_timeout) + unsigned long msg_timeout, + unsigned char fixed_rate) { if (!mud->peer.set || state > MUD_UP) { errno = EINVAL; @@ -1530,6 +1536,9 @@ mud_set_state(struct mud *mud, struct sockaddr *addr, if (msg_timeout) path->conf.msg_timeout = msg_timeout; + if (fixed_rate) + path->conf.fixed_rate = fixed_rate >> 1; + if (state && path->state != state) { path->state = state; mud_reset_path(path); diff --git a/mud.h b/mud.h index 55d5f79..2361198 100644 --- a/mud.h +++ b/mud.h @@ -56,6 +56,7 @@ struct mud_path { uint64_t tx_max_rate; uint64_t rx_max_rate; uint64_t msg_timeout; + unsigned char fixed_rate; } conf; unsigned char ok; }; @@ -88,7 +89,7 @@ int mud_set_tc (struct mud *, int); int mud_set_aes (struct mud *); int mud_set_state (struct mud *, struct sockaddr *, enum mud_state, - unsigned long, unsigned long, unsigned long); + unsigned long, unsigned long, unsigned long, unsigned char); int mud_peer (struct mud *, struct sockaddr *); diff --git a/test.c b/test.c index 64b0527..e056f81 100644 --- a/test.c +++ b/test.c @@ -52,7 +52,7 @@ main(int argc, char **argv) // ...from loopback at 1MBps (not 1Mbps) if (mud_set_state(mud, (struct sockaddr *)&local, - MUD_UP, 1000 * 1000, 1000 * 1000, 0)) { + MUD_UP, 1000 * 1000, 1000 * 1000, 0, 0)) { perror("mud_set_state"); return -1; }