dataReceiver.c 16.4 KB
Newer Older
1
#include <netdb.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
2
#include <stdio.h>
3
#include <arpa/inet.h>
4 5 6
#include "../../defines.h"
#include "../../util/dbg.h"
#include "../../util/common.h"
7
#include "../../util/time.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
8
#include "../types/lossStatistics.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
9
#include "../types/block.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
10 11
#include "../clock.h"
#include "../socket.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
12
#include "dataReceiver.h"
13

14 15 16 17
static void retrieve_data_packets_for_block(PrrtSocket *sock_ptr,
                                            prrtSequenceNumber_t base_seqno,
                                            uint8_t k,
                                            const PrrtBlock *block) {
18
    List *res = List_create();
19 20
    prrtSequenceNumber_t last_seqno = (prrtSequenceNumber_t) (base_seqno + k - 1);
    debug(DEBUG_BLOCK, "Size: %d", PrrtDataPacketStore_size(sock_ptr->dataPacketStore));
21
    PrrtDataPacketStore_remove_range(sock_ptr->dataPacketStore, res, base_seqno,
22 23
                                     last_seqno);
    debug(DEBUG_BLOCK, "Retrieve %d packets in range: %u-%u.", List_count(res), base_seqno, last_seqno);
24

25
    LIST_FOREACH(res, first, next, cur) {
26 27
        PrrtPacket *packet = cur->value;
        check(PrrtBlock_insert_data_packet((PrrtBlock *) block, packet), "Insert failed!")
28
    }
29 30
    List_destroy(res);
    return;
31
    error:
32
    PERROR("Insert failed.");
33
    List_destroy(res);
34 35
}

Andreas Schmidt's avatar
Andreas Schmidt committed
36 37
static void decode_block(PrrtSocket *sock_ptr, PrrtBlock *block) {
    if (block != NULL && PrrtBlock_decode_ready(block)) {
38 39 40 41 42 43 44 45 46 47 48 49 50 51
        bool data_relevant = PrrtDeliveredPacketTable_test_is_block_relevant(sock_ptr->deliveredPacketTable,
                                                                             block->baseSequenceNumber,
                                                                             block->codingParams->n);
        if (data_relevant) {
            check(PrrtBlock_decode(block), "Decoding failed");

            while (List_count(block->dataPackets) > 0) {
                PrrtPacket *pkt = List_shift(block->dataPackets);
                if (PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable,
                                                                         pkt->sequenceNumber)) {
                    PrrtPacketDeliveryStore_insert(sock_ptr->packetDeliveryStore, pkt);
                } else {
                    PrrtPacket_destroy(pkt);
                }
52 53
            }
        }
54
        PrrtRepairBlockStore_delete(sock_ptr->repairBlockStore, block->baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
55
        PrrtBlock_destroy(block);
56
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
57 58

    return;
59 60
    error:
    PERROR("Decoding failed.%s", "")
61
}
62

Andreas Schmidt's avatar
Andreas Schmidt committed
63 64 65 66 67 68 69
static bool send_feedback(PrrtSocket *sock_ptr,
                          struct sockaddr_in remote,
                          prrtSequenceNumber_t seqno,
                          prrtTimestamp_t receiveStamp,
                          prrtTimestamp_t sentTimestamp,
                          prrtPacketType_t type) {

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    enum XlapTimestampPacketKind kind = ts_data_packet;
    if (type == PACKET_TYPE_DATA) {
        kind = ts_data_packet;
    } else if (type == PACKET_TYPE_REDUNDANCY) {
        kind = ts_redundancy_packet;
    }

    XlapTimeStampCycle(sock_ptr, kind, seqno, SendFeedbackStart);

    prrtFeedback_t feedback = {
            .seqNo = seqno,
            .type = type,
            .receivedTime = receiveStamp,
            .sentTime = sentTimestamp
    };

86 87 88
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);

89 90 91
    struct sockaddr_in targetaddr;
    memset((char *) &targetaddr, 0, sizeof(targetaddr));
    targetaddr.sin_family = AF_INET;
Andreas Schmidt's avatar
Andreas Schmidt committed
92
    targetaddr.sin_port = htons((uint16_t) (remote_port));
93 94 95 96 97

    struct hostent *hp;
    hp = gethostbyname(remote_host);
    memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);

