Cake - FQ_codel the next generation
 help / color / mirror / Atom feed
From: Hal Murray <halmurray+bufferbloat@sonic.net>
To: Matt Mathis <mattmathis@google.com>
Cc: Dave Taht <dave.taht@gmail.com>,
	bloat <bloat@lists.bufferbloat.net>,
	Cake List <cake@lists.bufferbloat.net>,
	cerowrt-devel <cerowrt-devel@lists.bufferbloat.net>,
	Hal Murray <halmurray+bufferbloat@sonic.net>
Subject: Re: [Cake] [Bloat]  access to cmsg from go?
Date: Sun, 20 Jun 2021 13:08:33 -0700	[thread overview]
Message-ID: <20210620200833.1AC6A28C157@107-137-68-211.lightspeed.sntcca.sbcglobal.net> (raw)
In-Reply-To: Message from Matt Mathis <mattmathis@google.com> of "Sat, 19 Jun 2021 18:59:06 -0700." <CAH56bmB3RMGCKhKzdOV8uW=8yjYrW0BNp7_AtKJG9krACj7x8Q@mail.gmail.com>

> Is there running code in C?

Yes.  The NTPsec code is full of #ifdef kludgery.

The C API started with SO_TIMESTAMP for microsecond precision then added 
SO_TIMESTAMPNS for nanosecond precision: timeval vs timespec

There is also SO_TIMESTAMPNS vs SCM_TIMESTAMPNS

The basic idea is to use recvmsg rather than recv.  In addition to the data 
buffer, you feed it another buffer where it can put meta data like timestamps. 
 Then you have to scan that buffer to find the part you want.  Both buffers 
are passed to the kernel via a single msghdr parameter (which also has a 
pointer for the addr you get via recvfrom).

Details are in man recvmsg and man cmsg
I thought they were reasonably clear.

Without the ifdefs:

You have to start by using setsockopt to turn on SO_TIMESTAMPNS.
  setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
                               (const void *)&on, sizeof(on)));

The setup code for recvmsg:
        struct msghdr msghdr;
        struct iovec iovec;
        char control[100];   /* FIXME: Need space for time stamp plus overhead 
*/

        iovec.iov_base        = &rb->recv_buffer;
        iovec.iov_len         = sizeof(rb->recv_buffer);
        memset(&msghdr, '\0', sizeof(msghdr));
        msghdr.msg_name       = &rb->recv_srcadr;
        msghdr.msg_namelen    = fromlen;
        msghdr.msg_iov        = &iovec;
        msghdr.msg_iovlen     = 1;
        msghdr.msg_flags      = 0;
        msghdr.msg_control    = (void *)&control;
        msghdr.msg_controllen = sizeof(control);

The actual call:
        buflen                = recvmsg(fd, &msghdr, 0);

The extract code:
        struct timespec *       tsp;
        cmsghdr = CMSG_FIRSTHDR(msghdr);
        if (NULL == cmsghdr) {
             extra checking because this code is in a subroutine
             error
        }
        if (SCM_TIMESTAMPNS != cmsghdr->cmsg_type) {
             There is only one -- no need to loop
             error
        }
        tsp = (struct timespec *)CMSG_DATA(cmsghdr);


Actual code is in:
  read_network_packet in ntpd/ntp_io.c
  https://gitlab.com/NTPsec/ntpsec/-/blob/master/ntpd/ntp_io.c
and
  fetch_packetstamp() in ntpd/ntp_packetstamp.c
  https://gitlab.com/NTPsec/ntpsec/-/blob/master/ntpd/ntp_packetstamp.c


-- 
These are my opinions.  I hate spam.




  reply	other threads:[~2021-06-20 20:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-19 21:59 [Cake] " Dave Taht
2021-06-20  1:59 ` Matt Mathis
2021-06-20 20:08   ` Hal Murray [this message]
     [not found] ` <mailman.3123.1624154360.24343.cerowrt-devel@lists.bufferbloat.net>
2021-06-23 16:09   ` [Cake] [Cerowrt-devel] " David P. Reed
2021-06-23 19:29 ` Etienne Champetier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.bufferbloat.net/postorius/lists/cake.lists.bufferbloat.net/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210620200833.1AC6A28C157@107-137-68-211.lightspeed.sntcca.sbcglobal.net \
    --to=halmurray+bufferbloat@sonic.net \
    --cc=bloat@lists.bufferbloat.net \
    --cc=cake@lists.bufferbloat.net \
    --cc=cerowrt-devel@lists.bufferbloat.net \
    --cc=dave.taht@gmail.com \
    --cc=mattmathis@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox