@@ -3,12 +3,9 @@ package exporter
33import (
44 "context"
55
6- "github.com/sirupsen/logrus"
7- "google.golang.org/protobuf/types/known/timestamppb"
8-
96 "github.com/netobserv/netobserv-ebpf-agent/pkg/flow"
107 "github.com/netobserv/netobserv-ebpf-agent/pkg/grpc"
11- "github.com/netobserv/netobserv-ebpf-agent/pkg/pbflow "
8+ "github.com/sirupsen/logrus "
129)
1310
1411var glog = logrus .WithField ("component" , "exporter/GRPCProto" )
@@ -37,99 +34,13 @@ func StartGRPCProto(hostPort string) (*GRPCProto, error) {
3734func (g * GRPCProto ) ExportFlows (input <- chan []* flow.Record ) {
3835 log := glog .WithField ("collector" , g .hostPort )
3936 for inputRecords := range input {
40- entries := make ([]* pbflow.Record , 0 , len (inputRecords ))
41- for _ , record := range inputRecords {
42- entries = append (entries , flowToPB (record ))
43- }
44- log .Debugf ("sending %d records" , len (entries ))
45- if _ , err := g .clientConn .Client ().Send (context .TODO (), & pbflow.Records {
46- Entries : entries ,
47- }); err != nil {
37+ pbRecords := flowsToPB (inputRecords )
38+ log .Debugf ("sending %d records" , len (pbRecords .Entries ))
39+ if _ , err := g .clientConn .Client ().Send (context .TODO (), pbRecords ); err != nil {
4840 log .WithError (err ).Error ("couldn't send flow records to collector" )
4941 }
50-
5142 }
5243 if err := g .clientConn .Close (); err != nil {
5344 log .WithError (err ).Warn ("couldn't close flow export client" )
5445 }
5546}
56-
57- func v4FlowToPB (fr * flow.Record ) * pbflow.Record {
58- return & pbflow.Record {
59- EthProtocol : uint32 (fr .EthProtocol ),
60- Direction : pbflow .Direction (fr .Direction ),
61- DataLink : & pbflow.DataLink {
62- SrcMac : macToUint64 (& fr .DataLink .SrcMac ),
63- DstMac : macToUint64 (& fr .DataLink .DstMac ),
64- },
65- Network : & pbflow.Network {
66- SrcAddr : & pbflow.IP {IpFamily : & pbflow.IP_Ipv4 {Ipv4 : fr .Network .SrcAddr .IntEncodeV4 ()}},
67- DstAddr : & pbflow.IP {IpFamily : & pbflow.IP_Ipv4 {Ipv4 : fr .Network .DstAddr .IntEncodeV4 ()}},
68- },
69- Transport : & pbflow.Transport {
70- Protocol : uint32 (fr .Transport .Protocol ),
71- SrcPort : uint32 (fr .Transport .SrcPort ),
72- DstPort : uint32 (fr .Transport .DstPort ),
73- },
74- Bytes : fr .Bytes ,
75- TimeFlowStart : & timestamppb.Timestamp {
76- Seconds : fr .TimeFlowStart .Unix (),
77- Nanos : int32 (fr .TimeFlowStart .Nanosecond ()),
78- },
79- TimeFlowEnd : & timestamppb.Timestamp {
80- Seconds : fr .TimeFlowEnd .Unix (),
81- Nanos : int32 (fr .TimeFlowEnd .Nanosecond ()),
82- },
83- Packets : uint64 (fr .Packets ),
84- Interface : fr .Interface ,
85- }
86- }
87-
88- func v6FlowToPB (fr * flow.Record ) * pbflow.Record {
89- return & pbflow.Record {
90- EthProtocol : uint32 (fr .EthProtocol ),
91- Direction : pbflow .Direction (fr .Direction ),
92- DataLink : & pbflow.DataLink {
93- SrcMac : macToUint64 (& fr .DataLink .SrcMac ),
94- DstMac : macToUint64 (& fr .DataLink .DstMac ),
95- },
96- Network : & pbflow.Network {
97- SrcAddr : & pbflow.IP {IpFamily : & pbflow.IP_Ipv6 {Ipv6 : fr .Network .SrcAddr [:]}},
98- DstAddr : & pbflow.IP {IpFamily : & pbflow.IP_Ipv6 {Ipv6 : fr .Network .DstAddr [:]}},
99- },
100- Transport : & pbflow.Transport {
101- Protocol : uint32 (fr .Transport .Protocol ),
102- SrcPort : uint32 (fr .Transport .SrcPort ),
103- DstPort : uint32 (fr .Transport .DstPort ),
104- },
105- Bytes : fr .Bytes ,
106- TimeFlowStart : & timestamppb.Timestamp {
107- Seconds : fr .TimeFlowStart .Unix (),
108- Nanos : int32 (fr .TimeFlowStart .Nanosecond ()),
109- },
110- TimeFlowEnd : & timestamppb.Timestamp {
111- Seconds : fr .TimeFlowEnd .Unix (),
112- Nanos : int32 (fr .TimeFlowEnd .Nanosecond ()),
113- },
114- Packets : uint64 (fr .Packets ),
115- Interface : fr .Interface ,
116- }
117- }
118-
119- func flowToPB (fr * flow.Record ) * pbflow.Record {
120- if fr .EthProtocol == flow .IPv6Type {
121- return v6FlowToPB (fr )
122- }
123- return v4FlowToPB (fr )
124- }
125-
126- // Mac bytes are encoded in the same order as in the array. This is, a Mac
127- // like 11:22:33:44:55:66 will be encoded as 0x112233445566
128- func macToUint64 (m * flow.MacAddr ) uint64 {
129- return uint64 (m [5 ]) |
130- (uint64 (m [4 ]) << 8 ) |
131- (uint64 (m [3 ]) << 16 ) |
132- (uint64 (m [2 ]) << 24 ) |
133- (uint64 (m [1 ]) << 32 ) |
134- (uint64 (m [0 ]) << 40 )
135- }
0 commit comments