pgm_socket.cpp 21.9 KB
Newer Older
malosek's avatar
malosek committed
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
malosek's avatar
malosek committed
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
malosek's avatar
malosek committed
8 9 10 11 12 13
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU Lesser General Public License for more details.
malosek's avatar
malosek committed
15

16
    You should have received a copy of the GNU Lesser General Public License
malosek's avatar
malosek committed
17 18 19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "platform.hpp"

malosek's avatar
malosek committed
22
#ifdef ZMQ_HAVE_OPENPGM
malosek's avatar
malosek committed
23

24 25 26 27
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

malosek's avatar
malosek committed
28
#ifdef ZMQ_HAVE_LINUX
29
#include <poll.h>
malosek's avatar
malosek committed
30 31
#endif

Martin Sustrik's avatar
Martin Sustrik committed
32
#include <stdlib.h>
33
#include <string.h>
malosek's avatar
malosek committed
34 35 36 37 38 39
#include <string>

#include "options.hpp"
#include "pgm_socket.hpp"
#include "config.hpp"
#include "err.hpp"
malosek's avatar
malosek committed
40
#include "uuid.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
41
#include "stdint.hpp"
malosek's avatar
malosek committed
42

43
#ifndef MSG_ERRQUEUE
Martin Sustrik's avatar
Martin Sustrik committed
44
#define MSG_ERRQUEUE 0x2000
45 46
#endif

malosek's avatar
malosek committed
47
zmq::pgm_socket_t::pgm_socket_t (bool receiver_, const options_t &options_) :
Steven McCoy's avatar
Steven McCoy committed
48
    sock (NULL),
malosek's avatar
malosek committed
49 50 51
    options (options_),
    receiver (receiver_),
    pgm_msgv (NULL),
52
    pgm_msgv_len (0),
malosek's avatar
malosek committed
53 54
    nbytes_rec (0),
    nbytes_processed (0),
55
    pgm_msgv_processed (0)
malosek's avatar
malosek committed
56 57 58
{
}

