Skip to contentSkip to conversation

iscsi: Simplify serial number comparisons

PATCH Nicholas A. Bellinger 1 participant 1 message Apr 12, 2011 1 message not in the archive SCSI

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 44 lines · Mark Rustad

Unsigned serial number comparison is very simple if you simply put the difference into a signed integer of the same size and then compare that value with zero. All the complexity and confusion fall away.

---
 include/scsi/iscsi_proto.h |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)
include/scsi/iscsi_proto.h +9 -7
index 0c6c1d6..98ebc3d 100644
@@ -35,30 +35,33 @@
 /*
  * Serial Number Arithmetic, 32 bits, RFC1982
  */
-#define SNA32_CHECK 2147483648UL
 
 static inline int iscsi_sna_lt(u32 n1, u32 n2)
 {
-	return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
-			(n1 > n2 && (n2 - n1 < SNA32_CHECK)));
+	s32 diff = n1 - n2;
+
+	return diff < 0;
 }
 
 static inline int iscsi_sna_lte(u32 n1, u32 n2)
 {
-	return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
-			(n1 > n2 && (n2 - n1 < SNA32_CHECK)));
+	s32 diff = n1 - n2;
+
+	return diff <= 0;
 }
 
 static inline int iscsi_sna_gt(u32 n1, u32 n2)
 {
-	return n1 != n2 && (((n1 < n2) && ((n2 - n1) > SNA32_CHECK)) ||
-			((n1 > n2) && ((n1 - n2) < SNA32_CHECK)));
+	s32 diff = n1 - n2;
+
+	return diff > 0;

include/scsi/iscsi_proto.h line 65

Quoted 10 lines
include/scsi/iscsi_proto.h +3 -2
 }
 
 static inline int iscsi_sna_gte(u32 n1, u32 n2)
 {
-	return n1 == n2 || (((n1 < n2) && ((n2 - n1) > SNA32_CHECK)) ||
-			((n1 > n2) && ((n1 - n2) < SNA32_CHECK)));
+	s32 diff = n1 - n2;
+
+	return diff >= 0;
 }

Hi Mark,

My apolgies for the delayed response on this patch.

I think this simplification to use s32 for iSCSI Command Serial Number arithmetic looks perfectly reasonable, and am including this into lio-core-2.6.git/lio-4.1. Thank you for your contribution!!

Mike, Hannes and Co, do you have any objections here for mainline Open-iSCSI and iSCSI-Target code for .40 code..?

Best Regards,

--nab