UDP Packet Header Formats

From epsciwiki
Revision as of 21:45, 16 November 2023 by Timmer (talk | contribs)
Jump to navigation Jump to search

Load Balancer Header

Following is the C routine to write the header that the LB reads, and eventually strips off, in order to direct the packet to the proper destination.
Currently using version = 2 and protocol = 1.



        /**
         * Set the Load Balancer header data.
         * The first four bytes go as ordered.
         * The entropy goes as a single, network byte ordered, 16-bit int.
         * The tick goes as a single, network byte ordered, 64-bit int.
         *
         *
         *  0                   1                   2                   3
         *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |       L       |       B       |    Version    |    Protocol   |
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  3               4                   5                   6
         *  2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |              Rsvd             |            Entropy            |
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  6                                               12
         *  4 5       ...           ...         ...         0 1 2 3 4 5 6 7
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *  |                                                               |
         *  +                              Tick                             +
         *  |                                                               |
         *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
         *
         * @param buffer   buffer in which to write the header.
         * @param tick     unsigned 64 bit tick number used to tell the load balancer
         *                 which backend host to direct the packet to.
         * @param version  version of this software.
         * @param protocol protocol this software uses.
         * @param entropy  entropy field used to determine destination port.
         */
        static void setLbMetadata(char* buffer, uint64_t tick, int version, int protocol, int entropy) {
            *buffer     = 'L';
            *(buffer+1) = 'B';
            *(buffer+2) = version;
            *(buffer+3) = protocol;
            // Put the data in network byte order (big endian)
            *((uint16_t *)(buffer + 6)) = htons(entropy);
            *((uint64_t *)(buffer + 8)) = htonll(tick);
        }





      /**
        * <p>Set the Reassembly Header data.
        * The first 16 bits go as ordered. The dataId is put in network byte order.
        * The offset, length and tick are also put into network byte order.</p>
        * Implemented <b>without</b> using C++ bit fields.
        * This is the new, version 2, RE header.
        *
        *  protocol 'Version:4, Rsvd:12, Data-ID:16, Offset:32, Length:32, Tick:64'
        *
        *  0                   1                   2                   3
        *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        *  |Version|        Rsvd           |            Data-ID            |
        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        *  |                         Buffer Offset                         |
        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        *  |                         Buffer Length                         |
        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        *  |                                                               |
        *  +                             Tick                              +
        *  |                                                               |
        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        *
        * @param buffer  buffer in which to write the header.
        * @param offset  byte offset into full buffer payload.
        * @param length  total length in bytes of full buffer payload.
        * @param tick    64 bit tick number used to tell the load balancer
        *                which backend host to direct the packet to. Necessary to
        *                disentangle packets from different ticks at one destination
        *                as there may be overlap in time.
        * @param version the version of this software.
        * @param dataId  the data source id number.
        */
        static void setReMetadata(char* buffer, uint32_t offset, uint32_t length,
                                  uint64_t tick, int version, uint16_t dataId) {

            buffer[0] = version << 4;

            *((uint16_t *)(buffer + 2))  = htons(dataId);
            *((uint32_t *)(buffer + 4))  = htonl(offset);
            *((uint32_t *)(buffer + 8))  = htonl(length);
            *((uint64_t *)(buffer + 12)) = htonll(tick);
        }