Skip to content

Commit

Permalink
[NET]: Introduce SIOCGSTAMPNS ioctl to get timestamps with nanosec re…
Browse files Browse the repository at this point in the history
…solution

Now network timestamps use ktime_t infrastructure, we can add a new
ioctl() SIOCGSTAMPNS command to get timestamps in 'struct timespec'.
User programs can thus access to nanosecond resolution.

Signed-off-by: Eric Dumazet <[email protected]>
CC: Stephen Hemminger <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Eric Dumazet authored and David S. Miller committed Apr 26, 2007
1 parent cb69cc5 commit ae40eb1
Show file tree
Hide file tree
Showing 37 changed files with 146 additions and 22 deletions.
18 changes: 18 additions & 0 deletions fs/compat_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,23 @@ static int do_siocgstamp(unsigned int fd, unsigned int cmd, unsigned long arg)
return err;
}

static int do_siocgstampns(unsigned int fd, unsigned int cmd, unsigned long arg)
{
struct compat_timespec __user *up = compat_ptr(arg);
struct timespec kts;
mm_segment_t old_fs = get_fs();
int err;

set_fs(KERNEL_DS);
err = sys_ioctl(fd, cmd, (unsigned long)&kts);
set_fs(old_fs);
if (!err) {
err = put_user(kts.tv_sec, &up->tv_sec);
err |= __put_user(kts.tv_nsec, &up->tv_nsec);
}
return err;
}

