You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2016/08/11 21:27:20 UTC

[20/50] [abbrv] incubator-mynewt-core git commit: NimBLE - high / low prio event buffers.

NimBLE - high / low prio event buffers.

Events have one of two priorities:
o Low-priority   (BLE_HCI_TRANS_BUF_EVT_LO)
o High-priority  (BLE_HCI_TRANS_BUF_EVT_HI)

Low-priority event buffers are only used for advertising reports.  If
there are no free low-priority event buffers, then an incoming
advertising report will get dropped.

High-priority event buffers are for everything except advertising
reports.  If there are no free high-priority event buffers, a request to
allocate one will try to allocate a low-priority buffer instead.

If you want all events to be given equal treatment, then you should
allocate low-priority events only.

Event priorities solve the problem of critical events getting dropped
due to a flood of advertising reports.  This solution is likely
temporary: when HCI flow control is added, event priorities may become
obsolete.

Not all transports distinguish between low and high priority events.  If
the transport does not have separate settings for low and high buffer
counts, then it treats all events with equal priority.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/13019264
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/13019264
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/13019264

Branch: refs/heads/phyrx_no_mbuf
Commit: 13019264c26633b973b0a6a0ff600ab8b34c3928
Parents: 5862c8f
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Aug 4 16:31:05 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Aug 11 14:26:25 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_hs_test_util.c     |  1 -
 net/nimble/include/nimble/ble_hci_trans.h       | 27 ++++--
 .../ram/include/transport/ram/ble_hci_ram.h     | 17 +++-
 net/nimble/transport/ram/src/ble_hci_ram.c      | 95 +++++++++++++++-----
 4 files changed, 110 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index cfb86fa..4f1c706 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -1382,7 +1382,6 @@ ble_hs_test_util_init(void)
                          ble_hs_test_util_pkt_txed, NULL);
 
     hci_cfg = ble_hci_ram_cfg_dflt;
-    hci_cfg.num_evt_bufs = cfg.max_hci_bufs;
     rc = ble_hci_ram_init(&hci_cfg);
     TEST_ASSERT_FATAL(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/include/nimble/ble_hci_trans.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble_hci_trans.h b/net/nimble/include/nimble/ble_hci_trans.h
index 822da96..3e2ec7a 100644
--- a/net/nimble/include/nimble/ble_hci_trans.h
+++ b/net/nimble/include/nimble/ble_hci_trans.h
@@ -27,13 +27,30 @@ struct os_mbuf;
 
 /*** Type of buffers for holding commands and events. */
 /**
- * Low-priority event (advertising reports).  A request to allocate a
- * high-priority event buffer may allocate one of these instead if no
- * high-priority buffers are available.
+ * Controller-to-host event buffers.  Events have one of two priorities:
+ * o Low-priority   (BLE_HCI_TRANS_BUF_EVT_LO)
+ * o High-priority  (BLE_HCI_TRANS_BUF_EVT_HI)
+ *
+ * Low-priority event buffers are only used for advertising reports.  If there
+ * are no free low-priority event buffers, then an incoming advertising report
+ * will get dropped.
+ *
+ * High-priority event buffers are for everything except advertising reports.
+ * If there are no free high-priority event buffers, a request to allocate one
+ * will try to allocate a low-priority buffer instead.
+ *
+ * If you want all events to be given equal treatment, then you should allocate
+ * low-priority events only.
+ *
+ * Event priorities solve the problem of critical events getting dropped due to
+ * a flood of advertising reports.  This solution is likely temporary: when
+ * HCI flow control is added, event priorities may become obsolete.
+ *
+ * Not all transports distinguish between low and high priority events.  If the
+ * transport does not have separate settings for low and high buffer counts,
+ * then it treats all events with equal priority.
  */
 #define BLE_HCI_TRANS_BUF_EVT_LO    1
-
-/* High-priority event (all events except advertising reports). */
 #define BLE_HCI_TRANS_BUF_EVT_HI    2
 
 /* Host-to-controller command. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
index 1c5b58e..9d2d672 100644
--- a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
+++ b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
@@ -4,8 +4,23 @@
 #include "nimble/ble_hci_trans.h"
 
 struct ble_hci_ram_cfg {
-    uint16_t num_evt_bufs;
+    /** Number of high-priority event buffers. */
+    uint16_t num_evt_hi_bufs;
+
+    /** Number of low-priority event buffers. */
+    uint16_t num_evt_lo_bufs;
+
+    /** Size of each event buffer, in bytes. */
     uint16_t evt_buf_sz;
+
+    /* Note: For information about high-priority vs. low-priority event
+     * buffers, see net/nimble/include/nimble/ble_hci_trans.h.
+     */
+
+    /* Note: host-to-controller command buffers are not configurable.  The RAM
+     * transport only allows one outstanding command, so it uses a single
+     * statically-allocated buffer.
+     */
 };
 
 extern const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/13019264/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c
