Skip to contentSkip to conversation

Flexcan - timestamp from message buffer to userspace

DISCUSSION Martin Kozusky 3 participants 12 messages Oct 18, 2013 – Oct 22, 2013 CAN network drivers

lore
Share & tools
lore
#0 lore raw

Hello,
is there any official way how to read timestamp from flexcans message buffer? I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in flexcan.c, but not used. I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

but what to do with timestamp variable next and how to read it in userspace? :) I could change structure can_frame and add u32 timestamp into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN message arrives?

Thanks,
Martin

#1 lore raw

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 8 lines

Hello,
is there any official way how to read timestamp from flexcans message buffer?
I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in flexcan.c, but not used.
I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 2 lines

but what to do with timestamp variable next and how to read it in userspace? :) I could change structure can_frame and add u32 timestamp

also u16, not u32

Quoted 8 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN message arrives?

Thanks,
Martin

#2 lore raw
Quoted 9 lines · Martin Kozusky

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 7 lines

Hello,
is there any official way how to read timestamp from flexcans message buffer?
I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in flexcan.c, but not used.
I would modify function flecan_read_fifo like

Quoted 13 lines
Quoted 1 line

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 2 lines

but what to do with timestamp variable next and how to read it in userspace? :) I could change structure can_frame and add u32 timestamp

also u16, not u32

Quoted 5 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN message arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network layer by the CAN drivers interrupt routine.

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet devices in the mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping capabilities

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

There needs to be added some infrastructure to the network devices to query and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Even if I moved a wrongly assigned hardware timestamp to another place here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

the rest of the hardware timestamping (& configuration) is still missing.

Regards,
Oliver

#3 lore raw

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 26 lines · Martin Kozusky
Quoted 9 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 7 lines

Hello,
is there any official way how to read timestamp from flexcans message buffer?
I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in flexcan.c, but not used.
I would modify function flecan_read_fifo like

Quoted 13 lines
Quoted 1 line

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 2 lines

but what to do with timestamp variable next and how to read it in userspace? :) I could change structure can_frame and add u32 timestamp

also u16, not u32

Quoted 5 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN message arrives?

Hello Martin,

Quoted 8 lines

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network layer by the CAN drivers interrupt routine.

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Hello Oliver,
I'm already using SO_TIMESTAMP with recvmmsg so may be I should switch to SO_TIMESTAMPNS for better times, right?

Quoted 18 lines

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet devices in the mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping capabilities

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

There needs to be added some infrastructure to the network devices to query and configure the timestamping possibilities.

Quoted 7 lines

This is currently missing for CAN devices ...

Even if I moved a wrongly assigned hardware timestamp to another place here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

the rest of the hardware timestamping (& configuration) is still missing.

So I could just do

struct skb_shared_hwtstamps *hwts;
hwts = skb_hwtstamps(skb);
hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

in flecan_read_fifo ?

And then how to read it?

Or will it be easier for me just to change can_frame structure? :)

Thanks,
Martin

Quoted 8 lines

Regards,
Oliver

--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw

On Mon, 21 Oct 2013 08:54:30 +0200, Martin Kozusky <[email protected]>

Quoted 33 lines · wrote:

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 31 lines · Martin Kozusky
Quoted 30 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 8 lines

Hello,

is there any official way how to read timestamp from flexcans message

buffer?

I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in flexcan.c,

Quoted 7 lines

but not used.

I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 3 lines

but what to do with timestamp variable next and how to read it in

userspace? :) I could change structure can_frame and add u32

timestamp

Quoted 34 lines
Quoted 33 lines
Quoted 15 lines

also u16, not u32

Quoted 11 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN

message

arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network layer

by

the CAN drivers interrupt routine.

Quoted 5 lines
Quoted 5 lines

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Quoted 31 lines

Hello Oliver,

I'm already using SO_TIMESTAMP with recvmmsg so may be I should switch

to SO_TIMESTAMPNS for better times, right?

Quoted 23 lines

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet

devices in the

mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping

capabilities

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

Quoted 7 lines
Quoted 7 lines

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

Quoted 14 lines
Quoted 13 lines

There needs to be added some infrastructure to the network devices to

query

and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Quoted 7 lines
Quoted 7 lines

Even if I moved a wrongly assigned hardware timestamp to another place

here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

Quoted 3 lines
Quoted 3 lines

the rest of the hardware timestamping (& configuration) is still

missing.

Quoted 16 lines

So I could just do

struct skb_shared_hwtstamps *hwts;

  hwts = skb_hwtstamps(skb);



  hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);
Quoted 5 lines

in flecan_read_fifo ?

And then how to read it?

See:

http://lxr.free-electrons.com/source/Documentation/networking/timestamping/

Quoted 3 lines

Or will it be easier for me just to change can_frame structure? :)

No.

Quoted 33 lines

Thanks,

Martin

Quoted 6 lines

Regards,

Oliver

Quoted 9 lines

--

