TCP/IP交换技术和负载均衡

上次青云会议 提到七层交换,根据 URL 自定义转发策略,可以自由实现负载均衡,本文给出相关知识点,包括交换技术、OSI 七层模型和 TCP/IP。

Internet protocol suite (TCP/IP)

IP Stack

IP stack connections

In more precise terminology segment refers to the TCP protocol data unit (PDU), datagram (rfc791) to the IP PDU, and frame to the data link layer PDU.
segment (TCP PDU) -> datagram (IP PDU) -> frame (LINK PDU)

The internet layer is a datagram service provided by an IP. For example, UDP is run by a datagram service in the internet layer.

IP is an entirely connectionless, best effort, unreliable, message delivery service. TCP is a higher level protocol running on top of IP that can provide the addition of a connection-oriented service.

The term datagram is often considered synonymous to packet but there are some nuances. The term datagram is generally reserved for packets of an unreliable service, which cannot notify the sender if delivery fails, while the term packet applies to any packet, reliable or not.

Datagrams are the IP packets that provide a quick and unreliable service like UDP, and all IP packets are datagrams;

However, at the TCP layer what is termed a TCP segment is the sometimes necessary IP fragmentation of a datagram, but those are referred to as "packets".

Internet layering scheme and its relation to some common protocols
  • Encapsulation of application data (UDP)

    Encapsulation of application data (UDP) descending through the layers described in RFC 1122

  • RFC 1122:Requirements for Internet Hosts -- Communication Layers。

  • NAT:Network Address Translation。What is NATHow NAT Tables Work

    NAT table 的每条记录就是一条连接(a connection)。局域网的每个终端会有多个活跃的连接(active connections)。一条典型的记录约需 160 字节。网络地址转换是一个繁重的处理操作,极度耗费CPU处理性能。

  • UDP/IP frame and UDP header

    UDP header(固定8字节) and UDP/IP frame

    DNS query 53端口

  • TCP/IP frame and TCP header


    TCP header and TCP/IP frame(对比下图:在 acknowledgement number 前缺少4个字节的sequence number,Options and Padding Up to 40 bytes)
  • TCP Header 详解

    TCP Header 20-60 bytes: 基础20字节,可选数据最多40字节

    HLEN:Header Length

    This is a 4-bit field that indicates the length of the TCP header by a number of 4-byte words in the header, i.e if the header is 20 bytes(min length of TCP header), then this field will hold 5 (because 5 x 4 = 20) and the maximum length: 60 bytes, then it’ll hold the value 15(because 15 x 4 = 60). Hence, the value of this field is always between 5 and 15.


    TCP header 示例
  • List of TCP Ports and UDP Ports

OSI Model

OSI Reference Model
  • Segment:段

  • Packet:包

  • Frame:帧

  • Bit:字节

  • PDU & SDU

    PDU & SDU:来自 X.200.pdf

    PDU和SUD关系

  • (N-1)PDU=(N-1)PCI+(N-1)SDU

  • (N-1)SDU=(N)PDU。

OSI 和 TCP/IP 的关系
  • Application,Transport,Internet,Link,


    OSI 和 TCP/IP 的关系([draw.io])
  • IP corresponds to a subset of OSI Layer 3, the Network Layer.
  • TCP corresponds to OSI Layer 4 (transport) and some functions of Layer 5 (session).
  • TCP/IP makes no assumptions about what happens above the level of a network session (part of OSI Layer 5), while OSI defines several more layers of standardized functions.
  • TCP/IP makes no prescriptions as to the link layers below IP, where OSI specifies two.
  • IP defines how computers can get data to each other over a routed, interconnected set of networks.
  • TCP defines how applications can create reliable channels of communication across such a network.
  • IP defines addressing and routing, while TCP defines how to have a conversation across the link without garbling or losing data.
    一句话:IP 协议保证数据的传输,TCP 协议保证数据传输的质量;
  • TCP/IP doesn't map cleanly to the OSI model, since it was developed before the OSI model and was designed to solve a specific set of problems, not to be a general description for all network communications.
Network Interface Layer

As its name suggests, this layer represents the place where the actual TCP/IP protocols running at higher layers interface to the local network.

This layer is somewhat “controversial” in that some people don't even consider it a “legitimate” part of TCP/IP. This is usually because none of the core IP protocols run at this layer. Despite this, the network interface layer is part of the architecture. It is equivalent to the data link layer (layer two) in the OSI Reference Model and is also sometimes called the link layer. You may also see the name network access layer.