98 99 100
    prrtTimestamp_t forwardTripTime = (uint32_t) (((int64_t) PrrtClock_get_current_time_us()) +
                                                  (sock_ptr->lastSentTimestamp - sock_ptr->lastReceivedTimestamp));

Andreas Schmidt's avatar
Andreas Schmidt committed
101 102
    PrrtLossStatistics stats = sock_ptr->lossStatistics;

103 104 105 106
    int group_RTT = 0; // TODO: To be determined.
    PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, sock_ptr->sequenceNumberFeedback++, group_RTT,
                                                                     stats.gapLength, stats.gapCount, stats.burstLength,
                                                                     stats.burstCount, forwardTripTime,
Andreas Schmidt's avatar
Andreas Schmidt committed
107 108
                                                                     stats.erasureCount, stats.packetCount,
                                                                     feedback.seqNo,
109
                                                                     feedback.type);
110
    prrtPacketLength_t length = PrrtPacket_size(feedback_pkt_ptr);
111
    void *buf = calloc(1, length);
112
    check_mem(buf);
113

114
    check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr), "Buffer for encoding feedback is too small");
115

116
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
Andreas Schmidt's avatar
Andreas Schmidt committed
117
    check(sendto(sock_ptr->socketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) ==
118
          length, "Sending feedback failed.");
119
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
120
    free(buf);
121

122
    PrrtPacket_destroy(feedback_pkt_ptr);
123
    XlapTimeStampCycle(sock_ptr, kind, seqno, SendFeedbackEnd);
124

125
    return true;
126 127

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
128 129
    if (buf != NULL) { free(buf); }
    if (feedback_pkt_ptr != NULL) { PrrtPacket_destroy(feedback_pkt_ptr); }
130
    return false;
131 132
}

Andreas Schmidt's avatar
Andreas Schmidt committed
133
static void handle_data_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
134
    PrrtPacketDataPayload *payload = packet->payload;
135 136 137
    prrtTimestamp_t sentTimestamp = payload->timestamp;
    sock_ptr->lastSentTimestamp = sentTimestamp;
    PrrtClock_update(&sock_ptr->clock, sentTimestamp, payload->groupRTprop_us);
138
    debug(DEBUG_DATARECEIVER, "Timeout: %lu", payload->packetTimeout_us);
139

140
    PrrtPacketTimeout *packetTimeout = PrrtPacketTimeout_create(packet);
141
    check(PrrtPacketTimeoutTable_insert(sock_ptr->packetTimeoutTable, packetTimeout), "Could not insert data packet.");
142

Andreas Schmidt's avatar
Andreas Schmidt committed
143
    prrtSequenceNumber_t seqno = packet->sequenceNumber;
Andreas Schmidt's avatar
Andreas Schmidt committed
144
    PrrtReceptionTable_mark_received(sock_ptr->dataReceptionTable, seqno);
Stefan Reif's avatar
Stefan Reif committed
145

146 147 148 149
    prrtTimestamp_t now = PrrtClock_get_current_time_us();
    if (PrrtTimestamp_cmp(now, payload->packetTimeout_us) > 0) {
        debug(DEBUG_DATARECEIVER, "Timeout data packet %u (%lu > %lu)", seqno, (unsigned long) now,
              (unsigned long) payload->packetTimeout_us);
150
        PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable, packet->sequenceNumber);
