transport: Correct comparison in while loop condition
With the latest libpldm code version 0.4, sometimes the calling
`pldmtool platform GetPDR` command while polling the sensors will be
ended with the error `Failed to receive RC = 8`. This error code is
printed when the `pldm_transport_send_recv_msg()` responses
PLDM_REQUESTER_RECV_FAIL when it exits the while loop.
The while loop will continue while the now time is still not later
than the end time. The comparison should be corrected.
Tested:
1. Call "pldmtool platform GetPDR" while reading the sensors.
2. No "Failed to receive RC = 8".
3. Added a test case and verified we see multiple invocations of poll:
```
22:49:31.807615 timerfd_create(CLOCK_MONOTONIC, 0) = 3
22:49:31.807774 timerfd_settime(3, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=0, tv_nsec=0}}, NULL) = 0
22:49:31.807893 timerfd_settime(3, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=1, tv_nsec=0}}, NULL) = 0
22:49:31.808009 ppoll([{fd=3, events=POLLIN}], 1, {tv_sec=4, tv_nsec=800000000}, NULL, 0) = 1 ([{fd=3, revents=POLLIN}], left {tv_sec=3, tv_nsec=799317702})
22:49:32.809245 timerfd_settime(3, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=0, tv_nsec=0}}, NULL) = 0
22:49:32.810336 timerfd_settime(3, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=4, tv_nsec=0}}, NULL) = 0
22:49:32.810899 ppoll([{fd=3, events=POLLIN}], 1, {tv_sec=3, tv_nsec=798000000}, NULL, 0) = 0 (Timeout)
22:49:36.614235 close(3) = 0
```
Fixes: abaf61f45e2a ("transport: Prevent sticking in waiting for response")
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
[AJ: Add unit test]
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: If1829a35d755d68fd6a18d8e6ad54d1648ccc4ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 87626ac..068c8a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -62,6 +62,7 @@
1. requester: Fix response buffer cast in pldm_send_recv()
2. pdr: Hoist record handle overflow test to avoid memory leak
+3. transport: Correct comparison in while loop condition
## [0.4.0] - 2023-07-14
diff --git a/src/transport/transport.c b/src/transport/transport.c
index 919cc61..1eeffb6 100644
--- a/src/transport/transport.c
+++ b/src/transport/transport.c
@@ -209,7 +209,7 @@
if (ret < 0) {
return PLDM_REQUESTER_POLL_FAIL;
}
- } while (!timercmp(&now, &end, <));
+ } while (timercmp(&now, &end, <));
return PLDM_REQUESTER_RECV_FAIL;
}
diff --git a/tests/transport.cpp b/tests/transport.cpp
index 79b09e8..9f00986 100644
--- a/tests/transport.cpp
+++ b/tests/transport.cpp
@@ -161,3 +161,58 @@
pldm_transport_test_destroy(test);
}
#endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(Transport, send_recv_unwanted)
+{
+ uint8_t req[] = {0x81, 0x00, 0x01, 0x01};
+ uint8_t resp[] = {0x02, 0x00, 0x01, 0x00};
+ const struct pldm_transport_test_descriptor seq[] = {
+ {
+ .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND,
+ .send_msg =
+ {
+ .dst = 1,
+ .msg = req,
+ .len = sizeof(req),
+ },
+ },
+ {
+ .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY,
+ .latency =
+ {
+ .it_interval = {0, 0},
+ .it_value = {1, 0},
+ },
+ },
+ {
+ .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV,
+ .recv_msg =
+ {
+ .src = 2,
+ .msg = resp,
+ .len = sizeof(resp),
+ },
+ },
+ {
+ .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY,
+ .latency =
+ {
+ .it_interval = {0, 0},
+ .it_value = {4, 0},
+ },
+ },
+ };
+ struct pldm_transport_test* test = NULL;
+ struct pldm_transport* ctx;
+ size_t len;
+ void* msg;
+ int rc;
+
+ EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0);
+ ctx = pldm_transport_test_core(test);
+ rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len);
+ EXPECT_EQ(rc, PLDM_REQUESTER_RECV_FAIL);
+ pldm_transport_test_destroy(test);
+}
+#endif