X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Fnet_packet.c;h=96f8f10e7b645a5364687671b357146cfc7f6544;hp=76b3c5e93ca0b4c248927d702287fbb55ec193af;hb=f0afde0467443969eb408090d6b8ee542768ee33;hpb=ce7079f4af3157eaef514d6d160933a016b2ab62 diff --git a/src/net_packet.c b/src/net_packet.c index 76b3c5e9..96f8f10e 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -95,6 +95,7 @@ static void udp_probe_timeout_handler(void *data) { logger(DEBUG_TRAFFIC, LOG_INFO, "Too much time has elapsed since last UDP ping response from %s (%s), stopping UDP communication", n->name, n->hostname); n->status.udp_confirmed = false; + n->maxrecentlen = 0; n->mtuprobes = 0; n->minmtu = 0; n->maxmtu = MTU; @@ -117,10 +118,6 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { uint8_t *data = DATA(packet); *data++ = 2; uint16_t len16 = htons(len); memcpy(data, &len16, 2); data += 2; - struct timeval now; - gettimeofday(&now, NULL); - uint32_t sec = htonl(now.tv_sec); memcpy(data, &sec, 4); data += 4; - uint32_t usec = htonl(now.tv_usec); memcpy(data, &usec, 4); data += 4; packet->len = MIN_PROBE_SIZE; } else { /* Legacy protocol: n won't understand type 2 probe replies. */ @@ -155,47 +152,24 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { timeout_add(&n->udp_ping_timeout, &udp_probe_timeout_handler, n, &(struct timeval){udp_discovery_timeout, 0}); } - if(probelen >= n->maxmtu + 1) { + if(probelen > n->maxmtu) { logger(DEBUG_TRAFFIC, LOG_INFO, "Increase in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname); + n->minmtu = probelen; n->maxmtu = MTU; /* Set mtuprobes to 1 so that try_mtu() doesn't reset maxmtu */ n->mtuprobes = 1; return; + } else if(n->mtuprobes < 0 && probelen == n->maxmtu) { + /* We got a maxmtu sized packet, confirming the PMTU is still valid. */ + n->mtuprobes = -1; } /* If applicable, raise the minimum supported MTU */ - if(probelen > n->maxmtu) - probelen = n->maxmtu; if(n->minmtu < probelen) { n->minmtu = probelen; try_fix_mtu(n); } - - /* Calculate RTT. - The RTT is the time between the MTU probe burst was sent and the first - reply is received. - */ - - struct timeval now, diff; - gettimeofday(&now, NULL); - timersub(&now, &n->probe_time, &diff); - - struct timeval probe_timestamp = now; - if (DATA(packet)[0] == 2 && packet->len >= 11) { - uint32_t sec; memcpy(&sec, DATA(packet) + 3, 4); - uint32_t usec; memcpy(&usec, DATA(packet) + 7, 4); - probe_timestamp.tv_sec = ntohl(sec); - probe_timestamp.tv_usec = ntohl(usec); - } - - n->probe_counter++; - - if(n->probe_counter == 1) { - n->rtt = diff.tv_sec + diff.tv_usec * 1e-6; - n->probe_time = probe_timestamp; - logger(DEBUG_TRAFFIC, LOG_DEBUG, "%s (%s) RTT %.2f ms, rx packet loss %.2f %%", n->name, n->hostname, n->rtt * 1e3, n->packetloss * 1e2); - } } } @@ -414,6 +388,9 @@ static bool receive_udppacket(node_t *n, vpn_packet_t *inpkt) { origlen -= MTU/64 + 20; } + if(inpkt->len > n->maxrecentlen) + n->maxrecentlen = inpkt->len; + inpkt->priority = 0; if(!DATA(inpkt)[12] && !DATA(inpkt)[13]) @@ -989,6 +966,7 @@ static void try_mtu(node_t *n) { return; if(udp_discovery && !n->status.udp_confirmed) { + n->maxrecentlen = 0; n->mtuprobes = 0; n->minmtu = 0; n->maxmtu = MTU; @@ -997,26 +975,43 @@ static void try_mtu(node_t *n) { /* mtuprobes == 0..19: initial discovery, send bursts with 1 second interval, mtuprobes++ mtuprobes == 20: fix MTU, and go to -1 - mtuprobes == -1: send one >maxmtu probe every pingtimeout */ + mtuprobes == -1: send one maxmtu and one maxmtu+1 probe every pinginterval + mtuprobes ==-2..-3: send one maxmtu probe every second + mtuprobes == -4: maxmtu no longer valid, reset minmtu and maxmtu and go to 0 */ struct timeval elapsed; - timersub(&now, &n->probe_sent_time, &elapsed); + timersub(&now, &n->mtu_ping_sent, &elapsed); if(n->mtuprobes >= 0) { if(n->mtuprobes != 0 && elapsed.tv_sec == 0 && elapsed.tv_usec < 333333) return; } else { - if(elapsed.tv_sec < pingtimeout) - return; + if(n->mtuprobes < -1) { + if(elapsed.tv_sec < 1) + return; + } else { + if(elapsed.tv_sec < pinginterval) + return; + } } + n->mtu_ping_sent = now; + try_fix_mtu(n); + if(n->mtuprobes < -3) { + /* We lost three MTU probes, restart discovery */ + logger(DEBUG_TRAFFIC, LOG_INFO, "Decrease in PMTU to %s (%s) detected, restarting PMTU discovery", n->name, n->hostname); + n->mtuprobes = 0; + n->minmtu = 0; + } + if(n->mtuprobes < 0) { /* After the initial discovery, we only send one maxmtu and one maxmtu+1 probe to detect PMTU increases. */ send_udp_probe_packet(n, n->maxmtu); - if(n->maxmtu + 1 < MTU) + if(n->mtuprobes == -1 && n->maxmtu + 1 < MTU) send_udp_probe_packet(n, n->maxmtu + 1); + n->mtuprobes--; } else { /* Before initial discovery begins, set maxmtu to the most likely value. If it's underestimated, we will correct it after initial discovery. */ @@ -1061,23 +1056,6 @@ static void try_mtu(node_t *n) { if(n->mtuprobes >= 0) n->mtuprobes++; } - - n->probe_counter = 0; - n->probe_sent_time = now; - n->probe_time = now; - - /* Calculate the packet loss of incoming traffic by comparing the rate of - packets received to the rate with which the sequence number has increased. - TODO: this is unrelated to PMTU discovery - it should be moved elsewhere. - */ - - if(n->received > n->prev_received) - n->packetloss = 1.0 - (n->received - n->prev_received) / (float)(n->received_seqno - n->prev_received_seqno); - else - n->packetloss = n->received_seqno <= n->prev_received_seqno; - - n->prev_received_seqno = n->received_seqno; - n->prev_received = n->received; } /* These functions try to establish a tunnel to a node (or its relay) so that