1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// libquorum interface for Rust
// Copyright (c) 2021 Red Hat, Inc.
//
// All rights reserved.
//
// Author: Christine Caulfield (ccaulfi@redhat.com)
//

#![allow(clippy::type_complexity)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::single_match)]

// For the code generated by bindgen
use crate::sys::quorum as ffi;

use crate::{CsError, DispatchFlags, NodeId, Result, TrackFlags};
use std::collections::HashMap;
use std::os::raw::{c_int, c_void};
use std::slice;
use std::sync::Mutex;

/// Data for model1 [initialize]
#[derive(Copy, Clone)]
pub enum ModelData {
    ModelNone,
    ModelV1(Model1Data),
}

/// Value returned from [initialize]. Indicates whether quorum is currently active on this cluster.
pub enum QuorumType {
    Free,
    Set,
}

/// Flags for [initialize], none currently supported
#[derive(Copy, Clone)]
pub enum Model1Flags {
    None,
}

/// RingId returned in quorum_notification_fn
pub struct RingId {
    pub nodeid: NodeId,
    pub seq: u64,
}

// Used to convert a QUORUM handle into one of ours
lazy_static! {
    static ref HANDLE_HASH: Mutex<HashMap<u64, Handle>> = Mutex::new(HashMap::new());
}

fn list_to_vec(list_entries: u32, list: *const u32) -> Vec<NodeId> {
    let mut r_member_list = Vec::<NodeId>::new();
    let temp_members: &[u32] = unsafe { slice::from_raw_parts(list, list_entries as usize) };
    for i in 0..list_entries as usize {
        r_member_list.push(NodeId::from(temp_members[i]));
    }
    r_member_list
}

// Called from quorum callback function - munge params back to Rust from C
extern "C" fn rust_quorum_notification_fn(
    handle: ffi::quorum_handle_t,
    quorate: u32,
    ring_id: ffi::quorum_ring_id,
    member_list_entries: u32,
    member_list: *const u32,
) {
    if let Some(h) = HANDLE_HASH.lock().unwrap().get(&handle) {
        let r_ring_id = RingId {
            nodeid: NodeId::from(ring_id.nodeid),
            seq: ring_id.seq,
        };
        let r_member_list = list_to_vec(member_list_entries, member_list);
        let r_quorate = match quorate {
            0 => false,
            1 => true,
            _ => false,
        };
        match &h.model_data {
            ModelData::ModelV1(md) => {
                if let Some(cb) = md.quorum_notification_fn {
                    (cb)(h, r_quorate, r_ring_id, r_member_list);
                }
            }
            _ => {}
        }
    }
}

extern "C" fn rust_nodelist_notification_fn(
    handle: ffi::quorum_handle_t,
    ring_id: ffi::quorum_ring_id,
    member_list_entries: u32,
    member_list: *const u32,
    joined_list_entries: u32,
    joined_list: *const u32,
    left_list_entries: u32,
    left_list: *const u32,
) {
    if let Some(h) = HANDLE_HASH.lock().unwrap().get(&handle) {
        let r_ring_id = RingId {
            nodeid: NodeId::from(ring_id.nodeid),
            seq: ring_id.seq,
        };

        let r_member_list = list_to_vec(member_list_entries, member_list);
        let r_joined_list = list_to_vec(joined_list_entries, joined_list);
        let r_left_list = list_to_vec(left_list_entries, left_list);

        match &h.model_data {
            ModelData::ModelV1(md) => {
                if let Some(cb) = md.nodelist_notification_fn {
                    (cb)(h, r_ring_id, r_member_list, r_joined_list, r_left_list);
                }
            }
            _ => {}
        }
    }
}

#[derive(Copy, Clone)]
/// Data for model1 [initialize]
pub struct Model1Data {
    pub flags: Model1Flags,
    pub quorum_notification_fn:
        Option<fn(hande: &Handle, quorate: bool, ring_id: RingId, member_list: Vec<NodeId>)>,
    pub nodelist_notification_fn: Option<
        fn(
            hande: &Handle,
            ring_id: RingId,
            member_list: Vec<NodeId>,
            joined_list: Vec<NodeId>,
            left_list: Vec<NodeId>,
        ),
    >,
}

/// A handle into the quorum library. Returned from [initialize] and needed for all other calls
#[derive(Copy, Clone)]
pub struct Handle {
    quorum_handle: u64,
    model_data: ModelData,
}

