Chinaunix首页 | 论坛 | 博客
  • 博客访问: 612596
  • 博文数量: 363
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 1347
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-22 16:07
文章分类

全部博文(363)

文章存档

2018年(83)

2016年(1)

2014年(2)

2013年(34)

2012年(236)

2011年(7)

分类: LINUX

2018-02-01 12:48:12

作者:gfree.wind@gmail.com
博客:linuxfocus.blog.chinaunix.net

在前面的学习过程中,遇到了位于struct inet_sock中的cork,当时没有把这个变量搞得很明白,没有完全搞清这个变量的用途。
  1.  /*
  2.  ...... ......
  3.  * @cork - info to build ip hdr on each ip frag while socket is corked
  4.  */
  5. struct inet_sock {
  6.     /* sk and pinet6 has to be the first two members of inet_sock */
  7.     struct sock        sk;
  8.     
  9.     ...... ......
  10.     
  11.     struct {
  12.         unsigned int        flags;
  13.         unsigned int        fragsize;
  14.         struct ip_options    *opt;
  15.         struct dst_entry    *dst;
  16.         int            length; /* Total length of all frames */
  17.         __be32            addr;
  18.         struct flowi        fl;
  19.     } cork;
  20. };
从注释上看,这个cork是用于ip报文分片。那么究竟是不是这样的呢,如果是的话,它在分片中起得作用又是什么呢?

搜索整个linux目录,cork在四个文件中被使用,分别是ip6_output.c,ip_output.c,raw.c,和udp.c。其中ip6_output是为了支持IPv6,逻辑与ip_output.c应该大同小异,所以就不需要关注这个文件。

首先,关注一下cork何时被赋值
1. ip_output.c中的ip_append_data函数
  1. if (skb_queue_empty(&sk->sk_write_queue)) {
  2.     /*
  3.      * setup for corking.
  4.     */
  5.     /* 设置cork*/
  6.     opt = ipc->opt;
  7.     if (opt) {
  8.         /* 将ip option保存到cork中 */    
  9.         if (inet->cork.opt == NULL) {
  10.             inet->cork.opt = kmalloc(sizeof(struct ip_options) + 40, sk->sk_allocation);
  11.             if (unlikely(inet->cork.opt == NULL))
  12.                  return -ENOBUFS;
  13.             }
  14.             memcpy(inet->cork.opt, opt, sizeof(struct ip_options)+opt->optlen);
  15.             inet->cork.flags |= IPCORK_OPT;
  16.             inet->cork.addr = ipc->addr;
  17.         }
  18.         rt = *rtp;
  19.         if (unlikely(!rt))
  20.             return -EFAULT;
  21.         /*
  22.          * We steal reference to this route, caller should not release it
  23.          */
  24.         *rtp = NULL;
  25.         /* 
  26.         保存分片大小。根据是否enable了PMTU探测,来得到分片的大小,PMTU或者MTU。
  27.         */
  28.         inet->cork.fragsize = mtu = inet->pmtudisc == IP_PMTUDISC_PROBE ?
  29.                      rt->dst.dev->mtu :
  30.                      dst_mtu(rt->dst.path);
  31.         /* 保存下一跳信息 */
  32.         inet->cork.dst = &rt->dst;
  33.         inet->cork.length = 0;
  34. }
2. udp.c中 udp_sendmsg
  1. back_from_confirm:
  2. ...... ......
  3.     /*
  4.      *    Now cork the socket to pend data.
  5.      */
  6. /* 保存流量控制信息 */     inet->cork.fl.fl4_dst = daddr;
  7.     inet->cork.fl.fl_ip_dport = dport;
  8.     inet->cork.fl.fl4_src = saddr;
  9.     inet->cork.fl.fl_ip_sport = inet->inet_sport;

而对cork的使用也集中于这两个文件。
1. 在ip_append_data中,如果该sk的sk_write_queue不为空的话,就会直接使用cork中的下一跳信息(cork->dst),ip报文的option(cork->opt),MTU值(cork->fragsize)。且每次ip_append_data成功时,cork->length都会加上该次的数据长度。这说明了这个length为所有sk_write_queue中的数据总长度。
2. 在udp_push_pending_frames中,linux需要从cork中获得udp头部所需要的信息源地址,目的地址,包长等。

cork的释放是通过ip_cork_release来完成的。调用ip_cork_release的函数一共有两个函数ip_push_pending_frames和ip_flush_pending_frames。当数据成功append或者push后,就会调用ip_push_pending_frames完成发送动作,并是否cork。如果出错,则调用ip_flush_pending_frames。

到此,可以明确了cork的用途了。
1.cork保存了同一个IP包中的一些共用信息。主要用于分片,或者多个数据组成一个IP报文发送时;
2.cork在udp中可以保存UDP首部需要的信息。这样当append UDP数据时,不需要通过参数传递这些信息;




阅读(649) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~