summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/buffer
diff options
context:
space:
mode:
authorTing-Yu Wang <anivia@google.com>2021-03-03 16:03:04 -0800
committergVisor bot <gvisor-bot@google.com>2021-03-03 16:05:16 -0800
commit1cd76d958a9b3eb29f6b55a8bea71fbe464e67d3 (patch)
tree1f4df3b516c62a2aa630ffaf9c6ecba99482e3d3 /pkg/tcpip/buffer
parentcfd2c31962a4358d7d05a4bd04dde271dc238339 (diff)
Make dedicated methods for data operations in PacketBuffer
One of the preparation to decouple underlying buffer implementation. There are still some methods that tie to VectorisedView, and they will be changed gradually in later CLs. This CL also introduce a new ICMPv6ChecksumParams to replace long list of parameters when calling ICMPv6Checksum, aiming to be more descriptive. PiperOrigin-RevId: 360778149
Diffstat (limited to 'pkg/tcpip/buffer')
-rw-r--r--pkg/tcpip/buffer/view.go10
-rw-r--r--pkg/tcpip/buffer/view_test.go46
2 files changed, 50 insertions, 6 deletions
diff --git a/pkg/tcpip/buffer/view.go b/pkg/tcpip/buffer/view.go
index b05e81526..f4a30effd 100644
--- a/pkg/tcpip/buffer/view.go
+++ b/pkg/tcpip/buffer/view.go
@@ -196,7 +196,7 @@ func (vv *VectorisedView) CapLength(length int) {
// If the buffer argument is large enough to contain all the Views of this
// VectorisedView, the method will avoid allocations and use the buffer to
// store the Views of the clone.
-func (vv *VectorisedView) Clone(buffer []View) VectorisedView {
+func (vv VectorisedView) Clone(buffer []View) VectorisedView {
return VectorisedView{views: append(buffer[:0], vv.views...), size: vv.size}
}
@@ -290,6 +290,14 @@ func (vv *VectorisedView) AppendView(v View) {
vv.size += len(v)
}
+// AppendViews appends views to vv.
+func (vv *VectorisedView) AppendViews(views []View) {
+ vv.views = append(vv.views, views...)
+ for _, v := range views {
+ vv.size += len(v)
+ }
+}
+
// Readers returns a bytes.Reader for each of vv's views.
func (vv *VectorisedView) Readers() []bytes.Reader {
readers := make([]bytes.Reader, 0, len(vv.views))
diff --git a/pkg/tcpip/buffer/view_test.go b/pkg/tcpip/buffer/view_test.go
index 78b2faa26..d296d9c2b 100644
--- a/pkg/tcpip/buffer/view_test.go
+++ b/pkg/tcpip/buffer/view_test.go
@@ -45,6 +45,11 @@ func vv(size int, pieces ...string) buffer.VectorisedView {
return buffer.NewVectorisedView(size, views)
}
+// v returns a buffer.View containing piece.
+func v(piece string) buffer.View {
+ return buffer.View(piece)
+}
+
var capLengthTestCases = []struct {
comment string
in buffer.VectorisedView
@@ -125,6 +130,12 @@ var trimFrontTestCases = []struct {
want: vv(1, "3"),
},
{
+ comment: "Case with one empty Views",
+ in: vv(3, "1", "", "23"),
+ count: 2,
+ want: vv(1, "3"),
+ },
+ {
comment: "Corner case with negative count",
in: vv(1, "1"),
count: -1,
@@ -566,11 +577,11 @@ func TestAppendView(t *testing.T) {
in buffer.View
want buffer.VectorisedView
}{
- {buffer.VectorisedView{}, nil, buffer.VectorisedView{}},
- {buffer.VectorisedView{}, buffer.View{}, buffer.VectorisedView{}},
- {buffer.NewVectorisedView(4, []buffer.View{{'a', 'b', 'c', 'd'}}), nil, buffer.NewVectorisedView(4, []buffer.View{{'a', 'b', 'c', 'd'}})},
- {buffer.NewVectorisedView(4, []buffer.View{{'a', 'b', 'c', 'd'}}), buffer.View{}, buffer.NewVectorisedView(4, []buffer.View{{'a', 'b', 'c', 'd'}})},
- {buffer.NewVectorisedView(4, []buffer.View{{'a', 'b', 'c', 'd'}}), buffer.View{'e'}, buffer.NewVectorisedView(5, []buffer.View{{'a', 'b', 'c', 'd'}, {'e'}})},
+ {vv(0), nil, vv(0)},
+ {vv(0), v(""), vv(0)},
+ {vv(4, "abcd"), nil, vv(4, "abcd")},
+ {vv(4, "abcd"), v(""), vv(4, "abcd")},
+ {vv(4, "abcd"), v("e"), vv(5, "abcd", "e")},
}
for _, tc := range testCases {
tc.vv.AppendView(tc.in)
@@ -580,6 +591,31 @@ func TestAppendView(t *testing.T) {
}
}
+func TestAppendViews(t *testing.T) {
+ testCases := []struct {
+ vv buffer.VectorisedView
+ in []buffer.View
+ want buffer.VectorisedView
+ }{
+ {vv(0), nil, vv(0)},
+ {vv(0), []buffer.View{}, vv(0)},
+ {vv(0), []buffer.View{v("")}, vv(0, "")},
+ {vv(4, "abcd"), nil, vv(4, "abcd")},
+ {vv(4, "abcd"), []buffer.View{}, vv(4, "abcd")},
+ {vv(4, "abcd"), []buffer.View{v("")}, vv(4, "abcd", "")},
+ {vv(4, "abcd"), []buffer.View{v("")}, vv(4, "abcd", "")},
+ {vv(4, "abcd"), []buffer.View{v("e")}, vv(5, "abcd", "e")},
+ {vv(4, "abcd"), []buffer.View{v("e"), v("fg")}, vv(7, "abcd", "e", "fg")},
+ {vv(4, "abcd"), []buffer.View{v(""), v("fg")}, vv(6, "abcd", "", "fg")},
+ }
+ for _, tc := range testCases {
+ tc.vv.AppendViews(tc.in)
+ if got, want := tc.vv, tc.want; !reflect.DeepEqual(got, want) {
+ t.Errorf("(%v).ToVectorisedView failed got: %+v, want: %+v", tc.in, got, want)
+ }
+ }
+}
+
func TestMemSize(t *testing.T) {
const perViewCap = 128
views := make([]buffer.View, 2, 32)