net: wire up support for file_operations->uring_cmd() io_uring-fops
authorJens Axboe <axboe@kernel.dk>
Fri, 18 Dec 2020 22:12:46 +0000 (15:12 -0700)
committerJens Axboe <axboe@kernel.dk>
Sat, 19 Dec 2020 03:06:45 +0000 (20:06 -0700)
Support for SOCKET_URING_OP_SIOCINQ and SOCKET_URING_OP_SIOCOUTQ
for tcp/udp/raw ipv4/ipv6.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
12 files changed:
include/net/raw.h
include/net/sock.h
include/net/tcp.h
include/net/udp.h
include/uapi/linux/net.h
net/ipv4/raw.c
net/ipv4/tcp.c
net/ipv4/tcp_ipv4.c
net/ipv4/udp.c
net/ipv6/raw.c
net/ipv6/tcp_ipv6.c
net/ipv6/udp.c

index 8ad8df5948536483c3467bff68ec0796b0712e67..fbaa123c245857a423466d69fc9f7fc7db9231f5 100644 (file)
@@ -82,4 +82,7 @@ static inline bool raw_sk_bound_dev_eq(struct net *net, int bound_dev_if,
 #endif
 }
 
+int raw_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                 enum io_uring_cmd_flags flags);
+
 #endif /* _RAW_H */
index bdc4323ce53c957c30809b0773b2d558dce555b7..c33f0b58a06f59f45cf993ee4ef610d00280ea4a 100644 (file)
@@ -1146,6 +1146,9 @@ struct proto {
 
        int                     (*ioctl)(struct sock *sk, int cmd,
                                         unsigned long arg);
+       int                     (*uring_cmd)(struct sock *sk,
+                                       struct sock_uring_cmd *scmd,
+                                       enum io_uring_cmd_flags flags);
        int                     (*init)(struct sock *sk);
        void                    (*destroy)(struct sock *sk);
        void                    (*shutdown)(struct sock *sk, int how);
index 78d13c88720fda50e3f1880ac741cea1985ef3e9..8174d9752e52f5f06151e76343c9ed7d1fee53e2 100644 (file)
@@ -350,6 +350,8 @@ void tcp_twsk_destructor(struct sock *sk);
 ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
                        struct pipe_inode_info *pipe, size_t len,
                        unsigned int flags);
+int tcp_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                       enum io_uring_cmd_flags flags);
 
 void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
 static inline void tcp_dec_quickack_mode(struct sock *sk,
index 877832bed4713a011a514a2f6f522728c8c89e20..362224fe83fe269828eaccdfb4ce73f2fc0f9ca1 100644 (file)
@@ -326,6 +326,8 @@ struct sock *__udp6_lib_lookup(struct net *net,
                               struct sk_buff *skb);
 struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
                                 __be16 sport, __be16 dport);
+int udp_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                       enum io_uring_cmd_flags flags);
 
 /* UDP uses skb->dev_scratch to cache as much information as possible and avoid
  * possibly multiple cache miss on dequeue()
index 4dabec6bd957d604dd3f18f7b9896c82a789eb87..629e5df40858c4c7cc60fc37e3b67d6fba3774cb 100644 (file)
@@ -55,4 +55,16 @@ typedef enum {
 
 #define __SO_ACCEPTCON (1 << 16)       /* performed a listen           */
 
+enum {
+       SOCKET_URING_OP_SIOCINQ         = 0,
+       SOCKET_URING_OP_SIOCOUTQ,
+};
+
+struct sock_uring_cmd {
+       __u16   op;
+       __u16   unused[13];
+       __u64   reserved;               /* will be overwritten by core */
+       __u64   unused2;
+};
+
 #endif /* _UAPI_LINUX_NET_H */
index 50a73178d63a841a60bf2fdb441b846df775874a..106200033a6024d7271096e9fbf1f6a6c74c1dc3 100644 (file)
@@ -878,6 +878,28 @@ static int raw_getsockopt(struct sock *sk, int level, int optname,
        return do_raw_getsockopt(sk, level, optname, optval, optlen);
 }
 
