当前位置: 技术问答>linux和unix
linux内核tcp_sendmsg函数里数据与压缩的问题
来源: 互联网 发布时间:2017-03-24
本文导语: 各位大侠我想请教您个问题: 我用ftp协议发送数据3M的时候我在原始的int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,size_t size)函数中打印了iov->iov_len,我统计了iov->iov_len的之和 明...
各位大侠我想请教您个问题:
我用ftp协议发送数据3M的时候我在原始的int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,size_t size)函数中打印了iov->iov_len,我统计了iov->iov_len的之和 明显的大于3M,(按道理数据他们之和应该是等于3M的),我在想 他的这个buffer(msghdr)是不是动态的变化 也就是边发送数据变存放数据。我准备在这里对用户数据进行压缩然后再由TCP发送:代码如下:红色为自己加的代码 谢谢各位了!!
int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t size)
{
struct iovec *iov;
struct tcp_sock *tp = tcp_sk(sk);
struct sk_buff *skb;
int iovlen, flags;
int mss_now, size_goal;
int sg, err, copied;
long timeo;
/*2012 8 23 mouchange*/
char *mouiov;
char *duiov;
int duiov_len;
struct crypto_comp *tfm;
int mouflag=0;
int ret;
struct iovec *mdiov;
lock_sock(sk);
flags = msg->msg_flags;
timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
/* Wait for a connection to finish. */
if ((1 sk_socket->flags);
mss_now = tcp_send_mss(sk, &size_goal, flags);
/* Ok commence sending. */
iovlen = msg->msg_iovlen;
iov = msg->msg_iov;
copied = 0;
err = -EPIPE;
if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
goto out_err;
sg = sk->sk_route_caps & NETIF_F_SG;
/*2012 8 23 mouchange*/
if(iov->iov_len>2000)
{
mdiov=iov;
if((mouiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is %dn",ksize(mouiov));
if((duiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is %dn",ksize(duiov));
if(copy_from_user(mouiov,iov->iov_base,iov->iov_len))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is UnSuccessn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is Successn");
tfm = crypto_alloc_comp("deflate",0,0);
if(IS_ERR(tfm)) {
printk("failed to load transform for deflate !n");
release_sock(sk);
return 0;
}
ret = crypto_comp_compress(tfm,mouiov,iov->iov_len,duiov,&duiov_len);
if(ret) {
printk("failed to compress !n");
release_sock(sk);
//return copied;
return 0;
}
printk(KERN_INFO " tcp_sendmsg iov->iov_len %d unSuccessn",iov->iov_len);
printk(KERN_INFO " tcp_sendmsg duiov_len %d unSuccessn",duiov_len);
iov->iov_base=duiov;
iov->iov_len=duiov_len;
crypto_free_comp(tfm);
kfree(mouiov);
mouflag=1;
}
我用ftp协议发送数据3M的时候我在原始的int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,size_t size)函数中打印了iov->iov_len,我统计了iov->iov_len的之和 明显的大于3M,(按道理数据他们之和应该是等于3M的),我在想 他的这个buffer(msghdr)是不是动态的变化 也就是边发送数据变存放数据。我准备在这里对用户数据进行压缩然后再由TCP发送:代码如下:红色为自己加的代码 谢谢各位了!!
int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t size)
{
struct iovec *iov;
struct tcp_sock *tp = tcp_sk(sk);
struct sk_buff *skb;
int iovlen, flags;
int mss_now, size_goal;
int sg, err, copied;
long timeo;
/*2012 8 23 mouchange*/
char *mouiov;
char *duiov;
int duiov_len;
struct crypto_comp *tfm;
int mouflag=0;
int ret;
struct iovec *mdiov;
lock_sock(sk);
flags = msg->msg_flags;
timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
/* Wait for a connection to finish. */
if ((1 sk_socket->flags);
mss_now = tcp_send_mss(sk, &size_goal, flags);
/* Ok commence sending. */
iovlen = msg->msg_iovlen;
iov = msg->msg_iov;
copied = 0;
err = -EPIPE;
if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
goto out_err;
sg = sk->sk_route_caps & NETIF_F_SG;
/*2012 8 23 mouchange*/
if(iov->iov_len>2000)
{
mdiov=iov;
if((mouiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is %dn",ksize(mouiov));
if((duiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is %dn",ksize(duiov));
if(copy_from_user(mouiov,iov->iov_base,iov->iov_len))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is UnSuccessn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is Successn");
tfm = crypto_alloc_comp("deflate",0,0);
if(IS_ERR(tfm)) {
printk("failed to load transform for deflate !n");
release_sock(sk);
return 0;
}
ret = crypto_comp_compress(tfm,mouiov,iov->iov_len,duiov,&duiov_len);
if(ret) {
printk("failed to compress !n");
release_sock(sk);
//return copied;
return 0;
}
printk(KERN_INFO " tcp_sendmsg iov->iov_len %d unSuccessn",iov->iov_len);
printk(KERN_INFO " tcp_sendmsg duiov_len %d unSuccessn",duiov_len);
iov->iov_base=duiov;
iov->iov_len=duiov_len;
crypto_free_comp(tfm);
kfree(mouiov);
mouflag=1;
}
|
多线程 吧 不一定是按照顺序来的