@@ -24,17 +24,21 @@ import (
2424 "strings"
2525
2626 "github.com/deepflowio/deepflow/message/trident"
27+ flow_metrics "github.com/deepflowio/deepflow/server/libs/flow-metrics"
2728 "github.com/deepflowio/deepflow/server/libs/utils"
2829 "github.com/google/gopacket/layers"
2930)
3031
3132type ServiceTable struct {
32- epcIDIPv4Table [trident .ServiceProtocol_UDP_SERVICE + 1 ]map [uint64 ]uint32
33- epcIDIPv6Table map [EpcIDIPv6Key ]uint32
34- podClusterIDTable map [uint64 ]uint32
35- podGroupIDTable map [uint64 ]uint32
36- customServiceIpv4Table map [uint64 ]uint32
37- customServiceIpv6Table map [EpcIDIPv6Key ]uint32
33+ epcIDIPv4Table [trident .ServiceProtocol_UDP_SERVICE + 1 ]map [uint64 ]uint32
34+ epcIDIPv6Table map [EpcIDIPv6Key ]uint32
35+ podClusterIDTable map [uint64 ]uint32
36+ podGroupIDTable map [uint64 ]uint32
37+ customServiceIpv4Table map [uint64 ]uint32
38+ customServiceIpv6Table map [EpcIDIPv6Key ]uint32
39+ customServicePodGroup map [uint32 ]uint32
40+ customServicePodService map [uint32 ]uint32
41+ customServiceChost map [uint32 ]uint32
3842}
3943
4044type EpcIDIPv6Key struct {
@@ -113,12 +117,35 @@ func (s *ServiceTable) QueryPodService(podID, podNodeID, podClusterID, podGroupI
113117 return s .epcIDIPv4Table [serviceProtocol ][genEpcIDIPv4Key (epcID , ipv4 , serverPort )]
114118}
115119
116- func (s * ServiceTable ) QueryCustomService (epcID int32 , isIPv6 bool , ipv4 uint32 , ipv6 net.IP , serverPort uint16 ) uint32 {
120+ func (s * ServiceTable ) QueryCustomService (epcID int32 , isIPv6 bool , ipv4 uint32 , ipv6 net.IP , serverPort uint16 , podServiceId , podGroupId , l3DeviceId uint32 , l3DeviceType uint8 ) uint32 {
117121 // for performance optimization, return directly. Since when epcID <= 0, there is no Service information.
118122 if epcID <= 0 {
119123 return 0
120124 }
121125
126+ // 1. pod service
127+ if podServiceId != 0 {
128+ serviceId := s .customServicePodService [podServiceId ]
129+ if serviceId != 0 {
130+ return serviceId
131+ }
132+ }
133+ // 2. pod group
134+ if podGroupId != 0 {
135+ serviceId := s .customServicePodGroup [podGroupId ]
136+ if serviceId != 0 {
137+ return serviceId
138+ }
139+ }
140+ // 3. chost
141+ if l3DeviceId != 0 && l3DeviceType == uint8 (flow_metrics .VMDevice ) {
142+ serviceId := s .customServiceChost [l3DeviceId ]
143+ if serviceId != 0 {
144+ return serviceId
145+ }
146+ }
147+
148+ // 4. port ip
122149 if isIPv6 {
123150 if len (s .customServiceIpv6Table ) == 0 {
124151 return 0
@@ -141,11 +168,14 @@ func (s *ServiceTable) QueryCustomService(epcID int32, isIPv6 bool, ipv4 uint32,
141168
142169func NewServiceTable (grpcServices []* trident.ServiceInfo ) * ServiceTable {
143170 s := & ServiceTable {
144- epcIDIPv6Table : make (map [EpcIDIPv6Key ]uint32 ),
145- podClusterIDTable : make (map [uint64 ]uint32 ),
146- podGroupIDTable : make (map [uint64 ]uint32 ),
147- customServiceIpv4Table : make (map [uint64 ]uint32 ),
148- customServiceIpv6Table : make (map [EpcIDIPv6Key ]uint32 ),
171+ epcIDIPv6Table : make (map [EpcIDIPv6Key ]uint32 ),
172+ podClusterIDTable : make (map [uint64 ]uint32 ),
173+ podGroupIDTable : make (map [uint64 ]uint32 ),
174+ customServiceIpv4Table : make (map [uint64 ]uint32 ),
175+ customServiceIpv6Table : make (map [EpcIDIPv6Key ]uint32 ),
176+ customServicePodGroup : make (map [uint32 ]uint32 ),
177+ customServicePodService : make (map [uint32 ]uint32 ),
178+ customServiceChost : make (map [uint32 ]uint32 ),
149179 }
150180 for i := range s .epcIDIPv4Table {
151181 s .epcIDIPv4Table [i ] = make (map [uint64 ]uint32 )
@@ -168,7 +198,10 @@ func NewServiceTable(grpcServices []*trident.ServiceInfo) *ServiceTable {
168198 s .podClusterIDTable [genPodXIDKey (podClusterId , trident .ServiceProtocol_ANY , 0 )] = serviceId
169199 // Service from 'pod + port' generate 'pod_group_id + port' table
170200 case trident .ServiceType_POD_SERVICE_POD_GROUP :
171- podGroupIds := svc .GetPodGroupIds () // FIXME @zhuofeng
201+ podGroupIds := svc .GetPodGroupIds ()
202+ if len (podGroupIds ) == 0 {
203+ break
204+ }
172205 podGroupId := podGroupIds [0 ]
173206 for _ , port := range svc .GetServerPorts () {
174207 s .podGroupIDTable [genPodXIDKey (podGroupId , protocol , uint16 (port ))] = serviceId
@@ -239,6 +272,18 @@ func (s *ServiceTable) addCustomService(svc *trident.ServiceInfo) {
239272 epcId := int32 (svc .GetEpcId ())
240273 serviceId := svc .GetId ()
241274
275+ for _ , podGroupId := range svc .GetPodGroupIds () {
276+ s .customServicePodGroup [podGroupId ] = serviceId
277+ }
278+
279+ for _ , podServiceId := range svc .GetPodServiceIds () {
280+ s .customServicePodService [podServiceId ] = serviceId
281+ }
282+
283+ for _ , chostId := range svc .GetChostIds () {
284+ s .customServiceChost [chostId ] = serviceId
285+ }
286+
242287 ips := svc .GetIps ()
243288 if len (ips ) == 0 {
244289 return
@@ -264,6 +309,10 @@ func (s *ServiceTable) addCustomService(svc *trident.ServiceInfo) {
264309 }
265310}
266311
312+ func (s * ServiceTable ) addCustomServicePodGroup (svc * trident.ServiceInfo ) {
313+
314+ }
315+
267316func (s * ServiceTable ) String () string {
268317 sb := & strings.Builder {}
269318
@@ -382,5 +431,33 @@ func (s *ServiceTable) String() string {
382431 epcID , ipv6 , _ , port := parseEpcIDIPv6Key (& epcIP )
383432 fmt .Fprintf (sb , " %-6d %-15s %-15d %-6d \n " , epcID , ipv6 , port , id )
384433 }
434+
435+ if len (s .customServicePodService ) > 0 {
436+ sb .WriteString ("\n podServiceId custom service\n " )
437+ sb .WriteString ("\n 7 podServiceId serviceID\n " )
438+ sb .WriteString ("------------------------------------------------------\n " )
439+ for podServiceId , serviceId := range s .customServicePodService {
440+ fmt .Fprintf (sb , " %-11s %-15d \n " , podServiceId , serviceId )
441+ }
442+ }
443+
444+ if len (s .customServicePodGroup ) > 0 {
445+ sb .WriteString ("\n podServiceId custom service\n " )
446+ sb .WriteString ("\n 8 podGrouopId serviceID\n " )
447+ sb .WriteString ("------------------------------------------------------\n " )
448+ for podGroupId , serviceId := range s .customServicePodGroup {
449+ fmt .Fprintf (sb , " %-11s %-15d \n " , podGroupId , serviceId )
450+ }
451+ }
452+
453+ if len (s .customServiceChost ) > 0 {
454+ sb .WriteString ("\n chostId custom service\n " )
455+ sb .WriteString ("\n 9 chostId serviceID\n " )
456+ sb .WriteString ("------------------------------------------------------\n " )
457+ for chostId , serviceId := range s .customServiceChost {
458+ fmt .Fprintf (sb , " %-11s %-15d \n " , chostId , serviceId )
459+ }
460+ }
461+
385462 return sb .String ()
386463}
0 commit comments