netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api
@ 2013-04-16 11:29 Daniel Borkmann
  2013-04-16 11:29 ` [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper Daniel Borkmann
  2013-04-19 20:50 ` [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api David Miller
  0 siblings, 2 replies; 5+ messages in thread
From: Daniel Borkmann @ 2013-04-16 11:29 UTC (permalink / raw)
  To: davem; +Cc: netdev, willemb, hawk

Currently, ktime2ts is a small helper function that is only used in
net/socket.c. Move this helper into the ktime API as a small inline
function, so that i) it's maintained together with ktime routines,
and ii) also other files can make use of it. The function is named
ktime_to_timespec_cond() and placed into the generic part of ktime,
since we internally make use of ktime_to_timespec(). ktime_to_timespec()
itself does not check the ktime variable for zero, hence, we name
this function ktime_to_timespec_cond() for only a conditional
conversion, and adapt its users to it.

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
---
 ktime_to_timespec_cond() will be used in the 2nd patch as well.

 include/linux/ktime.h | 18 ++++++++++++++++++
 net/socket.c          | 20 ++++----------------
 2 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index e83512f..68ceba0 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -330,6 +330,24 @@ static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
 
 extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
 
+/**
+ * ktime_to_timespec_cond - convert a ktime_t variable to timespec
+ *			    format only if the variable contains data
+ * @kt:		the ktime_t variable to convert
+ * @ts:		the timespec variable to store the result in
+ *
+ * Returns true if there was a successful conversion, false if kt was 0.
+ */
+static inline bool ktime_to_timespec_cond(const ktime_t kt, struct timespec *ts)
+{
+	if (kt.tv64) {
+		*ts = ktime_to_timespec(kt);
+		return true;
+	} else {
+		return false;
+	}
+}
+
 /*
  * The resolution of the clocks. The resolution value is returned in
  * the clock_getres() system call to give application programmers an
diff --git a/net/socket.c b/net/socket.c
index 36883fe..280283f 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -681,16 +681,6 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
 }
 EXPORT_SYMBOL(kernel_sendmsg);
 
-static int ktime2ts(ktime_t kt, struct timespec *ts)
-{
-	if (kt.tv64) {
-		*ts = ktime_to_timespec(kt);
-		return 1;
-	} else {
-		return 0;
-	}
-}
-
 /*
  * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  */
@@ -723,17 +713,15 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
 
 
 	memset(ts, 0, sizeof(ts));
-	if (skb->tstamp.tv64 &&
-	    sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
-		skb_get_timestampns(skb, ts + 0);
+	if (sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE) &&
+	    ktime_to_timespec_cond(skb->tstamp, ts + 0))
 		empty = 0;
-	}
 	if (shhwtstamps) {
 		if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
-		    ktime2ts(shhwtstamps->syststamp, ts + 1))
+		    ktime_to_timespec_cond(shhwtstamps->syststamp, ts + 1))
 			empty = 0;
 		if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