To unsubscribe from this list: send the line "unsubscribe linux-can" in

the body of a message to [email protected]

More majordomo info at http://vger.kernel.org/majordomo-info.html

--

To unsubscribe from this list: send the line "unsubscribe linux-can" in

the body of a message to [email protected]

More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

My previous mail went out to early, sorry... completing it now...

On Mon, 21 Oct 2013 10:00:28 +0200, Wolfgang Grandegger

Quoted 15 lines · <[email protected]> wrote:

On Mon, 21 Oct 2013 08:54:30 +0200, Martin Kozusky <[email protected]>

Quoted 11 lines · wrote:

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 9 lines · Martin Kozusky
Quoted 8 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 3 lines

Hello,

is there any official way how to read timestamp from flexcans

message

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

buffer?

I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in

flexcan.c,

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

but not used.

I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 33 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

but what to do with timestamp variable next and how to read it in

userspace? :) I could change structure can_frame and add u32

timestamp

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines

also u16, not u32

Quoted 11 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN

message

arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network

layer

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

by

the CAN drivers interrupt routine.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Quoted 26 lines
Quoted 25 lines

Hello Oliver,

I'm already using SO_TIMESTAMP with recvmmsg so may be I should switch

to SO_TIMESTAMPNS for better times, right?

Quoted 17 lines

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet

devices in the

mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping

capabilities

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines

There needs to be added some infrastructure to the network devices to

query

and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

Even if I moved a wrongly assigned hardware timestamp to another place

here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

the rest of the hardware timestamping (& configuration) is still

missing.

Supporting SIOCSHWTSTAMP seem the right way to go.

Quoted 9 lines
Quoted 9 lines

So I could just do

struct skb_shared_hwtstamps *hwts;

  hwts = skb_hwtstamps(skb);

Most drivers do as well:

memset(hwts, 0, sizeof(*hwts));

Quoted 1 line
Quoted 1 line
  hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

This should assign a proper ktime. [nm]s_to_ktime() may help.

Quoted 6 lines
Quoted 5 lines

in flecan_read_fifo ?

Quoted 9 lines
Quoted 1 line

And then how to read it?

See:

http://lxr.free-electrons.com/source/Documentation/networking/timestamping/

This also shows how to do read the timestamp.

Quoted 9 lines
Quoted 5 lines

Or will it be easier for me just to change can_frame structure? :)

No.

Please use the forseen interface.

Wolfgang

#6 lore raw

Dne 21.10.2013 10:11, Wolfgang Grandegger napsal(a):

Quoted 204 lines

My previous mail went out to early, sorry... completing it now...

On Mon, 21 Oct 2013 10:00:28 +0200, Wolfgang Grandegger

Quoted 15 lines · <[email protected]> wrote:

On Mon, 21 Oct 2013 08:54:30 +0200, Martin Kozusky <[email protected]>

Quoted 11 lines · wrote:

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 9 lines · Martin Kozusky
Quoted 8 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 3 lines

Hello,

is there any official way how to read timestamp from flexcans

message

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

buffer?

I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in

flexcan.c,

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

but not used.

I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 33 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

but what to do with timestamp variable next and how to read it in

userspace? :) I could change structure can_frame and add u32

timestamp

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines

also u16, not u32

Quoted 11 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN

message

arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network

layer

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

by

the CAN drivers interrupt routine.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Quoted 26 lines
Quoted 25 lines

Hello Oliver,

I'm already using SO_TIMESTAMP with recvmmsg so may be I should switch

to SO_TIMESTAMPNS for better times, right?

Quoted 17 lines

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet

devices in the

mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping

capabilities

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines

There needs to be added some infrastructure to the network devices to

query

and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

Even if I moved a wrongly assigned hardware timestamp to another place

here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

the rest of the hardware timestamping (& configuration) is still

missing.

Supporting SIOCSHWTSTAMP seem the right way to go.

Quoted 9 lines
Quoted 9 lines

So I could just do

   struct skb_shared_hwtstamps *hwts;

   hwts = skb_hwtstamps(skb);

Most drivers do as well:

  memset(hwts, 0, sizeof(*hwts));
Quoted 5 lines
Quoted 1 line
Quoted 1 line
   hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

This should assign a proper ktime. [nm]s_to_ktime() may help.

But value read from registers is just 16bit value having nothing to do with real time. But now when I see the ns_to_ktime function, it will just assign my 16bit value to .tv64 union member, ok I will use it.

But I see another problem here:
flexcan registers are read in function flexcan_read_fifo but I need to
know the timestamp in function flexcan_read_frame, how do I transfer it from there?

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/flexcan.c#n519

Should I just create some global variable in flexcan.c and store the 16bit value into it and then use it?

Quoted 15 lines
Quoted 5 lines
Quoted 4 lines

in flecan_read_fifo ?

Quoted 9 lines
Quoted 1 line

And then how to read it?

See:

Quoted 6 lines

