time-protocol.h 10.5 KB
Newer Older
1
#include <assert.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
2
#include "util/time.h"
3 4 5 6 7 8 9 10 11 12 13 14 15

#ifdef XLAP
#  error "does not support XLAP right now"
#endif

#ifdef TCP
#
#  define PROTOCOL "tcp"
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <netdb.h>
#  include <unistd.h>
#  include <netinet/in.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
16
#  include <netinet/tcp.h>
17 18 19

typedef int socket_t;

Andreas Schmidt's avatar
Andreas Schmidt committed
20 21 22 23 24 25 26 27 28 29 30 31
static inline void _time() {
    long long       ns; // Milliseconds
    time_t          s;  // Seconds
    struct timespec spec;

    clock_gettime(CLOCK_REALTIME, &spec);

    s  = spec.tv_sec;
    ns = spec.tv_nsec;
    printf("T: %09ld.%09ld\n", s, ns);
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static inline socket_t _open_sender(struct arguments *args)
{
    struct addrinfo hints = {
      .ai_socktype = SOCK_STREAM,
      .ai_family = AF_UNSPEC,
      .ai_flags = AI_ADDRCONFIG,
    };

    char port[16];
    snprintf(port, sizeof(port), "%d", (int) args->port);

    struct addrinfo *head;
    int fail = getaddrinfo(args->target, port, &hints, &head);
    assert(!fail);
    int sock;
    struct addrinfo *iter;
Andreas Schmidt's avatar
Andreas Schmidt committed
48 49
    int send_buffer_size = args->sndbuf;
    uint32_t send_buffer_size_len = sizeof(send_buffer_size);
50 51 52 53

    for (iter = head; iter != NULL; iter = iter->ai_next) {
        sock = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);

Andreas Schmidt's avatar
Andreas Schmidt committed
54 55 56 57 58 59 60 61 62 63
        if(args->optimized) {
          // Disable Nagles algorithm.
          int flag = 1;
          setsockopt(sock, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
          setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer_size, send_buffer_size_len);
          getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &send_buffer_size, &send_buffer_size_len);
          printf("SO_SNDBUF: %d\n", send_buffer_size);
        }
        int connect_res = connect(sock, iter->ai_addr, iter->ai_addrlen);
        if (connect_res == 0) {
64
            break;
Andreas Schmidt's avatar
Andreas Schmidt committed
65 66 67
        } else {
            printf("CONNECT ERROR: %d, errno: %d\n", connect_res, errno);
        }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        close(sock);
    }
    assert(iter && "failed to connect via TCP");
    freeaddrinfo(head);
    return sock;
}

static inline socket_t _open_receiver(struct arguments *args)
{
    int ssock = socket(AF_INET, SOCK_STREAM, 0);
    assert(ssock >= 0);

    int yes = 1;
    setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

Andreas Schmidt's avatar
Andreas Schmidt committed
83 84 85 86 87 88 89 90 91 92
    if(args->optimized) {
      setsockopt(ssock, SOL_TCP, TCP_QUICKACK, &yes, sizeof(yes));

      int recv_buffer_size = args->rcvbuf;
      uint32_t recv_buffer_size_len = sizeof(recv_buffer_size);
      setsockopt(ssock, SOL_SOCKET, SO_RCVBUF, &recv_buffer_size, recv_buffer_size_len);
      getsockopt(ssock, SOL_SOCKET, SO_RCVBUF, &recv_buffer_size, &recv_buffer_size_len);
      printf("SO_RCVBUF: %d\n", recv_buffer_size);
    }

93 94 95 96 97 98 99 100 101 102 103 104 105
    struct sockaddr_in address = {
        .sin_family = AF_INET,
        .sin_port = htons(args->port),
    };
    memset(address.sin_zero, '\0', sizeof(address.sin_zero));
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    assert(0 == bind(ssock, (const struct sockaddr *) &address, sizeof(address)));

    assert(0 == listen(ssock, SOMAXCONN));

    int sock = accept(ssock, NULL, NULL);
    assert(sock >= 0);

Andreas Schmidt's avatar
Andreas Schmidt committed
106 107
    int res = close(ssock);
    assert(res >= 0);
108 109 110 111

    return sock;
}