151
        PrrtPacket_destroy(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
152 153
    } else if (!PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable,
                                                                    packet->sequenceNumber)) {
154
        debug(DEBUG_DATARECEIVER, "Not relevant: %u", seqno);
155 156
        PrrtPacket_destroy(packet);
    } else {
157
        PrrtChannelStateInformation_update_delivery_rate(sock_ptr->senderChannelStateInformation, payload->btlbw);
158 159
        prrtSequenceNumber_t baseSequenceNumber = packet->sequenceNumber - packet->index;

160 161 162 163
        // forward to application layer
        debug(DEBUG_DATARECEIVER, "Forward: %u", seqno);
        PrrtPacketDeliveryStore_insert(sock_ptr->packetDeliveryStore, packet);

164
        PrrtPacket *reference = PrrtPacket_copy(packet);
165 166 167 168
        
        // forward to application layer
        debug(DEBUG_DATARECEIVER, "Forward: %u", seqno);
        PrrtPacketDeliveryStore_insert(sock_ptr->packetDeliveryStore, packet);
169 170

        PrrtBlock *block = PrrtRepairBlockStore_get_block(sock_ptr->repairBlockStore, baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
171
        if (block != NULL) {
172 173 174 175 176
            if(PrrtBlock_insert_data_packet(block, reference)) {
                decode_block(sock_ptr, block);
            } else {
                PrrtPacket_destroy(reference);
            }
177
        } else {
178
            debug(DEBUG_DATARECEIVER, "Inserting data packet %d for later.", reference->sequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
179
            if (PrrtDataStore_insert(sock_ptr->dataPacketStore, reference) == false) {
180
                debug(DEBUG_DATARECEIVER, "Failed to insert %d.", reference->sequenceNumber);
181
                PrrtPacket_destroy(reference);
182 183
            } else {
                debug(DEBUG_DATARECEIVER, "Inserted %d.", reference->sequenceNumber);
184 185 186
            }
        }

187
        XlapTimeStampClock(sock_ptr, ts_data_packet, seqno, PrrtReturnPackage);
Andreas Schmidt's avatar
Andreas Schmidt committed
188
        XlapTimeStampCycle(sock_ptr, ts_data_packet, seqno, PrrtReturnPackage);
189 190 191 192 193 194 195
    }
    return;

    error:
    PERROR("Handling data packet failed%s.", "");
}

Andreas Schmidt's avatar
Andreas Schmidt committed
196
static void handle_redundancy_packet(PrrtSocket *socket, PrrtPacket *packet) {
197 198 199 200
    PrrtPacketRedundancyPayload *payload = packet->payload;
    prrtTimestamp_t sentTimestamp = payload->timestamp;
    socket->lastSentTimestamp = sentTimestamp;

201 202
    PrrtPacketRedundancyPayload *redundancyPayload = packet->payload;

Andreas Schmidt's avatar
Andreas Schmidt committed
203 204
    PrrtReceptionTable_mark_received(socket->redundancyReceptionTable, packet->sequenceNumber);

205 206 207
    if (!PrrtDeliveredPacketTable_test_is_block_relevant(socket->deliveredPacketTable,
                                                         redundancyPayload->baseSequenceNumber,
                                                         redundancyPayload->n)) {
208 209 210 211
        PrrtPacket_destroy(packet);
    } else {
        PrrtBlock *block = PrrtRepairBlockStore_get_block(socket->repairBlockStore,
                                                          redundancyPayload->baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
212
        if (block == NULL) {
213
            uint8_t n_cycle[1] = {redundancyPayload->n - redundancyPayload->k};
Andreas Schmidt's avatar
Andreas Schmidt committed
214
            PrrtCodingConfiguration *codingParams = PrrtCodingConfiguration_create(redundancyPayload->k,
215
                                                                                   redundancyPayload->n, 1, n_cycle);
216

Andreas Schmidt's avatar
Andreas Schmidt committed
217 218
            block = PrrtBlock_create(codingParams, PrrtSocket_get_matching_coder(socket, codingParams),
                                     redundancyPayload->baseSequenceNumber);
219

220
            PrrtRepairBlockStore_insert(socket->repairBlockStore, block);
221

222 223
            retrieve_data_packets_for_block(socket, redundancyPayload->baseSequenceNumber, block->codingParams->k, block);
        }
224

Andreas Schmidt's avatar
Andreas Schmidt committed
225
        if (PrrtBlock_insert_redundancy_packet(block, packet)) {
226 227 228 229 230 231 232
            decode_block(socket, block);
        } else {
            PrrtPacket_destroy(packet);
        }
    }
}

233 234
void handle_feedback_packet(PrrtSocket *socket, PrrtPacket *packet, prrtTimestamp_t receiveTime) {
    check(packet != NULL, "Cannot be null");
Andreas Schmidt's avatar
Andreas Schmidt committed
235
    debug(DEBUG_DATARECEIVER, "handle_feedback_packet");
236
    PrrtPacketFeedbackPayload *feedbackPayload = (PrrtPacketFeedbackPayload *) packet->payload;
Andreas Schmidt's avatar
Andreas Schmidt committed
237 238
    prrtTimestamp_t forwardTripTimestamp = feedbackPayload->forwardTripTimestamp_us;

239
    prrtTimedelta_t rtt = (prrtTimedelta_t) (receiveTime - forwardTripTimestamp);
Andreas Schmidt's avatar
Andreas Schmidt committed
240

241
    PrrtReceiver_on_ack(socket->receiver, feedbackPayload, receiveTime, rtt, socket->applicationConstraints);
Andreas Schmidt's avatar
Andreas Schmidt committed
242 243 244 245 246 247
    return;

    error:
    PERROR("handle_feedback_packet failed.");
}

248
void receive_from_socket(PrrtSocket *socket_ptr, unsigned char buffer_ptr[65528], ssize_t *received_size,
Andreas Schmidt's avatar
Andreas Schmidt committed
249 250
                         struct sockaddr_in *remote_ptr, socklen_t *remote_len_ptr,
                         struct timespec *packet_timestamp_ptr,
251
                         uint64_t *packet_cyclestamp_ptr) {
Andreas Schmidt's avatar
Andreas Schmidt committed
252
    if (socket_ptr->isHardwareTimestamping) {
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        struct cmsghdr *cmsg;
        struct msghdr msg;
        struct iovec entry;

        struct {
            struct cmsghdr cm;
            char control[512];
        } control;

        memset(&msg, 0, sizeof(msg));
        msg.msg_iov = &entry;
        msg.msg_iovlen = 1;
        entry.iov_base = buffer_ptr;
        entry.iov_len = MAX_PAYLOAD_LENGTH;
        msg.msg_name = (caddr_t) remote_ptr;
        msg.msg_namelen = *remote_len_ptr;
        msg.msg_control = &control;
        msg.msg_controllen = sizeof(control);

Andreas Schmidt's avatar
Andreas Schmidt committed
272
        *received_size = recvmsg(socket_ptr->socketFd, &msg, 0);
273

Andreas Schmidt's avatar
Andreas Schmidt committed
274 275
        for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
            switch (cmsg->cmsg_type) {
276
                case SO_TIMESTAMPNS: {
277
                    memcpy(packet_timestamp_ptr, (struct timespec *) CMSG_DATA(cmsg), sizeof(struct timespec));
278 279 280 281 282 283 284 285
                    break;
                }
                default:
                    PERROR("Unknown Control Msg Type: %d", cmsg->cmsg_type);
                    break;
            }
        }
    } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
286
        *received_size = recvfrom(socket_ptr->socketFd, buffer_ptr, MAX_PAYLOAD_LENGTH, 0,
287
                                  (struct sockaddr *) remote_ptr, remote_len_ptr);
288
        clock_gettime(CLOCK_REALTIME, packet_timestamp_ptr);
289
    }
290
    *packet_cyclestamp_ptr = __builtin_ia32_rdtsc();
291 292
}

