Skip to contentSkip to conversation

selftests/bpf: Properly enable hwtstamp in xdp_hw_metadata

PATCH Superseded Stanislav Fomichev 3 participants 4 messages Jan 25, 2023 – Jan 27, 2023

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Properly enable hwtstamp in xdp_hw_metadata

The existing timestamping_enable() is a no-op because it applies to the socket-related path that we are not verifying here anymore. (but still leaving the code around hoping we can have xdp->skb path verified here as well)

poll: 1 (0)
xsk_ring_cons__peek: 1
0xf64788: rx_desc[0]->addr=100000000008000 addr=8100 comp_addr=8000
rx_hash: 3697961069

  rx_timestamp:  1674657672142214773 (sec:1674657672.1422)
  XDP RX-time:   1674657709561774876 (sec:1674657709.5618) delta sec:37.4196
  AF_XDP time:   1674657709561871034 (sec:1674657709.5619) delta

sec:0.0001 (96.158 usec)
0xf64788: complete idx=8 addr=8000

Also, maybe something to archive here, see [0] for Jesper's note about NIC vs host clock delta.

0: https://lore.kernel.org/bpf/[email protected]/

Fixes 297a3f124155 ("selftests/bpf: Simple program to dump XDP RX metadata")
---
 tools/testing/selftests/bpf/xdp_hw_metadata.c | 28 ++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/xdp_hw_metadata.c +27 -1
index 0008f0f239e8..dc899c53db5e 100644
@@ -24,6 +24,7 @@
 #include <linux/net_tstamp.h>
 #include <linux/udp.h>
 #include <linux/sockios.h>
+#include <linux/net_tstamp.h>
 #include <sys/mman.h>
 #include <net/if.h>
 #include <poll.h>
@@ -278,13 +279,36 @@ static int rxq_num(const char *ifname)
 
 	ret = ioctl(fd, SIOCETHTOOL, &ifr);
 	if (ret < 0)
-		error(-1, errno, "socket");
+		error(-1, errno, "ioctl(SIOCETHTOOL)");
 
 	close(fd);
 
 	return ch.rx_count + ch.combined_count;
 }
 
+static void hwtstamp_enable(const char *ifname)
+{
+	struct hwtstamp_config cfg = {
+		.rx_filter = HWTSTAMP_FILTER_ALL,
+	};
+
+	struct ifreq ifr = {
+		.ifr_data = (void *)&cfg,
+	};
+	strcpy(ifr.ifr_name, ifname);
+	int fd, ret;
+
+	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (fd < 0)
+		error(-1, errno, "socket");
+
+	ret = ioctl(fd, SIOCSHWTSTAMP, &ifr);
+	if (ret < 0)
+		error(-1, errno, "ioctl(SIOCSHWTSTAMP)");
+
+	close(fd);
+}
+
 static void cleanup(void)
 {
 	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
@@ -341,6 +365,8 @@ int main(int argc, char *argv[])
 
 	printf("rxq: %d\n", rxq);
 
+	hwtstamp_enable(ifname);
+
 	rx_xsk = malloc(sizeof(struct xsk) * rxq);
 	if (!rx_xsk)
 		error(-1, ENOMEM, "malloc");
Signature · 1 line
2.39.1.456.gfc5497dd1b-goog
#1 lore raw
Quoted 79 lines · Stanislav Fomichev

The existing timestamping_enable() is a no-op because it applies to the socket-related path that we are not verifying here anymore. (but still leaving the code around hoping we can have xdp->skb path verified here as well)

   poll: 1 (0)
   xsk_ring_cons__peek: 1
   0xf64788: rx_desc[0]->addr=100000000008000 addr=8100 comp_addr=8000
   rx_hash: 3697961069
   rx_timestamp:  1674657672142214773 (sec:1674657672.1422)
   XDP RX-time:   1674657709561774876 (sec:1674657709.5618) delta sec:37.4196
   AF_XDP time:   1674657709561871034 (sec:1674657709.5619) delta

sec:0.0001 (96.158 usec)
0xf64788: complete idx=8 addr=8000

Also, maybe something to archive here, see [0] for Jesper's note about NIC vs host clock delta.

0: https://lore.kernel.org/bpf/[email protected]/

Fixes 297a3f124155 ("selftests/bpf: Simple program to dump XDP RX metadata")
---
  tools/testing/selftests/bpf/xdp_hw_metadata.c | 28 ++++++++++++++++++-
  1 file changed, 27 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/xdp_hw_metadata.c +25 -1
index 0008f0f239e8..dc899c53db5e 100644
@@ -24,6 +24,7 @@
  #include <linux/net_tstamp.h>
  #include <linux/udp.h>
  #include <linux/sockios.h>
+#include <linux/net_tstamp.h>
  #include <sys/mman.h>
  #include <net/if.h>
  #include <poll.h>
@@ -278,13 +279,36 @@ static int rxq_num(const char *ifname)
 
  	ret = ioctl(fd, SIOCETHTOOL, &ifr);
  	if (ret < 0)
-		error(-1, errno, "socket");
+		error(-1, errno, "ioctl(SIOCETHTOOL)");
 
  	close(fd);
 
  	return ch.rx_count + ch.combined_count;
  }
 
+static void hwtstamp_enable(const char *ifname)
+{
+	struct hwtstamp_config cfg = {
+		.rx_filter = HWTSTAMP_FILTER_ALL,
+	};
+
+	struct ifreq ifr = {
+		.ifr_data = (void *)&cfg,
+	};
+	strcpy(ifr.ifr_name, ifname);
+	int fd, ret;
+
+	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+	if (fd < 0)
+		error(-1, errno, "socket");
+
+	ret = ioctl(fd, SIOCSHWTSTAMP, &ifr);
+	if (ret < 0)
+		error(-1, errno, "ioctl(SIOCSHWTSTAMP)");
+
+	close(fd);
+}
+
  static void cleanup(void)
  {
  	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);

tools/testing/selftests/bpf/xdp_hw_metadata.c int main(int argc, char *argv[]) · line 368

Quoted 4 lines
tools/testing/selftests/bpf/xdp_hw_metadata.c +1 -0
@@ -341,6 +365,8 @@ int main(int argc, char *argv[])
 
  	printf("rxq: %d\n", rxq);
 
+	hwtstamp_enable(ifname);

Will it to restore to the earlier setting when the process exits?

#2 lore raw
Quoted 82 lines · Martin KaFai Lau
Quoted 80 lines · Stanislav Fomichev

The existing timestamping_enable() is a no-op because it applies to the socket-related path that we are not verifying here anymore. (but still leaving the code around hoping we can have xdp->skb path verified here as well)

   poll: 1 (0)
   xsk_ring_cons__peek: 1
   0xf64788: rx_desc[0]->addr=100000000008000 addr=8100 comp_addr=8000
   rx_hash: 3697961069
   rx_timestamp:  1674657672142214773 (sec:1674657672.1422)
   XDP RX-time:   1674657709561774876 (sec:1674657709.5618) delta sec:37.4196
   AF_XDP time:   1674657709561871034 (sec:1674657709.5619) delta

sec:0.0001 (96.158 usec)
0xf64788: complete idx=8 addr=8000

Also, maybe something to archive here, see [0] for Jesper's note about NIC vs host clock delta.

0: https://lore.kernel.org/bpf/[email protected]/

Fixes 297a3f124155 ("selftests/bpf: Simple program to dump XDP RX metadata")
---
  tools/testing/selftests/bpf/xdp_hw_metadata.c | 28 ++++++++++++++++++-
  1 file changed, 27 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/xdp_hw_metadata.c +25 -1
index 0008f0f239e8..dc899c53db5e 100644
@@ -24,6 +24,7 @@
  #include <linux/net_tstamp.h>
  #include <linux/udp.h>
  #include <linux/sockios.h>
+#include <linux/net_tstamp.h>
  #include <sys/mman.h>
  #include <net/if.h>
  #include <poll.h>
@@ -278,13 +279,36 @@ static int rxq_num(const char *ifname)
 
      ret = ioctl(fd, SIOCETHTOOL, &ifr);
      if (ret < 0)
-             error(-1, errno, "socket");
+             error(-1, errno, "ioctl(SIOCETHTOOL)");
 
      close(fd);
 
      return ch.rx_count + ch.combined_count;
  }
 
+static void hwtstamp_enable(const char *ifname)
+{
+     struct hwtstamp_config cfg = {
+             .rx_filter = HWTSTAMP_FILTER_ALL,
+     };
+
+     struct ifreq ifr = {
+             .ifr_data = (void *)&cfg,
+     };
+     strcpy(ifr.ifr_name, ifname);
+     int fd, ret;
+
+     fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+     if (fd < 0)
+             error(-1, errno, "socket");
+
+     ret = ioctl(fd, SIOCSHWTSTAMP, &ifr);
+     if (ret < 0)
+             error(-1, errno, "ioctl(SIOCSHWTSTAMP)");
+
+     close(fd);
+}
+
  static void cleanup(void)
  {
      LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
@@ -341,6 +365,8 @@ int main(int argc, char *argv[])
 
Quoted 5 lines

tools/testing/selftests/bpf/xdp_hw_metadata.c int main(int argc, char *argv[]) · line 368

Quoted 3 lines
tools/testing/selftests/bpf/xdp_hw_metadata.c +1 -0
      printf("rxq: %d\n", rxq);
 
+     hwtstamp_enable(ifname);

Will it to restore to the earlier setting when the process exits?

Nope, do you think we should? Probably a good idea, let me add that...

#3 lore raw
In reply to Stanislav Fomichev
Quoted 5 lines · Stanislav Fomichev

The existing timestamping_enable() is a no-op because it applies to the socket-related path that we are not verifying here anymore. (but still leaving the code around hoping we can have xdp->skb path verified here as well)

Quoted 7 lines
   poll: 1 (0)
   xsk_ring_cons__peek: 1
   0xf64788: rx_desc[0]->addr=100000000008000 addr=8100 comp_addr=8000
   rx_hash: 3697961069
   rx_timestamp:  1674657672142214773 (sec:1674657672.1422)
   XDP RX-time:   1674657709561774876 (sec:1674657709.5618) delta sec:37.4196
   AF_XDP time:   1674657709561871034 (sec:1674657709.5619) delta sec:0.0001 (96.158 usec)

This output contains some extra output data, which is not part of current upstream.
I will soon submit an RFC-patch with this extra output to discuss and figure out what timestamp type we want/expect HW to provide.

Quoted 7 lines
   0xf64788: complete idx=8 addr=8000

Also, maybe something to archive here, see [0] for Jesper's note about NIC vs host clock delta.

0: https://lore.kernel.org/bpf/[email protected]/

--Jesper