index aac5eb1..23a375d 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -7,7 +7,8 @@
 
 /** Default configuration. */
 const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
-    .num_evt_bufs = 3,
+    .num_evt_hi_bufs = 1,
+    .num_evt_lo_bufs = 2,
     .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
 };
 
@@ -23,17 +24,19 @@ static void *ble_hci_ram_rx_acl_hs_arg;
 static ble_hci_trans_rx_acl_fn *ble_hci_ram_rx_acl_ll_cb;
 static void *ble_hci_ram_rx_acl_ll_arg;
 
-static struct os_mempool ble_hci_ram_evt_pool;
-static void *ble_hci_ram_evt_buf;
+static struct os_mempool ble_hci_ram_evt_hi_pool;
+static void *ble_hci_ram_evt_hi_buf;
+static struct os_mempool ble_hci_ram_evt_lo_pool;
+static void *ble_hci_ram_evt_lo_buf;
 
 static uint8_t *ble_hci_ram_hs_cmd_buf;
 static uint8_t ble_hci_ram_hs_cmd_buf_alloced;
 
 void
 ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
-                                void *cmd_arg,
-                                ble_hci_trans_rx_acl_fn *acl_cb,
-                                void *acl_arg)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_ram_rx_cmd_hs_cb = cmd_cb;
     ble_hci_ram_rx_cmd_hs_arg = cmd_arg;
@@ -43,9 +46,9 @@ ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
 
 void
 ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
-                                void *cmd_arg,
-                                ble_hci_trans_rx_acl_fn *acl_cb,
-                                void *acl_arg)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_ram_rx_cmd_ll_cb = cmd_cb;
     ble_hci_ram_rx_cmd_ll_arg = cmd_arg;
