forked from libvirt/libvirt-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.cs
166 lines (148 loc) · 6.54 KB
/
Node.cs
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
/*
* Copyright (C)
* Arnaud Champion <[email protected]>
* Jaromír Červenka <[email protected]>
*
* See COPYING.LIB for the License of this software
*/
using System;
using System.Runtime.InteropServices;
namespace Libvirt
{
/// <summary>
/// The Node class expose all libvirt node related functions
/// </summary>
public class Node
{
private const int MaxStringLength = 1024;
// TODO virNodeDeviceCreateXML
// TODO virNodeDeviceDestroy
// TODO virNodeDeviceDettach
// TODO virNodeDeviceFree
// TODO virNodeDeviceGetName
// TODO virNodeDeviceGetParent
/// <summary>
/// Fetch an XML document describing all aspects of the device.
/// </summary>
/// <param name="dev">pointer to the node device</param>
/// <param name="flags">flags for XML generation (unused, pass 0)</param>
/// <returns>the XML document, or NULL on error</returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeDeviceGetXMLDesc")]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StringWithoutNativeCleanUpMarshaler))]
public static extern string DeviceGetXMLDesc(IntPtr dev, uint flags);
// TODO virNodeDeviceListCaps
/// <summary>
/// Lookup a node device by its name.
/// </summary>
/// <param name="conn">pointer to the hypervisor connection</param>
/// <param name="name">unique device name</param>
/// <returns>a virNodeDevicePtr if found, NULL otherwise.</returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeDeviceLookupByName")]
public static extern IntPtr DeviceLookupByName(IntPtr conn, string name);
// TODO virNodeDeviceNumOfCaps
// TODO virNodeDeviceReAttach
// TODO virNodeDeviceRef
// TODO virNodeDeviceReset
// TODO virNodeGetCellsFreeMemory
/// <summary>
/// Provides the free memory available on the Node Note: most libvirt APIs provide memory sizes in kilobytes,
/// but in this function the returned value is in bytes. Divide by 1024 as necessary.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to the hypervisor connection.
/// </param>
/// <returns>
/// The available free memory in bytes or 0 in case of error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeGetFreeMemory")]
public static extern ulong GetFreeMemory(IntPtr conn);
/// <summary>
/// Extract hardware information about the node.
/// </summary>
/// <param name="h">
/// A <see cref="IntPtr"/>pointer to the hypervisor connection.
/// </param>
/// <param name="info">
/// Pointer to a virNodeInfo structure allocated by the user.
/// </param>
/// <returns>
/// 0 in case of success and -1 in case of failure.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeGetInfo")]
public static extern int GetInfo(IntPtr h, [Out] NodeInfo info);
// TODO virNodeGetSecurityModel
/// <summary>
/// Collect the list of node devices, and store their names in @names.
/// If the optional 'cap' argument is non-NULL, then the count will be restricted to devices with the specified capability.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to the hypervisor connection.
/// </param>
/// <param name="cap">
/// Capability name.
/// </param>
/// <param name="names">
/// Array to collect the list of node device names.
/// </param>
/// <param name="maxnames">
/// Size of @names.
/// </param>
/// <param name="flags">
/// Flags (unused, pass 0).
/// </param>
/// <returns>
/// The number of node devices found or -1 in case of error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeListDevices")]
private static extern int ListDevices(IntPtr conn, string cap, IntPtr names, int maxnames, uint flags);
/// <summary>
/// Collect the list of node devices, and store their names in @names.
/// If the optional 'cap' argument is non-NULL, then the count will be restricted to devices with the specified capability.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to the hypervisor connection.
/// </param>
/// <param name="cap">
/// Capability name.
/// </param>
/// <param name="names">
/// Array to collect the list of node device names.
/// </param>
/// <param name="maxnames">
/// Size of @names.
/// </param>
/// <param name="flags">
/// Flags (unused, pass 0).
/// </param>
/// <returns>
/// The number of node devices found or -1 in case of error.
/// </returns>
public static int ListDevices(IntPtr conn, string cap, ref string[] names, int maxnames, uint flags)
{
IntPtr namesPtr = Marshal.AllocHGlobal(MaxStringLength);
int count = ListDevices(conn, cap, namesPtr, maxnames, 0);
if (count > 0)
names = MarshalHelper.ptrToStringArray(namesPtr, count);
Marshal.FreeHGlobal(namesPtr);
return count;
}
/// <summary>
/// Provides the number of node devices. If the optional 'cap' argument is non-NULL,
/// then the count will be restricted to devices with the specified capability.
/// </summary>
/// <param name="conn">
/// A <see cref="IntPtr"/>pointer to the hypervisor connection.
/// </param>
/// <param name="cap">
/// A <see cref="System.String"/>capability name.
/// </param>
/// <param name="flags">
/// A <see cref="System.UInt32"/>flags (unused, pass 0).
/// </param>
/// <returns>
/// The number of node devices or -1 in case of error.
/// </returns>
[DllImport("libvirt-0.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "virNodeNumOfDevices")]
public static extern int NumOfDevices(IntPtr conn, string cap, uint flags);
}
}