-
Notifications
You must be signed in to change notification settings - Fork 66
/
hostDevCard.jsx
323 lines (286 loc) · 12.5 KB
/
hostDevCard.jsx
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* This file is part of Cockpit.
*
* Copyright (C) 2021 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Button } from "@patternfly/react-core/dist/esm/components/Button";
import { DescriptionList, DescriptionListDescription, DescriptionListGroup, DescriptionListTerm } from "@patternfly/react-core/dist/esm/components/DescriptionList";
import { useDialogs } from 'dialogs.jsx';
import cockpit from 'cockpit';
import { vmId, findMatchingNodeDevices, getHostDevSourceObject } from "../../../helpers.js";
import { ListingTable } from "cockpit-components-table.jsx";
import AddHostDev from "./hostDevAdd.jsx";
import { domainGet, domainDetachHostDevice } from '../../../libvirtApi/domain.js';
import { nodeDeviceGetAll } from '../../../libvirtApi/nodeDevice.js';
import { DeleteResourceButton } from '../../common/deleteResource.jsx';
const _ = cockpit.gettext;
function getClass(hostDev, nodeDevices) {
const nodeDev = findMatchingNodeDevices(hostDev, nodeDevices)[0];
if (nodeDev && (["usb_device", "pci"].includes(nodeDev.capability.type)))
return nodeDev.class;
}
function getProduct(hostDev, nodeDevices) {
const nodeDev = findMatchingNodeDevices(hostDev, nodeDevices)[0];
if (["usb", "pci"].includes(hostDev.type)) {
if (nodeDev)
return nodeDev.capability.product._value;
else if (hostDev.type === "usb")
return hostDev.source.product.id;
}
}
function getPciSlot(hostDev) {
let domain = hostDev.source.address.domain.split('x')[1];
let bus = hostDev.source.address.bus.split('x')[1];
let slot = hostDev.source.address.slot.split('x')[1];
let func = hostDev.source.address.func.split('x')[1];
domain = String(domain).padStart(4, '0');
bus = String(bus).padStart(2, '0');
slot = String(slot).padStart(2, '0');
func = String(func).padStart(1, '0');
return `${domain}:${bus}:${slot}.${func}`;
}
function getVendor(hostDev, nodeDevices) {
const nodeDev = findMatchingNodeDevices(hostDev, nodeDevices)[0];
if (["usb", "pci"].includes(hostDev.type)) {
if (nodeDev)
return nodeDev.capability.vendor._value;
else if (hostDev.type === "usb")
return hostDev.source.vendor.id;
}
}
function getSource(hostDev, nodeDevices, hostdevId) {
const cells = [];
if (hostDev.type === "usb") {
const nodeDevs = findMatchingNodeDevices(hostDev, nodeDevices);
let device;
let bus;
if (nodeDevs.length === 1) {
device = nodeDevs[0].devnum;
bus = nodeDevs[0].busnum;
} else {
// If there are multiple usb devices without specified bus/device and same vendor/product,
// it's impossible to identify which one is the one referred in VM's XML
device = _("Unspecified");
bus = _("Unspecified");
}
// If there are 2 usb devices without specified bus/device and same vendor/product,
// it's impossible to identify which one is the one referred in VM's XML
cells.push(getOptionalValue(device, `device-${hostdevId}`, _("Device")));
cells.push(getOptionalValue(bus, `bus-${hostdevId}`, _("Bus")));
} else if (hostDev.type === "pci") {
cells.push(getOptionalValue(getPciSlot(hostDev), `slot-${hostdevId}`, _("Slot")));
} else if (hostDev.type === "scsi") {
const bus = hostDev.source.address.bus;
const target = hostDev.source.address.target;
const unit = hostDev.source.address.lun;
cells.push(getOptionalValue(bus, `bus-${hostdevId}`, _("Bus")));
cells.push(getOptionalValue(unit, `unit-${hostdevId}`, _("Slot")));
cells.push(getOptionalValue(target, `target-${hostdevId}`, _("Target")));
} else if (hostDev.type === "scsi_host") {
const protocol = hostDev.source.protocol;
const wwpn = hostDev.source.wwpn;
cells.push(getOptionalValue(protocol, `protocol-${hostdevId}`, _("Protocol")));
cells.push(getOptionalValue(wwpn, `wwpn-${hostdevId}`, _("WWPN")));
} else if (hostDev.type === "mdev") {
const uuid = hostDev.source.address.uuid;
cells.push(getOptionalValue(uuid, `uuid-${hostdevId}`, _("UUID")));
} else if (hostDev.type === "storage") {
const block = hostDev.source.block;
cells.push(getOptionalValue(block, `block-${hostdevId}`, _("Path")));
} else if (hostDev.type === "misc") {
const ch = hostDev.source.char;
cells.push(getOptionalValue(ch, `char-${hostdevId}`, _("Path")));
} else if (hostDev.type === "net") {
const iface = hostDev.source.interface;
cells.push(getOptionalValue(iface, `interface-${hostdevId}`, _("Interface")));
}
return <DescriptionList isHorizontal>{cells}</DescriptionList>;
}
export const VmHostDevActions = ({ vm }) => {
const Dialogs = useDialogs();
const idPrefix = `${vmId(vm.name)}-hostdevs`;
function open() {
Dialogs.show(<AddHostDev idPrefix={idPrefix} vm={vm} />);
}
return (
<Button id={`${idPrefix}-add`} variant='secondary' onClick={open}>
{_("Add host device")}
</Button>
);
};
/* Adds an optional description-value pair to an array which represents multiple values of a table cell
*
* @param value a value of the descr-value pair
* @param descr a description of the descr-value pair
* @param id
*/
export function getOptionalValue(value, id, descr) {
return (
<DescriptionListGroup key={descr}>
<DescriptionListTerm>
{descr}
</DescriptionListTerm>
<DescriptionListDescription id={id}>
{value}
</DescriptionListDescription>
</DescriptionListGroup>
);
}
export const VmHostDevCard = ({ vm, nodeDevices }) => {
const id = vmId(vm.name);
// Hostdev data mapping to rows
const detailMap = [
{
name: _("Type"),
value: (hostdev, hostdevId) => {
return (
<span id={`${id}-hostdev-${hostdevId}-type`}>
{hostdev.type}
</span>
);
}
},
{
name: _("Class"),
value: (hostdev, hostdevId) => {
return (
<span id={`${id}-hostdev-${hostdevId}-class`}>
{getClass(hostdev, nodeDevices)}
</span>
);
}
},
{
name: _("Model"),
value: (hostdev, hostdevId) => {
return (
<div id={`${id}-hostdev-${hostdevId}-product`}>
{getProduct(hostdev, nodeDevices)}
</div>
);
}
},
{
name: _("Vendor"),
value: (hostdev, hostdevId) => {
return (
<div id={`${id}-hostdev-${hostdevId}-vendor`}>
{getVendor(hostdev, nodeDevices)}
</div>
);
}
},
{
name: _("Source"),
value: (hostdev, hostdevId) => {
return (
<div id={`${id}-hostdev-${hostdevId}-source`}>
{getSource(hostdev, nodeDevices, hostdevId)}
</div>
);
}
},
{
name: "",
value: (hostdev, hostdevId) => {
const source = getHostDevSourceObject(hostdev);
let objectDescription = [];
if (hostdev.type === "pci") {
objectDescription = [
{ name: _("Vendor"), value: getVendor(hostdev, nodeDevices) },
{ name: _("Product"), value: getProduct(hostdev, nodeDevices) },
{ name: _("Slot"), value: getPciSlot(hostdev) },
];
} else if (hostdev.type == "usb") {
objectDescription = [
{ name: _("Vendor"), value: getVendor(hostdev, nodeDevices) },
{ name: _("Product"), value: getProduct(hostdev, nodeDevices) },
];
if (source) {
objectDescription.push({ name: _("Device"), value: source.device });
objectDescription.push({ name: _("Bus"), value: source.bus });
}
}
const deleteNICAction = (
<DeleteResourceButton objectId={`${id}-hostdev-${hostdevId}`}
actionName={_("Remove")}
dialogProps={{
title: _("Remove host device from VM?"),
errorMessage: ("Host device could not be removed"),
actionDescription: cockpit.format(_("Host device will be removed from $0:"), vm.name),
objectDescription,
actionName: _("Remove"),
deleteHandler: () => {
// refresh nodeDevice since usb number may be changed
return domainDetachHostDevice({ connectionName: vm.connectionName, vmId: vm.id, live: vm.state !== 'shut off', dev: hostdev })
.then(() => nodeDeviceGetAll({ connectionName: vm.connectionName }))
.then(() => domainGet({ connectionName: vm.connectionName, id: vm.id }));
}
}}
isSecondary />
);
return ["usb", "pci"].includes(hostdev.type)
? <div className='machines-listing-actions'>
{deleteNICAction}
</div>
: null;
}
},
];
let hostdevId = 1;
const sortHostDevices = (a, b) => {
if (a.type !== b.type)
return a.type > b.type ? 1 : -1;
const aSource = getHostDevSourceObject(a);
const bSource = getHostDevSourceObject(b);
if (aSource && bSource) {
if (a.type === "pci") {
const aSlot = `${aSource.domain}:${aSource.bus}:${aSource.slot}.${aSource.func}`;
const bSlot = `${bSource.domain}:${bSource.bus}:${bSource.slot}.${bSource.func}`;
if (aSlot !== bSlot)
return aSlot > bSlot ? 1 : -1;
} else if (a.type === "usb") {
const aVendorAndProduct = `${aSource.vendor}-${aSource.product}`;
const bVendorAndProduct = `${bSource.vendor}-${bSource.product}`;
if (aVendorAndProduct !== bVendorAndProduct)
return aVendorAndProduct > bVendorAndProduct ? 1 : -1;
}
}
return 0;
};
const columnTitles = detailMap.map(target => target.name);
const rows = vm.hostDevices.sort(sortHostDevices).map(target => {
const columns = detailMap.map(d => {
return { title: d.value(target, hostdevId, vm.connectionName) };
});
hostdevId++;
return { columns, props: { key: hostdevId } };
});
return (
<ListingTable aria-label={cockpit.format(_("VM $0 Host Devices"), vm.name)}
gridBreakPoint='grid-lg'
variant='compact'
emptyCaption={_("No host devices assigned to this VM")}
rows={rows}
columns={columnTitles} />
);
};
VmHostDevCard.propTypes = {
vm: PropTypes.object.isRequired,
nodeDevices: PropTypes.array.isRequired,
};