Skip to content

Commit

Permalink
Dobby is FREE!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 28, 2024
0 parents commit 656fb87
Show file tree
Hide file tree
Showing 14 changed files with 2,929 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI
on:
push:
branches: ["main", "devel"]
paths:
- '**.go'
- '**.yml'
pull_request:
branches: ["main"]

jobs:
test:
strategy:
matrix:
go-version: ['1.21']
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- run: go test -v ./...

govulncheck:
runs-on: ubuntu-latest
steps:
- uses: golang/govulncheck-action@v1
with:
go-version-input: 1.21
check-latest: true

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Test Coverage
run: go test -v -coverprofile=profile.cov ./...

- uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov

linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.21

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Karl Gaissmaier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# package bart

`package bart` provides a Balanced-Routing-Table (BART).

BART is balanced in terms of memory consumption versus
lookup time.

The lookup time is by a factor of <1.5 slower on average as the
routing algorithms ART, SMART, CPE, ... but reduces the memory
consumption by an order of magnitude in comparison.

BART is a popcount compressed multibit-trie, using the
'baseIndex' function from the ART algorithm to build the
complete binary prefix tree (CBT) for each stride.

The second key factor is popcount level compression
and backtracking along the CBT prefix tree in O(k).

The CBT is implemented as a bitvector, backtracking is just
a matter of fast cache friendly bitmask operations.

Due to the cache locality of the popcount compressed CBT,
the backtracking algorithm is as fast as possible.

# API (not stable but ready to use!!!)

```golang
import "github.com/gaissmai/bart"

type Table[V any] struct {
// Has unexported fields.
}
Table is an IPv4 and IPv6 routing table with payload V. The zero value is
ready to use.

func (t *Table[V]) Insert(pfx netip.Prefix, val V)
func (t *Table[V]) Delete(pfx netip.Prefix)

func (t *Table[V]) Get(ip netip.Addr) (val V, ok bool)
func (t *Table[V]) Lookup(ip netip.Addr) (lpm netip.Prefix, val V, ok bool)

func (t *Table[V]) String() string
func (t *Table[V]) Fprint(w io.Writer) error

func (t *Table[V]) Dump(w io.Writer)
```

# LICENSE

MIT
Binary file added doc/artlookup.pdf
Binary file not shown.
201 changes: 201 additions & 0 deletions dumper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Copyright (c) 2024 Karl Gaissmaier
// SPDX-License-Identifier: MIT

package bart

import (
"fmt"
"io"
"strconv"
"strings"
)

// Dump the IPv4 and IPv6 tables to w.
// Needed during development and debugging, especially for the
// hierarchical tree in the String method, OMG.
// Will be removed when the API stabilizes.
//
// Output:
//
// [FULL] depth: 0 path: [] / 0
// indexs(#6): 1 66 128 133 266 383
// prefxs(#6): 0/0 8/6 0/7 10/7 10/8 127/8
// childs(#3): 10 127 192
//
// .[IMED] depth: 1 path: [10] / 8
// .childs(#1): 0
//
// ..[LEAF] depth: 2 path: [10.0] / 16
// ..indexs(#2): 256 257
// ..prefxs(#2): 0/8 1/8
//
// .[IMED] depth: 1 path: [127] / 8
// .childs(#1): 0
//
// ..[IMED] depth: 2 path: [127.0] / 16
// ..childs(#1): 0
//
// ...[LEAF] depth: 3 path: [127.0.0] / 24
// ...indexs(#1): 257
// ...prefxs(#1): 1/8
//
// ...
func (t *Table[V]) Dump(w io.Writer) error {
t.init()

if err := t.dump4(w); err != nil {
return err
}

if err := t.dump6(w); err != nil {
return err
}

return nil
}

// dump4, dumps the IPv4 routing table to w.
func (t *Table[V]) dump4(w io.Writer) error {
is4 := true
root4 := t.rootNodeVersion(is4)
if _, err := fmt.Fprint(w, "IPv4:\n"); err != nil {
return err
}

root4.dumpRec(w, nil, is4)
return nil
}