59 60 61 62 63
//  Create, bind and connect PGM socket.
//  network_ of the form <interface & multicast group decls>:<IP port>
//  e.g. eth0;239.192.0.1:7500
//       link-local;224.250.0.1,224.250.0.2;224.250.0.3:8000
//       ;[fe80::1%en0]:7500
64
int zmq::pgm_socket_t::init (bool udp_encapsulation_, const char *network_)
malosek's avatar
malosek committed
65
{
Martin Sustrik's avatar
Martin Sustrik committed
66
    //  Can not open transport before destroying old one. 
Steven McCoy's avatar
Steven McCoy committed
67
    zmq_assert (sock == NULL);
malosek's avatar
malosek committed
68
 
Steven McCoy's avatar
Steven McCoy committed
69 70
    //  Parse port number, start from end for IPv6
    const char *port_delim = strrchr (network_, ':');
malosek's avatar
malosek committed
71 72 73 74 75
    if (!port_delim) {
        errno = EINVAL;
        return -1;
    }

Martin Sustrik's avatar
Martin Sustrik committed
76
    uint16_t port_number = atoi (port_delim + 1);
malosek's avatar
malosek committed
77
  
Martin Sustrik's avatar
Martin Sustrik committed
78
    char network [256];
79
    if (port_delim - network_ >= (int) sizeof (network) - 1) {
malosek's avatar
malosek committed
80 81 82 83
        errno = EINVAL;
        return -1;
    }
    memset (network, '\0', sizeof (network));
84
    memcpy (network, network_, port_delim - network_);
Steven McCoy's avatar
Steven McCoy committed
85 86 87 88 89 90
   
    //  Validate socket options 
    //  Data rate is in [B/s]. options.rate is in [kb/s].
    if (options.rate <= 0) {
        errno = EINVAL;
        return -1;
malosek's avatar
malosek committed
91
    }
92 93
    //  Recovery interval [s] or [ms] - based on the user's call 
    if ((options.recovery_ivl <= 0) && (options.recovery_ivl_msec <= 0)) {
malosek's avatar
malosek committed
94 95 96 97
        errno = EINVAL;
        return -1;
    }

Steven McCoy's avatar
Steven McCoy committed
98 99 100 101
    //  Zero counter used in msgrecv.
    nbytes_rec = 0;
    nbytes_processed = 0;
    pgm_msgv_processed = 0;
102

Steven McCoy's avatar
Steven McCoy committed
103 104 105 106 107 108 109
    pgm_error_t *pgm_error = NULL;
    struct pgm_addrinfo_t hints, *res = NULL;
    sa_family_t sa_family;

    memset (&hints, 0, sizeof (hints));
    hints.ai_family = AF_UNSPEC;
    if (!pgm_getaddrinfo (network, NULL, &res, &pgm_error)) {
110 111

        //  Invalid parameters don't set pgm_error_t.
112
        zmq_assert (pgm_error != NULL);
Steven McCoy's avatar
Steven McCoy committed
113
        if (pgm_error->domain == PGM_ERROR_DOMAIN_IF && (
114 115

              //  NB: cannot catch EAI_BADFLAGS.
116 117
              pgm_error->code != PGM_ERROR_SERVICE &&
              pgm_error->code != PGM_ERROR_SOCKTNOSUPPORT))
118 119

            //  User, host, or network configuration or transient error.
Steven McCoy's avatar
Steven McCoy committed
120 121
            goto err_abort;

122
        //  Fatal OpenPGM internal error.
123
        zmq_assert (false);
malosek's avatar
malosek committed
124 125
    }

126 127
    zmq_assert (res != NULL);

128
    //  Pick up detected IP family.
Steven McCoy's avatar
Steven McCoy committed
129
    sa_family = res->ai_send_addrs[0].gsr_group.ss_family;
malosek's avatar
malosek committed
130

131
    //  Create IP/PGM or UDP/PGM socket.
Martin Sustrik's avatar
Martin Sustrik committed
132
    if (udp_encapsulation_) {
133 134 135 136
        if (!pgm_socket (&sock, sa_family, SOCK_SEQPACKET, IPPROTO_UDP,
              &pgm_error)) {

            //  Invalid parameters don't set pgm_error_t.
137
            zmq_assert (pgm_error != NULL);
Steven McCoy's avatar
Steven McCoy committed
138
            if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET && (
139 140 141 142
                  pgm_error->code != PGM_ERROR_BADF &&
                  pgm_error->code != PGM_ERROR_FAULT &&
                  pgm_error->code != PGM_ERROR_NOPROTOOPT &&
                  pgm_error->code != PGM_ERROR_FAILED))
143 144

                //  User, host, or network configuration or transient error.
Steven McCoy's avatar
Steven McCoy committed
145 146
                goto err_abort;

147
            //  Fatal OpenPGM internal error.
Steven McCoy's avatar
Steven McCoy committed
148
            zmq_assert (false);
149 150
        }

Steven McCoy's avatar
Steven McCoy committed
151 152
        //  All options are of data type int
        const int encapsulation_port = port_number;
153 154 155 156
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_UDP_ENCAP_UCAST_PORT,
                &encapsulation_port, sizeof (encapsulation_port)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_UDP_ENCAP_MCAST_PORT,
                &encapsulation_port, sizeof (encapsulation_port)))
Steven McCoy's avatar
Steven McCoy committed
157
            goto err_abort;
158 159 160 161 162 163
    }
    else {
        if (!pgm_socket (&sock, sa_family, SOCK_SEQPACKET, IPPROTO_PGM,
              &pgm_error)) {

            //  Invalid parameters don't set pgm_error_t.
164
            zmq_assert (pgm_error != NULL);
Steven McCoy's avatar
Steven McCoy committed
165
            if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET && (
166 167 168 169
                  pgm_error->code != PGM_ERROR_BADF &&
                  pgm_error->code != PGM_ERROR_FAULT &&
                  pgm_error->code != PGM_ERROR_NOPROTOOPT &&
                  pgm_error->code != PGM_ERROR_FAILED))
170 171

                //  User, host, or network configuration or transient error.
Steven McCoy's avatar
Steven McCoy committed
172 173
                goto err_abort;

174
            //  Fatal OpenPGM internal error.
Steven McCoy's avatar
Steven McCoy committed
175 176
            zmq_assert (false);
        }
malosek's avatar
malosek committed
177 178
    }

