Linux内核中TCP协议实现的关键数据结构
1. TCP协议头tcphdr
TCP协议头描述了TCP数据段发送的源地址、目标地址、数据段传送管理和连接管理的信息,是TCP协议实现的重要数据结构之一。
struct tcphdr {__be16 source;__be16 dest;__be32 seq;__be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)__u16 res1:4,doff:4,fin:1,syn:1,rst:1,psh:1,ack:1,urg:1,ece:1,cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)__u16 doff:4,res1:4,cwr:1,ece:1,urg:1,ack:1,psh:1,rst:1,syn:1,fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif __be16 window;__sum16 check;__be16 urg_ptr;
};
2. TCP的控制缓冲区tcp_skb_cb
TCP是完全异步的协议,实际数据段的传送独立于所有来自套接字层的写操作。
TCP层分配Socket Buffer来存放应用层写入套接字的数据,但应用程序控制管理数据包的信息存放在TCP的控制缓冲区中。
TCP控制缓冲区由struct tcp_skb_cb数据结构描述。
可以看到TCP控制缓冲区中的信息与TCP协议头的信息有部分重合,当数据从应用程序复制到TCP层的Socket Buffer时,函数需要用TCP控制缓冲区中TCP协议头的信息,来设置struct tcphdr数据结构中的相关数据域。
/* This is what the send packet queuing engine uses to pass* TCP per-packet control information to the transmission* code. We also store the host-order sequence numbers in* here too. This is 36 bytes on 32-bit architectures,* 40 bytes on 64-bit machines, if this grows please adjust* skbuff.h:skbuff->cb[xxx] size appropriately.*/
struct tcp_skb_cb {union {struct inet_skb_parm h4;
#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)struct inet6_skb_parm h6;
#endif} header; /* For incoming frames */__u32 seq; /* Starting sequence number */__u32 end_seq; /* SEQ + FIN + SYN + datalen */__u32 when; /* used to compute rtt's */__u8 flags; /* TCP header flags. *//* NOTE: These must match up to the flags byte in a* real TCP header.*/
#define TCPCB_FLAG_FIN 0x01
#define TCPCB_FLAG_SYN 0x02
#define TCPCB_FLAG_RST 0x04
#define TCPCB_FLAG_PSH 0x08
#define TCPCB_FLAG_ACK 0x10
#define TCPCB_FLAG_URG 0x20
#define TCPCB_FLAG_ECE 0x40
#define TCPCB_FLAG_CWR 0x80__u8 sacked; /* State flags for SACK/FACK. */
#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
#define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
#define TCPCB_LOST 0x04 /* SKB is lost */
#define TCPCB_TAGBITS 0x07 /* All tag bits */#define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
#define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)__u32 ack_seq; /* Sequence number ACK'd */
};
3. TCP套接字的数据结构
TCP套接字的数据结构中包含了管理TCP协议各方面的信息,如发送和接收序列号、TCP窗口尺寸、避免网络阻塞等。
这些管理信息由TCP套接字的数据结构struct tcp_sock描述。struct tcp_sock是一个庞大的数据结构,其中包含了TCP层管理数据传送需要的所有信息。
struct tcp_sock数据结构是SOCK_STREAM类的套接字数据结构stuct sock中的一部分,在分配套接字的同时也分配内存空间。
struct tcp_sock {/* inet_connection_sock has to be the first member of tcp_sock */struct inet_connection_sock inet_conn;u16 tcp_header_len; /* Bytes of tcp header to send */u16 xmit_size_goal_segs; /* Goal for segmenting output packets *//** Header prediction flags* 0x5?10 << 16 + snd_wnd in net byte order*/__be32 pred_flags;/** RFC793 variables by their proper names. This means you can* read the code and the spec side by side (and laugh ...)* See RFC793 and RFC1122. The RFC writes these in capitals.*/u32 rcv_nxt; /* What we want to receive next */u32 copied_seq; /* Head of yet unread data */u32 rcv_wup; /* rcv_nxt on last window update sent */u32 snd_nxt; /* Next sequence we send */u32 snd_una; /* First byte we want an ack for */u32 snd_sml; /* Last byte of the most recently transmitted small packet */u32 rcv_tstamp; /* timestamp of last received ACK (for keepalives) */u32 lsndtime; /* timestamp of last sent data packet (for restart window) *//* Data for direct copy to user */struct {struct sk_buff_head prequeue;struct task_struct *task;struct iovec *iov;int memory;int len;
#ifdef CONFIG_NET_DMA/* members for async copy */struct dma_chan *dma_chan;int wakeup;struct dma_pinned_list *pinned_list;dma_cookie_t dma_cookie;
#endif} ucopy;u32 snd_wl1; /* Sequence for window update */u32 snd_wnd; /* The window we expect to receive */u32 max_window; /* Maximal window ever seen from peer */u32 mss_cache; /* Cached effective mss, not including SACKS */u32 window_clamp; /* Maximal window to advertise */u32 rcv_ssthresh; /* Current window clamp */u32 frto_highmark; /* snd_nxt when RTO occurred */u16 advmss; /* Advertised MSS */u8 frto_counter; /* Number of new acks after RTO */u8 nonagle : 4,/* Disable Nagle algorithm? */thin_lto : 1,/* Use linear timeouts for thin streams */thin_dupack : 1,/* Fast retransmit on first dupack */unused : 2;/* RTT measurement */u32 srtt; /* smoothed round trip time << 3 */u32 mdev; /* medium deviation */u32 mdev_max; /* maximal mdev for the last rtt period */u32 rttvar; /* smoothed mdev_max */u32 rtt_seq; /* sequence number to update rttvar */u32 packets_out; /* Packets which are "in flight" */u32 retrans_out; /* Retransmitted packets out */u16 urg_data; /* Saved octet of OOB data and control flags */u8 ecn_flags; /* ECN status bits. */u8 reordering; /* Packet reordering metric. */u32 snd_up; /* Urgent pointer */u8 keepalive_probes; /* num of allowed keep alive probes */
/** Options received (usually on last packet, some only on SYN packets).*/struct tcp_options_received rx_opt;/** Slow start and congestion control (see also Nagle, and Karn & Partridge)*/u32 snd_ssthresh; /* Slow start size threshold */u32 snd_cwnd; /* Sending congestion window */u32 snd_cwnd_cnt; /* Linear increase counter */u32 snd_cwnd_clamp; /* Do not allow snd_cwnd to grow above this */u32 snd_cwnd_used;u32 snd_cwnd_stamp;u32 rcv_wnd; /* Current receiver window */u32 write_seq; /* Tail(+1) of data held in tcp send buffer */u32 pushed_seq; /* Last pushed seq, required to talk to windows */u32 lost_out; /* Lost packets */u32 sacked_out; /* SACK'd packets */u32 fackets_out; /* FACK'd packets */u32 tso_deferred;u32 bytes_acked; /* Appropriate Byte Counting - RFC3465 *//* from STCP, retrans queue hinting */struct sk_buff* lost_skb_hint;struct sk_buff *scoreboard_skb_hint;struct sk_buff *retransmit_skb_hint;struct sk_buff_head out_of_order_queue; /* Out of order segments go here *//* SACKs data, these 2 need to be together (see tcp_build_and_update_options) */struct tcp_sack_block duplicate_sack[1]; /* D-SACK block */struct tcp_sack_block selective_acks[4]; /* The SACKS themselves*/struct tcp_sack_block recv_sack_cache[4];struct sk_buff *highest_sack; /* highest skb with SACK received* (validity guaranteed only if* sacked_out > 0)*/int lost_cnt_hint;u32 retransmit_high; /* L-bits may be on up to this seqno */u32 lost_retrans_low; /* Sent seq after any rxmit (lowest) */u32 prior_ssthresh; /* ssthresh saved at recovery start */u32 high_seq; /* snd_nxt at onset of congestion */u32 retrans_stamp; /* Timestamp of the last retransmit,* also used in SYN-SENT to remember stamp of* the first SYN. */u32 undo_marker; /* tracking retrans started here. */int undo_retrans; /* number of undoable retransmissions. */u32 total_retrans; /* Total retransmits for entire connection */u32 urg_seq; /* Seq of received urgent pointer */unsigned int keepalive_time; /* time before keep alive takes place */unsigned int keepalive_intvl; /* time interval between keep alive probes */int linger2;/* Receiver side RTT estimation */struct {u32 rtt;u32 seq;u32 time;} rcv_rtt_est;/* Receiver queue space */struct {int space;u32 seq;u32 time;} rcvq_space;/* TCP-specific MTU probe information. */struct {u32 probe_seq_start;u32 probe_seq_end;} mtu_probe;#ifdef CONFIG_TCP_MD5SIG
/* TCP AF-Specific parts; only used by MD5 Signature support so far */const struct tcp_sock_af_ops *af_specific;/* TCP MD5 Signature Option information */struct tcp_md5sig_info *md5sig_info;
#endif/* When the cookie options are generated and exchanged, then this* object holds a reference to them (cookie_values->kref). Also* contains related tcp_cookie_transactions fields.*/struct tcp_cookie_values *cookie_values;
};
4. 应用层传送给传输层信息的数据结构
struct msghdr数据结构中包含了来自应用层数据的信息。
它将作为参数从套接字传送给TCP协议实例的传送处理函数,在传送处理函数中应用数据从套接字复制到内核地址空间的SocketBuffer中。
/** As we do 4.4BSD message passing we use a 4.4BSD message passing* system, not 4.3. Thus msg_accrights(len) are now missing. They* belong in an obscure libc emulation or the bin.*/struct msghdr {void * msg_name; /* Socket name */int msg_namelen; /* Length of name */struct iovec * msg_iov; /* Data blocks */__kernel_size_t msg_iovlen; /* Number of blocks */void * msg_control; /* Per protocol magic (eg BSD file descriptor passing) */__kernel_size_t msg_controllen; /* Length of cmsg list */unsigned msg_flags;
};
Linux内核中TCP协议实现的关键数据结构相关推荐
- linux内核中TCP接收的实现
linux内核中TCP接收的实现入口函数是tcp_v4_rcv 1. 数据包检查处理 一开始做一些数据包详细检查处理,一旦出错,可能导致内核挂掉 int tcp_v4_rcv(struct sk_bu ...
- linux做预警机制,预警通告:Linux内核中TCP SACK机制远程DoS
漏洞描述 2019年6月18日,RedHat官网发布报告:安全研究人员在Linux内核处理TCP SACK数据包模块中发现了三个漏洞,CVE编号为CVE-2019-11477.CVE-2019-114 ...
- linux内核中TCP发送的实现
TCP发送功能是指将从应用层通过打开的套接字写入的数据移入内核,通过TCP/IP协议栈,最终通过网络设备发送到远端接收主机. TCP传送的特点如下: 异步传送:TCP的实际传送独立于应用层. 汇集从套 ...
- linux程序获取透传参数,Linux内核中TCP SACK处理流程分析
frankzfz2014-07-27 17:32 demo121:frankzfz您好: 我想请教一个问题,就是将写好的GenericApp项目(没有配置工具),我加入zigbee协议栈的配置工具后还 ...
- Linux内核中的算法和数据结构
算法和数据结构纷繁复杂,但是对于Linux Kernel开发人员来说重点了解Linux内核中使用到的算法和数据结构很有必要. 在一个国外问答平台stackexchange.com的Theoretica ...
- linux下IPROTO_TCP,TCP/IP协议栈在Linux内核中的运行时序分析
可选题目三:TCP/IP协议栈在Linux内核中的运行时序分析 在深入理解Linux内核任务调度(中断处理.softirg.tasklet.wq.内核线程等)机制的基础上,分析梳理send和recv过 ...
- TCP/IP协议栈在Linux内核中的运行时序分析
本文主要是讲解TCP/IP协议栈在Linux内核中的运行时序,文章较长,里面有配套的视频讲解,建议收藏观看. 1 Linux概述 1.1 Linux操作系统架构简介 Linux操作系统总体上由Linu ...
- TCP三次握手在linux内核中的实现
TCP三次握手在linux内核中的实现 以下基于linux内核2.4.0源码(转自www.yuanma.org/) 以前一直使用的网络通讯的函数都是工作在阻塞模式.在看connect实现源码时,突然想 ...
- TCP/IP网络协议栈在Linux内核中的如何使用丨内核开发丨驱动开发丨操作系统丨内核源码
TCP/IP网络协议栈在Linux内核中的如何使用 视频讲解如下,点击观看: TCP/IP网络协议栈在Linux内核中的如何使用丨内核开发丨驱动开发丨操作系统丨内核源码 C/C++Linux服务器开发 ...
最新文章
- [SHOI2007]园丁的烦恼
- Ubuntu 安装docker CE以及harbor
- json数据 提示框flash.now[:notice] flash.now[:alert]
- Openlayers下载与加载geoserver的wms服务显示地图
- Apache和Nginx的区别
- css复选框如何调大一些,创建CSS放大复选框
- SQL Server 和 Oracle 以及 MySQL 有哪些区别?
- dj鲜生-06-其它模块以应用方式生成并归位
- 游标sql server_SQL Server游标属性
- php将数组转成字符串的,php怎样将数组转化成字符串
- SQLite的查询优化
- ps切图怎么做成html,PS切图怎么导出网页 PS切图怎么生成源代码
- java使用itext导出pdf,图片、表格、背景图
- 仿QQ项目(一):好友列表
- MATLAB - 数字信号包络线的求取
- 迅捷路由器造成计算机无法上网,迅捷fw325r路由器不能上网(连不上网)怎么办?...
- cropper(裁剪图片)插件使用(案例)
- PCIE Configuration Space
- 智能与人机融合智能的思考
- 左益豪:用代码创造一个新世界|OneFlow U
热门文章
- vs工具箱里面没有工具怎么办_装机技巧系列(一):用U盘制作自己的PE工具箱...
- echarts 动态设置标题_echart动态生成标题
- idea 修改样式要编译_在IDEA中DEBUG Javac源码
- levedb 导入 mysql_[LevelDB] 数据库3:循序渐进 —— 操作接口
- python猜词游戏源代码_Python趣味小游戏编写教学
- matlab矩阵中的 *、/、\、.*
- 计算机应用能力考试用书有哪些,全国专业技术人员计算机应用能力考试用书
- python自动安装pip教程_谈谈全自动安装常使用的pip install的原理及作用!!!
- 求计算机技术在创新上的应用,计算机技术在教学上的应用
- 代码居中对齐_HTML span标签如何居中和右对齐?这里有HTML span标签的样式解析