• Stars
    star
    572
  • Rank 75,319 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Linux networking in Go

Linux networking in Golang

GoDoc License

tenus is a Golang package which allows you to configure and manage Linux network devices programmatically. It communicates with Linux Kernel via netlink to facilitate creation and configuration of network devices on the Linux host. The package also allows for more advanced network setups with Linux containers including Docker.

tenus uses runc's implementation of netlink protocol. The package only works with newer Linux Kernels (3.10+) which are shipping reasonably new netlink protocol implementation, so if you are running older kernel this package won't be of much use to you I'm afraid. I have developed this package on Ubuntu Trusty Tahr which ships with 3.13+ and verified its functionality on Precise Pangolin with upgraded kernel to version 3.10. I could worked around the netlink issues by using ioctl syscalls, but I decided to prefer "pure netlink" implementation, so suck it old Kernels.

At the moment only functional tests are available, but the interface design should hopefully allow for easy (ish) unit testing in the future. I do appreciate that the package's test coverage is not great at the moment, but the core functionality should be covered. I would massively welcome PRs.

Get started

There is a Vagrantfile available in the repo so using vagrant is the easiest way to get started:

milosgajdos@bimbonet ~ $ git clone https://github.com/milosgajdos/tenus.git
milosgajdos@bimbonet ~ $ vagrant up

Note using the provided Vagrantfile will take quite a long time to spin the VM as vagrant will setup Ubuntu Trusty VM with all the prerequisities:

  • it will install golang and docker onto the VM
  • it will export GOPATH and go get the tenus package onto the VM
  • it will also "pull" Docker ubuntu image so that you can run the tests once the VM is set up

At the moment running the tests require Docker to be installed, but in the future I'd love to separate tests per interface so that you can run only chosen test sets.

Once the VM is running, cd into particular repo directory and you can run the tests:

milosgajdos@bimbonet ~ $ cd $GOPATH/src/github.com/milosgajdos/tenus
milosgajdos@bimbonet ~ $ sudo go test

If you don't want to use the provided Vagrantfile, you can simply run your own Linux VM (with 3.10+ kernel) and follow the regular golang development flow:

milosgajdos@bimbonet ~ $ go get github.com/milosgajdos/tenus
milosgajdos@bimbonet ~ $ cd $GOPATH/src/github.com/milosgajdos/tenus
milosgajdos@bimbonet ~ $ sudo go test

Once you've got the package and ran the tests (you don't need to run the tests!), you can start hacking. Below you can find simple code samples to get started with the package.

Examples

Below you can find a few code snippets which can help you get started writing your own programs.

New network bridge, add dummy link into it

The example below shows a simple program example which creates a new network bridge, a new dummy network link and adds it into the bridge.

package main

import (
	"fmt"
	"log"

	"github.com/milosgajdos/tenus"
)

func main() {
	// Create a new network bridge
	br, err := tenus.NewBridgeWithName("mybridge")
	if err != nil {
		log.Fatal(err)
	}

	// Bring the bridge up
	if err = br.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// Create a dummy link
	dl, err := tenus.NewLink("mydummylink")
	if err != nil {
		log.Fatal(err)
	}

	// Add the dummy link into bridge
	if err = br.AddSlaveIfc(dl.NetInterface()); err != nil {
		log.Fatal(err)
	}

	// Bring the dummy link up
	if err = dl.SetLinkUp(); err != nil {
		fmt.Println(err)
	}
}

New network bridge, veth pair, one peer in Docker

The example below shows how you can create a new network bride, configure its IP address, add a new veth pair and send one of the veth peers into Docker with a given name.

!! You must make sure that particular Docker is runnig if you want the code sample below to work properly !! So before you compile and run the program below you should create a particular docker with the below used name:

milosgajdos@bimbonet ~ $ docker run -i -t --rm --privileged -h vethdckr --name vethdckr ubuntu:14.04 /bin/bash
package main

import (
	"fmt"
	"log"
	"net"

	"github.com/milosgajdos/tenus"
)

func main() {
	// CREATE BRIDGE AND BRING IT UP
	br, err := tenus.NewBridgeWithName("vethbridge")
	if err != nil {
		log.Fatal(err)
	}

	brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := br.SetLinkIp(brIp, brIpNet); err != nil {
		fmt.Println(err)
	}

	if err = br.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// CREATE VETH PAIR
	veth, err := tenus.NewVethPairWithOptions("myveth01", tenus.VethOptions{PeerName: "myveth02"})
	if err != nil {
		log.Fatal(err)
	}

	// ASSIGN IP ADDRESS TO THE HOST VETH INTERFACE
	vethHostIp, vethHostIpNet, err := net.ParseCIDR("10.0.41.2/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetLinkIp(vethHostIp, vethHostIpNet); err != nil {
		fmt.Println(err)
	}

	// ADD MYVETH01 INTERFACE TO THE MYBRIDGE BRIDGE
	myveth01, err := net.InterfaceByName("myveth01")
	if err != nil {
		log.Fatal(err)
	}

	if err = br.AddSlaveIfc(myveth01); err != nil {
		fmt.Println(err)
	}

	if err = veth.SetLinkUp(); err != nil {
		fmt.Println(err)
	}

	// PASS VETH PEER INTERFACE TO A RUNNING DOCKER BY PID
	pid, err := tenus.DockerPidByName("vethdckr", "/var/run/docker.sock")
	if err != nil {
		fmt.Println(err)
	}

	if err := veth.SetPeerLinkNsPid(pid); err != nil {
		log.Fatal(err)
	}

	// ALLOCATE AND SET IP FOR THE NEW DOCKER INTERFACE
	vethGuestIp, vethGuestIpNet, err := net.ParseCIDR("10.0.41.5/16")
	if err != nil {
		log.Fatal(err)
	}

	if err := veth.SetPeerLinkNetInNs(pid, vethGuestIp, vethGuestIpNet, nil); err != nil {
		log.Fatal(err)
	}
}

