1 /* SPDX-License-Identifier: GPL-3.0-or-later
2  * Copyright © 2016-2018 The TokTok team.
3  * Copyright © 2013-2015 Tox project.
4  */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8 
9 #include "bwcontroller.h"
10 
11 #include <assert.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "ring_buffer.h"
17 
18 #include "../toxcore/logger.h"
19 #include "../toxcore/mono_time.h"
20 #include "../toxcore/util.h"
21 
22 #define BWC_PACKET_ID 196
23 #define BWC_SEND_INTERVAL_MS 950     // 0.95s
24 #define BWC_AVG_PKT_COUNT 20
25 #define BWC_AVG_LOSS_OVER_CYCLES_COUNT 30
26 
27 typedef struct BWCCycle {
28     uint32_t last_recv_timestamp; /* Last recv update time stamp */
29     uint32_t last_sent_timestamp; /* Last sent update time stamp */
30     uint32_t last_refresh_timestamp; /* Last refresh time stamp */
31 
32     uint32_t lost;
33     uint32_t recv;
34 } BWCCycle;
35 
36 typedef struct BWCRcvPkt {
37     uint32_t packet_length_array[BWC_AVG_PKT_COUNT];
38     RingBuffer *rb;
39 } BWCRcvPkt;
40 
41 struct BWController_s {
42     m_cb *mcb;
43     void *mcb_user_data;
44 
45     Messenger *m;
46     Tox *tox;
47     uint32_t friend_number;
48 
49     BWCCycle cycle;
50 
51     BWCRcvPkt rcvpkt; /* To calculate average received packet (this means split parts, not the full message!) */
52 
53     uint32_t packet_loss_counted_cycles;
54     Mono_Time *bwc_mono_time;
55 };
56 
57 struct BWCMessage {
58     uint32_t lost;
59     uint32_t recv;
60 };
61 
62 static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object);
63 static int bwc_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length);
64 static void send_update(BWController *bwc);
65 
bwc_new(Messenger * m,Tox * tox,uint32_t friendnumber,m_cb * mcb,void * mcb_user_data,Mono_Time * bwc_mono_time)66 BWController *bwc_new(Messenger *m, Tox *tox, uint32_t friendnumber, m_cb *mcb, void *mcb_user_data,
67                       Mono_Time *bwc_mono_time)
68 {
69     BWController *retu = (BWController *)calloc(sizeof(struct BWController_s), 1);
70 
71     if (retu == nullptr) {
72         return nullptr;
73     }
74 
75     LOGGER_DEBUG(m->log, "Creating bandwidth controller");
76     retu->mcb = mcb;
77     retu->mcb_user_data = mcb_user_data;
78     retu->m = m;
79     retu->friend_number = friendnumber;
80     retu->bwc_mono_time = bwc_mono_time;
81     uint64_t now = current_time_monotonic(bwc_mono_time);
82     retu->cycle.last_sent_timestamp = now;
83     retu->cycle.last_refresh_timestamp = now;
84     retu->tox = tox;
85     retu->rcvpkt.rb = rb_new(BWC_AVG_PKT_COUNT);
86     retu->cycle.lost = 0;
87     retu->cycle.recv = 0;
88     retu->packet_loss_counted_cycles = 0;
89 
90     /* Fill with zeros */
91     for (int i = 0; i < BWC_AVG_PKT_COUNT; ++i) {
92         rb_write(retu->rcvpkt.rb, &retu->rcvpkt.packet_length_array[i]);
93     }
94 
95     m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu);
96     return retu;
97 }
98 
bwc_kill(BWController * bwc)99 void bwc_kill(BWController *bwc)
100 {
101     if (!bwc) {
102         return;
103     }
104 
105     m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, nullptr, nullptr);
106     rb_kill(bwc->rcvpkt.rb);
107     free(bwc);
108 }
109 
bwc_add_lost(BWController * bwc,uint32_t bytes_lost)110 void bwc_add_lost(BWController *bwc, uint32_t bytes_lost)
111 {
112     if (!bwc) {
113         return;
114     }
115 
116     if (bytes_lost > 0) {
117         LOGGER_DEBUG(bwc->m->log, "BWC lost(1): %d", (int)bytes_lost);
118         bwc->cycle.lost += bytes_lost;
119         send_update(bwc);
120     }
121 }
122 
bwc_add_recv(BWController * bwc,uint32_t recv_bytes)123 void bwc_add_recv(BWController *bwc, uint32_t recv_bytes)
124 {
125     if (!bwc || !recv_bytes) {
126         return;
127     }
128 
129     ++bwc->packet_loss_counted_cycles;
130     bwc->cycle.recv += recv_bytes;
131     send_update(bwc);
132 }
133 
send_update(BWController * bwc)134 static void send_update(BWController *bwc)
135 {
136     if (bwc->packet_loss_counted_cycles > BWC_AVG_LOSS_OVER_CYCLES_COUNT &&
137             current_time_monotonic(bwc->bwc_mono_time) - bwc->cycle.last_sent_timestamp > BWC_SEND_INTERVAL_MS) {
138         bwc->packet_loss_counted_cycles = 0;
139 
140         if (bwc->cycle.lost) {
141             LOGGER_DEBUG(bwc->m->log, "%p Sent update rcv: %u lost: %u percent: %f %%",
142                          (void *)bwc, bwc->cycle.recv, bwc->cycle.lost,
143                          (((double) bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0));
144             uint8_t bwc_packet[sizeof(struct BWCMessage) + 1];
145             size_t offset = 0;
146 
147             bwc_packet[offset] = BWC_PACKET_ID; // set packet ID
148             ++offset;
149 
150             offset += net_pack_u32(bwc_packet + offset, bwc->cycle.lost);
151             offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv);
152             assert(offset == sizeof(bwc_packet));
153 
154             if (bwc_send_custom_lossy_packet(bwc->tox, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) {
155                 const char *netstrerror = net_new_strerror(net_error());
156                 LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s",
157                                (unsigned)sizeof(bwc_packet), strerror(errno), netstrerror);
158                 net_kill_strerror(netstrerror);
159             }
160         }
161 
162         bwc->cycle.last_sent_timestamp = current_time_monotonic(bwc->bwc_mono_time);
163         bwc->cycle.lost = 0;
164         bwc->cycle.recv = 0;
165     }
166 }
167 
on_update(BWController * bwc,const struct BWCMessage * msg)168 static int on_update(BWController *bwc, const struct BWCMessage *msg)
169 {
170     LOGGER_DEBUG(bwc->m->log, "%p Got update from peer", (void *)bwc);
171 
172     /* Peers sent update too soon */
173     if (bwc->cycle.last_recv_timestamp + BWC_SEND_INTERVAL_MS > current_time_monotonic(bwc->bwc_mono_time)) {
174         LOGGER_INFO(bwc->m->log, "%p Rejecting extra update", (void *)bwc);
175         return -1;
176     }
177 
178     bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->bwc_mono_time);
179 
180     const uint32_t recv = msg->recv;
181     const uint32_t lost = msg->lost;
182 
183     if (lost && bwc->mcb) {
184         LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u percentage: %f %%", recv, lost,
185                      (((double) lost / (recv + lost)) * 100.0));
186         bwc->mcb(bwc, bwc->friend_number,
187                  ((float) lost / (recv + lost)),
188                  bwc->mcb_user_data);
189     }
190 
191     return 0;
192 }
193 
194 /*
195  * return -1 on failure, 0 on success
196  *
197  */
bwc_send_custom_lossy_packet(Tox * tox,int32_t friendnumber,const uint8_t * data,uint32_t length)198 static int bwc_send_custom_lossy_packet(Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length)
199 {
200     Tox_Err_Friend_Custom_Packet error;
201     tox_friend_send_lossy_packet(tox, friendnumber, data, (size_t)length, &error);
202 
203     if (error == TOX_ERR_FRIEND_CUSTOM_PACKET_OK) {
204         return 0;
205     }
206 
207     return -1;
208 }
209 
bwc_handle_data(Messenger * m,uint32_t friendnumber,const uint8_t * data,uint16_t length,void * object)210 static int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object)
211 {
212     if (length - 1 != sizeof(struct BWCMessage)) {
213         return -1;
214     }
215 
216     size_t offset = 1;  // Ignore packet id.
217     struct BWCMessage msg;
218     offset += net_unpack_u32(data + offset, &msg.lost);
219     offset += net_unpack_u32(data + offset, &msg.recv);
220     assert(offset == length);
221 
222     return on_update((BWController *)object, &msg);
223 }
224