-		    ktime2ts(shhwtstamps->hwtstamp, ts + 2))
+		    ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts + 2))
 			empty = 0;
 	}
 	if (!empty)
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper
  2013-04-16 11:29 [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api Daniel Borkmann
@ 2013-04-16 11:29 ` Daniel Borkmann
  2013-04-16 14:26   ` Willem de Bruijn
  2013-04-19 20:50   ` David Miller
  2013-04-19 20:50 ` [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api David Miller
  1 sibling, 2 replies; 5+ messages in thread
From: Daniel Borkmann @ 2013-04-16 11:29 UTC (permalink / raw)
  To: davem; +Cc: netdev, willemb, hawk

This patch introduces a small, internal helper function, that is used by
PF_PACKET. Based on the flags that are passed, it extracts the packet
timestamp in the receive path. This is merely a refactoring to remove
some duplicate code in tpacket_rcv(), to make it more readable, and to
enable others to use this function in PF_PACKET as well, e.g. for TX.

Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
---
 v1->v2:
   - To not break user space, I decided for now to not get rid of tp_tstamp
   - After some more thinking about it, I'm now with Willem to not make it
     too generic, thus having it stay in af_packet.c
   - Use ktime_to_timespec_cond() from patch 1 to make it more similar to
     __sock_recv_timestamp()

 net/packet/af_packet.c | 57 ++++++++++++++++++++------------------------------
 1 file changed, 23 insertions(+), 34 deletions(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 77d71f8..e381adf 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -1663,6 +1663,26 @@ drop:
 	return 0;
 }
 
+static void tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
+				  unsigned int flags)
+{
+	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
+
+	if (shhwtstamps) {
+		if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
+		    ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
+			return;
+		if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
+		    ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
+			return;
+	}
+
+	if (ktime_to_timespec_cond(skb->tstamp, ts))
+		return;
+
+	getnstimeofday(ts);
+}
+
 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		       struct packet_type *pt, struct net_device *orig_dev)
 {
@@ -1681,9 +1701,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	unsigned long status = TP_STATUS_USER;
 	unsigned short macoff, netoff, hdrlen;
 	struct sk_buff *copy_skb = NULL;
-	struct timeval tv;
 	struct timespec ts;
-	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
 
 	if (skb->pkt_type == PACKET_LOOPBACK)
 		goto drop;
@@ -1766,6 +1784,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 	spin_unlock(&sk->sk_receive_queue.lock);
 
 	skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
+	tpacket_get_timestamp(skb, &ts, po->tp_tstamp);
 
 	switch (po->tp_version) {
 	case TPACKET_V1:
@@ -1773,18 +1792,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		h.h1->tp_snaplen = snaplen;
 		h.h1->tp_mac = macoff;
 		h.h1->tp_net = netoff;
-		if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
-				&& shhwtstamps->syststamp.tv64)
-			tv = ktime_to_timeval(shhwtstamps->syststamp);
-		else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
-				&& shhwtstamps->hwtstamp.tv64)
-			tv = ktime_to_timeval(shhwtstamps->hwtstamp);
-		else if (skb->tstamp.tv64)
-			tv = ktime_to_timeval(skb->tstamp);
-		else
-			do_gettimeofday(&tv);
-		h.h1->tp_sec = tv.tv_sec;
-		h.h1->tp_usec = tv.tv_usec;
+		h.h1->tp_sec = ts.tv_sec;
+		h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
 		hdrlen = sizeof(*h.h1);
 		break;
 	case TPACKET_V2:
@@ -1792,16 +1801,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		h.h2->tp_snaplen = snaplen;
 		h.h2->tp_mac = macoff;
 		h.h2->tp_net = netoff;
-		if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
-				&& shhwtstamps->syststamp.tv64)
-			ts = ktime_to_timespec(shhwtstamps->syststamp);
-		else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
-				&& shhwtstamps->hwtstamp.tv64)
-			ts = ktime_to_timespec(shhwtstamps->hwtstamp);
-		else if (skb->tstamp.tv64)
-			ts = ktime_to_timespec(skb->tstamp);
-		else
-			getnstimeofday(&ts);
 		h.h2->tp_sec = ts.tv_sec;
 		h.h2->tp_nsec = ts.tv_nsec;
 		if (vlan_tx_tag_present(skb)) {
@@ -1822,16 +1821,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
 		h.h3->tp_snaplen = snaplen;
 		h.h3->tp_mac = macoff;
 		h.h3->tp_net = netoff;
-		if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
-				&& shhwtstamps->syststamp.tv64)
-			ts = ktime_to_timespec(shhwtstamps->syststamp);
-		else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
-				&& shhwtstamps->hwtstamp.tv64)
-			ts = ktime_to_timespec(shhwtstamps->hwtstamp);
-		else if (skb->tstamp.tv64)
-			ts = ktime_to_timespec(skb->tstamp);
-		else
-			getnstimeofday(&ts);
 		h.h3->tp_sec  = ts.tv_sec;
 		h.h3->tp_nsec = ts.tv_nsec;
 		hdrlen = sizeof(*h.h3);
