2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-08-26 07:36:42 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2015-10-11 05:54:23 +08:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2016-09-11 22:47:51 +08:00
|
|
|
#include "bwcontroller.h"
|
|
|
|
|
2018-02-27 10:07:59 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-09-22 22:20:33 +08:00
|
|
|
#include "ring_buffer.h"
|
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2015-10-11 05:54:23 +08:00
|
|
|
#include "../toxcore/util.h"
|
2016-09-11 22:47:51 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
#define BWC_PACKET_ID 196
|
2018-08-13 00:09:06 +08:00
|
|
|
#define BWC_SEND_INTERVAL_MS 950 // 0.95s
|
2015-10-11 05:54:23 +08:00
|
|
|
#define BWC_AVG_PKT_COUNT 20
|
2018-01-20 05:59:42 +08:00
|
|
|
#define BWC_AVG_LOSS_OVER_CYCLES_COUNT 30
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 00:09:06 +08:00
|
|
|
typedef struct BWCCycle {
|
|
|
|
uint32_t last_recv_timestamp; /* Last recv update time stamp */
|
|
|
|
uint32_t last_sent_timestamp; /* Last sent update time stamp */
|
|
|
|
uint32_t last_refresh_timestamp; /* Last refresh time stamp */
|
|
|
|
|
|
|
|
uint32_t lost;
|
|
|
|
uint32_t recv;
|
|
|
|
} BWCCycle;
|
|
|
|
|
|
|
|
typedef struct BWCRcvPkt {
|
|
|
|
uint32_t packet_length_array[BWC_AVG_PKT_COUNT];
|
|
|
|
RingBuffer *rb;
|
|
|
|
} BWCRcvPkt;
|
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
struct BWController_s {
|
2018-08-13 00:09:06 +08:00
|
|
|
m_cb *mcb;
|
|
|
|
void *mcb_user_data;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
Messenger *m;
|
|
|
|
uint32_t friend_number;
|
|
|
|
|
2018-08-13 00:09:06 +08:00
|
|
|
BWCCycle cycle;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 00:09:06 +08:00
|
|
|
BWCRcvPkt rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
uint32_t packet_loss_counted_cycles;
|
2018-01-20 05:03:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BWCMessage {
|
|
|
|
uint32_t lost;
|
|
|
|
uint32_t recv;
|
2015-10-11 05:54:23 +08:00
|
|
|
};
|
|
|
|
|
2020-03-18 08:42:23 +08:00
|
|
|
static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object);
|
|
|
|
static void send_update(BWController *bwc);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 00:09:06 +08:00
|
|
|
BWController *bwc_new(Messenger *m, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
BWController *retu = (BWController *)calloc(sizeof(struct BWController_s), 1);
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(m->log, "Creating bandwidth controller");
|
2015-10-11 05:54:23 +08:00
|
|
|
retu->mcb = mcb;
|
2018-08-13 00:09:06 +08:00
|
|
|
retu->mcb_user_data = mcb_user_data;
|
2015-10-11 05:54:23 +08:00
|
|
|
retu->m = m;
|
|
|
|
retu->friend_number = friendnumber;
|
2018-08-18 01:22:18 +08:00
|
|
|
uint64_t now = current_time_monotonic(m->mono_time);
|
2018-08-13 00:09:06 +08:00
|
|
|
retu->cycle.last_sent_timestamp = now;
|
|
|
|
retu->cycle.last_refresh_timestamp = now;
|
2015-10-11 05:54:23 +08:00
|
|
|
retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT);
|
2018-01-20 05:59:42 +08:00
|
|
|
retu->cycle.lost = 0;
|
|
|
|
retu->cycle.recv = 0;
|
|
|
|
retu->packet_loss_counted_cycles = 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
/* Fill with zeros */
|
2018-08-13 00:09:06 +08:00
|
|
|
for (int i = 0; i < BWC_AVG_PKT_COUNT; ++i) {
|
2018-01-20 05:59:42 +08:00
|
|
|
rb_write(retu->rcvpkt.rb, &retu->rcvpkt.packet_length_array[i]);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
|
|
|
m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu);
|
2015-10-11 05:54:23 +08:00
|
|
|
return retu;
|
|
|
|
}
|
2018-01-20 05:03:48 +08:00
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
void bwc_kill(BWController *bwc)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!bwc) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, nullptr, nullptr);
|
2015-10-11 05:54:23 +08:00
|
|
|
rb_kill(bwc->rcvpkt.rb);
|
|
|
|
free(bwc);
|
|
|
|
}
|
2018-01-20 05:03:48 +08:00
|
|
|
|
|
|
|
void bwc_add_lost(BWController *bwc, uint32_t bytes_lost)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (!bwc) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
if (bytes_lost > 0) {
|
|
|
|
LOGGER_DEBUG(bwc->m->log, "BWC lost(1): %d", (int)bytes_lost);
|
|
|
|
bwc->cycle.lost += bytes_lost;
|
|
|
|
send_update(bwc);
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 05:03:48 +08:00
|
|
|
|
|
|
|
void bwc_add_recv(BWController *bwc, uint32_t recv_bytes)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2018-01-20 05:03:48 +08:00
|
|
|
if (!bwc || !recv_bytes) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-08-13 00:09:06 +08:00
|
|
|
++bwc->packet_loss_counted_cycles;
|
2018-01-20 05:03:48 +08:00
|
|
|
bwc->cycle.recv += recv_bytes;
|
2015-10-11 05:54:23 +08:00
|
|
|
send_update(bwc);
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static void send_update(BWController *bwc)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2018-01-20 05:59:42 +08:00
|
|
|
if (bwc->packet_loss_counted_cycles > BWC_AVG_LOSS_OVER_CYCLES_COUNT &&
|
2018-08-18 01:22:18 +08:00
|
|
|
current_time_monotonic(bwc->m->mono_time) - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) {
|
2018-01-20 05:59:42 +08:00
|
|
|
bwc->packet_loss_counted_cycles = 0;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
|
|
|
if (bwc->cycle.lost) {
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u percent: %f %%",
|
2018-02-23 10:22:38 +08:00
|
|
|
(void *)bwc, bwc->cycle.recv, bwc->cycle.lost,
|
|
|
|
(((double) bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0));
|
2018-01-20 05:59:42 +08:00
|
|
|
uint8_t bwc_packet[sizeof(struct BWCMessage) + 1];
|
2020-03-06 00:20:17 +08:00
|
|
|
size_t offset = 0;
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-06 00:20:17 +08:00
|
|
|
bwc_packet[offset] = BWC_PACKET_ID; // set packet ID
|
|
|
|
++offset;
|
|
|
|
|
|
|
|
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.lost);
|
|
|
|
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv);
|
|
|
|
assert(offset == sizeof(bwc_packet));
|
|
|
|
|
|
|
|
if (m_send_custom_lossy_packet(bwc->m, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) {
|
2018-04-08 10:09:42 +08:00
|
|
|
const char *netstrerror = net_new_strerror(net_error());
|
|
|
|
LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s",
|
|
|
|
(unsigned)sizeof(bwc_packet), strerror(errno), netstrerror);
|
|
|
|
net_kill_strerror(netstrerror);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-08-18 01:22:18 +08:00
|
|
|
bwc->cycle.last_sent_timestamp = current_time_monotonic(bwc->m->mono_time);
|
2018-01-20 05:59:42 +08:00
|
|
|
bwc->cycle.lost = 0;
|
|
|
|
bwc->cycle.recv = 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 05:03:48 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
static int on_update(BWController *bwc, const struct BWCMessage *msg)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", (void *)bwc);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/* Peers sent update too soon */
|
2018-08-18 01:22:18 +08:00
|
|
|
if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic(bwc->m->mono_time)) {
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_INFO(bwc->m->log, "%p Rejecting extra update", (void *)bwc);
|
2015-10-11 05:54:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-08-18 01:22:18 +08:00
|
|
|
bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->m->mono_time);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-03-06 00:20:17 +08:00
|
|
|
const uint32_t recv = msg->recv;
|
|
|
|
const uint32_t lost = msg->lost;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-05 23:39:18 +08:00
|
|
|
if (lost && bwc->mcb) {
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u percentage: %f %%", recv, lost,
|
2018-02-23 10:22:38 +08:00
|
|
|
(((double) lost / (recv + lost)) * 100.0));
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc->mcb(bwc, bwc->friend_number,
|
2016-09-05 23:39:18 +08:00
|
|
|
((float) lost / (recv + lost)),
|
2018-08-13 00:09:06 +08:00
|
|
|
bwc->mcb_user_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-20 05:03:48 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (length - 1 != sizeof(struct BWCMessage)) {
|
2015-10-11 06:01:44 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-03-06 00:20:17 +08:00
|
|
|
size_t offset = 1; // Ignore packet id.
|
|
|
|
struct BWCMessage msg;
|
|
|
|
offset += net_unpack_u32(data + offset, &msg.lost);
|
|
|
|
offset += net_unpack_u32(data + offset, &msg.recv);
|
|
|
|
assert(offset == length);
|
|
|
|
|
|
|
|
return on_update((BWController *)object, &msg);
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|