From 201f159836b0430d2a8b1b3c735ef16d7e67a8d3 Mon Sep 17 00:00:00 2001 From: Nikhil Malik Date: Fri, 3 Nov 2023 15:21:53 +0900 Subject: [PATCH] gh-428 : Associate service name with LB rule and connection entries --- api/models/conntrack_entry.go | 3 +++ api/models/loadbalance_entry.go | 3 +++ api/restapi/embedded_spec.go | 20 ++++++++++++++++++++ api/restapi/handler/conntrack.go | 3 ++- api/restapi/handler/loadbalancer.go | 6 +++++- api/swagger.yml | 8 ++++++-- common/common.go | 4 ++++ loxinet/dpbroker.go | 12 +++++++++++- loxinet/dpebpf_linux.go | 1 + loxinet/rules.go | 3 +++ 10 files changed, 58 insertions(+), 5 deletions(-) diff --git a/api/models/conntrack_entry.go b/api/models/conntrack_entry.go index f6cba6dfc..292723023 100644 --- a/api/models/conntrack_entry.go +++ b/api/models/conntrack_entry.go @@ -38,6 +38,9 @@ type ConntrackEntry struct { // value for access protocol Protocol string `json:"protocol,omitempty"` + // Connection's Service Name + ServName string `json:"servName,omitempty"` + // IP address for externel access SourceIP string `json:"sourceIP,omitempty"` diff --git a/api/models/loadbalance_entry.go b/api/models/loadbalance_entry.go index e2c1c1fcd..778477599 100644 --- a/api/models/loadbalance_entry.go +++ b/api/models/loadbalance_entry.go @@ -330,6 +330,9 @@ type LoadbalanceEntryServiceArguments struct { // value for monitoring enabled or not Monitor bool `json:"monitor,omitempty"` + // service name + Name string `json:"name,omitempty"` + // port number for the access Port int64 `json:"port,omitempty"` diff --git a/api/restapi/embedded_spec.go b/api/restapi/embedded_spec.go index b7e8f4e45..e3fd0e31f 100644 --- a/api/restapi/embedded_spec.go +++ b/api/restapi/embedded_spec.go @@ -3343,6 +3343,10 @@ func init() { "description": "value for access protocol", "type": "string" }, + "servName": { + "description": "Connection's Service Name", + "type": "string" + }, "sourceIP": { "description": "IP address for externel access", "type": "string" @@ -3744,6 +3748,10 @@ func init() { "description": "value for monitoring enabled or not", "type": "boolean" }, + "name": { + "description": "service name", + "type": "string" + }, "port": { "description": "port number for the access", "type": "integer" @@ -7722,6 +7730,10 @@ func init() { "description": "value for access protocol", "type": "string" }, + "servName": { + "description": "Connection's Service Name", + "type": "string" + }, "sourceIP": { "description": "IP address for externel access", "type": "string" @@ -8097,6 +8109,10 @@ func init() { "description": "value for monitoring enabled or not", "type": "boolean" }, + "name": { + "description": "service name", + "type": "string" + }, "port": { "description": "port number for the access", "type": "integer" @@ -8196,6 +8212,10 @@ func init() { "description": "value for monitoring enabled or not", "type": "boolean" }, + "name": { + "description": "service name", + "type": "string" + }, "port": { "description": "port number for the access", "type": "integer" diff --git a/api/restapi/handler/conntrack.go b/api/restapi/handler/conntrack.go index bd0f9e398..9bc26a64b 100644 --- a/api/restapi/handler/conntrack.go +++ b/api/restapi/handler/conntrack.go @@ -16,6 +16,7 @@ package handler import ( + "github.com/loxilb-io/loxilb/api/models" "github.com/loxilb-io/loxilb/api/restapi/operations" tk "github.com/loxilb-io/loxilib" @@ -44,7 +45,7 @@ func ConfigGetConntrack(params operations.GetConfigConntrackAllParams) middlewar tmpResult.Protocol = conntrack.Proto tmpResult.SourceIP = conntrack.Sip.String() tmpResult.SourcePort = int64(conntrack.Sport) - + tmpResult.ServName = conntrack.ServiceName result = append(result, &tmpResult) } return operations.NewGetConfigConntrackAllOK().WithPayload(&operations.GetConfigConntrackAllOKBody{CtAttr: result}) diff --git a/api/restapi/handler/loadbalancer.go b/api/restapi/handler/loadbalancer.go index d56d079e8..221ab1b01 100644 --- a/api/restapi/handler/loadbalancer.go +++ b/api/restapi/handler/loadbalancer.go @@ -42,6 +42,7 @@ func ConfigPostLoadbalancer(params operations.PostConfigLoadbalancerParams) midd lbRules.Serv.ProbePort = params.Attr.ServiceArguments.Probeport lbRules.Serv.ProbeReq = params.Attr.ServiceArguments.Probereq lbRules.Serv.ProbeResp = params.Attr.ServiceArguments.Proberesp + lbRules.Serv.Name = params.Attr.ServiceArguments.Name if lbRules.Serv.Proto == "sctp" { for _, data := range params.Attr.SecondaryIPs { @@ -101,6 +102,7 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid // Get LB rules tk.LogIt(tk.LogDebug, "[API] Load balancer %s API called. url : %s\n", params.HTTPRequest.Method, params.HTTPRequest.URL) + res, err := ApiHooks.NetLbRuleGet() if err != nil { tk.LogIt(tk.LogDebug, "[API] Error occur : %v\n", err) @@ -111,7 +113,7 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid for _, lb := range res { var tmpLB models.LoadbalanceEntry var tmpSvc models.LoadbalanceEntryServiceArguments - + // Service Arg match tmpSvc.ExternalIP = lb.Serv.ServIP tmpSvc.Bgp = lb.Serv.Bgp @@ -125,6 +127,7 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid tmpSvc.Managed = lb.Serv.Managed tmpSvc.Probetype = lb.Serv.ProbeType tmpSvc.Probeport = lb.Serv.ProbePort + tmpSvc.Name = lb.Serv.Name tmpLB.ServiceArguments = &tmpSvc @@ -149,6 +152,7 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid } return operations.NewGetConfigLoadbalancerAllOK().WithPayload(&operations.GetConfigLoadbalancerAllOKBody{LbAttr: result}) } + func ConfigDeleteAllLoadbalancer(params operations.DeleteConfigLoadbalancerAllParams) middleware.Responder { tk.LogIt(tk.LogDebug, "[API] Load balancer %s API called. url : %s\n", params.HTTPRequest.Method, params.HTTPRequest.URL) diff --git a/api/swagger.yml b/api/swagger.yml index d7e81ae5a..8541303cb 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -193,7 +193,6 @@ paths: description: Maintanence mode schema: $ref: '#/definitions/Error' - #---------------------------------------------- # Conntrack #---------------------------------------------- @@ -224,7 +223,6 @@ paths: schema: $ref: '#/definitions/Error' - #---------------------------------------------- # Port Dump #---------------------------------------------- @@ -2331,6 +2329,9 @@ definitions: type: integer format: int32 description: value for inactivity timeout (in seconds) + name: + type: string + description: service name endpoints: type: array @@ -2435,6 +2436,9 @@ definitions: bytes: type: integer description: Packet bytes of the conntrack + servName: + type: string + description: Connection's Service Name PortEntry: type: object diff --git a/common/common.go b/common/common.go index 1a6a217d7..317c690af 100644 --- a/common/common.go +++ b/common/common.go @@ -513,6 +513,8 @@ type LbServiceArg struct { ProbeReq string `json:"probereq"` // ProbeResp - Response string for liveness check ProbeResp string `json:"proberesp"` + // Name - Service name + Name string `json:"name"` } // LbEndPointArg - Information related to load-balancer end-point @@ -566,6 +568,8 @@ type CtInfo struct { Pkts uint64 `json:"packets"` // Bytes - bytes tracked by ct entry Bytes uint64 `json:"bytes"` + // ServiceName - Connection's service name + ServiceName string `json:"servName"` } // UlClArg - ulcl argument information diff --git a/loxinet/dpbroker.go b/loxinet/dpbroker.go index 14b7162ba..c29285ee7 100644 --- a/loxinet/dpbroker.go +++ b/loxinet/dpbroker.go @@ -311,6 +311,7 @@ type DpCtInfo struct { ServProto string `json:"servproto"` L4ServPort uint16 `json:"l4servproto"` BlockNum uint16 `json:"blocknum"` + RuleID uint32 `json:"ruleid"` } const ( @@ -802,6 +803,8 @@ func DpWorker(dp *DpH, f chan int, ch chan interface{}) { // DpMapGetCt4 - get DP conntrack information as a map func (dp *DpH) DpMapGetCt4() []cmn.CtInfo { var CtInfoArr []cmn.CtInfo + var servName string + nTable := new(TableDpWorkQ) nTable.Work = DpMapGet nTable.Name = MapNameCt4 @@ -814,9 +817,16 @@ func (dp *DpH) DpMapGetCt4() []cmn.CtInfo { switch r := ret.(type) { case map[string]*DpCtInfo: for _, dCti := range r { + + mh.mtx.Lock() + rule := mh.zr.Rules.GetNatLbRuleByID(dCti.RuleID) + mh.mtx.Unlock() + if rule != nil { + servName = rule.name + } cti := cmn.CtInfo{Dip: dCti.DIP, Sip: dCti.SIP, Dport: dCti.Dport, Sport: dCti.Sport, Proto: dCti.Proto, CState: dCti.CState, CAct: dCti.CAct, - Pkts: dCti.Packets, Bytes: dCti.Bytes} + Pkts: dCti.Packets, Bytes: dCti.Bytes, ServiceName: servName} CtInfoArr = append(CtInfoArr, cti) } } diff --git a/loxinet/dpebpf_linux.go b/loxinet/dpebpf_linux.go index 38d0ae2ca..bc84e81ad 100644 --- a/loxinet/dpebpf_linux.go +++ b/loxinet/dpebpf_linux.go @@ -1325,6 +1325,7 @@ func (e *DpEbpfH) DpTableGet(w *TableDpWorkQ) (DpRetT, error) { goCt4Ent.Bytes += b goCt4Ent.Packets += p } + goCt4Ent.RuleID = uint32(act.rid) //fmt.Println(goCt4Ent) ctMap[goCt4Ent.Key()] = goCt4Ent } diff --git a/loxinet/rules.go b/loxinet/rules.go index 76166610e..c0f9de542 100644 --- a/loxinet/rules.go +++ b/loxinet/rules.go @@ -272,6 +272,7 @@ type ruleEnt struct { act ruleAct secIP []ruleNatSIP stat ruleStat + name string } type ruleTable struct { @@ -743,6 +744,7 @@ func (R *RuleH) GetNatLbRule() ([]cmn.LbRuleMod, error) { ret.Serv.ProbePort = data.hChk.prbPort ret.Serv.ProbeReq = data.hChk.prbReq ret.Serv.ProbeResp = data.hChk.prbResp + ret.Serv.Name = data.name for _, sip := range data.secIP { ret.SecIPs = append(ret.SecIPs, cmn.LbSecIPArg{SecIP: sip.sIP.String()}) @@ -1257,6 +1259,7 @@ func (R *RuleH) AddNatLbRule(serv cmn.LbServiceArg, servSecIPs []cmn.LbSecIPArg, r := new(ruleEnt) r.tuples = rt r.zone = R.zone + r.name = serv.Name if serv.Mode == cmn.LBModeFullNAT || serv.Mode == cmn.LBModeOneArm { r.act.actType = RtActFullNat } else {