-- 
1.7.11.7

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper
  2013-04-16 11:29 ` [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper Daniel Borkmann
@ 2013-04-16 14:26   ` Willem de Bruijn
  2013-04-19 20:50   ` David Miller
  1 sibling, 0 replies; 5+ messages in thread
From: Willem de Bruijn @ 2013-04-16 14:26 UTC (permalink / raw)
  To: Daniel Borkmann; +Cc: David Miller, netdev, hawk

On Tue, Apr 16, 2013 at 7:29 AM, Daniel Borkmann <dborkman@redhat.com> wrote:
> This patch introduces a small, internal helper function, that is used by
> PF_PACKET. Based on the flags that are passed, it extracts the packet
> timestamp in the receive path. This is merely a refactoring to remove
> some duplicate code in tpacket_rcv(), to make it more readable, and to
> enable others to use this function in PF_PACKET as well, e.g. for TX.

This looks great to me. Merge is dependent on the
ktime_to_timespec_cond patch, but once it makes it in, I will resubmit
txring timestamp to use this function.

> Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
> ---
>  v1->v2:
>    - To not break user space, I decided for now to not get rid of tp_tstamp
>    - After some more thinking about it, I'm now with Willem to not make it
>      too generic, thus having it stay in af_packet.c
>    - Use ktime_to_timespec_cond() from patch 1 to make it more similar to
>      __sock_recv_timestamp()
>
>  net/packet/af_packet.c | 57 ++++++++++++++++++++------------------------------
>  1 file changed, 23 insertions(+), 34 deletions(-)
>
> diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
> index 77d71f8..e381adf 100644
> --- a/net/packet/af_packet.c
> +++ b/net/packet/af_packet.c
> @@ -1663,6 +1663,26 @@ drop:
>         return 0;
>  }
>
> +static void tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
> +                                 unsigned int flags)
> +{
> +       struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
> +
> +       if (shhwtstamps) {
> +               if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
> +                   ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
> +                       return;
> +               if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
> +                   ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
> +                       return;
> +       }
> +
> +       if (ktime_to_timespec_cond(skb->tstamp, ts))
> +               return;
> +
> +       getnstimeofday(ts);
> +}
> +
>  static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>                        struct packet_type *pt, struct net_device *orig_dev)
>  {
> @@ -1681,9 +1701,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>         unsigned long status = TP_STATUS_USER;
>         unsigned short macoff, netoff, hdrlen;
>         struct sk_buff *copy_skb = NULL;
> -       struct timeval tv;
>         struct timespec ts;
> -       struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
>
>         if (skb->pkt_type == PACKET_LOOPBACK)
>                 goto drop;
> @@ -1766,6 +1784,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>         spin_unlock(&sk->sk_receive_queue.lock);
>
>         skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
> +       tpacket_get_timestamp(skb, &ts, po->tp_tstamp);
>
>         switch (po->tp_version) {
>         case TPACKET_V1:
> @@ -1773,18 +1792,8 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>                 h.h1->tp_snaplen = snaplen;
>                 h.h1->tp_mac = macoff;
>                 h.h1->tp_net = netoff;
> -               if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
> -                               && shhwtstamps->syststamp.tv64)
> -                       tv = ktime_to_timeval(shhwtstamps->syststamp);
> -               else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
> -                               && shhwtstamps->hwtstamp.tv64)
> -                       tv = ktime_to_timeval(shhwtstamps->hwtstamp);
> -               else if (skb->tstamp.tv64)
> -                       tv = ktime_to_timeval(skb->tstamp);
> -               else
> -                       do_gettimeofday(&tv);
> -               h.h1->tp_sec = tv.tv_sec;
> -               h.h1->tp_usec = tv.tv_usec;
> +               h.h1->tp_sec = ts.tv_sec;
> +               h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
>                 hdrlen = sizeof(*h.h1);
>                 break;
>         case TPACKET_V2:
> @@ -1792,16 +1801,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>                 h.h2->tp_snaplen = snaplen;
>                 h.h2->tp_mac = macoff;
>                 h.h2->tp_net = netoff;
> -               if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
> -                               && shhwtstamps->syststamp.tv64)
> -                       ts = ktime_to_timespec(shhwtstamps->syststamp);
> -               else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
> -                               && shhwtstamps->hwtstamp.tv64)
> -                       ts = ktime_to_timespec(shhwtstamps->hwtstamp);
> -               else if (skb->tstamp.tv64)
> -                       ts = ktime_to_timespec(skb->tstamp);
> -               else
> -                       getnstimeofday(&ts);
>                 h.h2->tp_sec = ts.tv_sec;
>                 h.h2->tp_nsec = ts.tv_nsec;
>                 if (vlan_tx_tag_present(skb)) {
> @@ -1822,16 +1821,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
>                 h.h3->tp_snaplen = snaplen;
>                 h.h3->tp_mac = macoff;
>                 h.h3->tp_net = netoff;
> -               if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
> -                               && shhwtstamps->syststamp.tv64)
> -                       ts = ktime_to_timespec(shhwtstamps->syststamp);
> -               else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
> -                               && shhwtstamps->hwtstamp.tv64)
> -                       ts = ktime_to_timespec(shhwtstamps->hwtstamp);
> -               else if (skb->tstamp.tv64)
> -                       ts = ktime_to_timespec(skb->tstamp);
> -               else
> -                       getnstimeofday(&ts);
>                 h.h3->tp_sec  = ts.tv_sec;
>                 h.h3->tp_nsec = ts.tv_nsec;
>                 hdrlen = sizeof(*h.h3);
> --
> 1.7.11.7
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api
  2013-04-16 11:29 [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api Daniel Borkmann
  2013-04-16 11:29 ` [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper Daniel Borkmann
@ 2013-04-19 20:50 ` David Miller
  1 sibling, 0 replies; 5+ messages in thread
From: David Miller @ 2013-04-19 20:50 UTC (permalink / raw)
  To: dborkman; +Cc: netdev, willemb, hawk

From: Daniel Borkmann <dborkman@redhat.com>
Date: Tue, 16 Apr 2013 13:29:10 +0200

> Currently, ktime2ts is a small helper function that is only used in
> net/socket.c. Move this helper into the ktime API as a small inline
> function, so that i) it's maintained together with ktime routines,
> and ii) also other files can make use of it. The function is named
> ktime_to_timespec_cond() and placed into the generic part of ktime,
> since we internally make use of ktime_to_timespec(). ktime_to_timespec()
> itself does not check the ktime variable for zero, hence, we name
> this function ktime_to_timespec_cond() for only a conditional
> conversion, and adapt its users to it.
> 
> Signed-off-by: Daniel Borkmann <dborkman@redhat.com>

Applied.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper
  2013-04-16 11:29 ` [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper Daniel Borkmann
  2013-04-16 14:26   ` Willem de Bruijn
@ 2013-04-19 20:50   ` David Miller
  1 sibling, 0 replies; 5+ messages in thread
From: David Miller @ 2013-04-19 20:50 UTC (permalink / raw)
  To: dborkman; +Cc: netdev, willemb, hawk

From: Daniel Borkmann <dborkman@redhat.com>
Date: Tue, 16 Apr 2013 13:29:11 +0200

> This patch introduces a small, internal helper function, that is used by
> PF_PACKET. Based on the flags that are passed, it extracts the packet
> timestamp in the receive path. This is merely a refactoring to remove
> some duplicate code in tpacket_rcv(), to make it more readable, and to
> enable others to use this function in PF_PACKET as well, e.g. for TX.
> 
> Signed-off-by: Daniel Borkmann <dborkman@redhat.com>

Applied.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-04-19 20:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-16 11:29 [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api Daniel Borkmann
2013-04-16 11:29 ` [PATCH net-next 2/2] packet: move hw/sw timestamp extraction into a small helper Daniel Borkmann
2013-04-16 14:26   ` Willem de Bruijn
2013-04-19 20:50   ` David Miller
2013-04-19 20:50 ` [PATCH net-next 1/2] net: socket: move ktime2ts to ktime header api David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).