Steven McCoy's avatar
Steven McCoy committed
179 180 181 182 183
    {
        const int rcvbuf = (int) options.rcvbuf,
                  sndbuf = (int) options.sndbuf,
                  max_tpdu = (int) pgm_max_tpdu;
        if (rcvbuf) {
184 185 186
            if (!pgm_setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
                  sizeof (rcvbuf)))
                goto err_abort;
Steven McCoy's avatar
Steven McCoy committed
187 188
        }
        if (sndbuf) {
189 190 191
            if (!pgm_setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &sndbuf,
                  sizeof (sndbuf)))
                goto err_abort;
Steven McCoy's avatar
Steven McCoy committed
192
        }
malosek's avatar
malosek committed
193

194 195 196
        //  Set maximum transport protocol data unit size (TPDU).
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_MTU, &max_tpdu,
              sizeof (max_tpdu)))
Steven McCoy's avatar
Steven McCoy committed
197
            goto err_abort;
malosek's avatar
malosek committed
198 199
    }

malosek's avatar
malosek committed
200
    if (receiver) {
Steven McCoy's avatar
Steven McCoy committed
201
        const int recv_only        = 1,
202 203 204 205 206 207
                  rxw_max_tpdu     = (int) pgm_max_tpdu,
                  rxw_sqns         = (options.recovery_ivl_msec >= 0 ?
                                      options.recovery_ivl_msec * options.rate /
                                      (1000 * rxw_max_tpdu) :
                                      options.recovery_ivl * options.rate /
                                      rxw_max_tpdu),
208
                  peer_expiry      = pgm_secs (300),
Steven McCoy's avatar
Steven McCoy committed
209 210 211 212
                  spmr_expiry      = pgm_msecs (25),
                  nak_bo_ivl       = pgm_msecs (50),
                  nak_rpt_ivl      = pgm_msecs (200),
                  nak_rdata_ivl    = pgm_msecs (200),
213 214
                  nak_data_retries = 50,
                  nak_ncf_retries  = 50;
Steven McCoy's avatar
Steven McCoy committed
215

216 217
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_RECV_ONLY, &recv_only,
                sizeof (recv_only)) ||
218 219
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_RXW_SQNS, &rxw_sqns,
                sizeof (rxw_sqns)) ||
220 221 222 223 224 225 226 227 228 229 230 231 232 233
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_PEER_EXPIRY, &peer_expiry,
                sizeof (peer_expiry)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_SPMR_EXPIRY, &spmr_expiry,
                sizeof (spmr_expiry)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_BO_IVL, &nak_bo_ivl,
                sizeof (nak_bo_ivl)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_RPT_IVL, &nak_rpt_ivl,
                sizeof (nak_rpt_ivl)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_RDATA_IVL,
                &nak_rdata_ivl, sizeof (nak_rdata_ivl)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_DATA_RETRIES,
                &nak_data_retries, sizeof (nak_data_retries)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_NCF_RETRIES,
                &nak_ncf_retries, sizeof (nak_ncf_retries)))
Steven McCoy's avatar
Steven McCoy committed
234
            goto err_abort;
malosek's avatar
malosek committed
235
    } else {
Steven McCoy's avatar
Steven McCoy committed
236
        const int send_only        = 1,
237 238 239 240 241 242
                  txw_max_tpdu     = (int) pgm_max_tpdu,
                  txw_sqns         = (options.recovery_ivl_msec >= 0 ?
                                      options.recovery_ivl_msec * options.rate /
                                      (1000 * txw_max_tpdu) :
                                      options.recovery_ivl * options.rate /
                                      txw_max_tpdu),
243 244 245 246 247 248 249 250 251 252
                  ambient_spm      = pgm_secs (30),
                  heartbeat_spm[]  = { pgm_msecs (100),
                                       pgm_msecs (100),
                                       pgm_msecs (100),
                                       pgm_msecs (100),
                                       pgm_msecs (1300),
                                       pgm_secs  (7),
                                       pgm_secs  (16),
                                       pgm_secs  (25),
                                       pgm_secs  (30) };
Steven McCoy's avatar
Steven McCoy committed
253

254 255
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_SEND_ONLY,
                &send_only, sizeof (send_only)) ||
