Skip to content

Commit

Permalink
Enable Pfcwd Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
zbud-msft committed Dec 19, 2024
1 parent 6eade51 commit 1b4313e
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 61 deletions.
93 changes: 93 additions & 0 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,99 @@ func createKeepAliveServer(t *testing.T, port int64) *Server {
return s
}

func TestPFCWDErrors(t *testing.T) {
s := createServer(t, 8081)
go runServer(t, s)

mock := gomonkey.ApplyFunc(sdc.GetPfcwdMap, func() (map[string]map[string]string, error) {
return nil, fmt.Errorf("Mock error")
})
defer mock.Reset()

fileName := "../testdata/COUNTERS:Ethernet_wildcard_alias.txt"
countersEthernetWildcardByte, err := ioutil.ReadFile(fileName)
if err != nil {
t.Fatalf("read file %v err: %v", fileName, err)
}
var countersEthernetWildcardJson interface{}
json.Unmarshal(countersEthernetWildcardByte, &countersEthernetWildcardJson)

tests := []struct {
desc string
q client.Query
wantNoti []client.Notification
poll int
}{
{
desc: "query COUNTERS/Ethernet*",
poll: 1,
q: client.Query{
Target: "COUNTERS_DB",
Type: client.Poll,
Queries: []client.Path{{"COUNTERS", "Ethernet*"}},
TLS: &tls.Config{InsecureSkipVerify: true},
},
wantNoti: []client.Notification{
client.Update{Path: []string{"COUNTERS", "Ethernet*"}, TS: time.Unix(0, 200), Val: countersEthernetWildcardJson},
client.Update{Path: []string{"COUNTERS", "Ethernet*"}, TS: time.Unix(0, 200), Val: countersEthernetWildcardJson},
},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
q := tt.q
q.Addrs = []string{"127.0.0.1:8081"}
c := client.New()
var gotNoti []client.Notification
var mutexGotNoti sync.Mutex
q.NotificationHandler = func(n client.Notification) error {
mutexGotNoti.Lock()
if nn, ok := n.(client.Update); ok {
nn.TS = time.Unix(0, 200)
gotNoti = append(gotNoti, nn)
}
mutexGotNoti.Unlock()
return nil
}

wg := new(sync.WaitGroup)
wg.Add(1)

go func() {
defer wg.Done()
if err := c.Subscribe(context.Background(), q); err != nil {
t.Errorf("c.Subscribe(): got error %v, expected nil", err)
}
}()

wg.Wait()

for i := 0; i < tt.poll; i++ {
err := c.Poll()
if err != nil {
t.Errorf("c.Poll(): got error %v, expected nil", err)
}
}

if len(gotNoti) == 0 {
t.Errorf("Expected non zero length of notifications")
}

mutexGotNoti.Lock()
if diff := pretty.Compare(tt.wantNoti, gotNoti); diff != "" {
t.Log("\n Want: \n", tt.wantNoti)
t.Log("\n Got : \n", gotNoti)
t.Errorf("unexpected updates:\n%s", diff)
}
mutexGotNoti.Unlock()
c.Close()
})
}
s.s.Stop()
}


// runTestGet requests a path from the server by Get grpc call, and compares if
// the return code and response value are expected.
func runTestGet(t *testing.T, ctx context.Context, gClient pb.GNMIClient, pathTarget string,
Expand Down
2 changes: 1 addition & 1 deletion sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]
}
err = initCountersPfcwdNameMap()
if err != nil {
return err
log.Errorf("Could not create CountersPfcwdNameMap: %v", err)
}
}