struct ifmap32 {
compat_ulong_t mem_start;
compat_ulong_t mem_end;
Expand Down Expand Up @@ -2437,6 +2454,7 @@ HANDLE_IOCTL(SIOCBRDELIF, dev_ifsioc)
/* Note SIOCRTMSG is no longer, so this is safe and * the user would have seen just an -EINVAL anyways. */
HANDLE_IOCTL(SIOCRTMSG, ret_einval)
HANDLE_IOCTL(SIOCGSTAMP, do_siocgstamp)
HANDLE_IOCTL(SIOCGSTAMPNS, do_siocgstampns)
#endif
#ifdef CONFIG_BLOCK
HANDLE_IOCTL(HDIO_GETGEO, hdio_getgeo)
Expand Down
3 changes: 2 additions & 1 deletion include/asm-alpha/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define SIOCSPGRP _IOW('s', 8, pid_t)
#define SIOCGPGRP _IOR('s', 9, pid_t)

#define SIOCGSTAMP 0x8906 /* Get stamp - linux-specific */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_ALPHA_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-arm/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-arm26/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-avr32/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* __ASM_AVR32_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-cris/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-frv/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_SOCKIOS__ */

3 changes: 2 additions & 1 deletion include/asm-h8300/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* __ARCH_H8300_SOCKIOS__ */
3 changes: 2 additions & 1 deletion include/asm-i386/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-ia64/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_IA64_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-m32r/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_M32R_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-m68k/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* __ARCH_M68K_SOCKIOS__ */
3 changes: 2 additions & 1 deletion include/asm-mips/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define SIOCSPGRP _IOW('s', 8, pid_t)
#define SIOCGPGRP _IOR('s', 9, pid_t)

#define SIOCGSTAMP 0x8906 /* Get stamp - linux-specific */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-parisc/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-powerpc/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _ASM_POWERPC_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-s390/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-sh/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#define SIOCSPGRP _IOW('s', 8, pid_t)
#define SIOCGPGRP _IOR('s', 9, pid_t)

#define SIOCGSTAMP _IOR('s', 100, struct timeval) /* Get stamp - linux-specific */
#define SIOCGSTAMP _IOR('s', 100, struct timeval) /* Get stamp (timeval) */
#define SIOCGSTAMPNS _IOR('s', 101, struct timespec) /* Get stamp (timespec) */
#endif /* __ASM_SH_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-sh64/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#define SIOCSPGRP _IOW('s', 8, pid_t)
#define SIOCGPGRP _IOR('s', 9, pid_t)

#define SIOCGSTAMP _IOR('s', 100, struct timeval) /* Get stamp - linux-specific */
#define SIOCGSTAMP _IOR('s', 100, struct timeval) /* Get stamp (timeval) */
#define SIOCGSTAMPNS _IOR('s', 101, struct timespec) /* Get stamp (timespec) */
#endif /* __ASM_SH64_SOCKIOS_H */
3 changes: 2 additions & 1 deletion include/asm-sparc/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* !(_ASM_SPARC_SOCKIOS_H) */

3 changes: 2 additions & 1 deletion include/asm-sparc64/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* !(_ASM_SPARC64_SOCKIOS_H) */

3 changes: 2 additions & 1 deletion include/asm-v850/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* __V850_SOCKIOS_H__ */
3 changes: 2 additions & 1 deletion include/asm-x86_64/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define FIOGETOWN 0x8903
#define SIOCGPGRP 0x8904
#define SIOCATMARK 0x8905
#define SIOCGSTAMP 0x8906 /* Get stamp */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif
3 changes: 2 additions & 1 deletion include/asm-xtensa/sockios.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define SIOCSPGRP _IOW('s', 8, pid_t)
#define SIOCGPGRP _IOR('s', 9, pid_t)

#define SIOCGSTAMP 0x8906 /* Get stamp - linux-specific */
#define SIOCGSTAMP 0x8906 /* Get stamp (timeval) */
#define SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */

#endif /* _XTENSA_SOCKIOS_H */
1 change: 1 addition & 0 deletions include/net/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct compat_cmsghdr {
};

extern int compat_sock_get_timestamp(struct sock *, struct timeval __user *);
extern int compat_sock_get_timestampns(struct sock *, struct timespec __user *);

#else /* defined(CONFIG_COMPAT) */
#define compat_msghdr msghdr /* to avoid compiler warnings */
Expand Down
1 change: 1 addition & 0 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb, int copied_e

extern void sock_enable_timestamp(struct sock *sk);
extern int sock_get_timestamp(struct sock *, struct timeval __user *);
extern int sock_get_timestampns(struct sock *, struct timespec __user *);

/*
* Enable debug/info messages
Expand Down
3 changes: 3 additions & 0 deletions net/appletalk/ddp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,9 @@ static int atalk_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
case SIOCGSTAMP:
rc = sock_get_timestamp(sk, argp);
break;
case SIOCGSTAMPNS:
rc = sock_get_timestampns(sk, argp);
break;
/* Routing */
case SIOCADDRT:
case SIOCDELRT:
Expand Down
3 changes: 3 additions & 0 deletions net/atm/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
case SIOCGSTAMP: /* borrowed from IP */
error = sock_get_timestamp(sk, argp);
goto done;
case SIOCGSTAMPNS: /* borrowed from IP */
error = sock_get_timestampns(sk, argp);
goto done;
case ATM_SETSC:
printk(KERN_WARNING "ATM_SETSC is obsolete\n");
error = 0;
Expand Down
4 changes: 4 additions & 0 deletions net/ax25/af_ax25.c
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,10 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
res = sock_get_timestamp(sk, argp);
break;

case SIOCGSTAMPNS:
res = sock_get_timestampns(sk, argp);
break;

case SIOCAX25ADDUID: /* Add a uid to the uid/call map table */
case SIOCAX25DELUID: /* Delete a uid from the uid/call map table */
case SIOCAX25GETUID: {
Expand Down
24 changes: 24 additions & 0 deletions net/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,30 @@ int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
}
EXPORT_SYMBOL(compat_sock_get_timestamp);

int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
{
struct compat_timespec __user *ctv =
(struct compat_timespec __user*) userstamp;
int err = -ENOENT;
struct timespec ts;

if (!sock_flag(sk, SOCK_TIMESTAMP))
sock_enable_timestamp(sk);
ts = ktime_to_timespec(sk->sk_stamp);
if (ts.tv_sec == -1)
return err;
if (ts.tv_sec == 0) {
sk->sk_stamp = ktime_get_real();
ts = ktime_to_timespec(sk->sk_stamp);
}
err = 0;
if (put_user(ts.tv_sec, &ctv->tv_sec) ||
put_user(ts.tv_nsec, &ctv->tv_nsec))
err = -EFAULT;
return err;
}
EXPORT_SYMBOL(compat_sock_get_timestampns);

asmlinkage long compat_sys_getsockopt(int fd, int level, int optname,
char __user *optval, int __user *optlen)
{
Expand Down
16 changes: 16 additions & 0 deletions net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,22 @@ int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
}
EXPORT_SYMBOL(sock_get_timestamp);

int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
{
struct timespec ts;
if (!sock_flag(sk, SOCK_TIMESTAMP))
sock_enable_timestamp(sk);
ts = ktime_to_timespec(sk->sk_stamp);
if (ts.tv_sec == -1)
return -ENOENT;
if (ts.tv_sec == 0) {
sk->sk_stamp = ktime_get_real();
ts = ktime_to_timespec(sk->sk_stamp);
}
return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;
}
EXPORT_SYMBOL(sock_get_timestampns);

void sock_enable_timestamp(struct sock *sk)
{
if (!sock_flag(sk, SOCK_TIMESTAMP)) {
Expand Down
3 changes: 3 additions & 0 deletions net/econet/af_econet.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,9 @@ static int econet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg
case SIOCGSTAMP:
return sock_get_timestamp(sk, argp);

case SIOCGSTAMPNS:
return sock_get_timestampns(sk, argp);

case SIOCSIFADDR:
case SIOCGIFADDR:
return ec_dev_ioctl(sock, cmd, argp);
Expand Down
3 changes: 3 additions & 0 deletions net/ipv4/af_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,9 @@ int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
case SIOCGSTAMP:
err = sock_get_timestamp(sk, (struct timeval __user *)arg);
break;
case SIOCGSTAMPNS:
err = sock_get_timestampns(sk, (struct timespec __user *)arg);
break;
case SIOCADDRT:
case SIOCDELRT:
case SIOCRTMSG:
Expand Down
3 changes: 3 additions & 0 deletions net/ipv6/af_inet6.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
case SIOCGSTAMP:
return sock_get_timestamp(sk, (struct timeval __user *)arg);

case SIOCGSTAMPNS:
return sock_get_timestampns(sk, (struct timespec __user *)arg);

case SIOCADDRT:
case SIOCDELRT:

Expand Down
6 changes: 6 additions & 0 deletions net/netrom/af_netrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,12 @@ static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
release_sock(sk);
return ret;

case SIOCGSTAMPNS:
lock_sock(sk);
ret = sock_get_timestampns(sk, argp);
release_sock(sk);
return ret;

case SIOCGIFADDR:
case SIOCSIFADDR:
case SIOCGIFDSTADDR:
Expand Down
2 changes: 2 additions & 0 deletions net/packet/af_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,8 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
}
case SIOCGSTAMP:
return sock_get_timestamp(sk, (struct timeval __user *)arg);
case SIOCGSTAMPNS:
return sock_get_timestampns(sk, (struct timespec __user *)arg);

#ifdef CONFIG_INET
case SIOCADDRT:
Expand Down
3 changes: 3 additions & 0 deletions net/rose/af_rose.c
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,9 @@ static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
case SIOCGSTAMP:
return sock_get_timestamp(sk, (struct timeval __user *) argp);

case SIOCGSTAMPNS:
return sock_get_timestampns(sk, (struct timespec __user *) argp);

case SIOCGIFADDR:
case SIOCSIFADDR:
case SIOCGIFDSTADDR:
Expand Down
Loading

0 comments on commit ae40eb1

Please sign in to comment.