github.com/cilium/cilium@v1.16.2/pkg/monitor/payload/monitor_payload.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package payload 5 6 import ( 7 "bytes" 8 "encoding/binary" 9 "encoding/gob" 10 "io" 11 12 "github.com/cilium/cilium/pkg/byteorder" 13 ) 14 15 // Below constants are based on the ones from <linux/perf_event.h>. 16 const ( 17 // EventSample is equivalent to PERF_RECORD_SAMPLE 18 EventSample = 9 19 // RecordLost is equivalent to PERF_RECORD_LOST 20 RecordLost = 2 21 ) 22 23 // Meta is used by readers to get information about the payload. 24 type Meta struct { 25 Size uint32 26 _ [28]byte // Reserved 28 bytes for future fields. 27 } 28 29 // UnmarshalBinary decodes the metadata from its binary representation. 30 func (meta *Meta) UnmarshalBinary(data []byte) error { 31 return meta.ReadBinary(bytes.NewReader(data)) 32 } 33 34 // MarshalBinary encodes the metadata into its binary representation. 35 func (meta *Meta) MarshalBinary() ([]byte, error) { 36 var buf bytes.Buffer 37 if err := meta.WriteBinary(&buf); err != nil { 38 return nil, err 39 } 40 return buf.Bytes(), nil 41 } 42 43 // ReadBinary reads the metadata from its binary representation. 44 func (meta *Meta) ReadBinary(r io.Reader) error { 45 return binary.Read(r, byteorder.Native, meta) 46 } 47 48 // WriteBinary writes the metadata into its binary representation. 49 func (meta *Meta) WriteBinary(w io.Writer) error { 50 return binary.Write(w, byteorder.Native, meta) 51 } 52 53 // Payload is the structure used when copying events from the main monitor. 54 type Payload struct { 55 Data []byte 56 CPU int 57 Lost uint64 58 Type int 59 } 60 61 // Decode decodes the payload from its binary representation. 62 func (pl *Payload) Decode(data []byte) error { 63 // Note that this method can't be named UnmarshalBinary, because the gob encoder would call 64 // this method, resulting in infinite recursion. 65 return pl.ReadBinary(bytes.NewBuffer(data)) 66 } 67 68 // Encode encodes the payload into its binary representation. 69 func (pl *Payload) Encode() ([]byte, error) { 70 // Note that this method can't be named MarshalBinary, because the gob encoder would call 71 // this method, resulting in infinite recursion. 72 var buf bytes.Buffer 73 if err := pl.WriteBinary(&buf); err != nil { 74 return nil, err 75 } 76 return buf.Bytes(), nil 77 } 78 79 // ReadBinary reads the payload from its binary representation. 80 func (pl *Payload) ReadBinary(r io.Reader) error { 81 dec := gob.NewDecoder(r) 82 return pl.DecodeBinary(dec) 83 } 84 85 // WriteBinary writes the payload into its binary representation. 86 func (pl *Payload) WriteBinary(w io.Writer) error { 87 enc := gob.NewEncoder(w) 88 return pl.EncodeBinary(enc) 89 } 90 91 // EncodeBinary writes the payload into its binary representation. 92 func (pl *Payload) EncodeBinary(enc *gob.Encoder) error { 93 return enc.Encode(pl) 94 } 95 96 // DecodeBinary reads the payload from its binary representation. 97 func (pl *Payload) DecodeBinary(dec *gob.Decoder) error { 98 return dec.Decode(pl) 99 }