http://lxr.free-electrons.com/source/Documentation/networking/timestamping/

This also shows how to do read the timestamp.

So you mean this setup:

memset(&hwtstamp, 0, sizeof(hwtstamp)); strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name)); hwtstamp.ifr_data = (void *)&hwconfig; memset(&hwconfig, 0, sizeof(hwconfig)); hwconfig.tx_type = HWTSTAMP_TX_OFF;
hwconfig.rx_filter = HWTSTAMP_FILTER_ALL; hwconfig_requested = hwconfig;

  if (ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) < 0) {
     if ((errno == EINVAL || errno == ENOTSUP) &&
         hwconfig_requested.tx_type == HWTSTAMP_TX_OFF &&
         hwconfig_requested.rx_filter == HWTSTAMP_FILTER_NONE)
       printf("SIOCSHWTSTAMP: disabling hardware time stamping not 
possible\n");
     else
       bail("SIOCSHWTSTAMP");

}
printf("SIOCSHWTSTAMP: tx_type %d requested, got %d; rx_filter %d
requested, got %d\n",
hwconfig_requested.tx_type, hwconfig.tx_type, hwconfig_requested.rx_filter, hwconfig.rx_filter);

and read from this cmsg_type:

case SO_TIMESTAMPING: {
struct scm_timestamping *stamp =

  (struct scm_timestamping *)CMSG_DATA(cmsg);
    printf("SO_TIMESTAMPING ");
    printf("HW raw %ld.%09ld",
     (long)stamp->hwtimeraw->tv_sec,
     (long)stamp->hwtimeraw->tv_nsec);

break;
}

or did I forget something?

Thank you,
Martin

Quoted 21 lines
Quoted 8 lines
Quoted 4 lines

Or will it be easier for me just to change can_frame structure? :)

No.

Please use the forseen interface.

Wolfgang
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw

Dne 21.10.2013 10:54, Martin Kozusky napsal(a):

Quoted 273 lines

Dne 21.10.2013 10:11, Wolfgang Grandegger napsal(a):

Quoted 208 lines

My previous mail went out to early, sorry... completing it now...

On Mon, 21 Oct 2013 10:00:28 +0200, Wolfgang Grandegger

Quoted 15 lines · <[email protected]> wrote:

On Mon, 21 Oct 2013 08:54:30 +0200, Martin Kozusky <[email protected]>

Quoted 11 lines · wrote:

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 9 lines · Martin Kozusky
Quoted 8 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 3 lines

Hello,

is there any official way how to read timestamp from flexcans

message

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

buffer?

I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in

flexcan.c,

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

but not used.

I would modify function flecan_read_fifo like

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 33 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

but what to do with timestamp variable next and how to read it in

userspace? :) I could change structure can_frame and add u32

timestamp

Quoted 27 lines
Quoted 27 lines
Quoted 15 lines

also u16, not u32

Quoted 11 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN

message

arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec resolution.

It is set at the time when the CAN frame is pushed to the network

layer

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

by

the CAN drivers interrupt routine.

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See net_timestamp_check() in netif_rx():

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Quoted 26 lines
Quoted 25 lines

Hello Oliver,

I'm already using SO_TIMESTAMP with recvmmsg so may be I should switch

to SO_TIMESTAMPNS for better times, right?

Quoted 17 lines

You may get this timestamp via ioctl() or recvmsg().

To pass hardware timestamps there's some infrastructure for ethernet

devices in the

mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping

capabilities

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

See details at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

And some example source code at

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines

There needs to be added some infrastructure to the network devices to

query

and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

Even if I moved a wrongly assigned hardware timestamp to another place

here:

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

the rest of the hardware timestamping (& configuration) is still

missing.

Supporting SIOCSHWTSTAMP seem the right way to go.

Quoted 9 lines
Quoted 9 lines

So I could just do

   struct skb_shared_hwtstamps *hwts;

   hwts = skb_hwtstamps(skb);

Most drivers do as well:

  memset(hwts, 0, sizeof(*hwts));
Quoted 5 lines
Quoted 1 line
Quoted 1 line
   hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

This should assign a proper ktime. [nm]s_to_ktime() may help.

But value read from registers is just 16bit value having nothing to do with real time. But now when I see the ns_to_ktime function, it will just assign my 16bit value to .tv64 union member, ok I will use it.

But I see another problem here:
flexcan registers are read in function flexcan_read_fifo but I need to
know the timestamp in function flexcan_read_frame, how do I transfer it from there?

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/flexcan.c#n519

Should I just create some global variable in flexcan.c and store the 16bit value into it and then use it?

Quoted 15 lines
Quoted 5 lines
Quoted 4 lines

in flecan_read_fifo ?

Quoted 9 lines
Quoted 1 line

And then how to read it?

See:

Quoted 7 lines

http://lxr.free-electrons.com/source/Documentation/networking/timestamping/

This also shows how to do read the timestamp.

