bugs never end
[swift-upb.git] / sendrecv.cpp
1 /*
2  *  sendrecv.cpp
3  *  most of the swift's state machine
4  *
5  *  Created by Victor Grishchenko on 3/6/09.
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9 #include "swift.h"
10 #include <algorithm>  // kill it
11
12 using namespace swift;
13 using namespace std;
14
15 /*
16  TODO  25 Oct 18:55
17  - range: ALL
18  - randomized testing of advanced ops (new testcase)
19  */
20
21 void    Channel::AddPeakHashes (Datagram& dgram) {
22     for(int i=0; i<file().peak_count(); i++) {
23         bin64_t peak = file().peak(i);
24         dgram.Push8(SWIFT_HASH);
25         dgram.Push32((uint32_t)peak);
26         dgram.PushHash(file().peak_hash(i));
27         //DLOG(INFO)<<"#"<<id<<" +pHASH"<<file().peak(i);
28         dprintf("%s #%u +phash %s\n",tintstr(),id_,peak.str());
29     }
30 }
31
32
33 void    Channel::AddUncleHashes (Datagram& dgram, bin64_t pos) {
34     bin64_t peak = file().peak_for(pos);
35     while (pos!=peak && ((NOW&3)==3 || !data_out_cap_.within(pos.parent())) &&
36             ack_in_.get(pos.parent())==binmap_t::EMPTY  ) {
37         bin64_t uncle = pos.sibling();
38         dgram.Push8(SWIFT_HASH);
39         dgram.Push32((uint32_t)uncle);
40         dgram.PushHash( file().hash(uncle) );
41         //DLOG(INFO)<<"#"<<id<<" +uHASH"<<uncle;
42         dprintf("%s #%u +hash %s\n",tintstr(),id_,uncle.str());
43         pos = pos.parent();
44     }
45 }
46
47
48 bin64_t           Channel::ImposeHint () {
49     uint64_t twist = peer_channel_id_;  // got no hints, send something randomly
50     twist &= file().peak(0); // FIXME may make it semi-seq here
51     file().ack_out().twist(twist);
52     ack_in_.twist(twist);
53     bin64_t my_pick =
54         file().ack_out().find_filtered(ack_in_,bin64_t::ALL,binmap_t::FILLED);
55     while (my_pick.width()>max(1,(int)cwnd_))
56         my_pick = my_pick.left();
57     file().ack_out().twist(0);
58     ack_in_.twist(0);
59     return my_pick.twisted(twist);
60 }
61
62
63 bin64_t        Channel::DequeueHint () {
64     if (hint_in_.empty() && last_recv_time_>NOW-rtt_avg_-TINT_SEC) {
65         bin64_t my_pick = ImposeHint(); // FIXME move to the loop
66         if (my_pick!=bin64_t::NONE) {
67             hint_in_.push_back(my_pick);
68             dprintf("%s #%u *hint %s\n",tintstr(),id_,my_pick.str());
69         }
70     }
71     bin64_t send = bin64_t::NONE;
72     while (!hint_in_.empty() && send==bin64_t::NONE) {
73         bin64_t hint = hint_in_.front().bin;
74         tint time = hint_in_.front().time;
75         hint_in_.pop_front();
76         while (!hint.is_base()) { // FIXME optimize; possible attack
77             hint_in_.push_front(tintbin(time,hint.right()));
78             hint = hint.left();
79         }
80         //if (time < NOW-TINT_SEC*3/2 )
81         //    continue;  bad idea
82         if (ack_in_.get(hint)!=binmap_t::FILLED)
83             send = hint;
84     }
85     uint64_t mass = 0;
86     for(int i=0; i<hint_in_.size(); i++)
87         mass += hint_in_[i].bin.width();
88     dprintf("%s #%u dequeued %s [%lli]\n",tintstr(),id_,send.str(),mass);
89     return send;
90 }
91
92
93 void    Channel::AddHandshake (Datagram& dgram) {
94     if (!peer_channel_id_) { // initiating
95         dgram.Push8(SWIFT_HASH);
96         dgram.Push32(bin64_t::ALL32);
97         dgram.PushHash(file().root_hash());
98         dprintf("%s #%u +hash ALL %s\n",
99                 tintstr(),id_,file().root_hash().hex().c_str());
100     }
101     dgram.Push8(SWIFT_HANDSHAKE);
102     int encoded = EncodeID(id_);
103     dgram.Push32(encoded);
104     dprintf("%s #%u +hs %x\n",tintstr(),id_,encoded);
105     have_out_.clear();
106     AddHave(dgram);
107 }
108
109
110 void    Channel::Send () {
111     Datagram dgram(socket_,peer());
112     dgram.Push32(peer_channel_id_);
113     bin64_t data = bin64_t::NONE;
114     if ( is_established() ) {
115         // FIXME: seeder check
116         AddAck(dgram);
117         if (!file().is_complete())
118             AddHint(dgram);
119         AddPex(dgram);
120         TimeoutDataOut();
121         data = AddData(dgram);
122     } else {
123         AddHandshake(dgram);
124         AddAck(dgram);
125     }
126     dprintf("%s #%u sent %ib %s:%x\n",
127             tintstr(),id_,dgram.size(),peer().str(),peer_channel_id_);
128     if (dgram.size()==4) {// only the channel id; bare keep-alive
129         data = bin64_t::ALL;
130     }
131     if (dgram.Send()==-1)
132         print_error("can't send datagram");
133     last_send_time_ = NOW;
134     sent_since_recv_++;
135     dgrams_sent_++;
136 }
137
138
139 void    Channel::AddHint (Datagram& dgram) {
140
141     tint plan_for = max(TINT_SEC,rtt_avg_*4);
142
143     tint timed_out = NOW - plan_for*2;
144     while ( !hint_out_.empty() && hint_out_.front().time < timed_out ) {
145         hint_out_size_ -= hint_out_.front().bin.width();
146         hint_out_.pop_front();
147     }
148
149     int plan_pck = max ( (tint)1, plan_for / dip_avg_ );
150
151     if ( hint_out_size_ < plan_pck ) {
152
153         int diff = plan_pck - hint_out_size_; // TODO: aggregate
154         bin64_t hint = transfer().picker().Pick(ack_in_,diff,NOW+plan_for*2);
155
156         if (hint!=bin64_t::NONE) {
157             dgram.Push8(SWIFT_HINT);
158             dgram.Push32(hint);
159             dprintf("%s #%u +hint %s [%lli]\n",tintstr(),id_,hint.str(),hint_out_size_);
160             hint_out_.push_back(hint);
161             hint_out_size_ += hint.width();
162         } else
163             dprintf("%s #%u Xhint\n",tintstr(),id_);
164
165     }
166 }
167
168
169 bin64_t        Channel::AddData (Datagram& dgram) {
170
171     if (!file().size()) // know nothing
172         return bin64_t::NONE;
173
174     bin64_t tosend = bin64_t::NONE;
175     tint luft = send_interval_>>4; // may wake up a bit earlier
176     if (data_out_.size()<cwnd_ &&
177             last_data_out_time_+send_interval_<=NOW+luft) {
178         tosend = DequeueHint();
179         if (tosend==bin64_t::NONE) {
180             dprintf("%s #%u sendctrl no idea what to send\n",tintstr(),id_);
181             if (send_control_!=KEEP_ALIVE_CONTROL)
182                 SwitchSendControl(KEEP_ALIVE_CONTROL);
183         }
184     } else
185         dprintf("%s #%u sendctrl wait cwnd %f data_out %i next %s\n",
186                 tintstr(),id_,cwnd_,data_out_.size(),tintstr(last_data_out_time_+NOW-send_interval_));
187
188     if (tosend==bin64_t::NONE)// && (last_data_out_time_>NOW-TINT_SEC || data_out_.empty()))
189         return bin64_t::NONE; // once in a while, empty data is sent just to check rtt FIXED
190
191     if (ack_in_.is_empty() && file().size())
192         AddPeakHashes(dgram);
193     AddUncleHashes(dgram,tosend);
194     if (!ack_in_.is_empty()) // TODO: cwnd_>1
195         data_out_cap_ = tosend;
196
197     if (dgram.size()>254) {
198         dgram.Send(); // kind of fragmentation
199         dgram.Push32(peer_channel_id_);
200     }
201
202     dgram.Push8(SWIFT_DATA);
203     dgram.Push32(tosend.to32());
204
205     uint8_t buf[1024];
206     size_t r = pread(file().file_descriptor(),buf,1024,tosend.base_offset()<<10);
207     // TODO: corrupted data, retries, caching
208     if (r<0) {
209         print_error("error on reading");
210         return bin64_t::NONE;
211     }
212     assert(dgram.space()>=r+4+1);
213     dgram.Push(buf,r);
214
215     last_data_out_time_ = NOW;
216     data_out_.push_back(tosend);
217     dprintf("%s #%u +data %s\n",tintstr(),id_,tosend.str());
218
219     return tosend;
220 }
221
222
223 void    Channel::AddAck (Datagram& dgram) {
224     if (data_in_==tintbin())
225         return;
226     dgram.Push8(SWIFT_ACK);
227     dgram.Push32(data_in_.bin.to32()); // FIXME not cover
228     dgram.Push64(data_in_.time); // FIXME 32
229     have_out_.set(data_in_.bin);
230     dprintf("%s #%u +ack %s %s\n",
231         tintstr(),id_,data_in_.bin.str(),tintstr(data_in_.time));
232     if (data_in_.bin.layer()>2)
233         data_in_dbl_ = data_in_.bin;
234     data_in_ = tintbin();
235 }
236
237
238 void    Channel::AddHave (Datagram& dgram) {
239     if (data_in_dbl_!=bin64_t::NONE) { // TODO: do redundancy better
240         dgram.Push8(SWIFT_HAVE);
241         dgram.Push32(data_in_dbl_.to32());
242         data_in_dbl_=bin64_t::NONE;
243     }
244     for(int count=0; count<4; count++) {
245         bin64_t ack = file().ack_out().find_filtered // FIXME: do rotating queue
246             (have_out_, bin64_t::ALL, binmap_t::FILLED);
247         if (ack==bin64_t::NONE)
248             break;
249         ack = file().ack_out().cover(ack);
250         have_out_.set(ack);
251         dgram.Push8(SWIFT_HAVE);
252         dgram.Push32(ack.to32());
253         dprintf("%s #%u +have %s\n",tintstr(),id_,ack.str());
254     }
255 }
256
257
258 void    Channel::Recv (Datagram& dgram) {
259     dprintf("%s #%u recvd %ib\n",tintstr(),id_,dgram.size()+4);
260     dgrams_rcvd_++;
261     if (last_send_time_ && rtt_avg_==TINT_SEC && dev_avg_==0) {
262         rtt_avg_ = NOW - last_send_time_;
263         dev_avg_ = rtt_avg_;
264         dip_avg_ = rtt_avg_;
265         dprintf("%s #%u sendctrl rtt init %lli\n",tintstr(),id_,rtt_avg_);
266     }
267     bin64_t data = dgram.size() ? bin64_t::NONE : bin64_t::ALL;
268     while (dgram.size()) {
269         uint8_t type = dgram.Pull8();
270         switch (type) {
271             case SWIFT_HANDSHAKE: OnHandshake(dgram); break;
272             case SWIFT_DATA:      data=OnData(dgram); break;
273             case SWIFT_HAVE:      OnHave(dgram); break;
274             case SWIFT_ACK:       OnAck(dgram); break;
275             case SWIFT_HASH:      OnHash(dgram); break;
276             case SWIFT_HINT:      OnHint(dgram); break;
277             case SWIFT_PEX_ADD:   OnPex(dgram); break;
278             default:
279                 eprintf("%s #%u ?msg id unknown %i\n",tintstr(),id_,(int)type);
280                 return;
281         }
282     }
283     last_recv_time_ = NOW;
284     sent_since_recv_ = 0;
285 }
286
287
288 void    Channel::OnHash (Datagram& dgram) {
289     bin64_t pos = dgram.Pull32();
290     Sha1Hash hash = dgram.PullHash();
291     file().OfferHash(pos,hash);
292     dprintf("%s #%u -hash %s\n",tintstr(),id_,pos.str());
293 }
294
295
296 void    Channel::CleanHintOut (bin64_t pos) {
297     int hi = 0;
298     while (hi<hint_out_.size() && !pos.within(hint_out_[hi].bin))
299         hi++;
300     if (hi==hint_out_.size())
301         return; // something not hinted or hinted in far past
302     while (hi--) { // removing likely snubbed hints
303         hint_out_size_ -= hint_out_.front().bin.width();
304         hint_out_.pop_front();
305     }
306     while (hint_out_.front().bin!=pos) {
307         tintbin f = hint_out_.front();
308         f.bin = f.bin.towards(pos);
309         hint_out_.front().bin = f.bin.sibling();
310         hint_out_.push_front(f);
311     }
312     hint_out_.pop_front();
313     hint_out_size_--;
314 }
315
316
317 bin64_t Channel::OnData (Datagram& dgram) {  // TODO: HAVE NONE for corrupted data
318     bin64_t pos = dgram.Pull32();
319     uint8_t *data;
320     int length = dgram.Pull(&data,1024);
321     bool ok = (pos==bin64_t::NONE) || file().OfferData(pos, (char*)data, length) ;
322     dprintf("%s #%u %cdata %s\n",tintstr(),id_,ok?'-':'!',pos.str());
323     data_in_ = tintbin(NOW,bin64_t::NONE);
324     if (!ok)
325         return bin64_t::NONE;
326     data_in_.bin = pos;
327     if (pos!=bin64_t::NONE) {
328         if (last_data_in_time_) {
329             tint dip = NOW - last_data_in_time_;
330             dip_avg_ = ( dip_avg_*3 + dip ) >> 2;
331         }
332         last_data_in_time_ = NOW;
333     }
334     CleanHintOut(pos);
335     return pos;
336 }
337
338
339 void    Channel::OnAck (Datagram& dgram) {
340     bin64_t ackd_pos = dgram.Pull32();
341     tint peer_time_ = dgram.Pull64(); // FIXME 32
342     // FIXME FIXME: wrap around here
343     if (ackd_pos==bin64_t::NONE)
344         return; // likely, brocken packet / insufficient hashes
345     if (file().size() && ackd_pos.base_offset()>=file().packet_size()) {
346         eprintf("invalid ack: %s\n",ackd_pos.str());
347         return;
348     }
349     ack_in_.set(ackd_pos);
350     int di = 0, ri = 0;
351     // find an entry for the send (data out) event
352     while (  di<data_out_.size() && ( data_out_[di]==tintbin() ||
353            !data_out_[di].bin.within(ackd_pos) )  )
354         di++;
355     // FUTURE: delayed acks
356     // rule out retransmits
357     while (  ri<data_out_tmo_.size() && !data_out_tmo_[ri].bin.within(ackd_pos) )
358         ri++;
359     dprintf("%s #%u %cack %s %lli\n",tintstr(),id_,
360             di==data_out_.size()?'?':'-',ackd_pos.str(),peer_time_);
361     if (di!=data_out_.size() && ri==data_out_tmo_.size()) { // not a retransmit
362             // round trip time calculations
363         tint rtt = NOW-data_out_[di].time;
364         rtt_avg_ = (rtt_avg_*7 + rtt) >> 3;
365         dev_avg_ = ( dev_avg_*3 + abs(rtt-rtt_avg_) ) >> 2;
366         assert(data_out_[di].time!=TINT_NEVER);
367             // one-way delay calculations
368         tint owd = peer_time_ - data_out_[di].time;
369         owd_cur_bin_ = (owd_cur_bin_+1) & 3;
370         owd_current_[owd_cur_bin_] = owd;
371         if ( owd_min_bin_start_+TINT_SEC*30 < NOW ) {
372             owd_min_bin_start_ = NOW;
373             owd_min_bin_ = (owd_min_bin_+1) & 3;
374             owd_min_bins_[owd_min_bin_] = TINT_NEVER;
375         }
376         if (owd_min_bins_[owd_min_bin_]>owd)
377             owd_min_bins_[owd_min_bin_] = owd;
378         dprintf("%s #%u sendctrl rtt %lli dev %lli based on %s\n",
379                 tintstr(),id_,rtt_avg_,dev_avg_,data_out_[di].bin.str());
380         ack_rcvd_recent_++;
381         data_out_[di]=tintbin();
382     }
383     // early loss detection by packet reordering
384     for (int re=0; re<di-MAX_REORDERING; re++) {
385         if (data_out_[re]==tintbin())
386             continue;
387         ack_not_rcvd_recent_++;
388         data_out_tmo_.push_back(data_out_.front().bin);
389         dprintf("%s #%u Rdata %s\n",tintstr(),id_,data_out_.front().bin.str());
390         data_out_cap_ = bin64_t::ALL;
391         data_out_[re] = tintbin();
392     }
393     // clear zeroed items
394     while (!data_out_.empty() && ( data_out_.front()==tintbin() ||
395             ack_in_.is_filled(data_out_.front().bin) ) )
396         data_out_.pop_front();
397     assert(data_out_.empty() || data_out_.front().time!=TINT_NEVER);
398 }
399
400
401 void Channel::TimeoutDataOut ( ) {
402     // losses: timeouted packets
403     tint timeout = NOW - ack_timeout();
404     while (!data_out_.empty() && 
405         ( data_out_.front().time<timeout || data_out_.front()==tintbin() ) ) {
406         if (data_out_.front()!=tintbin() && ack_in_.is_empty(data_out_.front().bin)) {
407             ack_not_rcvd_recent_++;
408             data_out_cap_ = bin64_t::ALL;
409             data_out_tmo_.push_back(data_out_.front().bin);
410             dprintf("%s #%u Tdata %s\n",tintstr(),id_,data_out_.front().bin.str());
411         }
412         data_out_.pop_front();
413     }
414     // clear retransmit queue of older items
415     while (!data_out_tmo_.empty() && data_out_tmo_.front().time<NOW-MAX_POSSIBLE_RTT)
416         data_out_tmo_.pop_front();
417 }
418
419
420 void Channel::OnHave (Datagram& dgram) {
421     bin64_t ackd_pos = dgram.Pull32();
422     if (ackd_pos==bin64_t::NONE)
423         return; // wow, peer has hashes
424     ack_in_.set(ackd_pos);
425     dprintf("%s #%u -have %s\n",tintstr(),id_,ackd_pos.str());
426 }
427
428
429 void    Channel::OnHint (Datagram& dgram) {
430     bin64_t hint = dgram.Pull32();
431     // FIXME: wake up here
432     hint_in_.push_back(hint);
433     dprintf("%s #%u -hint %s\n",tintstr(),id_,hint.str());
434 }
435
436
437 void Channel::OnHandshake (Datagram& dgram) {
438     peer_channel_id_ = dgram.Pull32();
439     dprintf("%s #%u -hs %x\n",tintstr(),id_,peer_channel_id_);
440     // self-connection check
441     if (!SELF_CONN_OK) {
442         uint32_t try_id = DecodeID(peer_channel_id_);
443         if (channel(try_id) && !channel(try_id)->peer_channel_id_) {
444             peer_channel_id_ = 0;
445             Close();
446             return; // this is a self-connection
447         }
448     }
449     // FUTURE: channel forking
450 }
451
452
453 void Channel::OnPex (Datagram& dgram) {
454     uint32_t ipv4 = dgram.Pull32();
455     uint16_t port = dgram.Pull16();
456     Address addr(ipv4,port);
457     dprintf("%s #%u -pex %s\n",tintstr(),id_,addr.str());
458     transfer().OnPexIn(addr);
459 }
460
461
462 void    Channel::AddPex (Datagram& dgram) {
463     int chid = transfer().RevealChannel(pex_out_);
464     if (chid==-1 || chid==id_)
465         return;
466     Address a = channels[chid]->peer();
467     dgram.Push8(SWIFT_PEX_ADD);
468     dgram.Push32(a.ipv4());
469     dgram.Push16(a.port());
470     dprintf("%s #%u +pex %s\n",tintstr(),id_,a.str());
471 }
472
473
474 Channel*    Channel::RecvDatagram (int socket) {
475     Datagram data(socket);
476     data.Recv();
477     const Address& addr = data.address();
478 #define return_log(...) { printf(__VA_ARGS__); return NULL; }
479     if (data.size()<4)
480         return_log("datagram shorter than 4 bytes %s\n",addr.str());
481     uint32_t mych = data.Pull32();
482     Sha1Hash hash;
483     Channel* channel = NULL;
484     if (!mych) { // handshake initiated
485         if (data.size()<1+4+1+4+Sha1Hash::SIZE)
486             return_log ("%s #0 incorrect size %i initial handshake packet %s\n",
487                         tintstr(),data.size(),addr.str());
488         uint8_t hashid = data.Pull8();
489         if (hashid!=SWIFT_HASH)
490             return_log ("%s #0 no hash in the initial handshake %s\n",
491                         tintstr(),addr.str());
492         bin64_t pos = data.Pull32();
493         if (pos!=bin64_t::ALL)
494             return_log ("%s #0 that is not the root hash %s\n",tintstr(),addr.str());
495         hash = data.PullHash();
496         FileTransfer* file = FileTransfer::Find(hash);
497         if (!file)
498             return_log ("%s #0 hash %s unknown, no such file %s\n",tintstr(),hash.hex().c_str(),addr.str());
499         dprintf("%s #0 -hash ALL %s\n",tintstr(),hash.hex().c_str());
500         for(binqueue::iterator i=file->hs_in_.begin(); i!=file->hs_in_.end(); i++)
501             if (channels[*i] && channels[*i]->peer_==data.address() &&
502                 channels[*i]->last_recv_time_>NOW-TINT_SEC*2)
503                 return_log("%s #0 have a channel already to %s\n",tintstr(),addr.str());
504         channel = new Channel(file, socket, data.address());
505     } else {
506         mych = DecodeID(mych);
507         if (mych>=channels.size())
508             return_log("%s invalid channel #%u, %s\n",tintstr(),mych,addr.str());
509         channel = channels[mych];
510         if (!channel)
511             return_log ("%s #%u is already closed\n",tintstr(),mych,addr.str());
512         if (channel->peer() != addr)
513             return_log ("%s #%u invalid peer address %s!=%s\n",
514                         tintstr(),mych,channel->peer().str(),addr.str());
515         channel->own_id_mentioned_ = true;
516     }
517     //dprintf("recvd %i bytes for %i\n",data.size(),channel->id);
518     channel->Recv(data);
519     return channel;
520 }
521
522
523 void    Channel::Loop (tint howlong) {
524
525     tint limit = Datagram::Time() + howlong;
526
527     do {
528
529         tint send_time(TINT_NEVER);
530         Channel* sender(NULL);
531         while (!sender && !send_queue.is_empty()) { // dequeue
532             tintbin next = send_queue.pop();
533             sender = channel((int)next.bin);
534             send_time = next.time;
535             if (sender && sender->next_send_time_!=send_time &&
536                      sender->next_send_time_!=TINT_NEVER )
537                 sender = NULL; // it was a stale entry
538         }
539
540         if ( sender!=NULL && send_time<=NOW ) { // it's time
541
542             dprintf("%s #%u sch_send %s\n",tintstr(),sender->id(),
543                     tintstr(send_time));
544             sender->Send();
545             sender->Reschedule();
546
547         } else {  // it's too early, wait
548
549             tint towait = min(limit,send_time) - NOW;
550             dprintf("%s #0 waiting %lliusec\n",tintstr(),towait);
551             int rd = Datagram::Wait(socket_count,sockets,towait);
552             if (rd!=INVALID_SOCKET) { // in meantime, received something
553                 Channel* receiver = RecvDatagram(rd);
554                 if (receiver) // receiver's state may have changed
555                     receiver->Reschedule();
556             }
557             if (sender)  // get back to that later
558                 send_queue.push(tintbin(send_time,sender->id()));
559
560         }
561
562     } while (NOW<limit);
563
564 }
565
566
567 void Channel::Close () {
568     this->SwitchSendControl(CLOSE_CONTROL);
569 }
570
571
572 void Channel::Reschedule () {
573     next_send_time_ = NextSendTime();
574     if (next_send_time_!=TINT_NEVER) {
575         assert(next_send_time_<NOW+TINT_MIN);
576         send_queue.push(tintbin(next_send_time_,id_));
577         dprintf("%s #%u requeue for %s\n",tintstr(),id_,tintstr(next_send_time_));
578     } else {
579         dprintf("%s #%u closed\n",tintstr(),id_);
580         delete this;
581     }
582 }