@@ -19,6 +19,7 @@
#include <net/inet_sock.h>
#include <linux/skbuff.h>
+#include <linux/spinlock_types.h>
#include <net/netns/hash.h>
#include <uapi/linux/udp.h>
@@ -55,6 +56,12 @@ struct udp_sock {
* when the socket is uncorked.
*/
__u16 len; /* total length of pending frames */
+
+ spinlock_t stats_lock; /* lock for statistics counters */
+ __u64 dgrams_out; /* total datagrams sent*/
+ __u64 bytes_out; /* total bytes sent */
+ __u64 dgrams_in; /* total datagrams received */
+ __u64 bytes_in; /* total bytes received */
/*
* Fields specific to UDP-Lite.
*/
@@ -185,6 +185,8 @@ static inline void udp_lib_hash(struct sock *sk)
void udp_lib_unhash(struct sock *sk);
void udp_lib_rehash(struct sock *sk, u16 new_hash);
+int udp_lib_init_sock(struct sock *sk);
+
static inline void udp_lib_close(struct sock *sk, long timeout)
{
sk_common_release(sk);
@@ -38,6 +38,10 @@ struct udphdr {
#define UDP_ENCAP_L2TPINUDP 3 /* rfc2661 */
struct udp_info {
+ __u64 udpi_dgrams_out;
+ __u64 udpi_bytes_out;
+ __u64 udpi_dgrams_in;
+ __u64 udpi_bytes_in;
};
@@ -797,6 +797,7 @@ static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
{
struct sock *sk = skb->sk;
struct inet_sock *inet = inet_sk(sk);
+ struct udp_sock *up = udp_sk(sk);
struct udphdr *uh;
int err = 0;
int is_udplite = IS_UDPLITE(sk);
@@ -843,9 +844,14 @@ send:
UDP_MIB_SNDBUFERRORS, is_udplite);
err = 0;
}
- } else
+ } else {
UDP_INC_STATS_USER(sock_net(sk),
UDP_MIB_OUTDATAGRAMS, is_udplite);
+ spin_lock(&up->stats_lock);
+ up->dgrams_out++;
+ up->bytes_out += len - sizeof(struct udphdr);
+ spin_unlock(&up->stats_lock);
+ }
return err;
}
@@ -1277,6 +1283,7 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
int flags, int *addr_len)
{
struct inet_sock *inet = inet_sk(sk);
+ struct udp_sock *up = udp_sk(sk);
DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
struct sk_buff *skb;
unsigned int ulen, copied;
@@ -1333,9 +1340,14 @@ try_again:
goto out_free;
}
- if (!peeked)
+ if (!peeked) {
UDP_INC_STATS_USER(sock_net(sk),
UDP_MIB_INDATAGRAMS, is_udplite);
+ spin_lock(&up->stats_lock);
+ up->dgrams_in++;
+ up->bytes_in += skb->len - sizeof(struct udphdr);
+ spin_unlock(&up->stats_lock);
+ }
sock_recv_ts_and_drops(msg, sk, skb);
@@ -2036,6 +2048,15 @@ int udp_rcv(struct sk_buff *skb)
return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
}
+int udp_lib_init_sock(struct sock *sk)
+{
+ struct udp_sock *up = udp_sk(sk);
+
+ spin_lock_init(&up->stats_lock);
+ return 0;
+}
+EXPORT_SYMBOL(udp_lib_init_sock);
+
void udp_destroy_sock(struct sock *sk)
{
struct udp_sock *up = udp_sk(sk);
@@ -2273,6 +2294,7 @@ struct proto udp_prot = {
.connect = ip4_datagram_connect,
.disconnect = udp_disconnect,
.ioctl = udp_ioctl,
+ .init = udp_lib_init_sock,
.destroy = udp_destroy_sock,
.setsockopt = udp_setsockopt,
.getsockopt = udp_getsockopt,
@@ -161,8 +161,20 @@ static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
void *info)
{
+ struct udp_sock *up = udp_sk(sk);
+ struct udp_info *i = info;
+
r->idiag_rqueue = sk_rmem_alloc_get(sk);
r->idiag_wqueue = sk_wmem_alloc_get(sk);
+ if (!info)
+ return;
+
+ spin_lock(&up->stats_lock);
+ i->udpi_dgrams_out = up->dgrams_out;
+ i->udpi_bytes_out = up->bytes_out;
+ i->udpi_dgrams_in = up->dgrams_in;
+ i->udpi_bytes_in = up->bytes_in;
+ spin_unlock(&up->stats_lock);
}
static const struct inet_diag_handler udp_diag_handler = {
@@ -394,6 +394,7 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
{
struct ipv6_pinfo *np = inet6_sk(sk);
struct inet_sock *inet = inet_sk(sk);
+ struct udp_sock *up = udp_sk(sk);
struct sk_buff *skb;
unsigned int ulen, copied;
int peeked, off = 0;
@@ -464,6 +465,10 @@ try_again:
else
UDP6_INC_STATS_USER(sock_net(sk),
UDP_MIB_INDATAGRAMS, is_udplite);
+ spin_lock(&up->stats_lock);
+ up->dgrams_in++;
+ up->bytes_in += skb->len - sizeof(struct udphdr);
+ spin_unlock(&up->stats_lock);
}
sock_recv_ts_and_drops(msg, sk, skb);
@@ -1024,6 +1029,7 @@ static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
{
struct sock *sk = skb->sk;
+ struct udp_sock *up = udp_sk(sk);
struct udphdr *uh;
int err = 0;
int is_udplite = IS_UDPLITE(sk);
@@ -1065,9 +1071,14 @@ send:
UDP_MIB_SNDBUFERRORS, is_udplite);
err = 0;
}
- } else
+ } else {
UDP6_INC_STATS_USER(sock_net(sk),
UDP_MIB_OUTDATAGRAMS, is_udplite);
+ spin_lock(&up->stats_lock);
+ up->dgrams_out++;
+ up->bytes_out += len - sizeof(struct udphdr);
+ spin_unlock(&up->stats_lock);
+ }
return err;
}
@@ -1522,6 +1533,7 @@ struct proto udpv6_prot = {
.connect = ip6_datagram_connect,
.disconnect = udp_disconnect,
.ioctl = udp_ioctl,
+ .init = udp_lib_init_sock,
.destroy = udpv6_destroy_sock,
.setsockopt = udpv6_setsockopt,
.getsockopt = udpv6_getsockopt,