Andreas Schmidt's avatar
Andreas Schmidt committed
293
void *receive_data_loop(void *ptr) {
294 295 296 297
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
    unsigned char buffer[MAX_PAYLOAD_LENGTH];
298
    PrrtSocket *s = ptr;
299

Andreas Schmidt's avatar
Andreas Schmidt committed
300
    while (1) {
301
        debug(DEBUG_DATARECEIVER, "About to receive.");
Andreas Schmidt's avatar
Andreas Schmidt committed
302 303 304 305 306 307
        XlapTimestampPlaceholder tsph1;
        XlapTimestampPlaceholder tsph2;
        XlapTimestampPlaceholder tsph3;
        XlapTimestampPlaceholderInitialize(&tsph1);
        XlapTimestampPlaceholderInitialize(&tsph2);
        XlapTimestampPlaceholderInitialize(&tsph3);
Stefan Reif's avatar
Stefan Reif committed
308

309 310
        struct timespec packet_recv_timestamp;
        uint64_t packet_recv_cyclestamp = 0;
311
        receive_from_socket(s, buffer, &n, &remote, &addrlen, &packet_recv_timestamp, &packet_recv_cyclestamp);
312
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
313

314 315
        debug(DEBUG_HARDSTAMPING, "Packet TS:\t%ld.%09ld; Who? %s", (long) packet_recv_timestamp.tv_sec,
              packet_recv_timestamp.tv_nsec, inet_ntoa(remote.sin_addr));
Andreas Schmidt's avatar
Andreas Schmidt committed
316 317
        XlapTimeStampClock(&tsph1, ts_any_packet, 0, LinkReceive);
        XlapTimeStampCycle(&tsph1, ts_any_packet, 0, LinkReceive);
318

319
        PrrtPacket *packet = (PrrtPacket *) calloc(1, sizeof(PrrtPacket));
320
        check_mem(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
321
        XlapTimeStampCycle(&tsph2, ts_any_packet, 0, DecodeStart);
322
        check(PrrtPacket_decode(buffer, (uint16_t) n, packet), "Decode failed.");
Andreas Schmidt's avatar
Andreas Schmidt committed
323 324
        XlapTimeStampCycle(&tsph3, ts_any_packet, 0, DecodeEnd);
        prrtSequenceNumber_t seqno = packet->sequenceNumber;
Andreas Schmidt's avatar
Andreas Schmidt committed
325
        prrtTimestamp_t prrt_recv_timestamp = PrrtClock_TimespecToPrrtTimestamp(packet_recv_timestamp);
326
        memcpy(&packet->sender_addr, &remote, addrlen);
327

328
        prrtPacketType_t packetType = PrrtPacket_type(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
329
        debug(DEBUG_DATARECEIVER, "received packet %d:%u", (int) packetType, seqno);
330
        packet->channelReceive = packet_recv_timestamp;
331

Andreas Schmidt's avatar
Andreas Schmidt committed
332
        enum XlapTimestampPacketKind kind = ts_any_packet;
333
        prrtTimestamp_t sentTimestamp;
Andreas Schmidt's avatar
Andreas Schmidt committed
334
        if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
335
            kind = ts_data_packet;
336
            sentTimestamp = PrrtPacket_get_data_timestamp(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
337
        } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
338
            kind = ts_redundancy_packet;
339
            sentTimestamp = PrrtPacket_get_redundancy_timestamp(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
340 341
        } else if (packetType == PACKET_TYPE_FEEDBACK) {
            kind = ts_feedback_packet;
Andreas Schmidt's avatar
Andreas Schmidt committed
342 343
        }
        if (packetType == PACKET_TYPE_DATA || packetType == PACKET_TYPE_REDUNDANCY) {
344
            s->lastReceivedTimestamp = prrt_recv_timestamp;
Andreas Schmidt's avatar
Andreas Schmidt committed
345

346 347
            XlapCycleStampValue(s, kind, seqno, ChannelReceive, packet_recv_cyclestamp);
            XlapTimeStampValue(s, kind, seqno, ChannelReceive, packet_recv_timestamp);
Andreas Schmidt's avatar
Andreas Schmidt committed
348

349 350 351
            XlapTimestampPlaceholderUse(s, kind, seqno, &tsph1);
            XlapTimestampPlaceholderUse(s, kind, seqno, &tsph2);
            XlapTimestampPlaceholderUse(s, kind, seqno, &tsph3);
Andreas Schmidt's avatar
Andreas Schmidt committed
352

353
            XlapTimeStampCycle(s, kind, seqno, HandlePacketStart);
354

Andreas Schmidt's avatar
Andreas Schmidt committed
355
            if (packetType == PACKET_TYPE_DATA) {
356
                handle_data_packet(s, packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
357
            } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
358
                handle_redundancy_packet(s, packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
359
            }
360 361
            send_feedback(s, remote, seqno, prrt_recv_timestamp, sentTimestamp, packetType);
            XlapTimeStampCycle(s, kind, seqno, HandlePacketEnd);
Andreas Schmidt's avatar
Andreas Schmidt committed
362
        } else if (packetType == PACKET_TYPE_FEEDBACK) {
363
            handle_feedback_packet(s, packet, prrt_recv_timestamp);
Andreas Schmidt's avatar
Andreas Schmidt committed
364
            PrrtPacket_destroy(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
365 366 367
        } else {
            PrrtPacket_print(packet);
            PrrtPacket_destroy(packet);
368
        }
369

Andreas Schmidt's avatar
Andreas Schmidt committed
370
        debug(DEBUG_DATARECEIVER, "Cleanup");
371
        PrrtSocket_cleanup(s);
Andreas Schmidt's avatar
Andreas Schmidt committed
372
        debug(DEBUG_DATARECEIVER, "Cleaned");
373
        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
374 375 376
    }

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
377
    PERROR("receive_data_loop() ended unexpectedly.");
378
    PrrtSocket_cleanup(s);
Andreas Schmidt's avatar
Andreas Schmidt committed
379
    return NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
380
}