From: Mat Martineau <mathewm@codeaurora.org>
To: Andrei Emeltchenko <andrei.emeltchenko.news@gmail.com>
Cc: linux-bluetooth@vger.kernel.org, gustavo@padovan.org,
marcel@holtmann.org, pkrystad@codeaurora.org
Subject: Re: [RFC 1/8] Bluetooth: Improve ERTM sequence number offset calculation
Date: Thu, 26 Apr 2012 16:35:56 -0700 (PDT) [thread overview]
Message-ID: <alpine.DEB.2.02.1204261504510.13179@mathewm-linux> (raw)
In-Reply-To: <20120426071002.GB2659@aemeltch-MOBL1>
Hi Andrei -
On Thu, 26 Apr 2012, Andrei Emeltchenko wrote:
> Hi Mat,
>
> On Wed, Apr 25, 2012 at 04:36:12PM -0700, Mat Martineau wrote:
>> Instead of using modular division, the offset can be calculated using
>> only addition and subtraction. The previous calculation did not work
>> as intended and was more difficult to understand, involving unsigned
>> integer underflow and a check for a negative value where one was not
>> possible.
>
> BTW: in what cases this was not working?
"offset" was always positive, because "x % y" always gives a positive
result if y is a positive number. The 'if' clause was dead code,
which was obviously not intended.
If seq2 > seq1, then the value would wrap back around to a large
positive integer because both seq1 and seq2 are unsigned, so the value
of (seq1 - seq2) is also unsigned. Suppose seq1 is 0 and seq2 is 1.
In unsigned 16-bit integer math, 0 - 1 == 65535 due to underflow.
65535 % (63+1) is 63: luckily, the right answer for the most common tx
window (63).
65535 % (163+1) is 99: but the offset should be 163
>>
>> Signed-off-by: Mat Martineau <mathewm@codeaurora.org>
>> ---
>> include/net/bluetooth/l2cap.h | 11 ++++-------
>> 1 file changed, 4 insertions(+), 7 deletions(-)
>>
>> diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h
>> index 22e9ec9..92c0423 100644
>> --- a/include/net/bluetooth/l2cap.h
>> +++ b/include/net/bluetooth/l2cap.h
>> @@ -724,13 +724,10 @@ static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
>>
>> static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
>> {
>> - int offset;
>> -
>> - offset = (seq1 - seq2) % (chan->tx_win_max + 1);
>> - if (offset < 0)
>> - offset += (chan->tx_win_max + 1);
>> -
>> - return offset;
>> + if (seq1 >= seq2)
>> + return seq1 - seq2;
>> + else
>> + return chan->tx_win_max + 1 - seq2 + seq1;
>> }
>
> You seems are changing logic, not improving as you patch states.
> Your offset might be bigger then tx_win_max. Was this intended?
The new code is correct.
Here's a python test program for all possible inputs with
tx_win_max == 63:
<code>
#!/usr/bin/env python
def seq_offset(tx_win_max, seq1, seq2):
if (seq1 >= seq2):
return seq1 - seq2
else:
return tx_win_max + 1 - seq2 + seq1
tx_win_max = 63
max_offset = -1
min_offset = tx_win_max + 1
for i in range(tx_win_max+1):
for j in range(tx_win_max+1):
offset = seq_offset(tx_win_max, i, j)
min_offset = min(offset, min_offset)
max_offset = max(offset, max_offset)
print "min: %d, max: %d" % (min_offset, max_offset)
</code>
It prints:
min: 0, max: 63
--
Mat Martineau
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum
next prev parent reply other threads:[~2012-04-26 23:35 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-25 23:36 [RFC 0/8] ERTM state machine changes, part 2 Mat Martineau
2012-04-25 23:36 ` [RFC 1/8] Bluetooth: Improve ERTM sequence number offset calculation Mat Martineau
2012-04-26 6:39 ` Marcel Holtmann
2012-04-26 7:10 ` Andrei Emeltchenko
2012-04-26 23:35 ` Mat Martineau [this message]
2012-04-27 2:19 ` Gustavo Padovan
2012-04-25 23:36 ` [RFC 2/8] Bluetooth: Initialize new l2cap_chan structure members Mat Martineau
2012-04-26 6:38 ` Marcel Holtmann
2012-04-26 22:03 ` Mat Martineau
2012-04-25 23:36 ` [RFC 3/8] Bluetooth: Remove duplicate structure members from bt_skb_cb Mat Martineau
2012-04-26 6:39 ` Marcel Holtmann
2012-04-27 3:22 ` Gustavo Padovan
2012-04-25 23:36 ` [RFC 4/8] Bluetooth: Move recently-added ERTM header packing functions Mat Martineau
2012-04-26 6:40 ` Marcel Holtmann
2012-04-27 3:26 ` Gustavo Padovan
2012-04-25 23:36 ` [RFC 5/8] Bluetooth: Move recently-added ERTM header unpacking functions Mat Martineau
2012-04-26 6:40 ` Marcel Holtmann
2012-04-27 3:26 ` Gustavo Padovan
2012-04-25 23:36 ` [RFC 6/8] Bluetooth: Lock the L2CAP channel when sending Mat Martineau
2012-04-26 6:41 ` Marcel Holtmann
2012-04-26 8:22 ` Andrei Emeltchenko
2012-04-26 23:48 ` Mat Martineau
2012-04-27 13:40 ` [RFCv0] Bluetooth: Change locking logic in sock send Andrei Emeltchenko
2012-04-25 23:36 ` [RFC 7/8] Bluetooth: Refactor L2CAP ERTM and streaming transmit segmentation Mat Martineau
2012-04-26 6:43 ` Marcel Holtmann
2012-04-25 23:36 ` [RFC 8/8] Bluetooth: Add Code Aurora Forum copyright Mat Martineau
2012-04-26 6:44 ` Marcel Holtmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.2.02.1204261504510.13179@mathewm-linux \
--to=mathewm@codeaurora.org \
--cc=andrei.emeltchenko.news@gmail.com \
--cc=gustavo@padovan.org \
--cc=linux-bluetooth@vger.kernel.org \
--cc=marcel@holtmann.org \
--cc=pkrystad@codeaurora.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox