Skip to content

Commit e32212a

Browse files
sihuihan88lguohan
authored andcommitted
[lua]: Only set the entry values when they are not nil (sonic-net#446)
* [lua]: Only set the entries when they are not nil Signed-off-by: Sihui Han <[email protected]> * udpate * further fix the detection time nil error Signed-off-by: Sihui Han <[email protected]>
1 parent 6e274fb commit e32212a

File tree

4 files changed

+124
-107
lines changed

4 files changed

+124
-107
lines changed

orchagent/pfc_detect_broadcom.lua

+62-53
Original file line numberDiff line numberDiff line change
@@ -22,69 +22,78 @@ for i = n, 1, -1 do
2222
local pfc_wd_status = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_STATUS')
2323
local pfc_wd_action = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_ACTION')
2424
if pfc_wd_status == 'operational' or pfc_wd_action == 'alert' then
25-
local detection_time = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME'))
26-
local time_left = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT')
27-
if not time_left then
28-
time_left = detection_time
29-
else
30-
time_left = tonumber(time_left)
31-
end
25+
local detection_time = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME')
26+
if detection_time then
27+
detection_time = tonumber(detection_time)
28+
local time_left = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT')
29+
if not time_left then
30+
time_left = detection_time
31+
else
32+
time_left = tonumber(time_left)
33+
end
3234

33-
local queue_index = redis.call('HGET', 'COUNTERS_QUEUE_INDEX_MAP', KEYS[i])
34-
local port_id = redis.call('HGET', 'COUNTERS_QUEUE_PORT_MAP', KEYS[i])
35-
local pfc_rx_pkt_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PKTS'
36-
local pfc_on2off_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_ON2OFF_RX_PKTS'
37-
35+
local queue_index = redis.call('HGET', 'COUNTERS_QUEUE_INDEX_MAP', KEYS[i])
36+
local port_id = redis.call('HGET', 'COUNTERS_QUEUE_PORT_MAP', KEYS[i])
37+
local pfc_rx_pkt_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PKTS'
38+
local pfc_on2off_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_ON2OFF_RX_PKTS'
3839

39-
-- Get all counters
40-
local occupancy_bytes = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_CURR_OCCUPANCY_BYTES'))
41-
local packets = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS'))
42-
local pfc_rx_packets = tonumber(redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key))
43-
local pfc_on2off = tonumber(redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_on2off_key))
44-
local queue_pause_status = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS')
45-
46-
local packets_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last')
47-
local pfc_rx_packets_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last')
48-
local pfc_on2off_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_on2off_key .. '_last')
49-
local queue_pause_status_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS_last')
40+
-- Get all counters
41+
local occupancy_bytes = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_CURR_OCCUPANCY_BYTES')
42+
local packets = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS')
43+
local pfc_rx_packets = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key)
44+
local pfc_on2off = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_on2off_key)
45+
local queue_pause_status = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS')
5046

51-
-- DEBUG CODE START. Uncomment to enable
52-
local debug_storm = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'DEBUG_STORM')
53-
-- DEBUG CODE END.
47+
if occupancy_bytes and packets and pfc_rx_packets and pfc_on2off and queue_pause_status then
48+
occupancy_bytes = tonumber(occupancy_bytes)
49+
packets = tonumber(packets)
50+
pfc_rx_packets = tonumber(pfc_rx_packets)
51+
pfc_on2off = tonumber(pfc_on2off)
5452

55-
-- If this is not a first run, then we have last values available
56-
if packets_last and pfc_rx_packets_last and pfc_on2off_last and queue_pause_status_last then
57-
packets_last = tonumber(packets_last)
58-
pfc_rx_packets_last = tonumber(pfc_rx_packets_last)
59-
pfc_on2off_last = tonumber(pfc_on2off_last)
53+
local packets_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last')
54+
local pfc_rx_packets_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last')
55+
local pfc_on2off_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_on2off_key .. '_last')
56+
local queue_pause_status_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS_last')
6057

