1
1
Fork 1
mirror of https://github.com/oddlama/nixos-extra-modules.git synced 2025-10-10 22:00:39 +02:00

fix: libnet usage

fix: use list for lookuptable for speeeeeeeeeeeeeeeeeeeeeed
This commit is contained in:
Patrick 2025-04-12 21:53:17 +02:00
parent 32f790ae7f
commit 0875c4f06e
No known key found for this signature in database
GPG key ID: 451F95EFB8BECD0F
2 changed files with 87 additions and 87 deletions

View file

@ -32,7 +32,7 @@ let
libNet = libNet =
(import ./netu.nix { (import ./netu.nix {
inherit (inputs.nixpkgs) lib; inherit (inputs.nixpkgs) lib;
}).lib.net; }).lib;
in in
{ {
lib = recursiveUpdate prev.lib { lib = recursiveUpdate prev.lib {
@ -44,7 +44,7 @@ in
parsers parsers
; ;
net = recursiveUpdate (removeAttrs libNet [ "types" ]) { net = recursiveUpdate (removeAttrs libNet.net [ "types" ]) {
cidr = rec { cidr = rec {
# host :: (ip | mac | integer) -> cidr -> ip # host :: (ip | mac | integer) -> cidr -> ip
# #
@ -66,10 +66,10 @@ in
host = host =
i: n: i: n:
let let
cap = libNet.cidr.capacity n; cap = libNet.net.cidr.capacity n;
in in
assert assertMsg (i >= (-cap) && i < cap) "The host ${toString i} lies outside of ${n}"; assert assertMsg (i >= (-cap) && i < cap) "The host ${toString i} lies outside of ${n}";
libNet.cidr.host i n; libNet.net.cidr.host i n;
# hostCidr :: (ip | mac | integer) -> cidr -> cidr # hostCidr :: (ip | mac | integer) -> cidr -> cidr
# #
# Returns the nth host in the given cidr range (like cidr.host) # Returns the nth host in the given cidr range (like cidr.host)
@ -79,7 +79,7 @@ in
# #
# > net.cidr.hostCidr 2 "192.168.1.0/24" # > net.cidr.hostCidr 2 "192.168.1.0/24"
# "192.168.1.2/24" # "192.168.1.2/24"
hostCidr = n: x: "${libNet.cidr.host n x}/${toString (libNet.cidr.length x)}"; hostCidr = n: x: "${libNet.net.cidr.host n x}/${toString (libNet.net.cidr.length x)}";
# ip :: (cidr | ip) -> ip # ip :: (cidr | ip) -> ip
# #
# Returns just the ip part of the cidr. # Returns just the ip part of the cidr.
@ -100,7 +100,7 @@ in
# #
# > net.cidr.canonicalize "192.168.1.100/24" # > net.cidr.canonicalize "192.168.1.100/24"
# "192.168.1.0/24" # "192.168.1.0/24"
canonicalize = x: libNet.cidr.make (libNet.cidr.length x) (ip x); canonicalize = x: libNet.net.cidr.make (libNet.net.cidr.length x) (ip x);
# mergev4 :: [cidrv4 | ipv4] -> (cidrv4 | null) # mergev4 :: [cidrv4 | ipv4] -> (cidrv4 | null)
# #
# Returns the smallest cidr network that includes all given networks. # Returns the smallest cidr network that includes all given networks.
@ -118,7 +118,7 @@ in
# The smallest occurring length is the first we need to start checking, since # The smallest occurring length is the first we need to start checking, since
# any greater cidr length represents a smaller address range which # any greater cidr length represents a smaller address range which
# wouldn't contain all of the original addresses. # wouldn't contain all of the original addresses.
startLength = foldl' min 32 (map libNet.cidr.length addrs); startLength = foldl' min 32 (map libNet.net.cidr.length addrs);
possibleLengths = reverseList (range 0 startLength); possibleLengths = reverseList (range 0 startLength);
# The first ip address will be "expanded" in cidr length until it covers all other # The first ip address will be "expanded" in cidr length until it covers all other
# used addresses. # used addresses.
@ -128,12 +128,12 @@ in
bestLength = head ( bestLength = head (
filter filter
# All given addresses must be contained by the generated address. # All given addresses must be contained by the generated address.
(len: all (x: libNet.cidr.contains (ip x) (libNet.cidr.make len firstIp)) addrs) (len: all (x: libNet.net.cidr.contains (ip x) (libNet.net.cidr.make len firstIp)) addrs)
possibleLengths possibleLengths
); );
in in
assert assertMsg (!any (hasInfix ":") addrs) "mergev4 cannot operate on ipv6 addresses"; assert assertMsg (!any (hasInfix ":") addrs) "mergev4 cannot operate on ipv6 addresses";
if addrs == [ ] then null else libNet.cidr.make bestLength firstIp; if addrs == [ ] then null else libNet.net.cidr.make bestLength firstIp;
# mergev6 :: [cidrv6 | ipv6] -> (cidrv6 | null) # mergev6 :: [cidrv6 | ipv6] -> (cidrv6 | null)
# #
# Returns the smallest cidr network that includes all given networks. # Returns the smallest cidr network that includes all given networks.
@ -151,7 +151,7 @@ in
# The smallest occurring length is the first we need to start checking, since # The smallest occurring length is the first we need to start checking, since
# any greater cidr length represents a smaller address range which # any greater cidr length represents a smaller address range which
# wouldn't contain all of the original addresses. # wouldn't contain all of the original addresses.
startLength = foldl' min 128 (map libNet.cidr.length addrs); startLength = foldl' min 128 (map libNet.net.cidr.length addrs);
possibleLengths = reverseList (range 0 startLength); possibleLengths = reverseList (range 0 startLength);
# The first ip address will be "expanded" in cidr length until it covers all other # The first ip address will be "expanded" in cidr length until it covers all other
# used addresses. # used addresses.
@ -161,12 +161,12 @@ in
bestLength = head ( bestLength = head (
filter filter
# All given addresses must be contained by the generated address. # All given addresses must be contained by the generated address.
(len: all (x: libNet.cidr.contains (ip x) (libNet.cidr.make len firstIp)) addrs) (len: all (x: libNet.net.cidr.contains (ip x) (libNet.net.cidr.make len firstIp)) addrs)
possibleLengths possibleLengths
); );
in in
assert assertMsg (all (hasInfix ":") addrs) "mergev6 cannot operate on ipv4 addresses"; assert assertMsg (all (hasInfix ":") addrs) "mergev6 cannot operate on ipv4 addresses";
if addrs == [ ] then null else libNet.cidr.make bestLength firstIp; if addrs == [ ] then null else libNet.net.cidr.make bestLength firstIp;
# merge :: [cidr] -> { cidrv4 = (cidrv4 | null); cidrv6 = (cidrv4 | null); } # merge :: [cidr] -> { cidrv4 = (cidrv4 | null); cidrv6 = (cidrv4 | null); }
# #
# Returns the smallest cidr network that includes all given networks, # Returns the smallest cidr network that includes all given networks,
@ -202,8 +202,8 @@ in
assignIps = assignIps =
net: reserved: hosts: net: reserved: hosts:
let let
cidrSize = libNet.cidr.size net; cidrSize = libNet.net.cidr.size net;
capacity = libNet.cidr.capacity net; capacity = libNet.net.cidr.capacity net;
# The base address of the network. Used to convert ip-based reservations to offsets # The base address of the network. Used to convert ip-based reservations to offsets
baseAddr = host 0 net; baseAddr = host 0 net;
# Reserve some values for the network, host and broadcast address. # Reserve some values for the network, host and broadcast address.
@ -215,7 +215,7 @@ in
0 0
(capacity - 1) (capacity - 1)
] ]
++ flip map reserved (x: if builtins.typeOf x == "int" then x else -(libNet.ip.diff baseAddr x)) ++ flip map reserved (x: if builtins.typeOf x == "int" then x else -(libNet.net.ip.diff baseAddr x))
); );
nHosts = builtins.length hosts; nHosts = builtins.length hosts;
nInit = builtins.length init; nInit = builtins.length init;
@ -282,7 +282,7 @@ in
addPrivate = addPrivate =
base: offset: base: offset:
let let
added = libNet.mac.add base offset; added = libNet.net.mac.add base offset;
pre = substring 0 1 added; pre = substring 0 1 added;
suf = substring 2 (-1) added; suf = substring 2 (-1) added;
in in
@ -364,6 +364,6 @@ in
} sortedHosts).assigned; } sortedHosts).assigned;
}; };
}; };
types.net = libNet.types; types.net = libNet.net.types;
}; };
} }

