From 559c62b8ba539c978419a51b787d7e4a59a267dc Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Mon, 8 Jul 2019 11:27:32 +0300 Subject: [PATCH 1/2] fix the memory leak in on_pmpe. objects created via sx_api having convention new_ should be release explicitly via delete_. --- platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd b/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd index 08d1f1188005..8df567d8d782 100644 --- a/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd +++ b/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd @@ -205,7 +205,6 @@ class MlnxSfpd: pkt = new_uint8_t_arr(pkt_size) recv_info_p = new_sx_receive_info_t_p() pmpe_t = sx_event_pmpe_t() - logical_port_list = new_sx_port_log_id_t_arr(4) port_attributes_list = new_sx_port_attributes_t_arr(64) port_cnt_p = new_uint32_t_p() uint32_t_p_assign(port_cnt_p,64) @@ -217,6 +216,13 @@ class MlnxSfpd: if rc != 0: log_error("sx_lib_host_ifc_recv exited with error, rc %d" % rc) status = False + + delete_uint32_t_p(pkt_size_p) + delete_uint8_t_arr(pkt) + delete_sx_receive_info_t_p(recv_info_p) + delete_sx_port_attributes_t_arr(port_attributes_list) + delete_uint32_t_p(port_cnt_p) + return status, label_port_list, module_state pmpe_t = recv_info_p.event_info.pmpe @@ -236,6 +242,12 @@ class MlnxSfpd: break label_port_list.append(lable_port) + delete_uint32_t_p(pkt_size_p) + delete_uint8_t_arr(pkt) + delete_sx_receive_info_t_p(recv_info_p) + delete_sx_port_attributes_t_arr(port_attributes_list) + delete_uint32_t_p(port_cnt_p) + return status, label_port_list, module_state, From 67be03089f1402098fd3860763a7c1bacc482a37 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Mon, 8 Jul 2019 12:13:12 +0300 Subject: [PATCH 2/2] avoid duplicate code. --- platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd b/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd index 8df567d8d782..20bd14ffb4c1 100644 --- a/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd +++ b/platform/mellanox/mlnx-sfpd/scripts/mlnx-sfpd @@ -209,38 +209,30 @@ class MlnxSfpd: port_cnt_p = new_uint32_t_p() uint32_t_p_assign(port_cnt_p,64) label_port_list = [] - status = True module_state = 0 rc = sx_lib_host_ifc_recv(fd_p, pkt, pkt_size_p, recv_info_p) if rc != 0: log_error("sx_lib_host_ifc_recv exited with error, rc %d" % rc) status = False - - delete_uint32_t_p(pkt_size_p) - delete_uint8_t_arr(pkt) - delete_sx_receive_info_t_p(recv_info_p) - delete_sx_port_attributes_t_arr(port_attributes_list) - delete_uint32_t_p(port_cnt_p) - - return status, label_port_list, module_state - - pmpe_t = recv_info_p.event_info.pmpe - port_list_size = pmpe_t.list_size - logical_port_list = pmpe_t.log_port_list - module_state = pmpe_t.module_state - - for i in xrange(port_list_size): - logical_port = sx_port_log_id_t_arr_getitem(logical_port_list, i) - rc = sx_api_port_device_get(self.handle, 1 , 0, port_attributes_list, port_cnt_p) - port_cnt = uint32_t_p_value(port_cnt_p) - - for i in xrange(port_cnt): - port_attributes = sx_port_attributes_t_arr_getitem(port_attributes_list,i) - if port_attributes.log_port == logical_port: - lable_port = port_attributes.port_mapping.module_port - break - label_port_list.append(lable_port) + else: + status = True + pmpe_t = recv_info_p.event_info.pmpe + port_list_size = pmpe_t.list_size + logical_port_list = pmpe_t.log_port_list + module_state = pmpe_t.module_state + + for i in xrange(port_list_size): + logical_port = sx_port_log_id_t_arr_getitem(logical_port_list, i) + rc = sx_api_port_device_get(self.handle, 1 , 0, port_attributes_list, port_cnt_p) + port_cnt = uint32_t_p_value(port_cnt_p) + + for i in xrange(port_cnt): + port_attributes = sx_port_attributes_t_arr_getitem(port_attributes_list,i) + if port_attributes.log_port == logical_port: + lable_port = port_attributes.port_mapping.module_port + break + label_port_list.append(lable_port) delete_uint32_t_p(pkt_size_p) delete_uint8_t_arr(pkt)