# DRBD replication

---

# Distributed Replicated Block Device - DRBD

_Peios Kernel / Administration / System administration / DRBD replication_

> DRBD is a shared-nothing, synchronously replicated block device. It is designed to serve as a building block for high availability clusters and in this context, is a "drop-in" replacement for shared storage. Simplistically, you could see…

<a id="rst-distributed-replicated-block-device-drbd"></a>

<a id="rst-description"></a>

## Description

> DRBD is a shared-nothing, synchronously replicated block device. It is designed to serve as a building block for high availability clusters and in this context, is a "drop-in" replacement for shared storage. Simplistically, you could see it as a network RAID 1.
>
> Please visit [https://www.drbd.org](<https://www.drbd.org>) to find out more.

- [kernel data structure for DRBD-9](</kernel/administration/admin-guide/blockdev-drbd/data-structure-v9.md>)
- [Data flows that Relate some functions, and write packets](</kernel/administration/admin-guide/blockdev-drbd/figures.md>)

---

# kernel data structure for DRBD-9

_Peios Kernel / Administration / System administration / DRBD replication_

> This describes the in kernel data structure for DRBD-9. Starting with Linux v3.14 we are reorganizing DRBD to use this data structure.

<a id="rst-kernel-data-structure-for-drbd-9"></a>

This describes the in kernel data structure for DRBD-9. Starting with Linux v3.14 we are reorganizing DRBD to use this data structure.

<a id="rst-basic-data-structure"></a>

## Basic Data Structure

A node has a number of DRBD resources.  Each such resource has a number of devices (aka volumes) and connections to other nodes ("peer nodes"). Each DRBD device is represented by a block device locally.

The DRBD objects are interconnected to form a matrix as depicted below; a drbd_peer_device object sits at each intersection between a drbd_device and a drbd_connection:



```text
/--------------+---------------+.....+---------------\
|   resource   |    device     |     |    device     |
+--------------+---------------+.....+---------------+
|  connection  |  peer_device  |     |  peer_device  |
+--------------+---------------+.....+---------------+
:              :               :     :               :
:              :               :     :               :
+--------------+---------------+.....+---------------+
|  connection  |  peer_device  |     |  peer_device  |
\--------------+---------------+.....+---------------/
```

In this table, horizontally, devices can be accessed from resources by their volume number.  Likewise, peer_devices can be accessed from connections by their volume number.  Objects in the vertical direction are connected by double linked lists.  There are back pointers from peer_devices to their connections a devices, and from connections and devices to their resource.

All resources are in the drbd_resources double-linked list.  In addition, all devices can be accessed by their minor device number via the drbd_devices idr.

The drbd_resource, drbd_connection, and drbd_device objects are reference counted.  The peer_device objects only serve to establish the links between devices and connections; their lifetime is determined by the lifetime of the device and connection which they reference.

---

# Data flows that Relate some functions, and write packets

_Peios Kernel / Administration / System administration / DRBD replication_

> Reference for Data flows that Relate some functions, and write packets.

<!-- SPDX-License-Identifier: GPL-2.0 -->

<!-- The here included files are intended to help understand the implementation -->

<a id="rst-data-flows-that-relate-some-functions-and-write-packets"></a>

## Data flows that Relate some functions, and write packets

![DRBD-8.3-data-packets.svg](/kernel/administration/admin-guide/blockdev-drbd/asset-5e49ee5f7722-drbd-8-3-data-packets.svg)

![DRBD-data-packets.svg](/kernel/administration/admin-guide/blockdev-drbd/asset-1b44fc93f6cf-drbd-data-packets.svg)

<a id="rst-sub-graphs-of-drbd-s-state-transitions"></a>

## Sub graphs of DRBD's state transitions

```mermaid
flowchart TB
  n0(["StandAllone"])
  n1(["WFConnection"])
  n2(["Unconnected"])
  n3(["WFReportParams"])
  n4(["Connected"])
  n5(["WFBitMapS"])
  n6(["WFBitMapT"])
  n7(["SyncSource"])
  n8(["SyncTarget"])
  n9(["PausedSyncS"])
  n10(["PausedSyncT"])
  n0 -->|"ioctl_set_net()"| n1
  n1 -->|"unable to bind()"| n2
  n1 -->|"in connect() after accept"| n3
  n3 -->|"checks in receive_param()"| n0
  n3 -->|"in receive_param()"| n4
  n3 -->|"sync_handshake()"| n5
  n3 -->|"sync_handshake()"| n6
  n5 -->|"receive_bitmap()"| n7
  n6 -->|"receive_bitmap()"| n8
  n7 --> n4
  n8 --> n4
  n7 --> n9
  n8 --> n10
  n9 --> n7
  n10 --> n8
  n4 -->|"* on network error"| n1
```

conn-states-8.dot

```mermaid
flowchart TB
  n0(["Diskless"])
  n1(["Inconsistent"])
  n2(["Consistent"])
  n3(["Outdated"])
  n4(["UpToDate"])
  n5(["Failed"])
  n0 -->|"ioctl_set_disk()"| n1
  n0 -->|"ioctl_set_disk()"| n2
  n0 -->|"ioctl_set_disk()"| n3
  n2 -->|"receive_param()"| n3
  n2 -->|"receive_param()"| n4
  n2 -->|"start resync"| n1
  n3 -->|"start resync"| n1
  n4 -->|"ioctl_replicate"| n1
  n1 -->|"resync completed"| n4
  n2 -->|"io completion error"| n5
  n3 -->|"io completion error"| n5
  n4 -->|"io completion error"| n5
  n1 -->|"io completion error"| n5
  n5 -->|"sending notify to peer"| n0
```

disk-states-8.dot

```mermaid
flowchart TB
  n0(["Secondary"])
  n1(["Primary"])
  n2(["Unknown"])
  n0 -->|"recv state packet"| n1
  n1 -->|"recv state packet"| n0
  n1 -->|"connection lost"| n2
  n0 -->|"connection lost"| n2
  n2 -->|"connected"| n1
  n2 -->|"connected"| n0
```

peer-states-8.dot

---

# DRBD connection state overview

_Peios Kernel / Administration / System administration / DRBD replication_

> Connection and synchronization states from the retained DRBD state diagram.

```mermaid
flowchart BT
  n0["StandAlone"]
  n1["Unconnected"]
  n2["communication loss<br/>Timeout<br/>BrokenPipe<br/>NetworkFailure"]
  n5["TearDown"]
  n6["Connected"]
  n7["StartingSyncS"]
  n8["StartingSyncT"]
  n12["state resynchronisation process running<br/>'concurrent' application requests allowed<br/>state PausedSyncT<br/>SyncTarget<br/>state PausedSyncS<br/>SyncSource"]
  subgraph g0["try to connect, handshake"]
    n3["WFConnection"]
    n4["WFReportParams"]
  end
  subgraph g1["new application (WRITE?) requests blocked<br/>while bitmap is exchanged"]
    n9["WFBitMapT"]
    n10["WFSyncUUID"]
    n11["WFBitMapS"]
  end
  n0 -->|"drbdadm connect"| n1
  n1 -->|"drbdadm disconnect<br/>or serious communication trouble"| n0
  n1 -->|"receiver thread is started"| n3
  n3 -->|"accept()<br/>and/or<br/>connect()"| n4
  n4 -->|"during handshake<br/>peers do not agree<br/>about something essential"| n0
  n4 -->|"data identical<br/>no sync needed"| n6
  n4 --> n11
  n4 --> n9
  n9 --> n10
  n11 -->|"to state PausedSyncS<br/>SyncSource"| n12
  n10 -->|"to state PausedSyncT<br/>SyncTarget"| n12
  n12 -->|"resnyc done<br/>from any resynchronisation state"| n6
  n4 --> n2
  n6 --> n2
  n12 -->|"from any resynchronisation state"| n2
  n2 -->|"receiver thread is stopped"| n1
  style n0 fill:gray,color:black,fill:transparent,stroke:transparent
  style n1 color:#444,fill:transparent,stroke:transparent
  style n2 color:#444
  style n3 color:#555,fill:transparent,stroke:transparent
  style n4 color:#555,fill:transparent,stroke:transparent
  style n5 color:#555,fill:transparent,stroke:transparent
  style n6 fill:green,color:black,fill:transparent,stroke:transparent
  style n7 color:#006c9c,fill:transparent,stroke:transparent
  style n8 color:#006c9c,fill:transparent,stroke:transparent
  style n9 color:red,fill:transparent,stroke:transparent
  style n10 color:red,fill:transparent,stroke:transparent
  style n11 color:red,fill:transparent,stroke:transparent
  style n12 color:blue
  linkStyle 5 stroke:green,color:green
  linkStyle 11 stroke:green,color:green
  linkStyle 12 stroke:red
  linkStyle 13 stroke:red
  linkStyle 14 stroke:red
  linkStyle 15 stroke:black
```
