Page MenuHomeVyOS Platform

Alternative validators
Open, Requires assessmentPublicFEATURE REQUEST

Description

We can decrease validation time for some validators with go

Standard validator ipv4-range

[email protected]# time sudo sh -c ' for ((n=0;n<10000;n++)); do  /usr/libexec/vyos/validate-value  --exec /usr/libexec/vyos/validators/ipv4-range --value "192.0.2.1-192.0.2.100"; done'

real	0m48.825s
user	0m26.534s
sys	0m23.904s
[edit]
[email protected]#

The same validator in PoC go

[email protected]# time sudo sh -c ' for ((n=0;n<10000;n++)); do  /usr/libexec/vyos/validate-value  --exec /usr/libexec/vyos/validators/ipv4-range-go --value "192.0.2.1-192.0.2.100"; done'

real	0m27.069s
user	0m12.814s
sys	0m14.784s
[edit]
[email protected]#

There is an example of code:

$ cat ipv4-range.go
package main

import (
    "net"
    "os"
    "strings"
    "math/big"
)

var (
    hosts = os.Args[1]
)


func IP4toInt(IPv4Address net.IP) int64 {
    IPv4Int := big.NewInt(0)
    IPv4Int.SetBytes(IPv4Address.To4())
    return IPv4Int.Int64()
}

func IsIPv4(ip string) bool {
    if net.ParseIP(ip) == nil {
        return false
    }
    return true
}

func main() {
    dash := strings.Contains(hosts, "-")
    if dash == false {
        os.Exit(1)
    }

    lines := strings.Split(hosts, "-")
    first, second := string(lines[0]), string(lines[1])

    dec_first := IP4toInt(net.ParseIP(first))
    dec_second := IP4toInt(net.ParseIP(second))

    if dec_first  >= dec_second  {
        os.Exit(1)
    }

    for _, host := range lines {
        if IsIPv4(host) == false {
            os.Exit(1)
        }
    }
}

Some comparisons:
https://programming-language-benchmarks.vercel.app/python-vs-go
https://programming-language-benchmarks.vercel.app/go-vs-ocaml

Details

Difficulty level
Unknown (require assessment)
Version
1.4
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Unspecified (possibly destroys the router)
Issue type
Feature (new functionality)