So you mean this setup:

   memset(&hwtstamp, 0, sizeof(hwtstamp));
   strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));
   hwtstamp.ifr_data = (void *)&hwconfig;
   memset(&hwconfig, 0, sizeof(hwconfig));
   hwconfig.tx_type = HWTSTAMP_TX_OFF;
   hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;
   hwconfig_requested = hwconfig;
   if (ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) < 0) {
      if ((errno == EINVAL || errno == ENOTSUP) &&
          hwconfig_requested.tx_type == HWTSTAMP_TX_OFF &&
          hwconfig_requested.rx_filter == HWTSTAMP_FILTER_NONE)
        printf("SIOCSHWTSTAMP: disabling hardware time stamping not
possible\n");
      else
        bail("SIOCSHWTSTAMP");
Quoted 5 lines

}
printf("SIOCSHWTSTAMP: tx_type %d requested, got %d; rx_filter %d
requested, got %d\n",
hwconfig_requested.tx_type, hwconfig.tx_type, hwconfig_requested.rx_filter, hwconfig.rx_filter);

When I do
ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) it returns ENOTSUP :( do I have
to define this ioctl somehow in the flexcan driver or somewhere else?

Martin

Quoted 47 lines

and read from this cmsg_type:

case SO_TIMESTAMPING: {
   struct scm_timestamping *stamp =
   (struct scm_timestamping *)CMSG_DATA(cmsg);
     printf("SO_TIMESTAMPING ");
     printf("HW raw %ld.%09ld",
      (long)stamp->hwtimeraw->tv_sec,
      (long)stamp->hwtimeraw->tv_nsec);

break;
}

or did I forget something?

Thank you,
Martin

Quoted 15 lines
Quoted 8 lines
Quoted 4 lines

Or will it be easier for me just to change can_frame structure? :)

No.

Please use the forseen interface.

Quoted 6 lines

Wolfgang
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

On Mon, 21 Oct 2013 11:51:20 +0200, Martin Kozusky <[email protected]>

Quoted 155 lines · wrote:

Dne 21.10.2013 10:54, Martin Kozusky napsal(a):

Quoted 153 lines

Dne 21.10.2013 10:11, Wolfgang Grandegger napsal(a):

Quoted 151 lines

My previous mail went out to early, sorry... completing it now...

On Mon, 21 Oct 2013 10:00:28 +0200, Wolfgang Grandegger

Quoted 31 lines · <[email protected]> wrote:

On Mon, 21 Oct 2013 08:54:30 +0200, Martin Kozusky

<[email protected]>

Quoted 21 lines · wrote:

Dne 18.10.2013 16:42, Oliver Hartkopp napsal(a):

Quoted 17 lines · Martin Kozusky
Quoted 16 lines

Dne 18.10.2013 11:56, Martin Kozusky napsal(a):

Quoted 5 lines

Hello,

is there any official way how to read timestamp from flexcans

message

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

buffer?

I see there is a macro defined FLEXCAN_MB_CNT_TIMESTAMP in

flexcan.c,

Quoted 85 lines
Quoted 28 lines
Quoted 25 lines
Quoted 25 lines
Quoted 12 lines

but not used.

I would modify function flecan_read_fifo like

Quoted 1 line

+ u32 timestamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);

sorry, u32 should be just u16

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

but what to do with timestamp variable next and how to read it in

userspace? :) I could change structure can_frame and add u32

timestamp

Quoted 45 lines
Quoted 45 lines
Quoted 29 lines

also u16, not u32

Quoted 21 lines

into it, but it wouldn't be very nice hack I think.

BTW: is timestamp read with SO_TIMESTAMP actualy taken when CAN

message

arrives?

Hello Martin,

with SO_TIMESTAMP you get the system timestamp in nanosec

resolution.

Quoted 36 lines
Quoted 35 lines
Quoted 35 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

It is set at the time when the CAN frame is pushed to the network

layer

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

by

the CAN drivers interrupt routine.

See net_timestamp_check() in netif_rx():

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c#n3228

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Hello Oliver,

I'm already using SO_TIMESTAMP with recvmmsg so may be I should

switch

Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
Quoted 15 lines
Quoted 15 lines

to SO_TIMESTAMPNS for better times, right?

Quoted 5 lines

You may get this timestamp via ioctl() or recvmsg().

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

To pass hardware timestamps there's some infrastructure for

ethernet

Quoted 36 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

devices in the

mainlaine kernel, which is configured with "ethtool":

ethtool -T|--show-time-stamping DEVNAME Show time stamping

capabilities

See details at

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping.txt

Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines

And some example source code at

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/timestamping/timestamping.c

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

There needs to be added some infrastructure to the network devices

to

Quoted 38 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

query

and configure the timestamping possibilities.

This is currently missing for CAN devices ...

Even if I moved a wrongly assigned hardware timestamp to another

place

here:

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/drivers/net/can/usb/peak_usb?id=c9faaa09e2a1335678f09c70a0d0eda095564bab

Quoted 84 lines
Quoted 83 lines
Quoted 76 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

