Add skb_send_sock to send an skbuff on a socket within the kernel.
Arguments include and offset so that an skbuf might be sent in mulitple
calls (e.g.  send buffer limit is hit).
---
 include/linux/skbuff.h |  2 ++
 net/core/skbuff.c      | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index a17e235..83849e3 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -3115,6 +3115,8 @@ __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, 
int offset, u8 *to,
 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
                    struct pipe_inode_info *pipe, unsigned int len,
                    unsigned int flags);
+int skb_send_sock(struct sk_buff *skb, struct socket *sock,
+                                 unsigned int offset);
 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
 unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
 int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index f75897a..ff9f88d 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -2005,6 +2005,72 @@ int skb_splice_bits(struct sk_buff *skb, struct sock 
*sk, unsigned int offset,
 }
 EXPORT_SYMBOL_GPL(skb_splice_bits);
 
+/* Send skb data on a socket. */
+int skb_send_sock(struct sk_buff *skb, struct socket *sock, unsigned int 
offset)
+{
+       unsigned int sent = 0;
+       unsigned int ret;
+       unsigned short fragidx;
+
+       /* Deal with head data */
+       while (offset < skb_headlen(skb)) {
+               size_t len = skb_headlen(skb) - offset;
+               struct kvec kv;
+               struct msghdr msg;
+
+               kv.iov_base = skb->data + offset;
+               kv.iov_len = len;
+               memset(&msg, 0, sizeof(msg));
+
+               ret = kernel_sendmsg(sock, &msg, &kv, 1, len);
+               if (ret < 0)
+                       goto error;
+
+               offset += ret;
+               sent += ret;
+       }
+
+       offset -= skb_headlen(skb);
+
+       /* Are there frags? */
+       if (!skb_shinfo(skb)->nr_frags)
+               goto out;
+
+       /* Find where we are in frag list */
+       for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
+               skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
+
+               if (offset < frag->size)
+                       break;
+
+               offset -= frag->size;
+       }
+
+       for (; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
+               skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
+
+               ret = kernel_sendpage(sock, frag->page.p,
+                                     frag->page_offset + offset,
+                                     frag->size - offset,
+                                     MSG_DONTWAIT);
+               if (ret < 0)
+                       goto error;
+
+               sent += ret;
+               offset = 0;
+       }
+
+out:
+       return sent;
+
+error:
+       if (sent)
+               return sent;
+       else
+               return ret;
+}
+EXPORT_SYMBOL_GPL(skb_send_sock);
+
 /**
  *     skb_store_bits - store bits from kernel buffer to skb
  *     @skb: destination buffer
-- 
2.7.4

Reply via email to