repo
stringlengths
5
54
path
stringlengths
4
155
func_name
stringlengths
1
118
original_string
stringlengths
52
85.5k
language
stringclasses
1 value
code
stringlengths
52
85.5k
code_tokens
list
docstring
stringlengths
6
2.61k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
85
252
partition
stringclasses
1 value
google/netstack
tcpip/header/eth.go
DestinationAddress
func (b Ethernet) DestinationAddress() tcpip.LinkAddress { return tcpip.LinkAddress(b[dstMAC:][:EthernetAddressSize]) }
go
func (b Ethernet) DestinationAddress() tcpip.LinkAddress { return tcpip.LinkAddress(b[dstMAC:][:EthernetAddressSize]) }
[ "func", "(", "b", "Ethernet", ")", "DestinationAddress", "(", ")", "tcpip", ".", "LinkAddress", "{", "return", "tcpip", ".", "LinkAddress", "(", "b", "[", "dstMAC", ":", "]", "[", ":", "EthernetAddressSize", "]", ")", "\n", "}" ]
// DestinationAddress returns the "MAC destination" field of the ethernet frame // header.
[ "DestinationAddress", "returns", "the", "MAC", "destination", "field", "of", "the", "ethernet", "frame", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/eth.go#L60-L62
train
google/netstack
tcpip/header/eth.go
Type
func (b Ethernet) Type() tcpip.NetworkProtocolNumber { return tcpip.NetworkProtocolNumber(binary.BigEndian.Uint16(b[ethType:])) }
go
func (b Ethernet) Type() tcpip.NetworkProtocolNumber { return tcpip.NetworkProtocolNumber(binary.BigEndian.Uint16(b[ethType:])) }
[ "func", "(", "b", "Ethernet", ")", "Type", "(", ")", "tcpip", ".", "NetworkProtocolNumber", "{", "return", "tcpip", ".", "NetworkProtocolNumber", "(", "binary", ".", "BigEndian", ".", "Uint16", "(", "b", "[", "ethType", ":", "]", ")", ")", "\n", "}" ]
// Type returns the "ethertype" field of the ethernet frame header.
[ "Type", "returns", "the", "ethertype", "field", "of", "the", "ethernet", "frame", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/eth.go#L65-L67
train
google/netstack
tcpip/header/eth.go
Encode
func (b Ethernet) Encode(e *EthernetFields) { binary.BigEndian.PutUint16(b[ethType:], uint16(e.Type)) copy(b[srcMAC:][:EthernetAddressSize], e.SrcAddr) copy(b[dstMAC:][:EthernetAddressSize], e.DstAddr) }
go
func (b Ethernet) Encode(e *EthernetFields) { binary.BigEndian.PutUint16(b[ethType:], uint16(e.Type)) copy(b[srcMAC:][:EthernetAddressSize], e.SrcAddr) copy(b[dstMAC:][:EthernetAddressSize], e.DstAddr) }
[ "func", "(", "b", "Ethernet", ")", "Encode", "(", "e", "*", "EthernetFields", ")", "{", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "ethType", ":", "]", ",", "uint16", "(", "e", ".", "Type", ")", ")", "\n", "copy", "(", "b", "[", "srcMAC", ":", "]", "[", ":", "EthernetAddressSize", "]", ",", "e", ".", "SrcAddr", ")", "\n", "copy", "(", "b", "[", "dstMAC", ":", "]", "[", ":", "EthernetAddressSize", "]", ",", "e", ".", "DstAddr", ")", "\n", "}" ]
// Encode encodes all the fields of the ethernet frame header.
[ "Encode", "encodes", "all", "the", "fields", "of", "the", "ethernet", "frame", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/eth.go#L70-L74
train
google/netstack
tcpip/seqnum/seqnum.go
LessThanEq
func (v Value) LessThanEq(w Value) bool { if v == w { return true } return v.LessThan(w) }
go
func (v Value) LessThanEq(w Value) bool { if v == w { return true } return v.LessThan(w) }
[ "func", "(", "v", "Value", ")", "LessThanEq", "(", "w", "Value", ")", "bool", "{", "if", "v", "==", "w", "{", "return", "true", "\n", "}", "\n", "return", "v", ".", "LessThan", "(", "w", ")", "\n", "}" ]
// LessThanEq returns true if v==w or v is before i.e., v < w.
[ "LessThanEq", "returns", "true", "if", "v", "==", "w", "or", "v", "is", "before", "i", ".", "e", ".", "v", "<", "w", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/seqnum/seqnum.go#L31-L36
train
google/netstack
tcpip/seqnum/seqnum.go
InRange
func (v Value) InRange(a, b Value) bool { return v-a < b-a }
go
func (v Value) InRange(a, b Value) bool { return v-a < b-a }
[ "func", "(", "v", "Value", ")", "InRange", "(", "a", ",", "b", "Value", ")", "bool", "{", "return", "v", "-", "a", "<", "b", "-", "a", "\n", "}" ]
// InRange checks if v is in the range [a,b), i.e., a <= v < b.
[ "InRange", "checks", "if", "v", "is", "in", "the", "range", "[", "a", "b", ")", "i", ".", "e", ".", "a", "<", "=", "v", "<", "b", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/seqnum/seqnum.go#L39-L41
train
google/netstack
tcpip/seqnum/seqnum.go
InWindow
func (v Value) InWindow(first Value, size Size) bool { return v.InRange(first, first.Add(size)) }
go
func (v Value) InWindow(first Value, size Size) bool { return v.InRange(first, first.Add(size)) }
[ "func", "(", "v", "Value", ")", "InWindow", "(", "first", "Value", ",", "size", "Size", ")", "bool", "{", "return", "v", ".", "InRange", "(", "first", ",", "first", ".", "Add", "(", "size", ")", ")", "\n", "}" ]
// InWindow checks if v is in the window that starts at 'first' and spans 'size' // sequence numbers.
[ "InWindow", "checks", "if", "v", "is", "in", "the", "window", "that", "starts", "at", "first", "and", "spans", "size", "sequence", "numbers", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/seqnum/seqnum.go#L45-L47
train
google/netstack
tcpip/seqnum/seqnum.go
Overlap
func Overlap(a Value, b Size, x Value, y Size) bool { return a.LessThan(x.Add(y)) && x.LessThan(a.Add(b)) }
go
func Overlap(a Value, b Size, x Value, y Size) bool { return a.LessThan(x.Add(y)) && x.LessThan(a.Add(b)) }
[ "func", "Overlap", "(", "a", "Value", ",", "b", "Size", ",", "x", "Value", ",", "y", "Size", ")", "bool", "{", "return", "a", ".", "LessThan", "(", "x", ".", "Add", "(", "y", ")", ")", "&&", "x", ".", "LessThan", "(", "a", ".", "Add", "(", "b", ")", ")", "\n", "}" ]
// Overlap checks if the window [a,a+b) overlaps with the window [x, x+y).
[ "Overlap", "checks", "if", "the", "window", "[", "a", "a", "+", "b", ")", "overlaps", "with", "the", "window", "[", "x", "x", "+", "y", ")", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/seqnum/seqnum.go#L50-L52
train
google/netstack
tcpip/transport/tcp/reno.go
updateSlowStart
func (r *renoState) updateSlowStart(packetsAcked int) int { // Don't let the congestion window cross into the congestion // avoidance range. newcwnd := r.s.sndCwnd + packetsAcked if newcwnd >= r.s.sndSsthresh { newcwnd = r.s.sndSsthresh r.s.sndCAAckCount = 0 } packetsAcked -= newcwnd - r.s.sndCwnd r.s.sndCwnd = newcwnd return packetsAcked }
go
func (r *renoState) updateSlowStart(packetsAcked int) int { // Don't let the congestion window cross into the congestion // avoidance range. newcwnd := r.s.sndCwnd + packetsAcked if newcwnd >= r.s.sndSsthresh { newcwnd = r.s.sndSsthresh r.s.sndCAAckCount = 0 } packetsAcked -= newcwnd - r.s.sndCwnd r.s.sndCwnd = newcwnd return packetsAcked }
[ "func", "(", "r", "*", "renoState", ")", "updateSlowStart", "(", "packetsAcked", "int", ")", "int", "{", "newcwnd", ":=", "r", ".", "s", ".", "sndCwnd", "+", "packetsAcked", "\n", "if", "newcwnd", ">=", "r", ".", "s", ".", "sndSsthresh", "{", "newcwnd", "=", "r", ".", "s", ".", "sndSsthresh", "\n", "r", ".", "s", ".", "sndCAAckCount", "=", "0", "\n", "}", "\n", "packetsAcked", "-=", "newcwnd", "-", "r", ".", "s", ".", "sndCwnd", "\n", "r", ".", "s", ".", "sndCwnd", "=", "newcwnd", "\n", "return", "packetsAcked", "\n", "}" ]
// updateSlowStart will update the congestion window as per the slow-start // algorithm used by NewReno. If after adjusting the congestion window // we cross the SSthreshold then it will return the number of packets that // must be consumed in congestion avoidance mode.
[ "updateSlowStart", "will", "update", "the", "congestion", "window", "as", "per", "the", "slow", "-", "start", "algorithm", "used", "by", "NewReno", ".", "If", "after", "adjusting", "the", "congestion", "window", "we", "cross", "the", "SSthreshold", "then", "it", "will", "return", "the", "number", "of", "packets", "that", "must", "be", "consumed", "in", "congestion", "avoidance", "mode", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/reno.go#L34-L46
train
google/netstack
tcpip/transport/tcp/reno.go
updateCongestionAvoidance
func (r *renoState) updateCongestionAvoidance(packetsAcked int) { // Consume the packets in congestion avoidance mode. r.s.sndCAAckCount += packetsAcked if r.s.sndCAAckCount >= r.s.sndCwnd { r.s.sndCwnd += r.s.sndCAAckCount / r.s.sndCwnd r.s.sndCAAckCount = r.s.sndCAAckCount % r.s.sndCwnd } }
go
func (r *renoState) updateCongestionAvoidance(packetsAcked int) { // Consume the packets in congestion avoidance mode. r.s.sndCAAckCount += packetsAcked if r.s.sndCAAckCount >= r.s.sndCwnd { r.s.sndCwnd += r.s.sndCAAckCount / r.s.sndCwnd r.s.sndCAAckCount = r.s.sndCAAckCount % r.s.sndCwnd } }
[ "func", "(", "r", "*", "renoState", ")", "updateCongestionAvoidance", "(", "packetsAcked", "int", ")", "{", "r", ".", "s", ".", "sndCAAckCount", "+=", "packetsAcked", "\n", "if", "r", ".", "s", ".", "sndCAAckCount", ">=", "r", ".", "s", ".", "sndCwnd", "{", "r", ".", "s", ".", "sndCwnd", "+=", "r", ".", "s", ".", "sndCAAckCount", "/", "r", ".", "s", ".", "sndCwnd", "\n", "r", ".", "s", ".", "sndCAAckCount", "=", "r", ".", "s", ".", "sndCAAckCount", "%", "r", ".", "s", ".", "sndCwnd", "\n", "}", "\n", "}" ]
// updateCongestionAvoidance will update congestion window in congestion // avoidance mode as described in RFC5681 section 3.1
[ "updateCongestionAvoidance", "will", "update", "congestion", "window", "in", "congestion", "avoidance", "mode", "as", "described", "in", "RFC5681", "section", "3", ".", "1" ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/reno.go#L50-L57
train
google/netstack
tcpip/transport/tcp/reno.go
reduceSlowStartThreshold
func (r *renoState) reduceSlowStartThreshold() { r.s.sndSsthresh = r.s.outstanding / 2 if r.s.sndSsthresh < 2 { r.s.sndSsthresh = 2 } }
go
func (r *renoState) reduceSlowStartThreshold() { r.s.sndSsthresh = r.s.outstanding / 2 if r.s.sndSsthresh < 2 { r.s.sndSsthresh = 2 } }
[ "func", "(", "r", "*", "renoState", ")", "reduceSlowStartThreshold", "(", ")", "{", "r", ".", "s", ".", "sndSsthresh", "=", "r", ".", "s", ".", "outstanding", "/", "2", "\n", "if", "r", ".", "s", ".", "sndSsthresh", "<", "2", "{", "r", ".", "s", ".", "sndSsthresh", "=", "2", "\n", "}", "\n", "}" ]
// reduceSlowStartThreshold reduces the slow-start threshold per RFC 5681, // page 6, eq. 4. It is called when we detect congestion in the network.
[ "reduceSlowStartThreshold", "reduces", "the", "slow", "-", "start", "threshold", "per", "RFC", "5681", "page", "6", "eq", ".", "4", ".", "It", "is", "called", "when", "we", "detect", "congestion", "in", "the", "network", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/reno.go#L61-L67
train
google/netstack
tcpip/transport/tcp/reno.go
Update
func (r *renoState) Update(packetsAcked int) { if r.s.sndCwnd < r.s.sndSsthresh { packetsAcked = r.updateSlowStart(packetsAcked) if packetsAcked == 0 { return } } r.updateCongestionAvoidance(packetsAcked) }
go
func (r *renoState) Update(packetsAcked int) { if r.s.sndCwnd < r.s.sndSsthresh { packetsAcked = r.updateSlowStart(packetsAcked) if packetsAcked == 0 { return } } r.updateCongestionAvoidance(packetsAcked) }
[ "func", "(", "r", "*", "renoState", ")", "Update", "(", "packetsAcked", "int", ")", "{", "if", "r", ".", "s", ".", "sndCwnd", "<", "r", ".", "s", ".", "sndSsthresh", "{", "packetsAcked", "=", "r", ".", "updateSlowStart", "(", "packetsAcked", ")", "\n", "if", "packetsAcked", "==", "0", "{", "return", "\n", "}", "\n", "}", "\n", "r", ".", "updateCongestionAvoidance", "(", "packetsAcked", ")", "\n", "}" ]
// Update updates the congestion state based on the number of packets that // were acknowledged. // Update implements congestionControl.Update.
[ "Update", "updates", "the", "congestion", "state", "based", "on", "the", "number", "of", "packets", "that", "were", "acknowledged", ".", "Update", "implements", "congestionControl", ".", "Update", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/reno.go#L72-L80
train
google/netstack
tcpip/hash/jenkins/jenkins.go
Sum32
func (s *Sum32) Sum32() uint32 { hash := *s hash += (hash << 3) hash ^= hash >> 11 hash += hash << 15 return uint32(hash) }
go
func (s *Sum32) Sum32() uint32 { hash := *s hash += (hash << 3) hash ^= hash >> 11 hash += hash << 15 return uint32(hash) }
[ "func", "(", "s", "*", "Sum32", ")", "Sum32", "(", ")", "uint32", "{", "hash", ":=", "*", "s", "\n", "hash", "+=", "(", "hash", "<<", "3", ")", "\n", "hash", "^=", "hash", ">>", "11", "\n", "hash", "+=", "hash", "<<", "15", "\n", "return", "uint32", "(", "hash", ")", "\n", "}" ]
// Sum32 returns the hash value
[ "Sum32", "returns", "the", "hash", "value" ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/hash/jenkins/jenkins.go#L44-L52
train
google/netstack
tcpip/hash/jenkins/jenkins.go
Write
func (s *Sum32) Write(data []byte) (int, error) { hash := *s for _, b := range data { hash += Sum32(b) hash += hash << 10 hash ^= hash >> 6 } *s = hash return len(data), nil }
go
func (s *Sum32) Write(data []byte) (int, error) { hash := *s for _, b := range data { hash += Sum32(b) hash += hash << 10 hash ^= hash >> 6 } *s = hash return len(data), nil }
[ "func", "(", "s", "*", "Sum32", ")", "Write", "(", "data", "[", "]", "byte", ")", "(", "int", ",", "error", ")", "{", "hash", ":=", "*", "s", "\n", "for", "_", ",", "b", ":=", "range", "data", "{", "hash", "+=", "Sum32", "(", "b", ")", "\n", "hash", "+=", "hash", "<<", "10", "\n", "hash", "^=", "hash", ">>", "6", "\n", "}", "\n", "*", "s", "=", "hash", "\n", "return", "len", "(", "data", ")", ",", "nil", "\n", "}" ]
// Write adds more data to the running hash. // // It never returns an error.
[ "Write", "adds", "more", "data", "to", "the", "running", "hash", ".", "It", "never", "returns", "an", "error", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/hash/jenkins/jenkins.go#L57-L66
train
google/netstack
tcpip/hash/jenkins/jenkins.go
Sum
func (s *Sum32) Sum(in []byte) []byte { v := s.Sum32() return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) }
go
func (s *Sum32) Sum(in []byte) []byte { v := s.Sum32() return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) }
[ "func", "(", "s", "*", "Sum32", ")", "Sum", "(", "in", "[", "]", "byte", ")", "[", "]", "byte", "{", "v", ":=", "s", ".", "Sum32", "(", ")", "\n", "return", "append", "(", "in", ",", "byte", "(", "v", ">>", "24", ")", ",", "byte", "(", "v", ">>", "16", ")", ",", "byte", "(", "v", ">>", "8", ")", ",", "byte", "(", "v", ")", ")", "\n", "}" ]
// Sum appends the current hash to in and returns the resulting slice. // // It does not change the underlying hash state.
[ "Sum", "appends", "the", "current", "hash", "to", "in", "and", "returns", "the", "resulting", "slice", ".", "It", "does", "not", "change", "the", "underlying", "hash", "state", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/hash/jenkins/jenkins.go#L77-L80
train
google/netstack
tcpip/sample/tun_tcp_connect/main.go
writer
func writer(ch chan struct{}, ep tcpip.Endpoint) { defer func() { ep.Shutdown(tcpip.ShutdownWrite) close(ch) }() r := bufio.NewReader(os.Stdin) for { v := buffer.NewView(1024) n, err := r.Read(v) if err != nil { return } v.CapLength(n) for len(v) > 0 { n, _, err := ep.Write(tcpip.SlicePayload(v), tcpip.WriteOptions{}) if err != nil { fmt.Println("Write failed:", err) return } v.TrimFront(int(n)) } } }
go
func writer(ch chan struct{}, ep tcpip.Endpoint) { defer func() { ep.Shutdown(tcpip.ShutdownWrite) close(ch) }() r := bufio.NewReader(os.Stdin) for { v := buffer.NewView(1024) n, err := r.Read(v) if err != nil { return } v.CapLength(n) for len(v) > 0 { n, _, err := ep.Write(tcpip.SlicePayload(v), tcpip.WriteOptions{}) if err != nil { fmt.Println("Write failed:", err) return } v.TrimFront(int(n)) } } }
[ "func", "writer", "(", "ch", "chan", "struct", "{", "}", ",", "ep", "tcpip", ".", "Endpoint", ")", "{", "defer", "func", "(", ")", "{", "ep", ".", "Shutdown", "(", "tcpip", ".", "ShutdownWrite", ")", "\n", "close", "(", "ch", ")", "\n", "}", "(", ")", "\n", "r", ":=", "bufio", ".", "NewReader", "(", "os", ".", "Stdin", ")", "\n", "for", "{", "v", ":=", "buffer", ".", "NewView", "(", "1024", ")", "\n", "n", ",", "err", ":=", "r", ".", "Read", "(", "v", ")", "\n", "if", "err", "!=", "nil", "{", "return", "\n", "}", "\n", "v", ".", "CapLength", "(", "n", ")", "\n", "for", "len", "(", "v", ")", ">", "0", "{", "n", ",", "_", ",", "err", ":=", "ep", ".", "Write", "(", "tcpip", ".", "SlicePayload", "(", "v", ")", ",", "tcpip", ".", "WriteOptions", "{", "}", ")", "\n", "if", "err", "!=", "nil", "{", "fmt", ".", "Println", "(", "\"Write failed:\"", ",", "err", ")", "\n", "return", "\n", "}", "\n", "v", ".", "TrimFront", "(", "int", "(", "n", ")", ")", "\n", "}", "\n", "}", "\n", "}" ]
// writer reads from standard input and writes to the endpoint until standard // input is closed. It signals that it's done by closing the provided channel.
[ "writer", "reads", "from", "standard", "input", "and", "writes", "to", "the", "endpoint", "until", "standard", "input", "is", "closed", ".", "It", "signals", "that", "it", "s", "done", "by", "closing", "the", "provided", "channel", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/sample/tun_tcp_connect/main.go#L67-L92
train
google/netstack
sleep/commit_noasm.go
commitSleep
func commitSleep(g uintptr, waitingG *uintptr) bool { for { // Check if the wait was aborted. if atomic.LoadUintptr(waitingG) == 0 { return false } // Try to store the G so that wakers know who to wake. if atomic.CompareAndSwapUintptr(waitingG, preparingG, g) { return true } } }
go
func commitSleep(g uintptr, waitingG *uintptr) bool { for { // Check if the wait was aborted. if atomic.LoadUintptr(waitingG) == 0 { return false } // Try to store the G so that wakers know who to wake. if atomic.CompareAndSwapUintptr(waitingG, preparingG, g) { return true } } }
[ "func", "commitSleep", "(", "g", "uintptr", ",", "waitingG", "*", "uintptr", ")", "bool", "{", "for", "{", "if", "atomic", ".", "LoadUintptr", "(", "waitingG", ")", "==", "0", "{", "return", "false", "\n", "}", "\n", "if", "atomic", ".", "CompareAndSwapUintptr", "(", "waitingG", ",", "preparingG", ",", "g", ")", "{", "return", "true", "\n", "}", "\n", "}", "\n", "}" ]
// commitSleep signals to wakers that the given g is now sleeping. Wakers can // then fetch it and wake it. // // The commit may fail if wakers have been asserted after our last check, in // which case they will have set s.waitingG to zero. // // It is written in assembly because it is called from g0, so it doesn't have // a race context.
[ "commitSleep", "signals", "to", "wakers", "that", "the", "given", "g", "is", "now", "sleeping", ".", "Wakers", "can", "then", "fetch", "it", "and", "wake", "it", ".", "The", "commit", "may", "fail", "if", "wakers", "have", "been", "asserted", "after", "our", "last", "check", "in", "which", "case", "they", "will", "have", "set", "s", ".", "waitingG", "to", "zero", ".", "It", "is", "written", "in", "assembly", "because", "it", "is", "called", "from", "g0", "so", "it", "doesn", "t", "have", "a", "race", "context", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/sleep/commit_noasm.go#L30-L42
train
google/netstack
tcpip/stack/route.go
makeRoute
func makeRoute(netProto tcpip.NetworkProtocolNumber, localAddr, remoteAddr tcpip.Address, localLinkAddr tcpip.LinkAddress, ref *referencedNetworkEndpoint, handleLocal, multicastLoop bool) Route { loop := PacketOut if handleLocal && localAddr != "" && remoteAddr == localAddr { loop = PacketLoop } else if multicastLoop && (header.IsV4MulticastAddress(remoteAddr) || header.IsV6MulticastAddress(remoteAddr)) { loop |= PacketLoop } return Route{ NetProto: netProto, LocalAddress: localAddr, LocalLinkAddress: localLinkAddr, RemoteAddress: remoteAddr, ref: ref, loop: loop, } }
go
func makeRoute(netProto tcpip.NetworkProtocolNumber, localAddr, remoteAddr tcpip.Address, localLinkAddr tcpip.LinkAddress, ref *referencedNetworkEndpoint, handleLocal, multicastLoop bool) Route { loop := PacketOut if handleLocal && localAddr != "" && remoteAddr == localAddr { loop = PacketLoop } else if multicastLoop && (header.IsV4MulticastAddress(remoteAddr) || header.IsV6MulticastAddress(remoteAddr)) { loop |= PacketLoop } return Route{ NetProto: netProto, LocalAddress: localAddr, LocalLinkAddress: localLinkAddr, RemoteAddress: remoteAddr, ref: ref, loop: loop, } }
[ "func", "makeRoute", "(", "netProto", "tcpip", ".", "NetworkProtocolNumber", ",", "localAddr", ",", "remoteAddr", "tcpip", ".", "Address", ",", "localLinkAddr", "tcpip", ".", "LinkAddress", ",", "ref", "*", "referencedNetworkEndpoint", ",", "handleLocal", ",", "multicastLoop", "bool", ")", "Route", "{", "loop", ":=", "PacketOut", "\n", "if", "handleLocal", "&&", "localAddr", "!=", "\"\"", "&&", "remoteAddr", "==", "localAddr", "{", "loop", "=", "PacketLoop", "\n", "}", "else", "if", "multicastLoop", "&&", "(", "header", ".", "IsV4MulticastAddress", "(", "remoteAddr", ")", "||", "header", ".", "IsV6MulticastAddress", "(", "remoteAddr", ")", ")", "{", "loop", "|=", "PacketLoop", "\n", "}", "\n", "return", "Route", "{", "NetProto", ":", "netProto", ",", "LocalAddress", ":", "localAddr", ",", "LocalLinkAddress", ":", "localLinkAddr", ",", "RemoteAddress", ":", "remoteAddr", ",", "ref", ":", "ref", ",", "loop", ":", "loop", ",", "}", "\n", "}" ]
// makeRoute initializes a new route. It takes ownership of the provided // reference to a network endpoint.
[ "makeRoute", "initializes", "a", "new", "route", ".", "It", "takes", "ownership", "of", "the", "provided", "reference", "to", "a", "network", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/stack/route.go#L56-L72
train
google/netstack
tcpip/stack/route.go
Stats
func (r *Route) Stats() tcpip.Stats { return r.ref.nic.stack.Stats() }
go
func (r *Route) Stats() tcpip.Stats { return r.ref.nic.stack.Stats() }
[ "func", "(", "r", "*", "Route", ")", "Stats", "(", ")", "tcpip", ".", "Stats", "{", "return", "r", ".", "ref", ".", "nic", ".", "stack", ".", "Stats", "(", ")", "\n", "}" ]
// Stats returns a mutable copy of current stats.
[ "Stats", "returns", "a", "mutable", "copy", "of", "current", "stats", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/stack/route.go#L85-L87
train
google/netstack
tcpip/stack/route.go
PseudoHeaderChecksum
func (r *Route) PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, totalLen uint16) uint16 { return header.PseudoHeaderChecksum(protocol, r.LocalAddress, r.RemoteAddress, totalLen) }
go
func (r *Route) PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, totalLen uint16) uint16 { return header.PseudoHeaderChecksum(protocol, r.LocalAddress, r.RemoteAddress, totalLen) }
[ "func", "(", "r", "*", "Route", ")", "PseudoHeaderChecksum", "(", "protocol", "tcpip", ".", "TransportProtocolNumber", ",", "totalLen", "uint16", ")", "uint16", "{", "return", "header", ".", "PseudoHeaderChecksum", "(", "protocol", ",", "r", ".", "LocalAddress", ",", "r", ".", "RemoteAddress", ",", "totalLen", ")", "\n", "}" ]
// PseudoHeaderChecksum forwards the call to the network endpoint's // implementation.
[ "PseudoHeaderChecksum", "forwards", "the", "call", "to", "the", "network", "endpoint", "s", "implementation", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/stack/route.go#L91-L93
train
google/netstack
tcpip/stack/route.go
WritePacket
func (r *Route) WritePacket(gso *GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.TransportProtocolNumber, ttl uint8) *tcpip.Error { err := r.ref.ep.WritePacket(r, gso, hdr, payload, protocol, ttl, r.loop) if err != nil { r.Stats().IP.OutgoingPacketErrors.Increment() } else { r.ref.nic.stats.Tx.Packets.Increment() r.ref.nic.stats.Tx.Bytes.IncrementBy(uint64(hdr.UsedLength() + payload.Size())) } return err }
go
func (r *Route) WritePacket(gso *GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.TransportProtocolNumber, ttl uint8) *tcpip.Error { err := r.ref.ep.WritePacket(r, gso, hdr, payload, protocol, ttl, r.loop) if err != nil { r.Stats().IP.OutgoingPacketErrors.Increment() } else { r.ref.nic.stats.Tx.Packets.Increment() r.ref.nic.stats.Tx.Bytes.IncrementBy(uint64(hdr.UsedLength() + payload.Size())) } return err }
[ "func", "(", "r", "*", "Route", ")", "WritePacket", "(", "gso", "*", "GSO", ",", "hdr", "buffer", ".", "Prependable", ",", "payload", "buffer", ".", "VectorisedView", ",", "protocol", "tcpip", ".", "TransportProtocolNumber", ",", "ttl", "uint8", ")", "*", "tcpip", ".", "Error", "{", "err", ":=", "r", ".", "ref", ".", "ep", ".", "WritePacket", "(", "r", ",", "gso", ",", "hdr", ",", "payload", ",", "protocol", ",", "ttl", ",", "r", ".", "loop", ")", "\n", "if", "err", "!=", "nil", "{", "r", ".", "Stats", "(", ")", ".", "IP", ".", "OutgoingPacketErrors", ".", "Increment", "(", ")", "\n", "}", "else", "{", "r", ".", "ref", ".", "nic", ".", "stats", ".", "Tx", ".", "Packets", ".", "Increment", "(", ")", "\n", "r", ".", "ref", ".", "nic", ".", "stats", ".", "Tx", ".", "Bytes", ".", "IncrementBy", "(", "uint64", "(", "hdr", ".", "UsedLength", "(", ")", "+", "payload", ".", "Size", "(", ")", ")", ")", "\n", "}", "\n", "return", "err", "\n", "}" ]
// WritePacket writes the packet through the given route.
[ "WritePacket", "writes", "the", "packet", "through", "the", "given", "route", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/stack/route.go#L155-L164
train
google/netstack
tcpip/stack/route.go
Release
func (r *Route) Release() { if r.ref != nil { r.ref.decRef() r.ref = nil } }
go
func (r *Route) Release() { if r.ref != nil { r.ref.decRef() r.ref = nil } }
[ "func", "(", "r", "*", "Route", ")", "Release", "(", ")", "{", "if", "r", ".", "ref", "!=", "nil", "{", "r", ".", "ref", ".", "decRef", "(", ")", "\n", "r", ".", "ref", "=", "nil", "\n", "}", "\n", "}" ]
// Release frees all resources associated with the route.
[ "Release", "frees", "all", "resources", "associated", "with", "the", "route", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/stack/route.go#L177-L182
train
google/netstack
tcpip/header/udp.go
SetSourcePort
func (b UDP) SetSourcePort(port uint16) { binary.BigEndian.PutUint16(b[udpSrcPort:], port) }
go
func (b UDP) SetSourcePort(port uint16) { binary.BigEndian.PutUint16(b[udpSrcPort:], port) }
[ "func", "(", "b", "UDP", ")", "SetSourcePort", "(", "port", "uint16", ")", "{", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpSrcPort", ":", "]", ",", "port", ")", "\n", "}" ]
// SetSourcePort sets the "source port" field of the udp header.
[ "SetSourcePort", "sets", "the", "source", "port", "field", "of", "the", "udp", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/udp.go#L83-L85
train
google/netstack
tcpip/header/udp.go
SetDestinationPort
func (b UDP) SetDestinationPort(port uint16) { binary.BigEndian.PutUint16(b[udpDstPort:], port) }
go
func (b UDP) SetDestinationPort(port uint16) { binary.BigEndian.PutUint16(b[udpDstPort:], port) }
[ "func", "(", "b", "UDP", ")", "SetDestinationPort", "(", "port", "uint16", ")", "{", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpDstPort", ":", "]", ",", "port", ")", "\n", "}" ]
// SetDestinationPort sets the "destination port" field of the udp header.
[ "SetDestinationPort", "sets", "the", "destination", "port", "field", "of", "the", "udp", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/udp.go#L88-L90
train
google/netstack
tcpip/header/udp.go
SetChecksum
func (b UDP) SetChecksum(checksum uint16) { binary.BigEndian.PutUint16(b[udpChecksum:], checksum) }
go
func (b UDP) SetChecksum(checksum uint16) { binary.BigEndian.PutUint16(b[udpChecksum:], checksum) }
[ "func", "(", "b", "UDP", ")", "SetChecksum", "(", "checksum", "uint16", ")", "{", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpChecksum", ":", "]", ",", "checksum", ")", "\n", "}" ]
// SetChecksum sets the "checksum" field of the udp header.
[ "SetChecksum", "sets", "the", "checksum", "field", "of", "the", "udp", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/udp.go#L93-L95
train
google/netstack
tcpip/header/udp.go
CalculateChecksum
func (b UDP) CalculateChecksum(partialChecksum uint16) uint16 { // Calculate the rest of the checksum. return Checksum(b[:UDPMinimumSize], partialChecksum) }
go
func (b UDP) CalculateChecksum(partialChecksum uint16) uint16 { // Calculate the rest of the checksum. return Checksum(b[:UDPMinimumSize], partialChecksum) }
[ "func", "(", "b", "UDP", ")", "CalculateChecksum", "(", "partialChecksum", "uint16", ")", "uint16", "{", "return", "Checksum", "(", "b", "[", ":", "UDPMinimumSize", "]", ",", "partialChecksum", ")", "\n", "}" ]
// CalculateChecksum calculates the checksum of the udp packet, given the // checksum of the network-layer pseudo-header and the checksum of the payload.
[ "CalculateChecksum", "calculates", "the", "checksum", "of", "the", "udp", "packet", "given", "the", "checksum", "of", "the", "network", "-", "layer", "pseudo", "-", "header", "and", "the", "checksum", "of", "the", "payload", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/udp.go#L99-L102
train
google/netstack
tcpip/header/udp.go
Encode
func (b UDP) Encode(u *UDPFields) { binary.BigEndian.PutUint16(b[udpSrcPort:], u.SrcPort) binary.BigEndian.PutUint16(b[udpDstPort:], u.DstPort) binary.BigEndian.PutUint16(b[udpLength:], u.Length) binary.BigEndian.PutUint16(b[udpChecksum:], u.Checksum) }
go
func (b UDP) Encode(u *UDPFields) { binary.BigEndian.PutUint16(b[udpSrcPort:], u.SrcPort) binary.BigEndian.PutUint16(b[udpDstPort:], u.DstPort) binary.BigEndian.PutUint16(b[udpLength:], u.Length) binary.BigEndian.PutUint16(b[udpChecksum:], u.Checksum) }
[ "func", "(", "b", "UDP", ")", "Encode", "(", "u", "*", "UDPFields", ")", "{", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpSrcPort", ":", "]", ",", "u", ".", "SrcPort", ")", "\n", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpDstPort", ":", "]", ",", "u", ".", "DstPort", ")", "\n", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpLength", ":", "]", ",", "u", ".", "Length", ")", "\n", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "udpChecksum", ":", "]", ",", "u", ".", "Checksum", ")", "\n", "}" ]
// Encode encodes all the fields of the udp header.
[ "Encode", "encodes", "all", "the", "fields", "of", "the", "udp", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/udp.go#L105-L110
train
google/netstack
tcpip/link/sharedmem/sharedmem.go
New
func New(mtu, bufferSize uint32, addr tcpip.LinkAddress, tx, rx QueueConfig) (tcpip.LinkEndpointID, error) { e := &endpoint{ mtu: mtu, bufferSize: bufferSize, addr: addr, } if err := e.tx.init(bufferSize, &tx); err != nil { return 0, err } if err := e.rx.init(bufferSize, &rx); err != nil { e.tx.cleanup() return 0, err } return stack.RegisterLinkEndpoint(e), nil }
go
func New(mtu, bufferSize uint32, addr tcpip.LinkAddress, tx, rx QueueConfig) (tcpip.LinkEndpointID, error) { e := &endpoint{ mtu: mtu, bufferSize: bufferSize, addr: addr, } if err := e.tx.init(bufferSize, &tx); err != nil { return 0, err } if err := e.rx.init(bufferSize, &rx); err != nil { e.tx.cleanup() return 0, err } return stack.RegisterLinkEndpoint(e), nil }
[ "func", "New", "(", "mtu", ",", "bufferSize", "uint32", ",", "addr", "tcpip", ".", "LinkAddress", ",", "tx", ",", "rx", "QueueConfig", ")", "(", "tcpip", ".", "LinkEndpointID", ",", "error", ")", "{", "e", ":=", "&", "endpoint", "{", "mtu", ":", "mtu", ",", "bufferSize", ":", "bufferSize", ",", "addr", ":", "addr", ",", "}", "\n", "if", "err", ":=", "e", ".", "tx", ".", "init", "(", "bufferSize", ",", "&", "tx", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n", "if", "err", ":=", "e", ".", "rx", ".", "init", "(", "bufferSize", ",", "&", "rx", ")", ";", "err", "!=", "nil", "{", "e", ".", "tx", ".", "cleanup", "(", ")", "\n", "return", "0", ",", "err", "\n", "}", "\n", "return", "stack", ".", "RegisterLinkEndpoint", "(", "e", ")", ",", "nil", "\n", "}" ]
// New creates a new shared-memory-based endpoint. Buffers will be broken up // into buffers of "bufferSize" bytes.
[ "New", "creates", "a", "new", "shared", "-", "memory", "-", "based", "endpoint", ".", "Buffers", "will", "be", "broken", "up", "into", "buffers", "of", "bufferSize", "bytes", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/sharedmem.go#L97-L114
train
google/netstack
tcpip/link/sharedmem/sharedmem.go
Close
func (e *endpoint) Close() { // Tell dispatch goroutine to stop, then write to the eventfd so that // it wakes up in case it's sleeping. atomic.StoreUint32(&e.stopRequested, 1) syscall.Write(e.rx.eventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Cleanup the queues inline if the worker hasn't started yet; we also // know it won't start from now on because stopRequested is set to 1. e.mu.Lock() workerPresent := e.workerStarted e.mu.Unlock() if !workerPresent { e.tx.cleanup() e.rx.cleanup() } }
go
func (e *endpoint) Close() { // Tell dispatch goroutine to stop, then write to the eventfd so that // it wakes up in case it's sleeping. atomic.StoreUint32(&e.stopRequested, 1) syscall.Write(e.rx.eventFD, []byte{1, 0, 0, 0, 0, 0, 0, 0}) // Cleanup the queues inline if the worker hasn't started yet; we also // know it won't start from now on because stopRequested is set to 1. e.mu.Lock() workerPresent := e.workerStarted e.mu.Unlock() if !workerPresent { e.tx.cleanup() e.rx.cleanup() } }
[ "func", "(", "e", "*", "endpoint", ")", "Close", "(", ")", "{", "atomic", ".", "StoreUint32", "(", "&", "e", ".", "stopRequested", ",", "1", ")", "\n", "syscall", ".", "Write", "(", "e", ".", "rx", ".", "eventFD", ",", "[", "]", "byte", "{", "1", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", ",", "0", "}", ")", "\n", "e", ".", "mu", ".", "Lock", "(", ")", "\n", "workerPresent", ":=", "e", ".", "workerStarted", "\n", "e", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "!", "workerPresent", "{", "e", ".", "tx", ".", "cleanup", "(", ")", "\n", "e", ".", "rx", ".", "cleanup", "(", ")", "\n", "}", "\n", "}" ]
// Close frees all resources associated with the endpoint.
[ "Close", "frees", "all", "resources", "associated", "with", "the", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/sharedmem.go#L117-L133
train
google/netstack
tcpip/link/sharedmem/sharedmem.go
Attach
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) { e.mu.Lock() if !e.workerStarted && atomic.LoadUint32(&e.stopRequested) == 0 { e.workerStarted = true e.completed.Add(1) // Link endpoints are not savable. When transportation endpoints // are saved, they stop sending outgoing packets and all // incoming packets are rejected. go e.dispatchLoop(dispatcher) } e.mu.Unlock() }
go
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) { e.mu.Lock() if !e.workerStarted && atomic.LoadUint32(&e.stopRequested) == 0 { e.workerStarted = true e.completed.Add(1) // Link endpoints are not savable. When transportation endpoints // are saved, they stop sending outgoing packets and all // incoming packets are rejected. go e.dispatchLoop(dispatcher) } e.mu.Unlock() }
[ "func", "(", "e", "*", "endpoint", ")", "Attach", "(", "dispatcher", "stack", ".", "NetworkDispatcher", ")", "{", "e", ".", "mu", ".", "Lock", "(", ")", "\n", "if", "!", "e", ".", "workerStarted", "&&", "atomic", ".", "LoadUint32", "(", "&", "e", ".", "stopRequested", ")", "==", "0", "{", "e", ".", "workerStarted", "=", "true", "\n", "e", ".", "completed", ".", "Add", "(", "1", ")", "\n", "go", "e", ".", "dispatchLoop", "(", "dispatcher", ")", "\n", "}", "\n", "e", ".", "mu", ".", "Unlock", "(", ")", "\n", "}" ]
// Attach implements stack.LinkEndpoint.Attach. It launches the goroutine that // reads packets from the rx queue.
[ "Attach", "implements", "stack", ".", "LinkEndpoint", ".", "Attach", ".", "It", "launches", "the", "goroutine", "that", "reads", "packets", "from", "the", "rx", "queue", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/sharedmem.go#L142-L153
train
google/netstack
tcpip/link/sharedmem/sharedmem.go
IsAttached
func (e *endpoint) IsAttached() bool { e.mu.Lock() defer e.mu.Unlock() return e.workerStarted }
go
func (e *endpoint) IsAttached() bool { e.mu.Lock() defer e.mu.Unlock() return e.workerStarted }
[ "func", "(", "e", "*", "endpoint", ")", "IsAttached", "(", ")", "bool", "{", "e", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "e", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "e", ".", "workerStarted", "\n", "}" ]
// IsAttached implements stack.LinkEndpoint.IsAttached.
[ "IsAttached", "implements", "stack", ".", "LinkEndpoint", ".", "IsAttached", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/sharedmem.go#L156-L160
train
google/netstack
tcpip/link/sharedmem/sharedmem.go
dispatchLoop
func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) { // Post initial set of buffers. limit := e.rx.q.PostedBuffersLimit() if l := uint64(len(e.rx.data)) / uint64(e.bufferSize); limit > l { limit = l } for i := uint64(0); i < limit; i++ { b := queue.RxBuffer{ Offset: i * uint64(e.bufferSize), Size: e.bufferSize, ID: i, } if !e.rx.q.PostBuffers([]queue.RxBuffer{b}) { log.Printf("Unable to post %v-th buffer", i) } } // Read in a loop until a stop is requested. var rxb []queue.RxBuffer for atomic.LoadUint32(&e.stopRequested) == 0 { var n uint32 rxb, n = e.rx.postAndReceive(rxb, &e.stopRequested) // Copy data from the shared area to its own buffer, then // prepare to repost the buffer. b := make([]byte, n) offset := uint32(0) for i := range rxb { copy(b[offset:], e.rx.data[rxb[i].Offset:][:rxb[i].Size]) offset += rxb[i].Size rxb[i].Size = e.bufferSize } if n < header.EthernetMinimumSize { continue } // Send packet up the stack. eth := header.Ethernet(b) d.DeliverNetworkPacket(e, eth.SourceAddress(), eth.DestinationAddress(), eth.Type(), buffer.View(b[header.EthernetMinimumSize:]).ToVectorisedView()) } // Clean state. e.tx.cleanup() e.rx.cleanup() e.completed.Done() }
go
func (e *endpoint) dispatchLoop(d stack.NetworkDispatcher) { // Post initial set of buffers. limit := e.rx.q.PostedBuffersLimit() if l := uint64(len(e.rx.data)) / uint64(e.bufferSize); limit > l { limit = l } for i := uint64(0); i < limit; i++ { b := queue.RxBuffer{ Offset: i * uint64(e.bufferSize), Size: e.bufferSize, ID: i, } if !e.rx.q.PostBuffers([]queue.RxBuffer{b}) { log.Printf("Unable to post %v-th buffer", i) } } // Read in a loop until a stop is requested. var rxb []queue.RxBuffer for atomic.LoadUint32(&e.stopRequested) == 0 { var n uint32 rxb, n = e.rx.postAndReceive(rxb, &e.stopRequested) // Copy data from the shared area to its own buffer, then // prepare to repost the buffer. b := make([]byte, n) offset := uint32(0) for i := range rxb { copy(b[offset:], e.rx.data[rxb[i].Offset:][:rxb[i].Size]) offset += rxb[i].Size rxb[i].Size = e.bufferSize } if n < header.EthernetMinimumSize { continue } // Send packet up the stack. eth := header.Ethernet(b) d.DeliverNetworkPacket(e, eth.SourceAddress(), eth.DestinationAddress(), eth.Type(), buffer.View(b[header.EthernetMinimumSize:]).ToVectorisedView()) } // Clean state. e.tx.cleanup() e.rx.cleanup() e.completed.Done() }
[ "func", "(", "e", "*", "endpoint", ")", "dispatchLoop", "(", "d", "stack", ".", "NetworkDispatcher", ")", "{", "limit", ":=", "e", ".", "rx", ".", "q", ".", "PostedBuffersLimit", "(", ")", "\n", "if", "l", ":=", "uint64", "(", "len", "(", "e", ".", "rx", ".", "data", ")", ")", "/", "uint64", "(", "e", ".", "bufferSize", ")", ";", "limit", ">", "l", "{", "limit", "=", "l", "\n", "}", "\n", "for", "i", ":=", "uint64", "(", "0", ")", ";", "i", "<", "limit", ";", "i", "++", "{", "b", ":=", "queue", ".", "RxBuffer", "{", "Offset", ":", "i", "*", "uint64", "(", "e", ".", "bufferSize", ")", ",", "Size", ":", "e", ".", "bufferSize", ",", "ID", ":", "i", ",", "}", "\n", "if", "!", "e", ".", "rx", ".", "q", ".", "PostBuffers", "(", "[", "]", "queue", ".", "RxBuffer", "{", "b", "}", ")", "{", "log", ".", "Printf", "(", "\"Unable to post %v-th buffer\"", ",", "i", ")", "\n", "}", "\n", "}", "\n", "var", "rxb", "[", "]", "queue", ".", "RxBuffer", "\n", "for", "atomic", ".", "LoadUint32", "(", "&", "e", ".", "stopRequested", ")", "==", "0", "{", "var", "n", "uint32", "\n", "rxb", ",", "n", "=", "e", ".", "rx", ".", "postAndReceive", "(", "rxb", ",", "&", "e", ".", "stopRequested", ")", "\n", "b", ":=", "make", "(", "[", "]", "byte", ",", "n", ")", "\n", "offset", ":=", "uint32", "(", "0", ")", "\n", "for", "i", ":=", "range", "rxb", "{", "copy", "(", "b", "[", "offset", ":", "]", ",", "e", ".", "rx", ".", "data", "[", "rxb", "[", "i", "]", ".", "Offset", ":", "]", "[", ":", "rxb", "[", "i", "]", ".", "Size", "]", ")", "\n", "offset", "+=", "rxb", "[", "i", "]", ".", "Size", "\n", "rxb", "[", "i", "]", ".", "Size", "=", "e", ".", "bufferSize", "\n", "}", "\n", "if", "n", "<", "header", ".", "EthernetMinimumSize", "{", "continue", "\n", "}", "\n", "eth", ":=", "header", ".", "Ethernet", "(", "b", ")", "\n", "d", ".", "DeliverNetworkPacket", "(", "e", ",", "eth", ".", "SourceAddress", "(", ")", ",", "eth", ".", "DestinationAddress", "(", ")", ",", "eth", ".", "Type", "(", ")", ",", "buffer", ".", "View", "(", "b", "[", "header", ".", "EthernetMinimumSize", ":", "]", ")", ".", "ToVectorisedView", "(", ")", ")", "\n", "}", "\n", "e", ".", "tx", ".", "cleanup", "(", ")", "\n", "e", ".", "rx", ".", "cleanup", "(", ")", "\n", "e", ".", "completed", ".", "Done", "(", ")", "\n", "}" ]
// dispatchLoop reads packets from the rx queue in a loop and dispatches them // to the network stack.
[ "dispatchLoop", "reads", "packets", "from", "the", "rx", "queue", "in", "a", "loop", "and", "dispatches", "them", "to", "the", "network", "stack", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/sharedmem.go#L216-L264
train
google/netstack
tcpip/transport/raw/raw.go
Close
func (ep *endpoint) Close() { ep.mu.Lock() defer ep.mu.Unlock() if ep.closed { return } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) ep.rcvMu.Lock() defer ep.rcvMu.Unlock() // Clear the receive list. ep.rcvClosed = true ep.rcvBufSize = 0 for !ep.rcvList.Empty() { ep.rcvList.Remove(ep.rcvList.Front()) } if ep.connected { ep.route.Release() } ep.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.EventIn | waiter.EventOut) }
go
func (ep *endpoint) Close() { ep.mu.Lock() defer ep.mu.Unlock() if ep.closed { return } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) ep.rcvMu.Lock() defer ep.rcvMu.Unlock() // Clear the receive list. ep.rcvClosed = true ep.rcvBufSize = 0 for !ep.rcvList.Empty() { ep.rcvList.Remove(ep.rcvList.Front()) } if ep.connected { ep.route.Release() } ep.waiterQueue.Notify(waiter.EventHUp | waiter.EventErr | waiter.EventIn | waiter.EventOut) }
[ "func", "(", "ep", "*", "endpoint", ")", "Close", "(", ")", "{", "ep", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "ep", ".", "closed", "{", "return", "\n", "}", "\n", "ep", ".", "stack", ".", "UnregisterRawTransportEndpoint", "(", "ep", ".", "registeredNIC", ",", "ep", ".", "netProto", ",", "ep", ".", "transProto", ",", "ep", ")", "\n", "ep", ".", "rcvMu", ".", "Lock", "(", ")", "\n", "defer", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "ep", ".", "rcvClosed", "=", "true", "\n", "ep", ".", "rcvBufSize", "=", "0", "\n", "for", "!", "ep", ".", "rcvList", ".", "Empty", "(", ")", "{", "ep", ".", "rcvList", ".", "Remove", "(", "ep", ".", "rcvList", ".", "Front", "(", ")", ")", "\n", "}", "\n", "if", "ep", ".", "connected", "{", "ep", ".", "route", ".", "Release", "(", ")", "\n", "}", "\n", "ep", ".", "waiterQueue", ".", "Notify", "(", "waiter", ".", "EventHUp", "|", "waiter", ".", "EventErr", "|", "waiter", ".", "EventIn", "|", "waiter", ".", "EventOut", ")", "\n", "}" ]
// Close implements tcpip.Endpoint.Close.
[ "Close", "implements", "tcpip", ".", "Endpoint", ".", "Close", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L126-L151
train
google/netstack
tcpip/transport/raw/raw.go
Read
func (ep *endpoint) Read(addr *tcpip.FullAddress) (buffer.View, tcpip.ControlMessages, *tcpip.Error) { ep.rcvMu.Lock() // If there's no data to read, return that read would block or that the // endpoint is closed. if ep.rcvList.Empty() { err := tcpip.ErrWouldBlock if ep.rcvClosed { err = tcpip.ErrClosedForReceive } ep.rcvMu.Unlock() return buffer.View{}, tcpip.ControlMessages{}, err } packet := ep.rcvList.Front() ep.rcvList.Remove(packet) ep.rcvBufSize -= packet.data.Size() ep.rcvMu.Unlock() if addr != nil { *addr = packet.senderAddr } return packet.data.ToView(), tcpip.ControlMessages{HasTimestamp: true, Timestamp: packet.timestampNS}, nil }
go
func (ep *endpoint) Read(addr *tcpip.FullAddress) (buffer.View, tcpip.ControlMessages, *tcpip.Error) { ep.rcvMu.Lock() // If there's no data to read, return that read would block or that the // endpoint is closed. if ep.rcvList.Empty() { err := tcpip.ErrWouldBlock if ep.rcvClosed { err = tcpip.ErrClosedForReceive } ep.rcvMu.Unlock() return buffer.View{}, tcpip.ControlMessages{}, err } packet := ep.rcvList.Front() ep.rcvList.Remove(packet) ep.rcvBufSize -= packet.data.Size() ep.rcvMu.Unlock() if addr != nil { *addr = packet.senderAddr } return packet.data.ToView(), tcpip.ControlMessages{HasTimestamp: true, Timestamp: packet.timestampNS}, nil }
[ "func", "(", "ep", "*", "endpoint", ")", "Read", "(", "addr", "*", "tcpip", ".", "FullAddress", ")", "(", "buffer", ".", "View", ",", "tcpip", ".", "ControlMessages", ",", "*", "tcpip", ".", "Error", ")", "{", "ep", ".", "rcvMu", ".", "Lock", "(", ")", "\n", "if", "ep", ".", "rcvList", ".", "Empty", "(", ")", "{", "err", ":=", "tcpip", ".", "ErrWouldBlock", "\n", "if", "ep", ".", "rcvClosed", "{", "err", "=", "tcpip", ".", "ErrClosedForReceive", "\n", "}", "\n", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "return", "buffer", ".", "View", "{", "}", ",", "tcpip", ".", "ControlMessages", "{", "}", ",", "err", "\n", "}", "\n", "packet", ":=", "ep", ".", "rcvList", ".", "Front", "(", ")", "\n", "ep", ".", "rcvList", ".", "Remove", "(", "packet", ")", "\n", "ep", ".", "rcvBufSize", "-=", "packet", ".", "data", ".", "Size", "(", ")", "\n", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "if", "addr", "!=", "nil", "{", "*", "addr", "=", "packet", ".", "senderAddr", "\n", "}", "\n", "return", "packet", ".", "data", ".", "ToView", "(", ")", ",", "tcpip", ".", "ControlMessages", "{", "HasTimestamp", ":", "true", ",", "Timestamp", ":", "packet", ".", "timestampNS", "}", ",", "nil", "\n", "}" ]
// Read implements tcpip.Endpoint.Read.
[ "Read", "implements", "tcpip", ".", "Endpoint", ".", "Read", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L154-L179
train
google/netstack
tcpip/transport/raw/raw.go
Write
func (ep *endpoint) Write(payload tcpip.Payload, opts tcpip.WriteOptions) (uintptr, <-chan struct{}, *tcpip.Error) { // MSG_MORE is unimplemented. This also means that MSG_EOR is a no-op. if opts.More { return 0, nil, tcpip.ErrInvalidOptionValue } ep.mu.RLock() if ep.closed { ep.mu.RUnlock() return 0, nil, tcpip.ErrInvalidEndpointState } // Check whether we've shutdown writing. if ep.shutdownFlags&tcpip.ShutdownWrite != 0 { ep.mu.RUnlock() return 0, nil, tcpip.ErrClosedForSend } // Did the user caller provide a destination? If not, use the connected // destination. if opts.To == nil { // If the user doesn't specify a destination, they should have // connected to another address. if !ep.connected { ep.mu.RUnlock() return 0, nil, tcpip.ErrNotConnected } if ep.route.IsResolutionRequired() { savedRoute := &ep.route // Promote lock to exclusive if using a shared route, // given that it may need to change in finishWrite. ep.mu.RUnlock() ep.mu.Lock() // Make sure that the route didn't change during the // time we didn't hold the lock. if !ep.connected || savedRoute != &ep.route { ep.mu.Unlock() return 0, nil, tcpip.ErrInvalidEndpointState } n, ch, err := ep.finishWrite(payload, savedRoute) ep.mu.Unlock() return n, ch, err } n, ch, err := ep.finishWrite(payload, &ep.route) ep.mu.RUnlock() return n, ch, err } // The caller provided a destination. Reject destination address if it // goes through a different NIC than the endpoint was bound to. nic := opts.To.NIC if ep.bound && nic != 0 && nic != ep.boundNIC { ep.mu.RUnlock() return 0, nil, tcpip.ErrNoRoute } // We don't support IPv6 yet, so this has to be an IPv4 address. if len(opts.To.Addr) != header.IPv4AddressSize { ep.mu.RUnlock() return 0, nil, tcpip.ErrInvalidEndpointState } // Find the route to the destination. If boundAddress is 0, // FindRoute will choose an appropriate source address. route, err := ep.stack.FindRoute(nic, ep.boundAddr, opts.To.Addr, ep.netProto, false) if err != nil { ep.mu.RUnlock() return 0, nil, err } n, ch, err := ep.finishWrite(payload, &route) route.Release() ep.mu.RUnlock() return n, ch, err }
go
func (ep *endpoint) Write(payload tcpip.Payload, opts tcpip.WriteOptions) (uintptr, <-chan struct{}, *tcpip.Error) { // MSG_MORE is unimplemented. This also means that MSG_EOR is a no-op. if opts.More { return 0, nil, tcpip.ErrInvalidOptionValue } ep.mu.RLock() if ep.closed { ep.mu.RUnlock() return 0, nil, tcpip.ErrInvalidEndpointState } // Check whether we've shutdown writing. if ep.shutdownFlags&tcpip.ShutdownWrite != 0 { ep.mu.RUnlock() return 0, nil, tcpip.ErrClosedForSend } // Did the user caller provide a destination? If not, use the connected // destination. if opts.To == nil { // If the user doesn't specify a destination, they should have // connected to another address. if !ep.connected { ep.mu.RUnlock() return 0, nil, tcpip.ErrNotConnected } if ep.route.IsResolutionRequired() { savedRoute := &ep.route // Promote lock to exclusive if using a shared route, // given that it may need to change in finishWrite. ep.mu.RUnlock() ep.mu.Lock() // Make sure that the route didn't change during the // time we didn't hold the lock. if !ep.connected || savedRoute != &ep.route { ep.mu.Unlock() return 0, nil, tcpip.ErrInvalidEndpointState } n, ch, err := ep.finishWrite(payload, savedRoute) ep.mu.Unlock() return n, ch, err } n, ch, err := ep.finishWrite(payload, &ep.route) ep.mu.RUnlock() return n, ch, err } // The caller provided a destination. Reject destination address if it // goes through a different NIC than the endpoint was bound to. nic := opts.To.NIC if ep.bound && nic != 0 && nic != ep.boundNIC { ep.mu.RUnlock() return 0, nil, tcpip.ErrNoRoute } // We don't support IPv6 yet, so this has to be an IPv4 address. if len(opts.To.Addr) != header.IPv4AddressSize { ep.mu.RUnlock() return 0, nil, tcpip.ErrInvalidEndpointState } // Find the route to the destination. If boundAddress is 0, // FindRoute will choose an appropriate source address. route, err := ep.stack.FindRoute(nic, ep.boundAddr, opts.To.Addr, ep.netProto, false) if err != nil { ep.mu.RUnlock() return 0, nil, err } n, ch, err := ep.finishWrite(payload, &route) route.Release() ep.mu.RUnlock() return n, ch, err }
[ "func", "(", "ep", "*", "endpoint", ")", "Write", "(", "payload", "tcpip", ".", "Payload", ",", "opts", "tcpip", ".", "WriteOptions", ")", "(", "uintptr", ",", "<-", "chan", "struct", "{", "}", ",", "*", "tcpip", ".", "Error", ")", "{", "if", "opts", ".", "More", "{", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrInvalidOptionValue", "\n", "}", "\n", "ep", ".", "mu", ".", "RLock", "(", ")", "\n", "if", "ep", ".", "closed", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "if", "ep", ".", "shutdownFlags", "&", "tcpip", ".", "ShutdownWrite", "!=", "0", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrClosedForSend", "\n", "}", "\n", "if", "opts", ".", "To", "==", "nil", "{", "if", "!", "ep", ".", "connected", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrNotConnected", "\n", "}", "\n", "if", "ep", ".", "route", ".", "IsResolutionRequired", "(", ")", "{", "savedRoute", ":=", "&", "ep", ".", "route", "\n", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "ep", ".", "mu", ".", "Lock", "(", ")", "\n", "if", "!", "ep", ".", "connected", "||", "savedRoute", "!=", "&", "ep", ".", "route", "{", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "n", ",", "ch", ",", "err", ":=", "ep", ".", "finishWrite", "(", "payload", ",", "savedRoute", ")", "\n", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "n", ",", "ch", ",", "err", "\n", "}", "\n", "n", ",", "ch", ",", "err", ":=", "ep", ".", "finishWrite", "(", "payload", ",", "&", "ep", ".", "route", ")", "\n", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "n", ",", "ch", ",", "err", "\n", "}", "\n", "nic", ":=", "opts", ".", "To", ".", "NIC", "\n", "if", "ep", ".", "bound", "&&", "nic", "!=", "0", "&&", "nic", "!=", "ep", ".", "boundNIC", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrNoRoute", "\n", "}", "\n", "if", "len", "(", "opts", ".", "To", ".", "Addr", ")", "!=", "header", ".", "IPv4AddressSize", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "route", ",", "err", ":=", "ep", ".", "stack", ".", "FindRoute", "(", "nic", ",", "ep", ".", "boundAddr", ",", "opts", ".", "To", ".", "Addr", ",", "ep", ".", "netProto", ",", "false", ")", "\n", "if", "err", "!=", "nil", "{", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "n", ",", "ch", ",", "err", ":=", "ep", ".", "finishWrite", "(", "payload", ",", "&", "route", ")", "\n", "route", ".", "Release", "(", ")", "\n", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "return", "n", ",", "ch", ",", "err", "\n", "}" ]
// Write implements tcpip.Endpoint.Write.
[ "Write", "implements", "tcpip", ".", "Endpoint", ".", "Write", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L182-L261
train
google/netstack
tcpip/transport/raw/raw.go
finishWrite
func (ep *endpoint) finishWrite(payload tcpip.Payload, route *stack.Route) (uintptr, <-chan struct{}, *tcpip.Error) { // We may need to resolve the route (match a link layer address to the // network address). If that requires blocking (e.g. to use ARP), // return a channel on which the caller can wait. if route.IsResolutionRequired() { waker := &sleep.Waker{} if ch, err := route.Resolve(waker); err != nil { if err == tcpip.ErrWouldBlock { // Link address needs to be resolved. // Resolution was triggered the background. // Better luck next time. route.RemoveWaker(waker) return 0, ch, tcpip.ErrNoLinkAddress } return 0, nil, err } } payloadBytes, err := payload.Get(payload.Size()) if err != nil { return 0, nil, err } switch ep.netProto { case header.IPv4ProtocolNumber: hdr := buffer.NewPrependable(len(payloadBytes) + int(route.MaxHeaderLength())) if err := route.WritePacket(nil /* gso */, hdr, buffer.View(payloadBytes).ToVectorisedView(), header.ICMPv4ProtocolNumber, route.DefaultTTL()); err != nil { return 0, nil, err } default: return 0, nil, tcpip.ErrUnknownProtocol } return uintptr(len(payloadBytes)), nil, nil }
go
func (ep *endpoint) finishWrite(payload tcpip.Payload, route *stack.Route) (uintptr, <-chan struct{}, *tcpip.Error) { // We may need to resolve the route (match a link layer address to the // network address). If that requires blocking (e.g. to use ARP), // return a channel on which the caller can wait. if route.IsResolutionRequired() { waker := &sleep.Waker{} if ch, err := route.Resolve(waker); err != nil { if err == tcpip.ErrWouldBlock { // Link address needs to be resolved. // Resolution was triggered the background. // Better luck next time. route.RemoveWaker(waker) return 0, ch, tcpip.ErrNoLinkAddress } return 0, nil, err } } payloadBytes, err := payload.Get(payload.Size()) if err != nil { return 0, nil, err } switch ep.netProto { case header.IPv4ProtocolNumber: hdr := buffer.NewPrependable(len(payloadBytes) + int(route.MaxHeaderLength())) if err := route.WritePacket(nil /* gso */, hdr, buffer.View(payloadBytes).ToVectorisedView(), header.ICMPv4ProtocolNumber, route.DefaultTTL()); err != nil { return 0, nil, err } default: return 0, nil, tcpip.ErrUnknownProtocol } return uintptr(len(payloadBytes)), nil, nil }
[ "func", "(", "ep", "*", "endpoint", ")", "finishWrite", "(", "payload", "tcpip", ".", "Payload", ",", "route", "*", "stack", ".", "Route", ")", "(", "uintptr", ",", "<-", "chan", "struct", "{", "}", ",", "*", "tcpip", ".", "Error", ")", "{", "if", "route", ".", "IsResolutionRequired", "(", ")", "{", "waker", ":=", "&", "sleep", ".", "Waker", "{", "}", "\n", "if", "ch", ",", "err", ":=", "route", ".", "Resolve", "(", "waker", ")", ";", "err", "!=", "nil", "{", "if", "err", "==", "tcpip", ".", "ErrWouldBlock", "{", "route", ".", "RemoveWaker", "(", "waker", ")", "\n", "return", "0", ",", "ch", ",", "tcpip", ".", "ErrNoLinkAddress", "\n", "}", "\n", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "}", "\n", "payloadBytes", ",", "err", ":=", "payload", ".", "Get", "(", "payload", ".", "Size", "(", ")", ")", "\n", "if", "err", "!=", "nil", "{", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "switch", "ep", ".", "netProto", "{", "case", "header", ".", "IPv4ProtocolNumber", ":", "hdr", ":=", "buffer", ".", "NewPrependable", "(", "len", "(", "payloadBytes", ")", "+", "int", "(", "route", ".", "MaxHeaderLength", "(", ")", ")", ")", "\n", "if", "err", ":=", "route", ".", "WritePacket", "(", "nil", ",", "hdr", ",", "buffer", ".", "View", "(", "payloadBytes", ")", ".", "ToVectorisedView", "(", ")", ",", "header", ".", "ICMPv4ProtocolNumber", ",", "route", ".", "DefaultTTL", "(", ")", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "nil", ",", "err", "\n", "}", "\n", "default", ":", "return", "0", ",", "nil", ",", "tcpip", ".", "ErrUnknownProtocol", "\n", "}", "\n", "return", "uintptr", "(", "len", "(", "payloadBytes", ")", ")", ",", "nil", ",", "nil", "\n", "}" ]
// finishWrite writes the payload to a route. It resolves the route if // necessary. It's really just a helper to make defer unnecessary in Write.
[ "finishWrite", "writes", "the", "payload", "to", "a", "route", ".", "It", "resolves", "the", "route", "if", "necessary", ".", "It", "s", "really", "just", "a", "helper", "to", "make", "defer", "unnecessary", "in", "Write", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L265-L300
train
google/netstack
tcpip/transport/raw/raw.go
Connect
func (ep *endpoint) Connect(addr tcpip.FullAddress) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() if ep.closed { return tcpip.ErrInvalidEndpointState } // We don't support IPv6 yet. if len(addr.Addr) != header.IPv4AddressSize { return tcpip.ErrInvalidEndpointState } nic := addr.NIC if ep.bound { if ep.boundNIC == 0 { // If we're bound, but not to a specific NIC, the NIC // in addr will be used. Nothing to do here. } else if addr.NIC == 0 { // If we're bound to a specific NIC, but addr doesn't // specify a NIC, use the bound NIC. nic = ep.boundNIC } else if addr.NIC != ep.boundNIC { // We're bound and addr specifies a NIC. They must be // the same. return tcpip.ErrInvalidEndpointState } } // Find a route to the destination. route, err := ep.stack.FindRoute(nic, tcpip.Address(""), addr.Addr, ep.netProto, false) if err != nil { return err } defer route.Release() // Re-register the endpoint with the appropriate NIC. if err := ep.stack.RegisterRawTransportEndpoint(addr.NIC, ep.netProto, ep.transProto, ep); err != nil { return err } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) // Save the route and NIC we've connected via. ep.route = route.Clone() ep.registeredNIC = nic ep.connected = true return nil }
go
func (ep *endpoint) Connect(addr tcpip.FullAddress) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() if ep.closed { return tcpip.ErrInvalidEndpointState } // We don't support IPv6 yet. if len(addr.Addr) != header.IPv4AddressSize { return tcpip.ErrInvalidEndpointState } nic := addr.NIC if ep.bound { if ep.boundNIC == 0 { // If we're bound, but not to a specific NIC, the NIC // in addr will be used. Nothing to do here. } else if addr.NIC == 0 { // If we're bound to a specific NIC, but addr doesn't // specify a NIC, use the bound NIC. nic = ep.boundNIC } else if addr.NIC != ep.boundNIC { // We're bound and addr specifies a NIC. They must be // the same. return tcpip.ErrInvalidEndpointState } } // Find a route to the destination. route, err := ep.stack.FindRoute(nic, tcpip.Address(""), addr.Addr, ep.netProto, false) if err != nil { return err } defer route.Release() // Re-register the endpoint with the appropriate NIC. if err := ep.stack.RegisterRawTransportEndpoint(addr.NIC, ep.netProto, ep.transProto, ep); err != nil { return err } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) // Save the route and NIC we've connected via. ep.route = route.Clone() ep.registeredNIC = nic ep.connected = true return nil }
[ "func", "(", "ep", "*", "endpoint", ")", "Connect", "(", "addr", "tcpip", ".", "FullAddress", ")", "*", "tcpip", ".", "Error", "{", "ep", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "ep", ".", "closed", "{", "return", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "if", "len", "(", "addr", ".", "Addr", ")", "!=", "header", ".", "IPv4AddressSize", "{", "return", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "nic", ":=", "addr", ".", "NIC", "\n", "if", "ep", ".", "bound", "{", "if", "ep", ".", "boundNIC", "==", "0", "{", "}", "else", "if", "addr", ".", "NIC", "==", "0", "{", "nic", "=", "ep", ".", "boundNIC", "\n", "}", "else", "if", "addr", ".", "NIC", "!=", "ep", ".", "boundNIC", "{", "return", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "}", "\n", "route", ",", "err", ":=", "ep", ".", "stack", ".", "FindRoute", "(", "nic", ",", "tcpip", ".", "Address", "(", "\"\"", ")", ",", "addr", ".", "Addr", ",", "ep", ".", "netProto", ",", "false", ")", "\n", "if", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "defer", "route", ".", "Release", "(", ")", "\n", "if", "err", ":=", "ep", ".", "stack", ".", "RegisterRawTransportEndpoint", "(", "addr", ".", "NIC", ",", "ep", ".", "netProto", ",", "ep", ".", "transProto", ",", "ep", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "ep", ".", "stack", ".", "UnregisterRawTransportEndpoint", "(", "ep", ".", "registeredNIC", ",", "ep", ".", "netProto", ",", "ep", ".", "transProto", ",", "ep", ")", "\n", "ep", ".", "route", "=", "route", ".", "Clone", "(", ")", "\n", "ep", ".", "registeredNIC", "=", "nic", "\n", "ep", ".", "connected", "=", "true", "\n", "return", "nil", "\n", "}" ]
// Connect implements tcpip.Endpoint.Connect.
[ "Connect", "implements", "tcpip", ".", "Endpoint", ".", "Connect", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L308-L356
train
google/netstack
tcpip/transport/raw/raw.go
Shutdown
func (ep *endpoint) Shutdown(flags tcpip.ShutdownFlags) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() if !ep.connected { return tcpip.ErrNotConnected } ep.shutdownFlags |= flags if flags&tcpip.ShutdownRead != 0 { ep.rcvMu.Lock() wasClosed := ep.rcvClosed ep.rcvClosed = true ep.rcvMu.Unlock() if !wasClosed { ep.waiterQueue.Notify(waiter.EventIn) } } return nil }
go
func (ep *endpoint) Shutdown(flags tcpip.ShutdownFlags) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() if !ep.connected { return tcpip.ErrNotConnected } ep.shutdownFlags |= flags if flags&tcpip.ShutdownRead != 0 { ep.rcvMu.Lock() wasClosed := ep.rcvClosed ep.rcvClosed = true ep.rcvMu.Unlock() if !wasClosed { ep.waiterQueue.Notify(waiter.EventIn) } } return nil }
[ "func", "(", "ep", "*", "endpoint", ")", "Shutdown", "(", "flags", "tcpip", ".", "ShutdownFlags", ")", "*", "tcpip", ".", "Error", "{", "ep", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "!", "ep", ".", "connected", "{", "return", "tcpip", ".", "ErrNotConnected", "\n", "}", "\n", "ep", ".", "shutdownFlags", "|=", "flags", "\n", "if", "flags", "&", "tcpip", ".", "ShutdownRead", "!=", "0", "{", "ep", ".", "rcvMu", ".", "Lock", "(", ")", "\n", "wasClosed", ":=", "ep", ".", "rcvClosed", "\n", "ep", ".", "rcvClosed", "=", "true", "\n", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "if", "!", "wasClosed", "{", "ep", ".", "waiterQueue", ".", "Notify", "(", "waiter", ".", "EventIn", ")", "\n", "}", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// Shutdown implements tcpip.Endpoint.Shutdown.
[ "Shutdown", "implements", "tcpip", ".", "Endpoint", ".", "Shutdown", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L359-L381
train
google/netstack
tcpip/transport/raw/raw.go
Accept
func (ep *endpoint) Accept() (tcpip.Endpoint, *waiter.Queue, *tcpip.Error) { return nil, nil, tcpip.ErrNotSupported }
go
func (ep *endpoint) Accept() (tcpip.Endpoint, *waiter.Queue, *tcpip.Error) { return nil, nil, tcpip.ErrNotSupported }
[ "func", "(", "ep", "*", "endpoint", ")", "Accept", "(", ")", "(", "tcpip", ".", "Endpoint", ",", "*", "waiter", ".", "Queue", ",", "*", "tcpip", ".", "Error", ")", "{", "return", "nil", ",", "nil", ",", "tcpip", ".", "ErrNotSupported", "\n", "}" ]
// Accept implements tcpip.Endpoint.Accept.
[ "Accept", "implements", "tcpip", ".", "Endpoint", ".", "Accept", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L389-L391
train
google/netstack
tcpip/transport/raw/raw.go
Bind
func (ep *endpoint) Bind(addr tcpip.FullAddress) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() // Callers must provide an IPv4 address or no network address (for // binding to a NIC, but not an address). if len(addr.Addr) != 0 && len(addr.Addr) != 4 { return tcpip.ErrInvalidEndpointState } // If a local address was specified, verify that it's valid. if len(addr.Addr) == header.IPv4AddressSize && ep.stack.CheckLocalAddress(addr.NIC, ep.netProto, addr.Addr) == 0 { return tcpip.ErrBadLocalAddress } // Re-register the endpoint with the appropriate NIC. if err := ep.stack.RegisterRawTransportEndpoint(addr.NIC, ep.netProto, ep.transProto, ep); err != nil { return err } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) ep.registeredNIC = addr.NIC ep.boundNIC = addr.NIC ep.boundAddr = addr.Addr ep.bound = true return nil }
go
func (ep *endpoint) Bind(addr tcpip.FullAddress) *tcpip.Error { ep.mu.Lock() defer ep.mu.Unlock() // Callers must provide an IPv4 address or no network address (for // binding to a NIC, but not an address). if len(addr.Addr) != 0 && len(addr.Addr) != 4 { return tcpip.ErrInvalidEndpointState } // If a local address was specified, verify that it's valid. if len(addr.Addr) == header.IPv4AddressSize && ep.stack.CheckLocalAddress(addr.NIC, ep.netProto, addr.Addr) == 0 { return tcpip.ErrBadLocalAddress } // Re-register the endpoint with the appropriate NIC. if err := ep.stack.RegisterRawTransportEndpoint(addr.NIC, ep.netProto, ep.transProto, ep); err != nil { return err } ep.stack.UnregisterRawTransportEndpoint(ep.registeredNIC, ep.netProto, ep.transProto, ep) ep.registeredNIC = addr.NIC ep.boundNIC = addr.NIC ep.boundAddr = addr.Addr ep.bound = true return nil }
[ "func", "(", "ep", "*", "endpoint", ")", "Bind", "(", "addr", "tcpip", ".", "FullAddress", ")", "*", "tcpip", ".", "Error", "{", "ep", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "ep", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "len", "(", "addr", ".", "Addr", ")", "!=", "0", "&&", "len", "(", "addr", ".", "Addr", ")", "!=", "4", "{", "return", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "if", "len", "(", "addr", ".", "Addr", ")", "==", "header", ".", "IPv4AddressSize", "&&", "ep", ".", "stack", ".", "CheckLocalAddress", "(", "addr", ".", "NIC", ",", "ep", ".", "netProto", ",", "addr", ".", "Addr", ")", "==", "0", "{", "return", "tcpip", ".", "ErrBadLocalAddress", "\n", "}", "\n", "if", "err", ":=", "ep", ".", "stack", ".", "RegisterRawTransportEndpoint", "(", "addr", ".", "NIC", ",", "ep", ".", "netProto", ",", "ep", ".", "transProto", ",", "ep", ")", ";", "err", "!=", "nil", "{", "return", "err", "\n", "}", "\n", "ep", ".", "stack", ".", "UnregisterRawTransportEndpoint", "(", "ep", ".", "registeredNIC", ",", "ep", ".", "netProto", ",", "ep", ".", "transProto", ",", "ep", ")", "\n", "ep", ".", "registeredNIC", "=", "addr", ".", "NIC", "\n", "ep", ".", "boundNIC", "=", "addr", ".", "NIC", "\n", "ep", ".", "boundAddr", "=", "addr", ".", "Addr", "\n", "ep", ".", "bound", "=", "true", "\n", "return", "nil", "\n", "}" ]
// Bind implements tcpip.Endpoint.Bind.
[ "Bind", "implements", "tcpip", ".", "Endpoint", ".", "Bind", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L394-L421
train
google/netstack
tcpip/transport/raw/raw.go
GetLocalAddress
func (ep *endpoint) GetLocalAddress() (tcpip.FullAddress, *tcpip.Error) { return tcpip.FullAddress{}, tcpip.ErrNotSupported }
go
func (ep *endpoint) GetLocalAddress() (tcpip.FullAddress, *tcpip.Error) { return tcpip.FullAddress{}, tcpip.ErrNotSupported }
[ "func", "(", "ep", "*", "endpoint", ")", "GetLocalAddress", "(", ")", "(", "tcpip", ".", "FullAddress", ",", "*", "tcpip", ".", "Error", ")", "{", "return", "tcpip", ".", "FullAddress", "{", "}", ",", "tcpip", ".", "ErrNotSupported", "\n", "}" ]
// GetLocalAddress implements tcpip.Endpoint.GetLocalAddress.
[ "GetLocalAddress", "implements", "tcpip", ".", "Endpoint", ".", "GetLocalAddress", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L424-L426
train
google/netstack
tcpip/transport/raw/raw.go
GetRemoteAddress
func (ep *endpoint) GetRemoteAddress() (tcpip.FullAddress, *tcpip.Error) { ep.mu.RLock() defer ep.mu.RUnlock() if !ep.connected { return tcpip.FullAddress{}, tcpip.ErrNotConnected } return tcpip.FullAddress{ NIC: ep.registeredNIC, Addr: ep.route.RemoteAddress, }, nil }
go
func (ep *endpoint) GetRemoteAddress() (tcpip.FullAddress, *tcpip.Error) { ep.mu.RLock() defer ep.mu.RUnlock() if !ep.connected { return tcpip.FullAddress{}, tcpip.ErrNotConnected } return tcpip.FullAddress{ NIC: ep.registeredNIC, Addr: ep.route.RemoteAddress, }, nil }
[ "func", "(", "ep", "*", "endpoint", ")", "GetRemoteAddress", "(", ")", "(", "tcpip", ".", "FullAddress", ",", "*", "tcpip", ".", "Error", ")", "{", "ep", ".", "mu", ".", "RLock", "(", ")", "\n", "defer", "ep", ".", "mu", ".", "RUnlock", "(", ")", "\n", "if", "!", "ep", ".", "connected", "{", "return", "tcpip", ".", "FullAddress", "{", "}", ",", "tcpip", ".", "ErrNotConnected", "\n", "}", "\n", "return", "tcpip", ".", "FullAddress", "{", "NIC", ":", "ep", ".", "registeredNIC", ",", "Addr", ":", "ep", ".", "route", ".", "RemoteAddress", ",", "}", ",", "nil", "\n", "}" ]
// GetRemoteAddress implements tcpip.Endpoint.GetRemoteAddress.
[ "GetRemoteAddress", "implements", "tcpip", ".", "Endpoint", ".", "GetRemoteAddress", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L429-L441
train
google/netstack
tcpip/transport/raw/raw.go
HandlePacket
func (ep *endpoint) HandlePacket(route *stack.Route, netHeader buffer.View, vv buffer.VectorisedView) { ep.rcvMu.Lock() // Drop the packet if our buffer is currently full. if ep.rcvClosed || ep.rcvBufSize >= ep.rcvBufSizeMax { ep.stack.Stats().DroppedPackets.Increment() ep.rcvMu.Unlock() return } if ep.bound { // If bound to a NIC, only accept data for that NIC. if ep.boundNIC != 0 && ep.boundNIC != route.NICID() { ep.rcvMu.Unlock() return } // If bound to an address, only accept data for that address. if ep.boundAddr != "" && ep.boundAddr != route.RemoteAddress { ep.rcvMu.Unlock() return } } // If connected, only accept packets from the remote address we // connected to. if ep.connected && ep.route.RemoteAddress != route.RemoteAddress { ep.rcvMu.Unlock() return } wasEmpty := ep.rcvBufSize == 0 // Push new packet into receive list and increment the buffer size. packet := &packet{ senderAddr: tcpip.FullAddress{ NIC: route.NICID(), Addr: route.RemoteAddress, }, } combinedVV := netHeader.ToVectorisedView() combinedVV.Append(vv) packet.data = combinedVV.Clone(packet.views[:]) packet.timestampNS = ep.stack.NowNanoseconds() ep.rcvList.PushBack(packet) ep.rcvBufSize += packet.data.Size() ep.rcvMu.Unlock() // Notify waiters that there's data to be read. if wasEmpty { ep.waiterQueue.Notify(waiter.EventIn) } }
go
func (ep *endpoint) HandlePacket(route *stack.Route, netHeader buffer.View, vv buffer.VectorisedView) { ep.rcvMu.Lock() // Drop the packet if our buffer is currently full. if ep.rcvClosed || ep.rcvBufSize >= ep.rcvBufSizeMax { ep.stack.Stats().DroppedPackets.Increment() ep.rcvMu.Unlock() return } if ep.bound { // If bound to a NIC, only accept data for that NIC. if ep.boundNIC != 0 && ep.boundNIC != route.NICID() { ep.rcvMu.Unlock() return } // If bound to an address, only accept data for that address. if ep.boundAddr != "" && ep.boundAddr != route.RemoteAddress { ep.rcvMu.Unlock() return } } // If connected, only accept packets from the remote address we // connected to. if ep.connected && ep.route.RemoteAddress != route.RemoteAddress { ep.rcvMu.Unlock() return } wasEmpty := ep.rcvBufSize == 0 // Push new packet into receive list and increment the buffer size. packet := &packet{ senderAddr: tcpip.FullAddress{ NIC: route.NICID(), Addr: route.RemoteAddress, }, } combinedVV := netHeader.ToVectorisedView() combinedVV.Append(vv) packet.data = combinedVV.Clone(packet.views[:]) packet.timestampNS = ep.stack.NowNanoseconds() ep.rcvList.PushBack(packet) ep.rcvBufSize += packet.data.Size() ep.rcvMu.Unlock() // Notify waiters that there's data to be read. if wasEmpty { ep.waiterQueue.Notify(waiter.EventIn) } }
[ "func", "(", "ep", "*", "endpoint", ")", "HandlePacket", "(", "route", "*", "stack", ".", "Route", ",", "netHeader", "buffer", ".", "View", ",", "vv", "buffer", ".", "VectorisedView", ")", "{", "ep", ".", "rcvMu", ".", "Lock", "(", ")", "\n", "if", "ep", ".", "rcvClosed", "||", "ep", ".", "rcvBufSize", ">=", "ep", ".", "rcvBufSizeMax", "{", "ep", ".", "stack", ".", "Stats", "(", ")", ".", "DroppedPackets", ".", "Increment", "(", ")", "\n", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n", "if", "ep", ".", "bound", "{", "if", "ep", ".", "boundNIC", "!=", "0", "&&", "ep", ".", "boundNIC", "!=", "route", ".", "NICID", "(", ")", "{", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n", "if", "ep", ".", "boundAddr", "!=", "\"\"", "&&", "ep", ".", "boundAddr", "!=", "route", ".", "RemoteAddress", "{", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n", "}", "\n", "if", "ep", ".", "connected", "&&", "ep", ".", "route", ".", "RemoteAddress", "!=", "route", ".", "RemoteAddress", "{", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n", "wasEmpty", ":=", "ep", ".", "rcvBufSize", "==", "0", "\n", "packet", ":=", "&", "packet", "{", "senderAddr", ":", "tcpip", ".", "FullAddress", "{", "NIC", ":", "route", ".", "NICID", "(", ")", ",", "Addr", ":", "route", ".", "RemoteAddress", ",", "}", ",", "}", "\n", "combinedVV", ":=", "netHeader", ".", "ToVectorisedView", "(", ")", "\n", "combinedVV", ".", "Append", "(", "vv", ")", "\n", "packet", ".", "data", "=", "combinedVV", ".", "Clone", "(", "packet", ".", "views", "[", ":", "]", ")", "\n", "packet", ".", "timestampNS", "=", "ep", ".", "stack", ".", "NowNanoseconds", "(", ")", "\n", "ep", ".", "rcvList", ".", "PushBack", "(", "packet", ")", "\n", "ep", ".", "rcvBufSize", "+=", "packet", ".", "data", ".", "Size", "(", ")", "\n", "ep", ".", "rcvMu", ".", "Unlock", "(", ")", "\n", "if", "wasEmpty", "{", "ep", ".", "waiterQueue", ".", "Notify", "(", "waiter", ".", "EventIn", ")", "\n", "}", "\n", "}" ]
// HandlePacket implements stack.RawTransportEndpoint.HandlePacket.
[ "HandlePacket", "implements", "stack", ".", "RawTransportEndpoint", ".", "HandlePacket", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/raw/raw.go#L504-L558
train
google/netstack
tcpip/link/sniffer/sniffer.go
New
func New(lower tcpip.LinkEndpointID) tcpip.LinkEndpointID { return stack.RegisterLinkEndpoint(&endpoint{ lower: stack.FindLinkEndpoint(lower), }) }
go
func New(lower tcpip.LinkEndpointID) tcpip.LinkEndpointID { return stack.RegisterLinkEndpoint(&endpoint{ lower: stack.FindLinkEndpoint(lower), }) }
[ "func", "New", "(", "lower", "tcpip", ".", "LinkEndpointID", ")", "tcpip", ".", "LinkEndpointID", "{", "return", "stack", ".", "RegisterLinkEndpoint", "(", "&", "endpoint", "{", "lower", ":", "stack", ".", "FindLinkEndpoint", "(", "lower", ")", ",", "}", ")", "\n", "}" ]
// New creates a new sniffer link-layer endpoint. It wraps around another // endpoint and logs packets and they traverse the endpoint.
[ "New", "creates", "a", "new", "sniffer", "link", "-", "layer", "endpoint", ".", "It", "wraps", "around", "another", "endpoint", "and", "logs", "packets", "and", "they", "traverse", "the", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sniffer/sniffer.go#L61-L65
train
google/netstack
tcpip/link/sniffer/sniffer.go
NewWithFile
func NewWithFile(lower tcpip.LinkEndpointID, file *os.File, snapLen uint32) (tcpip.LinkEndpointID, error) { if err := writePCAPHeader(file, snapLen); err != nil { return 0, err } return stack.RegisterLinkEndpoint(&endpoint{ lower: stack.FindLinkEndpoint(lower), file: file, maxPCAPLen: snapLen, }), nil }
go
func NewWithFile(lower tcpip.LinkEndpointID, file *os.File, snapLen uint32) (tcpip.LinkEndpointID, error) { if err := writePCAPHeader(file, snapLen); err != nil { return 0, err } return stack.RegisterLinkEndpoint(&endpoint{ lower: stack.FindLinkEndpoint(lower), file: file, maxPCAPLen: snapLen, }), nil }
[ "func", "NewWithFile", "(", "lower", "tcpip", ".", "LinkEndpointID", ",", "file", "*", "os", ".", "File", ",", "snapLen", "uint32", ")", "(", "tcpip", ".", "LinkEndpointID", ",", "error", ")", "{", "if", "err", ":=", "writePCAPHeader", "(", "file", ",", "snapLen", ")", ";", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n", "return", "stack", ".", "RegisterLinkEndpoint", "(", "&", "endpoint", "{", "lower", ":", "stack", ".", "FindLinkEndpoint", "(", "lower", ")", ",", "file", ":", "file", ",", "maxPCAPLen", ":", "snapLen", ",", "}", ")", ",", "nil", "\n", "}" ]
// NewWithFile creates a new sniffer link-layer endpoint. It wraps around // another endpoint and logs packets and they traverse the endpoint. // // Packets can be logged to file in the pcap format. A sniffer created // with this function will not emit packets using the standard log // package. // // snapLen is the maximum amount of a packet to be saved. Packets with a length // less than or equal too snapLen will be saved in their entirety. Longer // packets will be truncated to snapLen.
[ "NewWithFile", "creates", "a", "new", "sniffer", "link", "-", "layer", "endpoint", ".", "It", "wraps", "around", "another", "endpoint", "and", "logs", "packets", "and", "they", "traverse", "the", "endpoint", ".", "Packets", "can", "be", "logged", "to", "file", "in", "the", "pcap", "format", ".", "A", "sniffer", "created", "with", "this", "function", "will", "not", "emit", "packets", "using", "the", "standard", "log", "package", ".", "snapLen", "is", "the", "maximum", "amount", "of", "a", "packet", "to", "be", "saved", ".", "Packets", "with", "a", "length", "less", "than", "or", "equal", "too", "snapLen", "will", "be", "saved", "in", "their", "entirety", ".", "Longer", "packets", "will", "be", "truncated", "to", "snapLen", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sniffer/sniffer.go#L105-L114
train
google/netstack
tcpip/link/sniffer/sniffer.go
DeliverNetworkPacket
func (e *endpoint) DeliverNetworkPacket(linkEP stack.LinkEndpoint, remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) { if atomic.LoadUint32(&LogPackets) == 1 && e.file == nil { logPacket("recv", protocol, vv.First()) } if e.file != nil && atomic.LoadUint32(&LogPacketsToFile) == 1 { vs := vv.Views() length := vv.Size() if length > int(e.maxPCAPLen) { length = int(e.maxPCAPLen) } buf := bytes.NewBuffer(make([]byte, 0, pcapPacketHeaderLen+length)) if err := binary.Write(buf, binary.BigEndian, newPCAPPacketHeader(uint32(length), uint32(vv.Size()))); err != nil { panic(err) } for _, v := range vs { if length == 0 { break } if len(v) > length { v = v[:length] } if _, err := buf.Write([]byte(v)); err != nil { panic(err) } length -= len(v) } if _, err := e.file.Write(buf.Bytes()); err != nil { panic(err) } } e.dispatcher.DeliverNetworkPacket(e, remote, local, protocol, vv) }
go
func (e *endpoint) DeliverNetworkPacket(linkEP stack.LinkEndpoint, remote, local tcpip.LinkAddress, protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) { if atomic.LoadUint32(&LogPackets) == 1 && e.file == nil { logPacket("recv", protocol, vv.First()) } if e.file != nil && atomic.LoadUint32(&LogPacketsToFile) == 1 { vs := vv.Views() length := vv.Size() if length > int(e.maxPCAPLen) { length = int(e.maxPCAPLen) } buf := bytes.NewBuffer(make([]byte, 0, pcapPacketHeaderLen+length)) if err := binary.Write(buf, binary.BigEndian, newPCAPPacketHeader(uint32(length), uint32(vv.Size()))); err != nil { panic(err) } for _, v := range vs { if length == 0 { break } if len(v) > length { v = v[:length] } if _, err := buf.Write([]byte(v)); err != nil { panic(err) } length -= len(v) } if _, err := e.file.Write(buf.Bytes()); err != nil { panic(err) } } e.dispatcher.DeliverNetworkPacket(e, remote, local, protocol, vv) }
[ "func", "(", "e", "*", "endpoint", ")", "DeliverNetworkPacket", "(", "linkEP", "stack", ".", "LinkEndpoint", ",", "remote", ",", "local", "tcpip", ".", "LinkAddress", ",", "protocol", "tcpip", ".", "NetworkProtocolNumber", ",", "vv", "buffer", ".", "VectorisedView", ")", "{", "if", "atomic", ".", "LoadUint32", "(", "&", "LogPackets", ")", "==", "1", "&&", "e", ".", "file", "==", "nil", "{", "logPacket", "(", "\"recv\"", ",", "protocol", ",", "vv", ".", "First", "(", ")", ")", "\n", "}", "\n", "if", "e", ".", "file", "!=", "nil", "&&", "atomic", ".", "LoadUint32", "(", "&", "LogPacketsToFile", ")", "==", "1", "{", "vs", ":=", "vv", ".", "Views", "(", ")", "\n", "length", ":=", "vv", ".", "Size", "(", ")", "\n", "if", "length", ">", "int", "(", "e", ".", "maxPCAPLen", ")", "{", "length", "=", "int", "(", "e", ".", "maxPCAPLen", ")", "\n", "}", "\n", "buf", ":=", "bytes", ".", "NewBuffer", "(", "make", "(", "[", "]", "byte", ",", "0", ",", "pcapPacketHeaderLen", "+", "length", ")", ")", "\n", "if", "err", ":=", "binary", ".", "Write", "(", "buf", ",", "binary", ".", "BigEndian", ",", "newPCAPPacketHeader", "(", "uint32", "(", "length", ")", ",", "uint32", "(", "vv", ".", "Size", "(", ")", ")", ")", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "for", "_", ",", "v", ":=", "range", "vs", "{", "if", "length", "==", "0", "{", "break", "\n", "}", "\n", "if", "len", "(", "v", ")", ">", "length", "{", "v", "=", "v", "[", ":", "length", "]", "\n", "}", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "[", "]", "byte", "(", "v", ")", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "length", "-=", "len", "(", "v", ")", "\n", "}", "\n", "if", "_", ",", "err", ":=", "e", ".", "file", ".", "Write", "(", "buf", ".", "Bytes", "(", ")", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "}", "\n", "e", ".", "dispatcher", ".", "DeliverNetworkPacket", "(", "e", ",", "remote", ",", "local", ",", "protocol", ",", "vv", ")", "\n", "}" ]
// DeliverNetworkPacket implements the stack.NetworkDispatcher interface. It is // called by the link-layer endpoint being wrapped when a packet arrives, and // logs the packet before forwarding to the actual dispatcher.
[ "DeliverNetworkPacket", "implements", "the", "stack", ".", "NetworkDispatcher", "interface", ".", "It", "is", "called", "by", "the", "link", "-", "layer", "endpoint", "being", "wrapped", "when", "a", "packet", "arrives", "and", "logs", "the", "packet", "before", "forwarding", "to", "the", "actual", "dispatcher", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sniffer/sniffer.go#L119-L151
train
google/netstack
tcpip/link/sniffer/sniffer.go
Attach
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) { e.dispatcher = dispatcher e.lower.Attach(e) }
go
func (e *endpoint) Attach(dispatcher stack.NetworkDispatcher) { e.dispatcher = dispatcher e.lower.Attach(e) }
[ "func", "(", "e", "*", "endpoint", ")", "Attach", "(", "dispatcher", "stack", ".", "NetworkDispatcher", ")", "{", "e", ".", "dispatcher", "=", "dispatcher", "\n", "e", ".", "lower", ".", "Attach", "(", "e", ")", "\n", "}" ]
// Attach implements the stack.LinkEndpoint interface. It saves the dispatcher // and registers with the lower endpoint as its dispatcher so that "e" is called // for inbound packets.
[ "Attach", "implements", "the", "stack", ".", "LinkEndpoint", "interface", ".", "It", "saves", "the", "dispatcher", "and", "registers", "with", "the", "lower", "endpoint", "as", "its", "dispatcher", "so", "that", "e", "is", "called", "for", "inbound", "packets", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sniffer/sniffer.go#L156-L159
train
google/netstack
tcpip/link/sniffer/sniffer.go
WritePacket
func (e *endpoint) WritePacket(r *stack.Route, gso *stack.GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { if atomic.LoadUint32(&LogPackets) == 1 && e.file == nil { logPacket("send", protocol, hdr.View()) } if e.file != nil && atomic.LoadUint32(&LogPacketsToFile) == 1 { hdrBuf := hdr.View() length := len(hdrBuf) + payload.Size() if length > int(e.maxPCAPLen) { length = int(e.maxPCAPLen) } buf := bytes.NewBuffer(make([]byte, 0, pcapPacketHeaderLen+length)) if err := binary.Write(buf, binary.BigEndian, newPCAPPacketHeader(uint32(length), uint32(len(hdrBuf)+payload.Size()))); err != nil { panic(err) } if len(hdrBuf) > length { hdrBuf = hdrBuf[:length] } if _, err := buf.Write(hdrBuf); err != nil { panic(err) } length -= len(hdrBuf) if length > 0 { for _, v := range payload.Views() { if len(v) > length { v = v[:length] } n, err := buf.Write(v) if err != nil { panic(err) } length -= n if length == 0 { break } } } if _, err := e.file.Write(buf.Bytes()); err != nil { panic(err) } } return e.lower.WritePacket(r, gso, hdr, payload, protocol) }
go
func (e *endpoint) WritePacket(r *stack.Route, gso *stack.GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { if atomic.LoadUint32(&LogPackets) == 1 && e.file == nil { logPacket("send", protocol, hdr.View()) } if e.file != nil && atomic.LoadUint32(&LogPacketsToFile) == 1 { hdrBuf := hdr.View() length := len(hdrBuf) + payload.Size() if length > int(e.maxPCAPLen) { length = int(e.maxPCAPLen) } buf := bytes.NewBuffer(make([]byte, 0, pcapPacketHeaderLen+length)) if err := binary.Write(buf, binary.BigEndian, newPCAPPacketHeader(uint32(length), uint32(len(hdrBuf)+payload.Size()))); err != nil { panic(err) } if len(hdrBuf) > length { hdrBuf = hdrBuf[:length] } if _, err := buf.Write(hdrBuf); err != nil { panic(err) } length -= len(hdrBuf) if length > 0 { for _, v := range payload.Views() { if len(v) > length { v = v[:length] } n, err := buf.Write(v) if err != nil { panic(err) } length -= n if length == 0 { break } } } if _, err := e.file.Write(buf.Bytes()); err != nil { panic(err) } } return e.lower.WritePacket(r, gso, hdr, payload, protocol) }
[ "func", "(", "e", "*", "endpoint", ")", "WritePacket", "(", "r", "*", "stack", ".", "Route", ",", "gso", "*", "stack", ".", "GSO", ",", "hdr", "buffer", ".", "Prependable", ",", "payload", "buffer", ".", "VectorisedView", ",", "protocol", "tcpip", ".", "NetworkProtocolNumber", ")", "*", "tcpip", ".", "Error", "{", "if", "atomic", ".", "LoadUint32", "(", "&", "LogPackets", ")", "==", "1", "&&", "e", ".", "file", "==", "nil", "{", "logPacket", "(", "\"send\"", ",", "protocol", ",", "hdr", ".", "View", "(", ")", ")", "\n", "}", "\n", "if", "e", ".", "file", "!=", "nil", "&&", "atomic", ".", "LoadUint32", "(", "&", "LogPacketsToFile", ")", "==", "1", "{", "hdrBuf", ":=", "hdr", ".", "View", "(", ")", "\n", "length", ":=", "len", "(", "hdrBuf", ")", "+", "payload", ".", "Size", "(", ")", "\n", "if", "length", ">", "int", "(", "e", ".", "maxPCAPLen", ")", "{", "length", "=", "int", "(", "e", ".", "maxPCAPLen", ")", "\n", "}", "\n", "buf", ":=", "bytes", ".", "NewBuffer", "(", "make", "(", "[", "]", "byte", ",", "0", ",", "pcapPacketHeaderLen", "+", "length", ")", ")", "\n", "if", "err", ":=", "binary", ".", "Write", "(", "buf", ",", "binary", ".", "BigEndian", ",", "newPCAPPacketHeader", "(", "uint32", "(", "length", ")", ",", "uint32", "(", "len", "(", "hdrBuf", ")", "+", "payload", ".", "Size", "(", ")", ")", ")", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "if", "len", "(", "hdrBuf", ")", ">", "length", "{", "hdrBuf", "=", "hdrBuf", "[", ":", "length", "]", "\n", "}", "\n", "if", "_", ",", "err", ":=", "buf", ".", "Write", "(", "hdrBuf", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "length", "-=", "len", "(", "hdrBuf", ")", "\n", "if", "length", ">", "0", "{", "for", "_", ",", "v", ":=", "range", "payload", ".", "Views", "(", ")", "{", "if", "len", "(", "v", ")", ">", "length", "{", "v", "=", "v", "[", ":", "length", "]", "\n", "}", "\n", "n", ",", "err", ":=", "buf", ".", "Write", "(", "v", ")", "\n", "if", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "length", "-=", "n", "\n", "if", "length", "==", "0", "{", "break", "\n", "}", "\n", "}", "\n", "}", "\n", "if", "_", ",", "err", ":=", "e", ".", "file", ".", "Write", "(", "buf", ".", "Bytes", "(", ")", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "}", "\n", "return", "e", ".", "lower", ".", "WritePacket", "(", "r", ",", "gso", ",", "hdr", ",", "payload", ",", "protocol", ")", "\n", "}" ]
// WritePacket implements the stack.LinkEndpoint interface. It is called by // higher-level protocols to write packets; it just logs the packet and forwards // the request to the lower endpoint.
[ "WritePacket", "implements", "the", "stack", ".", "LinkEndpoint", "interface", ".", "It", "is", "called", "by", "higher", "-", "level", "protocols", "to", "write", "packets", ";", "it", "just", "logs", "the", "packet", "and", "forwards", "the", "request", "to", "the", "lower", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sniffer/sniffer.go#L199-L241
train
google/netstack
tcpip/transport/tcp/forwarder.go
NewForwarder
func NewForwarder(s *stack.Stack, rcvWnd, maxInFlight int, handler func(*ForwarderRequest)) *Forwarder { if rcvWnd == 0 { rcvWnd = DefaultBufferSize } return &Forwarder{ maxInFlight: maxInFlight, handler: handler, inFlight: make(map[stack.TransportEndpointID]struct{}), listen: newListenContext(s, seqnum.Size(rcvWnd), true, 0), } }
go
func NewForwarder(s *stack.Stack, rcvWnd, maxInFlight int, handler func(*ForwarderRequest)) *Forwarder { if rcvWnd == 0 { rcvWnd = DefaultBufferSize } return &Forwarder{ maxInFlight: maxInFlight, handler: handler, inFlight: make(map[stack.TransportEndpointID]struct{}), listen: newListenContext(s, seqnum.Size(rcvWnd), true, 0), } }
[ "func", "NewForwarder", "(", "s", "*", "stack", ".", "Stack", ",", "rcvWnd", ",", "maxInFlight", "int", ",", "handler", "func", "(", "*", "ForwarderRequest", ")", ")", "*", "Forwarder", "{", "if", "rcvWnd", "==", "0", "{", "rcvWnd", "=", "DefaultBufferSize", "\n", "}", "\n", "return", "&", "Forwarder", "{", "maxInFlight", ":", "maxInFlight", ",", "handler", ":", "handler", ",", "inFlight", ":", "make", "(", "map", "[", "stack", ".", "TransportEndpointID", "]", "struct", "{", "}", ")", ",", "listen", ":", "newListenContext", "(", "s", ",", "seqnum", ".", "Size", "(", "rcvWnd", ")", ",", "true", ",", "0", ")", ",", "}", "\n", "}" ]
// NewForwarder allocates and initializes a new forwarder with the given // maximum number of in-flight connection attempts. Once the maximum is reached // new incoming connection requests will be ignored. // // If rcvWnd is set to zero, the default buffer size is used instead.
[ "NewForwarder", "allocates", "and", "initializes", "a", "new", "forwarder", "with", "the", "given", "maximum", "number", "of", "in", "-", "flight", "connection", "attempts", ".", "Once", "the", "maximum", "is", "reached", "new", "incoming", "connection", "requests", "will", "be", "ignored", ".", "If", "rcvWnd", "is", "set", "to", "zero", "the", "default", "buffer", "size", "is", "used", "instead", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/forwarder.go#L48-L58
train
google/netstack
tcpip/transport/tcp/forwarder.go
Complete
func (r *ForwarderRequest) Complete(sendReset bool) { r.mu.Lock() defer r.mu.Unlock() if r.segment == nil { panic("Completing already completed forwarder request") } // Remove request from the forwarder. r.forwarder.mu.Lock() delete(r.forwarder.inFlight, r.segment.id) r.forwarder.mu.Unlock() // If the caller requested, send a reset. if sendReset { replyWithReset(r.segment) } // Release all resources. r.segment.decRef() r.segment = nil r.forwarder = nil }
go
func (r *ForwarderRequest) Complete(sendReset bool) { r.mu.Lock() defer r.mu.Unlock() if r.segment == nil { panic("Completing already completed forwarder request") } // Remove request from the forwarder. r.forwarder.mu.Lock() delete(r.forwarder.inFlight, r.segment.id) r.forwarder.mu.Unlock() // If the caller requested, send a reset. if sendReset { replyWithReset(r.segment) } // Release all resources. r.segment.decRef() r.segment = nil r.forwarder = nil }
[ "func", "(", "r", "*", "ForwarderRequest", ")", "Complete", "(", "sendReset", "bool", ")", "{", "r", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "r", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "r", ".", "segment", "==", "nil", "{", "panic", "(", "\"Completing already completed forwarder request\"", ")", "\n", "}", "\n", "r", ".", "forwarder", ".", "mu", ".", "Lock", "(", ")", "\n", "delete", "(", "r", ".", "forwarder", ".", "inFlight", ",", "r", ".", "segment", ".", "id", ")", "\n", "r", ".", "forwarder", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "sendReset", "{", "replyWithReset", "(", "r", ".", "segment", ")", "\n", "}", "\n", "r", ".", "segment", ".", "decRef", "(", ")", "\n", "r", ".", "segment", "=", "nil", "\n", "r", ".", "forwarder", "=", "nil", "\n", "}" ]
// Complete completes the request, and optionally sends a RST segment back to the // sender.
[ "Complete", "completes", "the", "request", "and", "optionally", "sends", "a", "RST", "segment", "back", "to", "the", "sender", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/forwarder.go#L120-L142
train
google/netstack
tcpip/transport/tcp/forwarder.go
CreateEndpoint
func (r *ForwarderRequest) CreateEndpoint(queue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { r.mu.Lock() defer r.mu.Unlock() if r.segment == nil { return nil, tcpip.ErrInvalidEndpointState } f := r.forwarder ep, err := f.listen.createEndpointAndPerformHandshake(r.segment, &header.TCPSynOptions{ MSS: r.synOptions.MSS, WS: r.synOptions.WS, TS: r.synOptions.TS, TSVal: r.synOptions.TSVal, TSEcr: r.synOptions.TSEcr, SACKPermitted: r.synOptions.SACKPermitted, }) if err != nil { return nil, err } // Start the protocol goroutine. ep.startAcceptedLoop(queue) return ep, nil }
go
func (r *ForwarderRequest) CreateEndpoint(queue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { r.mu.Lock() defer r.mu.Unlock() if r.segment == nil { return nil, tcpip.ErrInvalidEndpointState } f := r.forwarder ep, err := f.listen.createEndpointAndPerformHandshake(r.segment, &header.TCPSynOptions{ MSS: r.synOptions.MSS, WS: r.synOptions.WS, TS: r.synOptions.TS, TSVal: r.synOptions.TSVal, TSEcr: r.synOptions.TSEcr, SACKPermitted: r.synOptions.SACKPermitted, }) if err != nil { return nil, err } // Start the protocol goroutine. ep.startAcceptedLoop(queue) return ep, nil }
[ "func", "(", "r", "*", "ForwarderRequest", ")", "CreateEndpoint", "(", "queue", "*", "waiter", ".", "Queue", ")", "(", "tcpip", ".", "Endpoint", ",", "*", "tcpip", ".", "Error", ")", "{", "r", ".", "mu", ".", "Lock", "(", ")", "\n", "defer", "r", ".", "mu", ".", "Unlock", "(", ")", "\n", "if", "r", ".", "segment", "==", "nil", "{", "return", "nil", ",", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "f", ":=", "r", ".", "forwarder", "\n", "ep", ",", "err", ":=", "f", ".", "listen", ".", "createEndpointAndPerformHandshake", "(", "r", ".", "segment", ",", "&", "header", ".", "TCPSynOptions", "{", "MSS", ":", "r", ".", "synOptions", ".", "MSS", ",", "WS", ":", "r", ".", "synOptions", ".", "WS", ",", "TS", ":", "r", ".", "synOptions", ".", "TS", ",", "TSVal", ":", "r", ".", "synOptions", ".", "TSVal", ",", "TSEcr", ":", "r", ".", "synOptions", ".", "TSEcr", ",", "SACKPermitted", ":", "r", ".", "synOptions", ".", "SACKPermitted", ",", "}", ")", "\n", "if", "err", "!=", "nil", "{", "return", "nil", ",", "err", "\n", "}", "\n", "ep", ".", "startAcceptedLoop", "(", "queue", ")", "\n", "return", "ep", ",", "nil", "\n", "}" ]
// CreateEndpoint creates a TCP endpoint for the connection request, performing // the 3-way handshake in the process.
[ "CreateEndpoint", "creates", "a", "TCP", "endpoint", "for", "the", "connection", "request", "performing", "the", "3", "-", "way", "handshake", "in", "the", "process", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/forwarder.go#L146-L171
train
google/netstack
tcpip/transport/tcp/snd.go
sendAck
func (s *sender) sendAck() { s.sendSegment(buffer.VectorisedView{}, header.TCPFlagAck, s.sndNxt) }
go
func (s *sender) sendAck() { s.sendSegment(buffer.VectorisedView{}, header.TCPFlagAck, s.sndNxt) }
[ "func", "(", "s", "*", "sender", ")", "sendAck", "(", ")", "{", "s", ".", "sendSegment", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagAck", ",", "s", ".", "sndNxt", ")", "\n", "}" ]
// sendAck sends an ACK segment.
[ "sendAck", "sends", "an", "ACK", "segment", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L287-L289
train
google/netstack
tcpip/transport/tcp/snd.go
updateRTO
func (s *sender) updateRTO(rtt time.Duration) { s.rtt.Lock() if !s.srttInited { s.rtt.rttvar = rtt / 2 s.rtt.srtt = rtt s.srttInited = true } else { diff := s.rtt.srtt - rtt if diff < 0 { diff = -diff } // Use RFC6298 standard algorithm to update rttvar and srtt when // no timestamps are available. if !s.ep.sendTSOk { s.rtt.rttvar = (3*s.rtt.rttvar + diff) / 4 s.rtt.srtt = (7*s.rtt.srtt + rtt) / 8 } else { // When we are taking RTT measurements of every ACK then // we need to use a modified method as specified in // https://tools.ietf.org/html/rfc7323#appendix-G if s.outstanding == 0 { s.rtt.Unlock() return } // Netstack measures congestion window/inflight all in // terms of packets and not bytes. This is similar to // how linux also does cwnd and inflight. In practice // this approximation works as expected. expectedSamples := math.Ceil(float64(s.outstanding) / 2) // alpha & beta values are the original values as recommended in // https://tools.ietf.org/html/rfc6298#section-2.3. const alpha = 0.125 const beta = 0.25 alphaPrime := alpha / expectedSamples betaPrime := beta / expectedSamples rttVar := (1-betaPrime)*s.rtt.rttvar.Seconds() + betaPrime*diff.Seconds() srtt := (1-alphaPrime)*s.rtt.srtt.Seconds() + alphaPrime*rtt.Seconds() s.rtt.rttvar = time.Duration(rttVar * float64(time.Second)) s.rtt.srtt = time.Duration(srtt * float64(time.Second)) } } s.rto = s.rtt.srtt + 4*s.rtt.rttvar s.rtt.Unlock() if s.rto < minRTO { s.rto = minRTO } }
go
func (s *sender) updateRTO(rtt time.Duration) { s.rtt.Lock() if !s.srttInited { s.rtt.rttvar = rtt / 2 s.rtt.srtt = rtt s.srttInited = true } else { diff := s.rtt.srtt - rtt if diff < 0 { diff = -diff } // Use RFC6298 standard algorithm to update rttvar and srtt when // no timestamps are available. if !s.ep.sendTSOk { s.rtt.rttvar = (3*s.rtt.rttvar + diff) / 4 s.rtt.srtt = (7*s.rtt.srtt + rtt) / 8 } else { // When we are taking RTT measurements of every ACK then // we need to use a modified method as specified in // https://tools.ietf.org/html/rfc7323#appendix-G if s.outstanding == 0 { s.rtt.Unlock() return } // Netstack measures congestion window/inflight all in // terms of packets and not bytes. This is similar to // how linux also does cwnd and inflight. In practice // this approximation works as expected. expectedSamples := math.Ceil(float64(s.outstanding) / 2) // alpha & beta values are the original values as recommended in // https://tools.ietf.org/html/rfc6298#section-2.3. const alpha = 0.125 const beta = 0.25 alphaPrime := alpha / expectedSamples betaPrime := beta / expectedSamples rttVar := (1-betaPrime)*s.rtt.rttvar.Seconds() + betaPrime*diff.Seconds() srtt := (1-alphaPrime)*s.rtt.srtt.Seconds() + alphaPrime*rtt.Seconds() s.rtt.rttvar = time.Duration(rttVar * float64(time.Second)) s.rtt.srtt = time.Duration(srtt * float64(time.Second)) } } s.rto = s.rtt.srtt + 4*s.rtt.rttvar s.rtt.Unlock() if s.rto < minRTO { s.rto = minRTO } }
[ "func", "(", "s", "*", "sender", ")", "updateRTO", "(", "rtt", "time", ".", "Duration", ")", "{", "s", ".", "rtt", ".", "Lock", "(", ")", "\n", "if", "!", "s", ".", "srttInited", "{", "s", ".", "rtt", ".", "rttvar", "=", "rtt", "/", "2", "\n", "s", ".", "rtt", ".", "srtt", "=", "rtt", "\n", "s", ".", "srttInited", "=", "true", "\n", "}", "else", "{", "diff", ":=", "s", ".", "rtt", ".", "srtt", "-", "rtt", "\n", "if", "diff", "<", "0", "{", "diff", "=", "-", "diff", "\n", "}", "\n", "if", "!", "s", ".", "ep", ".", "sendTSOk", "{", "s", ".", "rtt", ".", "rttvar", "=", "(", "3", "*", "s", ".", "rtt", ".", "rttvar", "+", "diff", ")", "/", "4", "\n", "s", ".", "rtt", ".", "srtt", "=", "(", "7", "*", "s", ".", "rtt", ".", "srtt", "+", "rtt", ")", "/", "8", "\n", "}", "else", "{", "if", "s", ".", "outstanding", "==", "0", "{", "s", ".", "rtt", ".", "Unlock", "(", ")", "\n", "return", "\n", "}", "\n", "expectedSamples", ":=", "math", ".", "Ceil", "(", "float64", "(", "s", ".", "outstanding", ")", "/", "2", ")", "\n", "const", "alpha", "=", "0.125", "\n", "const", "beta", "=", "0.25", "\n", "alphaPrime", ":=", "alpha", "/", "expectedSamples", "\n", "betaPrime", ":=", "beta", "/", "expectedSamples", "\n", "rttVar", ":=", "(", "1", "-", "betaPrime", ")", "*", "s", ".", "rtt", ".", "rttvar", ".", "Seconds", "(", ")", "+", "betaPrime", "*", "diff", ".", "Seconds", "(", ")", "\n", "srtt", ":=", "(", "1", "-", "alphaPrime", ")", "*", "s", ".", "rtt", ".", "srtt", ".", "Seconds", "(", ")", "+", "alphaPrime", "*", "rtt", ".", "Seconds", "(", ")", "\n", "s", ".", "rtt", ".", "rttvar", "=", "time", ".", "Duration", "(", "rttVar", "*", "float64", "(", "time", ".", "Second", ")", ")", "\n", "s", ".", "rtt", ".", "srtt", "=", "time", ".", "Duration", "(", "srtt", "*", "float64", "(", "time", ".", "Second", ")", ")", "\n", "}", "\n", "}", "\n", "s", ".", "rto", "=", "s", ".", "rtt", ".", "srtt", "+", "4", "*", "s", ".", "rtt", ".", "rttvar", "\n", "s", ".", "rtt", ".", "Unlock", "(", ")", "\n", "if", "s", ".", "rto", "<", "minRTO", "{", "s", ".", "rto", "=", "minRTO", "\n", "}", "\n", "}" ]
// updateRTO updates the retransmit timeout when a new roud-trip time is // available. This is done in accordance with section 2 of RFC 6298.
[ "updateRTO", "updates", "the", "retransmit", "timeout", "when", "a", "new", "roud", "-", "trip", "time", "is", "available", ".", "This", "is", "done", "in", "accordance", "with", "section", "2", "of", "RFC", "6298", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L293-L342
train
google/netstack
tcpip/transport/tcp/snd.go
resendSegment
func (s *sender) resendSegment() { // Don't use any segments we already sent to measure RTT as they may // have been affected by packets being lost. s.rttMeasureSeqNum = s.sndNxt // Resend the segment. if seg := s.writeList.Front(); seg != nil { if seg.data.Size() > s.maxPayloadSize { available := s.maxPayloadSize // Split this segment up. nSeg := seg.clone() nSeg.data.TrimFront(available) nSeg.sequenceNumber.UpdateForward(seqnum.Size(available)) s.writeList.InsertAfter(seg, nSeg) seg.data.CapLength(available) } s.sendSegment(seg.data, seg.flags, seg.sequenceNumber) s.ep.stack.Stats().TCP.FastRetransmit.Increment() s.ep.stack.Stats().TCP.Retransmits.Increment() } }
go
func (s *sender) resendSegment() { // Don't use any segments we already sent to measure RTT as they may // have been affected by packets being lost. s.rttMeasureSeqNum = s.sndNxt // Resend the segment. if seg := s.writeList.Front(); seg != nil { if seg.data.Size() > s.maxPayloadSize { available := s.maxPayloadSize // Split this segment up. nSeg := seg.clone() nSeg.data.TrimFront(available) nSeg.sequenceNumber.UpdateForward(seqnum.Size(available)) s.writeList.InsertAfter(seg, nSeg) seg.data.CapLength(available) } s.sendSegment(seg.data, seg.flags, seg.sequenceNumber) s.ep.stack.Stats().TCP.FastRetransmit.Increment() s.ep.stack.Stats().TCP.Retransmits.Increment() } }
[ "func", "(", "s", "*", "sender", ")", "resendSegment", "(", ")", "{", "s", ".", "rttMeasureSeqNum", "=", "s", ".", "sndNxt", "\n", "if", "seg", ":=", "s", ".", "writeList", ".", "Front", "(", ")", ";", "seg", "!=", "nil", "{", "if", "seg", ".", "data", ".", "Size", "(", ")", ">", "s", ".", "maxPayloadSize", "{", "available", ":=", "s", ".", "maxPayloadSize", "\n", "nSeg", ":=", "seg", ".", "clone", "(", ")", "\n", "nSeg", ".", "data", ".", "TrimFront", "(", "available", ")", "\n", "nSeg", ".", "sequenceNumber", ".", "UpdateForward", "(", "seqnum", ".", "Size", "(", "available", ")", ")", "\n", "s", ".", "writeList", ".", "InsertAfter", "(", "seg", ",", "nSeg", ")", "\n", "seg", ".", "data", ".", "CapLength", "(", "available", ")", "\n", "}", "\n", "s", ".", "sendSegment", "(", "seg", ".", "data", ",", "seg", ".", "flags", ",", "seg", ".", "sequenceNumber", ")", "\n", "s", ".", "ep", ".", "stack", ".", "Stats", "(", ")", ".", "TCP", ".", "FastRetransmit", ".", "Increment", "(", ")", "\n", "s", ".", "ep", ".", "stack", ".", "Stats", "(", ")", ".", "TCP", ".", "Retransmits", ".", "Increment", "(", ")", "\n", "}", "\n", "}" ]
// resendSegment resends the first unacknowledged segment.
[ "resendSegment", "resends", "the", "first", "unacknowledged", "segment", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L345-L365
train
google/netstack
tcpip/transport/tcp/snd.go
retransmitTimerExpired
func (s *sender) retransmitTimerExpired() bool { // Check if the timer actually expired or if it's a spurious wake due // to a previously orphaned runtime timer. if !s.resendTimer.checkExpiration() { return true } s.ep.stack.Stats().TCP.Timeouts.Increment() // Give up if we've waited more than a minute since the last resend. if s.rto >= 60*time.Second { return false } // Set new timeout. The timer will be restarted by the call to sendData // below. s.rto *= 2 if s.fr.active { // We were attempting fast recovery but were not successful. // Leave the state. We don't need to update ssthresh because it // has already been updated when entered fast-recovery. s.leaveFastRecovery() } // See: https://tools.ietf.org/html/rfc6582#section-3.2 Step 4. // We store the highest sequence number transmitted in cases where // we were not in fast recovery. s.fr.last = s.sndNxt - 1 s.cc.HandleRTOExpired() // Mark the next segment to be sent as the first unacknowledged one and // start sending again. Set the number of outstanding packets to 0 so // that we'll be able to retransmit. // // We'll keep on transmitting (or retransmitting) as we get acks for // the data we transmit. s.outstanding = 0 // Expunge all SACK information as per https://tools.ietf.org/html/rfc6675#section-5.1 // // In order to avoid memory deadlocks, the TCP receiver is allowed to // discard data that has already been selectively acknowledged. As a // result, [RFC2018] suggests that a TCP sender SHOULD expunge the SACK // information gathered from a receiver upon a retransmission timeout // (RTO) "since the timeout might indicate that the data receiver has // reneged." Additionally, a TCP sender MUST "ignore prior SACK // information in determining which data to retransmit." // // NOTE: We take the stricter interpretation and just expunge all // information as we lack more rigorous checks to validate if the SACK // information is usable after an RTO. s.ep.scoreboard.Reset() s.writeNext = s.writeList.Front() s.sendData() return true }
go
func (s *sender) retransmitTimerExpired() bool { // Check if the timer actually expired or if it's a spurious wake due // to a previously orphaned runtime timer. if !s.resendTimer.checkExpiration() { return true } s.ep.stack.Stats().TCP.Timeouts.Increment() // Give up if we've waited more than a minute since the last resend. if s.rto >= 60*time.Second { return false } // Set new timeout. The timer will be restarted by the call to sendData // below. s.rto *= 2 if s.fr.active { // We were attempting fast recovery but were not successful. // Leave the state. We don't need to update ssthresh because it // has already been updated when entered fast-recovery. s.leaveFastRecovery() } // See: https://tools.ietf.org/html/rfc6582#section-3.2 Step 4. // We store the highest sequence number transmitted in cases where // we were not in fast recovery. s.fr.last = s.sndNxt - 1 s.cc.HandleRTOExpired() // Mark the next segment to be sent as the first unacknowledged one and // start sending again. Set the number of outstanding packets to 0 so // that we'll be able to retransmit. // // We'll keep on transmitting (or retransmitting) as we get acks for // the data we transmit. s.outstanding = 0 // Expunge all SACK information as per https://tools.ietf.org/html/rfc6675#section-5.1 // // In order to avoid memory deadlocks, the TCP receiver is allowed to // discard data that has already been selectively acknowledged. As a // result, [RFC2018] suggests that a TCP sender SHOULD expunge the SACK // information gathered from a receiver upon a retransmission timeout // (RTO) "since the timeout might indicate that the data receiver has // reneged." Additionally, a TCP sender MUST "ignore prior SACK // information in determining which data to retransmit." // // NOTE: We take the stricter interpretation and just expunge all // information as we lack more rigorous checks to validate if the SACK // information is usable after an RTO. s.ep.scoreboard.Reset() s.writeNext = s.writeList.Front() s.sendData() return true }
[ "func", "(", "s", "*", "sender", ")", "retransmitTimerExpired", "(", ")", "bool", "{", "if", "!", "s", ".", "resendTimer", ".", "checkExpiration", "(", ")", "{", "return", "true", "\n", "}", "\n", "s", ".", "ep", ".", "stack", ".", "Stats", "(", ")", ".", "TCP", ".", "Timeouts", ".", "Increment", "(", ")", "\n", "if", "s", ".", "rto", ">=", "60", "*", "time", ".", "Second", "{", "return", "false", "\n", "}", "\n", "s", ".", "rto", "*=", "2", "\n", "if", "s", ".", "fr", ".", "active", "{", "s", ".", "leaveFastRecovery", "(", ")", "\n", "}", "\n", "s", ".", "fr", ".", "last", "=", "s", ".", "sndNxt", "-", "1", "\n", "s", ".", "cc", ".", "HandleRTOExpired", "(", ")", "\n", "s", ".", "outstanding", "=", "0", "\n", "s", ".", "ep", ".", "scoreboard", ".", "Reset", "(", ")", "\n", "s", ".", "writeNext", "=", "s", ".", "writeList", ".", "Front", "(", ")", "\n", "s", ".", "sendData", "(", ")", "\n", "return", "true", "\n", "}" ]
// retransmitTimerExpired is called when the retransmit timer expires, and // unacknowledged segments are assumed lost, and thus need to be resent. // Returns true if the connection is still usable, or false if the connection // is deemed lost.
[ "retransmitTimerExpired", "is", "called", "when", "the", "retransmit", "timer", "expires", "and", "unacknowledged", "segments", "are", "assumed", "lost", "and", "thus", "need", "to", "be", "resent", ".", "Returns", "true", "if", "the", "connection", "is", "still", "usable", "or", "false", "if", "the", "connection", "is", "deemed", "lost", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L371-L429
train
google/netstack
tcpip/transport/tcp/snd.go
pCount
func (s *sender) pCount(seg *segment) int { size := seg.data.Size() if size == 0 { return 1 } return (size-1)/s.maxPayloadSize + 1 }
go
func (s *sender) pCount(seg *segment) int { size := seg.data.Size() if size == 0 { return 1 } return (size-1)/s.maxPayloadSize + 1 }
[ "func", "(", "s", "*", "sender", ")", "pCount", "(", "seg", "*", "segment", ")", "int", "{", "size", ":=", "seg", ".", "data", ".", "Size", "(", ")", "\n", "if", "size", "==", "0", "{", "return", "1", "\n", "}", "\n", "return", "(", "size", "-", "1", ")", "/", "s", ".", "maxPayloadSize", "+", "1", "\n", "}" ]
// pCount returns the number of packets in the segment. Due to GSO, a segment // can be composed of multiple packets.
[ "pCount", "returns", "the", "number", "of", "packets", "in", "the", "segment", ".", "Due", "to", "GSO", "a", "segment", "can", "be", "composed", "of", "multiple", "packets", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L433-L440
train
google/netstack
tcpip/transport/tcp/snd.go
sendSegment
func (s *sender) sendSegment(data buffer.VectorisedView, flags byte, seq seqnum.Value) *tcpip.Error { s.lastSendTime = time.Now() if seq == s.rttMeasureSeqNum { s.rttMeasureTime = s.lastSendTime } rcvNxt, rcvWnd := s.ep.rcv.getSendParams() // Remember the max sent ack. s.maxSentAck = rcvNxt return s.ep.sendRaw(data, flags, seq, rcvNxt, rcvWnd) }
go
func (s *sender) sendSegment(data buffer.VectorisedView, flags byte, seq seqnum.Value) *tcpip.Error { s.lastSendTime = time.Now() if seq == s.rttMeasureSeqNum { s.rttMeasureTime = s.lastSendTime } rcvNxt, rcvWnd := s.ep.rcv.getSendParams() // Remember the max sent ack. s.maxSentAck = rcvNxt return s.ep.sendRaw(data, flags, seq, rcvNxt, rcvWnd) }
[ "func", "(", "s", "*", "sender", ")", "sendSegment", "(", "data", "buffer", ".", "VectorisedView", ",", "flags", "byte", ",", "seq", "seqnum", ".", "Value", ")", "*", "tcpip", ".", "Error", "{", "s", ".", "lastSendTime", "=", "time", ".", "Now", "(", ")", "\n", "if", "seq", "==", "s", ".", "rttMeasureSeqNum", "{", "s", ".", "rttMeasureTime", "=", "s", ".", "lastSendTime", "\n", "}", "\n", "rcvNxt", ",", "rcvWnd", ":=", "s", ".", "ep", ".", "rcv", ".", "getSendParams", "(", ")", "\n", "s", ".", "maxSentAck", "=", "rcvNxt", "\n", "return", "s", ".", "ep", ".", "sendRaw", "(", "data", ",", "flags", ",", "seq", ",", "rcvNxt", ",", "rcvWnd", ")", "\n", "}" ]
// sendSegment sends a new segment containing the given payload, flags and // sequence number.
[ "sendSegment", "sends", "a", "new", "segment", "containing", "the", "given", "payload", "flags", "and", "sequence", "number", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/snd.go#L835-L847
train
google/netstack
tcpip/link/muxed/injectable.go
MTU
func (m *InjectableEndpoint) MTU() uint32 { minMTU := ^uint32(0) for _, endpoint := range m.routes { if endpointMTU := endpoint.MTU(); endpointMTU < minMTU { minMTU = endpointMTU } } return minMTU }
go
func (m *InjectableEndpoint) MTU() uint32 { minMTU := ^uint32(0) for _, endpoint := range m.routes { if endpointMTU := endpoint.MTU(); endpointMTU < minMTU { minMTU = endpointMTU } } return minMTU }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "MTU", "(", ")", "uint32", "{", "minMTU", ":=", "^", "uint32", "(", "0", ")", "\n", "for", "_", ",", "endpoint", ":=", "range", "m", ".", "routes", "{", "if", "endpointMTU", ":=", "endpoint", ".", "MTU", "(", ")", ";", "endpointMTU", "<", "minMTU", "{", "minMTU", "=", "endpointMTU", "\n", "}", "\n", "}", "\n", "return", "minMTU", "\n", "}" ]
// MTU implements stack.LinkEndpoint.
[ "MTU", "implements", "stack", ".", "LinkEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L34-L42
train
google/netstack
tcpip/link/muxed/injectable.go
Capabilities
func (m *InjectableEndpoint) Capabilities() stack.LinkEndpointCapabilities { minCapabilities := stack.LinkEndpointCapabilities(^uint(0)) for _, endpoint := range m.routes { minCapabilities &= endpoint.Capabilities() } return minCapabilities }
go
func (m *InjectableEndpoint) Capabilities() stack.LinkEndpointCapabilities { minCapabilities := stack.LinkEndpointCapabilities(^uint(0)) for _, endpoint := range m.routes { minCapabilities &= endpoint.Capabilities() } return minCapabilities }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "Capabilities", "(", ")", "stack", ".", "LinkEndpointCapabilities", "{", "minCapabilities", ":=", "stack", ".", "LinkEndpointCapabilities", "(", "^", "uint", "(", "0", ")", ")", "\n", "for", "_", ",", "endpoint", ":=", "range", "m", ".", "routes", "{", "minCapabilities", "&=", "endpoint", ".", "Capabilities", "(", ")", "\n", "}", "\n", "return", "minCapabilities", "\n", "}" ]
// Capabilities implements stack.LinkEndpoint.
[ "Capabilities", "implements", "stack", ".", "LinkEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L45-L51
train
google/netstack
tcpip/link/muxed/injectable.go
MaxHeaderLength
func (m *InjectableEndpoint) MaxHeaderLength() uint16 { minHeaderLen := ^uint16(0) for _, endpoint := range m.routes { if headerLen := endpoint.MaxHeaderLength(); headerLen < minHeaderLen { minHeaderLen = headerLen } } return minHeaderLen }
go
func (m *InjectableEndpoint) MaxHeaderLength() uint16 { minHeaderLen := ^uint16(0) for _, endpoint := range m.routes { if headerLen := endpoint.MaxHeaderLength(); headerLen < minHeaderLen { minHeaderLen = headerLen } } return minHeaderLen }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "MaxHeaderLength", "(", ")", "uint16", "{", "minHeaderLen", ":=", "^", "uint16", "(", "0", ")", "\n", "for", "_", ",", "endpoint", ":=", "range", "m", ".", "routes", "{", "if", "headerLen", ":=", "endpoint", ".", "MaxHeaderLength", "(", ")", ";", "headerLen", "<", "minHeaderLen", "{", "minHeaderLen", "=", "headerLen", "\n", "}", "\n", "}", "\n", "return", "minHeaderLen", "\n", "}" ]
// MaxHeaderLength implements stack.LinkEndpoint.
[ "MaxHeaderLength", "implements", "stack", ".", "LinkEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L54-L62
train
google/netstack
tcpip/link/muxed/injectable.go
Attach
func (m *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) { for _, endpoint := range m.routes { endpoint.Attach(dispatcher) } m.dispatcher = dispatcher }
go
func (m *InjectableEndpoint) Attach(dispatcher stack.NetworkDispatcher) { for _, endpoint := range m.routes { endpoint.Attach(dispatcher) } m.dispatcher = dispatcher }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "Attach", "(", "dispatcher", "stack", ".", "NetworkDispatcher", ")", "{", "for", "_", ",", "endpoint", ":=", "range", "m", ".", "routes", "{", "endpoint", ".", "Attach", "(", "dispatcher", ")", "\n", "}", "\n", "m", ".", "dispatcher", "=", "dispatcher", "\n", "}" ]
// Attach implements stack.LinkEndpoint.
[ "Attach", "implements", "stack", ".", "LinkEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L70-L75
train
google/netstack
tcpip/link/muxed/injectable.go
Inject
func (m *InjectableEndpoint) Inject(protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) { m.dispatcher.DeliverNetworkPacket(m, "" /* remote */, "" /* local */, protocol, vv) }
go
func (m *InjectableEndpoint) Inject(protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView) { m.dispatcher.DeliverNetworkPacket(m, "" /* remote */, "" /* local */, protocol, vv) }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "Inject", "(", "protocol", "tcpip", ".", "NetworkProtocolNumber", ",", "vv", "buffer", ".", "VectorisedView", ")", "{", "m", ".", "dispatcher", ".", "DeliverNetworkPacket", "(", "m", ",", "\"\"", ",", "\"\"", ",", "protocol", ",", "vv", ")", "\n", "}" ]
// Inject implements stack.InjectableLinkEndpoint.
[ "Inject", "implements", "stack", ".", "InjectableLinkEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L83-L85
train
google/netstack
tcpip/link/muxed/injectable.go
WritePacket
func (m *InjectableEndpoint) WritePacket(r *stack.Route, _ *stack.GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { if endpoint, ok := m.routes[r.RemoteAddress]; ok { return endpoint.WritePacket(r, nil /* gso */, hdr, payload, protocol) } return tcpip.ErrNoRoute }
go
func (m *InjectableEndpoint) WritePacket(r *stack.Route, _ *stack.GSO, hdr buffer.Prependable, payload buffer.VectorisedView, protocol tcpip.NetworkProtocolNumber) *tcpip.Error { if endpoint, ok := m.routes[r.RemoteAddress]; ok { return endpoint.WritePacket(r, nil /* gso */, hdr, payload, protocol) } return tcpip.ErrNoRoute }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "WritePacket", "(", "r", "*", "stack", ".", "Route", ",", "_", "*", "stack", ".", "GSO", ",", "hdr", "buffer", ".", "Prependable", ",", "payload", "buffer", ".", "VectorisedView", ",", "protocol", "tcpip", ".", "NetworkProtocolNumber", ")", "*", "tcpip", ".", "Error", "{", "if", "endpoint", ",", "ok", ":=", "m", ".", "routes", "[", "r", ".", "RemoteAddress", "]", ";", "ok", "{", "return", "endpoint", ".", "WritePacket", "(", "r", ",", "nil", ",", "hdr", ",", "payload", ",", "protocol", ")", "\n", "}", "\n", "return", "tcpip", ".", "ErrNoRoute", "\n", "}" ]
// WritePacket writes outbound packets to the appropriate LinkInjectableEndpoint // based on the RemoteAddress. HandleLocal only works if r.RemoteAddress has a // route registered in this endpoint.
[ "WritePacket", "writes", "outbound", "packets", "to", "the", "appropriate", "LinkInjectableEndpoint", "based", "on", "the", "RemoteAddress", ".", "HandleLocal", "only", "works", "if", "r", ".", "RemoteAddress", "has", "a", "route", "registered", "in", "this", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L90-L95
train
google/netstack
tcpip/link/muxed/injectable.go
WriteRawPacket
func (m *InjectableEndpoint) WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error { endpoint, ok := m.routes[dest] if !ok { return tcpip.ErrNoRoute } return endpoint.WriteRawPacket(dest, packet) }
go
func (m *InjectableEndpoint) WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error { endpoint, ok := m.routes[dest] if !ok { return tcpip.ErrNoRoute } return endpoint.WriteRawPacket(dest, packet) }
[ "func", "(", "m", "*", "InjectableEndpoint", ")", "WriteRawPacket", "(", "dest", "tcpip", ".", "Address", ",", "packet", "[", "]", "byte", ")", "*", "tcpip", ".", "Error", "{", "endpoint", ",", "ok", ":=", "m", ".", "routes", "[", "dest", "]", "\n", "if", "!", "ok", "{", "return", "tcpip", ".", "ErrNoRoute", "\n", "}", "\n", "return", "endpoint", ".", "WriteRawPacket", "(", "dest", ",", "packet", ")", "\n", "}" ]
// WriteRawPacket writes outbound packets to the appropriate // LinkInjectableEndpoint based on the dest address.
[ "WriteRawPacket", "writes", "outbound", "packets", "to", "the", "appropriate", "LinkInjectableEndpoint", "based", "on", "the", "dest", "address", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L99-L105
train
google/netstack
tcpip/link/muxed/injectable.go
NewInjectableEndpoint
func NewInjectableEndpoint(routes map[tcpip.Address]stack.InjectableLinkEndpoint) (tcpip.LinkEndpointID, *InjectableEndpoint) { e := &InjectableEndpoint{ routes: routes, } return stack.RegisterLinkEndpoint(e), e }
go
func NewInjectableEndpoint(routes map[tcpip.Address]stack.InjectableLinkEndpoint) (tcpip.LinkEndpointID, *InjectableEndpoint) { e := &InjectableEndpoint{ routes: routes, } return stack.RegisterLinkEndpoint(e), e }
[ "func", "NewInjectableEndpoint", "(", "routes", "map", "[", "tcpip", ".", "Address", "]", "stack", ".", "InjectableLinkEndpoint", ")", "(", "tcpip", ".", "LinkEndpointID", ",", "*", "InjectableEndpoint", ")", "{", "e", ":=", "&", "InjectableEndpoint", "{", "routes", ":", "routes", ",", "}", "\n", "return", "stack", ".", "RegisterLinkEndpoint", "(", "e", ")", ",", "e", "\n", "}" ]
// NewInjectableEndpoint creates a new multi-endpoint injectable endpoint.
[ "NewInjectableEndpoint", "creates", "a", "new", "multi", "-", "endpoint", "injectable", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/muxed/injectable.go#L108-L113
train
google/netstack
tcpip/link/rawfile/rawfile_unsafe.go
GetMTU
func GetMTU(name string) (uint32, error) { fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0) if err != nil { return 0, err } defer syscall.Close(fd) var ifreq struct { name [16]byte mtu int32 _ [20]byte } copy(ifreq.name[:], name) _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifreq))) if errno != 0 { return 0, errno } return uint32(ifreq.mtu), nil }
go
func GetMTU(name string) (uint32, error) { fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0) if err != nil { return 0, err } defer syscall.Close(fd) var ifreq struct { name [16]byte mtu int32 _ [20]byte } copy(ifreq.name[:], name) _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifreq))) if errno != 0 { return 0, errno } return uint32(ifreq.mtu), nil }
[ "func", "GetMTU", "(", "name", "string", ")", "(", "uint32", ",", "error", ")", "{", "fd", ",", "err", ":=", "syscall", ".", "Socket", "(", "syscall", ".", "AF_UNIX", ",", "syscall", ".", "SOCK_DGRAM", ",", "0", ")", "\n", "if", "err", "!=", "nil", "{", "return", "0", ",", "err", "\n", "}", "\n", "defer", "syscall", ".", "Close", "(", "fd", ")", "\n", "var", "ifreq", "struct", "{", "name", "[", "16", "]", "byte", "\n", "mtu", "int32", "\n", "_", "[", "20", "]", "byte", "\n", "}", "\n", "copy", "(", "ifreq", ".", "name", "[", ":", "]", ",", "name", ")", "\n", "_", ",", "_", ",", "errno", ":=", "syscall", ".", "Syscall", "(", "syscall", ".", "SYS_IOCTL", ",", "uintptr", "(", "fd", ")", ",", "syscall", ".", "SIOCGIFMTU", ",", "uintptr", "(", "unsafe", ".", "Pointer", "(", "&", "ifreq", ")", ")", ")", "\n", "if", "errno", "!=", "0", "{", "return", "0", ",", "errno", "\n", "}", "\n", "return", "uint32", "(", "ifreq", ".", "mtu", ")", ",", "nil", "\n", "}" ]
// GetMTU determines the MTU of a network interface device.
[ "GetMTU", "determines", "the", "MTU", "of", "a", "network", "interface", "device", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/rawfile/rawfile_unsafe.go#L29-L50
train
google/netstack
tcpip/link/rawfile/rawfile_unsafe.go
NonBlockingWrite
func NonBlockingWrite(fd int, buf []byte) *tcpip.Error { var ptr unsafe.Pointer if len(buf) > 0 { ptr = unsafe.Pointer(&buf[0]) } _, _, e := syscall.RawSyscall(syscall.SYS_WRITE, uintptr(fd), uintptr(ptr), uintptr(len(buf))) if e != 0 { return TranslateErrno(e) } return nil }
go
func NonBlockingWrite(fd int, buf []byte) *tcpip.Error { var ptr unsafe.Pointer if len(buf) > 0 { ptr = unsafe.Pointer(&buf[0]) } _, _, e := syscall.RawSyscall(syscall.SYS_WRITE, uintptr(fd), uintptr(ptr), uintptr(len(buf))) if e != 0 { return TranslateErrno(e) } return nil }
[ "func", "NonBlockingWrite", "(", "fd", "int", ",", "buf", "[", "]", "byte", ")", "*", "tcpip", ".", "Error", "{", "var", "ptr", "unsafe", ".", "Pointer", "\n", "if", "len", "(", "buf", ")", ">", "0", "{", "ptr", "=", "unsafe", ".", "Pointer", "(", "&", "buf", "[", "0", "]", ")", "\n", "}", "\n", "_", ",", "_", ",", "e", ":=", "syscall", ".", "RawSyscall", "(", "syscall", ".", "SYS_WRITE", ",", "uintptr", "(", "fd", ")", ",", "uintptr", "(", "ptr", ")", ",", "uintptr", "(", "len", "(", "buf", ")", ")", ")", "\n", "if", "e", "!=", "0", "{", "return", "TranslateErrno", "(", "e", ")", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// NonBlockingWrite writes the given buffer to a file descriptor. It fails if // partial data is written.
[ "NonBlockingWrite", "writes", "the", "given", "buffer", "to", "a", "file", "descriptor", ".", "It", "fails", "if", "partial", "data", "is", "written", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/rawfile/rawfile_unsafe.go#L54-L66
train
google/netstack
tcpip/link/rawfile/rawfile_unsafe.go
NonBlockingWrite3
func NonBlockingWrite3(fd int, b1, b2, b3 []byte) *tcpip.Error { // If the is no second buffer, issue a regular write. if len(b2) == 0 { return NonBlockingWrite(fd, b1) } // We have two buffers. Build the iovec that represents them and issue // a writev syscall. iovec := [3]syscall.Iovec{ { Base: &b1[0], Len: uint64(len(b1)), }, { Base: &b2[0], Len: uint64(len(b2)), }, } iovecLen := uintptr(2) if len(b3) > 0 { iovecLen++ iovec[2].Base = &b3[0] iovec[2].Len = uint64(len(b3)) } _, _, e := syscall.RawSyscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen) if e != 0 { return TranslateErrno(e) } return nil }
go
func NonBlockingWrite3(fd int, b1, b2, b3 []byte) *tcpip.Error { // If the is no second buffer, issue a regular write. if len(b2) == 0 { return NonBlockingWrite(fd, b1) } // We have two buffers. Build the iovec that represents them and issue // a writev syscall. iovec := [3]syscall.Iovec{ { Base: &b1[0], Len: uint64(len(b1)), }, { Base: &b2[0], Len: uint64(len(b2)), }, } iovecLen := uintptr(2) if len(b3) > 0 { iovecLen++ iovec[2].Base = &b3[0] iovec[2].Len = uint64(len(b3)) } _, _, e := syscall.RawSyscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), iovecLen) if e != 0 { return TranslateErrno(e) } return nil }
[ "func", "NonBlockingWrite3", "(", "fd", "int", ",", "b1", ",", "b2", ",", "b3", "[", "]", "byte", ")", "*", "tcpip", ".", "Error", "{", "if", "len", "(", "b2", ")", "==", "0", "{", "return", "NonBlockingWrite", "(", "fd", ",", "b1", ")", "\n", "}", "\n", "iovec", ":=", "[", "3", "]", "syscall", ".", "Iovec", "{", "{", "Base", ":", "&", "b1", "[", "0", "]", ",", "Len", ":", "uint64", "(", "len", "(", "b1", ")", ")", ",", "}", ",", "{", "Base", ":", "&", "b2", "[", "0", "]", ",", "Len", ":", "uint64", "(", "len", "(", "b2", ")", ")", ",", "}", ",", "}", "\n", "iovecLen", ":=", "uintptr", "(", "2", ")", "\n", "if", "len", "(", "b3", ")", ">", "0", "{", "iovecLen", "++", "\n", "iovec", "[", "2", "]", ".", "Base", "=", "&", "b3", "[", "0", "]", "\n", "iovec", "[", "2", "]", ".", "Len", "=", "uint64", "(", "len", "(", "b3", ")", ")", "\n", "}", "\n", "_", ",", "_", ",", "e", ":=", "syscall", ".", "RawSyscall", "(", "syscall", ".", "SYS_WRITEV", ",", "uintptr", "(", "fd", ")", ",", "uintptr", "(", "unsafe", ".", "Pointer", "(", "&", "iovec", "[", "0", "]", ")", ")", ",", "iovecLen", ")", "\n", "if", "e", "!=", "0", "{", "return", "TranslateErrno", "(", "e", ")", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// NonBlockingWrite3 writes up to three byte slices to a file descriptor in a // single syscall. It fails if partial data is written.
[ "NonBlockingWrite3", "writes", "up", "to", "three", "byte", "slices", "to", "a", "file", "descriptor", "in", "a", "single", "syscall", ".", "It", "fails", "if", "partial", "data", "is", "written", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/rawfile/rawfile_unsafe.go#L70-L102
train
google/netstack
tcpip/transport/tcp/segment_queue.go
empty
func (q *segmentQueue) empty() bool { q.mu.Lock() r := q.used == 0 q.mu.Unlock() return r }
go
func (q *segmentQueue) empty() bool { q.mu.Lock() r := q.used == 0 q.mu.Unlock() return r }
[ "func", "(", "q", "*", "segmentQueue", ")", "empty", "(", ")", "bool", "{", "q", ".", "mu", ".", "Lock", "(", ")", "\n", "r", ":=", "q", ".", "used", "==", "0", "\n", "q", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "r", "\n", "}" ]
// empty determines if the queue is empty.
[ "empty", "determines", "if", "the", "queue", "is", "empty", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/segment_queue.go#L34-L40
train
google/netstack
tcpip/transport/tcp/segment_queue.go
setLimit
func (q *segmentQueue) setLimit(limit int) { q.mu.Lock() q.limit = limit q.mu.Unlock() }
go
func (q *segmentQueue) setLimit(limit int) { q.mu.Lock() q.limit = limit q.mu.Unlock() }
[ "func", "(", "q", "*", "segmentQueue", ")", "setLimit", "(", "limit", "int", ")", "{", "q", ".", "mu", ".", "Lock", "(", ")", "\n", "q", ".", "limit", "=", "limit", "\n", "q", ".", "mu", ".", "Unlock", "(", ")", "\n", "}" ]
// setLimit updates the limit. No segments are immediately dropped in case the // queue becomes full due to the new limit.
[ "setLimit", "updates", "the", "limit", ".", "No", "segments", "are", "immediately", "dropped", "in", "case", "the", "queue", "becomes", "full", "due", "to", "the", "new", "limit", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/segment_queue.go#L44-L48
train
google/netstack
tcpip/transport/tcp/segment_queue.go
enqueue
func (q *segmentQueue) enqueue(s *segment) bool { q.mu.Lock() r := q.used < q.limit if r { q.list.PushBack(s) q.used += s.data.Size() + header.TCPMinimumSize } q.mu.Unlock() return r }
go
func (q *segmentQueue) enqueue(s *segment) bool { q.mu.Lock() r := q.used < q.limit if r { q.list.PushBack(s) q.used += s.data.Size() + header.TCPMinimumSize } q.mu.Unlock() return r }
[ "func", "(", "q", "*", "segmentQueue", ")", "enqueue", "(", "s", "*", "segment", ")", "bool", "{", "q", ".", "mu", ".", "Lock", "(", ")", "\n", "r", ":=", "q", ".", "used", "<", "q", ".", "limit", "\n", "if", "r", "{", "q", ".", "list", ".", "PushBack", "(", "s", ")", "\n", "q", ".", "used", "+=", "s", ".", "data", ".", "Size", "(", ")", "+", "header", ".", "TCPMinimumSize", "\n", "}", "\n", "q", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "r", "\n", "}" ]
// enqueue adds the given segment to the queue. // // Returns true when the segment is successfully added to the queue, in which // case ownership of the reference is transferred to the queue. And returns // false if the queue is full, in which case ownership is retained by the // caller.
[ "enqueue", "adds", "the", "given", "segment", "to", "the", "queue", ".", "Returns", "true", "when", "the", "segment", "is", "successfully", "added", "to", "the", "queue", "in", "which", "case", "ownership", "of", "the", "reference", "is", "transferred", "to", "the", "queue", ".", "And", "returns", "false", "if", "the", "queue", "is", "full", "in", "which", "case", "ownership", "is", "retained", "by", "the", "caller", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/segment_queue.go#L56-L66
train
google/netstack
tcpip/transport/tcp/segment_queue.go
dequeue
func (q *segmentQueue) dequeue() *segment { q.mu.Lock() s := q.list.Front() if s != nil { q.list.Remove(s) q.used -= s.data.Size() + header.TCPMinimumSize } q.mu.Unlock() return s }
go
func (q *segmentQueue) dequeue() *segment { q.mu.Lock() s := q.list.Front() if s != nil { q.list.Remove(s) q.used -= s.data.Size() + header.TCPMinimumSize } q.mu.Unlock() return s }
[ "func", "(", "q", "*", "segmentQueue", ")", "dequeue", "(", ")", "*", "segment", "{", "q", ".", "mu", ".", "Lock", "(", ")", "\n", "s", ":=", "q", ".", "list", ".", "Front", "(", ")", "\n", "if", "s", "!=", "nil", "{", "q", ".", "list", ".", "Remove", "(", "s", ")", "\n", "q", ".", "used", "-=", "s", ".", "data", ".", "Size", "(", ")", "+", "header", ".", "TCPMinimumSize", "\n", "}", "\n", "q", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "s", "\n", "}" ]
// dequeue removes and returns the next segment from queue, if one exists. // Ownership is transferred to the caller, who is responsible for decrementing // the ref count when done.
[ "dequeue", "removes", "and", "returns", "the", "next", "segment", "from", "queue", "if", "one", "exists", ".", "Ownership", "is", "transferred", "to", "the", "caller", "who", "is", "responsible", "for", "decrementing", "the", "ref", "count", "when", "done", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/segment_queue.go#L71-L81
train
google/netstack
tcpip/transport/tcp/connect.go
FindWndScale
func FindWndScale(wnd seqnum.Size) int { if wnd < 0x10000 { return 0 } max := seqnum.Size(0xffff) s := 0 for wnd > max && s < header.MaxWndScale { s++ max <<= 1 } return s }
go
func FindWndScale(wnd seqnum.Size) int { if wnd < 0x10000 { return 0 } max := seqnum.Size(0xffff) s := 0 for wnd > max && s < header.MaxWndScale { s++ max <<= 1 } return s }
[ "func", "FindWndScale", "(", "wnd", "seqnum", ".", "Size", ")", "int", "{", "if", "wnd", "<", "0x10000", "{", "return", "0", "\n", "}", "\n", "max", ":=", "seqnum", ".", "Size", "(", "0xffff", ")", "\n", "s", ":=", "0", "\n", "for", "wnd", ">", "max", "&&", "s", "<", "header", ".", "MaxWndScale", "{", "s", "++", "\n", "max", "<<=", "1", "\n", "}", "\n", "return", "s", "\n", "}" ]
// FindWndScale determines the window scale to use for the given maximum window // size.
[ "FindWndScale", "determines", "the", "window", "scale", "to", "use", "for", "the", "given", "maximum", "window", "size", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L102-L115
train
google/netstack
tcpip/transport/tcp/connect.go
resetState
func (h *handshake) resetState() { b := make([]byte, 4) if _, err := rand.Read(b); err != nil { panic(err) } h.state = handshakeSynSent h.flags = header.TCPFlagSyn h.ackNum = 0 h.mss = 0 h.iss = seqnum.Value(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24) }
go
func (h *handshake) resetState() { b := make([]byte, 4) if _, err := rand.Read(b); err != nil { panic(err) } h.state = handshakeSynSent h.flags = header.TCPFlagSyn h.ackNum = 0 h.mss = 0 h.iss = seqnum.Value(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24) }
[ "func", "(", "h", "*", "handshake", ")", "resetState", "(", ")", "{", "b", ":=", "make", "(", "[", "]", "byte", ",", "4", ")", "\n", "if", "_", ",", "err", ":=", "rand", ".", "Read", "(", "b", ")", ";", "err", "!=", "nil", "{", "panic", "(", "err", ")", "\n", "}", "\n", "h", ".", "state", "=", "handshakeSynSent", "\n", "h", ".", "flags", "=", "header", ".", "TCPFlagSyn", "\n", "h", ".", "ackNum", "=", "0", "\n", "h", ".", "mss", "=", "0", "\n", "h", ".", "iss", "=", "seqnum", ".", "Value", "(", "uint32", "(", "b", "[", "0", "]", ")", "|", "uint32", "(", "b", "[", "1", "]", ")", "<<", "8", "|", "uint32", "(", "b", "[", "2", "]", ")", "<<", "16", "|", "uint32", "(", "b", "[", "3", "]", ")", "<<", "24", ")", "\n", "}" ]
// resetState resets the state of the handshake object such that it becomes // ready for a new 3-way handshake.
[ "resetState", "resets", "the", "state", "of", "the", "handshake", "object", "such", "that", "it", "becomes", "ready", "for", "a", "new", "3", "-", "way", "handshake", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L119-L130
train
google/netstack
tcpip/transport/tcp/connect.go
resetToSynRcvd
func (h *handshake) resetToSynRcvd(iss seqnum.Value, irs seqnum.Value, opts *header.TCPSynOptions) { h.active = false h.state = handshakeSynRcvd h.flags = header.TCPFlagSyn | header.TCPFlagAck h.iss = iss h.ackNum = irs + 1 h.mss = opts.MSS h.sndWndScale = opts.WS }
go
func (h *handshake) resetToSynRcvd(iss seqnum.Value, irs seqnum.Value, opts *header.TCPSynOptions) { h.active = false h.state = handshakeSynRcvd h.flags = header.TCPFlagSyn | header.TCPFlagAck h.iss = iss h.ackNum = irs + 1 h.mss = opts.MSS h.sndWndScale = opts.WS }
[ "func", "(", "h", "*", "handshake", ")", "resetToSynRcvd", "(", "iss", "seqnum", ".", "Value", ",", "irs", "seqnum", ".", "Value", ",", "opts", "*", "header", ".", "TCPSynOptions", ")", "{", "h", ".", "active", "=", "false", "\n", "h", ".", "state", "=", "handshakeSynRcvd", "\n", "h", ".", "flags", "=", "header", ".", "TCPFlagSyn", "|", "header", ".", "TCPFlagAck", "\n", "h", ".", "iss", "=", "iss", "\n", "h", ".", "ackNum", "=", "irs", "+", "1", "\n", "h", ".", "mss", "=", "opts", ".", "MSS", "\n", "h", ".", "sndWndScale", "=", "opts", ".", "WS", "\n", "}" ]
// resetToSynRcvd resets the state of the handshake object to the SYN-RCVD // state.
[ "resetToSynRcvd", "resets", "the", "state", "of", "the", "handshake", "object", "to", "the", "SYN", "-", "RCVD", "state", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L144-L152
train
google/netstack
tcpip/transport/tcp/connect.go
checkAck
func (h *handshake) checkAck(s *segment) bool { if s.flagIsSet(header.TCPFlagAck) && s.ackNumber != h.iss+1 { // RFC 793, page 36, states that a reset must be generated when // the connection is in any non-synchronized state and an // incoming segment acknowledges something not yet sent. The // connection remains in the same state. ack := s.sequenceNumber.Add(s.logicalLen()) h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, s.ackNumber, ack, 0) return false } return true }
go
func (h *handshake) checkAck(s *segment) bool { if s.flagIsSet(header.TCPFlagAck) && s.ackNumber != h.iss+1 { // RFC 793, page 36, states that a reset must be generated when // the connection is in any non-synchronized state and an // incoming segment acknowledges something not yet sent. The // connection remains in the same state. ack := s.sequenceNumber.Add(s.logicalLen()) h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, s.ackNumber, ack, 0) return false } return true }
[ "func", "(", "h", "*", "handshake", ")", "checkAck", "(", "s", "*", "segment", ")", "bool", "{", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "&&", "s", ".", "ackNumber", "!=", "h", ".", "iss", "+", "1", "{", "ack", ":=", "s", ".", "sequenceNumber", ".", "Add", "(", "s", ".", "logicalLen", "(", ")", ")", "\n", "h", ".", "ep", ".", "sendRaw", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagRst", "|", "header", ".", "TCPFlagAck", ",", "s", ".", "ackNumber", ",", "ack", ",", "0", ")", "\n", "return", "false", "\n", "}", "\n", "return", "true", "\n", "}" ]
// checkAck checks if the ACK number, if present, of a segment received during // a TCP 3-way handshake is valid. If it's not, a RST segment is sent back in // response.
[ "checkAck", "checks", "if", "the", "ACK", "number", "if", "present", "of", "a", "segment", "received", "during", "a", "TCP", "3", "-", "way", "handshake", "is", "valid", ".", "If", "it", "s", "not", "a", "RST", "segment", "is", "sent", "back", "in", "response", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L157-L169
train
google/netstack
tcpip/transport/tcp/connect.go
synSentState
func (h *handshake) synSentState(s *segment) *tcpip.Error { // RFC 793, page 37, states that in the SYN-SENT state, a reset is // acceptable if the ack field acknowledges the SYN. if s.flagIsSet(header.TCPFlagRst) { if s.flagIsSet(header.TCPFlagAck) && s.ackNumber == h.iss+1 { return tcpip.ErrConnectionRefused } return nil } if !h.checkAck(s) { return nil } // We are in the SYN-SENT state. We only care about segments that have // the SYN flag. if !s.flagIsSet(header.TCPFlagSyn) { return nil } // Parse the SYN options. rcvSynOpts := parseSynSegmentOptions(s) // Remember if the Timestamp option was negotiated. h.ep.maybeEnableTimestamp(&rcvSynOpts) // Remember if the SACKPermitted option was negotiated. h.ep.maybeEnableSACKPermitted(&rcvSynOpts) // Remember the sequence we'll ack from now on. h.ackNum = s.sequenceNumber + 1 h.flags |= header.TCPFlagAck h.mss = rcvSynOpts.MSS h.sndWndScale = rcvSynOpts.WS // If this is a SYN ACK response, we only need to acknowledge the SYN // and the handshake is completed. if s.flagIsSet(header.TCPFlagAck) { h.state = handshakeCompleted h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck, h.iss+1, h.ackNum, h.rcvWnd>>h.effectiveRcvWndScale()) return nil } // A SYN segment was received, but no ACK in it. We acknowledge the SYN // but resend our own SYN and wait for it to be acknowledged in the // SYN-RCVD state. h.state = handshakeSynRcvd synOpts := header.TCPSynOptions{ WS: h.rcvWndScale, TS: rcvSynOpts.TS, TSVal: h.ep.timestamp(), TSEcr: h.ep.recentTS, // We only send SACKPermitted if the other side indicated it // permits SACK. This is not explicitly defined in the RFC but // this is the behaviour implemented by Linux. SACKPermitted: rcvSynOpts.SACKPermitted, } sendSynTCP(&s.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) return nil }
go
func (h *handshake) synSentState(s *segment) *tcpip.Error { // RFC 793, page 37, states that in the SYN-SENT state, a reset is // acceptable if the ack field acknowledges the SYN. if s.flagIsSet(header.TCPFlagRst) { if s.flagIsSet(header.TCPFlagAck) && s.ackNumber == h.iss+1 { return tcpip.ErrConnectionRefused } return nil } if !h.checkAck(s) { return nil } // We are in the SYN-SENT state. We only care about segments that have // the SYN flag. if !s.flagIsSet(header.TCPFlagSyn) { return nil } // Parse the SYN options. rcvSynOpts := parseSynSegmentOptions(s) // Remember if the Timestamp option was negotiated. h.ep.maybeEnableTimestamp(&rcvSynOpts) // Remember if the SACKPermitted option was negotiated. h.ep.maybeEnableSACKPermitted(&rcvSynOpts) // Remember the sequence we'll ack from now on. h.ackNum = s.sequenceNumber + 1 h.flags |= header.TCPFlagAck h.mss = rcvSynOpts.MSS h.sndWndScale = rcvSynOpts.WS // If this is a SYN ACK response, we only need to acknowledge the SYN // and the handshake is completed. if s.flagIsSet(header.TCPFlagAck) { h.state = handshakeCompleted h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck, h.iss+1, h.ackNum, h.rcvWnd>>h.effectiveRcvWndScale()) return nil } // A SYN segment was received, but no ACK in it. We acknowledge the SYN // but resend our own SYN and wait for it to be acknowledged in the // SYN-RCVD state. h.state = handshakeSynRcvd synOpts := header.TCPSynOptions{ WS: h.rcvWndScale, TS: rcvSynOpts.TS, TSVal: h.ep.timestamp(), TSEcr: h.ep.recentTS, // We only send SACKPermitted if the other side indicated it // permits SACK. This is not explicitly defined in the RFC but // this is the behaviour implemented by Linux. SACKPermitted: rcvSynOpts.SACKPermitted, } sendSynTCP(&s.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) return nil }
[ "func", "(", "h", "*", "handshake", ")", "synSentState", "(", "s", "*", "segment", ")", "*", "tcpip", ".", "Error", "{", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagRst", ")", "{", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "&&", "s", ".", "ackNumber", "==", "h", ".", "iss", "+", "1", "{", "return", "tcpip", ".", "ErrConnectionRefused", "\n", "}", "\n", "return", "nil", "\n", "}", "\n", "if", "!", "h", ".", "checkAck", "(", "s", ")", "{", "return", "nil", "\n", "}", "\n", "if", "!", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagSyn", ")", "{", "return", "nil", "\n", "}", "\n", "rcvSynOpts", ":=", "parseSynSegmentOptions", "(", "s", ")", "\n", "h", ".", "ep", ".", "maybeEnableTimestamp", "(", "&", "rcvSynOpts", ")", "\n", "h", ".", "ep", ".", "maybeEnableSACKPermitted", "(", "&", "rcvSynOpts", ")", "\n", "h", ".", "ackNum", "=", "s", ".", "sequenceNumber", "+", "1", "\n", "h", ".", "flags", "|=", "header", ".", "TCPFlagAck", "\n", "h", ".", "mss", "=", "rcvSynOpts", ".", "MSS", "\n", "h", ".", "sndWndScale", "=", "rcvSynOpts", ".", "WS", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "{", "h", ".", "state", "=", "handshakeCompleted", "\n", "h", ".", "ep", ".", "sendRaw", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagAck", ",", "h", ".", "iss", "+", "1", ",", "h", ".", "ackNum", ",", "h", ".", "rcvWnd", ">>", "h", ".", "effectiveRcvWndScale", "(", ")", ")", "\n", "return", "nil", "\n", "}", "\n", "h", ".", "state", "=", "handshakeSynRcvd", "\n", "synOpts", ":=", "header", ".", "TCPSynOptions", "{", "WS", ":", "h", ".", "rcvWndScale", ",", "TS", ":", "rcvSynOpts", ".", "TS", ",", "TSVal", ":", "h", ".", "ep", ".", "timestamp", "(", ")", ",", "TSEcr", ":", "h", ".", "ep", ".", "recentTS", ",", "SACKPermitted", ":", "rcvSynOpts", ".", "SACKPermitted", ",", "}", "\n", "sendSynTCP", "(", "&", "s", ".", "route", ",", "h", ".", "ep", ".", "id", ",", "h", ".", "flags", ",", "h", ".", "iss", ",", "h", ".", "ackNum", ",", "h", ".", "rcvWnd", ",", "synOpts", ")", "\n", "return", "nil", "\n", "}" ]
// synSentState handles a segment received when the TCP 3-way handshake is in // the SYN-SENT state.
[ "synSentState", "handles", "a", "segment", "received", "when", "the", "TCP", "3", "-", "way", "handshake", "is", "in", "the", "SYN", "-", "SENT", "state", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L173-L234
train
google/netstack
tcpip/transport/tcp/connect.go
synRcvdState
func (h *handshake) synRcvdState(s *segment) *tcpip.Error { if s.flagIsSet(header.TCPFlagRst) { // RFC 793, page 37, states that in the SYN-RCVD state, a reset // is acceptable if the sequence number is in the window. if s.sequenceNumber.InWindow(h.ackNum, h.rcvWnd) { return tcpip.ErrConnectionRefused } return nil } if !h.checkAck(s) { return nil } if s.flagIsSet(header.TCPFlagSyn) && s.sequenceNumber != h.ackNum-1 { // We received two SYN segments with different sequence // numbers, so we reset this and restart the whole // process, except that we don't reset the timer. ack := s.sequenceNumber.Add(s.logicalLen()) seq := seqnum.Value(0) if s.flagIsSet(header.TCPFlagAck) { seq = s.ackNumber } h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, seq, ack, 0) if !h.active { return tcpip.ErrInvalidEndpointState } h.resetState() synOpts := header.TCPSynOptions{ WS: h.rcvWndScale, TS: h.ep.sendTSOk, TSVal: h.ep.timestamp(), TSEcr: h.ep.recentTS, SACKPermitted: h.ep.sackPermitted, } sendSynTCP(&s.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) return nil } // We have previously received (and acknowledged) the peer's SYN. If the // peer acknowledges our SYN, the handshake is completed. if s.flagIsSet(header.TCPFlagAck) { // If the timestamp option is negotiated and the segment does // not carry a timestamp option then the segment must be dropped // as per https://tools.ietf.org/html/rfc7323#section-3.2. if h.ep.sendTSOk && !s.parsedOptions.TS { h.ep.stack.Stats().DroppedPackets.Increment() return nil } // Update timestamp if required. See RFC7323, section-4.3. if h.ep.sendTSOk && s.parsedOptions.TS { h.ep.updateRecentTimestamp(s.parsedOptions.TSVal, h.ackNum, s.sequenceNumber) } h.state = handshakeCompleted return nil } return nil }
go
func (h *handshake) synRcvdState(s *segment) *tcpip.Error { if s.flagIsSet(header.TCPFlagRst) { // RFC 793, page 37, states that in the SYN-RCVD state, a reset // is acceptable if the sequence number is in the window. if s.sequenceNumber.InWindow(h.ackNum, h.rcvWnd) { return tcpip.ErrConnectionRefused } return nil } if !h.checkAck(s) { return nil } if s.flagIsSet(header.TCPFlagSyn) && s.sequenceNumber != h.ackNum-1 { // We received two SYN segments with different sequence // numbers, so we reset this and restart the whole // process, except that we don't reset the timer. ack := s.sequenceNumber.Add(s.logicalLen()) seq := seqnum.Value(0) if s.flagIsSet(header.TCPFlagAck) { seq = s.ackNumber } h.ep.sendRaw(buffer.VectorisedView{}, header.TCPFlagRst|header.TCPFlagAck, seq, ack, 0) if !h.active { return tcpip.ErrInvalidEndpointState } h.resetState() synOpts := header.TCPSynOptions{ WS: h.rcvWndScale, TS: h.ep.sendTSOk, TSVal: h.ep.timestamp(), TSEcr: h.ep.recentTS, SACKPermitted: h.ep.sackPermitted, } sendSynTCP(&s.route, h.ep.id, h.flags, h.iss, h.ackNum, h.rcvWnd, synOpts) return nil } // We have previously received (and acknowledged) the peer's SYN. If the // peer acknowledges our SYN, the handshake is completed. if s.flagIsSet(header.TCPFlagAck) { // If the timestamp option is negotiated and the segment does // not carry a timestamp option then the segment must be dropped // as per https://tools.ietf.org/html/rfc7323#section-3.2. if h.ep.sendTSOk && !s.parsedOptions.TS { h.ep.stack.Stats().DroppedPackets.Increment() return nil } // Update timestamp if required. See RFC7323, section-4.3. if h.ep.sendTSOk && s.parsedOptions.TS { h.ep.updateRecentTimestamp(s.parsedOptions.TSVal, h.ackNum, s.sequenceNumber) } h.state = handshakeCompleted return nil } return nil }
[ "func", "(", "h", "*", "handshake", ")", "synRcvdState", "(", "s", "*", "segment", ")", "*", "tcpip", ".", "Error", "{", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagRst", ")", "{", "if", "s", ".", "sequenceNumber", ".", "InWindow", "(", "h", ".", "ackNum", ",", "h", ".", "rcvWnd", ")", "{", "return", "tcpip", ".", "ErrConnectionRefused", "\n", "}", "\n", "return", "nil", "\n", "}", "\n", "if", "!", "h", ".", "checkAck", "(", "s", ")", "{", "return", "nil", "\n", "}", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagSyn", ")", "&&", "s", ".", "sequenceNumber", "!=", "h", ".", "ackNum", "-", "1", "{", "ack", ":=", "s", ".", "sequenceNumber", ".", "Add", "(", "s", ".", "logicalLen", "(", ")", ")", "\n", "seq", ":=", "seqnum", ".", "Value", "(", "0", ")", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "{", "seq", "=", "s", ".", "ackNumber", "\n", "}", "\n", "h", ".", "ep", ".", "sendRaw", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagRst", "|", "header", ".", "TCPFlagAck", ",", "seq", ",", "ack", ",", "0", ")", "\n", "if", "!", "h", ".", "active", "{", "return", "tcpip", ".", "ErrInvalidEndpointState", "\n", "}", "\n", "h", ".", "resetState", "(", ")", "\n", "synOpts", ":=", "header", ".", "TCPSynOptions", "{", "WS", ":", "h", ".", "rcvWndScale", ",", "TS", ":", "h", ".", "ep", ".", "sendTSOk", ",", "TSVal", ":", "h", ".", "ep", ".", "timestamp", "(", ")", ",", "TSEcr", ":", "h", ".", "ep", ".", "recentTS", ",", "SACKPermitted", ":", "h", ".", "ep", ".", "sackPermitted", ",", "}", "\n", "sendSynTCP", "(", "&", "s", ".", "route", ",", "h", ".", "ep", ".", "id", ",", "h", ".", "flags", ",", "h", ".", "iss", ",", "h", ".", "ackNum", ",", "h", ".", "rcvWnd", ",", "synOpts", ")", "\n", "return", "nil", "\n", "}", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "{", "if", "h", ".", "ep", ".", "sendTSOk", "&&", "!", "s", ".", "parsedOptions", ".", "TS", "{", "h", ".", "ep", ".", "stack", ".", "Stats", "(", ")", ".", "DroppedPackets", ".", "Increment", "(", ")", "\n", "return", "nil", "\n", "}", "\n", "if", "h", ".", "ep", ".", "sendTSOk", "&&", "s", ".", "parsedOptions", ".", "TS", "{", "h", ".", "ep", ".", "updateRecentTimestamp", "(", "s", ".", "parsedOptions", ".", "TSVal", ",", "h", ".", "ackNum", ",", "s", ".", "sequenceNumber", ")", "\n", "}", "\n", "h", ".", "state", "=", "handshakeCompleted", "\n", "return", "nil", "\n", "}", "\n", "return", "nil", "\n", "}" ]
// synRcvdState handles a segment received when the TCP 3-way handshake is in // the SYN-RCVD state.
[ "synRcvdState", "handles", "a", "segment", "received", "when", "the", "TCP", "3", "-", "way", "handshake", "is", "in", "the", "SYN", "-", "RCVD", "state", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L238-L300
train
google/netstack
tcpip/transport/tcp/connect.go
sendTCP
func sendTCP(r *stack.Route, id stack.TransportEndpointID, data buffer.VectorisedView, ttl uint8, flags byte, seq, ack seqnum.Value, rcvWnd seqnum.Size, opts []byte, gso *stack.GSO) *tcpip.Error { optLen := len(opts) // Allocate a buffer for the TCP header. hdr := buffer.NewPrependable(header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen) if rcvWnd > 0xffff { rcvWnd = 0xffff } // Initialize the header. tcp := header.TCP(hdr.Prepend(header.TCPMinimumSize + optLen)) tcp.Encode(&header.TCPFields{ SrcPort: id.LocalPort, DstPort: id.RemotePort, SeqNum: uint32(seq), AckNum: uint32(ack), DataOffset: uint8(header.TCPMinimumSize + optLen), Flags: flags, WindowSize: uint16(rcvWnd), }) copy(tcp[header.TCPMinimumSize:], opts) length := uint16(hdr.UsedLength() + data.Size()) xsum := r.PseudoHeaderChecksum(ProtocolNumber, length) // Only calculate the checksum if offloading isn't supported. if gso != nil && gso.NeedsCsum { // This is called CHECKSUM_PARTIAL in the Linux kernel. We // calculate a checksum of the pseudo-header and save it in the // TCP header, then the kernel calculate a checksum of the // header and data and get the right sum of the TCP packet. tcp.SetChecksum(xsum) } else if r.Capabilities()&stack.CapabilityTXChecksumOffload == 0 { xsum = header.ChecksumVV(data, xsum) tcp.SetChecksum(^tcp.CalculateChecksum(xsum)) } r.Stats().TCP.SegmentsSent.Increment() if (flags & header.TCPFlagRst) != 0 { r.Stats().TCP.ResetsSent.Increment() } return r.WritePacket(gso, hdr, data, ProtocolNumber, ttl) }
go
func sendTCP(r *stack.Route, id stack.TransportEndpointID, data buffer.VectorisedView, ttl uint8, flags byte, seq, ack seqnum.Value, rcvWnd seqnum.Size, opts []byte, gso *stack.GSO) *tcpip.Error { optLen := len(opts) // Allocate a buffer for the TCP header. hdr := buffer.NewPrependable(header.TCPMinimumSize + int(r.MaxHeaderLength()) + optLen) if rcvWnd > 0xffff { rcvWnd = 0xffff } // Initialize the header. tcp := header.TCP(hdr.Prepend(header.TCPMinimumSize + optLen)) tcp.Encode(&header.TCPFields{ SrcPort: id.LocalPort, DstPort: id.RemotePort, SeqNum: uint32(seq), AckNum: uint32(ack), DataOffset: uint8(header.TCPMinimumSize + optLen), Flags: flags, WindowSize: uint16(rcvWnd), }) copy(tcp[header.TCPMinimumSize:], opts) length := uint16(hdr.UsedLength() + data.Size()) xsum := r.PseudoHeaderChecksum(ProtocolNumber, length) // Only calculate the checksum if offloading isn't supported. if gso != nil && gso.NeedsCsum { // This is called CHECKSUM_PARTIAL in the Linux kernel. We // calculate a checksum of the pseudo-header and save it in the // TCP header, then the kernel calculate a checksum of the // header and data and get the right sum of the TCP packet. tcp.SetChecksum(xsum) } else if r.Capabilities()&stack.CapabilityTXChecksumOffload == 0 { xsum = header.ChecksumVV(data, xsum) tcp.SetChecksum(^tcp.CalculateChecksum(xsum)) } r.Stats().TCP.SegmentsSent.Increment() if (flags & header.TCPFlagRst) != 0 { r.Stats().TCP.ResetsSent.Increment() } return r.WritePacket(gso, hdr, data, ProtocolNumber, ttl) }
[ "func", "sendTCP", "(", "r", "*", "stack", ".", "Route", ",", "id", "stack", ".", "TransportEndpointID", ",", "data", "buffer", ".", "VectorisedView", ",", "ttl", "uint8", ",", "flags", "byte", ",", "seq", ",", "ack", "seqnum", ".", "Value", ",", "rcvWnd", "seqnum", ".", "Size", ",", "opts", "[", "]", "byte", ",", "gso", "*", "stack", ".", "GSO", ")", "*", "tcpip", ".", "Error", "{", "optLen", ":=", "len", "(", "opts", ")", "\n", "hdr", ":=", "buffer", ".", "NewPrependable", "(", "header", ".", "TCPMinimumSize", "+", "int", "(", "r", ".", "MaxHeaderLength", "(", ")", ")", "+", "optLen", ")", "\n", "if", "rcvWnd", ">", "0xffff", "{", "rcvWnd", "=", "0xffff", "\n", "}", "\n", "tcp", ":=", "header", ".", "TCP", "(", "hdr", ".", "Prepend", "(", "header", ".", "TCPMinimumSize", "+", "optLen", ")", ")", "\n", "tcp", ".", "Encode", "(", "&", "header", ".", "TCPFields", "{", "SrcPort", ":", "id", ".", "LocalPort", ",", "DstPort", ":", "id", ".", "RemotePort", ",", "SeqNum", ":", "uint32", "(", "seq", ")", ",", "AckNum", ":", "uint32", "(", "ack", ")", ",", "DataOffset", ":", "uint8", "(", "header", ".", "TCPMinimumSize", "+", "optLen", ")", ",", "Flags", ":", "flags", ",", "WindowSize", ":", "uint16", "(", "rcvWnd", ")", ",", "}", ")", "\n", "copy", "(", "tcp", "[", "header", ".", "TCPMinimumSize", ":", "]", ",", "opts", ")", "\n", "length", ":=", "uint16", "(", "hdr", ".", "UsedLength", "(", ")", "+", "data", ".", "Size", "(", ")", ")", "\n", "xsum", ":=", "r", ".", "PseudoHeaderChecksum", "(", "ProtocolNumber", ",", "length", ")", "\n", "if", "gso", "!=", "nil", "&&", "gso", ".", "NeedsCsum", "{", "tcp", ".", "SetChecksum", "(", "xsum", ")", "\n", "}", "else", "if", "r", ".", "Capabilities", "(", ")", "&", "stack", ".", "CapabilityTXChecksumOffload", "==", "0", "{", "xsum", "=", "header", ".", "ChecksumVV", "(", "data", ",", "xsum", ")", "\n", "tcp", ".", "SetChecksum", "(", "^", "tcp", ".", "CalculateChecksum", "(", "xsum", ")", ")", "\n", "}", "\n", "r", ".", "Stats", "(", ")", ".", "TCP", ".", "SegmentsSent", ".", "Increment", "(", ")", "\n", "if", "(", "flags", "&", "header", ".", "TCPFlagRst", ")", "!=", "0", "{", "r", ".", "Stats", "(", ")", ".", "TCP", ".", "ResetsSent", ".", "Increment", "(", ")", "\n", "}", "\n", "return", "r", ".", "WritePacket", "(", "gso", ",", "hdr", ",", "data", ",", "ProtocolNumber", ",", "ttl", ")", "\n", "}" ]
// sendTCP sends a TCP segment with the provided options via the provided // network endpoint and under the provided identity.
[ "sendTCP", "sends", "a", "TCP", "segment", "with", "the", "provided", "options", "via", "the", "provided", "network", "endpoint", "and", "under", "the", "provided", "identity", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L567-L609
train
google/netstack
tcpip/transport/tcp/connect.go
makeOptions
func (e *endpoint) makeOptions(sackBlocks []header.SACKBlock) []byte { options := getOptions() offset := 0 // N.B. the ordering here matches the ordering used by Linux internally // and described in the raw makeOptions function. We don't include // unnecessary cases here (post connection.) if e.sendTSOk { // Embed the timestamp if timestamp has been enabled. // // We only use the lower 32 bits of the unix time in // milliseconds. This is similar to what Linux does where it // uses the lower 32 bits of the jiffies value in the tsVal // field of the timestamp option. // // Further, RFC7323 section-5.4 recommends millisecond // resolution as the lowest recommended resolution for the // timestamp clock. // // Ref: https://tools.ietf.org/html/rfc7323#section-5.4. offset += header.EncodeNOP(options[offset:]) offset += header.EncodeNOP(options[offset:]) offset += header.EncodeTSOption(e.timestamp(), uint32(e.recentTS), options[offset:]) } if e.sackPermitted && len(sackBlocks) > 0 { offset += header.EncodeNOP(options[offset:]) offset += header.EncodeNOP(options[offset:]) offset += header.EncodeSACKBlocks(sackBlocks, options[offset:]) } // We expect the above to produce an aligned offset. if delta := header.AddTCPOptionPadding(options, offset); delta != 0 { panic("unexpected option encoding") } return options[:offset] }
go
func (e *endpoint) makeOptions(sackBlocks []header.SACKBlock) []byte { options := getOptions() offset := 0 // N.B. the ordering here matches the ordering used by Linux internally // and described in the raw makeOptions function. We don't include // unnecessary cases here (post connection.) if e.sendTSOk { // Embed the timestamp if timestamp has been enabled. // // We only use the lower 32 bits of the unix time in // milliseconds. This is similar to what Linux does where it // uses the lower 32 bits of the jiffies value in the tsVal // field of the timestamp option. // // Further, RFC7323 section-5.4 recommends millisecond // resolution as the lowest recommended resolution for the // timestamp clock. // // Ref: https://tools.ietf.org/html/rfc7323#section-5.4. offset += header.EncodeNOP(options[offset:]) offset += header.EncodeNOP(options[offset:]) offset += header.EncodeTSOption(e.timestamp(), uint32(e.recentTS), options[offset:]) } if e.sackPermitted && len(sackBlocks) > 0 { offset += header.EncodeNOP(options[offset:]) offset += header.EncodeNOP(options[offset:]) offset += header.EncodeSACKBlocks(sackBlocks, options[offset:]) } // We expect the above to produce an aligned offset. if delta := header.AddTCPOptionPadding(options, offset); delta != 0 { panic("unexpected option encoding") } return options[:offset] }
[ "func", "(", "e", "*", "endpoint", ")", "makeOptions", "(", "sackBlocks", "[", "]", "header", ".", "SACKBlock", ")", "[", "]", "byte", "{", "options", ":=", "getOptions", "(", ")", "\n", "offset", ":=", "0", "\n", "if", "e", ".", "sendTSOk", "{", "offset", "+=", "header", ".", "EncodeNOP", "(", "options", "[", "offset", ":", "]", ")", "\n", "offset", "+=", "header", ".", "EncodeNOP", "(", "options", "[", "offset", ":", "]", ")", "\n", "offset", "+=", "header", ".", "EncodeTSOption", "(", "e", ".", "timestamp", "(", ")", ",", "uint32", "(", "e", ".", "recentTS", ")", ",", "options", "[", "offset", ":", "]", ")", "\n", "}", "\n", "if", "e", ".", "sackPermitted", "&&", "len", "(", "sackBlocks", ")", ">", "0", "{", "offset", "+=", "header", ".", "EncodeNOP", "(", "options", "[", "offset", ":", "]", ")", "\n", "offset", "+=", "header", ".", "EncodeNOP", "(", "options", "[", "offset", ":", "]", ")", "\n", "offset", "+=", "header", ".", "EncodeSACKBlocks", "(", "sackBlocks", ",", "options", "[", "offset", ":", "]", ")", "\n", "}", "\n", "if", "delta", ":=", "header", ".", "AddTCPOptionPadding", "(", "options", ",", "offset", ")", ";", "delta", "!=", "0", "{", "panic", "(", "\"unexpected option encoding\"", ")", "\n", "}", "\n", "return", "options", "[", ":", "offset", "]", "\n", "}" ]
// makeOptions makes an options slice.
[ "makeOptions", "makes", "an", "options", "slice", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L612-L648
train
google/netstack
tcpip/transport/tcp/connect.go
sendRaw
func (e *endpoint) sendRaw(data buffer.VectorisedView, flags byte, seq, ack seqnum.Value, rcvWnd seqnum.Size) *tcpip.Error { var sackBlocks []header.SACKBlock if e.state == stateConnected && e.rcv.pendingBufSize > 0 && (flags&header.TCPFlagAck != 0) { sackBlocks = e.sack.Blocks[:e.sack.NumBlocks] } options := e.makeOptions(sackBlocks) err := sendTCP(&e.route, e.id, data, e.route.DefaultTTL(), flags, seq, ack, rcvWnd, options, e.gso) putOptions(options) return err }
go
func (e *endpoint) sendRaw(data buffer.VectorisedView, flags byte, seq, ack seqnum.Value, rcvWnd seqnum.Size) *tcpip.Error { var sackBlocks []header.SACKBlock if e.state == stateConnected && e.rcv.pendingBufSize > 0 && (flags&header.TCPFlagAck != 0) { sackBlocks = e.sack.Blocks[:e.sack.NumBlocks] } options := e.makeOptions(sackBlocks) err := sendTCP(&e.route, e.id, data, e.route.DefaultTTL(), flags, seq, ack, rcvWnd, options, e.gso) putOptions(options) return err }
[ "func", "(", "e", "*", "endpoint", ")", "sendRaw", "(", "data", "buffer", ".", "VectorisedView", ",", "flags", "byte", ",", "seq", ",", "ack", "seqnum", ".", "Value", ",", "rcvWnd", "seqnum", ".", "Size", ")", "*", "tcpip", ".", "Error", "{", "var", "sackBlocks", "[", "]", "header", ".", "SACKBlock", "\n", "if", "e", ".", "state", "==", "stateConnected", "&&", "e", ".", "rcv", ".", "pendingBufSize", ">", "0", "&&", "(", "flags", "&", "header", ".", "TCPFlagAck", "!=", "0", ")", "{", "sackBlocks", "=", "e", ".", "sack", ".", "Blocks", "[", ":", "e", ".", "sack", ".", "NumBlocks", "]", "\n", "}", "\n", "options", ":=", "e", ".", "makeOptions", "(", "sackBlocks", ")", "\n", "err", ":=", "sendTCP", "(", "&", "e", ".", "route", ",", "e", ".", "id", ",", "data", ",", "e", ".", "route", ".", "DefaultTTL", "(", ")", ",", "flags", ",", "seq", ",", "ack", ",", "rcvWnd", ",", "options", ",", "e", ".", "gso", ")", "\n", "putOptions", "(", "options", ")", "\n", "return", "err", "\n", "}" ]
// sendRaw sends a TCP segment to the endpoint's peer.
[ "sendRaw", "sends", "a", "TCP", "segment", "to", "the", "endpoint", "s", "peer", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L651-L660
train
google/netstack
tcpip/transport/tcp/connect.go
resetConnectionLocked
func (e *endpoint) resetConnectionLocked(err *tcpip.Error) { e.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck|header.TCPFlagRst, e.snd.sndUna, e.rcv.rcvNxt, 0) e.state = stateError e.hardError = err }
go
func (e *endpoint) resetConnectionLocked(err *tcpip.Error) { e.sendRaw(buffer.VectorisedView{}, header.TCPFlagAck|header.TCPFlagRst, e.snd.sndUna, e.rcv.rcvNxt, 0) e.state = stateError e.hardError = err }
[ "func", "(", "e", "*", "endpoint", ")", "resetConnectionLocked", "(", "err", "*", "tcpip", ".", "Error", ")", "{", "e", ".", "sendRaw", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagAck", "|", "header", ".", "TCPFlagRst", ",", "e", ".", "snd", ".", "sndUna", ",", "e", ".", "rcv", ".", "rcvNxt", ",", "0", ")", "\n", "e", ".", "state", "=", "stateError", "\n", "e", ".", "hardError", "=", "err", "\n", "}" ]
// resetConnectionLocked sends a RST segment and puts the endpoint in an error // state with the given error code. This method must only be called from the // protocol goroutine.
[ "resetConnectionLocked", "sends", "a", "RST", "segment", "and", "puts", "the", "endpoint", "in", "an", "error", "state", "with", "the", "given", "error", "code", ".", "This", "method", "must", "only", "be", "called", "from", "the", "protocol", "goroutine", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L702-L707
train
google/netstack
tcpip/transport/tcp/connect.go
handleSegments
func (e *endpoint) handleSegments() *tcpip.Error { checkRequeue := true for i := 0; i < maxSegmentsPerWake; i++ { s := e.segmentQueue.dequeue() if s == nil { checkRequeue = false break } // Invoke the tcp probe if installed. if e.probe != nil { e.probe(e.completeState()) } if s.flagIsSet(header.TCPFlagRst) { if e.rcv.acceptable(s.sequenceNumber, 0) { // RFC 793, page 37 states that "in all states // except SYN-SENT, all reset (RST) segments are // validated by checking their SEQ-fields." So // we only process it if it's acceptable. s.decRef() return tcpip.ErrConnectionReset } } else if s.flagIsSet(header.TCPFlagAck) { // Patch the window size in the segment according to the // send window scale. s.window <<= e.snd.sndWndScale // RFC 793, page 41 states that "once in the ESTABLISHED // state all segments must carry current acknowledgment // information." e.rcv.handleRcvdSegment(s) e.snd.handleRcvdSegment(s) } s.decRef() } // If the queue is not empty, make sure we'll wake up in the next // iteration. if checkRequeue && !e.segmentQueue.empty() { e.newSegmentWaker.Assert() } // Send an ACK for all processed packets if needed. if e.rcv.rcvNxt != e.snd.maxSentAck { e.snd.sendAck() } e.resetKeepaliveTimer(true) return nil }
go
func (e *endpoint) handleSegments() *tcpip.Error { checkRequeue := true for i := 0; i < maxSegmentsPerWake; i++ { s := e.segmentQueue.dequeue() if s == nil { checkRequeue = false break } // Invoke the tcp probe if installed. if e.probe != nil { e.probe(e.completeState()) } if s.flagIsSet(header.TCPFlagRst) { if e.rcv.acceptable(s.sequenceNumber, 0) { // RFC 793, page 37 states that "in all states // except SYN-SENT, all reset (RST) segments are // validated by checking their SEQ-fields." So // we only process it if it's acceptable. s.decRef() return tcpip.ErrConnectionReset } } else if s.flagIsSet(header.TCPFlagAck) { // Patch the window size in the segment according to the // send window scale. s.window <<= e.snd.sndWndScale // RFC 793, page 41 states that "once in the ESTABLISHED // state all segments must carry current acknowledgment // information." e.rcv.handleRcvdSegment(s) e.snd.handleRcvdSegment(s) } s.decRef() } // If the queue is not empty, make sure we'll wake up in the next // iteration. if checkRequeue && !e.segmentQueue.empty() { e.newSegmentWaker.Assert() } // Send an ACK for all processed packets if needed. if e.rcv.rcvNxt != e.snd.maxSentAck { e.snd.sendAck() } e.resetKeepaliveTimer(true) return nil }
[ "func", "(", "e", "*", "endpoint", ")", "handleSegments", "(", ")", "*", "tcpip", ".", "Error", "{", "checkRequeue", ":=", "true", "\n", "for", "i", ":=", "0", ";", "i", "<", "maxSegmentsPerWake", ";", "i", "++", "{", "s", ":=", "e", ".", "segmentQueue", ".", "dequeue", "(", ")", "\n", "if", "s", "==", "nil", "{", "checkRequeue", "=", "false", "\n", "break", "\n", "}", "\n", "if", "e", ".", "probe", "!=", "nil", "{", "e", ".", "probe", "(", "e", ".", "completeState", "(", ")", ")", "\n", "}", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagRst", ")", "{", "if", "e", ".", "rcv", ".", "acceptable", "(", "s", ".", "sequenceNumber", ",", "0", ")", "{", "s", ".", "decRef", "(", ")", "\n", "return", "tcpip", ".", "ErrConnectionReset", "\n", "}", "\n", "}", "else", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "{", "s", ".", "window", "<<=", "e", ".", "snd", ".", "sndWndScale", "\n", "e", ".", "rcv", ".", "handleRcvdSegment", "(", "s", ")", "\n", "e", ".", "snd", ".", "handleRcvdSegment", "(", "s", ")", "\n", "}", "\n", "s", ".", "decRef", "(", ")", "\n", "}", "\n", "if", "checkRequeue", "&&", "!", "e", ".", "segmentQueue", ".", "empty", "(", ")", "{", "e", ".", "newSegmentWaker", ".", "Assert", "(", ")", "\n", "}", "\n", "if", "e", ".", "rcv", ".", "rcvNxt", "!=", "e", ".", "snd", ".", "maxSentAck", "{", "e", ".", "snd", ".", "sendAck", "(", ")", "\n", "}", "\n", "e", ".", "resetKeepaliveTimer", "(", "true", ")", "\n", "return", "nil", "\n", "}" ]
// handleSegments pulls segments from the queue and processes them. It returns // no error if the protocol loop should continue, an error otherwise.
[ "handleSegments", "pulls", "segments", "from", "the", "queue", "and", "processes", "them", ".", "It", "returns", "no", "error", "if", "the", "protocol", "loop", "should", "continue", "an", "error", "otherwise", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L721-L772
train
google/netstack
tcpip/transport/tcp/connect.go
keepaliveTimerExpired
func (e *endpoint) keepaliveTimerExpired() *tcpip.Error { e.keepalive.Lock() if !e.keepalive.enabled || !e.keepalive.timer.checkExpiration() { e.keepalive.Unlock() return nil } if e.keepalive.unacked >= e.keepalive.count { e.keepalive.Unlock() return tcpip.ErrConnectionReset } // RFC1122 4.2.3.6: TCP keepalive is a dataless ACK with // seg.seq = snd.nxt-1. e.keepalive.unacked++ e.keepalive.Unlock() e.snd.sendSegment(buffer.VectorisedView{}, header.TCPFlagAck, e.snd.sndNxt-1) e.resetKeepaliveTimer(false) return nil }
go
func (e *endpoint) keepaliveTimerExpired() *tcpip.Error { e.keepalive.Lock() if !e.keepalive.enabled || !e.keepalive.timer.checkExpiration() { e.keepalive.Unlock() return nil } if e.keepalive.unacked >= e.keepalive.count { e.keepalive.Unlock() return tcpip.ErrConnectionReset } // RFC1122 4.2.3.6: TCP keepalive is a dataless ACK with // seg.seq = snd.nxt-1. e.keepalive.unacked++ e.keepalive.Unlock() e.snd.sendSegment(buffer.VectorisedView{}, header.TCPFlagAck, e.snd.sndNxt-1) e.resetKeepaliveTimer(false) return nil }
[ "func", "(", "e", "*", "endpoint", ")", "keepaliveTimerExpired", "(", ")", "*", "tcpip", ".", "Error", "{", "e", ".", "keepalive", ".", "Lock", "(", ")", "\n", "if", "!", "e", ".", "keepalive", ".", "enabled", "||", "!", "e", ".", "keepalive", ".", "timer", ".", "checkExpiration", "(", ")", "{", "e", ".", "keepalive", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "}", "\n", "if", "e", ".", "keepalive", ".", "unacked", ">=", "e", ".", "keepalive", ".", "count", "{", "e", ".", "keepalive", ".", "Unlock", "(", ")", "\n", "return", "tcpip", ".", "ErrConnectionReset", "\n", "}", "\n", "e", ".", "keepalive", ".", "unacked", "++", "\n", "e", ".", "keepalive", ".", "Unlock", "(", ")", "\n", "e", ".", "snd", ".", "sendSegment", "(", "buffer", ".", "VectorisedView", "{", "}", ",", "header", ".", "TCPFlagAck", ",", "e", ".", "snd", ".", "sndNxt", "-", "1", ")", "\n", "e", ".", "resetKeepaliveTimer", "(", "false", ")", "\n", "return", "nil", "\n", "}" ]
// keepaliveTimerExpired is called when the keepaliveTimer fires. We send TCP // keepalive packets periodically when the connection is idle. If we don't hear // from the other side after a number of tries, we terminate the connection.
[ "keepaliveTimerExpired", "is", "called", "when", "the", "keepaliveTimer", "fires", ".", "We", "send", "TCP", "keepalive", "packets", "periodically", "when", "the", "connection", "is", "idle", ".", "If", "we", "don", "t", "hear", "from", "the", "other", "side", "after", "a", "number", "of", "tries", "we", "terminate", "the", "connection", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L777-L796
train
google/netstack
tcpip/transport/tcp/connect.go
resetKeepaliveTimer
func (e *endpoint) resetKeepaliveTimer(receivedData bool) { e.keepalive.Lock() defer e.keepalive.Unlock() if receivedData { e.keepalive.unacked = 0 } // Start the keepalive timer IFF it's enabled and there is no pending // data to send. if !e.keepalive.enabled || e.snd == nil || e.snd.sndUna != e.snd.sndNxt { e.keepalive.timer.disable() return } if e.keepalive.unacked > 0 { e.keepalive.timer.enable(e.keepalive.interval) } else { e.keepalive.timer.enable(e.keepalive.idle) } }
go
func (e *endpoint) resetKeepaliveTimer(receivedData bool) { e.keepalive.Lock() defer e.keepalive.Unlock() if receivedData { e.keepalive.unacked = 0 } // Start the keepalive timer IFF it's enabled and there is no pending // data to send. if !e.keepalive.enabled || e.snd == nil || e.snd.sndUna != e.snd.sndNxt { e.keepalive.timer.disable() return } if e.keepalive.unacked > 0 { e.keepalive.timer.enable(e.keepalive.interval) } else { e.keepalive.timer.enable(e.keepalive.idle) } }
[ "func", "(", "e", "*", "endpoint", ")", "resetKeepaliveTimer", "(", "receivedData", "bool", ")", "{", "e", ".", "keepalive", ".", "Lock", "(", ")", "\n", "defer", "e", ".", "keepalive", ".", "Unlock", "(", ")", "\n", "if", "receivedData", "{", "e", ".", "keepalive", ".", "unacked", "=", "0", "\n", "}", "\n", "if", "!", "e", ".", "keepalive", ".", "enabled", "||", "e", ".", "snd", "==", "nil", "||", "e", ".", "snd", ".", "sndUna", "!=", "e", ".", "snd", ".", "sndNxt", "{", "e", ".", "keepalive", ".", "timer", ".", "disable", "(", ")", "\n", "return", "\n", "}", "\n", "if", "e", ".", "keepalive", ".", "unacked", ">", "0", "{", "e", ".", "keepalive", ".", "timer", ".", "enable", "(", "e", ".", "keepalive", ".", "interval", ")", "\n", "}", "else", "{", "e", ".", "keepalive", ".", "timer", ".", "enable", "(", "e", ".", "keepalive", ".", "idle", ")", "\n", "}", "\n", "}" ]
// resetKeepaliveTimer restarts or stops the keepalive timer, depending on // whether it is enabled for this endpoint.
[ "resetKeepaliveTimer", "restarts", "or", "stops", "the", "keepalive", "timer", "depending", "on", "whether", "it", "is", "enabled", "for", "this", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L800-L817
train
google/netstack
tcpip/transport/tcp/connect.go
disableKeepaliveTimer
func (e *endpoint) disableKeepaliveTimer() { e.keepalive.Lock() e.keepalive.timer.disable() e.keepalive.Unlock() }
go
func (e *endpoint) disableKeepaliveTimer() { e.keepalive.Lock() e.keepalive.timer.disable() e.keepalive.Unlock() }
[ "func", "(", "e", "*", "endpoint", ")", "disableKeepaliveTimer", "(", ")", "{", "e", ".", "keepalive", ".", "Lock", "(", ")", "\n", "e", ".", "keepalive", ".", "timer", ".", "disable", "(", ")", "\n", "e", ".", "keepalive", ".", "Unlock", "(", ")", "\n", "}" ]
// disableKeepaliveTimer stops the keepalive timer.
[ "disableKeepaliveTimer", "stops", "the", "keepalive", "timer", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/connect.go#L820-L824
train
google/netstack
tcpip/header/ipv6_fragment.go
Encode
func (b IPv6Fragment) Encode(i *IPv6FragmentFields) { b[nextHdrFrag] = i.NextHeader binary.BigEndian.PutUint16(b[fragOff:], i.FragmentOffset<<3) if i.M { b[more] |= 1 } binary.BigEndian.PutUint32(b[idV6:], i.Identification) }
go
func (b IPv6Fragment) Encode(i *IPv6FragmentFields) { b[nextHdrFrag] = i.NextHeader binary.BigEndian.PutUint16(b[fragOff:], i.FragmentOffset<<3) if i.M { b[more] |= 1 } binary.BigEndian.PutUint32(b[idV6:], i.Identification) }
[ "func", "(", "b", "IPv6Fragment", ")", "Encode", "(", "i", "*", "IPv6FragmentFields", ")", "{", "b", "[", "nextHdrFrag", "]", "=", "i", ".", "NextHeader", "\n", "binary", ".", "BigEndian", ".", "PutUint16", "(", "b", "[", "fragOff", ":", "]", ",", "i", ".", "FragmentOffset", "<<", "3", ")", "\n", "if", "i", ".", "M", "{", "b", "[", "more", "]", "|=", "1", "\n", "}", "\n", "binary", ".", "BigEndian", ".", "PutUint32", "(", "b", "[", "idV6", ":", "]", ",", "i", ".", "Identification", ")", "\n", "}" ]
// Encode encodes all the fields of the ipv6 fragment.
[ "Encode", "encodes", "all", "the", "fields", "of", "the", "ipv6", "fragment", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/ipv6_fragment.go#L62-L69
train
google/netstack
tcpip/link/fdbased/mmap_amd64_unsafe.go
tpStatus
func (t tPacketHdr) tpStatus() uint32 { hdr := unsafe.Pointer(&t[0]) statusPtr := unsafe.Pointer(uintptr(hdr) + uintptr(tpStatusOffset)) return atomic.LoadUint32((*uint32)(statusPtr)) }
go
func (t tPacketHdr) tpStatus() uint32 { hdr := unsafe.Pointer(&t[0]) statusPtr := unsafe.Pointer(uintptr(hdr) + uintptr(tpStatusOffset)) return atomic.LoadUint32((*uint32)(statusPtr)) }
[ "func", "(", "t", "tPacketHdr", ")", "tpStatus", "(", ")", "uint32", "{", "hdr", ":=", "unsafe", ".", "Pointer", "(", "&", "t", "[", "0", "]", ")", "\n", "statusPtr", ":=", "unsafe", ".", "Pointer", "(", "uintptr", "(", "hdr", ")", "+", "uintptr", "(", "tpStatusOffset", ")", ")", "\n", "return", "atomic", ".", "LoadUint32", "(", "(", "*", "uint32", ")", "(", "statusPtr", ")", ")", "\n", "}" ]
// tpStatus returns the frame status field. // The status is concurrently updated by the kernel as a result we must // use atomic operations to prevent races.
[ "tpStatus", "returns", "the", "frame", "status", "field", ".", "The", "status", "is", "concurrently", "updated", "by", "the", "kernel", "as", "a", "result", "we", "must", "use", "atomic", "operations", "to", "prevent", "races", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/fdbased/mmap_amd64_unsafe.go#L93-L97
train
google/netstack
tcpip/link/fdbased/mmap_amd64_unsafe.go
setTPStatus
func (t tPacketHdr) setTPStatus(status uint32) { hdr := unsafe.Pointer(&t[0]) statusPtr := unsafe.Pointer(uintptr(hdr) + uintptr(tpStatusOffset)) atomic.StoreUint32((*uint32)(statusPtr), status) }
go
func (t tPacketHdr) setTPStatus(status uint32) { hdr := unsafe.Pointer(&t[0]) statusPtr := unsafe.Pointer(uintptr(hdr) + uintptr(tpStatusOffset)) atomic.StoreUint32((*uint32)(statusPtr), status) }
[ "func", "(", "t", "tPacketHdr", ")", "setTPStatus", "(", "status", "uint32", ")", "{", "hdr", ":=", "unsafe", ".", "Pointer", "(", "&", "t", "[", "0", "]", ")", "\n", "statusPtr", ":=", "unsafe", ".", "Pointer", "(", "uintptr", "(", "hdr", ")", "+", "uintptr", "(", "tpStatusOffset", ")", ")", "\n", "atomic", ".", "StoreUint32", "(", "(", "*", "uint32", ")", "(", "statusPtr", ")", ",", "status", ")", "\n", "}" ]
// setTPStatus set's the frame status to the provided status. // The status is concurrently updated by the kernel as a result we must // use atomic operations to prevent races.
[ "setTPStatus", "set", "s", "the", "frame", "status", "to", "the", "provided", "status", ".", "The", "status", "is", "concurrently", "updated", "by", "the", "kernel", "as", "a", "result", "we", "must", "use", "atomic", "operations", "to", "prevent", "races", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/fdbased/mmap_amd64_unsafe.go#L102-L106
train
google/netstack
tcpip/link/fdbased/mmap_amd64_unsafe.go
packetMMapDispatch
func (e *endpoint) packetMMapDispatch() (bool, *tcpip.Error) { pkt, err := e.readMMappedPacket() if err != nil { return false, err } var ( p tcpip.NetworkProtocolNumber remote, local tcpip.LinkAddress ) if e.hdrSize > 0 { eth := header.Ethernet(pkt) p = eth.Type() remote = eth.SourceAddress() local = eth.DestinationAddress() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. switch header.IPVersion(pkt) { case header.IPv4Version: p = header.IPv4ProtocolNumber case header.IPv6Version: p = header.IPv6ProtocolNumber default: return true, nil } } pkt = pkt[e.hdrSize:] e.dispatcher.DeliverNetworkPacket(e, remote, local, p, buffer.NewVectorisedView(len(pkt), []buffer.View{buffer.View(pkt)})) return true, nil }
go
func (e *endpoint) packetMMapDispatch() (bool, *tcpip.Error) { pkt, err := e.readMMappedPacket() if err != nil { return false, err } var ( p tcpip.NetworkProtocolNumber remote, local tcpip.LinkAddress ) if e.hdrSize > 0 { eth := header.Ethernet(pkt) p = eth.Type() remote = eth.SourceAddress() local = eth.DestinationAddress() } else { // We don't get any indication of what the packet is, so try to guess // if it's an IPv4 or IPv6 packet. switch header.IPVersion(pkt) { case header.IPv4Version: p = header.IPv4ProtocolNumber case header.IPv6Version: p = header.IPv6ProtocolNumber default: return true, nil } } pkt = pkt[e.hdrSize:] e.dispatcher.DeliverNetworkPacket(e, remote, local, p, buffer.NewVectorisedView(len(pkt), []buffer.View{buffer.View(pkt)})) return true, nil }
[ "func", "(", "e", "*", "endpoint", ")", "packetMMapDispatch", "(", ")", "(", "bool", ",", "*", "tcpip", ".", "Error", ")", "{", "pkt", ",", "err", ":=", "e", ".", "readMMappedPacket", "(", ")", "\n", "if", "err", "!=", "nil", "{", "return", "false", ",", "err", "\n", "}", "\n", "var", "(", "p", "tcpip", ".", "NetworkProtocolNumber", "\n", "remote", ",", "local", "tcpip", ".", "LinkAddress", "\n", ")", "\n", "if", "e", ".", "hdrSize", ">", "0", "{", "eth", ":=", "header", ".", "Ethernet", "(", "pkt", ")", "\n", "p", "=", "eth", ".", "Type", "(", ")", "\n", "remote", "=", "eth", ".", "SourceAddress", "(", ")", "\n", "local", "=", "eth", ".", "DestinationAddress", "(", ")", "\n", "}", "else", "{", "switch", "header", ".", "IPVersion", "(", "pkt", ")", "{", "case", "header", ".", "IPv4Version", ":", "p", "=", "header", ".", "IPv4ProtocolNumber", "\n", "case", "header", ".", "IPv6Version", ":", "p", "=", "header", ".", "IPv6ProtocolNumber", "\n", "default", ":", "return", "true", ",", "nil", "\n", "}", "\n", "}", "\n", "pkt", "=", "pkt", "[", "e", ".", "hdrSize", ":", "]", "\n", "e", ".", "dispatcher", ".", "DeliverNetworkPacket", "(", "e", ",", "remote", ",", "local", ",", "p", ",", "buffer", ".", "NewVectorisedView", "(", "len", "(", "pkt", ")", ",", "[", "]", "buffer", ".", "View", "{", "buffer", ".", "View", "(", "pkt", ")", "}", ")", ")", "\n", "return", "true", ",", "nil", "\n", "}" ]
// packetMMapDispatch reads packets from an mmaped ring buffer and dispatches // them to the network stack.
[ "packetMMapDispatch", "reads", "packets", "from", "an", "mmaped", "ring", "buffer", "and", "dispatches", "them", "to", "the", "network", "stack", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/fdbased/mmap_amd64_unsafe.go#L196-L226
train
google/netstack
tcpip/header/checksum.go
ChecksumCombine
func ChecksumCombine(a, b uint16) uint16 { v := uint32(a) + uint32(b) return uint16(v + v>>16) }
go
func ChecksumCombine(a, b uint16) uint16 { v := uint32(a) + uint32(b) return uint16(v + v>>16) }
[ "func", "ChecksumCombine", "(", "a", ",", "b", "uint16", ")", "uint16", "{", "v", ":=", "uint32", "(", "a", ")", "+", "uint32", "(", "b", ")", "\n", "return", "uint16", "(", "v", "+", "v", ">>", "16", ")", "\n", "}" ]
// ChecksumCombine combines the two uint16 to form their checksum. This is done // by adding them and the carry. // // Note that checksum a must have been computed on an even number of bytes.
[ "ChecksumCombine", "combines", "the", "two", "uint16", "to", "form", "their", "checksum", ".", "This", "is", "done", "by", "adding", "them", "and", "the", "carry", ".", "Note", "that", "checksum", "a", "must", "have", "been", "computed", "on", "an", "even", "number", "of", "bytes", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/checksum.go#L76-L79
train
google/netstack
tcpip/header/checksum.go
PseudoHeaderChecksum
func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr tcpip.Address, dstAddr tcpip.Address, totalLen uint16) uint16 { xsum := Checksum([]byte(srcAddr), 0) xsum = Checksum([]byte(dstAddr), xsum) // Add the length portion of the checksum to the pseudo-checksum. tmp := make([]byte, 2) binary.BigEndian.PutUint16(tmp, totalLen) xsum = Checksum(tmp, xsum) return Checksum([]byte{0, uint8(protocol)}, xsum) }
go
func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr tcpip.Address, dstAddr tcpip.Address, totalLen uint16) uint16 { xsum := Checksum([]byte(srcAddr), 0) xsum = Checksum([]byte(dstAddr), xsum) // Add the length portion of the checksum to the pseudo-checksum. tmp := make([]byte, 2) binary.BigEndian.PutUint16(tmp, totalLen) xsum = Checksum(tmp, xsum) return Checksum([]byte{0, uint8(protocol)}, xsum) }
[ "func", "PseudoHeaderChecksum", "(", "protocol", "tcpip", ".", "TransportProtocolNumber", ",", "srcAddr", "tcpip", ".", "Address", ",", "dstAddr", "tcpip", ".", "Address", ",", "totalLen", "uint16", ")", "uint16", "{", "xsum", ":=", "Checksum", "(", "[", "]", "byte", "(", "srcAddr", ")", ",", "0", ")", "\n", "xsum", "=", "Checksum", "(", "[", "]", "byte", "(", "dstAddr", ")", ",", "xsum", ")", "\n", "tmp", ":=", "make", "(", "[", "]", "byte", ",", "2", ")", "\n", "binary", ".", "BigEndian", ".", "PutUint16", "(", "tmp", ",", "totalLen", ")", "\n", "xsum", "=", "Checksum", "(", "tmp", ",", "xsum", ")", "\n", "return", "Checksum", "(", "[", "]", "byte", "{", "0", ",", "uint8", "(", "protocol", ")", "}", ",", "xsum", ")", "\n", "}" ]
// PseudoHeaderChecksum calculates the pseudo-header checksum for the given // destination protocol and network address. Pseudo-headers are needed by // transport layers when calculating their own checksum.
[ "PseudoHeaderChecksum", "calculates", "the", "pseudo", "-", "header", "checksum", "for", "the", "given", "destination", "protocol", "and", "network", "address", ".", "Pseudo", "-", "headers", "are", "needed", "by", "transport", "layers", "when", "calculating", "their", "own", "checksum", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/header/checksum.go#L84-L94
train
google/netstack
tcpip/link/sharedmem/queue/rx.go
PostBuffers
func (r *Rx) PostBuffers(buffers []RxBuffer) bool { for i := range buffers { b := r.tx.Push(sizeOfPostedBuffer) if b == nil { r.tx.Abort() return false } pb := &buffers[i] binary.LittleEndian.PutUint64(b[postedOffset:], pb.Offset) binary.LittleEndian.PutUint32(b[postedSize:], pb.Size) binary.LittleEndian.PutUint32(b[postedRemainingInGroup:], 0) binary.LittleEndian.PutUint64(b[postedUserData:], pb.UserData) binary.LittleEndian.PutUint64(b[postedID:], pb.ID) } r.tx.Flush() return true }
go
func (r *Rx) PostBuffers(buffers []RxBuffer) bool { for i := range buffers { b := r.tx.Push(sizeOfPostedBuffer) if b == nil { r.tx.Abort() return false } pb := &buffers[i] binary.LittleEndian.PutUint64(b[postedOffset:], pb.Offset) binary.LittleEndian.PutUint32(b[postedSize:], pb.Size) binary.LittleEndian.PutUint32(b[postedRemainingInGroup:], 0) binary.LittleEndian.PutUint64(b[postedUserData:], pb.UserData) binary.LittleEndian.PutUint64(b[postedID:], pb.ID) } r.tx.Flush() return true }
[ "func", "(", "r", "*", "Rx", ")", "PostBuffers", "(", "buffers", "[", "]", "RxBuffer", ")", "bool", "{", "for", "i", ":=", "range", "buffers", "{", "b", ":=", "r", ".", "tx", ".", "Push", "(", "sizeOfPostedBuffer", ")", "\n", "if", "b", "==", "nil", "{", "r", ".", "tx", ".", "Abort", "(", ")", "\n", "return", "false", "\n", "}", "\n", "pb", ":=", "&", "buffers", "[", "i", "]", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "postedOffset", ":", "]", ",", "pb", ".", "Offset", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "b", "[", "postedSize", ":", "]", ",", "pb", ".", "Size", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "b", "[", "postedRemainingInGroup", ":", "]", ",", "0", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "postedUserData", ":", "]", ",", "pb", ".", "UserData", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "postedID", ":", "]", ",", "pb", ".", "ID", ")", "\n", "}", "\n", "r", ".", "tx", ".", "Flush", "(", ")", "\n", "return", "true", "\n", "}" ]
// PostBuffers makes the given buffers available for receiving data from the // peer. Once they are posted, the peer is free to write to them and will // eventually post them back for consumption.
[ "PostBuffers", "makes", "the", "given", "buffers", "available", "for", "receiving", "data", "from", "the", "peer", ".", "Once", "they", "are", "posted", "the", "peer", "is", "free", "to", "write", "to", "them", "and", "will", "eventually", "post", "them", "back", "for", "consumption", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/queue/rx.go#L105-L124
train
google/netstack
tcpip/link/sharedmem/queue/rx.go
DecodeRxBufferHeader
func DecodeRxBufferHeader(b []byte) RxBuffer { return RxBuffer{ Offset: binary.LittleEndian.Uint64(b[postedOffset:]), Size: binary.LittleEndian.Uint32(b[postedSize:]), ID: binary.LittleEndian.Uint64(b[postedID:]), UserData: binary.LittleEndian.Uint64(b[postedUserData:]), } }
go
func DecodeRxBufferHeader(b []byte) RxBuffer { return RxBuffer{ Offset: binary.LittleEndian.Uint64(b[postedOffset:]), Size: binary.LittleEndian.Uint32(b[postedSize:]), ID: binary.LittleEndian.Uint64(b[postedID:]), UserData: binary.LittleEndian.Uint64(b[postedUserData:]), } }
[ "func", "DecodeRxBufferHeader", "(", "b", "[", "]", "byte", ")", "RxBuffer", "{", "return", "RxBuffer", "{", "Offset", ":", "binary", ".", "LittleEndian", ".", "Uint64", "(", "b", "[", "postedOffset", ":", "]", ")", ",", "Size", ":", "binary", ".", "LittleEndian", ".", "Uint32", "(", "b", "[", "postedSize", ":", "]", ")", ",", "ID", ":", "binary", ".", "LittleEndian", ".", "Uint64", "(", "b", "[", "postedID", ":", "]", ")", ",", "UserData", ":", "binary", ".", "LittleEndian", ".", "Uint64", "(", "b", "[", "postedUserData", ":", "]", ")", ",", "}", "\n", "}" ]
// DecodeRxBufferHeader decodes the header of a buffer posted on an rx queue.
[ "DecodeRxBufferHeader", "decodes", "the", "header", "of", "a", "buffer", "posted", "on", "an", "rx", "queue", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/queue/rx.go#L193-L200
train
google/netstack
tcpip/link/sharedmem/queue/rx.go
EncodeRxCompletion
func EncodeRxCompletion(b []byte, size, reserved uint32) { binary.LittleEndian.PutUint32(b[consumedPacketSize:], size) binary.LittleEndian.PutUint32(b[consumedPacketReserved:], reserved) }
go
func EncodeRxCompletion(b []byte, size, reserved uint32) { binary.LittleEndian.PutUint32(b[consumedPacketSize:], size) binary.LittleEndian.PutUint32(b[consumedPacketReserved:], reserved) }
[ "func", "EncodeRxCompletion", "(", "b", "[", "]", "byte", ",", "size", ",", "reserved", "uint32", ")", "{", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "b", "[", "consumedPacketSize", ":", "]", ",", "size", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "b", "[", "consumedPacketReserved", ":", "]", ",", "reserved", ")", "\n", "}" ]
// EncodeRxCompletion encodes an rx completion header.
[ "EncodeRxCompletion", "encodes", "an", "rx", "completion", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/queue/rx.go#L209-L212
train
google/netstack
tcpip/link/sharedmem/queue/rx.go
EncodeRxCompletionBuffer
func EncodeRxCompletionBuffer(b []byte, i int, rxb RxBuffer) { b = b[RxCompletionSize(i):] binary.LittleEndian.PutUint64(b[consumedOffset:], rxb.Offset) binary.LittleEndian.PutUint32(b[consumedSize:], rxb.Size) binary.LittleEndian.PutUint64(b[consumedUserData:], rxb.UserData) binary.LittleEndian.PutUint64(b[consumedID:], rxb.ID) }
go
func EncodeRxCompletionBuffer(b []byte, i int, rxb RxBuffer) { b = b[RxCompletionSize(i):] binary.LittleEndian.PutUint64(b[consumedOffset:], rxb.Offset) binary.LittleEndian.PutUint32(b[consumedSize:], rxb.Size) binary.LittleEndian.PutUint64(b[consumedUserData:], rxb.UserData) binary.LittleEndian.PutUint64(b[consumedID:], rxb.ID) }
[ "func", "EncodeRxCompletionBuffer", "(", "b", "[", "]", "byte", ",", "i", "int", ",", "rxb", "RxBuffer", ")", "{", "b", "=", "b", "[", "RxCompletionSize", "(", "i", ")", ":", "]", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "consumedOffset", ":", "]", ",", "rxb", ".", "Offset", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint32", "(", "b", "[", "consumedSize", ":", "]", ",", "rxb", ".", "Size", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "consumedUserData", ":", "]", ",", "rxb", ".", "UserData", ")", "\n", "binary", ".", "LittleEndian", ".", "PutUint64", "(", "b", "[", "consumedID", ":", "]", ",", "rxb", ".", "ID", ")", "\n", "}" ]
// EncodeRxCompletionBuffer encodes the i-th rx completion buffer header.
[ "EncodeRxCompletionBuffer", "encodes", "the", "i", "-", "th", "rx", "completion", "buffer", "header", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/link/sharedmem/queue/rx.go#L215-L221
train
google/netstack
tcpip/transport/tcp/protocol.go
NewEndpoint
func (*protocol) NewEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return newEndpoint(stack, netProto, waiterQueue), nil }
go
func (*protocol) NewEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return newEndpoint(stack, netProto, waiterQueue), nil }
[ "func", "(", "*", "protocol", ")", "NewEndpoint", "(", "stack", "*", "stack", ".", "Stack", ",", "netProto", "tcpip", ".", "NetworkProtocolNumber", ",", "waiterQueue", "*", "waiter", ".", "Queue", ")", "(", "tcpip", ".", "Endpoint", ",", "*", "tcpip", ".", "Error", ")", "{", "return", "newEndpoint", "(", "stack", ",", "netProto", ",", "waiterQueue", ")", ",", "nil", "\n", "}" ]
// NewEndpoint creates a new tcp endpoint.
[ "NewEndpoint", "creates", "a", "new", "tcp", "endpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/protocol.go#L100-L102
train
google/netstack
tcpip/transport/tcp/protocol.go
NewRawEndpoint
func (p *protocol) NewRawEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return nil, tcpip.ErrUnknownProtocol }
go
func (p *protocol) NewRawEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) { return nil, tcpip.ErrUnknownProtocol }
[ "func", "(", "p", "*", "protocol", ")", "NewRawEndpoint", "(", "stack", "*", "stack", ".", "Stack", ",", "netProto", "tcpip", ".", "NetworkProtocolNumber", ",", "waiterQueue", "*", "waiter", ".", "Queue", ")", "(", "tcpip", ".", "Endpoint", ",", "*", "tcpip", ".", "Error", ")", "{", "return", "nil", ",", "tcpip", ".", "ErrUnknownProtocol", "\n", "}" ]
// NewRawEndpoint creates a new raw TCP endpoint. Raw TCP sockets are currently // unsupported. It implements stack.TransportProtocol.NewRawEndpoint.
[ "NewRawEndpoint", "creates", "a", "new", "raw", "TCP", "endpoint", ".", "Raw", "TCP", "sockets", "are", "currently", "unsupported", ".", "It", "implements", "stack", ".", "TransportProtocol", ".", "NewRawEndpoint", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/protocol.go#L106-L108
train
google/netstack
tcpip/transport/tcp/protocol.go
replyWithReset
func replyWithReset(s *segment) { // Get the seqnum from the packet if the ack flag is set. seq := seqnum.Value(0) if s.flagIsSet(header.TCPFlagAck) { seq = s.ackNumber } ack := s.sequenceNumber.Add(s.logicalLen()) sendTCP(&s.route, s.id, buffer.VectorisedView{}, s.route.DefaultTTL(), header.TCPFlagRst|header.TCPFlagAck, seq, ack, 0, nil /* options */, nil /* gso */) }
go
func replyWithReset(s *segment) { // Get the seqnum from the packet if the ack flag is set. seq := seqnum.Value(0) if s.flagIsSet(header.TCPFlagAck) { seq = s.ackNumber } ack := s.sequenceNumber.Add(s.logicalLen()) sendTCP(&s.route, s.id, buffer.VectorisedView{}, s.route.DefaultTTL(), header.TCPFlagRst|header.TCPFlagAck, seq, ack, 0, nil /* options */, nil /* gso */) }
[ "func", "replyWithReset", "(", "s", "*", "segment", ")", "{", "seq", ":=", "seqnum", ".", "Value", "(", "0", ")", "\n", "if", "s", ".", "flagIsSet", "(", "header", ".", "TCPFlagAck", ")", "{", "seq", "=", "s", ".", "ackNumber", "\n", "}", "\n", "ack", ":=", "s", ".", "sequenceNumber", ".", "Add", "(", "s", ".", "logicalLen", "(", ")", ")", "\n", "sendTCP", "(", "&", "s", ".", "route", ",", "s", ".", "id", ",", "buffer", ".", "VectorisedView", "{", "}", ",", "s", ".", "route", ".", "DefaultTTL", "(", ")", ",", "header", ".", "TCPFlagRst", "|", "header", ".", "TCPFlagAck", ",", "seq", ",", "ack", ",", "0", ",", "nil", ",", "nil", ")", "\n", "}" ]
// replyWithReset replies to the given segment with a reset segment.
[ "replyWithReset", "replies", "to", "the", "given", "segment", "with", "a", "reset", "segment", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/protocol.go#L147-L157
train
google/netstack
tcpip/transport/tcp/protocol.go
SetOption
func (p *protocol) SetOption(option interface{}) *tcpip.Error { switch v := option.(type) { case SACKEnabled: p.mu.Lock() p.sackEnabled = bool(v) p.mu.Unlock() return nil case SendBufferSizeOption: if v.Min <= 0 || v.Default < v.Min || v.Default > v.Max { return tcpip.ErrInvalidOptionValue } p.mu.Lock() p.sendBufferSize = v p.mu.Unlock() return nil case ReceiveBufferSizeOption: if v.Min <= 0 || v.Default < v.Min || v.Default > v.Max { return tcpip.ErrInvalidOptionValue } p.mu.Lock() p.recvBufferSize = v p.mu.Unlock() return nil case CongestionControlOption: for _, c := range p.availableCongestionControl { if string(v) == c { p.mu.Lock() p.congestionControl = string(v) p.mu.Unlock() return nil } } return tcpip.ErrInvalidOptionValue default: return tcpip.ErrUnknownProtocolOption } }
go
func (p *protocol) SetOption(option interface{}) *tcpip.Error { switch v := option.(type) { case SACKEnabled: p.mu.Lock() p.sackEnabled = bool(v) p.mu.Unlock() return nil case SendBufferSizeOption: if v.Min <= 0 || v.Default < v.Min || v.Default > v.Max { return tcpip.ErrInvalidOptionValue } p.mu.Lock() p.sendBufferSize = v p.mu.Unlock() return nil case ReceiveBufferSizeOption: if v.Min <= 0 || v.Default < v.Min || v.Default > v.Max { return tcpip.ErrInvalidOptionValue } p.mu.Lock() p.recvBufferSize = v p.mu.Unlock() return nil case CongestionControlOption: for _, c := range p.availableCongestionControl { if string(v) == c { p.mu.Lock() p.congestionControl = string(v) p.mu.Unlock() return nil } } return tcpip.ErrInvalidOptionValue default: return tcpip.ErrUnknownProtocolOption } }
[ "func", "(", "p", "*", "protocol", ")", "SetOption", "(", "option", "interface", "{", "}", ")", "*", "tcpip", ".", "Error", "{", "switch", "v", ":=", "option", ".", "(", "type", ")", "{", "case", "SACKEnabled", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "p", ".", "sackEnabled", "=", "bool", "(", "v", ")", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "SendBufferSizeOption", ":", "if", "v", ".", "Min", "<=", "0", "||", "v", ".", "Default", "<", "v", ".", "Min", "||", "v", ".", "Default", ">", "v", ".", "Max", "{", "return", "tcpip", ".", "ErrInvalidOptionValue", "\n", "}", "\n", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "p", ".", "sendBufferSize", "=", "v", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "ReceiveBufferSizeOption", ":", "if", "v", ".", "Min", "<=", "0", "||", "v", ".", "Default", "<", "v", ".", "Min", "||", "v", ".", "Default", ">", "v", ".", "Max", "{", "return", "tcpip", ".", "ErrInvalidOptionValue", "\n", "}", "\n", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "p", ".", "recvBufferSize", "=", "v", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "CongestionControlOption", ":", "for", "_", ",", "c", ":=", "range", "p", ".", "availableCongestionControl", "{", "if", "string", "(", "v", ")", "==", "c", "{", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "p", ".", "congestionControl", "=", "string", "(", "v", ")", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "}", "\n", "}", "\n", "return", "tcpip", ".", "ErrInvalidOptionValue", "\n", "default", ":", "return", "tcpip", ".", "ErrUnknownProtocolOption", "\n", "}", "\n", "}" ]
// SetOption implements TransportProtocol.SetOption.
[ "SetOption", "implements", "TransportProtocol", ".", "SetOption", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/protocol.go#L160-L199
train
google/netstack
tcpip/transport/tcp/protocol.go
Option
func (p *protocol) Option(option interface{}) *tcpip.Error { switch v := option.(type) { case *SACKEnabled: p.mu.Lock() *v = SACKEnabled(p.sackEnabled) p.mu.Unlock() return nil case *SendBufferSizeOption: p.mu.Lock() *v = p.sendBufferSize p.mu.Unlock() return nil case *ReceiveBufferSizeOption: p.mu.Lock() *v = p.recvBufferSize p.mu.Unlock() return nil case *CongestionControlOption: p.mu.Lock() *v = CongestionControlOption(p.congestionControl) p.mu.Unlock() return nil case *AvailableCongestionControlOption: p.mu.Lock() *v = AvailableCongestionControlOption(strings.Join(p.availableCongestionControl, " ")) p.mu.Unlock() return nil default: return tcpip.ErrUnknownProtocolOption } }
go
func (p *protocol) Option(option interface{}) *tcpip.Error { switch v := option.(type) { case *SACKEnabled: p.mu.Lock() *v = SACKEnabled(p.sackEnabled) p.mu.Unlock() return nil case *SendBufferSizeOption: p.mu.Lock() *v = p.sendBufferSize p.mu.Unlock() return nil case *ReceiveBufferSizeOption: p.mu.Lock() *v = p.recvBufferSize p.mu.Unlock() return nil case *CongestionControlOption: p.mu.Lock() *v = CongestionControlOption(p.congestionControl) p.mu.Unlock() return nil case *AvailableCongestionControlOption: p.mu.Lock() *v = AvailableCongestionControlOption(strings.Join(p.availableCongestionControl, " ")) p.mu.Unlock() return nil default: return tcpip.ErrUnknownProtocolOption } }
[ "func", "(", "p", "*", "protocol", ")", "Option", "(", "option", "interface", "{", "}", ")", "*", "tcpip", ".", "Error", "{", "switch", "v", ":=", "option", ".", "(", "type", ")", "{", "case", "*", "SACKEnabled", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "*", "v", "=", "SACKEnabled", "(", "p", ".", "sackEnabled", ")", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "*", "SendBufferSizeOption", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "*", "v", "=", "p", ".", "sendBufferSize", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "*", "ReceiveBufferSizeOption", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "*", "v", "=", "p", ".", "recvBufferSize", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "*", "CongestionControlOption", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "*", "v", "=", "CongestionControlOption", "(", "p", ".", "congestionControl", ")", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "case", "*", "AvailableCongestionControlOption", ":", "p", ".", "mu", ".", "Lock", "(", ")", "\n", "*", "v", "=", "AvailableCongestionControlOption", "(", "strings", ".", "Join", "(", "p", ".", "availableCongestionControl", ",", "\" \"", ")", ")", "\n", "p", ".", "mu", ".", "Unlock", "(", ")", "\n", "return", "nil", "\n", "default", ":", "return", "tcpip", ".", "ErrUnknownProtocolOption", "\n", "}", "\n", "}" ]
// Option implements TransportProtocol.Option.
[ "Option", "implements", "TransportProtocol", ".", "Option", "." ]
70ebca9c30730cf3887cdef3b85bb96ed7a97593
https://github.com/google/netstack/blob/70ebca9c30730cf3887cdef3b85bb96ed7a97593/tcpip/transport/tcp/protocol.go#L202-L234
train