the rest of the hardware timestamping (& configuration) is still

missing.

Supporting SIOCSHWTSTAMP seem the right way to go.

Quoted 12 lines
Quoted 9 lines

So I could just do

Quoted 5 lines
Quoted 5 lines
   struct skb_shared_hwtstamps *hwts;



   hwts = skb_hwtstamps(skb);

Most drivers do as well:

  memset(hwts, 0, sizeof(*hwts));
Quoted 1 line
Quoted 1 line
   hwts->hwtstamp = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);
Quoted 1 line

This should assign a proper ktime. [nm]s_to_ktime() may help.

But value read from registers is just 16bit value having nothing to do

with real time. But now when I see the ns_to_ktime function, it will

Quoted 7 lines
Quoted 7 lines

just assign my 16bit value to .tv64 union member, ok I will use it.

But I see another problem here:

flexcan registers are read in function flexcan_read_fifo but I need

to

Quoted 7 lines
Quoted 7 lines

know the timestamp in function flexcan_read_frame, how do I transfer it

from there?

http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/flexcan.c#n519

Quoted 30 lines
Quoted 29 lines

Should I just create some global variable in flexcan.c and store the

16bit value into it and then use it?

Quoted 17 lines
Quoted 15 lines
Quoted 11 lines

in flecan_read_fifo ?

And then how to read it?

Quoted 13 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

See:

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

http://lxr.free-electrons.com/source/Documentation/networking/timestamping/

Quoted 64 lines
Quoted 54 lines
Quoted 11 lines

This also shows how to do read the timestamp.

So you mean this setup:

   memset(&hwtstamp, 0, sizeof(hwtstamp));

   strncpy(hwtstamp.ifr_name, interface, sizeof(hwtstamp.ifr_name));

   hwtstamp.ifr_data = (void *)&hwconfig;

   memset(&hwconfig, 0, sizeof(hwconfig));

   hwconfig.tx_type = HWTSTAMP_TX_OFF;

   hwconfig.rx_filter = HWTSTAMP_FILTER_ALL;

   hwconfig_requested = hwconfig;

   if (ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) < 0) {
      if ((errno == EINVAL || errno == ENOTSUP) &&

          hwconfig_requested.tx_type == HWTSTAMP_TX_OFF &&

          hwconfig_requested.rx_filter == HWTSTAMP_FILTER_NONE)

        printf("SIOCSHWTSTAMP: disabling hardware time stamping not

possible\n");

      else

        bail("SIOCSHWTSTAMP");
   }
Quoted 7 lines

printf("SIOCSHWTSTAMP: tx_type %d requested, got %d; rx_filter %d

requested, got %d\n",

hwconfig_requested.tx_type, hwconfig.tx_type,

hwconfig_requested.rx_filter, hwconfig.rx_filter);

Quoted 5 lines

When I do

ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) it returns ENOTSUP :( do I have

to define this ioctl somehow in the flexcan driver or somewhere else?

Yes, it looks like. Doing an "identifier search" for SIOCSHWTSTAMP in

"http://lxr.free-electrons.com/source/" should list various drivers

implementing that request.

Wolfgang.

Quoted 77 lines

Martin

Quoted 73 lines

and read from this cmsg_type:

case SO_TIMESTAMPING: {

   struct scm_timestamping *stamp =

   (struct scm_timestamping *)CMSG_DATA(cmsg);

     printf("SO_TIMESTAMPING ");

     printf("HW raw %ld.%09ld",

      (long)stamp->hwtimeraw->tv_sec,

      (long)stamp->hwtimeraw->tv_nsec);

break;

}

or did I forget something?

Thank you,

Martin

Quoted 35 lines
Quoted 14 lines
Quoted 7 lines

Or will it be easier for me just to change can_frame structure? :)

Quoted 1 line

No.

Please use the forseen interface.

Wolfgang

--

To unsubscribe from this list: send the line "unsubscribe linux-can"

in

Quoted 29 lines
Quoted 10 lines
Quoted 5 lines

the body of a message to [email protected]

More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 9 lines

--

To unsubscribe from this list: send the line "unsubscribe linux-can" in

the body of a message to [email protected]

More majordomo info at http://vger.kernel.org/majordomo-info.html

--

To unsubscribe from this list: send the line "unsubscribe linux-can" in

the body of a message to [email protected]

More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
Quoted 12 lines · Wolfgang Grandegger

On Mon, 21 Oct 2013 11:51:20 +0200, Martin Kozusky <[email protected]>

Quoted 5 lines

