[Devel] [PATCH 1/3] netlink: allow to set peeking offset for sockets
Andrey Vagin
avagin at openvz.org
Wed Jun 15 00:09:19 PDT 2016
This allows us to read socket's queue without removing skbs from it.
The same logic was implemented for unix and inet sockets and we use this
to dump and restore sockets in CRIU.
Here is a question whether sk_peek_off has to be protected by locks.
Currently it isn't protected and an user who uses sk_peek_off has to be
sure that nobody calls recvmsg for a socket except him.
---
net/netlink/af_netlink.c | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index ad65bdd..79526e5 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2372,17 +2372,18 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
struct scm_cookie scm;
struct sock *sk = sock->sk;
struct netlink_sock *nlk = nlk_sk(sk);
- int noblock = flags&MSG_DONTWAIT;
size_t copied;
struct sk_buff *skb, *data_skb;
+ int peeked, skip;
int err, ret;
if (flags&MSG_OOB)
return -EOPNOTSUPP;
copied = 0;
+ skip = sk_peek_offset(sk, flags);
- skb = skb_recv_datagram(sk, flags, noblock, &err);
+ skb = __skb_recv_datagram(sk, flags, &peeked, &skip, &err);
if (skb == NULL)
goto out;
@@ -2410,14 +2411,19 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
16384);
- copied = data_skb->len;
+ copied = data_skb->len - skip;
if (len < copied) {
msg->msg_flags |= MSG_TRUNC;
copied = len;
}
skb_reset_transport_header(data_skb);
- err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
+ err = skb_copy_datagram_iovec(data_skb, skip, msg->msg_iov, copied);
+
+ if (flags & MSG_PEEK)
+ sk_peek_offset_fwd(sk, copied);
+ else
+ sk_peek_offset_bwd(sk, skb->len);
if (msg->msg_name) {
struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
@@ -2439,7 +2445,7 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
}
siocb->scm->creds = *NETLINK_CREDS(skb);
if (flags & MSG_TRUNC)
- copied = data_skb->len;
+ copied = data_skb->len - skip;
skb_free_datagram(sk, skb);
@@ -3086,6 +3092,13 @@ int netlink_unregister_notifier(struct notifier_block *nb)
}
EXPORT_SYMBOL(netlink_unregister_notifier);
+static int netlink_set_peek_off(struct sock *sk, int val)
+{
+ sk->sk_peek_off = val;
+
+ return 0;
+}
+
static const struct proto_ops netlink_ops = {
.family = PF_NETLINK,
.owner = THIS_MODULE,
@@ -3105,6 +3118,7 @@ static const struct proto_ops netlink_ops = {
.recvmsg = netlink_recvmsg,
.mmap = netlink_mmap,
.sendpage = sock_no_sendpage,
+ .set_peek_off = netlink_set_peek_off,
};
static const struct net_proto_family netlink_family_ops = {
--
2.5.5
More information about the Devel
mailing list