aboutsummaryrefslogtreecommitdiff
path: root/tun/errors.go
diff options
context:
space:
mode:
authorJordan Whited <jordan@tailscale.com>2023-03-02 14:48:02 -0800
committerJason A. Donenfeld <Jason@zx2c4.com>2023-03-10 14:52:13 +0100
commit3bb8fec7e41fcc2138ddb4cba3f46100814fc523 (patch)
treee6c83a2ae0178ffee5dde657cbbdd17ee427dbbe /tun/errors.go
parent21636207a6756120f56f836b06c086b627f7b911 (diff)
downloadwireguard-go-3bb8fec7e41fcc2138ddb4cba3f46100814fc523.tar.gz
wireguard-go-3bb8fec7e41fcc2138ddb4cba3f46100814fc523.zip
conn, device, tun: implement vectorized I/O plumbing
Accept packet vectors for reading and writing in the tun.Device and conn.Bind interfaces, so that the internal plumbing between these interfaces now passes a vector of packets. Vectors move untouched between these interfaces, i.e. if 128 packets are received from conn.Bind.Read(), 128 packets are passed to tun.Device.Write(). There is no internal buffering. Currently, existing implementations are only adjusted to have vectors of length one. Subsequent patches will improve that. Also, as a related fixup, use the unix and windows packages rather than the syscall package when possible. Co-authored-by: James Tucker <james@tailscale.com> Signed-off-by: James Tucker <james@tailscale.com> Signed-off-by: Jordan Whited <jordan@tailscale.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'tun/errors.go')
-rw-r--r--tun/errors.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/tun/errors.go b/tun/errors.go
new file mode 100644
index 0000000..e70b13c
--- /dev/null
+++ b/tun/errors.go
@@ -0,0 +1,60 @@
+package tun
+
+import (
+ "errors"
+ "fmt"
+)
+
+var (
+ // ErrTooManySegments is returned by Device.Read() when segmentation
+ // overflows the length of supplied buffers. This error should not cause
+ // reads to cease.
+ ErrTooManySegments = errors.New("too many segments")
+)
+
+type errorBatch []error
+
+// ErrorBatch takes a possibly nil or empty list of errors, and if the list is
+// non-nil returns an error type that wraps all of the errors. Expected usage is
+// to append to an []errors and coerce the set to an error using this method.
+func ErrorBatch(errs []error) error {
+ if len(errs) == 0 {
+ return nil
+ }
+ return errorBatch(errs)
+}
+
+func (e errorBatch) Error() string {
+ if len(e) == 0 {
+ return ""
+ }
+ if len(e) == 1 {
+ return e[0].Error()
+ }
+ return fmt.Sprintf("batch operation: %v (and %d more errors)", e[0], len(e)-1)
+}
+
+func (e errorBatch) Is(target error) bool {
+ for _, err := range e {
+ if errors.Is(err, target) {
+ return true
+ }
+ }
+ return false
+}
+
+func (e errorBatch) As(target interface{}) bool {
+ for _, err := range e {
+ if errors.As(err, target) {
+ return true
+ }
+ }
+ return false
+}
+
+func (e errorBatch) Unwrap() error {
+ if len(e) == 0 {
+ return nil
+ }
+ return e[0]
+}