On many TCP/IP networks, there is no TCP/IP protocol running at all on this layer, because it is simply not needed. For example, if you run TCP/IP over an Ethernet, then Ethernet handles layer two (and layer one) functions. However, the TCP/IP standards do define protocols for TCP/IP networks that do not have their own layer two implementation. These protocols, the Serial Line Internet Protocol (SLIP) and the Point-to-Point Protocol (PPP), serve to fill the gap between the network layer and the physical layer. They are commonly used to facilitate TCP/IP over direct serial line connections (such as dial-up telephone networking) and other technologies that operate directly at the physical layer.

地址转换
  • arp 表(ip 地址 -> mac 地址)
    ARP 协议功能是:根据 ip 地址获取对应的 mac 地址,在 IP 协议集中,划在 Link layer,而在 OSI Model 中,在 2-3 层之间。
    通过广播一个 ARP 协议请求消息,能匹配到消息中指定 IP 地址的主机要予以回应,给出对应的 MAC 地址。
  • Windows下,arp -a 可以查看本地 ARP Table;
    -a: Displays current ARP entries by interrogating the current protocol data. If inet_addr is specified, the IP and Physical addresses for only the specified computer are displayed. If more than one network interface uses ARP, entries for each ARP table are displayed.
    >arp -a
    Interface: 192.168.1.204 --- 0x10005
Internet Address Physical Address Type
192.168.1.1 cc-34-29-94-11-1a dynamic
  • DHCP 时,使用 ARP 协议进行 ARP probe 和 ARP announcements,避免 ip 地址冲突。
  • mac 表(mac 地址 -> 交换机物理端口)
    交换机:mac 地址 -> 交换机物理端口,即交换机从 mac 表查询到对应的端口,将数据复制到该端口即可,连接在该端口上的主机即可收到数据。在每台主机发包或回应包的时候,交换机即可自动收集到 mac 地址和对应的物理端口的映射关系。
  • 私有IP地址
10.0.0.0 to 10.255.255.255
172.16.0.0 to 172.31.255.255
192.168.0.0 to 192.168.255.255
169.254.0.0 to 169.254.255.255 for Automatic Private IP Addressing (APIPA) use only.
100.64.0.0/10 for use in carrier-grade NAT environments

交换机(Switch)

传统的 交换机 是一种基于 MAC 地址识别、属于数据链路层的网络设备。交换机可以简单的理解为将若干机器连接起来组成一个小局域网。交换机工作原理比较简单,通常提到的交换机指的都是二层交换机,其上存在 MAC 表。

  • 按所处网络架构位置划分:核心交换机、接入交换机、汇聚交换机;
    核心交换机:放在网络主干部分(核心层)的交换机;
    接入交换机:直接面向用户连接或访问网络的部分称为接入层交换机;
    汇聚交换机:位于接入层和核心层之间的部分称为分布层或汇聚层;
  • 按传输介质和传输速度划分:以太网交换机、快速以太网交换机、千兆(G位)以太网交换机,万兆以太网,40/100 Gbit/s以太网,...
    常见的接口:RJ-45,SC(是一种光纤接口 Optical fiber connector);
  • 按端口结构划分:固定端口交换机和模块化交换机;
    固定端口交换机:桌面式和机架式;
  • 按交换机工作的协议层划分: 二层交换机、三层交换机、四层交换机;
    依据 OSI Reference Model;
    二层交换机是最原始的交换技术产品,桌面型交换机一般属于这类;
    所有的交换机在协议层次上来说都是向下兼容的,也就是说所有的交换机都能够工作在第二层;
  • 按是否支持网管功能划分:网管型和非网管型;
  • 广播域(Broadcast domain)和冲突域(collision domain)
    广播域:在数据链路层(OSI 二层),通过广播所能到达的计算机范围;连接到同一个交换机(二层交换)的所有计算机都属于同一个广播域;路由器或者高层网络设备划分了不同的广播域;
    冲突域:通常比广播域小,且包含在广播域内,属于物理层(OSI 一层)概念;Hub(集线器)的广播域和冲突域是相同的;Hub 是一层设备;2011年 802.3 已经废弃使用 Hub 和 中继器(Repeater)作为联网设备;


    广播域和冲突域
  • 划分 VLAN 是一种控制广播域大小的方法;一个 VLAN 就是一个广播域;
    当在交换机上划分 VLAN 后,不同 VLAN 间的设备就如同被物理地分隔开来。也就是说,连接到同一交换机,然而处于不同 VLAN 的设备,就像是被物理地连接到两个位于不同网段的交换机上一样,彼此之间的通信一定要经过路由设备,否则将无法得知对方的存在,将无法进行任何联系。
    Cisco 设备可以通过 VTP 域(VLAN Trunking Protocol,也称 VLAN 管理域)集中管理各个交换机上的 VLAN 配置;

