From patchwork Wed May 18 12:22:13 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 1107 Return-Path: Received: from latitanza.investici.org (latitanza.investici.org [82.94.249.234]) by open-mesh.org (Postfix) with ESMTPS id 67CFE1544AB for ; Wed, 18 May 2011 14:22:10 +0200 (CEST) Authentication-Results: open-mesh.org; dkim=pass (1024-bit key) header.i=@autistici.org; dkim-adsp=pass Received: from [82.94.249.234] (latitanza [82.94.249.234]) (Authenticated sender: ordex@autistici.org) by localhost (Postfix) with ESMTPSA id 0E1D59829D; Wed, 18 May 2011 12:22:08 +0000 (UTC) X-DKIM: Sendmail DKIM Filter v2.8.2 latitanza.investici.org 0E1D59829D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1305721329; bh=nVT0yS5o9ygHNkcxEcxwK0my0hAMnTty6EdMmHe83cY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=YGnR4OOcECAJU8VJ0ahi3qPLWJ5GA6qBkVc2mLeodyon0LKk5aC8YXyWjyvC0fCar o5016Z0df7UQh+i5A0lQ82klA7taWW1pJi2T8VfV8+Kgj8uknrn/dpJztPKru9NCjf wksKkTyzhic68xRyMmTJjq97ScrvY3v2Vt7zWhxA= From: Antonio Quartulli To: "B.A.T.M.A.N" Date: Wed, 18 May 2011 14:22:13 +0200 Message-Id: <1305721333-28947-1-git-send-email-ordex@autistici.org> X-Mailer: git-send-email 1.7.3.4 In-Reply-To: <1305703250-23111-1-git-send-email-ordex@autistici.org> References: <1305703250-23111-1-git-send-email-ordex@autistici.org> Subject: [B.A.T.M.A.N.] [RFC/PATCH] net: add seq_before/seq_after functions X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.13 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 May 2011 12:22:10 -0000 Introduce two operations to handle comparison between packet sequence numbers taking into account overflow/wraparound. Batman-adv uses these functions already to check for successor packet even in case of overflow. --- I added this two functions in net.h because I didn't really know where best placement is. I saw several modules that redefine their own functions for the same purpose. include/linux/net.h | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-) diff --git a/include/linux/net.h b/include/linux/net.h index 94de83c..c7bc9bf 100644 --- a/include/linux/net.h +++ b/include/linux/net.h @@ -295,4 +295,21 @@ extern struct ratelimit_state net_ratelimit_state; #endif #endif /* __KERNEL__ */ + +/* Returns the smallest signed integer in two's complement with the sizeof x */ +#define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u))) + +/* Checks if a sequence number x is a predecessor/successor of y. + * they handle overflows/underflows and can correctly check for a + * predecessor/successor unless the variable sequence number has grown by + * more then 2**(bitwidth(x)-1)-1. + * This means that for a uint8_t with the maximum value 255, it would think: + * - when adding nothing - it is neither a predecessor nor a successor + * - before adding more than 127 to the starting value - it is a predecessor, + * - when adding 128 - it is neither a predecessor nor a successor, + * - after adding more than 127 to the starting value - it is a successor */ +#define seq_before(x, y) ({typeof(x) _dummy = (x - y); \ + _dummy > smallest_signed_int(_dummy); }) +#define seq_after(x, y) seq_before(y, x) + #endif /* _LINUX_NET_H */