256 257
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_TXW_SQNS,
                &txw_sqns, sizeof (txw_sqns)) ||
258 259 260 261
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_AMBIENT_SPM,
                &ambient_spm, sizeof (ambient_spm)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_HEARTBEAT_SPM,
                &heartbeat_spm, sizeof (heartbeat_spm)))
Steven McCoy's avatar
Steven McCoy committed
262 263
            goto err_abort;
    }
malosek's avatar
malosek committed
264

Steven McCoy's avatar
Steven McCoy committed
265 266
    //  PGM transport GSI.
    struct pgm_sockaddr_t addr;
Martin Sustrik's avatar
Martin Sustrik committed
267

Steven McCoy's avatar
Steven McCoy committed
268 269 270
    memset (&addr, 0, sizeof(addr));
    addr.sa_port = port_number;
    addr.sa_addr.sport = DEFAULT_DATA_SOURCE_PORT;
malosek's avatar
malosek committed
271

Steven McCoy's avatar
Steven McCoy committed
272
    if (options.identity.size () > 0) {
273

Steven McCoy's avatar
Steven McCoy committed
274
        //  Create gsi from identity.
275 276
        if (!pgm_gsi_create_from_data (&addr.sa_addr.gsi,
              options.identity.data (), options.identity.size ()))
Steven McCoy's avatar
Steven McCoy committed
277 278
            goto err_abort;
    } else {
malosek's avatar
malosek committed
279

Steven McCoy's avatar
Steven McCoy committed
280 281
        //  Generate random gsi.
        std::string gsi_base = uuid_t ().to_string ();
282 283
        if (!pgm_gsi_create_from_string (&addr.sa_addr.gsi,
              gsi_base.c_str (), -1))
Steven McCoy's avatar
Steven McCoy committed
284 285
            goto err_abort;
    }
malosek's avatar
malosek committed
286

Steven McCoy's avatar
Steven McCoy committed
287 288 289 290 291 292 293 294 295
    //  Bind a transport to the specified network devices.
    struct pgm_interface_req_t if_req;
    memset (&if_req, 0, sizeof(if_req));
    if_req.ir_interface = res->ai_recv_addrs[0].gsr_interface;
    if_req.ir_scope_id  = 0;
    if (AF_INET6 == sa_family) {
        struct sockaddr_in6 sa6;
        memcpy (&sa6, &res->ai_recv_addrs[0].gsr_group, sizeof (sa6));
        if_req.ir_scope_id = sa6.sin6_scope_id;
malosek's avatar
malosek committed
296
    }
297 298 299 300
    if (!pgm_bind3 (sock, &addr, sizeof (addr), &if_req, sizeof (if_req),
          &if_req, sizeof (if_req), &pgm_error)) {

        //  Invalid parameters don't set pgm_error_t.
301 302 303 304 305 306
        zmq_assert (pgm_error != NULL);
        if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
             pgm_error->domain == PGM_ERROR_DOMAIN_IF) && (
             pgm_error->code != PGM_ERROR_INVAL &&
             pgm_error->code != PGM_ERROR_BADF &&
             pgm_error->code != PGM_ERROR_FAULT))
307 308

            //  User, host, or network configuration or transient error.
Steven McCoy's avatar
Steven McCoy committed
309 310
            goto err_abort;

311
        //  Fatal OpenPGM internal error.
Steven McCoy's avatar
Steven McCoy committed
312
        zmq_assert (false);
malosek's avatar
malosek committed
313 314
    }

315 316 317 318
    //  Join IP multicast groups.
    for (unsigned i = 0; i < res->ai_recv_addrs_len; i++) {
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_JOIN_GROUP,
              &res->ai_recv_addrs [i], sizeof (struct group_req)))
Steven McCoy's avatar
Steven McCoy committed
319 320
            goto err_abort;
    }
321 322
    if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_SEND_GROUP,
          &res->ai_send_addrs [0], sizeof (struct group_req)))
Steven McCoy's avatar
Steven McCoy committed
323
        goto err_abort;
324

Steven McCoy's avatar
Steven McCoy committed
325
    pgm_freeaddrinfo (res);
326
    res = NULL;
327

328
    //  Set IP level parameters.