Expand Down
21 changes: 16 additions & 5 deletions sonic_data_client/virtual_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func initAliasMap() error {
func initCountersPfcwdNameMap() error {
var err error
if len(countersPfcwdNameMap) == 0 {
countersPfcwdNameMap, err = getPfcwdMap()
countersPfcwdNameMap, err = GetPfcwdMap()
if err != nil {
return err
}
Expand All @@ -119,7 +119,7 @@ func initCountersPfcwdNameMap() error {
}

// Get the mapping between sonic interface name and oids of their PFC-WD enabled queues in COUNTERS_DB
func getPfcwdMap() (map[string]map[string]string, error) {
func GetPfcwdMap() (map[string]map[string]string, error) {
var pfcwdName_map = make(map[string]map[string]string)

dbName := "CONFIG_DB"
Expand All @@ -135,7 +135,7 @@ func getPfcwdMap() (map[string]map[string]string, error) {
return nil, err
}

keyName := fmt.Sprintf("PFC_WD_TABLE%v*", separator)
keyName := fmt.Sprintf("PFC_WD%v*", separator)
resp, err := redisDb.Keys(keyName).Result()
if err != nil {
log.V(1).Infof("redis get keys failed for %v in namsepace %v, key = %v, err: %v", dbName, namespace, keyName, err)
Expand All @@ -149,7 +149,10 @@ func getPfcwdMap() (map[string]map[string]string, error) {
}

for _, key := range resp {
name := key[13:]
if strings.Contains(key, "GLOBAL") || strings.Contains(key, "global") { // ignore PFC_WD|global / PFC_WD|GLOBAL
continue
}
name := key[7:]
pfcwdName_map[name] = make(map[string]string)
}

Expand All @@ -164,7 +167,15 @@ func getPfcwdMap() (map[string]map[string]string, error) {
log.V(1).Infof("PFC WD not enabled on device")
return nil, nil
}
qos_key := resp[0]

var qos_key string
for _, key := range resp {
if strings.Contains(key, "GLOBAL") || strings.Contains(key, "global") { // ignore PORT_QOS_MAP|global / PORT_QOS_MAP|GLOBAL
continue
}
qos_key = key
break
}

fieldName := "pfc_enable"
priorities, err := redisDb.HGet(qos_key, fieldName).Result()
Expand Down
116 changes: 61 additions & 55 deletions testdata/CONFIG_PFCWD_PORTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,169 +3,175 @@
"3": "3",
"4": "4"
},
"PFC_WD_TABLE|Ethernet0": {
"PFC_WD|GLOBAL": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet1": {
"PFC_WD|Ethernet0": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet10": {
"PFC_WD|Ethernet1": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet11": {
"PFC_WD|Ethernet10": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet12": {
"PFC_WD|Ethernet11": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet13": {
"PFC_WD|Ethernet12": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet14": {
"PFC_WD|Ethernet13": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet15": {
"PFC_WD|Ethernet14": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet16": {
"PFC_WD|Ethernet15": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet17": {
"PFC_WD|Ethernet16": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet18": {
"PFC_WD|Ethernet17": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet19": {
"PFC_WD|Ethernet18": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet2": {
"PFC_WD|Ethernet19": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet20": {
"PFC_WD|Ethernet2": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet21": {
"PFC_WD|Ethernet20": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet22": {
"PFC_WD|Ethernet21": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet23": {
"PFC_WD|Ethernet22": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet24": {
"PFC_WD|Ethernet23": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet25": {
"PFC_WD|Ethernet24": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet26": {
"PFC_WD|Ethernet25": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet27": {
"PFC_WD|Ethernet26": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet28": {
"PFC_WD|Ethernet27": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet29": {
"PFC_WD|Ethernet28": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet3": {
"PFC_WD|Ethernet29": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet30": {
"PFC_WD|Ethernet3": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet31": {
"PFC_WD|Ethernet30": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet32": {
"PFC_WD|Ethernet31": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet33": {
"PFC_WD|Ethernet32": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet34": {
"PFC_WD|Ethernet33": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet35": {
"PFC_WD|Ethernet34": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet36": {
"PFC_WD|Ethernet35": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet37": {
"PFC_WD|Ethernet36": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet38": {
"PFC_WD|Ethernet37": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet39": {
"PFC_WD|Ethernet38": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet4": {
"PFC_WD|Ethernet39": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet40": {
"PFC_WD|Ethernet4": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet41": {
"PFC_WD|Ethernet40": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet42": {
"PFC_WD|Ethernet41": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet43": {
"PFC_WD|Ethernet42": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet44": {
"PFC_WD|Ethernet43": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet45": {
"PFC_WD|Ethernet44": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet46": {
"PFC_WD|Ethernet45": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet47": {
"PFC_WD|Ethernet46": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet48": {
"PFC_WD|Ethernet47": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet5": {
"PFC_WD|Ethernet48": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet52": {
"PFC_WD|Ethernet5": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet56": {
"PFC_WD|Ethernet52": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet6": {
"PFC_WD|Ethernet56": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet60": {
"PFC_WD|Ethernet6": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet64": {
"PFC_WD|Ethernet60": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet68": {
"PFC_WD|Ethernet64": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet7": {
"PFC_WD|Ethernet68": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet8": {
"PFC_WD|Ethernet7": {
"action": "drop"
},
"PFC_WD_TABLE|Ethernet9": {
"PFC_WD|Ethernet8": {
"action": "drop"
},
"PFC_WD|Ethernet9": {
"action": "drop"
},
"PORT_QOS_MAP|Ethernet0,Ethernet1,Ethernet2,Ethernet3,Ethernet4,Ethernet5,Ethernet6,Ethernet7,Ethernet8,Ethernet9,Ethernet10,Ethernet11,Ethernet12,Ethernet13,Ethernet14,Ethernet15,Ethernet16,Ethernet17,Ethernet18,Ethernet19,Ethernet20,Ethernet21,Ethernet22,Ethernet23,Ethernet24,Ethernet25,Ethernet26,Ethernet27,Ethernet28,Ethernet29,Ethernet30,Ethernet31,Ethernet32,Ethernet33,Ethernet34,Ethernet35,Ethernet36,Ethernet37,Ethernet38,Ethernet39,Ethernet40,Ethernet41,Ethernet42,Ethernet43,Ethernet44,Ethernet45,Ethernet46,Ethernet47,Ethernet48,Ethernet52,Ethernet56,Ethernet60,Ethernet64,Ethernet68": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|global": {
"dummy": "value"
}
}
}

0 comments on commit 1b4313e

Please sign in to comment.