对于read write的封装还是有问题

This commit is contained in:
impact-eintr
2022-12-30 18:27:17 +08:00
parent 12c26a86f1
commit 6156ee63cd
7 changed files with 269 additions and 21 deletions

View File

@@ -129,7 +129,7 @@ func (n *NIC) addAddressLocked(protocol tcpip.NetworkProtocolNumber, addr tcpip.
// 如果不存在 就会调用arp协议发广播来定位这个ip对应的设备
if n.linkEP.Capabilities()&CapabilityResolutionRequired != 0 {
if _, ok := n.stack.linkAddrResolvers[protocol]; ok {
ref.linkCache = n.stack
ref.linkCache = n.stack // 对于loopback驱动而言 他的缓存就是nil 不开启
}
}

View File

@@ -75,9 +75,7 @@ func (r *Route) Capabilities() LinkEndpointCapabilities {
// 如果需要地址解析则返回ErrNoLinkAddress和通知通道以阻止顶级调用者。
// 地址解析完成后,通道关闭(不管成功与否)。
func (r *Route) Resolve(waker *sleep.Waker) (<-chan struct{}, *tcpip.Error) {
if !r.IsResolutionRequired() {
// Nothing to do if there is no cache (which does the resolution on cache miss) or
// link address is already known.
if !r.IsResolutionRequired() { // 没有配置地址解析
return nil, nil
}
@@ -109,9 +107,7 @@ func (r *Route) RemoveWaker(waker *sleep.Waker) {
r.ref.linkCache.RemoveWaker(r.ref.nic.ID(), nextAddr, waker)
}
// IsResolutionRequired returns true if Resolve() must be called to resolve
// the link address before the this route can be written to.
// 本地缓存有东西 而且不知道远端的MAC
// 检查是否允许了地址解析 首先检查是否配置了mac缓存 然后检查目标mac是否已经存在
func (r *Route) IsResolutionRequired() bool {
return r.ref.linkCache != nil && r.RemoteLinkAddress == ""
}
@@ -119,6 +115,7 @@ func (r *Route) IsResolutionRequired() bool {
// WritePacket writes the packet through the given route.
func (r *Route) WritePacket(hdr buffer.Prependable, payload buffer.VectorisedView,
protocol tcpip.TransportProtocolNumber, ttl uint8) *tcpip.Error {
// 路由对应的IP的WritePacket
err := r.ref.ep.WritePacket(r, hdr, payload, protocol, ttl)
if err == tcpip.ErrNoRoute {
r.Stats().IP.OutgoingPacketErrors.Increment()

View File

@@ -580,11 +580,13 @@ func (s *Stack) RemoveAddress(id tcpip.NICID, addr tcpip.Address) *tcpip.Error {
}
// FindRoute 路由查找实现比如当tcp建立连接时会用该函数得到路由信息
// 注意仅仅包含 SrcMAC SrcIp DstIp 没有 DstMAC
func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address,
netProto tcpip.NetworkProtocolNumber) (Route, *tcpip.Error) {
s.mu.RLock()
defer s.mu.RUnlock()
// 遍历协议栈的路由表
for i := range s.routeTable {
if (id != 0 && id != s.routeTable[i].NIC) || // 检查是否是对应的网卡
(len(remoteAddr) != 0 && !s.routeTable[i].Match(remoteAddr)) {
@@ -609,9 +611,10 @@ func (s *Stack) FindRoute(id tcpip.NICID, localAddr, remoteAddr tcpip.Address,
if len(remoteAddr) == 0 {
// If no remote address was provided, then the route
// provided will refer to the link local address.
remoteAddr = ref.ep.ID().LocalAddress // 发回自己? TODO
remoteAddr = ref.ep.ID().LocalAddress // 本地环回
}
// 构建一个路由 包括 目标ip 目标mac 本地ip 本地mac
r := makeRoute(netProto, ref.ep.ID().LocalAddress, remoteAddr, nic.linkEP.LinkAddress(), ref)
r.NextHop = s.routeTable[i].Gateway
logger.GetInstance().Info(logger.IP, func() {