Steven McCoy's avatar
Steven McCoy committed
329 330 331 332 333
    {
        const int nonblocking      = 1,
                  multicast_loop   = options.use_multicast_loop ? 1 : 0,
                  multicast_hops   = 16,

334 335 336 337 338 339 340
                  //  Expedited Forwarding PHB for network elements, no ECN.
                  dscp             = 0x2e << 2; 

        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_MULTICAST_LOOP,
                &multicast_loop, sizeof (multicast_loop)) ||
            !pgm_setsockopt (sock, IPPROTO_PGM, PGM_MULTICAST_HOPS,
                &multicast_hops, sizeof (multicast_hops)))
Steven McCoy's avatar
Steven McCoy committed
341
            goto err_abort;
342 343
        if (AF_INET6 != sa_family && !pgm_setsockopt (sock,
              IPPROTO_PGM, PGM_TOS, &dscp, sizeof (dscp)))
Steven McCoy's avatar
Steven McCoy committed
344
            goto err_abort;
345 346
        if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NOBLOCK,
              &nonblocking, sizeof (nonblocking)))
Steven McCoy's avatar
Steven McCoy committed
347
            goto err_abort;
348
    }
malosek's avatar
malosek committed
349

350 351
    //  Connect PGM transport to start state machine.
    if (!pgm_connect (sock, &pgm_error)) {
352 353

        //  Invalid parameters don't set pgm_error_t.
354 355 356 357
        zmq_assert (pgm_error != NULL);
        goto err_abort;
    }

Martin Sustrik's avatar
Martin Sustrik committed
358 359 360 361 362 363 364 365 366 367 368 369
    //  For receiver transport preallocate pgm_msgv array.
    if (receiver) {
        zmq_assert (in_batch_size > 0);
        size_t max_tsdu_size = get_max_tsdu_size ();
        pgm_msgv_len = (int) in_batch_size / max_tsdu_size;
        if ((int) in_batch_size % max_tsdu_size)
            pgm_msgv_len++;
        zmq_assert (pgm_msgv_len);

        pgm_msgv = (pgm_msgv_t*) malloc (sizeof (pgm_msgv_t) * pgm_msgv_len);
    }

malosek's avatar
malosek committed
370
    return 0;
Steven McCoy's avatar
Steven McCoy committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

err_abort:
    if (sock != NULL) {
        pgm_close (sock, FALSE);
        sock = NULL;
    }
    if (res != NULL) {
        pgm_freeaddrinfo (res);
        res = NULL;
    }
    if (pgm_error != NULL) {
        pgm_error_free (pgm_error);
        pgm_error = NULL;
    }
    errno = EINVAL;
    return -1;
malosek's avatar
malosek committed
387 388 389 390
}

zmq::pgm_socket_t::~pgm_socket_t ()
{
Martin Sustrik's avatar
Martin Sustrik committed
391 392
    if (pgm_msgv)
        free (pgm_msgv);
Steven McCoy's avatar
Steven McCoy committed
393 394
    if (sock) 
        pgm_close (sock, TRUE);
malosek's avatar
malosek committed
395 396
}

397 398
//  Get receiver fds. receive_fd_ is signaled for incoming packets,
//  waiting_pipe_fd_ is signaled for state driven events and data.
399
void zmq::pgm_socket_t::get_receiver_fds (int *receive_fd_, 
malosek's avatar
malosek committed
400 401
    int *waiting_pipe_fd_)
{
Steven McCoy's avatar
Steven McCoy committed
402 403 404
    socklen_t socklen;
    bool rc;

405 406 407
    zmq_assert (receive_fd_);
    zmq_assert (waiting_pipe_fd_);

Steven McCoy's avatar
Steven McCoy committed
408
    socklen = sizeof (*receive_fd_);
409 410
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, receive_fd_,
        &socklen);
Steven McCoy's avatar
Steven McCoy committed
411 412
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*receive_fd_));
413

Steven McCoy's avatar
Steven McCoy committed
414
    socklen = sizeof (*waiting_pipe_fd_);
415 416
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK, waiting_pipe_fd_,
        &socklen);
Steven McCoy's avatar
Steven McCoy committed
417 418
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*waiting_pipe_fd_));
malosek's avatar
malosek committed
419 420
}