/// Initialize a connection to the quorum library. You must call this before doing anything
/// else and use the passed back [Handle].
/// Remember to free the handle using [finalize] when finished.
pub fn initialize(model_data: &ModelData, context: u64) -> Result<(Handle, QuorumType)> {
    let mut handle: ffi::quorum_handle_t = 0;
    let mut quorum_type: u32 = 0;

    let mut m = match model_data {
        ModelData::ModelV1(_v1) => ffi::quorum_model_v1_data_t {
            model: ffi::QUORUM_MODEL_V1,
            quorum_notify_fn: Some(rust_quorum_notification_fn),
            nodelist_notify_fn: Some(rust_nodelist_notification_fn),
        },
        // Only V1 supported. No point in doing legacy stuff in a new binding
        _ => return Err(CsError::CsErrInvalidParam),
    };

    handle = unsafe {
        let c_context: *mut c_void = &mut &context as *mut _ as *mut c_void;
        let c_model: *mut ffi::quorum_model_data_t =
            &mut m as *mut _ as *mut ffi::quorum_model_data_t;
        let res = ffi::quorum_model_initialize(
            &mut handle,
            m.model,
            c_model,
            &mut quorum_type,
            c_context,
        );

        if res == ffi::CS_OK {
            handle
        } else {
            return Err(CsError::from_c(res));
        }
    };

    let quorum_type = match quorum_type {
        0 => QuorumType::Free,
        1 => QuorumType::Set,
        _ => QuorumType::Set,
    };
    let rhandle = Handle {
        quorum_handle: handle,
        model_data: *model_data,
    };
    HANDLE_HASH.lock().unwrap().insert(handle, rhandle);
    Ok((rhandle, quorum_type))
}

/// Finish with a connection to corosync
pub fn finalize(handle: Handle) -> Result<()> {
    let res = unsafe { ffi::quorum_finalize(handle.quorum_handle) };
    if res == ffi::CS_OK {
        HANDLE_HASH.lock().unwrap().remove(&handle.quorum_handle);
        Ok(())
    } else {
        Err(CsError::from_c(res))
    }
}

// Not sure if an FD is the right thing to return here, but it will do for now.
/// Return a file descriptor to use for poll/select on the QUORUM handle
pub fn fd_get(handle: Handle) -> Result<i32> {
    let c_fd: *mut c_int = &mut 0 as *mut _ as *mut c_int;
    let res = unsafe { ffi::quorum_fd_get(handle.quorum_handle, c_fd) };
    if res == ffi::CS_OK {
        Ok(c_fd as i32)
    } else {
        Err(CsError::from_c(res))
    }
}

/// Display any/all active QUORUM callbacks for this [Handle], see [DispatchFlags] for details
pub fn dispatch(handle: Handle, flags: DispatchFlags) -> Result<()> {
    let res = unsafe { ffi::quorum_dispatch(handle.quorum_handle, flags as u32) };
    if res == ffi::CS_OK {
        Ok(())
    } else {
        Err(CsError::from_c(res))
    }
}

/// Return the quorate status of the cluster
pub fn getquorate(handle: Handle) -> Result<bool> {
    let c_quorate: *mut c_int = &mut 0 as *mut _ as *mut c_int;
    let (res, r_quorate) = unsafe {
        let res = ffi::quorum_getquorate(handle.quorum_handle, c_quorate);
        let r_quorate: i32 = *c_quorate;
        (res, r_quorate)
    };
    if res == ffi::CS_OK {
        match r_quorate {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(CsError::CsErrLibrary),
        }
    } else {
        Err(CsError::from_c(res))
    }
}

/// Track node and quorum changes
pub fn trackstart(handle: Handle, flags: TrackFlags) -> Result<()> {
    let res = unsafe { ffi::quorum_trackstart(handle.quorum_handle, flags as u32) };
    if res == ffi::CS_OK {
        Ok(())
    } else {
        Err(CsError::from_c(res))
    }
}

/// Stop tracking node and quorum changes
pub fn trackstop(handle: Handle) -> Result<()> {
    let res = unsafe { ffi::quorum_trackstop(handle.quorum_handle) };
    if res == ffi::CS_OK {
        Ok(())
    } else {
        Err(CsError::from_c(res))
    }
}

/// Get the current 'context' value for this handle.
/// The context value is an arbitrary value that is always passed
/// back to callbacks to help identify the source
pub fn context_get(handle: Handle) -> Result<u64> {
    let (res, context) = unsafe {
        let mut context: u64 = 0;
        let c_context: *mut c_void = &mut context as *mut _ as *mut c_void;
        let r = ffi::quorum_context_get(handle.quorum_handle, c_context as *mut *const c_void);
        (r, context)
    };
    if res == ffi::CS_OK {
        Ok(context)
    } else {
        Err(CsError::from_c(res))
    }
}

/// Set the current 'context' value for this handle.
/// The context value is an arbitrary value that is always passed
/// back to callbacks to help identify the source.
/// Normally this is set in [initialize], but this allows it to be changed
pub fn context_set(handle: Handle, context: u64) -> Result<()> {
    let res = unsafe {
        let c_context = context as *mut c_void;
        ffi::quorum_context_set(handle.quorum_handle, c_context)
    };
    if res == ffi::CS_OK {
        Ok(())
    } else {
        Err(CsError::from_c(res))
    }
}