When I do

ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) it returns ENOTSUP :( do I have

to define this ioctl somehow in the flexcan driver or somewhere else?

Quoted 6 lines

Yes, it looks like. Doing an "identifier search" for SIOCSHWTSTAMP in

"http://lxr.free-electrons.com/source/" should list various drivers

implementing that request.

Hi Martin,

if you want to configure the selection of available timestamps, there needs to be implemented something in the driver itself.

E.g. if a network driver supports filtering for special packet types to send the timestamps only for these packets, it need to present it's capabilities and also needs to implement the existing configuration API.

E.g. if you say:

# ethtool -T can0
Time stamping parameters for can0:
Capabilities:
	software-receive      (SOF_TIMESTAMPING_RX_SOFTWARE)
	software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
PTP Hardware Clock: none
Hardware Transmit Timestamp Modes: none
Hardware Receive Filter Modes: none

This is the output from my SJA1000 based EMS PCMCIA card. But for the software device vcan0 it's the same.

From what i get from this interface is only software timestamps.

I played with candump yesterday:

candump.c +41 -2
index c865bd7..44a59b2 100644
@@ -60,6 +60,7 @@
 
 #include <linux/can.h>
 #include <linux/can/raw.h>
+#include <linux/net_tstamp.h>
 
 #include "terminal.h"
 #include "lib.h"
@@ -537,6 +538,7 @@ int main(int argc, char **argv)
 
 		if (timestamp || log || logfrmt) {
 
+#if 1
 			const int timestamp_on = 1;
 
 			if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
@@ -544,6 +546,17 @@ int main(int argc, char **argv)
 				perror("setsockopt SO_TIMESTAMP");
 				return 1;
 			}
+#endif
+#if 1
+//			const int timestamping_flags = ( SOF_TIMESTAMPING_RX_SOFTWARE |  SOF_TIMESTAMPING_RX_HARDWARE |  SOF_TIMESTAMPING_SOFTWARE);
+			const int timestamping_flags = ( SOF_TIMESTAMPING_RX_SOFTWARE |  SOF_TIMESTAMPING_SOFTWARE);
+
+			if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMPING,
+				       &timestamping_flags, sizeof(timestamping_flags)) < 0) {
+				perror("setsockopt SO_TIMESTAMPING");
+				return 1;
+			}
+#endif
 		}
 
 		if (dropmonitor) {
@@ -664,10 +677,36 @@ int main(int argc, char **argv)
 				for (cmsg = CMSG_FIRSTHDR(&msg);
 				     cmsg && (cmsg->cmsg_level == SOL_SOCKET);
 				     cmsg = CMSG_NXTHDR(&msg,cmsg)) {
-					if (cmsg->cmsg_type == SO_TIMESTAMP)
+					if (cmsg->cmsg_type == SO_TIMESTAMP) {
+						printf("SO_TIMESTAMP\n");
 						tv = *(struct timeval *)CMSG_DATA(cmsg);
-					else if (cmsg->cmsg_type == SO_RXQ_OVFL)
+					} else if (cmsg->cmsg_type == SO_TIMESTAMPNS) {
+						printf("SO_TIMESTAMPNS\n");
+						tv = *(struct timeval *)CMSG_DATA(cmsg);
+					} else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
+
+						struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
+
+						printf("SO_TIMESTAMPING\n");
+						tv = *(struct timeval *)CMSG_DATA(cmsg);
+
+						printf("SW %lu.%09lu ",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						stamp++;
+						printf("HW transformed %lu.%09lu ",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						stamp++;
+						printf("HW raw %lu.%09lu",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						printf("\n");
+
+					} else if (cmsg->cmsg_type == SO_RXQ_OVFL) {
+						printf("SO_RXQ_OVFL\n");
 						dropcnt[i] = *(__u32 *)CMSG_DATA(cmsg);
+					} else printf("cmsg->cmsg_type = %d\n", cmsg->cmsg_type);
 				}
 
 				/* check for (unlikely) dropped frames on this specific socket */

The problem is, when i set both SO_TIMESTAMP AND SO_TIMESTAMPING i get trash in the SO_TIMESTAMPING values.

When I set either SO_TIMESTAMP OR SO_TIMESTAMPING i can see a good and valid software timestamp. (compared with an unmodified candump).

Any idea about that?

Best regards,
Oliver

#10 lore raw

Dne 21.10.2013 18:29, Oliver Hartkopp napsal(a):

Quoted 128 lines · Wolfgang Grandegger
Quoted 12 lines

On Mon, 21 Oct 2013 11:51:20 +0200, Martin Kozusky <[email protected]>

Quoted 5 lines

When I do

ioctl(sock, SIOCSHWTSTAMP, &hwtstamp) it returns ENOTSUP :( do I have

to define this ioctl somehow in the flexcan driver or somewhere else?

Quoted 6 lines

Yes, it looks like. Doing an "identifier search" for SIOCSHWTSTAMP in

"http://lxr.free-electrons.com/source/" should list various drivers

implementing that request.

Hi Martin,

if you want to configure the selection of available timestamps, there needs to be implemented something in the driver itself.

E.g. if a network driver supports filtering for special packet types to send the timestamps only for these packets, it need to present it's capabilities and also needs to implement the existing configuration API.

E.g. if you say:

# ethtool -T can0
Time stamping parameters for can0:
Capabilities:
	software-receive      (SOF_TIMESTAMPING_RX_SOFTWARE)
	software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
PTP Hardware Clock: none
Hardware Transmit Timestamp Modes: none
Hardware Receive Filter Modes: none

This is the output from my SJA1000 based EMS PCMCIA card. But for the software device vcan0 it's the same.

From what i get from this interface is only software timestamps.

I played with candump yesterday:

candump.c +41 -2
index c865bd7..44a59b2 100644
@@ -60,6 +60,7 @@
 
  #include <linux/can.h>
  #include <linux/can/raw.h>
+#include <linux/net_tstamp.h>
 
  #include "terminal.h"
  #include "lib.h"
@@ -537,6 +538,7 @@ int main(int argc, char **argv)
 
  		if (timestamp || log || logfrmt) {
 
+#if 1
  			const int timestamp_on = 1;
 
  			if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
@@ -544,6 +546,17 @@ int main(int argc, char **argv)
  				perror("setsockopt SO_TIMESTAMP");
  				return 1;
  			}
+#endif
+#if 1
+//			const int timestamping_flags = ( SOF_TIMESTAMPING_RX_SOFTWARE |  SOF_TIMESTAMPING_RX_HARDWARE |  SOF_TIMESTAMPING_SOFTWARE);
+			const int timestamping_flags = ( SOF_TIMESTAMPING_RX_SOFTWARE |  SOF_TIMESTAMPING_SOFTWARE);
+
+			if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMPING,
+				       &timestamping_flags, sizeof(timestamping_flags)) < 0) {
+				perror("setsockopt SO_TIMESTAMPING");
+				return 1;
+			}
+#endif
  		}
 
  		if (dropmonitor) {
@@ -664,10 +677,36 @@ int main(int argc, char **argv)
  				for (cmsg = CMSG_FIRSTHDR(&msg);
  				     cmsg && (cmsg->cmsg_level == SOL_SOCKET);
  				     cmsg = CMSG_NXTHDR(&msg,cmsg)) {
-					if (cmsg->cmsg_type == SO_TIMESTAMP)
+					if (cmsg->cmsg_type == SO_TIMESTAMP) {
+						printf("SO_TIMESTAMP\n");
  						tv = *(struct timeval *)CMSG_DATA(cmsg);
-					else if (cmsg->cmsg_type == SO_RXQ_OVFL)
+					} else if (cmsg->cmsg_type == SO_TIMESTAMPNS) {
+						printf("SO_TIMESTAMPNS\n");
+						tv = *(struct timeval *)CMSG_DATA(cmsg);
+					} else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
+
+						struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
+
+						printf("SO_TIMESTAMPING\n");
+						tv = *(struct timeval *)CMSG_DATA(cmsg);
+
+						printf("SW %lu.%09lu ",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						stamp++;
+						printf("HW transformed %lu.%09lu ",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						stamp++;
+						printf("HW raw %lu.%09lu",
+						       (long)stamp->tv_sec,
+						       (long)stamp->tv_nsec);
+						printf("\n");
+
+					} else if (cmsg->cmsg_type == SO_RXQ_OVFL) {
+						printf("SO_RXQ_OVFL\n");
  						dropcnt[i] = *(__u32 *)CMSG_DATA(cmsg);
+					} else printf("cmsg->cmsg_type = %d\n", cmsg->cmsg_type);
  				}
 
  				/* check for (unlikely) dropped frames on this specific socket */
Quoted 7 lines

The problem is, when i set both SO_TIMESTAMP AND SO_TIMESTAMPING i get trash in the SO_TIMESTAMPING values.

When I set either SO_TIMESTAMP OR SO_TIMESTAMPING i can see a good and valid software timestamp. (compared with an unmodified candump).

Any idea about that?

Hello Oliver,
today I was searching kernel sources for the place where SO_TIMESTAMPING msg is filled with data from skb_shared_hwtstamps. I found a place in net/socket.c - function __sock_recv_timestamp
I found that I just need to use those flags:

const int so_timestamping_flags = SOF_TIMESTAMPING_SOFTWARE|SOF_TIMESTAMPING_RAW_HARDWARE|SOF_TIMESTAMPING_SYS_HARDWARE

to have all 3 items of scm_timestamping struct filled.

So I enabled it with setsockopt SO_TIMESTAMPING ...
And also was getting strange results when I got SO_TIMESTAMP enabled.

Then I was going through kernel sources to the closest level to user and debuged data from msg. Timestamps were always correct in kernel, but not in program. Then I noticed, that I have ctrmsg used in recvmsg
msg.msg_control = &ctrlmsg
msg.msg_controllen = sizeof(ctrlmsg);

defined as
char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))]; (yes, copied from candump when I was starting :)