路由器(Router)

路由器是一种连接多个网络或网段的网络设备,它能将不同网络或网段之间的数据信息进行“翻译”,以使它们能够相互“读”懂对方的数据,从而构成一个更大的网络。路由技术实质上可概括为两种功能:决定最优路由转发数据包。路由器上存在路由表、ARP 表。

路由器和交换机的区别

路由器和交换机之间的主要区别就是:交换机工作在 OSI 参考模型第二层,即数据链路层;路由器工作在第三层,即网络层。路由器默认是不支持二层的。

负载均衡

随着网络应用环境的多样化,出现三层交换机、四层交换机、七层交换机,也常称为负载均衡设备。

  • 二层交换机基于 MAC 地址识别,或者说基于 MAC 地址的二层负载均衡。
  • 三层交换机基于网络层 IP 地址来完成端到端的数据交换,或者说基于 IP 地址的三层负载均衡。三层交换存在 MAC 表、路由表、ARP 表。
  • 四层交换机就是基于 IP+端口的负载均衡。
  • 七层交换机,是高层智能交换,实现基于 URL 等应用层信息的负载均衡。

三层交换

三层交换机就是具有部分路由器功能的交换机,三层交换机的最重要目的是加快大型局域网内部的数据交换,所具有的路由功能也是为这目的服务的,能够做到一次路由,多次转发。对于数据包转发等规律性的过程由硬件高速实现,而像路由信息更新、路由表维护、路由计算、路由确定等功能,由软件实现。

三层交换机不是简单的二层交换机和路由器的叠加。实际应用过程中,典型的做法是:处于同一个局域网中的各个子网的互联以及局域网中 VLAN 间的路由,用三层交换机来代替路由器,而只有局域网与公网互联之间要实现跨地域的网络访问时,才通过专业路由器。

四层交换

在四层交换中,数据包的传输不仅仅依据 MAC 地址(第二层交换)或源/目标 IP 地址(第三层路由),还依据 TCP | UDP 端口地址(第四层地址),也就是说第四层交换除了考虑三层的逻辑地址外,还要考虑对端口地址的处理。

由于端口地址代表了不同的业务协议,所以第四层交换不仅仅进行了物理上的交换,还包括了业务上的交换。

因此,第四层交换是由源端和终端 IP 地址、TCP | UDP 端口共同决定,四层交换是真正的 “会话交换”。

七层交换

第七层交换技术可以定义为:数据包的传送不仅仅依据 MAC 地址(第二层交换)或源/目标 IP 地址(第三层路由)以及依据 TCP | UDP 端口(第四层地址),还可以根据内容(表示/应用层)进行。这样的处理更具有智能性,交换的不仅仅是端口,还包括了内容,因此,第七层交换机是真正的“应用交换机”。

七层交换突破了一般意义上的交换的概念,开始进入以进程和内容级别为主的交换范围。

由于和应用相关,我们说这时候的交换就有了智能性。交换机具有了区别各种高层的应用和识别内容的能力。这时的交换机不仅能根据数据包的 IP 地址或者端口地址来传送数据,而且还能打开数据包,进入数据包内部并根据包中的信息做出负载均衡、内容识别等判断。

简单模型

  • 5-7 应用层:HTTP;应用报文(Message);
  • 4 TCP:TCP/UDP;Port(端口地址);Segment(报文段)/Datagram;
  • 3 IP:IP地址;IP 路由;Packet(IP 分组);
  • 2 链路层:MAC 地址(物理网络地址/硬件地址);Frame(帧);
  • 1 物理层:Ethernet;802.11;Bit(位);

参考

按:本文最初于 2015-05-26 发表在新浪博客,今日转发。


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • 1. OSI,TCP/IP,五层协议的体系结构,以及各层协议 OSI分层 (7层):物理层、数据链路层、...
    iCaptain阅读 2,372评论 0 4
  • 名词延伸 通俗的说,域名就相当于一个家庭的门牌号码,别人通过这个号码可以很容易的找到你。如果把IP地址比作一间房子...
    杨大虾阅读 20,394评论 2 57
  • OSI模型,即开放式通信系统互联参考模型(Open System Interconnection,OSI/RM,O...
    zhoulujun阅读 2,213评论 0 17
  • 你不理财,财不理你!带我走上理财之路的,正是对我说了这句话的同事张哥,也是我心中比较钦佩的理财男神一枚,同事几年,...
    鲸鱼暖暖阅读 327评论 1 5
  • 类型操作 String 转换为 Integer Note: 类型转型前必须得先确定要转换的对象是存在的,否则会导致...
    solobat阅读 500评论 0 1