Skip to content

Commit 04919be

Browse files
Cong Wangborkmann
authored andcommitted
tcp: Introduce tcp_read_skb()
This patch inroduces tcp_read_skb() based on tcp_read_sock(), a preparation for the next patch which actually introduces a new sock ops. TCP is special here, because it has tcp_read_sock() which is mainly used by splice(). tcp_read_sock() supports partial read and arbitrary offset, neither of them is needed for sockmap. Signed-off-by: Cong Wang <cong.wang@bytedance.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20220615162014.89193-2-xiyou.wangcong@gmail.com
1 parent 4336487 commit 04919be

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

include/net/tcp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ void tcp_get_info(struct sock *, struct tcp_info *);
672672
/* Read 'sendfile()'-style from a TCP socket */
673673
int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
674674
sk_read_actor_t recv_actor);
675+
int tcp_read_skb(struct sock *sk, read_descriptor_t *desc,
676+
sk_read_actor_t recv_actor);
675677

676678
void tcp_initialize_rcv_mss(struct sock *sk);
677679

net/ipv4/tcp.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,53 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
17341734
}
17351735
EXPORT_SYMBOL(tcp_read_sock);
17361736

1737+
int tcp_read_skb(struct sock *sk, read_descriptor_t *desc,
1738+
sk_read_actor_t recv_actor)
1739+
{
1740+
struct tcp_sock *tp = tcp_sk(sk);
1741+
u32 seq = tp->copied_seq;
1742+
struct sk_buff *skb;
1743+
int copied = 0;
1744+
u32 offset;
1745+
1746+
if (sk->sk_state == TCP_LISTEN)
1747+
return -ENOTCONN;
1748+
1749+
while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
1750+
int used;
1751+
1752+
__skb_unlink(skb, &sk->sk_receive_queue);
1753+
used = recv_actor(desc, skb, 0, skb->len);
1754+
if (used <= 0) {
1755+
if (!copied)
1756+
copied = used;
1757+
break;
1758+
}
1759+
seq += used;
1760+
copied += used;
1761+
1762+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
1763+
consume_skb(skb);
1764+
++seq;
1765+
break;
1766+
}
1767+
consume_skb(skb);
1768+
if (!desc->count)
1769+
break;
1770+
WRITE_ONCE(tp->copied_seq, seq);
1771+
}
1772+
WRITE_ONCE(tp->copied_seq, seq);
1773+
1774+
tcp_rcv_space_adjust(sk);
1775+
1776+
/* Clean up data we have read: This will do ACK frames. */
1777+
if (copied > 0)
1778+
tcp_cleanup_rbuf(sk, copied);
1779+
1780+
return copied;
1781+
}
1782+
EXPORT_SYMBOL(tcp_read_skb);
1783+
17371784
int tcp_peek_len(struct socket *sock)
17381785
{
17391786
return tcp_inq(sock->sk);

0 commit comments

Comments
 (0)