@@ -78,7 +81,7 @@ ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
 int
 ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
 {
-    int rc;
+   int rc;
 
     assert(ble_hci_ram_rx_acl_ll_cb != NULL);
 
@@ -103,9 +106,18 @@ ble_hci_trans_buf_alloc(int type)
     uint8_t *buf;
 
     switch (type) {
-    case BLE_HCI_TRANS_BUF_EVT_LO:
     case BLE_HCI_TRANS_BUF_EVT_HI:
-        buf = os_memblock_get(&ble_hci_ram_evt_pool);
+        buf = os_memblock_get(&ble_hci_ram_evt_hi_pool);
+        if (buf == NULL) {
+            /* If no high-priority event buffers remain, try to grab a
+             * low-priority one.
+             */
+            buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
+        }
+        break;
+
+    case BLE_HCI_TRANS_BUF_EVT_LO:
+        buf = os_memblock_get(&ble_hci_ram_evt_lo_pool);
         break;
 
     case BLE_HCI_TRANS_BUF_CMD:
@@ -130,8 +142,12 @@ ble_hci_trans_buf_free(uint8_t *buf)
     if (buf == ble_hci_ram_hs_cmd_buf) {
         assert(ble_hci_ram_hs_cmd_buf_alloced);
         ble_hci_ram_hs_cmd_buf_alloced = 0;
+    } else if (os_memblock_from(&ble_hci_ram_evt_hi_pool, buf)) {
+        rc = os_memblock_put(&ble_hci_ram_evt_hi_pool, buf);
+        assert(rc == 0);
     } else {
-        rc = os_memblock_put(&ble_hci_ram_evt_pool, buf);
+        assert(os_memblock_from(&ble_hci_ram_evt_lo_pool, buf));
+        rc = os_memblock_put(&ble_hci_ram_evt_lo_pool, buf);
         assert(rc == 0);
     }
 }
@@ -139,8 +155,11 @@ ble_hci_trans_buf_free(uint8_t *buf)
 static void
 ble_hci_ram_free_mem(void)
 {
-    free(ble_hci_ram_evt_buf);
-    ble_hci_ram_evt_buf = NULL;
+    free(ble_hci_ram_evt_hi_buf);
+    ble_hci_ram_evt_hi_buf = NULL;
+
+    free(ble_hci_ram_evt_lo_buf);
+    ble_hci_ram_evt_lo_buf = NULL;
 
     free(ble_hci_ram_hs_cmd_buf);
     ble_hci_ram_hs_cmd_buf = NULL;
@@ -150,9 +169,21 @@ ble_hci_ram_free_mem(void)
 int
 ble_hci_trans_reset(void)
 {
+    /* No work to do.  All allocated buffers are owned by the host or
+     * controller, and they will get freed by their owners.
+     */
     return 0;
 }
 
+/**
+ * Initializes the RAM HCI transport module.
+ *
+ * @param cfg                   The settings to initialize the HCI RAM
+ *                                  transport with.
+ *
+ * @return                      0 on success;
+ *                              A BLE_ERR_[...] error code on failure.
+ */
 int
 ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
 {
@@ -160,17 +191,35 @@ ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
 
     ble_hci_ram_free_mem();
 
-    ble_hci_ram_evt_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_bufs,
-                                                  cfg->evt_buf_sz));
-    if (ble_hci_ram_evt_buf == NULL) {
-        rc = ENOMEM;
+    if (cfg->num_evt_hi_bufs > 0) {
+        ble_hci_ram_evt_hi_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_hi_bufs,
+                                                         cfg->evt_buf_sz));
+        if (ble_hci_ram_evt_hi_buf == NULL) {
+            rc = ENOMEM;
+            goto err;
+        }
+    }
+
+    rc = os_mempool_init(&ble_hci_ram_evt_hi_pool, cfg->num_evt_hi_bufs,
+                         cfg->evt_buf_sz, ble_hci_ram_evt_hi_buf,
+                         "ble_hci_ram_evt_hi_pool");
+    if (rc != 0) {
+        rc = EINVAL;
         goto err;
     }
 
-    /* Create memory pool of command buffers */
-    rc = os_mempool_init(&ble_hci_ram_evt_pool, cfg->num_evt_bufs,
-                         cfg->evt_buf_sz, ble_hci_ram_evt_buf,
-                         "ble_hci_ram_evt_pool");
+    if (cfg->num_evt_lo_bufs > 0) {
+        ble_hci_ram_evt_lo_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_lo_bufs,
+                                                         cfg->evt_buf_sz));
+        if (ble_hci_ram_evt_lo_buf == NULL) {
+            rc = ENOMEM;
+            goto err;
+        }
+    }
+
+    rc = os_mempool_init(&ble_hci_ram_evt_lo_pool, cfg->num_evt_lo_bufs,
+                         cfg->evt_buf_sz, ble_hci_ram_evt_lo_buf,
+                         "ble_hci_ram_evt_lo_pool");
     if (rc != 0) {
         rc = EINVAL;
         goto err;