61-
-- Check actual condition of queue being in PFC storm
62-
if (occupancy_bytes > 0 and packets - packets_last == 0 and pfc_rx_packets - pfc_rx_packets_last > 0) or
6358
-- DEBUG CODE START. Uncomment to enable
64-
(debug_storm == "enabled") or
59+
local debug_storm = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'DEBUG_STORM')
6560
-- DEBUG CODE END.
66-
(occupancy_bytes == 0 and pfc_rx_packets - pfc_rx_packets_last > 0 and pfc_on2off - pfc_on2off_last == 0 and queue_pause_status_last == 'true' and queue_pause_status == 'true') then
67-
if time_left <= poll_time then
68-
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","storm"]')
69-
is_deadlock = true
70-
time_left = detection_time
71-
else
72-
time_left = time_left - poll_time
73-
end
74-
else
75-
if pfc_wd_action == 'alert' and pfc_wd_status ~= 'operational' then
76-
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","restore"]')
61+
62+
-- If this is not a first run, then we have last values available
63+
if packets_last and pfc_rx_packets_last and pfc_on2off_last and queue_pause_status_last then
64+
packets_last = tonumber(packets_last)
65+
pfc_rx_packets_last = tonumber(pfc_rx_packets_last)
66+
pfc_on2off_last = tonumber(pfc_on2off_last)
67+
68+
-- Check actual condition of queue being in PFC storm
69+
if (occupancy_bytes > 0 and packets - packets_last == 0 and pfc_rx_packets - pfc_rx_packets_last > 0) or
70+
-- DEBUG CODE START. Uncomment to enable
71+
(debug_storm == "enabled") or
72+
-- DEBUG CODE END.
73+
(occupancy_bytes == 0 and pfc_rx_packets - pfc_rx_packets_last > 0 and pfc_on2off - pfc_on2off_last == 0 and queue_pause_status_last == 'true' and queue_pause_status == 'true') then
74+
if time_left <= poll_time then
75+
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","storm"]')
76+
is_deadlock = true
77+
time_left = detection_time
78+
else
79+
time_left = time_left - poll_time
80+
end
81+
else
82+
if pfc_wd_action == 'alert' and pfc_wd_status ~= 'operational' then
83+
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","restore"]')
84+
end
85+
time_left = detection_time
86+
end
7787
end
78-
time_left = detection_time
88+
89+
-- Save values for next run
90+
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS_last', queue_pause_status)
91+
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last', packets)
92+
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT', time_left)
93+
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last', pfc_rx_packets)
94+
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_on2off_key .. '_last', pfc_on2off)
7995
end
8096
end
81-
82-
-- Save values for next run
83-
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_ATTR_PAUSE_STATUS_last', queue_pause_status)
84-
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last', packets)
85-
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT', time_left)
86-
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last', pfc_rx_packets)
87-
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_on2off_key .. '_last', pfc_on2off)
8897
end
8998
end
9099

orchagent/pfc_detect_mellanox.lua

+60-53
Original file line numberDiff line numberDiff line change
@@ -22,67 +22,74 @@ for i = n, 1, -1 do
2222
local pfc_wd_status = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_STATUS')
2323
local pfc_wd_action = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_ACTION')
2424
if pfc_wd_status == 'operational' or pfc_wd_action == 'alert' then
25-
local detection_time = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME'))
26-
local time_left = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT')
27-
if not time_left then
28-
time_left = detection_time
29-
else
30-
time_left = tonumber(time_left)
31-
end
32-
33-
local queue_index = redis.call('HGET', 'COUNTERS_QUEUE_INDEX_MAP', KEYS[i])
34-
local port_id = redis.call('HGET', 'COUNTERS_QUEUE_PORT_MAP', KEYS[i])
35-
local pfc_rx_pkt_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PKTS'
36-
local pfc_duration_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PAUSE_DURATION'
25+
local detection_time = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME')
26+
if detection_time then
27+
detection_time = tonumber(detection_time)
28+
local time_left = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT')
29+
if not time_left then
30+
time_left = detection_time
31+
else
32+
time_left = tonumber(time_left)
33+
end
3734

38-
-- Get all counters
39-
local occupancy_bytes = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_CURR_OCCUPANCY_BYTES'))
40-
local packets = tonumber(redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS'))
41-
local pfc_rx_packets = tonumber(redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key))
42-
local pfc_duration = tonumber(redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_duration_key))
35+
local queue_index = redis.call('HGET', 'COUNTERS_QUEUE_INDEX_MAP', KEYS[i])
36+
local port_id = redis.call('HGET', 'COUNTERS_QUEUE_PORT_MAP', KEYS[i])
37+
local pfc_rx_pkt_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PKTS'
38+
local pfc_duration_key = 'SAI_PORT_STAT_PFC_' .. queue_index .. '_RX_PAUSE_DURATION'
4339

44-
local packets_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last')
45-
local pfc_rx_packets_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last')
46-
local pfc_duration_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last')
47-
-- DEBUG CODE START. Uncomment to enable
48-
local debug_storm = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'DEBUG_STORM')
49-
-- DEBUG CODE END.
40+
-- Get all counters
41+
local occupancy_bytes = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_CURR_OCCUPANCY_BYTES')
42+
local packets = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS')
43+
local pfc_rx_packets = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key)
44+
local pfc_duration = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_duration_key)
5045

