Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/newtools/zsocket
Zero-copy sockets for Linux in Golang
https://github.com/newtools/zsocket
ethernet golang linux network-programming software-defined-network tcp zsocket
Last synced: about 2 months ago
JSON representation
Zero-copy sockets for Linux in Golang
- Host: GitHub
- URL: https://github.com/newtools/zsocket
- Owner: newtools
- License: mit
- Created: 2016-05-24T03:49:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-07-07T14:07:10.000Z (over 4 years ago)
- Last Synced: 2024-08-13T20:17:09.874Z (4 months ago)
- Topics: ethernet, golang, linux, network-programming, software-defined-network, tcp, zsocket
- Language: Go
- Homepage:
- Size: 90.8 KB
- Stars: 826
- Watchers: 30
- Forks: 73
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
- awesome-list - zsocket - copy sockets for Linux in Golang | newtools | 790 | (Go)
README
# ZSocket
ZSocket is a library that wraps the linux zero-copy socket syscall to create a ring buffer in a memory mapped file.
It also contains some utility functions and types to help with a handful of layer 2, 3, and 4 types.
It is a lot like libcap, except it has easy to understand facilities for writing (injecting packets) to an interface.ZSocket doesn't contain or wrap any C/C++, and it is lock free and thread safe.
The following program prints out all know layer types to ZSocket on a given interface:
```go
package mainimport (
"fmt""github.com/newtools/zsocket"
"github.com/newtools/zsocket/nettypes"
)func main() {
// args: interfaceIndex, options, maxFrameSize, and maxTotalFrames// inerfaceIndex: the index of the net device you want to open a raw socket to
// options: RX and TX, or just one or the other?
// maxFrameSize: must be a power of 2, bigger than zsocket.MinimumFrameSize,
// and smaller than maximum frame size
// maxTotalFrames: must be at least 16, and be a multiple of 8.
zs, err := zsocket.NewZSocket(14, zsocket.EnableRX, 2048, 64, nettypes.All)
// the above will result in a ring buffer of 64 frames at
// (2048 - zsocket.PacketOffset()) *writeable* bytes each
// for a total of 2048*64 bytes of *unswappable* system memory consumed.
if err != nil {
panic(err)
}
zs.Listen(func(f *nettypes.Frame, frameLen, capturedLen uint16) {
fmt.Printf(f.String(capturedLen, 0))
})
}
```1. See the examples folder for more simple programs that do various things with ZSocket.
2. Learn how to set up a docker container with a custom veth-pair in the utils folder (useful for setting up
complex virtual networking scenarios)3. Play around with FakeInterface to (and its examples folder) to play around with networking protocols.