Skip to content

Commit 96cd8ea

Browse files
Vudentzholtmann
authored andcommitted
Bluetooth: L2CAP: Derive rx credits from MTU and MPS
Give enough rx credits for a full packet instead of using an arbitrary number which may not be enough depending on the MTU and MPS which can cause interruptions while waiting for more credits, also remove debugfs entry for l2cap_le_max_credits. With these changes the credits are restored after each SDU is received instead of using fixed threshold, this way it is garanteed that there will always be enough credits to send a packet without waiting more credits to arrive. Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent fe14931 commit 96cd8ea

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎include/net/bluetooth/l2cap.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,6 @@ struct l2cap_conn_param_update_rsp {
455455
#define L2CAP_CONN_PARAM_ACCEPTED 0x0000
456456
#define L2CAP_CONN_PARAM_REJECTED 0x0001
457457

458-
#define L2CAP_LE_MAX_CREDITS 10
459-
460458
struct l2cap_le_conn_req {
461459
__le16 psm;
462460
__le16 scid;

‎net/bluetooth/l2cap_core.c‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
5151
static LIST_HEAD(chan_list);
5252
static DEFINE_RWLOCK(chan_list_lock);
5353

54-
static u16 le_max_credits = L2CAP_LE_MAX_CREDITS;
55-
5654
static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
5755
u8 code, u8 ident, u16 dlen, void *data);
5856
static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
@@ -518,9 +516,10 @@ static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
518516
chan->sdu_last_frag = NULL;
519517
chan->sdu_len = 0;
520518
chan->tx_credits = 0;
521-
chan->rx_credits = le_max_credits;
522519
/* Derive MPS from connection MTU to stop HCI fragmentation */
523520
chan->mps = min_t(u16, chan->imtu, chan->conn->mtu - L2CAP_HDR_SIZE);
521+
/* Give enough credits for a full packet */
522+
chan->rx_credits = (chan->imtu / chan->mps) + 1;
524523

525524
skb_queue_head_init(&chan->tx_q);
526525
}
@@ -6702,13 +6701,10 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
67026701
struct l2cap_le_credits pkt;
67036702
u16 return_credits;
67046703

6705-
/* We return more credits to the sender only after the amount of
6706-
* credits falls below half of the initial amount.
6707-
*/
6708-
if (chan->rx_credits >= (le_max_credits + 1) / 2)
6709-
return;
6704+
return_credits = ((chan->imtu / chan->mps) + 1) - chan->rx_credits;
67106705

6711-
return_credits = le_max_credits - chan->rx_credits;
6706+
if (!return_credits)
6707+
return;
67126708

67136709
BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
67146710

@@ -6722,6 +6718,21 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
67226718
l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
67236719
}
67246720

6721+
static int l2cap_le_recv(struct l2cap_chan *chan, struct sk_buff *skb)
6722+
{
6723+
int err;
6724+
6725+
BT_DBG("SDU reassemble complete: chan %p skb->len %u", chan, skb->len);
6726+
6727+
/* Wait recv to confirm reception before updating the credits */
6728+
err = chan->ops->recv(chan, skb);
6729+
6730+
/* Update credits whenever an SDU is received */
6731+
l2cap_chan_le_send_credits(chan);
6732+
6733+
return err;
6734+
}
6735+
67256736
static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
67266737
{
67276738
int err;
@@ -6740,7 +6751,11 @@ static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
67406751
chan->rx_credits--;
67416752
BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
67426753

6743-
l2cap_chan_le_send_credits(chan);
6754+
/* Update if remote had run out of credits, this should only happens
6755+
* if the remote is not using the entire MPS.
6756+
*/
6757+
if (!chan->rx_credits)
6758+
l2cap_chan_le_send_credits(chan);
67446759

67456760
err = 0;
67466761

@@ -6766,7 +6781,7 @@ static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
67666781
}
67676782

67686783
if (skb->len == sdu_len)
6769-
return chan->ops->recv(chan, skb);
6784+
return l2cap_le_recv(chan, skb);
67706785

67716786
chan->sdu = skb;
67726787
chan->sdu_len = sdu_len;
@@ -6788,7 +6803,7 @@ static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
67886803
skb = NULL;
67896804

67906805
if (chan->sdu->len == chan->sdu_len) {
6791-
err = chan->ops->recv(chan, chan->sdu);
6806+
err = l2cap_le_recv(chan, chan->sdu);
67926807
if (!err) {
67936808
chan->sdu = NULL;
67946809
chan->sdu_last_frag = NULL;
@@ -7647,9 +7662,6 @@ int __init l2cap_init(void)
76477662
l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
76487663
NULL, &l2cap_debugfs_fops);
76497664

7650-
debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
7651-
&le_max_credits);
7652-
76537665
return 0;
76547666
}
76557667

0 commit comments

Comments
 (0)