421
//  Get fds and store them into user allocated memory. 
Steven McCoy's avatar
Steven McCoy committed
422 423 424 425
//  send_fd is for non-blocking send wire notifications.
//  receive_fd_ is for incoming back-channel protocol packets.
//  rdata_notify_fd_ is raised for waiting repair transmissions.
//  pending_notify_fd_ is for state driven events.
426
void zmq::pgm_socket_t::get_sender_fds (int *send_fd_, int *receive_fd_, 
427
    int *rdata_notify_fd_, int *pending_notify_fd_)
malosek's avatar
malosek committed
428
{
Steven McCoy's avatar
Steven McCoy committed
429 430 431
    socklen_t socklen;
    bool rc;

malosek's avatar
malosek committed
432 433
    zmq_assert (send_fd_);
    zmq_assert (receive_fd_);
434
    zmq_assert (rdata_notify_fd_);
435
    zmq_assert (pending_notify_fd_);
436

Steven McCoy's avatar
Steven McCoy committed
437 438 439 440 441 442
    socklen = sizeof (*send_fd_);
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_SEND_SOCK, send_fd_, &socklen);
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*receive_fd_));

    socklen = sizeof (*receive_fd_);
443 444
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, receive_fd_,
        &socklen);
Steven McCoy's avatar
Steven McCoy committed
445 446 447 448
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*receive_fd_));

    socklen = sizeof (*rdata_notify_fd_);
449 450
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_REPAIR_SOCK, rdata_notify_fd_,
        &socklen);
Steven McCoy's avatar
Steven McCoy committed
451 452 453 454
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*rdata_notify_fd_));

    socklen = sizeof (*pending_notify_fd_);
455 456
    rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK,
        pending_notify_fd_, &socklen);
Steven McCoy's avatar
Steven McCoy committed
457 458
    zmq_assert (rc);
    zmq_assert (socklen == sizeof (*pending_notify_fd_));
malosek's avatar
malosek committed
459 460 461
}

//  Send one APDU, transmit window owned memory.
Steven McCoy's avatar
Steven McCoy committed
462
//  data_len_ must be less than one TPDU.
malosek's avatar
malosek committed
463 464
size_t zmq::pgm_socket_t::send (unsigned char *data_, size_t data_len_)
{
malosek's avatar
malosek committed
465 466
    size_t nbytes = 0;
   
Steven McCoy's avatar
Steven McCoy committed
467
    const int status = pgm_send (sock, data_, data_len_, &nbytes);
malosek's avatar
malosek committed
468

469
    //  We have to write all data as one packet.
470 471
    if (nbytes > 0) {
        zmq_assert (status == PGM_IO_STATUS_NORMAL);
malosek's avatar
malosek committed
472
        zmq_assert ((ssize_t) nbytes == (ssize_t) data_len_);
473
    } else {
474 475
        zmq_assert (status == PGM_IO_STATUS_RATE_LIMITED ||
            status == PGM_IO_STATUS_WOULD_BLOCK);
476 477 478 479 480 481 482

        if (status == PGM_IO_STATUS_RATE_LIMITED)
            errno = ENOMEM;
        else
            errno = EBUSY;
    }

483
    //  Save return value.
484
    last_tx_status = status;
malosek's avatar
malosek committed
485 486 487 488

    return nbytes;
}

489 490
long zmq::pgm_socket_t::get_rx_timeout ()
{
491 492
    if (last_rx_status != PGM_IO_STATUS_RATE_LIMITED &&
          last_rx_status != PGM_IO_STATUS_TIMER_PENDING)
493 494 495 496
        return -1;

    struct timeval tv;
    socklen_t optlen = sizeof (tv);
497 498 499
    const bool rc = pgm_getsockopt (sock, IPPROTO_PGM,
        last_rx_status == PGM_IO_STATUS_RATE_LIMITED ? PGM_RATE_REMAIN :
        PGM_TIME_REMAIN, &tv, &optlen);
500 501 502 503 504 505 506 507 508 509 510 511 512 513
    zmq_assert (rc);

    const long timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

    return timeout;
}

long zmq::pgm_socket_t::get_tx_timeout ()
{
    if (last_tx_status != PGM_IO_STATUS_RATE_LIMITED)
        return -1;

    struct timeval tv;
    socklen_t optlen = sizeof (tv);
514 515
    const bool rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv,
        &optlen);
516 517 518 519 520 521 522
    zmq_assert (rc);

    const long timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

    return timeout;
}

malosek's avatar
malosek committed
523
//  Return max TSDU size without fragmentation from current PGM transport.
524
size_t zmq::pgm_socket_t::get_max_tsdu_size ()
malosek's avatar
malosek committed
525
{
Steven McCoy's avatar
Steven McCoy committed
526 527 528 529 530 531 532
    int max_tsdu = 0;
    socklen_t optlen = sizeof (max_tsdu);

    bool rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_MSS, &max_tsdu, &optlen);
    zmq_assert (rc);
    zmq_assert (optlen == sizeof (max_tsdu));
    return (size_t) max_tsdu;
malosek's avatar
malosek committed
533 534
}

535 536
//  pgm_recvmsgv is called to fill the pgm_msgv array up to  pgm_msgv_len.
//  In subsequent calls data from pgm_msgv structure are returned.
malosek's avatar
malosek committed
537
ssize_t zmq::pgm_socket_t::receive (void **raw_data_, const pgm_tsi_t **tsi_)
malosek's avatar
malosek committed
538
{
malosek's avatar
malosek committed
539 540
    size_t raw_data_len = 0;

malosek's avatar
malosek committed
541 542 543 544 545 546 547 548
    //  We just sent all data from pgm_transport_recvmsgv up 
    //  and have to return 0 that another engine in this thread is scheduled.
    if (nbytes_rec == nbytes_processed && nbytes_rec > 0) {

        //  Reset all the counters.
        nbytes_rec = 0;
        nbytes_processed = 0;
        pgm_msgv_processed = 0;
549
        errno = EAGAIN;
550
        return 0;
malosek's avatar
malosek committed
551 552 553
    }

    //  If we have are going first time or if we have processed all pgm_msgv_t
554
    //  structure previously read from the pgm socket.
malosek's avatar
malosek committed
555 556 557 558 559 560 561 562 563
    if (nbytes_rec == nbytes_processed) {

        //  Check program flow.
        zmq_assert (pgm_msgv_processed == 0);
        zmq_assert (nbytes_processed == 0);
        zmq_assert (nbytes_rec == 0);

        //  Receive a vector of Application Protocol Domain Unit's (APDUs) 
        //  from the transport.
Steven McCoy's avatar
Steven McCoy committed
564
        pgm_error_t *pgm_error = NULL;
malosek's avatar
malosek committed
565

Steven McCoy's avatar
Steven McCoy committed
566 567
        const int status = pgm_recvmsgv (sock, pgm_msgv,
            pgm_msgv_len, MSG_ERRQUEUE, &nbytes_rec, &pgm_error);
568

569
        //  Invalid parameters.
Martin Sustrik's avatar
Martin Sustrik committed
570
        zmq_assert (status != PGM_IO_STATUS_ERROR);
571

572 573
        last_rx_status = status;

malosek's avatar
malosek committed
574
        //  In a case when no ODATA/RDATA fired POLLIN event (SPM...)
Steven McCoy's avatar
Steven McCoy committed
575
        //  pgm_recvmsg returns PGM_IO_STATUS_TIMER_PENDING.
576 577
        if (status == PGM_IO_STATUS_TIMER_PENDING) {

578 579
            zmq_assert (nbytes_rec == 0);

malosek's avatar
malosek committed
580 581 582
            //  In case if no RDATA/ODATA caused POLLIN 0 is 
            //  returned.
            nbytes_rec = 0;
583
            errno = EBUSY;
584
            return 0;
malosek's avatar
malosek committed
585
        }
586

Steven McCoy's avatar
Steven McCoy committed
587 588 589 590 591
        //  Send SPMR, NAK, ACK is rate limited.
        if (status == PGM_IO_STATUS_RATE_LIMITED) {

            zmq_assert (nbytes_rec == 0);

592
            //  In case if no RDATA/ODATA caused POLLIN 0 is returned.
Steven McCoy's avatar
Steven McCoy committed
593
            nbytes_rec = 0;
594 595
            errno = ENOMEM;
            return 0;
Steven McCoy's avatar
Steven McCoy committed
596 597 598 599 600 601 602
        }

        //  No peers and hence no incoming packets.
        if (status == PGM_IO_STATUS_WOULD_BLOCK) {

            zmq_assert (nbytes_rec == 0);

603
            //  In case if no RDATA/ODATA caused POLLIN 0 is returned.
Steven McCoy's avatar
Steven McCoy committed
604
            nbytes_rec = 0;
605
            errno = EAGAIN;
606
            return 0;
Steven McCoy's avatar
Steven McCoy committed
607 608
        }

609 610 611
        //  Data loss.
        if (status == PGM_IO_STATUS_RESET) {

Steven McCoy's avatar
Steven McCoy committed
612
            struct pgm_sk_buff_t* skb = pgm_msgv[0].msgv_skb[0];
613 614

            //  Save lost data TSI.
Steven McCoy's avatar
Steven McCoy committed
615
            *tsi_ = &skb->tsi;
616 617 618
            nbytes_rec = 0;

            //  In case of dala loss -1 is returned.
619
            errno = EINVAL;
Steven McCoy's avatar
Steven McCoy committed
620
            pgm_free_skb (skb);
621 622 623
            return -1;
        }

624
        zmq_assert (status == PGM_IO_STATUS_NORMAL);
malosek's avatar
malosek committed
625
    }
626 627 628 629
    else
    {
        zmq_assert (pgm_msgv_processed <= pgm_msgv_len);
    }
malosek's avatar
malosek committed
630

631
    // Zero byte payloads are valid in PGM, but not 0MQ protocol.
malosek's avatar
malosek committed
632 633
    zmq_assert (nbytes_rec > 0);

malosek's avatar
malosek committed
634 635 636 637 638 639 640 641 642 643 644 645
    // Only one APDU per pgm_msgv_t structure is allowed.
    zmq_assert (pgm_msgv [pgm_msgv_processed].msgv_len == 1);
 
    struct pgm_sk_buff_t* skb = 
        pgm_msgv [pgm_msgv_processed].msgv_skb [0];

    //  Take pointers from pgm_msgv_t structure.
    *raw_data_ = skb->data;
    raw_data_len = skb->len;

    //  Save current TSI.
    *tsi_ = &skb->tsi;
malosek's avatar
malosek committed
646 647 648

    //  Move the the next pgm_msgv_t structure.
    pgm_msgv_processed++;
649
    zmq_assert (pgm_msgv_processed <= pgm_msgv_len);
malosek's avatar
malosek committed
650 651 652 653 654
    nbytes_processed +=raw_data_len;

    return raw_data_len;
}

