… | |
… | |
52 | |
52 | |
53 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
53 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
55 | |
55 | |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
57 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
57 | #define MAX_SEND_INTERVAL 2. // optimistic? |
58 | |
58 | |
|
|
59 | #define LATENCY_FACTOR 0.5 // RTT * LATENCY_FACTOR == sending rate |
59 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
60 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
60 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | |
63 | |
63 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
64 | #define MAX_DOMAIN_SIZE 200 // 255 is legal limit, but bind doesn't compress well |
64 | // 240 leaves about 4 bytes of server reply data |
65 | // 240 leaves about 4 bytes of server reply data |
65 | // every two request bytes less give room for one reply byte |
66 | // every two request bytes less give room for one reply byte |
66 | |
67 | |
67 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
… | |
… | |
448 | flags = 0; |
449 | flags = 0; |
449 | def_ttl = 0; |
450 | def_ttl = 0; |
450 | seq_cdc = 26; |
451 | seq_cdc = 26; |
451 | req_cdc = 62; |
452 | req_cdc = 62; |
452 | rep_cdc = 0; |
453 | rep_cdc = 0; |
453 | max_size = ntohs (MAX_PKT_SIZE); |
454 | max_size = htons (MAX_PKT_SIZE); |
454 | client = ntohs (clientid); |
455 | client = htons (clientid); |
455 | uid = next_uid++; |
456 | uid = next_uid++; |
456 | |
457 | |
457 | r2 = r3 = r4 = 0; |
458 | r2 = r3 = r4 = 0; |
458 | r4 = r5 = r6 = r7 = 0; |
459 | r4 = r5 = r6 = r7 = 0; |
459 | } |
460 | } |
… | |
… | |
465 | && id3 == 'P' |
466 | && id3 == 'P' |
466 | && id4 == 'E' |
467 | && id4 == 'E' |
467 | && seq_cdc == 26 |
468 | && seq_cdc == 26 |
468 | && req_cdc == 62 |
469 | && req_cdc == 62 |
469 | && rep_cdc == 0 |
470 | && rep_cdc == 0 |
470 | && version == 1 |
471 | && version == 1; |
471 | && max_size == ntohs (MAX_PKT_SIZE); |
|
|
472 | } |
472 | } |
473 | |
473 | |
474 | struct dns_packet : net_packet |
474 | struct dns_packet : net_packet |
475 | { |
475 | { |
476 | u16 id; |
476 | u16 id; |
… | |
… | |
547 | |
547 | |
548 | bool established; |
548 | bool established; |
549 | |
549 | |
550 | tstamp last_received; |
550 | tstamp last_received; |
551 | tstamp last_sent; |
551 | tstamp last_sent; |
552 | double last_latency; |
552 | double min_latency; |
553 | double poll_interval, send_interval; |
553 | double poll_interval, send_interval; |
554 | |
554 | |
555 | vector<dns_rcv *> rcvpq; |
555 | vector<dns_rcv *> rcvpq; |
556 | |
556 | |
557 | byte_stream rcvdq; int rcvseq; |
557 | byte_stream rcvdq; int rcvseq; |
… | |
… | |
731 | rcvseq = sndseq = 0; |
731 | rcvseq = sndseq = 0; |
732 | |
732 | |
733 | last_sent = last_received = 0; |
733 | last_sent = last_received = 0; |
734 | poll_interval = MIN_POLL_INTERVAL; |
734 | poll_interval = MIN_POLL_INTERVAL; |
735 | send_interval = 0.5; // starting rate |
735 | send_interval = 0.5; // starting rate |
736 | last_latency = INITIAL_TIMEOUT; |
736 | min_latency = INITIAL_TIMEOUT; |
737 | } |
737 | } |
738 | |
738 | |
739 | dns_connection::~dns_connection () |
739 | dns_connection::~dns_connection () |
740 | { |
740 | { |
741 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
741 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
… | |
… | |
754 | poll_interval = send_interval; |
754 | poll_interval = send_interval; |
755 | } |
755 | } |
756 | else |
756 | else |
757 | { |
757 | { |
758 | poll_interval *= 1.5; |
758 | poll_interval *= 1.5; |
|
|
759 | |
759 | if (poll_interval > MAX_POLL_INTERVAL) |
760 | if (poll_interval > MAX_POLL_INTERVAL) |
760 | poll_interval = MAX_POLL_INTERVAL; |
761 | poll_interval = MAX_POLL_INTERVAL; |
761 | } |
762 | } |
762 | |
763 | |
763 | rcvpq.push_back (r); |
764 | rcvpq.push_back (r); |
… | |
… | |
892 | pkt [offs++] = 0; pkt [offs++] = 0; |
893 | pkt [offs++] = 0; pkt [offs++] = 0; |
893 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
894 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
894 | |
895 | |
895 | int rdlen_offs = offs += 2; |
896 | int rdlen_offs = offs += 2; |
896 | |
897 | |
897 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
|
|
898 | // bind doesn't compress well, so reduce further by one label length |
|
|
899 | dlen -= qlen; |
|
|
900 | |
|
|
901 | if (dns) |
898 | if (dns) |
902 | { |
899 | { |
|
|
900 | int dlen = ntohs (dns->cfg.max_size) - offs; |
|
|
901 | |
|
|
902 | // bind doesn't compress well, so reduce further by one label length |
|
|
903 | dlen -= qlen; |
|
|
904 | |
903 | // only put data into in-order sequence packets, if |
905 | // only put data into in-order sequence packets, if |
904 | // we receive out-of-order packets we generate empty |
906 | // we receive out-of-order packets we generate empty |
905 | // replies |
907 | // replies |
906 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
908 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
907 | { |
909 | { |
… | |
… | |
1015 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1017 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1016 | dns->send_interval = MAX_SEND_INTERVAL; |
1018 | dns->send_interval = MAX_SEND_INTERVAL; |
1017 | } |
1019 | } |
1018 | else |
1020 | else |
1019 | { |
1021 | { |
1020 | #if 1 |
1022 | #if 0 |
1021 | dns->send_interval *= 0.999; |
1023 | dns->send_interval *= 0.999; |
1022 | #endif |
1024 | #endif |
1023 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
1024 | dns->send_interval = MIN_SEND_INTERVAL; |
|
|
1025 | |
|
|
1026 | // the latency surely puts an upper bound on |
1025 | // the latency surely puts an upper bound on |
1027 | // the minimum send interval |
1026 | // the minimum send interval |
1028 | double latency = NOW - (*i)->sent; |
1027 | double latency = NOW - (*i)->sent; |
|
|
1028 | |
|
|
1029 | if (latency < dns->min_latency) |
1029 | dns->last_latency = latency; |
1030 | dns->min_latency = latency; |
1030 | |
1031 | |
1031 | if (dns->send_interval > latency) |
1032 | if (dns->send_interval > dns->min_latency * LATENCY_FACTOR) |
1032 | dns->send_interval = latency; |
1033 | dns->send_interval = dns->min_latency * LATENCY_FACTOR; |
|
|
1034 | |
|
|
1035 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
1036 | dns->send_interval = MIN_SEND_INTERVAL; |
1033 | } |
1037 | } |
1034 | |
1038 | |
1035 | delete *i; |
1039 | delete *i; |
1036 | dns_sndpq.erase (i); |
1040 | dns_sndpq.erase (i); |
1037 | |
1041 | |
… | |
… | |
1219 | if (!send) |
1223 | if (!send) |
1220 | { |
1224 | { |
1221 | send = r; |
1225 | send = r; |
1222 | |
1226 | |
1223 | r->retry++; |
1227 | r->retry++; |
1224 | r->timeout = NOW + (r->retry * last_latency * 8.); |
1228 | r->timeout = NOW + (r->retry * min_latency * 8.); |
1225 | |
1229 | |
1226 | // the following code changes the query section a bit, forcing |
1230 | // the following code changes the query section a bit, forcing |
1227 | // the forwarder to generate a new request |
1231 | // the forwarder to generate a new request |
1228 | if (r->stdhdr) |
1232 | if (r->stdhdr) |
1229 | { |
1233 | { |
1230 | //printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry);printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
1234 | //printf ("reencoded header for ID %d retry %d:%d:%d (%p)\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
1231 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1235 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1232 | } |
1236 | } |
1233 | } |
1237 | } |
1234 | } |
1238 | } |
1235 | else |
1239 | else |
1236 | NEXT (r->timeout); |
1240 | NEXT (r->timeout); |
1237 | } |
1241 | } |
1238 | |
1242 | |
1239 | if (last_sent + send_interval <= NOW) |
1243 | if (send || (last_sent + send_interval <= NOW)) |
1240 | { |
1244 | { |
1241 | if (!send) |
1245 | if (!send) |
1242 | { |
1246 | { |
1243 | // generate a new packet, if wise |
1247 | // generate a new packet, if wise |
1244 | |
1248 | |
… | |
… | |
1254 | } |
1258 | } |
1255 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1259 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1256 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1260 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1257 | { |
1261 | { |
1258 | //printf ("sending data request etc.\n"); //D |
1262 | //printf ("sending data request etc.\n"); //D |
1259 | if (!snddq.empty ()) |
1263 | if (!snddq.empty () || last_received + 1. > NOW) |
1260 | { |
1264 | { |
1261 | poll_interval = send_interval; |
1265 | poll_interval = send_interval; |
1262 | NEXT (NOW + send_interval); |
1266 | NEXT (NOW + send_interval); |
1263 | } |
1267 | } |
1264 | |
1268 | |
1265 | send = new dns_snd (this); |
1269 | send = new dns_snd (this); |
1266 | send->gen_stream_req (sndseq, snddq); |
1270 | send->gen_stream_req (sndseq, snddq); |
1267 | send->timeout = NOW + last_latency * 8.; |
1271 | send->timeout = NOW + min_latency * 8.; |
1268 | |
1272 | |
1269 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1273 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1270 | } |
1274 | } |
1271 | |
1275 | |
1272 | if (send) |
1276 | if (send && !send->retry) |
1273 | vpn->dns_sndpq.push_back (send); |
1277 | vpn->dns_sndpq.push_back (send); |
1274 | } |
1278 | } |
1275 | |
1279 | |
1276 | if (send) |
1280 | if (send) |
1277 | { |
1281 | { |
… | |
… | |
1282 | } |
1286 | } |
1283 | } |
1287 | } |
1284 | else |
1288 | else |
1285 | NEXT (last_sent + send_interval); |
1289 | NEXT (last_sent + send_interval); |
1286 | |
1290 | |
1287 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", |
1291 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1288 | poll_interval, send_interval, next - NOW, |
1292 | poll_interval, send_interval, next - NOW, |
1289 | vpn->dns_sndpq.size (), snddq.size ()); |
1293 | vpn->dns_sndpq.size (), snddq.size (), |
|
|
1294 | rcvpq.size ()); |
1290 | |
1295 | |
1291 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1296 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
1292 | if (next < NOW + 0.0001) |
1297 | if (next < NOW + 0.0001) |
1293 | next = NOW + 0.1; |
1298 | next = NOW + 0.1; |
1294 | |
1299 | |