On Mon, Apr 23, 2018 at 9:56 AM, Björn Töpel <[email protected]> wrote:
> From: Magnus Karlsson <[email protected]>
>
> In this commit, a new getsockopt is added: XDP_STATISTICS. This is
> used to obtain stats from the sockets.
>
> Signed-off-by: Magnus Karlsson <[email protected]>
> +static int xsk_getsockopt(struct socket *sock, int level, int optname,
> + char __user *optval, int __user *optlen)
> +{
> + struct sock *sk = sock->sk;
> + struct xdp_sock *xs = xdp_sk(sk);
> + int len;
> +
> + if (level != SOL_XDP)
> + return -ENOPROTOOPT;
> +
> + if (get_user(len, optlen))
> + return -EFAULT;
> + if (len < 0)
> + return -EINVAL;
> +
> + switch (optname) {
> + case XDP_STATISTICS:
> + {
> + struct xdp_statistics stats;
> +
> + if (len != sizeof(stats))
> + return -EINVAL;
> +
> + mutex_lock(&xs->mutex);
> + stats.rx_dropped = xs->rx_dropped;
> + stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
> + stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
> + mutex_unlock(&xs->mutex);
> +
> + if (copy_to_user(optval, &stats, sizeof(stats)))
> + return -EFAULT;
> + return 0;
For forward compatibility, could allow caller to pass a struct larger
than stats and return the number of bytes filled in.
The lock can also be elided with something like gnet_stats, but it is probably
taken rarely enough that that is not worth the effort, at least right now.