655
void zmq::pgm_socket_t::process_upstream ()
malosek's avatar
malosek committed
656
{
malosek's avatar
malosek committed
657
    pgm_msgv_t dummy_msg;
malosek's avatar
malosek committed
658

malosek's avatar
malosek committed
659
    size_t dummy_bytes = 0;
Steven McCoy's avatar
Steven McCoy committed
660
    pgm_error_t *pgm_error = NULL;
malosek's avatar
malosek committed
661

Steven McCoy's avatar
Steven McCoy committed
662 663
    const int status = pgm_recvmsgv (sock, &dummy_msg,
        1, MSG_ERRQUEUE, &dummy_bytes, &pgm_error);
malosek's avatar
malosek committed
664

665
    //  Invalid parameters.
Martin Sustrik's avatar
Martin Sustrik committed
666
    zmq_assert (status != PGM_IO_STATUS_ERROR);
667

malosek's avatar
malosek committed
668
    //  No data should be returned.
669
    zmq_assert (dummy_bytes == 0 && (status == PGM_IO_STATUS_TIMER_PENDING || 
670 671
        status == PGM_IO_STATUS_RATE_LIMITED ||
        status == PGM_IO_STATUS_WOULD_BLOCK));
672 673 674 675 676 677 678 679 680

    last_rx_status = status;

    if (status == PGM_IO_STATUS_TIMER_PENDING)
        errno = EBUSY;
    else if (status == PGM_IO_STATUS_RATE_LIMITED)
        errno = ENOMEM;
    else
        errno = EAGAIN;
malosek's avatar
malosek committed
681 682 683 684
}

#endif