+int raw_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                 enum io_uring_cmd_flags flags)
+{
+       switch (scmd->op) {
+       case SIOCOUTQ:
+               return sk_wmem_alloc_get(sk);
+       case SIOCINQ: {
+               struct sk_buff *skb;
+               int amount = 0;
+
+               spin_lock_bh(&sk->sk_receive_queue.lock);
+               skb = skb_peek(&sk->sk_receive_queue);
+               if (skb)
+                       amount = skb->len;
+               spin_unlock_bh(&sk->sk_receive_queue.lock);
+               return amount;
+               }
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
 static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
 {
        switch (cmd) {
@@ -956,6 +978,7 @@ struct proto raw_prot = {
        .release_cb        = ip4_datagram_release_cb,
        .hash              = raw_hash_sk,
        .unhash            = raw_unhash_sk,
+       .uring_cmd         = raw_uring_cmd,
        .obj_size          = sizeof(struct raw_sock),
        .useroffset        = offsetof(struct raw_sock, filter),
        .usersize          = sizeof_field(struct raw_sock, filter),
index ed42d2193c5c76bc9d48f36c13e72ca5be8aee1f..f8bc786775c580557b605a10b3088c58ef529307 100644 (file)
@@ -602,6 +602,39 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
 }
 EXPORT_SYMBOL(tcp_poll);
 
+int tcp_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                 enum io_uring_cmd_flags flags)
+{
+       struct tcp_sock *tp = tcp_sk(sk);
+       bool slow;
+       int ret;
+
+       switch (scmd->op) {
+       case SIOCINQ:
+               if (sk->sk_state == TCP_LISTEN)
+                       return -EINVAL;
+
+               slow = lock_sock_fast(sk);
+               ret = tcp_inq(sk);
+               unlock_sock_fast(sk, slow);
+               break;
+       case SIOCOUTQ:
+               if (sk->sk_state == TCP_LISTEN)
+                       return -EINVAL;
+
+               if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
+                       ret = 0;
+               else
+                       ret = READ_ONCE(tp->write_seq) - tp->snd_una;
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+               break;
+       }
+
+       return ret;
+}
+
 int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
 {
        struct tcp_sock *tp = tcp_sk(sk);
index 58207c7769d05693b650e3c93e4ef405a5d4b23a..bfb8c5aa05fcdadfda0149ae16524f141911ae41 100644 (file)
@@ -2787,6 +2787,7 @@ struct proto tcp_prot = {
        .disconnect             = tcp_disconnect,
        .accept                 = inet_csk_accept,
        .ioctl                  = tcp_ioctl,
+       .uring_cmd              = tcp_uring_cmd,
        .init                   = tcp_v4_init_sock,
        .destroy                = tcp_v4_destroy_sock,
        .shutdown               = tcp_shutdown,
index 7103b0a89756e24203261684e88432615c344581..2c6726361a121a8285c66e4155f4aec8b7937c65 100644 (file)
@@ -1676,6 +1676,19 @@ static int first_packet_length(struct sock *sk)
        return res;
 }
 
+int udp_uring_cmd(struct sock *sk, struct sock_uring_cmd *scmd,
+                 enum io_uring_cmd_flags flags)
+{
+       switch (scmd->op) {
+       case SIOCOUTQ:
+               return sk_wmem_alloc_get(sk);
+       case SIOCINQ:
+               return max_t(int, 0, first_packet_length(sk));
+       default:
+               return -EOPNOTSUPP;
+       }
+}
+
 /*
  *     IOCTL requests applicable to the UDP protocol
  */
@@ -2831,6 +2844,7 @@ struct proto udp_prot = {
        .connect                = ip4_datagram_connect,
        .disconnect             = udp_disconnect,
        .ioctl                  = udp_ioctl,
+       .uring_cmd              = udp_uring_cmd,
        .init                   = udp_init_sock,
        .destroy                = udp_destroy_sock,
        .setsockopt             = udp_setsockopt,
index 1f56d9aae5892bc958e19be49d8bcbf544c1d318..50f1e8189482393629917925da1b8762c06f9f7d 100644 (file)
@@ -1235,6 +1235,7 @@ struct proto rawv6_prot = {
        .connect           = ip6_datagram_connect_v6_only,
        .disconnect        = __udp_disconnect,
        .ioctl             = rawv6_ioctl,
+       .uring_cmd         = raw_uring_cmd,
        .init              = rawv6_init_sk,
        .setsockopt        = rawv6_setsockopt,
        .getsockopt        = rawv6_getsockopt,
index 0e1509b02cb3065d5841b55c7ea54fa4d668585b..e86af3503a4bf19f895258cb0e779f61d237b8e2 100644 (file)
@@ -2116,6 +2116,7 @@ struct proto tcpv6_prot = {
        .disconnect             = tcp_disconnect,
        .accept                 = inet_csk_accept,
        .ioctl                  = tcp_ioctl,
+       .uring_cmd              = tcp_uring_cmd,
        .init                   = tcp_v6_init_sock,
        .destroy                = tcp_v6_destroy_sock,
        .shutdown               = tcp_shutdown,
index b9f3dfdd238344b5d55c4bf46d05433a17c3a4b3..881ae4a1cdda096de37ff9085cb6a383c41d677e 100644 (file)
@@ -1701,6 +1701,7 @@ struct proto udpv6_prot = {
        .connect                = ip6_datagram_connect,
        .disconnect             = udp_disconnect,
        .ioctl                  = udp_ioctl,
+       .uring_cmd              = udp_uring_cmd,
        .init                   = udp_init_sock,
        .destroy                = udpv6_destroy_sock,
        .setsockopt             = udpv6_setsockopt,