Re: [PATCH 1/4] kernel: time: Add current_nw_timestamp() for network timestamps
Hi, Deepa Dinamani wrote: >>> include/linux/ip.h | 2 ++ >>> include/linux/time64.h | 3 +++ >>> kernel/time/time.c | 26 ++ >>> 3 files changed, 31 insertions(+) >>> >> Since net/ipv4/* are the only users, it is enough to put >> it in under net/ipv4/. > > time.c hosts functions that are used by individual subsystems like > current_fs_time() used by filesystems > (sometimes used by other subsystems also). > > The network timestamp function is used for both source route ip option > and timestamp icmp messages. > So it makes it difficult for it to be owned by a single layer. > This is the reason it was chosen to include here. > > Another option is to include it in the lowest layer its used: > af_inet.c. Is this what you were suggesting? > Yes, that's right. --yoshfuji > -Deepa >
Re: [Y2038] [PATCH 1/4] kernel: time: Add current_nw_timestamp() for network timestamps
On Wednesday 24 February 2016 23:07:08 Deepa Dinamani wrote: > ICMP timestamp messages and IP source route options require > timestamps to be in milliseconds modulo 24 hours from > midnight UTC format. > > Add a time function to support this. The function returns the > required timestamp in network byte order. > > The function also uses y2038 safe time functions and data > structures. > > Signed-off-by: Deepa Dinamani > For the implementation: Reviewed-by: Arnd Bergmann For where to put the code, I have no better idea, but I agree with Hideaki Yoshifuji that it's a bit odd to place it in kernel/*.c when it's only used by net/ipv4. Arnd
Re: [PATCH 1/4] kernel: time: Add current_nw_timestamp() for network timestamps
>> include/linux/ip.h | 2 ++ >> include/linux/time64.h | 3 +++ >> kernel/time/time.c | 26 ++ >> 3 files changed, 31 insertions(+) >> >> diff --git a/include/linux/ip.h b/include/linux/ip.h >> index 492bc65..edf923e 100644 >> --- a/include/linux/ip.h >> +++ b/include/linux/ip.h >> @@ -34,4 +34,6 @@ static inline struct iphdr *ipip_hdr(const struct sk_buff >> *skb) >> { >> return (struct iphdr *)skb_transport_header(skb); >> } >> + >> +extern __be32 current_nw_timestamp(void); >> #endif /* _LINUX_IP_H */ >> diff --git a/include/linux/time64.h b/include/linux/time64.h >> index 367d5af..5b5db3b 100644 >> --- a/include/linux/time64.h >> +++ b/include/linux/time64.h >> @@ -35,6 +35,9 @@ struct itimerspec64 { >> #define NSEC_PER_SEC 10L >> #define FSEC_PER_SEC 1000LL >> >> +#define SECONDS_PER_DAY 86400 >> +#define MSEC_PER_DAY (SECONDS_PER_DAY * MSEC_PER_SEC) >> + >> /* Located here for timespec[64]_valid_strict */ >> #define TIME64_MAX ((s64)~((u64)1 << 63)) >> #define KTIME_MAX((s64)~((u64)1 << 63)) >> diff --git a/kernel/time/time.c b/kernel/time/time.c >> index 86751c6..6df15df 100644 >> --- a/kernel/time/time.c >> +++ b/kernel/time/time.c >> @@ -245,6 +245,32 @@ struct timespec current_fs_time(struct super_block *sb) >> EXPORT_SYMBOL(current_fs_time); >> >> /* >> + * current_nw_timestamp - Return network time >> + * >> + * Return milliseconds since midnight in network byte order. >> + */ >> +__be32 current_nw_timestamp(void) >> +{ >> + u64 now; >> + u32 secs; >> + u32 msecs; >> + struct timespec64 ts; >> + >> + ktime_get_ts64(&ts); >> + >> + /* Get secs since midnight. */ >> + now = div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs); >> + /* Convert to msecs. */ >> + msecs = secs * MSEC_PER_SEC; >> + /* Convert nsec to msec. */ >> + msecs += (u32)ts.tv_nsec/NSEC_PER_MSEC; >> + >> + /* Convert to network byte order. */ >> + return htons(msecs); >> +} >> +EXPORT_SYMBOL(current_nw_timestamp); >> + >> +/* >> * Convert jiffies to milliseconds and back. >> * >> * Avoid unnecessary multiplications/divisions in the >> > > Since net/ipv4/* are the only users, it is enough to put > it in under net/ipv4/. time.c hosts functions that are used by individual subsystems like current_fs_time() used by filesystems (sometimes used by other subsystems also). The network timestamp function is used for both source route ip option and timestamp icmp messages. So it makes it difficult for it to be owned by a single layer. This is the reason it was chosen to include here. Another option is to include it in the lowest layer its used: af_inet.c. Is this what you were suggesting? -Deepa
Re: [PATCH 1/4] kernel: time: Add current_nw_timestamp() for network timestamps
Hi, Deepa Dinamani wrote: > ICMP timestamp messages and IP source route options require > timestamps to be in milliseconds modulo 24 hours from > midnight UTC format. > > Add a time function to support this. The function returns the > required timestamp in network byte order. > > The function also uses y2038 safe time functions and data > structures. > > Signed-off-by: Deepa Dinamani > Cc: John Stultz > Cc: Thomas Gleixner > Cc: "David S. Miller" > Cc: Alexey Kuznetsov > Cc: James Morris > Cc: Hideaki YOSHIFUJI > Cc: Patrick McHardy > --- > include/linux/ip.h | 2 ++ > include/linux/time64.h | 3 +++ > kernel/time/time.c | 26 ++ > 3 files changed, 31 insertions(+) > > diff --git a/include/linux/ip.h b/include/linux/ip.h > index 492bc65..edf923e 100644 > --- a/include/linux/ip.h > +++ b/include/linux/ip.h > @@ -34,4 +34,6 @@ static inline struct iphdr *ipip_hdr(const struct sk_buff > *skb) > { > return (struct iphdr *)skb_transport_header(skb); > } > + > +extern __be32 current_nw_timestamp(void); > #endif /* _LINUX_IP_H */ > diff --git a/include/linux/time64.h b/include/linux/time64.h > index 367d5af..5b5db3b 100644 > --- a/include/linux/time64.h > +++ b/include/linux/time64.h > @@ -35,6 +35,9 @@ struct itimerspec64 { > #define NSEC_PER_SEC 10L > #define FSEC_PER_SEC 1000LL > > +#define SECONDS_PER_DAY 86400 > +#define MSEC_PER_DAY (SECONDS_PER_DAY * MSEC_PER_SEC) > + > /* Located here for timespec[64]_valid_strict */ > #define TIME64_MAX ((s64)~((u64)1 << 63)) > #define KTIME_MAX((s64)~((u64)1 << 63)) > diff --git a/kernel/time/time.c b/kernel/time/time.c > index 86751c6..6df15df 100644 > --- a/kernel/time/time.c > +++ b/kernel/time/time.c > @@ -245,6 +245,32 @@ struct timespec current_fs_time(struct super_block *sb) > EXPORT_SYMBOL(current_fs_time); > > /* > + * current_nw_timestamp - Return network time > + * > + * Return milliseconds since midnight in network byte order. > + */ > +__be32 current_nw_timestamp(void) > +{ > + u64 now; > + u32 secs; > + u32 msecs; > + struct timespec64 ts; > + > + ktime_get_ts64(&ts); > + > + /* Get secs since midnight. */ > + now = div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs); > + /* Convert to msecs. */ > + msecs = secs * MSEC_PER_SEC; > + /* Convert nsec to msec. */ > + msecs += (u32)ts.tv_nsec/NSEC_PER_MSEC; > + > + /* Convert to network byte order. */ > + return htons(msecs); > +} > +EXPORT_SYMBOL(current_nw_timestamp); > + > +/* > * Convert jiffies to milliseconds and back. > * > * Avoid unnecessary multiplications/divisions in the > Since net/ipv4/* are the only users, it is enough to put it in under net/ipv4/. -- Hideaki Yoshifuji Technical Division, MIRACLE LINUX CORPORATION
[PATCH 1/4] kernel: time: Add current_nw_timestamp() for network timestamps
ICMP timestamp messages and IP source route options require timestamps to be in milliseconds modulo 24 hours from midnight UTC format. Add a time function to support this. The function returns the required timestamp in network byte order. The function also uses y2038 safe time functions and data structures. Signed-off-by: Deepa Dinamani Cc: John Stultz Cc: Thomas Gleixner Cc: "David S. Miller" Cc: Alexey Kuznetsov Cc: James Morris Cc: Hideaki YOSHIFUJI Cc: Patrick McHardy --- include/linux/ip.h | 2 ++ include/linux/time64.h | 3 +++ kernel/time/time.c | 26 ++ 3 files changed, 31 insertions(+) diff --git a/include/linux/ip.h b/include/linux/ip.h index 492bc65..edf923e 100644 --- a/include/linux/ip.h +++ b/include/linux/ip.h @@ -34,4 +34,6 @@ static inline struct iphdr *ipip_hdr(const struct sk_buff *skb) { return (struct iphdr *)skb_transport_header(skb); } + +extern __be32 current_nw_timestamp(void); #endif /* _LINUX_IP_H */ diff --git a/include/linux/time64.h b/include/linux/time64.h index 367d5af..5b5db3b 100644 --- a/include/linux/time64.h +++ b/include/linux/time64.h @@ -35,6 +35,9 @@ struct itimerspec64 { #define NSEC_PER_SEC 10L #define FSEC_PER_SEC 1000LL +#define SECONDS_PER_DAY86400 +#define MSEC_PER_DAY (SECONDS_PER_DAY * MSEC_PER_SEC) + /* Located here for timespec[64]_valid_strict */ #define TIME64_MAX ((s64)~((u64)1 << 63)) #define KTIME_MAX ((s64)~((u64)1 << 63)) diff --git a/kernel/time/time.c b/kernel/time/time.c index 86751c6..6df15df 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -245,6 +245,32 @@ struct timespec current_fs_time(struct super_block *sb) EXPORT_SYMBOL(current_fs_time); /* + * current_nw_timestamp - Return network time + * + * Return milliseconds since midnight in network byte order. + */ +__be32 current_nw_timestamp(void) +{ + u64 now; + u32 secs; + u32 msecs; + struct timespec64 ts; + + ktime_get_ts64(&ts); + + /* Get secs since midnight. */ + now = div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs); + /* Convert to msecs. */ + msecs = secs * MSEC_PER_SEC; + /* Convert nsec to msec. */ + msecs += (u32)ts.tv_nsec/NSEC_PER_MSEC; + + /* Convert to network byte order. */ + return htons(msecs); +} +EXPORT_SYMBOL(current_nw_timestamp); + +/* * Convert jiffies to milliseconds and back. * * Avoid unnecessary multiplications/divisions in the -- 1.9.1