1nbdkit-ocaml-plugin(3) NBDKIT nbdkit-ocaml-plugin(3)
2
3
4
6 nbdkit-ocaml-plugin - writing nbdkit plugins in OCaml
7
9 nbdkit /path/to/plugin.so [arguments...]
10
11 nbdkit plugin [arguments...]
12
14 This manual page describes how to write nbdkit plugins in natively
15 compiled OCaml code.
16
17 Note this requires OCaml ≥ 4.02.2, which has support for shared
18 libraries. See http://caml.inria.fr/mantis/view.php?id=6693
19
21 For an example plugin written in OCaml, see:
22 https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/ocaml/example.ml
23
24 Broadly speaking, OCaml nbdkit plugins work like C ones, so you should
25 read nbdkit-plugin(3) first.
26
27 You should also look at NBDKit(3) which describes the plugin interface
28 for OCaml plugins.
29
30 Your OCaml code should call "NBDKit.register_plugin" like this:
31
32 let plugin = {
33 NBDKit.default_callbacks with
34 NBDKit.name = "myplugin";
35 version = "1.0";
36 open_connection = Some myplugin_open;
37 get_size = Some myplugin_get_size;
38 pread = Some myplugin_pread;
39 thread_model =
40 Some (fun () -> NBDKit.THREAD_MODEL_SERIALIZE_ALL_REQUESTS);
41 (* etc *)
42 }
43
44 let () = NBDKit.register_plugin plugin
45
46 Your plugin must call "register_plugin" exactly once when the plugin is
47 loaded.
48
49 Compiling an OCaml nbdkit plugin
50 OCaml nbdkit plugins are natively compiled into shared object ("*.so")
51 files which nbdkit loads like regular C plugins.
52
53 After writing your OCaml plugin ("myplugin.ml"), compile and link it
54 using this command:
55
56 ocamlopt.opt -output-obj -runtime-variant _pic \
57 -o nbdkit-myplugin-plugin.so \
58 NBDKit.cmx myplugin.ml \
59 -cclib -lnbdkitocaml
60
61 You can then use "nbdkit-myplugin-plugin.so" as an nbdkit plugin (see
62 nbdkit(1), nbdkit-plugin(3)):
63
64 nbdkit ./nbdkit-myplugin-plugin.so [args ...]
65
66 or if the ".so" file is installed in the $plugindir directory:
67
68 nbdkit myplugin [args ...]
69
70 Handle
71 Your "open_connection" callback can return an OCaml value of any type.
72 The same value is passed back to the per-connection callbacks like
73 "get_size" and "pread".
74
75 Typically (although this is not a requirement) you define your own
76 handle struct in your plugin:
77
78 type handle = {
79 (* any per-connection data you want to store goes here *)
80 h_id : int; (* this is just an example field *)
81 h_readonly : bool;
82 }
83
84 let id = ref 0
85 let myplugin_open readonly =
86 (* return a newly allocated handle *)
87 incr id;
88 { h_id = !id; h_readonly = readonly }
89
90 let myplugin_get_size handle =
91 printf "handle ID = %d\n" handle.h_id;
92 (* ... *)
93
94 If you don't need to store per-connection data, "open_connection" can
95 return "()".
96
97 Errors
98 Plugins can return errors from methods by raising an exception.
99
100 If you need to control which errno is sent back to the client you have
101 to call "NBDKit.set_error" before raising the exception.
102
103 Note if you call some function in the OCaml "Unix" module or another
104 library which fails, then the errno of the failing system call will not
105 be returned to the client. You have to catch the exception and call
106 "NBDKit.set_error" before re-raising the exception if you need to
107 control this.
108
109 Threads
110 One of the members in the plugin record passed to
111 "NBDKit.register_plugin" is "thread model", which must return one of
112 the values in the table below. For more information on thread models,
113 see "THREADS" in nbdkit-plugin(3). If this optional function is not
114 provided, the thread model defaults to THREAD_MODEL_PARALLEL. Note
115 that because of the garbage collector lock in OCaml, callbacks are
116 never truly concurrent.
117
118 "NBDKit.THREAD_MODEL_SERIALIZE_CONNECTIONS"
119 "NBDKit.THREAD_MODEL_SERIALIZE_ALL_REQUESTS"
120 "NBDKit.THREAD_MODEL_SERIALIZE_REQUESTS"
121 "NBDKit.THREAD_MODEL_PARALLEL"
122
123 Debugging
124 You can add debugging messages which are printed only when nbdkit is in
125 verbose mode by calling:
126
127 NBDKit.debug fs [...]
128
129 This function works like "Printf.printf".
130
131 OCaml scripts
132 Using nbdkit-cc-plugin(1) it is possible to write OCaml plugins which
133 are compiled just before use, and so appear to work more like scripts.
134
136 OCaml plugins first appeared in nbdkit 1.2.
137
139 NBDKit(3), nbdkit(1), nbdkit-plugin(3), ocamlopt(1).
140
142 Richard W.M. Jones
143
145 Copyright (C) 2014 Red Hat Inc.
146
148 Redistribution and use in source and binary forms, with or without
149 modification, are permitted provided that the following conditions are
150 met:
151
152 • Redistributions of source code must retain the above copyright
153 notice, this list of conditions and the following disclaimer.
154
155 • Redistributions in binary form must reproduce the above copyright
156 notice, this list of conditions and the following disclaimer in the
157 documentation and/or other materials provided with the
158 distribution.
159
160 • Neither the name of Red Hat nor the names of its contributors may
161 be used to endorse or promote products derived from this software
162 without specific prior written permission.
163
164 THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
165 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
167 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
168 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
169 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
170 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
171 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
172 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
173 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
174 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175
176
177
178nbdkit-1.28.2 2021-11-09 nbdkit-ocaml-plugin(3)