Skip to contentSkip to conversation

bluetooth: Try to avoid forming scatternets

RFC In review Luiz Augusto von Dentz 2 participants 4 messages May 28, 2014 – Jun 1, 2014

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

RFC 1/1 bluetooth: Try to avoid forming scatternets

From: Luiz Augusto von Dentz <[email protected]>

In case of multiple connections automatically attempt to become master to avoid scatternet topology.

---
 net/bluetooth/hci_event.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
net/bluetooth/hci_event.c +16 -5
index 492d8d5..e6aed6e 100644
@@ -2017,8 +2017,15 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	if (ev->status) {
 		hci_proto_connect_cfm(conn, ev->status);
 		hci_conn_del(conn);
-	} else if (ev->link_type != ACL_LINK)
+	} else if (ev->link_type != ACL_LINK) {
 		hci_proto_connect_cfm(conn, ev->status);
+	} else if (hci_conn_num(hdev, ACL_LINK) > 1) {
+		/* Attempt to switch to master to avoid scatternet */
+		list_for_each_entry(conn, &hdev->conn_hash.list, list) {
+			if (conn->type == ACL_LINK)
+				hci_conn_switch_role(conn, 0x00);
+		}
+	}
 
 unlock:
 	hci_dev_unlock(hdev);
@@ -2035,6 +2042,12 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
 	       ev->link_type);
 
+	hci_dev_lock(hdev);
+
+	/* Allways attempt to become the master in multiple connection case */
+	if (hci_conn_num(hdev, ev->link_type) > 0)
+		mask |= HCI_LM_MASTER;
+
 	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
 				      &flags);
 
@@ -2044,8 +2057,6 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		struct inquiry_entry *ie;
 		struct hci_conn *conn;
 
-		hci_dev_lock(hdev);
-
 		ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
 		if (ie)
 			memcpy(ie->data.dev_class, ev->dev_class, 3);
@@ -2063,8 +2074,6 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 		memcpy(conn->dev_class, ev->dev_class, 3);
 
-		hci_dev_unlock(hdev);
-
 		if (ev->link_type == ACL_LINK ||
 		    (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
 			struct hci_cp_accept_conn_req cp;
@@ -2106,6 +2115,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		cp.reason = HCI_ERROR_REJ_BAD_ADDR;
 		hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
 	}
+
+	hci_dev_unlock(hdev);
 }
 
 static u8 hci_to_mgmt_reason(u8 err)
Signature · 1 line
1.9.0
maintainer
#1 lore raw

Hi Luiz,

Quoted 16 lines

In case of multiple connections automatically attempt to become master to avoid scatternet topology.

---
net/bluetooth/hci_event.c | 21 ++++++++++++++++-----

1 file changed, 16 insertions(+), 5 deletions(-)
net/bluetooth/hci_event.c +0 -0
index 492d8d5..e6aed6e 100644
@@ -2017,8 +2017,15 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	if (ev->status) {
 		hci_proto_connect_cfm(conn, ev->status);
 		hci_conn_del(conn);

net/bluetooth/hci_event.c static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2028

Quoted 10 lines
net/bluetooth/hci_event.c +8 -1
-	} else if (ev->link_type != ACL_LINK)
+	} else if (ev->link_type != ACL_LINK) {
 		hci_proto_connect_cfm(conn, ev->status);
+	} else if (hci_conn_num(hdev, ACL_LINK) > 1) {
+		/* Attempt to switch to master to avoid scatternet */
+		list_for_each_entry(conn, &hdev->conn_hash.list, list) {
+			if (conn->type == ACL_LINK)
+				hci_conn_switch_role(conn, 0x00);
+		}
+	}

I am not convinced this is a good idea. We do not know if the other also has two connections and wants to be the master. I think we can not generalize this on the HCI level. This has to be L2CAP socket policy that will allow us to make smart decisions. Sometimes the scatternet is just the better choice since our scatternet will result in a cheaper one than the others.

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2050

Quoted 13 lines

