google永远的神

This commit is contained in:
impact-eintr
2021-09-08 21:54:54 +08:00
parent a5683465e8
commit 91012e5685
6 changed files with 276 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import (
"sync"
"time"
"github.com/impact-eintr/netstack/sleep"
"github.com/impact-eintr/netstack/tcpip"
"github.com/impact-eintr/netstack/tcpip/buffer"
"github.com/impact-eintr/netstack/tcpip/ports"
@@ -15,6 +16,7 @@ type transportProtocolState struct {
defaultHandler func(*Route, TransportEndpointID, buffer.VectorisedView) bool
}
// Stack 是一个网络堆栈包含所有支持的协议、NIC 和路由表
type Stack struct {
transportProtocols map[tcpip.TransportProtocolNumber]*transportProtocolState
networkProtocols map[tcpip.NetworkProtocolNumber]NetworkProtocol
@@ -30,11 +32,14 @@ type Stack struct {
nics map[tcpip.NICID]*NIC
forwarding bool
// route 是用户通过 SetRouteTable() 传入的路由表Find Route() 使用它来构建特定目的地的路由
routeTable []tcpip.Route
*ports.PortManager
// 如果不是 nil则任何新端点每次收到 TCP 段时都会调用此探测函数
tcpProbeFunc TCPProbeFunc
clock tcpip.Clock
// 用于生成用户可见的时间
clock tcpip.Clock
}
type Options struct {
@@ -86,3 +91,64 @@ type TCPSenderState struct {
func (s *Stack) CreateNIC(id tcpip.NICID, linkEP tcpip.LinkEndpointID) *tcpip.Error {
return s.createNIC(id, "", linkEP, true)
}
func (s *Stack) CreateNamedNIC(id tcpip.NICID, name string, linkEP tcpip.LinkEndpointID) *tcpip.Error {
return s.createNIC(id, name, linkEP, true)
}
func (s *Stack) CreateDisableNamedNIC(id tcpip.NICID, name string, linkEP tcpip.LinkEndpointID) *tcpip.Error {
return s.createNIC(id, name, linkEP, false)
}
// 新建一个网卡对象,并且激活它,激活的意思就是准备好从网卡中读取和写入数据
func (s *Stack) createNIC(id tcpip.NICID, name string, linkEP tcpip.LinkEndpointID, enabled bool) *tcpip.Error {
ep := FindLinkEndpoint(linkEP)
if ep == nil {
return tcpip.ErrBadLinkEndpoint
}
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.nics[id]; ok {
}
n := newNIC(s, id, name, ep)
s.nics[id] = n
if enabled {
n.attachLinkEndpoint()
}
return nil
}
// CheckLocalAddress 确定给定的本地地址是否存在
func (s *Stack) CahceLocalAddress(nicid tcpip.NICID, protocol tcpip.NetworkProtocolNumber,
addr tcpip.Address) tcpip.NICID
// AddLinkAddress 向缓存添加链接地址
func (s *Stack) AddLinkAddress(nicid tcpip.NICID, addr tcpip.Address, linkAddr tcpip.LinkAddress)
// GetLinkAddress 查找缓存以将地址转换为链接地址(例如 IP -> MAC
// 如果 LinkEndpoint 请求地址解析并且存在使用网络协议注册的 Link Address Resolver则缓存尝试解析地址并返回 EWouldBlock。
// 如果需要地址解析,则返回 ErrNoLinkAddress 和通知通道以供顶级调用方阻止。 一旦地址解析完成(成功与否),通道就会关闭。
func (s *Stack) GetLinkAddress(nic tcpip.NICID, addr, localAddr tcpip.Address,
protocol tcpip.NetworkProtocolNumber, ww *sleep.Waker) (tcpip.LinkAddress,
<-chan struct{}, *tcpip.Error) {
}
// RemoveWaker 移除已在 GetLinkAddress() 中添加的唤醒器。
func (s *Stack) RemoveWaker(nicid tcpip.NICID, addr tcpip.Address, waker *sleep.Waker) {
}
// 当NIC从物理接口接受数据包时将调用此函数
// 比如protocol是arp协议号 那么会找到arp.HandlePacket来处理数据报
// protocol是ipv4协议号那么会找到ipv4.HahndlePacket来处理数据报
func (s *Stack) DeliverNetworkPacket(linkEP LinkEndpoint, dstLinkAddr, srcLinkAddr tcpip.LinkAddress,
protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) {
}