Andreas Schmidt's avatar
Andreas Schmidt committed
112
static inline ssize_t _send(socket_t conn, const char *buf, size_t size)
113 114
{
    int written = 0;
Andreas Schmidt's avatar
Andreas Schmidt committed
115
    ssize_t n = 0;
116
    while (written < size) {
Andreas Schmidt's avatar
Andreas Schmidt committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        fd_set wfds;
        struct timeval tv;
        int retval;
        FD_ZERO(&wfds);
        FD_SET(conn, &wfds);

        tv.tv_sec = 1;
        tv.tv_usec = 0;
        retval = select(conn + 1, NULL, &wfds, NULL, &tv);
        if (retval == -1) {
            printf("Select failed: %d.\n", errno);
            return -1;
        } else if (retval > 0) {
            if (!FD_ISSET(conn, &wfds)) {
                printf("FD not set: %d\n", conn);
                continue;
            }
            n = write(conn, buf, size - written);
            if (n < 0) {
                _time();
                printf("SEND FAILED: %ld, errno: %d, written: %ld / %lu\n", n, errno, written, size);
                printf("%s\n", buf - written);
                return written;
            }
            if (n == 0) {
                break;
            }
            written += n;
            buf += n;
        } else {
            continue;
        }

150
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
151
    return written;
152 153 154 155 156
}

static inline ssize_t _recv(socket_t conn, char *buf, size_t size)
{
    ssize_t received = 0;
Andreas Schmidt's avatar
Andreas Schmidt committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    ssize_t n = 0;
    while (received < size && keepRunning) {
        fd_set rfds;
        struct timeval tv;
        int retval;
        FD_ZERO(&rfds);
        FD_SET(conn, &rfds);

        tv.tv_sec = 1;
        tv.tv_usec = 0;
        retval = select(conn + 1, &rfds, NULL, NULL, &tv);
        if (!keepRunning) {
            printf("No longer running.\n");
            return -1;
        }

        if (retval == -1) {
            printf("Select failed: %d.\n", errno);
            return -1;
        } else if (retval > 0) {
            if (!FD_ISSET(conn, &rfds)) {
                printf("FD not set: %d\n", conn);
                continue;
            }
            n = recv(conn, buf, size - received, 0);
            if (n < 0) {
                _time();
                printf("RECV FAILED: %ld, errno: %d, received: %ld / %lu\n", n, errno, received, size);
                return -1;
            }
            if (n == 0) {
                break;
            }
            buf += n;
            received += n;
        } else {
            continue;
194 195 196 197 198 199 200
        }
    }
    return received;
}

static inline void _close(socket_t conn)
{
Andreas Schmidt's avatar
Andreas Schmidt committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    char buffer[10];
    int res;
    shutdown(conn, SHUT_WR);
    for(;;) {
        res = read(conn, buffer, 1);
        if(res < 0) {
            perror("reading");
            exit(1);
        }
        if(!res)
            break;
    }
    res = close(conn);
    assert(res >= 0);
}

static inline long long int _send_peer_btl_pace(socket_t conn) {
    return 0;
}

static inline long long int _recv_peer_btl_pace(socket_t conn) {
    return 0;
}

static inline long long int _nw_pace(socket_t conn) {
    return 0;
}

static inline long long int _app_send_pace_internal(socket_t conn) {
    return 0;
}

static inline long long int _app_send_pace_dependent(socket_t conn) {
    return 0;
}

static inline long long int _app_send_pace_external(socket_t conn) {
    return 0;
}

static inline long long int _trans_transmit_pace_internal(socket_t conn) {
    return 0;
}

static inline long long int _trans_transmit_pace_dependent(socket_t conn) {
    return 0;
}

static inline long long int _trans_transmit_pace_external(socket_t conn) {
    return 0;
}

static inline long long int _trans_receive_pace_internal(socket_t conn) {
    return 0;
}

static inline long long int _trans_receive_pace_dependent(socket_t conn) {
    return 0;
}

static inline long long int _trans_receive_pace_external(socket_t conn) {
    return 0;
}

static inline long long int _app_deliver_pace_internal(socket_t conn) {
    return 0;
}

static inline long long int _app_deliver_pace_dependent(socket_t conn) {
    return 0;
}

static inline long long int _app_deliver_pace_external(socket_t conn) {
    return 0;
275 276 277 278 279 280 281 282
}

#else
#  define PROTOCOL "prrt"

typedef PrrtSocket *socket_t;

static inline socket_t _open_sender(struct arguments *args) {
Andreas Schmidt's avatar
Andreas Schmidt committed
283
    PrrtSocket *s = PrrtSocket_create(args->size, 100 * 1000);
284 285 286 287 288 289
    s->withTimestamp = true;
    assert(s != NULL && "Socket create failed.");

    if (args->thread_pinning) {
        PrrtSocket_enable_thread_pinning(s);
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
290
    PrrtSocket_set_coding_parameters(s, 1, 1, 0, NULL);
291

Andreas Schmidt's avatar
Andreas Schmidt committed
292
    assert(PrrtSocket_bind(s, "0.0.0.0", args->local_port) == EXIT_SUCCESS && "bind failed");
293 294 295 296 297 298 299 300 301 302 303 304 305

    PrrtSocket_connect(s, args->target, args->port);
    return s;
}

static inline socket_t _open_receiver(struct arguments *args) {
    socket_t s = PrrtSocket_create(args->size, HALF_TIMESTAMP - 1);
    assert(s != NULL && "Could not create socket.");

    if (args->thread_pinning) {
        PrrtSocket_enable_thread_pinning(s);
    }

Andreas Schmidt's avatar
Andreas Schmidt committed
306
    assert(PrrtSocket_bind(s, "0.0.0.0", args->port) == EXIT_SUCCESS && "bind failed");
307 308 309
    return s;
}

Andreas Schmidt's avatar
Andreas Schmidt committed
310
static inline ssize_t _send(socket_t conn, const char *buf, size_t size) {
311
    PrrtSocket_send_sync(conn, (unsigned char *) buf, size);
Andreas Schmidt's avatar
Andreas Schmidt committed
312
    return size;
313 314 315 316 317 318 319 320 321 322 323 324
}

static inline ssize_t _recv(socket_t conn, char *buf, size_t size) {
    struct sockaddr_in addr;
    return PrrtSocket_receive_asap_wait(conn, buf, (struct sockaddr*) &addr);
}

static inline void _close(socket_t conn) {
    PrrtSocket_close(conn);
    free(conn);
}

Andreas Schmidt's avatar
Andreas Schmidt committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
static inline long long int _send_peer_btl_pace(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "send_peer_btl_pace");
}

static inline long long int _recv_peer_btl_pace(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "recv_peer_btl_pace");
}

