1.. _bluetooth_mesh_sar_cfg:
2
3Segmentation and reassembly (SAR)
4#################################
5
6Segmentation and reassembly (SAR) provides a way of handling larger upper transport layer messages
7in a mesh network, with a purpose of enhancing the Bluetooth Mesh throughput. The segmentation and
8reassembly mechanism is used by the lower transport layer.
9
10The lower transport layer defines how the upper transport layer PDUs are segmented and reassembled
11into multiple Lower Transport PDUs, and sends them to the lower transport layer on a peer device.
12If the Upper Transport PDU fits, it is sent in a single Lower Transport PDU. For longer packets,
13which do not fit into a single Lower Transport PDU, the lower transport layer performs segmentation,
14splitting the Upper Transport
15PDU into multiple segments.
16
17The lower transport layer on the receiving device reassembles the segments into a single Upper
18Transport PDU before passing it up the stack. Delivery of a segmented message is acknowledged by the
19lower transport layer of the receiving node, while an unsegmented message delivery is not
20acknowledged. However, an Upper Transport PDU that fits into one Lower Transport PDU can also be
21sent as a single-segment segmented message when acknowledgment by the lower transport layer is
22required. Set the ``send rel`` flag (see :c:struct:`bt_mesh_msg_ctx`) to use the reliable message
23transmission and acknowledge single-segment segmented messages.
24
25The transport layer is able to transport up to 32 segments with its SAR mechanism, with a maximum
26message (PDU) size of 384 octets.  To configure message size for the Bluetooth Mesh stack, use the
27following Kconfig options:
28
29* :kconfig:option:`CONFIG_BT_MESH_RX_SEG_MAX` to set the maximum number of segments in an incoming message.
30* :kconfig:option:`CONFIG_BT_MESH_TX_SEG_MAX` to set the maximum number of segments in an outgoing message.
31
32The Kconfig options :kconfig:option:`CONFIG_BT_MESH_TX_SEG_MSG_COUNT` and
33:kconfig:option:`CONFIG_BT_MESH_RX_SEG_MSG_COUNT` define how many outgoing and incoming segmented
34messages can be processed simultaneously. When more than one segmented message is sent to the same
35destination, the messages are queued and sent one at a time.
36
37Incoming and outgoing segmented messages share the same pool for allocation of their segments. This
38pool size is configured through the :kconfig:option:`CONFIG_BT_MESH_SEG_BUFS` Kconfig option.
39Both incoming and outgoing messages allocate segments at the start of the transaction. The outgoing
40segmented message releases its segments one by one as soon as they are acknowledged by the receiver,
41while the incoming message releases the segments first after the message is fully received.
42Keep this in mind when defining the size of the buffers.
43
44SAR does not impose extra overhead on the access layer payload per segment.
45
46Segmentation and reassembly (SAR) Configuration models
47******************************************************
48
49With Bluetooth Mesh Protocol Specification version 1.1, it became possible to configure SAR
50behavior, such as intervals, timers and retransmission counters, over a mesh network using SAR
51Configuration models:
52
53* :ref:`bluetooth_mesh_sar_cfg_cli`
54* :ref:`bluetooth_mesh_sar_cfg_srv`
55
56The following SAR behavior applies regardless of the presence of a SAR Configuration Server on a
57node.
58
59Transmission of segments is separated by a segment transmission interval (see the
60`SAR Segment Interval Step`_ state). Other configurable time intervals and delays available for the
61segmentation and reassembly are:
62
63* Interval between unicast retransmissions (see the states `SAR Unicast Retransmissions Interval Step`_ and `SAR Unicast Retransmissions Interval Increment`_).
64* Interval between multicast retransmissions (see the `SAR Multicast Retransmissions Interval Step`_ state).
65* Segment reception interval (see the `SAR Receiver Segment Interval Step`_ state).
66* Acknowledgment delay increment (see the `SAR Acknowledgment Delay Increment`_ state).
67
68When the last segment marked as unacknowledged is transmitted, the lower transport layer starts a
69retransmissions timer.  The initial value of the SAR Unicast Retransmissions timer depends on the
70value of the TTL field of the message. If the TTL field value is greater than ``0``, the initial
71value for the timer is set according to the following formula:
72
73.. math::
74
75   unicast~retransmissions~interval~step + unicast~retransmissions~interval~increment \times (TTL - 1)
76
77
78If the TTL field value is ``0``, the initial value of the timer is set to the unicast
79retransmissions interval step.
80
81The initial value of the SAR Multicast Retransmissions timer is set to the multicast retransmissions
82interval.
83
84When the lower transport layer receives a message segment, it starts a SAR Discard timer. The
85discard timer tells how long the lower transport layer waits before discarding the segmented message
86the segment belongs to. The initial value of the SAR Discard timer is the discard timeout value
87indicated by the `SAR Discard Timeout`_ state.
88
89SAR Acknowledgment timer holds the time before a Segment Acknowledgment message is sent for a
90received segment. The initial value of the SAR Acknowledgment timer is calculated using the
91following formula:
92
93.. math::
94
95   min(SegN + 0.5 , acknowledgment~delay~increment) \times segment~reception~interval
96
97
98The ``SegN`` field value identifies the total number of segments the Upper Transport PDU is
99segmented into.
100
101Four counters are related to SAR behavior:
102
103* Two unicast retransmissions counts (see `SAR Unicast Retransmissions Count`_ state and `SAR Unicast Retransmissions Without Progress Count`_ state)
104* Multicast retransmissions count (see `SAR Multicast Retransmissions Count`_ state)
105* Acknowledgment retransmissions count (see `SAR Acknowledgment Retransmissions Count`_ state)
106
107If the number of segments in the transmission is higher than the value of the
108`SAR Segments Threshold`_ state, Segment Acknowledgment messages are retransmitted using the value
109of the `SAR Acknowledgment Retransmissions Count`_ state.
110
111.. _bt_mesh_sar_cfg_states:
112
113SAR states
114**********
115
116There are two states defined related to segmentation and reassembly:
117
118* SAR Transmitter state
119* SAR Receiver state
120
121The SAR Transmitter state is a composite state that controls the number and timing of transmissions
122of segmented messages. It includes the following states:
123
124* SAR Segment Interval Step
125* SAR Unicast Retransmissions Count
126* SAR Unicast Retransmissions Without Progress Count
127* SAR Unicast Retransmissions Interval Step
128* SAR Unicast Retransmissions Interval Increment
129* SAR Multicast Retransmissions Count
130* SAR Multicast Retransmissions Interval Step
131
132The SAR Receiver state is a composite state that controls the number and timing of Segment
133Acknowledgment transmissions and the discarding of reassembly of a segmented message. It includes
134the following states:
135
136* SAR Segments Threshold
137* SAR Discard Timeout
138* SAR Acknowledgment Delay Increment
139* SAR Acknowledgment Retransmissions Count
140* SAR Receiver Segment Interval Step
141
142SAR Segment Interval Step
143=========================
144
145SAR Segment Interval Step state holds a value that controls the interval between transmissions of
146segments of a segmented message. The interval is measured in milliseconds.
147
148Use the :kconfig:option:`CONFIG_BT_MESH_SAR_TX_SEG_INT_STEP` Kconfig option to set the default
149value. Segment transmission interval is then calculated using the following formula:
150
151.. math::
152
153   (\mathtt{CONFIG\_BT\_MESH\_SAR\_TX\_SEG\_INT\_STEP} + 1) \times 10~\text{ms}
154
155
156SAR Unicast Retransmissions Count
157=================================
158
159SAR Unicast Retransmissions Count holds a value that defines the maximum number of retransmissions
160of a segmented message to a unicast destination. Use the
161:kconfig:option:`CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_COUNT` Kconfig option to set the default
162value for this state.
163
164SAR Unicast Retransmissions Without Progress Count
165==================================================
166
167This state holds a value that defines the maximum number of retransmissions of a segmented message
168to a unicast address that will be sent if no acknowledgment was received during the timeout, or if
169an acknowledgment with already confirmed segments was received. Use the Kconfig option
170:kconfig:option:`CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_WITHOUT_PROG_COUNT` to set the maximum number
171of retransmissions.
172
173SAR Unicast Retransmissions Interval Step
174=========================================
175
176The value of this state controls the interval step used for delaying the retransmissions of
177unacknowledged segments of a segmented message to a unicast address. The interval step is measured
178in milliseconds.
179
180Use the :kconfig:option:`CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_INT_STEP` Kconfig option to set the
181default value.  This value is then used to calculate the interval step using the following formula:
182
183.. math::
184
185   (\mathtt{CONFIG\_BT\_MESH\_SAR\_TX\_UNICAST\_RETRANS\_INT\_STEP} + 1) \times 25~\text{ms}
186
187
188SAR Unicast Retransmissions Interval Increment
189==============================================
190
191SAR Unicast Retransmissions Interval Increment holds a value that controls the interval increment
192used for delaying the retransmissions of unacknowledged segments of a segmented message to a unicast
193address. The increment is measured in milliseconds.
194
195Use the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_INT_INC` to set the
196default value.  The Kconfig option value is used to calculate the increment using the following
197formula:
198
199.. math::
200
201   (\mathtt{CONFIG\_BT\_MESH\_SAR\_TX\_UNICAST\_RETRANS\_INT\_INC} + 1) \times 25~\text{ms}
202
203
204SAR Multicast Retransmissions Count
205===================================
206
207The state holds a value that controls the total number of retransmissions of a segmented message to
208a multicast address. Use the Kconfig option
209:kconfig:option:`CONFIG_BT_MESH_SAR_TX_MULTICAST_RETRANS_COUNT` to set the total number of
210retransmissions.
211
212SAR Multicast Retransmissions Interval Step
213===========================================
214
215This state holds a value that controls the interval between retransmissions of all segments in a
216segmented message to a multicast address.  The interval is measured in milliseconds.
217
218Use the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SAR_TX_MULTICAST_RETRANS_INT` to set the
219default value that is used to calculate the interval using the following formula:
220
221.. math::
222
223   (\mathtt{CONFIG\_BT\_MESH\_SAR\_TX\_MULTICAST\_RETRANS\_INT} + 1) \times 25~\text{ms}
224
225
226SAR Discard Timeout
227===================
228
229The value of this state defines the time in seconds that the lower transport layer waits after
230receiving segments of a segmented message before discarding that segmented message. Use the Kconfig
231option :kconfig:option:`CONFIG_BT_MESH_SAR_RX_DISCARD_TIMEOUT` to set the default value. The discard
232timeout will be calculated using the following formula:
233
234.. math::
235
236   (\mathtt{CONFIG\_BT\_MESH\_SAR\_RX\_DISCARD\_TIMEOUT} + 1) \times 5~\text{seconds}
237
238
239SAR Acknowledgment Delay Increment
240==================================
241
242This state holds a value that controls the delay increment of an interval used for delaying the
243transmission of an acknowledgment message after receiving a new segment. The increment is measured
244in segments.
245
246Use the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SAR_RX_ACK_DELAY_INC` to set the default
247value. The increment value is calculated to be
248:math:`\verb|CONFIG_BT_MESH_SAR_RX_ACK_DELAY_INC| + 1.5`.
249
250SAR Segments Threshold
251======================
252
253SAR Segments Threshold state holds a value that defines a threshold in number of segments of a
254segmented message for acknowledgment retransmissions.  Use the Kconfig option
255:kconfig:option:`CONFIG_BT_MESH_SAR_RX_SEG_THRESHOLD` to set the threshold.
256
257When the number of segments of a segmented message is above this threshold, the stack will
258additionally retransmit every acknowledgment message the number of times given by the value of
259:kconfig:option:`CONFIG_BT_MESH_SAR_RX_ACK_RETRANS_COUNT`.
260
261SAR Acknowledgment Retransmissions Count
262========================================
263
264The SAR Acknowledgment Retransmissions Count state controls the number of retransmissions of Segment
265Acknowledgment messages sent by the lower transport layer.  It gives the total number of
266retranmissions of an acknowledgment message that the stack will additionally send when the size of
267segments in a segmented message is above the :kconfig:option:`CONFIG_BT_MESH_SAR_RX_SEG_THRESHOLD`
268value.
269
270Use the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SAR_RX_ACK_RETRANS_COUNT` to set the default
271value for this state.  The maximum number of transmissions of a Segment Acknowledgment message is
272:math:`\verb|CONFIG_BT_MESH_SAR_RX_ACK_RETRANS_COUNT| + 1`.
273
274SAR Receiver Segment Interval Step
275==================================
276
277The SAR Receiver Segment Interval Step defines the segments reception interval step used for
278delaying the transmission of an acknowledgment message after receiving a new segment. The interval
279is measured in milliseconds.
280
281Use the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SAR_RX_SEG_INT_STEP` to set the default value
282and calculate the interval using the following formula:
283
284.. math::
285
286   (\mathtt{CONFIG\_BT\_MESH\_SAR\_RX\_SEG\_INT\_STEP} + 1) \times 10~\text{ms}
287