// dump6, dumps the IPv6 routing table to w.
func (t *Table[V]) dump6(w io.Writer) error {
is4 := false
root6 := t.rootNodeVersion(is4)
if _, err := fmt.Fprint(w, "IPv6:\n"); err != nil {
return err
}

root6.dumpRec(w, nil, is4)
return nil
}

// dumpRec, rec-descent the trie.
func (n *node[V]) dumpRec(w io.Writer, path []byte, is4 bool) {
n.dump(w, path, is4)

for i, child := range n.children.nodes {
addr := n.children.addrs.Select(uint(i))
child.dumpRec(w, append(path, byte(addr)), is4)
}
}

// dump the node to w.
func (n *node[V]) dump(w io.Writer, path []byte, is4 bool) {
must := func(_ int, err error) {
if err != nil {
panic(err)
}
}

depth := len(path)
bits := depth * stride
indent := strings.Repeat(".", depth)

// node type with depth and addr path and bits.
must(fmt.Fprintf(w, "\n%s[%s] depth: %d path: [%v] / %d\n",
indent, n.hasType(), depth, ancestors(path, is4), bits))

if len(n.prefixes.values) != 0 {
indices := n.prefixes.allIndexes()
// print the baseIndices for this node.
must(fmt.Fprintf(w, "%sindexs(#%d): %v\n", indent, len(n.prefixes.values), indices))

// print the prefixes for this node
must(fmt.Fprintf(w, "%sprefxs(#%d): ", indent, len(n.prefixes.values)))

for _, idx := range indices {
addr, bits := baseIndexToPrefix(idx)
must(fmt.Fprintf(w, "%s/%d ", addrFmt(addr, is4), bits))
}
must(fmt.Fprintln(w))
}

if len(n.children.nodes) != 0 {
// print the childs for this node
must(fmt.Fprintf(w, "%schilds(#%d): ", indent, len(n.children.nodes)))

for i := range n.children.nodes {
addr := n.children.addrs.Select(uint(i))
must(fmt.Fprintf(w, "%s ", addrFmt(addr, is4)))
}
must(fmt.Fprintln(w))
}
}

// addrFmt, different format strings for IPv4 and IPv6, decimal versus hex.
func addrFmt(addr uint, is4 bool) string {
if is4 {
return fmt.Sprintf("%d", addr)
}
return fmt.Sprintf("0x%02x", addr)
}

// IP stride path, different formats for IPv4 and IPv6, dotted decimal or hex.
func ancestors(bs []byte, is4 bool) string {
buf := &strings.Builder{}

if is4 {
for i, b := range bs {
if i != 0 {
buf.WriteString(".")
}
buf.WriteString(strconv.Itoa(int(b)))
}
return buf.String()
}

for i, b := range bs {
if i != 0 && i%2 == 0 {
buf.WriteString(":")
}
buf.WriteString(fmt.Sprintf("%02x", b))
}
return buf.String()
}

// String implements Stringer for nodeType.
func (nt nodeType) String() string {
switch nt {
case nullNode:
return "ROOT"
case fullNode:
return "FULL"
case leafNode:
return "LEAF"
case intermediateNode:
return "IMED"
}
panic("unreachable")
}

// hasType returns the nodeType.
func (n *node[V]) hasType() nodeType {
lenPefixes := len(n.prefixes.values)
lenChilds := len(n.children.nodes)

if lenPefixes == 0 && lenChilds != 0 {
return intermediateNode
}

if lenPefixes == 0 && lenChilds == 0 {
return nullNode
}

if lenPefixes != 0 && lenChilds == 0 {
return leafNode
}

if lenPefixes != 0 && lenChilds != 0 {
return fullNode
}
panic("unreachable")
}
Loading

0 comments on commit 656fb87

Please sign in to comment.