View file

@ -460,71 +460,71 @@ let
bit = bit =
let let
lut = { lut = [
"0" = 1; 1 # 0
"1" = 2; 2 # 1
"2" = 4; 4 # 2
"3" = 8; 8 # 3
"4" = 16; 16 # 4
"5" = 32; 32 # 5
"6" = 64; 64 # 6
"7" = 128; 128 # 7
"8" = 256; 256 # 8
"9" = 512; 512 # 9
"10" = 1024; 1024 # 10
"11" = 2048; 2048 # 11
"12" = 4096; 4096 # 12
"13" = 8192; 8192 # 13
"14" = 16384; 16384 # 14
"15" = 32768; 32768 # 15
"16" = 65536; 65536 # 16
"17" = 131072; 131072 # 17
"18" = 262144; 262144 # 18
"19" = 524288; 524288 # 19
"20" = 1048576; 1048576 # 20
"21" = 2097152; 2097152 # 21
"22" = 4194304; 4194304 # 22
"23" = 8388608; 8388608 # 23
"24" = 16777216; 16777216 # 24
"25" = 33554432; 33554432 # 25
"26" = 67108864; 67108864 # 26
"27" = 134217728; 134217728 # 27
"28" = 268435456; 268435456 # 28
"29" = 536870912; 536870912 # 29
"30" = 1073741824; 1073741824 # 30
"31" = 2147483648; 2147483648 # 31
"32" = 4294967296; 4294967296 # 32
"33" = 8589934592; 8589934592 # 33
"34" = 17179869184; 17179869184 # 34
"35" = 34359738368; 34359738368 # 35
"36" = 68719476736; 68719476736 # 36
"37" = 137438953472; 137438953472 # 37
"38" = 274877906944; 274877906944 # 38
"39" = 549755813888; 549755813888 # 39
"40" = 1099511627776; 1099511627776 # 40
"41" = 2199023255552; 2199023255552 # 41
"42" = 4398046511104; 4398046511104 # 42
"43" = 8796093022208; 8796093022208 # 43
"44" = 17592186044416; 17592186044416 # 44
"45" = 35184372088832; 35184372088832 # 45
"46" = 70368744177664; 70368744177664 # 46
"47" = 140737488355328; 140737488355328 # 47
"48" = 281474976710656; 281474976710656 # 48
"49" = 562949953421312; 562949953421312 # 49
"50" = 1125899906842624; 1125899906842624 # 50
"51" = 2251799813685248; 2251799813685248 # 51
"52" = 4503599627370496; 4503599627370496 # 52
"53" = 9007199254740992; 9007199254740992 # 53
"54" = 18014398509481984; 18014398509481984 # 54
"55" = 36028797018963968; 36028797018963968 # 55
"56" = 72057594037927936; 72057594037927936 # 56
"57" = 144115188075855872; 144115188075855872 # 57
"58" = 288230376151711744; 288230376151711744 # 58
"59" = 576460752303423488; 576460752303423488 # 59
"60" = 1152921504606846976; 1152921504606846976 # 60
"61" = 2305843009213693952; 2305843009213693952 # 61
"62" = 4611686018427387904; 4611686018427387904 # 62
}; ];
intmin = (-9223372036854775807) - 1; intmin = (-9223372036854775807) - 1;
intmax = 9223372036854775807; intmax = 9223372036854775807;
left = left =
@ -539,11 +539,11 @@ let
else else
let let
inv = 63 - a; inv = 63 - a;
mask = if inv == 63 then intmax else lut.${toString inv} - 1; mask = if inv == 63 then intmax else (builtins.elemAt lut inv) - 1;
masked = and b mask; masked = and b mask;
checker = if inv == 63 then intmin else lut.${toString inv}; checker = if inv == 63 then intmin else builtins.elemAt lut inv;
negate = (and b checker) != 0; negate = (and b checker) != 0;
mult = if a == 63 then intmin else lut.${toString a}; mult = if a == 63 then intmin else builtins.elemAt lut a;
result = masked * mult; result = masked * mult;
in in
if !negate then result else intmin + result; if !negate then result else intmin + result;
@ -560,9 +560,9 @@ let
let let
masked = and b intmax; masked = and b intmax;
negate = b < 0; negate = b < 0;
result = masked / lut.${toString a}; result = masked / 2 / (builtins.elemAt lut (a - 1));
inv = 63 - a; inv = 63 - a;
highest_bit = lut.${toString inv}; highest_bit = builtins.elemAt lut inv;
in in
if !negate then result else result + highest_bit; if !negate then result else result + highest_bit;