mirror of
https://github.com/impact-eintr/netstack.git
synced 2025-10-24 13:13:06 +08:00
对于read write的封装还是有问题
This commit is contained in:
@@ -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 不开启
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user