51-
-- If this is not a first run, then we have last values available
52-
if packets_last and pfc_rx_packets_last and pfc_duration_last then
53-
packets_last = tonumber(packets_last)
54-
pfc_rx_packets_last = tonumber(pfc_rx_packets_last)
55-
pfc_duration_last = tonumber(pfc_duration_last)
46+
if occupancy_bytes and packets and pfc_rx_packets and pfc_duration then
47+
occupancy_bytes = tonumber(occupancy_bytes)
48+
packets = tonumber(packets)
49+
pfc_rx_packets = tonumber(pfc_rx_packets)
50+
pfc_duration = tonumber(pfc_duration)
5651

57-
-- Check actual condition of queue being in PFC storm
58-
if (occupancy_bytes > 0 and packets - packets_last == 0 and pfc_rx_packets - pfc_rx_packets_last > 0) or
52+
local packets_last = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last')
53+
local pfc_rx_packets_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last')
54+
local pfc_duration_last = redis.call('HGET', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last')
5955
-- DEBUG CODE START. Uncomment to enable
60-
(debug_storm == "enabled") or
56+
local debug_storm = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'DEBUG_STORM')
6157
-- DEBUG CODE END.
62-
(occupancy_bytes == 0 and packets - packets_last == 0 and (pfc_duration - pfc_duration_last) > poll_time * 0.8) then
63-
if time_left <= poll_time then
64-
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","storm"]')
65-
is_deadlock = true
66-
time_left = detection_time
67-
else
68-
time_left = time_left - poll_time
58+
59+
-- If this is not a first run, then we have last values available
60+
if packets_last and pfc_rx_packets_last and pfc_duration_last then
61+
packets_last = tonumber(packets_last)
62+
pfc_rx_packets_last = tonumber(pfc_rx_packets_last)
63+
pfc_duration_last = tonumber(pfc_duration_last)
64+
65+
-- Check actual condition of queue being in PFC storm
66+
if (occupancy_bytes > 0 and packets - packets_last == 0 and pfc_rx_packets - pfc_rx_packets_last > 0) or
67+
-- DEBUG CODE START. Uncomment to enable
68+
(debug_storm == "enabled") or
69+
-- DEBUG CODE END.
70+
(occupancy_bytes == 0 and packets - packets_last == 0 and (pfc_duration - pfc_duration_last) > poll_time * 0.8) then
71+
if time_left <= poll_time then
72+
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","storm"]')
73+
is_deadlock = true
74+
time_left = detection_time
75+
else
76+
time_left = time_left - poll_time
77+
end
78+
else
79+
if pfc_wd_action == 'alert' and pfc_wd_status ~= 'operational' then
80+
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","restore"]')
81+
end
82+
time_left = detection_time
83+
end
6984
end
70-
else
71-
if pfc_wd_action == 'alert' and pfc_wd_status ~= 'operational' then
72-
redis.call('PUBLISH', 'PFC_WD', '["' .. KEYS[i] .. '","restore"]')
73-
end
74-
time_left = detection_time
75-
end
76-
end
7785

78-
-- Save values for next run
79-
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last', packets)
80-
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT', time_left)
81-
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last', pfc_rx_packets)
82-
if is_deadlock then
83-
redis.call('HDEL', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last')
84-
else
85-
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last', pfc_duration)
86+
-- Save values for next run
87+
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'SAI_QUEUE_STAT_PACKETS_last', packets)
88+
redis.call('HSET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_DETECTION_TIME_LEFT', time_left)
89+
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_rx_pkt_key .. '_last', pfc_rx_packets)
90+
redis.call('HDEL', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last')
91+
redis.call('HSET', counters_table_name .. ':' .. port_id, pfc_duration_key .. '_last', pfc_duration)
92+
end
8693
end
8794
end
8895
end

orchagent/pfc_restore.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ for i = n, 1, -1 do
2020
local pfc_wd_status = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_STATUS')
2121
local restoration_time = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_RESTORATION_TIME')
2222
local pfc_wd_action = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_ACTION')
23-
if pfc_wd_status ~= 'operational' and pfc_wd_action ~= 'alert' and restoration_time ~= '' then
23+
if pfc_wd_status ~= 'operational' and pfc_wd_action ~= 'alert' and restoration_time then
2424
restoration_time = tonumber(restoration_time)
2525
local time_left = redis.call('HGET', counters_table_name .. ':' .. KEYS[i], 'PFC_WD_RESTORATION_TIME_LEFT')
2626
if time_left == nil then

orchagent/pfcactionhandler.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ PfcWdActionHandler::PfcWdActionHandler(sai_object_id_t port, sai_object_id_t que
4545
}
4646
else
4747
{
48+
m_portAlias = p.m_alias;
4849
SWSS_LOG_NOTICE(
4950
"PFC Watchdog detected PFC storm on port %s, queue index %d, queue id 0x%lx and port id 0x%lx.",
5051
m_portAlias.c_str(),

0 commit comments

Comments
 (0)