unlock:

	hci_dev_unlock(hdev);
net/bluetooth/hci_event.c +6 -0
@@ -2035,6 +2042,12 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
 	       ev->link_type);
 
+	hci_dev_lock(hdev);
+
+	/* Allways attempt to become the master in multiple connection case */
+	if (hci_conn_num(hdev, ev->link_type) > 0)
+		mask |= HCI_LM_MASTER;
+

Isn’t this useless for SCO and eSCO links since the master definition is really only for the ACL link.

Quoted 22 lines
net/bluetooth/hci_event.c +0 -4
 	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
 				      &flags);
 
@@ -2044,8 +2057,6 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		struct inquiry_entry *ie;
 		struct hci_conn *conn;
 
-		hci_dev_lock(hdev);
-
 		ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
 		if (ie)
 			memcpy(ie->data.dev_class, ev->dev_class, 3);
@@ -2063,8 +2074,6 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 		memcpy(conn->dev_class, ev->dev_class, 3);
 
-		hci_dev_unlock(hdev);
-
 		if (ev->link_type == ACL_LINK ||
 		    (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
 			struct hci_cp_accept_conn_req cp;

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2120

Quoted 6 lines
net/bluetooth/hci_event.c +2 -0
@@ -2106,6 +2115,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		cp.reason = HCI_ERROR_REJ_BAD_ADDR;
 		hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
 	}
+
+	hci_dev_unlock(hdev);
 }

Regards

Marcel

author maintainer
#2 lore raw

Hi Marcel,

On Sat, May 31, 2014 at 8:23 AM, Marcel Holtmann <[email protected]> wrot= e:

Quoted 10 lines

Hi Luiz,

Quoted 8 lines

In case of multiple connections automatically attempt to become master to avoid scatternet topology.

---
net/bluetooth/hci_event.c | 21 ++++++++++++++++-----

1 file changed, 16 insertions(+), 5 deletions(-)
Quoted 5 lines

net/bluetooth/hci_event.c static void hci_conn_complete_evt(struct hci_dev =

Quoted 5 lines
net/bluetooth/hci_event.c +0 -0
index 492d8d5..e6aed6e 100644
@@ -2017,8 +2017,15 @@ static void hci_conn_complete_evt(struct hci_dev =

*hdev, struct sk_buff *skb)

Quoted 14 lines

net/bluetooth/hci_event.c static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2028

Quoted 13 lines
net/bluetooth/hci_event.c +8 -1
      if (ev->status) {
              hci_proto_connect_cfm(conn, ev->status);
              hci_conn_del(conn);
-     } else if (ev->link_type !=3D ACL_LINK)
+     } else if (ev->link_type !=3D ACL_LINK) {
              hci_proto_connect_cfm(conn, ev->status);
+     } else if (hci_conn_num(hdev, ACL_LINK) > 1) {
+             /* Attempt to switch to master to avoid scatternet */
+             list_for_each_entry(conn, &hdev->conn_hash.list, list) {
+                     if (conn->type =3D=3D ACL_LINK)
+                             hci_conn_switch_role(conn, 0x00);
+             }
+     }
Quoted 1 line

I am not convinced this is a good idea. We do not know if the other also =

has two connections and wants to be the master. I think we can not generali= ze this on the HCI level. This has to be L2CAP socket policy that will allo= w us to make smart decisions. Sometimes the scatternet is just the better c= hoice since our scatternet will result in a cheaper one than the others.

I thought it is a very common policy in many stacks actually, for example apple's has the following in its bluetooth guidelines:

'In a Bluetooth connection, one device is the master and the other the slave. The master can have multiple
slaves, thusforming a piconet. The master device can also be a slave role to another master, creating a scatternet.
Such a scenario creates complications since the device has to alternate between the two piconets and thus
wastes valuable bandwidth. Managing the topology of the network is therefore important for maximum
performance. The Apple product may request a Role Switch, depending on its current topology, and the remote
device shall accept the request. The Apple product may also reject a request for a Role Switch because of
topology concerns. Having a suboptimal topology may degrade the audio quality and the user's experience.

