计算机网络 3. 传输层 2021-05-27 浏览量 694 暂无评论 [TOC] ## 课程主要内容 - 多路复用解复用(聚合主机数据-区分进程数据) - 可靠数据传输(RDT)※ - 流量控制 - 拥塞控制 - ### 3.1 概述和传输层服务(21:42) 提供的服务:远程的进程到进程间的以报文(Message)为逻辑通信 传输层加强网络层的服务品质,但不是所有服务品质都是可以加强的 ### 3.2 多路复用和解复用(19:01) 为什么叫复用呢?就是一个TCP/UDP实体上面进行着许多进程的信息交换 发送方主机多路复用:从多个套接字接受来自多个进程的报文,根据套接字对应的IP地址和端口号等信息对报文段用头部加以封装(传输层封装端口号,网络层封装IP) ### 3.3 无连接传输UDP(17:04) UDP:用户数据报协议 头部开销小 校验和:EDC(差错检测编码),检测不出残存错误(错了,但是校验和却通过了) 发送方:将整个数据报D(包括伪头部,头部,数据部分)分成数个16比特整数,将这些整数相加,进位时回滚(就是把进位挪到末尾),最终的和取反码即为校验和(EDC), 接收方:将接收到的D和EDC全分16位加起来,若校验范围(D)+校验和(EDC)=1111111111111111,则通过校验 ### 3.4 可靠数据传输(rdt)原理(2:20:15) 由udt变为rdt 只考虑单向数据传输,但控制信息是双向流动的 使用有限状态机(FSM)来描述发送方和接收方 FSM:状态1到状态2(经过什么事件和动作--边) RDT 1.0: 下层的信道是可靠的,只进行封装解封装 - 不出错 - 不丢失 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210606210238753.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) **RDT 2.0:** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210606210355599.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 缺陷:既然发送的Packet可能出错,那ACK和NAK也可能出错,在RDT 2.1中更新了,为Packet编号,发送方发P0,接收方成功接收到了P0,发回ACK,然后又接受到了P0,说明刚刚的ACK发回途中出错了,再发ACK **RDT 2.2:** 无NAK的协议 对ACK编号,便于流水线。比如我现在在等ACK1,但返回来个ACK0,答非所问,那不就是出错了吗。就像我们你她好看吗?你说她很温柔。 **RDT 3.0** 假设:下层提供的信号可能丢失 发送完后启动超时定时器,如果到时间还没等到想等的人,那重发 超时定时器的设置很重要 缺点:如果链路容量比较大,那一次发一个PDU不能充分利用链路的传输能力 **流水线协议/管道化协议(pipline)** :一次发送多个分组 分为两种: 1. 回退N步协议(GBN) 2. 选择性重发协议(SR) **滑动窗口协议(SW)** 1. SW = 1, RW = 1,rdt3.0 2. SW > 1, RW = 1,GBN 3. SW > 1, RW > 1,SR **发送缓冲区:** 在内存中,存放已发送,但未经确认的分组 **发送缓冲区的大小** :一次最多可以发送多少个未经确认的分组 **发送窗口:** 每发送一个分组,前言前移一个单位;每收到老分组的确认,后沿移动一个单位 发送缓冲区和发送窗口有何区别? 是不是可以这么理解:我想发一个分组,先把它放到发送缓冲区中等待发送,发送窗口实施发送 **接收窗口:** GBN: 窗口大小为1,并且罩在0号分组上,意思是指现在只接收0号分组,其他分组来了就丢弃,然后给出收到的最高分组的ACK确认(累计确认),超时定时器启动,从发送窗口的后沿重新发送 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210612204450998.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 疑问: 虽然可以流水线式地同时发送多个分组,但接收窗口是一个个移动的,那速度会变快吗?会的,因为接收窗口的移动不需要传输,直接软件实现 SR: 窗口大小大于1(非累计确认) 每个分组都都一个超时定时器 1. 收到的分组位于后沿,则给出后沿分组的ACK确认,接收窗口向前移动 2. 收到的分组不位于后沿,但在接收窗口范围内,则给出该分组的ACK确认,窗口不动 3. 收到的分组不位于接收窗口范围内,接收窗口复位? ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210612205445744.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 疑问: 1. 接收方怎么事先知道要接收哪些分组呢? 2. 如果后沿的分组迟迟不来呢?-> 后沿对应分组的超时计时器启动超时重传 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210612210120300.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 对于SR,如果窗口大小大于$2^{n-1}$,在一批流水ACK返回失败时,接收窗口移动后,却包含了需要重新发送的分组,导致程序以为这是符合逻辑的,区分不出这是异常情况。 ### 3.5 面向连接的传输TCP(1:33:51) 应用层向下交付字节流,传输层将大的字节流按最大报文段(MSS)分成多个组,加上TCP头部形成多个报文段,报文段的body部分第一个字节即为每个分组在整个字节流中的偏移量,后面是每个分组的内容。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210612215008609.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 序号(按最大报文段编号):报文段的首字节在字节流的编号 确认号:表明已收到该序号之前的所有字节段,期望从另一方收到的下一个字节的序号 **可靠数据传输** 通过字节流的序号和确认号来告诉对方传输的信息位于字节流的哪部分 疑问: 为什么不每次传输的时候告知报文段在字节流中的起始、终止位置,而要告诉对方:我这次是从哪开始的,你下次又是从哪开始的?因为通常字节流中包含多个需要传送的报文段,这些报文段共同组成所需信息,确认号意味着此序号之前的报文段已经传送完毕,下次希望对方从哪开始传。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210613101251124.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) **TCP发送方(简化版)** 首先指定我从哪开始发,我想要你从哪开始发 疑问: 为什么不从固定的序号开始发,而是每次都要指定呢?为了防止滞留在网络中的段对传输造成影响,老的数据对新的数据可能产生影响?是的,比如上次连接关闭前仍有数据滞留在网络中,下次连接又使用了相同的端口建立连接,这时会把滞留的数据当成新的数据接收,所以在3握手建立TCP连接时,要随机选择初始序号,或者用时钟的低32位。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210614102517312.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021061410583230.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) **TCP流量控制** 通过反馈将接收方的剩余空间(buffer)告诉发送方。 捎带作用。 **TCP连接管理** 正式交换数据前,建立连接。 同意建立连接 同意连接参数 Q:为什么2次握手建立连接不总是可行的? 1. 变化的延迟(连接请求的段没有丢,但可能超时,滞留在网络中,过会又到达,而由于超时定时器的作用,在没有收到确认之前,又发送了请求,过会又到达,第一次服务器同意,第二次服务器也同意,这样服务器维护了半连接,把旧数据当成新数据接接收资源浪费。疑问:为什么不将第二次丢弃呢?因为无法分辨?) 2. 由于丢失造成的重传 3. 报文乱序 4. 相互看不到对方 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210614110602118.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 第3次握手通常和数据传递放在一起。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210614154624794.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 疑问: 客户端怎么辨别虚假的连接?为什么服务器不能辨别? **TCP关闭连接** 对称释放,并不完美 一边一边拆除:客户端告诉服务器要拆除连接,服务器说好的,但服务器向客户端的连接还存在,仍要拆除。-> 两军问题 最后用一定定时器,在这期间确实没有数据交换,则真正关闭连接。 ### 3.6 拥塞控制(32:06) 太多的数据需要网络传输,超过了网络的处理能力。 表现:分组丢失(路由器缓冲区溢出);分组经历比较长的延迟(在路由器的队列中排队) 拥塞原因: 1. 进入的速率$\lambda_{in}$接近链路带宽R时,延迟增大; 2. 路由器的缓冲是有限的; 3. 分组丢失时,发送端要重传,此时应用层的输入=应用层的输出,传输层的输入大于应用层的输入,因为要包括重传; 控制方法: 1. 网络辅助,网络反馈信息给终端; 2. 端到端 ,TCP如此,自身判断是否拥塞。 实际: ATM网络,信元,轻载 ### 3.7 TCP拥塞控制(45:59) 端到端的拥塞控制,小型网络用网络设备反馈更好。 简化网络核心。 - 路由器的负担较轻; - 端系统根据自身得到的信息,判断是否拥塞 **如何判断:** - 超时(拥塞) - 原因1:网络拥塞(概率大) - 原因2:出错被丢弃(会造成误判,但概率小,不影响总体控制方向) - 有关某个段的3次重复ACK(轻微拥塞) **如何调节发送方向网络中注入的速率:** $rate=\frac{Cong Win}{RTT}$,CongWin是动态的,是感知到的网络拥塞程度的函数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615200648965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615200912591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210615214236923.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) - 阅读全文 -
计算机网络 2. 应用层 2021-05-22 浏览量 691 暂无评论 [TOC] ## 课程主要内容 - 应用层协议原理 - Web and HTTP - FTP* - Email - SMTP - POP3 - IMAP - DNS - P2P应用 - CDN - TCP套接字(Socket)编程 - UDP套接字编程 ## 2.1 应用层原理 传输层向应用层提供Socket API 应用层的协议最多(因为可以由用户自己实现) ### 客户-服务器(C/S)体系结构 服务器: - 一直运行 - 固定的IP地址和周知的端口号、 - 扩展性:服务器场 - 数据中心进行扩展 - 扩展性差 - 可靠性差 客户端: - 主动与服务器通信 - 可能是动态IP - 不直接与其它客户端通信 ### 对等体(P2P)体系结构 - (几乎)没有一直运行的服务器 - 管理麻烦(维护主机在线状态) ### 混合体 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210509105149908.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 进程通信 同一主机内,使用进程间通信机制通信(由操作系统定义) 不同主机,通过交换报文(Message)通信 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210509105611311.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 问题1:IP+端口 问题2:来自谁+是什么+给谁 每次传输都要携带如此多的信息,太繁琐易错,可以使用socket简化。 TCP socket:用代号标示通信的双方(4元祖,源IP,源port,目标IP,目标port)(像OS打开文件返回的句柄),本地标示,使得穿过层间的信息减少 UDP socket:用一个整数表示本应用实体的标示(2元祖,本IP,本port),因为前后报文可能给不同的分布式进程 但是传输报文时,必须提供对方IP,port 应用层协议: 定义了运行在不同端系统上的应用进程如何相互交换报文 - 报文类型 - 报文类型的语法 - 语义 - 进程何时、如何发送报文及对报文进行响应的规则 应用协议仅仅是应用的一个组成部分,如Web应用包括:HTTP协议,web客户端,web服务器,HTML ### 传输层提供的服务 TCP服务 - 可靠的传输服务 - 流量控制 - 拥塞控制 - 面向连接 - 不能保证时间、最小吞吐、安全 UDP服务 - 不可靠数据传输 - 没有流量控制、拥塞控制、带宽保证、建立连接 那为什么还有存在的必要呢? - IP提供主机到主机的通信,不能区分进程 - 无需建立连接 - 不可靠,但简单 - 没有那些控制,那可以按设定的速度进行传输 ### 安全性 TCP和UDP都没有加密,铭文通过互联网传输,甚至密码 SSL(安全套接字层),应用采用SSL库,SSL库使用TCP通信 - 在TCP上实现,提供加密的TCP连接 - 私密性 - 数据完整性 - 端到端的鉴别 SSL socket API 应用将明文交给socket,SSL将其加密在互联网上传输 ## 2.2 Web与HTTP ### HTTP概况 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513215627761.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) HTTP运行在TCP之上,一个socket监听80号端口的连接,如果收到客户端的请求,则创建一个新的socket与之连接,原来的socket不变,持续监听 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513221232573.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### HTTP连接 非持续(四次挥手后关连接) 持续(四次挥手后不关连接,流水(默认)和非流水方式) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513222100995.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### HTTP请求报文 Get,Post,Head,http 1.1新增 Put,Delete Get命令不需要实体部分,POST需要 ![在这里插入图片描述](https://img-blog.csdnimg.cn/202105132227381.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513222754917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 提交表单输入 有两种方式,Get和Post都可以。 Get:在URL中使用特定的参数,参数:wd,cl,参数值:xx+yy+zzz,3 `http://www.baidu.com/s?wd=xx+yy+zzz&cl=3` ### HTTP响应报文 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513223954925.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) `\r\n`回车换行,为什么要Content-Length呢?因为TCP是字节流的,不区分报文与报文的边界,需要应用进程自己维护边界 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210513224011354.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) #### 用户-服务器状态: cookies #### Web缓存(代理服务器) - 客户端访问更快,服务端负载减轻,网络负载减轻 - 可以使用本地代理服务器加速访问,因为用户访问的网站具有趋同性 - 但是可能缓存的和服务器的不一致的,过期了,这就要用到condition get,将本地保存的修改时间与服务器的修改时间比较 ### 2.3 FTP 端口号:21 FTP的用户名和密码都是明文传输的,怪不得要用SFTP呢 控制连接+数据连接 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210521222903938.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 2.4 EMail 3个主要组成部分: - 用户代理(客户端软件,outlook) - 邮件服务器(守候在25号端口) - 协议(SMTP ) 收到的邮件放在邮件服务器的文件夹中,由用户代理拉取,拉取的协议有POP3,IMAP(提供更多特性,可创建远程目录),HTTP 原始的协议只能传输ASCII,需要打补丁才能传汉字、可执行文件等,MIME(多媒体邮件扩展) ### 2.5 DNS 域名到IP地址的转换;负载均衡(决定解析为哪个分布式主机来提供服务) 运行在UDP之上,53号端口,在端系统(网络边缘)实现 #### 2.5.1 怎么命名? - 为避免重名问题,使用层次化的命名 DNS域名结构: 几百个顶级域名,包括通用的和国家的。 根名字服务器:13个 #### 2.5.2 怎么解析 名字空间划分为若干区域 上层域需要知道下层的物理地址,区域名字服务器维护资源记录(resource records) - 作用:维护域名-IP的映射关系 - 位置:Name Server的分布式数据库中 RR格式: - domain_name - ttl: time to live,权威(无限大)或缓冲(有限),缓冲为了性能,删除为了一致性 - class类别:对于Internet,值为IN - Value值:数字、域名或ASCII串 - Type类别:资源记录的类型 DNS记录: Type = A - Name为主机 - Value为IP地址 Type = NS - Name为域名 - Value为该域名的权威服务器的域名 Type = CNAME - Name为规范名字的别名 - value为规范名字 设备上网:IP,所在局域网的子网掩码,网关,local name server ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210524215114214.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210524215126444.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 添加记录: 1. 注册域名; 2. 一个记录指向新增子域的域名,一个记录指向域名服务器的地址 3. 在指定的域名服务器中要确保有用于Web服务器的类型为A的记录 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210524220730890.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 在腾讯云买的域名,提供DNS解析,要自己添加A记录,使域名指向IP,然后DNS服务器会将该映射记录下来。 #### 2.5.3 攻击DNS DDos攻击,重定向攻击 ### 2.6 P2P应用 C/S:扩展性问题,可靠性问题 文件的Hash值是唯一标识 非结构化P2P: 1. 集中化目录,最初的Napster a. 当对等方连接时,它告知中心服务器IP地址和内容(我来了,我有什么) b. 某客户端请求某个文件,目录服务器告诉它谁有 c. 客户端从“谁”那请求文件 缺点:单点故障问题,目录服务器侵权容易定位,性能瓶颈 2. 完全分布式,Gnutella a. 泛洪查询; b. 邻居会重复,需要解决 c. 网络是怎么建立来的呢?在安装软件是,有一些列表(它们大概率是在网络中的),ping它们,它们pang回来,随机选取一些节点构建overlay d. 怎么下线?A依次告诉邻居们下线了,邻居们知道后再选一个节点,维持连接强度 3. 混合式,KaZaA,安排个组长 BitTorrent:tit-for-tat 你为我提供的服务多,我为你提供的也多 torrent文件中储存了tracking sever信息,告诉客户端向哪些节点请求 DHT(结构化) 维护树状或环状拓扑,查询快,副本不需要太多 ### 2.7 CDN #### 2.7.1 视频流化服务和CDN:上下文 如何向众多用户提供服务?(Content Distribution Networks) 特点:用户规模大,具有异构性,“二八分布”,重复流量多,“跳”多 解决方案:分布式的,应用层面的基础设施,通过CDN,全网部署缓存节点 CDN运营商:节点部署,内容部署,节点拥塞时的动态选择 多媒体流化服务:DASH enter-deep:部署在local ISP,离用户近,用户体验好 bring-home:部署在少个关键位置 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210525220510953.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 简单的例子: 客户端请求视频http://example.com/abc123,视频存储在CDN,位于http://kingCDN.com/exabc123 访问local DNS,local DNS经过处理后告诉客户端到kingCDN的权威名字服务器解析http://kingCDN.com/exabc123,解析完后,告诉客户端到哪个最近的CDN服务器IP获取内容 ### 2.8 Socket编程 #### 2.8.1 TCP套接字编程 TCP传输字节流 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210526160045710.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210526160100594.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) #### 2.8.2 UDP套接字编程 在客户端和服务器之间没有连接,传送的也叫数据报,和IP传送的东西叫法一样 发送端在报文中要指明**目标IP和端口,发送的内容,谁发的** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210526162007296.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) - 阅读全文 -
计算机网络 1. 概述 2021-04-29 浏览量 1209 暂无评论 [TOC] ## 课程主要内容 - 计算机网络和互联网 - 应用层 - 传输层 - 网络层:数据平面 - 网络层:控制平面 - 数据链路层和局域网 - 网络安全 - 无线和移动网络 - 多媒体网络 - 网络管理 每层的功能范围比服务广,因为功能需要通过层间接口向上层提供服务(我有很多东西,但是给你的暂时只能这么多),本层的功能需要通过层间接口调用下层的服务来实现。 可靠性(不丢失,不失序,不重复)要求高的,跑在TCP(面向连接,三次握手,四次挥手)上; > 连接建立,连接确认,发请求,对象回来 可靠性要求不高,但实时性要求高,跑在UDP上,实时多媒体,网络直播,事务性的应用 传输层:进程->进程,粒度更细 网络层:提供端到端的服务,源主机->目标主机,IP数据报(不可靠) > 传统方式:路由器+IP:路由算法计算路由表给IP协议用,路由协议提供路由信息 > 缺点:死板,固定 > 软件定义网络(SDN):数据平面+控制平面(SDN交换机),流表,网络可编程,灵活性高 数据链路层:提供点到点(P2P)服务(相邻点),帧,网卡 物理层:数字信号--物理信号 ## 1.1 什么是Internet 网络 计算机网络构成角度: - 节点:主机节点host(源、目标),数据交换节点(路由器R:网络层,交换机S:数据链路层,中继器) - 边:接入网链路Access(与主机连的),主干链路Backbone(数据交换节点间的) - 协议:对等层的实体进行通信所遵守的规则,包括格式(语法,语义)、次序、动作 计算机网络服务角度: - 分布式的应用进程 - 基础设施(提供服务[面向连接TCP,无连接UDP],应用层以下) 互联网:以TCP/IP为主,接入公共网络的 Internet标准: - RFC - IETF ## 1.2 网络边缘 ### 1.2.1 网络结构 - 网络边缘 - 主机 - 应用程序 - 网络核心 - 互联的路由 - 网络的网络 - 接入网和物理媒体 - 将边缘接入核心 ### 1.2.2 工作模式 **客户端/服务器模式(C/S)** 可扩展性差 **对等模式(peer-peer)** 分布式通信,互相提供资源,迅雷,电驴 ### 1.2.3 面向连接服务 **目标:** 在端系统之间传输数据 **握手:** 在数据传输之前做好准备 **TCP服务:** 多愁善感,总是考虑别人。 - 可靠地、按顺序地传送数据 - 流量控制:发送方不会淹没接收方 - 拥塞控制:网络拥塞时,发送方降低发送速率 HTTP(Web), FTP, Telnet, SMTP ### 1.2.3 面向无连接服务 **UDP服务** 直爽,快,不可靠,容易伤害人 流媒体、远程会议、DNS、Internet电话 ## 1.3 网络核心 ### 1.3.1 电路交换 线路独享(传统电话) 将线路与线路之间的带宽分成片,方法: - 频分(FDM) - 时分(TDM),划分时间片,一个周期分成几份 - 波分(WDM),光通信 连接建立时间长,浪费多,不适合计算机之间的通信(具有突发性,嗖一下就结束通信了) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210421221148653.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 1.3.2 分组交换 将数据分组,每次传输packet时使用主机间的所有带宽,存储--转发实现线路共享(比如要从A传到C,先从A到B,这段时间B与C之间可以进行其他通信)。 延迟比电路交换高(存储时间+排队时间),以时间换共享(按需使用),支持的主机通信数量更多(突发性强的情况) 统计多路复用(特殊的时分) ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021042122543987.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) **数据报网络** - 在通信之前,无需建立起一个连接,有数据就传输 - 每个分组都独立路由(路径不一样,可能会失序,目标地址一样,但是路由表变,通俗点讲,就是条条大路通罗马) - 路由器根据分组的目标地址进行路由 **虚电路(VC)网络** 有连接,虚电路靠信令建立起来,存储,转发,没太懂 注意:有连接和面向连接不一样,面向连接只体现在源主机到目标主机的通信实体上,中间的路由器不维护它们的通信状态 ## 1.4 接入网和物理媒体 ### 1.4.1 住宅接入 **modem:** 当年长城将宽带通到每户,成本太大,死掉了,想法很好;一种节约成本的方式就是利用已有的资源,比如固话网路,电话线传播的是音频信号(最高4kHz),利用调制解调器(猫)将网络信号附着在音频信号上(调频、调幅、调相位,规定什么代表0,什么代表1),但缺点是带宽很窄(56kb/s),不能同时上网和打电话。 ADSL:后来对其改进,提高频率,4KHz一下用于语音通信,4kHz以上按非对称方式划分为上行1M(小)和下行10M(大) **接入网:线缆网络** 有线电视,同轴电缆,共享带宽 其实还可以利用电网,加一个power-modem ### 1.4.2 企业接入(Enternet) 路由器->交换机->交换机级联 ### 1.4.3 物理媒体 可见的:导引型,传的远 不可见的:非导引型,传的近 双绞线,同轴电缆(两根同心的铜导线),光缆 单模光纤:光只能垂直进入,加工难度大 多模光纤:光进入时可以有一定的角度,导体直径大,加工难度下 许多光纤加上钢芯和保护层才叫做光缆 ## 1.5 Internet结构和ISP 互联网由一堆ISP(Internet Service Providers)连接组成。 怎么连接呢?总不能全连接吧,不可扩展 将每个接入ISP都连接到全局ISP,客户ISPs和提供者ISPs有经济合约 不同投资者建了不同的全局ISP,那既有竞争,又要合作,把不同的全局ISP对等连接 业务细分(全球接入和区域接入):终端 -> access ISP -> regional ISP -> global ISP ICP部署数据中心网络来提升用户体验(各数据中心机房用专线连接,减少访问延迟),减少运营成本(长途费?) 可以接到很多ISP,养鱼 ## 1.6 分组延时、丢失和吞吐量 为什么会发生延时和丢失? > 因为分组需要排在路由器的分组队列中,排到头才能走,如果队列已经满了,分组就会被丢弃掉,发生丢失;分组到达链路的速率超过链路输出的能力,就会排队,导致延时+(传输延时) 四种分组延时 > 检查节点延时+排队延时+传输延时(分组长度/链路带宽)+传播时间(链路长度/媒体速率) 排队延时 > 流量强度 = La/R,其中R为链路带宽,L为分组长度,a为分组到达队列的平均速率 > 接近0时,平均排队延时很小,接近1时,延时变得很大,为什么呢 tracert命令测试延时,基于ICMP报文实现,利用TTL(生存时间)减为0时路由器发回通知报文计算。 ## 1.7 协议层次和服务模型 某层提供的服务包括所有下层提供的服务,并且新增了对等层间的新的服务。 本层协议实现需要借助下层提供的服务,目的是为上层提供服务。 服务访问点(SAP)用来识别传输目标,TCP向哪个应用(上层用户)传输什么数据。 数据单元(DU):上层SDU通过层间接口传到下层,加上下层的控制信息ICU,得到PDU(一对一,一对多,多对一) **PDU在各层的名称:** 应用层:应用报文(message) 传输层:报文段(segment),TCP段,UDP数据报,进程到进程(借助Socket) 网络层:分组(packet),如果是无连接的,则叫IP数据报(datagram),端到端 链路层:帧(frame),将物理层的位包装成帧,相邻两点 分层也有坏处:效率低 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210429214158112.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021042921480887.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) - 阅读全文 -
操作系统 4. 外设与文件系统 2021-04-13 浏览量 776 暂无评论 [TOC] ## 4.1 IO系统 显示器与键盘为终端设备。 ### 4.1.1 IO与显示器 给外设的控制器/存储器写入指令,让其操控设备,操控完向CPU发出中断。 形成**文件视图**;发出OUT指令;形成中断处理。 每个设备都有自己的访问方式(格式),为了使用方便,将它们写成文件,形成文件视图。 无论什么设备,系统的调用接口都是`open(), read(), write(), close()`。 每个接口下面都有好多分支,根据信息一步步找到需要操作的设备并操作。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408222924337.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 4.1.2 键盘 每个键都有一个扫描码,根据键表中对应的扫描码作出响应。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210408224933158.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ## 4.2 磁盘与文件 ### 4.2.1 生磁盘的使用 比键盘和显示器复杂得多。 磁盘由一个个盘面组成,每个盘面外环为磁道,内环为扇区,扇区是磁盘的访问单位,大小为512字节。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210411203634124.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) 磁盘IO过程:控制器-->寻道(磁臂)-->旋转(磁道)-->传输 控制器需要的参数:柱面(cyl)、磁头(head)、扇区(sec)、缓存位置 再进一步: 通过盘块号(block)读写磁盘,**磁盘驱动**负责从block计算出cyl,head,sec(CHS) 怎么计算呢? 由一维向三维编址 如何编址:要让block相邻的盘块可以快速读出(满足实际) 磁盘访问时间 = 写入控制器时间 + 寻道时间 (12ms to 8ms)+ 旋转时间(7200r/min, 半周4ms) + 传输时间(50M/s, 0.3ms) 寻道时间最长,那么要让相邻的盘块放在相邻的扇区或盘面(磁头),由此可得到 $block=C*(Heads*Sectors)+H*Sectors+S$ 盘块可以包含多个扇区,这样一次读取多个连续扇区,可以提高读写速度(以牺牲空间为代价) **多个进程通过队列使用磁盘** 涉及到调度,主要目标是平均访问延迟小,寻道时间占主要部分 FCFS SSTF(短寻道优先,不公平) SCAN C-SCAN(电梯算法,完成一次-复位【复位的时间很短】) ### 4.2.2 从生磁盘到文件 关键:从文件得到盘块号 用户:文件为字符流 磁盘:文件为盘块 文件:建立字符流到盘块号集合的映射关系 每个文件都有个对应的FCB,存放文件存放的起始块 不同的文件使用不同的实现方式(如word需要动态增长) 实现方式: - 连续结构,不适合动态增长,适合直接读写 - 链式结构 - 索引结构,多级索引 ### 4.2.3 文件使用磁盘的实现 inode->盘块号->电梯队列->CHS->out磁盘控制器->驱动马达->电磁形成数据 其他设备,inode->对应设备函数 ## 4.3 文件系统 ### 4.3.1 目录与文件系统 磁盘-->目录树 核心问题:怎么将一系列文件映射到整个磁盘 发展:所有文件放在一起-->分用户存放-->目录树(分治) **如何高效地根据路径名,得到文件的FCB(inode)?** 首先,不能在每次读取目录下的文件时,将该目录下所有文件的PCB读进内存,这样太浪费资源,而且频繁的硬盘读会降低速度 目录项:文件名+对应的FCB地址() 根目录(存在硬盘的FCB数组中的第0个,根据根目录文件找到它的数据块(目录中存放的内容))-->匹配数据块的目标字符,再定位FCB数组中第12个元素找到下一个数据块,依次往下进行,知道找到目标文件的FCB 那么,要使整个系统能够自举,还需要存储一些信息,比如根目录的位置,通常,磁盘被格式化为如下形式: 引导块 超级块 inode位图 盘块位图 inode 数据区 引导块:大小一般固定 超级块:存放inode位图和盘块位图的位置(**在系统中mount,其实就是读超级块**) inode位图:哪些文件的inode是空的(可以再被分配使用) 盘块位图:哪些盘块是空的 疑问:inode位图取多大呢?怎么知道我将来要放的文件的数量呢? ### 4.3.2 目录解析代码实现 1. 解析根目录: 在系统启动shell时,mount_root() 2. 读取inode--iget() 3. 找到目录项--find_entry() 匹配文件(夹)名 跳至2,直到树底 操作系统全图: 进程带动 多进程视图+文件视图 - 阅读全文 -
操作系统 3. 内存管理 2021-04-07 浏览量 763 暂无评论 [TOC] ## 3.1 内存管理 ### 3.1.1 内存使用与分段 时间:2021-03-28 场景:汇编中,call 40这条指令表明要跳到40这个地址(逻辑地址、相对地址)执行,那么把程序载入到内存中时,物理内存的40位置一定要存放对应的程序,如果不对内存进行分段的话,这显然不切实际,因为40这个地址可能存放了别的程序(事实上,0开始的位置存放的是操作系统),所以,程序在载入内存时应当找到一段空闲内存,并修改相应的逻辑地址为物理地址(==重定位==)。 重定位也有两种方式: 1. 编译时(硬系统,如一些嵌入式的系统,效率高,但不灵活) 2. 载入时(灵活通过,==但目前来讲,程序载入进内存就不能移动了==) 怎么才能让载入内存的程序实现移动呢? ==swap==:如果进程发生阻塞,那把它放在内存中有点浪费内存,这时候将该进程睡眠,换出到磁盘(已经重定位过了物理地址),想执行的时候再换入,那么问题来了,怎么保证它换入时的物理内存地址和原来一样呢?==运行时重定位(地址翻译)== `base+offset` `base`保存在哪呢?==PCB====>==基址寄存器== 还有个问题:如果找不到一段足够大的空闲内存放程序怎么办呢? 将程序分段,每个段有各自的特点、用途,如代码段(只读)、数据段(可写),每个段都是从0开始编址(用户可独立考虑每个段[==分治==]) 这样就可以将一个程序的每个段分别放入内存,==段号+段内偏移== 这时候PCB里要存放每个段的基址(进程段表LDT,和操作系统对应的段表GDT同理) ### 3.1.2 内存分区与分页(Memory Partition and Paging) 时间:2021-03-29 **程序进入内存的步骤:** 1. 程序分段 2. 在内存中找出空闲区域 3. 将程序从磁盘中读到内存 **为什么要分区?** 由上一小节很容易引出内存分区,因为要看哪些部分是空闲的,大小为多少 **怎么分区呢?** 固定分区与可变分区 可变分区的管理:请求分配,释放内存,再次申请(涉及到分配哪块空闲的) 分配哪块呢?首选适配(第一个,快),最佳适配(大小最合适,O(n)),最差适配(每个段均匀) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210329221126223.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70#pic_center) 实际的系统的物理内存并不使用内存分区,而是内存分页 **为什么要分页?** 解决内存分区导致的内存效率问题,比如内存碎片,需要将空闲分区合并(内存紧缩),但执行该操作的时候CPU不能干别的。 **怎么分页?** 将内存分成,比如每4K一页,针对每个段内存请求,将段打散,系统一页一页地分配给这个段,(那重定位就变得复杂点,得知道它想跳转的地方被分到了哪个页,地址//页大小(根据这个找到页框的物理地址)+地址%页大小),需要用到页表(cr3),==MMU会自动计算== ### 3.1.3 多级页表和快表 时间:2021-04-01 之前的分页方法有什么问题? 为了提高内存的空间利用率,页应该小,但是页小了页表就大了 怎么解决? 尝试1:分配给进程中的逻辑页中,有的逻辑页用不到,(要问为什么用不到还要分配给它的话,考虑重定位时的逻辑页总数:地址//页大小 + 1),那就只保留用到的逻辑页的页表项 问题1:这时的页表不连续,查找起来费时,顺序查找-->折半查找,但这也费时,降低运行速度,页号连续的话查找就很快,怎么结合这两个呢?==多级页表就来了== 尝试2:页目录表+页表(类比书的目录) 可以,但访问内存的次数变多,时间上更奢侈 尝试3:快表(TLB,相联快速存储,是寄存器) 快表中存放最近使用的页表项,找不到的话再到多级页表中找 要让快表好使,它的命中率一定要高,所以快表越大越好(但它贵呀,一般64~1024);同时,置换的算法也很重要 ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021040121290171.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) ### 3.1.4 段页结合的实际内存管理 程序(分段)-->虚拟内存(地址空间,段->页)-->物理内存(分页) 逻辑地址-->虚拟地址-->物理地址 段面向用户,页面向硬件 虚拟内存不用管理吗?还是说浪费了也没事,有碎片就有碎片,反正不贵 ``` 段页式内存下程序如何载入内存? 1. 在虚拟内存中分配段 2. 将程序对应段放到虚拟内存对应段; 3. 将虚拟内存的段分页放到物理内存; 4. 建立页表; 5. 使用。 ``` ## 3.2 虚拟内存 使用换入换出实现虚拟内存。 ### 3.2.1 请求调页与内存换入(Swap in) 2021-04-06 物理内存小于虚拟内存,虚拟内存比较规整,就算物理内存没那么大,虚拟内存也能让用户感觉可以使用那么大,不用因为物理内存的大小而头疼,那么就**使用换入换出实现“大内存”**,用到程序的哪段就把这段从硬盘换入到物理内存。 ### 3.2.2 内存换出(Swap out) 2021-04-07 还记得请求调页时候的`get_free_page`吗?而内存是有限的,并不能总是获得新的页,需要选择一页淘汰,换出到磁盘,那怎么选择呢?用缺页次数来评价方案的好坏。 - FIFO,刚换出去又要换进来的话,就很麻烦 - MIN,选最远将使用的页淘汰,是最优方案,但是要知道将来发生的事 - LRU(Least recently used),用过去的历史预测将来,选最近最长一段时间没有使用的页淘汰 LRU怎么实现呢? **时间戳(time stamp)** 每页维护一个时间戳,记录该页在第几个时刻使用到了。(每执行一条指令,都要维护这个时间戳) **页码栈** 每执行一条指令,都要更新栈 LRU近似实现SCR[又叫Clock Algorithm](因为准确实现太奢侈) 将时间计数变为是和否,每页加1个引用位R,每次访问一页时,硬件自动设置该位(就不用软件维护时间戳了,直接MMU),选择淘汰页:扫描该位,是1时清0,并继续扫描,是0时淘汰。 但如果缺页很少的话,几乎所有的R都为1(因为只有缺页时才会将0变为1),那需要换页的时候,指针会循环一遍,把所有的1变为0,然后换掉第一次指向的页,SCR退化为FIFO。 原因:记录了太长的历史信息 怎么办:定时清除R位(再来一个快的扫描指针),确保“最近” 给进程分配多少页框(frame)呢? 求工作集,覆盖局部。 与页框分配有关的问题:系统颠簸 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210407223739931.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NzkyOTU5,size_16,color_FFFFFF,t_70) - 阅读全文 -