static inline long long int _nw_pace(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "nw_pace");
}

static inline long long int _app_send_pace_internal(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appSend_pace_internal");
}

static inline long long int _app_send_pace_dependent(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appSend_pace_dependent");
}

static inline long long int _app_send_pace_external(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appSend_pace_external");
}

static inline long long int _trans_transmit_pace_internal(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtTransmit_pace_internal");
}

static inline long long int _trans_transmit_pace_dependent(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtTransmit_pace_dependent");
}

static inline long long int _trans_transmit_pace_external(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtTransmit_pace_external");
}

static inline long long int _trans_receive_pace_internal(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtReceive_pace_internal");
}

static inline long long int _trans_receive_pace_dependent(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtReceive_pace_dependent");
}

static inline long long int _trans_receive_pace_external(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "prrtReceive_pace_external");
}

static inline long long int _app_deliver_pace_internal(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appDeliver_pace_internal");
}

static inline long long int _app_deliver_pace_dependent(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appDeliver_pace_dependent");
}

static inline long long int _app_deliver_pace_external(socket_t conn) {
    return PrrtSocket_get_sock_opt(conn, "appDeliver_pace_external");
}

385 386 387 388 389 390 391 392
#endif

static inline socket_t _open(bool sender, struct arguments *args) {
    if (sender)
        return _open_sender(args);
    else
        return _open_receiver(args);
}