-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vam: Add network_of() func #5306
Open
mattnibs
wants to merge
1
commit into
main
Choose a base branch
from
vam-network-of
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package function | ||
|
||
import ( | ||
"net/netip" | ||
|
||
"github.com/brimdata/zed" | ||
"github.com/brimdata/zed/vector" | ||
) | ||
|
||
// https://github.com/brimdata/zed/blob/main/docs/language/functions.md#network_of | ||
type NetworkOf struct { | ||
zctx *zed.Context | ||
} | ||
|
||
func (n *NetworkOf) Call(args ...vector.Any) vector.Any { | ||
args = underAll(args) | ||
ipvec := args[0] | ||
if ipvec.Type().ID() != zed.IDIP { | ||
return vector.NewWrappedError(n.zctx, "network_of: not an IP", ipvec) | ||
} | ||
var nets []netip.Prefix | ||
size := ipvec.Len() | ||
if len(args) == 1 { | ||
var errs []uint32 | ||
for i := uint32(0); i < size; i++ { | ||
var bits int | ||
ip, _ := vector.IPValue(ipvec, i) | ||
switch { | ||
case !ip.Is4(): | ||
errs = append(errs, i) | ||
continue | ||
case ip.As4()[0] < 0x80: | ||
bits = 8 | ||
case ip.As4()[0] < 0xc0: | ||
bits = 16 | ||
default: | ||
bits = 24 | ||
} | ||
nets = append(nets, netip.PrefixFrom(ip, bits).Masked()) | ||
} | ||
b := vector.NewCombiner(vector.NewNet(nets, nil)) | ||
b.WrappedError(n.zctx, errs, "network_of: not an IPv4 address", args[0]) | ||
return b.Result() | ||
} else { | ||
// two args | ||
maskvec := args[1] | ||
switch id := maskvec.Type().ID(); { | ||
case id == zed.IDIP: | ||
var errsLen, errsCont []uint32 | ||
for i := uint32(0); i < size; i++ { | ||
ip, _ := vector.IPValue(ipvec, i) | ||
mask, _ := vector.IPValue(maskvec, i) | ||
if mask.BitLen() != ip.BitLen() { | ||
errsLen = append(errsLen, i) | ||
continue | ||
} | ||
bits := zed.LeadingOnes(mask.AsSlice()) | ||
if netip.PrefixFrom(mask, bits).Masked().Addr() != mask { | ||
errsCont = append(errsCont, i) | ||
continue | ||
} | ||
nets = append(nets, netip.PrefixFrom(ip, bits).Masked()) | ||
} | ||
b := vector.NewCombiner(vector.NewNet(nets, nil)) | ||
m := addressAndMask(n.zctx, args[0], args[1]) | ||
b.WrappedError(n.zctx, errsLen, "network_of: address and mask have different lengths", m) | ||
b.WrappedError(n.zctx, errsCont, "network_of: mask is non-contiguous", args[1]) | ||
return b.Result() | ||
case zed.IsInteger(id): | ||
var errs []uint32 | ||
for i := uint32(0); i < size; i++ { | ||
var bits int | ||
if zed.IsSigned(id) { | ||
b, _ := vector.IntValue(maskvec, i) | ||
bits = int(b) | ||
} else { | ||
b, _ := vector.UintValue(maskvec, i) | ||
bits = int(b) | ||
} | ||
ip, _ := vector.IPValue(ipvec, i) | ||
if bits > 128 || bits > 32 && ip.Is4() { | ||
errs = append(errs, i) | ||
continue | ||
} | ||
nets = append(nets, netip.PrefixFrom(ip, bits).Masked()) | ||
} | ||
b := vector.NewCombiner(vector.NewNet(nets, nil)) | ||
m := addressAndMask(n.zctx, args[0], args[1]) | ||
b.WrappedError(n.zctx, errs, "network_of: CIDR bit count out of range", m) | ||
return b.Result() | ||
default: | ||
return vector.NewWrappedError(n.zctx, "network_of: bad arg for CIDR mask", args[1]) | ||
} | ||
} | ||
} | ||
|
||
func addressAndMask(zctx *zed.Context, address, mask vector.Any) vector.Any { | ||
typ := zctx.MustLookupTypeRecord([]zed.Field{ | ||
{Name: "address", Type: address.Type()}, | ||
{Name: "mask", Type: mask.Type()}, | ||
}) | ||
return vector.NewRecord(typ, []vector.Any{address, mask}, address.Len(), nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
runtime/sam/expr/ztests/network-of.yaml → runtime/ztests/expr/function/network-of.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vector.Combine feels like a better API here than vector.NewCombiner since the latter allocates a vector.Error even when len(errs)==0. Why not stick with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't work for multiple error values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then how about using an API that works for multiple error values when you need that and an API that doesn't do unnecessary allocations when you don't?