Only when it is maintaining multiple links, either Bluetooth or WiFi, will the Apple product request or deny
role switches. Hence, it will grant a role switch if there is no reason for the Apple product to be master. It is
expected that the accessory will behave the same, only trying to be master when there is a legitimate reason.

The accessory should not always request to be master by default if there is no need in the system topology to
do so. If later the accessory needs to be master in order to maintain multiple links, it should ask to be master
at that time.'

We can't possible know how many connection the remote side has but it can reject the role switch which will just keep us in a scatternet.

Quoted 4 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *=

Quoted 4 lines

unlock:

      hci_dev_unlock(hdev);
net/bluetooth/hci_event.c +0 -0
@@ -2035,6 +2042,12 @@ static void hci_conn_request_evt(struct hci_dev *=

hdev, struct sk_buff *skb)

Quoted 6 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)

Quoted 6 lines
net/bluetooth/hci_event.c +3 -0
      BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
             ev->link_type);
 
+     hci_dev_lock(hdev);
+
+     /* Allways attempt to become the master in multiple connection cas=

e */

Quoted 5 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2050

Quoted 3 lines
net/bluetooth/hci_event.c +3 -0
+     if (hci_conn_num(hdev, ev->link_type) > 0)
+             mask |=3D HCI_LM_MASTER;
+

Isn=E2=80=99t this useless for SCO and eSCO links since the master defini=

tion is really only for the ACL link.

I believe the topology does influence SCO/eSCO too, there was actually a patch in AOSP that did always request to be the master if SCO/eSCO is connected which is probably the wrong think to do since that should depend on what role of HFP/HSP we are assuming, btw if the topology really influences SCO/eSCO I guess we shoud think about a socket option to request the initial role like L2CAP and RFCOMM have.

Quoted 4 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *h=

Quoted 4 lines
net/bluetooth/hci_event.c +0 -0
      mask |=3D hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
                                    &flags);
 
@@ -2044,8 +2057,6 @@ static void hci_conn_request_evt(struct hci_dev *h=

dev, struct sk_buff *skb)

Quoted 9 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *h=

Quoted 9 lines
net/bluetooth/hci_event.c +0 -2
              struct inquiry_entry *ie;
              struct hci_conn *conn;
 
-             hci_dev_lock(hdev);
-
              ie =3D hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
              if (ie)
                      memcpy(ie->data.dev_class, ev->dev_class, 3);
@@ -2063,8 +2074,6 @@ static void hci_conn_request_evt(struct hci_dev *h=

dev, struct sk_buff *skb)

Quoted 7 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)

Quoted 7 lines
net/bluetooth/hci_event.c +0 -2
              memcpy(conn->dev_class, ev->dev_class, 3);
 
-             hci_dev_unlock(hdev);
-
              if (ev->link_type =3D=3D ACL_LINK ||
                  (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev)=

)) {

Quoted 2 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *h=

Quoted 2 lines
net/bluetooth/hci_event.c +0 -0
                      struct hci_cp_accept_conn_req cp;
@@ -2106,6 +2115,8 @@ static void hci_conn_request_evt(struct hci_dev *h=

dev, struct sk_buff *skb)

Quoted 2 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *h= · line 2116

Quoted 2 lines
net/bluetooth/hci_event.c +0 -0
              cp.reason =3D HCI_ERROR_REJ_BAD_ADDR;
              hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp=

);

Quoted 9 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2120

Quoted 4 lines
net/bluetooth/hci_event.c +2 -0
      }
+
+     hci_dev_unlock(hdev);
 }

Regards

Marcel

--=20
Luiz Augusto von Dentz

maintainer
#3 lore raw

Hi Luiz,

Quoted 54 lines
Quoted 20 lines
Quoted 20 lines

In case of multiple connections automatically attempt to become master to avoid scatternet topology.