I tried to use definition from
http://lxr.free-electrons.com/source/Documentation/networking/timestamping/timestamping.c

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

and wow, it's working now :)
I have all 3 timestamps available and correct. And I can also use SO_TIMESTAMPING and SO_TIMESTAMP together

I also added support for the call of ioctl/SIOCSHWTSTAMP to the flexcan driver (although it does nothing and later I removed this call from the program and just use setsockopt/SO_TIMESTAMPING)

this is my patch for flexcan.c, I could also add support for hwtstamp to error frames, but I don't need that now.

flexcan.c 2013-10-22 14:17:53.000000000 +0200 renamed from flexcan.c_old_2013_10_21 2013-10-21 14:01:56.000000000 +0200 +25 -1
@@ -459,6 +459,8 @@
        return 1;
 }
 
+u16 rx_time;
+
 static void flexcan_read_fifo(const struct net_device *dev,
                              struct can_frame *cf)
 {
@@ -481,7 +483,7 @@
 
        *(__be32 *)(cf->data + 0) = cpu_to_be32(readl(&mb->data[0]));
        *(__be32 *)(cf->data + 4) = cpu_to_be32(readl(&mb->data[1]));
-
+        rx_time = FLEXCAN_MB_CNT_TIMESTAMP(reg_ctrl);
        /* mark as read */
        writel(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, &regs->iflag1);
        readl(&regs->timer);
@@ -492,6 +494,7 @@
        struct net_device_stats *stats = &dev->stats;
        struct can_frame *cf;
        struct sk_buff *skb;
+        struct skb_shared_hwtstamps *hwts;
 
        skb = alloc_can_skb(dev, &cf);
        if (unlikely(!skb)) {
@@ -499,9 +502,15 @@
                return 0;
        }
 
+       skb->tstamp=ktime_get_real(); //get most accurate time, don't wait for netif_receive_skb assignment
+        hwts = skb_hwtstamps(skb);
+        memset(hwts, 0, sizeof(*hwts));
        flexcan_read_fifo(dev, cf);
+        hwts->hwtstamp = ns_to_ktime(rx_time);
+        hwts->syststamp = skb->tstamp;
        netif_receive_skb(skb);
 
+       //dev_info(dev->dev.parent, "hwtstamp = %06d\n", rx_time); //timestamp from registers is OK here
        stats->rx_packets++;
        stats->rx_bytes += cf->can_dlc;
 
@@ -847,11 +856,26 @@
        return 0;
 }
 
+static int flexcan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
+{
+ switch (cmd)
+ {
+  case SIOCSHWTSTAMP:
+   return 0;
+  break;
+  default:
+   return -EOPNOTSUPP;
+  break;
+ }
+
+ return EINVAL;
+}
 
 static const struct net_device_ops flexcan_netdev_ops = {
        .ndo_open       = flexcan_open,
        .ndo_stop       = flexcan_close,
        .ndo_start_xmit = flexcan_start_xmit,
+       .ndo_do_ioctl     = flexcan_ioctl
 };
 
 static int __devinit register_flexcandev(struct net_device *dev)

Martin

Quoted 8 lines

Best regards,
Oliver

--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

---
Tato zpráva neobsahuje viry ani jiný škodlivý kód - avast! Antivirus je aktivní.
http://www.avast.com

#11 lore raw
Quoted 8 lines · Martin Kozusky

Then I noticed, that I have ctrmsg used in recvmsg msg.msg_control = &ctrlmsg msg.msg_controllen = sizeof(ctrlmsg);

defined as
char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))]; (yes, copied from candump when I was starting :)

OMG!

What a shame. It's the classic situation not seeing a bug and a guy passing by your place is pointing at the problem on the monitor :-)

Quoted 10 lines

I tried to use definition from
http://lxr.free-electrons.com/source/Documentation/networking/timestamping/timestamping.c

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

and wow, it's working now :)

\o/

Quoted 2 lines

I have all 3 timestamps available and correct. And I can also use SO_TIMESTAMPING and SO_TIMESTAMP together

Me too.

Quoted 4 lines

I also added support for the call of ioctl/SIOCSHWTSTAMP to the flexcan driver (although it does nothing and later I removed this call from the program and just use setsockopt/SO_TIMESTAMPING)

Yes. I can see the HW timestamp from the PCAN USB driver directly now - after enabling the HW timestamping flags in the TIMESTAMPING sockopt().

Quoted 3 lines

this is my patch for flexcan.c, I could also add support for hwtstamp to error frames, but I don't need that now.

This would be a good idea to implement this for error messages too. I assume this was the reason to implement the ioctl() to configure if the driver should do the work of evaluating the extra timestamps.

Btw. we know how to deal with the API for now. I was searching for a monotonic SW timestamp that is not affected by any system time adjustments. Maybe SOF_TIMESTAMPING_SYS_HARDWARE could be used for this, when a HW timestamp is not available anyway.

Alternatively a fourth element could be added to the existing timestamps in

struct scm_timestamping {
        struct timespec systime;
        struct timespec hwtimetrans;
        struct timespec hwtimeraw;
        struct timespec monotonic; /* ??? */
};

Let's see ...

Thanks,
Oliver