Skip to content

Commit b9c32fb

Browse files
Daniel Borkmanndavem330
authored andcommitted
packet: if hw/sw ts enabled in rx/tx ring, report which ts we got
Currently, there is no way to find out which timestamp is reported in tpacket{,2,3}_hdr's tp_sec, tp_{n,u}sec members. It can be one of SOF_TIMESTAMPING_SYS_HARDWARE, SOF_TIMESTAMPING_RAW_HARDWARE, SOF_TIMESTAMPING_SOFTWARE, or a fallback variant late call from the PF_PACKET code in software. Therefore, report in the tp_status member of the ring buffer which timestamp has been reported for RX and TX path. This should not break anything for the following reasons: i) in RX ring path, the user needs to test for tp_status & TP_STATUS_USER, and later for other flags as well such as TP_STATUS_VLAN_VALID et al, so adding other flags will do no harm; ii) in TX ring path, time stamps with PACKET_TIMESTAMP socketoption are not available resp. had no effect except that the application setting this is buggy. Next to TP_STATUS_AVAILABLE, the user also should check for other flags such as TP_STATUS_WRONG_FORMAT to reclaim frames to the application. Thus, in case TX ts are turned off (default case), nothing happens to the application logic, and in case we want to use this new feature, we now can also check which of the ts source is reported in the status field as provided in the docs. Reported-by: Richard Cochran <richardcochran@gmail.com> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7276d5d commit b9c32fb

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎include/uapi/linux/if_packet.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ struct tpacket_auxdata {
100100
#define TP_STATUS_SENDING (1 << 1)
101101
#define TP_STATUS_WRONG_FORMAT (1 << 2)
102102

103+
/* Rx and Tx ring - header status */
104+
#define TP_STATUS_TS_SOFTWARE (1 << 29)
105+
#define TP_STATUS_TS_SYS_HARDWARE (1 << 30)
106+
#define TP_STATUS_TS_RAW_HARDWARE (1 << 31)
107+
103108
/* Rx ring - feature request bits */
104109
#define TP_FT_REQ_FILL_RXHASH 0x1
105110

‎net/packet/af_packet.c‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,34 +339,35 @@ static int __packet_get_status(struct packet_sock *po, void *frame)
339339
}
340340
}
341341

342-
static bool tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
343-
unsigned int flags)
342+
static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
343+
unsigned int flags)
344344
{
345345
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
346346

347347
if (shhwtstamps) {
348348
if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
349349
ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
350-
return true;
350+
return TP_STATUS_TS_SYS_HARDWARE;
351351
if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
352352
ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
353-
return true;
353+
return TP_STATUS_TS_RAW_HARDWARE;
354354
}
355355

356356
if (ktime_to_timespec_cond(skb->tstamp, ts))
357-
return true;
357+
return TP_STATUS_TS_SOFTWARE;
358358

359-
return false;
359+
return 0;
360360
}
361361

362-
static void __packet_set_timestamp(struct packet_sock *po, void *frame,
363-
struct sk_buff *skb)
362+
static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
363+
struct sk_buff *skb)
364364
{
365365
union tpacket_uhdr h;
366366
struct timespec ts;
367+
__u32 ts_status;
367368

368-
if (!tpacket_get_timestamp(skb, &ts, po->tp_tstamp))
369-
return;
369+
if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
370+
return 0;
370371

371372
h.raw = frame;
372373
switch (po->tp_version) {
@@ -387,6 +388,8 @@ static void __packet_set_timestamp(struct packet_sock *po, void *frame,
387388
/* one flush is safe, as both fields always lie on the same cacheline */
388389
flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
389390
smp_wmb();
391+
392+
return ts_status;
390393
}
391394

392395
static void *packet_lookup_frame(struct packet_sock *po,
@@ -1721,6 +1724,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
17211724
unsigned short macoff, netoff, hdrlen;
17221725
struct sk_buff *copy_skb = NULL;
17231726
struct timespec ts;
1727+
__u32 ts_status;
17241728

17251729
if (skb->pkt_type == PACKET_LOOPBACK)
17261730
goto drop;
@@ -1803,9 +1807,12 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
18031807
spin_unlock(&sk->sk_receive_queue.lock);
18041808

18051809
skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
1806-
if (!tpacket_get_timestamp(skb, &ts, po->tp_tstamp))
1810+
1811+
if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
18071812
getnstimeofday(&ts);
18081813

1814+
status |= ts_status;
1815+
18091816
switch (po->tp_version) {
18101817
case TPACKET_V1:
18111818
h.h1->tp_len = skb->len;
@@ -1905,11 +1912,14 @@ static void tpacket_destruct_skb(struct sk_buff *skb)
19051912
void *ph;
19061913

19071914
if (likely(po->tx_ring.pg_vec)) {
1915+
__u32 ts;
1916+
19081917
ph = skb_shinfo(skb)->destructor_arg;
19091918
BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
19101919
atomic_dec(&po->tx_ring.pending);
1911-
__packet_set_timestamp(po, ph, skb);
1912-
__packet_set_status(po, ph, TP_STATUS_AVAILABLE);
1920+
1921+
ts = __packet_set_timestamp(po, ph, skb);
1922+
__packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
19131923
}
19141924

19151925
sock_wfree(skb);

0 commit comments

Comments
 (0)