Working with existing bridges and interfaces

The following examples show how to retrieve exisiting interfaces as a tenus link and bridge

package main

import (
	"fmt"
	"log"
	"net"

	"github.com/milosgajdos/tenus"
)

func main() {
	// RETRIEVE EXISTING BRIDGE
	br, err := tenus.BridgeFromName("bridge0")
	if err != nil {
		log.Fatal(err)
	}

	// REMOVING AN IP FROM A BRIDGE INTERFACE (BEFORE RECONFIGURATION)
	brIp, brIpNet, err := net.ParseCIDR("10.0.41.1/16")
	if err != nil {
		log.Fatal(err)
	}
	if err := br.UnsetLinkIp(brIp, brIpNet); err != nil {
		log.Fatal(err)
	}

	// RETRIEVE EXISTING INTERFACE
	dl, err := tenus.NewLinkFrom("eth0")
	if err != nil {
		log.Fatal(err)
	}

	// RENAMING AN INTERFACE BY NAME
	if err := tenus.RenameInterfaceByName("vethPSQSEl", "vethNEWNAME"); err != nil {
		log.Fatal(err)
	}

}

VLAN and MAC VLAN interfaces

You can check out VLAN and Mac VLAN examples, too.

More examples

Repo contains few more code sample in examples folder so make sure to check them out if you're interested.

TODO

This is just a rough beginning of the project which I put together over couple of weeks in my free time. I'd like to integrate this into my own Docker fork and test the advanced netowrking functionality with the core of Docker as oppose to configuring network interfaces from a separate golang program, because advanced networking in Docker was the main motivation for writing this package.

Documentation

More in depth package documentation is available via godoc

More Repositories

1

go-estimate

State estimation and filtering algorithms in Go
Go
109
star
2

gosom

Self-organizing maps in Go
Go
75
star
3

gopfield

Hopfield neural networks in Go
Go
54
star
4

gocv-playground

This repo contains various https://gocv.io/ examples
Go
41
star
5

servpeek

Introspective peek into your server guts
Go
30
star
6

go-embeddings

Go module for fetching embeddings from embeddings providers
Go
27
star
7

go-neural

Feedforward Neural Networks in Go
Go
20
star
8

ncs

Movidius Neural Compute Stick V2.0 API Go bindings
Go
18
star
9

taurus

Experimental Mesos framework for Docker containers written in Go
Go
15
star
10

bot-banter

Go vs Rust AI bot banter
Rust
14
star
11

kraph

Go module for scraping APIs to graphs
Go
12
star
12

ml-examples

Simple Machine Learning examples implemented in different languages
R
11
star
13

embeviz

A simple app for visualising vector embeddings
Go
9
star
14

machine-learning

Stanford university machine learning
MATLAB
6
star
15

vaultops

Setup your vault servers and store the keys in an encrypted store of your choice
Go
6
star
16

go-estimate-examples

Examples of usage for go-estimate
Go
6
star
17

matrix

Helper functions for working with https://www.gonum.org/ matrices in Go
Go
5
star
18

orbnet

GitHub stars network
Go
4
star
19

go-repo-template

Go repository template for my personal projects
Nix
4
star
20

libcontainer-milosgajdos83

Docker libcontainer fork for tenus project use
Go
3
star
21

meetuplytix

Simple Meetup groups analytics
Jupyter Notebook
3
star
22

playht_rs

PlayHT TTS API Rust crate
Rust
3
star
23

feeder

RSS news reader
Go
2
star
24

tf-dlstack

Terraform Deep Learning stack provisioning
HCL
2
star
25

udacity-ai-nanodegree

Udacity AI Nanodegree projects
Jupyter Notebook
2
star
26

netscrape

Go module for building networks from arbitrary data sources
Go
2
star
27

wasm-playground

Learning WASM
HTML
2
star
28

gollipse

Go package for plotting [confidence] ellipse of 2D normally distributed data
Go
2
star
29

embeddings-fun

A simple Go project that generates charts from embeddings
HTML
2
star
30

go-playht

PlayHT API client Go module
Go
2
star
31

kaggle-fun

Kaggle ML tutorials playground
1
star
32

.dotfiles

My workstation setup
Vim Script
1
star
33

learning-rust

List of various notes about Rust
1
star
34

udacity-deep-rl-nanodegree

Udacity Deep Reinforcement Learning Nanodegree projects
Python
1
star
35

datasciencecoursera

Data science coursera
1
star
36

sensu-presentation

1
star
37

alertify

Play Spotify song when some monitored activity is detected
Go
1
star
38

bttool

BitTorrent metainfo tool
Go
1
star
39

ros-playground

ROS (Robot Operating System) playground
Dockerfile
1
star
40

udacity-deep-learning-nanodegree

Udacity Deep Learning Nanodegree projects
HTML
1
star
41

macsetup

Macbook pro setup repo
Shell
1
star
42

milosgajdos

1
star
43

ARLondonSun

Example ARKit iOS app that lets you put a virtual sun in the middle of your room
Swift
1
star
44

flagmap

Wrapper around flag package which returns a map of parsed flag values with flag names as keys
Go
1
star
45

creep-dreamz

Deep Dream experiments inspired by Keras Deep Dream
Python
1
star