---
net/bluetooth/hci_event.c | 21 ++++++++++++++++-----

1 file changed, 16 insertions(+), 5 deletions(-)
net/bluetooth/hci_event.c +2 -1
index 492d8d5..e6aed6e 100644
@@ -2017,8 +2017,15 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
     if (ev->status) {
             hci_proto_connect_cfm(conn, ev->status);
             hci_conn_del(conn);
-     } else if (ev->link_type != ACL_LINK)
+     } else if (ev->link_type != ACL_LINK) {
             hci_proto_connect_cfm(conn, ev->status);
+     } else if (hci_conn_num(hdev, ACL_LINK) > 1) {
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2028

Quoted 6 lines
net/bluetooth/hci_event.c +6 -0
+             /* Attempt to switch to master to avoid scatternet */
+             list_for_each_entry(conn, &hdev->conn_hash.list, list) {
+                     if (conn->type == ACL_LINK)
+                             hci_conn_switch_role(conn, 0x00);
+             }
+     }

I am not convinced this is a good idea. We do not know if the other also has two connections and wants to be the master. I think we can not generalize this on the HCI level. This has to be L2CAP socket policy that will allow us to make smart decisions. Sometimes the scatternet is just the better choice since our scatternet will result in a cheaper one than the others.

I thought it is a very common policy in many stacks actually, for example apple's has the following in its bluetooth guidelines:

'In a Bluetooth connection, one device is the master and the other the slave. The master can have multiple
slaves, thusforming a piconet. The master device can also be a slave role to another master, creating a scatternet.
Such a scenario creates complications since the device has to alternate between the two piconets and thus
wastes valuable bandwidth. Managing the topology of the network is therefore important for maximum
performance. The Apple product may request a Role Switch, depending on its current topology, and the remote
device shall accept the request. The Apple product may also reject a request for a Role Switch because of
topology concerns. Having a suboptimal topology may degrade the audio quality and the user's experience.

Only when it is maintaining multiple links, either Bluetooth or WiFi, will the Apple product request or deny
role switches. Hence, it will grant a role switch if there is no reason for the Apple product to be master. It is
expected that the accessory will behave the same, only trying to be master when there is a legitimate reason.

Quoted 5 lines

The accessory should not always request to be master by default if there is no need in the system topology to
do so. If later the accessory needs to be master in order to maintain multiple links, it should ask to be master
at that time.’

and BlueZ can also be the accessory in a lot of cases. It is not that simple. If we would be always the phone than a simple policy like that might be acceptable. However BlueZ can also be the laptop or the car kit or something else. And that is my point, this really needs to be driven by the sockets that are open and not the ACL links.

Quoted 19 lines

We can't possible know how many connection the remote side has but it can reject the role switch which will just keep us in a scatternet.

Quoted 7 lines
Quoted 7 lines

unlock:

     hci_dev_unlock(hdev);
net/bluetooth/hci_event.c +0 -0
@@ -2035,6 +2042,12 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
     BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
            ev->link_type);
 
Quoted 8 lines

net/bluetooth/hci_event.c static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 2050

Quoted 6 lines
net/bluetooth/hci_event.c +6 -0
+     hci_dev_lock(hdev);
+
+     /* Allways attempt to become the master in multiple connection case */
+     if (hci_conn_num(hdev, ev->link_type) > 0)
+             mask |= HCI_LM_MASTER;
+

Isn’t this useless for SCO and eSCO links since the master definition is really only for the ACL link.

Quoted 6 lines

I believe the topology does influence SCO/eSCO too, there was actually a patch in AOSP that did always request to be the master if SCO/eSCO is connected which is probably the wrong think to do since that should depend on what role of HFP/HSP we are assuming, btw if the topology really influences SCO/eSCO I guess we shoud think about a socket option to request the initial role like L2CAP and RFCOMM have.

Every SCO and eSCO link require the existence of an ACL link. So it does not matter what SCO and eSCO topology we have. The topology deciding bearer is the ACL link.

Regards

Marcel